control.c 50 KB

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