usbmixer.c 57 KB

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