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 <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. snd_assert(card != NULL && id != NULL, return);
  130. read_lock(&card->ctl_files_rwlock);
  131. #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
  132. card->mixer_oss_change_count++;
  133. #endif
  134. list_for_each_entry(ctl, &card->ctl_files, list) {
  135. if (!ctl->subscribed)
  136. continue;
  137. spin_lock_irqsave(&ctl->read_lock, flags);
  138. list_for_each_entry(ev, &ctl->events, list) {
  139. if (ev->id.numid == id->numid) {
  140. ev->mask |= mask;
  141. goto _found;
  142. }
  143. }
  144. ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
  145. if (ev) {
  146. ev->id = *id;
  147. ev->mask = mask;
  148. list_add_tail(&ev->list, &ctl->events);
  149. } else {
  150. snd_printk(KERN_ERR "No memory available to allocate event\n");
  151. }
  152. _found:
  153. wake_up(&ctl->change_sleep);
  154. spin_unlock_irqrestore(&ctl->read_lock, flags);
  155. kill_fasync(&ctl->fasync, SIGIO, POLL_IN);
  156. }
  157. read_unlock(&card->ctl_files_rwlock);
  158. }
  159. EXPORT_SYMBOL(snd_ctl_notify);
  160. /**
  161. * snd_ctl_new - create a control instance from the template
  162. * @control: the control template
  163. * @access: the default control access
  164. *
  165. * Allocates a new struct snd_kcontrol instance and copies the given template
  166. * to the new instance. It does not copy volatile data (access).
  167. *
  168. * Returns the pointer of the new instance, or NULL on failure.
  169. */
  170. static struct snd_kcontrol *snd_ctl_new(struct snd_kcontrol *control,
  171. unsigned int access)
  172. {
  173. struct snd_kcontrol *kctl;
  174. unsigned int idx;
  175. snd_assert(control != NULL, return NULL);
  176. snd_assert(control->count > 0, return NULL);
  177. kctl = kzalloc(sizeof(*kctl) + sizeof(struct snd_kcontrol_volatile) * control->count, GFP_KERNEL);
  178. if (kctl == NULL) {
  179. snd_printk(KERN_ERR "Cannot allocate control instance\n");
  180. return NULL;
  181. }
  182. *kctl = *control;
  183. for (idx = 0; idx < kctl->count; idx++)
  184. kctl->vd[idx].access = access;
  185. return kctl;
  186. }
  187. /**
  188. * snd_ctl_new1 - create a control instance from the template
  189. * @ncontrol: the initialization record
  190. * @private_data: the private data to set
  191. *
  192. * Allocates a new struct snd_kcontrol instance and initialize from the given
  193. * template. When the access field of ncontrol is 0, it's assumed as
  194. * READWRITE access. When the count field is 0, it's assumes as one.
  195. *
  196. * Returns the pointer of the newly generated instance, or NULL on failure.
  197. */
  198. struct snd_kcontrol *snd_ctl_new1(const struct snd_kcontrol_new *ncontrol,
  199. void *private_data)
  200. {
  201. struct snd_kcontrol kctl;
  202. unsigned int access;
  203. snd_assert(ncontrol != NULL, return NULL);
  204. snd_assert(ncontrol->info != NULL, return NULL);
  205. memset(&kctl, 0, sizeof(kctl));
  206. kctl.id.iface = ncontrol->iface;
  207. kctl.id.device = ncontrol->device;
  208. kctl.id.subdevice = ncontrol->subdevice;
  209. if (ncontrol->name)
  210. strlcpy(kctl.id.name, ncontrol->name, sizeof(kctl.id.name));
  211. kctl.id.index = ncontrol->index;
  212. kctl.count = ncontrol->count ? ncontrol->count : 1;
  213. access = ncontrol->access == 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE :
  214. (ncontrol->access & (SNDRV_CTL_ELEM_ACCESS_READWRITE|
  215. SNDRV_CTL_ELEM_ACCESS_INACTIVE|
  216. SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE|
  217. SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK));
  218. kctl.info = ncontrol->info;
  219. kctl.get = ncontrol->get;
  220. kctl.put = ncontrol->put;
  221. kctl.tlv.p = ncontrol->tlv.p;
  222. kctl.private_value = ncontrol->private_value;
  223. kctl.private_data = private_data;
  224. return snd_ctl_new(&kctl, access);
  225. }
  226. EXPORT_SYMBOL(snd_ctl_new1);
  227. /**
  228. * snd_ctl_free_one - release the control instance
  229. * @kcontrol: the control instance
  230. *
  231. * Releases the control instance created via snd_ctl_new()
  232. * or snd_ctl_new1().
  233. * Don't call this after the control was added to the card.
  234. */
  235. void snd_ctl_free_one(struct snd_kcontrol *kcontrol)
  236. {
  237. if (kcontrol) {
  238. if (kcontrol->private_free)
  239. kcontrol->private_free(kcontrol);
  240. kfree(kcontrol);
  241. }
  242. }
  243. EXPORT_SYMBOL(snd_ctl_free_one);
  244. static unsigned int snd_ctl_hole_check(struct snd_card *card,
  245. unsigned int count)
  246. {
  247. struct snd_kcontrol *kctl;
  248. list_for_each_entry(kctl, &card->controls, list) {
  249. if ((kctl->id.numid <= card->last_numid &&
  250. kctl->id.numid + kctl->count > card->last_numid) ||
  251. (kctl->id.numid <= card->last_numid + count - 1 &&
  252. kctl->id.numid + kctl->count > card->last_numid + count - 1))
  253. return card->last_numid = kctl->id.numid + kctl->count - 1;
  254. }
  255. return card->last_numid;
  256. }
  257. static int snd_ctl_find_hole(struct snd_card *card, unsigned int count)
  258. {
  259. unsigned int last_numid, iter = 100000;
  260. last_numid = card->last_numid;
  261. while (last_numid != snd_ctl_hole_check(card, count)) {
  262. if (--iter == 0) {
  263. /* this situation is very unlikely */
  264. snd_printk(KERN_ERR "unable to allocate new control numid\n");
  265. return -ENOMEM;
  266. }
  267. last_numid = card->last_numid;
  268. }
  269. return 0;
  270. }
  271. /**
  272. * snd_ctl_add - add the control instance to the card
  273. * @card: the card instance
  274. * @kcontrol: the control instance to add
  275. *
  276. * Adds the control instance created via snd_ctl_new() or
  277. * snd_ctl_new1() to the given card. Assigns also an unique
  278. * numid used for fast search.
  279. *
  280. * Returns zero if successful, or a negative error code on failure.
  281. *
  282. * It frees automatically the control which cannot be added.
  283. */
  284. int snd_ctl_add(struct snd_card *card, struct snd_kcontrol *kcontrol)
  285. {
  286. struct snd_ctl_elem_id id;
  287. unsigned int idx;
  288. int err = -EINVAL;
  289. if (! kcontrol)
  290. return err;
  291. snd_assert(card != NULL, goto error);
  292. snd_assert(kcontrol->info != NULL, goto error);
  293. id = kcontrol->id;
  294. down_write(&card->controls_rwsem);
  295. if (snd_ctl_find_id(card, &id)) {
  296. up_write(&card->controls_rwsem);
  297. snd_printd(KERN_ERR "control %i:%i:%i:%s:%i is already present\n",
  298. id.iface,
  299. id.device,
  300. id.subdevice,
  301. id.name,
  302. id.index);
  303. err = -EBUSY;
  304. goto error;
  305. }
  306. if (snd_ctl_find_hole(card, kcontrol->count) < 0) {
  307. up_write(&card->controls_rwsem);
  308. err = -ENOMEM;
  309. goto error;
  310. }
  311. list_add_tail(&kcontrol->list, &card->controls);
  312. card->controls_count += kcontrol->count;
  313. kcontrol->id.numid = card->last_numid + 1;
  314. card->last_numid += kcontrol->count;
  315. up_write(&card->controls_rwsem);
  316. for (idx = 0; idx < kcontrol->count; idx++, id.index++, id.numid++)
  317. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_ADD, &id);
  318. return 0;
  319. error:
  320. snd_ctl_free_one(kcontrol);
  321. return err;
  322. }
  323. EXPORT_SYMBOL(snd_ctl_add);
  324. /**
  325. * snd_ctl_remove - remove the control from the card and release it
  326. * @card: the card instance
  327. * @kcontrol: the control instance to remove
  328. *
  329. * Removes the control from the card and then releases the instance.
  330. * You don't need to call snd_ctl_free_one(). You must be in
  331. * the write lock - down_write(&card->controls_rwsem).
  332. *
  333. * Returns 0 if successful, or a negative error code on failure.
  334. */
  335. int snd_ctl_remove(struct snd_card *card, struct snd_kcontrol *kcontrol)
  336. {
  337. struct snd_ctl_elem_id id;
  338. unsigned int idx;
  339. snd_assert(card != NULL && kcontrol != NULL, return -EINVAL);
  340. list_del(&kcontrol->list);
  341. card->controls_count -= kcontrol->count;
  342. id = kcontrol->id;
  343. for (idx = 0; idx < kcontrol->count; idx++, id.index++, id.numid++)
  344. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_REMOVE, &id);
  345. snd_ctl_free_one(kcontrol);
  346. return 0;
  347. }
  348. EXPORT_SYMBOL(snd_ctl_remove);
  349. /**
  350. * snd_ctl_remove_id - remove the control of the given id and release it
  351. * @card: the card instance
  352. * @id: the control id to remove
  353. *
  354. * Finds the control instance with the given id, removes it from the
  355. * card list and releases it.
  356. *
  357. * Returns 0 if successful, or a negative error code on failure.
  358. */
  359. int snd_ctl_remove_id(struct snd_card *card, struct snd_ctl_elem_id *id)
  360. {
  361. struct snd_kcontrol *kctl;
  362. int ret;
  363. down_write(&card->controls_rwsem);
  364. kctl = snd_ctl_find_id(card, id);
  365. if (kctl == NULL) {
  366. up_write(&card->controls_rwsem);
  367. return -ENOENT;
  368. }
  369. ret = snd_ctl_remove(card, kctl);
  370. up_write(&card->controls_rwsem);
  371. return ret;
  372. }
  373. EXPORT_SYMBOL(snd_ctl_remove_id);
  374. /**
  375. * snd_ctl_remove_unlocked_id - remove the unlocked control of the given id and release it
  376. * @file: active control handle
  377. * @id: the control id to remove
  378. *
  379. * Finds the control instance with the given id, removes it from the
  380. * card list and releases it.
  381. *
  382. * Returns 0 if successful, or a negative error code on failure.
  383. */
  384. static int snd_ctl_remove_unlocked_id(struct snd_ctl_file * file,
  385. struct snd_ctl_elem_id *id)
  386. {
  387. struct snd_card *card = file->card;
  388. struct snd_kcontrol *kctl;
  389. int idx, ret;
  390. down_write(&card->controls_rwsem);
  391. kctl = snd_ctl_find_id(card, id);
  392. if (kctl == NULL) {
  393. up_write(&card->controls_rwsem);
  394. return -ENOENT;
  395. }
  396. for (idx = 0; idx < kctl->count; idx++)
  397. if (kctl->vd[idx].owner != NULL && kctl->vd[idx].owner != file) {
  398. up_write(&card->controls_rwsem);
  399. return -EBUSY;
  400. }
  401. ret = snd_ctl_remove(card, kctl);
  402. up_write(&card->controls_rwsem);
  403. return ret;
  404. }
  405. /**
  406. * snd_ctl_rename_id - replace the id of a control on the card
  407. * @card: the card instance
  408. * @src_id: the old id
  409. * @dst_id: the new id
  410. *
  411. * Finds the control with the old id from the card, and replaces the
  412. * id with the new one.
  413. *
  414. * Returns zero if successful, or a negative error code on failure.
  415. */
  416. int snd_ctl_rename_id(struct snd_card *card, struct snd_ctl_elem_id *src_id,
  417. struct snd_ctl_elem_id *dst_id)
  418. {
  419. struct snd_kcontrol *kctl;
  420. down_write(&card->controls_rwsem);
  421. kctl = snd_ctl_find_id(card, src_id);
  422. if (kctl == NULL) {
  423. up_write(&card->controls_rwsem);
  424. return -ENOENT;
  425. }
  426. kctl->id = *dst_id;
  427. kctl->id.numid = card->last_numid + 1;
  428. card->last_numid += kctl->count;
  429. up_write(&card->controls_rwsem);
  430. return 0;
  431. }
  432. EXPORT_SYMBOL(snd_ctl_rename_id);
  433. /**
  434. * snd_ctl_find_numid - find the control instance with the given number-id
  435. * @card: the card instance
  436. * @numid: the number-id to search
  437. *
  438. * Finds the control instance with the given number-id from the card.
  439. *
  440. * Returns the pointer of the instance if found, or NULL if not.
  441. *
  442. * The caller must down card->controls_rwsem before calling this function
  443. * (if the race condition can happen).
  444. */
  445. struct snd_kcontrol *snd_ctl_find_numid(struct snd_card *card, unsigned int numid)
  446. {
  447. struct snd_kcontrol *kctl;
  448. snd_assert(card != NULL && numid != 0, return NULL);
  449. list_for_each_entry(kctl, &card->controls, list) {
  450. if (kctl->id.numid <= numid && kctl->id.numid + kctl->count > numid)
  451. return kctl;
  452. }
  453. return NULL;
  454. }
  455. EXPORT_SYMBOL(snd_ctl_find_numid);
  456. /**
  457. * snd_ctl_find_id - find the control instance with the given id
  458. * @card: the card instance
  459. * @id: the id to search
  460. *
  461. * Finds the control instance with the given id from the card.
  462. *
  463. * Returns the pointer of the instance if found, or NULL if not.
  464. *
  465. * The caller must down card->controls_rwsem before calling this function
  466. * (if the race condition can happen).
  467. */
  468. struct snd_kcontrol *snd_ctl_find_id(struct snd_card *card,
  469. struct snd_ctl_elem_id *id)
  470. {
  471. struct snd_kcontrol *kctl;
  472. snd_assert(card != NULL && id != NULL, return NULL);
  473. if (id->numid != 0)
  474. return snd_ctl_find_numid(card, id->numid);
  475. list_for_each_entry(kctl, &card->controls, list) {
  476. if (kctl->id.iface != id->iface)
  477. continue;
  478. if (kctl->id.device != id->device)
  479. continue;
  480. if (kctl->id.subdevice != id->subdevice)
  481. continue;
  482. if (strncmp(kctl->id.name, id->name, sizeof(kctl->id.name)))
  483. continue;
  484. if (kctl->id.index > id->index)
  485. continue;
  486. if (kctl->id.index + kctl->count <= id->index)
  487. continue;
  488. return kctl;
  489. }
  490. return NULL;
  491. }
  492. EXPORT_SYMBOL(snd_ctl_find_id);
  493. static int snd_ctl_card_info(struct snd_card *card, struct snd_ctl_file * ctl,
  494. unsigned int cmd, void __user *arg)
  495. {
  496. struct snd_ctl_card_info *info;
  497. info = kzalloc(sizeof(*info), GFP_KERNEL);
  498. if (! info)
  499. return -ENOMEM;
  500. down_read(&snd_ioctl_rwsem);
  501. info->card = card->number;
  502. strlcpy(info->id, card->id, sizeof(info->id));
  503. strlcpy(info->driver, card->driver, sizeof(info->driver));
  504. strlcpy(info->name, card->shortname, sizeof(info->name));
  505. strlcpy(info->longname, card->longname, sizeof(info->longname));
  506. strlcpy(info->mixername, card->mixername, sizeof(info->mixername));
  507. strlcpy(info->components, card->components, sizeof(info->components));
  508. up_read(&snd_ioctl_rwsem);
  509. if (copy_to_user(arg, info, sizeof(struct snd_ctl_card_info))) {
  510. kfree(info);
  511. return -EFAULT;
  512. }
  513. kfree(info);
  514. return 0;
  515. }
  516. static int snd_ctl_elem_list(struct snd_card *card,
  517. struct snd_ctl_elem_list __user *_list)
  518. {
  519. struct list_head *plist;
  520. struct snd_ctl_elem_list list;
  521. struct snd_kcontrol *kctl;
  522. struct snd_ctl_elem_id *dst, *id;
  523. unsigned int offset, space, first, jidx;
  524. if (copy_from_user(&list, _list, sizeof(list)))
  525. return -EFAULT;
  526. offset = list.offset;
  527. space = list.space;
  528. first = 0;
  529. /* try limit maximum space */
  530. if (space > 16384)
  531. return -ENOMEM;
  532. if (space > 0) {
  533. /* allocate temporary buffer for atomic operation */
  534. dst = vmalloc(space * sizeof(struct snd_ctl_elem_id));
  535. if (dst == NULL)
  536. return -ENOMEM;
  537. down_read(&card->controls_rwsem);
  538. list.count = card->controls_count;
  539. plist = card->controls.next;
  540. while (plist != &card->controls) {
  541. if (offset == 0)
  542. break;
  543. kctl = snd_kcontrol(plist);
  544. if (offset < kctl->count)
  545. break;
  546. offset -= kctl->count;
  547. plist = plist->next;
  548. }
  549. list.used = 0;
  550. id = dst;
  551. while (space > 0 && plist != &card->controls) {
  552. kctl = snd_kcontrol(plist);
  553. for (jidx = offset; space > 0 && jidx < kctl->count; jidx++) {
  554. snd_ctl_build_ioff(id, kctl, jidx);
  555. id++;
  556. space--;
  557. list.used++;
  558. }
  559. plist = plist->next;
  560. offset = 0;
  561. }
  562. up_read(&card->controls_rwsem);
  563. if (list.used > 0 &&
  564. copy_to_user(list.pids, dst,
  565. list.used * sizeof(struct snd_ctl_elem_id))) {
  566. vfree(dst);
  567. return -EFAULT;
  568. }
  569. vfree(dst);
  570. } else {
  571. down_read(&card->controls_rwsem);
  572. list.count = card->controls_count;
  573. up_read(&card->controls_rwsem);
  574. }
  575. if (copy_to_user(_list, &list, sizeof(list)))
  576. return -EFAULT;
  577. return 0;
  578. }
  579. static int snd_ctl_elem_info(struct snd_ctl_file *ctl,
  580. struct snd_ctl_elem_info *info)
  581. {
  582. struct snd_card *card = ctl->card;
  583. struct snd_kcontrol *kctl;
  584. struct snd_kcontrol_volatile *vd;
  585. unsigned int index_offset;
  586. int result;
  587. down_read(&card->controls_rwsem);
  588. kctl = snd_ctl_find_id(card, &info->id);
  589. if (kctl == NULL) {
  590. up_read(&card->controls_rwsem);
  591. return -ENOENT;
  592. }
  593. #ifdef CONFIG_SND_DEBUG
  594. info->access = 0;
  595. #endif
  596. result = kctl->info(kctl, info);
  597. if (result >= 0) {
  598. snd_assert(info->access == 0, );
  599. index_offset = snd_ctl_get_ioff(kctl, &info->id);
  600. vd = &kctl->vd[index_offset];
  601. snd_ctl_build_ioff(&info->id, kctl, index_offset);
  602. info->access = vd->access;
  603. if (vd->owner) {
  604. info->access |= SNDRV_CTL_ELEM_ACCESS_LOCK;
  605. if (vd->owner == ctl)
  606. info->access |= SNDRV_CTL_ELEM_ACCESS_OWNER;
  607. info->owner = vd->owner_pid;
  608. } else {
  609. info->owner = -1;
  610. }
  611. }
  612. up_read(&card->controls_rwsem);
  613. return result;
  614. }
  615. static int snd_ctl_elem_info_user(struct snd_ctl_file *ctl,
  616. struct snd_ctl_elem_info __user *_info)
  617. {
  618. struct snd_ctl_elem_info info;
  619. int result;
  620. if (copy_from_user(&info, _info, sizeof(info)))
  621. return -EFAULT;
  622. snd_power_lock(ctl->card);
  623. result = snd_power_wait(ctl->card, SNDRV_CTL_POWER_D0);
  624. if (result >= 0)
  625. result = snd_ctl_elem_info(ctl, &info);
  626. snd_power_unlock(ctl->card);
  627. if (result >= 0)
  628. if (copy_to_user(_info, &info, sizeof(info)))
  629. return -EFAULT;
  630. return result;
  631. }
  632. static int snd_ctl_elem_read(struct snd_card *card,
  633. 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. static 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);