control.c 45 KB

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