control.c 39 KB

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