mixer.c 60 KB

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