control.c 39 KB

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