control.c 50 KB

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