control.c 36 KB

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