control.c 51 KB

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