control.c 39 KB

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