control.c 50 KB

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