usbmixer.c 51 KB

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