mixer.c 61 KB

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