control.c 47 KB

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