control.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808
  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. access = info->access == 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE :
  1078. (info->access & (SNDRV_CTL_ELEM_ACCESS_READWRITE|
  1079. SNDRV_CTL_ELEM_ACCESS_INACTIVE|
  1080. SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE));
  1081. info->id.numid = 0;
  1082. memset(&kctl, 0, sizeof(kctl));
  1083. if (replace) {
  1084. err = snd_ctl_remove_user_ctl(file, &info->id);
  1085. if (err)
  1086. return err;
  1087. }
  1088. if (card->user_ctl_count >= MAX_USER_CONTROLS)
  1089. return -ENOMEM;
  1090. memcpy(&kctl.id, &info->id, sizeof(info->id));
  1091. kctl.count = info->owner ? info->owner : 1;
  1092. access |= SNDRV_CTL_ELEM_ACCESS_USER;
  1093. if (info->type == SNDRV_CTL_ELEM_TYPE_ENUMERATED)
  1094. kctl.info = snd_ctl_elem_user_enum_info;
  1095. else
  1096. kctl.info = snd_ctl_elem_user_info;
  1097. if (access & SNDRV_CTL_ELEM_ACCESS_READ)
  1098. kctl.get = snd_ctl_elem_user_get;
  1099. if (access & SNDRV_CTL_ELEM_ACCESS_WRITE)
  1100. kctl.put = snd_ctl_elem_user_put;
  1101. if (access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE) {
  1102. kctl.tlv.c = snd_ctl_elem_user_tlv;
  1103. access |= SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
  1104. }
  1105. switch (info->type) {
  1106. case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
  1107. case SNDRV_CTL_ELEM_TYPE_INTEGER:
  1108. private_size = sizeof(long);
  1109. if (info->count > 128)
  1110. return -EINVAL;
  1111. break;
  1112. case SNDRV_CTL_ELEM_TYPE_INTEGER64:
  1113. private_size = sizeof(long long);
  1114. if (info->count > 64)
  1115. return -EINVAL;
  1116. break;
  1117. case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
  1118. private_size = sizeof(unsigned int);
  1119. if (info->count > 128 || info->value.enumerated.items == 0)
  1120. return -EINVAL;
  1121. break;
  1122. case SNDRV_CTL_ELEM_TYPE_BYTES:
  1123. private_size = sizeof(unsigned char);
  1124. if (info->count > 512)
  1125. return -EINVAL;
  1126. break;
  1127. case SNDRV_CTL_ELEM_TYPE_IEC958:
  1128. private_size = sizeof(struct snd_aes_iec958);
  1129. if (info->count != 1)
  1130. return -EINVAL;
  1131. break;
  1132. default:
  1133. return -EINVAL;
  1134. }
  1135. private_size *= info->count;
  1136. ue = kzalloc(sizeof(struct user_element) + private_size, GFP_KERNEL);
  1137. if (ue == NULL)
  1138. return -ENOMEM;
  1139. ue->card = card;
  1140. ue->info = *info;
  1141. ue->info.access = 0;
  1142. ue->elem_data = (char *)ue + sizeof(*ue);
  1143. ue->elem_data_size = private_size;
  1144. if (ue->info.type == SNDRV_CTL_ELEM_TYPE_ENUMERATED) {
  1145. err = snd_ctl_elem_init_enum_names(ue);
  1146. if (err < 0) {
  1147. kfree(ue);
  1148. return err;
  1149. }
  1150. }
  1151. kctl.private_free = snd_ctl_elem_user_free;
  1152. _kctl = snd_ctl_new(&kctl, access);
  1153. if (_kctl == NULL) {
  1154. kfree(ue->priv_data);
  1155. kfree(ue);
  1156. return -ENOMEM;
  1157. }
  1158. _kctl->private_data = ue;
  1159. for (idx = 0; idx < _kctl->count; idx++)
  1160. _kctl->vd[idx].owner = file;
  1161. err = snd_ctl_add(card, _kctl);
  1162. if (err < 0)
  1163. return err;
  1164. down_write(&card->controls_rwsem);
  1165. card->user_ctl_count++;
  1166. up_write(&card->controls_rwsem);
  1167. return 0;
  1168. }
  1169. static int snd_ctl_elem_add_user(struct snd_ctl_file *file,
  1170. struct snd_ctl_elem_info __user *_info, int replace)
  1171. {
  1172. struct snd_ctl_elem_info info;
  1173. if (copy_from_user(&info, _info, sizeof(info)))
  1174. return -EFAULT;
  1175. return snd_ctl_elem_add(file, &info, replace);
  1176. }
  1177. static int snd_ctl_elem_remove(struct snd_ctl_file *file,
  1178. struct snd_ctl_elem_id __user *_id)
  1179. {
  1180. struct snd_ctl_elem_id id;
  1181. if (copy_from_user(&id, _id, sizeof(id)))
  1182. return -EFAULT;
  1183. return snd_ctl_remove_user_ctl(file, &id);
  1184. }
  1185. static int snd_ctl_subscribe_events(struct snd_ctl_file *file, int __user *ptr)
  1186. {
  1187. int subscribe;
  1188. if (get_user(subscribe, ptr))
  1189. return -EFAULT;
  1190. if (subscribe < 0) {
  1191. subscribe = file->subscribed;
  1192. if (put_user(subscribe, ptr))
  1193. return -EFAULT;
  1194. return 0;
  1195. }
  1196. if (subscribe) {
  1197. file->subscribed = 1;
  1198. return 0;
  1199. } else if (file->subscribed) {
  1200. snd_ctl_empty_read_queue(file);
  1201. file->subscribed = 0;
  1202. }
  1203. return 0;
  1204. }
  1205. static int snd_ctl_tlv_ioctl(struct snd_ctl_file *file,
  1206. struct snd_ctl_tlv __user *_tlv,
  1207. int op_flag)
  1208. {
  1209. struct snd_card *card = file->card;
  1210. struct snd_ctl_tlv tlv;
  1211. struct snd_kcontrol *kctl;
  1212. struct snd_kcontrol_volatile *vd;
  1213. unsigned int len;
  1214. int err = 0;
  1215. if (copy_from_user(&tlv, _tlv, sizeof(tlv)))
  1216. return -EFAULT;
  1217. if (tlv.length < sizeof(unsigned int) * 2)
  1218. return -EINVAL;
  1219. down_read(&card->controls_rwsem);
  1220. kctl = snd_ctl_find_numid(card, tlv.numid);
  1221. if (kctl == NULL) {
  1222. err = -ENOENT;
  1223. goto __kctl_end;
  1224. }
  1225. if (kctl->tlv.p == NULL) {
  1226. err = -ENXIO;
  1227. goto __kctl_end;
  1228. }
  1229. vd = &kctl->vd[tlv.numid - kctl->id.numid];
  1230. if ((op_flag == 0 && (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_READ) == 0) ||
  1231. (op_flag > 0 && (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE) == 0) ||
  1232. (op_flag < 0 && (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND) == 0)) {
  1233. err = -ENXIO;
  1234. goto __kctl_end;
  1235. }
  1236. if (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
  1237. if (vd->owner != NULL && vd->owner != file) {
  1238. err = -EPERM;
  1239. goto __kctl_end;
  1240. }
  1241. err = kctl->tlv.c(kctl, op_flag, tlv.length, _tlv->tlv);
  1242. if (err > 0) {
  1243. struct snd_ctl_elem_id id = kctl->id;
  1244. up_read(&card->controls_rwsem);
  1245. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_TLV, &id);
  1246. return 0;
  1247. }
  1248. } else {
  1249. if (op_flag) {
  1250. err = -ENXIO;
  1251. goto __kctl_end;
  1252. }
  1253. len = kctl->tlv.p[1] + 2 * sizeof(unsigned int);
  1254. if (tlv.length < len) {
  1255. err = -ENOMEM;
  1256. goto __kctl_end;
  1257. }
  1258. if (copy_to_user(_tlv->tlv, kctl->tlv.p, len))
  1259. err = -EFAULT;
  1260. }
  1261. __kctl_end:
  1262. up_read(&card->controls_rwsem);
  1263. return err;
  1264. }
  1265. static long snd_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  1266. {
  1267. struct snd_ctl_file *ctl;
  1268. struct snd_card *card;
  1269. struct snd_kctl_ioctl *p;
  1270. void __user *argp = (void __user *)arg;
  1271. int __user *ip = argp;
  1272. int err;
  1273. ctl = file->private_data;
  1274. card = ctl->card;
  1275. if (snd_BUG_ON(!card))
  1276. return -ENXIO;
  1277. switch (cmd) {
  1278. case SNDRV_CTL_IOCTL_PVERSION:
  1279. return put_user(SNDRV_CTL_VERSION, ip) ? -EFAULT : 0;
  1280. case SNDRV_CTL_IOCTL_CARD_INFO:
  1281. return snd_ctl_card_info(card, ctl, cmd, argp);
  1282. case SNDRV_CTL_IOCTL_ELEM_LIST:
  1283. return snd_ctl_elem_list(card, argp);
  1284. case SNDRV_CTL_IOCTL_ELEM_INFO:
  1285. return snd_ctl_elem_info_user(ctl, argp);
  1286. case SNDRV_CTL_IOCTL_ELEM_READ:
  1287. return snd_ctl_elem_read_user(card, argp);
  1288. case SNDRV_CTL_IOCTL_ELEM_WRITE:
  1289. return snd_ctl_elem_write_user(ctl, argp);
  1290. case SNDRV_CTL_IOCTL_ELEM_LOCK:
  1291. return snd_ctl_elem_lock(ctl, argp);
  1292. case SNDRV_CTL_IOCTL_ELEM_UNLOCK:
  1293. return snd_ctl_elem_unlock(ctl, argp);
  1294. case SNDRV_CTL_IOCTL_ELEM_ADD:
  1295. return snd_ctl_elem_add_user(ctl, argp, 0);
  1296. case SNDRV_CTL_IOCTL_ELEM_REPLACE:
  1297. return snd_ctl_elem_add_user(ctl, argp, 1);
  1298. case SNDRV_CTL_IOCTL_ELEM_REMOVE:
  1299. return snd_ctl_elem_remove(ctl, argp);
  1300. case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS:
  1301. return snd_ctl_subscribe_events(ctl, ip);
  1302. case SNDRV_CTL_IOCTL_TLV_READ:
  1303. return snd_ctl_tlv_ioctl(ctl, argp, SNDRV_CTL_TLV_OP_READ);
  1304. case SNDRV_CTL_IOCTL_TLV_WRITE:
  1305. return snd_ctl_tlv_ioctl(ctl, argp, SNDRV_CTL_TLV_OP_WRITE);
  1306. case SNDRV_CTL_IOCTL_TLV_COMMAND:
  1307. return snd_ctl_tlv_ioctl(ctl, argp, SNDRV_CTL_TLV_OP_CMD);
  1308. case SNDRV_CTL_IOCTL_POWER:
  1309. return -ENOPROTOOPT;
  1310. case SNDRV_CTL_IOCTL_POWER_STATE:
  1311. #ifdef CONFIG_PM
  1312. return put_user(card->power_state, ip) ? -EFAULT : 0;
  1313. #else
  1314. return put_user(SNDRV_CTL_POWER_D0, ip) ? -EFAULT : 0;
  1315. #endif
  1316. }
  1317. down_read(&snd_ioctl_rwsem);
  1318. list_for_each_entry(p, &snd_control_ioctls, list) {
  1319. err = p->fioctl(card, ctl, cmd, arg);
  1320. if (err != -ENOIOCTLCMD) {
  1321. up_read(&snd_ioctl_rwsem);
  1322. return err;
  1323. }
  1324. }
  1325. up_read(&snd_ioctl_rwsem);
  1326. dev_dbg(card->dev, "unknown ioctl = 0x%x\n", cmd);
  1327. return -ENOTTY;
  1328. }
  1329. static ssize_t snd_ctl_read(struct file *file, char __user *buffer,
  1330. size_t count, loff_t * offset)
  1331. {
  1332. struct snd_ctl_file *ctl;
  1333. int err = 0;
  1334. ssize_t result = 0;
  1335. ctl = file->private_data;
  1336. if (snd_BUG_ON(!ctl || !ctl->card))
  1337. return -ENXIO;
  1338. if (!ctl->subscribed)
  1339. return -EBADFD;
  1340. if (count < sizeof(struct snd_ctl_event))
  1341. return -EINVAL;
  1342. spin_lock_irq(&ctl->read_lock);
  1343. while (count >= sizeof(struct snd_ctl_event)) {
  1344. struct snd_ctl_event ev;
  1345. struct snd_kctl_event *kev;
  1346. while (list_empty(&ctl->events)) {
  1347. wait_queue_t wait;
  1348. if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
  1349. err = -EAGAIN;
  1350. goto __end_lock;
  1351. }
  1352. init_waitqueue_entry(&wait, current);
  1353. add_wait_queue(&ctl->change_sleep, &wait);
  1354. set_current_state(TASK_INTERRUPTIBLE);
  1355. spin_unlock_irq(&ctl->read_lock);
  1356. schedule();
  1357. remove_wait_queue(&ctl->change_sleep, &wait);
  1358. if (ctl->card->shutdown)
  1359. return -ENODEV;
  1360. if (signal_pending(current))
  1361. return -ERESTARTSYS;
  1362. spin_lock_irq(&ctl->read_lock);
  1363. }
  1364. kev = snd_kctl_event(ctl->events.next);
  1365. ev.type = SNDRV_CTL_EVENT_ELEM;
  1366. ev.data.elem.mask = kev->mask;
  1367. ev.data.elem.id = kev->id;
  1368. list_del(&kev->list);
  1369. spin_unlock_irq(&ctl->read_lock);
  1370. kfree(kev);
  1371. if (copy_to_user(buffer, &ev, sizeof(struct snd_ctl_event))) {
  1372. err = -EFAULT;
  1373. goto __end;
  1374. }
  1375. spin_lock_irq(&ctl->read_lock);
  1376. buffer += sizeof(struct snd_ctl_event);
  1377. count -= sizeof(struct snd_ctl_event);
  1378. result += sizeof(struct snd_ctl_event);
  1379. }
  1380. __end_lock:
  1381. spin_unlock_irq(&ctl->read_lock);
  1382. __end:
  1383. return result > 0 ? result : err;
  1384. }
  1385. static unsigned int snd_ctl_poll(struct file *file, poll_table * wait)
  1386. {
  1387. unsigned int mask;
  1388. struct snd_ctl_file *ctl;
  1389. ctl = file->private_data;
  1390. if (!ctl->subscribed)
  1391. return 0;
  1392. poll_wait(file, &ctl->change_sleep, wait);
  1393. mask = 0;
  1394. if (!list_empty(&ctl->events))
  1395. mask |= POLLIN | POLLRDNORM;
  1396. return mask;
  1397. }
  1398. /*
  1399. * register the device-specific control-ioctls.
  1400. * called from each device manager like pcm.c, hwdep.c, etc.
  1401. */
  1402. static int _snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn, struct list_head *lists)
  1403. {
  1404. struct snd_kctl_ioctl *pn;
  1405. pn = kzalloc(sizeof(struct snd_kctl_ioctl), GFP_KERNEL);
  1406. if (pn == NULL)
  1407. return -ENOMEM;
  1408. pn->fioctl = fcn;
  1409. down_write(&snd_ioctl_rwsem);
  1410. list_add_tail(&pn->list, lists);
  1411. up_write(&snd_ioctl_rwsem);
  1412. return 0;
  1413. }
  1414. /**
  1415. * snd_ctl_register_ioctl - register the device-specific control-ioctls
  1416. * @fcn: ioctl callback function
  1417. *
  1418. * called from each device manager like pcm.c, hwdep.c, etc.
  1419. */
  1420. int snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn)
  1421. {
  1422. return _snd_ctl_register_ioctl(fcn, &snd_control_ioctls);
  1423. }
  1424. EXPORT_SYMBOL(snd_ctl_register_ioctl);
  1425. #ifdef CONFIG_COMPAT
  1426. /**
  1427. * snd_ctl_register_ioctl_compat - register the device-specific 32bit compat
  1428. * control-ioctls
  1429. * @fcn: ioctl callback function
  1430. */
  1431. int snd_ctl_register_ioctl_compat(snd_kctl_ioctl_func_t fcn)
  1432. {
  1433. return _snd_ctl_register_ioctl(fcn, &snd_control_compat_ioctls);
  1434. }
  1435. EXPORT_SYMBOL(snd_ctl_register_ioctl_compat);
  1436. #endif
  1437. /*
  1438. * de-register the device-specific control-ioctls.
  1439. */
  1440. static int _snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn,
  1441. struct list_head *lists)
  1442. {
  1443. struct snd_kctl_ioctl *p;
  1444. if (snd_BUG_ON(!fcn))
  1445. return -EINVAL;
  1446. down_write(&snd_ioctl_rwsem);
  1447. list_for_each_entry(p, lists, list) {
  1448. if (p->fioctl == fcn) {
  1449. list_del(&p->list);
  1450. up_write(&snd_ioctl_rwsem);
  1451. kfree(p);
  1452. return 0;
  1453. }
  1454. }
  1455. up_write(&snd_ioctl_rwsem);
  1456. snd_BUG();
  1457. return -EINVAL;
  1458. }
  1459. /**
  1460. * snd_ctl_unregister_ioctl - de-register the device-specific control-ioctls
  1461. * @fcn: ioctl callback function to unregister
  1462. */
  1463. int snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn)
  1464. {
  1465. return _snd_ctl_unregister_ioctl(fcn, &snd_control_ioctls);
  1466. }
  1467. EXPORT_SYMBOL(snd_ctl_unregister_ioctl);
  1468. #ifdef CONFIG_COMPAT
  1469. /**
  1470. * snd_ctl_unregister_ioctl - de-register the device-specific compat 32bit
  1471. * control-ioctls
  1472. * @fcn: ioctl callback function to unregister
  1473. */
  1474. int snd_ctl_unregister_ioctl_compat(snd_kctl_ioctl_func_t fcn)
  1475. {
  1476. return _snd_ctl_unregister_ioctl(fcn, &snd_control_compat_ioctls);
  1477. }
  1478. EXPORT_SYMBOL(snd_ctl_unregister_ioctl_compat);
  1479. #endif
  1480. static int snd_ctl_fasync(int fd, struct file * file, int on)
  1481. {
  1482. struct snd_ctl_file *ctl;
  1483. ctl = file->private_data;
  1484. return fasync_helper(fd, file, on, &ctl->fasync);
  1485. }
  1486. /* return the preferred subdevice number if already assigned;
  1487. * otherwise return -1
  1488. */
  1489. int snd_ctl_get_preferred_subdevice(struct snd_card *card, int type)
  1490. {
  1491. struct snd_ctl_file *kctl;
  1492. int subdevice = -1;
  1493. read_lock(&card->ctl_files_rwlock);
  1494. list_for_each_entry(kctl, &card->ctl_files, list) {
  1495. if (kctl->pid == task_pid(current)) {
  1496. subdevice = kctl->preferred_subdevice[type];
  1497. if (subdevice != -1)
  1498. break;
  1499. }
  1500. }
  1501. read_unlock(&card->ctl_files_rwlock);
  1502. return subdevice;
  1503. }
  1504. EXPORT_SYMBOL_GPL(snd_ctl_get_preferred_subdevice);
  1505. /*
  1506. * ioctl32 compat
  1507. */
  1508. #ifdef CONFIG_COMPAT
  1509. #include "control_compat.c"
  1510. #else
  1511. #define snd_ctl_ioctl_compat NULL
  1512. #endif
  1513. /*
  1514. * INIT PART
  1515. */
  1516. static const struct file_operations snd_ctl_f_ops =
  1517. {
  1518. .owner = THIS_MODULE,
  1519. .read = snd_ctl_read,
  1520. .open = snd_ctl_open,
  1521. .release = snd_ctl_release,
  1522. .llseek = no_llseek,
  1523. .poll = snd_ctl_poll,
  1524. .unlocked_ioctl = snd_ctl_ioctl,
  1525. .compat_ioctl = snd_ctl_ioctl_compat,
  1526. .fasync = snd_ctl_fasync,
  1527. };
  1528. /*
  1529. * registration of the control device
  1530. */
  1531. static int snd_ctl_dev_register(struct snd_device *device)
  1532. {
  1533. struct snd_card *card = device->device_data;
  1534. return snd_register_device(SNDRV_DEVICE_TYPE_CONTROL, card, -1,
  1535. &snd_ctl_f_ops, card, &card->ctl_dev);
  1536. }
  1537. /*
  1538. * disconnection of the control device
  1539. */
  1540. static int snd_ctl_dev_disconnect(struct snd_device *device)
  1541. {
  1542. struct snd_card *card = device->device_data;
  1543. struct snd_ctl_file *ctl;
  1544. read_lock(&card->ctl_files_rwlock);
  1545. list_for_each_entry(ctl, &card->ctl_files, list) {
  1546. wake_up(&ctl->change_sleep);
  1547. kill_fasync(&ctl->fasync, SIGIO, POLL_ERR);
  1548. }
  1549. read_unlock(&card->ctl_files_rwlock);
  1550. return snd_unregister_device(&card->ctl_dev);
  1551. }
  1552. /*
  1553. * free all controls
  1554. */
  1555. static int snd_ctl_dev_free(struct snd_device *device)
  1556. {
  1557. struct snd_card *card = device->device_data;
  1558. struct snd_kcontrol *control;
  1559. down_write(&card->controls_rwsem);
  1560. while (!list_empty(&card->controls)) {
  1561. control = snd_kcontrol(card->controls.next);
  1562. snd_ctl_remove(card, control);
  1563. }
  1564. up_write(&card->controls_rwsem);
  1565. put_device(&card->ctl_dev);
  1566. return 0;
  1567. }
  1568. /*
  1569. * create control core:
  1570. * called from init.c
  1571. */
  1572. int snd_ctl_create(struct snd_card *card)
  1573. {
  1574. static struct snd_device_ops ops = {
  1575. .dev_free = snd_ctl_dev_free,
  1576. .dev_register = snd_ctl_dev_register,
  1577. .dev_disconnect = snd_ctl_dev_disconnect,
  1578. };
  1579. int err;
  1580. if (snd_BUG_ON(!card))
  1581. return -ENXIO;
  1582. if (snd_BUG_ON(card->number < 0 || card->number >= SNDRV_CARDS))
  1583. return -ENXIO;
  1584. snd_device_initialize(&card->ctl_dev, card);
  1585. dev_set_name(&card->ctl_dev, "controlC%d", card->number);
  1586. err = snd_device_new(card, SNDRV_DEV_CONTROL, card, &ops);
  1587. if (err < 0)
  1588. put_device(&card->ctl_dev);
  1589. return err;
  1590. }
  1591. /*
  1592. * Frequently used control callbacks/helpers
  1593. */
  1594. /**
  1595. * snd_ctl_boolean_mono_info - Helper function for a standard boolean info
  1596. * callback with a mono channel
  1597. * @kcontrol: the kcontrol instance
  1598. * @uinfo: info to store
  1599. *
  1600. * This is a function that can be used as info callback for a standard
  1601. * boolean control with a single mono channel.
  1602. */
  1603. int snd_ctl_boolean_mono_info(struct snd_kcontrol *kcontrol,
  1604. struct snd_ctl_elem_info *uinfo)
  1605. {
  1606. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1607. uinfo->count = 1;
  1608. uinfo->value.integer.min = 0;
  1609. uinfo->value.integer.max = 1;
  1610. return 0;
  1611. }
  1612. EXPORT_SYMBOL(snd_ctl_boolean_mono_info);
  1613. /**
  1614. * snd_ctl_boolean_stereo_info - Helper function for a standard boolean info
  1615. * callback with stereo two channels
  1616. * @kcontrol: the kcontrol instance
  1617. * @uinfo: info to store
  1618. *
  1619. * This is a function that can be used as info callback for a standard
  1620. * boolean control with stereo two channels.
  1621. */
  1622. int snd_ctl_boolean_stereo_info(struct snd_kcontrol *kcontrol,
  1623. struct snd_ctl_elem_info *uinfo)
  1624. {
  1625. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1626. uinfo->count = 2;
  1627. uinfo->value.integer.min = 0;
  1628. uinfo->value.integer.max = 1;
  1629. return 0;
  1630. }
  1631. EXPORT_SYMBOL(snd_ctl_boolean_stereo_info);
  1632. /**
  1633. * snd_ctl_enum_info - fills the info structure for an enumerated control
  1634. * @info: the structure to be filled
  1635. * @channels: the number of the control's channels; often one
  1636. * @items: the number of control values; also the size of @names
  1637. * @names: an array containing the names of all control values
  1638. *
  1639. * Sets all required fields in @info to their appropriate values.
  1640. * If the control's accessibility is not the default (readable and writable),
  1641. * the caller has to fill @info->access.
  1642. *
  1643. * Return: Zero.
  1644. */
  1645. int snd_ctl_enum_info(struct snd_ctl_elem_info *info, unsigned int channels,
  1646. unsigned int items, const char *const names[])
  1647. {
  1648. info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  1649. info->count = channels;
  1650. info->value.enumerated.items = items;
  1651. if (!items)
  1652. return 0;
  1653. if (info->value.enumerated.item >= items)
  1654. info->value.enumerated.item = items - 1;
  1655. WARN(strlen(names[info->value.enumerated.item]) >= sizeof(info->value.enumerated.name),
  1656. "ALSA: too long item name '%s'\n",
  1657. names[info->value.enumerated.item]);
  1658. strlcpy(info->value.enumerated.name,
  1659. names[info->value.enumerated.item],
  1660. sizeof(info->value.enumerated.name));
  1661. return 0;
  1662. }
  1663. EXPORT_SYMBOL(snd_ctl_enum_info);