control.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Routines for driver control interface
  4. * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
  5. */
  6. #include <linux/threads.h>
  7. #include <linux/interrupt.h>
  8. #include <linux/module.h>
  9. #include <linux/slab.h>
  10. #include <linux/vmalloc.h>
  11. #include <linux/time.h>
  12. #include <linux/mm.h>
  13. #include <linux/math64.h>
  14. #include <linux/sched/signal.h>
  15. #include <sound/core.h>
  16. #include <sound/minors.h>
  17. #include <sound/info.h>
  18. #include <sound/control.h>
  19. /* max number of user-defined controls */
  20. #define MAX_USER_CONTROLS 32
  21. #define MAX_CONTROL_COUNT 1028
  22. struct snd_kctl_ioctl {
  23. struct list_head list; /* list of all ioctls */
  24. snd_kctl_ioctl_func_t fioctl;
  25. };
  26. static DECLARE_RWSEM(snd_ioctl_rwsem);
  27. static LIST_HEAD(snd_control_ioctls);
  28. #ifdef CONFIG_COMPAT
  29. static LIST_HEAD(snd_control_compat_ioctls);
  30. #endif
  31. static int snd_ctl_open(struct inode *inode, struct file *file)
  32. {
  33. unsigned long flags;
  34. struct snd_card *card;
  35. struct snd_ctl_file *ctl;
  36. int i, err;
  37. err = stream_open(inode, file);
  38. if (err < 0)
  39. return err;
  40. card = snd_lookup_minor_data(iminor(inode), SNDRV_DEVICE_TYPE_CONTROL);
  41. if (!card) {
  42. err = -ENODEV;
  43. goto __error1;
  44. }
  45. err = snd_card_file_add(card, file);
  46. if (err < 0) {
  47. err = -ENODEV;
  48. goto __error1;
  49. }
  50. if (!try_module_get(card->module)) {
  51. err = -EFAULT;
  52. goto __error2;
  53. }
  54. ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
  55. if (ctl == NULL) {
  56. err = -ENOMEM;
  57. goto __error;
  58. }
  59. INIT_LIST_HEAD(&ctl->events);
  60. init_waitqueue_head(&ctl->change_sleep);
  61. spin_lock_init(&ctl->read_lock);
  62. ctl->card = card;
  63. for (i = 0; i < SND_CTL_SUBDEV_ITEMS; i++)
  64. ctl->preferred_subdevice[i] = -1;
  65. ctl->pid = get_pid(task_pid(current));
  66. file->private_data = ctl;
  67. write_lock_irqsave(&card->ctl_files_rwlock, flags);
  68. list_add_tail(&ctl->list, &card->ctl_files);
  69. write_unlock_irqrestore(&card->ctl_files_rwlock, flags);
  70. snd_card_unref(card);
  71. return 0;
  72. __error:
  73. module_put(card->module);
  74. __error2:
  75. snd_card_file_remove(card, file);
  76. __error1:
  77. if (card)
  78. snd_card_unref(card);
  79. return err;
  80. }
  81. static void snd_ctl_empty_read_queue(struct snd_ctl_file * ctl)
  82. {
  83. unsigned long flags;
  84. struct snd_kctl_event *cread;
  85. spin_lock_irqsave(&ctl->read_lock, flags);
  86. while (!list_empty(&ctl->events)) {
  87. cread = snd_kctl_event(ctl->events.next);
  88. list_del(&cread->list);
  89. kfree(cread);
  90. }
  91. spin_unlock_irqrestore(&ctl->read_lock, flags);
  92. }
  93. static int snd_ctl_release(struct inode *inode, struct file *file)
  94. {
  95. unsigned long flags;
  96. struct snd_card *card;
  97. struct snd_ctl_file *ctl;
  98. struct snd_kcontrol *control;
  99. unsigned int idx;
  100. ctl = file->private_data;
  101. file->private_data = NULL;
  102. card = ctl->card;
  103. write_lock_irqsave(&card->ctl_files_rwlock, flags);
  104. list_del(&ctl->list);
  105. write_unlock_irqrestore(&card->ctl_files_rwlock, flags);
  106. down_write(&card->controls_rwsem);
  107. list_for_each_entry(control, &card->controls, list)
  108. for (idx = 0; idx < control->count; idx++)
  109. if (control->vd[idx].owner == ctl)
  110. control->vd[idx].owner = NULL;
  111. up_write(&card->controls_rwsem);
  112. snd_ctl_empty_read_queue(ctl);
  113. put_pid(ctl->pid);
  114. kfree(ctl);
  115. module_put(card->module);
  116. snd_card_file_remove(card, file);
  117. return 0;
  118. }
  119. /**
  120. * snd_ctl_notify - Send notification to user-space for a control change
  121. * @card: the card to send notification
  122. * @mask: the event mask, SNDRV_CTL_EVENT_*
  123. * @id: the ctl element id to send notification
  124. *
  125. * This function adds an event record with the given id and mask, appends
  126. * to the list and wakes up the user-space for notification. This can be
  127. * called in the atomic context.
  128. */
  129. void snd_ctl_notify(struct snd_card *card, unsigned int mask,
  130. struct snd_ctl_elem_id *id)
  131. {
  132. unsigned long flags;
  133. struct snd_ctl_file *ctl;
  134. struct snd_kctl_event *ev;
  135. if (snd_BUG_ON(!card || !id))
  136. return;
  137. if (card->shutdown)
  138. return;
  139. read_lock_irqsave(&card->ctl_files_rwlock, flags);
  140. #if IS_ENABLED(CONFIG_SND_MIXER_OSS)
  141. card->mixer_oss_change_count++;
  142. #endif
  143. list_for_each_entry(ctl, &card->ctl_files, list) {
  144. if (!ctl->subscribed)
  145. continue;
  146. spin_lock(&ctl->read_lock);
  147. list_for_each_entry(ev, &ctl->events, list) {
  148. if (ev->id.numid == id->numid) {
  149. ev->mask |= mask;
  150. goto _found;
  151. }
  152. }
  153. ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
  154. if (ev) {
  155. ev->id = *id;
  156. ev->mask = mask;
  157. list_add_tail(&ev->list, &ctl->events);
  158. } else {
  159. dev_err(card->dev, "No memory available to allocate event\n");
  160. }
  161. _found:
  162. wake_up(&ctl->change_sleep);
  163. spin_unlock(&ctl->read_lock);
  164. kill_fasync(&ctl->fasync, SIGIO, POLL_IN);
  165. }
  166. read_unlock_irqrestore(&card->ctl_files_rwlock, flags);
  167. }
  168. EXPORT_SYMBOL(snd_ctl_notify);
  169. /**
  170. * snd_ctl_new - create a new control instance with some elements
  171. * @kctl: the pointer to store new control instance
  172. * @count: the number of elements in this control
  173. * @access: the default access flags for elements in this control
  174. * @file: given when locking these elements
  175. *
  176. * Allocates a memory object for a new control instance. The instance has
  177. * elements as many as the given number (@count). Each element has given
  178. * access permissions (@access). Each element is locked when @file is given.
  179. *
  180. * Return: 0 on success, error code on failure
  181. */
  182. static int snd_ctl_new(struct snd_kcontrol **kctl, unsigned int count,
  183. unsigned int access, struct snd_ctl_file *file)
  184. {
  185. unsigned int idx;
  186. if (count == 0 || count > MAX_CONTROL_COUNT)
  187. return -EINVAL;
  188. *kctl = kzalloc(struct_size(*kctl, vd, count), GFP_KERNEL);
  189. if (!*kctl)
  190. return -ENOMEM;
  191. for (idx = 0; idx < count; idx++) {
  192. (*kctl)->vd[idx].access = access;
  193. (*kctl)->vd[idx].owner = file;
  194. }
  195. (*kctl)->count = count;
  196. return 0;
  197. }
  198. /**
  199. * snd_ctl_new1 - create a control instance from the template
  200. * @ncontrol: the initialization record
  201. * @private_data: the private data to set
  202. *
  203. * Allocates a new struct snd_kcontrol instance and initialize from the given
  204. * template. When the access field of ncontrol is 0, it's assumed as
  205. * READWRITE access. When the count field is 0, it's assumes as one.
  206. *
  207. * Return: The pointer of the newly generated instance, or %NULL on failure.
  208. */
  209. struct snd_kcontrol *snd_ctl_new1(const struct snd_kcontrol_new *ncontrol,
  210. void *private_data)
  211. {
  212. struct snd_kcontrol *kctl;
  213. unsigned int count;
  214. unsigned int access;
  215. int err;
  216. if (snd_BUG_ON(!ncontrol || !ncontrol->info))
  217. return NULL;
  218. count = ncontrol->count;
  219. if (count == 0)
  220. count = 1;
  221. access = ncontrol->access;
  222. if (access == 0)
  223. access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
  224. access &= (SNDRV_CTL_ELEM_ACCESS_READWRITE |
  225. SNDRV_CTL_ELEM_ACCESS_VOLATILE |
  226. SNDRV_CTL_ELEM_ACCESS_INACTIVE |
  227. SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE |
  228. SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND |
  229. SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK |
  230. SNDRV_CTL_ELEM_ACCESS_SKIP_CHECK);
  231. err = snd_ctl_new(&kctl, count, access, NULL);
  232. if (err < 0)
  233. return NULL;
  234. /* The 'numid' member is decided when calling snd_ctl_add(). */
  235. kctl->id.iface = ncontrol->iface;
  236. kctl->id.device = ncontrol->device;
  237. kctl->id.subdevice = ncontrol->subdevice;
  238. if (ncontrol->name) {
  239. strlcpy(kctl->id.name, ncontrol->name, sizeof(kctl->id.name));
  240. if (strcmp(ncontrol->name, kctl->id.name) != 0)
  241. pr_warn("ALSA: Control name '%s' truncated to '%s'\n",
  242. ncontrol->name, kctl->id.name);
  243. }
  244. kctl->id.index = ncontrol->index;
  245. kctl->info = ncontrol->info;
  246. kctl->get = ncontrol->get;
  247. kctl->put = ncontrol->put;
  248. kctl->tlv.p = ncontrol->tlv.p;
  249. kctl->private_value = ncontrol->private_value;
  250. kctl->private_data = private_data;
  251. return kctl;
  252. }
  253. EXPORT_SYMBOL(snd_ctl_new1);
  254. /**
  255. * snd_ctl_free_one - release the control instance
  256. * @kcontrol: the control instance
  257. *
  258. * Releases the control instance created via snd_ctl_new()
  259. * or snd_ctl_new1().
  260. * Don't call this after the control was added to the card.
  261. */
  262. void snd_ctl_free_one(struct snd_kcontrol *kcontrol)
  263. {
  264. if (kcontrol) {
  265. if (kcontrol->private_free)
  266. kcontrol->private_free(kcontrol);
  267. kfree(kcontrol);
  268. }
  269. }
  270. EXPORT_SYMBOL(snd_ctl_free_one);
  271. static bool snd_ctl_remove_numid_conflict(struct snd_card *card,
  272. unsigned int count)
  273. {
  274. struct snd_kcontrol *kctl;
  275. /* Make sure that the ids assigned to the control do not wrap around */
  276. if (card->last_numid >= UINT_MAX - count)
  277. card->last_numid = 0;
  278. list_for_each_entry(kctl, &card->controls, list) {
  279. if (kctl->id.numid < card->last_numid + 1 + count &&
  280. kctl->id.numid + kctl->count > card->last_numid + 1) {
  281. card->last_numid = kctl->id.numid + kctl->count - 1;
  282. return true;
  283. }
  284. }
  285. return false;
  286. }
  287. static int snd_ctl_find_hole(struct snd_card *card, unsigned int count)
  288. {
  289. unsigned int iter = 100000;
  290. while (snd_ctl_remove_numid_conflict(card, count)) {
  291. if (--iter == 0) {
  292. /* this situation is very unlikely */
  293. dev_err(card->dev, "unable to allocate new control numid\n");
  294. return -ENOMEM;
  295. }
  296. }
  297. return 0;
  298. }
  299. enum snd_ctl_add_mode {
  300. CTL_ADD_EXCLUSIVE, CTL_REPLACE, CTL_ADD_ON_REPLACE,
  301. };
  302. /* add/replace a new kcontrol object; call with card->controls_rwsem locked */
  303. static int __snd_ctl_add_replace(struct snd_card *card,
  304. struct snd_kcontrol *kcontrol,
  305. enum snd_ctl_add_mode mode)
  306. {
  307. struct snd_ctl_elem_id id;
  308. unsigned int idx;
  309. unsigned int count;
  310. struct snd_kcontrol *old;
  311. int err;
  312. id = kcontrol->id;
  313. if (id.index > UINT_MAX - kcontrol->count)
  314. return -EINVAL;
  315. old = snd_ctl_find_id(card, &id);
  316. if (!old) {
  317. if (mode == CTL_REPLACE)
  318. return -EINVAL;
  319. } else {
  320. if (mode == CTL_ADD_EXCLUSIVE) {
  321. dev_err(card->dev,
  322. "control %i:%i:%i:%s:%i is already present\n",
  323. id.iface, id.device, id.subdevice, id.name,
  324. id.index);
  325. return -EBUSY;
  326. }
  327. err = snd_ctl_remove(card, old);
  328. if (err < 0)
  329. return err;
  330. }
  331. if (snd_ctl_find_hole(card, kcontrol->count) < 0)
  332. return -ENOMEM;
  333. list_add_tail(&kcontrol->list, &card->controls);
  334. card->controls_count += kcontrol->count;
  335. kcontrol->id.numid = card->last_numid + 1;
  336. card->last_numid += kcontrol->count;
  337. id = kcontrol->id;
  338. count = kcontrol->count;
  339. for (idx = 0; idx < count; idx++, id.index++, id.numid++)
  340. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_ADD, &id);
  341. return 0;
  342. }
  343. static int snd_ctl_add_replace(struct snd_card *card,
  344. struct snd_kcontrol *kcontrol,
  345. enum snd_ctl_add_mode mode)
  346. {
  347. int err = -EINVAL;
  348. if (! kcontrol)
  349. return err;
  350. if (snd_BUG_ON(!card || !kcontrol->info))
  351. goto error;
  352. down_write(&card->controls_rwsem);
  353. err = __snd_ctl_add_replace(card, kcontrol, mode);
  354. up_write(&card->controls_rwsem);
  355. if (err < 0)
  356. goto error;
  357. return 0;
  358. error:
  359. snd_ctl_free_one(kcontrol);
  360. return err;
  361. }
  362. /**
  363. * snd_ctl_add - add the control instance to the card
  364. * @card: the card instance
  365. * @kcontrol: the control instance to add
  366. *
  367. * Adds the control instance created via snd_ctl_new() or
  368. * snd_ctl_new1() to the given card. Assigns also an unique
  369. * numid used for fast search.
  370. *
  371. * It frees automatically the control which cannot be added.
  372. *
  373. * Return: Zero if successful, or a negative error code on failure.
  374. *
  375. */
  376. int snd_ctl_add(struct snd_card *card, struct snd_kcontrol *kcontrol)
  377. {
  378. return snd_ctl_add_replace(card, kcontrol, CTL_ADD_EXCLUSIVE);
  379. }
  380. EXPORT_SYMBOL(snd_ctl_add);
  381. /**
  382. * snd_ctl_replace - replace the control instance of the card
  383. * @card: the card instance
  384. * @kcontrol: the control instance to replace
  385. * @add_on_replace: add the control if not already added
  386. *
  387. * Replaces the given control. If the given control does not exist
  388. * and the add_on_replace flag is set, the control is added. If the
  389. * control exists, it is destroyed first.
  390. *
  391. * It frees automatically the control which cannot be added or replaced.
  392. *
  393. * Return: Zero if successful, or a negative error code on failure.
  394. */
  395. int snd_ctl_replace(struct snd_card *card, struct snd_kcontrol *kcontrol,
  396. bool add_on_replace)
  397. {
  398. return snd_ctl_add_replace(card, kcontrol,
  399. add_on_replace ? CTL_ADD_ON_REPLACE : CTL_REPLACE);
  400. }
  401. EXPORT_SYMBOL(snd_ctl_replace);
  402. /**
  403. * snd_ctl_remove - remove the control from the card and release it
  404. * @card: the card instance
  405. * @kcontrol: the control instance to remove
  406. *
  407. * Removes the control from the card and then releases the instance.
  408. * You don't need to call snd_ctl_free_one(). You must be in
  409. * the write lock - down_write(&card->controls_rwsem).
  410. *
  411. * Return: 0 if successful, or a negative error code on failure.
  412. */
  413. int snd_ctl_remove(struct snd_card *card, struct snd_kcontrol *kcontrol)
  414. {
  415. struct snd_ctl_elem_id id;
  416. unsigned int idx;
  417. if (snd_BUG_ON(!card || !kcontrol))
  418. return -EINVAL;
  419. list_del(&kcontrol->list);
  420. card->controls_count -= kcontrol->count;
  421. id = kcontrol->id;
  422. for (idx = 0; idx < kcontrol->count; idx++, id.index++, id.numid++)
  423. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_REMOVE, &id);
  424. snd_ctl_free_one(kcontrol);
  425. return 0;
  426. }
  427. EXPORT_SYMBOL(snd_ctl_remove);
  428. /**
  429. * snd_ctl_remove_id - remove the control of the given id and release it
  430. * @card: the card instance
  431. * @id: the control id to remove
  432. *
  433. * Finds the control instance with the given id, removes it from the
  434. * card list and releases it.
  435. *
  436. * Return: 0 if successful, or a negative error code on failure.
  437. */
  438. int snd_ctl_remove_id(struct snd_card *card, struct snd_ctl_elem_id *id)
  439. {
  440. struct snd_kcontrol *kctl;
  441. int ret;
  442. down_write(&card->controls_rwsem);
  443. kctl = snd_ctl_find_id(card, id);
  444. if (kctl == NULL) {
  445. up_write(&card->controls_rwsem);
  446. return -ENOENT;
  447. }
  448. ret = snd_ctl_remove(card, kctl);
  449. up_write(&card->controls_rwsem);
  450. return ret;
  451. }
  452. EXPORT_SYMBOL(snd_ctl_remove_id);
  453. /**
  454. * snd_ctl_remove_user_ctl - remove and release the unlocked user control
  455. * @file: active control handle
  456. * @id: the control id to remove
  457. *
  458. * Finds the control instance with the given id, removes it from the
  459. * card list and releases it.
  460. *
  461. * Return: 0 if successful, or a negative error code on failure.
  462. */
  463. static int snd_ctl_remove_user_ctl(struct snd_ctl_file * file,
  464. struct snd_ctl_elem_id *id)
  465. {
  466. struct snd_card *card = file->card;
  467. struct snd_kcontrol *kctl;
  468. int idx, ret;
  469. down_write(&card->controls_rwsem);
  470. kctl = snd_ctl_find_id(card, id);
  471. if (kctl == NULL) {
  472. ret = -ENOENT;
  473. goto error;
  474. }
  475. if (!(kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_USER)) {
  476. ret = -EINVAL;
  477. goto error;
  478. }
  479. for (idx = 0; idx < kctl->count; idx++)
  480. if (kctl->vd[idx].owner != NULL && kctl->vd[idx].owner != file) {
  481. ret = -EBUSY;
  482. goto error;
  483. }
  484. ret = snd_ctl_remove(card, kctl);
  485. if (ret < 0)
  486. goto error;
  487. card->user_ctl_count--;
  488. error:
  489. up_write(&card->controls_rwsem);
  490. return ret;
  491. }
  492. /**
  493. * snd_ctl_activate_id - activate/inactivate the control of the given id
  494. * @card: the card instance
  495. * @id: the control id to activate/inactivate
  496. * @active: non-zero to activate
  497. *
  498. * Finds the control instance with the given id, and activate or
  499. * inactivate the control together with notification, if changed.
  500. * The given ID data is filled with full information.
  501. *
  502. * Return: 0 if unchanged, 1 if changed, or a negative error code on failure.
  503. */
  504. int snd_ctl_activate_id(struct snd_card *card, struct snd_ctl_elem_id *id,
  505. int active)
  506. {
  507. struct snd_kcontrol *kctl;
  508. struct snd_kcontrol_volatile *vd;
  509. unsigned int index_offset;
  510. int ret;
  511. down_write(&card->controls_rwsem);
  512. kctl = snd_ctl_find_id(card, id);
  513. if (kctl == NULL) {
  514. ret = -ENOENT;
  515. goto unlock;
  516. }
  517. index_offset = snd_ctl_get_ioff(kctl, id);
  518. vd = &kctl->vd[index_offset];
  519. ret = 0;
  520. if (active) {
  521. if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_INACTIVE))
  522. goto unlock;
  523. vd->access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  524. } else {
  525. if (vd->access & SNDRV_CTL_ELEM_ACCESS_INACTIVE)
  526. goto unlock;
  527. vd->access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  528. }
  529. snd_ctl_build_ioff(id, kctl, index_offset);
  530. ret = 1;
  531. unlock:
  532. up_write(&card->controls_rwsem);
  533. if (ret > 0)
  534. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO, id);
  535. return ret;
  536. }
  537. EXPORT_SYMBOL_GPL(snd_ctl_activate_id);
  538. /**
  539. * snd_ctl_rename_id - replace the id of a control on the card
  540. * @card: the card instance
  541. * @src_id: the old id
  542. * @dst_id: the new id
  543. *
  544. * Finds the control with the old id from the card, and replaces the
  545. * id with the new one.
  546. *
  547. * Return: Zero if successful, or a negative error code on failure.
  548. */
  549. int snd_ctl_rename_id(struct snd_card *card, struct snd_ctl_elem_id *src_id,
  550. struct snd_ctl_elem_id *dst_id)
  551. {
  552. struct snd_kcontrol *kctl;
  553. down_write(&card->controls_rwsem);
  554. kctl = snd_ctl_find_id(card, src_id);
  555. if (kctl == NULL) {
  556. up_write(&card->controls_rwsem);
  557. return -ENOENT;
  558. }
  559. kctl->id = *dst_id;
  560. kctl->id.numid = card->last_numid + 1;
  561. card->last_numid += kctl->count;
  562. up_write(&card->controls_rwsem);
  563. return 0;
  564. }
  565. EXPORT_SYMBOL(snd_ctl_rename_id);
  566. /**
  567. * snd_ctl_find_numid - find the control instance with the given number-id
  568. * @card: the card instance
  569. * @numid: the number-id to search
  570. *
  571. * Finds the control instance with the given number-id from the card.
  572. *
  573. * The caller must down card->controls_rwsem before calling this function
  574. * (if the race condition can happen).
  575. *
  576. * Return: The pointer of the instance if found, or %NULL if not.
  577. *
  578. */
  579. struct snd_kcontrol *snd_ctl_find_numid(struct snd_card *card, unsigned int numid)
  580. {
  581. struct snd_kcontrol *kctl;
  582. if (snd_BUG_ON(!card || !numid))
  583. return NULL;
  584. list_for_each_entry(kctl, &card->controls, list) {
  585. if (kctl->id.numid <= numid && kctl->id.numid + kctl->count > numid)
  586. return kctl;
  587. }
  588. return NULL;
  589. }
  590. EXPORT_SYMBOL(snd_ctl_find_numid);
  591. /**
  592. * snd_ctl_find_id - find the control instance with the given id
  593. * @card: the card instance
  594. * @id: the id to search
  595. *
  596. * Finds the control instance with the given id from the card.
  597. *
  598. * The caller must down card->controls_rwsem before calling this function
  599. * (if the race condition can happen).
  600. *
  601. * Return: The pointer of the instance if found, or %NULL if not.
  602. *
  603. */
  604. struct snd_kcontrol *snd_ctl_find_id(struct snd_card *card,
  605. struct snd_ctl_elem_id *id)
  606. {
  607. struct snd_kcontrol *kctl;
  608. if (snd_BUG_ON(!card || !id))
  609. return NULL;
  610. if (id->numid != 0)
  611. return snd_ctl_find_numid(card, id->numid);
  612. list_for_each_entry(kctl, &card->controls, list) {
  613. if (kctl->id.iface != id->iface)
  614. continue;
  615. if (kctl->id.device != id->device)
  616. continue;
  617. if (kctl->id.subdevice != id->subdevice)
  618. continue;
  619. if (strncmp(kctl->id.name, id->name, sizeof(kctl->id.name)))
  620. continue;
  621. if (kctl->id.index > id->index)
  622. continue;
  623. if (kctl->id.index + kctl->count <= id->index)
  624. continue;
  625. return kctl;
  626. }
  627. return NULL;
  628. }
  629. EXPORT_SYMBOL(snd_ctl_find_id);
  630. static int snd_ctl_card_info(struct snd_card *card, struct snd_ctl_file * ctl,
  631. unsigned int cmd, void __user *arg)
  632. {
  633. struct snd_ctl_card_info *info;
  634. info = kzalloc(sizeof(*info), GFP_KERNEL);
  635. if (! info)
  636. return -ENOMEM;
  637. down_read(&snd_ioctl_rwsem);
  638. info->card = card->number;
  639. strlcpy(info->id, card->id, sizeof(info->id));
  640. strlcpy(info->driver, card->driver, sizeof(info->driver));
  641. strlcpy(info->name, card->shortname, sizeof(info->name));
  642. strlcpy(info->longname, card->longname, sizeof(info->longname));
  643. strlcpy(info->mixername, card->mixername, sizeof(info->mixername));
  644. strlcpy(info->components, card->components, sizeof(info->components));
  645. up_read(&snd_ioctl_rwsem);
  646. if (copy_to_user(arg, info, sizeof(struct snd_ctl_card_info))) {
  647. kfree(info);
  648. return -EFAULT;
  649. }
  650. kfree(info);
  651. return 0;
  652. }
  653. static int snd_ctl_elem_list(struct snd_card *card,
  654. struct snd_ctl_elem_list *list)
  655. {
  656. struct snd_kcontrol *kctl;
  657. struct snd_ctl_elem_id id;
  658. unsigned int offset, space, jidx;
  659. int err = 0;
  660. offset = list->offset;
  661. space = list->space;
  662. down_read(&card->controls_rwsem);
  663. list->count = card->controls_count;
  664. list->used = 0;
  665. if (space > 0) {
  666. list_for_each_entry(kctl, &card->controls, list) {
  667. if (offset >= kctl->count) {
  668. offset -= kctl->count;
  669. continue;
  670. }
  671. for (jidx = offset; jidx < kctl->count; jidx++) {
  672. snd_ctl_build_ioff(&id, kctl, jidx);
  673. if (copy_to_user(list->pids + list->used, &id,
  674. sizeof(id))) {
  675. err = -EFAULT;
  676. goto out;
  677. }
  678. list->used++;
  679. if (!--space)
  680. goto out;
  681. }
  682. offset = 0;
  683. }
  684. }
  685. out:
  686. up_read(&card->controls_rwsem);
  687. return err;
  688. }
  689. static int snd_ctl_elem_list_user(struct snd_card *card,
  690. struct snd_ctl_elem_list __user *_list)
  691. {
  692. struct snd_ctl_elem_list list;
  693. int err;
  694. if (copy_from_user(&list, _list, sizeof(list)))
  695. return -EFAULT;
  696. err = snd_ctl_elem_list(card, &list);
  697. if (err)
  698. return err;
  699. if (copy_to_user(_list, &list, sizeof(list)))
  700. return -EFAULT;
  701. return 0;
  702. }
  703. /* Check whether the given kctl info is valid */
  704. static int snd_ctl_check_elem_info(struct snd_card *card,
  705. const struct snd_ctl_elem_info *info)
  706. {
  707. static const unsigned int max_value_counts[] = {
  708. [SNDRV_CTL_ELEM_TYPE_BOOLEAN] = 128,
  709. [SNDRV_CTL_ELEM_TYPE_INTEGER] = 128,
  710. [SNDRV_CTL_ELEM_TYPE_ENUMERATED] = 128,
  711. [SNDRV_CTL_ELEM_TYPE_BYTES] = 512,
  712. [SNDRV_CTL_ELEM_TYPE_IEC958] = 1,
  713. [SNDRV_CTL_ELEM_TYPE_INTEGER64] = 64,
  714. };
  715. if (info->type < SNDRV_CTL_ELEM_TYPE_BOOLEAN ||
  716. info->type > SNDRV_CTL_ELEM_TYPE_INTEGER64) {
  717. if (card)
  718. dev_err(card->dev,
  719. "control %i:%i:%i:%s:%i: invalid type %d\n",
  720. info->id.iface, info->id.device,
  721. info->id.subdevice, info->id.name,
  722. info->id.index, info->type);
  723. return -EINVAL;
  724. }
  725. if (info->type == SNDRV_CTL_ELEM_TYPE_ENUMERATED &&
  726. info->value.enumerated.items == 0) {
  727. if (card)
  728. dev_err(card->dev,
  729. "control %i:%i:%i:%s:%i: zero enum items\n",
  730. info->id.iface, info->id.device,
  731. info->id.subdevice, info->id.name,
  732. info->id.index);
  733. return -EINVAL;
  734. }
  735. if (info->count > max_value_counts[info->type]) {
  736. if (card)
  737. dev_err(card->dev,
  738. "control %i:%i:%i:%s:%i: invalid count %d\n",
  739. info->id.iface, info->id.device,
  740. info->id.subdevice, info->id.name,
  741. info->id.index, info->count);
  742. return -EINVAL;
  743. }
  744. return 0;
  745. }
  746. /* The capacity of struct snd_ctl_elem_value.value.*/
  747. static const unsigned int value_sizes[] = {
  748. [SNDRV_CTL_ELEM_TYPE_BOOLEAN] = sizeof(long),
  749. [SNDRV_CTL_ELEM_TYPE_INTEGER] = sizeof(long),
  750. [SNDRV_CTL_ELEM_TYPE_ENUMERATED] = sizeof(unsigned int),
  751. [SNDRV_CTL_ELEM_TYPE_BYTES] = sizeof(unsigned char),
  752. [SNDRV_CTL_ELEM_TYPE_IEC958] = sizeof(struct snd_aes_iec958),
  753. [SNDRV_CTL_ELEM_TYPE_INTEGER64] = sizeof(long long),
  754. };
  755. #ifdef CONFIG_SND_CTL_VALIDATION
  756. /* fill the remaining snd_ctl_elem_value data with the given pattern */
  757. static void fill_remaining_elem_value(struct snd_ctl_elem_value *control,
  758. struct snd_ctl_elem_info *info,
  759. u32 pattern)
  760. {
  761. size_t offset = value_sizes[info->type] * info->count;
  762. offset = (offset + sizeof(u32) - 1) / sizeof(u32);
  763. memset32((u32 *)control->value.bytes.data + offset, pattern,
  764. sizeof(control->value) / sizeof(u32) - offset);
  765. }
  766. /* check whether the given integer ctl value is valid */
  767. static int sanity_check_int_value(struct snd_card *card,
  768. const struct snd_ctl_elem_value *control,
  769. const struct snd_ctl_elem_info *info,
  770. int i)
  771. {
  772. long long lval, lmin, lmax, lstep;
  773. u64 rem;
  774. switch (info->type) {
  775. default:
  776. case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
  777. lval = control->value.integer.value[i];
  778. lmin = 0;
  779. lmax = 1;
  780. lstep = 0;
  781. break;
  782. case SNDRV_CTL_ELEM_TYPE_INTEGER:
  783. lval = control->value.integer.value[i];
  784. lmin = info->value.integer.min;
  785. lmax = info->value.integer.max;
  786. lstep = info->value.integer.step;
  787. break;
  788. case SNDRV_CTL_ELEM_TYPE_INTEGER64:
  789. lval = control->value.integer64.value[i];
  790. lmin = info->value.integer64.min;
  791. lmax = info->value.integer64.max;
  792. lstep = info->value.integer64.step;
  793. break;
  794. case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
  795. lval = control->value.enumerated.item[i];
  796. lmin = 0;
  797. lmax = info->value.enumerated.items - 1;
  798. lstep = 0;
  799. break;
  800. }
  801. if (lval < lmin || lval > lmax) {
  802. dev_err(card->dev,
  803. "control %i:%i:%i:%s:%i: value out of range %lld (%lld/%lld) at count %i\n",
  804. control->id.iface, control->id.device,
  805. control->id.subdevice, control->id.name,
  806. control->id.index, lval, lmin, lmax, i);
  807. return -EINVAL;
  808. }
  809. if (lstep) {
  810. div64_u64_rem(lval, lstep, &rem);
  811. if (rem) {
  812. dev_err(card->dev,
  813. "control %i:%i:%i:%s:%i: unaligned value %lld (step %lld) at count %i\n",
  814. control->id.iface, control->id.device,
  815. control->id.subdevice, control->id.name,
  816. control->id.index, lval, lstep, i);
  817. return -EINVAL;
  818. }
  819. }
  820. return 0;
  821. }
  822. /* perform sanity checks to the given snd_ctl_elem_value object */
  823. static int sanity_check_elem_value(struct snd_card *card,
  824. const struct snd_ctl_elem_value *control,
  825. const struct snd_ctl_elem_info *info,
  826. u32 pattern)
  827. {
  828. size_t offset;
  829. int i, ret = 0;
  830. u32 *p;
  831. switch (info->type) {
  832. case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
  833. case SNDRV_CTL_ELEM_TYPE_INTEGER:
  834. case SNDRV_CTL_ELEM_TYPE_INTEGER64:
  835. case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
  836. for (i = 0; i < info->count; i++) {
  837. ret = sanity_check_int_value(card, control, info, i);
  838. if (ret < 0)
  839. return ret;
  840. }
  841. break;
  842. default:
  843. break;
  844. }
  845. /* check whether the remaining area kept untouched */
  846. offset = value_sizes[info->type] * info->count;
  847. offset = (offset + sizeof(u32) - 1) / sizeof(u32);
  848. p = (u32 *)control->value.bytes.data + offset;
  849. for (; offset < sizeof(control->value) / sizeof(u32); offset++, p++) {
  850. if (*p != pattern) {
  851. ret = -EINVAL;
  852. break;
  853. }
  854. *p = 0; /* clear the checked area */
  855. }
  856. return ret;
  857. }
  858. #else
  859. static inline void fill_remaining_elem_value(struct snd_ctl_elem_value *control,
  860. struct snd_ctl_elem_info *info,
  861. u32 pattern)
  862. {
  863. }
  864. static inline int sanity_check_elem_value(struct snd_card *card,
  865. struct snd_ctl_elem_value *control,
  866. struct snd_ctl_elem_info *info,
  867. u32 pattern)
  868. {
  869. return 0;
  870. }
  871. #endif
  872. static int __snd_ctl_elem_info(struct snd_card *card,
  873. struct snd_kcontrol *kctl,
  874. struct snd_ctl_elem_info *info,
  875. struct snd_ctl_file *ctl)
  876. {
  877. struct snd_kcontrol_volatile *vd;
  878. unsigned int index_offset;
  879. int result;
  880. #ifdef CONFIG_SND_DEBUG
  881. info->access = 0;
  882. #endif
  883. result = kctl->info(kctl, info);
  884. if (result >= 0) {
  885. snd_BUG_ON(info->access);
  886. index_offset = snd_ctl_get_ioff(kctl, &info->id);
  887. vd = &kctl->vd[index_offset];
  888. snd_ctl_build_ioff(&info->id, kctl, index_offset);
  889. info->access = vd->access;
  890. if (vd->owner) {
  891. info->access |= SNDRV_CTL_ELEM_ACCESS_LOCK;
  892. if (vd->owner == ctl)
  893. info->access |= SNDRV_CTL_ELEM_ACCESS_OWNER;
  894. info->owner = pid_vnr(vd->owner->pid);
  895. } else {
  896. info->owner = -1;
  897. }
  898. if (!snd_ctl_skip_validation(info) &&
  899. snd_ctl_check_elem_info(card, info) < 0)
  900. result = -EINVAL;
  901. }
  902. return result;
  903. }
  904. static int snd_ctl_elem_info(struct snd_ctl_file *ctl,
  905. struct snd_ctl_elem_info *info)
  906. {
  907. struct snd_card *card = ctl->card;
  908. struct snd_kcontrol *kctl;
  909. int result;
  910. down_read(&card->controls_rwsem);
  911. kctl = snd_ctl_find_id(card, &info->id);
  912. if (kctl == NULL)
  913. result = -ENOENT;
  914. else
  915. result = __snd_ctl_elem_info(card, kctl, info, ctl);
  916. up_read(&card->controls_rwsem);
  917. return result;
  918. }
  919. static int snd_ctl_elem_info_user(struct snd_ctl_file *ctl,
  920. struct snd_ctl_elem_info __user *_info)
  921. {
  922. struct snd_ctl_elem_info info;
  923. int result;
  924. if (copy_from_user(&info, _info, sizeof(info)))
  925. return -EFAULT;
  926. result = snd_power_wait(ctl->card, SNDRV_CTL_POWER_D0);
  927. if (result < 0)
  928. return result;
  929. result = snd_ctl_elem_info(ctl, &info);
  930. if (result < 0)
  931. return result;
  932. /* drop internal access flags */
  933. info.access &= ~SNDRV_CTL_ELEM_ACCESS_SKIP_CHECK;
  934. if (copy_to_user(_info, &info, sizeof(info)))
  935. return -EFAULT;
  936. return result;
  937. }
  938. static int snd_ctl_elem_read(struct snd_card *card,
  939. struct snd_ctl_elem_value *control)
  940. {
  941. struct snd_kcontrol *kctl;
  942. struct snd_kcontrol_volatile *vd;
  943. unsigned int index_offset;
  944. struct snd_ctl_elem_info info;
  945. const u32 pattern = 0xdeadbeef;
  946. int ret;
  947. kctl = snd_ctl_find_id(card, &control->id);
  948. if (kctl == NULL)
  949. return -ENOENT;
  950. index_offset = snd_ctl_get_ioff(kctl, &control->id);
  951. vd = &kctl->vd[index_offset];
  952. if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_READ) || kctl->get == NULL)
  953. return -EPERM;
  954. snd_ctl_build_ioff(&control->id, kctl, index_offset);
  955. #ifdef CONFIG_SND_CTL_VALIDATION
  956. /* info is needed only for validation */
  957. memset(&info, 0, sizeof(info));
  958. info.id = control->id;
  959. ret = __snd_ctl_elem_info(card, kctl, &info, NULL);
  960. if (ret < 0)
  961. return ret;
  962. #endif
  963. if (!snd_ctl_skip_validation(&info))
  964. fill_remaining_elem_value(control, &info, pattern);
  965. ret = kctl->get(kctl, control);
  966. if (ret < 0)
  967. return ret;
  968. if (!snd_ctl_skip_validation(&info) &&
  969. sanity_check_elem_value(card, control, &info, pattern) < 0) {
  970. dev_err(card->dev,
  971. "control %i:%i:%i:%s:%i: access overflow\n",
  972. control->id.iface, control->id.device,
  973. control->id.subdevice, control->id.name,
  974. control->id.index);
  975. return -EINVAL;
  976. }
  977. return ret;
  978. }
  979. static int snd_ctl_elem_read_user(struct snd_card *card,
  980. struct snd_ctl_elem_value __user *_control)
  981. {
  982. struct snd_ctl_elem_value *control;
  983. int result;
  984. control = memdup_user(_control, sizeof(*control));
  985. if (IS_ERR(control))
  986. return PTR_ERR(control);
  987. result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
  988. if (result < 0)
  989. goto error;
  990. down_read(&card->controls_rwsem);
  991. result = snd_ctl_elem_read(card, control);
  992. up_read(&card->controls_rwsem);
  993. if (result < 0)
  994. goto error;
  995. if (copy_to_user(_control, control, sizeof(*control)))
  996. result = -EFAULT;
  997. error:
  998. kfree(control);
  999. return result;
  1000. }
  1001. static int snd_ctl_elem_write(struct snd_card *card, struct snd_ctl_file *file,
  1002. struct snd_ctl_elem_value *control)
  1003. {
  1004. struct snd_kcontrol *kctl;
  1005. struct snd_kcontrol_volatile *vd;
  1006. unsigned int index_offset;
  1007. int result;
  1008. kctl = snd_ctl_find_id(card, &control->id);
  1009. if (kctl == NULL)
  1010. return -ENOENT;
  1011. index_offset = snd_ctl_get_ioff(kctl, &control->id);
  1012. vd = &kctl->vd[index_offset];
  1013. if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_WRITE) || kctl->put == NULL ||
  1014. (file && vd->owner && vd->owner != file)) {
  1015. return -EPERM;
  1016. }
  1017. snd_ctl_build_ioff(&control->id, kctl, index_offset);
  1018. result = kctl->put(kctl, control);
  1019. if (result < 0)
  1020. return result;
  1021. if (result > 0) {
  1022. struct snd_ctl_elem_id id = control->id;
  1023. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, &id);
  1024. }
  1025. return 0;
  1026. }
  1027. static int snd_ctl_elem_write_user(struct snd_ctl_file *file,
  1028. struct snd_ctl_elem_value __user *_control)
  1029. {
  1030. struct snd_ctl_elem_value *control;
  1031. struct snd_card *card;
  1032. int result;
  1033. control = memdup_user(_control, sizeof(*control));
  1034. if (IS_ERR(control))
  1035. return PTR_ERR(control);
  1036. card = file->card;
  1037. result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
  1038. if (result < 0)
  1039. goto error;
  1040. down_write(&card->controls_rwsem);
  1041. result = snd_ctl_elem_write(card, file, control);
  1042. up_write(&card->controls_rwsem);
  1043. if (result < 0)
  1044. goto error;
  1045. if (copy_to_user(_control, control, sizeof(*control)))
  1046. result = -EFAULT;
  1047. error:
  1048. kfree(control);
  1049. return result;
  1050. }
  1051. static int snd_ctl_elem_lock(struct snd_ctl_file *file,
  1052. struct snd_ctl_elem_id __user *_id)
  1053. {
  1054. struct snd_card *card = file->card;
  1055. struct snd_ctl_elem_id id;
  1056. struct snd_kcontrol *kctl;
  1057. struct snd_kcontrol_volatile *vd;
  1058. int result;
  1059. if (copy_from_user(&id, _id, sizeof(id)))
  1060. return -EFAULT;
  1061. down_write(&card->controls_rwsem);
  1062. kctl = snd_ctl_find_id(card, &id);
  1063. if (kctl == NULL) {
  1064. result = -ENOENT;
  1065. } else {
  1066. vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
  1067. if (vd->owner != NULL)
  1068. result = -EBUSY;
  1069. else {
  1070. vd->owner = file;
  1071. result = 0;
  1072. }
  1073. }
  1074. up_write(&card->controls_rwsem);
  1075. return result;
  1076. }
  1077. static int snd_ctl_elem_unlock(struct snd_ctl_file *file,
  1078. struct snd_ctl_elem_id __user *_id)
  1079. {
  1080. struct snd_card *card = file->card;
  1081. struct snd_ctl_elem_id id;
  1082. struct snd_kcontrol *kctl;
  1083. struct snd_kcontrol_volatile *vd;
  1084. int result;
  1085. if (copy_from_user(&id, _id, sizeof(id)))
  1086. return -EFAULT;
  1087. down_write(&card->controls_rwsem);
  1088. kctl = snd_ctl_find_id(card, &id);
  1089. if (kctl == NULL) {
  1090. result = -ENOENT;
  1091. } else {
  1092. vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
  1093. if (vd->owner == NULL)
  1094. result = -EINVAL;
  1095. else if (vd->owner != file)
  1096. result = -EPERM;
  1097. else {
  1098. vd->owner = NULL;
  1099. result = 0;
  1100. }
  1101. }
  1102. up_write(&card->controls_rwsem);
  1103. return result;
  1104. }
  1105. struct user_element {
  1106. struct snd_ctl_elem_info info;
  1107. struct snd_card *card;
  1108. char *elem_data; /* element data */
  1109. unsigned long elem_data_size; /* size of element data in bytes */
  1110. void *tlv_data; /* TLV data */
  1111. unsigned long tlv_data_size; /* TLV data size */
  1112. void *priv_data; /* private data (like strings for enumerated type) */
  1113. };
  1114. static int snd_ctl_elem_user_info(struct snd_kcontrol *kcontrol,
  1115. struct snd_ctl_elem_info *uinfo)
  1116. {
  1117. struct user_element *ue = kcontrol->private_data;
  1118. unsigned int offset;
  1119. offset = snd_ctl_get_ioff(kcontrol, &uinfo->id);
  1120. *uinfo = ue->info;
  1121. snd_ctl_build_ioff(&uinfo->id, kcontrol, offset);
  1122. return 0;
  1123. }
  1124. static int snd_ctl_elem_user_enum_info(struct snd_kcontrol *kcontrol,
  1125. struct snd_ctl_elem_info *uinfo)
  1126. {
  1127. struct user_element *ue = kcontrol->private_data;
  1128. const char *names;
  1129. unsigned int item;
  1130. unsigned int offset;
  1131. item = uinfo->value.enumerated.item;
  1132. offset = snd_ctl_get_ioff(kcontrol, &uinfo->id);
  1133. *uinfo = ue->info;
  1134. snd_ctl_build_ioff(&uinfo->id, kcontrol, offset);
  1135. item = min(item, uinfo->value.enumerated.items - 1);
  1136. uinfo->value.enumerated.item = item;
  1137. names = ue->priv_data;
  1138. for (; item > 0; --item)
  1139. names += strlen(names) + 1;
  1140. strcpy(uinfo->value.enumerated.name, names);
  1141. return 0;
  1142. }
  1143. static int snd_ctl_elem_user_get(struct snd_kcontrol *kcontrol,
  1144. struct snd_ctl_elem_value *ucontrol)
  1145. {
  1146. struct user_element *ue = kcontrol->private_data;
  1147. unsigned int size = ue->elem_data_size;
  1148. char *src = ue->elem_data +
  1149. snd_ctl_get_ioff(kcontrol, &ucontrol->id) * size;
  1150. memcpy(&ucontrol->value, src, size);
  1151. return 0;
  1152. }
  1153. static int snd_ctl_elem_user_put(struct snd_kcontrol *kcontrol,
  1154. struct snd_ctl_elem_value *ucontrol)
  1155. {
  1156. int change;
  1157. struct user_element *ue = kcontrol->private_data;
  1158. unsigned int size = ue->elem_data_size;
  1159. char *dst = ue->elem_data +
  1160. snd_ctl_get_ioff(kcontrol, &ucontrol->id) * size;
  1161. change = memcmp(&ucontrol->value, dst, size) != 0;
  1162. if (change)
  1163. memcpy(dst, &ucontrol->value, size);
  1164. return change;
  1165. }
  1166. static int replace_user_tlv(struct snd_kcontrol *kctl, unsigned int __user *buf,
  1167. unsigned int size)
  1168. {
  1169. struct user_element *ue = kctl->private_data;
  1170. unsigned int *container;
  1171. struct snd_ctl_elem_id id;
  1172. unsigned int mask = 0;
  1173. int i;
  1174. int change;
  1175. if (size > 1024 * 128) /* sane value */
  1176. return -EINVAL;
  1177. container = vmemdup_user(buf, size);
  1178. if (IS_ERR(container))
  1179. return PTR_ERR(container);
  1180. change = ue->tlv_data_size != size;
  1181. if (!change)
  1182. change = memcmp(ue->tlv_data, container, size) != 0;
  1183. if (!change) {
  1184. kvfree(container);
  1185. return 0;
  1186. }
  1187. if (ue->tlv_data == NULL) {
  1188. /* Now TLV data is available. */
  1189. for (i = 0; i < kctl->count; ++i)
  1190. kctl->vd[i].access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ;
  1191. mask = SNDRV_CTL_EVENT_MASK_INFO;
  1192. }
  1193. kvfree(ue->tlv_data);
  1194. ue->tlv_data = container;
  1195. ue->tlv_data_size = size;
  1196. mask |= SNDRV_CTL_EVENT_MASK_TLV;
  1197. for (i = 0; i < kctl->count; ++i) {
  1198. snd_ctl_build_ioff(&id, kctl, i);
  1199. snd_ctl_notify(ue->card, mask, &id);
  1200. }
  1201. return change;
  1202. }
  1203. static int read_user_tlv(struct snd_kcontrol *kctl, unsigned int __user *buf,
  1204. unsigned int size)
  1205. {
  1206. struct user_element *ue = kctl->private_data;
  1207. if (ue->tlv_data_size == 0 || ue->tlv_data == NULL)
  1208. return -ENXIO;
  1209. if (size < ue->tlv_data_size)
  1210. return -ENOSPC;
  1211. if (copy_to_user(buf, ue->tlv_data, ue->tlv_data_size))
  1212. return -EFAULT;
  1213. return 0;
  1214. }
  1215. static int snd_ctl_elem_user_tlv(struct snd_kcontrol *kctl, int op_flag,
  1216. unsigned int size, unsigned int __user *buf)
  1217. {
  1218. if (op_flag == SNDRV_CTL_TLV_OP_WRITE)
  1219. return replace_user_tlv(kctl, buf, size);
  1220. else
  1221. return read_user_tlv(kctl, buf, size);
  1222. }
  1223. static int snd_ctl_elem_init_enum_names(struct user_element *ue)
  1224. {
  1225. char *names, *p;
  1226. size_t buf_len, name_len;
  1227. unsigned int i;
  1228. const uintptr_t user_ptrval = ue->info.value.enumerated.names_ptr;
  1229. if (ue->info.value.enumerated.names_length > 64 * 1024)
  1230. return -EINVAL;
  1231. names = vmemdup_user((const void __user *)user_ptrval,
  1232. ue->info.value.enumerated.names_length);
  1233. if (IS_ERR(names))
  1234. return PTR_ERR(names);
  1235. /* check that there are enough valid names */
  1236. buf_len = ue->info.value.enumerated.names_length;
  1237. p = names;
  1238. for (i = 0; i < ue->info.value.enumerated.items; ++i) {
  1239. name_len = strnlen(p, buf_len);
  1240. if (name_len == 0 || name_len >= 64 || name_len == buf_len) {
  1241. kvfree(names);
  1242. return -EINVAL;
  1243. }
  1244. p += name_len + 1;
  1245. buf_len -= name_len + 1;
  1246. }
  1247. ue->priv_data = names;
  1248. ue->info.value.enumerated.names_ptr = 0;
  1249. return 0;
  1250. }
  1251. static void snd_ctl_elem_user_free(struct snd_kcontrol *kcontrol)
  1252. {
  1253. struct user_element *ue = kcontrol->private_data;
  1254. kvfree(ue->tlv_data);
  1255. kvfree(ue->priv_data);
  1256. kfree(ue);
  1257. }
  1258. static int snd_ctl_elem_add(struct snd_ctl_file *file,
  1259. struct snd_ctl_elem_info *info, int replace)
  1260. {
  1261. struct snd_card *card = file->card;
  1262. struct snd_kcontrol *kctl;
  1263. unsigned int count;
  1264. unsigned int access;
  1265. long private_size;
  1266. struct user_element *ue;
  1267. unsigned int offset;
  1268. int err;
  1269. if (!*info->id.name)
  1270. return -EINVAL;
  1271. if (strnlen(info->id.name, sizeof(info->id.name)) >= sizeof(info->id.name))
  1272. return -EINVAL;
  1273. /* Delete a control to replace them if needed. */
  1274. if (replace) {
  1275. info->id.numid = 0;
  1276. err = snd_ctl_remove_user_ctl(file, &info->id);
  1277. if (err)
  1278. return err;
  1279. }
  1280. /*
  1281. * The number of userspace controls are counted control by control,
  1282. * not element by element.
  1283. */
  1284. if (card->user_ctl_count + 1 > MAX_USER_CONTROLS)
  1285. return -ENOMEM;
  1286. /* Check the number of elements for this userspace control. */
  1287. count = info->owner;
  1288. if (count == 0)
  1289. count = 1;
  1290. /* Arrange access permissions if needed. */
  1291. access = info->access;
  1292. if (access == 0)
  1293. access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
  1294. access &= (SNDRV_CTL_ELEM_ACCESS_READWRITE |
  1295. SNDRV_CTL_ELEM_ACCESS_INACTIVE |
  1296. SNDRV_CTL_ELEM_ACCESS_TLV_WRITE);
  1297. /* In initial state, nothing is available as TLV container. */
  1298. if (access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE)
  1299. access |= SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
  1300. access |= SNDRV_CTL_ELEM_ACCESS_USER;
  1301. /*
  1302. * Check information and calculate the size of data specific to
  1303. * this userspace control.
  1304. */
  1305. /* pass NULL to card for suppressing error messages */
  1306. err = snd_ctl_check_elem_info(NULL, info);
  1307. if (err < 0)
  1308. return err;
  1309. /* user-space control doesn't allow zero-size data */
  1310. if (info->count < 1)
  1311. return -EINVAL;
  1312. private_size = value_sizes[info->type] * info->count;
  1313. /*
  1314. * Keep memory object for this userspace control. After passing this
  1315. * code block, the instance should be freed by snd_ctl_free_one().
  1316. *
  1317. * Note that these elements in this control are locked.
  1318. */
  1319. err = snd_ctl_new(&kctl, count, access, file);
  1320. if (err < 0)
  1321. return err;
  1322. memcpy(&kctl->id, &info->id, sizeof(kctl->id));
  1323. kctl->private_data = kzalloc(sizeof(struct user_element) + private_size * count,
  1324. GFP_KERNEL);
  1325. if (kctl->private_data == NULL) {
  1326. kfree(kctl);
  1327. return -ENOMEM;
  1328. }
  1329. kctl->private_free = snd_ctl_elem_user_free;
  1330. /* Set private data for this userspace control. */
  1331. ue = (struct user_element *)kctl->private_data;
  1332. ue->card = card;
  1333. ue->info = *info;
  1334. ue->info.access = 0;
  1335. ue->elem_data = (char *)ue + sizeof(*ue);
  1336. ue->elem_data_size = private_size;
  1337. if (ue->info.type == SNDRV_CTL_ELEM_TYPE_ENUMERATED) {
  1338. err = snd_ctl_elem_init_enum_names(ue);
  1339. if (err < 0) {
  1340. snd_ctl_free_one(kctl);
  1341. return err;
  1342. }
  1343. }
  1344. /* Set callback functions. */
  1345. if (info->type == SNDRV_CTL_ELEM_TYPE_ENUMERATED)
  1346. kctl->info = snd_ctl_elem_user_enum_info;
  1347. else
  1348. kctl->info = snd_ctl_elem_user_info;
  1349. if (access & SNDRV_CTL_ELEM_ACCESS_READ)
  1350. kctl->get = snd_ctl_elem_user_get;
  1351. if (access & SNDRV_CTL_ELEM_ACCESS_WRITE)
  1352. kctl->put = snd_ctl_elem_user_put;
  1353. if (access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE)
  1354. kctl->tlv.c = snd_ctl_elem_user_tlv;
  1355. /* This function manage to free the instance on failure. */
  1356. down_write(&card->controls_rwsem);
  1357. err = __snd_ctl_add_replace(card, kctl, CTL_ADD_EXCLUSIVE);
  1358. if (err < 0) {
  1359. snd_ctl_free_one(kctl);
  1360. goto unlock;
  1361. }
  1362. offset = snd_ctl_get_ioff(kctl, &info->id);
  1363. snd_ctl_build_ioff(&info->id, kctl, offset);
  1364. /*
  1365. * Here we cannot fill any field for the number of elements added by
  1366. * this operation because there're no specific fields. The usage of
  1367. * 'owner' field for this purpose may cause any bugs to userspace
  1368. * applications because the field originally means PID of a process
  1369. * which locks the element.
  1370. */
  1371. card->user_ctl_count++;
  1372. unlock:
  1373. up_write(&card->controls_rwsem);
  1374. return err;
  1375. }
  1376. static int snd_ctl_elem_add_user(struct snd_ctl_file *file,
  1377. struct snd_ctl_elem_info __user *_info, int replace)
  1378. {
  1379. struct snd_ctl_elem_info info;
  1380. int err;
  1381. if (copy_from_user(&info, _info, sizeof(info)))
  1382. return -EFAULT;
  1383. err = snd_ctl_elem_add(file, &info, replace);
  1384. if (err < 0)
  1385. return err;
  1386. if (copy_to_user(_info, &info, sizeof(info))) {
  1387. snd_ctl_remove_user_ctl(file, &info.id);
  1388. return -EFAULT;
  1389. }
  1390. return 0;
  1391. }
  1392. static int snd_ctl_elem_remove(struct snd_ctl_file *file,
  1393. struct snd_ctl_elem_id __user *_id)
  1394. {
  1395. struct snd_ctl_elem_id id;
  1396. if (copy_from_user(&id, _id, sizeof(id)))
  1397. return -EFAULT;
  1398. return snd_ctl_remove_user_ctl(file, &id);
  1399. }
  1400. static int snd_ctl_subscribe_events(struct snd_ctl_file *file, int __user *ptr)
  1401. {
  1402. int subscribe;
  1403. if (get_user(subscribe, ptr))
  1404. return -EFAULT;
  1405. if (subscribe < 0) {
  1406. subscribe = file->subscribed;
  1407. if (put_user(subscribe, ptr))
  1408. return -EFAULT;
  1409. return 0;
  1410. }
  1411. if (subscribe) {
  1412. file->subscribed = 1;
  1413. return 0;
  1414. } else if (file->subscribed) {
  1415. snd_ctl_empty_read_queue(file);
  1416. file->subscribed = 0;
  1417. }
  1418. return 0;
  1419. }
  1420. static int call_tlv_handler(struct snd_ctl_file *file, int op_flag,
  1421. struct snd_kcontrol *kctl,
  1422. struct snd_ctl_elem_id *id,
  1423. unsigned int __user *buf, unsigned int size)
  1424. {
  1425. static const struct {
  1426. int op;
  1427. int perm;
  1428. } pairs[] = {
  1429. {SNDRV_CTL_TLV_OP_READ, SNDRV_CTL_ELEM_ACCESS_TLV_READ},
  1430. {SNDRV_CTL_TLV_OP_WRITE, SNDRV_CTL_ELEM_ACCESS_TLV_WRITE},
  1431. {SNDRV_CTL_TLV_OP_CMD, SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND},
  1432. };
  1433. struct snd_kcontrol_volatile *vd = &kctl->vd[snd_ctl_get_ioff(kctl, id)];
  1434. int i;
  1435. /* Check support of the request for this element. */
  1436. for (i = 0; i < ARRAY_SIZE(pairs); ++i) {
  1437. if (op_flag == pairs[i].op && (vd->access & pairs[i].perm))
  1438. break;
  1439. }
  1440. if (i == ARRAY_SIZE(pairs))
  1441. return -ENXIO;
  1442. if (kctl->tlv.c == NULL)
  1443. return -ENXIO;
  1444. /* Write and command operations are not allowed for locked element. */
  1445. if (op_flag != SNDRV_CTL_TLV_OP_READ &&
  1446. vd->owner != NULL && vd->owner != file)
  1447. return -EPERM;
  1448. return kctl->tlv.c(kctl, op_flag, size, buf);
  1449. }
  1450. static int read_tlv_buf(struct snd_kcontrol *kctl, struct snd_ctl_elem_id *id,
  1451. unsigned int __user *buf, unsigned int size)
  1452. {
  1453. struct snd_kcontrol_volatile *vd = &kctl->vd[snd_ctl_get_ioff(kctl, id)];
  1454. unsigned int len;
  1455. if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_READ))
  1456. return -ENXIO;
  1457. if (kctl->tlv.p == NULL)
  1458. return -ENXIO;
  1459. len = sizeof(unsigned int) * 2 + kctl->tlv.p[1];
  1460. if (size < len)
  1461. return -ENOMEM;
  1462. if (copy_to_user(buf, kctl->tlv.p, len))
  1463. return -EFAULT;
  1464. return 0;
  1465. }
  1466. static int snd_ctl_tlv_ioctl(struct snd_ctl_file *file,
  1467. struct snd_ctl_tlv __user *buf,
  1468. int op_flag)
  1469. {
  1470. struct snd_ctl_tlv header;
  1471. unsigned int __user *container;
  1472. unsigned int container_size;
  1473. struct snd_kcontrol *kctl;
  1474. struct snd_ctl_elem_id id;
  1475. struct snd_kcontrol_volatile *vd;
  1476. if (copy_from_user(&header, buf, sizeof(header)))
  1477. return -EFAULT;
  1478. /* In design of control core, numerical ID starts at 1. */
  1479. if (header.numid == 0)
  1480. return -EINVAL;
  1481. /* At least, container should include type and length fields. */
  1482. if (header.length < sizeof(unsigned int) * 2)
  1483. return -EINVAL;
  1484. container_size = header.length;
  1485. container = buf->tlv;
  1486. kctl = snd_ctl_find_numid(file->card, header.numid);
  1487. if (kctl == NULL)
  1488. return -ENOENT;
  1489. /* Calculate index of the element in this set. */
  1490. id = kctl->id;
  1491. snd_ctl_build_ioff(&id, kctl, header.numid - id.numid);
  1492. vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
  1493. if (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
  1494. return call_tlv_handler(file, op_flag, kctl, &id, container,
  1495. container_size);
  1496. } else {
  1497. if (op_flag == SNDRV_CTL_TLV_OP_READ) {
  1498. return read_tlv_buf(kctl, &id, container,
  1499. container_size);
  1500. }
  1501. }
  1502. /* Not supported. */
  1503. return -ENXIO;
  1504. }
  1505. static long snd_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  1506. {
  1507. struct snd_ctl_file *ctl;
  1508. struct snd_card *card;
  1509. struct snd_kctl_ioctl *p;
  1510. void __user *argp = (void __user *)arg;
  1511. int __user *ip = argp;
  1512. int err;
  1513. ctl = file->private_data;
  1514. card = ctl->card;
  1515. if (snd_BUG_ON(!card))
  1516. return -ENXIO;
  1517. switch (cmd) {
  1518. case SNDRV_CTL_IOCTL_PVERSION:
  1519. return put_user(SNDRV_CTL_VERSION, ip) ? -EFAULT : 0;
  1520. case SNDRV_CTL_IOCTL_CARD_INFO:
  1521. return snd_ctl_card_info(card, ctl, cmd, argp);
  1522. case SNDRV_CTL_IOCTL_ELEM_LIST:
  1523. return snd_ctl_elem_list_user(card, argp);
  1524. case SNDRV_CTL_IOCTL_ELEM_INFO:
  1525. return snd_ctl_elem_info_user(ctl, argp);
  1526. case SNDRV_CTL_IOCTL_ELEM_READ:
  1527. return snd_ctl_elem_read_user(card, argp);
  1528. case SNDRV_CTL_IOCTL_ELEM_WRITE:
  1529. return snd_ctl_elem_write_user(ctl, argp);
  1530. case SNDRV_CTL_IOCTL_ELEM_LOCK:
  1531. return snd_ctl_elem_lock(ctl, argp);
  1532. case SNDRV_CTL_IOCTL_ELEM_UNLOCK:
  1533. return snd_ctl_elem_unlock(ctl, argp);
  1534. case SNDRV_CTL_IOCTL_ELEM_ADD:
  1535. return snd_ctl_elem_add_user(ctl, argp, 0);
  1536. case SNDRV_CTL_IOCTL_ELEM_REPLACE:
  1537. return snd_ctl_elem_add_user(ctl, argp, 1);
  1538. case SNDRV_CTL_IOCTL_ELEM_REMOVE:
  1539. return snd_ctl_elem_remove(ctl, argp);
  1540. case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS:
  1541. return snd_ctl_subscribe_events(ctl, ip);
  1542. case SNDRV_CTL_IOCTL_TLV_READ:
  1543. down_read(&ctl->card->controls_rwsem);
  1544. err = snd_ctl_tlv_ioctl(ctl, argp, SNDRV_CTL_TLV_OP_READ);
  1545. up_read(&ctl->card->controls_rwsem);
  1546. return err;
  1547. case SNDRV_CTL_IOCTL_TLV_WRITE:
  1548. down_write(&ctl->card->controls_rwsem);
  1549. err = snd_ctl_tlv_ioctl(ctl, argp, SNDRV_CTL_TLV_OP_WRITE);
  1550. up_write(&ctl->card->controls_rwsem);
  1551. return err;
  1552. case SNDRV_CTL_IOCTL_TLV_COMMAND:
  1553. down_write(&ctl->card->controls_rwsem);
  1554. err = snd_ctl_tlv_ioctl(ctl, argp, SNDRV_CTL_TLV_OP_CMD);
  1555. up_write(&ctl->card->controls_rwsem);
  1556. return err;
  1557. case SNDRV_CTL_IOCTL_POWER:
  1558. return -ENOPROTOOPT;
  1559. case SNDRV_CTL_IOCTL_POWER_STATE:
  1560. #ifdef CONFIG_PM
  1561. return put_user(card->power_state, ip) ? -EFAULT : 0;
  1562. #else
  1563. return put_user(SNDRV_CTL_POWER_D0, ip) ? -EFAULT : 0;
  1564. #endif
  1565. }
  1566. down_read(&snd_ioctl_rwsem);
  1567. list_for_each_entry(p, &snd_control_ioctls, list) {
  1568. err = p->fioctl(card, ctl, cmd, arg);
  1569. if (err != -ENOIOCTLCMD) {
  1570. up_read(&snd_ioctl_rwsem);
  1571. return err;
  1572. }
  1573. }
  1574. up_read(&snd_ioctl_rwsem);
  1575. dev_dbg(card->dev, "unknown ioctl = 0x%x\n", cmd);
  1576. return -ENOTTY;
  1577. }
  1578. static ssize_t snd_ctl_read(struct file *file, char __user *buffer,
  1579. size_t count, loff_t * offset)
  1580. {
  1581. struct snd_ctl_file *ctl;
  1582. int err = 0;
  1583. ssize_t result = 0;
  1584. ctl = file->private_data;
  1585. if (snd_BUG_ON(!ctl || !ctl->card))
  1586. return -ENXIO;
  1587. if (!ctl->subscribed)
  1588. return -EBADFD;
  1589. if (count < sizeof(struct snd_ctl_event))
  1590. return -EINVAL;
  1591. spin_lock_irq(&ctl->read_lock);
  1592. while (count >= sizeof(struct snd_ctl_event)) {
  1593. struct snd_ctl_event ev;
  1594. struct snd_kctl_event *kev;
  1595. while (list_empty(&ctl->events)) {
  1596. wait_queue_entry_t wait;
  1597. if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
  1598. err = -EAGAIN;
  1599. goto __end_lock;
  1600. }
  1601. init_waitqueue_entry(&wait, current);
  1602. add_wait_queue(&ctl->change_sleep, &wait);
  1603. set_current_state(TASK_INTERRUPTIBLE);
  1604. spin_unlock_irq(&ctl->read_lock);
  1605. schedule();
  1606. remove_wait_queue(&ctl->change_sleep, &wait);
  1607. if (ctl->card->shutdown)
  1608. return -ENODEV;
  1609. if (signal_pending(current))
  1610. return -ERESTARTSYS;
  1611. spin_lock_irq(&ctl->read_lock);
  1612. }
  1613. kev = snd_kctl_event(ctl->events.next);
  1614. ev.type = SNDRV_CTL_EVENT_ELEM;
  1615. ev.data.elem.mask = kev->mask;
  1616. ev.data.elem.id = kev->id;
  1617. list_del(&kev->list);
  1618. spin_unlock_irq(&ctl->read_lock);
  1619. kfree(kev);
  1620. if (copy_to_user(buffer, &ev, sizeof(struct snd_ctl_event))) {
  1621. err = -EFAULT;
  1622. goto __end;
  1623. }
  1624. spin_lock_irq(&ctl->read_lock);
  1625. buffer += sizeof(struct snd_ctl_event);
  1626. count -= sizeof(struct snd_ctl_event);
  1627. result += sizeof(struct snd_ctl_event);
  1628. }
  1629. __end_lock:
  1630. spin_unlock_irq(&ctl->read_lock);
  1631. __end:
  1632. return result > 0 ? result : err;
  1633. }
  1634. static __poll_t snd_ctl_poll(struct file *file, poll_table * wait)
  1635. {
  1636. __poll_t mask;
  1637. struct snd_ctl_file *ctl;
  1638. ctl = file->private_data;
  1639. if (!ctl->subscribed)
  1640. return 0;
  1641. poll_wait(file, &ctl->change_sleep, wait);
  1642. mask = 0;
  1643. if (!list_empty(&ctl->events))
  1644. mask |= EPOLLIN | EPOLLRDNORM;
  1645. return mask;
  1646. }
  1647. /*
  1648. * register the device-specific control-ioctls.
  1649. * called from each device manager like pcm.c, hwdep.c, etc.
  1650. */
  1651. static int _snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn, struct list_head *lists)
  1652. {
  1653. struct snd_kctl_ioctl *pn;
  1654. pn = kzalloc(sizeof(struct snd_kctl_ioctl), GFP_KERNEL);
  1655. if (pn == NULL)
  1656. return -ENOMEM;
  1657. pn->fioctl = fcn;
  1658. down_write(&snd_ioctl_rwsem);
  1659. list_add_tail(&pn->list, lists);
  1660. up_write(&snd_ioctl_rwsem);
  1661. return 0;
  1662. }
  1663. /**
  1664. * snd_ctl_register_ioctl - register the device-specific control-ioctls
  1665. * @fcn: ioctl callback function
  1666. *
  1667. * called from each device manager like pcm.c, hwdep.c, etc.
  1668. */
  1669. int snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn)
  1670. {
  1671. return _snd_ctl_register_ioctl(fcn, &snd_control_ioctls);
  1672. }
  1673. EXPORT_SYMBOL(snd_ctl_register_ioctl);
  1674. #ifdef CONFIG_COMPAT
  1675. /**
  1676. * snd_ctl_register_ioctl_compat - register the device-specific 32bit compat
  1677. * control-ioctls
  1678. * @fcn: ioctl callback function
  1679. */
  1680. int snd_ctl_register_ioctl_compat(snd_kctl_ioctl_func_t fcn)
  1681. {
  1682. return _snd_ctl_register_ioctl(fcn, &snd_control_compat_ioctls);
  1683. }
  1684. EXPORT_SYMBOL(snd_ctl_register_ioctl_compat);
  1685. #endif
  1686. /*
  1687. * de-register the device-specific control-ioctls.
  1688. */
  1689. static int _snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn,
  1690. struct list_head *lists)
  1691. {
  1692. struct snd_kctl_ioctl *p;
  1693. if (snd_BUG_ON(!fcn))
  1694. return -EINVAL;
  1695. down_write(&snd_ioctl_rwsem);
  1696. list_for_each_entry(p, lists, list) {
  1697. if (p->fioctl == fcn) {
  1698. list_del(&p->list);
  1699. up_write(&snd_ioctl_rwsem);
  1700. kfree(p);
  1701. return 0;
  1702. }
  1703. }
  1704. up_write(&snd_ioctl_rwsem);
  1705. snd_BUG();
  1706. return -EINVAL;
  1707. }
  1708. /**
  1709. * snd_ctl_unregister_ioctl - de-register the device-specific control-ioctls
  1710. * @fcn: ioctl callback function to unregister
  1711. */
  1712. int snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn)
  1713. {
  1714. return _snd_ctl_unregister_ioctl(fcn, &snd_control_ioctls);
  1715. }
  1716. EXPORT_SYMBOL(snd_ctl_unregister_ioctl);
  1717. #ifdef CONFIG_COMPAT
  1718. /**
  1719. * snd_ctl_unregister_ioctl_compat - de-register the device-specific compat
  1720. * 32bit control-ioctls
  1721. * @fcn: ioctl callback function to unregister
  1722. */
  1723. int snd_ctl_unregister_ioctl_compat(snd_kctl_ioctl_func_t fcn)
  1724. {
  1725. return _snd_ctl_unregister_ioctl(fcn, &snd_control_compat_ioctls);
  1726. }
  1727. EXPORT_SYMBOL(snd_ctl_unregister_ioctl_compat);
  1728. #endif
  1729. static int snd_ctl_fasync(int fd, struct file * file, int on)
  1730. {
  1731. struct snd_ctl_file *ctl;
  1732. ctl = file->private_data;
  1733. return fasync_helper(fd, file, on, &ctl->fasync);
  1734. }
  1735. /* return the preferred subdevice number if already assigned;
  1736. * otherwise return -1
  1737. */
  1738. int snd_ctl_get_preferred_subdevice(struct snd_card *card, int type)
  1739. {
  1740. struct snd_ctl_file *kctl;
  1741. int subdevice = -1;
  1742. unsigned long flags;
  1743. read_lock_irqsave(&card->ctl_files_rwlock, flags);
  1744. list_for_each_entry(kctl, &card->ctl_files, list) {
  1745. if (kctl->pid == task_pid(current)) {
  1746. subdevice = kctl->preferred_subdevice[type];
  1747. if (subdevice != -1)
  1748. break;
  1749. }
  1750. }
  1751. read_unlock_irqrestore(&card->ctl_files_rwlock, flags);
  1752. return subdevice;
  1753. }
  1754. EXPORT_SYMBOL_GPL(snd_ctl_get_preferred_subdevice);
  1755. /*
  1756. * ioctl32 compat
  1757. */
  1758. #ifdef CONFIG_COMPAT
  1759. #include "control_compat.c"
  1760. #else
  1761. #define snd_ctl_ioctl_compat NULL
  1762. #endif
  1763. /*
  1764. * INIT PART
  1765. */
  1766. static const struct file_operations snd_ctl_f_ops =
  1767. {
  1768. .owner = THIS_MODULE,
  1769. .read = snd_ctl_read,
  1770. .open = snd_ctl_open,
  1771. .release = snd_ctl_release,
  1772. .llseek = no_llseek,
  1773. .poll = snd_ctl_poll,
  1774. .unlocked_ioctl = snd_ctl_ioctl,
  1775. .compat_ioctl = snd_ctl_ioctl_compat,
  1776. .fasync = snd_ctl_fasync,
  1777. };
  1778. /*
  1779. * registration of the control device
  1780. */
  1781. static int snd_ctl_dev_register(struct snd_device *device)
  1782. {
  1783. struct snd_card *card = device->device_data;
  1784. return snd_register_device(SNDRV_DEVICE_TYPE_CONTROL, card, -1,
  1785. &snd_ctl_f_ops, card, &card->ctl_dev);
  1786. }
  1787. /*
  1788. * disconnection of the control device
  1789. */
  1790. static int snd_ctl_dev_disconnect(struct snd_device *device)
  1791. {
  1792. struct snd_card *card = device->device_data;
  1793. struct snd_ctl_file *ctl;
  1794. unsigned long flags;
  1795. read_lock_irqsave(&card->ctl_files_rwlock, flags);
  1796. list_for_each_entry(ctl, &card->ctl_files, list) {
  1797. wake_up(&ctl->change_sleep);
  1798. kill_fasync(&ctl->fasync, SIGIO, POLL_ERR);
  1799. }
  1800. read_unlock_irqrestore(&card->ctl_files_rwlock, flags);
  1801. return snd_unregister_device(&card->ctl_dev);
  1802. }
  1803. /*
  1804. * free all controls
  1805. */
  1806. static int snd_ctl_dev_free(struct snd_device *device)
  1807. {
  1808. struct snd_card *card = device->device_data;
  1809. struct snd_kcontrol *control;
  1810. down_write(&card->controls_rwsem);
  1811. while (!list_empty(&card->controls)) {
  1812. control = snd_kcontrol(card->controls.next);
  1813. snd_ctl_remove(card, control);
  1814. }
  1815. up_write(&card->controls_rwsem);
  1816. put_device(&card->ctl_dev);
  1817. return 0;
  1818. }
  1819. /*
  1820. * create control core:
  1821. * called from init.c
  1822. */
  1823. int snd_ctl_create(struct snd_card *card)
  1824. {
  1825. static const struct snd_device_ops ops = {
  1826. .dev_free = snd_ctl_dev_free,
  1827. .dev_register = snd_ctl_dev_register,
  1828. .dev_disconnect = snd_ctl_dev_disconnect,
  1829. };
  1830. int err;
  1831. if (snd_BUG_ON(!card))
  1832. return -ENXIO;
  1833. if (snd_BUG_ON(card->number < 0 || card->number >= SNDRV_CARDS))
  1834. return -ENXIO;
  1835. snd_device_initialize(&card->ctl_dev, card);
  1836. dev_set_name(&card->ctl_dev, "controlC%d", card->number);
  1837. err = snd_device_new(card, SNDRV_DEV_CONTROL, card, &ops);
  1838. if (err < 0)
  1839. put_device(&card->ctl_dev);
  1840. return err;
  1841. }
  1842. /*
  1843. * Frequently used control callbacks/helpers
  1844. */
  1845. /**
  1846. * snd_ctl_boolean_mono_info - Helper function for a standard boolean info
  1847. * callback with a mono channel
  1848. * @kcontrol: the kcontrol instance
  1849. * @uinfo: info to store
  1850. *
  1851. * This is a function that can be used as info callback for a standard
  1852. * boolean control with a single mono channel.
  1853. */
  1854. int snd_ctl_boolean_mono_info(struct snd_kcontrol *kcontrol,
  1855. struct snd_ctl_elem_info *uinfo)
  1856. {
  1857. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1858. uinfo->count = 1;
  1859. uinfo->value.integer.min = 0;
  1860. uinfo->value.integer.max = 1;
  1861. return 0;
  1862. }
  1863. EXPORT_SYMBOL(snd_ctl_boolean_mono_info);
  1864. /**
  1865. * snd_ctl_boolean_stereo_info - Helper function for a standard boolean info
  1866. * callback with stereo two channels
  1867. * @kcontrol: the kcontrol instance
  1868. * @uinfo: info to store
  1869. *
  1870. * This is a function that can be used as info callback for a standard
  1871. * boolean control with stereo two channels.
  1872. */
  1873. int snd_ctl_boolean_stereo_info(struct snd_kcontrol *kcontrol,
  1874. struct snd_ctl_elem_info *uinfo)
  1875. {
  1876. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1877. uinfo->count = 2;
  1878. uinfo->value.integer.min = 0;
  1879. uinfo->value.integer.max = 1;
  1880. return 0;
  1881. }
  1882. EXPORT_SYMBOL(snd_ctl_boolean_stereo_info);
  1883. /**
  1884. * snd_ctl_enum_info - fills the info structure for an enumerated control
  1885. * @info: the structure to be filled
  1886. * @channels: the number of the control's channels; often one
  1887. * @items: the number of control values; also the size of @names
  1888. * @names: an array containing the names of all control values
  1889. *
  1890. * Sets all required fields in @info to their appropriate values.
  1891. * If the control's accessibility is not the default (readable and writable),
  1892. * the caller has to fill @info->access.
  1893. *
  1894. * Return: Zero.
  1895. */
  1896. int snd_ctl_enum_info(struct snd_ctl_elem_info *info, unsigned int channels,
  1897. unsigned int items, const char *const names[])
  1898. {
  1899. info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  1900. info->count = channels;
  1901. info->value.enumerated.items = items;
  1902. if (!items)
  1903. return 0;
  1904. if (info->value.enumerated.item >= items)
  1905. info->value.enumerated.item = items - 1;
  1906. WARN(strlen(names[info->value.enumerated.item]) >= sizeof(info->value.enumerated.name),
  1907. "ALSA: too long item name '%s'\n",
  1908. names[info->value.enumerated.item]);
  1909. strlcpy(info->value.enumerated.name,
  1910. names[info->value.enumerated.item],
  1911. sizeof(info->value.enumerated.name));
  1912. return 0;
  1913. }
  1914. EXPORT_SYMBOL(snd_ctl_enum_info);