mixer.c 99 KB

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