control.c 39 KB

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