control.c 39 KB

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