control.c 50 KB

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