mixer.c 67 KB

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