usbmixer.c 62 KB

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