control.c 44 KB

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