control.c 39 KB

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