usbmixer.c 61 KB

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