control.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353
  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. result = snd_ctl_elem_info(ctl, &info);
  618. if (result >= 0)
  619. if (copy_to_user(_info, &info, sizeof(info)))
  620. return -EFAULT;
  621. return result;
  622. }
  623. int snd_ctl_elem_read(struct snd_card *card, struct snd_ctl_elem_value *control)
  624. {
  625. struct snd_kcontrol *kctl;
  626. struct snd_kcontrol_volatile *vd;
  627. unsigned int index_offset;
  628. int result, indirect;
  629. down_read(&card->controls_rwsem);
  630. kctl = snd_ctl_find_id(card, &control->id);
  631. if (kctl == NULL) {
  632. result = -ENOENT;
  633. } else {
  634. index_offset = snd_ctl_get_ioff(kctl, &control->id);
  635. vd = &kctl->vd[index_offset];
  636. indirect = vd->access & SNDRV_CTL_ELEM_ACCESS_INDIRECT ? 1 : 0;
  637. if (control->indirect != indirect) {
  638. result = -EACCES;
  639. } else {
  640. if ((vd->access & SNDRV_CTL_ELEM_ACCESS_READ) && kctl->get != NULL) {
  641. snd_ctl_build_ioff(&control->id, kctl, index_offset);
  642. result = kctl->get(kctl, control);
  643. } else {
  644. result = -EPERM;
  645. }
  646. }
  647. }
  648. up_read(&card->controls_rwsem);
  649. return result;
  650. }
  651. static int snd_ctl_elem_read_user(struct snd_card *card,
  652. struct snd_ctl_elem_value __user *_control)
  653. {
  654. struct snd_ctl_elem_value *control;
  655. int result;
  656. control = kmalloc(sizeof(*control), GFP_KERNEL);
  657. if (control == NULL)
  658. return -ENOMEM;
  659. if (copy_from_user(control, _control, sizeof(*control))) {
  660. kfree(control);
  661. return -EFAULT;
  662. }
  663. result = snd_ctl_elem_read(card, control);
  664. if (result >= 0)
  665. if (copy_to_user(_control, control, sizeof(*control)))
  666. result = -EFAULT;
  667. kfree(control);
  668. return result;
  669. }
  670. int snd_ctl_elem_write(struct snd_card *card, struct snd_ctl_file *file,
  671. struct snd_ctl_elem_value *control)
  672. {
  673. struct snd_kcontrol *kctl;
  674. struct snd_kcontrol_volatile *vd;
  675. unsigned int index_offset;
  676. int result, indirect;
  677. down_read(&card->controls_rwsem);
  678. kctl = snd_ctl_find_id(card, &control->id);
  679. if (kctl == NULL) {
  680. result = -ENOENT;
  681. } else {
  682. index_offset = snd_ctl_get_ioff(kctl, &control->id);
  683. vd = &kctl->vd[index_offset];
  684. indirect = vd->access & SNDRV_CTL_ELEM_ACCESS_INDIRECT ? 1 : 0;
  685. if (control->indirect != indirect) {
  686. result = -EACCES;
  687. } else {
  688. if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_WRITE) ||
  689. kctl->put == NULL ||
  690. (file && vd->owner != NULL && vd->owner != file)) {
  691. result = -EPERM;
  692. } else {
  693. snd_ctl_build_ioff(&control->id, kctl, index_offset);
  694. result = kctl->put(kctl, control);
  695. }
  696. if (result > 0) {
  697. up_read(&card->controls_rwsem);
  698. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, &control->id);
  699. return 0;
  700. }
  701. }
  702. }
  703. up_read(&card->controls_rwsem);
  704. return result;
  705. }
  706. static int snd_ctl_elem_write_user(struct snd_ctl_file *file,
  707. struct snd_ctl_elem_value __user *_control)
  708. {
  709. struct snd_ctl_elem_value *control;
  710. int result;
  711. control = kmalloc(sizeof(*control), GFP_KERNEL);
  712. if (control == NULL)
  713. return -ENOMEM;
  714. if (copy_from_user(control, _control, sizeof(*control))) {
  715. kfree(control);
  716. return -EFAULT;
  717. }
  718. result = snd_ctl_elem_write(file->card, file, control);
  719. if (result >= 0)
  720. if (copy_to_user(_control, control, sizeof(*control)))
  721. result = -EFAULT;
  722. kfree(control);
  723. return result;
  724. }
  725. static int snd_ctl_elem_lock(struct snd_ctl_file *file,
  726. struct snd_ctl_elem_id __user *_id)
  727. {
  728. struct snd_card *card = file->card;
  729. struct snd_ctl_elem_id id;
  730. struct snd_kcontrol *kctl;
  731. struct snd_kcontrol_volatile *vd;
  732. int result;
  733. if (copy_from_user(&id, _id, sizeof(id)))
  734. return -EFAULT;
  735. down_write(&card->controls_rwsem);
  736. kctl = snd_ctl_find_id(card, &id);
  737. if (kctl == NULL) {
  738. result = -ENOENT;
  739. } else {
  740. vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
  741. if (vd->owner != NULL)
  742. result = -EBUSY;
  743. else {
  744. vd->owner = file;
  745. vd->owner_pid = current->pid;
  746. result = 0;
  747. }
  748. }
  749. up_write(&card->controls_rwsem);
  750. return result;
  751. }
  752. static int snd_ctl_elem_unlock(struct snd_ctl_file *file,
  753. struct snd_ctl_elem_id __user *_id)
  754. {
  755. struct snd_card *card = file->card;
  756. struct snd_ctl_elem_id id;
  757. struct snd_kcontrol *kctl;
  758. struct snd_kcontrol_volatile *vd;
  759. int result;
  760. if (copy_from_user(&id, _id, sizeof(id)))
  761. return -EFAULT;
  762. down_write(&card->controls_rwsem);
  763. kctl = snd_ctl_find_id(card, &id);
  764. if (kctl == NULL) {
  765. result = -ENOENT;
  766. } else {
  767. vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
  768. if (vd->owner == NULL)
  769. result = -EINVAL;
  770. else if (vd->owner != file)
  771. result = -EPERM;
  772. else {
  773. vd->owner = NULL;
  774. vd->owner_pid = 0;
  775. result = 0;
  776. }
  777. }
  778. up_write(&card->controls_rwsem);
  779. return result;
  780. }
  781. struct user_element {
  782. struct snd_ctl_elem_info info;
  783. void *elem_data; /* element data */
  784. unsigned long elem_data_size; /* size of element data in bytes */
  785. void *priv_data; /* private data (like strings for enumerated type) */
  786. unsigned long priv_data_size; /* size of private data in bytes */
  787. };
  788. static int snd_ctl_elem_user_info(struct snd_kcontrol *kcontrol,
  789. struct snd_ctl_elem_info *uinfo)
  790. {
  791. struct user_element *ue = kcontrol->private_data;
  792. *uinfo = ue->info;
  793. return 0;
  794. }
  795. static int snd_ctl_elem_user_get(struct snd_kcontrol *kcontrol,
  796. struct snd_ctl_elem_value *ucontrol)
  797. {
  798. struct user_element *ue = kcontrol->private_data;
  799. memcpy(&ucontrol->value, ue->elem_data, ue->elem_data_size);
  800. return 0;
  801. }
  802. static int snd_ctl_elem_user_put(struct snd_kcontrol *kcontrol,
  803. struct snd_ctl_elem_value *ucontrol)
  804. {
  805. int change;
  806. struct user_element *ue = kcontrol->private_data;
  807. change = memcmp(&ucontrol->value, ue->elem_data, ue->elem_data_size) != 0;
  808. if (change)
  809. memcpy(ue->elem_data, &ucontrol->value, ue->elem_data_size);
  810. return change;
  811. }
  812. static void snd_ctl_elem_user_free(struct snd_kcontrol *kcontrol)
  813. {
  814. kfree(kcontrol->private_data);
  815. }
  816. static int snd_ctl_elem_add(struct snd_ctl_file *file,
  817. struct snd_ctl_elem_info *info, int replace)
  818. {
  819. struct snd_card *card = file->card;
  820. struct snd_kcontrol kctl, *_kctl;
  821. unsigned int access;
  822. long private_size;
  823. struct user_element *ue;
  824. int idx, err;
  825. if (card->user_ctl_count >= MAX_USER_CONTROLS)
  826. return -ENOMEM;
  827. if (info->count > 1024)
  828. return -EINVAL;
  829. access = info->access == 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE :
  830. (info->access & (SNDRV_CTL_ELEM_ACCESS_READWRITE|
  831. SNDRV_CTL_ELEM_ACCESS_INACTIVE));
  832. info->id.numid = 0;
  833. memset(&kctl, 0, sizeof(kctl));
  834. down_write(&card->controls_rwsem);
  835. _kctl = snd_ctl_find_id(card, &info->id);
  836. err = 0;
  837. if (_kctl) {
  838. if (replace)
  839. err = snd_ctl_remove(card, _kctl);
  840. else
  841. err = -EBUSY;
  842. } else {
  843. if (replace)
  844. err = -ENOENT;
  845. }
  846. up_write(&card->controls_rwsem);
  847. if (err < 0)
  848. return err;
  849. memcpy(&kctl.id, &info->id, sizeof(info->id));
  850. kctl.count = info->owner ? info->owner : 1;
  851. access |= SNDRV_CTL_ELEM_ACCESS_USER;
  852. kctl.info = snd_ctl_elem_user_info;
  853. if (access & SNDRV_CTL_ELEM_ACCESS_READ)
  854. kctl.get = snd_ctl_elem_user_get;
  855. if (access & SNDRV_CTL_ELEM_ACCESS_WRITE)
  856. kctl.put = snd_ctl_elem_user_put;
  857. switch (info->type) {
  858. case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
  859. private_size = sizeof(char);
  860. if (info->count > 128)
  861. return -EINVAL;
  862. break;
  863. case SNDRV_CTL_ELEM_TYPE_INTEGER:
  864. private_size = sizeof(long);
  865. if (info->count > 128)
  866. return -EINVAL;
  867. break;
  868. case SNDRV_CTL_ELEM_TYPE_INTEGER64:
  869. private_size = sizeof(long long);
  870. if (info->count > 64)
  871. return -EINVAL;
  872. break;
  873. case SNDRV_CTL_ELEM_TYPE_BYTES:
  874. private_size = sizeof(unsigned char);
  875. if (info->count > 512)
  876. return -EINVAL;
  877. break;
  878. case SNDRV_CTL_ELEM_TYPE_IEC958:
  879. private_size = sizeof(struct snd_aes_iec958);
  880. if (info->count != 1)
  881. return -EINVAL;
  882. break;
  883. default:
  884. return -EINVAL;
  885. }
  886. private_size *= info->count;
  887. ue = kzalloc(sizeof(struct user_element) + private_size, GFP_KERNEL);
  888. if (ue == NULL)
  889. return -ENOMEM;
  890. ue->info = *info;
  891. ue->elem_data = (char *)ue + sizeof(*ue);
  892. ue->elem_data_size = private_size;
  893. kctl.private_free = snd_ctl_elem_user_free;
  894. _kctl = snd_ctl_new(&kctl, access);
  895. if (_kctl == NULL) {
  896. kfree(_kctl->private_data);
  897. return -ENOMEM;
  898. }
  899. _kctl->private_data = ue;
  900. for (idx = 0; idx < _kctl->count; idx++)
  901. _kctl->vd[idx].owner = file;
  902. err = snd_ctl_add(card, _kctl);
  903. if (err < 0) {
  904. snd_ctl_free_one(_kctl);
  905. return err;
  906. }
  907. down_write(&card->controls_rwsem);
  908. card->user_ctl_count++;
  909. up_write(&card->controls_rwsem);
  910. return 0;
  911. }
  912. static int snd_ctl_elem_add_user(struct snd_ctl_file *file,
  913. struct snd_ctl_elem_info __user *_info, int replace)
  914. {
  915. struct snd_ctl_elem_info info;
  916. if (copy_from_user(&info, _info, sizeof(info)))
  917. return -EFAULT;
  918. return snd_ctl_elem_add(file, &info, replace);
  919. }
  920. static int snd_ctl_elem_remove(struct snd_ctl_file *file,
  921. struct snd_ctl_elem_id __user *_id)
  922. {
  923. struct snd_ctl_elem_id id;
  924. int err;
  925. if (copy_from_user(&id, _id, sizeof(id)))
  926. return -EFAULT;
  927. err = snd_ctl_remove_unlocked_id(file, &id);
  928. if (! err) {
  929. struct snd_card *card = file->card;
  930. down_write(&card->controls_rwsem);
  931. card->user_ctl_count--;
  932. up_write(&card->controls_rwsem);
  933. }
  934. return err;
  935. }
  936. static int snd_ctl_subscribe_events(struct snd_ctl_file *file, int __user *ptr)
  937. {
  938. int subscribe;
  939. if (get_user(subscribe, ptr))
  940. return -EFAULT;
  941. if (subscribe < 0) {
  942. subscribe = file->subscribed;
  943. if (put_user(subscribe, ptr))
  944. return -EFAULT;
  945. return 0;
  946. }
  947. if (subscribe) {
  948. file->subscribed = 1;
  949. return 0;
  950. } else if (file->subscribed) {
  951. snd_ctl_empty_read_queue(file);
  952. file->subscribed = 0;
  953. }
  954. return 0;
  955. }
  956. static long snd_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  957. {
  958. struct snd_ctl_file *ctl;
  959. struct snd_card *card;
  960. struct list_head *list;
  961. struct snd_kctl_ioctl *p;
  962. void __user *argp = (void __user *)arg;
  963. int __user *ip = argp;
  964. int err;
  965. ctl = file->private_data;
  966. card = ctl->card;
  967. snd_assert(card != NULL, return -ENXIO);
  968. switch (cmd) {
  969. case SNDRV_CTL_IOCTL_PVERSION:
  970. return put_user(SNDRV_CTL_VERSION, ip) ? -EFAULT : 0;
  971. case SNDRV_CTL_IOCTL_CARD_INFO:
  972. return snd_ctl_card_info(card, ctl, cmd, argp);
  973. case SNDRV_CTL_IOCTL_ELEM_LIST:
  974. return snd_ctl_elem_list(ctl->card, argp);
  975. case SNDRV_CTL_IOCTL_ELEM_INFO:
  976. return snd_ctl_elem_info_user(ctl, argp);
  977. case SNDRV_CTL_IOCTL_ELEM_READ:
  978. return snd_ctl_elem_read_user(ctl->card, argp);
  979. case SNDRV_CTL_IOCTL_ELEM_WRITE:
  980. return snd_ctl_elem_write_user(ctl, argp);
  981. case SNDRV_CTL_IOCTL_ELEM_LOCK:
  982. return snd_ctl_elem_lock(ctl, argp);
  983. case SNDRV_CTL_IOCTL_ELEM_UNLOCK:
  984. return snd_ctl_elem_unlock(ctl, argp);
  985. case SNDRV_CTL_IOCTL_ELEM_ADD:
  986. return snd_ctl_elem_add_user(ctl, argp, 0);
  987. case SNDRV_CTL_IOCTL_ELEM_REPLACE:
  988. return snd_ctl_elem_add_user(ctl, argp, 1);
  989. case SNDRV_CTL_IOCTL_ELEM_REMOVE:
  990. return snd_ctl_elem_remove(ctl, argp);
  991. case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS:
  992. return snd_ctl_subscribe_events(ctl, ip);
  993. case SNDRV_CTL_IOCTL_POWER:
  994. return -ENOPROTOOPT;
  995. case SNDRV_CTL_IOCTL_POWER_STATE:
  996. #ifdef CONFIG_PM
  997. return put_user(card->power_state, ip) ? -EFAULT : 0;
  998. #else
  999. return put_user(SNDRV_CTL_POWER_D0, ip) ? -EFAULT : 0;
  1000. #endif
  1001. }
  1002. down_read(&snd_ioctl_rwsem);
  1003. list_for_each(list, &snd_control_ioctls) {
  1004. p = list_entry(list, struct snd_kctl_ioctl, list);
  1005. err = p->fioctl(card, ctl, cmd, arg);
  1006. if (err != -ENOIOCTLCMD) {
  1007. up_read(&snd_ioctl_rwsem);
  1008. return err;
  1009. }
  1010. }
  1011. up_read(&snd_ioctl_rwsem);
  1012. snd_printdd("unknown ioctl = 0x%x\n", cmd);
  1013. return -ENOTTY;
  1014. }
  1015. static ssize_t snd_ctl_read(struct file *file, char __user *buffer,
  1016. size_t count, loff_t * offset)
  1017. {
  1018. struct snd_ctl_file *ctl;
  1019. int err = 0;
  1020. ssize_t result = 0;
  1021. ctl = file->private_data;
  1022. snd_assert(ctl != NULL && ctl->card != NULL, return -ENXIO);
  1023. if (!ctl->subscribed)
  1024. return -EBADFD;
  1025. if (count < sizeof(struct snd_ctl_event))
  1026. return -EINVAL;
  1027. spin_lock_irq(&ctl->read_lock);
  1028. while (count >= sizeof(struct snd_ctl_event)) {
  1029. struct snd_ctl_event ev;
  1030. struct snd_kctl_event *kev;
  1031. while (list_empty(&ctl->events)) {
  1032. wait_queue_t wait;
  1033. if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
  1034. err = -EAGAIN;
  1035. goto __end_lock;
  1036. }
  1037. init_waitqueue_entry(&wait, current);
  1038. add_wait_queue(&ctl->change_sleep, &wait);
  1039. set_current_state(TASK_INTERRUPTIBLE);
  1040. spin_unlock_irq(&ctl->read_lock);
  1041. schedule();
  1042. remove_wait_queue(&ctl->change_sleep, &wait);
  1043. if (signal_pending(current))
  1044. return result > 0 ? result : -ERESTARTSYS;
  1045. spin_lock_irq(&ctl->read_lock);
  1046. }
  1047. kev = snd_kctl_event(ctl->events.next);
  1048. ev.type = SNDRV_CTL_EVENT_ELEM;
  1049. ev.data.elem.mask = kev->mask;
  1050. ev.data.elem.id = kev->id;
  1051. list_del(&kev->list);
  1052. spin_unlock_irq(&ctl->read_lock);
  1053. kfree(kev);
  1054. if (copy_to_user(buffer, &ev, sizeof(struct snd_ctl_event))) {
  1055. err = -EFAULT;
  1056. goto __end;
  1057. }
  1058. spin_lock_irq(&ctl->read_lock);
  1059. buffer += sizeof(struct snd_ctl_event);
  1060. count -= sizeof(struct snd_ctl_event);
  1061. result += sizeof(struct snd_ctl_event);
  1062. }
  1063. __end_lock:
  1064. spin_unlock_irq(&ctl->read_lock);
  1065. __end:
  1066. return result > 0 ? result : err;
  1067. }
  1068. static unsigned int snd_ctl_poll(struct file *file, poll_table * wait)
  1069. {
  1070. unsigned int mask;
  1071. struct snd_ctl_file *ctl;
  1072. ctl = file->private_data;
  1073. if (!ctl->subscribed)
  1074. return 0;
  1075. poll_wait(file, &ctl->change_sleep, wait);
  1076. mask = 0;
  1077. if (!list_empty(&ctl->events))
  1078. mask |= POLLIN | POLLRDNORM;
  1079. return mask;
  1080. }
  1081. /*
  1082. * register the device-specific control-ioctls.
  1083. * called from each device manager like pcm.c, hwdep.c, etc.
  1084. */
  1085. static int _snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn, struct list_head *lists)
  1086. {
  1087. struct snd_kctl_ioctl *pn;
  1088. pn = kzalloc(sizeof(struct snd_kctl_ioctl), GFP_KERNEL);
  1089. if (pn == NULL)
  1090. return -ENOMEM;
  1091. pn->fioctl = fcn;
  1092. down_write(&snd_ioctl_rwsem);
  1093. list_add_tail(&pn->list, lists);
  1094. up_write(&snd_ioctl_rwsem);
  1095. return 0;
  1096. }
  1097. int snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn)
  1098. {
  1099. return _snd_ctl_register_ioctl(fcn, &snd_control_ioctls);
  1100. }
  1101. #ifdef CONFIG_COMPAT
  1102. int snd_ctl_register_ioctl_compat(snd_kctl_ioctl_func_t fcn)
  1103. {
  1104. return _snd_ctl_register_ioctl(fcn, &snd_control_compat_ioctls);
  1105. }
  1106. #endif
  1107. /*
  1108. * de-register the device-specific control-ioctls.
  1109. */
  1110. static int _snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn,
  1111. struct list_head *lists)
  1112. {
  1113. struct list_head *list;
  1114. struct snd_kctl_ioctl *p;
  1115. snd_assert(fcn != NULL, return -EINVAL);
  1116. down_write(&snd_ioctl_rwsem);
  1117. list_for_each(list, lists) {
  1118. p = list_entry(list, struct snd_kctl_ioctl, list);
  1119. if (p->fioctl == fcn) {
  1120. list_del(&p->list);
  1121. up_write(&snd_ioctl_rwsem);
  1122. kfree(p);
  1123. return 0;
  1124. }
  1125. }
  1126. up_write(&snd_ioctl_rwsem);
  1127. snd_BUG();
  1128. return -EINVAL;
  1129. }
  1130. int snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn)
  1131. {
  1132. return _snd_ctl_unregister_ioctl(fcn, &snd_control_ioctls);
  1133. }
  1134. #ifdef CONFIG_COMPAT
  1135. int snd_ctl_unregister_ioctl_compat(snd_kctl_ioctl_func_t fcn)
  1136. {
  1137. return _snd_ctl_unregister_ioctl(fcn, &snd_control_compat_ioctls);
  1138. }
  1139. #endif
  1140. static int snd_ctl_fasync(int fd, struct file * file, int on)
  1141. {
  1142. struct snd_ctl_file *ctl;
  1143. int err;
  1144. ctl = file->private_data;
  1145. err = fasync_helper(fd, file, on, &ctl->fasync);
  1146. if (err < 0)
  1147. return err;
  1148. return 0;
  1149. }
  1150. /*
  1151. * ioctl32 compat
  1152. */
  1153. #ifdef CONFIG_COMPAT
  1154. #include "control_compat.c"
  1155. #else
  1156. #define snd_ctl_ioctl_compat NULL
  1157. #endif
  1158. /*
  1159. * INIT PART
  1160. */
  1161. static struct file_operations snd_ctl_f_ops =
  1162. {
  1163. .owner = THIS_MODULE,
  1164. .read = snd_ctl_read,
  1165. .open = snd_ctl_open,
  1166. .release = snd_ctl_release,
  1167. .poll = snd_ctl_poll,
  1168. .unlocked_ioctl = snd_ctl_ioctl,
  1169. .compat_ioctl = snd_ctl_ioctl_compat,
  1170. .fasync = snd_ctl_fasync,
  1171. };
  1172. /*
  1173. * registration of the control device
  1174. */
  1175. static int snd_ctl_dev_register(struct snd_device *device)
  1176. {
  1177. struct snd_card *card = device->device_data;
  1178. int err, cardnum;
  1179. char name[16];
  1180. snd_assert(card != NULL, return -ENXIO);
  1181. cardnum = card->number;
  1182. snd_assert(cardnum >= 0 && cardnum < SNDRV_CARDS, return -ENXIO);
  1183. sprintf(name, "controlC%i", cardnum);
  1184. if ((err = snd_register_device(SNDRV_DEVICE_TYPE_CONTROL, card, -1,
  1185. &snd_ctl_f_ops, card, name)) < 0)
  1186. return err;
  1187. return 0;
  1188. }
  1189. /*
  1190. * disconnection of the control device
  1191. */
  1192. static int snd_ctl_dev_disconnect(struct snd_device *device)
  1193. {
  1194. struct snd_card *card = device->device_data;
  1195. struct list_head *flist;
  1196. struct snd_ctl_file *ctl;
  1197. down_read(&card->controls_rwsem);
  1198. list_for_each(flist, &card->ctl_files) {
  1199. ctl = snd_ctl_file(flist);
  1200. wake_up(&ctl->change_sleep);
  1201. kill_fasync(&ctl->fasync, SIGIO, POLL_ERR);
  1202. }
  1203. up_read(&card->controls_rwsem);
  1204. return 0;
  1205. }
  1206. /*
  1207. * free all controls
  1208. */
  1209. static int snd_ctl_dev_free(struct snd_device *device)
  1210. {
  1211. struct snd_card *card = device->device_data;
  1212. struct snd_kcontrol *control;
  1213. down_write(&card->controls_rwsem);
  1214. while (!list_empty(&card->controls)) {
  1215. control = snd_kcontrol(card->controls.next);
  1216. snd_ctl_remove(card, control);
  1217. }
  1218. up_write(&card->controls_rwsem);
  1219. return 0;
  1220. }
  1221. /*
  1222. * de-registration of the control device
  1223. */
  1224. static int snd_ctl_dev_unregister(struct snd_device *device)
  1225. {
  1226. struct snd_card *card = device->device_data;
  1227. int err, cardnum;
  1228. snd_assert(card != NULL, return -ENXIO);
  1229. cardnum = card->number;
  1230. snd_assert(cardnum >= 0 && cardnum < SNDRV_CARDS, return -ENXIO);
  1231. if ((err = snd_unregister_device(SNDRV_DEVICE_TYPE_CONTROL,
  1232. card, -1)) < 0)
  1233. return err;
  1234. return snd_ctl_dev_free(device);
  1235. }
  1236. /*
  1237. * create control core:
  1238. * called from init.c
  1239. */
  1240. int snd_ctl_create(struct snd_card *card)
  1241. {
  1242. static struct snd_device_ops ops = {
  1243. .dev_free = snd_ctl_dev_free,
  1244. .dev_register = snd_ctl_dev_register,
  1245. .dev_disconnect = snd_ctl_dev_disconnect,
  1246. .dev_unregister = snd_ctl_dev_unregister
  1247. };
  1248. snd_assert(card != NULL, return -ENXIO);
  1249. return snd_device_new(card, SNDRV_DEV_CONTROL, card, &ops);
  1250. }