usbmixer.c 62 KB

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