control.c 39 KB

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