control.c 35 KB

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