control.c 50 KB

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