mixer.c 92 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471
  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(const struct usbmix_name_map *p, int unitid, int control)
  105. {
  106. if (!p)
  107. return NULL;
  108. for (; p->id; p++) {
  109. if (p->id == unitid &&
  110. (!control || !p->control || control == p->control))
  111. return p;
  112. }
  113. return NULL;
  114. }
  115. /* get the mapped name if the unit matches */
  116. static int
  117. check_mapped_name(const struct usbmix_name_map *p, char *buf, int buflen)
  118. {
  119. if (!p || !p->name)
  120. return 0;
  121. buflen--;
  122. return strlcpy(buf, p->name, buflen);
  123. }
  124. /* ignore the error value if ignore_ctl_error flag is set */
  125. #define filter_error(cval, err) \
  126. ((cval)->head.mixer->ignore_ctl_error ? 0 : (err))
  127. /* check whether the control should be ignored */
  128. static inline int
  129. check_ignored_ctl(const struct usbmix_name_map *p)
  130. {
  131. if (!p || p->name || p->dB)
  132. return 0;
  133. return 1;
  134. }
  135. /* dB mapping */
  136. static inline void check_mapped_dB(const struct usbmix_name_map *p,
  137. struct usb_mixer_elem_info *cval)
  138. {
  139. if (p && p->dB) {
  140. cval->dBmin = p->dB->min;
  141. cval->dBmax = p->dB->max;
  142. cval->initialized = 1;
  143. }
  144. }
  145. /* get the mapped selector source name */
  146. static int check_mapped_selector_name(struct mixer_build *state, int unitid,
  147. int index, char *buf, int buflen)
  148. {
  149. const struct usbmix_selector_map *p;
  150. if (!state->selector_map)
  151. return 0;
  152. for (p = state->selector_map; p->id; p++) {
  153. if (p->id == unitid && index < p->count)
  154. return strlcpy(buf, p->names[index], buflen);
  155. }
  156. return 0;
  157. }
  158. /*
  159. * find an audio control unit with the given unit id
  160. */
  161. static void *find_audio_control_unit(struct mixer_build *state,
  162. unsigned char unit)
  163. {
  164. /* we just parse the header */
  165. struct uac_feature_unit_descriptor *hdr = NULL;
  166. while ((hdr = snd_usb_find_desc(state->buffer, state->buflen, hdr,
  167. USB_DT_CS_INTERFACE)) != NULL) {
  168. if (hdr->bLength >= 4 &&
  169. hdr->bDescriptorSubtype >= UAC_INPUT_TERMINAL &&
  170. hdr->bDescriptorSubtype <= UAC3_SAMPLE_RATE_CONVERTER &&
  171. hdr->bUnitID == unit)
  172. return hdr;
  173. }
  174. return NULL;
  175. }
  176. /*
  177. * copy a string with the given id
  178. */
  179. static int snd_usb_copy_string_desc(struct snd_usb_audio *chip,
  180. int index, char *buf, int maxlen)
  181. {
  182. int len = usb_string(chip->dev, index, buf, maxlen - 1);
  183. if (len < 0)
  184. return 0;
  185. buf[len] = 0;
  186. return len;
  187. }
  188. /*
  189. * convert from the byte/word on usb descriptor to the zero-based integer
  190. */
  191. static int convert_signed_value(struct usb_mixer_elem_info *cval, int val)
  192. {
  193. switch (cval->val_type) {
  194. case USB_MIXER_BOOLEAN:
  195. return !!val;
  196. case USB_MIXER_INV_BOOLEAN:
  197. return !val;
  198. case USB_MIXER_U8:
  199. val &= 0xff;
  200. break;
  201. case USB_MIXER_S8:
  202. val &= 0xff;
  203. if (val >= 0x80)
  204. val -= 0x100;
  205. break;
  206. case USB_MIXER_U16:
  207. val &= 0xffff;
  208. break;
  209. case USB_MIXER_S16:
  210. val &= 0xffff;
  211. if (val >= 0x8000)
  212. val -= 0x10000;
  213. break;
  214. }
  215. return val;
  216. }
  217. /*
  218. * convert from the zero-based int to the byte/word for usb descriptor
  219. */
  220. static int convert_bytes_value(struct usb_mixer_elem_info *cval, int val)
  221. {
  222. switch (cval->val_type) {
  223. case USB_MIXER_BOOLEAN:
  224. return !!val;
  225. case USB_MIXER_INV_BOOLEAN:
  226. return !val;
  227. case USB_MIXER_S8:
  228. case USB_MIXER_U8:
  229. return val & 0xff;
  230. case USB_MIXER_S16:
  231. case USB_MIXER_U16:
  232. return val & 0xffff;
  233. }
  234. return 0; /* not reached */
  235. }
  236. static int get_relative_value(struct usb_mixer_elem_info *cval, int val)
  237. {
  238. if (!cval->res)
  239. cval->res = 1;
  240. if (val < cval->min)
  241. return 0;
  242. else if (val >= cval->max)
  243. return (cval->max - cval->min + cval->res - 1) / cval->res;
  244. else
  245. return (val - cval->min) / cval->res;
  246. }
  247. static int get_abs_value(struct usb_mixer_elem_info *cval, int val)
  248. {
  249. if (val < 0)
  250. return cval->min;
  251. if (!cval->res)
  252. cval->res = 1;
  253. val *= cval->res;
  254. val += cval->min;
  255. if (val > cval->max)
  256. return cval->max;
  257. return val;
  258. }
  259. static int uac2_ctl_value_size(int val_type)
  260. {
  261. switch (val_type) {
  262. case USB_MIXER_S32:
  263. case USB_MIXER_U32:
  264. return 4;
  265. case USB_MIXER_S16:
  266. case USB_MIXER_U16:
  267. return 2;
  268. default:
  269. return 1;
  270. }
  271. return 0; /* unreachable */
  272. }
  273. /*
  274. * retrieve a mixer value
  275. */
  276. static int get_ctl_value_v1(struct usb_mixer_elem_info *cval, int request,
  277. int validx, int *value_ret)
  278. {
  279. struct snd_usb_audio *chip = cval->head.mixer->chip;
  280. unsigned char buf[2];
  281. int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
  282. int timeout = 10;
  283. int idx = 0, err;
  284. err = snd_usb_lock_shutdown(chip);
  285. if (err < 0)
  286. return -EIO;
  287. while (timeout-- > 0) {
  288. idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8);
  289. err = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), request,
  290. USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
  291. validx, idx, buf, val_len);
  292. if (err >= val_len) {
  293. *value_ret = convert_signed_value(cval, snd_usb_combine_bytes(buf, val_len));
  294. err = 0;
  295. goto out;
  296. } else if (err == -ETIMEDOUT) {
  297. goto out;
  298. }
  299. }
  300. usb_audio_dbg(chip,
  301. "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
  302. request, validx, idx, cval->val_type);
  303. err = -EINVAL;
  304. out:
  305. snd_usb_unlock_shutdown(chip);
  306. return err;
  307. }
  308. static int get_ctl_value_v2(struct usb_mixer_elem_info *cval, int request,
  309. int validx, int *value_ret)
  310. {
  311. struct snd_usb_audio *chip = cval->head.mixer->chip;
  312. /* enough space for one range */
  313. unsigned char buf[sizeof(__u16) + 3 * sizeof(__u32)];
  314. unsigned char *val;
  315. int idx = 0, ret, val_size, size;
  316. __u8 bRequest;
  317. val_size = uac2_ctl_value_size(cval->val_type);
  318. if (request == UAC_GET_CUR) {
  319. bRequest = UAC2_CS_CUR;
  320. size = val_size;
  321. } else {
  322. bRequest = UAC2_CS_RANGE;
  323. size = sizeof(__u16) + 3 * val_size;
  324. }
  325. memset(buf, 0, sizeof(buf));
  326. ret = snd_usb_lock_shutdown(chip) ? -EIO : 0;
  327. if (ret)
  328. goto error;
  329. idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8);
  330. ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), bRequest,
  331. USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
  332. validx, idx, buf, size);
  333. snd_usb_unlock_shutdown(chip);
  334. if (ret < 0) {
  335. error:
  336. usb_audio_err(chip,
  337. "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
  338. request, validx, idx, cval->val_type);
  339. return ret;
  340. }
  341. /* FIXME: how should we handle multiple triplets here? */
  342. switch (request) {
  343. case UAC_GET_CUR:
  344. val = buf;
  345. break;
  346. case UAC_GET_MIN:
  347. val = buf + sizeof(__u16);
  348. break;
  349. case UAC_GET_MAX:
  350. val = buf + sizeof(__u16) + val_size;
  351. break;
  352. case UAC_GET_RES:
  353. val = buf + sizeof(__u16) + val_size * 2;
  354. break;
  355. default:
  356. return -EINVAL;
  357. }
  358. *value_ret = convert_signed_value(cval,
  359. snd_usb_combine_bytes(val, val_size));
  360. return 0;
  361. }
  362. static int get_ctl_value(struct usb_mixer_elem_info *cval, int request,
  363. int validx, int *value_ret)
  364. {
  365. validx += cval->idx_off;
  366. return (cval->head.mixer->protocol == UAC_VERSION_1) ?
  367. get_ctl_value_v1(cval, request, validx, value_ret) :
  368. get_ctl_value_v2(cval, request, validx, value_ret);
  369. }
  370. static int get_cur_ctl_value(struct usb_mixer_elem_info *cval,
  371. int validx, int *value)
  372. {
  373. return get_ctl_value(cval, UAC_GET_CUR, validx, value);
  374. }
  375. /* channel = 0: master, 1 = first channel */
  376. static inline int get_cur_mix_raw(struct usb_mixer_elem_info *cval,
  377. int channel, int *value)
  378. {
  379. return get_ctl_value(cval, UAC_GET_CUR,
  380. (cval->control << 8) | channel,
  381. value);
  382. }
  383. int snd_usb_get_cur_mix_value(struct usb_mixer_elem_info *cval,
  384. int channel, int index, int *value)
  385. {
  386. int err;
  387. if (cval->cached & (1 << channel)) {
  388. *value = cval->cache_val[index];
  389. return 0;
  390. }
  391. err = get_cur_mix_raw(cval, channel, value);
  392. if (err < 0) {
  393. if (!cval->head.mixer->ignore_ctl_error)
  394. usb_audio_dbg(cval->head.mixer->chip,
  395. "cannot get current value for control %d ch %d: err = %d\n",
  396. cval->control, channel, err);
  397. return err;
  398. }
  399. cval->cached |= 1 << channel;
  400. cval->cache_val[index] = *value;
  401. return 0;
  402. }
  403. /*
  404. * set a mixer value
  405. */
  406. int snd_usb_mixer_set_ctl_value(struct usb_mixer_elem_info *cval,
  407. int request, int validx, int value_set)
  408. {
  409. struct snd_usb_audio *chip = cval->head.mixer->chip;
  410. unsigned char buf[4];
  411. int idx = 0, val_len, err, timeout = 10;
  412. validx += cval->idx_off;
  413. if (cval->head.mixer->protocol == UAC_VERSION_1) {
  414. val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
  415. } else { /* UAC_VERSION_2/3 */
  416. val_len = uac2_ctl_value_size(cval->val_type);
  417. /* FIXME */
  418. if (request != UAC_SET_CUR) {
  419. usb_audio_dbg(chip, "RANGE setting not yet supported\n");
  420. return -EINVAL;
  421. }
  422. request = UAC2_CS_CUR;
  423. }
  424. value_set = convert_bytes_value(cval, value_set);
  425. buf[0] = value_set & 0xff;
  426. buf[1] = (value_set >> 8) & 0xff;
  427. buf[2] = (value_set >> 16) & 0xff;
  428. buf[3] = (value_set >> 24) & 0xff;
  429. err = snd_usb_lock_shutdown(chip);
  430. if (err < 0)
  431. return -EIO;
  432. while (timeout-- > 0) {
  433. idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8);
  434. err = snd_usb_ctl_msg(chip->dev,
  435. usb_sndctrlpipe(chip->dev, 0), request,
  436. USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
  437. validx, idx, buf, val_len);
  438. if (err >= 0) {
  439. err = 0;
  440. goto out;
  441. } else if (err == -ETIMEDOUT) {
  442. goto out;
  443. }
  444. }
  445. usb_audio_dbg(chip, "cannot set ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d, data = %#x/%#x\n",
  446. request, validx, idx, cval->val_type, buf[0], buf[1]);
  447. err = -EINVAL;
  448. out:
  449. snd_usb_unlock_shutdown(chip);
  450. return err;
  451. }
  452. static int set_cur_ctl_value(struct usb_mixer_elem_info *cval,
  453. int validx, int value)
  454. {
  455. return snd_usb_mixer_set_ctl_value(cval, UAC_SET_CUR, validx, value);
  456. }
  457. int snd_usb_set_cur_mix_value(struct usb_mixer_elem_info *cval, int channel,
  458. int index, int value)
  459. {
  460. int err;
  461. unsigned int read_only = (channel == 0) ?
  462. cval->master_readonly :
  463. cval->ch_readonly & (1 << (channel - 1));
  464. if (read_only) {
  465. usb_audio_dbg(cval->head.mixer->chip,
  466. "%s(): channel %d of control %d is read_only\n",
  467. __func__, channel, cval->control);
  468. return 0;
  469. }
  470. err = snd_usb_mixer_set_ctl_value(cval,
  471. UAC_SET_CUR, (cval->control << 8) | channel,
  472. value);
  473. if (err < 0)
  474. return err;
  475. cval->cached |= 1 << channel;
  476. cval->cache_val[index] = value;
  477. return 0;
  478. }
  479. /*
  480. * TLV callback for mixer volume controls
  481. */
  482. int snd_usb_mixer_vol_tlv(struct snd_kcontrol *kcontrol, int op_flag,
  483. unsigned int size, unsigned int __user *_tlv)
  484. {
  485. struct usb_mixer_elem_info *cval = kcontrol->private_data;
  486. DECLARE_TLV_DB_MINMAX(scale, 0, 0);
  487. if (size < sizeof(scale))
  488. return -ENOMEM;
  489. if (cval->min_mute)
  490. scale[0] = SNDRV_CTL_TLVT_DB_MINMAX_MUTE;
  491. scale[2] = cval->dBmin;
  492. scale[3] = cval->dBmax;
  493. if (copy_to_user(_tlv, scale, sizeof(scale)))
  494. return -EFAULT;
  495. return 0;
  496. }
  497. /*
  498. * parser routines begin here...
  499. */
  500. static int parse_audio_unit(struct mixer_build *state, int unitid);
  501. /*
  502. * check if the input/output channel routing is enabled on the given bitmap.
  503. * used for mixer unit parser
  504. */
  505. static int check_matrix_bitmap(unsigned char *bmap,
  506. int ich, int och, int num_outs)
  507. {
  508. int idx = ich * num_outs + och;
  509. return bmap[idx >> 3] & (0x80 >> (idx & 7));
  510. }
  511. /*
  512. * add an alsa control element
  513. * search and increment the index until an empty slot is found.
  514. *
  515. * if failed, give up and free the control instance.
  516. */
  517. int snd_usb_mixer_add_control(struct usb_mixer_elem_list *list,
  518. struct snd_kcontrol *kctl)
  519. {
  520. struct usb_mixer_interface *mixer = list->mixer;
  521. int err;
  522. while (snd_ctl_find_id(mixer->chip->card, &kctl->id))
  523. kctl->id.index++;
  524. err = snd_ctl_add(mixer->chip->card, kctl);
  525. if (err < 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 snd_usb_audio *chip, 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(chip, 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. * Get logical cluster information for UAC3 devices.
  636. */
  637. static int get_cluster_channels_v3(struct mixer_build *state, unsigned int cluster_id)
  638. {
  639. struct uac3_cluster_header_descriptor c_header;
  640. int err;
  641. err = snd_usb_ctl_msg(state->chip->dev,
  642. usb_rcvctrlpipe(state->chip->dev, 0),
  643. UAC3_CS_REQ_HIGH_CAPABILITY_DESCRIPTOR,
  644. USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
  645. cluster_id,
  646. snd_usb_ctrl_intf(state->chip),
  647. &c_header, sizeof(c_header));
  648. if (err < 0)
  649. goto error;
  650. if (err != sizeof(c_header)) {
  651. err = -EIO;
  652. goto error;
  653. }
  654. return c_header.bNrChannels;
  655. error:
  656. usb_audio_err(state->chip, "cannot request logical cluster ID: %d (err: %d)\n", cluster_id, err);
  657. return err;
  658. }
  659. /*
  660. * Get number of channels for a Mixer Unit.
  661. */
  662. static int uac_mixer_unit_get_channels(struct mixer_build *state,
  663. struct uac_mixer_unit_descriptor *desc)
  664. {
  665. int mu_channels;
  666. if (desc->bLength < 11)
  667. return -EINVAL;
  668. if (!desc->bNrInPins)
  669. return -EINVAL;
  670. switch (state->mixer->protocol) {
  671. case UAC_VERSION_1:
  672. case UAC_VERSION_2:
  673. default:
  674. mu_channels = uac_mixer_unit_bNrChannels(desc);
  675. break;
  676. case UAC_VERSION_3:
  677. mu_channels = get_cluster_channels_v3(state,
  678. uac3_mixer_unit_wClusterDescrID(desc));
  679. break;
  680. }
  681. if (!mu_channels)
  682. return -EINVAL;
  683. return mu_channels;
  684. }
  685. /*
  686. * parse the source unit recursively until it reaches to a terminal
  687. * or a branched unit.
  688. */
  689. static int check_input_term(struct mixer_build *state, int id,
  690. struct usb_audio_term *term)
  691. {
  692. int protocol = state->mixer->protocol;
  693. int err;
  694. void *p1;
  695. memset(term, 0, sizeof(*term));
  696. while ((p1 = find_audio_control_unit(state, id)) != NULL) {
  697. unsigned char *hdr = p1;
  698. term->id = id;
  699. if (protocol == UAC_VERSION_1 || protocol == UAC_VERSION_2) {
  700. switch (hdr[2]) {
  701. case UAC_INPUT_TERMINAL:
  702. if (protocol == UAC_VERSION_1) {
  703. struct uac_input_terminal_descriptor *d = p1;
  704. term->type = le16_to_cpu(d->wTerminalType);
  705. term->channels = d->bNrChannels;
  706. term->chconfig = le16_to_cpu(d->wChannelConfig);
  707. term->name = d->iTerminal;
  708. } else { /* UAC_VERSION_2 */
  709. struct uac2_input_terminal_descriptor *d = p1;
  710. /* call recursively to verify that the
  711. * referenced clock entity is valid */
  712. err = check_input_term(state, d->bCSourceID, term);
  713. if (err < 0)
  714. return err;
  715. /* save input term properties after recursion,
  716. * to ensure they are not overriden by the
  717. * recursion calls */
  718. term->id = id;
  719. term->type = le16_to_cpu(d->wTerminalType);
  720. term->channels = d->bNrChannels;
  721. term->chconfig = le32_to_cpu(d->bmChannelConfig);
  722. term->name = d->iTerminal;
  723. }
  724. return 0;
  725. case UAC_FEATURE_UNIT: {
  726. /* the header is the same for v1 and v2 */
  727. struct uac_feature_unit_descriptor *d = p1;
  728. id = d->bSourceID;
  729. break; /* continue to parse */
  730. }
  731. case UAC_MIXER_UNIT: {
  732. struct uac_mixer_unit_descriptor *d = p1;
  733. term->type = d->bDescriptorSubtype << 16; /* virtual type */
  734. term->channels = uac_mixer_unit_bNrChannels(d);
  735. term->chconfig = uac_mixer_unit_wChannelConfig(d, protocol);
  736. term->name = uac_mixer_unit_iMixer(d);
  737. return 0;
  738. }
  739. case UAC_SELECTOR_UNIT:
  740. case UAC2_CLOCK_SELECTOR: {
  741. struct uac_selector_unit_descriptor *d = p1;
  742. /* call recursively to retrieve the channel info */
  743. err = check_input_term(state, d->baSourceID[0], term);
  744. if (err < 0)
  745. return err;
  746. term->type = d->bDescriptorSubtype << 16; /* virtual type */
  747. term->id = id;
  748. term->name = uac_selector_unit_iSelector(d);
  749. return 0;
  750. }
  751. case UAC1_PROCESSING_UNIT:
  752. case UAC1_EXTENSION_UNIT:
  753. /* UAC2_PROCESSING_UNIT_V2 */
  754. /* UAC2_EFFECT_UNIT */
  755. case UAC2_EXTENSION_UNIT_V2: {
  756. struct uac_processing_unit_descriptor *d = p1;
  757. if (protocol == UAC_VERSION_2 &&
  758. hdr[2] == UAC2_EFFECT_UNIT) {
  759. /* UAC2/UAC1 unit IDs overlap here in an
  760. * uncompatible way. Ignore this unit for now.
  761. */
  762. return 0;
  763. }
  764. if (d->bNrInPins) {
  765. id = d->baSourceID[0];
  766. break; /* continue to parse */
  767. }
  768. term->type = d->bDescriptorSubtype << 16; /* virtual type */
  769. term->channels = uac_processing_unit_bNrChannels(d);
  770. term->chconfig = uac_processing_unit_wChannelConfig(d, protocol);
  771. term->name = uac_processing_unit_iProcessing(d, protocol);
  772. return 0;
  773. }
  774. case UAC2_CLOCK_SOURCE: {
  775. struct uac_clock_source_descriptor *d = p1;
  776. term->type = d->bDescriptorSubtype << 16; /* virtual type */
  777. term->id = id;
  778. term->name = d->iClockSource;
  779. return 0;
  780. }
  781. default:
  782. return -ENODEV;
  783. }
  784. } else { /* UAC_VERSION_3 */
  785. switch (hdr[2]) {
  786. case UAC_INPUT_TERMINAL: {
  787. struct uac3_input_terminal_descriptor *d = p1;
  788. /* call recursively to verify that the
  789. * referenced clock entity is valid */
  790. err = check_input_term(state, d->bCSourceID, term);
  791. if (err < 0)
  792. return err;
  793. /* save input term properties after recursion,
  794. * to ensure they are not overriden by the
  795. * recursion calls */
  796. term->id = id;
  797. term->type = le16_to_cpu(d->wTerminalType);
  798. err = get_cluster_channels_v3(state, le16_to_cpu(d->wClusterDescrID));
  799. if (err < 0)
  800. return err;
  801. term->channels = err;
  802. /* REVISIT: UAC3 IT doesn't have channels cfg */
  803. term->chconfig = 0;
  804. term->name = le16_to_cpu(d->wTerminalDescrStr);
  805. return 0;
  806. }
  807. case UAC3_FEATURE_UNIT: {
  808. struct uac3_feature_unit_descriptor *d = p1;
  809. id = d->bSourceID;
  810. break; /* continue to parse */
  811. }
  812. case UAC3_CLOCK_SOURCE: {
  813. struct uac3_clock_source_descriptor *d = p1;
  814. term->type = d->bDescriptorSubtype << 16; /* virtual type */
  815. term->id = id;
  816. term->name = le16_to_cpu(d->wClockSourceStr);
  817. return 0;
  818. }
  819. case UAC3_MIXER_UNIT: {
  820. struct uac_mixer_unit_descriptor *d = p1;
  821. err = uac_mixer_unit_get_channels(state, d);
  822. if (err < 0)
  823. return err;
  824. term->channels = err;
  825. term->type = d->bDescriptorSubtype << 16; /* virtual type */
  826. return 0;
  827. }
  828. default:
  829. return -ENODEV;
  830. }
  831. }
  832. }
  833. return -ENODEV;
  834. }
  835. /*
  836. * Feature Unit
  837. */
  838. /* feature unit control information */
  839. struct usb_feature_control_info {
  840. int control;
  841. const char *name;
  842. int type; /* data type for uac1 */
  843. int type_uac2; /* data type for uac2 if different from uac1, else -1 */
  844. };
  845. static struct usb_feature_control_info audio_feature_info[] = {
  846. { UAC_FU_MUTE, "Mute", USB_MIXER_INV_BOOLEAN, -1 },
  847. { UAC_FU_VOLUME, "Volume", USB_MIXER_S16, -1 },
  848. { UAC_FU_BASS, "Tone Control - Bass", USB_MIXER_S8, -1 },
  849. { UAC_FU_MID, "Tone Control - Mid", USB_MIXER_S8, -1 },
  850. { UAC_FU_TREBLE, "Tone Control - Treble", USB_MIXER_S8, -1 },
  851. { UAC_FU_GRAPHIC_EQUALIZER, "Graphic Equalizer", USB_MIXER_S8, -1 }, /* FIXME: not implemented yet */
  852. { UAC_FU_AUTOMATIC_GAIN, "Auto Gain Control", USB_MIXER_BOOLEAN, -1 },
  853. { UAC_FU_DELAY, "Delay Control", USB_MIXER_U16, USB_MIXER_U32 },
  854. { UAC_FU_BASS_BOOST, "Bass Boost", USB_MIXER_BOOLEAN, -1 },
  855. { UAC_FU_LOUDNESS, "Loudness", USB_MIXER_BOOLEAN, -1 },
  856. /* UAC2 specific */
  857. { UAC2_FU_INPUT_GAIN, "Input Gain Control", USB_MIXER_S16, -1 },
  858. { UAC2_FU_INPUT_GAIN_PAD, "Input Gain Pad Control", USB_MIXER_S16, -1 },
  859. { UAC2_FU_PHASE_INVERTER, "Phase Inverter Control", USB_MIXER_BOOLEAN, -1 },
  860. };
  861. /* private_free callback */
  862. void snd_usb_mixer_elem_free(struct snd_kcontrol *kctl)
  863. {
  864. kfree(kctl->private_data);
  865. kctl->private_data = NULL;
  866. }
  867. /*
  868. * interface to ALSA control for feature/mixer units
  869. */
  870. /* volume control quirks */
  871. static void volume_control_quirks(struct usb_mixer_elem_info *cval,
  872. struct snd_kcontrol *kctl)
  873. {
  874. struct snd_usb_audio *chip = cval->head.mixer->chip;
  875. switch (chip->usb_id) {
  876. case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */
  877. case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C600 */
  878. if (strcmp(kctl->id.name, "Effect Duration") == 0) {
  879. cval->min = 0x0000;
  880. cval->max = 0xffff;
  881. cval->res = 0x00e6;
  882. break;
  883. }
  884. if (strcmp(kctl->id.name, "Effect Volume") == 0 ||
  885. strcmp(kctl->id.name, "Effect Feedback Volume") == 0) {
  886. cval->min = 0x00;
  887. cval->max = 0xff;
  888. break;
  889. }
  890. if (strstr(kctl->id.name, "Effect Return") != NULL) {
  891. cval->min = 0xb706;
  892. cval->max = 0xff7b;
  893. cval->res = 0x0073;
  894. break;
  895. }
  896. if ((strstr(kctl->id.name, "Playback Volume") != NULL) ||
  897. (strstr(kctl->id.name, "Effect Send") != NULL)) {
  898. cval->min = 0xb5fb; /* -73 dB = 0xb6ff */
  899. cval->max = 0xfcfe;
  900. cval->res = 0x0073;
  901. }
  902. break;
  903. case USB_ID(0x0763, 0x2081): /* M-Audio Fast Track Ultra 8R */
  904. case USB_ID(0x0763, 0x2080): /* M-Audio Fast Track Ultra */
  905. if (strcmp(kctl->id.name, "Effect Duration") == 0) {
  906. usb_audio_info(chip,
  907. "set quirk for FTU Effect Duration\n");
  908. cval->min = 0x0000;
  909. cval->max = 0x7f00;
  910. cval->res = 0x0100;
  911. break;
  912. }
  913. if (strcmp(kctl->id.name, "Effect Volume") == 0 ||
  914. strcmp(kctl->id.name, "Effect Feedback Volume") == 0) {
  915. usb_audio_info(chip,
  916. "set quirks for FTU Effect Feedback/Volume\n");
  917. cval->min = 0x00;
  918. cval->max = 0x7f;
  919. break;
  920. }
  921. break;
  922. case USB_ID(0x0d8c, 0x0103):
  923. if (!strcmp(kctl->id.name, "PCM Playback Volume")) {
  924. usb_audio_info(chip,
  925. "set volume quirk for CM102-A+/102S+\n");
  926. cval->min = -256;
  927. }
  928. break;
  929. case USB_ID(0x0471, 0x0101):
  930. case USB_ID(0x0471, 0x0104):
  931. case USB_ID(0x0471, 0x0105):
  932. case USB_ID(0x0672, 0x1041):
  933. /* quirk for UDA1321/N101.
  934. * note that detection between firmware 2.1.1.7 (N101)
  935. * and later 2.1.1.21 is not very clear from datasheets.
  936. * I hope that the min value is -15360 for newer firmware --jk
  937. */
  938. if (!strcmp(kctl->id.name, "PCM Playback Volume") &&
  939. cval->min == -15616) {
  940. usb_audio_info(chip,
  941. "set volume quirk for UDA1321/N101 chip\n");
  942. cval->max = -256;
  943. }
  944. break;
  945. case USB_ID(0x046d, 0x09a4):
  946. if (!strcmp(kctl->id.name, "Mic Capture Volume")) {
  947. usb_audio_info(chip,
  948. "set volume quirk for QuickCam E3500\n");
  949. cval->min = 6080;
  950. cval->max = 8768;
  951. cval->res = 192;
  952. }
  953. break;
  954. case USB_ID(0x046d, 0x0807): /* Logitech Webcam C500 */
  955. case USB_ID(0x046d, 0x0808):
  956. case USB_ID(0x046d, 0x0809):
  957. case USB_ID(0x046d, 0x0819): /* Logitech Webcam C210 */
  958. case USB_ID(0x046d, 0x081b): /* HD Webcam c310 */
  959. case USB_ID(0x046d, 0x081d): /* HD Webcam c510 */
  960. case USB_ID(0x046d, 0x0825): /* HD Webcam c270 */
  961. case USB_ID(0x046d, 0x0826): /* HD Webcam c525 */
  962. case USB_ID(0x046d, 0x08ca): /* Logitech Quickcam Fusion */
  963. case USB_ID(0x046d, 0x0991):
  964. case USB_ID(0x046d, 0x09a2): /* QuickCam Communicate Deluxe/S7500 */
  965. /* Most audio usb devices lie about volume resolution.
  966. * Most Logitech webcams have res = 384.
  967. * Probably there is some logitech magic behind this number --fishor
  968. */
  969. if (!strcmp(kctl->id.name, "Mic Capture Volume")) {
  970. usb_audio_info(chip,
  971. "set resolution quirk: cval->res = 384\n");
  972. cval->res = 384;
  973. }
  974. break;
  975. }
  976. }
  977. /*
  978. * retrieve the minimum and maximum values for the specified control
  979. */
  980. static int get_min_max_with_quirks(struct usb_mixer_elem_info *cval,
  981. int default_min, struct snd_kcontrol *kctl)
  982. {
  983. /* for failsafe */
  984. cval->min = default_min;
  985. cval->max = cval->min + 1;
  986. cval->res = 1;
  987. cval->dBmin = cval->dBmax = 0;
  988. if (cval->val_type == USB_MIXER_BOOLEAN ||
  989. cval->val_type == USB_MIXER_INV_BOOLEAN) {
  990. cval->initialized = 1;
  991. } else {
  992. int minchn = 0;
  993. if (cval->cmask) {
  994. int i;
  995. for (i = 0; i < MAX_CHANNELS; i++)
  996. if (cval->cmask & (1 << i)) {
  997. minchn = i + 1;
  998. break;
  999. }
  1000. }
  1001. if (get_ctl_value(cval, UAC_GET_MAX, (cval->control << 8) | minchn, &cval->max) < 0 ||
  1002. get_ctl_value(cval, UAC_GET_MIN, (cval->control << 8) | minchn, &cval->min) < 0) {
  1003. usb_audio_err(cval->head.mixer->chip,
  1004. "%d:%d: cannot get min/max values for control %d (id %d)\n",
  1005. cval->head.id, snd_usb_ctrl_intf(cval->head.mixer->chip),
  1006. cval->control, cval->head.id);
  1007. return -EINVAL;
  1008. }
  1009. if (get_ctl_value(cval, UAC_GET_RES,
  1010. (cval->control << 8) | minchn,
  1011. &cval->res) < 0) {
  1012. cval->res = 1;
  1013. } else {
  1014. int last_valid_res = cval->res;
  1015. while (cval->res > 1) {
  1016. if (snd_usb_mixer_set_ctl_value(cval, UAC_SET_RES,
  1017. (cval->control << 8) | minchn,
  1018. cval->res / 2) < 0)
  1019. break;
  1020. cval->res /= 2;
  1021. }
  1022. if (get_ctl_value(cval, UAC_GET_RES,
  1023. (cval->control << 8) | minchn, &cval->res) < 0)
  1024. cval->res = last_valid_res;
  1025. }
  1026. if (cval->res == 0)
  1027. cval->res = 1;
  1028. /* Additional checks for the proper resolution
  1029. *
  1030. * Some devices report smaller resolutions than actually
  1031. * reacting. They don't return errors but simply clip
  1032. * to the lower aligned value.
  1033. */
  1034. if (cval->min + cval->res < cval->max) {
  1035. int last_valid_res = cval->res;
  1036. int saved, test, check;
  1037. get_cur_mix_raw(cval, minchn, &saved);
  1038. for (;;) {
  1039. test = saved;
  1040. if (test < cval->max)
  1041. test += cval->res;
  1042. else
  1043. test -= cval->res;
  1044. if (test < cval->min || test > cval->max ||
  1045. snd_usb_set_cur_mix_value(cval, minchn, 0, test) ||
  1046. get_cur_mix_raw(cval, minchn, &check)) {
  1047. cval->res = last_valid_res;
  1048. break;
  1049. }
  1050. if (test == check)
  1051. break;
  1052. cval->res *= 2;
  1053. }
  1054. snd_usb_set_cur_mix_value(cval, minchn, 0, saved);
  1055. }
  1056. cval->initialized = 1;
  1057. }
  1058. if (kctl)
  1059. volume_control_quirks(cval, kctl);
  1060. /* USB descriptions contain the dB scale in 1/256 dB unit
  1061. * while ALSA TLV contains in 1/100 dB unit
  1062. */
  1063. cval->dBmin = (convert_signed_value(cval, cval->min) * 100) / 256;
  1064. cval->dBmax = (convert_signed_value(cval, cval->max) * 100) / 256;
  1065. if (cval->dBmin > cval->dBmax) {
  1066. /* something is wrong; assume it's either from/to 0dB */
  1067. if (cval->dBmin < 0)
  1068. cval->dBmax = 0;
  1069. else if (cval->dBmin > 0)
  1070. cval->dBmin = 0;
  1071. if (cval->dBmin > cval->dBmax) {
  1072. /* totally crap, return an error */
  1073. return -EINVAL;
  1074. }
  1075. }
  1076. return 0;
  1077. }
  1078. #define get_min_max(cval, def) get_min_max_with_quirks(cval, def, NULL)
  1079. /* get a feature/mixer unit info */
  1080. static int mixer_ctl_feature_info(struct snd_kcontrol *kcontrol,
  1081. struct snd_ctl_elem_info *uinfo)
  1082. {
  1083. struct usb_mixer_elem_info *cval = kcontrol->private_data;
  1084. if (cval->val_type == USB_MIXER_BOOLEAN ||
  1085. cval->val_type == USB_MIXER_INV_BOOLEAN)
  1086. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1087. else
  1088. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1089. uinfo->count = cval->channels;
  1090. if (cval->val_type == USB_MIXER_BOOLEAN ||
  1091. cval->val_type == USB_MIXER_INV_BOOLEAN) {
  1092. uinfo->value.integer.min = 0;
  1093. uinfo->value.integer.max = 1;
  1094. } else {
  1095. if (!cval->initialized) {
  1096. get_min_max_with_quirks(cval, 0, kcontrol);
  1097. if (cval->initialized && cval->dBmin >= cval->dBmax) {
  1098. kcontrol->vd[0].access &=
  1099. ~(SNDRV_CTL_ELEM_ACCESS_TLV_READ |
  1100. SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK);
  1101. snd_ctl_notify(cval->head.mixer->chip->card,
  1102. SNDRV_CTL_EVENT_MASK_INFO,
  1103. &kcontrol->id);
  1104. }
  1105. }
  1106. uinfo->value.integer.min = 0;
  1107. uinfo->value.integer.max =
  1108. (cval->max - cval->min + cval->res - 1) / cval->res;
  1109. }
  1110. return 0;
  1111. }
  1112. /* get the current value from feature/mixer unit */
  1113. static int mixer_ctl_feature_get(struct snd_kcontrol *kcontrol,
  1114. struct snd_ctl_elem_value *ucontrol)
  1115. {
  1116. struct usb_mixer_elem_info *cval = kcontrol->private_data;
  1117. int c, cnt, val, err;
  1118. ucontrol->value.integer.value[0] = cval->min;
  1119. if (cval->cmask) {
  1120. cnt = 0;
  1121. for (c = 0; c < MAX_CHANNELS; c++) {
  1122. if (!(cval->cmask & (1 << c)))
  1123. continue;
  1124. err = snd_usb_get_cur_mix_value(cval, c + 1, cnt, &val);
  1125. if (err < 0)
  1126. return filter_error(cval, err);
  1127. val = get_relative_value(cval, val);
  1128. ucontrol->value.integer.value[cnt] = val;
  1129. cnt++;
  1130. }
  1131. return 0;
  1132. } else {
  1133. /* master channel */
  1134. err = snd_usb_get_cur_mix_value(cval, 0, 0, &val);
  1135. if (err < 0)
  1136. return filter_error(cval, err);
  1137. val = get_relative_value(cval, val);
  1138. ucontrol->value.integer.value[0] = val;
  1139. }
  1140. return 0;
  1141. }
  1142. /* put the current value to feature/mixer unit */
  1143. static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol,
  1144. struct snd_ctl_elem_value *ucontrol)
  1145. {
  1146. struct usb_mixer_elem_info *cval = kcontrol->private_data;
  1147. int c, cnt, val, oval, err;
  1148. int changed = 0;
  1149. if (cval->cmask) {
  1150. cnt = 0;
  1151. for (c = 0; c < MAX_CHANNELS; c++) {
  1152. if (!(cval->cmask & (1 << c)))
  1153. continue;
  1154. err = snd_usb_get_cur_mix_value(cval, c + 1, cnt, &oval);
  1155. if (err < 0)
  1156. return filter_error(cval, err);
  1157. val = ucontrol->value.integer.value[cnt];
  1158. val = get_abs_value(cval, val);
  1159. if (oval != val) {
  1160. snd_usb_set_cur_mix_value(cval, c + 1, cnt, val);
  1161. changed = 1;
  1162. }
  1163. cnt++;
  1164. }
  1165. } else {
  1166. /* master channel */
  1167. err = snd_usb_get_cur_mix_value(cval, 0, 0, &oval);
  1168. if (err < 0)
  1169. return filter_error(cval, err);
  1170. val = ucontrol->value.integer.value[0];
  1171. val = get_abs_value(cval, val);
  1172. if (val != oval) {
  1173. snd_usb_set_cur_mix_value(cval, 0, 0, val);
  1174. changed = 1;
  1175. }
  1176. }
  1177. return changed;
  1178. }
  1179. /* get the boolean value from the master channel of a UAC control */
  1180. static int mixer_ctl_master_bool_get(struct snd_kcontrol *kcontrol,
  1181. struct snd_ctl_elem_value *ucontrol)
  1182. {
  1183. struct usb_mixer_elem_info *cval = kcontrol->private_data;
  1184. int val, err;
  1185. err = snd_usb_get_cur_mix_value(cval, 0, 0, &val);
  1186. if (err < 0)
  1187. return filter_error(cval, err);
  1188. val = (val != 0);
  1189. ucontrol->value.integer.value[0] = val;
  1190. return 0;
  1191. }
  1192. /* get the connectors status and report it as boolean type */
  1193. static int mixer_ctl_connector_get(struct snd_kcontrol *kcontrol,
  1194. struct snd_ctl_elem_value *ucontrol)
  1195. {
  1196. struct usb_mixer_elem_info *cval = kcontrol->private_data;
  1197. struct snd_usb_audio *chip = cval->head.mixer->chip;
  1198. int idx = 0, validx, ret, val;
  1199. validx = cval->control << 8 | 0;
  1200. ret = snd_usb_lock_shutdown(chip) ? -EIO : 0;
  1201. if (ret)
  1202. goto error;
  1203. idx = snd_usb_ctrl_intf(chip) | (cval->head.id << 8);
  1204. if (cval->head.mixer->protocol == UAC_VERSION_2) {
  1205. struct uac2_connectors_ctl_blk uac2_conn;
  1206. ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), UAC2_CS_CUR,
  1207. USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
  1208. validx, idx, &uac2_conn, sizeof(uac2_conn));
  1209. val = !!uac2_conn.bNrChannels;
  1210. } else { /* UAC_VERSION_3 */
  1211. struct uac3_insertion_ctl_blk uac3_conn;
  1212. ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0), UAC2_CS_CUR,
  1213. USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
  1214. validx, idx, &uac3_conn, sizeof(uac3_conn));
  1215. val = !!uac3_conn.bmConInserted;
  1216. }
  1217. snd_usb_unlock_shutdown(chip);
  1218. if (ret < 0) {
  1219. error:
  1220. usb_audio_err(chip,
  1221. "cannot get connectors status: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
  1222. UAC_GET_CUR, validx, idx, cval->val_type);
  1223. return ret;
  1224. }
  1225. ucontrol->value.integer.value[0] = val;
  1226. return 0;
  1227. }
  1228. static struct snd_kcontrol_new usb_feature_unit_ctl = {
  1229. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1230. .name = "", /* will be filled later manually */
  1231. .info = mixer_ctl_feature_info,
  1232. .get = mixer_ctl_feature_get,
  1233. .put = mixer_ctl_feature_put,
  1234. };
  1235. /* the read-only variant */
  1236. static const struct snd_kcontrol_new usb_feature_unit_ctl_ro = {
  1237. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1238. .name = "", /* will be filled later manually */
  1239. .info = mixer_ctl_feature_info,
  1240. .get = mixer_ctl_feature_get,
  1241. .put = NULL,
  1242. };
  1243. /*
  1244. * A control which shows the boolean value from reading a UAC control on
  1245. * the master channel.
  1246. */
  1247. static struct snd_kcontrol_new usb_bool_master_control_ctl_ro = {
  1248. .iface = SNDRV_CTL_ELEM_IFACE_CARD,
  1249. .name = "", /* will be filled later manually */
  1250. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  1251. .info = snd_ctl_boolean_mono_info,
  1252. .get = mixer_ctl_master_bool_get,
  1253. .put = NULL,
  1254. };
  1255. static const struct snd_kcontrol_new usb_connector_ctl_ro = {
  1256. .iface = SNDRV_CTL_ELEM_IFACE_CARD,
  1257. .name = "", /* will be filled later manually */
  1258. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  1259. .info = snd_ctl_boolean_mono_info,
  1260. .get = mixer_ctl_connector_get,
  1261. .put = NULL,
  1262. };
  1263. /*
  1264. * This symbol is exported in order to allow the mixer quirks to
  1265. * hook up to the standard feature unit control mechanism
  1266. */
  1267. struct snd_kcontrol_new *snd_usb_feature_unit_ctl = &usb_feature_unit_ctl;
  1268. /*
  1269. * build a feature control
  1270. */
  1271. static size_t append_ctl_name(struct snd_kcontrol *kctl, const char *str)
  1272. {
  1273. return strlcat(kctl->id.name, str, sizeof(kctl->id.name));
  1274. }
  1275. /*
  1276. * A lot of headsets/headphones have a "Speaker" mixer. Make sure we
  1277. * rename it to "Headphone". We determine if something is a headphone
  1278. * similar to how udev determines form factor.
  1279. */
  1280. static void check_no_speaker_on_headset(struct snd_kcontrol *kctl,
  1281. struct snd_card *card)
  1282. {
  1283. const char *names_to_check[] = {
  1284. "Headset", "headset", "Headphone", "headphone", NULL};
  1285. const char **s;
  1286. bool found = false;
  1287. if (strcmp("Speaker", kctl->id.name))
  1288. return;
  1289. for (s = names_to_check; *s; s++)
  1290. if (strstr(card->shortname, *s)) {
  1291. found = true;
  1292. break;
  1293. }
  1294. if (!found)
  1295. return;
  1296. strlcpy(kctl->id.name, "Headphone", sizeof(kctl->id.name));
  1297. }
  1298. static struct usb_feature_control_info *get_feature_control_info(int control)
  1299. {
  1300. int i;
  1301. for (i = 0; i < ARRAY_SIZE(audio_feature_info); ++i) {
  1302. if (audio_feature_info[i].control == control)
  1303. return &audio_feature_info[i];
  1304. }
  1305. return NULL;
  1306. }
  1307. static void __build_feature_ctl(struct usb_mixer_interface *mixer,
  1308. const struct usbmix_name_map *imap,
  1309. unsigned int ctl_mask, int control,
  1310. struct usb_audio_term *iterm,
  1311. struct usb_audio_term *oterm,
  1312. int unitid, int nameid, int readonly_mask)
  1313. {
  1314. struct usb_feature_control_info *ctl_info;
  1315. unsigned int len = 0;
  1316. int mapped_name = 0;
  1317. struct snd_kcontrol *kctl;
  1318. struct usb_mixer_elem_info *cval;
  1319. const struct usbmix_name_map *map;
  1320. unsigned int range;
  1321. if (control == UAC_FU_GRAPHIC_EQUALIZER) {
  1322. /* FIXME: not supported yet */
  1323. return;
  1324. }
  1325. map = find_map(imap, unitid, control);
  1326. if (check_ignored_ctl(map))
  1327. return;
  1328. cval = kzalloc(sizeof(*cval), GFP_KERNEL);
  1329. if (!cval)
  1330. return;
  1331. snd_usb_mixer_elem_init_std(&cval->head, mixer, unitid);
  1332. cval->control = control;
  1333. cval->cmask = ctl_mask;
  1334. ctl_info = get_feature_control_info(control);
  1335. if (!ctl_info) {
  1336. kfree(cval);
  1337. return;
  1338. }
  1339. if (mixer->protocol == UAC_VERSION_1)
  1340. cval->val_type = ctl_info->type;
  1341. else /* UAC_VERSION_2 */
  1342. cval->val_type = ctl_info->type_uac2 >= 0 ?
  1343. ctl_info->type_uac2 : ctl_info->type;
  1344. if (ctl_mask == 0) {
  1345. cval->channels = 1; /* master channel */
  1346. cval->master_readonly = readonly_mask;
  1347. } else {
  1348. int i, c = 0;
  1349. for (i = 0; i < 16; i++)
  1350. if (ctl_mask & (1 << i))
  1351. c++;
  1352. cval->channels = c;
  1353. cval->ch_readonly = readonly_mask;
  1354. }
  1355. /*
  1356. * If all channels in the mask are marked read-only, make the control
  1357. * read-only. snd_usb_set_cur_mix_value() will check the mask again and won't
  1358. * issue write commands to read-only channels.
  1359. */
  1360. if (cval->channels == readonly_mask)
  1361. kctl = snd_ctl_new1(&usb_feature_unit_ctl_ro, cval);
  1362. else
  1363. kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
  1364. if (!kctl) {
  1365. usb_audio_err(mixer->chip, "cannot malloc kcontrol\n");
  1366. kfree(cval);
  1367. return;
  1368. }
  1369. kctl->private_free = snd_usb_mixer_elem_free;
  1370. len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
  1371. mapped_name = len != 0;
  1372. if (!len && nameid)
  1373. len = snd_usb_copy_string_desc(mixer->chip, nameid,
  1374. kctl->id.name, sizeof(kctl->id.name));
  1375. switch (control) {
  1376. case UAC_FU_MUTE:
  1377. case UAC_FU_VOLUME:
  1378. /*
  1379. * determine the control name. the rule is:
  1380. * - if a name id is given in descriptor, use it.
  1381. * - if the connected input can be determined, then use the name
  1382. * of terminal type.
  1383. * - if the connected output can be determined, use it.
  1384. * - otherwise, anonymous name.
  1385. */
  1386. if (!len) {
  1387. if (iterm)
  1388. len = get_term_name(mixer->chip, iterm,
  1389. kctl->id.name,
  1390. sizeof(kctl->id.name), 1);
  1391. if (!len && oterm)
  1392. len = get_term_name(mixer->chip, oterm,
  1393. kctl->id.name,
  1394. sizeof(kctl->id.name), 1);
  1395. if (!len)
  1396. snprintf(kctl->id.name, sizeof(kctl->id.name),
  1397. "Feature %d", unitid);
  1398. }
  1399. if (!mapped_name)
  1400. check_no_speaker_on_headset(kctl, mixer->chip->card);
  1401. /*
  1402. * determine the stream direction:
  1403. * if the connected output is USB stream, then it's likely a
  1404. * capture stream. otherwise it should be playback (hopefully :)
  1405. */
  1406. if (!mapped_name && oterm && !(oterm->type >> 16)) {
  1407. if ((oterm->type & 0xff00) == 0x0100)
  1408. append_ctl_name(kctl, " Capture");
  1409. else
  1410. append_ctl_name(kctl, " Playback");
  1411. }
  1412. append_ctl_name(kctl, control == UAC_FU_MUTE ?
  1413. " Switch" : " Volume");
  1414. break;
  1415. default:
  1416. if (!len)
  1417. strlcpy(kctl->id.name, audio_feature_info[control-1].name,
  1418. sizeof(kctl->id.name));
  1419. break;
  1420. }
  1421. /* get min/max values */
  1422. get_min_max_with_quirks(cval, 0, kctl);
  1423. if (control == UAC_FU_VOLUME) {
  1424. check_mapped_dB(map, cval);
  1425. if (cval->dBmin < cval->dBmax || !cval->initialized) {
  1426. kctl->tlv.c = snd_usb_mixer_vol_tlv;
  1427. kctl->vd[0].access |=
  1428. SNDRV_CTL_ELEM_ACCESS_TLV_READ |
  1429. SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
  1430. }
  1431. }
  1432. snd_usb_mixer_fu_apply_quirk(mixer, cval, unitid, kctl);
  1433. range = (cval->max - cval->min) / cval->res;
  1434. /*
  1435. * Are there devices with volume range more than 255? I use a bit more
  1436. * to be sure. 384 is a resolution magic number found on Logitech
  1437. * devices. It will definitively catch all buggy Logitech devices.
  1438. */
  1439. if (range > 384) {
  1440. usb_audio_warn(mixer->chip,
  1441. "Warning! Unlikely big volume range (=%u), cval->res is probably wrong.",
  1442. range);
  1443. usb_audio_warn(mixer->chip,
  1444. "[%d] FU [%s] ch = %d, val = %d/%d/%d",
  1445. cval->head.id, kctl->id.name, cval->channels,
  1446. cval->min, cval->max, cval->res);
  1447. }
  1448. usb_audio_dbg(mixer->chip, "[%d] FU [%s] ch = %d, val = %d/%d/%d\n",
  1449. cval->head.id, kctl->id.name, cval->channels,
  1450. cval->min, cval->max, cval->res);
  1451. snd_usb_mixer_add_control(&cval->head, kctl);
  1452. }
  1453. static void build_feature_ctl(struct mixer_build *state, void *raw_desc,
  1454. unsigned int ctl_mask, int control,
  1455. struct usb_audio_term *iterm, int unitid,
  1456. int readonly_mask)
  1457. {
  1458. struct uac_feature_unit_descriptor *desc = raw_desc;
  1459. int nameid = uac_feature_unit_iFeature(desc);
  1460. __build_feature_ctl(state->mixer, state->map, ctl_mask, control,
  1461. iterm, &state->oterm, unitid, nameid, readonly_mask);
  1462. }
  1463. static void build_feature_ctl_badd(struct usb_mixer_interface *mixer,
  1464. unsigned int ctl_mask, int control, int unitid,
  1465. const struct usbmix_name_map *badd_map)
  1466. {
  1467. __build_feature_ctl(mixer, badd_map, ctl_mask, control,
  1468. NULL, NULL, unitid, 0, 0);
  1469. }
  1470. static void get_connector_control_name(struct usb_mixer_interface *mixer,
  1471. struct usb_audio_term *term,
  1472. bool is_input, char *name, int name_size)
  1473. {
  1474. int name_len = get_term_name(mixer->chip, term, name, name_size, 0);
  1475. if (name_len == 0)
  1476. strlcpy(name, "Unknown", name_size);
  1477. /*
  1478. * sound/core/ctljack.c has a convention of naming jack controls
  1479. * by ending in " Jack". Make it slightly more useful by
  1480. * indicating Input or Output after the terminal name.
  1481. */
  1482. if (is_input)
  1483. strlcat(name, " - Input Jack", name_size);
  1484. else
  1485. strlcat(name, " - Output Jack", name_size);
  1486. }
  1487. /* Build a mixer control for a UAC connector control (jack-detect) */
  1488. static void build_connector_control(struct usb_mixer_interface *mixer,
  1489. struct usb_audio_term *term, bool is_input)
  1490. {
  1491. struct snd_kcontrol *kctl;
  1492. struct usb_mixer_elem_info *cval;
  1493. cval = kzalloc(sizeof(*cval), GFP_KERNEL);
  1494. if (!cval)
  1495. return;
  1496. snd_usb_mixer_elem_init_std(&cval->head, mixer, term->id);
  1497. /*
  1498. * UAC2: The first byte from reading the UAC2_TE_CONNECTOR control returns the
  1499. * number of channels connected.
  1500. *
  1501. * UAC3: The first byte specifies size of bitmap for the inserted controls. The
  1502. * following byte(s) specifies which connectors are inserted.
  1503. *
  1504. * This boolean ctl will simply report if any channels are connected
  1505. * or not.
  1506. */
  1507. if (mixer->protocol == UAC_VERSION_2)
  1508. cval->control = UAC2_TE_CONNECTOR;
  1509. else /* UAC_VERSION_3 */
  1510. cval->control = UAC3_TE_INSERTION;
  1511. cval->val_type = USB_MIXER_BOOLEAN;
  1512. cval->channels = 1; /* report true if any channel is connected */
  1513. cval->min = 0;
  1514. cval->max = 1;
  1515. kctl = snd_ctl_new1(&usb_connector_ctl_ro, cval);
  1516. if (!kctl) {
  1517. usb_audio_err(mixer->chip, "cannot malloc kcontrol\n");
  1518. kfree(cval);
  1519. return;
  1520. }
  1521. get_connector_control_name(mixer, term, is_input, kctl->id.name,
  1522. sizeof(kctl->id.name));
  1523. kctl->private_free = snd_usb_mixer_elem_free;
  1524. snd_usb_mixer_add_control(&cval->head, kctl);
  1525. }
  1526. static int parse_clock_source_unit(struct mixer_build *state, int unitid,
  1527. void *_ftr)
  1528. {
  1529. struct uac_clock_source_descriptor *hdr = _ftr;
  1530. struct usb_mixer_elem_info *cval;
  1531. struct snd_kcontrol *kctl;
  1532. char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
  1533. int ret;
  1534. if (state->mixer->protocol != UAC_VERSION_2)
  1535. return -EINVAL;
  1536. if (hdr->bLength != sizeof(*hdr)) {
  1537. usb_audio_dbg(state->chip,
  1538. "Bogus clock source descriptor length of %d, ignoring.\n",
  1539. hdr->bLength);
  1540. return 0;
  1541. }
  1542. /*
  1543. * The only property of this unit we are interested in is the
  1544. * clock source validity. If that isn't readable, just bail out.
  1545. */
  1546. if (!uac_v2v3_control_is_readable(hdr->bmControls,
  1547. UAC2_CS_CONTROL_CLOCK_VALID))
  1548. return 0;
  1549. cval = kzalloc(sizeof(*cval), GFP_KERNEL);
  1550. if (!cval)
  1551. return -ENOMEM;
  1552. snd_usb_mixer_elem_init_std(&cval->head, state->mixer, hdr->bClockID);
  1553. cval->min = 0;
  1554. cval->max = 1;
  1555. cval->channels = 1;
  1556. cval->val_type = USB_MIXER_BOOLEAN;
  1557. cval->control = UAC2_CS_CONTROL_CLOCK_VALID;
  1558. cval->master_readonly = 1;
  1559. /* From UAC2 5.2.5.1.2 "Only the get request is supported." */
  1560. kctl = snd_ctl_new1(&usb_bool_master_control_ctl_ro, cval);
  1561. if (!kctl) {
  1562. kfree(cval);
  1563. return -ENOMEM;
  1564. }
  1565. kctl->private_free = snd_usb_mixer_elem_free;
  1566. ret = snd_usb_copy_string_desc(state->chip, hdr->iClockSource,
  1567. name, sizeof(name));
  1568. if (ret > 0)
  1569. snprintf(kctl->id.name, sizeof(kctl->id.name),
  1570. "%s Validity", name);
  1571. else
  1572. snprintf(kctl->id.name, sizeof(kctl->id.name),
  1573. "Clock Source %d Validity", hdr->bClockID);
  1574. return snd_usb_mixer_add_control(&cval->head, kctl);
  1575. }
  1576. /*
  1577. * parse a feature unit
  1578. *
  1579. * most of controls are defined here.
  1580. */
  1581. static int parse_audio_feature_unit(struct mixer_build *state, int unitid,
  1582. void *_ftr)
  1583. {
  1584. int channels, i, j;
  1585. struct usb_audio_term iterm;
  1586. unsigned int master_bits, first_ch_bits;
  1587. int err, csize;
  1588. struct uac_feature_unit_descriptor *hdr = _ftr;
  1589. __u8 *bmaControls;
  1590. if (state->mixer->protocol == UAC_VERSION_1) {
  1591. if (hdr->bLength < 7) {
  1592. usb_audio_err(state->chip,
  1593. "unit %u: invalid UAC_FEATURE_UNIT descriptor\n",
  1594. unitid);
  1595. return -EINVAL;
  1596. }
  1597. csize = hdr->bControlSize;
  1598. if (!csize) {
  1599. usb_audio_dbg(state->chip,
  1600. "unit %u: invalid bControlSize == 0\n",
  1601. unitid);
  1602. return -EINVAL;
  1603. }
  1604. channels = (hdr->bLength - 7) / csize - 1;
  1605. bmaControls = hdr->bmaControls;
  1606. if (hdr->bLength < 7 + csize) {
  1607. usb_audio_err(state->chip,
  1608. "unit %u: invalid UAC_FEATURE_UNIT descriptor\n",
  1609. unitid);
  1610. return -EINVAL;
  1611. }
  1612. } else if (state->mixer->protocol == UAC_VERSION_2) {
  1613. struct uac2_feature_unit_descriptor *ftr = _ftr;
  1614. if (hdr->bLength < 6) {
  1615. usb_audio_err(state->chip,
  1616. "unit %u: invalid UAC_FEATURE_UNIT descriptor\n",
  1617. unitid);
  1618. return -EINVAL;
  1619. }
  1620. csize = 4;
  1621. channels = (hdr->bLength - 6) / 4 - 1;
  1622. bmaControls = ftr->bmaControls;
  1623. if (hdr->bLength < 6 + csize) {
  1624. usb_audio_err(state->chip,
  1625. "unit %u: invalid UAC_FEATURE_UNIT descriptor\n",
  1626. unitid);
  1627. return -EINVAL;
  1628. }
  1629. } else { /* UAC_VERSION_3 */
  1630. struct uac3_feature_unit_descriptor *ftr = _ftr;
  1631. if (hdr->bLength < 7) {
  1632. usb_audio_err(state->chip,
  1633. "unit %u: invalid UAC3_FEATURE_UNIT descriptor\n",
  1634. unitid);
  1635. return -EINVAL;
  1636. }
  1637. csize = 4;
  1638. channels = (ftr->bLength - 7) / 4 - 1;
  1639. bmaControls = ftr->bmaControls;
  1640. if (hdr->bLength < 7 + csize) {
  1641. usb_audio_err(state->chip,
  1642. "unit %u: invalid UAC3_FEATURE_UNIT descriptor\n",
  1643. unitid);
  1644. return -EINVAL;
  1645. }
  1646. }
  1647. /* parse the source unit */
  1648. err = parse_audio_unit(state, hdr->bSourceID);
  1649. if (err < 0)
  1650. return err;
  1651. /* determine the input source type and name */
  1652. err = check_input_term(state, hdr->bSourceID, &iterm);
  1653. if (err < 0)
  1654. return err;
  1655. master_bits = snd_usb_combine_bytes(bmaControls, csize);
  1656. /* master configuration quirks */
  1657. switch (state->chip->usb_id) {
  1658. case USB_ID(0x08bb, 0x2702):
  1659. usb_audio_info(state->chip,
  1660. "usbmixer: master volume quirk for PCM2702 chip\n");
  1661. /* disable non-functional volume control */
  1662. master_bits &= ~UAC_CONTROL_BIT(UAC_FU_VOLUME);
  1663. break;
  1664. case USB_ID(0x1130, 0xf211):
  1665. usb_audio_info(state->chip,
  1666. "usbmixer: volume control quirk for Tenx TP6911 Audio Headset\n");
  1667. /* disable non-functional volume control */
  1668. channels = 0;
  1669. break;
  1670. }
  1671. if (channels > 0)
  1672. first_ch_bits = snd_usb_combine_bytes(bmaControls + csize, csize);
  1673. else
  1674. first_ch_bits = 0;
  1675. if (state->mixer->protocol == UAC_VERSION_1) {
  1676. /* check all control types */
  1677. for (i = 0; i < 10; i++) {
  1678. unsigned int ch_bits = 0;
  1679. int control = audio_feature_info[i].control;
  1680. for (j = 0; j < channels; j++) {
  1681. unsigned int mask;
  1682. mask = snd_usb_combine_bytes(bmaControls +
  1683. csize * (j+1), csize);
  1684. if (mask & (1 << i))
  1685. ch_bits |= (1 << j);
  1686. }
  1687. /* audio class v1 controls are never read-only */
  1688. /*
  1689. * The first channel must be set
  1690. * (for ease of programming).
  1691. */
  1692. if (ch_bits & 1)
  1693. build_feature_ctl(state, _ftr, ch_bits, control,
  1694. &iterm, unitid, 0);
  1695. if (master_bits & (1 << i))
  1696. build_feature_ctl(state, _ftr, 0, control,
  1697. &iterm, unitid, 0);
  1698. }
  1699. } else { /* UAC_VERSION_2/3 */
  1700. for (i = 0; i < ARRAY_SIZE(audio_feature_info); i++) {
  1701. unsigned int ch_bits = 0;
  1702. unsigned int ch_read_only = 0;
  1703. int control = audio_feature_info[i].control;
  1704. for (j = 0; j < channels; j++) {
  1705. unsigned int mask;
  1706. mask = snd_usb_combine_bytes(bmaControls +
  1707. csize * (j+1), csize);
  1708. if (uac_v2v3_control_is_readable(mask, control)) {
  1709. ch_bits |= (1 << j);
  1710. if (!uac_v2v3_control_is_writeable(mask, control))
  1711. ch_read_only |= (1 << j);
  1712. }
  1713. }
  1714. /*
  1715. * NOTE: build_feature_ctl() will mark the control
  1716. * read-only if all channels are marked read-only in
  1717. * the descriptors. Otherwise, the control will be
  1718. * reported as writeable, but the driver will not
  1719. * actually issue a write command for read-only
  1720. * channels.
  1721. */
  1722. /*
  1723. * The first channel must be set
  1724. * (for ease of programming).
  1725. */
  1726. if (ch_bits & 1)
  1727. build_feature_ctl(state, _ftr, ch_bits, control,
  1728. &iterm, unitid, ch_read_only);
  1729. if (uac_v2v3_control_is_readable(master_bits, control))
  1730. build_feature_ctl(state, _ftr, 0, control,
  1731. &iterm, unitid,
  1732. !uac_v2v3_control_is_writeable(master_bits,
  1733. control));
  1734. }
  1735. }
  1736. return 0;
  1737. }
  1738. /*
  1739. * Mixer Unit
  1740. */
  1741. /*
  1742. * build a mixer unit control
  1743. *
  1744. * the callbacks are identical with feature unit.
  1745. * input channel number (zero based) is given in control field instead.
  1746. */
  1747. static void build_mixer_unit_ctl(struct mixer_build *state,
  1748. struct uac_mixer_unit_descriptor *desc,
  1749. int in_pin, int in_ch, int num_outs,
  1750. int unitid, struct usb_audio_term *iterm)
  1751. {
  1752. struct usb_mixer_elem_info *cval;
  1753. unsigned int i, len;
  1754. struct snd_kcontrol *kctl;
  1755. const struct usbmix_name_map *map;
  1756. map = find_map(state->map, unitid, 0);
  1757. if (check_ignored_ctl(map))
  1758. return;
  1759. cval = kzalloc(sizeof(*cval), GFP_KERNEL);
  1760. if (!cval)
  1761. return;
  1762. snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid);
  1763. cval->control = in_ch + 1; /* based on 1 */
  1764. cval->val_type = USB_MIXER_S16;
  1765. for (i = 0; i < num_outs; i++) {
  1766. __u8 *c = uac_mixer_unit_bmControls(desc, state->mixer->protocol);
  1767. if (check_matrix_bitmap(c, in_ch, i, num_outs)) {
  1768. cval->cmask |= (1 << i);
  1769. cval->channels++;
  1770. }
  1771. }
  1772. /* get min/max values */
  1773. get_min_max(cval, 0);
  1774. kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
  1775. if (!kctl) {
  1776. usb_audio_err(state->chip, "cannot malloc kcontrol\n");
  1777. kfree(cval);
  1778. return;
  1779. }
  1780. kctl->private_free = snd_usb_mixer_elem_free;
  1781. len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
  1782. if (!len)
  1783. len = get_term_name(state->chip, iterm, kctl->id.name,
  1784. sizeof(kctl->id.name), 0);
  1785. if (!len)
  1786. len = sprintf(kctl->id.name, "Mixer Source %d", in_ch + 1);
  1787. append_ctl_name(kctl, " Volume");
  1788. usb_audio_dbg(state->chip, "[%d] MU [%s] ch = %d, val = %d/%d\n",
  1789. cval->head.id, kctl->id.name, cval->channels, cval->min, cval->max);
  1790. snd_usb_mixer_add_control(&cval->head, kctl);
  1791. }
  1792. static int parse_audio_input_terminal(struct mixer_build *state, int unitid,
  1793. void *raw_desc)
  1794. {
  1795. struct usb_audio_term iterm;
  1796. unsigned int control, bmctls, term_id;
  1797. if (state->mixer->protocol == UAC_VERSION_2) {
  1798. struct uac2_input_terminal_descriptor *d_v2 = raw_desc;
  1799. control = UAC2_TE_CONNECTOR;
  1800. term_id = d_v2->bTerminalID;
  1801. bmctls = le16_to_cpu(d_v2->bmControls);
  1802. } else if (state->mixer->protocol == UAC_VERSION_3) {
  1803. struct uac3_input_terminal_descriptor *d_v3 = raw_desc;
  1804. control = UAC3_TE_INSERTION;
  1805. term_id = d_v3->bTerminalID;
  1806. bmctls = le32_to_cpu(d_v3->bmControls);
  1807. } else {
  1808. return 0; /* UAC1. No Insertion control */
  1809. }
  1810. check_input_term(state, term_id, &iterm);
  1811. /* Check for jack detection. */
  1812. if (uac_v2v3_control_is_readable(bmctls, control))
  1813. build_connector_control(state->mixer, &iterm, true);
  1814. return 0;
  1815. }
  1816. /*
  1817. * parse a mixer unit
  1818. */
  1819. static int parse_audio_mixer_unit(struct mixer_build *state, int unitid,
  1820. void *raw_desc)
  1821. {
  1822. struct uac_mixer_unit_descriptor *desc = raw_desc;
  1823. struct usb_audio_term iterm;
  1824. int input_pins, num_ins, num_outs;
  1825. int pin, ich, err;
  1826. err = uac_mixer_unit_get_channels(state, desc);
  1827. if (err < 0) {
  1828. usb_audio_err(state->chip,
  1829. "invalid MIXER UNIT descriptor %d\n",
  1830. unitid);
  1831. return err;
  1832. }
  1833. num_outs = err;
  1834. input_pins = desc->bNrInPins;
  1835. num_ins = 0;
  1836. ich = 0;
  1837. for (pin = 0; pin < input_pins; pin++) {
  1838. err = parse_audio_unit(state, desc->baSourceID[pin]);
  1839. if (err < 0)
  1840. continue;
  1841. /* no bmControls field (e.g. Maya44) -> ignore */
  1842. if (desc->bLength <= 10 + input_pins)
  1843. continue;
  1844. err = check_input_term(state, desc->baSourceID[pin], &iterm);
  1845. if (err < 0)
  1846. return err;
  1847. num_ins += iterm.channels;
  1848. for (; ich < num_ins; ich++) {
  1849. int och, ich_has_controls = 0;
  1850. for (och = 0; och < num_outs; och++) {
  1851. __u8 *c = uac_mixer_unit_bmControls(desc,
  1852. state->mixer->protocol);
  1853. if (check_matrix_bitmap(c, ich, och, num_outs)) {
  1854. ich_has_controls = 1;
  1855. break;
  1856. }
  1857. }
  1858. if (ich_has_controls)
  1859. build_mixer_unit_ctl(state, desc, pin, ich, num_outs,
  1860. unitid, &iterm);
  1861. }
  1862. }
  1863. return 0;
  1864. }
  1865. /*
  1866. * Processing Unit / Extension Unit
  1867. */
  1868. /* get callback for processing/extension unit */
  1869. static int mixer_ctl_procunit_get(struct snd_kcontrol *kcontrol,
  1870. struct snd_ctl_elem_value *ucontrol)
  1871. {
  1872. struct usb_mixer_elem_info *cval = kcontrol->private_data;
  1873. int err, val;
  1874. err = get_cur_ctl_value(cval, cval->control << 8, &val);
  1875. if (err < 0) {
  1876. ucontrol->value.integer.value[0] = cval->min;
  1877. return filter_error(cval, err);
  1878. }
  1879. val = get_relative_value(cval, val);
  1880. ucontrol->value.integer.value[0] = val;
  1881. return 0;
  1882. }
  1883. /* put callback for processing/extension unit */
  1884. static int mixer_ctl_procunit_put(struct snd_kcontrol *kcontrol,
  1885. struct snd_ctl_elem_value *ucontrol)
  1886. {
  1887. struct usb_mixer_elem_info *cval = kcontrol->private_data;
  1888. int val, oval, err;
  1889. err = get_cur_ctl_value(cval, cval->control << 8, &oval);
  1890. if (err < 0)
  1891. return filter_error(cval, err);
  1892. val = ucontrol->value.integer.value[0];
  1893. val = get_abs_value(cval, val);
  1894. if (val != oval) {
  1895. set_cur_ctl_value(cval, cval->control << 8, val);
  1896. return 1;
  1897. }
  1898. return 0;
  1899. }
  1900. /* alsa control interface for processing/extension unit */
  1901. static const struct snd_kcontrol_new mixer_procunit_ctl = {
  1902. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1903. .name = "", /* will be filled later */
  1904. .info = mixer_ctl_feature_info,
  1905. .get = mixer_ctl_procunit_get,
  1906. .put = mixer_ctl_procunit_put,
  1907. };
  1908. /*
  1909. * predefined data for processing units
  1910. */
  1911. struct procunit_value_info {
  1912. int control;
  1913. char *suffix;
  1914. int val_type;
  1915. int min_value;
  1916. };
  1917. struct procunit_info {
  1918. int type;
  1919. char *name;
  1920. struct procunit_value_info *values;
  1921. };
  1922. static struct procunit_value_info updown_proc_info[] = {
  1923. { UAC_UD_ENABLE, "Switch", USB_MIXER_BOOLEAN },
  1924. { UAC_UD_MODE_SELECT, "Mode Select", USB_MIXER_U8, 1 },
  1925. { 0 }
  1926. };
  1927. static struct procunit_value_info prologic_proc_info[] = {
  1928. { UAC_DP_ENABLE, "Switch", USB_MIXER_BOOLEAN },
  1929. { UAC_DP_MODE_SELECT, "Mode Select", USB_MIXER_U8, 1 },
  1930. { 0 }
  1931. };
  1932. static struct procunit_value_info threed_enh_proc_info[] = {
  1933. { UAC_3D_ENABLE, "Switch", USB_MIXER_BOOLEAN },
  1934. { UAC_3D_SPACE, "Spaciousness", USB_MIXER_U8 },
  1935. { 0 }
  1936. };
  1937. static struct procunit_value_info reverb_proc_info[] = {
  1938. { UAC_REVERB_ENABLE, "Switch", USB_MIXER_BOOLEAN },
  1939. { UAC_REVERB_LEVEL, "Level", USB_MIXER_U8 },
  1940. { UAC_REVERB_TIME, "Time", USB_MIXER_U16 },
  1941. { UAC_REVERB_FEEDBACK, "Feedback", USB_MIXER_U8 },
  1942. { 0 }
  1943. };
  1944. static struct procunit_value_info chorus_proc_info[] = {
  1945. { UAC_CHORUS_ENABLE, "Switch", USB_MIXER_BOOLEAN },
  1946. { UAC_CHORUS_LEVEL, "Level", USB_MIXER_U8 },
  1947. { UAC_CHORUS_RATE, "Rate", USB_MIXER_U16 },
  1948. { UAC_CHORUS_DEPTH, "Depth", USB_MIXER_U16 },
  1949. { 0 }
  1950. };
  1951. static struct procunit_value_info dcr_proc_info[] = {
  1952. { UAC_DCR_ENABLE, "Switch", USB_MIXER_BOOLEAN },
  1953. { UAC_DCR_RATE, "Ratio", USB_MIXER_U16 },
  1954. { UAC_DCR_MAXAMPL, "Max Amp", USB_MIXER_S16 },
  1955. { UAC_DCR_THRESHOLD, "Threshold", USB_MIXER_S16 },
  1956. { UAC_DCR_ATTACK_TIME, "Attack Time", USB_MIXER_U16 },
  1957. { UAC_DCR_RELEASE_TIME, "Release Time", USB_MIXER_U16 },
  1958. { 0 }
  1959. };
  1960. static struct procunit_info procunits[] = {
  1961. { UAC_PROCESS_UP_DOWNMIX, "Up Down", updown_proc_info },
  1962. { UAC_PROCESS_DOLBY_PROLOGIC, "Dolby Prologic", prologic_proc_info },
  1963. { UAC_PROCESS_STEREO_EXTENDER, "3D Stereo Extender", threed_enh_proc_info },
  1964. { UAC_PROCESS_REVERB, "Reverb", reverb_proc_info },
  1965. { UAC_PROCESS_CHORUS, "Chorus", chorus_proc_info },
  1966. { UAC_PROCESS_DYN_RANGE_COMP, "DCR", dcr_proc_info },
  1967. { 0 },
  1968. };
  1969. /*
  1970. * predefined data for extension units
  1971. */
  1972. static struct procunit_value_info clock_rate_xu_info[] = {
  1973. { USB_XU_CLOCK_RATE_SELECTOR, "Selector", USB_MIXER_U8, 0 },
  1974. { 0 }
  1975. };
  1976. static struct procunit_value_info clock_source_xu_info[] = {
  1977. { USB_XU_CLOCK_SOURCE_SELECTOR, "External", USB_MIXER_BOOLEAN },
  1978. { 0 }
  1979. };
  1980. static struct procunit_value_info spdif_format_xu_info[] = {
  1981. { USB_XU_DIGITAL_FORMAT_SELECTOR, "SPDIF/AC3", USB_MIXER_BOOLEAN },
  1982. { 0 }
  1983. };
  1984. static struct procunit_value_info soft_limit_xu_info[] = {
  1985. { USB_XU_SOFT_LIMIT_SELECTOR, " ", USB_MIXER_BOOLEAN },
  1986. { 0 }
  1987. };
  1988. static struct procunit_info extunits[] = {
  1989. { USB_XU_CLOCK_RATE, "Clock rate", clock_rate_xu_info },
  1990. { USB_XU_CLOCK_SOURCE, "DigitalIn CLK source", clock_source_xu_info },
  1991. { USB_XU_DIGITAL_IO_STATUS, "DigitalOut format:", spdif_format_xu_info },
  1992. { USB_XU_DEVICE_OPTIONS, "AnalogueIn Soft Limit", soft_limit_xu_info },
  1993. { 0 }
  1994. };
  1995. /*
  1996. * build a processing/extension unit
  1997. */
  1998. static int build_audio_procunit(struct mixer_build *state, int unitid,
  1999. void *raw_desc, struct procunit_info *list,
  2000. char *name)
  2001. {
  2002. struct uac_processing_unit_descriptor *desc = raw_desc;
  2003. int num_ins = desc->bNrInPins;
  2004. struct usb_mixer_elem_info *cval;
  2005. struct snd_kcontrol *kctl;
  2006. int i, err, nameid, type, len;
  2007. struct procunit_info *info;
  2008. struct procunit_value_info *valinfo;
  2009. const struct usbmix_name_map *map;
  2010. static struct procunit_value_info default_value_info[] = {
  2011. { 0x01, "Switch", USB_MIXER_BOOLEAN },
  2012. { 0 }
  2013. };
  2014. static struct procunit_info default_info = {
  2015. 0, NULL, default_value_info
  2016. };
  2017. if (desc->bLength < 13 || desc->bLength < 13 + num_ins ||
  2018. desc->bLength < num_ins + uac_processing_unit_bControlSize(desc, state->mixer->protocol)) {
  2019. usb_audio_err(state->chip, "invalid %s descriptor (id %d)\n", name, unitid);
  2020. return -EINVAL;
  2021. }
  2022. for (i = 0; i < num_ins; i++) {
  2023. err = parse_audio_unit(state, desc->baSourceID[i]);
  2024. if (err < 0)
  2025. return err;
  2026. }
  2027. type = le16_to_cpu(desc->wProcessType);
  2028. for (info = list; info && info->type; info++)
  2029. if (info->type == type)
  2030. break;
  2031. if (!info || !info->type)
  2032. info = &default_info;
  2033. for (valinfo = info->values; valinfo->control; valinfo++) {
  2034. __u8 *controls = uac_processing_unit_bmControls(desc, state->mixer->protocol);
  2035. if (!(controls[valinfo->control / 8] & (1 << ((valinfo->control % 8) - 1))))
  2036. continue;
  2037. map = find_map(state->map, unitid, valinfo->control);
  2038. if (check_ignored_ctl(map))
  2039. continue;
  2040. cval = kzalloc(sizeof(*cval), GFP_KERNEL);
  2041. if (!cval)
  2042. return -ENOMEM;
  2043. snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid);
  2044. cval->control = valinfo->control;
  2045. cval->val_type = valinfo->val_type;
  2046. cval->channels = 1;
  2047. /* get min/max values */
  2048. if (type == UAC_PROCESS_UP_DOWNMIX && cval->control == UAC_UD_MODE_SELECT) {
  2049. __u8 *control_spec = uac_processing_unit_specific(desc, state->mixer->protocol);
  2050. /* FIXME: hard-coded */
  2051. cval->min = 1;
  2052. cval->max = control_spec[0];
  2053. cval->res = 1;
  2054. cval->initialized = 1;
  2055. } else {
  2056. if (type == USB_XU_CLOCK_RATE) {
  2057. /*
  2058. * E-Mu USB 0404/0202/TrackerPre/0204
  2059. * samplerate control quirk
  2060. */
  2061. cval->min = 0;
  2062. cval->max = 5;
  2063. cval->res = 1;
  2064. cval->initialized = 1;
  2065. } else
  2066. get_min_max(cval, valinfo->min_value);
  2067. }
  2068. kctl = snd_ctl_new1(&mixer_procunit_ctl, cval);
  2069. if (!kctl) {
  2070. kfree(cval);
  2071. return -ENOMEM;
  2072. }
  2073. kctl->private_free = snd_usb_mixer_elem_free;
  2074. if (check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name))) {
  2075. /* nothing */ ;
  2076. } else if (info->name) {
  2077. strlcpy(kctl->id.name, info->name, sizeof(kctl->id.name));
  2078. } else {
  2079. nameid = uac_processing_unit_iProcessing(desc, state->mixer->protocol);
  2080. len = 0;
  2081. if (nameid)
  2082. len = snd_usb_copy_string_desc(state->chip,
  2083. nameid,
  2084. kctl->id.name,
  2085. sizeof(kctl->id.name));
  2086. if (!len)
  2087. strlcpy(kctl->id.name, name, sizeof(kctl->id.name));
  2088. }
  2089. append_ctl_name(kctl, " ");
  2090. append_ctl_name(kctl, valinfo->suffix);
  2091. usb_audio_dbg(state->chip,
  2092. "[%d] PU [%s] ch = %d, val = %d/%d\n",
  2093. cval->head.id, kctl->id.name, cval->channels,
  2094. cval->min, cval->max);
  2095. err = snd_usb_mixer_add_control(&cval->head, kctl);
  2096. if (err < 0)
  2097. return err;
  2098. }
  2099. return 0;
  2100. }
  2101. static int parse_audio_processing_unit(struct mixer_build *state, int unitid,
  2102. void *raw_desc)
  2103. {
  2104. return build_audio_procunit(state, unitid, raw_desc,
  2105. procunits, "Processing Unit");
  2106. }
  2107. static int parse_audio_extension_unit(struct mixer_build *state, int unitid,
  2108. void *raw_desc)
  2109. {
  2110. /*
  2111. * Note that we parse extension units with processing unit descriptors.
  2112. * That's ok as the layout is the same.
  2113. */
  2114. return build_audio_procunit(state, unitid, raw_desc,
  2115. extunits, "Extension Unit");
  2116. }
  2117. /*
  2118. * Selector Unit
  2119. */
  2120. /*
  2121. * info callback for selector unit
  2122. * use an enumerator type for routing
  2123. */
  2124. static int mixer_ctl_selector_info(struct snd_kcontrol *kcontrol,
  2125. struct snd_ctl_elem_info *uinfo)
  2126. {
  2127. struct usb_mixer_elem_info *cval = kcontrol->private_data;
  2128. const char **itemlist = (const char **)kcontrol->private_value;
  2129. if (snd_BUG_ON(!itemlist))
  2130. return -EINVAL;
  2131. return snd_ctl_enum_info(uinfo, 1, cval->max, itemlist);
  2132. }
  2133. /* get callback for selector unit */
  2134. static int mixer_ctl_selector_get(struct snd_kcontrol *kcontrol,
  2135. struct snd_ctl_elem_value *ucontrol)
  2136. {
  2137. struct usb_mixer_elem_info *cval = kcontrol->private_data;
  2138. int val, err;
  2139. err = get_cur_ctl_value(cval, cval->control << 8, &val);
  2140. if (err < 0) {
  2141. ucontrol->value.enumerated.item[0] = 0;
  2142. return filter_error(cval, err);
  2143. }
  2144. val = get_relative_value(cval, val);
  2145. ucontrol->value.enumerated.item[0] = val;
  2146. return 0;
  2147. }
  2148. /* put callback for selector unit */
  2149. static int mixer_ctl_selector_put(struct snd_kcontrol *kcontrol,
  2150. struct snd_ctl_elem_value *ucontrol)
  2151. {
  2152. struct usb_mixer_elem_info *cval = kcontrol->private_data;
  2153. int val, oval, err;
  2154. err = get_cur_ctl_value(cval, cval->control << 8, &oval);
  2155. if (err < 0)
  2156. return filter_error(cval, err);
  2157. val = ucontrol->value.enumerated.item[0];
  2158. val = get_abs_value(cval, val);
  2159. if (val != oval) {
  2160. set_cur_ctl_value(cval, cval->control << 8, val);
  2161. return 1;
  2162. }
  2163. return 0;
  2164. }
  2165. /* alsa control interface for selector unit */
  2166. static const struct snd_kcontrol_new mixer_selectunit_ctl = {
  2167. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2168. .name = "", /* will be filled later */
  2169. .info = mixer_ctl_selector_info,
  2170. .get = mixer_ctl_selector_get,
  2171. .put = mixer_ctl_selector_put,
  2172. };
  2173. /*
  2174. * private free callback.
  2175. * free both private_data and private_value
  2176. */
  2177. static void usb_mixer_selector_elem_free(struct snd_kcontrol *kctl)
  2178. {
  2179. int i, num_ins = 0;
  2180. if (kctl->private_data) {
  2181. struct usb_mixer_elem_info *cval = kctl->private_data;
  2182. num_ins = cval->max;
  2183. kfree(cval);
  2184. kctl->private_data = NULL;
  2185. }
  2186. if (kctl->private_value) {
  2187. char **itemlist = (char **)kctl->private_value;
  2188. for (i = 0; i < num_ins; i++)
  2189. kfree(itemlist[i]);
  2190. kfree(itemlist);
  2191. kctl->private_value = 0;
  2192. }
  2193. }
  2194. /*
  2195. * parse a selector unit
  2196. */
  2197. static int parse_audio_selector_unit(struct mixer_build *state, int unitid,
  2198. void *raw_desc)
  2199. {
  2200. struct uac_selector_unit_descriptor *desc = raw_desc;
  2201. unsigned int i, nameid, len;
  2202. int err;
  2203. struct usb_mixer_elem_info *cval;
  2204. struct snd_kcontrol *kctl;
  2205. const struct usbmix_name_map *map;
  2206. char **namelist;
  2207. if (desc->bLength < 5 || !desc->bNrInPins ||
  2208. desc->bLength < 5 + desc->bNrInPins) {
  2209. usb_audio_err(state->chip,
  2210. "invalid SELECTOR UNIT descriptor %d\n", unitid);
  2211. return -EINVAL;
  2212. }
  2213. for (i = 0; i < desc->bNrInPins; i++) {
  2214. err = parse_audio_unit(state, desc->baSourceID[i]);
  2215. if (err < 0)
  2216. return err;
  2217. }
  2218. if (desc->bNrInPins == 1) /* only one ? nonsense! */
  2219. return 0;
  2220. map = find_map(state->map, unitid, 0);
  2221. if (check_ignored_ctl(map))
  2222. return 0;
  2223. cval = kzalloc(sizeof(*cval), GFP_KERNEL);
  2224. if (!cval)
  2225. return -ENOMEM;
  2226. snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid);
  2227. cval->val_type = USB_MIXER_U8;
  2228. cval->channels = 1;
  2229. cval->min = 1;
  2230. cval->max = desc->bNrInPins;
  2231. cval->res = 1;
  2232. cval->initialized = 1;
  2233. if (state->mixer->protocol == UAC_VERSION_1)
  2234. cval->control = 0;
  2235. else /* UAC_VERSION_2 */
  2236. cval->control = (desc->bDescriptorSubtype == UAC2_CLOCK_SELECTOR) ?
  2237. UAC2_CX_CLOCK_SELECTOR : UAC2_SU_SELECTOR;
  2238. namelist = kmalloc_array(desc->bNrInPins, sizeof(char *), GFP_KERNEL);
  2239. if (!namelist) {
  2240. kfree(cval);
  2241. return -ENOMEM;
  2242. }
  2243. #define MAX_ITEM_NAME_LEN 64
  2244. for (i = 0; i < desc->bNrInPins; i++) {
  2245. struct usb_audio_term iterm;
  2246. len = 0;
  2247. namelist[i] = kmalloc(MAX_ITEM_NAME_LEN, GFP_KERNEL);
  2248. if (!namelist[i]) {
  2249. while (i--)
  2250. kfree(namelist[i]);
  2251. kfree(namelist);
  2252. kfree(cval);
  2253. return -ENOMEM;
  2254. }
  2255. len = check_mapped_selector_name(state, unitid, i, namelist[i],
  2256. MAX_ITEM_NAME_LEN);
  2257. if (! len && check_input_term(state, desc->baSourceID[i], &iterm) >= 0)
  2258. len = get_term_name(state->chip, &iterm, namelist[i],
  2259. MAX_ITEM_NAME_LEN, 0);
  2260. if (! len)
  2261. sprintf(namelist[i], "Input %u", i);
  2262. }
  2263. kctl = snd_ctl_new1(&mixer_selectunit_ctl, cval);
  2264. if (! kctl) {
  2265. usb_audio_err(state->chip, "cannot malloc kcontrol\n");
  2266. kfree(namelist);
  2267. kfree(cval);
  2268. return -ENOMEM;
  2269. }
  2270. kctl->private_value = (unsigned long)namelist;
  2271. kctl->private_free = usb_mixer_selector_elem_free;
  2272. /* check the static mapping table at first */
  2273. len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
  2274. if (!len) {
  2275. /* no mapping ? */
  2276. /* if iSelector is given, use it */
  2277. nameid = uac_selector_unit_iSelector(desc);
  2278. if (nameid)
  2279. len = snd_usb_copy_string_desc(state->chip, nameid,
  2280. kctl->id.name,
  2281. sizeof(kctl->id.name));
  2282. /* ... or pick up the terminal name at next */
  2283. if (!len)
  2284. len = get_term_name(state->chip, &state->oterm,
  2285. kctl->id.name, sizeof(kctl->id.name), 0);
  2286. /* ... or use the fixed string "USB" as the last resort */
  2287. if (!len)
  2288. strlcpy(kctl->id.name, "USB", sizeof(kctl->id.name));
  2289. /* and add the proper suffix */
  2290. if (desc->bDescriptorSubtype == UAC2_CLOCK_SELECTOR)
  2291. append_ctl_name(kctl, " Clock Source");
  2292. else if ((state->oterm.type & 0xff00) == 0x0100)
  2293. append_ctl_name(kctl, " Capture Source");
  2294. else
  2295. append_ctl_name(kctl, " Playback Source");
  2296. }
  2297. usb_audio_dbg(state->chip, "[%d] SU [%s] items = %d\n",
  2298. cval->head.id, kctl->id.name, desc->bNrInPins);
  2299. return snd_usb_mixer_add_control(&cval->head, kctl);
  2300. }
  2301. /*
  2302. * parse an audio unit recursively
  2303. */
  2304. static int parse_audio_unit(struct mixer_build *state, int unitid)
  2305. {
  2306. unsigned char *p1;
  2307. int protocol = state->mixer->protocol;
  2308. if (test_and_set_bit(unitid, state->unitbitmap))
  2309. return 0; /* the unit already visited */
  2310. p1 = find_audio_control_unit(state, unitid);
  2311. if (!p1) {
  2312. usb_audio_err(state->chip, "unit %d not found!\n", unitid);
  2313. return -EINVAL;
  2314. }
  2315. if (protocol == UAC_VERSION_1 || protocol == UAC_VERSION_2) {
  2316. switch (p1[2]) {
  2317. case UAC_INPUT_TERMINAL:
  2318. return parse_audio_input_terminal(state, unitid, p1);
  2319. case UAC_MIXER_UNIT:
  2320. return parse_audio_mixer_unit(state, unitid, p1);
  2321. case UAC2_CLOCK_SOURCE:
  2322. return parse_clock_source_unit(state, unitid, p1);
  2323. case UAC_SELECTOR_UNIT:
  2324. case UAC2_CLOCK_SELECTOR:
  2325. return parse_audio_selector_unit(state, unitid, p1);
  2326. case UAC_FEATURE_UNIT:
  2327. return parse_audio_feature_unit(state, unitid, p1);
  2328. case UAC1_PROCESSING_UNIT:
  2329. /* UAC2_EFFECT_UNIT has the same value */
  2330. if (protocol == UAC_VERSION_1)
  2331. return parse_audio_processing_unit(state, unitid, p1);
  2332. else
  2333. return 0; /* FIXME - effect units not implemented yet */
  2334. case UAC1_EXTENSION_UNIT:
  2335. /* UAC2_PROCESSING_UNIT_V2 has the same value */
  2336. if (protocol == UAC_VERSION_1)
  2337. return parse_audio_extension_unit(state, unitid, p1);
  2338. else /* UAC_VERSION_2 */
  2339. return parse_audio_processing_unit(state, unitid, p1);
  2340. case UAC2_EXTENSION_UNIT_V2:
  2341. return parse_audio_extension_unit(state, unitid, p1);
  2342. default:
  2343. usb_audio_err(state->chip,
  2344. "unit %u: unexpected type 0x%02x\n", unitid, p1[2]);
  2345. return -EINVAL;
  2346. }
  2347. } else { /* UAC_VERSION_3 */
  2348. switch (p1[2]) {
  2349. case UAC_INPUT_TERMINAL:
  2350. return parse_audio_input_terminal(state, unitid, p1);
  2351. case UAC3_MIXER_UNIT:
  2352. return parse_audio_mixer_unit(state, unitid, p1);
  2353. case UAC3_CLOCK_SOURCE:
  2354. return parse_clock_source_unit(state, unitid, p1);
  2355. case UAC3_CLOCK_SELECTOR:
  2356. return parse_audio_selector_unit(state, unitid, p1);
  2357. case UAC3_FEATURE_UNIT:
  2358. return parse_audio_feature_unit(state, unitid, p1);
  2359. case UAC3_EFFECT_UNIT:
  2360. return 0; /* FIXME - effect units not implemented yet */
  2361. case UAC3_PROCESSING_UNIT:
  2362. return parse_audio_processing_unit(state, unitid, p1);
  2363. case UAC3_EXTENSION_UNIT:
  2364. return parse_audio_extension_unit(state, unitid, p1);
  2365. default:
  2366. usb_audio_err(state->chip,
  2367. "unit %u: unexpected type 0x%02x\n", unitid, p1[2]);
  2368. return -EINVAL;
  2369. }
  2370. }
  2371. }
  2372. static void snd_usb_mixer_free(struct usb_mixer_interface *mixer)
  2373. {
  2374. /* kill pending URBs */
  2375. snd_usb_mixer_disconnect(mixer);
  2376. kfree(mixer->id_elems);
  2377. if (mixer->urb) {
  2378. kfree(mixer->urb->transfer_buffer);
  2379. usb_free_urb(mixer->urb);
  2380. }
  2381. usb_free_urb(mixer->rc_urb);
  2382. kfree(mixer->rc_setup_packet);
  2383. kfree(mixer);
  2384. }
  2385. static int snd_usb_mixer_dev_free(struct snd_device *device)
  2386. {
  2387. struct usb_mixer_interface *mixer = device->device_data;
  2388. snd_usb_mixer_free(mixer);
  2389. return 0;
  2390. }
  2391. /* UAC3 predefined channels configuration */
  2392. struct uac3_badd_profile {
  2393. int subclass;
  2394. const char *name;
  2395. int c_chmask; /* capture channels mask */
  2396. int p_chmask; /* playback channels mask */
  2397. int st_chmask; /* side tone mixing channel mask */
  2398. };
  2399. static struct uac3_badd_profile uac3_badd_profiles[] = {
  2400. {
  2401. /*
  2402. * BAIF, BAOF or combination of both
  2403. * IN: Mono or Stereo cfg, Mono alt possible
  2404. * OUT: Mono or Stereo cfg, Mono alt possible
  2405. */
  2406. .subclass = UAC3_FUNCTION_SUBCLASS_GENERIC_IO,
  2407. .name = "GENERIC IO",
  2408. .c_chmask = -1, /* dynamic channels */
  2409. .p_chmask = -1, /* dynamic channels */
  2410. },
  2411. {
  2412. /* BAOF; Stereo only cfg, Mono alt possible */
  2413. .subclass = UAC3_FUNCTION_SUBCLASS_HEADPHONE,
  2414. .name = "HEADPHONE",
  2415. .p_chmask = 3,
  2416. },
  2417. {
  2418. /* BAOF; Mono or Stereo cfg, Mono alt possible */
  2419. .subclass = UAC3_FUNCTION_SUBCLASS_SPEAKER,
  2420. .name = "SPEAKER",
  2421. .p_chmask = -1, /* dynamic channels */
  2422. },
  2423. {
  2424. /* BAIF; Mono or Stereo cfg, Mono alt possible */
  2425. .subclass = UAC3_FUNCTION_SUBCLASS_MICROPHONE,
  2426. .name = "MICROPHONE",
  2427. .c_chmask = -1, /* dynamic channels */
  2428. },
  2429. {
  2430. /*
  2431. * BAIOF topology
  2432. * IN: Mono only
  2433. * OUT: Mono or Stereo cfg, Mono alt possible
  2434. */
  2435. .subclass = UAC3_FUNCTION_SUBCLASS_HEADSET,
  2436. .name = "HEADSET",
  2437. .c_chmask = 1,
  2438. .p_chmask = -1, /* dynamic channels */
  2439. .st_chmask = 1,
  2440. },
  2441. {
  2442. /* BAIOF; IN: Mono only; OUT: Stereo only, Mono alt possible */
  2443. .subclass = UAC3_FUNCTION_SUBCLASS_HEADSET_ADAPTER,
  2444. .name = "HEADSET ADAPTER",
  2445. .c_chmask = 1,
  2446. .p_chmask = 3,
  2447. .st_chmask = 1,
  2448. },
  2449. {
  2450. /* BAIF + BAOF; IN: Mono only; OUT: Mono only */
  2451. .subclass = UAC3_FUNCTION_SUBCLASS_SPEAKERPHONE,
  2452. .name = "SPEAKERPHONE",
  2453. .c_chmask = 1,
  2454. .p_chmask = 1,
  2455. },
  2456. { 0 } /* terminator */
  2457. };
  2458. static bool uac3_badd_func_has_valid_channels(struct usb_mixer_interface *mixer,
  2459. struct uac3_badd_profile *f,
  2460. int c_chmask, int p_chmask)
  2461. {
  2462. /*
  2463. * If both playback/capture channels are dynamic, make sure
  2464. * at least one channel is present
  2465. */
  2466. if (f->c_chmask < 0 && f->p_chmask < 0) {
  2467. if (!c_chmask && !p_chmask) {
  2468. usb_audio_warn(mixer->chip, "BAAD %s: no channels?",
  2469. f->name);
  2470. return false;
  2471. }
  2472. return true;
  2473. }
  2474. if ((f->c_chmask < 0 && !c_chmask) ||
  2475. (f->c_chmask >= 0 && f->c_chmask != c_chmask)) {
  2476. usb_audio_warn(mixer->chip, "BAAD %s c_chmask mismatch",
  2477. f->name);
  2478. return false;
  2479. }
  2480. if ((f->p_chmask < 0 && !p_chmask) ||
  2481. (f->p_chmask >= 0 && f->p_chmask != p_chmask)) {
  2482. usb_audio_warn(mixer->chip, "BAAD %s p_chmask mismatch",
  2483. f->name);
  2484. return false;
  2485. }
  2486. return true;
  2487. }
  2488. /*
  2489. * create mixer controls for UAC3 BADD profiles
  2490. *
  2491. * UAC3 BADD device doesn't contain CS descriptors thus we will guess everything
  2492. *
  2493. * BADD device may contain Mixer Unit, which doesn't have any controls, skip it
  2494. */
  2495. static int snd_usb_mixer_controls_badd(struct usb_mixer_interface *mixer,
  2496. int ctrlif)
  2497. {
  2498. struct usb_device *dev = mixer->chip->dev;
  2499. struct usb_interface_assoc_descriptor *assoc;
  2500. int badd_profile = mixer->chip->badd_profile;
  2501. struct uac3_badd_profile *f;
  2502. const struct usbmix_ctl_map *map;
  2503. int p_chmask = 0, c_chmask = 0, st_chmask = 0;
  2504. int i;
  2505. assoc = usb_ifnum_to_if(dev, ctrlif)->intf_assoc;
  2506. /* Detect BADD capture/playback channels from AS EP descriptors */
  2507. for (i = 0; i < assoc->bInterfaceCount; i++) {
  2508. int intf = assoc->bFirstInterface + i;
  2509. struct usb_interface *iface;
  2510. struct usb_host_interface *alts;
  2511. struct usb_interface_descriptor *altsd;
  2512. unsigned int maxpacksize;
  2513. char dir_in;
  2514. int chmask, num;
  2515. if (intf == ctrlif)
  2516. continue;
  2517. iface = usb_ifnum_to_if(dev, intf);
  2518. num = iface->num_altsetting;
  2519. if (num < 2)
  2520. return -EINVAL;
  2521. /*
  2522. * The number of Channels in an AudioStreaming interface
  2523. * and the audio sample bit resolution (16 bits or 24
  2524. * bits) can be derived from the wMaxPacketSize field in
  2525. * the Standard AS Audio Data Endpoint descriptor in
  2526. * Alternate Setting 1
  2527. */
  2528. alts = &iface->altsetting[1];
  2529. altsd = get_iface_desc(alts);
  2530. if (altsd->bNumEndpoints < 1)
  2531. return -EINVAL;
  2532. /* check direction */
  2533. dir_in = (get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN);
  2534. maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
  2535. switch (maxpacksize) {
  2536. default:
  2537. usb_audio_err(mixer->chip,
  2538. "incorrect wMaxPacketSize 0x%x for BADD profile\n",
  2539. maxpacksize);
  2540. return -EINVAL;
  2541. case UAC3_BADD_EP_MAXPSIZE_SYNC_MONO_16:
  2542. case UAC3_BADD_EP_MAXPSIZE_ASYNC_MONO_16:
  2543. case UAC3_BADD_EP_MAXPSIZE_SYNC_MONO_24:
  2544. case UAC3_BADD_EP_MAXPSIZE_ASYNC_MONO_24:
  2545. chmask = 1;
  2546. break;
  2547. case UAC3_BADD_EP_MAXPSIZE_SYNC_STEREO_16:
  2548. case UAC3_BADD_EP_MAXPSIZE_ASYNC_STEREO_16:
  2549. case UAC3_BADD_EP_MAXPSIZE_SYNC_STEREO_24:
  2550. case UAC3_BADD_EP_MAXPSIZE_ASYNC_STEREO_24:
  2551. chmask = 3;
  2552. break;
  2553. }
  2554. if (dir_in)
  2555. c_chmask = chmask;
  2556. else
  2557. p_chmask = chmask;
  2558. }
  2559. usb_audio_dbg(mixer->chip,
  2560. "UAC3 BADD profile 0x%x: detected c_chmask=%d p_chmask=%d\n",
  2561. badd_profile, c_chmask, p_chmask);
  2562. /* check the mapping table */
  2563. for (map = uac3_badd_usbmix_ctl_maps; map->id; map++) {
  2564. if (map->id == badd_profile)
  2565. break;
  2566. }
  2567. if (!map->id)
  2568. return -EINVAL;
  2569. for (f = uac3_badd_profiles; f->name; f++) {
  2570. if (badd_profile == f->subclass)
  2571. break;
  2572. }
  2573. if (!f->name)
  2574. return -EINVAL;
  2575. if (!uac3_badd_func_has_valid_channels(mixer, f, c_chmask, p_chmask))
  2576. return -EINVAL;
  2577. st_chmask = f->st_chmask;
  2578. /* Playback */
  2579. if (p_chmask) {
  2580. /* Master channel, always writable */
  2581. build_feature_ctl_badd(mixer, 0, UAC_FU_MUTE,
  2582. UAC3_BADD_FU_ID2, map->map);
  2583. /* Mono/Stereo volume channels, always writable */
  2584. build_feature_ctl_badd(mixer, p_chmask, UAC_FU_VOLUME,
  2585. UAC3_BADD_FU_ID2, map->map);
  2586. }
  2587. /* Capture */
  2588. if (c_chmask) {
  2589. /* Master channel, always writable */
  2590. build_feature_ctl_badd(mixer, 0, UAC_FU_MUTE,
  2591. UAC3_BADD_FU_ID5, map->map);
  2592. /* Mono/Stereo volume channels, always writable */
  2593. build_feature_ctl_badd(mixer, c_chmask, UAC_FU_VOLUME,
  2594. UAC3_BADD_FU_ID5, map->map);
  2595. }
  2596. /* Side tone-mixing */
  2597. if (st_chmask) {
  2598. /* Master channel, always writable */
  2599. build_feature_ctl_badd(mixer, 0, UAC_FU_MUTE,
  2600. UAC3_BADD_FU_ID7, map->map);
  2601. /* Mono volume channel, always writable */
  2602. build_feature_ctl_badd(mixer, 1, UAC_FU_VOLUME,
  2603. UAC3_BADD_FU_ID7, map->map);
  2604. }
  2605. /* Insertion Control */
  2606. if (f->subclass == UAC3_FUNCTION_SUBCLASS_HEADSET_ADAPTER) {
  2607. struct usb_audio_term iterm, oterm;
  2608. /* Input Term - Insertion control */
  2609. memset(&iterm, 0, sizeof(iterm));
  2610. iterm.id = UAC3_BADD_IT_ID4;
  2611. iterm.type = UAC_BIDIR_TERMINAL_HEADSET;
  2612. build_connector_control(mixer, &iterm, true);
  2613. /* Output Term - Insertion control */
  2614. memset(&oterm, 0, sizeof(oterm));
  2615. oterm.id = UAC3_BADD_OT_ID3;
  2616. oterm.type = UAC_BIDIR_TERMINAL_HEADSET;
  2617. build_connector_control(mixer, &oterm, false);
  2618. }
  2619. return 0;
  2620. }
  2621. /*
  2622. * create mixer controls
  2623. *
  2624. * walk through all UAC_OUTPUT_TERMINAL descriptors to search for mixers
  2625. */
  2626. static int snd_usb_mixer_controls(struct usb_mixer_interface *mixer)
  2627. {
  2628. struct mixer_build state;
  2629. int err;
  2630. const struct usbmix_ctl_map *map;
  2631. void *p;
  2632. memset(&state, 0, sizeof(state));
  2633. state.chip = mixer->chip;
  2634. state.mixer = mixer;
  2635. state.buffer = mixer->hostif->extra;
  2636. state.buflen = mixer->hostif->extralen;
  2637. /* check the mapping table */
  2638. for (map = usbmix_ctl_maps; map->id; map++) {
  2639. if (map->id == state.chip->usb_id) {
  2640. state.map = map->map;
  2641. state.selector_map = map->selector_map;
  2642. mixer->ignore_ctl_error = map->ignore_ctl_error;
  2643. break;
  2644. }
  2645. }
  2646. p = NULL;
  2647. while ((p = snd_usb_find_csint_desc(mixer->hostif->extra,
  2648. mixer->hostif->extralen,
  2649. p, UAC_OUTPUT_TERMINAL)) != NULL) {
  2650. if (mixer->protocol == UAC_VERSION_1) {
  2651. struct uac1_output_terminal_descriptor *desc = p;
  2652. if (desc->bLength < sizeof(*desc))
  2653. continue; /* invalid descriptor? */
  2654. /* mark terminal ID as visited */
  2655. set_bit(desc->bTerminalID, state.unitbitmap);
  2656. state.oterm.id = desc->bTerminalID;
  2657. state.oterm.type = le16_to_cpu(desc->wTerminalType);
  2658. state.oterm.name = desc->iTerminal;
  2659. err = parse_audio_unit(&state, desc->bSourceID);
  2660. if (err < 0 && err != -EINVAL)
  2661. return err;
  2662. } else if (mixer->protocol == UAC_VERSION_2) {
  2663. struct uac2_output_terminal_descriptor *desc = p;
  2664. if (desc->bLength < sizeof(*desc))
  2665. continue; /* invalid descriptor? */
  2666. /* mark terminal ID as visited */
  2667. set_bit(desc->bTerminalID, state.unitbitmap);
  2668. state.oterm.id = desc->bTerminalID;
  2669. state.oterm.type = le16_to_cpu(desc->wTerminalType);
  2670. state.oterm.name = desc->iTerminal;
  2671. err = parse_audio_unit(&state, desc->bSourceID);
  2672. if (err < 0 && err != -EINVAL)
  2673. return err;
  2674. /*
  2675. * For UAC2, use the same approach to also add the
  2676. * clock selectors
  2677. */
  2678. err = parse_audio_unit(&state, desc->bCSourceID);
  2679. if (err < 0 && err != -EINVAL)
  2680. return err;
  2681. if (uac_v2v3_control_is_readable(le16_to_cpu(desc->bmControls),
  2682. UAC2_TE_CONNECTOR)) {
  2683. build_connector_control(state.mixer, &state.oterm,
  2684. false);
  2685. }
  2686. } else { /* UAC_VERSION_3 */
  2687. struct uac3_output_terminal_descriptor *desc = p;
  2688. if (desc->bLength < sizeof(*desc))
  2689. continue; /* invalid descriptor? */
  2690. /* mark terminal ID as visited */
  2691. set_bit(desc->bTerminalID, state.unitbitmap);
  2692. state.oterm.id = desc->bTerminalID;
  2693. state.oterm.type = le16_to_cpu(desc->wTerminalType);
  2694. state.oterm.name = le16_to_cpu(desc->wTerminalDescrStr);
  2695. err = parse_audio_unit(&state, desc->bSourceID);
  2696. if (err < 0 && err != -EINVAL)
  2697. return err;
  2698. /*
  2699. * For UAC3, use the same approach to also add the
  2700. * clock selectors
  2701. */
  2702. err = parse_audio_unit(&state, desc->bCSourceID);
  2703. if (err < 0 && err != -EINVAL)
  2704. return err;
  2705. if (uac_v2v3_control_is_readable(le32_to_cpu(desc->bmControls),
  2706. UAC3_TE_INSERTION)) {
  2707. build_connector_control(state.mixer, &state.oterm,
  2708. false);
  2709. }
  2710. }
  2711. }
  2712. return 0;
  2713. }
  2714. void snd_usb_mixer_notify_id(struct usb_mixer_interface *mixer, int unitid)
  2715. {
  2716. struct usb_mixer_elem_list *list;
  2717. for_each_mixer_elem(list, mixer, unitid) {
  2718. struct usb_mixer_elem_info *info =
  2719. mixer_elem_list_to_info(list);
  2720. /* invalidate cache, so the value is read from the device */
  2721. info->cached = 0;
  2722. snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
  2723. &list->kctl->id);
  2724. }
  2725. }
  2726. static void snd_usb_mixer_dump_cval(struct snd_info_buffer *buffer,
  2727. struct usb_mixer_elem_list *list)
  2728. {
  2729. struct usb_mixer_elem_info *cval = mixer_elem_list_to_info(list);
  2730. static char *val_types[] = {"BOOLEAN", "INV_BOOLEAN",
  2731. "S8", "U8", "S16", "U16"};
  2732. snd_iprintf(buffer, " Info: id=%i, control=%i, cmask=0x%x, "
  2733. "channels=%i, type=\"%s\"\n", cval->head.id,
  2734. cval->control, cval->cmask, cval->channels,
  2735. val_types[cval->val_type]);
  2736. snd_iprintf(buffer, " Volume: min=%i, max=%i, dBmin=%i, dBmax=%i\n",
  2737. cval->min, cval->max, cval->dBmin, cval->dBmax);
  2738. }
  2739. static void snd_usb_mixer_proc_read(struct snd_info_entry *entry,
  2740. struct snd_info_buffer *buffer)
  2741. {
  2742. struct snd_usb_audio *chip = entry->private_data;
  2743. struct usb_mixer_interface *mixer;
  2744. struct usb_mixer_elem_list *list;
  2745. int unitid;
  2746. list_for_each_entry(mixer, &chip->mixer_list, list) {
  2747. snd_iprintf(buffer,
  2748. "USB Mixer: usb_id=0x%08x, ctrlif=%i, ctlerr=%i\n",
  2749. chip->usb_id, snd_usb_ctrl_intf(chip),
  2750. mixer->ignore_ctl_error);
  2751. snd_iprintf(buffer, "Card: %s\n", chip->card->longname);
  2752. for (unitid = 0; unitid < MAX_ID_ELEMS; unitid++) {
  2753. for_each_mixer_elem(list, mixer, unitid) {
  2754. snd_iprintf(buffer, " Unit: %i\n", list->id);
  2755. if (list->kctl)
  2756. snd_iprintf(buffer,
  2757. " Control: name=\"%s\", index=%i\n",
  2758. list->kctl->id.name,
  2759. list->kctl->id.index);
  2760. if (list->dump)
  2761. list->dump(buffer, list);
  2762. }
  2763. }
  2764. }
  2765. }
  2766. static void snd_usb_mixer_interrupt_v2(struct usb_mixer_interface *mixer,
  2767. int attribute, int value, int index)
  2768. {
  2769. struct usb_mixer_elem_list *list;
  2770. __u8 unitid = (index >> 8) & 0xff;
  2771. __u8 control = (value >> 8) & 0xff;
  2772. __u8 channel = value & 0xff;
  2773. unsigned int count = 0;
  2774. if (channel >= MAX_CHANNELS) {
  2775. usb_audio_dbg(mixer->chip,
  2776. "%s(): bogus channel number %d\n",
  2777. __func__, channel);
  2778. return;
  2779. }
  2780. for_each_mixer_elem(list, mixer, unitid)
  2781. count++;
  2782. if (count == 0)
  2783. return;
  2784. for_each_mixer_elem(list, mixer, unitid) {
  2785. struct usb_mixer_elem_info *info;
  2786. if (!list->kctl)
  2787. continue;
  2788. info = mixer_elem_list_to_info(list);
  2789. if (count > 1 && info->control != control)
  2790. continue;
  2791. switch (attribute) {
  2792. case UAC2_CS_CUR:
  2793. /* invalidate cache, so the value is read from the device */
  2794. if (channel)
  2795. info->cached &= ~(1 << channel);
  2796. else /* master channel */
  2797. info->cached = 0;
  2798. snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
  2799. &info->head.kctl->id);
  2800. break;
  2801. case UAC2_CS_RANGE:
  2802. /* TODO */
  2803. break;
  2804. case UAC2_CS_MEM:
  2805. /* TODO */
  2806. break;
  2807. default:
  2808. usb_audio_dbg(mixer->chip,
  2809. "unknown attribute %d in interrupt\n",
  2810. attribute);
  2811. break;
  2812. } /* switch */
  2813. }
  2814. }
  2815. static void snd_usb_mixer_interrupt(struct urb *urb)
  2816. {
  2817. struct usb_mixer_interface *mixer = urb->context;
  2818. int len = urb->actual_length;
  2819. int ustatus = urb->status;
  2820. if (ustatus != 0)
  2821. goto requeue;
  2822. if (mixer->protocol == UAC_VERSION_1) {
  2823. struct uac1_status_word *status;
  2824. for (status = urb->transfer_buffer;
  2825. len >= sizeof(*status);
  2826. len -= sizeof(*status), status++) {
  2827. dev_dbg(&urb->dev->dev, "status interrupt: %02x %02x\n",
  2828. status->bStatusType,
  2829. status->bOriginator);
  2830. /* ignore any notifications not from the control interface */
  2831. if ((status->bStatusType & UAC1_STATUS_TYPE_ORIG_MASK) !=
  2832. UAC1_STATUS_TYPE_ORIG_AUDIO_CONTROL_IF)
  2833. continue;
  2834. if (status->bStatusType & UAC1_STATUS_TYPE_MEM_CHANGED)
  2835. snd_usb_mixer_rc_memory_change(mixer, status->bOriginator);
  2836. else
  2837. snd_usb_mixer_notify_id(mixer, status->bOriginator);
  2838. }
  2839. } else { /* UAC_VERSION_2 */
  2840. struct uac2_interrupt_data_msg *msg;
  2841. for (msg = urb->transfer_buffer;
  2842. len >= sizeof(*msg);
  2843. len -= sizeof(*msg), msg++) {
  2844. /* drop vendor specific and endpoint requests */
  2845. if ((msg->bInfo & UAC2_INTERRUPT_DATA_MSG_VENDOR) ||
  2846. (msg->bInfo & UAC2_INTERRUPT_DATA_MSG_EP))
  2847. continue;
  2848. snd_usb_mixer_interrupt_v2(mixer, msg->bAttribute,
  2849. le16_to_cpu(msg->wValue),
  2850. le16_to_cpu(msg->wIndex));
  2851. }
  2852. }
  2853. requeue:
  2854. if (ustatus != -ENOENT &&
  2855. ustatus != -ECONNRESET &&
  2856. ustatus != -ESHUTDOWN) {
  2857. urb->dev = mixer->chip->dev;
  2858. usb_submit_urb(urb, GFP_ATOMIC);
  2859. }
  2860. }
  2861. /* create the handler for the optional status interrupt endpoint */
  2862. static int snd_usb_mixer_status_create(struct usb_mixer_interface *mixer)
  2863. {
  2864. struct usb_endpoint_descriptor *ep;
  2865. void *transfer_buffer;
  2866. int buffer_length;
  2867. unsigned int epnum;
  2868. /* we need one interrupt input endpoint */
  2869. if (get_iface_desc(mixer->hostif)->bNumEndpoints < 1)
  2870. return 0;
  2871. ep = get_endpoint(mixer->hostif, 0);
  2872. if (!usb_endpoint_dir_in(ep) || !usb_endpoint_xfer_int(ep))
  2873. return 0;
  2874. epnum = usb_endpoint_num(ep);
  2875. buffer_length = le16_to_cpu(ep->wMaxPacketSize);
  2876. transfer_buffer = kmalloc(buffer_length, GFP_KERNEL);
  2877. if (!transfer_buffer)
  2878. return -ENOMEM;
  2879. mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
  2880. if (!mixer->urb) {
  2881. kfree(transfer_buffer);
  2882. return -ENOMEM;
  2883. }
  2884. usb_fill_int_urb(mixer->urb, mixer->chip->dev,
  2885. usb_rcvintpipe(mixer->chip->dev, epnum),
  2886. transfer_buffer, buffer_length,
  2887. snd_usb_mixer_interrupt, mixer, ep->bInterval);
  2888. usb_submit_urb(mixer->urb, GFP_KERNEL);
  2889. return 0;
  2890. }
  2891. static int keep_iface_ctl_get(struct snd_kcontrol *kcontrol,
  2892. struct snd_ctl_elem_value *ucontrol)
  2893. {
  2894. struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
  2895. ucontrol->value.integer.value[0] = mixer->chip->keep_iface;
  2896. return 0;
  2897. }
  2898. static int keep_iface_ctl_put(struct snd_kcontrol *kcontrol,
  2899. struct snd_ctl_elem_value *ucontrol)
  2900. {
  2901. struct usb_mixer_interface *mixer = snd_kcontrol_chip(kcontrol);
  2902. bool keep_iface = !!ucontrol->value.integer.value[0];
  2903. if (mixer->chip->keep_iface == keep_iface)
  2904. return 0;
  2905. mixer->chip->keep_iface = keep_iface;
  2906. return 1;
  2907. }
  2908. static const struct snd_kcontrol_new keep_iface_ctl = {
  2909. .iface = SNDRV_CTL_ELEM_IFACE_CARD,
  2910. .name = "Keep Interface",
  2911. .info = snd_ctl_boolean_mono_info,
  2912. .get = keep_iface_ctl_get,
  2913. .put = keep_iface_ctl_put,
  2914. };
  2915. static int create_keep_iface_ctl(struct usb_mixer_interface *mixer)
  2916. {
  2917. struct snd_kcontrol *kctl = snd_ctl_new1(&keep_iface_ctl, mixer);
  2918. /* need only one control per card */
  2919. if (snd_ctl_find_id(mixer->chip->card, &kctl->id)) {
  2920. snd_ctl_free_one(kctl);
  2921. return 0;
  2922. }
  2923. return snd_ctl_add(mixer->chip->card, kctl);
  2924. }
  2925. int snd_usb_create_mixer(struct snd_usb_audio *chip, int ctrlif,
  2926. int ignore_error)
  2927. {
  2928. static struct snd_device_ops dev_ops = {
  2929. .dev_free = snd_usb_mixer_dev_free
  2930. };
  2931. struct usb_mixer_interface *mixer;
  2932. struct snd_info_entry *entry;
  2933. int err;
  2934. strcpy(chip->card->mixername, "USB Mixer");
  2935. mixer = kzalloc(sizeof(*mixer), GFP_KERNEL);
  2936. if (!mixer)
  2937. return -ENOMEM;
  2938. mixer->chip = chip;
  2939. mixer->ignore_ctl_error = ignore_error;
  2940. mixer->id_elems = kcalloc(MAX_ID_ELEMS, sizeof(*mixer->id_elems),
  2941. GFP_KERNEL);
  2942. if (!mixer->id_elems) {
  2943. kfree(mixer);
  2944. return -ENOMEM;
  2945. }
  2946. mixer->hostif = &usb_ifnum_to_if(chip->dev, ctrlif)->altsetting[0];
  2947. switch (get_iface_desc(mixer->hostif)->bInterfaceProtocol) {
  2948. case UAC_VERSION_1:
  2949. default:
  2950. mixer->protocol = UAC_VERSION_1;
  2951. break;
  2952. case UAC_VERSION_2:
  2953. mixer->protocol = UAC_VERSION_2;
  2954. break;
  2955. case UAC_VERSION_3:
  2956. mixer->protocol = UAC_VERSION_3;
  2957. break;
  2958. }
  2959. if (mixer->protocol == UAC_VERSION_3 &&
  2960. chip->badd_profile >= UAC3_FUNCTION_SUBCLASS_GENERIC_IO) {
  2961. err = snd_usb_mixer_controls_badd(mixer, ctrlif);
  2962. if (err < 0)
  2963. goto _error;
  2964. } else {
  2965. err = snd_usb_mixer_controls(mixer);
  2966. if (err < 0)
  2967. goto _error;
  2968. }
  2969. err = snd_usb_mixer_status_create(mixer);
  2970. if (err < 0)
  2971. goto _error;
  2972. err = create_keep_iface_ctl(mixer);
  2973. if (err < 0)
  2974. goto _error;
  2975. snd_usb_mixer_apply_create_quirk(mixer);
  2976. err = snd_device_new(chip->card, SNDRV_DEV_CODEC, mixer, &dev_ops);
  2977. if (err < 0)
  2978. goto _error;
  2979. if (list_empty(&chip->mixer_list) &&
  2980. !snd_card_proc_new(chip->card, "usbmixer", &entry))
  2981. snd_info_set_text_ops(entry, chip, snd_usb_mixer_proc_read);
  2982. list_add(&mixer->list, &chip->mixer_list);
  2983. return 0;
  2984. _error:
  2985. snd_usb_mixer_free(mixer);
  2986. return err;
  2987. }
  2988. void snd_usb_mixer_disconnect(struct usb_mixer_interface *mixer)
  2989. {
  2990. if (mixer->disconnected)
  2991. return;
  2992. if (mixer->urb)
  2993. usb_kill_urb(mixer->urb);
  2994. if (mixer->rc_urb)
  2995. usb_kill_urb(mixer->rc_urb);
  2996. mixer->disconnected = true;
  2997. }
  2998. #ifdef CONFIG_PM
  2999. /* stop any bus activity of a mixer */
  3000. static void snd_usb_mixer_inactivate(struct usb_mixer_interface *mixer)
  3001. {
  3002. usb_kill_urb(mixer->urb);
  3003. usb_kill_urb(mixer->rc_urb);
  3004. }
  3005. static int snd_usb_mixer_activate(struct usb_mixer_interface *mixer)
  3006. {
  3007. int err;
  3008. if (mixer->urb) {
  3009. err = usb_submit_urb(mixer->urb, GFP_NOIO);
  3010. if (err < 0)
  3011. return err;
  3012. }
  3013. return 0;
  3014. }
  3015. int snd_usb_mixer_suspend(struct usb_mixer_interface *mixer)
  3016. {
  3017. snd_usb_mixer_inactivate(mixer);
  3018. return 0;
  3019. }
  3020. static int restore_mixer_value(struct usb_mixer_elem_list *list)
  3021. {
  3022. struct usb_mixer_elem_info *cval = mixer_elem_list_to_info(list);
  3023. int c, err, idx;
  3024. if (cval->cmask) {
  3025. idx = 0;
  3026. for (c = 0; c < MAX_CHANNELS; c++) {
  3027. if (!(cval->cmask & (1 << c)))
  3028. continue;
  3029. if (cval->cached & (1 << (c + 1))) {
  3030. err = snd_usb_set_cur_mix_value(cval, c + 1, idx,
  3031. cval->cache_val[idx]);
  3032. if (err < 0)
  3033. return err;
  3034. }
  3035. idx++;
  3036. }
  3037. } else {
  3038. /* master */
  3039. if (cval->cached) {
  3040. err = snd_usb_set_cur_mix_value(cval, 0, 0, *cval->cache_val);
  3041. if (err < 0)
  3042. return err;
  3043. }
  3044. }
  3045. return 0;
  3046. }
  3047. int snd_usb_mixer_resume(struct usb_mixer_interface *mixer, bool reset_resume)
  3048. {
  3049. struct usb_mixer_elem_list *list;
  3050. int id, err;
  3051. if (reset_resume) {
  3052. /* restore cached mixer values */
  3053. for (id = 0; id < MAX_ID_ELEMS; id++) {
  3054. for_each_mixer_elem(list, mixer, id) {
  3055. if (list->resume) {
  3056. err = list->resume(list);
  3057. if (err < 0)
  3058. return err;
  3059. }
  3060. }
  3061. }
  3062. }
  3063. snd_usb_mixer_resume_quirk(mixer);
  3064. return snd_usb_mixer_activate(mixer);
  3065. }
  3066. #endif
  3067. void snd_usb_mixer_elem_init_std(struct usb_mixer_elem_list *list,
  3068. struct usb_mixer_interface *mixer,
  3069. int unitid)
  3070. {
  3071. list->mixer = mixer;
  3072. list->id = unitid;
  3073. list->dump = snd_usb_mixer_dump_cval;
  3074. #ifdef CONFIG_PM
  3075. list->resume = restore_mixer_value;
  3076. #endif
  3077. }