usbmixer.c 53 KB

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