usbmixer.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545
  1. /*
  2. * (Tentative) USB Audio Driver for ALSA
  3. *
  4. * Mixer control part
  5. *
  6. * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
  7. *
  8. * Many codes borrowed from audio.c by
  9. * Alan Cox (alan@lxorguk.ukuu.org.uk)
  10. * Thomas Sailer (sailer@ife.ee.ethz.ch)
  11. *
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. *
  27. */
  28. #include <sound/driver.h>
  29. #include <linux/bitops.h>
  30. #include <linux/init.h>
  31. #include <linux/list.h>
  32. #include <linux/slab.h>
  33. #include <linux/string.h>
  34. #include <linux/usb.h>
  35. #include <sound/core.h>
  36. #include <sound/control.h>
  37. #include "usbaudio.h"
  38. /*
  39. */
  40. /* ignore error from controls - for debugging */
  41. /* #define IGNORE_CTL_ERROR */
  42. typedef struct usb_mixer_build mixer_build_t;
  43. typedef struct usb_audio_term usb_audio_term_t;
  44. typedef struct usb_mixer_elem_info usb_mixer_elem_info_t;
  45. struct usb_audio_term {
  46. int id;
  47. int type;
  48. int channels;
  49. unsigned int chconfig;
  50. int name;
  51. };
  52. struct usbmix_name_map;
  53. struct usb_mixer_build {
  54. snd_usb_audio_t *chip;
  55. unsigned char *buffer;
  56. unsigned int buflen;
  57. unsigned int ctrlif;
  58. unsigned short vendor;
  59. unsigned short product;
  60. DECLARE_BITMAP(unitbitmap, 32*32);
  61. usb_audio_term_t oterm;
  62. const struct usbmix_name_map *map;
  63. };
  64. struct usb_mixer_elem_info {
  65. snd_usb_audio_t *chip;
  66. unsigned int ctrlif;
  67. unsigned int id;
  68. unsigned int control; /* CS or ICN (high byte) */
  69. unsigned int cmask; /* channel mask bitmap: 0 = master */
  70. int channels;
  71. int val_type;
  72. int min, max, res;
  73. unsigned int initialized: 1;
  74. };
  75. enum {
  76. USB_FEATURE_NONE = 0,
  77. USB_FEATURE_MUTE = 1,
  78. USB_FEATURE_VOLUME,
  79. USB_FEATURE_BASS,
  80. USB_FEATURE_MID,
  81. USB_FEATURE_TREBLE,
  82. USB_FEATURE_GEQ,
  83. USB_FEATURE_AGC,
  84. USB_FEATURE_DELAY,
  85. USB_FEATURE_BASSBOOST,
  86. USB_FEATURE_LOUDNESS
  87. };
  88. enum {
  89. USB_MIXER_BOOLEAN,
  90. USB_MIXER_INV_BOOLEAN,
  91. USB_MIXER_S8,
  92. USB_MIXER_U8,
  93. USB_MIXER_S16,
  94. USB_MIXER_U16,
  95. };
  96. enum {
  97. USB_PROC_UPDOWN = 1,
  98. USB_PROC_UPDOWN_SWITCH = 1,
  99. USB_PROC_UPDOWN_MODE_SEL = 2,
  100. USB_PROC_PROLOGIC = 2,
  101. USB_PROC_PROLOGIC_SWITCH = 1,
  102. USB_PROC_PROLOGIC_MODE_SEL = 2,
  103. USB_PROC_3DENH = 3,
  104. USB_PROC_3DENH_SWITCH = 1,
  105. USB_PROC_3DENH_SPACE = 2,
  106. USB_PROC_REVERB = 4,
  107. USB_PROC_REVERB_SWITCH = 1,
  108. USB_PROC_REVERB_LEVEL = 2,
  109. USB_PROC_REVERB_TIME = 3,
  110. USB_PROC_REVERB_DELAY = 4,
  111. USB_PROC_CHORUS = 5,
  112. USB_PROC_CHORUS_SWITCH = 1,
  113. USB_PROC_CHORUS_LEVEL = 2,
  114. USB_PROC_CHORUS_RATE = 3,
  115. USB_PROC_CHORUS_DEPTH = 4,
  116. USB_PROC_DCR = 6,
  117. USB_PROC_DCR_SWITCH = 1,
  118. USB_PROC_DCR_RATIO = 2,
  119. USB_PROC_DCR_MAX_AMP = 3,
  120. USB_PROC_DCR_THRESHOLD = 4,
  121. USB_PROC_DCR_ATTACK = 5,
  122. USB_PROC_DCR_RELEASE = 6,
  123. };
  124. #define MAX_CHANNELS 10 /* max logical channels */
  125. /*
  126. * manual mapping of mixer names
  127. * if the mixer topology is too complicated and the parsed names are
  128. * ambiguous, add the entries in usbmixer_maps.c.
  129. */
  130. #include "usbmixer_maps.c"
  131. /* get the mapped name if the unit matches */
  132. static int check_mapped_name(mixer_build_t *state, int unitid, int control, char *buf, int buflen)
  133. {
  134. const struct usbmix_name_map *p;
  135. if (! state->map)
  136. return 0;
  137. for (p = state->map; p->id; p++) {
  138. if (p->id == unitid && p->name &&
  139. (! control || ! p->control || control == p->control)) {
  140. buflen--;
  141. return strlcpy(buf, p->name, buflen);
  142. }
  143. }
  144. return 0;
  145. }
  146. /* check whether the control should be ignored */
  147. static int check_ignored_ctl(mixer_build_t *state, int unitid, int control)
  148. {
  149. const struct usbmix_name_map *p;
  150. if (! state->map)
  151. return 0;
  152. for (p = state->map; p->id; p++) {
  153. if (p->id == unitid && ! p->name &&
  154. (! control || ! p->control || control == p->control)) {
  155. // printk("ignored control %d:%d\n", unitid, control);
  156. return 1;
  157. }
  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(mixer_build_t *state, unsigned char unit)
  165. {
  166. unsigned char *p;
  167. p = NULL;
  168. while ((p = snd_usb_find_desc(state->buffer, state->buflen, p,
  169. USB_DT_CS_INTERFACE)) != NULL) {
  170. if (p[0] >= 4 && p[2] >= INPUT_TERMINAL && p[2] <= EXTENSION_UNIT && p[3] == unit)
  171. return p;
  172. }
  173. return NULL;
  174. }
  175. /*
  176. * copy a string with the given id
  177. */
  178. static int snd_usb_copy_string_desc(mixer_build_t *state, int index, char *buf, int maxlen)
  179. {
  180. int len = usb_string(state->chip->dev, index, buf, maxlen - 1);
  181. buf[len] = 0;
  182. return len;
  183. }
  184. /*
  185. * convert from the byte/word on usb descriptor to the zero-based integer
  186. */
  187. static int convert_signed_value(usb_mixer_elem_info_t *cval, int val)
  188. {
  189. switch (cval->val_type) {
  190. case USB_MIXER_BOOLEAN:
  191. return !!val;
  192. case USB_MIXER_INV_BOOLEAN:
  193. return !val;
  194. case USB_MIXER_U8:
  195. val &= 0xff;
  196. break;
  197. case USB_MIXER_S8:
  198. val &= 0xff;
  199. if (val >= 0x80)
  200. val -= 0x100;
  201. break;
  202. case USB_MIXER_U16:
  203. val &= 0xffff;
  204. break;
  205. case USB_MIXER_S16:
  206. val &= 0xffff;
  207. if (val >= 0x8000)
  208. val -= 0x10000;
  209. break;
  210. }
  211. return val;
  212. }
  213. /*
  214. * convert from the zero-based int to the byte/word for usb descriptor
  215. */
  216. static int convert_bytes_value(usb_mixer_elem_info_t *cval, int val)
  217. {
  218. switch (cval->val_type) {
  219. case USB_MIXER_BOOLEAN:
  220. return !!val;
  221. case USB_MIXER_INV_BOOLEAN:
  222. return !val;
  223. case USB_MIXER_S8:
  224. case USB_MIXER_U8:
  225. return val & 0xff;
  226. case USB_MIXER_S16:
  227. case USB_MIXER_U16:
  228. return val & 0xffff;
  229. }
  230. return 0; /* not reached */
  231. }
  232. static int get_relative_value(usb_mixer_elem_info_t *cval, int val)
  233. {
  234. if (! cval->res)
  235. cval->res = 1;
  236. if (val < cval->min)
  237. return 0;
  238. else if (val > cval->max)
  239. return (cval->max - cval->min) / cval->res;
  240. else
  241. return (val - cval->min) / cval->res;
  242. }
  243. static int get_abs_value(usb_mixer_elem_info_t *cval, int val)
  244. {
  245. if (val < 0)
  246. return cval->min;
  247. if (! cval->res)
  248. cval->res = 1;
  249. val *= cval->res;
  250. val += cval->min;
  251. if (val > cval->max)
  252. return cval->max;
  253. return val;
  254. }
  255. /*
  256. * retrieve a mixer value
  257. */
  258. static int get_ctl_value(usb_mixer_elem_info_t *cval, int request, int validx, int *value_ret)
  259. {
  260. unsigned char buf[2];
  261. int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
  262. int timeout = 10;
  263. while (timeout-- > 0) {
  264. if (snd_usb_ctl_msg(cval->chip->dev, usb_rcvctrlpipe(cval->chip->dev, 0),
  265. request,
  266. USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
  267. validx, cval->ctrlif | (cval->id << 8),
  268. buf, val_len, 100) >= 0) {
  269. *value_ret = convert_signed_value(cval, snd_usb_combine_bytes(buf, val_len));
  270. return 0;
  271. }
  272. }
  273. snd_printdd(KERN_ERR "cannot get ctl value: req = 0x%x, wValue = 0x%x, wIndex = 0x%x, type = %d\n", request, validx, cval->ctrlif | (cval->id << 8), cval->val_type);
  274. return -EINVAL;
  275. }
  276. static int get_cur_ctl_value(usb_mixer_elem_info_t *cval, int validx, int *value)
  277. {
  278. return get_ctl_value(cval, GET_CUR, validx, value);
  279. }
  280. /* channel = 0: master, 1 = first channel */
  281. inline static int get_cur_mix_value(usb_mixer_elem_info_t *cval, int channel, int *value)
  282. {
  283. return get_ctl_value(cval, GET_CUR, (cval->control << 8) | channel, value);
  284. }
  285. /*
  286. * set a mixer value
  287. */
  288. static int set_ctl_value(usb_mixer_elem_info_t *cval, int request, int validx, int value_set)
  289. {
  290. unsigned char buf[2];
  291. int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
  292. int timeout = 10;
  293. value_set = convert_bytes_value(cval, value_set);
  294. buf[0] = value_set & 0xff;
  295. buf[1] = (value_set >> 8) & 0xff;
  296. while (timeout -- > 0)
  297. if (snd_usb_ctl_msg(cval->chip->dev, usb_sndctrlpipe(cval->chip->dev, 0),
  298. request,
  299. USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
  300. validx, cval->ctrlif | (cval->id << 8),
  301. buf, val_len, 100) >= 0)
  302. return 0;
  303. snd_printdd(KERN_ERR "cannot set ctl value: req = 0x%x, wValue = 0x%x, wIndex = 0x%x, type = %d, data = 0x%x/0x%x\n", request, validx, cval->ctrlif | (cval->id << 8), cval->val_type, buf[0], buf[1]);
  304. return -EINVAL;
  305. }
  306. static int set_cur_ctl_value(usb_mixer_elem_info_t *cval, int validx, int value)
  307. {
  308. return set_ctl_value(cval, SET_CUR, validx, value);
  309. }
  310. inline static int set_cur_mix_value(usb_mixer_elem_info_t *cval, int channel, int value)
  311. {
  312. return set_ctl_value(cval, SET_CUR, (cval->control << 8) | channel, value);
  313. }
  314. /*
  315. * parser routines begin here...
  316. */
  317. static int parse_audio_unit(mixer_build_t *state, int unitid);
  318. /*
  319. * check if the input/output channel routing is enabled on the given bitmap.
  320. * used for mixer unit parser
  321. */
  322. static int check_matrix_bitmap(unsigned char *bmap, int ich, int och, int num_outs)
  323. {
  324. int idx = ich * num_outs + och;
  325. return bmap[idx >> 3] & (0x80 >> (idx & 7));
  326. }
  327. /*
  328. * add an alsa control element
  329. * search and increment the index until an empty slot is found.
  330. *
  331. * if failed, give up and free the control instance.
  332. */
  333. static int add_control_to_empty(snd_card_t *card, snd_kcontrol_t *kctl)
  334. {
  335. int err;
  336. while (snd_ctl_find_id(card, &kctl->id))
  337. kctl->id.index++;
  338. if ((err = snd_ctl_add(card, kctl)) < 0) {
  339. snd_printd(KERN_ERR "cannot add control (err = %d)\n", err);
  340. snd_ctl_free_one(kctl);
  341. }
  342. return err;
  343. }
  344. /*
  345. * get a terminal name string
  346. */
  347. static struct iterm_name_combo {
  348. int type;
  349. char *name;
  350. } iterm_names[] = {
  351. { 0x0300, "Output" },
  352. { 0x0301, "Speaker" },
  353. { 0x0302, "Headphone" },
  354. { 0x0303, "HMD Audio" },
  355. { 0x0304, "Desktop Speaker" },
  356. { 0x0305, "Room Speaker" },
  357. { 0x0306, "Com Speaker" },
  358. { 0x0307, "LFE" },
  359. { 0x0600, "External In" },
  360. { 0x0601, "Analog In" },
  361. { 0x0602, "Digital In" },
  362. { 0x0603, "Line" },
  363. { 0x0604, "Legacy In" },
  364. { 0x0605, "IEC958 In" },
  365. { 0x0606, "1394 DA Stream" },
  366. { 0x0607, "1394 DV Stream" },
  367. { 0x0700, "Embedded" },
  368. { 0x0701, "Noise Source" },
  369. { 0x0702, "Equalization Noise" },
  370. { 0x0703, "CD" },
  371. { 0x0704, "DAT" },
  372. { 0x0705, "DCC" },
  373. { 0x0706, "MiniDisk" },
  374. { 0x0707, "Analog Tape" },
  375. { 0x0708, "Phonograph" },
  376. { 0x0709, "VCR Audio" },
  377. { 0x070a, "Video Disk Audio" },
  378. { 0x070b, "DVD Audio" },
  379. { 0x070c, "TV Tuner Audio" },
  380. { 0x070d, "Satellite Rec Audio" },
  381. { 0x070e, "Cable Tuner Audio" },
  382. { 0x070f, "DSS Audio" },
  383. { 0x0710, "Radio Receiver" },
  384. { 0x0711, "Radio Transmitter" },
  385. { 0x0712, "Multi-Track Recorder" },
  386. { 0x0713, "Synthesizer" },
  387. { 0 },
  388. };
  389. static int get_term_name(mixer_build_t *state, usb_audio_term_t *iterm,
  390. unsigned char *name, int maxlen, int term_only)
  391. {
  392. struct iterm_name_combo *names;
  393. if (iterm->name)
  394. return snd_usb_copy_string_desc(state, iterm->name, name, maxlen);
  395. /* virtual type - not a real terminal */
  396. if (iterm->type >> 16) {
  397. if (term_only)
  398. return 0;
  399. switch (iterm->type >> 16) {
  400. case SELECTOR_UNIT:
  401. strcpy(name, "Selector"); return 8;
  402. case PROCESSING_UNIT:
  403. strcpy(name, "Process Unit"); return 12;
  404. case EXTENSION_UNIT:
  405. strcpy(name, "Ext Unit"); return 8;
  406. case MIXER_UNIT:
  407. strcpy(name, "Mixer"); return 5;
  408. default:
  409. return sprintf(name, "Unit %d", iterm->id);
  410. }
  411. }
  412. switch (iterm->type & 0xff00) {
  413. case 0x0100:
  414. strcpy(name, "PCM"); return 3;
  415. case 0x0200:
  416. strcpy(name, "Mic"); return 3;
  417. case 0x0400:
  418. strcpy(name, "Headset"); return 7;
  419. case 0x0500:
  420. strcpy(name, "Phone"); return 5;
  421. }
  422. for (names = iterm_names; names->type; names++)
  423. if (names->type == iterm->type) {
  424. strcpy(name, names->name);
  425. return strlen(names->name);
  426. }
  427. return 0;
  428. }
  429. /*
  430. * parse the source unit recursively until it reaches to a terminal
  431. * or a branched unit.
  432. */
  433. static int check_input_term(mixer_build_t *state, int id, usb_audio_term_t *term)
  434. {
  435. unsigned char *p1;
  436. memset(term, 0, sizeof(*term));
  437. while ((p1 = find_audio_control_unit(state, id)) != NULL) {
  438. term->id = id;
  439. switch (p1[2]) {
  440. case INPUT_TERMINAL:
  441. term->type = combine_word(p1 + 4);
  442. term->channels = p1[7];
  443. term->chconfig = combine_word(p1 + 8);
  444. term->name = p1[11];
  445. return 0;
  446. case FEATURE_UNIT:
  447. id = p1[4];
  448. break; /* continue to parse */
  449. case MIXER_UNIT:
  450. term->type = p1[2] << 16; /* virtual type */
  451. term->channels = p1[5 + p1[4]];
  452. term->chconfig = combine_word(p1 + 6 + p1[4]);
  453. term->name = p1[p1[0] - 1];
  454. return 0;
  455. case SELECTOR_UNIT:
  456. /* call recursively to retrieve the channel info */
  457. if (check_input_term(state, p1[5], term) < 0)
  458. return -ENODEV;
  459. term->type = p1[2] << 16; /* virtual type */
  460. term->id = id;
  461. term->name = p1[9 + p1[0] - 1];
  462. return 0;
  463. case PROCESSING_UNIT:
  464. case EXTENSION_UNIT:
  465. if (p1[6] == 1) {
  466. id = p1[7];
  467. break; /* continue to parse */
  468. }
  469. term->type = p1[2] << 16; /* virtual type */
  470. term->channels = p1[7 + p1[6]];
  471. term->chconfig = combine_word(p1 + 8 + p1[6]);
  472. term->name = p1[12 + p1[6] + p1[11 + p1[6]]];
  473. return 0;
  474. default:
  475. return -ENODEV;
  476. }
  477. }
  478. return -ENODEV;
  479. }
  480. /*
  481. * Feature Unit
  482. */
  483. /* feature unit control information */
  484. struct usb_feature_control_info {
  485. const char *name;
  486. unsigned int type; /* control type (mute, volume, etc.) */
  487. };
  488. static struct usb_feature_control_info audio_feature_info[] = {
  489. { "Mute", USB_MIXER_INV_BOOLEAN },
  490. { "Volume", USB_MIXER_S16 },
  491. { "Tone Control - Bass", USB_MIXER_S8 },
  492. { "Tone Control - Mid", USB_MIXER_S8 },
  493. { "Tone Control - Treble", USB_MIXER_S8 },
  494. { "Graphic Equalizer", USB_MIXER_S8 }, /* FIXME: not implemeted yet */
  495. { "Auto Gain Control", USB_MIXER_BOOLEAN },
  496. { "Delay Control", USB_MIXER_U16 },
  497. { "Bass Boost", USB_MIXER_BOOLEAN },
  498. { "Loudness", USB_MIXER_BOOLEAN },
  499. };
  500. /* private_free callback */
  501. static void usb_mixer_elem_free(snd_kcontrol_t *kctl)
  502. {
  503. if (kctl->private_data) {
  504. kfree(kctl->private_data);
  505. kctl->private_data = NULL;
  506. }
  507. }
  508. /*
  509. * interface to ALSA control for feature/mixer units
  510. */
  511. /*
  512. * retrieve the minimum and maximum values for the specified control
  513. */
  514. static int get_min_max(usb_mixer_elem_info_t *cval, int default_min)
  515. {
  516. /* for failsafe */
  517. cval->min = default_min;
  518. cval->max = cval->min + 1;
  519. cval->res = 1;
  520. if (cval->val_type == USB_MIXER_BOOLEAN ||
  521. cval->val_type == USB_MIXER_INV_BOOLEAN) {
  522. cval->initialized = 1;
  523. } else {
  524. int minchn = 0;
  525. if (cval->cmask) {
  526. int i;
  527. for (i = 0; i < MAX_CHANNELS; i++)
  528. if (cval->cmask & (1 << i)) {
  529. minchn = i + 1;
  530. break;
  531. }
  532. }
  533. if (get_ctl_value(cval, GET_MAX, (cval->control << 8) | minchn, &cval->max) < 0 ||
  534. get_ctl_value(cval, GET_MIN, (cval->control << 8) | minchn, &cval->min) < 0) {
  535. snd_printd(KERN_ERR "%d:%d: cannot get min/max values for control %d (id %d)\n", cval->id, cval->ctrlif, cval->control, cval->id);
  536. return -EINVAL;
  537. }
  538. if (get_ctl_value(cval, GET_RES, (cval->control << 8) | minchn, &cval->res) < 0) {
  539. cval->res = 1;
  540. } else {
  541. int last_valid_res = cval->res;
  542. while (cval->res > 1) {
  543. if (set_ctl_value(cval, SET_RES, (cval->control << 8) | minchn, cval->res / 2) < 0)
  544. break;
  545. cval->res /= 2;
  546. }
  547. if (get_ctl_value(cval, GET_RES, (cval->control << 8) | minchn, &cval->res) < 0)
  548. cval->res = last_valid_res;
  549. }
  550. if (cval->res == 0)
  551. cval->res = 1;
  552. cval->initialized = 1;
  553. }
  554. return 0;
  555. }
  556. /* get a feature/mixer unit info */
  557. static int mixer_ctl_feature_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
  558. {
  559. usb_mixer_elem_info_t *cval = kcontrol->private_data;
  560. if (cval->val_type == USB_MIXER_BOOLEAN ||
  561. cval->val_type == USB_MIXER_INV_BOOLEAN)
  562. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  563. else
  564. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  565. uinfo->count = cval->channels;
  566. if (cval->val_type == USB_MIXER_BOOLEAN ||
  567. cval->val_type == USB_MIXER_INV_BOOLEAN) {
  568. uinfo->value.integer.min = 0;
  569. uinfo->value.integer.max = 1;
  570. } else {
  571. if (! cval->initialized)
  572. get_min_max(cval, 0);
  573. uinfo->value.integer.min = 0;
  574. uinfo->value.integer.max = (cval->max - cval->min) / cval->res;
  575. }
  576. return 0;
  577. }
  578. /* get the current value from feature/mixer unit */
  579. static int mixer_ctl_feature_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  580. {
  581. usb_mixer_elem_info_t *cval = kcontrol->private_data;
  582. int c, cnt, val, err;
  583. if (cval->cmask) {
  584. cnt = 0;
  585. for (c = 0; c < MAX_CHANNELS; c++) {
  586. if (cval->cmask & (1 << c)) {
  587. err = get_cur_mix_value(cval, c + 1, &val);
  588. if (err < 0) {
  589. if (cval->chip->ignore_ctl_error) {
  590. ucontrol->value.integer.value[0] = cval->min;
  591. return 0;
  592. }
  593. snd_printd(KERN_ERR "cannot get current value for control %d ch %d: err = %d\n", cval->control, c + 1, err);
  594. return err;
  595. }
  596. val = get_relative_value(cval, val);
  597. ucontrol->value.integer.value[cnt] = val;
  598. cnt++;
  599. }
  600. }
  601. } else {
  602. /* master channel */
  603. err = get_cur_mix_value(cval, 0, &val);
  604. if (err < 0) {
  605. if (cval->chip->ignore_ctl_error) {
  606. ucontrol->value.integer.value[0] = cval->min;
  607. return 0;
  608. }
  609. snd_printd(KERN_ERR "cannot get current value for control %d master ch: err = %d\n", cval->control, err);
  610. return err;
  611. }
  612. val = get_relative_value(cval, val);
  613. ucontrol->value.integer.value[0] = val;
  614. }
  615. return 0;
  616. }
  617. /* put the current value to feature/mixer unit */
  618. static int mixer_ctl_feature_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  619. {
  620. usb_mixer_elem_info_t *cval = kcontrol->private_data;
  621. int c, cnt, val, oval, err;
  622. int changed = 0;
  623. if (cval->cmask) {
  624. cnt = 0;
  625. for (c = 0; c < MAX_CHANNELS; c++) {
  626. if (cval->cmask & (1 << c)) {
  627. err = get_cur_mix_value(cval, c + 1, &oval);
  628. if (err < 0) {
  629. if (cval->chip->ignore_ctl_error)
  630. return 0;
  631. return err;
  632. }
  633. val = ucontrol->value.integer.value[cnt];
  634. val = get_abs_value(cval, val);
  635. if (oval != val) {
  636. set_cur_mix_value(cval, c + 1, val);
  637. changed = 1;
  638. }
  639. get_cur_mix_value(cval, c + 1, &val);
  640. cnt++;
  641. }
  642. }
  643. } else {
  644. /* master channel */
  645. err = get_cur_mix_value(cval, 0, &oval);
  646. if (err < 0 && cval->chip->ignore_ctl_error)
  647. return 0;
  648. if (err < 0)
  649. return err;
  650. val = ucontrol->value.integer.value[0];
  651. val = get_abs_value(cval, val);
  652. if (val != oval) {
  653. set_cur_mix_value(cval, 0, val);
  654. changed = 1;
  655. }
  656. }
  657. return changed;
  658. }
  659. static snd_kcontrol_new_t usb_feature_unit_ctl = {
  660. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  661. .name = "", /* will be filled later manually */
  662. .info = mixer_ctl_feature_info,
  663. .get = mixer_ctl_feature_get,
  664. .put = mixer_ctl_feature_put,
  665. };
  666. /*
  667. * build a feature control
  668. */
  669. static void build_feature_ctl(mixer_build_t *state, unsigned char *desc,
  670. unsigned int ctl_mask, int control,
  671. usb_audio_term_t *iterm, int unitid)
  672. {
  673. unsigned int len = 0;
  674. int mapped_name = 0;
  675. int nameid = desc[desc[0] - 1];
  676. snd_kcontrol_t *kctl;
  677. usb_mixer_elem_info_t *cval;
  678. control++; /* change from zero-based to 1-based value */
  679. if (control == USB_FEATURE_GEQ) {
  680. /* FIXME: not supported yet */
  681. return;
  682. }
  683. if (check_ignored_ctl(state, unitid, control))
  684. return;
  685. cval = kcalloc(1, sizeof(*cval), GFP_KERNEL);
  686. if (! cval) {
  687. snd_printk(KERN_ERR "cannot malloc kcontrol\n");
  688. return;
  689. }
  690. cval->chip = state->chip;
  691. cval->ctrlif = state->ctrlif;
  692. cval->id = unitid;
  693. cval->control = control;
  694. cval->cmask = ctl_mask;
  695. cval->val_type = audio_feature_info[control-1].type;
  696. if (ctl_mask == 0)
  697. cval->channels = 1; /* master channel */
  698. else {
  699. int i, c = 0;
  700. for (i = 0; i < 16; i++)
  701. if (ctl_mask & (1 << i))
  702. c++;
  703. cval->channels = c;
  704. }
  705. /* get min/max values */
  706. get_min_max(cval, 0);
  707. kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
  708. if (! kctl) {
  709. snd_printk(KERN_ERR "cannot malloc kcontrol\n");
  710. kfree(cval);
  711. return;
  712. }
  713. kctl->private_free = usb_mixer_elem_free;
  714. len = check_mapped_name(state, unitid, control, kctl->id.name, sizeof(kctl->id.name));
  715. mapped_name = len != 0;
  716. if (! len && nameid)
  717. len = snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
  718. switch (control) {
  719. case USB_FEATURE_MUTE:
  720. case USB_FEATURE_VOLUME:
  721. /* determine the control name. the rule is:
  722. * - if a name id is given in descriptor, use it.
  723. * - if the connected input can be determined, then use the name
  724. * of terminal type.
  725. * - if the connected output can be determined, use it.
  726. * - otherwise, anonymous name.
  727. */
  728. if (! len) {
  729. len = get_term_name(state, iterm, kctl->id.name, sizeof(kctl->id.name), 1);
  730. if (! len)
  731. len = get_term_name(state, &state->oterm, kctl->id.name, sizeof(kctl->id.name), 1);
  732. if (! len)
  733. len = snprintf(kctl->id.name, sizeof(kctl->id.name),
  734. "Feature %d", unitid);
  735. }
  736. /* determine the stream direction:
  737. * if the connected output is USB stream, then it's likely a
  738. * capture stream. otherwise it should be playback (hopefully :)
  739. */
  740. if (! mapped_name && ! (state->oterm.type >> 16)) {
  741. if ((state->oterm.type & 0xff00) == 0x0100) {
  742. len = strlcat(kctl->id.name, " Capture", sizeof(kctl->id.name));
  743. } else {
  744. len = strlcat(kctl->id.name + len, " Playback", sizeof(kctl->id.name));
  745. }
  746. }
  747. strlcat(kctl->id.name + len, control == USB_FEATURE_MUTE ? " Switch" : " Volume",
  748. sizeof(kctl->id.name));
  749. break;
  750. default:
  751. if (! len)
  752. strlcpy(kctl->id.name, audio_feature_info[control-1].name,
  753. sizeof(kctl->id.name));
  754. break;
  755. }
  756. /* quirk for UDA1321/N101 */
  757. /* note that detection between firmware 2.1.1.7 (N101) and later 2.1.1.21 */
  758. /* is not very clear from datasheets */
  759. /* I hope that the min value is -15360 for newer firmware --jk */
  760. if (((state->vendor == 0x471 && (state->product == 0x104 || state->product == 0x105 || state->product == 0x101)) ||
  761. (state->vendor == 0x672 && state->product == 0x1041)) && !strcmp(kctl->id.name, "PCM Playback Volume") &&
  762. cval->min == -15616) {
  763. snd_printk("USB Audio: using volume control quirk for the UDA1321/N101 chip\n");
  764. cval->max = -256;
  765. }
  766. snd_printdd(KERN_INFO "[%d] FU [%s] ch = %d, val = %d/%d/%d\n",
  767. cval->id, kctl->id.name, cval->channels, cval->min, cval->max, cval->res);
  768. add_control_to_empty(state->chip->card, kctl);
  769. }
  770. /*
  771. * parse a feature unit
  772. *
  773. * most of controlls are defined here.
  774. */
  775. static int parse_audio_feature_unit(mixer_build_t *state, int unitid, unsigned char *ftr)
  776. {
  777. int channels, i, j;
  778. usb_audio_term_t iterm;
  779. unsigned int master_bits, first_ch_bits;
  780. int err, csize;
  781. if (ftr[0] < 7 || ! (csize = ftr[5]) || ftr[0] < 7 + csize) {
  782. snd_printk(KERN_ERR "usbaudio: unit %u: invalid FEATURE_UNIT descriptor\n", unitid);
  783. return -EINVAL;
  784. }
  785. /* parse the source unit */
  786. if ((err = parse_audio_unit(state, ftr[4])) < 0)
  787. return err;
  788. /* determine the input source type and name */
  789. if (check_input_term(state, ftr[4], &iterm) < 0)
  790. return -EINVAL;
  791. channels = (ftr[0] - 7) / csize - 1;
  792. master_bits = snd_usb_combine_bytes(ftr + 6, csize);
  793. if (channels > 0)
  794. first_ch_bits = snd_usb_combine_bytes(ftr + 6 + csize, csize);
  795. else
  796. first_ch_bits = 0;
  797. /* check all control types */
  798. for (i = 0; i < 10; i++) {
  799. unsigned int ch_bits = 0;
  800. for (j = 0; j < channels; j++) {
  801. unsigned int mask = snd_usb_combine_bytes(ftr + 6 + csize * (j+1), csize);
  802. if (mask & (1 << i))
  803. ch_bits |= (1 << j);
  804. }
  805. if (ch_bits & 1) /* the first channel must be set (for ease of programming) */
  806. build_feature_ctl(state, ftr, ch_bits, i, &iterm, unitid);
  807. if (master_bits & (1 << i))
  808. build_feature_ctl(state, ftr, 0, i, &iterm, unitid);
  809. }
  810. return 0;
  811. }
  812. /*
  813. * Mixer Unit
  814. */
  815. /*
  816. * build a mixer unit control
  817. *
  818. * the callbacks are identical with feature unit.
  819. * input channel number (zero based) is given in control field instead.
  820. */
  821. static void build_mixer_unit_ctl(mixer_build_t *state, unsigned char *desc,
  822. int in_pin, int in_ch, int unitid,
  823. usb_audio_term_t *iterm)
  824. {
  825. usb_mixer_elem_info_t *cval;
  826. unsigned int input_pins = desc[4];
  827. unsigned int num_outs = desc[5 + input_pins];
  828. unsigned int i, len;
  829. snd_kcontrol_t *kctl;
  830. if (check_ignored_ctl(state, unitid, 0))
  831. return;
  832. cval = kcalloc(1, sizeof(*cval), GFP_KERNEL);
  833. if (! cval)
  834. return;
  835. cval->chip = state->chip;
  836. cval->ctrlif = state->ctrlif;
  837. cval->id = unitid;
  838. cval->control = in_ch + 1; /* based on 1 */
  839. cval->val_type = USB_MIXER_S16;
  840. for (i = 0; i < num_outs; i++) {
  841. if (check_matrix_bitmap(desc + 9 + input_pins, in_ch, i, num_outs)) {
  842. cval->cmask |= (1 << i);
  843. cval->channels++;
  844. }
  845. }
  846. /* get min/max values */
  847. get_min_max(cval, 0);
  848. kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
  849. if (! kctl) {
  850. snd_printk(KERN_ERR "cannot malloc kcontrol\n");
  851. kfree(cval);
  852. return;
  853. }
  854. kctl->private_free = usb_mixer_elem_free;
  855. len = check_mapped_name(state, unitid, 0, kctl->id.name, sizeof(kctl->id.name));
  856. if (! len)
  857. len = get_term_name(state, iterm, kctl->id.name, sizeof(kctl->id.name), 0);
  858. if (! len)
  859. len = sprintf(kctl->id.name, "Mixer Source %d", in_ch + 1);
  860. strlcat(kctl->id.name + len, " Volume", sizeof(kctl->id.name));
  861. snd_printdd(KERN_INFO "[%d] MU [%s] ch = %d, val = %d/%d\n",
  862. cval->id, kctl->id.name, cval->channels, cval->min, cval->max);
  863. add_control_to_empty(state->chip->card, kctl);
  864. }
  865. /*
  866. * parse a mixer unit
  867. */
  868. static int parse_audio_mixer_unit(mixer_build_t *state, int unitid, unsigned char *desc)
  869. {
  870. usb_audio_term_t iterm;
  871. int input_pins, num_ins, num_outs;
  872. int pin, ich, err;
  873. if (desc[0] < 11 || ! (input_pins = desc[4]) || ! (num_outs = desc[5 + input_pins])) {
  874. snd_printk(KERN_ERR "invalid MIXER UNIT descriptor %d\n", unitid);
  875. return -EINVAL;
  876. }
  877. /* no bmControls field (e.g. Maya44) -> ignore */
  878. if (desc[0] <= 10 + input_pins) {
  879. snd_printdd(KERN_INFO "MU %d has no bmControls field\n", unitid);
  880. return 0;
  881. }
  882. num_ins = 0;
  883. ich = 0;
  884. for (pin = 0; pin < input_pins; pin++) {
  885. err = parse_audio_unit(state, desc[5 + pin]);
  886. if (err < 0)
  887. return err;
  888. err = check_input_term(state, desc[5 + pin], &iterm);
  889. if (err < 0)
  890. return err;
  891. num_ins += iterm.channels;
  892. for (; ich < num_ins; ++ich) {
  893. int och, ich_has_controls = 0;
  894. for (och = 0; och < num_outs; ++och) {
  895. if (check_matrix_bitmap(desc + 9 + input_pins,
  896. ich, och, num_outs)) {
  897. ich_has_controls = 1;
  898. break;
  899. }
  900. }
  901. if (ich_has_controls)
  902. build_mixer_unit_ctl(state, desc, pin, ich,
  903. unitid, &iterm);
  904. }
  905. }
  906. return 0;
  907. }
  908. /*
  909. * Processing Unit / Extension Unit
  910. */
  911. /* get callback for processing/extension unit */
  912. static int mixer_ctl_procunit_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  913. {
  914. usb_mixer_elem_info_t *cval = kcontrol->private_data;
  915. int err, val;
  916. err = get_cur_ctl_value(cval, cval->control << 8, &val);
  917. if (err < 0 && cval->chip->ignore_ctl_error) {
  918. ucontrol->value.integer.value[0] = cval->min;
  919. return 0;
  920. }
  921. if (err < 0)
  922. return err;
  923. val = get_relative_value(cval, val);
  924. ucontrol->value.integer.value[0] = val;
  925. return 0;
  926. }
  927. /* put callback for processing/extension unit */
  928. static int mixer_ctl_procunit_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  929. {
  930. usb_mixer_elem_info_t *cval = kcontrol->private_data;
  931. int val, oval, err;
  932. err = get_cur_ctl_value(cval, cval->control << 8, &oval);
  933. if (err < 0) {
  934. if (cval->chip->ignore_ctl_error)
  935. return 0;
  936. return err;
  937. }
  938. val = ucontrol->value.integer.value[0];
  939. val = get_abs_value(cval, val);
  940. if (val != oval) {
  941. set_cur_ctl_value(cval, cval->control << 8, val);
  942. return 1;
  943. }
  944. return 0;
  945. }
  946. /* alsa control interface for processing/extension unit */
  947. static snd_kcontrol_new_t mixer_procunit_ctl = {
  948. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  949. .name = "", /* will be filled later */
  950. .info = mixer_ctl_feature_info,
  951. .get = mixer_ctl_procunit_get,
  952. .put = mixer_ctl_procunit_put,
  953. };
  954. /*
  955. * predefined data for processing units
  956. */
  957. struct procunit_value_info {
  958. int control;
  959. char *suffix;
  960. int val_type;
  961. int min_value;
  962. };
  963. struct procunit_info {
  964. int type;
  965. char *name;
  966. struct procunit_value_info *values;
  967. };
  968. static struct procunit_value_info updown_proc_info[] = {
  969. { USB_PROC_UPDOWN_SWITCH, "Switch", USB_MIXER_BOOLEAN },
  970. { USB_PROC_UPDOWN_MODE_SEL, "Mode Select", USB_MIXER_U8, 1 },
  971. { 0 }
  972. };
  973. static struct procunit_value_info prologic_proc_info[] = {
  974. { USB_PROC_PROLOGIC_SWITCH, "Switch", USB_MIXER_BOOLEAN },
  975. { USB_PROC_PROLOGIC_MODE_SEL, "Mode Select", USB_MIXER_U8, 1 },
  976. { 0 }
  977. };
  978. static struct procunit_value_info threed_enh_proc_info[] = {
  979. { USB_PROC_3DENH_SWITCH, "Switch", USB_MIXER_BOOLEAN },
  980. { USB_PROC_3DENH_SPACE, "Spaciousness", USB_MIXER_U8 },
  981. { 0 }
  982. };
  983. static struct procunit_value_info reverb_proc_info[] = {
  984. { USB_PROC_REVERB_SWITCH, "Switch", USB_MIXER_BOOLEAN },
  985. { USB_PROC_REVERB_LEVEL, "Level", USB_MIXER_U8 },
  986. { USB_PROC_REVERB_TIME, "Time", USB_MIXER_U16 },
  987. { USB_PROC_REVERB_DELAY, "Delay", USB_MIXER_U8 },
  988. { 0 }
  989. };
  990. static struct procunit_value_info chorus_proc_info[] = {
  991. { USB_PROC_CHORUS_SWITCH, "Switch", USB_MIXER_BOOLEAN },
  992. { USB_PROC_CHORUS_LEVEL, "Level", USB_MIXER_U8 },
  993. { USB_PROC_CHORUS_RATE, "Rate", USB_MIXER_U16 },
  994. { USB_PROC_CHORUS_DEPTH, "Depth", USB_MIXER_U16 },
  995. { 0 }
  996. };
  997. static struct procunit_value_info dcr_proc_info[] = {
  998. { USB_PROC_DCR_SWITCH, "Switch", USB_MIXER_BOOLEAN },
  999. { USB_PROC_DCR_RATIO, "Ratio", USB_MIXER_U16 },
  1000. { USB_PROC_DCR_MAX_AMP, "Max Amp", USB_MIXER_S16 },
  1001. { USB_PROC_DCR_THRESHOLD, "Threshold", USB_MIXER_S16 },
  1002. { USB_PROC_DCR_ATTACK, "Attack Time", USB_MIXER_U16 },
  1003. { USB_PROC_DCR_RELEASE, "Release Time", USB_MIXER_U16 },
  1004. { 0 }
  1005. };
  1006. static struct procunit_info procunits[] = {
  1007. { USB_PROC_UPDOWN, "Up Down", updown_proc_info },
  1008. { USB_PROC_PROLOGIC, "Dolby Prologic", prologic_proc_info },
  1009. { USB_PROC_3DENH, "3D Stereo Extender", threed_enh_proc_info },
  1010. { USB_PROC_REVERB, "Reverb", reverb_proc_info },
  1011. { USB_PROC_CHORUS, "Chorus", chorus_proc_info },
  1012. { USB_PROC_DCR, "DCR", dcr_proc_info },
  1013. { 0 },
  1014. };
  1015. /*
  1016. * build a processing/extension unit
  1017. */
  1018. static int build_audio_procunit(mixer_build_t *state, int unitid, unsigned char *dsc, struct procunit_info *list, char *name)
  1019. {
  1020. int num_ins = dsc[6];
  1021. usb_mixer_elem_info_t *cval;
  1022. snd_kcontrol_t *kctl;
  1023. int i, err, nameid, type, len;
  1024. struct procunit_info *info;
  1025. struct procunit_value_info *valinfo;
  1026. static struct procunit_value_info default_value_info[] = {
  1027. { 0x01, "Switch", USB_MIXER_BOOLEAN },
  1028. { 0 }
  1029. };
  1030. static struct procunit_info default_info = {
  1031. 0, NULL, default_value_info
  1032. };
  1033. if (dsc[0] < 13 || dsc[0] < 13 + num_ins || dsc[0] < num_ins + dsc[11 + num_ins]) {
  1034. snd_printk(KERN_ERR "invalid %s descriptor (id %d)\n", name, unitid);
  1035. return -EINVAL;
  1036. }
  1037. for (i = 0; i < num_ins; i++) {
  1038. if ((err = parse_audio_unit(state, dsc[7 + i])) < 0)
  1039. return err;
  1040. }
  1041. type = combine_word(&dsc[4]);
  1042. if (! type)
  1043. return 0; /* undefined? */
  1044. for (info = list; info && info->type; info++)
  1045. if (info->type == type)
  1046. break;
  1047. if (! info || ! info->type)
  1048. info = &default_info;
  1049. for (valinfo = info->values; valinfo->control; valinfo++) {
  1050. /* FIXME: bitmap might be longer than 8bit */
  1051. if (! (dsc[12 + num_ins] & (1 << (valinfo->control - 1))))
  1052. continue;
  1053. if (check_ignored_ctl(state, unitid, valinfo->control))
  1054. continue;
  1055. cval = kcalloc(1, sizeof(*cval), GFP_KERNEL);
  1056. if (! cval) {
  1057. snd_printk(KERN_ERR "cannot malloc kcontrol\n");
  1058. return -ENOMEM;
  1059. }
  1060. cval->chip = state->chip;
  1061. cval->ctrlif = state->ctrlif;
  1062. cval->id = unitid;
  1063. cval->control = valinfo->control;
  1064. cval->val_type = valinfo->val_type;
  1065. cval->channels = 1;
  1066. /* get min/max values */
  1067. if (type == USB_PROC_UPDOWN && cval->control == USB_PROC_UPDOWN_MODE_SEL) {
  1068. /* FIXME: hard-coded */
  1069. cval->min = 1;
  1070. cval->max = dsc[15];
  1071. cval->res = 1;
  1072. cval->initialized = 1;
  1073. } else
  1074. get_min_max(cval, valinfo->min_value);
  1075. kctl = snd_ctl_new1(&mixer_procunit_ctl, cval);
  1076. if (! kctl) {
  1077. snd_printk(KERN_ERR "cannot malloc kcontrol\n");
  1078. kfree(cval);
  1079. return -ENOMEM;
  1080. }
  1081. kctl->private_free = usb_mixer_elem_free;
  1082. if (check_mapped_name(state, unitid, cval->control, kctl->id.name, sizeof(kctl->id.name)))
  1083. ;
  1084. else if (info->name)
  1085. strlcpy(kctl->id.name, info->name, sizeof(kctl->id.name));
  1086. else {
  1087. nameid = dsc[12 + num_ins + dsc[11 + num_ins]];
  1088. len = 0;
  1089. if (nameid)
  1090. len = snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
  1091. if (! len)
  1092. strlcpy(kctl->id.name, name, sizeof(kctl->id.name));
  1093. }
  1094. strlcat(kctl->id.name, " ", sizeof(kctl->id.name));
  1095. strlcat(kctl->id.name, valinfo->suffix, sizeof(kctl->id.name));
  1096. snd_printdd(KERN_INFO "[%d] PU [%s] ch = %d, val = %d/%d\n",
  1097. cval->id, kctl->id.name, cval->channels, cval->min, cval->max);
  1098. if ((err = add_control_to_empty(state->chip->card, kctl)) < 0)
  1099. return err;
  1100. }
  1101. return 0;
  1102. }
  1103. static int parse_audio_processing_unit(mixer_build_t *state, int unitid, unsigned char *desc)
  1104. {
  1105. return build_audio_procunit(state, unitid, desc, procunits, "Processing Unit");
  1106. }
  1107. static int parse_audio_extension_unit(mixer_build_t *state, int unitid, unsigned char *desc)
  1108. {
  1109. return build_audio_procunit(state, unitid, desc, NULL, "Extension Unit");
  1110. }
  1111. /*
  1112. * Selector Unit
  1113. */
  1114. /* info callback for selector unit
  1115. * use an enumerator type for routing
  1116. */
  1117. static int mixer_ctl_selector_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
  1118. {
  1119. usb_mixer_elem_info_t *cval = kcontrol->private_data;
  1120. char **itemlist = (char **)kcontrol->private_value;
  1121. snd_assert(itemlist, return -EINVAL);
  1122. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  1123. uinfo->count = 1;
  1124. uinfo->value.enumerated.items = cval->max;
  1125. if ((int)uinfo->value.enumerated.item >= cval->max)
  1126. uinfo->value.enumerated.item = cval->max - 1;
  1127. strcpy(uinfo->value.enumerated.name, itemlist[uinfo->value.enumerated.item]);
  1128. return 0;
  1129. }
  1130. /* get callback for selector unit */
  1131. static int mixer_ctl_selector_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  1132. {
  1133. usb_mixer_elem_info_t *cval = kcontrol->private_data;
  1134. int val, err;
  1135. err = get_cur_ctl_value(cval, 0, &val);
  1136. if (err < 0) {
  1137. if (cval->chip->ignore_ctl_error) {
  1138. ucontrol->value.enumerated.item[0] = 0;
  1139. return 0;
  1140. }
  1141. return err;
  1142. }
  1143. val = get_relative_value(cval, val);
  1144. ucontrol->value.enumerated.item[0] = val;
  1145. return 0;
  1146. }
  1147. /* put callback for selector unit */
  1148. static int mixer_ctl_selector_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  1149. {
  1150. usb_mixer_elem_info_t *cval = kcontrol->private_data;
  1151. int val, oval, err;
  1152. err = get_cur_ctl_value(cval, 0, &oval);
  1153. if (err < 0) {
  1154. if (cval->chip->ignore_ctl_error)
  1155. return 0;
  1156. return err;
  1157. }
  1158. val = ucontrol->value.enumerated.item[0];
  1159. val = get_abs_value(cval, val);
  1160. if (val != oval) {
  1161. set_cur_ctl_value(cval, 0, val);
  1162. return 1;
  1163. }
  1164. return 0;
  1165. }
  1166. /* alsa control interface for selector unit */
  1167. static snd_kcontrol_new_t mixer_selectunit_ctl = {
  1168. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1169. .name = "", /* will be filled later */
  1170. .info = mixer_ctl_selector_info,
  1171. .get = mixer_ctl_selector_get,
  1172. .put = mixer_ctl_selector_put,
  1173. };
  1174. /* private free callback.
  1175. * free both private_data and private_value
  1176. */
  1177. static void usb_mixer_selector_elem_free(snd_kcontrol_t *kctl)
  1178. {
  1179. int i, num_ins = 0;
  1180. if (kctl->private_data) {
  1181. usb_mixer_elem_info_t *cval = kctl->private_data;
  1182. num_ins = cval->max;
  1183. kfree(cval);
  1184. kctl->private_data = NULL;
  1185. }
  1186. if (kctl->private_value) {
  1187. char **itemlist = (char **)kctl->private_value;
  1188. for (i = 0; i < num_ins; i++)
  1189. kfree(itemlist[i]);
  1190. kfree(itemlist);
  1191. kctl->private_value = 0;
  1192. }
  1193. }
  1194. /*
  1195. * parse a selector unit
  1196. */
  1197. static int parse_audio_selector_unit(mixer_build_t *state, int unitid, unsigned char *desc)
  1198. {
  1199. unsigned int num_ins = desc[4];
  1200. unsigned int i, nameid, len;
  1201. int err;
  1202. usb_mixer_elem_info_t *cval;
  1203. snd_kcontrol_t *kctl;
  1204. char **namelist;
  1205. if (! num_ins || desc[0] < 6 + num_ins) {
  1206. snd_printk(KERN_ERR "invalid SELECTOR UNIT descriptor %d\n", unitid);
  1207. return -EINVAL;
  1208. }
  1209. for (i = 0; i < num_ins; i++) {
  1210. if ((err = parse_audio_unit(state, desc[5 + i])) < 0)
  1211. return err;
  1212. }
  1213. if (num_ins == 1) /* only one ? nonsense! */
  1214. return 0;
  1215. if (check_ignored_ctl(state, unitid, 0))
  1216. return 0;
  1217. cval = kcalloc(1, sizeof(*cval), GFP_KERNEL);
  1218. if (! cval) {
  1219. snd_printk(KERN_ERR "cannot malloc kcontrol\n");
  1220. return -ENOMEM;
  1221. }
  1222. cval->chip = state->chip;
  1223. cval->ctrlif = state->ctrlif;
  1224. cval->id = unitid;
  1225. cval->val_type = USB_MIXER_U8;
  1226. cval->channels = 1;
  1227. cval->min = 1;
  1228. cval->max = num_ins;
  1229. cval->res = 1;
  1230. cval->initialized = 1;
  1231. namelist = kmalloc(sizeof(char *) * num_ins, GFP_KERNEL);
  1232. if (! namelist) {
  1233. snd_printk(KERN_ERR "cannot malloc\n");
  1234. kfree(cval);
  1235. return -ENOMEM;
  1236. }
  1237. #define MAX_ITEM_NAME_LEN 64
  1238. for (i = 0; i < num_ins; i++) {
  1239. usb_audio_term_t iterm;
  1240. len = 0;
  1241. namelist[i] = kmalloc(MAX_ITEM_NAME_LEN, GFP_KERNEL);
  1242. if (! namelist[i]) {
  1243. snd_printk(KERN_ERR "cannot malloc\n");
  1244. while (--i > 0)
  1245. kfree(namelist[i]);
  1246. kfree(namelist);
  1247. kfree(cval);
  1248. return -ENOMEM;
  1249. }
  1250. if (check_input_term(state, desc[5 + i], &iterm) >= 0)
  1251. len = get_term_name(state, &iterm, namelist[i], MAX_ITEM_NAME_LEN, 0);
  1252. if (! len)
  1253. sprintf(namelist[i], "Input %d", i);
  1254. }
  1255. kctl = snd_ctl_new1(&mixer_selectunit_ctl, cval);
  1256. if (! kctl) {
  1257. snd_printk(KERN_ERR "cannot malloc kcontrol\n");
  1258. kfree(cval);
  1259. return -ENOMEM;
  1260. }
  1261. kctl->private_value = (unsigned long)namelist;
  1262. kctl->private_free = usb_mixer_selector_elem_free;
  1263. nameid = desc[desc[0] - 1];
  1264. len = check_mapped_name(state, unitid, 0, kctl->id.name, sizeof(kctl->id.name));
  1265. if (len)
  1266. ;
  1267. else if (nameid)
  1268. snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
  1269. else {
  1270. len = get_term_name(state, &state->oterm,
  1271. kctl->id.name, sizeof(kctl->id.name), 0);
  1272. if (! len)
  1273. strlcpy(kctl->id.name, "USB", sizeof(kctl->id.name));
  1274. if ((state->oterm.type & 0xff00) == 0x0100)
  1275. strlcat(kctl->id.name, " Capture Source", sizeof(kctl->id.name));
  1276. else
  1277. strlcat(kctl->id.name, " Playback Source", sizeof(kctl->id.name));
  1278. }
  1279. snd_printdd(KERN_INFO "[%d] SU [%s] items = %d\n",
  1280. cval->id, kctl->id.name, num_ins);
  1281. if ((err = add_control_to_empty(state->chip->card, kctl)) < 0)
  1282. return err;
  1283. return 0;
  1284. }
  1285. /*
  1286. * parse an audio unit recursively
  1287. */
  1288. static int parse_audio_unit(mixer_build_t *state, int unitid)
  1289. {
  1290. unsigned char *p1;
  1291. if (test_and_set_bit(unitid, state->unitbitmap))
  1292. return 0; /* the unit already visited */
  1293. p1 = find_audio_control_unit(state, unitid);
  1294. if (!p1) {
  1295. snd_printk(KERN_ERR "usbaudio: unit %d not found!\n", unitid);
  1296. return -EINVAL;
  1297. }
  1298. switch (p1[2]) {
  1299. case INPUT_TERMINAL:
  1300. return 0; /* NOP */
  1301. case MIXER_UNIT:
  1302. return parse_audio_mixer_unit(state, unitid, p1);
  1303. case SELECTOR_UNIT:
  1304. return parse_audio_selector_unit(state, unitid, p1);
  1305. case FEATURE_UNIT:
  1306. return parse_audio_feature_unit(state, unitid, p1);
  1307. case PROCESSING_UNIT:
  1308. return parse_audio_processing_unit(state, unitid, p1);
  1309. case EXTENSION_UNIT:
  1310. return parse_audio_extension_unit(state, unitid, p1);
  1311. default:
  1312. snd_printk(KERN_ERR "usbaudio: unit %u: unexpected type 0x%02x\n", unitid, p1[2]);
  1313. return -EINVAL;
  1314. }
  1315. }
  1316. /*
  1317. * create mixer controls
  1318. *
  1319. * walk through all OUTPUT_TERMINAL descriptors to search for mixers
  1320. */
  1321. int snd_usb_create_mixer(snd_usb_audio_t *chip, int ctrlif)
  1322. {
  1323. unsigned char *desc;
  1324. mixer_build_t state;
  1325. int err;
  1326. const struct usbmix_ctl_map *map;
  1327. struct usb_device_descriptor *dev = &chip->dev->descriptor;
  1328. struct usb_host_interface *hostif = &usb_ifnum_to_if(chip->dev, ctrlif)->altsetting[0];
  1329. strcpy(chip->card->mixername, "USB Mixer");
  1330. memset(&state, 0, sizeof(state));
  1331. state.chip = chip;
  1332. state.buffer = hostif->extra;
  1333. state.buflen = hostif->extralen;
  1334. state.ctrlif = ctrlif;
  1335. state.vendor = le16_to_cpu(dev->idVendor);
  1336. state.product = le16_to_cpu(dev->idProduct);
  1337. /* check the mapping table */
  1338. for (map = usbmix_ctl_maps; map->vendor; map++) {
  1339. if (map->vendor == state.vendor && map->product == state.product) {
  1340. state.map = map->map;
  1341. chip->ignore_ctl_error = map->ignore_ctl_error;
  1342. break;
  1343. }
  1344. }
  1345. #ifdef IGNORE_CTL_ERROR
  1346. chip->ignore_ctl_error = 1;
  1347. #endif
  1348. desc = NULL;
  1349. while ((desc = snd_usb_find_csint_desc(hostif->extra, hostif->extralen, desc, OUTPUT_TERMINAL)) != NULL) {
  1350. if (desc[0] < 9)
  1351. continue; /* invalid descriptor? */
  1352. set_bit(desc[3], state.unitbitmap); /* mark terminal ID as visited */
  1353. state.oterm.id = desc[3];
  1354. state.oterm.type = combine_word(&desc[4]);
  1355. state.oterm.name = desc[8];
  1356. err = parse_audio_unit(&state, desc[7]);
  1357. if (err < 0)
  1358. return err;
  1359. }
  1360. return 0;
  1361. }