mixer.c 67 KB

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