mixer.c 68 KB

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