mixer.c 61 KB

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