mixer_scarlett.c 28 KB

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