control.c 50 KB

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