control.c 36 KB

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