control.c 36 KB

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