mixer.c 58 KB

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