usbmixer.c 41 KB

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