control.c 44 KB

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