mixer.c 70 KB

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