usbmixer.c 57 KB

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