control.c 50 KB

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