usbmixer.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870
  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 "usbaudio.h"
  39. /*
  40. */
  41. /* ignore error from controls - for debugging */
  42. /* #define IGNORE_CTL_ERROR */
  43. typedef struct usb_mixer_build mixer_build_t;
  44. typedef struct usb_audio_term usb_audio_term_t;
  45. typedef struct usb_mixer_elem_info usb_mixer_elem_info_t;
  46. struct usb_mixer_interface {
  47. snd_usb_audio_t *chip;
  48. unsigned int ctrlif;
  49. struct list_head list;
  50. unsigned int ignore_ctl_error;
  51. struct urb *urb;
  52. usb_mixer_elem_info_t **id_elems; /* array[256], indexed by unit id */
  53. /* Sound Blaster remote control stuff */
  54. enum {
  55. RC_NONE,
  56. RC_EXTIGY,
  57. RC_AUDIGY2NX,
  58. } rc_type;
  59. unsigned long rc_hwdep_open;
  60. u32 rc_code;
  61. wait_queue_head_t rc_waitq;
  62. struct urb *rc_urb;
  63. struct usb_ctrlrequest *rc_setup_packet;
  64. u8 rc_buffer[6];
  65. };
  66. struct usb_audio_term {
  67. int id;
  68. int type;
  69. int channels;
  70. unsigned int chconfig;
  71. int name;
  72. };
  73. struct usbmix_name_map;
  74. struct usb_mixer_build {
  75. snd_usb_audio_t *chip;
  76. struct usb_mixer_interface *mixer;
  77. unsigned char *buffer;
  78. unsigned int buflen;
  79. DECLARE_BITMAP(unitbitmap, 256);
  80. usb_audio_term_t oterm;
  81. const struct usbmix_name_map *map;
  82. const struct usbmix_selector_map *selector_map;
  83. };
  84. struct usb_mixer_elem_info {
  85. struct usb_mixer_interface *mixer;
  86. usb_mixer_elem_info_t *next_id_elem; /* list of controls with same id */
  87. snd_ctl_elem_id_t *elem_id;
  88. unsigned int id;
  89. unsigned int control; /* CS or ICN (high byte) */
  90. unsigned int cmask; /* channel mask bitmap: 0 = master */
  91. int channels;
  92. int val_type;
  93. int min, max, res;
  94. u8 initialized;
  95. };
  96. enum {
  97. USB_FEATURE_NONE = 0,
  98. USB_FEATURE_MUTE = 1,
  99. USB_FEATURE_VOLUME,
  100. USB_FEATURE_BASS,
  101. USB_FEATURE_MID,
  102. USB_FEATURE_TREBLE,
  103. USB_FEATURE_GEQ,
  104. USB_FEATURE_AGC,
  105. USB_FEATURE_DELAY,
  106. USB_FEATURE_BASSBOOST,
  107. USB_FEATURE_LOUDNESS
  108. };
  109. enum {
  110. USB_MIXER_BOOLEAN,
  111. USB_MIXER_INV_BOOLEAN,
  112. USB_MIXER_S8,
  113. USB_MIXER_U8,
  114. USB_MIXER_S16,
  115. USB_MIXER_U16,
  116. };
  117. enum {
  118. USB_PROC_UPDOWN = 1,
  119. USB_PROC_UPDOWN_SWITCH = 1,
  120. USB_PROC_UPDOWN_MODE_SEL = 2,
  121. USB_PROC_PROLOGIC = 2,
  122. USB_PROC_PROLOGIC_SWITCH = 1,
  123. USB_PROC_PROLOGIC_MODE_SEL = 2,
  124. USB_PROC_3DENH = 3,
  125. USB_PROC_3DENH_SWITCH = 1,
  126. USB_PROC_3DENH_SPACE = 2,
  127. USB_PROC_REVERB = 4,
  128. USB_PROC_REVERB_SWITCH = 1,
  129. USB_PROC_REVERB_LEVEL = 2,
  130. USB_PROC_REVERB_TIME = 3,
  131. USB_PROC_REVERB_DELAY = 4,
  132. USB_PROC_CHORUS = 5,
  133. USB_PROC_CHORUS_SWITCH = 1,
  134. USB_PROC_CHORUS_LEVEL = 2,
  135. USB_PROC_CHORUS_RATE = 3,
  136. USB_PROC_CHORUS_DEPTH = 4,
  137. USB_PROC_DCR = 6,
  138. USB_PROC_DCR_SWITCH = 1,
  139. USB_PROC_DCR_RATIO = 2,
  140. USB_PROC_DCR_MAX_AMP = 3,
  141. USB_PROC_DCR_THRESHOLD = 4,
  142. USB_PROC_DCR_ATTACK = 5,
  143. USB_PROC_DCR_RELEASE = 6,
  144. };
  145. #define MAX_CHANNELS 10 /* max logical channels */
  146. /*
  147. * manual mapping of mixer names
  148. * if the mixer topology is too complicated and the parsed names are
  149. * ambiguous, add the entries in usbmixer_maps.c.
  150. */
  151. #include "usbmixer_maps.c"
  152. /* get the mapped name if the unit matches */
  153. static int check_mapped_name(mixer_build_t *state, int unitid, int control, char *buf, int buflen)
  154. {
  155. const struct usbmix_name_map *p;
  156. if (! state->map)
  157. return 0;
  158. for (p = state->map; p->id; p++) {
  159. if (p->id == unitid && p->name &&
  160. (! control || ! p->control || control == p->control)) {
  161. buflen--;
  162. return strlcpy(buf, p->name, buflen);
  163. }
  164. }
  165. return 0;
  166. }
  167. /* check whether the control should be ignored */
  168. static int check_ignored_ctl(mixer_build_t *state, int unitid, int control)
  169. {
  170. const struct usbmix_name_map *p;
  171. if (! state->map)
  172. return 0;
  173. for (p = state->map; p->id; p++) {
  174. if (p->id == unitid && ! p->name &&
  175. (! control || ! p->control || control == p->control)) {
  176. // printk("ignored control %d:%d\n", unitid, control);
  177. return 1;
  178. }
  179. }
  180. return 0;
  181. }
  182. /* get the mapped selector source name */
  183. static int check_mapped_selector_name(mixer_build_t *state, int unitid,
  184. int index, char *buf, int buflen)
  185. {
  186. const struct usbmix_selector_map *p;
  187. if (! state->selector_map)
  188. return 0;
  189. for (p = state->selector_map; p->id; p++) {
  190. if (p->id == unitid && index < p->count)
  191. return strlcpy(buf, p->names[index], buflen);
  192. }
  193. return 0;
  194. }
  195. /*
  196. * find an audio control unit with the given unit id
  197. */
  198. static void *find_audio_control_unit(mixer_build_t *state, unsigned char unit)
  199. {
  200. unsigned char *p;
  201. p = NULL;
  202. while ((p = snd_usb_find_desc(state->buffer, state->buflen, p,
  203. USB_DT_CS_INTERFACE)) != NULL) {
  204. if (p[0] >= 4 && p[2] >= INPUT_TERMINAL && p[2] <= EXTENSION_UNIT && p[3] == unit)
  205. return p;
  206. }
  207. return NULL;
  208. }
  209. /*
  210. * copy a string with the given id
  211. */
  212. static int snd_usb_copy_string_desc(mixer_build_t *state, int index, char *buf, int maxlen)
  213. {
  214. int len = usb_string(state->chip->dev, index, buf, maxlen - 1);
  215. buf[len] = 0;
  216. return len;
  217. }
  218. /*
  219. * convert from the byte/word on usb descriptor to the zero-based integer
  220. */
  221. static int convert_signed_value(usb_mixer_elem_info_t *cval, int val)
  222. {
  223. switch (cval->val_type) {
  224. case USB_MIXER_BOOLEAN:
  225. return !!val;
  226. case USB_MIXER_INV_BOOLEAN:
  227. return !val;
  228. case USB_MIXER_U8:
  229. val &= 0xff;
  230. break;
  231. case USB_MIXER_S8:
  232. val &= 0xff;
  233. if (val >= 0x80)
  234. val -= 0x100;
  235. break;
  236. case USB_MIXER_U16:
  237. val &= 0xffff;
  238. break;
  239. case USB_MIXER_S16:
  240. val &= 0xffff;
  241. if (val >= 0x8000)
  242. val -= 0x10000;
  243. break;
  244. }
  245. return val;
  246. }
  247. /*
  248. * convert from the zero-based int to the byte/word for usb descriptor
  249. */
  250. static int convert_bytes_value(usb_mixer_elem_info_t *cval, int val)
  251. {
  252. switch (cval->val_type) {
  253. case USB_MIXER_BOOLEAN:
  254. return !!val;
  255. case USB_MIXER_INV_BOOLEAN:
  256. return !val;
  257. case USB_MIXER_S8:
  258. case USB_MIXER_U8:
  259. return val & 0xff;
  260. case USB_MIXER_S16:
  261. case USB_MIXER_U16:
  262. return val & 0xffff;
  263. }
  264. return 0; /* not reached */
  265. }
  266. static int get_relative_value(usb_mixer_elem_info_t *cval, int val)
  267. {
  268. if (! cval->res)
  269. cval->res = 1;
  270. if (val < cval->min)
  271. return 0;
  272. else if (val > cval->max)
  273. return (cval->max - cval->min) / cval->res;
  274. else
  275. return (val - cval->min) / cval->res;
  276. }
  277. static int get_abs_value(usb_mixer_elem_info_t *cval, int val)
  278. {
  279. if (val < 0)
  280. return cval->min;
  281. if (! cval->res)
  282. cval->res = 1;
  283. val *= cval->res;
  284. val += cval->min;
  285. if (val > cval->max)
  286. return cval->max;
  287. return val;
  288. }
  289. /*
  290. * retrieve a mixer value
  291. */
  292. static int get_ctl_value(usb_mixer_elem_info_t *cval, int request, int validx, int *value_ret)
  293. {
  294. unsigned char buf[2];
  295. int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
  296. int timeout = 10;
  297. while (timeout-- > 0) {
  298. if (snd_usb_ctl_msg(cval->mixer->chip->dev,
  299. usb_rcvctrlpipe(cval->mixer->chip->dev, 0),
  300. request,
  301. USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
  302. validx, cval->mixer->ctrlif | (cval->id << 8),
  303. buf, val_len, 100) >= 0) {
  304. *value_ret = convert_signed_value(cval, snd_usb_combine_bytes(buf, val_len));
  305. return 0;
  306. }
  307. }
  308. snd_printdd(KERN_ERR "cannot get ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d\n",
  309. request, validx, cval->mixer->ctrlif | (cval->id << 8), cval->val_type);
  310. return -EINVAL;
  311. }
  312. static int get_cur_ctl_value(usb_mixer_elem_info_t *cval, int validx, int *value)
  313. {
  314. return get_ctl_value(cval, GET_CUR, validx, value);
  315. }
  316. /* channel = 0: master, 1 = first channel */
  317. inline static int get_cur_mix_value(usb_mixer_elem_info_t *cval, int channel, int *value)
  318. {
  319. return get_ctl_value(cval, GET_CUR, (cval->control << 8) | channel, value);
  320. }
  321. /*
  322. * set a mixer value
  323. */
  324. static int set_ctl_value(usb_mixer_elem_info_t *cval, int request, int validx, int value_set)
  325. {
  326. unsigned char buf[2];
  327. int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
  328. int timeout = 10;
  329. value_set = convert_bytes_value(cval, value_set);
  330. buf[0] = value_set & 0xff;
  331. buf[1] = (value_set >> 8) & 0xff;
  332. while (timeout -- > 0)
  333. if (snd_usb_ctl_msg(cval->mixer->chip->dev,
  334. usb_sndctrlpipe(cval->mixer->chip->dev, 0),
  335. request,
  336. USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
  337. validx, cval->mixer->ctrlif | (cval->id << 8),
  338. buf, val_len, 100) >= 0)
  339. return 0;
  340. snd_printdd(KERN_ERR "cannot set ctl value: req = %#x, wValue = %#x, wIndex = %#x, type = %d, data = %#x/%#x\n",
  341. request, validx, cval->mixer->ctrlif | (cval->id << 8), cval->val_type, buf[0], buf[1]);
  342. return -EINVAL;
  343. }
  344. static int set_cur_ctl_value(usb_mixer_elem_info_t *cval, int validx, int value)
  345. {
  346. return set_ctl_value(cval, SET_CUR, validx, value);
  347. }
  348. inline static int set_cur_mix_value(usb_mixer_elem_info_t *cval, int channel, int value)
  349. {
  350. return set_ctl_value(cval, SET_CUR, (cval->control << 8) | channel, value);
  351. }
  352. /*
  353. * parser routines begin here...
  354. */
  355. static int parse_audio_unit(mixer_build_t *state, int unitid);
  356. /*
  357. * check if the input/output channel routing is enabled on the given bitmap.
  358. * used for mixer unit parser
  359. */
  360. static int check_matrix_bitmap(unsigned char *bmap, int ich, int och, int num_outs)
  361. {
  362. int idx = ich * num_outs + och;
  363. return bmap[idx >> 3] & (0x80 >> (idx & 7));
  364. }
  365. /*
  366. * add an alsa control element
  367. * search and increment the index until an empty slot is found.
  368. *
  369. * if failed, give up and free the control instance.
  370. */
  371. static int add_control_to_empty(mixer_build_t *state, snd_kcontrol_t *kctl)
  372. {
  373. usb_mixer_elem_info_t *cval = kctl->private_data;
  374. int err;
  375. while (snd_ctl_find_id(state->chip->card, &kctl->id))
  376. kctl->id.index++;
  377. if ((err = snd_ctl_add(state->chip->card, kctl)) < 0) {
  378. snd_printd(KERN_ERR "cannot add control (err = %d)\n", err);
  379. snd_ctl_free_one(kctl);
  380. return err;
  381. }
  382. cval->elem_id = &kctl->id;
  383. cval->next_id_elem = state->mixer->id_elems[cval->id];
  384. state->mixer->id_elems[cval->id] = cval;
  385. return 0;
  386. }
  387. /*
  388. * get a terminal name string
  389. */
  390. static struct iterm_name_combo {
  391. int type;
  392. char *name;
  393. } iterm_names[] = {
  394. { 0x0300, "Output" },
  395. { 0x0301, "Speaker" },
  396. { 0x0302, "Headphone" },
  397. { 0x0303, "HMD Audio" },
  398. { 0x0304, "Desktop Speaker" },
  399. { 0x0305, "Room Speaker" },
  400. { 0x0306, "Com Speaker" },
  401. { 0x0307, "LFE" },
  402. { 0x0600, "External In" },
  403. { 0x0601, "Analog In" },
  404. { 0x0602, "Digital In" },
  405. { 0x0603, "Line" },
  406. { 0x0604, "Legacy In" },
  407. { 0x0605, "IEC958 In" },
  408. { 0x0606, "1394 DA Stream" },
  409. { 0x0607, "1394 DV Stream" },
  410. { 0x0700, "Embedded" },
  411. { 0x0701, "Noise Source" },
  412. { 0x0702, "Equalization Noise" },
  413. { 0x0703, "CD" },
  414. { 0x0704, "DAT" },
  415. { 0x0705, "DCC" },
  416. { 0x0706, "MiniDisk" },
  417. { 0x0707, "Analog Tape" },
  418. { 0x0708, "Phonograph" },
  419. { 0x0709, "VCR Audio" },
  420. { 0x070a, "Video Disk Audio" },
  421. { 0x070b, "DVD Audio" },
  422. { 0x070c, "TV Tuner Audio" },
  423. { 0x070d, "Satellite Rec Audio" },
  424. { 0x070e, "Cable Tuner Audio" },
  425. { 0x070f, "DSS Audio" },
  426. { 0x0710, "Radio Receiver" },
  427. { 0x0711, "Radio Transmitter" },
  428. { 0x0712, "Multi-Track Recorder" },
  429. { 0x0713, "Synthesizer" },
  430. { 0 },
  431. };
  432. static int get_term_name(mixer_build_t *state, usb_audio_term_t *iterm,
  433. unsigned char *name, int maxlen, int term_only)
  434. {
  435. struct iterm_name_combo *names;
  436. if (iterm->name)
  437. return snd_usb_copy_string_desc(state, iterm->name, name, maxlen);
  438. /* virtual type - not a real terminal */
  439. if (iterm->type >> 16) {
  440. if (term_only)
  441. return 0;
  442. switch (iterm->type >> 16) {
  443. case SELECTOR_UNIT:
  444. strcpy(name, "Selector"); return 8;
  445. case PROCESSING_UNIT:
  446. strcpy(name, "Process Unit"); return 12;
  447. case EXTENSION_UNIT:
  448. strcpy(name, "Ext Unit"); return 8;
  449. case MIXER_UNIT:
  450. strcpy(name, "Mixer"); return 5;
  451. default:
  452. return sprintf(name, "Unit %d", iterm->id);
  453. }
  454. }
  455. switch (iterm->type & 0xff00) {
  456. case 0x0100:
  457. strcpy(name, "PCM"); return 3;
  458. case 0x0200:
  459. strcpy(name, "Mic"); return 3;
  460. case 0x0400:
  461. strcpy(name, "Headset"); return 7;
  462. case 0x0500:
  463. strcpy(name, "Phone"); return 5;
  464. }
  465. for (names = iterm_names; names->type; names++)
  466. if (names->type == iterm->type) {
  467. strcpy(name, names->name);
  468. return strlen(names->name);
  469. }
  470. return 0;
  471. }
  472. /*
  473. * parse the source unit recursively until it reaches to a terminal
  474. * or a branched unit.
  475. */
  476. static int check_input_term(mixer_build_t *state, int id, usb_audio_term_t *term)
  477. {
  478. unsigned char *p1;
  479. memset(term, 0, sizeof(*term));
  480. while ((p1 = find_audio_control_unit(state, id)) != NULL) {
  481. term->id = id;
  482. switch (p1[2]) {
  483. case INPUT_TERMINAL:
  484. term->type = combine_word(p1 + 4);
  485. term->channels = p1[7];
  486. term->chconfig = combine_word(p1 + 8);
  487. term->name = p1[11];
  488. return 0;
  489. case FEATURE_UNIT:
  490. id = p1[4];
  491. break; /* continue to parse */
  492. case MIXER_UNIT:
  493. term->type = p1[2] << 16; /* virtual type */
  494. term->channels = p1[5 + p1[4]];
  495. term->chconfig = combine_word(p1 + 6 + p1[4]);
  496. term->name = p1[p1[0] - 1];
  497. return 0;
  498. case SELECTOR_UNIT:
  499. /* call recursively to retrieve the channel info */
  500. if (check_input_term(state, p1[5], term) < 0)
  501. return -ENODEV;
  502. term->type = p1[2] << 16; /* virtual type */
  503. term->id = id;
  504. term->name = p1[9 + p1[0] - 1];
  505. return 0;
  506. case PROCESSING_UNIT:
  507. case EXTENSION_UNIT:
  508. if (p1[6] == 1) {
  509. id = p1[7];
  510. break; /* continue to parse */
  511. }
  512. term->type = p1[2] << 16; /* virtual type */
  513. term->channels = p1[7 + p1[6]];
  514. term->chconfig = combine_word(p1 + 8 + p1[6]);
  515. term->name = p1[12 + p1[6] + p1[11 + p1[6]]];
  516. return 0;
  517. default:
  518. return -ENODEV;
  519. }
  520. }
  521. return -ENODEV;
  522. }
  523. /*
  524. * Feature Unit
  525. */
  526. /* feature unit control information */
  527. struct usb_feature_control_info {
  528. const char *name;
  529. unsigned int type; /* control type (mute, volume, etc.) */
  530. };
  531. static struct usb_feature_control_info audio_feature_info[] = {
  532. { "Mute", USB_MIXER_INV_BOOLEAN },
  533. { "Volume", USB_MIXER_S16 },
  534. { "Tone Control - Bass", USB_MIXER_S8 },
  535. { "Tone Control - Mid", USB_MIXER_S8 },
  536. { "Tone Control - Treble", USB_MIXER_S8 },
  537. { "Graphic Equalizer", USB_MIXER_S8 }, /* FIXME: not implemeted yet */
  538. { "Auto Gain Control", USB_MIXER_BOOLEAN },
  539. { "Delay Control", USB_MIXER_U16 },
  540. { "Bass Boost", USB_MIXER_BOOLEAN },
  541. { "Loudness", USB_MIXER_BOOLEAN },
  542. };
  543. /* private_free callback */
  544. static void usb_mixer_elem_free(snd_kcontrol_t *kctl)
  545. {
  546. if (kctl->private_data) {
  547. kfree(kctl->private_data);
  548. kctl->private_data = NULL;
  549. }
  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. if (! type)
  1090. return 0; /* undefined? */
  1091. for (info = list; info && info->type; info++)
  1092. if (info->type == type)
  1093. break;
  1094. if (! info || ! info->type)
  1095. info = &default_info;
  1096. for (valinfo = info->values; valinfo->control; valinfo++) {
  1097. /* FIXME: bitmap might be longer than 8bit */
  1098. if (! (dsc[12 + num_ins] & (1 << (valinfo->control - 1))))
  1099. continue;
  1100. if (check_ignored_ctl(state, unitid, valinfo->control))
  1101. continue;
  1102. cval = kcalloc(1, sizeof(*cval), GFP_KERNEL);
  1103. if (! cval) {
  1104. snd_printk(KERN_ERR "cannot malloc kcontrol\n");
  1105. return -ENOMEM;
  1106. }
  1107. cval->mixer = state->mixer;
  1108. cval->id = unitid;
  1109. cval->control = valinfo->control;
  1110. cval->val_type = valinfo->val_type;
  1111. cval->channels = 1;
  1112. /* get min/max values */
  1113. if (type == USB_PROC_UPDOWN && cval->control == USB_PROC_UPDOWN_MODE_SEL) {
  1114. /* FIXME: hard-coded */
  1115. cval->min = 1;
  1116. cval->max = dsc[15];
  1117. cval->res = 1;
  1118. cval->initialized = 1;
  1119. } else
  1120. get_min_max(cval, valinfo->min_value);
  1121. kctl = snd_ctl_new1(&mixer_procunit_ctl, cval);
  1122. if (! kctl) {
  1123. snd_printk(KERN_ERR "cannot malloc kcontrol\n");
  1124. kfree(cval);
  1125. return -ENOMEM;
  1126. }
  1127. kctl->private_free = usb_mixer_elem_free;
  1128. if (check_mapped_name(state, unitid, cval->control, kctl->id.name, sizeof(kctl->id.name)))
  1129. ;
  1130. else if (info->name)
  1131. strlcpy(kctl->id.name, info->name, sizeof(kctl->id.name));
  1132. else {
  1133. nameid = dsc[12 + num_ins + dsc[11 + num_ins]];
  1134. len = 0;
  1135. if (nameid)
  1136. len = snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
  1137. if (! len)
  1138. strlcpy(kctl->id.name, name, sizeof(kctl->id.name));
  1139. }
  1140. strlcat(kctl->id.name, " ", sizeof(kctl->id.name));
  1141. strlcat(kctl->id.name, valinfo->suffix, sizeof(kctl->id.name));
  1142. snd_printdd(KERN_INFO "[%d] PU [%s] ch = %d, val = %d/%d\n",
  1143. cval->id, kctl->id.name, cval->channels, cval->min, cval->max);
  1144. if ((err = add_control_to_empty(state, kctl)) < 0)
  1145. return err;
  1146. }
  1147. return 0;
  1148. }
  1149. static int parse_audio_processing_unit(mixer_build_t *state, int unitid, unsigned char *desc)
  1150. {
  1151. return build_audio_procunit(state, unitid, desc, procunits, "Processing Unit");
  1152. }
  1153. static int parse_audio_extension_unit(mixer_build_t *state, int unitid, unsigned char *desc)
  1154. {
  1155. return build_audio_procunit(state, unitid, desc, NULL, "Extension Unit");
  1156. }
  1157. /*
  1158. * Selector Unit
  1159. */
  1160. /* info callback for selector unit
  1161. * use an enumerator type for routing
  1162. */
  1163. static int mixer_ctl_selector_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
  1164. {
  1165. usb_mixer_elem_info_t *cval = kcontrol->private_data;
  1166. char **itemlist = (char **)kcontrol->private_value;
  1167. snd_assert(itemlist, return -EINVAL);
  1168. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  1169. uinfo->count = 1;
  1170. uinfo->value.enumerated.items = cval->max;
  1171. if ((int)uinfo->value.enumerated.item >= cval->max)
  1172. uinfo->value.enumerated.item = cval->max - 1;
  1173. strcpy(uinfo->value.enumerated.name, itemlist[uinfo->value.enumerated.item]);
  1174. return 0;
  1175. }
  1176. /* get callback for selector unit */
  1177. static int mixer_ctl_selector_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  1178. {
  1179. usb_mixer_elem_info_t *cval = kcontrol->private_data;
  1180. int val, err;
  1181. err = get_cur_ctl_value(cval, 0, &val);
  1182. if (err < 0) {
  1183. if (cval->mixer->ignore_ctl_error) {
  1184. ucontrol->value.enumerated.item[0] = 0;
  1185. return 0;
  1186. }
  1187. return err;
  1188. }
  1189. val = get_relative_value(cval, val);
  1190. ucontrol->value.enumerated.item[0] = val;
  1191. return 0;
  1192. }
  1193. /* put callback for selector unit */
  1194. static int mixer_ctl_selector_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  1195. {
  1196. usb_mixer_elem_info_t *cval = kcontrol->private_data;
  1197. int val, oval, err;
  1198. err = get_cur_ctl_value(cval, 0, &oval);
  1199. if (err < 0) {
  1200. if (cval->mixer->ignore_ctl_error)
  1201. return 0;
  1202. return err;
  1203. }
  1204. val = ucontrol->value.enumerated.item[0];
  1205. val = get_abs_value(cval, val);
  1206. if (val != oval) {
  1207. set_cur_ctl_value(cval, 0, val);
  1208. return 1;
  1209. }
  1210. return 0;
  1211. }
  1212. /* alsa control interface for selector unit */
  1213. static snd_kcontrol_new_t mixer_selectunit_ctl = {
  1214. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1215. .name = "", /* will be filled later */
  1216. .info = mixer_ctl_selector_info,
  1217. .get = mixer_ctl_selector_get,
  1218. .put = mixer_ctl_selector_put,
  1219. };
  1220. /* private free callback.
  1221. * free both private_data and private_value
  1222. */
  1223. static void usb_mixer_selector_elem_free(snd_kcontrol_t *kctl)
  1224. {
  1225. int i, num_ins = 0;
  1226. if (kctl->private_data) {
  1227. usb_mixer_elem_info_t *cval = kctl->private_data;
  1228. num_ins = cval->max;
  1229. kfree(cval);
  1230. kctl->private_data = NULL;
  1231. }
  1232. if (kctl->private_value) {
  1233. char **itemlist = (char **)kctl->private_value;
  1234. for (i = 0; i < num_ins; i++)
  1235. kfree(itemlist[i]);
  1236. kfree(itemlist);
  1237. kctl->private_value = 0;
  1238. }
  1239. }
  1240. /*
  1241. * parse a selector unit
  1242. */
  1243. static int parse_audio_selector_unit(mixer_build_t *state, int unitid, unsigned char *desc)
  1244. {
  1245. unsigned int num_ins = desc[4];
  1246. unsigned int i, nameid, len;
  1247. int err;
  1248. usb_mixer_elem_info_t *cval;
  1249. snd_kcontrol_t *kctl;
  1250. char **namelist;
  1251. if (! num_ins || desc[0] < 6 + num_ins) {
  1252. snd_printk(KERN_ERR "invalid SELECTOR UNIT descriptor %d\n", unitid);
  1253. return -EINVAL;
  1254. }
  1255. for (i = 0; i < num_ins; i++) {
  1256. if ((err = parse_audio_unit(state, desc[5 + i])) < 0)
  1257. return err;
  1258. }
  1259. if (num_ins == 1) /* only one ? nonsense! */
  1260. return 0;
  1261. if (check_ignored_ctl(state, unitid, 0))
  1262. return 0;
  1263. cval = kcalloc(1, sizeof(*cval), GFP_KERNEL);
  1264. if (! cval) {
  1265. snd_printk(KERN_ERR "cannot malloc kcontrol\n");
  1266. return -ENOMEM;
  1267. }
  1268. cval->mixer = state->mixer;
  1269. cval->id = unitid;
  1270. cval->val_type = USB_MIXER_U8;
  1271. cval->channels = 1;
  1272. cval->min = 1;
  1273. cval->max = num_ins;
  1274. cval->res = 1;
  1275. cval->initialized = 1;
  1276. namelist = kmalloc(sizeof(char *) * num_ins, GFP_KERNEL);
  1277. if (! namelist) {
  1278. snd_printk(KERN_ERR "cannot malloc\n");
  1279. kfree(cval);
  1280. return -ENOMEM;
  1281. }
  1282. #define MAX_ITEM_NAME_LEN 64
  1283. for (i = 0; i < num_ins; i++) {
  1284. usb_audio_term_t iterm;
  1285. len = 0;
  1286. namelist[i] = kmalloc(MAX_ITEM_NAME_LEN, GFP_KERNEL);
  1287. if (! namelist[i]) {
  1288. snd_printk(KERN_ERR "cannot malloc\n");
  1289. while (--i > 0)
  1290. kfree(namelist[i]);
  1291. kfree(namelist);
  1292. kfree(cval);
  1293. return -ENOMEM;
  1294. }
  1295. len = check_mapped_selector_name(state, unitid, i, namelist[i],
  1296. MAX_ITEM_NAME_LEN);
  1297. if (! len && check_input_term(state, desc[5 + i], &iterm) >= 0)
  1298. len = get_term_name(state, &iterm, namelist[i], MAX_ITEM_NAME_LEN, 0);
  1299. if (! len)
  1300. sprintf(namelist[i], "Input %d", i);
  1301. }
  1302. kctl = snd_ctl_new1(&mixer_selectunit_ctl, cval);
  1303. if (! kctl) {
  1304. snd_printk(KERN_ERR "cannot malloc kcontrol\n");
  1305. kfree(cval);
  1306. return -ENOMEM;
  1307. }
  1308. kctl->private_value = (unsigned long)namelist;
  1309. kctl->private_free = usb_mixer_selector_elem_free;
  1310. nameid = desc[desc[0] - 1];
  1311. len = check_mapped_name(state, unitid, 0, kctl->id.name, sizeof(kctl->id.name));
  1312. if (len)
  1313. ;
  1314. else if (nameid)
  1315. snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
  1316. else {
  1317. len = get_term_name(state, &state->oterm,
  1318. kctl->id.name, sizeof(kctl->id.name), 0);
  1319. if (! len)
  1320. strlcpy(kctl->id.name, "USB", sizeof(kctl->id.name));
  1321. if ((state->oterm.type & 0xff00) == 0x0100)
  1322. strlcat(kctl->id.name, " Capture Source", sizeof(kctl->id.name));
  1323. else
  1324. strlcat(kctl->id.name, " Playback Source", sizeof(kctl->id.name));
  1325. }
  1326. snd_printdd(KERN_INFO "[%d] SU [%s] items = %d\n",
  1327. cval->id, kctl->id.name, num_ins);
  1328. if ((err = add_control_to_empty(state, kctl)) < 0)
  1329. return err;
  1330. return 0;
  1331. }
  1332. /*
  1333. * parse an audio unit recursively
  1334. */
  1335. static int parse_audio_unit(mixer_build_t *state, int unitid)
  1336. {
  1337. unsigned char *p1;
  1338. if (test_and_set_bit(unitid, state->unitbitmap))
  1339. return 0; /* the unit already visited */
  1340. p1 = find_audio_control_unit(state, unitid);
  1341. if (!p1) {
  1342. snd_printk(KERN_ERR "usbaudio: unit %d not found!\n", unitid);
  1343. return -EINVAL;
  1344. }
  1345. switch (p1[2]) {
  1346. case INPUT_TERMINAL:
  1347. return 0; /* NOP */
  1348. case MIXER_UNIT:
  1349. return parse_audio_mixer_unit(state, unitid, p1);
  1350. case SELECTOR_UNIT:
  1351. return parse_audio_selector_unit(state, unitid, p1);
  1352. case FEATURE_UNIT:
  1353. return parse_audio_feature_unit(state, unitid, p1);
  1354. case PROCESSING_UNIT:
  1355. return parse_audio_processing_unit(state, unitid, p1);
  1356. case EXTENSION_UNIT:
  1357. return parse_audio_extension_unit(state, unitid, p1);
  1358. default:
  1359. snd_printk(KERN_ERR "usbaudio: unit %u: unexpected type 0x%02x\n", unitid, p1[2]);
  1360. return -EINVAL;
  1361. }
  1362. }
  1363. static void snd_usb_mixer_free(struct usb_mixer_interface *mixer)
  1364. {
  1365. kfree(mixer->id_elems);
  1366. if (mixer->urb) {
  1367. kfree(mixer->urb->transfer_buffer);
  1368. usb_free_urb(mixer->urb);
  1369. }
  1370. if (mixer->rc_urb)
  1371. usb_free_urb(mixer->rc_urb);
  1372. kfree(mixer->rc_setup_packet);
  1373. kfree(mixer);
  1374. }
  1375. static int snd_usb_mixer_dev_free(snd_device_t *device)
  1376. {
  1377. struct usb_mixer_interface *mixer = device->device_data;
  1378. snd_usb_mixer_free(mixer);
  1379. return 0;
  1380. }
  1381. /*
  1382. * create mixer controls
  1383. *
  1384. * walk through all OUTPUT_TERMINAL descriptors to search for mixers
  1385. */
  1386. static int snd_usb_mixer_controls(struct usb_mixer_interface *mixer)
  1387. {
  1388. unsigned char *desc;
  1389. mixer_build_t state;
  1390. int err;
  1391. const struct usbmix_ctl_map *map;
  1392. struct usb_host_interface *hostif;
  1393. hostif = &usb_ifnum_to_if(mixer->chip->dev, mixer->ctrlif)->altsetting[0];
  1394. memset(&state, 0, sizeof(state));
  1395. state.chip = mixer->chip;
  1396. state.mixer = mixer;
  1397. state.buffer = hostif->extra;
  1398. state.buflen = hostif->extralen;
  1399. /* check the mapping table */
  1400. for (map = usbmix_ctl_maps; map->id; map++) {
  1401. if (map->id == state.chip->usb_id) {
  1402. state.map = map->map;
  1403. state.selector_map = map->selector_map;
  1404. mixer->ignore_ctl_error = map->ignore_ctl_error;
  1405. break;
  1406. }
  1407. }
  1408. desc = NULL;
  1409. while ((desc = snd_usb_find_csint_desc(hostif->extra, hostif->extralen, desc, OUTPUT_TERMINAL)) != NULL) {
  1410. if (desc[0] < 9)
  1411. continue; /* invalid descriptor? */
  1412. set_bit(desc[3], state.unitbitmap); /* mark terminal ID as visited */
  1413. state.oterm.id = desc[3];
  1414. state.oterm.type = combine_word(&desc[4]);
  1415. state.oterm.name = desc[8];
  1416. err = parse_audio_unit(&state, desc[7]);
  1417. if (err < 0)
  1418. return err;
  1419. }
  1420. return 0;
  1421. }
  1422. static void snd_usb_mixer_notify_id(struct usb_mixer_interface *mixer,
  1423. int unitid)
  1424. {
  1425. usb_mixer_elem_info_t *info;
  1426. for (info = mixer->id_elems[unitid]; info; info = info->next_id_elem)
  1427. snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
  1428. info->elem_id);
  1429. }
  1430. static void snd_usb_mixer_memory_change(struct usb_mixer_interface *mixer,
  1431. int unitid)
  1432. {
  1433. /* SB remote control */
  1434. if (mixer->rc_type != RC_NONE && unitid == 0) {
  1435. /* read control code from device memory */
  1436. mixer->rc_urb->dev = mixer->chip->dev;
  1437. usb_submit_urb(mixer->rc_urb, GFP_ATOMIC);
  1438. }
  1439. }
  1440. static void snd_usb_mixer_status_complete(struct urb *urb, struct pt_regs *regs)
  1441. {
  1442. struct usb_mixer_interface *mixer = urb->context;
  1443. if (urb->status == 0) {
  1444. u8 *buf = urb->transfer_buffer;
  1445. int i;
  1446. for (i = urb->actual_length; i >= 2; buf += 2, i -= 2) {
  1447. snd_printd(KERN_DEBUG "status interrupt: %02x %02x\n",
  1448. buf[0], buf[1]);
  1449. /* ignore any notifications not from the control interface */
  1450. if ((buf[0] & 0x0f) != 0)
  1451. continue;
  1452. if (!(buf[0] & 0x40))
  1453. snd_usb_mixer_notify_id(mixer, buf[1]);
  1454. else
  1455. snd_usb_mixer_memory_change(mixer, buf[1]);
  1456. }
  1457. }
  1458. if (urb->status != -ENOENT && urb->status != -ECONNRESET) {
  1459. urb->dev = mixer->chip->dev;
  1460. usb_submit_urb(urb, GFP_ATOMIC);
  1461. }
  1462. }
  1463. /* create the handler for the optional status interrupt endpoint */
  1464. static int snd_usb_mixer_status_create(struct usb_mixer_interface *mixer)
  1465. {
  1466. struct usb_host_interface *hostif;
  1467. struct usb_endpoint_descriptor *ep;
  1468. void *transfer_buffer;
  1469. int buffer_length;
  1470. unsigned int epnum;
  1471. hostif = &usb_ifnum_to_if(mixer->chip->dev, mixer->ctrlif)->altsetting[0];
  1472. /* we need one interrupt input endpoint */
  1473. if (get_iface_desc(hostif)->bNumEndpoints < 1)
  1474. return 0;
  1475. ep = get_endpoint(hostif, 0);
  1476. if ((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != USB_DIR_IN ||
  1477. (ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
  1478. return 0;
  1479. epnum = ep->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
  1480. buffer_length = le16_to_cpu(ep->wMaxPacketSize);
  1481. transfer_buffer = kmalloc(buffer_length, GFP_KERNEL);
  1482. if (!transfer_buffer)
  1483. return -ENOMEM;
  1484. mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
  1485. if (!mixer->urb) {
  1486. kfree(transfer_buffer);
  1487. return -ENOMEM;
  1488. }
  1489. usb_fill_int_urb(mixer->urb, mixer->chip->dev,
  1490. usb_rcvintpipe(mixer->chip->dev, epnum),
  1491. transfer_buffer, buffer_length,
  1492. snd_usb_mixer_status_complete, mixer, ep->bInterval);
  1493. usb_submit_urb(mixer->urb, GFP_KERNEL);
  1494. return 0;
  1495. }
  1496. static void snd_usb_soundblaster_remote_complete(struct urb *urb,
  1497. struct pt_regs *regs)
  1498. {
  1499. struct usb_mixer_interface *mixer = urb->context;
  1500. /*
  1501. * format of remote control data:
  1502. * Extigy: xx 00
  1503. * Audigy 2 NX: 06 80 xx 00 00 00
  1504. */
  1505. int offset = mixer->rc_type == RC_EXTIGY ? 0 : 2;
  1506. u32 code;
  1507. if (urb->status < 0 || urb->actual_length <= offset)
  1508. return;
  1509. code = mixer->rc_buffer[offset];
  1510. /* the Mute button actually changes the mixer control */
  1511. if (code == 13)
  1512. snd_usb_mixer_notify_id(mixer, 18);
  1513. mixer->rc_code = code;
  1514. wmb();
  1515. wake_up(&mixer->rc_waitq);
  1516. }
  1517. static int snd_usb_sbrc_hwdep_open(snd_hwdep_t *hw, struct file *file)
  1518. {
  1519. struct usb_mixer_interface *mixer = hw->private_data;
  1520. if (test_and_set_bit(0, &mixer->rc_hwdep_open))
  1521. return -EBUSY;
  1522. return 0;
  1523. }
  1524. static int snd_usb_sbrc_hwdep_release(snd_hwdep_t *hw, struct file *file)
  1525. {
  1526. struct usb_mixer_interface *mixer = hw->private_data;
  1527. clear_bit(0, &mixer->rc_hwdep_open);
  1528. smp_mb__after_clear_bit();
  1529. return 0;
  1530. }
  1531. static long snd_usb_sbrc_hwdep_read(snd_hwdep_t *hw, char __user *buf,
  1532. long count, loff_t *offset)
  1533. {
  1534. struct usb_mixer_interface *mixer = hw->private_data;
  1535. int err;
  1536. u32 rc_code;
  1537. if (count != 1)
  1538. return -EINVAL;
  1539. err = wait_event_interruptible(mixer->rc_waitq,
  1540. (rc_code = xchg(&mixer->rc_code, 0)) != 0);
  1541. if (err == 0) {
  1542. err = put_user(rc_code, buf);
  1543. }
  1544. return err < 0 ? err : count;
  1545. }
  1546. static unsigned int snd_usb_sbrc_hwdep_poll(snd_hwdep_t *hw, struct file *file,
  1547. poll_table *wait)
  1548. {
  1549. struct usb_mixer_interface *mixer = hw->private_data;
  1550. poll_wait(file, &mixer->rc_waitq, wait);
  1551. return mixer->rc_code ? POLLIN | POLLRDNORM : 0;
  1552. }
  1553. static int snd_usb_soundblaster_remote_init(struct usb_mixer_interface *mixer)
  1554. {
  1555. snd_hwdep_t *hwdep;
  1556. int err, len;
  1557. switch (mixer->chip->usb_id) {
  1558. case USB_ID(0x041e, 0x3000):
  1559. mixer->rc_type = RC_EXTIGY;
  1560. len = 2;
  1561. break;
  1562. case USB_ID(0x041e, 0x3020):
  1563. mixer->rc_type = RC_AUDIGY2NX;
  1564. len = 6;
  1565. break;
  1566. default:
  1567. return 0;
  1568. }
  1569. init_waitqueue_head(&mixer->rc_waitq);
  1570. err = snd_hwdep_new(mixer->chip->card, "SB remote control", 0, &hwdep);
  1571. if (err < 0)
  1572. return err;
  1573. snprintf(hwdep->name, sizeof(hwdep->name),
  1574. "%s remote control", mixer->chip->card->shortname);
  1575. hwdep->iface = SNDRV_HWDEP_IFACE_SB_RC;
  1576. hwdep->private_data = mixer;
  1577. hwdep->ops.read = snd_usb_sbrc_hwdep_read;
  1578. hwdep->ops.open = snd_usb_sbrc_hwdep_open;
  1579. hwdep->ops.release = snd_usb_sbrc_hwdep_release;
  1580. hwdep->ops.poll = snd_usb_sbrc_hwdep_poll;
  1581. mixer->rc_urb = usb_alloc_urb(0, GFP_KERNEL);
  1582. if (!mixer->rc_urb)
  1583. return -ENOMEM;
  1584. mixer->rc_setup_packet = kmalloc(sizeof(*mixer->rc_setup_packet), GFP_KERNEL);
  1585. if (!mixer->rc_setup_packet) {
  1586. usb_free_urb(mixer->rc_urb);
  1587. mixer->rc_urb = NULL;
  1588. return -ENOMEM;
  1589. }
  1590. mixer->rc_setup_packet->bRequestType =
  1591. USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
  1592. mixer->rc_setup_packet->bRequest = GET_MEM;
  1593. mixer->rc_setup_packet->wValue = cpu_to_le16(0);
  1594. mixer->rc_setup_packet->wIndex = cpu_to_le16(0);
  1595. mixer->rc_setup_packet->wLength = cpu_to_le16(len);
  1596. usb_fill_control_urb(mixer->rc_urb, mixer->chip->dev,
  1597. usb_rcvctrlpipe(mixer->chip->dev, 0),
  1598. (u8*)mixer->rc_setup_packet, mixer->rc_buffer, len,
  1599. snd_usb_soundblaster_remote_complete, mixer);
  1600. return 0;
  1601. }
  1602. int snd_usb_create_mixer(snd_usb_audio_t *chip, int ctrlif)
  1603. {
  1604. static snd_device_ops_t dev_ops = {
  1605. .dev_free = snd_usb_mixer_dev_free
  1606. };
  1607. struct usb_mixer_interface *mixer;
  1608. int err;
  1609. strcpy(chip->card->mixername, "USB Mixer");
  1610. mixer = kcalloc(1, sizeof(*mixer), GFP_KERNEL);
  1611. if (!mixer)
  1612. return -ENOMEM;
  1613. mixer->chip = chip;
  1614. mixer->ctrlif = ctrlif;
  1615. #ifdef IGNORE_CTL_ERROR
  1616. mixer->ignore_ctl_error = 1;
  1617. #endif
  1618. mixer->id_elems = kcalloc(256, sizeof(*mixer->id_elems), GFP_KERNEL);
  1619. if (!mixer->id_elems) {
  1620. kfree(mixer);
  1621. return -ENOMEM;
  1622. }
  1623. if ((err = snd_usb_mixer_controls(mixer)) < 0 ||
  1624. (err = snd_usb_mixer_status_create(mixer)) < 0) {
  1625. snd_usb_mixer_free(mixer);
  1626. return err;
  1627. }
  1628. if ((err = snd_usb_soundblaster_remote_init(mixer)) < 0) {
  1629. snd_usb_mixer_free(mixer);
  1630. return err;
  1631. }
  1632. err = snd_device_new(chip->card, SNDRV_DEV_LOWLEVEL, mixer, &dev_ops);
  1633. if (err < 0) {
  1634. snd_usb_mixer_free(mixer);
  1635. return err;
  1636. }
  1637. list_add(&mixer->list, &chip->mixer_list);
  1638. return 0;
  1639. }
  1640. void snd_usb_mixer_disconnect(struct list_head *p)
  1641. {
  1642. struct usb_mixer_interface *mixer;
  1643. mixer = list_entry(p, struct usb_mixer_interface, list);
  1644. if (mixer->urb)
  1645. usb_kill_urb(mixer->urb);
  1646. if (mixer->rc_urb)
  1647. usb_kill_urb(mixer->rc_urb);
  1648. }