control.c 42 KB

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