control.c 45 KB

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