mixer.c 50 KB

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