control.c 50 KB

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