control.c 41 KB

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