usbmixer.c 51 KB

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