control.c 44 KB

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