control.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649
  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/slab.h>
  24. #include <linux/vmalloc.h>
  25. #include <linux/time.h>
  26. #include <sound/core.h>
  27. #include <sound/minors.h>
  28. #include <sound/info.h>
  29. #include <sound/control.h>
  30. /* max number of user-defined controls */
  31. #define MAX_USER_CONTROLS 32
  32. #define MAX_CONTROL_COUNT 1028
  33. struct snd_kctl_ioctl {
  34. struct list_head list; /* list of all ioctls */
  35. snd_kctl_ioctl_func_t fioctl;
  36. };
  37. static DECLARE_RWSEM(snd_ioctl_rwsem);
  38. static LIST_HEAD(snd_control_ioctls);
  39. #ifdef CONFIG_COMPAT
  40. static LIST_HEAD(snd_control_compat_ioctls);
  41. #endif
  42. static int snd_ctl_open(struct inode *inode, struct file *file)
  43. {
  44. unsigned long flags;
  45. struct snd_card *card;
  46. struct snd_ctl_file *ctl;
  47. int err;
  48. err = nonseekable_open(inode, file);
  49. if (err < 0)
  50. return err;
  51. card = snd_lookup_minor_data(iminor(inode), SNDRV_DEVICE_TYPE_CONTROL);
  52. if (!card) {
  53. err = -ENODEV;
  54. goto __error1;
  55. }
  56. err = snd_card_file_add(card, file);
  57. if (err < 0) {
  58. err = -ENODEV;
  59. goto __error1;
  60. }
  61. if (!try_module_get(card->module)) {
  62. err = -EFAULT;
  63. goto __error2;
  64. }
  65. ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
  66. if (ctl == NULL) {
  67. err = -ENOMEM;
  68. goto __error;
  69. }
  70. INIT_LIST_HEAD(&ctl->events);
  71. init_waitqueue_head(&ctl->change_sleep);
  72. spin_lock_init(&ctl->read_lock);
  73. ctl->card = card;
  74. ctl->prefer_pcm_subdevice = -1;
  75. ctl->prefer_rawmidi_subdevice = -1;
  76. ctl->pid = get_pid(task_pid(current));
  77. file->private_data = ctl;
  78. write_lock_irqsave(&card->ctl_files_rwlock, flags);
  79. list_add_tail(&ctl->list, &card->ctl_files);
  80. write_unlock_irqrestore(&card->ctl_files_rwlock, flags);
  81. return 0;
  82. __error:
  83. module_put(card->module);
  84. __error2:
  85. snd_card_file_remove(card, file);
  86. __error1:
  87. return err;
  88. }
  89. static void snd_ctl_empty_read_queue(struct snd_ctl_file * ctl)
  90. {
  91. unsigned long flags;
  92. struct snd_kctl_event *cread;
  93. spin_lock_irqsave(&ctl->read_lock, flags);
  94. while (!list_empty(&ctl->events)) {
  95. cread = snd_kctl_event(ctl->events.next);
  96. list_del(&cread->list);
  97. kfree(cread);
  98. }
  99. spin_unlock_irqrestore(&ctl->read_lock, flags);
  100. }
  101. static int snd_ctl_release(struct inode *inode, struct file *file)
  102. {
  103. unsigned long flags;
  104. struct snd_card *card;
  105. struct snd_ctl_file *ctl;
  106. struct snd_kcontrol *control;
  107. unsigned int idx;
  108. ctl = file->private_data;
  109. file->private_data = NULL;
  110. card = ctl->card;
  111. write_lock_irqsave(&card->ctl_files_rwlock, flags);
  112. list_del(&ctl->list);
  113. write_unlock_irqrestore(&card->ctl_files_rwlock, flags);
  114. down_write(&card->controls_rwsem);
  115. list_for_each_entry(control, &card->controls, list)
  116. for (idx = 0; idx < control->count; idx++)
  117. if (control->vd[idx].owner == ctl)
  118. control->vd[idx].owner = NULL;
  119. up_write(&card->controls_rwsem);
  120. snd_ctl_empty_read_queue(ctl);
  121. put_pid(ctl->pid);
  122. kfree(ctl);
  123. module_put(card->module);
  124. snd_card_file_remove(card, file);
  125. return 0;
  126. }
  127. void snd_ctl_notify(struct snd_card *card, unsigned int mask,
  128. struct snd_ctl_elem_id *id)
  129. {
  130. unsigned long flags;
  131. struct snd_ctl_file *ctl;
  132. struct snd_kctl_event *ev;
  133. if (snd_BUG_ON(!card || !id))
  134. return;
  135. read_lock(&card->ctl_files_rwlock);
  136. #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
  137. card->mixer_oss_change_count++;
  138. #endif
  139. list_for_each_entry(ctl, &card->ctl_files, list) {
  140. if (!ctl->subscribed)
  141. continue;
  142. spin_lock_irqsave(&ctl->read_lock, flags);
  143. list_for_each_entry(ev, &ctl->events, list) {
  144. if (ev->id.numid == id->numid) {
  145. ev->mask |= mask;
  146. goto _found;
  147. }
  148. }
  149. ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
  150. if (ev) {
  151. ev->id = *id;
  152. ev->mask = mask;
  153. list_add_tail(&ev->list, &ctl->events);
  154. } else {
  155. snd_printk(KERN_ERR "No memory available to allocate event\n");
  156. }
  157. _found:
  158. wake_up(&ctl->change_sleep);
  159. spin_unlock_irqrestore(&ctl->read_lock, flags);
  160. kill_fasync(&ctl->fasync, SIGIO, POLL_IN);
  161. }
  162. read_unlock(&card->ctl_files_rwlock);
  163. }
  164. EXPORT_SYMBOL(snd_ctl_notify);
  165. /**
  166. * snd_ctl_new - create a control instance from the template
  167. * @control: the control template
  168. * @access: the default control access
  169. *
  170. * Allocates a new struct snd_kcontrol instance and copies the given template
  171. * to the new instance. It does not copy volatile data (access).
  172. *
  173. * Returns the pointer of the new instance, or NULL on failure.
  174. */
  175. static struct snd_kcontrol *snd_ctl_new(struct snd_kcontrol *control,
  176. unsigned int access)
  177. {
  178. struct snd_kcontrol *kctl;
  179. unsigned int idx;
  180. if (snd_BUG_ON(!control || !control->count))
  181. return NULL;
  182. if (control->count > MAX_CONTROL_COUNT)
  183. return NULL;
  184. kctl = kzalloc(sizeof(*kctl) + sizeof(struct snd_kcontrol_volatile) * control->count, GFP_KERNEL);
  185. if (kctl == NULL) {
  186. snd_printk(KERN_ERR "Cannot allocate control instance\n");
  187. return NULL;
  188. }
  189. *kctl = *control;
  190. for (idx = 0; idx < kctl->count; idx++)
  191. kctl->vd[idx].access = access;
  192. return kctl;
  193. }
  194. /**
  195. * snd_ctl_new1 - create a control instance from the template
  196. * @ncontrol: the initialization record
  197. * @private_data: the private data to set
  198. *
  199. * Allocates a new struct snd_kcontrol instance and initialize from the given
  200. * template. When the access field of ncontrol is 0, it's assumed as
  201. * READWRITE access. When the count field is 0, it's assumes as one.
  202. *
  203. * Returns the pointer of the newly generated instance, or NULL on failure.
  204. */
  205. struct snd_kcontrol *snd_ctl_new1(const struct snd_kcontrol_new *ncontrol,
  206. void *private_data)
  207. {
  208. struct snd_kcontrol kctl;
  209. unsigned int access;
  210. if (snd_BUG_ON(!ncontrol || !ncontrol->info))
  211. return NULL;
  212. memset(&kctl, 0, sizeof(kctl));
  213. kctl.id.iface = ncontrol->iface;
  214. kctl.id.device = ncontrol->device;
  215. kctl.id.subdevice = ncontrol->subdevice;
  216. if (ncontrol->name) {
  217. strlcpy(kctl.id.name, ncontrol->name, sizeof(kctl.id.name));
  218. if (strcmp(ncontrol->name, kctl.id.name) != 0)
  219. snd_printk(KERN_WARNING
  220. "Control name '%s' truncated to '%s'\n",
  221. ncontrol->name, kctl.id.name);
  222. }
  223. kctl.id.index = ncontrol->index;
  224. kctl.count = ncontrol->count ? ncontrol->count : 1;
  225. access = ncontrol->access == 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE :
  226. (ncontrol->access & (SNDRV_CTL_ELEM_ACCESS_READWRITE|
  227. SNDRV_CTL_ELEM_ACCESS_INACTIVE|
  228. SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE|
  229. SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND|
  230. SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK));
  231. kctl.info = ncontrol->info;
  232. kctl.get = ncontrol->get;
  233. kctl.put = ncontrol->put;
  234. kctl.tlv.p = ncontrol->tlv.p;
  235. kctl.private_value = ncontrol->private_value;
  236. kctl.private_data = private_data;
  237. return snd_ctl_new(&kctl, access);
  238. }
  239. EXPORT_SYMBOL(snd_ctl_new1);
  240. /**
  241. * snd_ctl_free_one - release the control instance
  242. * @kcontrol: the control instance
  243. *
  244. * Releases the control instance created via snd_ctl_new()
  245. * or snd_ctl_new1().
  246. * Don't call this after the control was added to the card.
  247. */
  248. void snd_ctl_free_one(struct snd_kcontrol *kcontrol)
  249. {
  250. if (kcontrol) {
  251. if (kcontrol->private_free)
  252. kcontrol->private_free(kcontrol);
  253. kfree(kcontrol);
  254. }
  255. }
  256. EXPORT_SYMBOL(snd_ctl_free_one);
  257. static bool snd_ctl_remove_numid_conflict(struct snd_card *card,
  258. unsigned int count)
  259. {
  260. struct snd_kcontrol *kctl;
  261. list_for_each_entry(kctl, &card->controls, list) {
  262. if (kctl->id.numid < card->last_numid + 1 + count &&
  263. kctl->id.numid + kctl->count > card->last_numid + 1) {
  264. card->last_numid = kctl->id.numid + kctl->count - 1;
  265. return true;
  266. }
  267. }
  268. return false;
  269. }
  270. static int snd_ctl_find_hole(struct snd_card *card, unsigned int count)
  271. {
  272. unsigned int iter = 100000;
  273. while (snd_ctl_remove_numid_conflict(card, count)) {
  274. if (--iter == 0) {
  275. /* this situation is very unlikely */
  276. snd_printk(KERN_ERR "unable to allocate new control numid\n");
  277. return -ENOMEM;
  278. }
  279. }
  280. return 0;
  281. }
  282. /**
  283. * snd_ctl_add - add the control instance to the card
  284. * @card: the card instance
  285. * @kcontrol: the control instance to add
  286. *
  287. * Adds the control instance created via snd_ctl_new() or
  288. * snd_ctl_new1() to the given card. Assigns also an unique
  289. * numid used for fast search.
  290. *
  291. * Returns zero if successful, or a negative error code on failure.
  292. *
  293. * It frees automatically the control which cannot be added.
  294. */
  295. int snd_ctl_add(struct snd_card *card, struct snd_kcontrol *kcontrol)
  296. {
  297. struct snd_ctl_elem_id id;
  298. unsigned int idx;
  299. int err = -EINVAL;
  300. if (! kcontrol)
  301. return err;
  302. if (snd_BUG_ON(!card || !kcontrol->info))
  303. goto error;
  304. id = kcontrol->id;
  305. down_write(&card->controls_rwsem);
  306. if (snd_ctl_find_id(card, &id)) {
  307. up_write(&card->controls_rwsem);
  308. snd_printd(KERN_ERR "control %i:%i:%i:%s:%i is already present\n",
  309. id.iface,
  310. id.device,
  311. id.subdevice,
  312. id.name,
  313. id.index);
  314. err = -EBUSY;
  315. goto error;
  316. }
  317. if (snd_ctl_find_hole(card, kcontrol->count) < 0) {
  318. up_write(&card->controls_rwsem);
  319. err = -ENOMEM;
  320. goto error;
  321. }
  322. list_add_tail(&kcontrol->list, &card->controls);
  323. card->controls_count += kcontrol->count;
  324. kcontrol->id.numid = card->last_numid + 1;
  325. card->last_numid += kcontrol->count;
  326. up_write(&card->controls_rwsem);
  327. for (idx = 0; idx < kcontrol->count; idx++, id.index++, id.numid++)
  328. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_ADD, &id);
  329. return 0;
  330. error:
  331. snd_ctl_free_one(kcontrol);
  332. return err;
  333. }
  334. EXPORT_SYMBOL(snd_ctl_add);
  335. /**
  336. * snd_ctl_replace - replace the control instance of the card
  337. * @card: the card instance
  338. * @kcontrol: the control instance to replace
  339. * @add_on_replace: add the control if not already added
  340. *
  341. * Replaces the given control. If the given control does not exist
  342. * and the add_on_replace flag is set, the control is added. If the
  343. * control exists, it is destroyed first.
  344. *
  345. * Returns zero if successful, or a negative error code on failure.
  346. *
  347. * It frees automatically the control which cannot be added or replaced.
  348. */
  349. int snd_ctl_replace(struct snd_card *card, struct snd_kcontrol *kcontrol,
  350. bool add_on_replace)
  351. {
  352. struct snd_ctl_elem_id id;
  353. unsigned int idx;
  354. struct snd_kcontrol *old;
  355. int ret;
  356. if (!kcontrol)
  357. return -EINVAL;
  358. if (snd_BUG_ON(!card || !kcontrol->info)) {
  359. ret = -EINVAL;
  360. goto error;
  361. }
  362. id = kcontrol->id;
  363. down_write(&card->controls_rwsem);
  364. old = snd_ctl_find_id(card, &id);
  365. if (!old) {
  366. if (add_on_replace)
  367. goto add;
  368. up_write(&card->controls_rwsem);
  369. ret = -EINVAL;
  370. goto error;
  371. }
  372. ret = snd_ctl_remove(card, old);
  373. if (ret < 0) {
  374. up_write(&card->controls_rwsem);
  375. goto error;
  376. }
  377. add:
  378. if (snd_ctl_find_hole(card, kcontrol->count) < 0) {
  379. up_write(&card->controls_rwsem);
  380. ret = -ENOMEM;
  381. goto error;
  382. }
  383. list_add_tail(&kcontrol->list, &card->controls);
  384. card->controls_count += kcontrol->count;
  385. kcontrol->id.numid = card->last_numid + 1;
  386. card->last_numid += kcontrol->count;
  387. up_write(&card->controls_rwsem);
  388. for (idx = 0; idx < kcontrol->count; idx++, id.index++, id.numid++)
  389. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_ADD, &id);
  390. return 0;
  391. error:
  392. snd_ctl_free_one(kcontrol);
  393. return ret;
  394. }
  395. EXPORT_SYMBOL(snd_ctl_replace);
  396. /**
  397. * snd_ctl_remove - remove the control from the card and release it
  398. * @card: the card instance
  399. * @kcontrol: the control instance to remove
  400. *
  401. * Removes the control from the card and then releases the instance.
  402. * You don't need to call snd_ctl_free_one(). You must be in
  403. * the write lock - down_write(&card->controls_rwsem).
  404. *
  405. * Returns 0 if successful, or a negative error code on failure.
  406. */
  407. int snd_ctl_remove(struct snd_card *card, struct snd_kcontrol *kcontrol)
  408. {
  409. struct snd_ctl_elem_id id;
  410. unsigned int idx;
  411. if (snd_BUG_ON(!card || !kcontrol))
  412. return -EINVAL;
  413. list_del(&kcontrol->list);
  414. card->controls_count -= kcontrol->count;
  415. id = kcontrol->id;
  416. for (idx = 0; idx < kcontrol->count; idx++, id.index++, id.numid++)
  417. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_REMOVE, &id);
  418. snd_ctl_free_one(kcontrol);
  419. return 0;
  420. }
  421. EXPORT_SYMBOL(snd_ctl_remove);
  422. /**
  423. * snd_ctl_remove_id - remove the control of the given id and release it
  424. * @card: the card instance
  425. * @id: the control id to remove
  426. *
  427. * Finds the control instance with the given id, removes it from the
  428. * card list and releases it.
  429. *
  430. * Returns 0 if successful, or a negative error code on failure.
  431. */
  432. int snd_ctl_remove_id(struct snd_card *card, struct snd_ctl_elem_id *id)
  433. {
  434. struct snd_kcontrol *kctl;
  435. int ret;
  436. down_write(&card->controls_rwsem);
  437. kctl = snd_ctl_find_id(card, id);
  438. if (kctl == NULL) {
  439. up_write(&card->controls_rwsem);
  440. return -ENOENT;
  441. }
  442. ret = snd_ctl_remove(card, kctl);
  443. up_write(&card->controls_rwsem);
  444. return ret;
  445. }
  446. EXPORT_SYMBOL(snd_ctl_remove_id);
  447. /**
  448. * snd_ctl_remove_user_ctl - remove and release the unlocked user control
  449. * @file: active control handle
  450. * @id: the control id to remove
  451. *
  452. * Finds the control instance with the given id, removes it from the
  453. * card list and releases it.
  454. *
  455. * Returns 0 if successful, or a negative error code on failure.
  456. */
  457. static int snd_ctl_remove_user_ctl(struct snd_ctl_file * file,
  458. struct snd_ctl_elem_id *id)
  459. {
  460. struct snd_card *card = file->card;
  461. struct snd_kcontrol *kctl;
  462. int idx, ret;
  463. down_write(&card->controls_rwsem);
  464. kctl = snd_ctl_find_id(card, id);
  465. if (kctl == NULL) {
  466. ret = -ENOENT;
  467. goto error;
  468. }
  469. if (!(kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_USER)) {
  470. ret = -EINVAL;
  471. goto error;
  472. }
  473. for (idx = 0; idx < kctl->count; idx++)
  474. if (kctl->vd[idx].owner != NULL && kctl->vd[idx].owner != file) {
  475. ret = -EBUSY;
  476. goto error;
  477. }
  478. ret = snd_ctl_remove(card, kctl);
  479. if (ret < 0)
  480. goto error;
  481. card->user_ctl_count--;
  482. error:
  483. up_write(&card->controls_rwsem);
  484. return ret;
  485. }
  486. /**
  487. * snd_ctl_activate_id - activate/inactivate the control of the given id
  488. * @card: the card instance
  489. * @id: the control id to activate/inactivate
  490. * @active: non-zero to activate
  491. *
  492. * Finds the control instance with the given id, and activate or
  493. * inactivate the control together with notification, if changed.
  494. *
  495. * Returns 0 if unchanged, 1 if changed, or a negative error code on failure.
  496. */
  497. int snd_ctl_activate_id(struct snd_card *card, struct snd_ctl_elem_id *id,
  498. int active)
  499. {
  500. struct snd_kcontrol *kctl;
  501. struct snd_kcontrol_volatile *vd;
  502. unsigned int index_offset;
  503. int ret;
  504. down_write(&card->controls_rwsem);
  505. kctl = snd_ctl_find_id(card, id);
  506. if (kctl == NULL) {
  507. ret = -ENOENT;
  508. goto unlock;
  509. }
  510. index_offset = snd_ctl_get_ioff(kctl, &kctl->id);
  511. vd = &kctl->vd[index_offset];
  512. ret = 0;
  513. if (active) {
  514. if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_INACTIVE))
  515. goto unlock;
  516. vd->access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  517. } else {
  518. if (vd->access & SNDRV_CTL_ELEM_ACCESS_INACTIVE)
  519. goto unlock;
  520. vd->access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  521. }
  522. ret = 1;
  523. unlock:
  524. up_write(&card->controls_rwsem);
  525. if (ret > 0)
  526. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO, id);
  527. return ret;
  528. }
  529. EXPORT_SYMBOL_GPL(snd_ctl_activate_id);
  530. /**
  531. * snd_ctl_rename_id - replace the id of a control on the card
  532. * @card: the card instance
  533. * @src_id: the old id
  534. * @dst_id: the new id
  535. *
  536. * Finds the control with the old id from the card, and replaces the
  537. * id with the new one.
  538. *
  539. * Returns zero if successful, or a negative error code on failure.
  540. */
  541. int snd_ctl_rename_id(struct snd_card *card, struct snd_ctl_elem_id *src_id,
  542. struct snd_ctl_elem_id *dst_id)
  543. {
  544. struct snd_kcontrol *kctl;
  545. down_write(&card->controls_rwsem);
  546. kctl = snd_ctl_find_id(card, src_id);
  547. if (kctl == NULL) {
  548. up_write(&card->controls_rwsem);
  549. return -ENOENT;
  550. }
  551. kctl->id = *dst_id;
  552. kctl->id.numid = card->last_numid + 1;
  553. card->last_numid += kctl->count;
  554. up_write(&card->controls_rwsem);
  555. return 0;
  556. }
  557. EXPORT_SYMBOL(snd_ctl_rename_id);
  558. /**
  559. * snd_ctl_find_numid - find the control instance with the given number-id
  560. * @card: the card instance
  561. * @numid: the number-id to search
  562. *
  563. * Finds the control instance with the given number-id from the card.
  564. *
  565. * Returns the pointer of the instance if found, or NULL if not.
  566. *
  567. * The caller must down card->controls_rwsem before calling this function
  568. * (if the race condition can happen).
  569. */
  570. struct snd_kcontrol *snd_ctl_find_numid(struct snd_card *card, unsigned int numid)
  571. {
  572. struct snd_kcontrol *kctl;
  573. if (snd_BUG_ON(!card || !numid))
  574. return NULL;
  575. list_for_each_entry(kctl, &card->controls, list) {
  576. if (kctl->id.numid <= numid && kctl->id.numid + kctl->count > numid)
  577. return kctl;
  578. }
  579. return NULL;
  580. }
  581. EXPORT_SYMBOL(snd_ctl_find_numid);
  582. /**
  583. * snd_ctl_find_id - find the control instance with the given id
  584. * @card: the card instance
  585. * @id: the id to search
  586. *
  587. * Finds the control instance with the given id from the card.
  588. *
  589. * Returns the pointer of the instance if found, or NULL if not.
  590. *
  591. * The caller must down card->controls_rwsem before calling this function
  592. * (if the race condition can happen).
  593. */
  594. struct snd_kcontrol *snd_ctl_find_id(struct snd_card *card,
  595. struct snd_ctl_elem_id *id)
  596. {
  597. struct snd_kcontrol *kctl;
  598. if (snd_BUG_ON(!card || !id))
  599. return NULL;
  600. if (id->numid != 0)
  601. return snd_ctl_find_numid(card, id->numid);
  602. list_for_each_entry(kctl, &card->controls, list) {
  603. if (kctl->id.iface != id->iface)
  604. continue;
  605. if (kctl->id.device != id->device)
  606. continue;
  607. if (kctl->id.subdevice != id->subdevice)
  608. continue;
  609. if (strncmp(kctl->id.name, id->name, sizeof(kctl->id.name)))
  610. continue;
  611. if (kctl->id.index > id->index)
  612. continue;
  613. if (kctl->id.index + kctl->count <= id->index)
  614. continue;
  615. return kctl;
  616. }
  617. return NULL;
  618. }
  619. EXPORT_SYMBOL(snd_ctl_find_id);
  620. static int snd_ctl_card_info(struct snd_card *card, struct snd_ctl_file * ctl,
  621. unsigned int cmd, void __user *arg)
  622. {
  623. struct snd_ctl_card_info *info;
  624. info = kzalloc(sizeof(*info), GFP_KERNEL);
  625. if (! info)
  626. return -ENOMEM;
  627. down_read(&snd_ioctl_rwsem);
  628. info->card = card->number;
  629. strlcpy(info->id, card->id, sizeof(info->id));
  630. strlcpy(info->driver, card->driver, sizeof(info->driver));
  631. strlcpy(info->name, card->shortname, sizeof(info->name));
  632. strlcpy(info->longname, card->longname, sizeof(info->longname));
  633. strlcpy(info->mixername, card->mixername, sizeof(info->mixername));
  634. strlcpy(info->components, card->components, sizeof(info->components));
  635. up_read(&snd_ioctl_rwsem);
  636. if (copy_to_user(arg, info, sizeof(struct snd_ctl_card_info))) {
  637. kfree(info);
  638. return -EFAULT;
  639. }
  640. kfree(info);
  641. return 0;
  642. }
  643. static int snd_ctl_elem_list(struct snd_card *card,
  644. struct snd_ctl_elem_list __user *_list)
  645. {
  646. struct list_head *plist;
  647. struct snd_ctl_elem_list list;
  648. struct snd_kcontrol *kctl;
  649. struct snd_ctl_elem_id *dst, *id;
  650. unsigned int offset, space, first, jidx;
  651. if (copy_from_user(&list, _list, sizeof(list)))
  652. return -EFAULT;
  653. offset = list.offset;
  654. space = list.space;
  655. first = 0;
  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. unsigned long priv_data_size; /* size of private data in bytes */
  917. };
  918. static int snd_ctl_elem_user_info(struct snd_kcontrol *kcontrol,
  919. struct snd_ctl_elem_info *uinfo)
  920. {
  921. struct user_element *ue = kcontrol->private_data;
  922. *uinfo = ue->info;
  923. return 0;
  924. }
  925. static int snd_ctl_elem_user_get(struct snd_kcontrol *kcontrol,
  926. struct snd_ctl_elem_value *ucontrol)
  927. {
  928. struct user_element *ue = kcontrol->private_data;
  929. memcpy(&ucontrol->value, ue->elem_data, ue->elem_data_size);
  930. return 0;
  931. }
  932. static int snd_ctl_elem_user_put(struct snd_kcontrol *kcontrol,
  933. struct snd_ctl_elem_value *ucontrol)
  934. {
  935. int change;
  936. struct user_element *ue = kcontrol->private_data;
  937. change = memcmp(&ucontrol->value, ue->elem_data, ue->elem_data_size) != 0;
  938. if (change)
  939. memcpy(ue->elem_data, &ucontrol->value, ue->elem_data_size);
  940. return change;
  941. }
  942. static int snd_ctl_elem_user_tlv(struct snd_kcontrol *kcontrol,
  943. int op_flag,
  944. unsigned int size,
  945. unsigned int __user *tlv)
  946. {
  947. struct user_element *ue = kcontrol->private_data;
  948. int change = 0;
  949. void *new_data;
  950. if (op_flag > 0) {
  951. if (size > 1024 * 128) /* sane value */
  952. return -EINVAL;
  953. new_data = memdup_user(tlv, size);
  954. if (IS_ERR(new_data))
  955. return PTR_ERR(new_data);
  956. change = ue->tlv_data_size != size;
  957. if (!change)
  958. change = memcmp(ue->tlv_data, new_data, size);
  959. kfree(ue->tlv_data);
  960. ue->tlv_data = new_data;
  961. ue->tlv_data_size = size;
  962. } else {
  963. if (! ue->tlv_data_size || ! ue->tlv_data)
  964. return -ENXIO;
  965. if (size < ue->tlv_data_size)
  966. return -ENOSPC;
  967. if (copy_to_user(tlv, ue->tlv_data, ue->tlv_data_size))
  968. return -EFAULT;
  969. }
  970. return change;
  971. }
  972. static void snd_ctl_elem_user_free(struct snd_kcontrol *kcontrol)
  973. {
  974. struct user_element *ue = kcontrol->private_data;
  975. if (ue->tlv_data)
  976. kfree(ue->tlv_data);
  977. kfree(ue);
  978. }
  979. static int snd_ctl_elem_add(struct snd_ctl_file *file,
  980. struct snd_ctl_elem_info *info, int replace)
  981. {
  982. struct snd_card *card = file->card;
  983. struct snd_kcontrol kctl, *_kctl;
  984. unsigned int access;
  985. long private_size;
  986. struct user_element *ue;
  987. int idx, err;
  988. if (card->user_ctl_count >= MAX_USER_CONTROLS)
  989. return -ENOMEM;
  990. if (info->count < 1)
  991. return -EINVAL;
  992. access = info->access == 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE :
  993. (info->access & (SNDRV_CTL_ELEM_ACCESS_READWRITE|
  994. SNDRV_CTL_ELEM_ACCESS_INACTIVE|
  995. SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE));
  996. info->id.numid = 0;
  997. memset(&kctl, 0, sizeof(kctl));
  998. down_write(&card->controls_rwsem);
  999. _kctl = snd_ctl_find_id(card, &info->id);
  1000. err = 0;
  1001. if (_kctl) {
  1002. if (replace)
  1003. err = snd_ctl_remove(card, _kctl);
  1004. else
  1005. err = -EBUSY;
  1006. } else {
  1007. if (replace)
  1008. err = -ENOENT;
  1009. }
  1010. up_write(&card->controls_rwsem);
  1011. if (err < 0)
  1012. return err;
  1013. memcpy(&kctl.id, &info->id, sizeof(info->id));
  1014. kctl.count = info->owner ? info->owner : 1;
  1015. access |= SNDRV_CTL_ELEM_ACCESS_USER;
  1016. kctl.info = snd_ctl_elem_user_info;
  1017. if (access & SNDRV_CTL_ELEM_ACCESS_READ)
  1018. kctl.get = snd_ctl_elem_user_get;
  1019. if (access & SNDRV_CTL_ELEM_ACCESS_WRITE)
  1020. kctl.put = snd_ctl_elem_user_put;
  1021. if (access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE) {
  1022. kctl.tlv.c = snd_ctl_elem_user_tlv;
  1023. access |= SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
  1024. }
  1025. switch (info->type) {
  1026. case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
  1027. case SNDRV_CTL_ELEM_TYPE_INTEGER:
  1028. private_size = sizeof(long);
  1029. if (info->count > 128)
  1030. return -EINVAL;
  1031. break;
  1032. case SNDRV_CTL_ELEM_TYPE_INTEGER64:
  1033. private_size = sizeof(long long);
  1034. if (info->count > 64)
  1035. return -EINVAL;
  1036. break;
  1037. case SNDRV_CTL_ELEM_TYPE_BYTES:
  1038. private_size = sizeof(unsigned char);
  1039. if (info->count > 512)
  1040. return -EINVAL;
  1041. break;
  1042. case SNDRV_CTL_ELEM_TYPE_IEC958:
  1043. private_size = sizeof(struct snd_aes_iec958);
  1044. if (info->count != 1)
  1045. return -EINVAL;
  1046. break;
  1047. default:
  1048. return -EINVAL;
  1049. }
  1050. private_size *= info->count;
  1051. ue = kzalloc(sizeof(struct user_element) + private_size, GFP_KERNEL);
  1052. if (ue == NULL)
  1053. return -ENOMEM;
  1054. ue->info = *info;
  1055. ue->info.access = 0;
  1056. ue->elem_data = (char *)ue + sizeof(*ue);
  1057. ue->elem_data_size = private_size;
  1058. kctl.private_free = snd_ctl_elem_user_free;
  1059. _kctl = snd_ctl_new(&kctl, access);
  1060. if (_kctl == NULL) {
  1061. kfree(ue);
  1062. return -ENOMEM;
  1063. }
  1064. _kctl->private_data = ue;
  1065. for (idx = 0; idx < _kctl->count; idx++)
  1066. _kctl->vd[idx].owner = file;
  1067. err = snd_ctl_add(card, _kctl);
  1068. if (err < 0)
  1069. return err;
  1070. down_write(&card->controls_rwsem);
  1071. card->user_ctl_count++;
  1072. up_write(&card->controls_rwsem);
  1073. return 0;
  1074. }
  1075. static int snd_ctl_elem_add_user(struct snd_ctl_file *file,
  1076. struct snd_ctl_elem_info __user *_info, int replace)
  1077. {
  1078. struct snd_ctl_elem_info info;
  1079. if (copy_from_user(&info, _info, sizeof(info)))
  1080. return -EFAULT;
  1081. return snd_ctl_elem_add(file, &info, replace);
  1082. }
  1083. static int snd_ctl_elem_remove(struct snd_ctl_file *file,
  1084. struct snd_ctl_elem_id __user *_id)
  1085. {
  1086. struct snd_ctl_elem_id id;
  1087. if (copy_from_user(&id, _id, sizeof(id)))
  1088. return -EFAULT;
  1089. return snd_ctl_remove_user_ctl(file, &id);
  1090. }
  1091. static int snd_ctl_subscribe_events(struct snd_ctl_file *file, int __user *ptr)
  1092. {
  1093. int subscribe;
  1094. if (get_user(subscribe, ptr))
  1095. return -EFAULT;
  1096. if (subscribe < 0) {
  1097. subscribe = file->subscribed;
  1098. if (put_user(subscribe, ptr))
  1099. return -EFAULT;
  1100. return 0;
  1101. }
  1102. if (subscribe) {
  1103. file->subscribed = 1;
  1104. return 0;
  1105. } else if (file->subscribed) {
  1106. snd_ctl_empty_read_queue(file);
  1107. file->subscribed = 0;
  1108. }
  1109. return 0;
  1110. }
  1111. static int snd_ctl_tlv_ioctl(struct snd_ctl_file *file,
  1112. struct snd_ctl_tlv __user *_tlv,
  1113. int op_flag)
  1114. {
  1115. struct snd_card *card = file->card;
  1116. struct snd_ctl_tlv tlv;
  1117. struct snd_kcontrol *kctl;
  1118. struct snd_kcontrol_volatile *vd;
  1119. unsigned int len;
  1120. int err = 0;
  1121. if (copy_from_user(&tlv, _tlv, sizeof(tlv)))
  1122. return -EFAULT;
  1123. if (tlv.length < sizeof(unsigned int) * 2)
  1124. return -EINVAL;
  1125. down_read(&card->controls_rwsem);
  1126. kctl = snd_ctl_find_numid(card, tlv.numid);
  1127. if (kctl == NULL) {
  1128. err = -ENOENT;
  1129. goto __kctl_end;
  1130. }
  1131. if (kctl->tlv.p == NULL) {
  1132. err = -ENXIO;
  1133. goto __kctl_end;
  1134. }
  1135. vd = &kctl->vd[tlv.numid - kctl->id.numid];
  1136. if ((op_flag == 0 && (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_READ) == 0) ||
  1137. (op_flag > 0 && (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE) == 0) ||
  1138. (op_flag < 0 && (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND) == 0)) {
  1139. err = -ENXIO;
  1140. goto __kctl_end;
  1141. }
  1142. if (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
  1143. if (vd->owner != NULL && vd->owner != file) {
  1144. err = -EPERM;
  1145. goto __kctl_end;
  1146. }
  1147. err = kctl->tlv.c(kctl, op_flag, tlv.length, _tlv->tlv);
  1148. if (err > 0) {
  1149. up_read(&card->controls_rwsem);
  1150. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_TLV, &kctl->id);
  1151. return 0;
  1152. }
  1153. } else {
  1154. if (op_flag) {
  1155. err = -ENXIO;
  1156. goto __kctl_end;
  1157. }
  1158. len = kctl->tlv.p[1] + 2 * sizeof(unsigned int);
  1159. if (tlv.length < len) {
  1160. err = -ENOMEM;
  1161. goto __kctl_end;
  1162. }
  1163. if (copy_to_user(_tlv->tlv, kctl->tlv.p, len))
  1164. err = -EFAULT;
  1165. }
  1166. __kctl_end:
  1167. up_read(&card->controls_rwsem);
  1168. return err;
  1169. }
  1170. static long snd_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  1171. {
  1172. struct snd_ctl_file *ctl;
  1173. struct snd_card *card;
  1174. struct snd_kctl_ioctl *p;
  1175. void __user *argp = (void __user *)arg;
  1176. int __user *ip = argp;
  1177. int err;
  1178. ctl = file->private_data;
  1179. card = ctl->card;
  1180. if (snd_BUG_ON(!card))
  1181. return -ENXIO;
  1182. switch (cmd) {
  1183. case SNDRV_CTL_IOCTL_PVERSION:
  1184. return put_user(SNDRV_CTL_VERSION, ip) ? -EFAULT : 0;
  1185. case SNDRV_CTL_IOCTL_CARD_INFO:
  1186. return snd_ctl_card_info(card, ctl, cmd, argp);
  1187. case SNDRV_CTL_IOCTL_ELEM_LIST:
  1188. return snd_ctl_elem_list(card, argp);
  1189. case SNDRV_CTL_IOCTL_ELEM_INFO:
  1190. return snd_ctl_elem_info_user(ctl, argp);
  1191. case SNDRV_CTL_IOCTL_ELEM_READ:
  1192. return snd_ctl_elem_read_user(card, argp);
  1193. case SNDRV_CTL_IOCTL_ELEM_WRITE:
  1194. return snd_ctl_elem_write_user(ctl, argp);
  1195. case SNDRV_CTL_IOCTL_ELEM_LOCK:
  1196. return snd_ctl_elem_lock(ctl, argp);
  1197. case SNDRV_CTL_IOCTL_ELEM_UNLOCK:
  1198. return snd_ctl_elem_unlock(ctl, argp);
  1199. case SNDRV_CTL_IOCTL_ELEM_ADD:
  1200. return snd_ctl_elem_add_user(ctl, argp, 0);
  1201. case SNDRV_CTL_IOCTL_ELEM_REPLACE:
  1202. return snd_ctl_elem_add_user(ctl, argp, 1);
  1203. case SNDRV_CTL_IOCTL_ELEM_REMOVE:
  1204. return snd_ctl_elem_remove(ctl, argp);
  1205. case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS:
  1206. return snd_ctl_subscribe_events(ctl, ip);
  1207. case SNDRV_CTL_IOCTL_TLV_READ:
  1208. return snd_ctl_tlv_ioctl(ctl, argp, 0);
  1209. case SNDRV_CTL_IOCTL_TLV_WRITE:
  1210. return snd_ctl_tlv_ioctl(ctl, argp, 1);
  1211. case SNDRV_CTL_IOCTL_TLV_COMMAND:
  1212. return snd_ctl_tlv_ioctl(ctl, argp, -1);
  1213. case SNDRV_CTL_IOCTL_POWER:
  1214. return -ENOPROTOOPT;
  1215. case SNDRV_CTL_IOCTL_POWER_STATE:
  1216. #ifdef CONFIG_PM
  1217. return put_user(card->power_state, ip) ? -EFAULT : 0;
  1218. #else
  1219. return put_user(SNDRV_CTL_POWER_D0, ip) ? -EFAULT : 0;
  1220. #endif
  1221. }
  1222. down_read(&snd_ioctl_rwsem);
  1223. list_for_each_entry(p, &snd_control_ioctls, list) {
  1224. err = p->fioctl(card, ctl, cmd, arg);
  1225. if (err != -ENOIOCTLCMD) {
  1226. up_read(&snd_ioctl_rwsem);
  1227. return err;
  1228. }
  1229. }
  1230. up_read(&snd_ioctl_rwsem);
  1231. snd_printdd("unknown ioctl = 0x%x\n", cmd);
  1232. return -ENOTTY;
  1233. }
  1234. static ssize_t snd_ctl_read(struct file *file, char __user *buffer,
  1235. size_t count, loff_t * offset)
  1236. {
  1237. struct snd_ctl_file *ctl;
  1238. int err = 0;
  1239. ssize_t result = 0;
  1240. ctl = file->private_data;
  1241. if (snd_BUG_ON(!ctl || !ctl->card))
  1242. return -ENXIO;
  1243. if (!ctl->subscribed)
  1244. return -EBADFD;
  1245. if (count < sizeof(struct snd_ctl_event))
  1246. return -EINVAL;
  1247. spin_lock_irq(&ctl->read_lock);
  1248. while (count >= sizeof(struct snd_ctl_event)) {
  1249. struct snd_ctl_event ev;
  1250. struct snd_kctl_event *kev;
  1251. while (list_empty(&ctl->events)) {
  1252. wait_queue_t wait;
  1253. if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
  1254. err = -EAGAIN;
  1255. goto __end_lock;
  1256. }
  1257. init_waitqueue_entry(&wait, current);
  1258. add_wait_queue(&ctl->change_sleep, &wait);
  1259. set_current_state(TASK_INTERRUPTIBLE);
  1260. spin_unlock_irq(&ctl->read_lock);
  1261. schedule();
  1262. remove_wait_queue(&ctl->change_sleep, &wait);
  1263. if (signal_pending(current))
  1264. return -ERESTARTSYS;
  1265. spin_lock_irq(&ctl->read_lock);
  1266. }
  1267. kev = snd_kctl_event(ctl->events.next);
  1268. ev.type = SNDRV_CTL_EVENT_ELEM;
  1269. ev.data.elem.mask = kev->mask;
  1270. ev.data.elem.id = kev->id;
  1271. list_del(&kev->list);
  1272. spin_unlock_irq(&ctl->read_lock);
  1273. kfree(kev);
  1274. if (copy_to_user(buffer, &ev, sizeof(struct snd_ctl_event))) {
  1275. err = -EFAULT;
  1276. goto __end;
  1277. }
  1278. spin_lock_irq(&ctl->read_lock);
  1279. buffer += sizeof(struct snd_ctl_event);
  1280. count -= sizeof(struct snd_ctl_event);
  1281. result += sizeof(struct snd_ctl_event);
  1282. }
  1283. __end_lock:
  1284. spin_unlock_irq(&ctl->read_lock);
  1285. __end:
  1286. return result > 0 ? result : err;
  1287. }
  1288. static unsigned int snd_ctl_poll(struct file *file, poll_table * wait)
  1289. {
  1290. unsigned int mask;
  1291. struct snd_ctl_file *ctl;
  1292. ctl = file->private_data;
  1293. if (!ctl->subscribed)
  1294. return 0;
  1295. poll_wait(file, &ctl->change_sleep, wait);
  1296. mask = 0;
  1297. if (!list_empty(&ctl->events))
  1298. mask |= POLLIN | POLLRDNORM;
  1299. return mask;
  1300. }
  1301. /*
  1302. * register the device-specific control-ioctls.
  1303. * called from each device manager like pcm.c, hwdep.c, etc.
  1304. */
  1305. static int _snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn, struct list_head *lists)
  1306. {
  1307. struct snd_kctl_ioctl *pn;
  1308. pn = kzalloc(sizeof(struct snd_kctl_ioctl), GFP_KERNEL);
  1309. if (pn == NULL)
  1310. return -ENOMEM;
  1311. pn->fioctl = fcn;
  1312. down_write(&snd_ioctl_rwsem);
  1313. list_add_tail(&pn->list, lists);
  1314. up_write(&snd_ioctl_rwsem);
  1315. return 0;
  1316. }
  1317. int snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn)
  1318. {
  1319. return _snd_ctl_register_ioctl(fcn, &snd_control_ioctls);
  1320. }
  1321. EXPORT_SYMBOL(snd_ctl_register_ioctl);
  1322. #ifdef CONFIG_COMPAT
  1323. int snd_ctl_register_ioctl_compat(snd_kctl_ioctl_func_t fcn)
  1324. {
  1325. return _snd_ctl_register_ioctl(fcn, &snd_control_compat_ioctls);
  1326. }
  1327. EXPORT_SYMBOL(snd_ctl_register_ioctl_compat);
  1328. #endif
  1329. /*
  1330. * de-register the device-specific control-ioctls.
  1331. */
  1332. static int _snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn,
  1333. struct list_head *lists)
  1334. {
  1335. struct snd_kctl_ioctl *p;
  1336. if (snd_BUG_ON(!fcn))
  1337. return -EINVAL;
  1338. down_write(&snd_ioctl_rwsem);
  1339. list_for_each_entry(p, lists, list) {
  1340. if (p->fioctl == fcn) {
  1341. list_del(&p->list);
  1342. up_write(&snd_ioctl_rwsem);
  1343. kfree(p);
  1344. return 0;
  1345. }
  1346. }
  1347. up_write(&snd_ioctl_rwsem);
  1348. snd_BUG();
  1349. return -EINVAL;
  1350. }
  1351. int snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn)
  1352. {
  1353. return _snd_ctl_unregister_ioctl(fcn, &snd_control_ioctls);
  1354. }
  1355. EXPORT_SYMBOL(snd_ctl_unregister_ioctl);
  1356. #ifdef CONFIG_COMPAT
  1357. int snd_ctl_unregister_ioctl_compat(snd_kctl_ioctl_func_t fcn)
  1358. {
  1359. return _snd_ctl_unregister_ioctl(fcn, &snd_control_compat_ioctls);
  1360. }
  1361. EXPORT_SYMBOL(snd_ctl_unregister_ioctl_compat);
  1362. #endif
  1363. static int snd_ctl_fasync(int fd, struct file * file, int on)
  1364. {
  1365. struct snd_ctl_file *ctl;
  1366. ctl = file->private_data;
  1367. return fasync_helper(fd, file, on, &ctl->fasync);
  1368. }
  1369. /*
  1370. * ioctl32 compat
  1371. */
  1372. #ifdef CONFIG_COMPAT
  1373. #include "control_compat.c"
  1374. #else
  1375. #define snd_ctl_ioctl_compat NULL
  1376. #endif
  1377. /*
  1378. * INIT PART
  1379. */
  1380. static const struct file_operations snd_ctl_f_ops =
  1381. {
  1382. .owner = THIS_MODULE,
  1383. .read = snd_ctl_read,
  1384. .open = snd_ctl_open,
  1385. .release = snd_ctl_release,
  1386. .llseek = no_llseek,
  1387. .poll = snd_ctl_poll,
  1388. .unlocked_ioctl = snd_ctl_ioctl,
  1389. .compat_ioctl = snd_ctl_ioctl_compat,
  1390. .fasync = snd_ctl_fasync,
  1391. };
  1392. /*
  1393. * registration of the control device
  1394. */
  1395. static int snd_ctl_dev_register(struct snd_device *device)
  1396. {
  1397. struct snd_card *card = device->device_data;
  1398. int err, cardnum;
  1399. char name[16];
  1400. if (snd_BUG_ON(!card))
  1401. return -ENXIO;
  1402. cardnum = card->number;
  1403. if (snd_BUG_ON(cardnum < 0 || cardnum >= SNDRV_CARDS))
  1404. return -ENXIO;
  1405. sprintf(name, "controlC%i", cardnum);
  1406. if ((err = snd_register_device(SNDRV_DEVICE_TYPE_CONTROL, card, -1,
  1407. &snd_ctl_f_ops, card, name)) < 0)
  1408. return err;
  1409. return 0;
  1410. }
  1411. /*
  1412. * disconnection of the control device
  1413. */
  1414. static int snd_ctl_dev_disconnect(struct snd_device *device)
  1415. {
  1416. struct snd_card *card = device->device_data;
  1417. struct snd_ctl_file *ctl;
  1418. int err, cardnum;
  1419. if (snd_BUG_ON(!card))
  1420. return -ENXIO;
  1421. cardnum = card->number;
  1422. if (snd_BUG_ON(cardnum < 0 || cardnum >= SNDRV_CARDS))
  1423. return -ENXIO;
  1424. read_lock(&card->ctl_files_rwlock);
  1425. list_for_each_entry(ctl, &card->ctl_files, list) {
  1426. wake_up(&ctl->change_sleep);
  1427. kill_fasync(&ctl->fasync, SIGIO, POLL_ERR);
  1428. }
  1429. read_unlock(&card->ctl_files_rwlock);
  1430. if ((err = snd_unregister_device(SNDRV_DEVICE_TYPE_CONTROL,
  1431. card, -1)) < 0)
  1432. return err;
  1433. return 0;
  1434. }
  1435. /*
  1436. * free all controls
  1437. */
  1438. static int snd_ctl_dev_free(struct snd_device *device)
  1439. {
  1440. struct snd_card *card = device->device_data;
  1441. struct snd_kcontrol *control;
  1442. down_write(&card->controls_rwsem);
  1443. while (!list_empty(&card->controls)) {
  1444. control = snd_kcontrol(card->controls.next);
  1445. snd_ctl_remove(card, control);
  1446. }
  1447. up_write(&card->controls_rwsem);
  1448. return 0;
  1449. }
  1450. /*
  1451. * create control core:
  1452. * called from init.c
  1453. */
  1454. int snd_ctl_create(struct snd_card *card)
  1455. {
  1456. static struct snd_device_ops ops = {
  1457. .dev_free = snd_ctl_dev_free,
  1458. .dev_register = snd_ctl_dev_register,
  1459. .dev_disconnect = snd_ctl_dev_disconnect,
  1460. };
  1461. if (snd_BUG_ON(!card))
  1462. return -ENXIO;
  1463. return snd_device_new(card, SNDRV_DEV_CONTROL, card, &ops);
  1464. }
  1465. /*
  1466. * Frequently used control callbacks/helpers
  1467. */
  1468. int snd_ctl_boolean_mono_info(struct snd_kcontrol *kcontrol,
  1469. struct snd_ctl_elem_info *uinfo)
  1470. {
  1471. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1472. uinfo->count = 1;
  1473. uinfo->value.integer.min = 0;
  1474. uinfo->value.integer.max = 1;
  1475. return 0;
  1476. }
  1477. EXPORT_SYMBOL(snd_ctl_boolean_mono_info);
  1478. int snd_ctl_boolean_stereo_info(struct snd_kcontrol *kcontrol,
  1479. struct snd_ctl_elem_info *uinfo)
  1480. {
  1481. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1482. uinfo->count = 2;
  1483. uinfo->value.integer.min = 0;
  1484. uinfo->value.integer.max = 1;
  1485. return 0;
  1486. }
  1487. EXPORT_SYMBOL(snd_ctl_boolean_stereo_info);
  1488. /**
  1489. * snd_ctl_enum_info - fills the info structure for an enumerated control
  1490. * @info: the structure to be filled
  1491. * @channels: the number of the control's channels; often one
  1492. * @items: the number of control values; also the size of @names
  1493. * @names: an array containing the names of all control values
  1494. *
  1495. * Sets all required fields in @info to their appropriate values.
  1496. * If the control's accessibility is not the default (readable and writable),
  1497. * the caller has to fill @info->access.
  1498. */
  1499. int snd_ctl_enum_info(struct snd_ctl_elem_info *info, unsigned int channels,
  1500. unsigned int items, const char *const names[])
  1501. {
  1502. info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  1503. info->count = channels;
  1504. info->value.enumerated.items = items;
  1505. if (info->value.enumerated.item >= items)
  1506. info->value.enumerated.item = items - 1;
  1507. strlcpy(info->value.enumerated.name,
  1508. names[info->value.enumerated.item],
  1509. sizeof(info->value.enumerated.name));
  1510. return 0;
  1511. }
  1512. EXPORT_SYMBOL(snd_ctl_enum_info);