control.c 44 KB

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