control.c 41 KB

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