usbmixer.c 51 KB

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