mixer.c 61 KB

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