control.c 44 KB

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