mixer.c 70 KB

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