mixer.c 96 KB

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