mixer.c 98 KB

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