mixer.c 61 KB

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