mixer.c 71 KB

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