control.c 35 KB

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