mixer_scarlett.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  1. /*
  2. * Scarlett Driver for ALSA
  3. *
  4. * Copyright (c) 2013 by Tobias Hoffmann
  5. * Copyright (c) 2013 by Robin Gareus <robin at gareus.org>
  6. * Copyright (c) 2002 by Takashi Iwai <tiwai at suse.de>
  7. * Copyright (c) 2014 by Chris J Arges <chris.j.arges at canonical.com>
  8. *
  9. * Many codes borrowed from audio.c by
  10. * Alan Cox (alan at lxorguk.ukuu.org.uk)
  11. * Thomas Sailer (sailer at ife.ee.ethz.ch)
  12. *
  13. * Code cleanup:
  14. * David Henningsson <david.henningsson at canonical.com>
  15. *
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License as published by
  18. * the Free Software Foundation; either version 2 of the License, or
  19. * (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. */
  27. /*
  28. * Rewritten and extended to support more models, e.g. Scarlett 18i8.
  29. *
  30. * Auto-detection via UAC2 is not feasible to properly discover the vast
  31. * majority of features. It's related to both Linux/ALSA's UAC2 as well as
  32. * Focusrite's implementation of it. Eventually quirks may be sufficient but
  33. * right now it's a major headache to work arount these things.
  34. *
  35. * NB. Neither the OSX nor the win driver provided by Focusrite performs
  36. * discovery, they seem to operate the same as this driver.
  37. */
  38. /* Mixer Interface for the Focusrite Scarlett 18i6 audio interface.
  39. *
  40. * The protocol was reverse engineered by looking at communication between
  41. * Scarlett MixControl (v 1.2.128.0) and the Focusrite(R) Scarlett 18i6
  42. * (firmware v305) using wireshark and usbmon in January 2013.
  43. * Extended in July 2013.
  44. *
  45. * this mixer gives complete access to all features of the device:
  46. * - change Impedance of inputs (Line-in, Mic / Instrument, Hi-Z)
  47. * - select clock source
  48. * - dynamic input to mixer-matrix assignment
  49. * - 18 x 6 mixer-matrix gain stages
  50. * - bus routing & volume control
  51. * - automatic re-initialization on connect if device was power-cycled
  52. *
  53. * USB URB commands overview (bRequest = 0x01 = UAC2_CS_CUR)
  54. * wIndex
  55. * 0x01 Analog Input line/instrument impedance switch, wValue=0x0901 +
  56. * channel, data=Line/Inst (2bytes)
  57. * pad (-10dB) switch, wValue=0x0b01 + channel, data=Off/On (2bytes)
  58. * ?? wValue=0x0803/04, ?? (2bytes)
  59. * 0x0a Master Volume, wValue=0x0200+bus[0:all + only 1..4?] data(2bytes)
  60. * Bus Mute/Unmute wValue=0x0100+bus[0:all + only 1..4?], data(2bytes)
  61. * 0x28 Clock source, wValue=0x0100, data={1:int,2:spdif,3:adat} (1byte)
  62. * 0x29 Set Sample-rate, wValue=0x0100, data=sample-rate(4bytes)
  63. * 0x32 Mixer mux, wValue=0x0600 + mixer-channel, data=input-to-connect(2bytes)
  64. * 0x33 Output mux, wValue=bus, data=input-to-connect(2bytes)
  65. * 0x34 Capture mux, wValue=0...18, data=input-to-connect(2bytes)
  66. * 0x3c Matrix Mixer gains, wValue=mixer-node data=gain(2bytes)
  67. * ?? [sometimes](4bytes, e.g 0x000003be 0x000003bf ...03ff)
  68. *
  69. * USB reads: (i.e. actually issued by original software)
  70. * 0x01 wValue=0x0901+channel (1byte!!), wValue=0x0b01+channed (1byte!!)
  71. * 0x29 wValue=0x0100 sample-rate(4bytes)
  72. * wValue=0x0200 ?? 1byte (only once)
  73. * 0x2a wValue=0x0100 ?? 4bytes, sample-rate2 ??
  74. *
  75. * USB reads with bRequest = 0x03 = UAC2_CS_MEM
  76. * 0x3c wValue=0x0002 1byte: sync status (locked=1)
  77. * wValue=0x0000 18*2byte: peak meter (inputs)
  78. * wValue=0x0001 8(?)*2byte: peak meter (mix)
  79. * wValue=0x0003 6*2byte: peak meter (pcm/daw)
  80. *
  81. * USB write with bRequest = 0x03
  82. * 0x3c Save settings to hardware: wValue=0x005a, data=0xa5
  83. *
  84. *
  85. * <ditaa>
  86. * /--------------\ 18chn 6chn /--------------\
  87. * | Hardware in +--+-------\ /------+--+ ALSA PCM out |
  88. * \--------------/ | | | | \--------------/
  89. * | | | |
  90. * | v v |
  91. * | +---------------+ |
  92. * | \ Matrix Mux / |
  93. * | +-----+-----+ |
  94. * | | |
  95. * | | 18chn |
  96. * | v |
  97. * | +-----------+ |
  98. * | | Mixer | |
  99. * | | Matrix | |
  100. * | | | |
  101. * | | 18x6 Gain | |
  102. * | | stages | |
  103. * | +-----+-----+ |
  104. * | | |
  105. * | | |
  106. * | 18chn | 6chn | 6chn
  107. * v v v
  108. * =========================
  109. * +---------------+ +--—------------+
  110. * \ Output Mux / \ Capture Mux /
  111. * +-----+-----+ +-----+-----+
  112. * | |
  113. * | 6chn |
  114. * v |
  115. * +-------------+ |
  116. * | Master Gain | |
  117. * +------+------+ |
  118. * | |
  119. * | 6chn | 18chn
  120. * | (3 stereo pairs) |
  121. * /--------------\ | | /--------------\
  122. * | Hardware out |<--/ \-->| ALSA PCM in |
  123. * \--------------/ \--------------/
  124. * </ditaa>
  125. *
  126. */
  127. #include <linux/slab.h>
  128. #include <linux/usb.h>
  129. #include <linux/usb/audio-v2.h>
  130. #include <sound/core.h>
  131. #include <sound/control.h>
  132. #include <sound/tlv.h>
  133. #include "usbaudio.h"
  134. #include "mixer.h"
  135. #include "helper.h"
  136. #include "power.h"
  137. #include "mixer_scarlett.h"
  138. /* some gui mixers can't handle negative ctl values */
  139. #define SND_SCARLETT_LEVEL_BIAS 128
  140. #define SND_SCARLETT_MATRIX_IN_MAX 18
  141. #define SND_SCARLETT_CONTROLS_MAX 10
  142. #define SND_SCARLETT_OFFSETS_MAX 5
  143. enum {
  144. SCARLETT_OUTPUTS,
  145. SCARLETT_SWITCH_IMPEDANCE,
  146. SCARLETT_SWITCH_PAD,
  147. };
  148. enum {
  149. SCARLETT_OFFSET_PCM = 0,
  150. SCARLETT_OFFSET_ANALOG = 1,
  151. SCARLETT_OFFSET_SPDIF = 2,
  152. SCARLETT_OFFSET_ADAT = 3,
  153. SCARLETT_OFFSET_MIX = 4,
  154. };
  155. struct scarlett_mixer_elem_enum_info {
  156. int start;
  157. int len;
  158. int offsets[SND_SCARLETT_OFFSETS_MAX];
  159. char const * const *names;
  160. };
  161. struct scarlett_mixer_control {
  162. unsigned char num;
  163. unsigned char type;
  164. const char *name;
  165. };
  166. struct scarlett_device_info {
  167. int matrix_in;
  168. int matrix_out;
  169. int input_len;
  170. int output_len;
  171. struct scarlett_mixer_elem_enum_info opt_master;
  172. struct scarlett_mixer_elem_enum_info opt_matrix;
  173. /* initial values for matrix mux */
  174. int matrix_mux_init[SND_SCARLETT_MATRIX_IN_MAX];
  175. int num_controls; /* number of items in controls */
  176. const struct scarlett_mixer_control controls[SND_SCARLETT_CONTROLS_MAX];
  177. };
  178. /********************** Enum Strings *************************/
  179. static const struct scarlett_mixer_elem_enum_info opt_pad = {
  180. .start = 0,
  181. .len = 2,
  182. .offsets = {},
  183. .names = (char const * const []){
  184. "0dB", "-10dB"
  185. }
  186. };
  187. static const struct scarlett_mixer_elem_enum_info opt_impedance = {
  188. .start = 0,
  189. .len = 2,
  190. .offsets = {},
  191. .names = (char const * const []){
  192. "Line", "Hi-Z"
  193. }
  194. };
  195. static const struct scarlett_mixer_elem_enum_info opt_clock = {
  196. .start = 1,
  197. .len = 3,
  198. .offsets = {},
  199. .names = (char const * const []){
  200. "Internal", "SPDIF", "ADAT"
  201. }
  202. };
  203. static const struct scarlett_mixer_elem_enum_info opt_sync = {
  204. .start = 0,
  205. .len = 2,
  206. .offsets = {},
  207. .names = (char const * const []){
  208. "No Lock", "Locked"
  209. }
  210. };
  211. static int scarlett_ctl_switch_info(struct snd_kcontrol *kctl,
  212. struct snd_ctl_elem_info *uinfo)
  213. {
  214. struct usb_mixer_elem_info *elem = kctl->private_data;
  215. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  216. uinfo->count = elem->channels;
  217. uinfo->value.integer.min = 0;
  218. uinfo->value.integer.max = 1;
  219. return 0;
  220. }
  221. static int scarlett_ctl_switch_get(struct snd_kcontrol *kctl,
  222. struct snd_ctl_elem_value *ucontrol)
  223. {
  224. struct usb_mixer_elem_info *elem = kctl->private_data;
  225. int i, err, val;
  226. for (i = 0; i < elem->channels; i++) {
  227. err = snd_usb_get_cur_mix_value(elem, i, i, &val);
  228. if (err < 0)
  229. return err;
  230. val = !val; /* invert mute logic for mixer */
  231. ucontrol->value.integer.value[i] = val;
  232. }
  233. return 0;
  234. }
  235. static int scarlett_ctl_switch_put(struct snd_kcontrol *kctl,
  236. struct snd_ctl_elem_value *ucontrol)
  237. {
  238. struct usb_mixer_elem_info *elem = kctl->private_data;
  239. int i, changed = 0;
  240. int err, oval, val;
  241. for (i = 0; i < elem->channels; i++) {
  242. err = snd_usb_get_cur_mix_value(elem, i, i, &oval);
  243. if (err < 0)
  244. return err;
  245. val = ucontrol->value.integer.value[i];
  246. val = !val;
  247. if (oval != val) {
  248. err = snd_usb_set_cur_mix_value(elem, i, i, val);
  249. if (err < 0)
  250. return err;
  251. changed = 1;
  252. }
  253. }
  254. return changed;
  255. }
  256. static int scarlett_ctl_resume(struct usb_mixer_elem_list *list)
  257. {
  258. struct usb_mixer_elem_info *elem = mixer_elem_list_to_info(list);
  259. int i;
  260. for (i = 0; i < elem->channels; i++)
  261. if (elem->cached & (1 << i))
  262. snd_usb_set_cur_mix_value(elem, i, i,
  263. elem->cache_val[i]);
  264. return 0;
  265. }
  266. static int scarlett_ctl_info(struct snd_kcontrol *kctl,
  267. struct snd_ctl_elem_info *uinfo)
  268. {
  269. struct usb_mixer_elem_info *elem = kctl->private_data;
  270. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  271. uinfo->count = elem->channels;
  272. uinfo->value.integer.min = 0;
  273. uinfo->value.integer.max = (int)kctl->private_value +
  274. SND_SCARLETT_LEVEL_BIAS;
  275. uinfo->value.integer.step = 1;
  276. return 0;
  277. }
  278. static int scarlett_ctl_get(struct snd_kcontrol *kctl,
  279. struct snd_ctl_elem_value *ucontrol)
  280. {
  281. struct usb_mixer_elem_info *elem = kctl->private_data;
  282. int i, err, val;
  283. for (i = 0; i < elem->channels; i++) {
  284. err = snd_usb_get_cur_mix_value(elem, i, i, &val);
  285. if (err < 0)
  286. return err;
  287. val = clamp(val / 256, -128, (int)kctl->private_value) +
  288. SND_SCARLETT_LEVEL_BIAS;
  289. ucontrol->value.integer.value[i] = val;
  290. }
  291. return 0;
  292. }
  293. static int scarlett_ctl_put(struct snd_kcontrol *kctl,
  294. struct snd_ctl_elem_value *ucontrol)
  295. {
  296. struct usb_mixer_elem_info *elem = kctl->private_data;
  297. int i, changed = 0;
  298. int err, oval, val;
  299. for (i = 0; i < elem->channels; i++) {
  300. err = snd_usb_get_cur_mix_value(elem, i, i, &oval);
  301. if (err < 0)
  302. return err;
  303. val = ucontrol->value.integer.value[i] -
  304. SND_SCARLETT_LEVEL_BIAS;
  305. val = val * 256;
  306. if (oval != val) {
  307. err = snd_usb_set_cur_mix_value(elem, i, i, val);
  308. if (err < 0)
  309. return err;
  310. changed = 1;
  311. }
  312. }
  313. return changed;
  314. }
  315. static void scarlett_generate_name(int i, char *dst, int offsets[])
  316. {
  317. if (i > offsets[SCARLETT_OFFSET_MIX])
  318. sprintf(dst, "Mix %c",
  319. 'A'+(i - offsets[SCARLETT_OFFSET_MIX] - 1));
  320. else if (i > offsets[SCARLETT_OFFSET_ADAT])
  321. sprintf(dst, "ADAT %d", i - offsets[SCARLETT_OFFSET_ADAT]);
  322. else if (i > offsets[SCARLETT_OFFSET_SPDIF])
  323. sprintf(dst, "SPDIF %d", i - offsets[SCARLETT_OFFSET_SPDIF]);
  324. else if (i > offsets[SCARLETT_OFFSET_ANALOG])
  325. sprintf(dst, "Analog %d", i - offsets[SCARLETT_OFFSET_ANALOG]);
  326. else if (i > offsets[SCARLETT_OFFSET_PCM])
  327. sprintf(dst, "PCM %d", i - offsets[SCARLETT_OFFSET_PCM]);
  328. else
  329. sprintf(dst, "Off");
  330. }
  331. static int scarlett_ctl_enum_dynamic_info(struct snd_kcontrol *kctl,
  332. struct snd_ctl_elem_info *uinfo)
  333. {
  334. struct usb_mixer_elem_info *elem = kctl->private_data;
  335. struct scarlett_mixer_elem_enum_info *opt = elem->private_data;
  336. unsigned int items = opt->len;
  337. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  338. uinfo->count = elem->channels;
  339. uinfo->value.enumerated.items = items;
  340. if (uinfo->value.enumerated.item >= items)
  341. uinfo->value.enumerated.item = items - 1;
  342. /* generate name dynamically based on item number and offset info */
  343. scarlett_generate_name(uinfo->value.enumerated.item,
  344. uinfo->value.enumerated.name,
  345. opt->offsets);
  346. return 0;
  347. }
  348. static int scarlett_ctl_enum_info(struct snd_kcontrol *kctl,
  349. struct snd_ctl_elem_info *uinfo)
  350. {
  351. struct usb_mixer_elem_info *elem = kctl->private_data;
  352. struct scarlett_mixer_elem_enum_info *opt = elem->private_data;
  353. return snd_ctl_enum_info(uinfo, elem->channels, opt->len,
  354. (const char * const *)opt->names);
  355. }
  356. static int scarlett_ctl_enum_get(struct snd_kcontrol *kctl,
  357. struct snd_ctl_elem_value *ucontrol)
  358. {
  359. struct usb_mixer_elem_info *elem = kctl->private_data;
  360. struct scarlett_mixer_elem_enum_info *opt = elem->private_data;
  361. int err, val;
  362. err = snd_usb_get_cur_mix_value(elem, 0, 0, &val);
  363. if (err < 0)
  364. return err;
  365. val = clamp(val - opt->start, 0, opt->len-1);
  366. ucontrol->value.enumerated.item[0] = val;
  367. return 0;
  368. }
  369. static int scarlett_ctl_enum_put(struct snd_kcontrol *kctl,
  370. struct snd_ctl_elem_value *ucontrol)
  371. {
  372. struct usb_mixer_elem_info *elem = kctl->private_data;
  373. struct scarlett_mixer_elem_enum_info *opt = elem->private_data;
  374. int err, oval, val;
  375. err = snd_usb_get_cur_mix_value(elem, 0, 0, &oval);
  376. if (err < 0)
  377. return err;
  378. val = ucontrol->value.integer.value[0];
  379. val = val + opt->start;
  380. if (val != oval) {
  381. snd_usb_set_cur_mix_value(elem, 0, 0, val);
  382. return 1;
  383. }
  384. return 0;
  385. }
  386. static int scarlett_ctl_enum_resume(struct usb_mixer_elem_list *list)
  387. {
  388. struct usb_mixer_elem_info *elem = mixer_elem_list_to_info(list);
  389. if (elem->cached)
  390. snd_usb_set_cur_mix_value(elem, 0, 0, *elem->cache_val);
  391. return 0;
  392. }
  393. static int scarlett_ctl_meter_get(struct snd_kcontrol *kctl,
  394. struct snd_ctl_elem_value *ucontrol)
  395. {
  396. struct usb_mixer_elem_info *elem = kctl->private_data;
  397. struct snd_usb_audio *chip = elem->head.mixer->chip;
  398. unsigned char buf[2 * MAX_CHANNELS] = {0, };
  399. int wValue = (elem->control << 8) | elem->idx_off;
  400. int idx = snd_usb_ctrl_intf(chip) | (elem->head.id << 8);
  401. int err;
  402. err = snd_usb_ctl_msg(chip->dev,
  403. usb_rcvctrlpipe(chip->dev, 0),
  404. UAC2_CS_MEM,
  405. USB_RECIP_INTERFACE | USB_TYPE_CLASS |
  406. USB_DIR_IN, wValue, idx, buf, elem->channels);
  407. if (err < 0)
  408. return err;
  409. ucontrol->value.enumerated.item[0] = clamp((int)buf[0], 0, 1);
  410. return 0;
  411. }
  412. static const struct snd_kcontrol_new usb_scarlett_ctl_switch = {
  413. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  414. .name = "",
  415. .info = scarlett_ctl_switch_info,
  416. .get = scarlett_ctl_switch_get,
  417. .put = scarlett_ctl_switch_put,
  418. };
  419. static const DECLARE_TLV_DB_SCALE(db_scale_scarlett_gain, -12800, 100, 0);
  420. static const struct snd_kcontrol_new usb_scarlett_ctl = {
  421. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  422. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
  423. SNDRV_CTL_ELEM_ACCESS_TLV_READ,
  424. .name = "",
  425. .info = scarlett_ctl_info,
  426. .get = scarlett_ctl_get,
  427. .put = scarlett_ctl_put,
  428. .private_value = 6, /* max value */
  429. .tlv = { .p = db_scale_scarlett_gain }
  430. };
  431. static const struct snd_kcontrol_new usb_scarlett_ctl_master = {
  432. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  433. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
  434. SNDRV_CTL_ELEM_ACCESS_TLV_READ,
  435. .name = "",
  436. .info = scarlett_ctl_info,
  437. .get = scarlett_ctl_get,
  438. .put = scarlett_ctl_put,
  439. .private_value = 6, /* max value */
  440. .tlv = { .p = db_scale_scarlett_gain }
  441. };
  442. static const struct snd_kcontrol_new usb_scarlett_ctl_enum = {
  443. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  444. .name = "",
  445. .info = scarlett_ctl_enum_info,
  446. .get = scarlett_ctl_enum_get,
  447. .put = scarlett_ctl_enum_put,
  448. };
  449. static const struct snd_kcontrol_new usb_scarlett_ctl_dynamic_enum = {
  450. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  451. .name = "",
  452. .info = scarlett_ctl_enum_dynamic_info,
  453. .get = scarlett_ctl_enum_get,
  454. .put = scarlett_ctl_enum_put,
  455. };
  456. static const struct snd_kcontrol_new usb_scarlett_ctl_sync = {
  457. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  458. .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
  459. .name = "",
  460. .info = scarlett_ctl_enum_info,
  461. .get = scarlett_ctl_meter_get,
  462. };
  463. static int add_new_ctl(struct usb_mixer_interface *mixer,
  464. const struct snd_kcontrol_new *ncontrol,
  465. usb_mixer_elem_resume_func_t resume,
  466. int index, int offset, int num,
  467. int val_type, int channels, const char *name,
  468. const struct scarlett_mixer_elem_enum_info *opt,
  469. struct usb_mixer_elem_info **elem_ret
  470. )
  471. {
  472. struct snd_kcontrol *kctl;
  473. struct usb_mixer_elem_info *elem;
  474. int err;
  475. elem = kzalloc(sizeof(*elem), GFP_KERNEL);
  476. if (!elem)
  477. return -ENOMEM;
  478. elem->head.mixer = mixer;
  479. elem->head.resume = resume;
  480. elem->control = offset;
  481. elem->idx_off = num;
  482. elem->head.id = index;
  483. elem->val_type = val_type;
  484. elem->channels = channels;
  485. /* add scarlett_mixer_elem_enum_info struct */
  486. elem->private_data = (void *)opt;
  487. kctl = snd_ctl_new1(ncontrol, elem);
  488. if (!kctl) {
  489. kfree(elem);
  490. return -ENOMEM;
  491. }
  492. kctl->private_free = snd_usb_mixer_elem_free;
  493. strlcpy(kctl->id.name, name, sizeof(kctl->id.name));
  494. err = snd_usb_mixer_add_control(&elem->head, kctl);
  495. if (err < 0)
  496. return err;
  497. if (elem_ret)
  498. *elem_ret = elem;
  499. return 0;
  500. }
  501. static int add_output_ctls(struct usb_mixer_interface *mixer,
  502. int index, const char *name,
  503. const struct scarlett_device_info *info)
  504. {
  505. int err;
  506. char mx[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
  507. struct usb_mixer_elem_info *elem;
  508. /* Add mute switch */
  509. snprintf(mx, sizeof(mx), "Master %d (%s) Playback Switch",
  510. index + 1, name);
  511. err = add_new_ctl(mixer, &usb_scarlett_ctl_switch,
  512. scarlett_ctl_resume, 0x0a, 0x01,
  513. 2*index+1, USB_MIXER_S16, 2, mx, NULL, &elem);
  514. if (err < 0)
  515. return err;
  516. /* Add volume control and initialize to 0 */
  517. snprintf(mx, sizeof(mx), "Master %d (%s) Playback Volume",
  518. index + 1, name);
  519. err = add_new_ctl(mixer, &usb_scarlett_ctl_master,
  520. scarlett_ctl_resume, 0x0a, 0x02,
  521. 2*index+1, USB_MIXER_S16, 2, mx, NULL, &elem);
  522. if (err < 0)
  523. return err;
  524. /* Add L channel source playback enumeration */
  525. snprintf(mx, sizeof(mx), "Master %dL (%s) Source Playback Enum",
  526. index + 1, name);
  527. err = add_new_ctl(mixer, &usb_scarlett_ctl_dynamic_enum,
  528. scarlett_ctl_enum_resume, 0x33, 0x00,
  529. 2*index, USB_MIXER_S16, 1, mx, &info->opt_master,
  530. &elem);
  531. if (err < 0)
  532. return err;
  533. /* Add R channel source playback enumeration */
  534. snprintf(mx, sizeof(mx), "Master %dR (%s) Source Playback Enum",
  535. index + 1, name);
  536. err = add_new_ctl(mixer, &usb_scarlett_ctl_dynamic_enum,
  537. scarlett_ctl_enum_resume, 0x33, 0x00,
  538. 2*index+1, USB_MIXER_S16, 1, mx, &info->opt_master,
  539. &elem);
  540. if (err < 0)
  541. return err;
  542. return 0;
  543. }
  544. /********************** device-specific config *************************/
  545. /* untested... */
  546. static struct scarlett_device_info s6i6_info = {
  547. .matrix_in = 18,
  548. .matrix_out = 8,
  549. .input_len = 6,
  550. .output_len = 6,
  551. .opt_master = {
  552. .start = -1,
  553. .len = 27,
  554. .offsets = {0, 12, 16, 18, 18},
  555. .names = NULL
  556. },
  557. .opt_matrix = {
  558. .start = -1,
  559. .len = 19,
  560. .offsets = {0, 12, 16, 18, 18},
  561. .names = NULL
  562. },
  563. .num_controls = 9,
  564. .controls = {
  565. { .num = 0, .type = SCARLETT_OUTPUTS, .name = "Monitor" },
  566. { .num = 1, .type = SCARLETT_OUTPUTS, .name = "Headphone" },
  567. { .num = 2, .type = SCARLETT_OUTPUTS, .name = "SPDIF" },
  568. { .num = 1, .type = SCARLETT_SWITCH_IMPEDANCE, .name = NULL},
  569. { .num = 1, .type = SCARLETT_SWITCH_PAD, .name = NULL},
  570. { .num = 2, .type = SCARLETT_SWITCH_IMPEDANCE, .name = NULL},
  571. { .num = 2, .type = SCARLETT_SWITCH_PAD, .name = NULL},
  572. { .num = 3, .type = SCARLETT_SWITCH_PAD, .name = NULL},
  573. { .num = 4, .type = SCARLETT_SWITCH_PAD, .name = NULL},
  574. },
  575. .matrix_mux_init = {
  576. 12, 13, 14, 15, /* Analog -> 1..4 */
  577. 16, 17, /* SPDIF -> 5,6 */
  578. 0, 1, 2, 3, 4, 5, 6, 7, /* PCM[1..12] -> 7..18 */
  579. 8, 9, 10, 11
  580. }
  581. };
  582. /* untested... */
  583. static struct scarlett_device_info s8i6_info = {
  584. .matrix_in = 18,
  585. .matrix_out = 6,
  586. .input_len = 8,
  587. .output_len = 6,
  588. .opt_master = {
  589. .start = -1,
  590. .len = 25,
  591. .offsets = {0, 12, 16, 18, 18},
  592. .names = NULL
  593. },
  594. .opt_matrix = {
  595. .start = -1,
  596. .len = 19,
  597. .offsets = {0, 12, 16, 18, 18},
  598. .names = NULL
  599. },
  600. .num_controls = 7,
  601. .controls = {
  602. { .num = 0, .type = SCARLETT_OUTPUTS, .name = "Monitor" },
  603. { .num = 1, .type = SCARLETT_OUTPUTS, .name = "Headphone" },
  604. { .num = 2, .type = SCARLETT_OUTPUTS, .name = "SPDIF" },
  605. { .num = 1, .type = SCARLETT_SWITCH_IMPEDANCE, .name = NULL},
  606. { .num = 2, .type = SCARLETT_SWITCH_IMPEDANCE, .name = NULL},
  607. { .num = 3, .type = SCARLETT_SWITCH_PAD, .name = NULL},
  608. { .num = 4, .type = SCARLETT_SWITCH_PAD, .name = NULL},
  609. },
  610. .matrix_mux_init = {
  611. 12, 13, 14, 15, /* Analog -> 1..4 */
  612. 16, 17, /* SPDIF -> 5,6 */
  613. 0, 1, 2, 3, 4, 5, 6, 7, /* PCM[1..12] -> 7..18 */
  614. 8, 9, 10, 11
  615. }
  616. };
  617. static struct scarlett_device_info s18i6_info = {
  618. .matrix_in = 18,
  619. .matrix_out = 6,
  620. .input_len = 18,
  621. .output_len = 6,
  622. .opt_master = {
  623. .start = -1,
  624. .len = 31,
  625. .offsets = {0, 6, 14, 16, 24},
  626. .names = NULL,
  627. },
  628. .opt_matrix = {
  629. .start = -1,
  630. .len = 25,
  631. .offsets = {0, 6, 14, 16, 24},
  632. .names = NULL,
  633. },
  634. .num_controls = 5,
  635. .controls = {
  636. { .num = 0, .type = SCARLETT_OUTPUTS, .name = "Monitor" },
  637. { .num = 1, .type = SCARLETT_OUTPUTS, .name = "Headphone" },
  638. { .num = 2, .type = SCARLETT_OUTPUTS, .name = "SPDIF" },
  639. { .num = 1, .type = SCARLETT_SWITCH_IMPEDANCE, .name = NULL},
  640. { .num = 2, .type = SCARLETT_SWITCH_IMPEDANCE, .name = NULL},
  641. },
  642. .matrix_mux_init = {
  643. 6, 7, 8, 9, 10, 11, 12, 13, /* Analog -> 1..8 */
  644. 16, 17, 18, 19, 20, 21, /* ADAT[1..6] -> 9..14 */
  645. 14, 15, /* SPDIF -> 15,16 */
  646. 0, 1 /* PCM[1,2] -> 17,18 */
  647. }
  648. };
  649. static struct scarlett_device_info s18i8_info = {
  650. .matrix_in = 18,
  651. .matrix_out = 8,
  652. .input_len = 18,
  653. .output_len = 8,
  654. .opt_master = {
  655. .start = -1,
  656. .len = 35,
  657. .offsets = {0, 8, 16, 18, 26},
  658. .names = NULL
  659. },
  660. .opt_matrix = {
  661. .start = -1,
  662. .len = 27,
  663. .offsets = {0, 8, 16, 18, 26},
  664. .names = NULL
  665. },
  666. .num_controls = 10,
  667. .controls = {
  668. { .num = 0, .type = SCARLETT_OUTPUTS, .name = "Monitor" },
  669. { .num = 1, .type = SCARLETT_OUTPUTS, .name = "Headphone 1" },
  670. { .num = 2, .type = SCARLETT_OUTPUTS, .name = "Headphone 2" },
  671. { .num = 3, .type = SCARLETT_OUTPUTS, .name = "SPDIF" },
  672. { .num = 1, .type = SCARLETT_SWITCH_IMPEDANCE, .name = NULL},
  673. { .num = 1, .type = SCARLETT_SWITCH_PAD, .name = NULL},
  674. { .num = 2, .type = SCARLETT_SWITCH_IMPEDANCE, .name = NULL},
  675. { .num = 2, .type = SCARLETT_SWITCH_PAD, .name = NULL},
  676. { .num = 3, .type = SCARLETT_SWITCH_PAD, .name = NULL},
  677. { .num = 4, .type = SCARLETT_SWITCH_PAD, .name = NULL},
  678. },
  679. .matrix_mux_init = {
  680. 8, 9, 10, 11, 12, 13, 14, 15, /* Analog -> 1..8 */
  681. 18, 19, 20, 21, 22, 23, /* ADAT[1..6] -> 9..14 */
  682. 16, 17, /* SPDIF -> 15,16 */
  683. 0, 1 /* PCM[1,2] -> 17,18 */
  684. }
  685. };
  686. static struct scarlett_device_info s18i20_info = {
  687. .matrix_in = 18,
  688. .matrix_out = 8,
  689. .input_len = 18,
  690. .output_len = 20,
  691. .opt_master = {
  692. .start = -1,
  693. .len = 47,
  694. .offsets = {0, 20, 28, 30, 38},
  695. .names = NULL
  696. },
  697. .opt_matrix = {
  698. .start = -1,
  699. .len = 39,
  700. .offsets = {0, 20, 28, 30, 38},
  701. .names = NULL
  702. },
  703. .num_controls = 10,
  704. .controls = {
  705. { .num = 0, .type = SCARLETT_OUTPUTS, .name = "Monitor" },
  706. { .num = 1, .type = SCARLETT_OUTPUTS, .name = "Line 3/4" },
  707. { .num = 2, .type = SCARLETT_OUTPUTS, .name = "Line 5/6" },
  708. { .num = 3, .type = SCARLETT_OUTPUTS, .name = "Line 7/8" },
  709. { .num = 4, .type = SCARLETT_OUTPUTS, .name = "Line 9/10" },
  710. { .num = 5, .type = SCARLETT_OUTPUTS, .name = "SPDIF" },
  711. { .num = 6, .type = SCARLETT_OUTPUTS, .name = "ADAT 1/2" },
  712. { .num = 7, .type = SCARLETT_OUTPUTS, .name = "ADAT 3/4" },
  713. { .num = 8, .type = SCARLETT_OUTPUTS, .name = "ADAT 5/6" },
  714. { .num = 9, .type = SCARLETT_OUTPUTS, .name = "ADAT 7/8" },
  715. /*{ .num = 1, .type = SCARLETT_SWITCH_IMPEDANCE, .name = NULL},
  716. { .num = 1, .type = SCARLETT_SWITCH_PAD, .name = NULL},
  717. { .num = 2, .type = SCARLETT_SWITCH_IMPEDANCE, .name = NULL},
  718. { .num = 2, .type = SCARLETT_SWITCH_PAD, .name = NULL},
  719. { .num = 3, .type = SCARLETT_SWITCH_PAD, .name = NULL},
  720. { .num = 4, .type = SCARLETT_SWITCH_PAD, .name = NULL},*/
  721. },
  722. .matrix_mux_init = {
  723. 20, 21, 22, 23, 24, 25, 26, 27, /* Analog -> 1..8 */
  724. 30, 31, 32, 33, 34, 35, /* ADAT[1..6] -> 9..14 */
  725. 28, 29, /* SPDIF -> 15,16 */
  726. 0, 1 /* PCM[1,2] -> 17,18 */
  727. }
  728. };
  729. static int scarlett_controls_create_generic(struct usb_mixer_interface *mixer,
  730. struct scarlett_device_info *info)
  731. {
  732. int i, err;
  733. char mx[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
  734. const struct scarlett_mixer_control *ctl;
  735. struct usb_mixer_elem_info *elem;
  736. /* create master switch and playback volume */
  737. err = add_new_ctl(mixer, &usb_scarlett_ctl_switch,
  738. scarlett_ctl_resume, 0x0a, 0x01, 0,
  739. USB_MIXER_S16, 1, "Master Playback Switch", NULL,
  740. &elem);
  741. if (err < 0)
  742. return err;
  743. err = add_new_ctl(mixer, &usb_scarlett_ctl_master,
  744. scarlett_ctl_resume, 0x0a, 0x02, 0,
  745. USB_MIXER_S16, 1, "Master Playback Volume", NULL,
  746. &elem);
  747. if (err < 0)
  748. return err;
  749. /* iterate through controls in info struct and create each one */
  750. for (i = 0; i < info->num_controls; i++) {
  751. ctl = &info->controls[i];
  752. switch (ctl->type) {
  753. case SCARLETT_OUTPUTS:
  754. err = add_output_ctls(mixer, ctl->num, ctl->name, info);
  755. if (err < 0)
  756. return err;
  757. break;
  758. case SCARLETT_SWITCH_IMPEDANCE:
  759. sprintf(mx, "Input %d Impedance Switch", ctl->num);
  760. err = add_new_ctl(mixer, &usb_scarlett_ctl_enum,
  761. scarlett_ctl_enum_resume, 0x01,
  762. 0x09, ctl->num, USB_MIXER_S16, 1, mx,
  763. &opt_impedance, &elem);
  764. if (err < 0)
  765. return err;
  766. break;
  767. case SCARLETT_SWITCH_PAD:
  768. sprintf(mx, "Input %d Pad Switch", ctl->num);
  769. err = add_new_ctl(mixer, &usb_scarlett_ctl_enum,
  770. scarlett_ctl_enum_resume, 0x01,
  771. 0x0b, ctl->num, USB_MIXER_S16, 1, mx,
  772. &opt_pad, &elem);
  773. if (err < 0)
  774. return err;
  775. break;
  776. }
  777. }
  778. return 0;
  779. }
  780. /*
  781. * Create and initialize a mixer for the Focusrite(R) Scarlett
  782. */
  783. int snd_scarlett_controls_create(struct usb_mixer_interface *mixer)
  784. {
  785. int err, i, o;
  786. char mx[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
  787. struct scarlett_device_info *info;
  788. struct usb_mixer_elem_info *elem;
  789. static char sample_rate_buffer[4] = { '\x80', '\xbb', '\x00', '\x00' };
  790. /* only use UAC_VERSION_2 */
  791. if (!mixer->protocol)
  792. return 0;
  793. switch (mixer->chip->usb_id) {
  794. case USB_ID(0x1235, 0x8012):
  795. info = &s6i6_info;
  796. break;
  797. case USB_ID(0x1235, 0x8002):
  798. info = &s8i6_info;
  799. break;
  800. case USB_ID(0x1235, 0x8004):
  801. info = &s18i6_info;
  802. break;
  803. case USB_ID(0x1235, 0x8014):
  804. info = &s18i8_info;
  805. break;
  806. case USB_ID(0x1235, 0x800c):
  807. info = &s18i20_info;
  808. break;
  809. default: /* device not (yet) supported */
  810. return -EINVAL;
  811. }
  812. /* generic function to create controls */
  813. err = scarlett_controls_create_generic(mixer, info);
  814. if (err < 0)
  815. return err;
  816. /* setup matrix controls */
  817. for (i = 0; i < info->matrix_in; i++) {
  818. snprintf(mx, sizeof(mx), "Matrix %02d Input Playback Route",
  819. i+1);
  820. err = add_new_ctl(mixer, &usb_scarlett_ctl_dynamic_enum,
  821. scarlett_ctl_enum_resume, 0x32,
  822. 0x06, i, USB_MIXER_S16, 1, mx,
  823. &info->opt_matrix, &elem);
  824. if (err < 0)
  825. return err;
  826. for (o = 0; o < info->matrix_out; o++) {
  827. sprintf(mx, "Matrix %02d Mix %c Playback Volume", i+1,
  828. o+'A');
  829. err = add_new_ctl(mixer, &usb_scarlett_ctl,
  830. scarlett_ctl_resume, 0x3c, 0x00,
  831. (i << 3) + (o & 0x07), USB_MIXER_S16,
  832. 1, mx, NULL, &elem);
  833. if (err < 0)
  834. return err;
  835. }
  836. }
  837. for (i = 0; i < info->input_len; i++) {
  838. snprintf(mx, sizeof(mx), "Input Source %02d Capture Route",
  839. i+1);
  840. err = add_new_ctl(mixer, &usb_scarlett_ctl_dynamic_enum,
  841. scarlett_ctl_enum_resume, 0x34,
  842. 0x00, i, USB_MIXER_S16, 1, mx,
  843. &info->opt_master, &elem);
  844. if (err < 0)
  845. return err;
  846. }
  847. /* val_len == 1 needed here */
  848. err = add_new_ctl(mixer, &usb_scarlett_ctl_enum,
  849. scarlett_ctl_enum_resume, 0x28, 0x01, 0,
  850. USB_MIXER_U8, 1, "Sample Clock Source",
  851. &opt_clock, &elem);
  852. if (err < 0)
  853. return err;
  854. /* val_len == 1 and UAC2_CS_MEM */
  855. err = add_new_ctl(mixer, &usb_scarlett_ctl_sync, NULL, 0x3c, 0x00, 2,
  856. USB_MIXER_U8, 1, "Sample Clock Sync Status",
  857. &opt_sync, &elem);
  858. if (err < 0)
  859. return err;
  860. /* initialize sampling rate to 48000 */
  861. err = snd_usb_ctl_msg(mixer->chip->dev,
  862. usb_sndctrlpipe(mixer->chip->dev, 0), UAC2_CS_CUR,
  863. USB_RECIP_INTERFACE | USB_TYPE_CLASS |
  864. USB_DIR_OUT, 0x0100, snd_usb_ctrl_intf(mixer->chip) |
  865. (0x29 << 8), sample_rate_buffer, 4);
  866. if (err < 0)
  867. return err;
  868. return err;
  869. }