mixer.c 58 KB

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