control.c 39 KB

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