control.c 39 KB

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