control.c 47 KB

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