mixer.c 95 KB

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