control.c 44 KB

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