mixer.c 70 KB

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