control.c 35 KB

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