control.c 35 KB

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