control.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992
  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. /* add a new kcontrol object; call with card->controls_rwsem locked */
  315. static int __snd_ctl_add(struct snd_card *card, struct snd_kcontrol *kcontrol)
  316. {
  317. struct snd_ctl_elem_id id;
  318. unsigned int idx;
  319. unsigned int count;
  320. id = kcontrol->id;
  321. if (id.index > UINT_MAX - kcontrol->count)
  322. return -EINVAL;
  323. if (snd_ctl_find_id(card, &id)) {
  324. dev_err(card->dev,
  325. "control %i:%i:%i:%s:%i is already present\n",
  326. id.iface, id.device, id.subdevice, id.name, id.index);
  327. return -EBUSY;
  328. }
  329. if (snd_ctl_find_hole(card, kcontrol->count) < 0)
  330. return -ENOMEM;
  331. list_add_tail(&kcontrol->list, &card->controls);
  332. card->controls_count += kcontrol->count;
  333. kcontrol->id.numid = card->last_numid + 1;
  334. card->last_numid += kcontrol->count;
  335. id = kcontrol->id;
  336. count = kcontrol->count;
  337. for (idx = 0; idx < count; idx++, id.index++, id.numid++)
  338. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_ADD, &id);
  339. return 0;
  340. }
  341. /**
  342. * snd_ctl_add - add the control instance to the card
  343. * @card: the card instance
  344. * @kcontrol: the control instance to add
  345. *
  346. * Adds the control instance created via snd_ctl_new() or
  347. * snd_ctl_new1() to the given card. Assigns also an unique
  348. * numid used for fast search.
  349. *
  350. * It frees automatically the control which cannot be added.
  351. *
  352. * Return: Zero if successful, or a negative error code on failure.
  353. *
  354. */
  355. int snd_ctl_add(struct snd_card *card, struct snd_kcontrol *kcontrol)
  356. {
  357. int err = -EINVAL;
  358. if (! kcontrol)
  359. return err;
  360. if (snd_BUG_ON(!card || !kcontrol->info))
  361. goto error;
  362. down_write(&card->controls_rwsem);
  363. err = __snd_ctl_add(card, kcontrol);
  364. up_write(&card->controls_rwsem);
  365. if (err < 0)
  366. goto error;
  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. result = snd_power_wait(ctl->card, SNDRV_CTL_POWER_D0);
  797. if (result < 0)
  798. return result;
  799. result = snd_ctl_elem_info(ctl, &info);
  800. if (result < 0)
  801. return result;
  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. kctl = snd_ctl_find_id(card, &control->id);
  813. if (kctl == NULL)
  814. return -ENOENT;
  815. index_offset = snd_ctl_get_ioff(kctl, &control->id);
  816. vd = &kctl->vd[index_offset];
  817. if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_READ) || kctl->get == NULL)
  818. return -EPERM;
  819. snd_ctl_build_ioff(&control->id, kctl, index_offset);
  820. return kctl->get(kctl, control);
  821. }
  822. static int snd_ctl_elem_read_user(struct snd_card *card,
  823. struct snd_ctl_elem_value __user *_control)
  824. {
  825. struct snd_ctl_elem_value *control;
  826. int result;
  827. control = memdup_user(_control, sizeof(*control));
  828. if (IS_ERR(control))
  829. return PTR_ERR(control);
  830. result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
  831. if (result < 0)
  832. goto error;
  833. down_read(&card->controls_rwsem);
  834. result = snd_ctl_elem_read(card, control);
  835. up_read(&card->controls_rwsem);
  836. if (result < 0)
  837. goto error;
  838. if (copy_to_user(_control, control, sizeof(*control)))
  839. result = -EFAULT;
  840. error:
  841. kfree(control);
  842. return result;
  843. }
  844. static int snd_ctl_elem_write(struct snd_card *card, struct snd_ctl_file *file,
  845. struct snd_ctl_elem_value *control)
  846. {
  847. struct snd_kcontrol *kctl;
  848. struct snd_kcontrol_volatile *vd;
  849. unsigned int index_offset;
  850. int result;
  851. kctl = snd_ctl_find_id(card, &control->id);
  852. if (kctl == NULL)
  853. return -ENOENT;
  854. index_offset = snd_ctl_get_ioff(kctl, &control->id);
  855. vd = &kctl->vd[index_offset];
  856. if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_WRITE) || kctl->put == NULL ||
  857. (file && vd->owner && vd->owner != file)) {
  858. return -EPERM;
  859. }
  860. snd_ctl_build_ioff(&control->id, kctl, index_offset);
  861. result = kctl->put(kctl, control);
  862. if (result < 0)
  863. return result;
  864. if (result > 0) {
  865. struct snd_ctl_elem_id id = control->id;
  866. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, &id);
  867. }
  868. return 0;
  869. }
  870. static int snd_ctl_elem_write_user(struct snd_ctl_file *file,
  871. struct snd_ctl_elem_value __user *_control)
  872. {
  873. struct snd_ctl_elem_value *control;
  874. struct snd_card *card;
  875. int result;
  876. control = memdup_user(_control, sizeof(*control));
  877. if (IS_ERR(control))
  878. return PTR_ERR(control);
  879. card = file->card;
  880. result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
  881. if (result < 0)
  882. goto error;
  883. down_write(&card->controls_rwsem);
  884. result = snd_ctl_elem_write(card, file, control);
  885. up_write(&card->controls_rwsem);
  886. if (result < 0)
  887. goto error;
  888. if (copy_to_user(_control, control, sizeof(*control)))
  889. result = -EFAULT;
  890. error:
  891. kfree(control);
  892. return result;
  893. }
  894. static int snd_ctl_elem_lock(struct snd_ctl_file *file,
  895. struct snd_ctl_elem_id __user *_id)
  896. {
  897. struct snd_card *card = file->card;
  898. struct snd_ctl_elem_id id;
  899. struct snd_kcontrol *kctl;
  900. struct snd_kcontrol_volatile *vd;
  901. int result;
  902. if (copy_from_user(&id, _id, sizeof(id)))
  903. return -EFAULT;
  904. down_write(&card->controls_rwsem);
  905. kctl = snd_ctl_find_id(card, &id);
  906. if (kctl == NULL) {
  907. result = -ENOENT;
  908. } else {
  909. vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
  910. if (vd->owner != NULL)
  911. result = -EBUSY;
  912. else {
  913. vd->owner = file;
  914. result = 0;
  915. }
  916. }
  917. up_write(&card->controls_rwsem);
  918. return result;
  919. }
  920. static int snd_ctl_elem_unlock(struct snd_ctl_file *file,
  921. struct snd_ctl_elem_id __user *_id)
  922. {
  923. struct snd_card *card = file->card;
  924. struct snd_ctl_elem_id id;
  925. struct snd_kcontrol *kctl;
  926. struct snd_kcontrol_volatile *vd;
  927. int result;
  928. if (copy_from_user(&id, _id, sizeof(id)))
  929. return -EFAULT;
  930. down_write(&card->controls_rwsem);
  931. kctl = snd_ctl_find_id(card, &id);
  932. if (kctl == NULL) {
  933. result = -ENOENT;
  934. } else {
  935. vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
  936. if (vd->owner == NULL)
  937. result = -EINVAL;
  938. else if (vd->owner != file)
  939. result = -EPERM;
  940. else {
  941. vd->owner = NULL;
  942. result = 0;
  943. }
  944. }
  945. up_write(&card->controls_rwsem);
  946. return result;
  947. }
  948. struct user_element {
  949. struct snd_ctl_elem_info info;
  950. struct snd_card *card;
  951. char *elem_data; /* element data */
  952. unsigned long elem_data_size; /* size of element data in bytes */
  953. void *tlv_data; /* TLV data */
  954. unsigned long tlv_data_size; /* TLV data size */
  955. void *priv_data; /* private data (like strings for enumerated type) */
  956. };
  957. static int snd_ctl_elem_user_info(struct snd_kcontrol *kcontrol,
  958. struct snd_ctl_elem_info *uinfo)
  959. {
  960. struct user_element *ue = kcontrol->private_data;
  961. unsigned int offset;
  962. offset = snd_ctl_get_ioff(kcontrol, &uinfo->id);
  963. *uinfo = ue->info;
  964. snd_ctl_build_ioff(&uinfo->id, kcontrol, offset);
  965. return 0;
  966. }
  967. static int snd_ctl_elem_user_enum_info(struct snd_kcontrol *kcontrol,
  968. struct snd_ctl_elem_info *uinfo)
  969. {
  970. struct user_element *ue = kcontrol->private_data;
  971. const char *names;
  972. unsigned int item;
  973. unsigned int offset;
  974. item = uinfo->value.enumerated.item;
  975. offset = snd_ctl_get_ioff(kcontrol, &uinfo->id);
  976. *uinfo = ue->info;
  977. snd_ctl_build_ioff(&uinfo->id, kcontrol, offset);
  978. item = min(item, uinfo->value.enumerated.items - 1);
  979. uinfo->value.enumerated.item = item;
  980. names = ue->priv_data;
  981. for (; item > 0; --item)
  982. names += strlen(names) + 1;
  983. strcpy(uinfo->value.enumerated.name, names);
  984. return 0;
  985. }
  986. static int snd_ctl_elem_user_get(struct snd_kcontrol *kcontrol,
  987. struct snd_ctl_elem_value *ucontrol)
  988. {
  989. struct user_element *ue = kcontrol->private_data;
  990. unsigned int size = ue->elem_data_size;
  991. char *src = ue->elem_data +
  992. snd_ctl_get_ioff(kcontrol, &ucontrol->id) * size;
  993. memcpy(&ucontrol->value, src, size);
  994. return 0;
  995. }
  996. static int snd_ctl_elem_user_put(struct snd_kcontrol *kcontrol,
  997. struct snd_ctl_elem_value *ucontrol)
  998. {
  999. int change;
  1000. struct user_element *ue = kcontrol->private_data;
  1001. unsigned int size = ue->elem_data_size;
  1002. char *dst = ue->elem_data +
  1003. snd_ctl_get_ioff(kcontrol, &ucontrol->id) * size;
  1004. change = memcmp(&ucontrol->value, dst, size) != 0;
  1005. if (change)
  1006. memcpy(dst, &ucontrol->value, size);
  1007. return change;
  1008. }
  1009. static int replace_user_tlv(struct snd_kcontrol *kctl, unsigned int __user *buf,
  1010. unsigned int size)
  1011. {
  1012. struct user_element *ue = kctl->private_data;
  1013. unsigned int *container;
  1014. struct snd_ctl_elem_id id;
  1015. unsigned int mask = 0;
  1016. int i;
  1017. int change;
  1018. if (size > 1024 * 128) /* sane value */
  1019. return -EINVAL;
  1020. container = memdup_user(buf, size);
  1021. if (IS_ERR(container))
  1022. return PTR_ERR(container);
  1023. change = ue->tlv_data_size != size;
  1024. if (!change)
  1025. change = memcmp(ue->tlv_data, container, size) != 0;
  1026. if (!change) {
  1027. kfree(container);
  1028. return 0;
  1029. }
  1030. if (ue->tlv_data == NULL) {
  1031. /* Now TLV data is available. */
  1032. for (i = 0; i < kctl->count; ++i)
  1033. kctl->vd[i].access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ;
  1034. mask = SNDRV_CTL_EVENT_MASK_INFO;
  1035. }
  1036. kfree(ue->tlv_data);
  1037. ue->tlv_data = container;
  1038. ue->tlv_data_size = size;
  1039. mask |= SNDRV_CTL_EVENT_MASK_TLV;
  1040. for (i = 0; i < kctl->count; ++i) {
  1041. snd_ctl_build_ioff(&id, kctl, i);
  1042. snd_ctl_notify(ue->card, mask, &id);
  1043. }
  1044. return change;
  1045. }
  1046. static int read_user_tlv(struct snd_kcontrol *kctl, unsigned int __user *buf,
  1047. unsigned int size)
  1048. {
  1049. struct user_element *ue = kctl->private_data;
  1050. if (ue->tlv_data_size == 0 || ue->tlv_data == NULL)
  1051. return -ENXIO;
  1052. if (size < ue->tlv_data_size)
  1053. return -ENOSPC;
  1054. if (copy_to_user(buf, ue->tlv_data, ue->tlv_data_size))
  1055. return -EFAULT;
  1056. return 0;
  1057. }
  1058. static int snd_ctl_elem_user_tlv(struct snd_kcontrol *kctl, int op_flag,
  1059. unsigned int size, unsigned int __user *buf)
  1060. {
  1061. if (op_flag == SNDRV_CTL_TLV_OP_WRITE)
  1062. return replace_user_tlv(kctl, buf, size);
  1063. else
  1064. return read_user_tlv(kctl, buf, size);
  1065. }
  1066. static int snd_ctl_elem_init_enum_names(struct user_element *ue)
  1067. {
  1068. char *names, *p;
  1069. size_t buf_len, name_len;
  1070. unsigned int i;
  1071. const uintptr_t user_ptrval = ue->info.value.enumerated.names_ptr;
  1072. if (ue->info.value.enumerated.names_length > 64 * 1024)
  1073. return -EINVAL;
  1074. names = memdup_user((const void __user *)user_ptrval,
  1075. ue->info.value.enumerated.names_length);
  1076. if (IS_ERR(names))
  1077. return PTR_ERR(names);
  1078. /* check that there are enough valid names */
  1079. buf_len = ue->info.value.enumerated.names_length;
  1080. p = names;
  1081. for (i = 0; i < ue->info.value.enumerated.items; ++i) {
  1082. name_len = strnlen(p, buf_len);
  1083. if (name_len == 0 || name_len >= 64 || name_len == buf_len) {
  1084. kfree(names);
  1085. return -EINVAL;
  1086. }
  1087. p += name_len + 1;
  1088. buf_len -= name_len + 1;
  1089. }
  1090. ue->priv_data = names;
  1091. ue->info.value.enumerated.names_ptr = 0;
  1092. return 0;
  1093. }
  1094. static void snd_ctl_elem_user_free(struct snd_kcontrol *kcontrol)
  1095. {
  1096. struct user_element *ue = kcontrol->private_data;
  1097. kfree(ue->tlv_data);
  1098. kfree(ue->priv_data);
  1099. kfree(ue);
  1100. }
  1101. static int snd_ctl_elem_add(struct snd_ctl_file *file,
  1102. struct snd_ctl_elem_info *info, int replace)
  1103. {
  1104. /* The capacity of struct snd_ctl_elem_value.value.*/
  1105. static const unsigned int value_sizes[] = {
  1106. [SNDRV_CTL_ELEM_TYPE_BOOLEAN] = sizeof(long),
  1107. [SNDRV_CTL_ELEM_TYPE_INTEGER] = sizeof(long),
  1108. [SNDRV_CTL_ELEM_TYPE_ENUMERATED] = sizeof(unsigned int),
  1109. [SNDRV_CTL_ELEM_TYPE_BYTES] = sizeof(unsigned char),
  1110. [SNDRV_CTL_ELEM_TYPE_IEC958] = sizeof(struct snd_aes_iec958),
  1111. [SNDRV_CTL_ELEM_TYPE_INTEGER64] = sizeof(long long),
  1112. };
  1113. static const unsigned int max_value_counts[] = {
  1114. [SNDRV_CTL_ELEM_TYPE_BOOLEAN] = 128,
  1115. [SNDRV_CTL_ELEM_TYPE_INTEGER] = 128,
  1116. [SNDRV_CTL_ELEM_TYPE_ENUMERATED] = 128,
  1117. [SNDRV_CTL_ELEM_TYPE_BYTES] = 512,
  1118. [SNDRV_CTL_ELEM_TYPE_IEC958] = 1,
  1119. [SNDRV_CTL_ELEM_TYPE_INTEGER64] = 64,
  1120. };
  1121. struct snd_card *card = file->card;
  1122. struct snd_kcontrol *kctl;
  1123. unsigned int count;
  1124. unsigned int access;
  1125. long private_size;
  1126. struct user_element *ue;
  1127. unsigned int offset;
  1128. int err;
  1129. if (!*info->id.name)
  1130. return -EINVAL;
  1131. if (strnlen(info->id.name, sizeof(info->id.name)) >= sizeof(info->id.name))
  1132. return -EINVAL;
  1133. /* Delete a control to replace them if needed. */
  1134. if (replace) {
  1135. info->id.numid = 0;
  1136. err = snd_ctl_remove_user_ctl(file, &info->id);
  1137. if (err)
  1138. return err;
  1139. }
  1140. /*
  1141. * The number of userspace controls are counted control by control,
  1142. * not element by element.
  1143. */
  1144. if (card->user_ctl_count + 1 > MAX_USER_CONTROLS)
  1145. return -ENOMEM;
  1146. /* Check the number of elements for this userspace control. */
  1147. count = info->owner;
  1148. if (count == 0)
  1149. count = 1;
  1150. /* Arrange access permissions if needed. */
  1151. access = info->access;
  1152. if (access == 0)
  1153. access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
  1154. access &= (SNDRV_CTL_ELEM_ACCESS_READWRITE |
  1155. SNDRV_CTL_ELEM_ACCESS_INACTIVE |
  1156. SNDRV_CTL_ELEM_ACCESS_TLV_WRITE);
  1157. /* In initial state, nothing is available as TLV container. */
  1158. if (access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE)
  1159. access |= SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
  1160. access |= SNDRV_CTL_ELEM_ACCESS_USER;
  1161. /*
  1162. * Check information and calculate the size of data specific to
  1163. * this userspace control.
  1164. */
  1165. if (info->type < SNDRV_CTL_ELEM_TYPE_BOOLEAN ||
  1166. info->type > SNDRV_CTL_ELEM_TYPE_INTEGER64)
  1167. return -EINVAL;
  1168. if (info->type == SNDRV_CTL_ELEM_TYPE_ENUMERATED &&
  1169. info->value.enumerated.items == 0)
  1170. return -EINVAL;
  1171. if (info->count < 1 ||
  1172. info->count > max_value_counts[info->type])
  1173. return -EINVAL;
  1174. if (!validate_element_member_dimension(info))
  1175. return -EINVAL;
  1176. private_size = value_sizes[info->type] * info->count;
  1177. /*
  1178. * Keep memory object for this userspace control. After passing this
  1179. * code block, the instance should be freed by snd_ctl_free_one().
  1180. *
  1181. * Note that these elements in this control are locked.
  1182. */
  1183. err = snd_ctl_new(&kctl, count, access, file);
  1184. if (err < 0)
  1185. return err;
  1186. memcpy(&kctl->id, &info->id, sizeof(kctl->id));
  1187. kctl->private_data = kzalloc(sizeof(struct user_element) + private_size * count,
  1188. GFP_KERNEL);
  1189. if (kctl->private_data == NULL) {
  1190. kfree(kctl);
  1191. return -ENOMEM;
  1192. }
  1193. kctl->private_free = snd_ctl_elem_user_free;
  1194. /* Set private data for this userspace control. */
  1195. ue = (struct user_element *)kctl->private_data;
  1196. ue->card = card;
  1197. ue->info = *info;
  1198. ue->info.access = 0;
  1199. ue->elem_data = (char *)ue + sizeof(*ue);
  1200. ue->elem_data_size = private_size;
  1201. if (ue->info.type == SNDRV_CTL_ELEM_TYPE_ENUMERATED) {
  1202. err = snd_ctl_elem_init_enum_names(ue);
  1203. if (err < 0) {
  1204. snd_ctl_free_one(kctl);
  1205. return err;
  1206. }
  1207. }
  1208. /* Set callback functions. */
  1209. if (info->type == SNDRV_CTL_ELEM_TYPE_ENUMERATED)
  1210. kctl->info = snd_ctl_elem_user_enum_info;
  1211. else
  1212. kctl->info = snd_ctl_elem_user_info;
  1213. if (access & SNDRV_CTL_ELEM_ACCESS_READ)
  1214. kctl->get = snd_ctl_elem_user_get;
  1215. if (access & SNDRV_CTL_ELEM_ACCESS_WRITE)
  1216. kctl->put = snd_ctl_elem_user_put;
  1217. if (access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE)
  1218. kctl->tlv.c = snd_ctl_elem_user_tlv;
  1219. /* This function manage to free the instance on failure. */
  1220. down_write(&card->controls_rwsem);
  1221. err = __snd_ctl_add(card, kctl);
  1222. if (err < 0) {
  1223. snd_ctl_free_one(kctl);
  1224. goto unlock;
  1225. }
  1226. offset = snd_ctl_get_ioff(kctl, &info->id);
  1227. snd_ctl_build_ioff(&info->id, kctl, offset);
  1228. /*
  1229. * Here we cannot fill any field for the number of elements added by
  1230. * this operation because there're no specific fields. The usage of
  1231. * 'owner' field for this purpose may cause any bugs to userspace
  1232. * applications because the field originally means PID of a process
  1233. * which locks the element.
  1234. */
  1235. card->user_ctl_count++;
  1236. unlock:
  1237. up_write(&card->controls_rwsem);
  1238. return err;
  1239. }
  1240. static int snd_ctl_elem_add_user(struct snd_ctl_file *file,
  1241. struct snd_ctl_elem_info __user *_info, int replace)
  1242. {
  1243. struct snd_ctl_elem_info info;
  1244. int err;
  1245. if (copy_from_user(&info, _info, sizeof(info)))
  1246. return -EFAULT;
  1247. err = snd_ctl_elem_add(file, &info, replace);
  1248. if (err < 0)
  1249. return err;
  1250. if (copy_to_user(_info, &info, sizeof(info))) {
  1251. snd_ctl_remove_user_ctl(file, &info.id);
  1252. return -EFAULT;
  1253. }
  1254. return 0;
  1255. }
  1256. static int snd_ctl_elem_remove(struct snd_ctl_file *file,
  1257. struct snd_ctl_elem_id __user *_id)
  1258. {
  1259. struct snd_ctl_elem_id id;
  1260. if (copy_from_user(&id, _id, sizeof(id)))
  1261. return -EFAULT;
  1262. return snd_ctl_remove_user_ctl(file, &id);
  1263. }
  1264. static int snd_ctl_subscribe_events(struct snd_ctl_file *file, int __user *ptr)
  1265. {
  1266. int subscribe;
  1267. if (get_user(subscribe, ptr))
  1268. return -EFAULT;
  1269. if (subscribe < 0) {
  1270. subscribe = file->subscribed;
  1271. if (put_user(subscribe, ptr))
  1272. return -EFAULT;
  1273. return 0;
  1274. }
  1275. if (subscribe) {
  1276. file->subscribed = 1;
  1277. return 0;
  1278. } else if (file->subscribed) {
  1279. snd_ctl_empty_read_queue(file);
  1280. file->subscribed = 0;
  1281. }
  1282. return 0;
  1283. }
  1284. static int call_tlv_handler(struct snd_ctl_file *file, int op_flag,
  1285. struct snd_kcontrol *kctl,
  1286. struct snd_ctl_elem_id *id,
  1287. unsigned int __user *buf, unsigned int size)
  1288. {
  1289. static const struct {
  1290. int op;
  1291. int perm;
  1292. } pairs[] = {
  1293. {SNDRV_CTL_TLV_OP_READ, SNDRV_CTL_ELEM_ACCESS_TLV_READ},
  1294. {SNDRV_CTL_TLV_OP_WRITE, SNDRV_CTL_ELEM_ACCESS_TLV_WRITE},
  1295. {SNDRV_CTL_TLV_OP_CMD, SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND},
  1296. };
  1297. struct snd_kcontrol_volatile *vd = &kctl->vd[snd_ctl_get_ioff(kctl, id)];
  1298. int i;
  1299. /* Check support of the request for this element. */
  1300. for (i = 0; i < ARRAY_SIZE(pairs); ++i) {
  1301. if (op_flag == pairs[i].op && (vd->access & pairs[i].perm))
  1302. break;
  1303. }
  1304. if (i == ARRAY_SIZE(pairs))
  1305. return -ENXIO;
  1306. if (kctl->tlv.c == NULL)
  1307. return -ENXIO;
  1308. /* Write and command operations are not allowed for locked element. */
  1309. if (op_flag != SNDRV_CTL_TLV_OP_READ &&
  1310. vd->owner != NULL && vd->owner != file)
  1311. return -EPERM;
  1312. return kctl->tlv.c(kctl, op_flag, size, buf);
  1313. }
  1314. static int read_tlv_buf(struct snd_kcontrol *kctl, struct snd_ctl_elem_id *id,
  1315. unsigned int __user *buf, unsigned int size)
  1316. {
  1317. struct snd_kcontrol_volatile *vd = &kctl->vd[snd_ctl_get_ioff(kctl, id)];
  1318. unsigned int len;
  1319. if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_READ))
  1320. return -ENXIO;
  1321. if (kctl->tlv.p == NULL)
  1322. return -ENXIO;
  1323. len = sizeof(unsigned int) * 2 + kctl->tlv.p[1];
  1324. if (size < len)
  1325. return -ENOMEM;
  1326. if (copy_to_user(buf, kctl->tlv.p, len))
  1327. return -EFAULT;
  1328. return 0;
  1329. }
  1330. static int snd_ctl_tlv_ioctl(struct snd_ctl_file *file,
  1331. struct snd_ctl_tlv __user *buf,
  1332. int op_flag)
  1333. {
  1334. struct snd_ctl_tlv header;
  1335. unsigned int *container;
  1336. unsigned int container_size;
  1337. struct snd_kcontrol *kctl;
  1338. struct snd_ctl_elem_id id;
  1339. struct snd_kcontrol_volatile *vd;
  1340. if (copy_from_user(&header, buf, sizeof(header)))
  1341. return -EFAULT;
  1342. /* In design of control core, numerical ID starts at 1. */
  1343. if (header.numid == 0)
  1344. return -EINVAL;
  1345. /* At least, container should include type and length fields. */
  1346. if (header.length < sizeof(unsigned int) * 2)
  1347. return -EINVAL;
  1348. container_size = header.length;
  1349. container = buf->tlv;
  1350. kctl = snd_ctl_find_numid(file->card, header.numid);
  1351. if (kctl == NULL)
  1352. return -ENOENT;
  1353. /* Calculate index of the element in this set. */
  1354. id = kctl->id;
  1355. snd_ctl_build_ioff(&id, kctl, header.numid - id.numid);
  1356. vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
  1357. if (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
  1358. return call_tlv_handler(file, op_flag, kctl, &id, container,
  1359. container_size);
  1360. } else {
  1361. if (op_flag == SNDRV_CTL_TLV_OP_READ) {
  1362. return read_tlv_buf(kctl, &id, container,
  1363. container_size);
  1364. }
  1365. }
  1366. /* Not supported. */
  1367. return -ENXIO;
  1368. }
  1369. static long snd_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  1370. {
  1371. struct snd_ctl_file *ctl;
  1372. struct snd_card *card;
  1373. struct snd_kctl_ioctl *p;
  1374. void __user *argp = (void __user *)arg;
  1375. int __user *ip = argp;
  1376. int err;
  1377. ctl = file->private_data;
  1378. card = ctl->card;
  1379. if (snd_BUG_ON(!card))
  1380. return -ENXIO;
  1381. switch (cmd) {
  1382. case SNDRV_CTL_IOCTL_PVERSION:
  1383. return put_user(SNDRV_CTL_VERSION, ip) ? -EFAULT : 0;
  1384. case SNDRV_CTL_IOCTL_CARD_INFO:
  1385. return snd_ctl_card_info(card, ctl, cmd, argp);
  1386. case SNDRV_CTL_IOCTL_ELEM_LIST:
  1387. return snd_ctl_elem_list(card, argp);
  1388. case SNDRV_CTL_IOCTL_ELEM_INFO:
  1389. return snd_ctl_elem_info_user(ctl, argp);
  1390. case SNDRV_CTL_IOCTL_ELEM_READ:
  1391. return snd_ctl_elem_read_user(card, argp);
  1392. case SNDRV_CTL_IOCTL_ELEM_WRITE:
  1393. return snd_ctl_elem_write_user(ctl, argp);
  1394. case SNDRV_CTL_IOCTL_ELEM_LOCK:
  1395. return snd_ctl_elem_lock(ctl, argp);
  1396. case SNDRV_CTL_IOCTL_ELEM_UNLOCK:
  1397. return snd_ctl_elem_unlock(ctl, argp);
  1398. case SNDRV_CTL_IOCTL_ELEM_ADD:
  1399. return snd_ctl_elem_add_user(ctl, argp, 0);
  1400. case SNDRV_CTL_IOCTL_ELEM_REPLACE:
  1401. return snd_ctl_elem_add_user(ctl, argp, 1);
  1402. case SNDRV_CTL_IOCTL_ELEM_REMOVE:
  1403. return snd_ctl_elem_remove(ctl, argp);
  1404. case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS:
  1405. return snd_ctl_subscribe_events(ctl, ip);
  1406. case SNDRV_CTL_IOCTL_TLV_READ:
  1407. down_read(&ctl->card->controls_rwsem);
  1408. err = snd_ctl_tlv_ioctl(ctl, argp, SNDRV_CTL_TLV_OP_READ);
  1409. up_read(&ctl->card->controls_rwsem);
  1410. return err;
  1411. case SNDRV_CTL_IOCTL_TLV_WRITE:
  1412. down_write(&ctl->card->controls_rwsem);
  1413. err = snd_ctl_tlv_ioctl(ctl, argp, SNDRV_CTL_TLV_OP_WRITE);
  1414. up_write(&ctl->card->controls_rwsem);
  1415. return err;
  1416. case SNDRV_CTL_IOCTL_TLV_COMMAND:
  1417. down_write(&ctl->card->controls_rwsem);
  1418. err = snd_ctl_tlv_ioctl(ctl, argp, SNDRV_CTL_TLV_OP_CMD);
  1419. up_write(&ctl->card->controls_rwsem);
  1420. return err;
  1421. case SNDRV_CTL_IOCTL_POWER:
  1422. return -ENOPROTOOPT;
  1423. case SNDRV_CTL_IOCTL_POWER_STATE:
  1424. #ifdef CONFIG_PM
  1425. return put_user(card->power_state, ip) ? -EFAULT : 0;
  1426. #else
  1427. return put_user(SNDRV_CTL_POWER_D0, ip) ? -EFAULT : 0;
  1428. #endif
  1429. }
  1430. down_read(&snd_ioctl_rwsem);
  1431. list_for_each_entry(p, &snd_control_ioctls, list) {
  1432. err = p->fioctl(card, ctl, cmd, arg);
  1433. if (err != -ENOIOCTLCMD) {
  1434. up_read(&snd_ioctl_rwsem);
  1435. return err;
  1436. }
  1437. }
  1438. up_read(&snd_ioctl_rwsem);
  1439. dev_dbg(card->dev, "unknown ioctl = 0x%x\n", cmd);
  1440. return -ENOTTY;
  1441. }
  1442. static ssize_t snd_ctl_read(struct file *file, char __user *buffer,
  1443. size_t count, loff_t * offset)
  1444. {
  1445. struct snd_ctl_file *ctl;
  1446. int err = 0;
  1447. ssize_t result = 0;
  1448. ctl = file->private_data;
  1449. if (snd_BUG_ON(!ctl || !ctl->card))
  1450. return -ENXIO;
  1451. if (!ctl->subscribed)
  1452. return -EBADFD;
  1453. if (count < sizeof(struct snd_ctl_event))
  1454. return -EINVAL;
  1455. spin_lock_irq(&ctl->read_lock);
  1456. while (count >= sizeof(struct snd_ctl_event)) {
  1457. struct snd_ctl_event ev;
  1458. struct snd_kctl_event *kev;
  1459. while (list_empty(&ctl->events)) {
  1460. wait_queue_entry_t wait;
  1461. if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
  1462. err = -EAGAIN;
  1463. goto __end_lock;
  1464. }
  1465. init_waitqueue_entry(&wait, current);
  1466. add_wait_queue(&ctl->change_sleep, &wait);
  1467. set_current_state(TASK_INTERRUPTIBLE);
  1468. spin_unlock_irq(&ctl->read_lock);
  1469. schedule();
  1470. remove_wait_queue(&ctl->change_sleep, &wait);
  1471. if (ctl->card->shutdown)
  1472. return -ENODEV;
  1473. if (signal_pending(current))
  1474. return -ERESTARTSYS;
  1475. spin_lock_irq(&ctl->read_lock);
  1476. }
  1477. kev = snd_kctl_event(ctl->events.next);
  1478. ev.type = SNDRV_CTL_EVENT_ELEM;
  1479. ev.data.elem.mask = kev->mask;
  1480. ev.data.elem.id = kev->id;
  1481. list_del(&kev->list);
  1482. spin_unlock_irq(&ctl->read_lock);
  1483. kfree(kev);
  1484. if (copy_to_user(buffer, &ev, sizeof(struct snd_ctl_event))) {
  1485. err = -EFAULT;
  1486. goto __end;
  1487. }
  1488. spin_lock_irq(&ctl->read_lock);
  1489. buffer += sizeof(struct snd_ctl_event);
  1490. count -= sizeof(struct snd_ctl_event);
  1491. result += sizeof(struct snd_ctl_event);
  1492. }
  1493. __end_lock:
  1494. spin_unlock_irq(&ctl->read_lock);
  1495. __end:
  1496. return result > 0 ? result : err;
  1497. }
  1498. static unsigned int snd_ctl_poll(struct file *file, poll_table * wait)
  1499. {
  1500. unsigned int mask;
  1501. struct snd_ctl_file *ctl;
  1502. ctl = file->private_data;
  1503. if (!ctl->subscribed)
  1504. return 0;
  1505. poll_wait(file, &ctl->change_sleep, wait);
  1506. mask = 0;
  1507. if (!list_empty(&ctl->events))
  1508. mask |= POLLIN | POLLRDNORM;
  1509. return mask;
  1510. }
  1511. /*
  1512. * register the device-specific control-ioctls.
  1513. * called from each device manager like pcm.c, hwdep.c, etc.
  1514. */
  1515. static int _snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn, struct list_head *lists)
  1516. {
  1517. struct snd_kctl_ioctl *pn;
  1518. pn = kzalloc(sizeof(struct snd_kctl_ioctl), GFP_KERNEL);
  1519. if (pn == NULL)
  1520. return -ENOMEM;
  1521. pn->fioctl = fcn;
  1522. down_write(&snd_ioctl_rwsem);
  1523. list_add_tail(&pn->list, lists);
  1524. up_write(&snd_ioctl_rwsem);
  1525. return 0;
  1526. }
  1527. /**
  1528. * snd_ctl_register_ioctl - register the device-specific control-ioctls
  1529. * @fcn: ioctl callback function
  1530. *
  1531. * called from each device manager like pcm.c, hwdep.c, etc.
  1532. */
  1533. int snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn)
  1534. {
  1535. return _snd_ctl_register_ioctl(fcn, &snd_control_ioctls);
  1536. }
  1537. EXPORT_SYMBOL(snd_ctl_register_ioctl);
  1538. #ifdef CONFIG_COMPAT
  1539. /**
  1540. * snd_ctl_register_ioctl_compat - register the device-specific 32bit compat
  1541. * control-ioctls
  1542. * @fcn: ioctl callback function
  1543. */
  1544. int snd_ctl_register_ioctl_compat(snd_kctl_ioctl_func_t fcn)
  1545. {
  1546. return _snd_ctl_register_ioctl(fcn, &snd_control_compat_ioctls);
  1547. }
  1548. EXPORT_SYMBOL(snd_ctl_register_ioctl_compat);
  1549. #endif
  1550. /*
  1551. * de-register the device-specific control-ioctls.
  1552. */
  1553. static int _snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn,
  1554. struct list_head *lists)
  1555. {
  1556. struct snd_kctl_ioctl *p;
  1557. if (snd_BUG_ON(!fcn))
  1558. return -EINVAL;
  1559. down_write(&snd_ioctl_rwsem);
  1560. list_for_each_entry(p, lists, list) {
  1561. if (p->fioctl == fcn) {
  1562. list_del(&p->list);
  1563. up_write(&snd_ioctl_rwsem);
  1564. kfree(p);
  1565. return 0;
  1566. }
  1567. }
  1568. up_write(&snd_ioctl_rwsem);
  1569. snd_BUG();
  1570. return -EINVAL;
  1571. }
  1572. /**
  1573. * snd_ctl_unregister_ioctl - de-register the device-specific control-ioctls
  1574. * @fcn: ioctl callback function to unregister
  1575. */
  1576. int snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn)
  1577. {
  1578. return _snd_ctl_unregister_ioctl(fcn, &snd_control_ioctls);
  1579. }
  1580. EXPORT_SYMBOL(snd_ctl_unregister_ioctl);
  1581. #ifdef CONFIG_COMPAT
  1582. /**
  1583. * snd_ctl_unregister_ioctl - de-register the device-specific compat 32bit
  1584. * control-ioctls
  1585. * @fcn: ioctl callback function to unregister
  1586. */
  1587. int snd_ctl_unregister_ioctl_compat(snd_kctl_ioctl_func_t fcn)
  1588. {
  1589. return _snd_ctl_unregister_ioctl(fcn, &snd_control_compat_ioctls);
  1590. }
  1591. EXPORT_SYMBOL(snd_ctl_unregister_ioctl_compat);
  1592. #endif
  1593. static int snd_ctl_fasync(int fd, struct file * file, int on)
  1594. {
  1595. struct snd_ctl_file *ctl;
  1596. ctl = file->private_data;
  1597. return fasync_helper(fd, file, on, &ctl->fasync);
  1598. }
  1599. /* return the preferred subdevice number if already assigned;
  1600. * otherwise return -1
  1601. */
  1602. int snd_ctl_get_preferred_subdevice(struct snd_card *card, int type)
  1603. {
  1604. struct snd_ctl_file *kctl;
  1605. int subdevice = -1;
  1606. read_lock(&card->ctl_files_rwlock);
  1607. list_for_each_entry(kctl, &card->ctl_files, list) {
  1608. if (kctl->pid == task_pid(current)) {
  1609. subdevice = kctl->preferred_subdevice[type];
  1610. if (subdevice != -1)
  1611. break;
  1612. }
  1613. }
  1614. read_unlock(&card->ctl_files_rwlock);
  1615. return subdevice;
  1616. }
  1617. EXPORT_SYMBOL_GPL(snd_ctl_get_preferred_subdevice);
  1618. /*
  1619. * ioctl32 compat
  1620. */
  1621. #ifdef CONFIG_COMPAT
  1622. #include "control_compat.c"
  1623. #else
  1624. #define snd_ctl_ioctl_compat NULL
  1625. #endif
  1626. /*
  1627. * INIT PART
  1628. */
  1629. static const struct file_operations snd_ctl_f_ops =
  1630. {
  1631. .owner = THIS_MODULE,
  1632. .read = snd_ctl_read,
  1633. .open = snd_ctl_open,
  1634. .release = snd_ctl_release,
  1635. .llseek = no_llseek,
  1636. .poll = snd_ctl_poll,
  1637. .unlocked_ioctl = snd_ctl_ioctl,
  1638. .compat_ioctl = snd_ctl_ioctl_compat,
  1639. .fasync = snd_ctl_fasync,
  1640. };
  1641. /*
  1642. * registration of the control device
  1643. */
  1644. static int snd_ctl_dev_register(struct snd_device *device)
  1645. {
  1646. struct snd_card *card = device->device_data;
  1647. return snd_register_device(SNDRV_DEVICE_TYPE_CONTROL, card, -1,
  1648. &snd_ctl_f_ops, card, &card->ctl_dev);
  1649. }
  1650. /*
  1651. * disconnection of the control device
  1652. */
  1653. static int snd_ctl_dev_disconnect(struct snd_device *device)
  1654. {
  1655. struct snd_card *card = device->device_data;
  1656. struct snd_ctl_file *ctl;
  1657. read_lock(&card->ctl_files_rwlock);
  1658. list_for_each_entry(ctl, &card->ctl_files, list) {
  1659. wake_up(&ctl->change_sleep);
  1660. kill_fasync(&ctl->fasync, SIGIO, POLL_ERR);
  1661. }
  1662. read_unlock(&card->ctl_files_rwlock);
  1663. return snd_unregister_device(&card->ctl_dev);
  1664. }
  1665. /*
  1666. * free all controls
  1667. */
  1668. static int snd_ctl_dev_free(struct snd_device *device)
  1669. {
  1670. struct snd_card *card = device->device_data;
  1671. struct snd_kcontrol *control;
  1672. down_write(&card->controls_rwsem);
  1673. while (!list_empty(&card->controls)) {
  1674. control = snd_kcontrol(card->controls.next);
  1675. snd_ctl_remove(card, control);
  1676. }
  1677. up_write(&card->controls_rwsem);
  1678. put_device(&card->ctl_dev);
  1679. return 0;
  1680. }
  1681. /*
  1682. * create control core:
  1683. * called from init.c
  1684. */
  1685. int snd_ctl_create(struct snd_card *card)
  1686. {
  1687. static struct snd_device_ops ops = {
  1688. .dev_free = snd_ctl_dev_free,
  1689. .dev_register = snd_ctl_dev_register,
  1690. .dev_disconnect = snd_ctl_dev_disconnect,
  1691. };
  1692. int err;
  1693. if (snd_BUG_ON(!card))
  1694. return -ENXIO;
  1695. if (snd_BUG_ON(card->number < 0 || card->number >= SNDRV_CARDS))
  1696. return -ENXIO;
  1697. snd_device_initialize(&card->ctl_dev, card);
  1698. dev_set_name(&card->ctl_dev, "controlC%d", card->number);
  1699. err = snd_device_new(card, SNDRV_DEV_CONTROL, card, &ops);
  1700. if (err < 0)
  1701. put_device(&card->ctl_dev);
  1702. return err;
  1703. }
  1704. /*
  1705. * Frequently used control callbacks/helpers
  1706. */
  1707. /**
  1708. * snd_ctl_boolean_mono_info - Helper function for a standard boolean info
  1709. * callback with a mono channel
  1710. * @kcontrol: the kcontrol instance
  1711. * @uinfo: info to store
  1712. *
  1713. * This is a function that can be used as info callback for a standard
  1714. * boolean control with a single mono channel.
  1715. */
  1716. int snd_ctl_boolean_mono_info(struct snd_kcontrol *kcontrol,
  1717. struct snd_ctl_elem_info *uinfo)
  1718. {
  1719. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1720. uinfo->count = 1;
  1721. uinfo->value.integer.min = 0;
  1722. uinfo->value.integer.max = 1;
  1723. return 0;
  1724. }
  1725. EXPORT_SYMBOL(snd_ctl_boolean_mono_info);
  1726. /**
  1727. * snd_ctl_boolean_stereo_info - Helper function for a standard boolean info
  1728. * callback with stereo two channels
  1729. * @kcontrol: the kcontrol instance
  1730. * @uinfo: info to store
  1731. *
  1732. * This is a function that can be used as info callback for a standard
  1733. * boolean control with stereo two channels.
  1734. */
  1735. int snd_ctl_boolean_stereo_info(struct snd_kcontrol *kcontrol,
  1736. struct snd_ctl_elem_info *uinfo)
  1737. {
  1738. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1739. uinfo->count = 2;
  1740. uinfo->value.integer.min = 0;
  1741. uinfo->value.integer.max = 1;
  1742. return 0;
  1743. }
  1744. EXPORT_SYMBOL(snd_ctl_boolean_stereo_info);
  1745. /**
  1746. * snd_ctl_enum_info - fills the info structure for an enumerated control
  1747. * @info: the structure to be filled
  1748. * @channels: the number of the control's channels; often one
  1749. * @items: the number of control values; also the size of @names
  1750. * @names: an array containing the names of all control values
  1751. *
  1752. * Sets all required fields in @info to their appropriate values.
  1753. * If the control's accessibility is not the default (readable and writable),
  1754. * the caller has to fill @info->access.
  1755. *
  1756. * Return: Zero.
  1757. */
  1758. int snd_ctl_enum_info(struct snd_ctl_elem_info *info, unsigned int channels,
  1759. unsigned int items, const char *const names[])
  1760. {
  1761. info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  1762. info->count = channels;
  1763. info->value.enumerated.items = items;
  1764. if (!items)
  1765. return 0;
  1766. if (info->value.enumerated.item >= items)
  1767. info->value.enumerated.item = items - 1;
  1768. WARN(strlen(names[info->value.enumerated.item]) >= sizeof(info->value.enumerated.name),
  1769. "ALSA: too long item name '%s'\n",
  1770. names[info->value.enumerated.item]);
  1771. strlcpy(info->value.enumerated.name,
  1772. names[info->value.enumerated.item],
  1773. sizeof(info->value.enumerated.name));
  1774. return 0;
  1775. }
  1776. EXPORT_SYMBOL(snd_ctl_enum_info);