usbmixer.c 61 KB

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