mixer.c 68 KB

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