usbmixer.c 49 KB

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