mixer.c 98 KB

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