control.c 39 KB

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