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