control.c 45 KB

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