mixer.c 97 KB

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