control.c 44 KB

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