mixer.c 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967
  1. /*
  2. * (Tentative) USB Audio Driver for ALSA
  3. *
  4. * Mixer control part
  5. *
  6. * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
  7. *
  8. * Many codes borrowed from audio.c by
  9. * Alan Cox (alan@lxorguk.ukuu.org.uk)
  10. * Thomas Sailer (sailer@ife.ee.ethz.ch)
  11. *
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. *
  27. */
  28. /*
  29. * TODOs, for both the mixer and the streaming interfaces:
  30. *
  31. * - support for UAC2 effect units
  32. * - support for graphical equalizers
  33. * - RANGE and MEM set commands (UAC2)
  34. * - RANGE and MEM interrupt dispatchers (UAC2)
  35. * - audio channel clustering (UAC2)
  36. * - audio sample rate converter units (UAC2)
  37. * - proper handling of clock multipliers (UAC2)
  38. * - dispatch clock change notifications (UAC2)
  39. * - stop PCM streams which use a clock that became invalid
  40. * - stop PCM streams which use a clock selector that has changed
  41. * - parse available sample rates again when clock sources changed
  42. */
  43. #include <linux/bitops.h>
  44. #include <linux/init.h>
  45. #include <linux/list.h>
  46. #include <linux/log2.h>
  47. #include <linux/slab.h>
  48. #include <linux/string.h>
  49. #include <linux/usb.h>
  50. #include <linux/usb/audio.h>
  51. #include <linux/usb/audio-v2.h>
  52. #include <linux/usb/audio-v3.h>
  53. #include <sound/core.h>
  54. #include <sound/control.h>
  55. #include <sound/hwdep.h>
  56. #include <sound/info.h>
  57. #include <sound/tlv.h>
  58. #include "usbaudio.h"
  59. #include "mixer.h"
  60. #include "helper.h"
  61. #include "mixer_quirks.h"
  62. #include "power.h"
  63. #define MAX_ID_ELEMS 256
  64. struct usb_audio_term {
  65. int id;
  66. int type;
  67. int channels;
  68. unsigned int chconfig;
  69. int name;
  70. };
  71. struct usbmix_name_map;
  72. struct mixer_build {
  73. struct snd_usb_audio *chip;
  74. struct usb_mixer_interface *mixer;
  75. unsigned char *buffer;
  76. unsigned int buflen;
  77. DECLARE_BITMAP(unitbitmap, MAX_ID_ELEMS);
  78. struct usb_audio_term oterm;
  79. const struct usbmix_name_map *map;
  80. const struct usbmix_selector_map *selector_map;
  81. };
  82. /*E-mu 0202/0404/0204 eXtension Unit(XU) control*/
  83. enum {
  84. USB_XU_CLOCK_RATE = 0xe301,
  85. USB_XU_CLOCK_SOURCE = 0xe302,
  86. USB_XU_DIGITAL_IO_STATUS = 0xe303,
  87. USB_XU_DEVICE_OPTIONS = 0xe304,
  88. USB_XU_DIRECT_MONITORING = 0xe305,
  89. USB_XU_METERING = 0xe306
  90. };
  91. enum {
  92. USB_XU_CLOCK_SOURCE_SELECTOR = 0x02, /* clock source*/
  93. USB_XU_CLOCK_RATE_SELECTOR = 0x03, /* clock rate */
  94. USB_XU_DIGITAL_FORMAT_SELECTOR = 0x01, /* the spdif format */
  95. USB_XU_SOFT_LIMIT_SELECTOR = 0x03 /* soft limiter */
  96. };
  97. /*
  98. * manual mapping of mixer names
  99. * if the mixer topology is too complicated and the parsed names are
  100. * ambiguous, add the entries in usbmixer_maps.c.
  101. */
  102. #include "mixer_maps.c"
  103. static const struct usbmix_name_map *
  104. find_map(struct mixer_build *state, int unitid, int control)
  105. {
  106. const struct usbmix_name_map *p = state->map;
  107. if (!p)
  108. return NULL;
  109. for (p = state->map; p->id; p++) {
  110. if (p->id == unitid &&
  111. (!control || !p->control || control == p->control))
  112. return p;
  113. }
  114. return NULL;
  115. }
  116. /* get the mapped name if the unit matches */
  117. static int
  118. check_mapped_name(const struct usbmix_name_map *p, char *buf, int buflen)
  119. {
  120. if (!p || !p->name)
  121. return 0;
  122. buflen--;
  123. return strlcpy(buf, p->name, buflen);
  124. }
  125. /* ignore the error value if ignore_ctl_error flag is set */
  126. #define filter_error(cval, err) \
  127. ((cval)->head.mixer->ignore_ctl_error ? 0 : (err))
  128. /* check whether the control should be ignored */
  129. static inline int
  130. check_ignored_ctl(const struct usbmix_name_map *p)
  131. {
  132. if (!p || p->name || p->dB)
  133. return 0;
  134. return 1;
  135. }
  136. /* dB mapping */
  137. static inline void check_mapped_dB(const struct usbmix_name_map *p,
  138. struct usb_mixer_elem_info *cval)
  139. {
  140. if (p && p->dB) {
  141. cval->dBmin = p->dB->min;
  142. cval->dBmax = p->dB->max;
  143. cval->initialized = 1;
  144. }
  145. }
  146. /* get the mapped selector source name */
  147. static int check_mapped_selector_name(struct mixer_build *state, int unitid,
  148. int index, char *buf, int buflen)
  149. {
  150. const struct usbmix_selector_map *p;
  151. if (!state->selector_map)
  152. return 0;
  153. for (p = state->selector_map; p->id; p++) {
  154. if (p->id == unitid && index < p->count)
  155. return strlcpy(buf, p->names[index], buflen);
  156. }
  157. return 0;
  158. }
  159. /*
  160. * find an audio control unit with the given unit id
  161. */
  162. static void *find_audio_control_unit(struct mixer_build *state,
  163. unsigned char unit)
  164. {
  165. /* we just parse the header */
  166. struct uac_feature_unit_descriptor *hdr = NULL;
  167. while ((hdr = snd_usb_find_desc(state->buffer, state->buflen, hdr,
  168. USB_DT_CS_INTERFACE)) != NULL) {
  169. if (hdr->bLength >= 4 &&
  170. hdr->bDescriptorSubtype >= UAC_INPUT_TERMINAL &&
  171. hdr->bDescriptorSubtype <= UAC3_SAMPLE_RATE_CONVERTER &&
  172. hdr->bUnitID == unit)
  173. return hdr;
  174. }
  175. return NULL;
  176. }
  177. /*
  178. * copy a string with the given id
  179. */
  180. static int snd_usb_copy_string_desc(struct mixer_build *state,
  181. int index, char *buf, int maxlen)
  182. {
  183. int len = usb_string(state->chip->dev, index, buf, maxlen - 1);
  184. if (len < 0)
  185. return 0;
  186. buf[len] = 0;
  187. return len;
  188. }
  189. /*
  190. * convert from the byte/word on usb descriptor to the zero-based integer
  191. */
  192. static int convert_signed_value(struct usb_mixer_elem_info *cval, int val)
  193. {
  194. switch (cval->val_type) {
  195. case USB_MIXER_BOOLEAN:
  196. return !!val;
  197. case USB_MIXER_INV_BOOLEAN:
  198. return !val;
  199. case USB_MIXER_U8:
  200. val &= 0xff;
  201. break;
  202. case USB_MIXER_S8:
  203. val &= 0xff;
  204. if (val >= 0x80)
  205. val -= 0x100;
  206. break;
  207. case USB_MIXER_U16:
  208. val &= 0xffff;
  209. break;
  210. case USB_MIXER_S16:
  211. val &= 0xffff;
  212. if (val >= 0x8000)
  213. val -= 0x10000;
  214. break;
  215. }
  216. return val;
  217. }
  218. /*
  219. * convert from the zero-based int to the byte/word for usb descriptor
  220. */
  221. static int convert_bytes_value(struct usb_mixer_elem_info *cval, int val)
  222. {
  223. switch (cval->val_type) {
  224. case USB_MIXER_BOOLEAN:
  225. return !!val;
  226. case USB_MIXER_INV_BOOLEAN:
  227. return !val;
  228. case USB_MIXER_S8:
  229. case USB_MIXER_U8:
  230. return val & 0xff;
  231. case USB_MIXER_S16:
  232. case USB_MIXER_U16:
  233. return val & 0xffff;
  234. }
  235. return 0; /* not reached */
  236. }
  237. static int get_relative_value(struct usb_mixer_elem_info *cval, int val)
  238. {
  239. if (!cval->res)
  240. cval->res = 1;
  241. if (val < cval->min)
  242. return 0;
  243. else if (val >= cval->max)
  244. return (cval->max - cval->min + cval->res - 1) / cval->res;
  245. else
  246. return (val - cval->min) / cval->res;
  247. }
  248. static int get_abs_value(struct usb_mixer_elem_info *cval, int val)
  249. {
  250. if (val < 0)
  251. return cval->min;
  252. if (!cval->res)
  253. cval->res = 1;
  254. val *= cval->res;
  255. val += cval->min;
  256. if (val > cval->max)
  257. return cval->max;
  258. return val;
  259. }
  260. static int uac2_ctl_value_size(int val_type)
  261. {
  262. switch (val_type) {
  263. case USB_MIXER_S32:
  264. case USB_MIXER_U32:
  265. return 4;
  266. case USB_MIXER_S16:
  267. case USB_MIXER_U16:
  268. return 2;
  269. default:
  270. return 1;
  271. }
  272. return 0; /* unreachable */
  273. }
  274. /*
  275. * retrieve a mixer value
  276. */
  277. static int get_ctl_value_v1(struct usb_mixer_elem_info *cval, int request,
  278. int validx, int *value_ret)
  279. {
  280. struct snd_usb_audio *chip = cval->head.mixer->chip;
  281. unsigned char buf[2];
  282. int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
  283. int timeout = 10;
  284. int idx = 0, err;
  285. err = snd_usb_lock_shutdown(chip);
  286. if (err < 0)
  287. return -EIO;
  288. while (timeout-- > 0) {
  289. idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8);
  290. err = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), request,
  291. USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
  292. validx, idx, buf, val_len);
  293. if (err >= val_len) {
  294. *value_ret = convert_signed_value(cval, snd_usb_combine_bytes(buf, val_len));
  295. err = 0;
  296. goto out;
  297. } else if (err == -ETIMEDOUT) {
  298. goto out;
  299. }
  300. }
  301. usb_audio_dbg(chip,
  302. "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
  303. request, validx, idx, cval->val_type);
  304. err = -EINVAL;
  305. out:
  306. snd_usb_unlock_shutdown(chip);
  307. return err;
  308. }
  309. static int get_ctl_value_v2(struct usb_mixer_elem_info *cval, int request,
  310. int validx, int *value_ret)
  311. {
  312. struct snd_usb_audio *chip = cval->head.mixer->chip;
  313. /* enough space for one range */
  314. unsigned char buf[sizeof(__u16) + 3 * sizeof(__u32)];
  315. unsigned char *val;
  316. int idx = 0, ret, val_size, size;
  317. __u8 bRequest;
  318. val_size = uac2_ctl_value_size(cval->val_type);
  319. if (request == UAC_GET_CUR) {
  320. bRequest = UAC2_CS_CUR;
  321. size = val_size;
  322. } else {
  323. bRequest = UAC2_CS_RANGE;
  324. size = sizeof(__u16) + 3 * val_size;
  325. }
  326. memset(buf, 0, sizeof(buf));
  327. ret = snd_usb_lock_shutdown(chip) ? -EIO : 0;
  328. if (ret)
  329. goto error;
  330. idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8);
  331. ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), bRequest,
  332. USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
  333. validx, idx, buf, size);
  334. snd_usb_unlock_shutdown(chip);
  335. if (ret < 0) {
  336. error:
  337. usb_audio_err(chip,
  338. "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
  339. request, validx, idx, cval->val_type);
  340. return ret;
  341. }
  342. /* FIXME: how should we handle multiple triplets here? */
  343. switch (request) {
  344. case UAC_GET_CUR:
  345. val = buf;
  346. break;
  347. case UAC_GET_MIN:
  348. val = buf + sizeof(__u16);
  349. break;
  350. case UAC_GET_MAX:
  351. val = buf + sizeof(__u16) + val_size;
  352. break;
  353. case UAC_GET_RES:
  354. val = buf + sizeof(__u16) + val_size * 2;
  355. break;
  356. default:
  357. return -EINVAL;
  358. }
  359. *value_ret = convert_signed_value(cval,
  360. snd_usb_combine_bytes(val, val_size));
  361. return 0;
  362. }
  363. static int get_ctl_value(struct usb_mixer_elem_info *cval, int request,
  364. int validx, int *value_ret)
  365. {
  366. validx += cval->idx_off;
  367. return (cval->head.mixer->protocol == UAC_VERSION_1) ?
  368. get_ctl_value_v1(cval, request, validx, value_ret) :
  369. get_ctl_value_v2(cval, request, validx, value_ret);
  370. }
  371. static int get_cur_ctl_value(struct usb_mixer_elem_info *cval,
  372. int validx, int *value)
  373. {
  374. return get_ctl_value(cval, UAC_GET_CUR, validx, value);
  375. }
  376. /* channel = 0: master, 1 = first channel */
  377. static inline int get_cur_mix_raw(struct usb_mixer_elem_info *cval,
  378. int channel, int *value)
  379. {
  380. return get_ctl_value(cval, UAC_GET_CUR,
  381. (cval->control << 8) | channel,
  382. value);
  383. }
  384. int snd_usb_get_cur_mix_value(struct usb_mixer_elem_info *cval,
  385. int channel, int index, int *value)
  386. {
  387. int err;
  388. if (cval->cached & (1 << channel)) {
  389. *value = cval->cache_val[index];
  390. return 0;
  391. }
  392. err = get_cur_mix_raw(cval, channel, value);
  393. if (err < 0) {
  394. if (!cval->head.mixer->ignore_ctl_error)
  395. usb_audio_dbg(cval->head.mixer->chip,
  396. "cannot get current value for control %d ch %d: err = %d\n",
  397. cval->control, channel, err);
  398. return err;
  399. }
  400. cval->cached |= 1 << channel;
  401. cval->cache_val[index] = *value;
  402. return 0;
  403. }
  404. /*
  405. * set a mixer value
  406. */
  407. int snd_usb_mixer_set_ctl_value(struct usb_mixer_elem_info *cval,
  408. int request, int validx, int value_set)
  409. {
  410. struct snd_usb_audio *chip = cval->head.mixer->chip;
  411. unsigned char buf[4];
  412. int idx = 0, val_len, err, timeout = 10;
  413. validx += cval->idx_off;
  414. if (cval->head.mixer->protocol == UAC_VERSION_1) {
  415. val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
  416. } else { /* UAC_VERSION_2/3 */
  417. val_len = uac2_ctl_value_size(cval->val_type);
  418. /* FIXME */
  419. if (request != UAC_SET_CUR) {
  420. usb_audio_dbg(chip, "RANGE setting not yet supported\n");
  421. return -EINVAL;
  422. }
  423. request = UAC2_CS_CUR;
  424. }
  425. value_set = convert_bytes_value(cval, value_set);
  426. buf[0] = value_set & 0xff;
  427. buf[1] = (value_set >> 8) & 0xff;
  428. buf[2] = (value_set >> 16) & 0xff;
  429. buf[3] = (value_set >> 24) & 0xff;
  430. err = snd_usb_lock_shutdown(chip);
  431. if (err < 0)
  432. return -EIO;
  433. while (timeout-- > 0) {
  434. idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8);
  435. err = snd_usb_ctl_msg(chip->dev,
  436. usb_sndctrlpipe(chip->dev, 0), request,
  437. USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
  438. validx, idx, buf, val_len);
  439. if (err >= 0) {
  440. err = 0;
  441. goto out;
  442. } else if (err == -ETIMEDOUT) {
  443. goto out;
  444. }
  445. }
  446. usb_audio_dbg(chip, "cannot set ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d, data = %#x/%#x\n",
  447. request, validx, idx, cval->val_type, buf[0], buf[1]);
  448. err = -EINVAL;
  449. out:
  450. snd_usb_unlock_shutdown(chip);
  451. return err;
  452. }
  453. static int set_cur_ctl_value(struct usb_mixer_elem_info *cval,
  454. int validx, int value)
  455. {
  456. return snd_usb_mixer_set_ctl_value(cval, UAC_SET_CUR, validx, value);
  457. }
  458. int snd_usb_set_cur_mix_value(struct usb_mixer_elem_info *cval, int channel,
  459. int index, int value)
  460. {
  461. int err;
  462. unsigned int read_only = (channel == 0) ?
  463. cval->master_readonly :
  464. cval->ch_readonly & (1 << (channel - 1));
  465. if (read_only) {
  466. usb_audio_dbg(cval->head.mixer->chip,
  467. "%s(): channel %d of control %d is read_only\n",
  468. __func__, channel, cval->control);
  469. return 0;
  470. }
  471. err = snd_usb_mixer_set_ctl_value(cval,
  472. UAC_SET_CUR, (cval->control << 8) | channel,
  473. value);
  474. if (err < 0)
  475. return err;
  476. cval->cached |= 1 << channel;
  477. cval->cache_val[index] = value;
  478. return 0;
  479. }
  480. /*
  481. * TLV callback for mixer volume controls
  482. */
  483. int snd_usb_mixer_vol_tlv(struct snd_kcontrol *kcontrol, int op_flag,
  484. unsigned int size, unsigned int __user *_tlv)
  485. {
  486. struct usb_mixer_elem_info *cval = kcontrol->private_data;
  487. DECLARE_TLV_DB_MINMAX(scale, 0, 0);
  488. if (size < sizeof(scale))
  489. return -ENOMEM;
  490. if (cval->min_mute)
  491. scale[0] = SNDRV_CTL_TLVT_DB_MINMAX_MUTE;
  492. scale[2] = cval->dBmin;
  493. scale[3] = cval->dBmax;
  494. if (copy_to_user(_tlv, scale, sizeof(scale)))
  495. return -EFAULT;
  496. return 0;
  497. }
  498. /*
  499. * parser routines begin here...
  500. */
  501. static int parse_audio_unit(struct mixer_build *state, int unitid);
  502. /*
  503. * check if the input/output channel routing is enabled on the given bitmap.
  504. * used for mixer unit parser
  505. */
  506. static int check_matrix_bitmap(unsigned char *bmap,
  507. int ich, int och, int num_outs)
  508. {
  509. int idx = ich * num_outs + och;
  510. return bmap[idx >> 3] & (0x80 >> (idx & 7));
  511. }
  512. /*
  513. * add an alsa control element
  514. * search and increment the index until an empty slot is found.
  515. *
  516. * if failed, give up and free the control instance.
  517. */
  518. int snd_usb_mixer_add_control(struct usb_mixer_elem_list *list,
  519. struct snd_kcontrol *kctl)
  520. {
  521. struct usb_mixer_interface *mixer = list->mixer;
  522. int err;
  523. while (snd_ctl_find_id(mixer->chip->card, &kctl->id))
  524. kctl->id.index++;
  525. if ((err = snd_ctl_add(mixer->chip->card, kctl)) < 0) {
  526. usb_audio_dbg(mixer->chip, "cannot add control (err = %d)\n",
  527. err);
  528. return err;
  529. }
  530. list->kctl = kctl;
  531. list->next_id_elem = mixer->id_elems[list->id];
  532. mixer->id_elems[list->id] = list;
  533. return 0;
  534. }
  535. /*
  536. * get a terminal name string
  537. */
  538. static struct iterm_name_combo {
  539. int type;
  540. char *name;
  541. } iterm_names[] = {
  542. { 0x0300, "Output" },
  543. { 0x0301, "Speaker" },
  544. { 0x0302, "Headphone" },
  545. { 0x0303, "HMD Audio" },
  546. { 0x0304, "Desktop Speaker" },
  547. { 0x0305, "Room Speaker" },
  548. { 0x0306, "Com Speaker" },
  549. { 0x0307, "LFE" },
  550. { 0x0600, "External In" },
  551. { 0x0601, "Analog In" },
  552. { 0x0602, "Digital In" },
  553. { 0x0603, "Line" },
  554. { 0x0604, "Legacy In" },
  555. { 0x0605, "IEC958 In" },
  556. { 0x0606, "1394 DA Stream" },
  557. { 0x0607, "1394 DV Stream" },
  558. { 0x0700, "Embedded" },
  559. { 0x0701, "Noise Source" },
  560. { 0x0702, "Equalization Noise" },
  561. { 0x0703, "CD" },
  562. { 0x0704, "DAT" },
  563. { 0x0705, "DCC" },
  564. { 0x0706, "MiniDisk" },
  565. { 0x0707, "Analog Tape" },
  566. { 0x0708, "Phonograph" },
  567. { 0x0709, "VCR Audio" },
  568. { 0x070a, "Video Disk Audio" },
  569. { 0x070b, "DVD Audio" },
  570. { 0x070c, "TV Tuner Audio" },
  571. { 0x070d, "Satellite Rec Audio" },
  572. { 0x070e, "Cable Tuner Audio" },
  573. { 0x070f, "DSS Audio" },
  574. { 0x0710, "Radio Receiver" },
  575. { 0x0711, "Radio Transmitter" },
  576. { 0x0712, "Multi-Track Recorder" },
  577. { 0x0713, "Synthesizer" },
  578. { 0 },
  579. };
  580. static int get_term_name(struct mixer_build *state, struct usb_audio_term *iterm,
  581. unsigned char *name, int maxlen, int term_only)
  582. {
  583. struct iterm_name_combo *names;
  584. int len;
  585. if (iterm->name) {
  586. len = snd_usb_copy_string_desc(state, iterm->name,
  587. name, maxlen);
  588. if (len)
  589. return len;
  590. }
  591. /* virtual type - not a real terminal */
  592. if (iterm->type >> 16) {
  593. if (term_only)
  594. return 0;
  595. switch (iterm->type >> 16) {
  596. case UAC_SELECTOR_UNIT:
  597. strcpy(name, "Selector");
  598. return 8;
  599. case UAC1_PROCESSING_UNIT:
  600. strcpy(name, "Process Unit");
  601. return 12;
  602. case UAC1_EXTENSION_UNIT:
  603. strcpy(name, "Ext Unit");
  604. return 8;
  605. case UAC_MIXER_UNIT:
  606. strcpy(name, "Mixer");
  607. return 5;
  608. default:
  609. return sprintf(name, "Unit %d", iterm->id);
  610. }
  611. }
  612. switch (iterm->type & 0xff00) {
  613. case 0x0100:
  614. strcpy(name, "PCM");
  615. return 3;
  616. case 0x0200:
  617. strcpy(name, "Mic");
  618. return 3;
  619. case 0x0400:
  620. strcpy(name, "Headset");
  621. return 7;
  622. case 0x0500:
  623. strcpy(name, "Phone");
  624. return 5;
  625. }
  626. for (names = iterm_names; names->type; names++) {
  627. if (names->type == iterm->type) {
  628. strcpy(name, names->name);
  629. return strlen(names->name);
  630. }
  631. }
  632. return 0;
  633. }
  634. /*
  635. * parse the source unit recursively until it reaches to a terminal
  636. * or a branched unit.
  637. */
  638. static int check_input_term(struct mixer_build *state, int id,
  639. struct usb_audio_term *term)
  640. {
  641. int protocol = state->mixer->protocol;
  642. int err;
  643. void *p1;
  644. memset(term, 0, sizeof(*term));
  645. while ((p1 = find_audio_control_unit(state, id)) != NULL) {
  646. unsigned char *hdr = p1;
  647. term->id = id;
  648. if (protocol == UAC_VERSION_1 || protocol == UAC_VERSION_2) {
  649. switch (hdr[2]) {
  650. case UAC_INPUT_TERMINAL:
  651. if (protocol == UAC_VERSION_1) {
  652. struct uac_input_terminal_descriptor *d = p1;
  653. term->type = le16_to_cpu(d->wTerminalType);
  654. term->channels = d->bNrChannels;
  655. term->chconfig = le16_to_cpu(d->wChannelConfig);
  656. term->name = d->iTerminal;
  657. } else { /* UAC_VERSION_2 */
  658. struct uac2_input_terminal_descriptor *d = p1;
  659. /* call recursively to verify that the
  660. * referenced clock entity is valid */
  661. err = check_input_term(state, d->bCSourceID, term);
  662. if (err < 0)
  663. return err;
  664. /* save input term properties after recursion,
  665. * to ensure they are not overriden by the
  666. * recursion calls */
  667. term->id = id;
  668. term->type = le16_to_cpu(d->wTerminalType);
  669. term->channels = d->bNrChannels;
  670. term->chconfig = le32_to_cpu(d->bmChannelConfig);
  671. term->name = d->iTerminal;
  672. }
  673. return 0;
  674. case UAC_FEATURE_UNIT: {
  675. /* the header is the same for v1 and v2 */
  676. struct uac_feature_unit_descriptor *d = p1;
  677. id = d->bSourceID;
  678. break; /* continue to parse */
  679. }
  680. case UAC_MIXER_UNIT: {
  681. struct uac_mixer_unit_descriptor *d = p1;
  682. term->type = d->bDescriptorSubtype << 16; /* virtual type */
  683. term->channels = uac_mixer_unit_bNrChannels(d);
  684. term->chconfig = uac_mixer_unit_wChannelConfig(d, protocol);
  685. term->name = uac_mixer_unit_iMixer(d);
  686. return 0;
  687. }
  688. case UAC_SELECTOR_UNIT:
  689. case UAC2_CLOCK_SELECTOR: {
  690. struct uac_selector_unit_descriptor *d = p1;
  691. /* call recursively to retrieve the channel info */
  692. err = check_input_term(state, d->baSourceID[0], term);
  693. if (err < 0)
  694. return err;
  695. term->type = d->bDescriptorSubtype << 16; /* virtual type */
  696. term->id = id;
  697. term->name = uac_selector_unit_iSelector(d);
  698. return 0;
  699. }
  700. case UAC1_PROCESSING_UNIT:
  701. case UAC1_EXTENSION_UNIT:
  702. /* UAC2_PROCESSING_UNIT_V2 */
  703. /* UAC2_EFFECT_UNIT */
  704. case UAC2_EXTENSION_UNIT_V2: {
  705. struct uac_processing_unit_descriptor *d = p1;
  706. if (protocol == UAC_VERSION_2 &&
  707. hdr[2] == UAC2_EFFECT_UNIT) {
  708. /* UAC2/UAC1 unit IDs overlap here in an
  709. * uncompatible way. Ignore this unit for now.
  710. */
  711. return 0;
  712. }
  713. if (d->bNrInPins) {
  714. id = d->baSourceID[0];
  715. break; /* continue to parse */
  716. }
  717. term->type = d->bDescriptorSubtype << 16; /* virtual type */
  718. term->channels = uac_processing_unit_bNrChannels(d);
  719. term->chconfig = uac_processing_unit_wChannelConfig(d, protocol);
  720. term->name = uac_processing_unit_iProcessing(d, protocol);
  721. return 0;
  722. }
  723. case UAC2_CLOCK_SOURCE: {
  724. struct uac_clock_source_descriptor *d = p1;
  725. term->type = d->bDescriptorSubtype << 16; /* virtual type */
  726. term->id = id;
  727. term->name = d->iClockSource;
  728. return 0;
  729. }
  730. default:
  731. return -ENODEV;
  732. }
  733. } else { /* UAC_VERSION_3 */
  734. switch (hdr[2]) {
  735. case UAC_INPUT_TERMINAL: {
  736. struct uac3_input_terminal_descriptor *d = p1;
  737. /* call recursively to verify that the
  738. * referenced clock entity is valid */
  739. err = check_input_term(state, d->bCSourceID, term);
  740. if (err < 0)
  741. return err;
  742. /* save input term properties after recursion,
  743. * to ensure they are not overriden by the
  744. * recursion calls */
  745. term->id = id;
  746. term->type = le16_to_cpu(d->wTerminalType);
  747. /* REVISIT: UAC3 IT doesn't have channels/cfg */
  748. term->channels = 0;
  749. term->chconfig = 0;
  750. term->name = le16_to_cpu(d->wTerminalDescrStr);
  751. return 0;
  752. }
  753. case UAC3_FEATURE_UNIT: {
  754. struct uac3_feature_unit_descriptor *d = p1;
  755. id = d->bSourceID;
  756. break; /* continue to parse */
  757. }
  758. case UAC3_CLOCK_SOURCE: {
  759. struct uac3_clock_source_descriptor *d = p1;
  760. term->type = d->bDescriptorSubtype << 16; /* virtual type */
  761. term->id = id;
  762. term->name = le16_to_cpu(d->wClockSourceStr);
  763. return 0;
  764. }
  765. default:
  766. return -ENODEV;
  767. }
  768. }
  769. }
  770. return -ENODEV;
  771. }
  772. /*
  773. * Feature Unit
  774. */
  775. /* feature unit control information */
  776. struct usb_feature_control_info {
  777. int control;
  778. const char *name;
  779. int type; /* data type for uac1 */
  780. int type_uac2; /* data type for uac2 if different from uac1, else -1 */
  781. };
  782. static struct usb_feature_control_info audio_feature_info[] = {
  783. { UAC_FU_MUTE, "Mute", USB_MIXER_INV_BOOLEAN, -1 },
  784. { UAC_FU_VOLUME, "Volume", USB_MIXER_S16, -1 },
  785. { UAC_FU_BASS, "Tone Control - Bass", USB_MIXER_S8, -1 },
  786. { UAC_FU_MID, "Tone Control - Mid", USB_MIXER_S8, -1 },
  787. { UAC_FU_TREBLE, "Tone Control - Treble", USB_MIXER_S8, -1 },
  788. { UAC_FU_GRAPHIC_EQUALIZER, "Graphic Equalizer", USB_MIXER_S8, -1 }, /* FIXME: not implemented yet */
  789. { UAC_FU_AUTOMATIC_GAIN, "Auto Gain Control", USB_MIXER_BOOLEAN, -1 },
  790. { UAC_FU_DELAY, "Delay Control", USB_MIXER_U16, USB_MIXER_U32 },
  791. { UAC_FU_BASS_BOOST, "Bass Boost", USB_MIXER_BOOLEAN, -1 },
  792. { UAC_FU_LOUDNESS, "Loudness", USB_MIXER_BOOLEAN, -1 },
  793. /* UAC2 specific */
  794. { UAC2_FU_INPUT_GAIN, "Input Gain Control", USB_MIXER_S16, -1 },
  795. { UAC2_FU_INPUT_GAIN_PAD, "Input Gain Pad Control", USB_MIXER_S16, -1 },
  796. { UAC2_FU_PHASE_INVERTER, "Phase Inverter Control", USB_MIXER_BOOLEAN, -1 },
  797. };
  798. /* private_free callback */
  799. void snd_usb_mixer_elem_free(struct snd_kcontrol *kctl)
  800. {
  801. kfree(kctl->private_data);
  802. kctl->private_data = NULL;
  803. }
  804. /*
  805. * interface to ALSA control for feature/mixer units
  806. */
  807. /* volume control quirks */
  808. static void volume_control_quirks(struct usb_mixer_elem_info *cval,
  809. struct snd_kcontrol *kctl)
  810. {
  811. struct snd_usb_audio *chip = cval->head.mixer->chip;
  812. switch (chip->usb_id) {
  813. case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */
  814. case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C600 */
  815. if (strcmp(kctl->id.name, "Effect Duration") == 0) {
  816. cval->min = 0x0000;
  817. cval->max = 0xffff;
  818. cval->res = 0x00e6;
  819. break;
  820. }
  821. if (strcmp(kctl->id.name, "Effect Volume") == 0 ||
  822. strcmp(kctl->id.name, "Effect Feedback Volume") == 0) {
  823. cval->min = 0x00;
  824. cval->max = 0xff;
  825. break;
  826. }
  827. if (strstr(kctl->id.name, "Effect Return") != NULL) {
  828. cval->min = 0xb706;
  829. cval->max = 0xff7b;
  830. cval->res = 0x0073;
  831. break;
  832. }
  833. if ((strstr(kctl->id.name, "Playback Volume") != NULL) ||
  834. (strstr(kctl->id.name, "Effect Send") != NULL)) {
  835. cval->min = 0xb5fb; /* -73 dB = 0xb6ff */
  836. cval->max = 0xfcfe;
  837. cval->res = 0x0073;
  838. }
  839. break;
  840. case USB_ID(0x0763, 0x2081): /* M-Audio Fast Track Ultra 8R */
  841. case USB_ID(0x0763, 0x2080): /* M-Audio Fast Track Ultra */
  842. if (strcmp(kctl->id.name, "Effect Duration") == 0) {
  843. usb_audio_info(chip,
  844. "set quirk for FTU Effect Duration\n");
  845. cval->min = 0x0000;
  846. cval->max = 0x7f00;
  847. cval->res = 0x0100;
  848. break;
  849. }
  850. if (strcmp(kctl->id.name, "Effect Volume") == 0 ||
  851. strcmp(kctl->id.name, "Effect Feedback Volume") == 0) {
  852. usb_audio_info(chip,
  853. "set quirks for FTU Effect Feedback/Volume\n");
  854. cval->min = 0x00;
  855. cval->max = 0x7f;
  856. break;
  857. }
  858. break;
  859. case USB_ID(0x0471, 0x0101):
  860. case USB_ID(0x0471, 0x0104):
  861. case USB_ID(0x0471, 0x0105):
  862. case USB_ID(0x0672, 0x1041):
  863. /* quirk for UDA1321/N101.
  864. * note that detection between firmware 2.1.1.7 (N101)
  865. * and later 2.1.1.21 is not very clear from datasheets.
  866. * I hope that the min value is -15360 for newer firmware --jk
  867. */
  868. if (!strcmp(kctl->id.name, "PCM Playback Volume") &&
  869. cval->min == -15616) {
  870. usb_audio_info(chip,
  871. "set volume quirk for UDA1321/N101 chip\n");
  872. cval->max = -256;
  873. }
  874. break;
  875. case USB_ID(0x046d, 0x09a4):
  876. if (!strcmp(kctl->id.name, "Mic Capture Volume")) {
  877. usb_audio_info(chip,
  878. "set volume quirk for QuickCam E3500\n");
  879. cval->min = 6080;
  880. cval->max = 8768;
  881. cval->res = 192;
  882. }
  883. break;
  884. case USB_ID(0x046d, 0x0807): /* Logitech Webcam C500 */
  885. case USB_ID(0x046d, 0x0808):
  886. case USB_ID(0x046d, 0x0809):
  887. case USB_ID(0x046d, 0x0819): /* Logitech Webcam C210 */
  888. case USB_ID(0x046d, 0x081b): /* HD Webcam c310 */
  889. case USB_ID(0x046d, 0x081d): /* HD Webcam c510 */
  890. case USB_ID(0x046d, 0x0825): /* HD Webcam c270 */
  891. case USB_ID(0x046d, 0x0826): /* HD Webcam c525 */
  892. case USB_ID(0x046d, 0x08ca): /* Logitech Quickcam Fusion */
  893. case USB_ID(0x046d, 0x0991):
  894. case USB_ID(0x046d, 0x09a2): /* QuickCam Communicate Deluxe/S7500 */
  895. /* Most audio usb devices lie about volume resolution.
  896. * Most Logitech webcams have res = 384.
  897. * Probably there is some logitech magic behind this number --fishor
  898. */
  899. if (!strcmp(kctl->id.name, "Mic Capture Volume")) {
  900. usb_audio_info(chip,
  901. "set resolution quirk: cval->res = 384\n");
  902. cval->res = 384;
  903. }
  904. break;
  905. }
  906. }
  907. /*
  908. * retrieve the minimum and maximum values for the specified control
  909. */
  910. static int get_min_max_with_quirks(struct usb_mixer_elem_info *cval,
  911. int default_min, struct snd_kcontrol *kctl)
  912. {
  913. /* for failsafe */
  914. cval->min = default_min;
  915. cval->max = cval->min + 1;
  916. cval->res = 1;
  917. cval->dBmin = cval->dBmax = 0;
  918. if (cval->val_type == USB_MIXER_BOOLEAN ||
  919. cval->val_type == USB_MIXER_INV_BOOLEAN) {
  920. cval->initialized = 1;
  921. } else {
  922. int minchn = 0;
  923. if (cval->cmask) {
  924. int i;
  925. for (i = 0; i < MAX_CHANNELS; i++)
  926. if (cval->cmask & (1 << i)) {
  927. minchn = i + 1;
  928. break;
  929. }
  930. }
  931. if (get_ctl_value(cval, UAC_GET_MAX, (cval->control << 8) | minchn, &cval->max) < 0 ||
  932. get_ctl_value(cval, UAC_GET_MIN, (cval->control << 8) | minchn, &cval->min) < 0) {
  933. usb_audio_err(cval->head.mixer->chip,
  934. "%d:%d: cannot get min/max values for control %d (id %d)\n",
  935. cval->head.id, snd_usb_ctrl_intf(cval->head.mixer->chip),
  936. cval->control, cval->head.id);
  937. return -EINVAL;
  938. }
  939. if (get_ctl_value(cval, UAC_GET_RES,
  940. (cval->control << 8) | minchn,
  941. &cval->res) < 0) {
  942. cval->res = 1;
  943. } else {
  944. int last_valid_res = cval->res;
  945. while (cval->res > 1) {
  946. if (snd_usb_mixer_set_ctl_value(cval, UAC_SET_RES,
  947. (cval->control << 8) | minchn,
  948. cval->res / 2) < 0)
  949. break;
  950. cval->res /= 2;
  951. }
  952. if (get_ctl_value(cval, UAC_GET_RES,
  953. (cval->control << 8) | minchn, &cval->res) < 0)
  954. cval->res = last_valid_res;
  955. }
  956. if (cval->res == 0)
  957. cval->res = 1;
  958. /* Additional checks for the proper resolution
  959. *
  960. * Some devices report smaller resolutions than actually
  961. * reacting. They don't return errors but simply clip
  962. * to the lower aligned value.
  963. */
  964. if (cval->min + cval->res < cval->max) {
  965. int last_valid_res = cval->res;
  966. int saved, test, check;
  967. get_cur_mix_raw(cval, minchn, &saved);
  968. for (;;) {
  969. test = saved;
  970. if (test < cval->max)
  971. test += cval->res;
  972. else
  973. test -= cval->res;
  974. if (test < cval->min || test > cval->max ||
  975. snd_usb_set_cur_mix_value(cval, minchn, 0, test) ||
  976. get_cur_mix_raw(cval, minchn, &check)) {
  977. cval->res = last_valid_res;
  978. break;
  979. }
  980. if (test == check)
  981. break;
  982. cval->res *= 2;
  983. }
  984. snd_usb_set_cur_mix_value(cval, minchn, 0, saved);
  985. }
  986. cval->initialized = 1;
  987. }
  988. if (kctl)
  989. volume_control_quirks(cval, kctl);
  990. /* USB descriptions contain the dB scale in 1/256 dB unit
  991. * while ALSA TLV contains in 1/100 dB unit
  992. */
  993. cval->dBmin = (convert_signed_value(cval, cval->min) * 100) / 256;
  994. cval->dBmax = (convert_signed_value(cval, cval->max) * 100) / 256;
  995. if (cval->dBmin > cval->dBmax) {
  996. /* something is wrong; assume it's either from/to 0dB */
  997. if (cval->dBmin < 0)
  998. cval->dBmax = 0;
  999. else if (cval->dBmin > 0)
  1000. cval->dBmin = 0;
  1001. if (cval->dBmin > cval->dBmax) {
  1002. /* totally crap, return an error */
  1003. return -EINVAL;
  1004. }
  1005. }
  1006. return 0;
  1007. }
  1008. #define get_min_max(cval, def) get_min_max_with_quirks(cval, def, NULL)
  1009. /* get a feature/mixer unit info */
  1010. static int mixer_ctl_feature_info(struct snd_kcontrol *kcontrol,
  1011. struct snd_ctl_elem_info *uinfo)
  1012. {
  1013. struct usb_mixer_elem_info *cval = kcontrol->private_data;
  1014. if (cval->val_type == USB_MIXER_BOOLEAN ||
  1015. cval->val_type == USB_MIXER_INV_BOOLEAN)
  1016. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1017. else
  1018. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1019. uinfo->count = cval->channels;
  1020. if (cval->val_type == USB_MIXER_BOOLEAN ||
  1021. cval->val_type == USB_MIXER_INV_BOOLEAN) {
  1022. uinfo->value.integer.min = 0;
  1023. uinfo->value.integer.max = 1;
  1024. } else {
  1025. if (!cval->initialized) {
  1026. get_min_max_with_quirks(cval, 0, kcontrol);
  1027. if (cval->initialized && cval->dBmin >= cval->dBmax) {
  1028. kcontrol->vd[0].access &=
  1029. ~(SNDRV_CTL_ELEM_ACCESS_TLV_READ |
  1030. SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK);
  1031. snd_ctl_notify(cval->head.mixer->chip->card,
  1032. SNDRV_CTL_EVENT_MASK_INFO,
  1033. &kcontrol->id);
  1034. }
  1035. }
  1036. uinfo->value.integer.min = 0;
  1037. uinfo->value.integer.max =
  1038. (cval->max - cval->min + cval->res - 1) / cval->res;
  1039. }
  1040. return 0;
  1041. }
  1042. /* get the current value from feature/mixer unit */
  1043. static int mixer_ctl_feature_get(struct snd_kcontrol *kcontrol,
  1044. struct snd_ctl_elem_value *ucontrol)
  1045. {
  1046. struct usb_mixer_elem_info *cval = kcontrol->private_data;
  1047. int c, cnt, val, err;
  1048. ucontrol->value.integer.value[0] = cval->min;
  1049. if (cval->cmask) {
  1050. cnt = 0;
  1051. for (c = 0; c < MAX_CHANNELS; c++) {
  1052. if (!(cval->cmask & (1 << c)))
  1053. continue;
  1054. err = snd_usb_get_cur_mix_value(cval, c + 1, cnt, &val);
  1055. if (err < 0)
  1056. return filter_error(cval, err);
  1057. val = get_relative_value(cval, val);
  1058. ucontrol->value.integer.value[cnt] = val;
  1059. cnt++;
  1060. }
  1061. return 0;
  1062. } else {
  1063. /* master channel */
  1064. err = snd_usb_get_cur_mix_value(cval, 0, 0, &val);
  1065. if (err < 0)
  1066. return filter_error(cval, err);
  1067. val = get_relative_value(cval, val);
  1068. ucontrol->value.integer.value[0] = val;
  1069. }
  1070. return 0;
  1071. }
  1072. /* put the current value to feature/mixer unit */
  1073. static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol,
  1074. struct snd_ctl_elem_value *ucontrol)
  1075. {
  1076. struct usb_mixer_elem_info *cval = kcontrol->private_data;
  1077. int c, cnt, val, oval, err;
  1078. int changed = 0;
  1079. if (cval->cmask) {
  1080. cnt = 0;
  1081. for (c = 0; c < MAX_CHANNELS; c++) {
  1082. if (!(cval->cmask & (1 << c)))
  1083. continue;
  1084. err = snd_usb_get_cur_mix_value(cval, c + 1, cnt, &oval);
  1085. if (err < 0)
  1086. return filter_error(cval, err);
  1087. val = ucontrol->value.integer.value[cnt];
  1088. val = get_abs_value(cval, val);
  1089. if (oval != val) {
  1090. snd_usb_set_cur_mix_value(cval, c + 1, cnt, val);
  1091. changed = 1;
  1092. }
  1093. cnt++;
  1094. }
  1095. } else {
  1096. /* master channel */
  1097. err = snd_usb_get_cur_mix_value(cval, 0, 0, &oval);
  1098. if (err < 0)
  1099. return filter_error(cval, err);
  1100. val = ucontrol->value.integer.value[0];
  1101. val = get_abs_value(cval, val);
  1102. if (val != oval) {
  1103. snd_usb_set_cur_mix_value(cval, 0, 0, val);
  1104. changed = 1;
  1105. }
  1106. }
  1107. return changed;
  1108. }
  1109. /* get the boolean value from the master channel of a UAC control */
  1110. static int mixer_ctl_master_bool_get(struct snd_kcontrol *kcontrol,
  1111. struct snd_ctl_elem_value *ucontrol)
  1112. {
  1113. struct usb_mixer_elem_info *cval = kcontrol->private_data;
  1114. int val, err;
  1115. err = snd_usb_get_cur_mix_value(cval, 0, 0, &val);
  1116. if (err < 0)
  1117. return filter_error(cval, err);
  1118. val = (val != 0);
  1119. ucontrol->value.integer.value[0] = val;
  1120. return 0;
  1121. }
  1122. static struct snd_kcontrol_new usb_feature_unit_ctl = {
  1123. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1124. .name = "", /* will be filled later manually */
  1125. .info = mixer_ctl_feature_info,
  1126. .get = mixer_ctl_feature_get,
  1127. .put = mixer_ctl_feature_put,
  1128. };
  1129. /* the read-only variant */
  1130. static const struct snd_kcontrol_new usb_feature_unit_ctl_ro = {
  1131. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1132. .name = "", /* will be filled later manually */
  1133. .info = mixer_ctl_feature_info,
  1134. .get = mixer_ctl_feature_get,
  1135. .put = NULL,
  1136. };
  1137. /*
  1138. * A control which shows the boolean value from reading a UAC control on
  1139. * the master channel.
  1140. */
  1141. static struct snd_kcontrol_new usb_bool_master_control_ctl_ro = {
  1142. .iface = SNDRV_CTL_ELEM_IFACE_CARD,
  1143. .name = "", /* will be filled later manually */
  1144. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  1145. .info = snd_ctl_boolean_mono_info,
  1146. .get = mixer_ctl_master_bool_get,
  1147. .put = NULL,
  1148. };
  1149. /*
  1150. * This symbol is exported in order to allow the mixer quirks to
  1151. * hook up to the standard feature unit control mechanism
  1152. */
  1153. struct snd_kcontrol_new *snd_usb_feature_unit_ctl = &usb_feature_unit_ctl;
  1154. /*
  1155. * build a feature control
  1156. */
  1157. static size_t append_ctl_name(struct snd_kcontrol *kctl, const char *str)
  1158. {
  1159. return strlcat(kctl->id.name, str, sizeof(kctl->id.name));
  1160. }
  1161. /*
  1162. * A lot of headsets/headphones have a "Speaker" mixer. Make sure we
  1163. * rename it to "Headphone". We determine if something is a headphone
  1164. * similar to how udev determines form factor.
  1165. */
  1166. static void check_no_speaker_on_headset(struct snd_kcontrol *kctl,
  1167. struct snd_card *card)
  1168. {
  1169. const char *names_to_check[] = {
  1170. "Headset", "headset", "Headphone", "headphone", NULL};
  1171. const char **s;
  1172. bool found = false;
  1173. if (strcmp("Speaker", kctl->id.name))
  1174. return;
  1175. for (s = names_to_check; *s; s++)
  1176. if (strstr(card->shortname, *s)) {
  1177. found = true;
  1178. break;
  1179. }
  1180. if (!found)
  1181. return;
  1182. strlcpy(kctl->id.name, "Headphone", sizeof(kctl->id.name));
  1183. }
  1184. static struct usb_feature_control_info *get_feature_control_info(int control)
  1185. {
  1186. int i;
  1187. for (i = 0; i < ARRAY_SIZE(audio_feature_info); ++i) {
  1188. if (audio_feature_info[i].control == control)
  1189. return &audio_feature_info[i];
  1190. }
  1191. return NULL;
  1192. }
  1193. static void build_feature_ctl(struct mixer_build *state, void *raw_desc,
  1194. unsigned int ctl_mask, int control,
  1195. struct usb_audio_term *iterm, int unitid,
  1196. int readonly_mask)
  1197. {
  1198. struct uac_feature_unit_descriptor *desc = raw_desc;
  1199. struct usb_feature_control_info *ctl_info;
  1200. unsigned int len = 0;
  1201. int mapped_name = 0;
  1202. int nameid = uac_feature_unit_iFeature(desc);
  1203. struct snd_kcontrol *kctl;
  1204. struct usb_mixer_elem_info *cval;
  1205. const struct usbmix_name_map *map;
  1206. unsigned int range;
  1207. if (control == UAC_FU_GRAPHIC_EQUALIZER) {
  1208. /* FIXME: not supported yet */
  1209. return;
  1210. }
  1211. map = find_map(state, unitid, control);
  1212. if (check_ignored_ctl(map))
  1213. return;
  1214. cval = kzalloc(sizeof(*cval), GFP_KERNEL);
  1215. if (!cval)
  1216. return;
  1217. snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid);
  1218. cval->control = control;
  1219. cval->cmask = ctl_mask;
  1220. ctl_info = get_feature_control_info(control);
  1221. if (!ctl_info) {
  1222. kfree(cval);
  1223. return;
  1224. }
  1225. if (state->mixer->protocol == UAC_VERSION_1)
  1226. cval->val_type = ctl_info->type;
  1227. else /* UAC_VERSION_2 */
  1228. cval->val_type = ctl_info->type_uac2 >= 0 ?
  1229. ctl_info->type_uac2 : ctl_info->type;
  1230. if (ctl_mask == 0) {
  1231. cval->channels = 1; /* master channel */
  1232. cval->master_readonly = readonly_mask;
  1233. } else {
  1234. int i, c = 0;
  1235. for (i = 0; i < 16; i++)
  1236. if (ctl_mask & (1 << i))
  1237. c++;
  1238. cval->channels = c;
  1239. cval->ch_readonly = readonly_mask;
  1240. }
  1241. /*
  1242. * If all channels in the mask are marked read-only, make the control
  1243. * read-only. snd_usb_set_cur_mix_value() will check the mask again and won't
  1244. * issue write commands to read-only channels.
  1245. */
  1246. if (cval->channels == readonly_mask)
  1247. kctl = snd_ctl_new1(&usb_feature_unit_ctl_ro, cval);
  1248. else
  1249. kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
  1250. if (!kctl) {
  1251. usb_audio_err(state->chip, "cannot malloc kcontrol\n");
  1252. kfree(cval);
  1253. return;
  1254. }
  1255. kctl->private_free = snd_usb_mixer_elem_free;
  1256. len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
  1257. mapped_name = len != 0;
  1258. if (!len && nameid)
  1259. len = snd_usb_copy_string_desc(state, nameid,
  1260. kctl->id.name, sizeof(kctl->id.name));
  1261. switch (control) {
  1262. case UAC_FU_MUTE:
  1263. case UAC_FU_VOLUME:
  1264. /*
  1265. * determine the control name. the rule is:
  1266. * - if a name id is given in descriptor, use it.
  1267. * - if the connected input can be determined, then use the name
  1268. * of terminal type.
  1269. * - if the connected output can be determined, use it.
  1270. * - otherwise, anonymous name.
  1271. */
  1272. if (!len) {
  1273. len = get_term_name(state, iterm, kctl->id.name,
  1274. sizeof(kctl->id.name), 1);
  1275. if (!len)
  1276. len = get_term_name(state, &state->oterm,
  1277. kctl->id.name,
  1278. sizeof(kctl->id.name), 1);
  1279. if (!len)
  1280. snprintf(kctl->id.name, sizeof(kctl->id.name),
  1281. "Feature %d", unitid);
  1282. }
  1283. if (!mapped_name)
  1284. check_no_speaker_on_headset(kctl, state->mixer->chip->card);
  1285. /*
  1286. * determine the stream direction:
  1287. * if the connected output is USB stream, then it's likely a
  1288. * capture stream. otherwise it should be playback (hopefully :)
  1289. */
  1290. if (!mapped_name && !(state->oterm.type >> 16)) {
  1291. if ((state->oterm.type & 0xff00) == 0x0100)
  1292. append_ctl_name(kctl, " Capture");
  1293. else
  1294. append_ctl_name(kctl, " Playback");
  1295. }
  1296. append_ctl_name(kctl, control == UAC_FU_MUTE ?
  1297. " Switch" : " Volume");
  1298. break;
  1299. default:
  1300. if (!len)
  1301. strlcpy(kctl->id.name, audio_feature_info[control-1].name,
  1302. sizeof(kctl->id.name));
  1303. break;
  1304. }
  1305. /* get min/max values */
  1306. get_min_max_with_quirks(cval, 0, kctl);
  1307. if (control == UAC_FU_VOLUME) {
  1308. check_mapped_dB(map, cval);
  1309. if (cval->dBmin < cval->dBmax || !cval->initialized) {
  1310. kctl->tlv.c = snd_usb_mixer_vol_tlv;
  1311. kctl->vd[0].access |=
  1312. SNDRV_CTL_ELEM_ACCESS_TLV_READ |
  1313. SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
  1314. }
  1315. }
  1316. snd_usb_mixer_fu_apply_quirk(state->mixer, cval, unitid, kctl);
  1317. range = (cval->max - cval->min) / cval->res;
  1318. /*
  1319. * Are there devices with volume range more than 255? I use a bit more
  1320. * to be sure. 384 is a resolution magic number found on Logitech
  1321. * devices. It will definitively catch all buggy Logitech devices.
  1322. */
  1323. if (range > 384) {
  1324. usb_audio_warn(state->chip,
  1325. "Warning! Unlikely big volume range (=%u), cval->res is probably wrong.",
  1326. range);
  1327. usb_audio_warn(state->chip,
  1328. "[%d] FU [%s] ch = %d, val = %d/%d/%d",
  1329. cval->head.id, kctl->id.name, cval->channels,
  1330. cval->min, cval->max, cval->res);
  1331. }
  1332. usb_audio_dbg(state->chip, "[%d] FU [%s] ch = %d, val = %d/%d/%d\n",
  1333. cval->head.id, kctl->id.name, cval->channels,
  1334. cval->min, cval->max, cval->res);
  1335. snd_usb_mixer_add_control(&cval->head, kctl);
  1336. }
  1337. static void get_connector_control_name(struct mixer_build *state,
  1338. struct usb_audio_term *term,
  1339. bool is_input, char *name, int name_size)
  1340. {
  1341. int name_len = get_term_name(state, term, name, name_size, 0);
  1342. if (name_len == 0)
  1343. strlcpy(name, "Unknown", name_size);
  1344. /*
  1345. * sound/core/ctljack.c has a convention of naming jack controls
  1346. * by ending in " Jack". Make it slightly more useful by
  1347. * indicating Input or Output after the terminal name.
  1348. */
  1349. if (is_input)
  1350. strlcat(name, " - Input Jack", name_size);
  1351. else
  1352. strlcat(name, " - Output Jack", name_size);
  1353. }
  1354. /* Build a mixer control for a UAC connector control (jack-detect) */
  1355. static void build_connector_control(struct mixer_build *state,
  1356. struct usb_audio_term *term, bool is_input)
  1357. {
  1358. struct snd_kcontrol *kctl;
  1359. struct usb_mixer_elem_info *cval;
  1360. cval = kzalloc(sizeof(*cval), GFP_KERNEL);
  1361. if (!cval)
  1362. return;
  1363. snd_usb_mixer_elem_init_std(&cval->head, state->mixer, term->id);
  1364. /*
  1365. * The first byte from reading the UAC2_TE_CONNECTOR control returns the
  1366. * number of channels connected. This boolean ctl will simply report
  1367. * if any channels are connected or not.
  1368. * (Audio20_final.pdf Table 5-10: Connector Control CUR Parameter Block)
  1369. */
  1370. cval->control = UAC2_TE_CONNECTOR;
  1371. cval->val_type = USB_MIXER_BOOLEAN;
  1372. cval->channels = 1; /* report true if any channel is connected */
  1373. cval->min = 0;
  1374. cval->max = 1;
  1375. kctl = snd_ctl_new1(&usb_bool_master_control_ctl_ro, cval);
  1376. if (!kctl) {
  1377. usb_audio_err(state->chip, "cannot malloc kcontrol\n");
  1378. kfree(cval);
  1379. return;
  1380. }
  1381. get_connector_control_name(state, term, is_input, kctl->id.name,
  1382. sizeof(kctl->id.name));
  1383. kctl->private_free = snd_usb_mixer_elem_free;
  1384. snd_usb_mixer_add_control(&cval->head, kctl);
  1385. }
  1386. static int parse_clock_source_unit(struct mixer_build *state, int unitid,
  1387. void *_ftr)
  1388. {
  1389. struct uac_clock_source_descriptor *hdr = _ftr;
  1390. struct usb_mixer_elem_info *cval;
  1391. struct snd_kcontrol *kctl;
  1392. char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
  1393. int ret;
  1394. if (state->mixer->protocol != UAC_VERSION_2)
  1395. return -EINVAL;
  1396. if (hdr->bLength != sizeof(*hdr)) {
  1397. usb_audio_dbg(state->chip,
  1398. "Bogus clock source descriptor length of %d, ignoring.\n",
  1399. hdr->bLength);
  1400. return 0;
  1401. }
  1402. /*
  1403. * The only property of this unit we are interested in is the
  1404. * clock source validity. If that isn't readable, just bail out.
  1405. */
  1406. if (!uac_v2v3_control_is_readable(hdr->bmControls,
  1407. UAC2_CS_CONTROL_CLOCK_VALID))
  1408. return 0;
  1409. cval = kzalloc(sizeof(*cval), GFP_KERNEL);
  1410. if (!cval)
  1411. return -ENOMEM;
  1412. snd_usb_mixer_elem_init_std(&cval->head, state->mixer, hdr->bClockID);
  1413. cval->min = 0;
  1414. cval->max = 1;
  1415. cval->channels = 1;
  1416. cval->val_type = USB_MIXER_BOOLEAN;
  1417. cval->control = UAC2_CS_CONTROL_CLOCK_VALID;
  1418. cval->master_readonly = 1;
  1419. /* From UAC2 5.2.5.1.2 "Only the get request is supported." */
  1420. kctl = snd_ctl_new1(&usb_bool_master_control_ctl_ro, cval);
  1421. if (!kctl) {
  1422. kfree(cval);
  1423. return -ENOMEM;
  1424. }
  1425. kctl->private_free = snd_usb_mixer_elem_free;
  1426. ret = snd_usb_copy_string_desc(state, hdr->iClockSource,
  1427. name, sizeof(name));
  1428. if (ret > 0)
  1429. snprintf(kctl->id.name, sizeof(kctl->id.name),
  1430. "%s Validity", name);
  1431. else
  1432. snprintf(kctl->id.name, sizeof(kctl->id.name),
  1433. "Clock Source %d Validity", hdr->bClockID);
  1434. return snd_usb_mixer_add_control(&cval->head, kctl);
  1435. }
  1436. /*
  1437. * parse a feature unit
  1438. *
  1439. * most of controls are defined here.
  1440. */
  1441. static int parse_audio_feature_unit(struct mixer_build *state, int unitid,
  1442. void *_ftr)
  1443. {
  1444. int channels, i, j;
  1445. struct usb_audio_term iterm;
  1446. unsigned int master_bits, first_ch_bits;
  1447. int err, csize;
  1448. struct uac_feature_unit_descriptor *hdr = _ftr;
  1449. __u8 *bmaControls;
  1450. if (state->mixer->protocol == UAC_VERSION_1) {
  1451. if (hdr->bLength < 7) {
  1452. usb_audio_err(state->chip,
  1453. "unit %u: invalid UAC_FEATURE_UNIT descriptor\n",
  1454. unitid);
  1455. return -EINVAL;
  1456. }
  1457. csize = hdr->bControlSize;
  1458. if (!csize) {
  1459. usb_audio_dbg(state->chip,
  1460. "unit %u: invalid bControlSize == 0\n",
  1461. unitid);
  1462. return -EINVAL;
  1463. }
  1464. channels = (hdr->bLength - 7) / csize - 1;
  1465. bmaControls = hdr->bmaControls;
  1466. if (hdr->bLength < 7 + csize) {
  1467. usb_audio_err(state->chip,
  1468. "unit %u: invalid UAC_FEATURE_UNIT descriptor\n",
  1469. unitid);
  1470. return -EINVAL;
  1471. }
  1472. } else if (state->mixer->protocol == UAC_VERSION_2) {
  1473. struct uac2_feature_unit_descriptor *ftr = _ftr;
  1474. if (hdr->bLength < 6) {
  1475. usb_audio_err(state->chip,
  1476. "unit %u: invalid UAC_FEATURE_UNIT descriptor\n",
  1477. unitid);
  1478. return -EINVAL;
  1479. }
  1480. csize = 4;
  1481. channels = (hdr->bLength - 6) / 4 - 1;
  1482. bmaControls = ftr->bmaControls;
  1483. if (hdr->bLength < 6 + csize) {
  1484. usb_audio_err(state->chip,
  1485. "unit %u: invalid UAC_FEATURE_UNIT descriptor\n",
  1486. unitid);
  1487. return -EINVAL;
  1488. }
  1489. } else { /* UAC_VERSION_3 */
  1490. struct uac3_feature_unit_descriptor *ftr = _ftr;
  1491. if (hdr->bLength < 7) {
  1492. usb_audio_err(state->chip,
  1493. "unit %u: invalid UAC3_FEATURE_UNIT descriptor\n",
  1494. unitid);
  1495. return -EINVAL;
  1496. }
  1497. csize = 4;
  1498. channels = (ftr->bLength - 7) / 4 - 1;
  1499. bmaControls = ftr->bmaControls;
  1500. if (hdr->bLength < 7 + csize) {
  1501. usb_audio_err(state->chip,
  1502. "unit %u: invalid UAC3_FEATURE_UNIT descriptor\n",
  1503. unitid);
  1504. return -EINVAL;
  1505. }
  1506. }
  1507. /* parse the source unit */
  1508. if ((err = parse_audio_unit(state, hdr->bSourceID)) < 0)
  1509. return err;
  1510. /* determine the input source type and name */
  1511. err = check_input_term(state, hdr->bSourceID, &iterm);
  1512. if (err < 0)
  1513. return err;
  1514. master_bits = snd_usb_combine_bytes(bmaControls, csize);
  1515. /* master configuration quirks */
  1516. switch (state->chip->usb_id) {
  1517. case USB_ID(0x08bb, 0x2702):
  1518. usb_audio_info(state->chip,
  1519. "usbmixer: master volume quirk for PCM2702 chip\n");
  1520. /* disable non-functional volume control */
  1521. master_bits &= ~UAC_CONTROL_BIT(UAC_FU_VOLUME);
  1522. break;
  1523. case USB_ID(0x1130, 0xf211):
  1524. usb_audio_info(state->chip,
  1525. "usbmixer: volume control quirk for Tenx TP6911 Audio Headset\n");
  1526. /* disable non-functional volume control */
  1527. channels = 0;
  1528. break;
  1529. }
  1530. if (channels > 0)
  1531. first_ch_bits = snd_usb_combine_bytes(bmaControls + csize, csize);
  1532. else
  1533. first_ch_bits = 0;
  1534. if (state->mixer->protocol == UAC_VERSION_1) {
  1535. /* check all control types */
  1536. for (i = 0; i < 10; i++) {
  1537. unsigned int ch_bits = 0;
  1538. int control = audio_feature_info[i].control;
  1539. for (j = 0; j < channels; j++) {
  1540. unsigned int mask;
  1541. mask = snd_usb_combine_bytes(bmaControls +
  1542. csize * (j+1), csize);
  1543. if (mask & (1 << i))
  1544. ch_bits |= (1 << j);
  1545. }
  1546. /* audio class v1 controls are never read-only */
  1547. /*
  1548. * The first channel must be set
  1549. * (for ease of programming).
  1550. */
  1551. if (ch_bits & 1)
  1552. build_feature_ctl(state, _ftr, ch_bits, control,
  1553. &iterm, unitid, 0);
  1554. if (master_bits & (1 << i))
  1555. build_feature_ctl(state, _ftr, 0, control,
  1556. &iterm, unitid, 0);
  1557. }
  1558. } else { /* UAC_VERSION_2/3 */
  1559. for (i = 0; i < ARRAY_SIZE(audio_feature_info); i++) {
  1560. unsigned int ch_bits = 0;
  1561. unsigned int ch_read_only = 0;
  1562. int control = audio_feature_info[i].control;
  1563. for (j = 0; j < channels; j++) {
  1564. unsigned int mask;
  1565. mask = snd_usb_combine_bytes(bmaControls +
  1566. csize * (j+1), csize);
  1567. if (uac_v2v3_control_is_readable(mask, control)) {
  1568. ch_bits |= (1 << j);
  1569. if (!uac_v2v3_control_is_writeable(mask, control))
  1570. ch_read_only |= (1 << j);
  1571. }
  1572. }
  1573. /*
  1574. * NOTE: build_feature_ctl() will mark the control
  1575. * read-only if all channels are marked read-only in
  1576. * the descriptors. Otherwise, the control will be
  1577. * reported as writeable, but the driver will not
  1578. * actually issue a write command for read-only
  1579. * channels.
  1580. */
  1581. /*
  1582. * The first channel must be set
  1583. * (for ease of programming).
  1584. */
  1585. if (ch_bits & 1)
  1586. build_feature_ctl(state, _ftr, ch_bits, control,
  1587. &iterm, unitid, ch_read_only);
  1588. if (uac_v2v3_control_is_readable(master_bits, control))
  1589. build_feature_ctl(state, _ftr, 0, control,
  1590. &iterm, unitid,
  1591. !uac_v2v3_control_is_writeable(master_bits,
  1592. control));
  1593. }
  1594. }
  1595. return 0;
  1596. }
  1597. /*
  1598. * Mixer Unit
  1599. */
  1600. /*
  1601. * build a mixer unit control
  1602. *
  1603. * the callbacks are identical with feature unit.
  1604. * input channel number (zero based) is given in control field instead.
  1605. */
  1606. static void build_mixer_unit_ctl(struct mixer_build *state,
  1607. struct uac_mixer_unit_descriptor *desc,
  1608. int in_pin, int in_ch, int unitid,
  1609. struct usb_audio_term *iterm)
  1610. {
  1611. struct usb_mixer_elem_info *cval;
  1612. unsigned int num_outs = uac_mixer_unit_bNrChannels(desc);
  1613. unsigned int i, len;
  1614. struct snd_kcontrol *kctl;
  1615. const struct usbmix_name_map *map;
  1616. map = find_map(state, unitid, 0);
  1617. if (check_ignored_ctl(map))
  1618. return;
  1619. cval = kzalloc(sizeof(*cval), GFP_KERNEL);
  1620. if (!cval)
  1621. return;
  1622. snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid);
  1623. cval->control = in_ch + 1; /* based on 1 */
  1624. cval->val_type = USB_MIXER_S16;
  1625. for (i = 0; i < num_outs; i++) {
  1626. __u8 *c = uac_mixer_unit_bmControls(desc, state->mixer->protocol);
  1627. if (check_matrix_bitmap(c, in_ch, i, num_outs)) {
  1628. cval->cmask |= (1 << i);
  1629. cval->channels++;
  1630. }
  1631. }
  1632. /* get min/max values */
  1633. get_min_max(cval, 0);
  1634. kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
  1635. if (!kctl) {
  1636. usb_audio_err(state->chip, "cannot malloc kcontrol\n");
  1637. kfree(cval);
  1638. return;
  1639. }
  1640. kctl->private_free = snd_usb_mixer_elem_free;
  1641. len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
  1642. if (!len)
  1643. len = get_term_name(state, iterm, kctl->id.name,
  1644. sizeof(kctl->id.name), 0);
  1645. if (!len)
  1646. len = sprintf(kctl->id.name, "Mixer Source %d", in_ch + 1);
  1647. append_ctl_name(kctl, " Volume");
  1648. usb_audio_dbg(state->chip, "[%d] MU [%s] ch = %d, val = %d/%d\n",
  1649. cval->head.id, kctl->id.name, cval->channels, cval->min, cval->max);
  1650. snd_usb_mixer_add_control(&cval->head, kctl);
  1651. }
  1652. static int parse_audio_input_terminal(struct mixer_build *state, int unitid,
  1653. void *raw_desc)
  1654. {
  1655. struct usb_audio_term iterm;
  1656. struct uac2_input_terminal_descriptor *d = raw_desc;
  1657. check_input_term(state, d->bTerminalID, &iterm);
  1658. if (state->mixer->protocol == UAC_VERSION_2) {
  1659. /* Check for jack detection. */
  1660. if (uac_v2v3_control_is_readable(le16_to_cpu(d->bmControls),
  1661. UAC2_TE_CONNECTOR)) {
  1662. build_connector_control(state, &iterm, true);
  1663. }
  1664. }
  1665. return 0;
  1666. }
  1667. /*
  1668. * parse a mixer unit
  1669. */
  1670. static int parse_audio_mixer_unit(struct mixer_build *state, int unitid,
  1671. void *raw_desc)
  1672. {
  1673. struct uac_mixer_unit_descriptor *desc = raw_desc;
  1674. struct usb_audio_term iterm;
  1675. int input_pins, num_ins, num_outs;
  1676. int pin, ich, err;
  1677. if (desc->bLength < 11 || !(input_pins = desc->bNrInPins) ||
  1678. !(num_outs = uac_mixer_unit_bNrChannels(desc))) {
  1679. usb_audio_err(state->chip,
  1680. "invalid MIXER UNIT descriptor %d\n",
  1681. unitid);
  1682. return -EINVAL;
  1683. }
  1684. num_ins = 0;
  1685. ich = 0;
  1686. for (pin = 0; pin < input_pins; pin++) {
  1687. err = parse_audio_unit(state, desc->baSourceID[pin]);
  1688. if (err < 0)
  1689. continue;
  1690. /* no bmControls field (e.g. Maya44) -> ignore */
  1691. if (desc->bLength <= 10 + input_pins)
  1692. continue;
  1693. err = check_input_term(state, desc->baSourceID[pin], &iterm);
  1694. if (err < 0)
  1695. return err;
  1696. num_ins += iterm.channels;
  1697. for (; ich < num_ins; ich++) {
  1698. int och, ich_has_controls = 0;
  1699. for (och = 0; och < num_outs; och++) {
  1700. __u8 *c = uac_mixer_unit_bmControls(desc,
  1701. state->mixer->protocol);
  1702. if (check_matrix_bitmap(c, ich, och, num_outs)) {
  1703. ich_has_controls = 1;
  1704. break;
  1705. }
  1706. }
  1707. if (ich_has_controls)
  1708. build_mixer_unit_ctl(state, desc, pin, ich,
  1709. unitid, &iterm);
  1710. }
  1711. }
  1712. return 0;
  1713. }
  1714. /*
  1715. * Processing Unit / Extension Unit
  1716. */
  1717. /* get callback for processing/extension unit */
  1718. static int mixer_ctl_procunit_get(struct snd_kcontrol *kcontrol,
  1719. struct snd_ctl_elem_value *ucontrol)
  1720. {
  1721. struct usb_mixer_elem_info *cval = kcontrol->private_data;
  1722. int err, val;
  1723. err = get_cur_ctl_value(cval, cval->control << 8, &val);
  1724. if (err < 0) {
  1725. ucontrol->value.integer.value[0] = cval->min;
  1726. return filter_error(cval, err);
  1727. }
  1728. val = get_relative_value(cval, val);
  1729. ucontrol->value.integer.value[0] = val;
  1730. return 0;
  1731. }
  1732. /* put callback for processing/extension unit */
  1733. static int mixer_ctl_procunit_put(struct snd_kcontrol *kcontrol,
  1734. struct snd_ctl_elem_value *ucontrol)
  1735. {
  1736. struct usb_mixer_elem_info *cval = kcontrol->private_data;
  1737. int val, oval, err;
  1738. err = get_cur_ctl_value(cval, cval->control << 8, &oval);
  1739. if (err < 0)
  1740. return filter_error(cval, err);
  1741. val = ucontrol->value.integer.value[0];
  1742. val = get_abs_value(cval, val);
  1743. if (val != oval) {
  1744. set_cur_ctl_value(cval, cval->control << 8, val);
  1745. return 1;
  1746. }
  1747. return 0;
  1748. }
  1749. /* alsa control interface for processing/extension unit */
  1750. static const struct snd_kcontrol_new mixer_procunit_ctl = {
  1751. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1752. .name = "", /* will be filled later */
  1753. .info = mixer_ctl_feature_info,
  1754. .get = mixer_ctl_procunit_get,
  1755. .put = mixer_ctl_procunit_put,
  1756. };
  1757. /*
  1758. * predefined data for processing units
  1759. */
  1760. struct procunit_value_info {
  1761. int control;
  1762. char *suffix;
  1763. int val_type;
  1764. int min_value;
  1765. };
  1766. struct procunit_info {
  1767. int type;
  1768. char *name;
  1769. struct procunit_value_info *values;
  1770. };
  1771. static struct procunit_value_info updown_proc_info[] = {
  1772. { UAC_UD_ENABLE, "Switch", USB_MIXER_BOOLEAN },
  1773. { UAC_UD_MODE_SELECT, "Mode Select", USB_MIXER_U8, 1 },
  1774. { 0 }
  1775. };
  1776. static struct procunit_value_info prologic_proc_info[] = {
  1777. { UAC_DP_ENABLE, "Switch", USB_MIXER_BOOLEAN },
  1778. { UAC_DP_MODE_SELECT, "Mode Select", USB_MIXER_U8, 1 },
  1779. { 0 }
  1780. };
  1781. static struct procunit_value_info threed_enh_proc_info[] = {
  1782. { UAC_3D_ENABLE, "Switch", USB_MIXER_BOOLEAN },
  1783. { UAC_3D_SPACE, "Spaciousness", USB_MIXER_U8 },
  1784. { 0 }
  1785. };
  1786. static struct procunit_value_info reverb_proc_info[] = {
  1787. { UAC_REVERB_ENABLE, "Switch", USB_MIXER_BOOLEAN },
  1788. { UAC_REVERB_LEVEL, "Level", USB_MIXER_U8 },
  1789. { UAC_REVERB_TIME, "Time", USB_MIXER_U16 },
  1790. { UAC_REVERB_FEEDBACK, "Feedback", USB_MIXER_U8 },
  1791. { 0 }
  1792. };
  1793. static struct procunit_value_info chorus_proc_info[] = {
  1794. { UAC_CHORUS_ENABLE, "Switch", USB_MIXER_BOOLEAN },
  1795. { UAC_CHORUS_LEVEL, "Level", USB_MIXER_U8 },
  1796. { UAC_CHORUS_RATE, "Rate", USB_MIXER_U16 },
  1797. { UAC_CHORUS_DEPTH, "Depth", USB_MIXER_U16 },
  1798. { 0 }
  1799. };
  1800. static struct procunit_value_info dcr_proc_info[] = {
  1801. { UAC_DCR_ENABLE, "Switch", USB_MIXER_BOOLEAN },
  1802. { UAC_DCR_RATE, "Ratio", USB_MIXER_U16 },
  1803. { UAC_DCR_MAXAMPL, "Max Amp", USB_MIXER_S16 },
  1804. { UAC_DCR_THRESHOLD, "Threshold", USB_MIXER_S16 },
  1805. { UAC_DCR_ATTACK_TIME, "Attack Time", USB_MIXER_U16 },
  1806. { UAC_DCR_RELEASE_TIME, "Release Time", USB_MIXER_U16 },
  1807. { 0 }
  1808. };
  1809. static struct procunit_info procunits[] = {
  1810. { UAC_PROCESS_UP_DOWNMIX, "Up Down", updown_proc_info },
  1811. { UAC_PROCESS_DOLBY_PROLOGIC, "Dolby Prologic", prologic_proc_info },
  1812. { UAC_PROCESS_STEREO_EXTENDER, "3D Stereo Extender", threed_enh_proc_info },
  1813. { UAC_PROCESS_REVERB, "Reverb", reverb_proc_info },
  1814. { UAC_PROCESS_CHORUS, "Chorus", chorus_proc_info },
  1815. { UAC_PROCESS_DYN_RANGE_COMP, "DCR", dcr_proc_info },
  1816. { 0 },
  1817. };
  1818. /*
  1819. * predefined data for extension units
  1820. */
  1821. static struct procunit_value_info clock_rate_xu_info[] = {
  1822. { USB_XU_CLOCK_RATE_SELECTOR, "Selector", USB_MIXER_U8, 0 },
  1823. { 0 }
  1824. };
  1825. static struct procunit_value_info clock_source_xu_info[] = {
  1826. { USB_XU_CLOCK_SOURCE_SELECTOR, "External", USB_MIXER_BOOLEAN },
  1827. { 0 }
  1828. };
  1829. static struct procunit_value_info spdif_format_xu_info[] = {
  1830. { USB_XU_DIGITAL_FORMAT_SELECTOR, "SPDIF/AC3", USB_MIXER_BOOLEAN },
  1831. { 0 }
  1832. };
  1833. static struct procunit_value_info soft_limit_xu_info[] = {
  1834. { USB_XU_SOFT_LIMIT_SELECTOR, " ", USB_MIXER_BOOLEAN },
  1835. { 0 }
  1836. };
  1837. static struct procunit_info extunits[] = {
  1838. { USB_XU_CLOCK_RATE, "Clock rate", clock_rate_xu_info },
  1839. { USB_XU_CLOCK_SOURCE, "DigitalIn CLK source", clock_source_xu_info },
  1840. { USB_XU_DIGITAL_IO_STATUS, "DigitalOut format:", spdif_format_xu_info },
  1841. { USB_XU_DEVICE_OPTIONS, "AnalogueIn Soft Limit", soft_limit_xu_info },
  1842. { 0 }
  1843. };
  1844. /*
  1845. * build a processing/extension unit
  1846. */
  1847. static int build_audio_procunit(struct mixer_build *state, int unitid,
  1848. void *raw_desc, struct procunit_info *list,
  1849. char *name)
  1850. {
  1851. struct uac_processing_unit_descriptor *desc = raw_desc;
  1852. int num_ins = desc->bNrInPins;
  1853. struct usb_mixer_elem_info *cval;
  1854. struct snd_kcontrol *kctl;
  1855. int i, err, nameid, type, len;
  1856. struct procunit_info *info;
  1857. struct procunit_value_info *valinfo;
  1858. const struct usbmix_name_map *map;
  1859. static struct procunit_value_info default_value_info[] = {
  1860. { 0x01, "Switch", USB_MIXER_BOOLEAN },
  1861. { 0 }
  1862. };
  1863. static struct procunit_info default_info = {
  1864. 0, NULL, default_value_info
  1865. };
  1866. if (desc->bLength < 13 || desc->bLength < 13 + num_ins ||
  1867. desc->bLength < num_ins + uac_processing_unit_bControlSize(desc, state->mixer->protocol)) {
  1868. usb_audio_err(state->chip, "invalid %s descriptor (id %d)\n", name, unitid);
  1869. return -EINVAL;
  1870. }
  1871. for (i = 0; i < num_ins; i++) {
  1872. if ((err = parse_audio_unit(state, desc->baSourceID[i])) < 0)
  1873. return err;
  1874. }
  1875. type = le16_to_cpu(desc->wProcessType);
  1876. for (info = list; info && info->type; info++)
  1877. if (info->type == type)
  1878. break;
  1879. if (!info || !info->type)
  1880. info = &default_info;
  1881. for (valinfo = info->values; valinfo->control; valinfo++) {
  1882. __u8 *controls = uac_processing_unit_bmControls(desc, state->mixer->protocol);
  1883. if (!(controls[valinfo->control / 8] & (1 << ((valinfo->control % 8) - 1))))
  1884. continue;
  1885. map = find_map(state, unitid, valinfo->control);
  1886. if (check_ignored_ctl(map))
  1887. continue;
  1888. cval = kzalloc(sizeof(*cval), GFP_KERNEL);
  1889. if (!cval)
  1890. return -ENOMEM;
  1891. snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid);
  1892. cval->control = valinfo->control;
  1893. cval->val_type = valinfo->val_type;
  1894. cval->channels = 1;
  1895. /* get min/max values */
  1896. if (type == UAC_PROCESS_UP_DOWNMIX && cval->control == UAC_UD_MODE_SELECT) {
  1897. __u8 *control_spec = uac_processing_unit_specific(desc, state->mixer->protocol);
  1898. /* FIXME: hard-coded */
  1899. cval->min = 1;
  1900. cval->max = control_spec[0];
  1901. cval->res = 1;
  1902. cval->initialized = 1;
  1903. } else {
  1904. if (type == USB_XU_CLOCK_RATE) {
  1905. /*
  1906. * E-Mu USB 0404/0202/TrackerPre/0204
  1907. * samplerate control quirk
  1908. */
  1909. cval->min = 0;
  1910. cval->max = 5;
  1911. cval->res = 1;
  1912. cval->initialized = 1;
  1913. } else
  1914. get_min_max(cval, valinfo->min_value);
  1915. }
  1916. kctl = snd_ctl_new1(&mixer_procunit_ctl, cval);
  1917. if (!kctl) {
  1918. kfree(cval);
  1919. return -ENOMEM;
  1920. }
  1921. kctl->private_free = snd_usb_mixer_elem_free;
  1922. if (check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name))) {
  1923. /* nothing */ ;
  1924. } else if (info->name) {
  1925. strlcpy(kctl->id.name, info->name, sizeof(kctl->id.name));
  1926. } else {
  1927. nameid = uac_processing_unit_iProcessing(desc, state->mixer->protocol);
  1928. len = 0;
  1929. if (nameid)
  1930. len = snd_usb_copy_string_desc(state, nameid,
  1931. kctl->id.name,
  1932. sizeof(kctl->id.name));
  1933. if (!len)
  1934. strlcpy(kctl->id.name, name, sizeof(kctl->id.name));
  1935. }
  1936. append_ctl_name(kctl, " ");
  1937. append_ctl_name(kctl, valinfo->suffix);
  1938. usb_audio_dbg(state->chip,
  1939. "[%d] PU [%s] ch = %d, val = %d/%d\n",
  1940. cval->head.id, kctl->id.name, cval->channels,
  1941. cval->min, cval->max);
  1942. err = snd_usb_mixer_add_control(&cval->head, kctl);
  1943. if (err < 0)
  1944. return err;
  1945. }
  1946. return 0;
  1947. }
  1948. static int parse_audio_processing_unit(struct mixer_build *state, int unitid,
  1949. void *raw_desc)
  1950. {
  1951. return build_audio_procunit(state, unitid, raw_desc,
  1952. procunits, "Processing Unit");
  1953. }
  1954. static int parse_audio_extension_unit(struct mixer_build *state, int unitid,
  1955. void *raw_desc)
  1956. {
  1957. /*
  1958. * Note that we parse extension units with processing unit descriptors.
  1959. * That's ok as the layout is the same.
  1960. */
  1961. return build_audio_procunit(state, unitid, raw_desc,
  1962. extunits, "Extension Unit");
  1963. }
  1964. /*
  1965. * Selector Unit
  1966. */
  1967. /*
  1968. * info callback for selector unit
  1969. * use an enumerator type for routing
  1970. */
  1971. static int mixer_ctl_selector_info(struct snd_kcontrol *kcontrol,
  1972. struct snd_ctl_elem_info *uinfo)
  1973. {
  1974. struct usb_mixer_elem_info *cval = kcontrol->private_data;
  1975. const char **itemlist = (const char **)kcontrol->private_value;
  1976. if (snd_BUG_ON(!itemlist))
  1977. return -EINVAL;
  1978. return snd_ctl_enum_info(uinfo, 1, cval->max, itemlist);
  1979. }
  1980. /* get callback for selector unit */
  1981. static int mixer_ctl_selector_get(struct snd_kcontrol *kcontrol,
  1982. struct snd_ctl_elem_value *ucontrol)
  1983. {
  1984. struct usb_mixer_elem_info *cval = kcontrol->private_data;
  1985. int val, err;
  1986. err = get_cur_ctl_value(cval, cval->control << 8, &val);
  1987. if (err < 0) {
  1988. ucontrol->value.enumerated.item[0] = 0;
  1989. return filter_error(cval, err);
  1990. }
  1991. val = get_relative_value(cval, val);
  1992. ucontrol->value.enumerated.item[0] = val;
  1993. return 0;
  1994. }
  1995. /* put callback for selector unit */
  1996. static int mixer_ctl_selector_put(struct snd_kcontrol *kcontrol,
  1997. struct snd_ctl_elem_value *ucontrol)
  1998. {
  1999. struct usb_mixer_elem_info *cval = kcontrol->private_data;
  2000. int val, oval, err;
  2001. err = get_cur_ctl_value(cval, cval->control << 8, &oval);
  2002. if (err < 0)
  2003. return filter_error(cval, err);
  2004. val = ucontrol->value.enumerated.item[0];
  2005. val = get_abs_value(cval, val);
  2006. if (val != oval) {
  2007. set_cur_ctl_value(cval, cval->control << 8, val);
  2008. return 1;
  2009. }
  2010. return 0;
  2011. }
  2012. /* alsa control interface for selector unit */
  2013. static const struct snd_kcontrol_new mixer_selectunit_ctl = {
  2014. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2015. .name = "", /* will be filled later */
  2016. .info = mixer_ctl_selector_info,
  2017. .get = mixer_ctl_selector_get,
  2018. .put = mixer_ctl_selector_put,
  2019. };
  2020. /*
  2021. * private free callback.
  2022. * free both private_data and private_value
  2023. */
  2024. static void usb_mixer_selector_elem_free(struct snd_kcontrol *kctl)
  2025. {
  2026. int i, num_ins = 0;
  2027. if (kctl->private_data) {
  2028. struct usb_mixer_elem_info *cval = kctl->private_data;
  2029. num_ins = cval->max;
  2030. kfree(cval);
  2031. kctl->private_data = NULL;
  2032. }
  2033. if (kctl->private_value) {
  2034. char **itemlist = (char **)kctl->private_value;
  2035. for (i = 0; i < num_ins; i++)
  2036. kfree(itemlist[i]);
  2037. kfree(itemlist);
  2038. kctl->private_value = 0;
  2039. }
  2040. }
  2041. /*
  2042. * parse a selector unit
  2043. */
  2044. static int parse_audio_selector_unit(struct mixer_build *state, int unitid,
  2045. void *raw_desc)
  2046. {
  2047. struct uac_selector_unit_descriptor *desc = raw_desc;
  2048. unsigned int i, nameid, len;
  2049. int err;
  2050. struct usb_mixer_elem_info *cval;
  2051. struct snd_kcontrol *kctl;
  2052. const struct usbmix_name_map *map;
  2053. char **namelist;
  2054. if (desc->bLength < 5 || !desc->bNrInPins ||
  2055. desc->bLength < 5 + desc->bNrInPins) {
  2056. usb_audio_err(state->chip,
  2057. "invalid SELECTOR UNIT descriptor %d\n", unitid);
  2058. return -EINVAL;
  2059. }
  2060. for (i = 0; i < desc->bNrInPins; i++) {
  2061. if ((err = parse_audio_unit(state, desc->baSourceID[i])) < 0)
  2062. return err;
  2063. }
  2064. if (desc->bNrInPins == 1) /* only one ? nonsense! */
  2065. return 0;
  2066. map = find_map(state, unitid, 0);
  2067. if (check_ignored_ctl(map))
  2068. return 0;
  2069. cval = kzalloc(sizeof(*cval), GFP_KERNEL);
  2070. if (!cval)
  2071. return -ENOMEM;
  2072. snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid);
  2073. cval->val_type = USB_MIXER_U8;
  2074. cval->channels = 1;
  2075. cval->min = 1;
  2076. cval->max = desc->bNrInPins;
  2077. cval->res = 1;
  2078. cval->initialized = 1;
  2079. if (state->mixer->protocol == UAC_VERSION_1)
  2080. cval->control = 0;
  2081. else /* UAC_VERSION_2 */
  2082. cval->control = (desc->bDescriptorSubtype == UAC2_CLOCK_SELECTOR) ?
  2083. UAC2_CX_CLOCK_SELECTOR : UAC2_SU_SELECTOR;
  2084. namelist = kmalloc(sizeof(char *) * desc->bNrInPins, GFP_KERNEL);
  2085. if (!namelist) {
  2086. kfree(cval);
  2087. return -ENOMEM;
  2088. }
  2089. #define MAX_ITEM_NAME_LEN 64
  2090. for (i = 0; i < desc->bNrInPins; i++) {
  2091. struct usb_audio_term iterm;
  2092. len = 0;
  2093. namelist[i] = kmalloc(MAX_ITEM_NAME_LEN, GFP_KERNEL);
  2094. if (!namelist[i]) {
  2095. while (i--)
  2096. kfree(namelist[i]);
  2097. kfree(namelist);
  2098. kfree(cval);
  2099. return -ENOMEM;
  2100. }
  2101. len = check_mapped_selector_name(state, unitid, i, namelist[i],
  2102. MAX_ITEM_NAME_LEN);
  2103. if (! len && check_input_term(state, desc->baSourceID[i], &iterm) >= 0)
  2104. len = get_term_name(state, &iterm, namelist[i], MAX_ITEM_NAME_LEN, 0);
  2105. if (! len)
  2106. sprintf(namelist[i], "Input %u", i);
  2107. }
  2108. kctl = snd_ctl_new1(&mixer_selectunit_ctl, cval);
  2109. if (! kctl) {
  2110. usb_audio_err(state->chip, "cannot malloc kcontrol\n");
  2111. kfree(namelist);
  2112. kfree(cval);
  2113. return -ENOMEM;
  2114. }
  2115. kctl->private_value = (unsigned long)namelist;
  2116. kctl->private_free = usb_mixer_selector_elem_free;
  2117. /* check the static mapping table at first */
  2118. len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
  2119. if (!len) {
  2120. /* no mapping ? */
  2121. /* if iSelector is given, use it */
  2122. nameid = uac_selector_unit_iSelector(desc);
  2123. if (nameid)
  2124. len = snd_usb_copy_string_desc(state, nameid,
  2125. kctl->id.name,
  2126. sizeof(kctl->id.name));
  2127. /* ... or pick up the terminal name at next */
  2128. if (!len)
  2129. len = get_term_name(state, &state->oterm,
  2130. kctl->id.name, sizeof(kctl->id.name), 0);
  2131. /* ... or use the fixed string "USB" as the last resort */
  2132. if (!len)
  2133. strlcpy(kctl->id.name, "USB", sizeof(kctl->id.name));
  2134. /* and add the proper suffix */
  2135. if (desc->bDescriptorSubtype == UAC2_CLOCK_SELECTOR)
  2136. append_ctl_name(kctl, " Clock Source");
  2137. else if ((state->oterm.type & 0xff00) == 0x0100)
  2138. append_ctl_name(kctl, " Capture Source");
  2139. else
  2140. append_ctl_name(kctl, " Playback Source");
  2141. }
  2142. usb_audio_dbg(state->chip, "[%d] SU [%s] items = %d\n",
  2143. cval->head.id, kctl->id.name, desc->bNrInPins);
  2144. return snd_usb_mixer_add_control(&cval->head, kctl);
  2145. }
  2146. /*
  2147. * parse an audio unit recursively
  2148. */
  2149. static int parse_audio_unit(struct mixer_build *state, int unitid)
  2150. {
  2151. unsigned char *p1;
  2152. int protocol = state->mixer->protocol;
  2153. if (test_and_set_bit(unitid, state->unitbitmap))
  2154. return 0; /* the unit already visited */
  2155. p1 = find_audio_control_unit(state, unitid);
  2156. if (!p1) {
  2157. usb_audio_err(state->chip, "unit %d not found!\n", unitid);
  2158. return -EINVAL;
  2159. }
  2160. if (protocol == UAC_VERSION_1 || protocol == UAC_VERSION_2) {
  2161. switch (p1[2]) {
  2162. case UAC_INPUT_TERMINAL:
  2163. return parse_audio_input_terminal(state, unitid, p1);
  2164. case UAC_MIXER_UNIT:
  2165. return parse_audio_mixer_unit(state, unitid, p1);
  2166. case UAC2_CLOCK_SOURCE:
  2167. return parse_clock_source_unit(state, unitid, p1);
  2168. case UAC_SELECTOR_UNIT:
  2169. case UAC2_CLOCK_SELECTOR:
  2170. return parse_audio_selector_unit(state, unitid, p1);
  2171. case UAC_FEATURE_UNIT:
  2172. return parse_audio_feature_unit(state, unitid, p1);
  2173. case UAC1_PROCESSING_UNIT:
  2174. /* UAC2_EFFECT_UNIT has the same value */
  2175. if (protocol == UAC_VERSION_1)
  2176. return parse_audio_processing_unit(state, unitid, p1);
  2177. else
  2178. return 0; /* FIXME - effect units not implemented yet */
  2179. case UAC1_EXTENSION_UNIT:
  2180. /* UAC2_PROCESSING_UNIT_V2 has the same value */
  2181. if (protocol == UAC_VERSION_1)
  2182. return parse_audio_extension_unit(state, unitid, p1);
  2183. else /* UAC_VERSION_2 */
  2184. return parse_audio_processing_unit(state, unitid, p1);
  2185. case UAC2_EXTENSION_UNIT_V2:
  2186. return parse_audio_extension_unit(state, unitid, p1);
  2187. default:
  2188. usb_audio_err(state->chip,
  2189. "unit %u: unexpected type 0x%02x\n", unitid, p1[2]);
  2190. return -EINVAL;
  2191. }
  2192. } else { /* UAC_VERSION_3 */
  2193. switch (p1[2]) {
  2194. case UAC_INPUT_TERMINAL:
  2195. return 0; /* NOP */
  2196. case UAC3_MIXER_UNIT:
  2197. return parse_audio_mixer_unit(state, unitid, p1);
  2198. case UAC3_CLOCK_SOURCE:
  2199. return parse_clock_source_unit(state, unitid, p1);
  2200. case UAC3_CLOCK_SELECTOR:
  2201. return parse_audio_selector_unit(state, unitid, p1);
  2202. case UAC3_FEATURE_UNIT:
  2203. return parse_audio_feature_unit(state, unitid, p1);
  2204. case UAC3_EFFECT_UNIT:
  2205. return 0; /* FIXME - effect units not implemented yet */
  2206. case UAC3_PROCESSING_UNIT:
  2207. return parse_audio_processing_unit(state, unitid, p1);
  2208. case UAC3_EXTENSION_UNIT:
  2209. return parse_audio_extension_unit(state, unitid, p1);
  2210. default:
  2211. usb_audio_err(state->chip,
  2212. "unit %u: unexpected type 0x%02x\n", unitid, p1[2]);
  2213. return -EINVAL;
  2214. }
  2215. }
  2216. }
  2217. static void snd_usb_mixer_free(struct usb_mixer_interface *mixer)
  2218. {
  2219. /* kill pending URBs */
  2220. snd_usb_mixer_disconnect(mixer);
  2221. kfree(mixer->id_elems);
  2222. if (mixer->urb) {
  2223. kfree(mixer->urb->transfer_buffer);
  2224. usb_free_urb(mixer->urb);
  2225. }
  2226. usb_free_urb(mixer->rc_urb);
  2227. kfree(mixer->rc_setup_packet);
  2228. kfree(mixer);
  2229. }
  2230. static int snd_usb_mixer_dev_free(struct snd_device *device)
  2231. {
  2232. struct usb_mixer_interface *mixer = device->device_data;
  2233. snd_usb_mixer_free(mixer);
  2234. return 0;
  2235. }
  2236. /*
  2237. * create mixer controls
  2238. *
  2239. * walk through all UAC_OUTPUT_TERMINAL descriptors to search for mixers
  2240. */
  2241. static int snd_usb_mixer_controls(struct usb_mixer_interface *mixer)
  2242. {
  2243. struct mixer_build state;
  2244. int err;
  2245. const struct usbmix_ctl_map *map;
  2246. void *p;
  2247. memset(&state, 0, sizeof(state));
  2248. state.chip = mixer->chip;
  2249. state.mixer = mixer;
  2250. state.buffer = mixer->hostif->extra;
  2251. state.buflen = mixer->hostif->extralen;
  2252. /* check the mapping table */
  2253. for (map = usbmix_ctl_maps; map->id; map++) {
  2254. if (map->id == state.chip->usb_id) {
  2255. state.map = map->map;
  2256. state.selector_map = map->selector_map;
  2257. mixer->ignore_ctl_error = map->ignore_ctl_error;
  2258. break;
  2259. }
  2260. }
  2261. p = NULL;
  2262. while ((p = snd_usb_find_csint_desc(mixer->hostif->extra,
  2263. mixer->hostif->extralen,
  2264. p, UAC_OUTPUT_TERMINAL)) != NULL) {
  2265. if (mixer->protocol == UAC_VERSION_1) {
  2266. struct uac1_output_terminal_descriptor *desc = p;
  2267. if (desc->bLength < sizeof(*desc))
  2268. continue; /* invalid descriptor? */
  2269. /* mark terminal ID as visited */
  2270. set_bit(desc->bTerminalID, state.unitbitmap);
  2271. state.oterm.id = desc->bTerminalID;
  2272. state.oterm.type = le16_to_cpu(desc->wTerminalType);
  2273. state.oterm.name = desc->iTerminal;
  2274. err = parse_audio_unit(&state, desc->bSourceID);
  2275. if (err < 0 && err != -EINVAL)
  2276. return err;
  2277. } else if (mixer->protocol == UAC_VERSION_2) {
  2278. struct uac2_output_terminal_descriptor *desc = p;
  2279. if (desc->bLength < sizeof(*desc))
  2280. continue; /* invalid descriptor? */
  2281. /* mark terminal ID as visited */
  2282. set_bit(desc->bTerminalID, state.unitbitmap);
  2283. state.oterm.id = desc->bTerminalID;
  2284. state.oterm.type = le16_to_cpu(desc->wTerminalType);
  2285. state.oterm.name = desc->iTerminal;
  2286. err = parse_audio_unit(&state, desc->bSourceID);
  2287. if (err < 0 && err != -EINVAL)
  2288. return err;
  2289. /*
  2290. * For UAC2, use the same approach to also add the
  2291. * clock selectors
  2292. */
  2293. err = parse_audio_unit(&state, desc->bCSourceID);
  2294. if (err < 0 && err != -EINVAL)
  2295. return err;
  2296. if (uac_v2v3_control_is_readable(le16_to_cpu(desc->bmControls),
  2297. UAC2_TE_CONNECTOR)) {
  2298. build_connector_control(&state, &state.oterm,
  2299. false);
  2300. }
  2301. } else { /* UAC_VERSION_3 */
  2302. struct uac3_output_terminal_descriptor *desc = p;
  2303. if (desc->bLength < sizeof(*desc))
  2304. continue; /* invalid descriptor? */
  2305. /* mark terminal ID as visited */
  2306. set_bit(desc->bTerminalID, state.unitbitmap);
  2307. state.oterm.id = desc->bTerminalID;
  2308. state.oterm.type = le16_to_cpu(desc->wTerminalType);
  2309. state.oterm.name = le16_to_cpu(desc->wTerminalDescrStr);
  2310. err = parse_audio_unit(&state, desc->bSourceID);
  2311. if (err < 0 && err != -EINVAL)
  2312. return err;
  2313. /*
  2314. * For UAC3, use the same approach to also add the
  2315. * clock selectors
  2316. */
  2317. err = parse_audio_unit(&state, desc->bCSourceID);
  2318. if (err < 0 && err != -EINVAL)
  2319. return err;
  2320. }
  2321. }
  2322. return 0;
  2323. }
  2324. void snd_usb_mixer_notify_id(struct usb_mixer_interface *mixer, int unitid)
  2325. {
  2326. struct usb_mixer_elem_list *list;
  2327. for (list = mixer->id_elems[unitid]; list; list = list->next_id_elem) {
  2328. struct usb_mixer_elem_info *info =
  2329. (struct usb_mixer_elem_info *)list;
  2330. /* invalidate cache, so the value is read from the device */
  2331. info->cached = 0;
  2332. snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
  2333. &list->kctl->id);
  2334. }
  2335. }
  2336. static void snd_usb_mixer_dump_cval(struct snd_info_buffer *buffer,
  2337. struct usb_mixer_elem_list *list)
  2338. {
  2339. struct usb_mixer_elem_info *cval = (struct usb_mixer_elem_info *)list;
  2340. static char *val_types[] = {"BOOLEAN", "INV_BOOLEAN",
  2341. "S8", "U8", "S16", "U16"};
  2342. snd_iprintf(buffer, " Info: id=%i, control=%i, cmask=0x%x, "
  2343. "channels=%i, type=\"%s\"\n", cval->head.id,
  2344. cval->control, cval->cmask, cval->channels,
  2345. val_types[cval->val_type]);
  2346. snd_iprintf(buffer, " Volume: min=%i, max=%i, dBmin=%i, dBmax=%i\n",
  2347. cval->min, cval->max, cval->dBmin, cval->dBmax);
  2348. }
  2349. static void snd_usb_mixer_proc_read(struct snd_info_entry *entry,
  2350. struct snd_info_buffer *buffer)
  2351. {
  2352. struct snd_usb_audio *chip = entry->private_data;
  2353. struct usb_mixer_interface *mixer;
  2354. struct usb_mixer_elem_list *list;
  2355. int unitid;
  2356. list_for_each_entry(mixer, &chip->mixer_list, list) {
  2357. snd_iprintf(buffer,
  2358. "USB Mixer: usb_id=0x%08x, ctrlif=%i, ctlerr=%i\n",
  2359. chip->usb_id, snd_usb_ctrl_intf(chip),
  2360. mixer->ignore_ctl_error);
  2361. snd_iprintf(buffer, "Card: %s\n", chip->card->longname);
  2362. for (unitid = 0; unitid < MAX_ID_ELEMS; unitid++) {
  2363. for (list = mixer->id_elems[unitid]; list;
  2364. list = list->next_id_elem) {
  2365. snd_iprintf(buffer, " Unit: %i\n", list->id);
  2366. if (list->kctl)
  2367. snd_iprintf(buffer,
  2368. " Control: name=\"%s\", index=%i\n",
  2369. list->kctl->id.name,
  2370. list->kctl->id.index);
  2371. if (list->dump)
  2372. list->dump(buffer, list);
  2373. }
  2374. }
  2375. }
  2376. }
  2377. static void snd_usb_mixer_interrupt_v2(struct usb_mixer_interface *mixer,
  2378. int attribute, int value, int index)
  2379. {
  2380. struct usb_mixer_elem_list *list;
  2381. __u8 unitid = (index >> 8) & 0xff;
  2382. __u8 control = (value >> 8) & 0xff;
  2383. __u8 channel = value & 0xff;
  2384. unsigned int count = 0;
  2385. if (channel >= MAX_CHANNELS) {
  2386. usb_audio_dbg(mixer->chip,
  2387. "%s(): bogus channel number %d\n",
  2388. __func__, channel);
  2389. return;
  2390. }
  2391. for (list = mixer->id_elems[unitid]; list; list = list->next_id_elem)
  2392. count++;
  2393. if (count == 0)
  2394. return;
  2395. for (list = mixer->id_elems[unitid]; list; list = list->next_id_elem) {
  2396. struct usb_mixer_elem_info *info;
  2397. if (!list->kctl)
  2398. continue;
  2399. info = (struct usb_mixer_elem_info *)list;
  2400. if (count > 1 && info->control != control)
  2401. continue;
  2402. switch (attribute) {
  2403. case UAC2_CS_CUR:
  2404. /* invalidate cache, so the value is read from the device */
  2405. if (channel)
  2406. info->cached &= ~(1 << channel);
  2407. else /* master channel */
  2408. info->cached = 0;
  2409. snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
  2410. &info->head.kctl->id);
  2411. break;
  2412. case UAC2_CS_RANGE:
  2413. /* TODO */
  2414. break;
  2415. case UAC2_CS_MEM:
  2416. /* TODO */
  2417. break;
  2418. default:
  2419. usb_audio_dbg(mixer->chip,
  2420. "unknown attribute %d in interrupt\n",
  2421. attribute);
  2422. break;
  2423. } /* switch */
  2424. }
  2425. }
  2426. static void snd_usb_mixer_interrupt(struct urb *urb)
  2427. {
  2428. struct usb_mixer_interface *mixer = urb->context;
  2429. int len = urb->actual_length;
  2430. int ustatus = urb->status;
  2431. if (ustatus != 0)
  2432. goto requeue;
  2433. if (mixer->protocol == UAC_VERSION_1) {
  2434. struct uac1_status_word *status;
  2435. for (status = urb->transfer_buffer;
  2436. len >= sizeof(*status);
  2437. len -= sizeof(*status), status++) {
  2438. dev_dbg(&urb->dev->dev, "status interrupt: %02x %02x\n",
  2439. status->bStatusType,
  2440. status->bOriginator);
  2441. /* ignore any notifications not from the control interface */
  2442. if ((status->bStatusType & UAC1_STATUS_TYPE_ORIG_MASK) !=
  2443. UAC1_STATUS_TYPE_ORIG_AUDIO_CONTROL_IF)
  2444. continue;
  2445. if (status->bStatusType & UAC1_STATUS_TYPE_MEM_CHANGED)
  2446. snd_usb_mixer_rc_memory_change(mixer, status->bOriginator);
  2447. else
  2448. snd_usb_mixer_notify_id(mixer, status->bOriginator);
  2449. }
  2450. } else { /* UAC_VERSION_2 */
  2451. struct uac2_interrupt_data_msg *msg;
  2452. for (msg = urb->transfer_buffer;
  2453. len >= sizeof(*msg);
  2454. len -= sizeof(*msg), msg++) {
  2455. /* drop vendor specific and endpoint requests */
  2456. if ((msg->bInfo & UAC2_INTERRUPT_DATA_MSG_VENDOR) ||
  2457. (msg->bInfo & UAC2_INTERRUPT_DATA_MSG_EP))
  2458. continue;
  2459. snd_usb_mixer_interrupt_v2(mixer, msg->bAttribute,
  2460. le16_to_cpu(msg->wValue),
  2461. le16_to_cpu(msg->wIndex));
  2462. }
  2463. }
  2464. requeue:
  2465. if (ustatus != -ENOENT &&
  2466. ustatus != -ECONNRESET &&
  2467. ustatus != -ESHUTDOWN) {
  2468. urb->dev = mixer->chip->dev;
  2469. usb_submit_urb(urb, GFP_ATOMIC);
  2470. }
  2471. }
  2472. /* create the handler for the optional status interrupt endpoint */
  2473. static int snd_usb_mixer_status_create(struct usb_mixer_interface *mixer)
  2474. {
  2475. struct usb_endpoint_descriptor *ep;
  2476. void *transfer_buffer;
  2477. int buffer_length;
  2478. unsigned int epnum;
  2479. /* we need one interrupt input endpoint */
  2480. if (get_iface_desc(mixer->hostif)->bNumEndpoints < 1)
  2481. return 0;
  2482. ep = get_endpoint(mixer->hostif, 0);
  2483. if (!usb_endpoint_dir_in(ep) || !usb_endpoint_xfer_int(ep))
  2484. return 0;
  2485. epnum = usb_endpoint_num(ep);
  2486. buffer_length = le16_to_cpu(ep->wMaxPacketSize);
  2487. transfer_buffer = kmalloc(buffer_length, GFP_KERNEL);
  2488. if (!transfer_buffer)
  2489. return -ENOMEM;
  2490. mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
  2491. if (!mixer->urb) {
  2492. kfree(transfer_buffer);
  2493. return -ENOMEM;
  2494. }
  2495. usb_fill_int_urb(mixer->urb, mixer->chip->dev,
  2496. usb_rcvintpipe(mixer->chip->dev, epnum),
  2497. transfer_buffer, buffer_length,
  2498. snd_usb_mixer_interrupt, mixer, ep->bInterval);
  2499. usb_submit_urb(mixer->urb, GFP_KERNEL);
  2500. return 0;
  2501. }
  2502. int snd_usb_create_mixer(struct snd_usb_audio *chip, int ctrlif,
  2503. int ignore_error)
  2504. {
  2505. static struct snd_device_ops dev_ops = {
  2506. .dev_free = snd_usb_mixer_dev_free
  2507. };
  2508. struct usb_mixer_interface *mixer;
  2509. struct snd_info_entry *entry;
  2510. int err;
  2511. strcpy(chip->card->mixername, "USB Mixer");
  2512. mixer = kzalloc(sizeof(*mixer), GFP_KERNEL);
  2513. if (!mixer)
  2514. return -ENOMEM;
  2515. mixer->chip = chip;
  2516. mixer->ignore_ctl_error = ignore_error;
  2517. mixer->id_elems = kcalloc(MAX_ID_ELEMS, sizeof(*mixer->id_elems),
  2518. GFP_KERNEL);
  2519. if (!mixer->id_elems) {
  2520. kfree(mixer);
  2521. return -ENOMEM;
  2522. }
  2523. mixer->hostif = &usb_ifnum_to_if(chip->dev, ctrlif)->altsetting[0];
  2524. switch (get_iface_desc(mixer->hostif)->bInterfaceProtocol) {
  2525. case UAC_VERSION_1:
  2526. default:
  2527. mixer->protocol = UAC_VERSION_1;
  2528. break;
  2529. case UAC_VERSION_2:
  2530. mixer->protocol = UAC_VERSION_2;
  2531. break;
  2532. case UAC_VERSION_3:
  2533. mixer->protocol = UAC_VERSION_3;
  2534. break;
  2535. }
  2536. if ((err = snd_usb_mixer_controls(mixer)) < 0 ||
  2537. (err = snd_usb_mixer_status_create(mixer)) < 0)
  2538. goto _error;
  2539. snd_usb_mixer_apply_create_quirk(mixer);
  2540. err = snd_device_new(chip->card, SNDRV_DEV_CODEC, mixer, &dev_ops);
  2541. if (err < 0)
  2542. goto _error;
  2543. if (list_empty(&chip->mixer_list) &&
  2544. !snd_card_proc_new(chip->card, "usbmixer", &entry))
  2545. snd_info_set_text_ops(entry, chip, snd_usb_mixer_proc_read);
  2546. list_add(&mixer->list, &chip->mixer_list);
  2547. return 0;
  2548. _error:
  2549. snd_usb_mixer_free(mixer);
  2550. return err;
  2551. }
  2552. void snd_usb_mixer_disconnect(struct usb_mixer_interface *mixer)
  2553. {
  2554. if (mixer->disconnected)
  2555. return;
  2556. if (mixer->urb)
  2557. usb_kill_urb(mixer->urb);
  2558. if (mixer->rc_urb)
  2559. usb_kill_urb(mixer->rc_urb);
  2560. mixer->disconnected = true;
  2561. }
  2562. #ifdef CONFIG_PM
  2563. /* stop any bus activity of a mixer */
  2564. static void snd_usb_mixer_inactivate(struct usb_mixer_interface *mixer)
  2565. {
  2566. usb_kill_urb(mixer->urb);
  2567. usb_kill_urb(mixer->rc_urb);
  2568. }
  2569. static int snd_usb_mixer_activate(struct usb_mixer_interface *mixer)
  2570. {
  2571. int err;
  2572. if (mixer->urb) {
  2573. err = usb_submit_urb(mixer->urb, GFP_NOIO);
  2574. if (err < 0)
  2575. return err;
  2576. }
  2577. return 0;
  2578. }
  2579. int snd_usb_mixer_suspend(struct usb_mixer_interface *mixer)
  2580. {
  2581. snd_usb_mixer_inactivate(mixer);
  2582. return 0;
  2583. }
  2584. static int restore_mixer_value(struct usb_mixer_elem_list *list)
  2585. {
  2586. struct usb_mixer_elem_info *cval = (struct usb_mixer_elem_info *)list;
  2587. int c, err, idx;
  2588. if (cval->cmask) {
  2589. idx = 0;
  2590. for (c = 0; c < MAX_CHANNELS; c++) {
  2591. if (!(cval->cmask & (1 << c)))
  2592. continue;
  2593. if (cval->cached & (1 << (c + 1))) {
  2594. err = snd_usb_set_cur_mix_value(cval, c + 1, idx,
  2595. cval->cache_val[idx]);
  2596. if (err < 0)
  2597. return err;
  2598. }
  2599. idx++;
  2600. }
  2601. } else {
  2602. /* master */
  2603. if (cval->cached) {
  2604. err = snd_usb_set_cur_mix_value(cval, 0, 0, *cval->cache_val);
  2605. if (err < 0)
  2606. return err;
  2607. }
  2608. }
  2609. return 0;
  2610. }
  2611. int snd_usb_mixer_resume(struct usb_mixer_interface *mixer, bool reset_resume)
  2612. {
  2613. struct usb_mixer_elem_list *list;
  2614. int id, err;
  2615. if (reset_resume) {
  2616. /* restore cached mixer values */
  2617. for (id = 0; id < MAX_ID_ELEMS; id++) {
  2618. for (list = mixer->id_elems[id]; list;
  2619. list = list->next_id_elem) {
  2620. if (list->resume) {
  2621. err = list->resume(list);
  2622. if (err < 0)
  2623. return err;
  2624. }
  2625. }
  2626. }
  2627. }
  2628. snd_usb_mixer_resume_quirk(mixer);
  2629. return snd_usb_mixer_activate(mixer);
  2630. }
  2631. #endif
  2632. void snd_usb_mixer_elem_init_std(struct usb_mixer_elem_list *list,
  2633. struct usb_mixer_interface *mixer,
  2634. int unitid)
  2635. {
  2636. list->mixer = mixer;
  2637. list->id = unitid;
  2638. list->dump = snd_usb_mixer_dump_cval;
  2639. #ifdef CONFIG_PM
  2640. list->resume = restore_mixer_value;
  2641. #endif
  2642. }