control.c 39 KB

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