control.c 51 KB

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