mixer.c 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775
  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 <sound/core.h>
  53. #include <sound/control.h>
  54. #include <sound/hwdep.h>
  55. #include <sound/info.h>
  56. #include <sound/tlv.h>
  57. #include "usbaudio.h"
  58. #include "mixer.h"
  59. #include "helper.h"
  60. #include "mixer_quirks.h"
  61. #include "power.h"
  62. #define MAX_ID_ELEMS 256
  63. struct usb_audio_term {
  64. int id;
  65. int type;
  66. int channels;
  67. unsigned int chconfig;
  68. int name;
  69. };
  70. struct usbmix_name_map;
  71. struct mixer_build {
  72. struct snd_usb_audio *chip;
  73. struct usb_mixer_interface *mixer;
  74. unsigned char *buffer;
  75. unsigned int buflen;
  76. DECLARE_BITMAP(unitbitmap, MAX_ID_ELEMS);
  77. DECLARE_BITMAP(termbitmap, MAX_ID_ELEMS);
  78. struct usb_audio_term oterm;
  79. const struct usbmix_name_map *map;
  80. const struct usbmix_selector_map *selector_map;
  81. };
  82. /*E-mu 0202/0404/0204 eXtension Unit(XU) control*/
  83. enum {
  84. USB_XU_CLOCK_RATE = 0xe301,
  85. USB_XU_CLOCK_SOURCE = 0xe302,
  86. USB_XU_DIGITAL_IO_STATUS = 0xe303,
  87. USB_XU_DEVICE_OPTIONS = 0xe304,
  88. USB_XU_DIRECT_MONITORING = 0xe305,
  89. USB_XU_METERING = 0xe306
  90. };
  91. enum {
  92. USB_XU_CLOCK_SOURCE_SELECTOR = 0x02, /* clock source*/
  93. USB_XU_CLOCK_RATE_SELECTOR = 0x03, /* clock rate */
  94. USB_XU_DIGITAL_FORMAT_SELECTOR = 0x01, /* the spdif format */
  95. USB_XU_SOFT_LIMIT_SELECTOR = 0x03 /* soft limiter */
  96. };
  97. /*
  98. * manual mapping of mixer names
  99. * if the mixer topology is too complicated and the parsed names are
  100. * ambiguous, add the entries in usbmixer_maps.c.
  101. */
  102. #include "mixer_maps.c"
  103. static const struct usbmix_name_map *
  104. find_map(struct mixer_build *state, int unitid, int control)
  105. {
  106. const struct usbmix_name_map *p = state->map;
  107. if (!p)
  108. return NULL;
  109. for (p = state->map; 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 <= UAC2_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 mixer_build *state,
  181. int index, char *buf, int maxlen)
  182. {
  183. int len = usb_string(state->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 */
  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. if ((err = snd_ctl_add(mixer->chip->card, kctl)) < 0) {
  527. usb_audio_dbg(mixer->chip, "cannot add control (err = %d)\n",
  528. err);
  529. return err;
  530. }
  531. list->kctl = kctl;
  532. list->is_std_info = is_std_info;
  533. list->next_id_elem = mixer->id_elems[list->id];
  534. mixer->id_elems[list->id] = list;
  535. return 0;
  536. }
  537. /*
  538. * get a terminal name string
  539. */
  540. static struct iterm_name_combo {
  541. int type;
  542. char *name;
  543. } iterm_names[] = {
  544. { 0x0300, "Output" },
  545. { 0x0301, "Speaker" },
  546. { 0x0302, "Headphone" },
  547. { 0x0303, "HMD Audio" },
  548. { 0x0304, "Desktop Speaker" },
  549. { 0x0305, "Room Speaker" },
  550. { 0x0306, "Com Speaker" },
  551. { 0x0307, "LFE" },
  552. { 0x0600, "External In" },
  553. { 0x0601, "Analog In" },
  554. { 0x0602, "Digital In" },
  555. { 0x0603, "Line" },
  556. { 0x0604, "Legacy In" },
  557. { 0x0605, "IEC958 In" },
  558. { 0x0606, "1394 DA Stream" },
  559. { 0x0607, "1394 DV Stream" },
  560. { 0x0700, "Embedded" },
  561. { 0x0701, "Noise Source" },
  562. { 0x0702, "Equalization Noise" },
  563. { 0x0703, "CD" },
  564. { 0x0704, "DAT" },
  565. { 0x0705, "DCC" },
  566. { 0x0706, "MiniDisk" },
  567. { 0x0707, "Analog Tape" },
  568. { 0x0708, "Phonograph" },
  569. { 0x0709, "VCR Audio" },
  570. { 0x070a, "Video Disk Audio" },
  571. { 0x070b, "DVD Audio" },
  572. { 0x070c, "TV Tuner Audio" },
  573. { 0x070d, "Satellite Rec Audio" },
  574. { 0x070e, "Cable Tuner Audio" },
  575. { 0x070f, "DSS Audio" },
  576. { 0x0710, "Radio Receiver" },
  577. { 0x0711, "Radio Transmitter" },
  578. { 0x0712, "Multi-Track Recorder" },
  579. { 0x0713, "Synthesizer" },
  580. { 0 },
  581. };
  582. static int get_term_name(struct mixer_build *state, struct usb_audio_term *iterm,
  583. unsigned char *name, int maxlen, int term_only)
  584. {
  585. struct iterm_name_combo *names;
  586. if (iterm->name)
  587. return snd_usb_copy_string_desc(state, iterm->name,
  588. name, maxlen);
  589. /* virtual type - not a real terminal */
  590. if (iterm->type >> 16) {
  591. if (term_only)
  592. return 0;
  593. switch (iterm->type >> 16) {
  594. case UAC_SELECTOR_UNIT:
  595. strcpy(name, "Selector");
  596. return 8;
  597. case UAC1_PROCESSING_UNIT:
  598. strcpy(name, "Process Unit");
  599. return 12;
  600. case UAC1_EXTENSION_UNIT:
  601. strcpy(name, "Ext Unit");
  602. return 8;
  603. case UAC_MIXER_UNIT:
  604. strcpy(name, "Mixer");
  605. return 5;
  606. default:
  607. return sprintf(name, "Unit %d", iterm->id);
  608. }
  609. }
  610. switch (iterm->type & 0xff00) {
  611. case 0x0100:
  612. strcpy(name, "PCM");
  613. return 3;
  614. case 0x0200:
  615. strcpy(name, "Mic");
  616. return 3;
  617. case 0x0400:
  618. strcpy(name, "Headset");
  619. return 7;
  620. case 0x0500:
  621. strcpy(name, "Phone");
  622. return 5;
  623. }
  624. for (names = iterm_names; names->type; names++) {
  625. if (names->type == iterm->type) {
  626. strcpy(name, names->name);
  627. return strlen(names->name);
  628. }
  629. }
  630. return 0;
  631. }
  632. /*
  633. * parse the source unit recursively until it reaches to a terminal
  634. * or a branched unit.
  635. */
  636. static int __check_input_term(struct mixer_build *state, int id,
  637. struct usb_audio_term *term)
  638. {
  639. int err;
  640. void *p1;
  641. unsigned char *hdr;
  642. memset(term, 0, sizeof(*term));
  643. for (;;) {
  644. /* a loop in the terminal chain? */
  645. if (test_and_set_bit(id, state->termbitmap))
  646. return -EINVAL;
  647. p1 = find_audio_control_unit(state, id);
  648. if (!p1)
  649. break;
  650. hdr = p1;
  651. term->id = id;
  652. switch (hdr[2]) {
  653. case UAC_INPUT_TERMINAL:
  654. if (state->mixer->protocol == UAC_VERSION_1) {
  655. struct uac_input_terminal_descriptor *d = p1;
  656. term->type = le16_to_cpu(d->wTerminalType);
  657. term->channels = d->bNrChannels;
  658. term->chconfig = le16_to_cpu(d->wChannelConfig);
  659. term->name = d->iTerminal;
  660. } else { /* UAC_VERSION_2 */
  661. struct uac2_input_terminal_descriptor *d = p1;
  662. /* call recursively to verify that the
  663. * referenced clock entity is valid */
  664. err = __check_input_term(state, d->bCSourceID, term);
  665. if (err < 0)
  666. return err;
  667. /* save input term properties after recursion,
  668. * to ensure they are not overriden by the
  669. * recursion calls */
  670. term->id = id;
  671. term->type = le16_to_cpu(d->wTerminalType);
  672. term->channels = d->bNrChannels;
  673. term->chconfig = le32_to_cpu(d->bmChannelConfig);
  674. term->name = d->iTerminal;
  675. }
  676. return 0;
  677. case UAC_FEATURE_UNIT: {
  678. /* the header is the same for v1 and v2 */
  679. struct uac_feature_unit_descriptor *d = p1;
  680. id = d->bSourceID;
  681. break; /* continue to parse */
  682. }
  683. case UAC_MIXER_UNIT: {
  684. struct uac_mixer_unit_descriptor *d = p1;
  685. term->type = d->bDescriptorSubtype << 16; /* virtual type */
  686. term->channels = uac_mixer_unit_bNrChannels(d);
  687. term->chconfig = uac_mixer_unit_wChannelConfig(d, state->mixer->protocol);
  688. term->name = uac_mixer_unit_iMixer(d);
  689. return 0;
  690. }
  691. case UAC_SELECTOR_UNIT:
  692. case UAC2_CLOCK_SELECTOR: {
  693. struct uac_selector_unit_descriptor *d = p1;
  694. /* call recursively to retrieve the channel info */
  695. err = __check_input_term(state, d->baSourceID[0], term);
  696. if (err < 0)
  697. return err;
  698. term->type = d->bDescriptorSubtype << 16; /* virtual type */
  699. term->id = id;
  700. term->name = uac_selector_unit_iSelector(d);
  701. return 0;
  702. }
  703. case UAC1_PROCESSING_UNIT:
  704. case UAC1_EXTENSION_UNIT:
  705. /* UAC2_PROCESSING_UNIT_V2 */
  706. /* UAC2_EFFECT_UNIT */
  707. case UAC2_EXTENSION_UNIT_V2: {
  708. struct uac_processing_unit_descriptor *d = p1;
  709. if (state->mixer->protocol == UAC_VERSION_2 &&
  710. hdr[2] == UAC2_EFFECT_UNIT) {
  711. /* UAC2/UAC1 unit IDs overlap here in an
  712. * uncompatible way. Ignore this unit for now.
  713. */
  714. return 0;
  715. }
  716. if (d->bNrInPins) {
  717. id = d->baSourceID[0];
  718. break; /* continue to parse */
  719. }
  720. term->type = d->bDescriptorSubtype << 16; /* virtual type */
  721. term->channels = uac_processing_unit_bNrChannels(d);
  722. term->chconfig = uac_processing_unit_wChannelConfig(d, state->mixer->protocol);
  723. term->name = uac_processing_unit_iProcessing(d, state->mixer->protocol);
  724. return 0;
  725. }
  726. case UAC2_CLOCK_SOURCE: {
  727. struct uac_clock_source_descriptor *d = p1;
  728. term->type = d->bDescriptorSubtype << 16; /* virtual type */
  729. term->id = id;
  730. term->name = d->iClockSource;
  731. return 0;
  732. }
  733. default:
  734. return -ENODEV;
  735. }
  736. }
  737. return -ENODEV;
  738. }
  739. static int check_input_term(struct mixer_build *state, int id,
  740. struct usb_audio_term *term)
  741. {
  742. memset(term, 0, sizeof(*term));
  743. memset(state->termbitmap, 0, sizeof(state->termbitmap));
  744. return __check_input_term(state, id, term);
  745. }
  746. /*
  747. * Feature Unit
  748. */
  749. /* feature unit control information */
  750. struct usb_feature_control_info {
  751. const char *name;
  752. int type; /* data type for uac1 */
  753. int type_uac2; /* data type for uac2 if different from uac1, else -1 */
  754. };
  755. static struct usb_feature_control_info audio_feature_info[] = {
  756. { "Mute", USB_MIXER_INV_BOOLEAN, -1 },
  757. { "Volume", USB_MIXER_S16, -1 },
  758. { "Tone Control - Bass", USB_MIXER_S8, -1 },
  759. { "Tone Control - Mid", USB_MIXER_S8, -1 },
  760. { "Tone Control - Treble", USB_MIXER_S8, -1 },
  761. { "Graphic Equalizer", USB_MIXER_S8, -1 }, /* FIXME: not implemeted yet */
  762. { "Auto Gain Control", USB_MIXER_BOOLEAN, -1 },
  763. { "Delay Control", USB_MIXER_U16, USB_MIXER_U32 },
  764. { "Bass Boost", USB_MIXER_BOOLEAN, -1 },
  765. { "Loudness", USB_MIXER_BOOLEAN, -1 },
  766. /* UAC2 specific */
  767. { "Input Gain Control", USB_MIXER_S16, -1 },
  768. { "Input Gain Pad Control", USB_MIXER_S16, -1 },
  769. { "Phase Inverter Control", USB_MIXER_BOOLEAN, -1 },
  770. };
  771. /* private_free callback */
  772. void snd_usb_mixer_elem_free(struct snd_kcontrol *kctl)
  773. {
  774. kfree(kctl->private_data);
  775. kctl->private_data = NULL;
  776. }
  777. /*
  778. * interface to ALSA control for feature/mixer units
  779. */
  780. /* volume control quirks */
  781. static void volume_control_quirks(struct usb_mixer_elem_info *cval,
  782. struct snd_kcontrol *kctl)
  783. {
  784. struct snd_usb_audio *chip = cval->head.mixer->chip;
  785. switch (chip->usb_id) {
  786. case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */
  787. case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C600 */
  788. if (strcmp(kctl->id.name, "Effect Duration") == 0) {
  789. cval->min = 0x0000;
  790. cval->max = 0xffff;
  791. cval->res = 0x00e6;
  792. break;
  793. }
  794. if (strcmp(kctl->id.name, "Effect Volume") == 0 ||
  795. strcmp(kctl->id.name, "Effect Feedback Volume") == 0) {
  796. cval->min = 0x00;
  797. cval->max = 0xff;
  798. break;
  799. }
  800. if (strstr(kctl->id.name, "Effect Return") != NULL) {
  801. cval->min = 0xb706;
  802. cval->max = 0xff7b;
  803. cval->res = 0x0073;
  804. break;
  805. }
  806. if ((strstr(kctl->id.name, "Playback Volume") != NULL) ||
  807. (strstr(kctl->id.name, "Effect Send") != NULL)) {
  808. cval->min = 0xb5fb; /* -73 dB = 0xb6ff */
  809. cval->max = 0xfcfe;
  810. cval->res = 0x0073;
  811. }
  812. break;
  813. case USB_ID(0x0763, 0x2081): /* M-Audio Fast Track Ultra 8R */
  814. case USB_ID(0x0763, 0x2080): /* M-Audio Fast Track Ultra */
  815. if (strcmp(kctl->id.name, "Effect Duration") == 0) {
  816. usb_audio_info(chip,
  817. "set quirk for FTU Effect Duration\n");
  818. cval->min = 0x0000;
  819. cval->max = 0x7f00;
  820. cval->res = 0x0100;
  821. break;
  822. }
  823. if (strcmp(kctl->id.name, "Effect Volume") == 0 ||
  824. strcmp(kctl->id.name, "Effect Feedback Volume") == 0) {
  825. usb_audio_info(chip,
  826. "set quirks for FTU Effect Feedback/Volume\n");
  827. cval->min = 0x00;
  828. cval->max = 0x7f;
  829. break;
  830. }
  831. break;
  832. case USB_ID(0x0d8c, 0x0103):
  833. if (!strcmp(kctl->id.name, "PCM Playback Volume")) {
  834. usb_audio_info(chip,
  835. "set volume quirk for CM102-A+/102S+\n");
  836. cval->min = -256;
  837. }
  838. break;
  839. case USB_ID(0x0471, 0x0101):
  840. case USB_ID(0x0471, 0x0104):
  841. case USB_ID(0x0471, 0x0105):
  842. case USB_ID(0x0672, 0x1041):
  843. /* quirk for UDA1321/N101.
  844. * note that detection between firmware 2.1.1.7 (N101)
  845. * and later 2.1.1.21 is not very clear from datasheets.
  846. * I hope that the min value is -15360 for newer firmware --jk
  847. */
  848. if (!strcmp(kctl->id.name, "PCM Playback Volume") &&
  849. cval->min == -15616) {
  850. usb_audio_info(chip,
  851. "set volume quirk for UDA1321/N101 chip\n");
  852. cval->max = -256;
  853. }
  854. break;
  855. case USB_ID(0x046d, 0x09a4):
  856. if (!strcmp(kctl->id.name, "Mic Capture Volume")) {
  857. usb_audio_info(chip,
  858. "set volume quirk for QuickCam E3500\n");
  859. cval->min = 6080;
  860. cval->max = 8768;
  861. cval->res = 192;
  862. }
  863. break;
  864. case USB_ID(0x046d, 0x0807): /* Logitech Webcam C500 */
  865. case USB_ID(0x046d, 0x0808):
  866. case USB_ID(0x046d, 0x0809):
  867. case USB_ID(0x046d, 0x0819): /* Logitech Webcam C210 */
  868. case USB_ID(0x046d, 0x081b): /* HD Webcam c310 */
  869. case USB_ID(0x046d, 0x081d): /* HD Webcam c510 */
  870. case USB_ID(0x046d, 0x0825): /* HD Webcam c270 */
  871. case USB_ID(0x046d, 0x0826): /* HD Webcam c525 */
  872. case USB_ID(0x046d, 0x08ca): /* Logitech Quickcam Fusion */
  873. case USB_ID(0x046d, 0x0991):
  874. case USB_ID(0x046d, 0x09a2): /* QuickCam Communicate Deluxe/S7500 */
  875. /* Most audio usb devices lie about volume resolution.
  876. * Most Logitech webcams have res = 384.
  877. * Probably there is some logitech magic behind this number --fishor
  878. */
  879. if (!strcmp(kctl->id.name, "Mic Capture Volume")) {
  880. usb_audio_info(chip,
  881. "set resolution quirk: cval->res = 384\n");
  882. cval->res = 384;
  883. }
  884. break;
  885. case USB_ID(0x0495, 0x3042): /* ESS Technology Asus USB DAC */
  886. if ((strstr(kctl->id.name, "Playback Volume") != NULL) ||
  887. strstr(kctl->id.name, "Capture Volume") != NULL) {
  888. cval->min >>= 8;
  889. cval->max = 0;
  890. cval->res = 1;
  891. }
  892. break;
  893. }
  894. }
  895. /*
  896. * retrieve the minimum and maximum values for the specified control
  897. */
  898. static int get_min_max_with_quirks(struct usb_mixer_elem_info *cval,
  899. int default_min, struct snd_kcontrol *kctl)
  900. {
  901. /* for failsafe */
  902. cval->min = default_min;
  903. cval->max = cval->min + 1;
  904. cval->res = 1;
  905. cval->dBmin = cval->dBmax = 0;
  906. if (cval->val_type == USB_MIXER_BOOLEAN ||
  907. cval->val_type == USB_MIXER_INV_BOOLEAN) {
  908. cval->initialized = 1;
  909. } else {
  910. int minchn = 0;
  911. if (cval->cmask) {
  912. int i;
  913. for (i = 0; i < MAX_CHANNELS; i++)
  914. if (cval->cmask & (1 << i)) {
  915. minchn = i + 1;
  916. break;
  917. }
  918. }
  919. if (get_ctl_value(cval, UAC_GET_MAX, (cval->control << 8) | minchn, &cval->max) < 0 ||
  920. get_ctl_value(cval, UAC_GET_MIN, (cval->control << 8) | minchn, &cval->min) < 0) {
  921. usb_audio_err(cval->head.mixer->chip,
  922. "%d:%d: cannot get min/max values for control %d (id %d)\n",
  923. cval->head.id, snd_usb_ctrl_intf(cval->head.mixer->chip),
  924. cval->control, cval->head.id);
  925. return -EINVAL;
  926. }
  927. if (get_ctl_value(cval, UAC_GET_RES,
  928. (cval->control << 8) | minchn,
  929. &cval->res) < 0) {
  930. cval->res = 1;
  931. } else {
  932. int last_valid_res = cval->res;
  933. while (cval->res > 1) {
  934. if (snd_usb_mixer_set_ctl_value(cval, UAC_SET_RES,
  935. (cval->control << 8) | minchn,
  936. cval->res / 2) < 0)
  937. break;
  938. cval->res /= 2;
  939. }
  940. if (get_ctl_value(cval, UAC_GET_RES,
  941. (cval->control << 8) | minchn, &cval->res) < 0)
  942. cval->res = last_valid_res;
  943. }
  944. if (cval->res == 0)
  945. cval->res = 1;
  946. /* Additional checks for the proper resolution
  947. *
  948. * Some devices report smaller resolutions than actually
  949. * reacting. They don't return errors but simply clip
  950. * to the lower aligned value.
  951. */
  952. if (cval->min + cval->res < cval->max) {
  953. int last_valid_res = cval->res;
  954. int saved, test, check;
  955. if (get_cur_mix_raw(cval, minchn, &saved) < 0)
  956. goto no_res_check;
  957. for (;;) {
  958. test = saved;
  959. if (test < cval->max)
  960. test += cval->res;
  961. else
  962. test -= cval->res;
  963. if (test < cval->min || test > cval->max ||
  964. snd_usb_set_cur_mix_value(cval, minchn, 0, test) ||
  965. get_cur_mix_raw(cval, minchn, &check)) {
  966. cval->res = last_valid_res;
  967. break;
  968. }
  969. if (test == check)
  970. break;
  971. cval->res *= 2;
  972. }
  973. snd_usb_set_cur_mix_value(cval, minchn, 0, saved);
  974. }
  975. no_res_check:
  976. cval->initialized = 1;
  977. }
  978. if (kctl)
  979. volume_control_quirks(cval, kctl);
  980. /* USB descriptions contain the dB scale in 1/256 dB unit
  981. * while ALSA TLV contains in 1/100 dB unit
  982. */
  983. cval->dBmin = (convert_signed_value(cval, cval->min) * 100) / 256;
  984. cval->dBmax = (convert_signed_value(cval, cval->max) * 100) / 256;
  985. if (cval->dBmin > cval->dBmax) {
  986. /* something is wrong; assume it's either from/to 0dB */
  987. if (cval->dBmin < 0)
  988. cval->dBmax = 0;
  989. else if (cval->dBmin > 0)
  990. cval->dBmin = 0;
  991. if (cval->dBmin > cval->dBmax) {
  992. /* totally crap, return an error */
  993. return -EINVAL;
  994. }
  995. }
  996. return 0;
  997. }
  998. #define get_min_max(cval, def) get_min_max_with_quirks(cval, def, NULL)
  999. /* get a feature/mixer unit info */
  1000. static int mixer_ctl_feature_info(struct snd_kcontrol *kcontrol,
  1001. struct snd_ctl_elem_info *uinfo)
  1002. {
  1003. struct usb_mixer_elem_info *cval = kcontrol->private_data;
  1004. if (cval->val_type == USB_MIXER_BOOLEAN ||
  1005. cval->val_type == USB_MIXER_INV_BOOLEAN)
  1006. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1007. else
  1008. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1009. uinfo->count = cval->channels;
  1010. if (cval->val_type == USB_MIXER_BOOLEAN ||
  1011. cval->val_type == USB_MIXER_INV_BOOLEAN) {
  1012. uinfo->value.integer.min = 0;
  1013. uinfo->value.integer.max = 1;
  1014. } else {
  1015. if (!cval->initialized) {
  1016. get_min_max_with_quirks(cval, 0, kcontrol);
  1017. if (cval->initialized && cval->dBmin >= cval->dBmax) {
  1018. kcontrol->vd[0].access &=
  1019. ~(SNDRV_CTL_ELEM_ACCESS_TLV_READ |
  1020. SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK);
  1021. snd_ctl_notify(cval->head.mixer->chip->card,
  1022. SNDRV_CTL_EVENT_MASK_INFO,
  1023. &kcontrol->id);
  1024. }
  1025. }
  1026. uinfo->value.integer.min = 0;
  1027. uinfo->value.integer.max =
  1028. (cval->max - cval->min + cval->res - 1) / cval->res;
  1029. }
  1030. return 0;
  1031. }
  1032. /* get the current value from feature/mixer unit */
  1033. static int mixer_ctl_feature_get(struct snd_kcontrol *kcontrol,
  1034. struct snd_ctl_elem_value *ucontrol)
  1035. {
  1036. struct usb_mixer_elem_info *cval = kcontrol->private_data;
  1037. int c, cnt, val, err;
  1038. ucontrol->value.integer.value[0] = cval->min;
  1039. if (cval->cmask) {
  1040. cnt = 0;
  1041. for (c = 0; c < MAX_CHANNELS; c++) {
  1042. if (!(cval->cmask & (1 << c)))
  1043. continue;
  1044. err = snd_usb_get_cur_mix_value(cval, c + 1, cnt, &val);
  1045. if (err < 0)
  1046. return filter_error(cval, err);
  1047. val = get_relative_value(cval, val);
  1048. ucontrol->value.integer.value[cnt] = val;
  1049. cnt++;
  1050. }
  1051. return 0;
  1052. } else {
  1053. /* master channel */
  1054. err = snd_usb_get_cur_mix_value(cval, 0, 0, &val);
  1055. if (err < 0)
  1056. return filter_error(cval, err);
  1057. val = get_relative_value(cval, val);
  1058. ucontrol->value.integer.value[0] = val;
  1059. }
  1060. return 0;
  1061. }
  1062. /* put the current value to feature/mixer unit */
  1063. static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol,
  1064. struct snd_ctl_elem_value *ucontrol)
  1065. {
  1066. struct usb_mixer_elem_info *cval = kcontrol->private_data;
  1067. int c, cnt, val, oval, err;
  1068. int changed = 0;
  1069. if (cval->cmask) {
  1070. cnt = 0;
  1071. for (c = 0; c < MAX_CHANNELS; c++) {
  1072. if (!(cval->cmask & (1 << c)))
  1073. continue;
  1074. err = snd_usb_get_cur_mix_value(cval, c + 1, cnt, &oval);
  1075. if (err < 0)
  1076. return filter_error(cval, err);
  1077. val = ucontrol->value.integer.value[cnt];
  1078. val = get_abs_value(cval, val);
  1079. if (oval != val) {
  1080. snd_usb_set_cur_mix_value(cval, c + 1, cnt, val);
  1081. changed = 1;
  1082. }
  1083. cnt++;
  1084. }
  1085. } else {
  1086. /* master channel */
  1087. err = snd_usb_get_cur_mix_value(cval, 0, 0, &oval);
  1088. if (err < 0)
  1089. return filter_error(cval, err);
  1090. val = ucontrol->value.integer.value[0];
  1091. val = get_abs_value(cval, val);
  1092. if (val != oval) {
  1093. snd_usb_set_cur_mix_value(cval, 0, 0, val);
  1094. changed = 1;
  1095. }
  1096. }
  1097. return changed;
  1098. }
  1099. static struct snd_kcontrol_new usb_feature_unit_ctl = {
  1100. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1101. .name = "", /* will be filled later manually */
  1102. .info = mixer_ctl_feature_info,
  1103. .get = mixer_ctl_feature_get,
  1104. .put = mixer_ctl_feature_put,
  1105. };
  1106. /* the read-only variant */
  1107. static const struct snd_kcontrol_new usb_feature_unit_ctl_ro = {
  1108. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1109. .name = "", /* will be filled later manually */
  1110. .info = mixer_ctl_feature_info,
  1111. .get = mixer_ctl_feature_get,
  1112. .put = NULL,
  1113. };
  1114. /*
  1115. * This symbol is exported in order to allow the mixer quirks to
  1116. * hook up to the standard feature unit control mechanism
  1117. */
  1118. struct snd_kcontrol_new *snd_usb_feature_unit_ctl = &usb_feature_unit_ctl;
  1119. /*
  1120. * build a feature control
  1121. */
  1122. static size_t append_ctl_name(struct snd_kcontrol *kctl, const char *str)
  1123. {
  1124. return strlcat(kctl->id.name, str, sizeof(kctl->id.name));
  1125. }
  1126. /*
  1127. * A lot of headsets/headphones have a "Speaker" mixer. Make sure we
  1128. * rename it to "Headphone". We determine if something is a headphone
  1129. * similar to how udev determines form factor.
  1130. */
  1131. static void check_no_speaker_on_headset(struct snd_kcontrol *kctl,
  1132. struct snd_card *card)
  1133. {
  1134. const char *names_to_check[] = {
  1135. "Headset", "headset", "Headphone", "headphone", NULL};
  1136. const char **s;
  1137. bool found = false;
  1138. if (strcmp("Speaker", kctl->id.name))
  1139. return;
  1140. for (s = names_to_check; *s; s++)
  1141. if (strstr(card->shortname, *s)) {
  1142. found = true;
  1143. break;
  1144. }
  1145. if (!found)
  1146. return;
  1147. strlcpy(kctl->id.name, "Headphone", sizeof(kctl->id.name));
  1148. }
  1149. static void build_feature_ctl(struct mixer_build *state, void *raw_desc,
  1150. unsigned int ctl_mask, int control,
  1151. struct usb_audio_term *iterm, int unitid,
  1152. int readonly_mask)
  1153. {
  1154. struct uac_feature_unit_descriptor *desc = raw_desc;
  1155. struct usb_feature_control_info *ctl_info;
  1156. unsigned int len = 0;
  1157. int mapped_name = 0;
  1158. int nameid = uac_feature_unit_iFeature(desc);
  1159. struct snd_kcontrol *kctl;
  1160. struct usb_mixer_elem_info *cval;
  1161. const struct usbmix_name_map *map;
  1162. unsigned int range;
  1163. control++; /* change from zero-based to 1-based value */
  1164. if (control == UAC_FU_GRAPHIC_EQUALIZER) {
  1165. /* FIXME: not supported yet */
  1166. return;
  1167. }
  1168. map = find_map(state, unitid, control);
  1169. if (check_ignored_ctl(map))
  1170. return;
  1171. cval = kzalloc(sizeof(*cval), GFP_KERNEL);
  1172. if (!cval)
  1173. return;
  1174. snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid);
  1175. cval->control = control;
  1176. cval->cmask = ctl_mask;
  1177. ctl_info = &audio_feature_info[control-1];
  1178. if (state->mixer->protocol == UAC_VERSION_1)
  1179. cval->val_type = ctl_info->type;
  1180. else /* UAC_VERSION_2 */
  1181. cval->val_type = ctl_info->type_uac2 >= 0 ?
  1182. ctl_info->type_uac2 : ctl_info->type;
  1183. if (ctl_mask == 0) {
  1184. cval->channels = 1; /* master channel */
  1185. cval->master_readonly = readonly_mask;
  1186. } else {
  1187. int i, c = 0;
  1188. for (i = 0; i < 16; i++)
  1189. if (ctl_mask & (1 << i))
  1190. c++;
  1191. cval->channels = c;
  1192. cval->ch_readonly = readonly_mask;
  1193. }
  1194. /*
  1195. * If all channels in the mask are marked read-only, make the control
  1196. * read-only. snd_usb_set_cur_mix_value() will check the mask again and won't
  1197. * issue write commands to read-only channels.
  1198. */
  1199. if (cval->channels == readonly_mask)
  1200. kctl = snd_ctl_new1(&usb_feature_unit_ctl_ro, cval);
  1201. else
  1202. kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
  1203. if (!kctl) {
  1204. usb_audio_err(state->chip, "cannot malloc kcontrol\n");
  1205. kfree(cval);
  1206. return;
  1207. }
  1208. kctl->private_free = snd_usb_mixer_elem_free;
  1209. len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
  1210. mapped_name = len != 0;
  1211. if (!len && nameid)
  1212. len = snd_usb_copy_string_desc(state, nameid,
  1213. kctl->id.name, sizeof(kctl->id.name));
  1214. switch (control) {
  1215. case UAC_FU_MUTE:
  1216. case UAC_FU_VOLUME:
  1217. /*
  1218. * determine the control name. the rule is:
  1219. * - if a name id is given in descriptor, use it.
  1220. * - if the connected input can be determined, then use the name
  1221. * of terminal type.
  1222. * - if the connected output can be determined, use it.
  1223. * - otherwise, anonymous name.
  1224. */
  1225. if (!len) {
  1226. len = get_term_name(state, iterm, kctl->id.name,
  1227. sizeof(kctl->id.name), 1);
  1228. if (!len)
  1229. len = get_term_name(state, &state->oterm,
  1230. kctl->id.name,
  1231. sizeof(kctl->id.name), 1);
  1232. if (!len)
  1233. snprintf(kctl->id.name, sizeof(kctl->id.name),
  1234. "Feature %d", unitid);
  1235. }
  1236. if (!mapped_name)
  1237. check_no_speaker_on_headset(kctl, state->mixer->chip->card);
  1238. /*
  1239. * determine the stream direction:
  1240. * if the connected output is USB stream, then it's likely a
  1241. * capture stream. otherwise it should be playback (hopefully :)
  1242. */
  1243. if (!mapped_name && !(state->oterm.type >> 16)) {
  1244. if ((state->oterm.type & 0xff00) == 0x0100)
  1245. append_ctl_name(kctl, " Capture");
  1246. else
  1247. append_ctl_name(kctl, " Playback");
  1248. }
  1249. append_ctl_name(kctl, control == UAC_FU_MUTE ?
  1250. " Switch" : " Volume");
  1251. break;
  1252. default:
  1253. if (!len)
  1254. strlcpy(kctl->id.name, audio_feature_info[control-1].name,
  1255. sizeof(kctl->id.name));
  1256. break;
  1257. }
  1258. /* get min/max values */
  1259. get_min_max_with_quirks(cval, 0, kctl);
  1260. if (control == UAC_FU_VOLUME) {
  1261. check_mapped_dB(map, cval);
  1262. if (cval->dBmin < cval->dBmax || !cval->initialized) {
  1263. kctl->tlv.c = snd_usb_mixer_vol_tlv;
  1264. kctl->vd[0].access |=
  1265. SNDRV_CTL_ELEM_ACCESS_TLV_READ |
  1266. SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
  1267. }
  1268. }
  1269. snd_usb_mixer_fu_apply_quirk(state->mixer, cval, unitid, kctl);
  1270. range = (cval->max - cval->min) / cval->res;
  1271. /*
  1272. * Are there devices with volume range more than 255? I use a bit more
  1273. * to be sure. 384 is a resolution magic number found on Logitech
  1274. * devices. It will definitively catch all buggy Logitech devices.
  1275. */
  1276. if (range > 384) {
  1277. usb_audio_warn(state->chip,
  1278. "Warning! Unlikely big volume range (=%u), cval->res is probably wrong.",
  1279. range);
  1280. usb_audio_warn(state->chip,
  1281. "[%d] FU [%s] ch = %d, val = %d/%d/%d",
  1282. cval->head.id, kctl->id.name, cval->channels,
  1283. cval->min, cval->max, cval->res);
  1284. }
  1285. usb_audio_dbg(state->chip, "[%d] FU [%s] ch = %d, val = %d/%d/%d\n",
  1286. cval->head.id, kctl->id.name, cval->channels,
  1287. cval->min, cval->max, cval->res);
  1288. snd_usb_mixer_add_control(&cval->head, kctl);
  1289. }
  1290. static int parse_clock_source_unit(struct mixer_build *state, int unitid,
  1291. void *_ftr)
  1292. {
  1293. struct uac_clock_source_descriptor *hdr = _ftr;
  1294. struct usb_mixer_elem_info *cval;
  1295. struct snd_kcontrol *kctl;
  1296. char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
  1297. int ret;
  1298. if (state->mixer->protocol != UAC_VERSION_2)
  1299. return -EINVAL;
  1300. if (hdr->bLength != sizeof(*hdr)) {
  1301. usb_audio_dbg(state->chip,
  1302. "Bogus clock source descriptor length of %d, ignoring.\n",
  1303. hdr->bLength);
  1304. return 0;
  1305. }
  1306. /*
  1307. * The only property of this unit we are interested in is the
  1308. * clock source validity. If that isn't readable, just bail out.
  1309. */
  1310. if (!uac2_control_is_readable(hdr->bmControls,
  1311. ilog2(UAC2_CS_CONTROL_CLOCK_VALID)))
  1312. return 0;
  1313. cval = kzalloc(sizeof(*cval), GFP_KERNEL);
  1314. if (!cval)
  1315. return -ENOMEM;
  1316. snd_usb_mixer_elem_init_std(&cval->head, state->mixer, hdr->bClockID);
  1317. cval->min = 0;
  1318. cval->max = 1;
  1319. cval->channels = 1;
  1320. cval->val_type = USB_MIXER_BOOLEAN;
  1321. cval->control = UAC2_CS_CONTROL_CLOCK_VALID;
  1322. if (uac2_control_is_writeable(hdr->bmControls,
  1323. ilog2(UAC2_CS_CONTROL_CLOCK_VALID)))
  1324. kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
  1325. else {
  1326. cval->master_readonly = 1;
  1327. kctl = snd_ctl_new1(&usb_feature_unit_ctl_ro, cval);
  1328. }
  1329. if (!kctl) {
  1330. kfree(cval);
  1331. return -ENOMEM;
  1332. }
  1333. kctl->private_free = snd_usb_mixer_elem_free;
  1334. ret = snd_usb_copy_string_desc(state, hdr->iClockSource,
  1335. name, sizeof(name));
  1336. if (ret > 0)
  1337. snprintf(kctl->id.name, sizeof(kctl->id.name),
  1338. "%s Validity", name);
  1339. else
  1340. snprintf(kctl->id.name, sizeof(kctl->id.name),
  1341. "Clock Source %d Validity", hdr->bClockID);
  1342. return snd_usb_mixer_add_control(&cval->head, kctl);
  1343. }
  1344. /*
  1345. * parse a feature unit
  1346. *
  1347. * most of controls are defined here.
  1348. */
  1349. static int parse_audio_feature_unit(struct mixer_build *state, int unitid,
  1350. void *_ftr)
  1351. {
  1352. int channels, i, j;
  1353. struct usb_audio_term iterm;
  1354. unsigned int master_bits, first_ch_bits;
  1355. int err, csize;
  1356. struct uac_feature_unit_descriptor *hdr = _ftr;
  1357. __u8 *bmaControls;
  1358. if (state->mixer->protocol == UAC_VERSION_1) {
  1359. if (hdr->bLength < 7) {
  1360. usb_audio_err(state->chip,
  1361. "unit %u: invalid UAC_FEATURE_UNIT descriptor\n",
  1362. unitid);
  1363. return -EINVAL;
  1364. }
  1365. csize = hdr->bControlSize;
  1366. if (!csize) {
  1367. usb_audio_dbg(state->chip,
  1368. "unit %u: invalid bControlSize == 0\n",
  1369. unitid);
  1370. return -EINVAL;
  1371. }
  1372. channels = (hdr->bLength - 7) / csize - 1;
  1373. bmaControls = hdr->bmaControls;
  1374. if (hdr->bLength < 7 + csize) {
  1375. usb_audio_err(state->chip,
  1376. "unit %u: invalid UAC_FEATURE_UNIT descriptor\n",
  1377. unitid);
  1378. return -EINVAL;
  1379. }
  1380. } else {
  1381. struct uac2_feature_unit_descriptor *ftr = _ftr;
  1382. if (hdr->bLength < 6) {
  1383. usb_audio_err(state->chip,
  1384. "unit %u: invalid UAC_FEATURE_UNIT descriptor\n",
  1385. unitid);
  1386. return -EINVAL;
  1387. }
  1388. csize = 4;
  1389. channels = (hdr->bLength - 6) / 4 - 1;
  1390. bmaControls = ftr->bmaControls;
  1391. if (hdr->bLength < 6 + csize) {
  1392. usb_audio_err(state->chip,
  1393. "unit %u: invalid UAC_FEATURE_UNIT descriptor\n",
  1394. unitid);
  1395. return -EINVAL;
  1396. }
  1397. }
  1398. /* parse the source unit */
  1399. if ((err = parse_audio_unit(state, hdr->bSourceID)) < 0)
  1400. return err;
  1401. /* determine the input source type and name */
  1402. err = check_input_term(state, hdr->bSourceID, &iterm);
  1403. if (err < 0)
  1404. return err;
  1405. master_bits = snd_usb_combine_bytes(bmaControls, csize);
  1406. /* master configuration quirks */
  1407. switch (state->chip->usb_id) {
  1408. case USB_ID(0x08bb, 0x2702):
  1409. usb_audio_info(state->chip,
  1410. "usbmixer: master volume quirk for PCM2702 chip\n");
  1411. /* disable non-functional volume control */
  1412. master_bits &= ~UAC_CONTROL_BIT(UAC_FU_VOLUME);
  1413. break;
  1414. case USB_ID(0x1130, 0xf211):
  1415. usb_audio_info(state->chip,
  1416. "usbmixer: volume control quirk for Tenx TP6911 Audio Headset\n");
  1417. /* disable non-functional volume control */
  1418. channels = 0;
  1419. break;
  1420. }
  1421. if (channels > 0)
  1422. first_ch_bits = snd_usb_combine_bytes(bmaControls + csize, csize);
  1423. else
  1424. first_ch_bits = 0;
  1425. if (state->mixer->protocol == UAC_VERSION_1) {
  1426. /* check all control types */
  1427. for (i = 0; i < 10; i++) {
  1428. unsigned int ch_bits = 0;
  1429. for (j = 0; j < channels; j++) {
  1430. unsigned int mask;
  1431. mask = snd_usb_combine_bytes(bmaControls +
  1432. csize * (j+1), csize);
  1433. if (mask & (1 << i))
  1434. ch_bits |= (1 << j);
  1435. }
  1436. /* audio class v1 controls are never read-only */
  1437. /*
  1438. * The first channel must be set
  1439. * (for ease of programming).
  1440. */
  1441. if (ch_bits & 1)
  1442. build_feature_ctl(state, _ftr, ch_bits, i,
  1443. &iterm, unitid, 0);
  1444. if (master_bits & (1 << i))
  1445. build_feature_ctl(state, _ftr, 0, i, &iterm,
  1446. unitid, 0);
  1447. }
  1448. } else { /* UAC_VERSION_2 */
  1449. for (i = 0; i < ARRAY_SIZE(audio_feature_info); i++) {
  1450. unsigned int ch_bits = 0;
  1451. unsigned int ch_read_only = 0;
  1452. for (j = 0; j < channels; j++) {
  1453. unsigned int mask;
  1454. mask = snd_usb_combine_bytes(bmaControls +
  1455. csize * (j+1), csize);
  1456. if (uac2_control_is_readable(mask, i)) {
  1457. ch_bits |= (1 << j);
  1458. if (!uac2_control_is_writeable(mask, i))
  1459. ch_read_only |= (1 << j);
  1460. }
  1461. }
  1462. /*
  1463. * NOTE: build_feature_ctl() will mark the control
  1464. * read-only if all channels are marked read-only in
  1465. * the descriptors. Otherwise, the control will be
  1466. * reported as writeable, but the driver will not
  1467. * actually issue a write command for read-only
  1468. * channels.
  1469. */
  1470. /*
  1471. * The first channel must be set
  1472. * (for ease of programming).
  1473. */
  1474. if (ch_bits & 1)
  1475. build_feature_ctl(state, _ftr, ch_bits, i,
  1476. &iterm, unitid, ch_read_only);
  1477. if (uac2_control_is_readable(master_bits, i))
  1478. build_feature_ctl(state, _ftr, 0, i, &iterm, unitid,
  1479. !uac2_control_is_writeable(master_bits, i));
  1480. }
  1481. }
  1482. return 0;
  1483. }
  1484. /*
  1485. * Mixer Unit
  1486. */
  1487. /*
  1488. * build a mixer unit control
  1489. *
  1490. * the callbacks are identical with feature unit.
  1491. * input channel number (zero based) is given in control field instead.
  1492. */
  1493. static void build_mixer_unit_ctl(struct mixer_build *state,
  1494. struct uac_mixer_unit_descriptor *desc,
  1495. int in_pin, int in_ch, int unitid,
  1496. struct usb_audio_term *iterm)
  1497. {
  1498. struct usb_mixer_elem_info *cval;
  1499. unsigned int num_outs = uac_mixer_unit_bNrChannels(desc);
  1500. unsigned int i, len;
  1501. struct snd_kcontrol *kctl;
  1502. const struct usbmix_name_map *map;
  1503. map = find_map(state, unitid, 0);
  1504. if (check_ignored_ctl(map))
  1505. return;
  1506. cval = kzalloc(sizeof(*cval), GFP_KERNEL);
  1507. if (!cval)
  1508. return;
  1509. snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid);
  1510. cval->control = in_ch + 1; /* based on 1 */
  1511. cval->val_type = USB_MIXER_S16;
  1512. for (i = 0; i < num_outs; i++) {
  1513. __u8 *c = uac_mixer_unit_bmControls(desc, state->mixer->protocol);
  1514. if (check_matrix_bitmap(c, in_ch, i, num_outs)) {
  1515. cval->cmask |= (1 << i);
  1516. cval->channels++;
  1517. }
  1518. }
  1519. /* get min/max values */
  1520. get_min_max(cval, 0);
  1521. kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
  1522. if (!kctl) {
  1523. usb_audio_err(state->chip, "cannot malloc kcontrol\n");
  1524. kfree(cval);
  1525. return;
  1526. }
  1527. kctl->private_free = snd_usb_mixer_elem_free;
  1528. len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
  1529. if (!len)
  1530. len = get_term_name(state, iterm, kctl->id.name,
  1531. sizeof(kctl->id.name), 0);
  1532. if (!len)
  1533. len = sprintf(kctl->id.name, "Mixer Source %d", in_ch + 1);
  1534. append_ctl_name(kctl, " Volume");
  1535. usb_audio_dbg(state->chip, "[%d] MU [%s] ch = %d, val = %d/%d\n",
  1536. cval->head.id, kctl->id.name, cval->channels, cval->min, cval->max);
  1537. snd_usb_mixer_add_control(&cval->head, kctl);
  1538. }
  1539. /*
  1540. * parse a mixer unit
  1541. */
  1542. static int parse_audio_mixer_unit(struct mixer_build *state, int unitid,
  1543. void *raw_desc)
  1544. {
  1545. struct uac_mixer_unit_descriptor *desc = raw_desc;
  1546. struct usb_audio_term iterm;
  1547. int input_pins, num_ins, num_outs;
  1548. int pin, ich, err;
  1549. if (desc->bLength < 11 || !(input_pins = desc->bNrInPins) ||
  1550. desc->bLength < sizeof(*desc) + desc->bNrInPins ||
  1551. !(num_outs = uac_mixer_unit_bNrChannels(desc))) {
  1552. usb_audio_err(state->chip,
  1553. "invalid MIXER UNIT descriptor %d\n",
  1554. unitid);
  1555. return -EINVAL;
  1556. }
  1557. num_ins = 0;
  1558. ich = 0;
  1559. for (pin = 0; pin < input_pins; pin++) {
  1560. err = parse_audio_unit(state, desc->baSourceID[pin]);
  1561. if (err < 0)
  1562. continue;
  1563. /* no bmControls field (e.g. Maya44) -> ignore */
  1564. if (desc->bLength <= 10 + input_pins)
  1565. continue;
  1566. err = check_input_term(state, desc->baSourceID[pin], &iterm);
  1567. if (err < 0)
  1568. return err;
  1569. num_ins += iterm.channels;
  1570. for (; ich < num_ins; ich++) {
  1571. int och, ich_has_controls = 0;
  1572. for (och = 0; och < num_outs; och++) {
  1573. __u8 *c = uac_mixer_unit_bmControls(desc,
  1574. state->mixer->protocol);
  1575. if (check_matrix_bitmap(c, ich, och, num_outs)) {
  1576. ich_has_controls = 1;
  1577. break;
  1578. }
  1579. }
  1580. if (ich_has_controls)
  1581. build_mixer_unit_ctl(state, desc, pin, ich,
  1582. unitid, &iterm);
  1583. }
  1584. }
  1585. return 0;
  1586. }
  1587. /*
  1588. * Processing Unit / Extension Unit
  1589. */
  1590. /* get callback for processing/extension unit */
  1591. static int mixer_ctl_procunit_get(struct snd_kcontrol *kcontrol,
  1592. struct snd_ctl_elem_value *ucontrol)
  1593. {
  1594. struct usb_mixer_elem_info *cval = kcontrol->private_data;
  1595. int err, val;
  1596. err = get_cur_ctl_value(cval, cval->control << 8, &val);
  1597. if (err < 0) {
  1598. ucontrol->value.integer.value[0] = cval->min;
  1599. return filter_error(cval, err);
  1600. }
  1601. val = get_relative_value(cval, val);
  1602. ucontrol->value.integer.value[0] = val;
  1603. return 0;
  1604. }
  1605. /* put callback for processing/extension unit */
  1606. static int mixer_ctl_procunit_put(struct snd_kcontrol *kcontrol,
  1607. struct snd_ctl_elem_value *ucontrol)
  1608. {
  1609. struct usb_mixer_elem_info *cval = kcontrol->private_data;
  1610. int val, oval, err;
  1611. err = get_cur_ctl_value(cval, cval->control << 8, &oval);
  1612. if (err < 0)
  1613. return filter_error(cval, err);
  1614. val = ucontrol->value.integer.value[0];
  1615. val = get_abs_value(cval, val);
  1616. if (val != oval) {
  1617. set_cur_ctl_value(cval, cval->control << 8, val);
  1618. return 1;
  1619. }
  1620. return 0;
  1621. }
  1622. /* alsa control interface for processing/extension unit */
  1623. static const struct snd_kcontrol_new mixer_procunit_ctl = {
  1624. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1625. .name = "", /* will be filled later */
  1626. .info = mixer_ctl_feature_info,
  1627. .get = mixer_ctl_procunit_get,
  1628. .put = mixer_ctl_procunit_put,
  1629. };
  1630. /*
  1631. * predefined data for processing units
  1632. */
  1633. struct procunit_value_info {
  1634. int control;
  1635. char *suffix;
  1636. int val_type;
  1637. int min_value;
  1638. };
  1639. struct procunit_info {
  1640. int type;
  1641. char *name;
  1642. struct procunit_value_info *values;
  1643. };
  1644. static struct procunit_value_info updown_proc_info[] = {
  1645. { UAC_UD_ENABLE, "Switch", USB_MIXER_BOOLEAN },
  1646. { UAC_UD_MODE_SELECT, "Mode Select", USB_MIXER_U8, 1 },
  1647. { 0 }
  1648. };
  1649. static struct procunit_value_info prologic_proc_info[] = {
  1650. { UAC_DP_ENABLE, "Switch", USB_MIXER_BOOLEAN },
  1651. { UAC_DP_MODE_SELECT, "Mode Select", USB_MIXER_U8, 1 },
  1652. { 0 }
  1653. };
  1654. static struct procunit_value_info threed_enh_proc_info[] = {
  1655. { UAC_3D_ENABLE, "Switch", USB_MIXER_BOOLEAN },
  1656. { UAC_3D_SPACE, "Spaciousness", USB_MIXER_U8 },
  1657. { 0 }
  1658. };
  1659. static struct procunit_value_info reverb_proc_info[] = {
  1660. { UAC_REVERB_ENABLE, "Switch", USB_MIXER_BOOLEAN },
  1661. { UAC_REVERB_LEVEL, "Level", USB_MIXER_U8 },
  1662. { UAC_REVERB_TIME, "Time", USB_MIXER_U16 },
  1663. { UAC_REVERB_FEEDBACK, "Feedback", USB_MIXER_U8 },
  1664. { 0 }
  1665. };
  1666. static struct procunit_value_info chorus_proc_info[] = {
  1667. { UAC_CHORUS_ENABLE, "Switch", USB_MIXER_BOOLEAN },
  1668. { UAC_CHORUS_LEVEL, "Level", USB_MIXER_U8 },
  1669. { UAC_CHORUS_RATE, "Rate", USB_MIXER_U16 },
  1670. { UAC_CHORUS_DEPTH, "Depth", USB_MIXER_U16 },
  1671. { 0 }
  1672. };
  1673. static struct procunit_value_info dcr_proc_info[] = {
  1674. { UAC_DCR_ENABLE, "Switch", USB_MIXER_BOOLEAN },
  1675. { UAC_DCR_RATE, "Ratio", USB_MIXER_U16 },
  1676. { UAC_DCR_MAXAMPL, "Max Amp", USB_MIXER_S16 },
  1677. { UAC_DCR_THRESHOLD, "Threshold", USB_MIXER_S16 },
  1678. { UAC_DCR_ATTACK_TIME, "Attack Time", USB_MIXER_U16 },
  1679. { UAC_DCR_RELEASE_TIME, "Release Time", USB_MIXER_U16 },
  1680. { 0 }
  1681. };
  1682. static struct procunit_info procunits[] = {
  1683. { UAC_PROCESS_UP_DOWNMIX, "Up Down", updown_proc_info },
  1684. { UAC_PROCESS_DOLBY_PROLOGIC, "Dolby Prologic", prologic_proc_info },
  1685. { UAC_PROCESS_STEREO_EXTENDER, "3D Stereo Extender", threed_enh_proc_info },
  1686. { UAC_PROCESS_REVERB, "Reverb", reverb_proc_info },
  1687. { UAC_PROCESS_CHORUS, "Chorus", chorus_proc_info },
  1688. { UAC_PROCESS_DYN_RANGE_COMP, "DCR", dcr_proc_info },
  1689. { 0 },
  1690. };
  1691. /*
  1692. * predefined data for extension units
  1693. */
  1694. static struct procunit_value_info clock_rate_xu_info[] = {
  1695. { USB_XU_CLOCK_RATE_SELECTOR, "Selector", USB_MIXER_U8, 0 },
  1696. { 0 }
  1697. };
  1698. static struct procunit_value_info clock_source_xu_info[] = {
  1699. { USB_XU_CLOCK_SOURCE_SELECTOR, "External", USB_MIXER_BOOLEAN },
  1700. { 0 }
  1701. };
  1702. static struct procunit_value_info spdif_format_xu_info[] = {
  1703. { USB_XU_DIGITAL_FORMAT_SELECTOR, "SPDIF/AC3", USB_MIXER_BOOLEAN },
  1704. { 0 }
  1705. };
  1706. static struct procunit_value_info soft_limit_xu_info[] = {
  1707. { USB_XU_SOFT_LIMIT_SELECTOR, " ", USB_MIXER_BOOLEAN },
  1708. { 0 }
  1709. };
  1710. static struct procunit_info extunits[] = {
  1711. { USB_XU_CLOCK_RATE, "Clock rate", clock_rate_xu_info },
  1712. { USB_XU_CLOCK_SOURCE, "DigitalIn CLK source", clock_source_xu_info },
  1713. { USB_XU_DIGITAL_IO_STATUS, "DigitalOut format:", spdif_format_xu_info },
  1714. { USB_XU_DEVICE_OPTIONS, "AnalogueIn Soft Limit", soft_limit_xu_info },
  1715. { 0 }
  1716. };
  1717. /*
  1718. * build a processing/extension unit
  1719. */
  1720. static int build_audio_procunit(struct mixer_build *state, int unitid,
  1721. void *raw_desc, struct procunit_info *list,
  1722. char *name)
  1723. {
  1724. struct uac_processing_unit_descriptor *desc = raw_desc;
  1725. int num_ins;
  1726. struct usb_mixer_elem_info *cval;
  1727. struct snd_kcontrol *kctl;
  1728. int i, err, nameid, type, len;
  1729. struct procunit_info *info;
  1730. struct procunit_value_info *valinfo;
  1731. const struct usbmix_name_map *map;
  1732. static struct procunit_value_info default_value_info[] = {
  1733. { 0x01, "Switch", USB_MIXER_BOOLEAN },
  1734. { 0 }
  1735. };
  1736. static struct procunit_info default_info = {
  1737. 0, NULL, default_value_info
  1738. };
  1739. if (desc->bLength < 13) {
  1740. usb_audio_err(state->chip, "invalid %s descriptor (id %d)\n", name, unitid);
  1741. return -EINVAL;
  1742. }
  1743. num_ins = desc->bNrInPins;
  1744. if (desc->bLength < 13 + num_ins ||
  1745. desc->bLength < num_ins + uac_processing_unit_bControlSize(desc, state->mixer->protocol)) {
  1746. usb_audio_err(state->chip, "invalid %s descriptor (id %d)\n", name, unitid);
  1747. return -EINVAL;
  1748. }
  1749. for (i = 0; i < num_ins; i++) {
  1750. if ((err = parse_audio_unit(state, desc->baSourceID[i])) < 0)
  1751. return err;
  1752. }
  1753. type = le16_to_cpu(desc->wProcessType);
  1754. for (info = list; info && info->type; info++)
  1755. if (info->type == type)
  1756. break;
  1757. if (!info || !info->type)
  1758. info = &default_info;
  1759. for (valinfo = info->values; valinfo->control; valinfo++) {
  1760. __u8 *controls = uac_processing_unit_bmControls(desc, state->mixer->protocol);
  1761. if (!(controls[valinfo->control / 8] & (1 << ((valinfo->control % 8) - 1))))
  1762. continue;
  1763. map = find_map(state, unitid, valinfo->control);
  1764. if (check_ignored_ctl(map))
  1765. continue;
  1766. cval = kzalloc(sizeof(*cval), GFP_KERNEL);
  1767. if (!cval)
  1768. return -ENOMEM;
  1769. snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid);
  1770. cval->control = valinfo->control;
  1771. cval->val_type = valinfo->val_type;
  1772. cval->channels = 1;
  1773. /* get min/max values */
  1774. if (type == UAC_PROCESS_UP_DOWNMIX && cval->control == UAC_UD_MODE_SELECT) {
  1775. __u8 *control_spec = uac_processing_unit_specific(desc, state->mixer->protocol);
  1776. /* FIXME: hard-coded */
  1777. cval->min = 1;
  1778. cval->max = control_spec[0];
  1779. cval->res = 1;
  1780. cval->initialized = 1;
  1781. } else {
  1782. if (type == USB_XU_CLOCK_RATE) {
  1783. /*
  1784. * E-Mu USB 0404/0202/TrackerPre/0204
  1785. * samplerate control quirk
  1786. */
  1787. cval->min = 0;
  1788. cval->max = 5;
  1789. cval->res = 1;
  1790. cval->initialized = 1;
  1791. } else
  1792. get_min_max(cval, valinfo->min_value);
  1793. }
  1794. kctl = snd_ctl_new1(&mixer_procunit_ctl, cval);
  1795. if (!kctl) {
  1796. kfree(cval);
  1797. return -ENOMEM;
  1798. }
  1799. kctl->private_free = snd_usb_mixer_elem_free;
  1800. if (check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name))) {
  1801. /* nothing */ ;
  1802. } else if (info->name) {
  1803. strlcpy(kctl->id.name, info->name, sizeof(kctl->id.name));
  1804. } else {
  1805. nameid = uac_processing_unit_iProcessing(desc, state->mixer->protocol);
  1806. len = 0;
  1807. if (nameid)
  1808. len = snd_usb_copy_string_desc(state, nameid,
  1809. kctl->id.name,
  1810. sizeof(kctl->id.name));
  1811. if (!len)
  1812. strlcpy(kctl->id.name, name, sizeof(kctl->id.name));
  1813. }
  1814. append_ctl_name(kctl, " ");
  1815. append_ctl_name(kctl, valinfo->suffix);
  1816. usb_audio_dbg(state->chip,
  1817. "[%d] PU [%s] ch = %d, val = %d/%d\n",
  1818. cval->head.id, kctl->id.name, cval->channels,
  1819. cval->min, cval->max);
  1820. err = snd_usb_mixer_add_control(&cval->head, kctl);
  1821. if (err < 0)
  1822. return err;
  1823. }
  1824. return 0;
  1825. }
  1826. static int parse_audio_processing_unit(struct mixer_build *state, int unitid,
  1827. void *raw_desc)
  1828. {
  1829. return build_audio_procunit(state, unitid, raw_desc,
  1830. procunits, "Processing Unit");
  1831. }
  1832. static int parse_audio_extension_unit(struct mixer_build *state, int unitid,
  1833. void *raw_desc)
  1834. {
  1835. /*
  1836. * Note that we parse extension units with processing unit descriptors.
  1837. * That's ok as the layout is the same.
  1838. */
  1839. return build_audio_procunit(state, unitid, raw_desc,
  1840. extunits, "Extension Unit");
  1841. }
  1842. /*
  1843. * Selector Unit
  1844. */
  1845. /*
  1846. * info callback for selector unit
  1847. * use an enumerator type for routing
  1848. */
  1849. static int mixer_ctl_selector_info(struct snd_kcontrol *kcontrol,
  1850. struct snd_ctl_elem_info *uinfo)
  1851. {
  1852. struct usb_mixer_elem_info *cval = kcontrol->private_data;
  1853. const char **itemlist = (const char **)kcontrol->private_value;
  1854. if (snd_BUG_ON(!itemlist))
  1855. return -EINVAL;
  1856. return snd_ctl_enum_info(uinfo, 1, cval->max, itemlist);
  1857. }
  1858. /* get callback for selector unit */
  1859. static int mixer_ctl_selector_get(struct snd_kcontrol *kcontrol,
  1860. struct snd_ctl_elem_value *ucontrol)
  1861. {
  1862. struct usb_mixer_elem_info *cval = kcontrol->private_data;
  1863. int val, err;
  1864. err = get_cur_ctl_value(cval, cval->control << 8, &val);
  1865. if (err < 0) {
  1866. ucontrol->value.enumerated.item[0] = 0;
  1867. return filter_error(cval, err);
  1868. }
  1869. val = get_relative_value(cval, val);
  1870. ucontrol->value.enumerated.item[0] = val;
  1871. return 0;
  1872. }
  1873. /* put callback for selector unit */
  1874. static int mixer_ctl_selector_put(struct snd_kcontrol *kcontrol,
  1875. struct snd_ctl_elem_value *ucontrol)
  1876. {
  1877. struct usb_mixer_elem_info *cval = kcontrol->private_data;
  1878. int val, oval, err;
  1879. err = get_cur_ctl_value(cval, cval->control << 8, &oval);
  1880. if (err < 0)
  1881. return filter_error(cval, err);
  1882. val = ucontrol->value.enumerated.item[0];
  1883. val = get_abs_value(cval, val);
  1884. if (val != oval) {
  1885. set_cur_ctl_value(cval, cval->control << 8, val);
  1886. return 1;
  1887. }
  1888. return 0;
  1889. }
  1890. /* alsa control interface for selector unit */
  1891. static const struct snd_kcontrol_new mixer_selectunit_ctl = {
  1892. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1893. .name = "", /* will be filled later */
  1894. .info = mixer_ctl_selector_info,
  1895. .get = mixer_ctl_selector_get,
  1896. .put = mixer_ctl_selector_put,
  1897. };
  1898. /*
  1899. * private free callback.
  1900. * free both private_data and private_value
  1901. */
  1902. static void usb_mixer_selector_elem_free(struct snd_kcontrol *kctl)
  1903. {
  1904. int i, num_ins = 0;
  1905. if (kctl->private_data) {
  1906. struct usb_mixer_elem_info *cval = kctl->private_data;
  1907. num_ins = cval->max;
  1908. kfree(cval);
  1909. kctl->private_data = NULL;
  1910. }
  1911. if (kctl->private_value) {
  1912. char **itemlist = (char **)kctl->private_value;
  1913. for (i = 0; i < num_ins; i++)
  1914. kfree(itemlist[i]);
  1915. kfree(itemlist);
  1916. kctl->private_value = 0;
  1917. }
  1918. }
  1919. /*
  1920. * parse a selector unit
  1921. */
  1922. static int parse_audio_selector_unit(struct mixer_build *state, int unitid,
  1923. void *raw_desc)
  1924. {
  1925. struct uac_selector_unit_descriptor *desc = raw_desc;
  1926. unsigned int i, nameid, len;
  1927. int err;
  1928. struct usb_mixer_elem_info *cval;
  1929. struct snd_kcontrol *kctl;
  1930. const struct usbmix_name_map *map;
  1931. char **namelist;
  1932. if (desc->bLength < 5 || !desc->bNrInPins ||
  1933. desc->bLength < 5 + desc->bNrInPins) {
  1934. usb_audio_err(state->chip,
  1935. "invalid SELECTOR UNIT descriptor %d\n", unitid);
  1936. return -EINVAL;
  1937. }
  1938. for (i = 0; i < desc->bNrInPins; i++) {
  1939. if ((err = parse_audio_unit(state, desc->baSourceID[i])) < 0)
  1940. return err;
  1941. }
  1942. if (desc->bNrInPins == 1) /* only one ? nonsense! */
  1943. return 0;
  1944. map = find_map(state, unitid, 0);
  1945. if (check_ignored_ctl(map))
  1946. return 0;
  1947. cval = kzalloc(sizeof(*cval), GFP_KERNEL);
  1948. if (!cval)
  1949. return -ENOMEM;
  1950. snd_usb_mixer_elem_init_std(&cval->head, state->mixer, unitid);
  1951. cval->val_type = USB_MIXER_U8;
  1952. cval->channels = 1;
  1953. cval->min = 1;
  1954. cval->max = desc->bNrInPins;
  1955. cval->res = 1;
  1956. cval->initialized = 1;
  1957. if (state->mixer->protocol == UAC_VERSION_1)
  1958. cval->control = 0;
  1959. else /* UAC_VERSION_2 */
  1960. cval->control = (desc->bDescriptorSubtype == UAC2_CLOCK_SELECTOR) ?
  1961. UAC2_CX_CLOCK_SELECTOR : UAC2_SU_SELECTOR;
  1962. namelist = kmalloc(sizeof(char *) * desc->bNrInPins, GFP_KERNEL);
  1963. if (!namelist) {
  1964. kfree(cval);
  1965. return -ENOMEM;
  1966. }
  1967. #define MAX_ITEM_NAME_LEN 64
  1968. for (i = 0; i < desc->bNrInPins; i++) {
  1969. struct usb_audio_term iterm;
  1970. len = 0;
  1971. namelist[i] = kmalloc(MAX_ITEM_NAME_LEN, GFP_KERNEL);
  1972. if (!namelist[i]) {
  1973. while (i--)
  1974. kfree(namelist[i]);
  1975. kfree(namelist);
  1976. kfree(cval);
  1977. return -ENOMEM;
  1978. }
  1979. len = check_mapped_selector_name(state, unitid, i, namelist[i],
  1980. MAX_ITEM_NAME_LEN);
  1981. if (! len && check_input_term(state, desc->baSourceID[i], &iterm) >= 0)
  1982. len = get_term_name(state, &iterm, namelist[i], MAX_ITEM_NAME_LEN, 0);
  1983. if (! len)
  1984. sprintf(namelist[i], "Input %u", i);
  1985. }
  1986. kctl = snd_ctl_new1(&mixer_selectunit_ctl, cval);
  1987. if (! kctl) {
  1988. usb_audio_err(state->chip, "cannot malloc kcontrol\n");
  1989. for (i = 0; i < desc->bNrInPins; i++)
  1990. kfree(namelist[i]);
  1991. kfree(namelist);
  1992. kfree(cval);
  1993. return -ENOMEM;
  1994. }
  1995. kctl->private_value = (unsigned long)namelist;
  1996. kctl->private_free = usb_mixer_selector_elem_free;
  1997. /* check the static mapping table at first */
  1998. len = check_mapped_name(map, kctl->id.name, sizeof(kctl->id.name));
  1999. if (!len) {
  2000. /* no mapping ? */
  2001. /* if iSelector is given, use it */
  2002. nameid = uac_selector_unit_iSelector(desc);
  2003. if (nameid)
  2004. len = snd_usb_copy_string_desc(state, nameid,
  2005. kctl->id.name,
  2006. sizeof(kctl->id.name));
  2007. /* ... or pick up the terminal name at next */
  2008. if (!len)
  2009. len = get_term_name(state, &state->oterm,
  2010. kctl->id.name, sizeof(kctl->id.name), 0);
  2011. /* ... or use the fixed string "USB" as the last resort */
  2012. if (!len)
  2013. strlcpy(kctl->id.name, "USB", sizeof(kctl->id.name));
  2014. /* and add the proper suffix */
  2015. if (desc->bDescriptorSubtype == UAC2_CLOCK_SELECTOR)
  2016. append_ctl_name(kctl, " Clock Source");
  2017. else if ((state->oterm.type & 0xff00) == 0x0100)
  2018. append_ctl_name(kctl, " Capture Source");
  2019. else
  2020. append_ctl_name(kctl, " Playback Source");
  2021. }
  2022. usb_audio_dbg(state->chip, "[%d] SU [%s] items = %d\n",
  2023. cval->head.id, kctl->id.name, desc->bNrInPins);
  2024. return snd_usb_mixer_add_control(&cval->head, kctl);
  2025. }
  2026. /*
  2027. * parse an audio unit recursively
  2028. */
  2029. static int parse_audio_unit(struct mixer_build *state, int unitid)
  2030. {
  2031. unsigned char *p1;
  2032. if (test_and_set_bit(unitid, state->unitbitmap))
  2033. return 0; /* the unit already visited */
  2034. p1 = find_audio_control_unit(state, unitid);
  2035. if (!p1) {
  2036. usb_audio_err(state->chip, "unit %d not found!\n", unitid);
  2037. return -EINVAL;
  2038. }
  2039. switch (p1[2]) {
  2040. case UAC_INPUT_TERMINAL:
  2041. return 0; /* NOP */
  2042. case UAC_MIXER_UNIT:
  2043. return parse_audio_mixer_unit(state, unitid, p1);
  2044. case UAC2_CLOCK_SOURCE:
  2045. return parse_clock_source_unit(state, unitid, p1);
  2046. case UAC_SELECTOR_UNIT:
  2047. case UAC2_CLOCK_SELECTOR:
  2048. return parse_audio_selector_unit(state, unitid, p1);
  2049. case UAC_FEATURE_UNIT:
  2050. return parse_audio_feature_unit(state, unitid, p1);
  2051. case UAC1_PROCESSING_UNIT:
  2052. /* UAC2_EFFECT_UNIT has the same value */
  2053. if (state->mixer->protocol == UAC_VERSION_1)
  2054. return parse_audio_processing_unit(state, unitid, p1);
  2055. else
  2056. return 0; /* FIXME - effect units not implemented yet */
  2057. case UAC1_EXTENSION_UNIT:
  2058. /* UAC2_PROCESSING_UNIT_V2 has the same value */
  2059. if (state->mixer->protocol == UAC_VERSION_1)
  2060. return parse_audio_extension_unit(state, unitid, p1);
  2061. else /* UAC_VERSION_2 */
  2062. return parse_audio_processing_unit(state, unitid, p1);
  2063. case UAC2_EXTENSION_UNIT_V2:
  2064. return parse_audio_extension_unit(state, unitid, p1);
  2065. default:
  2066. usb_audio_err(state->chip,
  2067. "unit %u: unexpected type 0x%02x\n", unitid, p1[2]);
  2068. return -EINVAL;
  2069. }
  2070. }
  2071. static void snd_usb_mixer_free(struct usb_mixer_interface *mixer)
  2072. {
  2073. /* kill pending URBs */
  2074. snd_usb_mixer_disconnect(mixer);
  2075. kfree(mixer->id_elems);
  2076. if (mixer->urb) {
  2077. kfree(mixer->urb->transfer_buffer);
  2078. usb_free_urb(mixer->urb);
  2079. }
  2080. usb_free_urb(mixer->rc_urb);
  2081. kfree(mixer->rc_setup_packet);
  2082. kfree(mixer);
  2083. }
  2084. static int snd_usb_mixer_dev_free(struct snd_device *device)
  2085. {
  2086. struct usb_mixer_interface *mixer = device->device_data;
  2087. snd_usb_mixer_free(mixer);
  2088. return 0;
  2089. }
  2090. /*
  2091. * create mixer controls
  2092. *
  2093. * walk through all UAC_OUTPUT_TERMINAL descriptors to search for mixers
  2094. */
  2095. static int snd_usb_mixer_controls(struct usb_mixer_interface *mixer)
  2096. {
  2097. struct mixer_build state;
  2098. int err;
  2099. const struct usbmix_ctl_map *map;
  2100. void *p;
  2101. memset(&state, 0, sizeof(state));
  2102. state.chip = mixer->chip;
  2103. state.mixer = mixer;
  2104. state.buffer = mixer->hostif->extra;
  2105. state.buflen = mixer->hostif->extralen;
  2106. /* check the mapping table */
  2107. for (map = usbmix_ctl_maps; map->id; map++) {
  2108. if (map->id == state.chip->usb_id) {
  2109. state.map = map->map;
  2110. state.selector_map = map->selector_map;
  2111. mixer->ignore_ctl_error |= map->ignore_ctl_error;
  2112. break;
  2113. }
  2114. }
  2115. p = NULL;
  2116. while ((p = snd_usb_find_csint_desc(mixer->hostif->extra,
  2117. mixer->hostif->extralen,
  2118. p, UAC_OUTPUT_TERMINAL)) != NULL) {
  2119. if (mixer->protocol == UAC_VERSION_1) {
  2120. struct uac1_output_terminal_descriptor *desc = p;
  2121. if (desc->bLength < sizeof(*desc))
  2122. continue; /* invalid descriptor? */
  2123. /* mark terminal ID as visited */
  2124. set_bit(desc->bTerminalID, state.unitbitmap);
  2125. state.oterm.id = desc->bTerminalID;
  2126. state.oterm.type = le16_to_cpu(desc->wTerminalType);
  2127. state.oterm.name = desc->iTerminal;
  2128. err = parse_audio_unit(&state, desc->bSourceID);
  2129. if (err < 0 && err != -EINVAL)
  2130. return err;
  2131. } else { /* UAC_VERSION_2 */
  2132. struct uac2_output_terminal_descriptor *desc = p;
  2133. if (desc->bLength < sizeof(*desc))
  2134. continue; /* invalid descriptor? */
  2135. /* mark terminal ID as visited */
  2136. set_bit(desc->bTerminalID, state.unitbitmap);
  2137. state.oterm.id = desc->bTerminalID;
  2138. state.oterm.type = le16_to_cpu(desc->wTerminalType);
  2139. state.oterm.name = desc->iTerminal;
  2140. err = parse_audio_unit(&state, desc->bSourceID);
  2141. if (err < 0 && err != -EINVAL)
  2142. return err;
  2143. /*
  2144. * For UAC2, use the same approach to also add the
  2145. * clock selectors
  2146. */
  2147. err = parse_audio_unit(&state, desc->bCSourceID);
  2148. if (err < 0 && err != -EINVAL)
  2149. return err;
  2150. }
  2151. }
  2152. return 0;
  2153. }
  2154. void snd_usb_mixer_notify_id(struct usb_mixer_interface *mixer, int unitid)
  2155. {
  2156. struct usb_mixer_elem_list *list;
  2157. for_each_mixer_elem(list, mixer, unitid) {
  2158. struct usb_mixer_elem_info *info;
  2159. if (!list->is_std_info)
  2160. continue;
  2161. info = mixer_elem_list_to_info(list);
  2162. /* invalidate cache, so the value is read from the device */
  2163. info->cached = 0;
  2164. snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
  2165. &list->kctl->id);
  2166. }
  2167. }
  2168. static void snd_usb_mixer_dump_cval(struct snd_info_buffer *buffer,
  2169. struct usb_mixer_elem_list *list)
  2170. {
  2171. struct usb_mixer_elem_info *cval = mixer_elem_list_to_info(list);
  2172. static char *val_types[] = {"BOOLEAN", "INV_BOOLEAN",
  2173. "S8", "U8", "S16", "U16"};
  2174. snd_iprintf(buffer, " Info: id=%i, control=%i, cmask=0x%x, "
  2175. "channels=%i, type=\"%s\"\n", cval->head.id,
  2176. cval->control, cval->cmask, cval->channels,
  2177. val_types[cval->val_type]);
  2178. snd_iprintf(buffer, " Volume: min=%i, max=%i, dBmin=%i, dBmax=%i\n",
  2179. cval->min, cval->max, cval->dBmin, cval->dBmax);
  2180. }
  2181. static void snd_usb_mixer_proc_read(struct snd_info_entry *entry,
  2182. struct snd_info_buffer *buffer)
  2183. {
  2184. struct snd_usb_audio *chip = entry->private_data;
  2185. struct usb_mixer_interface *mixer;
  2186. struct usb_mixer_elem_list *list;
  2187. int unitid;
  2188. list_for_each_entry(mixer, &chip->mixer_list, list) {
  2189. snd_iprintf(buffer,
  2190. "USB Mixer: usb_id=0x%08x, ctrlif=%i, ctlerr=%i\n",
  2191. chip->usb_id, snd_usb_ctrl_intf(chip),
  2192. mixer->ignore_ctl_error);
  2193. snd_iprintf(buffer, "Card: %s\n", chip->card->longname);
  2194. for (unitid = 0; unitid < MAX_ID_ELEMS; unitid++) {
  2195. for_each_mixer_elem(list, mixer, unitid) {
  2196. snd_iprintf(buffer, " Unit: %i\n", list->id);
  2197. if (list->kctl)
  2198. snd_iprintf(buffer,
  2199. " Control: name=\"%s\", index=%i\n",
  2200. list->kctl->id.name,
  2201. list->kctl->id.index);
  2202. if (list->dump)
  2203. list->dump(buffer, list);
  2204. }
  2205. }
  2206. }
  2207. }
  2208. static void snd_usb_mixer_interrupt_v2(struct usb_mixer_interface *mixer,
  2209. int attribute, int value, int index)
  2210. {
  2211. struct usb_mixer_elem_list *list;
  2212. __u8 unitid = (index >> 8) & 0xff;
  2213. __u8 control = (value >> 8) & 0xff;
  2214. __u8 channel = value & 0xff;
  2215. unsigned int count = 0;
  2216. if (channel >= MAX_CHANNELS) {
  2217. usb_audio_dbg(mixer->chip,
  2218. "%s(): bogus channel number %d\n",
  2219. __func__, channel);
  2220. return;
  2221. }
  2222. for_each_mixer_elem(list, mixer, unitid)
  2223. count++;
  2224. if (count == 0)
  2225. return;
  2226. for_each_mixer_elem(list, mixer, unitid) {
  2227. struct usb_mixer_elem_info *info;
  2228. if (!list->kctl)
  2229. continue;
  2230. if (!list->is_std_info)
  2231. continue;
  2232. info = mixer_elem_list_to_info(list);
  2233. if (count > 1 && info->control != control)
  2234. continue;
  2235. switch (attribute) {
  2236. case UAC2_CS_CUR:
  2237. /* invalidate cache, so the value is read from the device */
  2238. if (channel)
  2239. info->cached &= ~(1 << channel);
  2240. else /* master channel */
  2241. info->cached = 0;
  2242. snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
  2243. &info->head.kctl->id);
  2244. break;
  2245. case UAC2_CS_RANGE:
  2246. /* TODO */
  2247. break;
  2248. case UAC2_CS_MEM:
  2249. /* TODO */
  2250. break;
  2251. default:
  2252. usb_audio_dbg(mixer->chip,
  2253. "unknown attribute %d in interrupt\n",
  2254. attribute);
  2255. break;
  2256. } /* switch */
  2257. }
  2258. }
  2259. static void snd_usb_mixer_interrupt(struct urb *urb)
  2260. {
  2261. struct usb_mixer_interface *mixer = urb->context;
  2262. int len = urb->actual_length;
  2263. int ustatus = urb->status;
  2264. if (ustatus != 0)
  2265. goto requeue;
  2266. if (mixer->protocol == UAC_VERSION_1) {
  2267. struct uac1_status_word *status;
  2268. for (status = urb->transfer_buffer;
  2269. len >= sizeof(*status);
  2270. len -= sizeof(*status), status++) {
  2271. dev_dbg(&urb->dev->dev, "status interrupt: %02x %02x\n",
  2272. status->bStatusType,
  2273. status->bOriginator);
  2274. /* ignore any notifications not from the control interface */
  2275. if ((status->bStatusType & UAC1_STATUS_TYPE_ORIG_MASK) !=
  2276. UAC1_STATUS_TYPE_ORIG_AUDIO_CONTROL_IF)
  2277. continue;
  2278. if (status->bStatusType & UAC1_STATUS_TYPE_MEM_CHANGED)
  2279. snd_usb_mixer_rc_memory_change(mixer, status->bOriginator);
  2280. else
  2281. snd_usb_mixer_notify_id(mixer, status->bOriginator);
  2282. }
  2283. } else { /* UAC_VERSION_2 */
  2284. struct uac2_interrupt_data_msg *msg;
  2285. for (msg = urb->transfer_buffer;
  2286. len >= sizeof(*msg);
  2287. len -= sizeof(*msg), msg++) {
  2288. /* drop vendor specific and endpoint requests */
  2289. if ((msg->bInfo & UAC2_INTERRUPT_DATA_MSG_VENDOR) ||
  2290. (msg->bInfo & UAC2_INTERRUPT_DATA_MSG_EP))
  2291. continue;
  2292. snd_usb_mixer_interrupt_v2(mixer, msg->bAttribute,
  2293. le16_to_cpu(msg->wValue),
  2294. le16_to_cpu(msg->wIndex));
  2295. }
  2296. }
  2297. requeue:
  2298. if (ustatus != -ENOENT &&
  2299. ustatus != -ECONNRESET &&
  2300. ustatus != -ESHUTDOWN) {
  2301. urb->dev = mixer->chip->dev;
  2302. usb_submit_urb(urb, GFP_ATOMIC);
  2303. }
  2304. }
  2305. /* create the handler for the optional status interrupt endpoint */
  2306. static int snd_usb_mixer_status_create(struct usb_mixer_interface *mixer)
  2307. {
  2308. struct usb_endpoint_descriptor *ep;
  2309. void *transfer_buffer;
  2310. int buffer_length;
  2311. unsigned int epnum;
  2312. /* we need one interrupt input endpoint */
  2313. if (get_iface_desc(mixer->hostif)->bNumEndpoints < 1)
  2314. return 0;
  2315. ep = get_endpoint(mixer->hostif, 0);
  2316. if (!usb_endpoint_dir_in(ep) || !usb_endpoint_xfer_int(ep))
  2317. return 0;
  2318. epnum = usb_endpoint_num(ep);
  2319. buffer_length = le16_to_cpu(ep->wMaxPacketSize);
  2320. transfer_buffer = kmalloc(buffer_length, GFP_KERNEL);
  2321. if (!transfer_buffer)
  2322. return -ENOMEM;
  2323. mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
  2324. if (!mixer->urb) {
  2325. kfree(transfer_buffer);
  2326. return -ENOMEM;
  2327. }
  2328. usb_fill_int_urb(mixer->urb, mixer->chip->dev,
  2329. usb_rcvintpipe(mixer->chip->dev, epnum),
  2330. transfer_buffer, buffer_length,
  2331. snd_usb_mixer_interrupt, mixer, ep->bInterval);
  2332. usb_submit_urb(mixer->urb, GFP_KERNEL);
  2333. return 0;
  2334. }
  2335. int snd_usb_create_mixer(struct snd_usb_audio *chip, int ctrlif,
  2336. int ignore_error)
  2337. {
  2338. static struct snd_device_ops dev_ops = {
  2339. .dev_free = snd_usb_mixer_dev_free
  2340. };
  2341. struct usb_mixer_interface *mixer;
  2342. struct snd_info_entry *entry;
  2343. int err;
  2344. strcpy(chip->card->mixername, "USB Mixer");
  2345. mixer = kzalloc(sizeof(*mixer), GFP_KERNEL);
  2346. if (!mixer)
  2347. return -ENOMEM;
  2348. mixer->chip = chip;
  2349. mixer->ignore_ctl_error = ignore_error;
  2350. mixer->id_elems = kcalloc(MAX_ID_ELEMS, sizeof(*mixer->id_elems),
  2351. GFP_KERNEL);
  2352. if (!mixer->id_elems) {
  2353. kfree(mixer);
  2354. return -ENOMEM;
  2355. }
  2356. mixer->hostif = &usb_ifnum_to_if(chip->dev, ctrlif)->altsetting[0];
  2357. switch (get_iface_desc(mixer->hostif)->bInterfaceProtocol) {
  2358. case UAC_VERSION_1:
  2359. default:
  2360. mixer->protocol = UAC_VERSION_1;
  2361. break;
  2362. case UAC_VERSION_2:
  2363. mixer->protocol = UAC_VERSION_2;
  2364. break;
  2365. }
  2366. if ((err = snd_usb_mixer_controls(mixer)) < 0 ||
  2367. (err = snd_usb_mixer_status_create(mixer)) < 0)
  2368. goto _error;
  2369. err = snd_usb_mixer_apply_create_quirk(mixer);
  2370. if (err < 0)
  2371. goto _error;
  2372. err = snd_device_new(chip->card, SNDRV_DEV_CODEC, mixer, &dev_ops);
  2373. if (err < 0)
  2374. goto _error;
  2375. if (list_empty(&chip->mixer_list) &&
  2376. !snd_card_proc_new(chip->card, "usbmixer", &entry))
  2377. snd_info_set_text_ops(entry, chip, snd_usb_mixer_proc_read);
  2378. list_add(&mixer->list, &chip->mixer_list);
  2379. return 0;
  2380. _error:
  2381. snd_usb_mixer_free(mixer);
  2382. return err;
  2383. }
  2384. void snd_usb_mixer_disconnect(struct usb_mixer_interface *mixer)
  2385. {
  2386. if (mixer->disconnected)
  2387. return;
  2388. if (mixer->urb)
  2389. usb_kill_urb(mixer->urb);
  2390. if (mixer->rc_urb)
  2391. usb_kill_urb(mixer->rc_urb);
  2392. mixer->disconnected = true;
  2393. }
  2394. #ifdef CONFIG_PM
  2395. /* stop any bus activity of a mixer */
  2396. static void snd_usb_mixer_inactivate(struct usb_mixer_interface *mixer)
  2397. {
  2398. usb_kill_urb(mixer->urb);
  2399. usb_kill_urb(mixer->rc_urb);
  2400. }
  2401. static int snd_usb_mixer_activate(struct usb_mixer_interface *mixer)
  2402. {
  2403. int err;
  2404. if (mixer->urb) {
  2405. err = usb_submit_urb(mixer->urb, GFP_NOIO);
  2406. if (err < 0)
  2407. return err;
  2408. }
  2409. return 0;
  2410. }
  2411. int snd_usb_mixer_suspend(struct usb_mixer_interface *mixer)
  2412. {
  2413. snd_usb_mixer_inactivate(mixer);
  2414. return 0;
  2415. }
  2416. static int restore_mixer_value(struct usb_mixer_elem_list *list)
  2417. {
  2418. struct usb_mixer_elem_info *cval = mixer_elem_list_to_info(list);
  2419. int c, err, idx;
  2420. if (cval->cmask) {
  2421. idx = 0;
  2422. for (c = 0; c < MAX_CHANNELS; c++) {
  2423. if (!(cval->cmask & (1 << c)))
  2424. continue;
  2425. if (cval->cached & (1 << (c + 1))) {
  2426. err = snd_usb_set_cur_mix_value(cval, c + 1, idx,
  2427. cval->cache_val[idx]);
  2428. if (err < 0)
  2429. return err;
  2430. }
  2431. idx++;
  2432. }
  2433. } else {
  2434. /* master */
  2435. if (cval->cached) {
  2436. err = snd_usb_set_cur_mix_value(cval, 0, 0, *cval->cache_val);
  2437. if (err < 0)
  2438. return err;
  2439. }
  2440. }
  2441. return 0;
  2442. }
  2443. int snd_usb_mixer_resume(struct usb_mixer_interface *mixer, bool reset_resume)
  2444. {
  2445. struct usb_mixer_elem_list *list;
  2446. int id, err;
  2447. if (reset_resume) {
  2448. /* restore cached mixer values */
  2449. for (id = 0; id < MAX_ID_ELEMS; id++) {
  2450. for_each_mixer_elem(list, mixer, id) {
  2451. if (list->resume) {
  2452. err = list->resume(list);
  2453. if (err < 0)
  2454. return err;
  2455. }
  2456. }
  2457. }
  2458. }
  2459. return snd_usb_mixer_activate(mixer);
  2460. }
  2461. #endif
  2462. void snd_usb_mixer_elem_init_std(struct usb_mixer_elem_list *list,
  2463. struct usb_mixer_interface *mixer,
  2464. int unitid)
  2465. {
  2466. list->mixer = mixer;
  2467. list->id = unitid;
  2468. list->dump = snd_usb_mixer_dump_cval;
  2469. #ifdef CONFIG_PM
  2470. list->resume = restore_mixer_value;
  2471. #endif
  2472. }