usbmixer.c 40 KB

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