mixer.c 62 KB

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