control.c 49 KB

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