control.c 39 KB

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