control.c 38 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 = get_pid(task_pid(current));
  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. put_pid(ctl->pid);
  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. if (snd_BUG_ON(!card || !id))
  130. return;
  131. read_lock(&card->ctl_files_rwlock);
  132. #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
  133. card->mixer_oss_change_count++;
  134. #endif
  135. list_for_each_entry(ctl, &card->ctl_files, list) {
  136. if (!ctl->subscribed)
  137. continue;
  138. spin_lock_irqsave(&ctl->read_lock, flags);
  139. list_for_each_entry(ev, &ctl->events, list) {
  140. if (ev->id.numid == id->numid) {
  141. ev->mask |= mask;
  142. goto _found;
  143. }
  144. }
  145. ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
  146. if (ev) {
  147. ev->id = *id;
  148. ev->mask = mask;
  149. list_add_tail(&ev->list, &ctl->events);
  150. } else {
  151. snd_printk(KERN_ERR "No memory available to allocate event\n");
  152. }
  153. _found:
  154. wake_up(&ctl->change_sleep);
  155. spin_unlock_irqrestore(&ctl->read_lock, flags);
  156. kill_fasync(&ctl->fasync, SIGIO, POLL_IN);
  157. }
  158. read_unlock(&card->ctl_files_rwlock);
  159. }
  160. EXPORT_SYMBOL(snd_ctl_notify);
  161. /**
  162. * snd_ctl_new - create a control instance from the template
  163. * @control: the control template
  164. * @access: the default control access
  165. *
  166. * Allocates a new struct snd_kcontrol instance and copies the given template
  167. * to the new instance. It does not copy volatile data (access).
  168. *
  169. * Returns the pointer of the new instance, or NULL on failure.
  170. */
  171. static struct snd_kcontrol *snd_ctl_new(struct snd_kcontrol *control,
  172. unsigned int access)
  173. {
  174. struct snd_kcontrol *kctl;
  175. unsigned int idx;
  176. if (snd_BUG_ON(!control || !control->count))
  177. return NULL;
  178. kctl = kzalloc(sizeof(*kctl) + sizeof(struct snd_kcontrol_volatile) * control->count, GFP_KERNEL);
  179. if (kctl == NULL) {
  180. snd_printk(KERN_ERR "Cannot allocate control instance\n");
  181. return NULL;
  182. }
  183. *kctl = *control;
  184. for (idx = 0; idx < kctl->count; idx++)
  185. kctl->vd[idx].access = access;
  186. return kctl;
  187. }
  188. /**
  189. * snd_ctl_new1 - create a control instance from the template
  190. * @ncontrol: the initialization record
  191. * @private_data: the private data to set
  192. *
  193. * Allocates a new struct snd_kcontrol instance and initialize from the given
  194. * template. When the access field of ncontrol is 0, it's assumed as
  195. * READWRITE access. When the count field is 0, it's assumes as one.
  196. *
  197. * Returns the pointer of the newly generated instance, or NULL on failure.
  198. */
  199. struct snd_kcontrol *snd_ctl_new1(const struct snd_kcontrol_new *ncontrol,
  200. void *private_data)
  201. {
  202. struct snd_kcontrol kctl;
  203. unsigned int access;
  204. if (snd_BUG_ON(!ncontrol || !ncontrol->info))
  205. return NULL;
  206. memset(&kctl, 0, sizeof(kctl));
  207. kctl.id.iface = ncontrol->iface;
  208. kctl.id.device = ncontrol->device;
  209. kctl.id.subdevice = ncontrol->subdevice;
  210. if (ncontrol->name) {
  211. strlcpy(kctl.id.name, ncontrol->name, sizeof(kctl.id.name));
  212. if (strcmp(ncontrol->name, kctl.id.name) != 0)
  213. snd_printk(KERN_WARNING
  214. "Control name '%s' truncated to '%s'\n",
  215. ncontrol->name, kctl.id.name);
  216. }
  217. kctl.id.index = ncontrol->index;
  218. kctl.count = ncontrol->count ? ncontrol->count : 1;
  219. access = ncontrol->access == 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE :
  220. (ncontrol->access & (SNDRV_CTL_ELEM_ACCESS_READWRITE|
  221. SNDRV_CTL_ELEM_ACCESS_INACTIVE|
  222. SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE|
  223. SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND|
  224. SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK));
  225. kctl.info = ncontrol->info;
  226. kctl.get = ncontrol->get;
  227. kctl.put = ncontrol->put;
  228. kctl.tlv.p = ncontrol->tlv.p;
  229. kctl.private_value = ncontrol->private_value;
  230. kctl.private_data = private_data;
  231. return snd_ctl_new(&kctl, access);
  232. }
  233. EXPORT_SYMBOL(snd_ctl_new1);
  234. /**
  235. * snd_ctl_free_one - release the control instance
  236. * @kcontrol: the control instance
  237. *
  238. * Releases the control instance created via snd_ctl_new()
  239. * or snd_ctl_new1().
  240. * Don't call this after the control was added to the card.
  241. */
  242. void snd_ctl_free_one(struct snd_kcontrol *kcontrol)
  243. {
  244. if (kcontrol) {
  245. if (kcontrol->private_free)
  246. kcontrol->private_free(kcontrol);
  247. kfree(kcontrol);
  248. }
  249. }
  250. EXPORT_SYMBOL(snd_ctl_free_one);
  251. static unsigned int snd_ctl_hole_check(struct snd_card *card,
  252. unsigned int count)
  253. {
  254. struct snd_kcontrol *kctl;
  255. list_for_each_entry(kctl, &card->controls, list) {
  256. if ((kctl->id.numid <= card->last_numid &&
  257. kctl->id.numid + kctl->count > card->last_numid) ||
  258. (kctl->id.numid <= card->last_numid + count - 1 &&
  259. kctl->id.numid + kctl->count > card->last_numid + count - 1))
  260. return card->last_numid = kctl->id.numid + kctl->count - 1;
  261. }
  262. return card->last_numid;
  263. }
  264. static int snd_ctl_find_hole(struct snd_card *card, unsigned int count)
  265. {
  266. unsigned int last_numid, iter = 100000;
  267. last_numid = card->last_numid;
  268. while (last_numid != snd_ctl_hole_check(card, count)) {
  269. if (--iter == 0) {
  270. /* this situation is very unlikely */
  271. snd_printk(KERN_ERR "unable to allocate new control numid\n");
  272. return -ENOMEM;
  273. }
  274. last_numid = card->last_numid;
  275. }
  276. return 0;
  277. }
  278. /**
  279. * snd_ctl_add - add the control instance to the card
  280. * @card: the card instance
  281. * @kcontrol: the control instance to add
  282. *
  283. * Adds the control instance created via snd_ctl_new() or
  284. * snd_ctl_new1() to the given card. Assigns also an unique
  285. * numid used for fast search.
  286. *
  287. * Returns zero if successful, or a negative error code on failure.
  288. *
  289. * It frees automatically the control which cannot be added.
  290. */
  291. int snd_ctl_add(struct snd_card *card, struct snd_kcontrol *kcontrol)
  292. {
  293. struct snd_ctl_elem_id id;
  294. unsigned int idx;
  295. int err = -EINVAL;
  296. if (! kcontrol)
  297. return err;
  298. if (snd_BUG_ON(!card || !kcontrol->info))
  299. goto error;
  300. id = kcontrol->id;
  301. down_write(&card->controls_rwsem);
  302. if (snd_ctl_find_id(card, &id)) {
  303. up_write(&card->controls_rwsem);
  304. snd_printd(KERN_ERR "control %i:%i:%i:%s:%i is already present\n",
  305. id.iface,
  306. id.device,
  307. id.subdevice,
  308. id.name,
  309. id.index);
  310. err = -EBUSY;
  311. goto error;
  312. }
  313. if (snd_ctl_find_hole(card, kcontrol->count) < 0) {
  314. up_write(&card->controls_rwsem);
  315. err = -ENOMEM;
  316. goto error;
  317. }
  318. list_add_tail(&kcontrol->list, &card->controls);
  319. card->controls_count += kcontrol->count;
  320. kcontrol->id.numid = card->last_numid + 1;
  321. card->last_numid += kcontrol->count;
  322. up_write(&card->controls_rwsem);
  323. for (idx = 0; idx < kcontrol->count; idx++, id.index++, id.numid++)
  324. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_ADD, &id);
  325. return 0;
  326. error:
  327. snd_ctl_free_one(kcontrol);
  328. return err;
  329. }
  330. EXPORT_SYMBOL(snd_ctl_add);
  331. /**
  332. * snd_ctl_remove - remove the control from the card and release it
  333. * @card: the card instance
  334. * @kcontrol: the control instance to remove
  335. *
  336. * Removes the control from the card and then releases the instance.
  337. * You don't need to call snd_ctl_free_one(). You must be in
  338. * the write lock - down_write(&card->controls_rwsem).
  339. *
  340. * Returns 0 if successful, or a negative error code on failure.
  341. */
  342. int snd_ctl_remove(struct snd_card *card, struct snd_kcontrol *kcontrol)
  343. {
  344. struct snd_ctl_elem_id id;
  345. unsigned int idx;
  346. if (snd_BUG_ON(!card || !kcontrol))
  347. return -EINVAL;
  348. list_del(&kcontrol->list);
  349. card->controls_count -= kcontrol->count;
  350. id = kcontrol->id;
  351. for (idx = 0; idx < kcontrol->count; idx++, id.index++, id.numid++)
  352. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_REMOVE, &id);
  353. snd_ctl_free_one(kcontrol);
  354. return 0;
  355. }
  356. EXPORT_SYMBOL(snd_ctl_remove);
  357. /**
  358. * snd_ctl_remove_id - remove the control of the given id and release it
  359. * @card: the card instance
  360. * @id: the control id to remove
  361. *
  362. * Finds the control instance with the given id, removes it from the
  363. * card list and releases it.
  364. *
  365. * Returns 0 if successful, or a negative error code on failure.
  366. */
  367. int snd_ctl_remove_id(struct snd_card *card, struct snd_ctl_elem_id *id)
  368. {
  369. struct snd_kcontrol *kctl;
  370. int ret;
  371. down_write(&card->controls_rwsem);
  372. kctl = snd_ctl_find_id(card, id);
  373. if (kctl == NULL) {
  374. up_write(&card->controls_rwsem);
  375. return -ENOENT;
  376. }
  377. ret = snd_ctl_remove(card, kctl);
  378. up_write(&card->controls_rwsem);
  379. return ret;
  380. }
  381. EXPORT_SYMBOL(snd_ctl_remove_id);
  382. /**
  383. * snd_ctl_remove_user_ctl - remove and release the unlocked user control
  384. * @file: active control handle
  385. * @id: the control id to remove
  386. *
  387. * Finds the control instance with the given id, removes it from the
  388. * card list and releases it.
  389. *
  390. * Returns 0 if successful, or a negative error code on failure.
  391. */
  392. static int snd_ctl_remove_user_ctl(struct snd_ctl_file * file,
  393. struct snd_ctl_elem_id *id)
  394. {
  395. struct snd_card *card = file->card;
  396. struct snd_kcontrol *kctl;
  397. int idx, ret;
  398. down_write(&card->controls_rwsem);
  399. kctl = snd_ctl_find_id(card, id);
  400. if (kctl == NULL) {
  401. ret = -ENOENT;
  402. goto error;
  403. }
  404. if (!(kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_USER)) {
  405. ret = -EINVAL;
  406. goto error;
  407. }
  408. for (idx = 0; idx < kctl->count; idx++)
  409. if (kctl->vd[idx].owner != NULL && kctl->vd[idx].owner != file) {
  410. ret = -EBUSY;
  411. goto error;
  412. }
  413. ret = snd_ctl_remove(card, kctl);
  414. if (ret < 0)
  415. goto error;
  416. card->user_ctl_count--;
  417. error:
  418. up_write(&card->controls_rwsem);
  419. return ret;
  420. }
  421. /**
  422. * snd_ctl_rename_id - replace the id of a control on the card
  423. * @card: the card instance
  424. * @src_id: the old id
  425. * @dst_id: the new id
  426. *
  427. * Finds the control with the old id from the card, and replaces the
  428. * id with the new one.
  429. *
  430. * Returns zero if successful, or a negative error code on failure.
  431. */
  432. int snd_ctl_rename_id(struct snd_card *card, struct snd_ctl_elem_id *src_id,
  433. struct snd_ctl_elem_id *dst_id)
  434. {
  435. struct snd_kcontrol *kctl;
  436. down_write(&card->controls_rwsem);
  437. kctl = snd_ctl_find_id(card, src_id);
  438. if (kctl == NULL) {
  439. up_write(&card->controls_rwsem);
  440. return -ENOENT;
  441. }
  442. kctl->id = *dst_id;
  443. kctl->id.numid = card->last_numid + 1;
  444. card->last_numid += kctl->count;
  445. up_write(&card->controls_rwsem);
  446. return 0;
  447. }
  448. EXPORT_SYMBOL(snd_ctl_rename_id);
  449. /**
  450. * snd_ctl_find_numid - find the control instance with the given number-id
  451. * @card: the card instance
  452. * @numid: the number-id to search
  453. *
  454. * Finds the control instance with the given number-id from the card.
  455. *
  456. * Returns the pointer of the instance if found, or NULL if not.
  457. *
  458. * The caller must down card->controls_rwsem before calling this function
  459. * (if the race condition can happen).
  460. */
  461. struct snd_kcontrol *snd_ctl_find_numid(struct snd_card *card, unsigned int numid)
  462. {
  463. struct snd_kcontrol *kctl;
  464. if (snd_BUG_ON(!card || !numid))
  465. return NULL;
  466. list_for_each_entry(kctl, &card->controls, list) {
  467. if (kctl->id.numid <= numid && kctl->id.numid + kctl->count > numid)
  468. return kctl;
  469. }
  470. return NULL;
  471. }
  472. EXPORT_SYMBOL(snd_ctl_find_numid);
  473. /**
  474. * snd_ctl_find_id - find the control instance with the given id
  475. * @card: the card instance
  476. * @id: the id to search
  477. *
  478. * Finds the control instance with the given id from the card.
  479. *
  480. * Returns the pointer of the instance if found, or NULL if not.
  481. *
  482. * The caller must down card->controls_rwsem before calling this function
  483. * (if the race condition can happen).
  484. */
  485. struct snd_kcontrol *snd_ctl_find_id(struct snd_card *card,
  486. struct snd_ctl_elem_id *id)
  487. {
  488. struct snd_kcontrol *kctl;
  489. if (snd_BUG_ON(!card || !id))
  490. return NULL;
  491. if (id->numid != 0)
  492. return snd_ctl_find_numid(card, id->numid);
  493. list_for_each_entry(kctl, &card->controls, list) {
  494. if (kctl->id.iface != id->iface)
  495. continue;
  496. if (kctl->id.device != id->device)
  497. continue;
  498. if (kctl->id.subdevice != id->subdevice)
  499. continue;
  500. if (strncmp(kctl->id.name, id->name, sizeof(kctl->id.name)))
  501. continue;
  502. if (kctl->id.index > id->index)
  503. continue;
  504. if (kctl->id.index + kctl->count <= id->index)
  505. continue;
  506. return kctl;
  507. }
  508. return NULL;
  509. }
  510. EXPORT_SYMBOL(snd_ctl_find_id);
  511. static int snd_ctl_card_info(struct snd_card *card, struct snd_ctl_file * ctl,
  512. unsigned int cmd, void __user *arg)
  513. {
  514. struct snd_ctl_card_info *info;
  515. info = kzalloc(sizeof(*info), GFP_KERNEL);
  516. if (! info)
  517. return -ENOMEM;
  518. down_read(&snd_ioctl_rwsem);
  519. info->card = card->number;
  520. strlcpy(info->id, card->id, sizeof(info->id));
  521. strlcpy(info->driver, card->driver, sizeof(info->driver));
  522. strlcpy(info->name, card->shortname, sizeof(info->name));
  523. strlcpy(info->longname, card->longname, sizeof(info->longname));
  524. strlcpy(info->mixername, card->mixername, sizeof(info->mixername));
  525. strlcpy(info->components, card->components, sizeof(info->components));
  526. up_read(&snd_ioctl_rwsem);
  527. if (copy_to_user(arg, info, sizeof(struct snd_ctl_card_info))) {
  528. kfree(info);
  529. return -EFAULT;
  530. }
  531. kfree(info);
  532. return 0;
  533. }
  534. static int snd_ctl_elem_list(struct snd_card *card,
  535. struct snd_ctl_elem_list __user *_list)
  536. {
  537. struct list_head *plist;
  538. struct snd_ctl_elem_list list;
  539. struct snd_kcontrol *kctl;
  540. struct snd_ctl_elem_id *dst, *id;
  541. unsigned int offset, space, first, jidx;
  542. if (copy_from_user(&list, _list, sizeof(list)))
  543. return -EFAULT;
  544. offset = list.offset;
  545. space = list.space;
  546. first = 0;
  547. /* try limit maximum space */
  548. if (space > 16384)
  549. return -ENOMEM;
  550. if (space > 0) {
  551. /* allocate temporary buffer for atomic operation */
  552. dst = vmalloc(space * sizeof(struct snd_ctl_elem_id));
  553. if (dst == NULL)
  554. return -ENOMEM;
  555. down_read(&card->controls_rwsem);
  556. list.count = card->controls_count;
  557. plist = card->controls.next;
  558. while (plist != &card->controls) {
  559. if (offset == 0)
  560. break;
  561. kctl = snd_kcontrol(plist);
  562. if (offset < kctl->count)
  563. break;
  564. offset -= kctl->count;
  565. plist = plist->next;
  566. }
  567. list.used = 0;
  568. id = dst;
  569. while (space > 0 && plist != &card->controls) {
  570. kctl = snd_kcontrol(plist);
  571. for (jidx = offset; space > 0 && jidx < kctl->count; jidx++) {
  572. snd_ctl_build_ioff(id, kctl, jidx);
  573. id++;
  574. space--;
  575. list.used++;
  576. }
  577. plist = plist->next;
  578. offset = 0;
  579. }
  580. up_read(&card->controls_rwsem);
  581. if (list.used > 0 &&
  582. copy_to_user(list.pids, dst,
  583. list.used * sizeof(struct snd_ctl_elem_id))) {
  584. vfree(dst);
  585. return -EFAULT;
  586. }
  587. vfree(dst);
  588. } else {
  589. down_read(&card->controls_rwsem);
  590. list.count = card->controls_count;
  591. up_read(&card->controls_rwsem);
  592. }
  593. if (copy_to_user(_list, &list, sizeof(list)))
  594. return -EFAULT;
  595. return 0;
  596. }
  597. static int snd_ctl_elem_info(struct snd_ctl_file *ctl,
  598. struct snd_ctl_elem_info *info)
  599. {
  600. struct snd_card *card = ctl->card;
  601. struct snd_kcontrol *kctl;
  602. struct snd_kcontrol_volatile *vd;
  603. unsigned int index_offset;
  604. int result;
  605. down_read(&card->controls_rwsem);
  606. kctl = snd_ctl_find_id(card, &info->id);
  607. if (kctl == NULL) {
  608. up_read(&card->controls_rwsem);
  609. return -ENOENT;
  610. }
  611. #ifdef CONFIG_SND_DEBUG
  612. info->access = 0;
  613. #endif
  614. result = kctl->info(kctl, info);
  615. if (result >= 0) {
  616. snd_BUG_ON(info->access);
  617. index_offset = snd_ctl_get_ioff(kctl, &info->id);
  618. vd = &kctl->vd[index_offset];
  619. snd_ctl_build_ioff(&info->id, kctl, index_offset);
  620. info->access = vd->access;
  621. if (vd->owner) {
  622. info->access |= SNDRV_CTL_ELEM_ACCESS_LOCK;
  623. if (vd->owner == ctl)
  624. info->access |= SNDRV_CTL_ELEM_ACCESS_OWNER;
  625. info->owner = pid_vnr(vd->owner->pid);
  626. } else {
  627. info->owner = -1;
  628. }
  629. }
  630. up_read(&card->controls_rwsem);
  631. return result;
  632. }
  633. static int snd_ctl_elem_info_user(struct snd_ctl_file *ctl,
  634. struct snd_ctl_elem_info __user *_info)
  635. {
  636. struct snd_ctl_elem_info info;
  637. int result;
  638. if (copy_from_user(&info, _info, sizeof(info)))
  639. return -EFAULT;
  640. snd_power_lock(ctl->card);
  641. result = snd_power_wait(ctl->card, SNDRV_CTL_POWER_D0);
  642. if (result >= 0)
  643. result = snd_ctl_elem_info(ctl, &info);
  644. snd_power_unlock(ctl->card);
  645. if (result >= 0)
  646. if (copy_to_user(_info, &info, sizeof(info)))
  647. return -EFAULT;
  648. return result;
  649. }
  650. static int snd_ctl_elem_read(struct snd_card *card,
  651. struct snd_ctl_elem_value *control)
  652. {
  653. struct snd_kcontrol *kctl;
  654. struct snd_kcontrol_volatile *vd;
  655. unsigned int index_offset;
  656. int result;
  657. down_read(&card->controls_rwsem);
  658. kctl = snd_ctl_find_id(card, &control->id);
  659. if (kctl == NULL) {
  660. result = -ENOENT;
  661. } else {
  662. index_offset = snd_ctl_get_ioff(kctl, &control->id);
  663. vd = &kctl->vd[index_offset];
  664. if ((vd->access & SNDRV_CTL_ELEM_ACCESS_READ) &&
  665. kctl->get != NULL) {
  666. snd_ctl_build_ioff(&control->id, kctl, index_offset);
  667. result = kctl->get(kctl, control);
  668. } else
  669. result = -EPERM;
  670. }
  671. up_read(&card->controls_rwsem);
  672. return result;
  673. }
  674. static int snd_ctl_elem_read_user(struct snd_card *card,
  675. struct snd_ctl_elem_value __user *_control)
  676. {
  677. struct snd_ctl_elem_value *control;
  678. int result;
  679. control = memdup_user(_control, sizeof(*control));
  680. if (IS_ERR(control))
  681. return PTR_ERR(control);
  682. snd_power_lock(card);
  683. result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
  684. if (result >= 0)
  685. result = snd_ctl_elem_read(card, control);
  686. snd_power_unlock(card);
  687. if (result >= 0)
  688. if (copy_to_user(_control, control, sizeof(*control)))
  689. result = -EFAULT;
  690. kfree(control);
  691. return result;
  692. }
  693. static int snd_ctl_elem_write(struct snd_card *card, struct snd_ctl_file *file,
  694. struct snd_ctl_elem_value *control)
  695. {
  696. struct snd_kcontrol *kctl;
  697. struct snd_kcontrol_volatile *vd;
  698. unsigned int index_offset;
  699. int result;
  700. down_read(&card->controls_rwsem);
  701. kctl = snd_ctl_find_id(card, &control->id);
  702. if (kctl == NULL) {
  703. result = -ENOENT;
  704. } else {
  705. index_offset = snd_ctl_get_ioff(kctl, &control->id);
  706. vd = &kctl->vd[index_offset];
  707. if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_WRITE) ||
  708. kctl->put == NULL ||
  709. (file && vd->owner && vd->owner != file)) {
  710. result = -EPERM;
  711. } else {
  712. snd_ctl_build_ioff(&control->id, kctl, index_offset);
  713. result = kctl->put(kctl, control);
  714. }
  715. if (result > 0) {
  716. up_read(&card->controls_rwsem);
  717. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
  718. &control->id);
  719. return 0;
  720. }
  721. }
  722. up_read(&card->controls_rwsem);
  723. return result;
  724. }
  725. static int snd_ctl_elem_write_user(struct snd_ctl_file *file,
  726. struct snd_ctl_elem_value __user *_control)
  727. {
  728. struct snd_ctl_elem_value *control;
  729. struct snd_card *card;
  730. int result;
  731. control = memdup_user(_control, sizeof(*control));
  732. if (IS_ERR(control))
  733. return PTR_ERR(control);
  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. result = 0;
  767. }
  768. }
  769. up_write(&card->controls_rwsem);
  770. return result;
  771. }
  772. static int snd_ctl_elem_unlock(struct snd_ctl_file *file,
  773. struct snd_ctl_elem_id __user *_id)
  774. {
  775. struct snd_card *card = file->card;
  776. struct snd_ctl_elem_id id;
  777. struct snd_kcontrol *kctl;
  778. struct snd_kcontrol_volatile *vd;
  779. int result;
  780. if (copy_from_user(&id, _id, sizeof(id)))
  781. return -EFAULT;
  782. down_write(&card->controls_rwsem);
  783. kctl = snd_ctl_find_id(card, &id);
  784. if (kctl == NULL) {
  785. result = -ENOENT;
  786. } else {
  787. vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
  788. if (vd->owner == NULL)
  789. result = -EINVAL;
  790. else if (vd->owner != file)
  791. result = -EPERM;
  792. else {
  793. vd->owner = NULL;
  794. result = 0;
  795. }
  796. }
  797. up_write(&card->controls_rwsem);
  798. return result;
  799. }
  800. struct user_element {
  801. struct snd_ctl_elem_info info;
  802. void *elem_data; /* element data */
  803. unsigned long elem_data_size; /* size of element data in bytes */
  804. void *tlv_data; /* TLV data */
  805. unsigned long tlv_data_size; /* TLV data size */
  806. void *priv_data; /* private data (like strings for enumerated type) */
  807. unsigned long priv_data_size; /* size of private data in bytes */
  808. };
  809. static int snd_ctl_elem_user_info(struct snd_kcontrol *kcontrol,
  810. struct snd_ctl_elem_info *uinfo)
  811. {
  812. struct user_element *ue = kcontrol->private_data;
  813. *uinfo = ue->info;
  814. return 0;
  815. }
  816. static int snd_ctl_elem_user_get(struct snd_kcontrol *kcontrol,
  817. struct snd_ctl_elem_value *ucontrol)
  818. {
  819. struct user_element *ue = kcontrol->private_data;
  820. memcpy(&ucontrol->value, ue->elem_data, ue->elem_data_size);
  821. return 0;
  822. }
  823. static int snd_ctl_elem_user_put(struct snd_kcontrol *kcontrol,
  824. struct snd_ctl_elem_value *ucontrol)
  825. {
  826. int change;
  827. struct user_element *ue = kcontrol->private_data;
  828. change = memcmp(&ucontrol->value, ue->elem_data, ue->elem_data_size) != 0;
  829. if (change)
  830. memcpy(ue->elem_data, &ucontrol->value, ue->elem_data_size);
  831. return change;
  832. }
  833. static int snd_ctl_elem_user_tlv(struct snd_kcontrol *kcontrol,
  834. int op_flag,
  835. unsigned int size,
  836. unsigned int __user *tlv)
  837. {
  838. struct user_element *ue = kcontrol->private_data;
  839. int change = 0;
  840. void *new_data;
  841. if (op_flag > 0) {
  842. if (size > 1024 * 128) /* sane value */
  843. return -EINVAL;
  844. new_data = memdup_user(tlv, size);
  845. if (IS_ERR(new_data))
  846. return PTR_ERR(new_data);
  847. change = ue->tlv_data_size != size;
  848. if (!change)
  849. change = memcmp(ue->tlv_data, new_data, size);
  850. kfree(ue->tlv_data);
  851. ue->tlv_data = new_data;
  852. ue->tlv_data_size = size;
  853. } else {
  854. if (! ue->tlv_data_size || ! ue->tlv_data)
  855. return -ENXIO;
  856. if (size < ue->tlv_data_size)
  857. return -ENOSPC;
  858. if (copy_to_user(tlv, ue->tlv_data, ue->tlv_data_size))
  859. return -EFAULT;
  860. }
  861. return change;
  862. }
  863. static void snd_ctl_elem_user_free(struct snd_kcontrol *kcontrol)
  864. {
  865. struct user_element *ue = kcontrol->private_data;
  866. if (ue->tlv_data)
  867. kfree(ue->tlv_data);
  868. kfree(ue);
  869. }
  870. static int snd_ctl_elem_add(struct snd_ctl_file *file,
  871. struct snd_ctl_elem_info *info, int replace)
  872. {
  873. struct snd_card *card = file->card;
  874. struct snd_kcontrol kctl, *_kctl;
  875. unsigned int access;
  876. long private_size;
  877. struct user_element *ue;
  878. int idx, err;
  879. if (card->user_ctl_count >= MAX_USER_CONTROLS)
  880. return -ENOMEM;
  881. if (info->count < 1)
  882. return -EINVAL;
  883. access = info->access == 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE :
  884. (info->access & (SNDRV_CTL_ELEM_ACCESS_READWRITE|
  885. SNDRV_CTL_ELEM_ACCESS_INACTIVE|
  886. SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE));
  887. info->id.numid = 0;
  888. memset(&kctl, 0, sizeof(kctl));
  889. down_write(&card->controls_rwsem);
  890. _kctl = snd_ctl_find_id(card, &info->id);
  891. err = 0;
  892. if (_kctl) {
  893. if (replace)
  894. err = snd_ctl_remove(card, _kctl);
  895. else
  896. err = -EBUSY;
  897. } else {
  898. if (replace)
  899. err = -ENOENT;
  900. }
  901. up_write(&card->controls_rwsem);
  902. if (err < 0)
  903. return err;
  904. memcpy(&kctl.id, &info->id, sizeof(info->id));
  905. kctl.count = info->owner ? info->owner : 1;
  906. access |= SNDRV_CTL_ELEM_ACCESS_USER;
  907. kctl.info = snd_ctl_elem_user_info;
  908. if (access & SNDRV_CTL_ELEM_ACCESS_READ)
  909. kctl.get = snd_ctl_elem_user_get;
  910. if (access & SNDRV_CTL_ELEM_ACCESS_WRITE)
  911. kctl.put = snd_ctl_elem_user_put;
  912. if (access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE) {
  913. kctl.tlv.c = snd_ctl_elem_user_tlv;
  914. access |= SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
  915. }
  916. switch (info->type) {
  917. case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
  918. case SNDRV_CTL_ELEM_TYPE_INTEGER:
  919. private_size = sizeof(long);
  920. if (info->count > 128)
  921. return -EINVAL;
  922. break;
  923. case SNDRV_CTL_ELEM_TYPE_INTEGER64:
  924. private_size = sizeof(long long);
  925. if (info->count > 64)
  926. return -EINVAL;
  927. break;
  928. case SNDRV_CTL_ELEM_TYPE_BYTES:
  929. private_size = sizeof(unsigned char);
  930. if (info->count > 512)
  931. return -EINVAL;
  932. break;
  933. case SNDRV_CTL_ELEM_TYPE_IEC958:
  934. private_size = sizeof(struct snd_aes_iec958);
  935. if (info->count != 1)
  936. return -EINVAL;
  937. break;
  938. default:
  939. return -EINVAL;
  940. }
  941. private_size *= info->count;
  942. ue = kzalloc(sizeof(struct user_element) + private_size, GFP_KERNEL);
  943. if (ue == NULL)
  944. return -ENOMEM;
  945. ue->info = *info;
  946. ue->info.access = 0;
  947. ue->elem_data = (char *)ue + sizeof(*ue);
  948. ue->elem_data_size = private_size;
  949. kctl.private_free = snd_ctl_elem_user_free;
  950. _kctl = snd_ctl_new(&kctl, access);
  951. if (_kctl == NULL) {
  952. kfree(ue);
  953. return -ENOMEM;
  954. }
  955. _kctl->private_data = ue;
  956. for (idx = 0; idx < _kctl->count; idx++)
  957. _kctl->vd[idx].owner = file;
  958. err = snd_ctl_add(card, _kctl);
  959. if (err < 0)
  960. return err;
  961. down_write(&card->controls_rwsem);
  962. card->user_ctl_count++;
  963. up_write(&card->controls_rwsem);
  964. return 0;
  965. }
  966. static int snd_ctl_elem_add_user(struct snd_ctl_file *file,
  967. struct snd_ctl_elem_info __user *_info, int replace)
  968. {
  969. struct snd_ctl_elem_info info;
  970. if (copy_from_user(&info, _info, sizeof(info)))
  971. return -EFAULT;
  972. return snd_ctl_elem_add(file, &info, replace);
  973. }
  974. static int snd_ctl_elem_remove(struct snd_ctl_file *file,
  975. struct snd_ctl_elem_id __user *_id)
  976. {
  977. struct snd_ctl_elem_id id;
  978. if (copy_from_user(&id, _id, sizeof(id)))
  979. return -EFAULT;
  980. return snd_ctl_remove_user_ctl(file, &id);
  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) * 2)
  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 (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);