usbmixer.c 53 KB

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