control.c 45 KB

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