mixer.c 59 KB

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