usbmixer.c 54 KB

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