control.c 39 KB

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