mixer.c 67 KB

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