control.c 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Routines for driver control interface
  4. * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
  5. */
  6. #include <linux/threads.h>
  7. #include <linux/interrupt.h>
  8. #include <linux/module.h>
  9. #include <linux/moduleparam.h>
  10. #include <linux/slab.h>
  11. #include <linux/vmalloc.h>
  12. #include <linux/time.h>
  13. #include <linux/mm.h>
  14. #include <linux/math64.h>
  15. #include <linux/sched/signal.h>
  16. #include <sound/core.h>
  17. #include <sound/minors.h>
  18. #include <sound/info.h>
  19. #include <sound/control.h>
  20. // Max allocation size for user controls.
  21. static int max_user_ctl_alloc_size = 8 * 1024 * 1024;
  22. module_param_named(max_user_ctl_alloc_size, max_user_ctl_alloc_size, int, 0444);
  23. MODULE_PARM_DESC(max_user_ctl_alloc_size, "Max allocation size for user controls");
  24. #define MAX_CONTROL_COUNT 1028
  25. struct snd_kctl_ioctl {
  26. struct list_head list; /* list of all ioctls */
  27. snd_kctl_ioctl_func_t fioctl;
  28. };
  29. static DECLARE_RWSEM(snd_ioctl_rwsem);
  30. static DECLARE_RWSEM(snd_ctl_layer_rwsem);
  31. static LIST_HEAD(snd_control_ioctls);
  32. #ifdef CONFIG_COMPAT
  33. static LIST_HEAD(snd_control_compat_ioctls);
  34. #endif
  35. static struct snd_ctl_layer_ops *snd_ctl_layer;
  36. static int snd_ctl_remove_locked(struct snd_card *card,
  37. struct snd_kcontrol *kcontrol);
  38. static int snd_ctl_open(struct inode *inode, struct file *file)
  39. {
  40. struct snd_card *card;
  41. struct snd_ctl_file *ctl;
  42. int i, err;
  43. err = stream_open(inode, file);
  44. if (err < 0)
  45. return err;
  46. card = snd_lookup_minor_data(iminor(inode), SNDRV_DEVICE_TYPE_CONTROL);
  47. if (!card) {
  48. err = -ENODEV;
  49. goto __error1;
  50. }
  51. err = snd_card_file_add(card, file);
  52. if (err < 0) {
  53. err = -ENODEV;
  54. goto __error1;
  55. }
  56. if (!try_module_get(card->module)) {
  57. err = -EFAULT;
  58. goto __error2;
  59. }
  60. ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
  61. if (ctl == NULL) {
  62. err = -ENOMEM;
  63. goto __error;
  64. }
  65. INIT_LIST_HEAD(&ctl->events);
  66. init_waitqueue_head(&ctl->change_sleep);
  67. spin_lock_init(&ctl->read_lock);
  68. ctl->card = card;
  69. for (i = 0; i < SND_CTL_SUBDEV_ITEMS; i++)
  70. ctl->preferred_subdevice[i] = -1;
  71. ctl->pid = get_pid(task_pid(current));
  72. file->private_data = ctl;
  73. scoped_guard(write_lock_irqsave, &card->controls_rwlock)
  74. list_add_tail(&ctl->list, &card->ctl_files);
  75. snd_card_unref(card);
  76. return 0;
  77. __error:
  78. module_put(card->module);
  79. __error2:
  80. snd_card_file_remove(card, file);
  81. __error1:
  82. if (card)
  83. snd_card_unref(card);
  84. return err;
  85. }
  86. static void snd_ctl_empty_read_queue(struct snd_ctl_file * ctl)
  87. {
  88. struct snd_kctl_event *cread;
  89. guard(spinlock_irqsave)(&ctl->read_lock);
  90. while (!list_empty(&ctl->events)) {
  91. cread = snd_kctl_event(ctl->events.next);
  92. list_del(&cread->list);
  93. kfree(cread);
  94. }
  95. }
  96. static int snd_ctl_release(struct inode *inode, struct file *file)
  97. {
  98. struct snd_card *card;
  99. struct snd_ctl_file *ctl;
  100. struct snd_kcontrol *control;
  101. unsigned int idx;
  102. ctl = file->private_data;
  103. file->private_data = NULL;
  104. card = ctl->card;
  105. scoped_guard(write_lock_irqsave, &card->controls_rwlock)
  106. list_del(&ctl->list);
  107. scoped_guard(rwsem_write, &card->controls_rwsem) {
  108. list_for_each_entry(control, &card->controls, list)
  109. for (idx = 0; idx < control->count; idx++)
  110. if (control->vd[idx].owner == ctl)
  111. control->vd[idx].owner = NULL;
  112. }
  113. snd_fasync_free(ctl->fasync);
  114. snd_ctl_empty_read_queue(ctl);
  115. put_pid(ctl->pid);
  116. kfree(ctl);
  117. module_put(card->module);
  118. snd_card_file_remove(card, file);
  119. return 0;
  120. }
  121. /**
  122. * snd_ctl_notify - Send notification to user-space for a control change
  123. * @card: the card to send notification
  124. * @mask: the event mask, SNDRV_CTL_EVENT_*
  125. * @id: the ctl element id to send notification
  126. *
  127. * This function adds an event record with the given id and mask, appends
  128. * to the list and wakes up the user-space for notification. This can be
  129. * called in the atomic context.
  130. */
  131. void snd_ctl_notify(struct snd_card *card, unsigned int mask,
  132. struct snd_ctl_elem_id *id)
  133. {
  134. struct snd_ctl_file *ctl;
  135. struct snd_kctl_event *ev;
  136. if (snd_BUG_ON(!card || !id))
  137. return;
  138. if (card->shutdown)
  139. return;
  140. guard(read_lock_irqsave)(&card->controls_rwlock);
  141. #if IS_ENABLED(CONFIG_SND_MIXER_OSS)
  142. card->mixer_oss_change_count++;
  143. #endif
  144. list_for_each_entry(ctl, &card->ctl_files, list) {
  145. if (!ctl->subscribed)
  146. continue;
  147. scoped_guard(spinlock, &ctl->read_lock) {
  148. list_for_each_entry(ev, &ctl->events, list) {
  149. if (ev->id.numid == id->numid) {
  150. ev->mask |= mask;
  151. goto _found;
  152. }
  153. }
  154. ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
  155. if (ev) {
  156. ev->id = *id;
  157. ev->mask = mask;
  158. list_add_tail(&ev->list, &ctl->events);
  159. } else {
  160. dev_err(card->dev, "No memory available to allocate event\n");
  161. }
  162. _found:
  163. wake_up(&ctl->change_sleep);
  164. }
  165. snd_kill_fasync(ctl->fasync, SIGIO, POLL_IN);
  166. }
  167. }
  168. EXPORT_SYMBOL(snd_ctl_notify);
  169. /**
  170. * snd_ctl_notify_one - Send notification to user-space for a control change
  171. * @card: the card to send notification
  172. * @mask: the event mask, SNDRV_CTL_EVENT_*
  173. * @kctl: the pointer with the control instance
  174. * @ioff: the additional offset to the control index
  175. *
  176. * This function calls snd_ctl_notify() and does additional jobs
  177. * like LED state changes.
  178. */
  179. void snd_ctl_notify_one(struct snd_card *card, unsigned int mask,
  180. struct snd_kcontrol *kctl, unsigned int ioff)
  181. {
  182. struct snd_ctl_elem_id id = kctl->id;
  183. struct snd_ctl_layer_ops *lops;
  184. id.index += ioff;
  185. id.numid += ioff;
  186. snd_ctl_notify(card, mask, &id);
  187. guard(rwsem_read)(&snd_ctl_layer_rwsem);
  188. for (lops = snd_ctl_layer; lops; lops = lops->next)
  189. lops->lnotify(card, mask, kctl, ioff);
  190. }
  191. EXPORT_SYMBOL(snd_ctl_notify_one);
  192. /**
  193. * snd_ctl_new - create a new control instance with some elements
  194. * @kctl: the pointer to store new control instance
  195. * @count: the number of elements in this control
  196. * @access: the default access flags for elements in this control
  197. * @file: given when locking these elements
  198. *
  199. * Allocates a memory object for a new control instance. The instance has
  200. * elements as many as the given number (@count). Each element has given
  201. * access permissions (@access). Each element is locked when @file is given.
  202. *
  203. * Return: 0 on success, error code on failure
  204. */
  205. static int snd_ctl_new(struct snd_kcontrol **kctl, unsigned int count,
  206. unsigned int access, struct snd_ctl_file *file)
  207. {
  208. unsigned int idx;
  209. if (count == 0 || count > MAX_CONTROL_COUNT)
  210. return -EINVAL;
  211. *kctl = kzalloc(struct_size(*kctl, vd, count), GFP_KERNEL);
  212. if (!*kctl)
  213. return -ENOMEM;
  214. (*kctl)->count = count;
  215. for (idx = 0; idx < count; idx++) {
  216. (*kctl)->vd[idx].access = access;
  217. (*kctl)->vd[idx].owner = file;
  218. }
  219. return 0;
  220. }
  221. /**
  222. * snd_ctl_new1 - create a control instance from the template
  223. * @ncontrol: the initialization record
  224. * @private_data: the private data to set
  225. *
  226. * Allocates a new struct snd_kcontrol instance and initialize from the given
  227. * template. When the access field of ncontrol is 0, it's assumed as
  228. * READWRITE access. When the count field is 0, it's assumes as one.
  229. *
  230. * Return: The pointer of the newly generated instance, or %NULL on failure.
  231. */
  232. struct snd_kcontrol *snd_ctl_new1(const struct snd_kcontrol_new *ncontrol,
  233. void *private_data)
  234. {
  235. struct snd_kcontrol *kctl;
  236. unsigned int count;
  237. unsigned int access;
  238. int err;
  239. if (snd_BUG_ON(!ncontrol || !ncontrol->info))
  240. return NULL;
  241. count = ncontrol->count;
  242. if (count == 0)
  243. count = 1;
  244. access = ncontrol->access;
  245. if (access == 0)
  246. access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
  247. access &= (SNDRV_CTL_ELEM_ACCESS_READWRITE |
  248. SNDRV_CTL_ELEM_ACCESS_VOLATILE |
  249. SNDRV_CTL_ELEM_ACCESS_INACTIVE |
  250. SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE |
  251. SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND |
  252. SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK |
  253. SNDRV_CTL_ELEM_ACCESS_LED_MASK |
  254. SNDRV_CTL_ELEM_ACCESS_SKIP_CHECK);
  255. err = snd_ctl_new(&kctl, count, access, NULL);
  256. if (err < 0)
  257. return NULL;
  258. /* The 'numid' member is decided when calling snd_ctl_add(). */
  259. kctl->id.iface = ncontrol->iface;
  260. kctl->id.device = ncontrol->device;
  261. kctl->id.subdevice = ncontrol->subdevice;
  262. if (ncontrol->name) {
  263. strscpy(kctl->id.name, ncontrol->name, sizeof(kctl->id.name));
  264. if (strcmp(ncontrol->name, kctl->id.name) != 0)
  265. pr_warn("ALSA: Control name '%s' truncated to '%s'\n",
  266. ncontrol->name, kctl->id.name);
  267. }
  268. kctl->id.index = ncontrol->index;
  269. kctl->info = ncontrol->info;
  270. kctl->get = ncontrol->get;
  271. kctl->put = ncontrol->put;
  272. kctl->tlv.p = ncontrol->tlv.p;
  273. kctl->private_value = ncontrol->private_value;
  274. kctl->private_data = private_data;
  275. return kctl;
  276. }
  277. EXPORT_SYMBOL(snd_ctl_new1);
  278. /**
  279. * snd_ctl_free_one - release the control instance
  280. * @kcontrol: the control instance
  281. *
  282. * Releases the control instance created via snd_ctl_new()
  283. * or snd_ctl_new1().
  284. * Don't call this after the control was added to the card.
  285. */
  286. void snd_ctl_free_one(struct snd_kcontrol *kcontrol)
  287. {
  288. if (kcontrol) {
  289. if (kcontrol->private_free)
  290. kcontrol->private_free(kcontrol);
  291. kfree(kcontrol);
  292. }
  293. }
  294. EXPORT_SYMBOL(snd_ctl_free_one);
  295. static bool snd_ctl_remove_numid_conflict(struct snd_card *card,
  296. unsigned int count)
  297. {
  298. struct snd_kcontrol *kctl;
  299. /* Make sure that the ids assigned to the control do not wrap around */
  300. if (card->last_numid >= UINT_MAX - count)
  301. card->last_numid = 0;
  302. list_for_each_entry(kctl, &card->controls, list) {
  303. if (kctl->id.numid < card->last_numid + 1 + count &&
  304. kctl->id.numid + kctl->count > card->last_numid + 1) {
  305. card->last_numid = kctl->id.numid + kctl->count - 1;
  306. return true;
  307. }
  308. }
  309. return false;
  310. }
  311. static int snd_ctl_find_hole(struct snd_card *card, unsigned int count)
  312. {
  313. unsigned int iter = 100000;
  314. while (snd_ctl_remove_numid_conflict(card, count)) {
  315. if (--iter == 0) {
  316. /* this situation is very unlikely */
  317. dev_err(card->dev, "unable to allocate new control numid\n");
  318. return -ENOMEM;
  319. }
  320. }
  321. return 0;
  322. }
  323. /* check whether the given id is contained in the given kctl */
  324. static bool elem_id_matches(const struct snd_kcontrol *kctl,
  325. const struct snd_ctl_elem_id *id)
  326. {
  327. return kctl->id.iface == id->iface &&
  328. kctl->id.device == id->device &&
  329. kctl->id.subdevice == id->subdevice &&
  330. !strncmp(kctl->id.name, id->name, sizeof(kctl->id.name)) &&
  331. kctl->id.index <= id->index &&
  332. kctl->id.index + kctl->count > id->index;
  333. }
  334. #ifdef CONFIG_SND_CTL_FAST_LOOKUP
  335. /* Compute a hash key for the corresponding ctl id
  336. * It's for the name lookup, hence the numid is excluded.
  337. * The hash key is bound in LONG_MAX to be used for Xarray key.
  338. */
  339. #define MULTIPLIER 37
  340. static unsigned long get_ctl_id_hash(const struct snd_ctl_elem_id *id)
  341. {
  342. int i;
  343. unsigned long h;
  344. h = id->iface;
  345. h = MULTIPLIER * h + id->device;
  346. h = MULTIPLIER * h + id->subdevice;
  347. for (i = 0; i < SNDRV_CTL_ELEM_ID_NAME_MAXLEN && id->name[i]; i++)
  348. h = MULTIPLIER * h + id->name[i];
  349. h = MULTIPLIER * h + id->index;
  350. h &= LONG_MAX;
  351. return h;
  352. }
  353. /* add hash entries to numid and ctl xarray tables */
  354. static void add_hash_entries(struct snd_card *card,
  355. struct snd_kcontrol *kcontrol)
  356. {
  357. struct snd_ctl_elem_id id = kcontrol->id;
  358. int i;
  359. xa_store_range(&card->ctl_numids, kcontrol->id.numid,
  360. kcontrol->id.numid + kcontrol->count - 1,
  361. kcontrol, GFP_KERNEL);
  362. for (i = 0; i < kcontrol->count; i++) {
  363. id.index = kcontrol->id.index + i;
  364. if (xa_insert(&card->ctl_hash, get_ctl_id_hash(&id),
  365. kcontrol, GFP_KERNEL)) {
  366. /* skip hash for this entry, noting we had collision */
  367. card->ctl_hash_collision = true;
  368. dev_dbg(card->dev, "ctl_hash collision %d:%s:%d\n",
  369. id.iface, id.name, id.index);
  370. }
  371. }
  372. }
  373. /* remove hash entries that have been added */
  374. static void remove_hash_entries(struct snd_card *card,
  375. struct snd_kcontrol *kcontrol)
  376. {
  377. struct snd_ctl_elem_id id = kcontrol->id;
  378. struct snd_kcontrol *matched;
  379. unsigned long h;
  380. int i;
  381. for (i = 0; i < kcontrol->count; i++) {
  382. xa_erase(&card->ctl_numids, id.numid);
  383. h = get_ctl_id_hash(&id);
  384. matched = xa_load(&card->ctl_hash, h);
  385. if (matched && (matched == kcontrol ||
  386. elem_id_matches(matched, &id)))
  387. xa_erase(&card->ctl_hash, h);
  388. id.index++;
  389. id.numid++;
  390. }
  391. }
  392. #else /* CONFIG_SND_CTL_FAST_LOOKUP */
  393. static inline void add_hash_entries(struct snd_card *card,
  394. struct snd_kcontrol *kcontrol)
  395. {
  396. }
  397. static inline void remove_hash_entries(struct snd_card *card,
  398. struct snd_kcontrol *kcontrol)
  399. {
  400. }
  401. #endif /* CONFIG_SND_CTL_FAST_LOOKUP */
  402. enum snd_ctl_add_mode {
  403. CTL_ADD_EXCLUSIVE, CTL_REPLACE, CTL_ADD_ON_REPLACE,
  404. };
  405. /* add/replace a new kcontrol object; call with card->controls_rwsem locked */
  406. static int __snd_ctl_add_replace(struct snd_card *card,
  407. struct snd_kcontrol *kcontrol,
  408. enum snd_ctl_add_mode mode)
  409. {
  410. struct snd_ctl_elem_id id;
  411. unsigned int idx;
  412. struct snd_kcontrol *old;
  413. int err;
  414. lockdep_assert_held_write(&card->controls_rwsem);
  415. id = kcontrol->id;
  416. if (id.index > UINT_MAX - kcontrol->count)
  417. return -EINVAL;
  418. old = snd_ctl_find_id(card, &id);
  419. if (!old) {
  420. if (mode == CTL_REPLACE)
  421. return -EINVAL;
  422. } else {
  423. if (mode == CTL_ADD_EXCLUSIVE) {
  424. dev_err(card->dev,
  425. "control %i:%i:%i:%s:%i is already present\n",
  426. id.iface, id.device, id.subdevice, id.name,
  427. id.index);
  428. return -EBUSY;
  429. }
  430. err = snd_ctl_remove_locked(card, old);
  431. if (err < 0)
  432. return err;
  433. }
  434. if (snd_ctl_find_hole(card, kcontrol->count) < 0)
  435. return -ENOMEM;
  436. scoped_guard(write_lock_irq, &card->controls_rwlock) {
  437. list_add_tail(&kcontrol->list, &card->controls);
  438. card->controls_count += kcontrol->count;
  439. kcontrol->id.numid = card->last_numid + 1;
  440. card->last_numid += kcontrol->count;
  441. }
  442. add_hash_entries(card, kcontrol);
  443. for (idx = 0; idx < kcontrol->count; idx++)
  444. snd_ctl_notify_one(card, SNDRV_CTL_EVENT_MASK_ADD, kcontrol, idx);
  445. return 0;
  446. }
  447. static int snd_ctl_add_replace(struct snd_card *card,
  448. struct snd_kcontrol *kcontrol,
  449. enum snd_ctl_add_mode mode)
  450. {
  451. int err = -EINVAL;
  452. if (! kcontrol)
  453. return err;
  454. if (snd_BUG_ON(!card || !kcontrol->info))
  455. goto error;
  456. scoped_guard(rwsem_write, &card->controls_rwsem)
  457. err = __snd_ctl_add_replace(card, kcontrol, mode);
  458. if (err < 0)
  459. goto error;
  460. return 0;
  461. error:
  462. snd_ctl_free_one(kcontrol);
  463. return err;
  464. }
  465. /**
  466. * snd_ctl_add - add the control instance to the card
  467. * @card: the card instance
  468. * @kcontrol: the control instance to add
  469. *
  470. * Adds the control instance created via snd_ctl_new() or
  471. * snd_ctl_new1() to the given card. Assigns also an unique
  472. * numid used for fast search.
  473. *
  474. * It frees automatically the control which cannot be added.
  475. *
  476. * Return: Zero if successful, or a negative error code on failure.
  477. *
  478. */
  479. int snd_ctl_add(struct snd_card *card, struct snd_kcontrol *kcontrol)
  480. {
  481. return snd_ctl_add_replace(card, kcontrol, CTL_ADD_EXCLUSIVE);
  482. }
  483. EXPORT_SYMBOL(snd_ctl_add);
  484. /**
  485. * snd_ctl_replace - replace the control instance of the card
  486. * @card: the card instance
  487. * @kcontrol: the control instance to replace
  488. * @add_on_replace: add the control if not already added
  489. *
  490. * Replaces the given control. If the given control does not exist
  491. * and the add_on_replace flag is set, the control is added. If the
  492. * control exists, it is destroyed first.
  493. *
  494. * It frees automatically the control which cannot be added or replaced.
  495. *
  496. * Return: Zero if successful, or a negative error code on failure.
  497. */
  498. int snd_ctl_replace(struct snd_card *card, struct snd_kcontrol *kcontrol,
  499. bool add_on_replace)
  500. {
  501. return snd_ctl_add_replace(card, kcontrol,
  502. add_on_replace ? CTL_ADD_ON_REPLACE : CTL_REPLACE);
  503. }
  504. EXPORT_SYMBOL(snd_ctl_replace);
  505. static int __snd_ctl_remove(struct snd_card *card,
  506. struct snd_kcontrol *kcontrol,
  507. bool remove_hash)
  508. {
  509. unsigned int idx;
  510. lockdep_assert_held_write(&card->controls_rwsem);
  511. if (snd_BUG_ON(!card || !kcontrol))
  512. return -EINVAL;
  513. if (remove_hash)
  514. remove_hash_entries(card, kcontrol);
  515. scoped_guard(write_lock_irq, &card->controls_rwlock) {
  516. list_del(&kcontrol->list);
  517. card->controls_count -= kcontrol->count;
  518. }
  519. for (idx = 0; idx < kcontrol->count; idx++)
  520. snd_ctl_notify_one(card, SNDRV_CTL_EVENT_MASK_REMOVE, kcontrol, idx);
  521. snd_ctl_free_one(kcontrol);
  522. return 0;
  523. }
  524. static inline int snd_ctl_remove_locked(struct snd_card *card,
  525. struct snd_kcontrol *kcontrol)
  526. {
  527. return __snd_ctl_remove(card, kcontrol, true);
  528. }
  529. /**
  530. * snd_ctl_remove - remove the control from the card and release it
  531. * @card: the card instance
  532. * @kcontrol: the control instance to remove
  533. *
  534. * Removes the control from the card and then releases the instance.
  535. * You don't need to call snd_ctl_free_one().
  536. * Passing NULL to @kcontrol argument is allowed as noop.
  537. *
  538. * Return: 0 if successful, or a negative error code on failure.
  539. *
  540. * Note that this function takes card->controls_rwsem lock internally.
  541. */
  542. int snd_ctl_remove(struct snd_card *card, struct snd_kcontrol *kcontrol)
  543. {
  544. if (!kcontrol)
  545. return 0;
  546. guard(rwsem_write)(&card->controls_rwsem);
  547. return snd_ctl_remove_locked(card, kcontrol);
  548. }
  549. EXPORT_SYMBOL(snd_ctl_remove);
  550. /**
  551. * snd_ctl_remove_id - remove the control of the given id and release it
  552. * @card: the card instance
  553. * @id: the control id to remove
  554. *
  555. * Finds the control instance with the given id, removes it from the
  556. * card list and releases it.
  557. *
  558. * Return: 0 if successful, or a negative error code on failure.
  559. */
  560. int snd_ctl_remove_id(struct snd_card *card, struct snd_ctl_elem_id *id)
  561. {
  562. struct snd_kcontrol *kctl;
  563. guard(rwsem_write)(&card->controls_rwsem);
  564. kctl = snd_ctl_find_id(card, id);
  565. if (kctl == NULL)
  566. return -ENOENT;
  567. return snd_ctl_remove_locked(card, kctl);
  568. }
  569. EXPORT_SYMBOL(snd_ctl_remove_id);
  570. /**
  571. * snd_ctl_remove_user_ctl - remove and release the unlocked user control
  572. * @file: active control handle
  573. * @id: the control id to remove
  574. *
  575. * Finds the control instance with the given id, removes it from the
  576. * card list and releases it.
  577. *
  578. * Return: 0 if successful, or a negative error code on failure.
  579. */
  580. static int snd_ctl_remove_user_ctl(struct snd_ctl_file * file,
  581. struct snd_ctl_elem_id *id)
  582. {
  583. struct snd_card *card = file->card;
  584. struct snd_kcontrol *kctl;
  585. int idx;
  586. guard(rwsem_write)(&card->controls_rwsem);
  587. kctl = snd_ctl_find_id(card, id);
  588. if (kctl == NULL)
  589. return -ENOENT;
  590. if (!(kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_USER))
  591. return -EINVAL;
  592. for (idx = 0; idx < kctl->count; idx++)
  593. if (kctl->vd[idx].owner != NULL && kctl->vd[idx].owner != file)
  594. return -EBUSY;
  595. return snd_ctl_remove_locked(card, kctl);
  596. }
  597. /**
  598. * snd_ctl_activate_id - activate/inactivate the control of the given id
  599. * @card: the card instance
  600. * @id: the control id to activate/inactivate
  601. * @active: non-zero to activate
  602. *
  603. * Finds the control instance with the given id, and activate or
  604. * inactivate the control together with notification, if changed.
  605. * The given ID data is filled with full information.
  606. *
  607. * Return: 0 if unchanged, 1 if changed, or a negative error code on failure.
  608. */
  609. int snd_ctl_activate_id(struct snd_card *card, struct snd_ctl_elem_id *id,
  610. int active)
  611. {
  612. struct snd_kcontrol *kctl;
  613. struct snd_kcontrol_volatile *vd;
  614. unsigned int index_offset;
  615. int ret;
  616. down_write(&card->controls_rwsem);
  617. kctl = snd_ctl_find_id(card, id);
  618. if (kctl == NULL) {
  619. ret = -ENOENT;
  620. goto unlock;
  621. }
  622. index_offset = snd_ctl_get_ioff(kctl, id);
  623. vd = &kctl->vd[index_offset];
  624. ret = 0;
  625. if (active) {
  626. if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_INACTIVE))
  627. goto unlock;
  628. vd->access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  629. } else {
  630. if (vd->access & SNDRV_CTL_ELEM_ACCESS_INACTIVE)
  631. goto unlock;
  632. vd->access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  633. }
  634. snd_ctl_build_ioff(id, kctl, index_offset);
  635. downgrade_write(&card->controls_rwsem);
  636. snd_ctl_notify_one(card, SNDRV_CTL_EVENT_MASK_INFO, kctl, index_offset);
  637. up_read(&card->controls_rwsem);
  638. return 1;
  639. unlock:
  640. up_write(&card->controls_rwsem);
  641. return ret;
  642. }
  643. EXPORT_SYMBOL_GPL(snd_ctl_activate_id);
  644. /**
  645. * snd_ctl_rename_id - replace the id of a control on the card
  646. * @card: the card instance
  647. * @src_id: the old id
  648. * @dst_id: the new id
  649. *
  650. * Finds the control with the old id from the card, and replaces the
  651. * id with the new one.
  652. *
  653. * The function tries to keep the already assigned numid while replacing
  654. * the rest.
  655. *
  656. * Note that this function should be used only in the card initialization
  657. * phase. Calling after the card instantiation may cause issues with
  658. * user-space expecting persistent numids.
  659. *
  660. * Return: Zero if successful, or a negative error code on failure.
  661. */
  662. int snd_ctl_rename_id(struct snd_card *card, struct snd_ctl_elem_id *src_id,
  663. struct snd_ctl_elem_id *dst_id)
  664. {
  665. struct snd_kcontrol *kctl;
  666. int saved_numid;
  667. guard(rwsem_write)(&card->controls_rwsem);
  668. kctl = snd_ctl_find_id(card, src_id);
  669. if (kctl == NULL)
  670. return -ENOENT;
  671. saved_numid = kctl->id.numid;
  672. remove_hash_entries(card, kctl);
  673. kctl->id = *dst_id;
  674. kctl->id.numid = saved_numid;
  675. add_hash_entries(card, kctl);
  676. return 0;
  677. }
  678. EXPORT_SYMBOL(snd_ctl_rename_id);
  679. /**
  680. * snd_ctl_rename - rename the control on the card
  681. * @card: the card instance
  682. * @kctl: the control to rename
  683. * @name: the new name
  684. *
  685. * Renames the specified control on the card to the new name.
  686. *
  687. * Note that this function takes card->controls_rwsem lock internally.
  688. */
  689. void snd_ctl_rename(struct snd_card *card, struct snd_kcontrol *kctl,
  690. const char *name)
  691. {
  692. guard(rwsem_write)(&card->controls_rwsem);
  693. remove_hash_entries(card, kctl);
  694. if (strscpy(kctl->id.name, name, sizeof(kctl->id.name)) < 0)
  695. pr_warn("ALSA: Renamed control new name '%s' truncated to '%s'\n",
  696. name, kctl->id.name);
  697. add_hash_entries(card, kctl);
  698. }
  699. EXPORT_SYMBOL(snd_ctl_rename);
  700. #ifndef CONFIG_SND_CTL_FAST_LOOKUP
  701. static struct snd_kcontrol *
  702. snd_ctl_find_numid_slow(struct snd_card *card, unsigned int numid)
  703. {
  704. struct snd_kcontrol *kctl;
  705. guard(read_lock_irqsave)(&card->controls_rwlock);
  706. list_for_each_entry(kctl, &card->controls, list) {
  707. if (kctl->id.numid <= numid && kctl->id.numid + kctl->count > numid)
  708. return kctl;
  709. }
  710. return NULL;
  711. }
  712. #endif /* !CONFIG_SND_CTL_FAST_LOOKUP */
  713. /**
  714. * snd_ctl_find_numid - find the control instance with the given number-id
  715. * @card: the card instance
  716. * @numid: the number-id to search
  717. *
  718. * Finds the control instance with the given number-id from the card.
  719. *
  720. * Return: The pointer of the instance if found, or %NULL if not.
  721. *
  722. * Note that this function takes card->controls_rwlock lock internally.
  723. */
  724. struct snd_kcontrol *snd_ctl_find_numid(struct snd_card *card,
  725. unsigned int numid)
  726. {
  727. if (snd_BUG_ON(!card || !numid))
  728. return NULL;
  729. #ifdef CONFIG_SND_CTL_FAST_LOOKUP
  730. return xa_load(&card->ctl_numids, numid);
  731. #else
  732. return snd_ctl_find_numid_slow(card, numid);
  733. #endif
  734. }
  735. EXPORT_SYMBOL(snd_ctl_find_numid);
  736. /**
  737. * snd_ctl_find_id - find the control instance with the given id
  738. * @card: the card instance
  739. * @id: the id to search
  740. *
  741. * Finds the control instance with the given id from the card.
  742. *
  743. * Return: The pointer of the instance if found, or %NULL if not.
  744. *
  745. * Note that this function takes card->controls_rwlock lock internally.
  746. */
  747. struct snd_kcontrol *snd_ctl_find_id(struct snd_card *card,
  748. const struct snd_ctl_elem_id *id)
  749. {
  750. struct snd_kcontrol *kctl;
  751. if (snd_BUG_ON(!card || !id))
  752. return NULL;
  753. if (id->numid != 0)
  754. return snd_ctl_find_numid(card, id->numid);
  755. #ifdef CONFIG_SND_CTL_FAST_LOOKUP
  756. kctl = xa_load(&card->ctl_hash, get_ctl_id_hash(id));
  757. if (kctl && elem_id_matches(kctl, id))
  758. return kctl;
  759. if (!card->ctl_hash_collision)
  760. return NULL; /* we can rely on only hash table */
  761. #endif
  762. /* no matching in hash table - try all as the last resort */
  763. guard(read_lock_irqsave)(&card->controls_rwlock);
  764. list_for_each_entry(kctl, &card->controls, list)
  765. if (elem_id_matches(kctl, id))
  766. return kctl;
  767. return NULL;
  768. }
  769. EXPORT_SYMBOL(snd_ctl_find_id);
  770. static int snd_ctl_card_info(struct snd_card *card, struct snd_ctl_file * ctl,
  771. unsigned int cmd, void __user *arg)
  772. {
  773. struct snd_ctl_card_info *info __free(kfree) = NULL;
  774. info = kzalloc(sizeof(*info), GFP_KERNEL);
  775. if (! info)
  776. return -ENOMEM;
  777. scoped_guard(rwsem_read, &snd_ioctl_rwsem) {
  778. info->card = card->number;
  779. strscpy(info->id, card->id, sizeof(info->id));
  780. strscpy(info->driver, card->driver, sizeof(info->driver));
  781. strscpy(info->name, card->shortname, sizeof(info->name));
  782. strscpy(info->longname, card->longname, sizeof(info->longname));
  783. strscpy(info->mixername, card->mixername, sizeof(info->mixername));
  784. strscpy(info->components, card->components, sizeof(info->components));
  785. }
  786. if (copy_to_user(arg, info, sizeof(struct snd_ctl_card_info)))
  787. return -EFAULT;
  788. return 0;
  789. }
  790. static int snd_ctl_elem_list(struct snd_card *card,
  791. struct snd_ctl_elem_list *list)
  792. {
  793. struct snd_kcontrol *kctl;
  794. struct snd_ctl_elem_id id;
  795. unsigned int offset, space, jidx;
  796. offset = list->offset;
  797. space = list->space;
  798. guard(rwsem_read)(&card->controls_rwsem);
  799. list->count = card->controls_count;
  800. list->used = 0;
  801. if (!space)
  802. return 0;
  803. list_for_each_entry(kctl, &card->controls, list) {
  804. if (offset >= kctl->count) {
  805. offset -= kctl->count;
  806. continue;
  807. }
  808. for (jidx = offset; jidx < kctl->count; jidx++) {
  809. snd_ctl_build_ioff(&id, kctl, jidx);
  810. if (copy_to_user(list->pids + list->used, &id, sizeof(id)))
  811. return -EFAULT;
  812. list->used++;
  813. if (!--space)
  814. return 0;
  815. }
  816. offset = 0;
  817. }
  818. return 0;
  819. }
  820. static int snd_ctl_elem_list_user(struct snd_card *card,
  821. struct snd_ctl_elem_list __user *_list)
  822. {
  823. struct snd_ctl_elem_list list;
  824. int err;
  825. if (copy_from_user(&list, _list, sizeof(list)))
  826. return -EFAULT;
  827. err = snd_ctl_elem_list(card, &list);
  828. if (err)
  829. return err;
  830. if (copy_to_user(_list, &list, sizeof(list)))
  831. return -EFAULT;
  832. return 0;
  833. }
  834. /* Check whether the given kctl info is valid */
  835. static int snd_ctl_check_elem_info(struct snd_card *card,
  836. const struct snd_ctl_elem_info *info)
  837. {
  838. static const unsigned int max_value_counts[] = {
  839. [SNDRV_CTL_ELEM_TYPE_BOOLEAN] = 128,
  840. [SNDRV_CTL_ELEM_TYPE_INTEGER] = 128,
  841. [SNDRV_CTL_ELEM_TYPE_ENUMERATED] = 128,
  842. [SNDRV_CTL_ELEM_TYPE_BYTES] = 512,
  843. [SNDRV_CTL_ELEM_TYPE_IEC958] = 1,
  844. [SNDRV_CTL_ELEM_TYPE_INTEGER64] = 64,
  845. };
  846. if (info->type < SNDRV_CTL_ELEM_TYPE_BOOLEAN ||
  847. info->type > SNDRV_CTL_ELEM_TYPE_INTEGER64) {
  848. if (card)
  849. dev_err(card->dev,
  850. "control %i:%i:%i:%s:%i: invalid type %d\n",
  851. info->id.iface, info->id.device,
  852. info->id.subdevice, info->id.name,
  853. info->id.index, info->type);
  854. return -EINVAL;
  855. }
  856. if (info->type == SNDRV_CTL_ELEM_TYPE_ENUMERATED &&
  857. info->value.enumerated.items == 0) {
  858. if (card)
  859. dev_err(card->dev,
  860. "control %i:%i:%i:%s:%i: zero enum items\n",
  861. info->id.iface, info->id.device,
  862. info->id.subdevice, info->id.name,
  863. info->id.index);
  864. return -EINVAL;
  865. }
  866. if (info->count > max_value_counts[info->type]) {
  867. if (card)
  868. dev_err(card->dev,
  869. "control %i:%i:%i:%s:%i: invalid count %d\n",
  870. info->id.iface, info->id.device,
  871. info->id.subdevice, info->id.name,
  872. info->id.index, info->count);
  873. return -EINVAL;
  874. }
  875. return 0;
  876. }
  877. /* The capacity of struct snd_ctl_elem_value.value.*/
  878. static const unsigned int value_sizes[] = {
  879. [SNDRV_CTL_ELEM_TYPE_BOOLEAN] = sizeof(long),
  880. [SNDRV_CTL_ELEM_TYPE_INTEGER] = sizeof(long),
  881. [SNDRV_CTL_ELEM_TYPE_ENUMERATED] = sizeof(unsigned int),
  882. [SNDRV_CTL_ELEM_TYPE_BYTES] = sizeof(unsigned char),
  883. [SNDRV_CTL_ELEM_TYPE_IEC958] = sizeof(struct snd_aes_iec958),
  884. [SNDRV_CTL_ELEM_TYPE_INTEGER64] = sizeof(long long),
  885. };
  886. /* fill the remaining snd_ctl_elem_value data with the given pattern */
  887. static void fill_remaining_elem_value(struct snd_ctl_elem_value *control,
  888. struct snd_ctl_elem_info *info,
  889. u32 pattern)
  890. {
  891. size_t offset = value_sizes[info->type] * info->count;
  892. offset = DIV_ROUND_UP(offset, sizeof(u32));
  893. memset32((u32 *)control->value.bytes.data + offset, pattern,
  894. sizeof(control->value) / sizeof(u32) - offset);
  895. }
  896. /* check whether the given integer ctl value is valid */
  897. static int sanity_check_int_value(struct snd_card *card,
  898. const struct snd_ctl_elem_value *control,
  899. const struct snd_ctl_elem_info *info,
  900. int i, bool print_error)
  901. {
  902. long long lval, lmin, lmax, lstep;
  903. u64 rem;
  904. switch (info->type) {
  905. default:
  906. case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
  907. lval = control->value.integer.value[i];
  908. lmin = 0;
  909. lmax = 1;
  910. lstep = 0;
  911. break;
  912. case SNDRV_CTL_ELEM_TYPE_INTEGER:
  913. lval = control->value.integer.value[i];
  914. lmin = info->value.integer.min;
  915. lmax = info->value.integer.max;
  916. lstep = info->value.integer.step;
  917. break;
  918. case SNDRV_CTL_ELEM_TYPE_INTEGER64:
  919. lval = control->value.integer64.value[i];
  920. lmin = info->value.integer64.min;
  921. lmax = info->value.integer64.max;
  922. lstep = info->value.integer64.step;
  923. break;
  924. case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
  925. lval = control->value.enumerated.item[i];
  926. lmin = 0;
  927. lmax = info->value.enumerated.items - 1;
  928. lstep = 0;
  929. break;
  930. }
  931. if (lval < lmin || lval > lmax) {
  932. if (print_error)
  933. dev_err(card->dev,
  934. "control %i:%i:%i:%s:%i: value out of range %lld (%lld/%lld) at count %i\n",
  935. control->id.iface, control->id.device,
  936. control->id.subdevice, control->id.name,
  937. control->id.index, lval, lmin, lmax, i);
  938. return -EINVAL;
  939. }
  940. if (lstep) {
  941. div64_u64_rem(lval, lstep, &rem);
  942. if (rem) {
  943. if (print_error)
  944. dev_err(card->dev,
  945. "control %i:%i:%i:%s:%i: unaligned value %lld (step %lld) at count %i\n",
  946. control->id.iface, control->id.device,
  947. control->id.subdevice, control->id.name,
  948. control->id.index, lval, lstep, i);
  949. return -EINVAL;
  950. }
  951. }
  952. return 0;
  953. }
  954. /* check whether the all input values are valid for the given elem value */
  955. static int sanity_check_input_values(struct snd_card *card,
  956. const struct snd_ctl_elem_value *control,
  957. const struct snd_ctl_elem_info *info,
  958. bool print_error)
  959. {
  960. int i, ret;
  961. switch (info->type) {
  962. case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
  963. case SNDRV_CTL_ELEM_TYPE_INTEGER:
  964. case SNDRV_CTL_ELEM_TYPE_INTEGER64:
  965. case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
  966. for (i = 0; i < info->count; i++) {
  967. ret = sanity_check_int_value(card, control, info, i,
  968. print_error);
  969. if (ret < 0)
  970. return ret;
  971. }
  972. break;
  973. default:
  974. break;
  975. }
  976. return 0;
  977. }
  978. /* perform sanity checks to the given snd_ctl_elem_value object */
  979. static int sanity_check_elem_value(struct snd_card *card,
  980. const struct snd_ctl_elem_value *control,
  981. const struct snd_ctl_elem_info *info,
  982. u32 pattern)
  983. {
  984. size_t offset;
  985. int ret;
  986. u32 *p;
  987. ret = sanity_check_input_values(card, control, info, true);
  988. if (ret < 0)
  989. return ret;
  990. /* check whether the remaining area kept untouched */
  991. offset = value_sizes[info->type] * info->count;
  992. offset = DIV_ROUND_UP(offset, sizeof(u32));
  993. p = (u32 *)control->value.bytes.data + offset;
  994. for (; offset < sizeof(control->value) / sizeof(u32); offset++, p++) {
  995. if (*p != pattern) {
  996. ret = -EINVAL;
  997. break;
  998. }
  999. *p = 0; /* clear the checked area */
  1000. }
  1001. return ret;
  1002. }
  1003. static int __snd_ctl_elem_info(struct snd_card *card,
  1004. struct snd_kcontrol *kctl,
  1005. struct snd_ctl_elem_info *info,
  1006. struct snd_ctl_file *ctl)
  1007. {
  1008. struct snd_kcontrol_volatile *vd;
  1009. unsigned int index_offset;
  1010. int result;
  1011. #ifdef CONFIG_SND_DEBUG
  1012. info->access = 0;
  1013. #endif
  1014. result = kctl->info(kctl, info);
  1015. if (result >= 0) {
  1016. snd_BUG_ON(info->access);
  1017. index_offset = snd_ctl_get_ioff(kctl, &info->id);
  1018. vd = &kctl->vd[index_offset];
  1019. snd_ctl_build_ioff(&info->id, kctl, index_offset);
  1020. info->access = vd->access;
  1021. if (vd->owner) {
  1022. info->access |= SNDRV_CTL_ELEM_ACCESS_LOCK;
  1023. if (vd->owner == ctl)
  1024. info->access |= SNDRV_CTL_ELEM_ACCESS_OWNER;
  1025. info->owner = pid_vnr(vd->owner->pid);
  1026. } else {
  1027. info->owner = -1;
  1028. }
  1029. if (!snd_ctl_skip_validation(info) &&
  1030. snd_ctl_check_elem_info(card, info) < 0)
  1031. result = -EINVAL;
  1032. }
  1033. return result;
  1034. }
  1035. static int snd_ctl_elem_info(struct snd_ctl_file *ctl,
  1036. struct snd_ctl_elem_info *info)
  1037. {
  1038. struct snd_card *card = ctl->card;
  1039. struct snd_kcontrol *kctl;
  1040. guard(rwsem_read)(&card->controls_rwsem);
  1041. kctl = snd_ctl_find_id(card, &info->id);
  1042. if (!kctl)
  1043. return -ENOENT;
  1044. return __snd_ctl_elem_info(card, kctl, info, ctl);
  1045. }
  1046. static int snd_ctl_elem_info_user(struct snd_ctl_file *ctl,
  1047. struct snd_ctl_elem_info __user *_info)
  1048. {
  1049. struct snd_card *card = ctl->card;
  1050. struct snd_ctl_elem_info info;
  1051. int result;
  1052. if (copy_from_user(&info, _info, sizeof(info)))
  1053. return -EFAULT;
  1054. result = snd_power_ref_and_wait(card);
  1055. if (result)
  1056. return result;
  1057. result = snd_ctl_elem_info(ctl, &info);
  1058. snd_power_unref(card);
  1059. if (result < 0)
  1060. return result;
  1061. /* drop internal access flags */
  1062. info.access &= ~(SNDRV_CTL_ELEM_ACCESS_SKIP_CHECK|
  1063. SNDRV_CTL_ELEM_ACCESS_LED_MASK);
  1064. if (copy_to_user(_info, &info, sizeof(info)))
  1065. return -EFAULT;
  1066. return result;
  1067. }
  1068. static int snd_ctl_elem_read(struct snd_card *card,
  1069. struct snd_ctl_elem_value *control)
  1070. {
  1071. struct snd_kcontrol *kctl;
  1072. struct snd_kcontrol_volatile *vd;
  1073. unsigned int index_offset;
  1074. struct snd_ctl_elem_info info;
  1075. const u32 pattern = 0xdeadbeef;
  1076. int ret;
  1077. guard(rwsem_read)(&card->controls_rwsem);
  1078. kctl = snd_ctl_find_id(card, &control->id);
  1079. if (!kctl)
  1080. return -ENOENT;
  1081. index_offset = snd_ctl_get_ioff(kctl, &control->id);
  1082. vd = &kctl->vd[index_offset];
  1083. if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_READ) || !kctl->get)
  1084. return -EPERM;
  1085. snd_ctl_build_ioff(&control->id, kctl, index_offset);
  1086. #ifdef CONFIG_SND_CTL_DEBUG
  1087. /* info is needed only for validation */
  1088. memset(&info, 0, sizeof(info));
  1089. info.id = control->id;
  1090. ret = __snd_ctl_elem_info(card, kctl, &info, NULL);
  1091. if (ret < 0)
  1092. return ret;
  1093. #endif
  1094. if (!snd_ctl_skip_validation(&info))
  1095. fill_remaining_elem_value(control, &info, pattern);
  1096. ret = kctl->get(kctl, control);
  1097. if (ret < 0)
  1098. return ret;
  1099. if (!snd_ctl_skip_validation(&info) &&
  1100. sanity_check_elem_value(card, control, &info, pattern) < 0) {
  1101. dev_err(card->dev,
  1102. "control %i:%i:%i:%s:%i: access overflow\n",
  1103. control->id.iface, control->id.device,
  1104. control->id.subdevice, control->id.name,
  1105. control->id.index);
  1106. return -EINVAL;
  1107. }
  1108. return 0;
  1109. }
  1110. static int snd_ctl_elem_read_user(struct snd_card *card,
  1111. struct snd_ctl_elem_value __user *_control)
  1112. {
  1113. struct snd_ctl_elem_value *control __free(kfree) = NULL;
  1114. int result;
  1115. control = memdup_user(_control, sizeof(*control));
  1116. if (IS_ERR(control))
  1117. return PTR_ERR(control);
  1118. result = snd_power_ref_and_wait(card);
  1119. if (result)
  1120. return result;
  1121. result = snd_ctl_elem_read(card, control);
  1122. snd_power_unref(card);
  1123. if (result < 0)
  1124. return result;
  1125. if (copy_to_user(_control, control, sizeof(*control)))
  1126. return -EFAULT;
  1127. return result;
  1128. }
  1129. static int snd_ctl_elem_write(struct snd_card *card, struct snd_ctl_file *file,
  1130. struct snd_ctl_elem_value *control)
  1131. {
  1132. struct snd_kcontrol *kctl;
  1133. struct snd_kcontrol_volatile *vd;
  1134. unsigned int index_offset;
  1135. int result = 0;
  1136. down_write(&card->controls_rwsem);
  1137. kctl = snd_ctl_find_id(card, &control->id);
  1138. if (kctl == NULL) {
  1139. up_write(&card->controls_rwsem);
  1140. return -ENOENT;
  1141. }
  1142. index_offset = snd_ctl_get_ioff(kctl, &control->id);
  1143. vd = &kctl->vd[index_offset];
  1144. if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_WRITE) || kctl->put == NULL ||
  1145. (file && vd->owner && vd->owner != file)) {
  1146. up_write(&card->controls_rwsem);
  1147. return -EPERM;
  1148. }
  1149. snd_ctl_build_ioff(&control->id, kctl, index_offset);
  1150. /* validate input values */
  1151. if (IS_ENABLED(CONFIG_SND_CTL_INPUT_VALIDATION)) {
  1152. struct snd_ctl_elem_info info;
  1153. memset(&info, 0, sizeof(info));
  1154. info.id = control->id;
  1155. result = __snd_ctl_elem_info(card, kctl, &info, NULL);
  1156. if (!result)
  1157. result = sanity_check_input_values(card, control, &info,
  1158. false);
  1159. }
  1160. if (!result)
  1161. result = kctl->put(kctl, control);
  1162. if (result < 0) {
  1163. up_write(&card->controls_rwsem);
  1164. return result;
  1165. }
  1166. if (result > 0) {
  1167. downgrade_write(&card->controls_rwsem);
  1168. snd_ctl_notify_one(card, SNDRV_CTL_EVENT_MASK_VALUE, kctl, index_offset);
  1169. up_read(&card->controls_rwsem);
  1170. } else {
  1171. up_write(&card->controls_rwsem);
  1172. }
  1173. return 0;
  1174. }
  1175. static int snd_ctl_elem_write_user(struct snd_ctl_file *file,
  1176. struct snd_ctl_elem_value __user *_control)
  1177. {
  1178. struct snd_ctl_elem_value *control __free(kfree) = NULL;
  1179. struct snd_card *card;
  1180. int result;
  1181. control = memdup_user(_control, sizeof(*control));
  1182. if (IS_ERR(control))
  1183. return PTR_ERR(control);
  1184. card = file->card;
  1185. result = snd_power_ref_and_wait(card);
  1186. if (result < 0)
  1187. return result;
  1188. result = snd_ctl_elem_write(card, file, control);
  1189. snd_power_unref(card);
  1190. if (result < 0)
  1191. return result;
  1192. if (copy_to_user(_control, control, sizeof(*control)))
  1193. return -EFAULT;
  1194. return result;
  1195. }
  1196. static int snd_ctl_elem_lock(struct snd_ctl_file *file,
  1197. struct snd_ctl_elem_id __user *_id)
  1198. {
  1199. struct snd_card *card = file->card;
  1200. struct snd_ctl_elem_id id;
  1201. struct snd_kcontrol *kctl;
  1202. struct snd_kcontrol_volatile *vd;
  1203. if (copy_from_user(&id, _id, sizeof(id)))
  1204. return -EFAULT;
  1205. guard(rwsem_write)(&card->controls_rwsem);
  1206. kctl = snd_ctl_find_id(card, &id);
  1207. if (!kctl)
  1208. return -ENOENT;
  1209. vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
  1210. if (vd->owner)
  1211. return -EBUSY;
  1212. vd->owner = file;
  1213. return 0;
  1214. }
  1215. static int snd_ctl_elem_unlock(struct snd_ctl_file *file,
  1216. struct snd_ctl_elem_id __user *_id)
  1217. {
  1218. struct snd_card *card = file->card;
  1219. struct snd_ctl_elem_id id;
  1220. struct snd_kcontrol *kctl;
  1221. struct snd_kcontrol_volatile *vd;
  1222. if (copy_from_user(&id, _id, sizeof(id)))
  1223. return -EFAULT;
  1224. guard(rwsem_write)(&card->controls_rwsem);
  1225. kctl = snd_ctl_find_id(card, &id);
  1226. if (!kctl)
  1227. return -ENOENT;
  1228. vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
  1229. if (!vd->owner)
  1230. return -EINVAL;
  1231. if (vd->owner != file)
  1232. return -EPERM;
  1233. vd->owner = NULL;
  1234. return 0;
  1235. }
  1236. struct user_element {
  1237. struct snd_ctl_elem_info info;
  1238. struct snd_card *card;
  1239. char *elem_data; /* element data */
  1240. unsigned long elem_data_size; /* size of element data in bytes */
  1241. void *tlv_data; /* TLV data */
  1242. unsigned long tlv_data_size; /* TLV data size */
  1243. void *priv_data; /* private data (like strings for enumerated type) */
  1244. };
  1245. // check whether the addition (in bytes) of user ctl element may overflow the limit.
  1246. static bool check_user_elem_overflow(struct snd_card *card, ssize_t add)
  1247. {
  1248. return (ssize_t)card->user_ctl_alloc_size + add > max_user_ctl_alloc_size;
  1249. }
  1250. static int snd_ctl_elem_user_info(struct snd_kcontrol *kcontrol,
  1251. struct snd_ctl_elem_info *uinfo)
  1252. {
  1253. struct user_element *ue = kcontrol->private_data;
  1254. unsigned int offset;
  1255. offset = snd_ctl_get_ioff(kcontrol, &uinfo->id);
  1256. *uinfo = ue->info;
  1257. snd_ctl_build_ioff(&uinfo->id, kcontrol, offset);
  1258. return 0;
  1259. }
  1260. static int snd_ctl_elem_user_enum_info(struct snd_kcontrol *kcontrol,
  1261. struct snd_ctl_elem_info *uinfo)
  1262. {
  1263. struct user_element *ue = kcontrol->private_data;
  1264. const char *names;
  1265. unsigned int item;
  1266. unsigned int offset;
  1267. item = uinfo->value.enumerated.item;
  1268. offset = snd_ctl_get_ioff(kcontrol, &uinfo->id);
  1269. *uinfo = ue->info;
  1270. snd_ctl_build_ioff(&uinfo->id, kcontrol, offset);
  1271. item = min(item, uinfo->value.enumerated.items - 1);
  1272. uinfo->value.enumerated.item = item;
  1273. names = ue->priv_data;
  1274. for (; item > 0; --item)
  1275. names += strlen(names) + 1;
  1276. strcpy(uinfo->value.enumerated.name, names);
  1277. return 0;
  1278. }
  1279. static int snd_ctl_elem_user_get(struct snd_kcontrol *kcontrol,
  1280. struct snd_ctl_elem_value *ucontrol)
  1281. {
  1282. struct user_element *ue = kcontrol->private_data;
  1283. unsigned int size = ue->elem_data_size;
  1284. char *src = ue->elem_data +
  1285. snd_ctl_get_ioff(kcontrol, &ucontrol->id) * size;
  1286. memcpy(&ucontrol->value, src, size);
  1287. return 0;
  1288. }
  1289. static int snd_ctl_elem_user_put(struct snd_kcontrol *kcontrol,
  1290. struct snd_ctl_elem_value *ucontrol)
  1291. {
  1292. int err, change;
  1293. struct user_element *ue = kcontrol->private_data;
  1294. unsigned int size = ue->elem_data_size;
  1295. char *dst = ue->elem_data +
  1296. snd_ctl_get_ioff(kcontrol, &ucontrol->id) * size;
  1297. err = sanity_check_input_values(ue->card, ucontrol, &ue->info, false);
  1298. if (err < 0)
  1299. return err;
  1300. change = memcmp(&ucontrol->value, dst, size) != 0;
  1301. if (change)
  1302. memcpy(dst, &ucontrol->value, size);
  1303. return change;
  1304. }
  1305. /* called in controls_rwsem write lock */
  1306. static int replace_user_tlv(struct snd_kcontrol *kctl, unsigned int __user *buf,
  1307. unsigned int size)
  1308. {
  1309. struct user_element *ue = kctl->private_data;
  1310. unsigned int *container;
  1311. unsigned int mask = 0;
  1312. int i;
  1313. int change;
  1314. lockdep_assert_held_write(&ue->card->controls_rwsem);
  1315. if (size > 1024 * 128) /* sane value */
  1316. return -EINVAL;
  1317. // does the TLV size change cause overflow?
  1318. if (check_user_elem_overflow(ue->card, (ssize_t)(size - ue->tlv_data_size)))
  1319. return -ENOMEM;
  1320. container = vmemdup_user(buf, size);
  1321. if (IS_ERR(container))
  1322. return PTR_ERR(container);
  1323. change = ue->tlv_data_size != size;
  1324. if (!change)
  1325. change = memcmp(ue->tlv_data, container, size) != 0;
  1326. if (!change) {
  1327. kvfree(container);
  1328. return 0;
  1329. }
  1330. if (ue->tlv_data == NULL) {
  1331. /* Now TLV data is available. */
  1332. for (i = 0; i < kctl->count; ++i)
  1333. kctl->vd[i].access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ;
  1334. mask = SNDRV_CTL_EVENT_MASK_INFO;
  1335. } else {
  1336. ue->card->user_ctl_alloc_size -= ue->tlv_data_size;
  1337. ue->tlv_data_size = 0;
  1338. kvfree(ue->tlv_data);
  1339. }
  1340. ue->tlv_data = container;
  1341. ue->tlv_data_size = size;
  1342. // decremented at private_free.
  1343. ue->card->user_ctl_alloc_size += size;
  1344. mask |= SNDRV_CTL_EVENT_MASK_TLV;
  1345. for (i = 0; i < kctl->count; ++i)
  1346. snd_ctl_notify_one(ue->card, mask, kctl, i);
  1347. return change;
  1348. }
  1349. static int read_user_tlv(struct snd_kcontrol *kctl, unsigned int __user *buf,
  1350. unsigned int size)
  1351. {
  1352. struct user_element *ue = kctl->private_data;
  1353. if (ue->tlv_data_size == 0 || ue->tlv_data == NULL)
  1354. return -ENXIO;
  1355. if (size < ue->tlv_data_size)
  1356. return -ENOSPC;
  1357. if (copy_to_user(buf, ue->tlv_data, ue->tlv_data_size))
  1358. return -EFAULT;
  1359. return 0;
  1360. }
  1361. static int snd_ctl_elem_user_tlv(struct snd_kcontrol *kctl, int op_flag,
  1362. unsigned int size, unsigned int __user *buf)
  1363. {
  1364. if (op_flag == SNDRV_CTL_TLV_OP_WRITE)
  1365. return replace_user_tlv(kctl, buf, size);
  1366. else
  1367. return read_user_tlv(kctl, buf, size);
  1368. }
  1369. /* called in controls_rwsem write lock */
  1370. static int snd_ctl_elem_init_enum_names(struct user_element *ue)
  1371. {
  1372. char *names, *p;
  1373. size_t buf_len, name_len;
  1374. unsigned int i;
  1375. const uintptr_t user_ptrval = ue->info.value.enumerated.names_ptr;
  1376. lockdep_assert_held_write(&ue->card->controls_rwsem);
  1377. buf_len = ue->info.value.enumerated.names_length;
  1378. if (buf_len > 64 * 1024)
  1379. return -EINVAL;
  1380. if (check_user_elem_overflow(ue->card, buf_len))
  1381. return -ENOMEM;
  1382. names = vmemdup_user((const void __user *)user_ptrval, buf_len);
  1383. if (IS_ERR(names))
  1384. return PTR_ERR(names);
  1385. /* check that there are enough valid names */
  1386. p = names;
  1387. for (i = 0; i < ue->info.value.enumerated.items; ++i) {
  1388. name_len = strnlen(p, buf_len);
  1389. if (name_len == 0 || name_len >= 64 || name_len == buf_len) {
  1390. kvfree(names);
  1391. return -EINVAL;
  1392. }
  1393. p += name_len + 1;
  1394. buf_len -= name_len + 1;
  1395. }
  1396. ue->priv_data = names;
  1397. ue->info.value.enumerated.names_ptr = 0;
  1398. // increment the allocation size; decremented again at private_free.
  1399. ue->card->user_ctl_alloc_size += ue->info.value.enumerated.names_length;
  1400. return 0;
  1401. }
  1402. static size_t compute_user_elem_size(size_t size, unsigned int count)
  1403. {
  1404. return sizeof(struct user_element) + size * count;
  1405. }
  1406. static void snd_ctl_elem_user_free(struct snd_kcontrol *kcontrol)
  1407. {
  1408. struct user_element *ue = kcontrol->private_data;
  1409. // decrement the allocation size.
  1410. ue->card->user_ctl_alloc_size -= compute_user_elem_size(ue->elem_data_size, kcontrol->count);
  1411. ue->card->user_ctl_alloc_size -= ue->tlv_data_size;
  1412. if (ue->priv_data)
  1413. ue->card->user_ctl_alloc_size -= ue->info.value.enumerated.names_length;
  1414. kvfree(ue->tlv_data);
  1415. kvfree(ue->priv_data);
  1416. kfree(ue);
  1417. }
  1418. static int snd_ctl_elem_add(struct snd_ctl_file *file,
  1419. struct snd_ctl_elem_info *info, int replace)
  1420. {
  1421. struct snd_card *card = file->card;
  1422. struct snd_kcontrol *kctl;
  1423. unsigned int count;
  1424. unsigned int access;
  1425. long private_size;
  1426. size_t alloc_size;
  1427. struct user_element *ue;
  1428. unsigned int offset;
  1429. int err;
  1430. if (!*info->id.name)
  1431. return -EINVAL;
  1432. if (strnlen(info->id.name, sizeof(info->id.name)) >= sizeof(info->id.name))
  1433. return -EINVAL;
  1434. /* Delete a control to replace them if needed. */
  1435. if (replace) {
  1436. info->id.numid = 0;
  1437. err = snd_ctl_remove_user_ctl(file, &info->id);
  1438. if (err)
  1439. return err;
  1440. }
  1441. /* Check the number of elements for this userspace control. */
  1442. count = info->owner;
  1443. if (count == 0)
  1444. count = 1;
  1445. if (count > MAX_CONTROL_COUNT)
  1446. return -EINVAL;
  1447. /* Arrange access permissions if needed. */
  1448. access = info->access;
  1449. if (access == 0)
  1450. access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
  1451. access &= (SNDRV_CTL_ELEM_ACCESS_READWRITE |
  1452. SNDRV_CTL_ELEM_ACCESS_INACTIVE |
  1453. SNDRV_CTL_ELEM_ACCESS_TLV_WRITE);
  1454. /* In initial state, nothing is available as TLV container. */
  1455. if (access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE)
  1456. access |= SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
  1457. access |= SNDRV_CTL_ELEM_ACCESS_USER;
  1458. /*
  1459. * Check information and calculate the size of data specific to
  1460. * this userspace control.
  1461. */
  1462. /* pass NULL to card for suppressing error messages */
  1463. err = snd_ctl_check_elem_info(NULL, info);
  1464. if (err < 0)
  1465. return err;
  1466. /* user-space control doesn't allow zero-size data */
  1467. if (info->count < 1)
  1468. return -EINVAL;
  1469. private_size = value_sizes[info->type] * info->count;
  1470. alloc_size = compute_user_elem_size(private_size, count);
  1471. guard(rwsem_write)(&card->controls_rwsem);
  1472. if (check_user_elem_overflow(card, alloc_size))
  1473. return -ENOMEM;
  1474. /*
  1475. * Keep memory object for this userspace control. After passing this
  1476. * code block, the instance should be freed by snd_ctl_free_one().
  1477. *
  1478. * Note that these elements in this control are locked.
  1479. */
  1480. err = snd_ctl_new(&kctl, count, access, file);
  1481. if (err < 0)
  1482. return err;
  1483. memcpy(&kctl->id, &info->id, sizeof(kctl->id));
  1484. ue = kzalloc(alloc_size, GFP_KERNEL);
  1485. if (!ue) {
  1486. kfree(kctl);
  1487. return -ENOMEM;
  1488. }
  1489. kctl->private_data = ue;
  1490. kctl->private_free = snd_ctl_elem_user_free;
  1491. // increment the allocated size; decremented again at private_free.
  1492. card->user_ctl_alloc_size += alloc_size;
  1493. /* Set private data for this userspace control. */
  1494. ue->card = card;
  1495. ue->info = *info;
  1496. ue->info.access = 0;
  1497. ue->elem_data = (char *)ue + sizeof(*ue);
  1498. ue->elem_data_size = private_size;
  1499. if (ue->info.type == SNDRV_CTL_ELEM_TYPE_ENUMERATED) {
  1500. err = snd_ctl_elem_init_enum_names(ue);
  1501. if (err < 0) {
  1502. snd_ctl_free_one(kctl);
  1503. return err;
  1504. }
  1505. }
  1506. /* Set callback functions. */
  1507. if (info->type == SNDRV_CTL_ELEM_TYPE_ENUMERATED)
  1508. kctl->info = snd_ctl_elem_user_enum_info;
  1509. else
  1510. kctl->info = snd_ctl_elem_user_info;
  1511. if (access & SNDRV_CTL_ELEM_ACCESS_READ)
  1512. kctl->get = snd_ctl_elem_user_get;
  1513. if (access & SNDRV_CTL_ELEM_ACCESS_WRITE)
  1514. kctl->put = snd_ctl_elem_user_put;
  1515. if (access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE)
  1516. kctl->tlv.c = snd_ctl_elem_user_tlv;
  1517. /* This function manage to free the instance on failure. */
  1518. err = __snd_ctl_add_replace(card, kctl, CTL_ADD_EXCLUSIVE);
  1519. if (err < 0) {
  1520. snd_ctl_free_one(kctl);
  1521. return err;
  1522. }
  1523. offset = snd_ctl_get_ioff(kctl, &info->id);
  1524. snd_ctl_build_ioff(&info->id, kctl, offset);
  1525. /*
  1526. * Here we cannot fill any field for the number of elements added by
  1527. * this operation because there're no specific fields. The usage of
  1528. * 'owner' field for this purpose may cause any bugs to userspace
  1529. * applications because the field originally means PID of a process
  1530. * which locks the element.
  1531. */
  1532. return 0;
  1533. }
  1534. static int snd_ctl_elem_add_user(struct snd_ctl_file *file,
  1535. struct snd_ctl_elem_info __user *_info, int replace)
  1536. {
  1537. struct snd_ctl_elem_info info;
  1538. int err;
  1539. if (copy_from_user(&info, _info, sizeof(info)))
  1540. return -EFAULT;
  1541. err = snd_ctl_elem_add(file, &info, replace);
  1542. if (err < 0)
  1543. return err;
  1544. if (copy_to_user(_info, &info, sizeof(info))) {
  1545. snd_ctl_remove_user_ctl(file, &info.id);
  1546. return -EFAULT;
  1547. }
  1548. return 0;
  1549. }
  1550. static int snd_ctl_elem_remove(struct snd_ctl_file *file,
  1551. struct snd_ctl_elem_id __user *_id)
  1552. {
  1553. struct snd_ctl_elem_id id;
  1554. if (copy_from_user(&id, _id, sizeof(id)))
  1555. return -EFAULT;
  1556. return snd_ctl_remove_user_ctl(file, &id);
  1557. }
  1558. static int snd_ctl_subscribe_events(struct snd_ctl_file *file, int __user *ptr)
  1559. {
  1560. int subscribe;
  1561. if (get_user(subscribe, ptr))
  1562. return -EFAULT;
  1563. if (subscribe < 0) {
  1564. subscribe = file->subscribed;
  1565. if (put_user(subscribe, ptr))
  1566. return -EFAULT;
  1567. return 0;
  1568. }
  1569. if (subscribe) {
  1570. file->subscribed = 1;
  1571. return 0;
  1572. } else if (file->subscribed) {
  1573. snd_ctl_empty_read_queue(file);
  1574. file->subscribed = 0;
  1575. }
  1576. return 0;
  1577. }
  1578. static int call_tlv_handler(struct snd_ctl_file *file, int op_flag,
  1579. struct snd_kcontrol *kctl,
  1580. struct snd_ctl_elem_id *id,
  1581. unsigned int __user *buf, unsigned int size)
  1582. {
  1583. static const struct {
  1584. int op;
  1585. int perm;
  1586. } pairs[] = {
  1587. {SNDRV_CTL_TLV_OP_READ, SNDRV_CTL_ELEM_ACCESS_TLV_READ},
  1588. {SNDRV_CTL_TLV_OP_WRITE, SNDRV_CTL_ELEM_ACCESS_TLV_WRITE},
  1589. {SNDRV_CTL_TLV_OP_CMD, SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND},
  1590. };
  1591. struct snd_kcontrol_volatile *vd = &kctl->vd[snd_ctl_get_ioff(kctl, id)];
  1592. int i;
  1593. /* Check support of the request for this element. */
  1594. for (i = 0; i < ARRAY_SIZE(pairs); ++i) {
  1595. if (op_flag == pairs[i].op && (vd->access & pairs[i].perm))
  1596. break;
  1597. }
  1598. if (i == ARRAY_SIZE(pairs))
  1599. return -ENXIO;
  1600. if (kctl->tlv.c == NULL)
  1601. return -ENXIO;
  1602. /* Write and command operations are not allowed for locked element. */
  1603. if (op_flag != SNDRV_CTL_TLV_OP_READ &&
  1604. vd->owner != NULL && vd->owner != file)
  1605. return -EPERM;
  1606. return kctl->tlv.c(kctl, op_flag, size, buf);
  1607. }
  1608. static int read_tlv_buf(struct snd_kcontrol *kctl, struct snd_ctl_elem_id *id,
  1609. unsigned int __user *buf, unsigned int size)
  1610. {
  1611. struct snd_kcontrol_volatile *vd = &kctl->vd[snd_ctl_get_ioff(kctl, id)];
  1612. unsigned int len;
  1613. if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_READ))
  1614. return -ENXIO;
  1615. if (kctl->tlv.p == NULL)
  1616. return -ENXIO;
  1617. len = sizeof(unsigned int) * 2 + kctl->tlv.p[1];
  1618. if (size < len)
  1619. return -ENOMEM;
  1620. if (copy_to_user(buf, kctl->tlv.p, len))
  1621. return -EFAULT;
  1622. return 0;
  1623. }
  1624. static int snd_ctl_tlv_ioctl(struct snd_ctl_file *file,
  1625. struct snd_ctl_tlv __user *buf,
  1626. int op_flag)
  1627. {
  1628. struct snd_ctl_tlv header;
  1629. unsigned int __user *container;
  1630. unsigned int container_size;
  1631. struct snd_kcontrol *kctl;
  1632. struct snd_ctl_elem_id id;
  1633. struct snd_kcontrol_volatile *vd;
  1634. lockdep_assert_held(&file->card->controls_rwsem);
  1635. if (copy_from_user(&header, buf, sizeof(header)))
  1636. return -EFAULT;
  1637. /* In design of control core, numerical ID starts at 1. */
  1638. if (header.numid == 0)
  1639. return -EINVAL;
  1640. /* At least, container should include type and length fields. */
  1641. if (header.length < sizeof(unsigned int) * 2)
  1642. return -EINVAL;
  1643. container_size = header.length;
  1644. container = buf->tlv;
  1645. kctl = snd_ctl_find_numid(file->card, header.numid);
  1646. if (kctl == NULL)
  1647. return -ENOENT;
  1648. /* Calculate index of the element in this set. */
  1649. id = kctl->id;
  1650. snd_ctl_build_ioff(&id, kctl, header.numid - id.numid);
  1651. vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
  1652. if (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
  1653. return call_tlv_handler(file, op_flag, kctl, &id, container,
  1654. container_size);
  1655. } else {
  1656. if (op_flag == SNDRV_CTL_TLV_OP_READ) {
  1657. return read_tlv_buf(kctl, &id, container,
  1658. container_size);
  1659. }
  1660. }
  1661. /* Not supported. */
  1662. return -ENXIO;
  1663. }
  1664. static long snd_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  1665. {
  1666. struct snd_ctl_file *ctl;
  1667. struct snd_card *card;
  1668. struct snd_kctl_ioctl *p;
  1669. void __user *argp = (void __user *)arg;
  1670. int __user *ip = argp;
  1671. int err;
  1672. ctl = file->private_data;
  1673. card = ctl->card;
  1674. if (snd_BUG_ON(!card))
  1675. return -ENXIO;
  1676. switch (cmd) {
  1677. case SNDRV_CTL_IOCTL_PVERSION:
  1678. return put_user(SNDRV_CTL_VERSION, ip) ? -EFAULT : 0;
  1679. case SNDRV_CTL_IOCTL_CARD_INFO:
  1680. return snd_ctl_card_info(card, ctl, cmd, argp);
  1681. case SNDRV_CTL_IOCTL_ELEM_LIST:
  1682. return snd_ctl_elem_list_user(card, argp);
  1683. case SNDRV_CTL_IOCTL_ELEM_INFO:
  1684. return snd_ctl_elem_info_user(ctl, argp);
  1685. case SNDRV_CTL_IOCTL_ELEM_READ:
  1686. return snd_ctl_elem_read_user(card, argp);
  1687. case SNDRV_CTL_IOCTL_ELEM_WRITE:
  1688. return snd_ctl_elem_write_user(ctl, argp);
  1689. case SNDRV_CTL_IOCTL_ELEM_LOCK:
  1690. return snd_ctl_elem_lock(ctl, argp);
  1691. case SNDRV_CTL_IOCTL_ELEM_UNLOCK:
  1692. return snd_ctl_elem_unlock(ctl, argp);
  1693. case SNDRV_CTL_IOCTL_ELEM_ADD:
  1694. return snd_ctl_elem_add_user(ctl, argp, 0);
  1695. case SNDRV_CTL_IOCTL_ELEM_REPLACE:
  1696. return snd_ctl_elem_add_user(ctl, argp, 1);
  1697. case SNDRV_CTL_IOCTL_ELEM_REMOVE:
  1698. return snd_ctl_elem_remove(ctl, argp);
  1699. case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS:
  1700. return snd_ctl_subscribe_events(ctl, ip);
  1701. case SNDRV_CTL_IOCTL_TLV_READ:
  1702. err = snd_power_ref_and_wait(card);
  1703. if (err < 0)
  1704. return err;
  1705. scoped_guard(rwsem_read, &card->controls_rwsem)
  1706. err = snd_ctl_tlv_ioctl(ctl, argp, SNDRV_CTL_TLV_OP_READ);
  1707. snd_power_unref(card);
  1708. return err;
  1709. case SNDRV_CTL_IOCTL_TLV_WRITE:
  1710. err = snd_power_ref_and_wait(card);
  1711. if (err < 0)
  1712. return err;
  1713. scoped_guard(rwsem_write, &card->controls_rwsem)
  1714. err = snd_ctl_tlv_ioctl(ctl, argp, SNDRV_CTL_TLV_OP_WRITE);
  1715. snd_power_unref(card);
  1716. return err;
  1717. case SNDRV_CTL_IOCTL_TLV_COMMAND:
  1718. err = snd_power_ref_and_wait(card);
  1719. if (err < 0)
  1720. return err;
  1721. scoped_guard(rwsem_write, &card->controls_rwsem)
  1722. err = snd_ctl_tlv_ioctl(ctl, argp, SNDRV_CTL_TLV_OP_CMD);
  1723. snd_power_unref(card);
  1724. return err;
  1725. case SNDRV_CTL_IOCTL_POWER:
  1726. return -ENOPROTOOPT;
  1727. case SNDRV_CTL_IOCTL_POWER_STATE:
  1728. return put_user(SNDRV_CTL_POWER_D0, ip) ? -EFAULT : 0;
  1729. }
  1730. guard(rwsem_read)(&snd_ioctl_rwsem);
  1731. list_for_each_entry(p, &snd_control_ioctls, list) {
  1732. err = p->fioctl(card, ctl, cmd, arg);
  1733. if (err != -ENOIOCTLCMD)
  1734. return err;
  1735. }
  1736. dev_dbg(card->dev, "unknown ioctl = 0x%x\n", cmd);
  1737. return -ENOTTY;
  1738. }
  1739. static ssize_t snd_ctl_read(struct file *file, char __user *buffer,
  1740. size_t count, loff_t * offset)
  1741. {
  1742. struct snd_ctl_file *ctl;
  1743. int err = 0;
  1744. ssize_t result = 0;
  1745. ctl = file->private_data;
  1746. if (snd_BUG_ON(!ctl || !ctl->card))
  1747. return -ENXIO;
  1748. if (!ctl->subscribed)
  1749. return -EBADFD;
  1750. if (count < sizeof(struct snd_ctl_event))
  1751. return -EINVAL;
  1752. spin_lock_irq(&ctl->read_lock);
  1753. while (count >= sizeof(struct snd_ctl_event)) {
  1754. struct snd_ctl_event ev;
  1755. struct snd_kctl_event *kev;
  1756. while (list_empty(&ctl->events)) {
  1757. wait_queue_entry_t wait;
  1758. if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
  1759. err = -EAGAIN;
  1760. goto __end_lock;
  1761. }
  1762. init_waitqueue_entry(&wait, current);
  1763. add_wait_queue(&ctl->change_sleep, &wait);
  1764. set_current_state(TASK_INTERRUPTIBLE);
  1765. spin_unlock_irq(&ctl->read_lock);
  1766. schedule();
  1767. remove_wait_queue(&ctl->change_sleep, &wait);
  1768. if (ctl->card->shutdown)
  1769. return -ENODEV;
  1770. if (signal_pending(current))
  1771. return -ERESTARTSYS;
  1772. spin_lock_irq(&ctl->read_lock);
  1773. }
  1774. kev = snd_kctl_event(ctl->events.next);
  1775. ev.type = SNDRV_CTL_EVENT_ELEM;
  1776. ev.data.elem.mask = kev->mask;
  1777. ev.data.elem.id = kev->id;
  1778. list_del(&kev->list);
  1779. spin_unlock_irq(&ctl->read_lock);
  1780. kfree(kev);
  1781. if (copy_to_user(buffer, &ev, sizeof(struct snd_ctl_event))) {
  1782. err = -EFAULT;
  1783. goto __end;
  1784. }
  1785. spin_lock_irq(&ctl->read_lock);
  1786. buffer += sizeof(struct snd_ctl_event);
  1787. count -= sizeof(struct snd_ctl_event);
  1788. result += sizeof(struct snd_ctl_event);
  1789. }
  1790. __end_lock:
  1791. spin_unlock_irq(&ctl->read_lock);
  1792. __end:
  1793. return result > 0 ? result : err;
  1794. }
  1795. static __poll_t snd_ctl_poll(struct file *file, poll_table * wait)
  1796. {
  1797. __poll_t mask;
  1798. struct snd_ctl_file *ctl;
  1799. ctl = file->private_data;
  1800. if (!ctl->subscribed)
  1801. return 0;
  1802. poll_wait(file, &ctl->change_sleep, wait);
  1803. mask = 0;
  1804. if (!list_empty(&ctl->events))
  1805. mask |= EPOLLIN | EPOLLRDNORM;
  1806. return mask;
  1807. }
  1808. /*
  1809. * register the device-specific control-ioctls.
  1810. * called from each device manager like pcm.c, hwdep.c, etc.
  1811. */
  1812. static int _snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn, struct list_head *lists)
  1813. {
  1814. struct snd_kctl_ioctl *pn;
  1815. pn = kzalloc(sizeof(struct snd_kctl_ioctl), GFP_KERNEL);
  1816. if (pn == NULL)
  1817. return -ENOMEM;
  1818. pn->fioctl = fcn;
  1819. guard(rwsem_write)(&snd_ioctl_rwsem);
  1820. list_add_tail(&pn->list, lists);
  1821. return 0;
  1822. }
  1823. /**
  1824. * snd_ctl_register_ioctl - register the device-specific control-ioctls
  1825. * @fcn: ioctl callback function
  1826. *
  1827. * called from each device manager like pcm.c, hwdep.c, etc.
  1828. *
  1829. * Return: zero if successful, or a negative error code
  1830. */
  1831. int snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn)
  1832. {
  1833. return _snd_ctl_register_ioctl(fcn, &snd_control_ioctls);
  1834. }
  1835. EXPORT_SYMBOL(snd_ctl_register_ioctl);
  1836. #ifdef CONFIG_COMPAT
  1837. /**
  1838. * snd_ctl_register_ioctl_compat - register the device-specific 32bit compat
  1839. * control-ioctls
  1840. * @fcn: ioctl callback function
  1841. *
  1842. * Return: zero if successful, or a negative error code
  1843. */
  1844. int snd_ctl_register_ioctl_compat(snd_kctl_ioctl_func_t fcn)
  1845. {
  1846. return _snd_ctl_register_ioctl(fcn, &snd_control_compat_ioctls);
  1847. }
  1848. EXPORT_SYMBOL(snd_ctl_register_ioctl_compat);
  1849. #endif
  1850. /*
  1851. * de-register the device-specific control-ioctls.
  1852. */
  1853. static int _snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn,
  1854. struct list_head *lists)
  1855. {
  1856. struct snd_kctl_ioctl *p;
  1857. if (snd_BUG_ON(!fcn))
  1858. return -EINVAL;
  1859. guard(rwsem_write)(&snd_ioctl_rwsem);
  1860. list_for_each_entry(p, lists, list) {
  1861. if (p->fioctl == fcn) {
  1862. list_del(&p->list);
  1863. kfree(p);
  1864. return 0;
  1865. }
  1866. }
  1867. snd_BUG();
  1868. return -EINVAL;
  1869. }
  1870. /**
  1871. * snd_ctl_unregister_ioctl - de-register the device-specific control-ioctls
  1872. * @fcn: ioctl callback function to unregister
  1873. *
  1874. * Return: zero if successful, or a negative error code
  1875. */
  1876. int snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn)
  1877. {
  1878. return _snd_ctl_unregister_ioctl(fcn, &snd_control_ioctls);
  1879. }
  1880. EXPORT_SYMBOL(snd_ctl_unregister_ioctl);
  1881. #ifdef CONFIG_COMPAT
  1882. /**
  1883. * snd_ctl_unregister_ioctl_compat - de-register the device-specific compat
  1884. * 32bit control-ioctls
  1885. * @fcn: ioctl callback function to unregister
  1886. *
  1887. * Return: zero if successful, or a negative error code
  1888. */
  1889. int snd_ctl_unregister_ioctl_compat(snd_kctl_ioctl_func_t fcn)
  1890. {
  1891. return _snd_ctl_unregister_ioctl(fcn, &snd_control_compat_ioctls);
  1892. }
  1893. EXPORT_SYMBOL(snd_ctl_unregister_ioctl_compat);
  1894. #endif
  1895. static int snd_ctl_fasync(int fd, struct file * file, int on)
  1896. {
  1897. struct snd_ctl_file *ctl;
  1898. ctl = file->private_data;
  1899. return snd_fasync_helper(fd, file, on, &ctl->fasync);
  1900. }
  1901. /* return the preferred subdevice number if already assigned;
  1902. * otherwise return -1
  1903. */
  1904. int snd_ctl_get_preferred_subdevice(struct snd_card *card, int type)
  1905. {
  1906. struct snd_ctl_file *kctl;
  1907. int subdevice = -1;
  1908. guard(read_lock_irqsave)(&card->controls_rwlock);
  1909. list_for_each_entry(kctl, &card->ctl_files, list) {
  1910. if (kctl->pid == task_pid(current)) {
  1911. subdevice = kctl->preferred_subdevice[type];
  1912. if (subdevice != -1)
  1913. break;
  1914. }
  1915. }
  1916. return subdevice;
  1917. }
  1918. EXPORT_SYMBOL_GPL(snd_ctl_get_preferred_subdevice);
  1919. /*
  1920. * ioctl32 compat
  1921. */
  1922. #ifdef CONFIG_COMPAT
  1923. #include "control_compat.c"
  1924. #else
  1925. #define snd_ctl_ioctl_compat NULL
  1926. #endif
  1927. /*
  1928. * control layers (audio LED etc.)
  1929. */
  1930. /**
  1931. * snd_ctl_request_layer - request to use the layer
  1932. * @module_name: Name of the kernel module (NULL == build-in)
  1933. *
  1934. * Return: zero if successful, or an error code when the module cannot be loaded
  1935. */
  1936. int snd_ctl_request_layer(const char *module_name)
  1937. {
  1938. struct snd_ctl_layer_ops *lops;
  1939. if (module_name == NULL)
  1940. return 0;
  1941. scoped_guard(rwsem_read, &snd_ctl_layer_rwsem) {
  1942. for (lops = snd_ctl_layer; lops; lops = lops->next)
  1943. if (strcmp(lops->module_name, module_name) == 0)
  1944. return 0;
  1945. }
  1946. return request_module(module_name);
  1947. }
  1948. EXPORT_SYMBOL_GPL(snd_ctl_request_layer);
  1949. /**
  1950. * snd_ctl_register_layer - register new control layer
  1951. * @lops: operation structure
  1952. *
  1953. * The new layer can track all control elements and do additional
  1954. * operations on top (like audio LED handling).
  1955. */
  1956. void snd_ctl_register_layer(struct snd_ctl_layer_ops *lops)
  1957. {
  1958. struct snd_card *card;
  1959. int card_number;
  1960. scoped_guard(rwsem_write, &snd_ctl_layer_rwsem) {
  1961. lops->next = snd_ctl_layer;
  1962. snd_ctl_layer = lops;
  1963. }
  1964. for (card_number = 0; card_number < SNDRV_CARDS; card_number++) {
  1965. card = snd_card_ref(card_number);
  1966. if (card) {
  1967. scoped_guard(rwsem_read, &card->controls_rwsem)
  1968. lops->lregister(card);
  1969. snd_card_unref(card);
  1970. }
  1971. }
  1972. }
  1973. EXPORT_SYMBOL_GPL(snd_ctl_register_layer);
  1974. /**
  1975. * snd_ctl_disconnect_layer - disconnect control layer
  1976. * @lops: operation structure
  1977. *
  1978. * It is expected that the information about tracked cards
  1979. * is freed before this call (the disconnect callback is
  1980. * not called here).
  1981. */
  1982. void snd_ctl_disconnect_layer(struct snd_ctl_layer_ops *lops)
  1983. {
  1984. struct snd_ctl_layer_ops *lops2, *prev_lops2;
  1985. guard(rwsem_write)(&snd_ctl_layer_rwsem);
  1986. for (lops2 = snd_ctl_layer, prev_lops2 = NULL; lops2; lops2 = lops2->next) {
  1987. if (lops2 == lops) {
  1988. if (!prev_lops2)
  1989. snd_ctl_layer = lops->next;
  1990. else
  1991. prev_lops2->next = lops->next;
  1992. break;
  1993. }
  1994. prev_lops2 = lops2;
  1995. }
  1996. }
  1997. EXPORT_SYMBOL_GPL(snd_ctl_disconnect_layer);
  1998. /*
  1999. * INIT PART
  2000. */
  2001. static const struct file_operations snd_ctl_f_ops =
  2002. {
  2003. .owner = THIS_MODULE,
  2004. .read = snd_ctl_read,
  2005. .open = snd_ctl_open,
  2006. .release = snd_ctl_release,
  2007. .poll = snd_ctl_poll,
  2008. .unlocked_ioctl = snd_ctl_ioctl,
  2009. .compat_ioctl = snd_ctl_ioctl_compat,
  2010. .fasync = snd_ctl_fasync,
  2011. };
  2012. /* call lops under rwsems; called from snd_ctl_dev_*() below() */
  2013. #define call_snd_ctl_lops(_card, _op) \
  2014. do { \
  2015. struct snd_ctl_layer_ops *lops; \
  2016. guard(rwsem_read)(&(_card)->controls_rwsem); \
  2017. guard(rwsem_read)(&snd_ctl_layer_rwsem); \
  2018. for (lops = snd_ctl_layer; lops; lops = lops->next) \
  2019. lops->_op(_card); \
  2020. } while (0)
  2021. /*
  2022. * registration of the control device
  2023. */
  2024. static int snd_ctl_dev_register(struct snd_device *device)
  2025. {
  2026. struct snd_card *card = device->device_data;
  2027. int err;
  2028. err = snd_register_device(SNDRV_DEVICE_TYPE_CONTROL, card, -1,
  2029. &snd_ctl_f_ops, card, card->ctl_dev);
  2030. if (err < 0)
  2031. return err;
  2032. call_snd_ctl_lops(card, lregister);
  2033. return 0;
  2034. }
  2035. /*
  2036. * disconnection of the control device
  2037. */
  2038. static int snd_ctl_dev_disconnect(struct snd_device *device)
  2039. {
  2040. struct snd_card *card = device->device_data;
  2041. struct snd_ctl_file *ctl;
  2042. scoped_guard(read_lock_irqsave, &card->controls_rwlock) {
  2043. list_for_each_entry(ctl, &card->ctl_files, list) {
  2044. wake_up(&ctl->change_sleep);
  2045. snd_kill_fasync(ctl->fasync, SIGIO, POLL_ERR);
  2046. }
  2047. }
  2048. call_snd_ctl_lops(card, ldisconnect);
  2049. return snd_unregister_device(card->ctl_dev);
  2050. }
  2051. /*
  2052. * free all controls
  2053. */
  2054. static int snd_ctl_dev_free(struct snd_device *device)
  2055. {
  2056. struct snd_card *card = device->device_data;
  2057. struct snd_kcontrol *control;
  2058. scoped_guard(rwsem_write, &card->controls_rwsem) {
  2059. while (!list_empty(&card->controls)) {
  2060. control = snd_kcontrol(card->controls.next);
  2061. __snd_ctl_remove(card, control, false);
  2062. }
  2063. #ifdef CONFIG_SND_CTL_FAST_LOOKUP
  2064. xa_destroy(&card->ctl_numids);
  2065. xa_destroy(&card->ctl_hash);
  2066. #endif
  2067. }
  2068. put_device(card->ctl_dev);
  2069. return 0;
  2070. }
  2071. /*
  2072. * create control core:
  2073. * called from init.c
  2074. */
  2075. int snd_ctl_create(struct snd_card *card)
  2076. {
  2077. static const struct snd_device_ops ops = {
  2078. .dev_free = snd_ctl_dev_free,
  2079. .dev_register = snd_ctl_dev_register,
  2080. .dev_disconnect = snd_ctl_dev_disconnect,
  2081. };
  2082. int err;
  2083. if (snd_BUG_ON(!card))
  2084. return -ENXIO;
  2085. if (snd_BUG_ON(card->number < 0 || card->number >= SNDRV_CARDS))
  2086. return -ENXIO;
  2087. err = snd_device_alloc(&card->ctl_dev, card);
  2088. if (err < 0)
  2089. return err;
  2090. dev_set_name(card->ctl_dev, "controlC%d", card->number);
  2091. err = snd_device_new(card, SNDRV_DEV_CONTROL, card, &ops);
  2092. if (err < 0)
  2093. put_device(card->ctl_dev);
  2094. return err;
  2095. }
  2096. /*
  2097. * Frequently used control callbacks/helpers
  2098. */
  2099. /**
  2100. * snd_ctl_boolean_mono_info - Helper function for a standard boolean info
  2101. * callback with a mono channel
  2102. * @kcontrol: the kcontrol instance
  2103. * @uinfo: info to store
  2104. *
  2105. * This is a function that can be used as info callback for a standard
  2106. * boolean control with a single mono channel.
  2107. *
  2108. * Return: Zero (always successful)
  2109. */
  2110. int snd_ctl_boolean_mono_info(struct snd_kcontrol *kcontrol,
  2111. struct snd_ctl_elem_info *uinfo)
  2112. {
  2113. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  2114. uinfo->count = 1;
  2115. uinfo->value.integer.min = 0;
  2116. uinfo->value.integer.max = 1;
  2117. return 0;
  2118. }
  2119. EXPORT_SYMBOL(snd_ctl_boolean_mono_info);
  2120. /**
  2121. * snd_ctl_boolean_stereo_info - Helper function for a standard boolean info
  2122. * callback with stereo two channels
  2123. * @kcontrol: the kcontrol instance
  2124. * @uinfo: info to store
  2125. *
  2126. * This is a function that can be used as info callback for a standard
  2127. * boolean control with stereo two channels.
  2128. *
  2129. * Return: Zero (always successful)
  2130. */
  2131. int snd_ctl_boolean_stereo_info(struct snd_kcontrol *kcontrol,
  2132. struct snd_ctl_elem_info *uinfo)
  2133. {
  2134. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  2135. uinfo->count = 2;
  2136. uinfo->value.integer.min = 0;
  2137. uinfo->value.integer.max = 1;
  2138. return 0;
  2139. }
  2140. EXPORT_SYMBOL(snd_ctl_boolean_stereo_info);
  2141. /**
  2142. * snd_ctl_enum_info - fills the info structure for an enumerated control
  2143. * @info: the structure to be filled
  2144. * @channels: the number of the control's channels; often one
  2145. * @items: the number of control values; also the size of @names
  2146. * @names: an array containing the names of all control values
  2147. *
  2148. * Sets all required fields in @info to their appropriate values.
  2149. * If the control's accessibility is not the default (readable and writable),
  2150. * the caller has to fill @info->access.
  2151. *
  2152. * Return: Zero (always successful)
  2153. */
  2154. int snd_ctl_enum_info(struct snd_ctl_elem_info *info, unsigned int channels,
  2155. unsigned int items, const char *const names[])
  2156. {
  2157. info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  2158. info->count = channels;
  2159. info->value.enumerated.items = items;
  2160. if (!items)
  2161. return 0;
  2162. if (info->value.enumerated.item >= items)
  2163. info->value.enumerated.item = items - 1;
  2164. WARN(strlen(names[info->value.enumerated.item]) >= sizeof(info->value.enumerated.name),
  2165. "ALSA: too long item name '%s'\n",
  2166. names[info->value.enumerated.item]);
  2167. strscpy(info->value.enumerated.name,
  2168. names[info->value.enumerated.item],
  2169. sizeof(info->value.enumerated.name));
  2170. return 0;
  2171. }
  2172. EXPORT_SYMBOL(snd_ctl_enum_info);