control.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541
  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 unsigned int snd_ctl_hole_check(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 &&
  263. kctl->id.numid + kctl->count > card->last_numid) ||
  264. (kctl->id.numid <= card->last_numid + count - 1 &&
  265. kctl->id.numid + kctl->count > card->last_numid + count - 1))
  266. return card->last_numid = kctl->id.numid + kctl->count - 1;
  267. }
  268. return card->last_numid;
  269. }
  270. static int snd_ctl_find_hole(struct snd_card *card, unsigned int count)
  271. {
  272. unsigned int last_numid, iter = 100000;
  273. last_numid = card->last_numid;
  274. while (last_numid != snd_ctl_hole_check(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. last_numid = card->last_numid;
  281. }
  282. return 0;
  283. }
  284. /**
  285. * snd_ctl_add - add the control instance to the card
  286. * @card: the card instance
  287. * @kcontrol: the control instance to add
  288. *
  289. * Adds the control instance created via snd_ctl_new() or
  290. * snd_ctl_new1() to the given card. Assigns also an unique
  291. * numid used for fast search.
  292. *
  293. * Returns zero if successful, or a negative error code on failure.
  294. *
  295. * It frees automatically the control which cannot be added.
  296. */
  297. int snd_ctl_add(struct snd_card *card, struct snd_kcontrol *kcontrol)
  298. {
  299. struct snd_ctl_elem_id id;
  300. unsigned int idx;
  301. int err = -EINVAL;
  302. if (! kcontrol)
  303. return err;
  304. if (snd_BUG_ON(!card || !kcontrol->info))
  305. goto error;
  306. id = kcontrol->id;
  307. down_write(&card->controls_rwsem);
  308. if (snd_ctl_find_id(card, &id)) {
  309. up_write(&card->controls_rwsem);
  310. snd_printd(KERN_ERR "control %i:%i:%i:%s:%i is already present\n",
  311. id.iface,
  312. id.device,
  313. id.subdevice,
  314. id.name,
  315. id.index);
  316. err = -EBUSY;
  317. goto error;
  318. }
  319. if (snd_ctl_find_hole(card, kcontrol->count) < 0) {
  320. up_write(&card->controls_rwsem);
  321. err = -ENOMEM;
  322. goto error;
  323. }
  324. list_add_tail(&kcontrol->list, &card->controls);
  325. card->controls_count += kcontrol->count;
  326. kcontrol->id.numid = card->last_numid + 1;
  327. card->last_numid += kcontrol->count;
  328. up_write(&card->controls_rwsem);
  329. for (idx = 0; idx < kcontrol->count; idx++, id.index++, id.numid++)
  330. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_ADD, &id);
  331. return 0;
  332. error:
  333. snd_ctl_free_one(kcontrol);
  334. return err;
  335. }
  336. EXPORT_SYMBOL(snd_ctl_add);
  337. /**
  338. * snd_ctl_remove - remove the control from the card and release it
  339. * @card: the card instance
  340. * @kcontrol: the control instance to remove
  341. *
  342. * Removes the control from the card and then releases the instance.
  343. * You don't need to call snd_ctl_free_one(). You must be in
  344. * the write lock - down_write(&card->controls_rwsem).
  345. *
  346. * Returns 0 if successful, or a negative error code on failure.
  347. */
  348. int snd_ctl_remove(struct snd_card *card, struct snd_kcontrol *kcontrol)
  349. {
  350. struct snd_ctl_elem_id id;
  351. unsigned int idx;
  352. if (snd_BUG_ON(!card || !kcontrol))
  353. return -EINVAL;
  354. list_del(&kcontrol->list);
  355. card->controls_count -= kcontrol->count;
  356. id = kcontrol->id;
  357. for (idx = 0; idx < kcontrol->count; idx++, id.index++, id.numid++)
  358. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_REMOVE, &id);
  359. snd_ctl_free_one(kcontrol);
  360. return 0;
  361. }
  362. EXPORT_SYMBOL(snd_ctl_remove);
  363. /**
  364. * snd_ctl_remove_id - remove the control of the given id and release it
  365. * @card: the card instance
  366. * @id: the control id to remove
  367. *
  368. * Finds the control instance with the given id, removes it from the
  369. * card list and releases it.
  370. *
  371. * Returns 0 if successful, or a negative error code on failure.
  372. */
  373. int snd_ctl_remove_id(struct snd_card *card, struct snd_ctl_elem_id *id)
  374. {
  375. struct snd_kcontrol *kctl;
  376. int ret;
  377. down_write(&card->controls_rwsem);
  378. kctl = snd_ctl_find_id(card, id);
  379. if (kctl == NULL) {
  380. up_write(&card->controls_rwsem);
  381. return -ENOENT;
  382. }
  383. ret = snd_ctl_remove(card, kctl);
  384. up_write(&card->controls_rwsem);
  385. return ret;
  386. }
  387. EXPORT_SYMBOL(snd_ctl_remove_id);
  388. /**
  389. * snd_ctl_remove_user_ctl - remove and release the unlocked user control
  390. * @file: active control handle
  391. * @id: the control id to remove
  392. *
  393. * Finds the control instance with the given id, removes it from the
  394. * card list and releases it.
  395. *
  396. * Returns 0 if successful, or a negative error code on failure.
  397. */
  398. static int snd_ctl_remove_user_ctl(struct snd_ctl_file * file,
  399. struct snd_ctl_elem_id *id)
  400. {
  401. struct snd_card *card = file->card;
  402. struct snd_kcontrol *kctl;
  403. int idx, ret;
  404. down_write(&card->controls_rwsem);
  405. kctl = snd_ctl_find_id(card, id);
  406. if (kctl == NULL) {
  407. ret = -ENOENT;
  408. goto error;
  409. }
  410. if (!(kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_USER)) {
  411. ret = -EINVAL;
  412. goto error;
  413. }
  414. for (idx = 0; idx < kctl->count; idx++)
  415. if (kctl->vd[idx].owner != NULL && kctl->vd[idx].owner != file) {
  416. ret = -EBUSY;
  417. goto error;
  418. }
  419. ret = snd_ctl_remove(card, kctl);
  420. if (ret < 0)
  421. goto error;
  422. card->user_ctl_count--;
  423. error:
  424. up_write(&card->controls_rwsem);
  425. return ret;
  426. }
  427. /**
  428. * snd_ctl_rename_id - replace the id of a control on the card
  429. * @card: the card instance
  430. * @src_id: the old id
  431. * @dst_id: the new id
  432. *
  433. * Finds the control with the old id from the card, and replaces the
  434. * id with the new one.
  435. *
  436. * Returns zero if successful, or a negative error code on failure.
  437. */
  438. int snd_ctl_rename_id(struct snd_card *card, struct snd_ctl_elem_id *src_id,
  439. struct snd_ctl_elem_id *dst_id)
  440. {
  441. struct snd_kcontrol *kctl;
  442. down_write(&card->controls_rwsem);
  443. kctl = snd_ctl_find_id(card, src_id);
  444. if (kctl == NULL) {
  445. up_write(&card->controls_rwsem);
  446. return -ENOENT;
  447. }
  448. kctl->id = *dst_id;
  449. kctl->id.numid = card->last_numid + 1;
  450. card->last_numid += kctl->count;
  451. up_write(&card->controls_rwsem);
  452. return 0;
  453. }
  454. EXPORT_SYMBOL(snd_ctl_rename_id);
  455. /**
  456. * snd_ctl_find_numid - find the control instance with the given number-id
  457. * @card: the card instance
  458. * @numid: the number-id to search
  459. *
  460. * Finds the control instance with the given number-id from the card.
  461. *
  462. * Returns the pointer of the instance if found, or NULL if not.
  463. *
  464. * The caller must down card->controls_rwsem before calling this function
  465. * (if the race condition can happen).
  466. */
  467. struct snd_kcontrol *snd_ctl_find_numid(struct snd_card *card, unsigned int numid)
  468. {
  469. struct snd_kcontrol *kctl;
  470. if (snd_BUG_ON(!card || !numid))
  471. return NULL;
  472. list_for_each_entry(kctl, &card->controls, list) {
  473. if (kctl->id.numid <= numid && kctl->id.numid + kctl->count > numid)
  474. return kctl;
  475. }
  476. return NULL;
  477. }
  478. EXPORT_SYMBOL(snd_ctl_find_numid);
  479. /**
  480. * snd_ctl_find_id - find the control instance with the given id
  481. * @card: the card instance
  482. * @id: the id to search
  483. *
  484. * Finds the control instance with the given id from the card.
  485. *
  486. * Returns the pointer of the instance if found, or NULL if not.
  487. *
  488. * The caller must down card->controls_rwsem before calling this function
  489. * (if the race condition can happen).
  490. */
  491. struct snd_kcontrol *snd_ctl_find_id(struct snd_card *card,
  492. struct snd_ctl_elem_id *id)
  493. {
  494. struct snd_kcontrol *kctl;
  495. if (snd_BUG_ON(!card || !id))
  496. return NULL;
  497. if (id->numid != 0)
  498. return snd_ctl_find_numid(card, id->numid);
  499. list_for_each_entry(kctl, &card->controls, list) {
  500. if (kctl->id.iface != id->iface)
  501. continue;
  502. if (kctl->id.device != id->device)
  503. continue;
  504. if (kctl->id.subdevice != id->subdevice)
  505. continue;
  506. if (strncmp(kctl->id.name, id->name, sizeof(kctl->id.name)))
  507. continue;
  508. if (kctl->id.index > id->index)
  509. continue;
  510. if (kctl->id.index + kctl->count <= id->index)
  511. continue;
  512. return kctl;
  513. }
  514. return NULL;
  515. }
  516. EXPORT_SYMBOL(snd_ctl_find_id);
  517. static int snd_ctl_card_info(struct snd_card *card, struct snd_ctl_file * ctl,
  518. unsigned int cmd, void __user *arg)
  519. {
  520. struct snd_ctl_card_info *info;
  521. info = kzalloc(sizeof(*info), GFP_KERNEL);
  522. if (! info)
  523. return -ENOMEM;
  524. down_read(&snd_ioctl_rwsem);
  525. info->card = card->number;
  526. strlcpy(info->id, card->id, sizeof(info->id));
  527. strlcpy(info->driver, card->driver, sizeof(info->driver));
  528. strlcpy(info->name, card->shortname, sizeof(info->name));
  529. strlcpy(info->longname, card->longname, sizeof(info->longname));
  530. strlcpy(info->mixername, card->mixername, sizeof(info->mixername));
  531. strlcpy(info->components, card->components, sizeof(info->components));
  532. up_read(&snd_ioctl_rwsem);
  533. if (copy_to_user(arg, info, sizeof(struct snd_ctl_card_info))) {
  534. kfree(info);
  535. return -EFAULT;
  536. }
  537. kfree(info);
  538. return 0;
  539. }
  540. static int snd_ctl_elem_list(struct snd_card *card,
  541. struct snd_ctl_elem_list __user *_list)
  542. {
  543. struct list_head *plist;
  544. struct snd_ctl_elem_list list;
  545. struct snd_kcontrol *kctl;
  546. struct snd_ctl_elem_id *dst, *id;
  547. unsigned int offset, space, first, jidx;
  548. if (copy_from_user(&list, _list, sizeof(list)))
  549. return -EFAULT;
  550. offset = list.offset;
  551. space = list.space;
  552. first = 0;
  553. /* try limit maximum space */
  554. if (space > 16384)
  555. return -ENOMEM;
  556. if (space > 0) {
  557. /* allocate temporary buffer for atomic operation */
  558. dst = vmalloc(space * sizeof(struct snd_ctl_elem_id));
  559. if (dst == NULL)
  560. return -ENOMEM;
  561. down_read(&card->controls_rwsem);
  562. list.count = card->controls_count;
  563. plist = card->controls.next;
  564. while (plist != &card->controls) {
  565. if (offset == 0)
  566. break;
  567. kctl = snd_kcontrol(plist);
  568. if (offset < kctl->count)
  569. break;
  570. offset -= kctl->count;
  571. plist = plist->next;
  572. }
  573. list.used = 0;
  574. id = dst;
  575. while (space > 0 && plist != &card->controls) {
  576. kctl = snd_kcontrol(plist);
  577. for (jidx = offset; space > 0 && jidx < kctl->count; jidx++) {
  578. snd_ctl_build_ioff(id, kctl, jidx);
  579. id++;
  580. space--;
  581. list.used++;
  582. }
  583. plist = plist->next;
  584. offset = 0;
  585. }
  586. up_read(&card->controls_rwsem);
  587. if (list.used > 0 &&
  588. copy_to_user(list.pids, dst,
  589. list.used * sizeof(struct snd_ctl_elem_id))) {
  590. vfree(dst);
  591. return -EFAULT;
  592. }
  593. vfree(dst);
  594. } else {
  595. down_read(&card->controls_rwsem);
  596. list.count = card->controls_count;
  597. up_read(&card->controls_rwsem);
  598. }
  599. if (copy_to_user(_list, &list, sizeof(list)))
  600. return -EFAULT;
  601. return 0;
  602. }
  603. static int snd_ctl_elem_info(struct snd_ctl_file *ctl,
  604. struct snd_ctl_elem_info *info)
  605. {
  606. struct snd_card *card = ctl->card;
  607. struct snd_kcontrol *kctl;
  608. struct snd_kcontrol_volatile *vd;
  609. unsigned int index_offset;
  610. int result;
  611. down_read(&card->controls_rwsem);
  612. kctl = snd_ctl_find_id(card, &info->id);
  613. if (kctl == NULL) {
  614. up_read(&card->controls_rwsem);
  615. return -ENOENT;
  616. }
  617. #ifdef CONFIG_SND_DEBUG
  618. info->access = 0;
  619. #endif
  620. result = kctl->info(kctl, info);
  621. if (result >= 0) {
  622. snd_BUG_ON(info->access);
  623. index_offset = snd_ctl_get_ioff(kctl, &info->id);
  624. vd = &kctl->vd[index_offset];
  625. snd_ctl_build_ioff(&info->id, kctl, index_offset);
  626. info->access = vd->access;
  627. if (vd->owner) {
  628. info->access |= SNDRV_CTL_ELEM_ACCESS_LOCK;
  629. if (vd->owner == ctl)
  630. info->access |= SNDRV_CTL_ELEM_ACCESS_OWNER;
  631. info->owner = pid_vnr(vd->owner->pid);
  632. } else {
  633. info->owner = -1;
  634. }
  635. }
  636. up_read(&card->controls_rwsem);
  637. return result;
  638. }
  639. static int snd_ctl_elem_info_user(struct snd_ctl_file *ctl,
  640. struct snd_ctl_elem_info __user *_info)
  641. {
  642. struct snd_ctl_elem_info info;
  643. int result;
  644. if (copy_from_user(&info, _info, sizeof(info)))
  645. return -EFAULT;
  646. snd_power_lock(ctl->card);
  647. result = snd_power_wait(ctl->card, SNDRV_CTL_POWER_D0);
  648. if (result >= 0)
  649. result = snd_ctl_elem_info(ctl, &info);
  650. snd_power_unlock(ctl->card);
  651. if (result >= 0)
  652. if (copy_to_user(_info, &info, sizeof(info)))
  653. return -EFAULT;
  654. return result;
  655. }
  656. static int snd_ctl_elem_read(struct snd_card *card,
  657. struct snd_ctl_elem_value *control)
  658. {
  659. struct snd_kcontrol *kctl;
  660. struct snd_kcontrol_volatile *vd;
  661. unsigned int index_offset;
  662. int result;
  663. down_read(&card->controls_rwsem);
  664. kctl = snd_ctl_find_id(card, &control->id);
  665. if (kctl == NULL) {
  666. result = -ENOENT;
  667. } else {
  668. index_offset = snd_ctl_get_ioff(kctl, &control->id);
  669. vd = &kctl->vd[index_offset];
  670. if ((vd->access & SNDRV_CTL_ELEM_ACCESS_READ) &&
  671. kctl->get != NULL) {
  672. snd_ctl_build_ioff(&control->id, kctl, index_offset);
  673. result = kctl->get(kctl, control);
  674. } else
  675. result = -EPERM;
  676. }
  677. up_read(&card->controls_rwsem);
  678. return result;
  679. }
  680. static int snd_ctl_elem_read_user(struct snd_card *card,
  681. struct snd_ctl_elem_value __user *_control)
  682. {
  683. struct snd_ctl_elem_value *control;
  684. int result;
  685. control = memdup_user(_control, sizeof(*control));
  686. if (IS_ERR(control))
  687. return PTR_ERR(control);
  688. snd_power_lock(card);
  689. result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
  690. if (result >= 0)
  691. result = snd_ctl_elem_read(card, control);
  692. snd_power_unlock(card);
  693. if (result >= 0)
  694. if (copy_to_user(_control, control, sizeof(*control)))
  695. result = -EFAULT;
  696. kfree(control);
  697. return result;
  698. }
  699. static int snd_ctl_elem_write(struct snd_card *card, struct snd_ctl_file *file,
  700. struct snd_ctl_elem_value *control)
  701. {
  702. struct snd_kcontrol *kctl;
  703. struct snd_kcontrol_volatile *vd;
  704. unsigned int index_offset;
  705. int result;
  706. down_read(&card->controls_rwsem);
  707. kctl = snd_ctl_find_id(card, &control->id);
  708. if (kctl == NULL) {
  709. result = -ENOENT;
  710. } else {
  711. index_offset = snd_ctl_get_ioff(kctl, &control->id);
  712. vd = &kctl->vd[index_offset];
  713. if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_WRITE) ||
  714. kctl->put == NULL ||
  715. (file && vd->owner && vd->owner != file)) {
  716. result = -EPERM;
  717. } else {
  718. snd_ctl_build_ioff(&control->id, kctl, index_offset);
  719. result = kctl->put(kctl, control);
  720. }
  721. if (result > 0) {
  722. up_read(&card->controls_rwsem);
  723. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
  724. &control->id);
  725. return 0;
  726. }
  727. }
  728. up_read(&card->controls_rwsem);
  729. return result;
  730. }
  731. static int snd_ctl_elem_write_user(struct snd_ctl_file *file,
  732. struct snd_ctl_elem_value __user *_control)
  733. {
  734. struct snd_ctl_elem_value *control;
  735. struct snd_card *card;
  736. int result;
  737. control = memdup_user(_control, sizeof(*control));
  738. if (IS_ERR(control))
  739. return PTR_ERR(control);
  740. card = file->card;
  741. snd_power_lock(card);
  742. result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
  743. if (result >= 0)
  744. result = snd_ctl_elem_write(card, file, control);
  745. snd_power_unlock(card);
  746. if (result >= 0)
  747. if (copy_to_user(_control, control, sizeof(*control)))
  748. result = -EFAULT;
  749. kfree(control);
  750. return result;
  751. }
  752. static int snd_ctl_elem_lock(struct snd_ctl_file *file,
  753. struct snd_ctl_elem_id __user *_id)
  754. {
  755. struct snd_card *card = file->card;
  756. struct snd_ctl_elem_id id;
  757. struct snd_kcontrol *kctl;
  758. struct snd_kcontrol_volatile *vd;
  759. int result;
  760. if (copy_from_user(&id, _id, sizeof(id)))
  761. return -EFAULT;
  762. down_write(&card->controls_rwsem);
  763. kctl = snd_ctl_find_id(card, &id);
  764. if (kctl == NULL) {
  765. result = -ENOENT;
  766. } else {
  767. vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
  768. if (vd->owner != NULL)
  769. result = -EBUSY;
  770. else {
  771. vd->owner = file;
  772. result = 0;
  773. }
  774. }
  775. up_write(&card->controls_rwsem);
  776. return result;
  777. }
  778. static int snd_ctl_elem_unlock(struct snd_ctl_file *file,
  779. struct snd_ctl_elem_id __user *_id)
  780. {
  781. struct snd_card *card = file->card;
  782. struct snd_ctl_elem_id id;
  783. struct snd_kcontrol *kctl;
  784. struct snd_kcontrol_volatile *vd;
  785. int result;
  786. if (copy_from_user(&id, _id, sizeof(id)))
  787. return -EFAULT;
  788. down_write(&card->controls_rwsem);
  789. kctl = snd_ctl_find_id(card, &id);
  790. if (kctl == NULL) {
  791. result = -ENOENT;
  792. } else {
  793. vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
  794. if (vd->owner == NULL)
  795. result = -EINVAL;
  796. else if (vd->owner != file)
  797. result = -EPERM;
  798. else {
  799. vd->owner = NULL;
  800. result = 0;
  801. }
  802. }
  803. up_write(&card->controls_rwsem);
  804. return result;
  805. }
  806. struct user_element {
  807. struct snd_ctl_elem_info info;
  808. void *elem_data; /* element data */
  809. unsigned long elem_data_size; /* size of element data in bytes */
  810. void *tlv_data; /* TLV data */
  811. unsigned long tlv_data_size; /* TLV data size */
  812. void *priv_data; /* private data (like strings for enumerated type) */
  813. unsigned long priv_data_size; /* size of private data in bytes */
  814. };
  815. static int snd_ctl_elem_user_info(struct snd_kcontrol *kcontrol,
  816. struct snd_ctl_elem_info *uinfo)
  817. {
  818. struct user_element *ue = kcontrol->private_data;
  819. *uinfo = ue->info;
  820. return 0;
  821. }
  822. static int snd_ctl_elem_user_get(struct snd_kcontrol *kcontrol,
  823. struct snd_ctl_elem_value *ucontrol)
  824. {
  825. struct user_element *ue = kcontrol->private_data;
  826. memcpy(&ucontrol->value, ue->elem_data, ue->elem_data_size);
  827. return 0;
  828. }
  829. static int snd_ctl_elem_user_put(struct snd_kcontrol *kcontrol,
  830. struct snd_ctl_elem_value *ucontrol)
  831. {
  832. int change;
  833. struct user_element *ue = kcontrol->private_data;
  834. change = memcmp(&ucontrol->value, ue->elem_data, ue->elem_data_size) != 0;
  835. if (change)
  836. memcpy(ue->elem_data, &ucontrol->value, ue->elem_data_size);
  837. return change;
  838. }
  839. static int snd_ctl_elem_user_tlv(struct snd_kcontrol *kcontrol,
  840. int op_flag,
  841. unsigned int size,
  842. unsigned int __user *tlv)
  843. {
  844. struct user_element *ue = kcontrol->private_data;
  845. int change = 0;
  846. void *new_data;
  847. if (op_flag > 0) {
  848. if (size > 1024 * 128) /* sane value */
  849. return -EINVAL;
  850. new_data = memdup_user(tlv, size);
  851. if (IS_ERR(new_data))
  852. return PTR_ERR(new_data);
  853. change = ue->tlv_data_size != size;
  854. if (!change)
  855. change = memcmp(ue->tlv_data, new_data, size);
  856. kfree(ue->tlv_data);
  857. ue->tlv_data = new_data;
  858. ue->tlv_data_size = size;
  859. } else {
  860. if (! ue->tlv_data_size || ! ue->tlv_data)
  861. return -ENXIO;
  862. if (size < ue->tlv_data_size)
  863. return -ENOSPC;
  864. if (copy_to_user(tlv, ue->tlv_data, ue->tlv_data_size))
  865. return -EFAULT;
  866. }
  867. return change;
  868. }
  869. static void snd_ctl_elem_user_free(struct snd_kcontrol *kcontrol)
  870. {
  871. struct user_element *ue = kcontrol->private_data;
  872. if (ue->tlv_data)
  873. kfree(ue->tlv_data);
  874. kfree(ue);
  875. }
  876. static int snd_ctl_elem_add(struct snd_ctl_file *file,
  877. struct snd_ctl_elem_info *info, int replace)
  878. {
  879. struct snd_card *card = file->card;
  880. struct snd_kcontrol kctl, *_kctl;
  881. unsigned int access;
  882. long private_size;
  883. struct user_element *ue;
  884. int idx, err;
  885. if (card->user_ctl_count >= MAX_USER_CONTROLS)
  886. return -ENOMEM;
  887. if (info->count < 1)
  888. return -EINVAL;
  889. access = info->access == 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE :
  890. (info->access & (SNDRV_CTL_ELEM_ACCESS_READWRITE|
  891. SNDRV_CTL_ELEM_ACCESS_INACTIVE|
  892. SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE));
  893. info->id.numid = 0;
  894. memset(&kctl, 0, sizeof(kctl));
  895. down_write(&card->controls_rwsem);
  896. _kctl = snd_ctl_find_id(card, &info->id);
  897. err = 0;
  898. if (_kctl) {
  899. if (replace)
  900. err = snd_ctl_remove(card, _kctl);
  901. else
  902. err = -EBUSY;
  903. } else {
  904. if (replace)
  905. err = -ENOENT;
  906. }
  907. up_write(&card->controls_rwsem);
  908. if (err < 0)
  909. return err;
  910. memcpy(&kctl.id, &info->id, sizeof(info->id));
  911. kctl.count = info->owner ? info->owner : 1;
  912. access |= SNDRV_CTL_ELEM_ACCESS_USER;
  913. kctl.info = snd_ctl_elem_user_info;
  914. if (access & SNDRV_CTL_ELEM_ACCESS_READ)
  915. kctl.get = snd_ctl_elem_user_get;
  916. if (access & SNDRV_CTL_ELEM_ACCESS_WRITE)
  917. kctl.put = snd_ctl_elem_user_put;
  918. if (access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE) {
  919. kctl.tlv.c = snd_ctl_elem_user_tlv;
  920. access |= SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
  921. }
  922. switch (info->type) {
  923. case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
  924. case SNDRV_CTL_ELEM_TYPE_INTEGER:
  925. private_size = sizeof(long);
  926. if (info->count > 128)
  927. return -EINVAL;
  928. break;
  929. case SNDRV_CTL_ELEM_TYPE_INTEGER64:
  930. private_size = sizeof(long long);
  931. if (info->count > 64)
  932. return -EINVAL;
  933. break;
  934. case SNDRV_CTL_ELEM_TYPE_BYTES:
  935. private_size = sizeof(unsigned char);
  936. if (info->count > 512)
  937. return -EINVAL;
  938. break;
  939. case SNDRV_CTL_ELEM_TYPE_IEC958:
  940. private_size = sizeof(struct snd_aes_iec958);
  941. if (info->count != 1)
  942. return -EINVAL;
  943. break;
  944. default:
  945. return -EINVAL;
  946. }
  947. private_size *= info->count;
  948. ue = kzalloc(sizeof(struct user_element) + private_size, GFP_KERNEL);
  949. if (ue == NULL)
  950. return -ENOMEM;
  951. ue->info = *info;
  952. ue->info.access = 0;
  953. ue->elem_data = (char *)ue + sizeof(*ue);
  954. ue->elem_data_size = private_size;
  955. kctl.private_free = snd_ctl_elem_user_free;
  956. _kctl = snd_ctl_new(&kctl, access);
  957. if (_kctl == NULL) {
  958. kfree(ue);
  959. return -ENOMEM;
  960. }
  961. _kctl->private_data = ue;
  962. for (idx = 0; idx < _kctl->count; idx++)
  963. _kctl->vd[idx].owner = file;
  964. err = snd_ctl_add(card, _kctl);
  965. if (err < 0)
  966. return err;
  967. down_write(&card->controls_rwsem);
  968. card->user_ctl_count++;
  969. up_write(&card->controls_rwsem);
  970. return 0;
  971. }
  972. static int snd_ctl_elem_add_user(struct snd_ctl_file *file,
  973. struct snd_ctl_elem_info __user *_info, int replace)
  974. {
  975. struct snd_ctl_elem_info info;
  976. if (copy_from_user(&info, _info, sizeof(info)))
  977. return -EFAULT;
  978. return snd_ctl_elem_add(file, &info, replace);
  979. }
  980. static int snd_ctl_elem_remove(struct snd_ctl_file *file,
  981. struct snd_ctl_elem_id __user *_id)
  982. {
  983. struct snd_ctl_elem_id id;
  984. if (copy_from_user(&id, _id, sizeof(id)))
  985. return -EFAULT;
  986. return snd_ctl_remove_user_ctl(file, &id);
  987. }
  988. static int snd_ctl_subscribe_events(struct snd_ctl_file *file, int __user *ptr)
  989. {
  990. int subscribe;
  991. if (get_user(subscribe, ptr))
  992. return -EFAULT;
  993. if (subscribe < 0) {
  994. subscribe = file->subscribed;
  995. if (put_user(subscribe, ptr))
  996. return -EFAULT;
  997. return 0;
  998. }
  999. if (subscribe) {
  1000. file->subscribed = 1;
  1001. return 0;
  1002. } else if (file->subscribed) {
  1003. snd_ctl_empty_read_queue(file);
  1004. file->subscribed = 0;
  1005. }
  1006. return 0;
  1007. }
  1008. static int snd_ctl_tlv_ioctl(struct snd_ctl_file *file,
  1009. struct snd_ctl_tlv __user *_tlv,
  1010. int op_flag)
  1011. {
  1012. struct snd_card *card = file->card;
  1013. struct snd_ctl_tlv tlv;
  1014. struct snd_kcontrol *kctl;
  1015. struct snd_kcontrol_volatile *vd;
  1016. unsigned int len;
  1017. int err = 0;
  1018. if (copy_from_user(&tlv, _tlv, sizeof(tlv)))
  1019. return -EFAULT;
  1020. if (tlv.length < sizeof(unsigned int) * 2)
  1021. return -EINVAL;
  1022. down_read(&card->controls_rwsem);
  1023. kctl = snd_ctl_find_numid(card, tlv.numid);
  1024. if (kctl == NULL) {
  1025. err = -ENOENT;
  1026. goto __kctl_end;
  1027. }
  1028. if (kctl->tlv.p == NULL) {
  1029. err = -ENXIO;
  1030. goto __kctl_end;
  1031. }
  1032. vd = &kctl->vd[tlv.numid - kctl->id.numid];
  1033. if ((op_flag == 0 && (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_READ) == 0) ||
  1034. (op_flag > 0 && (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE) == 0) ||
  1035. (op_flag < 0 && (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND) == 0)) {
  1036. err = -ENXIO;
  1037. goto __kctl_end;
  1038. }
  1039. if (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
  1040. if (vd->owner != NULL && vd->owner != file) {
  1041. err = -EPERM;
  1042. goto __kctl_end;
  1043. }
  1044. err = kctl->tlv.c(kctl, op_flag, tlv.length, _tlv->tlv);
  1045. if (err > 0) {
  1046. up_read(&card->controls_rwsem);
  1047. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_TLV, &kctl->id);
  1048. return 0;
  1049. }
  1050. } else {
  1051. if (op_flag) {
  1052. err = -ENXIO;
  1053. goto __kctl_end;
  1054. }
  1055. len = kctl->tlv.p[1] + 2 * sizeof(unsigned int);
  1056. if (tlv.length < len) {
  1057. err = -ENOMEM;
  1058. goto __kctl_end;
  1059. }
  1060. if (copy_to_user(_tlv->tlv, kctl->tlv.p, len))
  1061. err = -EFAULT;
  1062. }
  1063. __kctl_end:
  1064. up_read(&card->controls_rwsem);
  1065. return err;
  1066. }
  1067. static long snd_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  1068. {
  1069. struct snd_ctl_file *ctl;
  1070. struct snd_card *card;
  1071. struct snd_kctl_ioctl *p;
  1072. void __user *argp = (void __user *)arg;
  1073. int __user *ip = argp;
  1074. int err;
  1075. ctl = file->private_data;
  1076. card = ctl->card;
  1077. if (snd_BUG_ON(!card))
  1078. return -ENXIO;
  1079. switch (cmd) {
  1080. case SNDRV_CTL_IOCTL_PVERSION:
  1081. return put_user(SNDRV_CTL_VERSION, ip) ? -EFAULT : 0;
  1082. case SNDRV_CTL_IOCTL_CARD_INFO:
  1083. return snd_ctl_card_info(card, ctl, cmd, argp);
  1084. case SNDRV_CTL_IOCTL_ELEM_LIST:
  1085. return snd_ctl_elem_list(card, argp);
  1086. case SNDRV_CTL_IOCTL_ELEM_INFO:
  1087. return snd_ctl_elem_info_user(ctl, argp);
  1088. case SNDRV_CTL_IOCTL_ELEM_READ:
  1089. return snd_ctl_elem_read_user(card, argp);
  1090. case SNDRV_CTL_IOCTL_ELEM_WRITE:
  1091. return snd_ctl_elem_write_user(ctl, argp);
  1092. case SNDRV_CTL_IOCTL_ELEM_LOCK:
  1093. return snd_ctl_elem_lock(ctl, argp);
  1094. case SNDRV_CTL_IOCTL_ELEM_UNLOCK:
  1095. return snd_ctl_elem_unlock(ctl, argp);
  1096. case SNDRV_CTL_IOCTL_ELEM_ADD:
  1097. return snd_ctl_elem_add_user(ctl, argp, 0);
  1098. case SNDRV_CTL_IOCTL_ELEM_REPLACE:
  1099. return snd_ctl_elem_add_user(ctl, argp, 1);
  1100. case SNDRV_CTL_IOCTL_ELEM_REMOVE:
  1101. return snd_ctl_elem_remove(ctl, argp);
  1102. case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS:
  1103. return snd_ctl_subscribe_events(ctl, ip);
  1104. case SNDRV_CTL_IOCTL_TLV_READ:
  1105. return snd_ctl_tlv_ioctl(ctl, argp, 0);
  1106. case SNDRV_CTL_IOCTL_TLV_WRITE:
  1107. return snd_ctl_tlv_ioctl(ctl, argp, 1);
  1108. case SNDRV_CTL_IOCTL_TLV_COMMAND:
  1109. return snd_ctl_tlv_ioctl(ctl, argp, -1);
  1110. case SNDRV_CTL_IOCTL_POWER:
  1111. return -ENOPROTOOPT;
  1112. case SNDRV_CTL_IOCTL_POWER_STATE:
  1113. #ifdef CONFIG_PM
  1114. return put_user(card->power_state, ip) ? -EFAULT : 0;
  1115. #else
  1116. return put_user(SNDRV_CTL_POWER_D0, ip) ? -EFAULT : 0;
  1117. #endif
  1118. }
  1119. down_read(&snd_ioctl_rwsem);
  1120. list_for_each_entry(p, &snd_control_ioctls, list) {
  1121. err = p->fioctl(card, ctl, cmd, arg);
  1122. if (err != -ENOIOCTLCMD) {
  1123. up_read(&snd_ioctl_rwsem);
  1124. return err;
  1125. }
  1126. }
  1127. up_read(&snd_ioctl_rwsem);
  1128. snd_printdd("unknown ioctl = 0x%x\n", cmd);
  1129. return -ENOTTY;
  1130. }
  1131. static ssize_t snd_ctl_read(struct file *file, char __user *buffer,
  1132. size_t count, loff_t * offset)
  1133. {
  1134. struct snd_ctl_file *ctl;
  1135. int err = 0;
  1136. ssize_t result = 0;
  1137. ctl = file->private_data;
  1138. if (snd_BUG_ON(!ctl || !ctl->card))
  1139. return -ENXIO;
  1140. if (!ctl->subscribed)
  1141. return -EBADFD;
  1142. if (count < sizeof(struct snd_ctl_event))
  1143. return -EINVAL;
  1144. spin_lock_irq(&ctl->read_lock);
  1145. while (count >= sizeof(struct snd_ctl_event)) {
  1146. struct snd_ctl_event ev;
  1147. struct snd_kctl_event *kev;
  1148. while (list_empty(&ctl->events)) {
  1149. wait_queue_t wait;
  1150. if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
  1151. err = -EAGAIN;
  1152. goto __end_lock;
  1153. }
  1154. init_waitqueue_entry(&wait, current);
  1155. add_wait_queue(&ctl->change_sleep, &wait);
  1156. set_current_state(TASK_INTERRUPTIBLE);
  1157. spin_unlock_irq(&ctl->read_lock);
  1158. schedule();
  1159. remove_wait_queue(&ctl->change_sleep, &wait);
  1160. if (signal_pending(current))
  1161. return -ERESTARTSYS;
  1162. spin_lock_irq(&ctl->read_lock);
  1163. }
  1164. kev = snd_kctl_event(ctl->events.next);
  1165. ev.type = SNDRV_CTL_EVENT_ELEM;
  1166. ev.data.elem.mask = kev->mask;
  1167. ev.data.elem.id = kev->id;
  1168. list_del(&kev->list);
  1169. spin_unlock_irq(&ctl->read_lock);
  1170. kfree(kev);
  1171. if (copy_to_user(buffer, &ev, sizeof(struct snd_ctl_event))) {
  1172. err = -EFAULT;
  1173. goto __end;
  1174. }
  1175. spin_lock_irq(&ctl->read_lock);
  1176. buffer += sizeof(struct snd_ctl_event);
  1177. count -= sizeof(struct snd_ctl_event);
  1178. result += sizeof(struct snd_ctl_event);
  1179. }
  1180. __end_lock:
  1181. spin_unlock_irq(&ctl->read_lock);
  1182. __end:
  1183. return result > 0 ? result : err;
  1184. }
  1185. static unsigned int snd_ctl_poll(struct file *file, poll_table * wait)
  1186. {
  1187. unsigned int mask;
  1188. struct snd_ctl_file *ctl;
  1189. ctl = file->private_data;
  1190. if (!ctl->subscribed)
  1191. return 0;
  1192. poll_wait(file, &ctl->change_sleep, wait);
  1193. mask = 0;
  1194. if (!list_empty(&ctl->events))
  1195. mask |= POLLIN | POLLRDNORM;
  1196. return mask;
  1197. }
  1198. /*
  1199. * register the device-specific control-ioctls.
  1200. * called from each device manager like pcm.c, hwdep.c, etc.
  1201. */
  1202. static int _snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn, struct list_head *lists)
  1203. {
  1204. struct snd_kctl_ioctl *pn;
  1205. pn = kzalloc(sizeof(struct snd_kctl_ioctl), GFP_KERNEL);
  1206. if (pn == NULL)
  1207. return -ENOMEM;
  1208. pn->fioctl = fcn;
  1209. down_write(&snd_ioctl_rwsem);
  1210. list_add_tail(&pn->list, lists);
  1211. up_write(&snd_ioctl_rwsem);
  1212. return 0;
  1213. }
  1214. int snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn)
  1215. {
  1216. return _snd_ctl_register_ioctl(fcn, &snd_control_ioctls);
  1217. }
  1218. EXPORT_SYMBOL(snd_ctl_register_ioctl);
  1219. #ifdef CONFIG_COMPAT
  1220. int snd_ctl_register_ioctl_compat(snd_kctl_ioctl_func_t fcn)
  1221. {
  1222. return _snd_ctl_register_ioctl(fcn, &snd_control_compat_ioctls);
  1223. }
  1224. EXPORT_SYMBOL(snd_ctl_register_ioctl_compat);
  1225. #endif
  1226. /*
  1227. * de-register the device-specific control-ioctls.
  1228. */
  1229. static int _snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn,
  1230. struct list_head *lists)
  1231. {
  1232. struct snd_kctl_ioctl *p;
  1233. if (snd_BUG_ON(!fcn))
  1234. return -EINVAL;
  1235. down_write(&snd_ioctl_rwsem);
  1236. list_for_each_entry(p, lists, list) {
  1237. if (p->fioctl == fcn) {
  1238. list_del(&p->list);
  1239. up_write(&snd_ioctl_rwsem);
  1240. kfree(p);
  1241. return 0;
  1242. }
  1243. }
  1244. up_write(&snd_ioctl_rwsem);
  1245. snd_BUG();
  1246. return -EINVAL;
  1247. }
  1248. int snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn)
  1249. {
  1250. return _snd_ctl_unregister_ioctl(fcn, &snd_control_ioctls);
  1251. }
  1252. EXPORT_SYMBOL(snd_ctl_unregister_ioctl);
  1253. #ifdef CONFIG_COMPAT
  1254. int snd_ctl_unregister_ioctl_compat(snd_kctl_ioctl_func_t fcn)
  1255. {
  1256. return _snd_ctl_unregister_ioctl(fcn, &snd_control_compat_ioctls);
  1257. }
  1258. EXPORT_SYMBOL(snd_ctl_unregister_ioctl_compat);
  1259. #endif
  1260. static int snd_ctl_fasync(int fd, struct file * file, int on)
  1261. {
  1262. struct snd_ctl_file *ctl;
  1263. ctl = file->private_data;
  1264. return fasync_helper(fd, file, on, &ctl->fasync);
  1265. }
  1266. /*
  1267. * ioctl32 compat
  1268. */
  1269. #ifdef CONFIG_COMPAT
  1270. #include "control_compat.c"
  1271. #else
  1272. #define snd_ctl_ioctl_compat NULL
  1273. #endif
  1274. /*
  1275. * INIT PART
  1276. */
  1277. static const struct file_operations snd_ctl_f_ops =
  1278. {
  1279. .owner = THIS_MODULE,
  1280. .read = snd_ctl_read,
  1281. .open = snd_ctl_open,
  1282. .release = snd_ctl_release,
  1283. .llseek = no_llseek,
  1284. .poll = snd_ctl_poll,
  1285. .unlocked_ioctl = snd_ctl_ioctl,
  1286. .compat_ioctl = snd_ctl_ioctl_compat,
  1287. .fasync = snd_ctl_fasync,
  1288. };
  1289. /*
  1290. * registration of the control device
  1291. */
  1292. static int snd_ctl_dev_register(struct snd_device *device)
  1293. {
  1294. struct snd_card *card = device->device_data;
  1295. int err, cardnum;
  1296. char name[16];
  1297. if (snd_BUG_ON(!card))
  1298. return -ENXIO;
  1299. cardnum = card->number;
  1300. if (snd_BUG_ON(cardnum < 0 || cardnum >= SNDRV_CARDS))
  1301. return -ENXIO;
  1302. sprintf(name, "controlC%i", cardnum);
  1303. if ((err = snd_register_device(SNDRV_DEVICE_TYPE_CONTROL, card, -1,
  1304. &snd_ctl_f_ops, card, name)) < 0)
  1305. return err;
  1306. return 0;
  1307. }
  1308. /*
  1309. * disconnection of the control device
  1310. */
  1311. static int snd_ctl_dev_disconnect(struct snd_device *device)
  1312. {
  1313. struct snd_card *card = device->device_data;
  1314. struct snd_ctl_file *ctl;
  1315. int err, cardnum;
  1316. if (snd_BUG_ON(!card))
  1317. return -ENXIO;
  1318. cardnum = card->number;
  1319. if (snd_BUG_ON(cardnum < 0 || cardnum >= SNDRV_CARDS))
  1320. return -ENXIO;
  1321. read_lock(&card->ctl_files_rwlock);
  1322. list_for_each_entry(ctl, &card->ctl_files, list) {
  1323. wake_up(&ctl->change_sleep);
  1324. kill_fasync(&ctl->fasync, SIGIO, POLL_ERR);
  1325. }
  1326. read_unlock(&card->ctl_files_rwlock);
  1327. if ((err = snd_unregister_device(SNDRV_DEVICE_TYPE_CONTROL,
  1328. card, -1)) < 0)
  1329. return err;
  1330. return 0;
  1331. }
  1332. /*
  1333. * free all controls
  1334. */
  1335. static int snd_ctl_dev_free(struct snd_device *device)
  1336. {
  1337. struct snd_card *card = device->device_data;
  1338. struct snd_kcontrol *control;
  1339. down_write(&card->controls_rwsem);
  1340. while (!list_empty(&card->controls)) {
  1341. control = snd_kcontrol(card->controls.next);
  1342. snd_ctl_remove(card, control);
  1343. }
  1344. up_write(&card->controls_rwsem);
  1345. return 0;
  1346. }
  1347. /*
  1348. * create control core:
  1349. * called from init.c
  1350. */
  1351. int snd_ctl_create(struct snd_card *card)
  1352. {
  1353. static struct snd_device_ops ops = {
  1354. .dev_free = snd_ctl_dev_free,
  1355. .dev_register = snd_ctl_dev_register,
  1356. .dev_disconnect = snd_ctl_dev_disconnect,
  1357. };
  1358. if (snd_BUG_ON(!card))
  1359. return -ENXIO;
  1360. return snd_device_new(card, SNDRV_DEV_CONTROL, card, &ops);
  1361. }
  1362. /*
  1363. * Frequently used control callbacks/helpers
  1364. */
  1365. int snd_ctl_boolean_mono_info(struct snd_kcontrol *kcontrol,
  1366. struct snd_ctl_elem_info *uinfo)
  1367. {
  1368. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1369. uinfo->count = 1;
  1370. uinfo->value.integer.min = 0;
  1371. uinfo->value.integer.max = 1;
  1372. return 0;
  1373. }
  1374. EXPORT_SYMBOL(snd_ctl_boolean_mono_info);
  1375. int snd_ctl_boolean_stereo_info(struct snd_kcontrol *kcontrol,
  1376. struct snd_ctl_elem_info *uinfo)
  1377. {
  1378. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1379. uinfo->count = 2;
  1380. uinfo->value.integer.min = 0;
  1381. uinfo->value.integer.max = 1;
  1382. return 0;
  1383. }
  1384. EXPORT_SYMBOL(snd_ctl_boolean_stereo_info);
  1385. /**
  1386. * snd_ctl_enum_info - fills the info structure for an enumerated control
  1387. * @info: the structure to be filled
  1388. * @channels: the number of the control's channels; often one
  1389. * @items: the number of control values; also the size of @names
  1390. * @names: an array containing the names of all control values
  1391. *
  1392. * Sets all required fields in @info to their appropriate values.
  1393. * If the control's accessibility is not the default (readable and writable),
  1394. * the caller has to fill @info->access.
  1395. */
  1396. int snd_ctl_enum_info(struct snd_ctl_elem_info *info, unsigned int channels,
  1397. unsigned int items, const char *const names[])
  1398. {
  1399. info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  1400. info->count = channels;
  1401. info->value.enumerated.items = items;
  1402. if (info->value.enumerated.item >= items)
  1403. info->value.enumerated.item = items - 1;
  1404. strlcpy(info->value.enumerated.name,
  1405. names[info->value.enumerated.item],
  1406. sizeof(info->value.enumerated.name));
  1407. return 0;
  1408. }
  1409. EXPORT_SYMBOL(snd_ctl_enum_info);