mixer_scarlett.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  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 =
  259. container_of(list, struct usb_mixer_elem_info, head);
  260. int i;
  261. for (i = 0; i < elem->channels; i++)
  262. if (elem->cached & (1 << i))
  263. snd_usb_set_cur_mix_value(elem, i, i,
  264. elem->cache_val[i]);
  265. return 0;
  266. }
  267. static int scarlett_ctl_info(struct snd_kcontrol *kctl,
  268. struct snd_ctl_elem_info *uinfo)
  269. {
  270. struct usb_mixer_elem_info *elem = kctl->private_data;
  271. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  272. uinfo->count = elem->channels;
  273. uinfo->value.integer.min = 0;
  274. uinfo->value.integer.max = (int)kctl->private_value +
  275. SND_SCARLETT_LEVEL_BIAS;
  276. uinfo->value.integer.step = 1;
  277. return 0;
  278. }
  279. static int scarlett_ctl_get(struct snd_kcontrol *kctl,
  280. struct snd_ctl_elem_value *ucontrol)
  281. {
  282. struct usb_mixer_elem_info *elem = kctl->private_data;
  283. int i, err, val;
  284. for (i = 0; i < elem->channels; i++) {
  285. err = snd_usb_get_cur_mix_value(elem, i, i, &val);
  286. if (err < 0)
  287. return err;
  288. val = clamp(val / 256, -128, (int)kctl->private_value) +
  289. SND_SCARLETT_LEVEL_BIAS;
  290. ucontrol->value.integer.value[i] = val;
  291. }
  292. return 0;
  293. }
  294. static int scarlett_ctl_put(struct snd_kcontrol *kctl,
  295. struct snd_ctl_elem_value *ucontrol)
  296. {
  297. struct usb_mixer_elem_info *elem = kctl->private_data;
  298. int i, changed = 0;
  299. int err, oval, val;
  300. for (i = 0; i < elem->channels; i++) {
  301. err = snd_usb_get_cur_mix_value(elem, i, i, &oval);
  302. if (err < 0)
  303. return err;
  304. val = ucontrol->value.integer.value[i] -
  305. SND_SCARLETT_LEVEL_BIAS;
  306. val = val * 256;
  307. if (oval != val) {
  308. err = snd_usb_set_cur_mix_value(elem, i, i, val);
  309. if (err < 0)
  310. return err;
  311. changed = 1;
  312. }
  313. }
  314. return changed;
  315. }
  316. static void scarlett_generate_name(int i, char *dst, int offsets[])
  317. {
  318. if (i > offsets[SCARLETT_OFFSET_MIX])
  319. sprintf(dst, "Mix %c",
  320. 'A'+(i - offsets[SCARLETT_OFFSET_MIX] - 1));
  321. else if (i > offsets[SCARLETT_OFFSET_ADAT])
  322. sprintf(dst, "ADAT %d", i - offsets[SCARLETT_OFFSET_ADAT]);
  323. else if (i > offsets[SCARLETT_OFFSET_SPDIF])
  324. sprintf(dst, "SPDIF %d", i - offsets[SCARLETT_OFFSET_SPDIF]);
  325. else if (i > offsets[SCARLETT_OFFSET_ANALOG])
  326. sprintf(dst, "Analog %d", i - offsets[SCARLETT_OFFSET_ANALOG]);
  327. else if (i > offsets[SCARLETT_OFFSET_PCM])
  328. sprintf(dst, "PCM %d", i - offsets[SCARLETT_OFFSET_PCM]);
  329. else
  330. sprintf(dst, "Off");
  331. }
  332. static int scarlett_ctl_enum_dynamic_info(struct snd_kcontrol *kctl,
  333. struct snd_ctl_elem_info *uinfo)
  334. {
  335. struct usb_mixer_elem_info *elem = kctl->private_data;
  336. struct scarlett_mixer_elem_enum_info *opt = elem->private_data;
  337. unsigned int items = opt->len;
  338. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  339. uinfo->count = elem->channels;
  340. uinfo->value.enumerated.items = items;
  341. if (uinfo->value.enumerated.item >= items)
  342. uinfo->value.enumerated.item = items - 1;
  343. /* generate name dynamically based on item number and offset info */
  344. scarlett_generate_name(uinfo->value.enumerated.item,
  345. uinfo->value.enumerated.name,
  346. opt->offsets);
  347. return 0;
  348. }
  349. static int scarlett_ctl_enum_info(struct snd_kcontrol *kctl,
  350. struct snd_ctl_elem_info *uinfo)
  351. {
  352. struct usb_mixer_elem_info *elem = kctl->private_data;
  353. struct scarlett_mixer_elem_enum_info *opt = elem->private_data;
  354. return snd_ctl_enum_info(uinfo, elem->channels, opt->len,
  355. (const char * const *)opt->names);
  356. }
  357. static int scarlett_ctl_enum_get(struct snd_kcontrol *kctl,
  358. struct snd_ctl_elem_value *ucontrol)
  359. {
  360. struct usb_mixer_elem_info *elem = kctl->private_data;
  361. struct scarlett_mixer_elem_enum_info *opt = elem->private_data;
  362. int err, val;
  363. err = snd_usb_get_cur_mix_value(elem, 0, 0, &val);
  364. if (err < 0)
  365. return err;
  366. val = clamp(val - opt->start, 0, opt->len-1);
  367. ucontrol->value.enumerated.item[0] = val;
  368. return 0;
  369. }
  370. static int scarlett_ctl_enum_put(struct snd_kcontrol *kctl,
  371. struct snd_ctl_elem_value *ucontrol)
  372. {
  373. struct usb_mixer_elem_info *elem = kctl->private_data;
  374. struct scarlett_mixer_elem_enum_info *opt = elem->private_data;
  375. int err, oval, val;
  376. err = snd_usb_get_cur_mix_value(elem, 0, 0, &oval);
  377. if (err < 0)
  378. return err;
  379. val = ucontrol->value.integer.value[0];
  380. val = val + opt->start;
  381. if (val != oval) {
  382. snd_usb_set_cur_mix_value(elem, 0, 0, val);
  383. return 1;
  384. }
  385. return 0;
  386. }
  387. static int scarlett_ctl_enum_resume(struct usb_mixer_elem_list *list)
  388. {
  389. struct usb_mixer_elem_info *elem =
  390. container_of(list, struct usb_mixer_elem_info, head);
  391. if (elem->cached)
  392. snd_usb_set_cur_mix_value(elem, 0, 0, *elem->cache_val);
  393. return 0;
  394. }
  395. static int scarlett_ctl_meter_get(struct snd_kcontrol *kctl,
  396. struct snd_ctl_elem_value *ucontrol)
  397. {
  398. struct usb_mixer_elem_info *elem = kctl->private_data;
  399. struct snd_usb_audio *chip = elem->head.mixer->chip;
  400. unsigned char buf[2 * MAX_CHANNELS] = {0, };
  401. int wValue = (elem->control << 8) | elem->idx_off;
  402. int idx = snd_usb_ctrl_intf(chip) | (elem->head.id << 8);
  403. int err;
  404. err = snd_usb_ctl_msg(chip->dev,
  405. usb_rcvctrlpipe(chip->dev, 0),
  406. UAC2_CS_MEM,
  407. USB_RECIP_INTERFACE | USB_TYPE_CLASS |
  408. USB_DIR_IN, wValue, idx, buf, elem->channels);
  409. if (err < 0)
  410. return err;
  411. ucontrol->value.enumerated.item[0] = clamp((int)buf[0], 0, 1);
  412. return 0;
  413. }
  414. static struct snd_kcontrol_new usb_scarlett_ctl_switch = {
  415. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  416. .name = "",
  417. .info = scarlett_ctl_switch_info,
  418. .get = scarlett_ctl_switch_get,
  419. .put = scarlett_ctl_switch_put,
  420. };
  421. static const DECLARE_TLV_DB_SCALE(db_scale_scarlett_gain, -12800, 100, 0);
  422. static struct snd_kcontrol_new usb_scarlett_ctl = {
  423. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  424. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
  425. SNDRV_CTL_ELEM_ACCESS_TLV_READ,
  426. .name = "",
  427. .info = scarlett_ctl_info,
  428. .get = scarlett_ctl_get,
  429. .put = scarlett_ctl_put,
  430. .private_value = 6, /* max value */
  431. .tlv = { .p = db_scale_scarlett_gain }
  432. };
  433. static struct snd_kcontrol_new usb_scarlett_ctl_master = {
  434. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  435. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
  436. SNDRV_CTL_ELEM_ACCESS_TLV_READ,
  437. .name = "",
  438. .info = scarlett_ctl_info,
  439. .get = scarlett_ctl_get,
  440. .put = scarlett_ctl_put,
  441. .private_value = 6, /* max value */
  442. .tlv = { .p = db_scale_scarlett_gain }
  443. };
  444. static struct snd_kcontrol_new usb_scarlett_ctl_enum = {
  445. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  446. .name = "",
  447. .info = scarlett_ctl_enum_info,
  448. .get = scarlett_ctl_enum_get,
  449. .put = scarlett_ctl_enum_put,
  450. };
  451. static struct snd_kcontrol_new usb_scarlett_ctl_dynamic_enum = {
  452. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  453. .name = "",
  454. .info = scarlett_ctl_enum_dynamic_info,
  455. .get = scarlett_ctl_enum_get,
  456. .put = scarlett_ctl_enum_put,
  457. };
  458. static struct snd_kcontrol_new usb_scarlett_ctl_sync = {
  459. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  460. .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
  461. .name = "",
  462. .info = scarlett_ctl_enum_info,
  463. .get = scarlett_ctl_meter_get,
  464. };
  465. static int add_new_ctl(struct usb_mixer_interface *mixer,
  466. const struct snd_kcontrol_new *ncontrol,
  467. usb_mixer_elem_resume_func_t resume,
  468. int index, int offset, int num,
  469. int val_type, int channels, const char *name,
  470. const struct scarlett_mixer_elem_enum_info *opt,
  471. struct usb_mixer_elem_info **elem_ret
  472. )
  473. {
  474. struct snd_kcontrol *kctl;
  475. struct usb_mixer_elem_info *elem;
  476. int err;
  477. elem = kzalloc(sizeof(*elem), GFP_KERNEL);
  478. if (!elem)
  479. return -ENOMEM;
  480. elem->head.mixer = mixer;
  481. elem->head.resume = resume;
  482. elem->control = offset;
  483. elem->idx_off = num;
  484. elem->head.id = index;
  485. elem->val_type = val_type;
  486. elem->channels = channels;
  487. /* add scarlett_mixer_elem_enum_info struct */
  488. elem->private_data = (void *)opt;
  489. kctl = snd_ctl_new1(ncontrol, elem);
  490. if (!kctl) {
  491. kfree(elem);
  492. return -ENOMEM;
  493. }
  494. kctl->private_free = snd_usb_mixer_elem_free;
  495. strlcpy(kctl->id.name, name, sizeof(kctl->id.name));
  496. err = snd_usb_mixer_add_control(&elem->head, kctl);
  497. if (err < 0)
  498. return err;
  499. if (elem_ret)
  500. *elem_ret = elem;
  501. return 0;
  502. }
  503. static int add_output_ctls(struct usb_mixer_interface *mixer,
  504. int index, const char *name,
  505. const struct scarlett_device_info *info)
  506. {
  507. int err;
  508. char mx[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
  509. struct usb_mixer_elem_info *elem;
  510. /* Add mute switch */
  511. snprintf(mx, sizeof(mx), "Master %d (%s) Playback Switch",
  512. index + 1, name);
  513. err = add_new_ctl(mixer, &usb_scarlett_ctl_switch,
  514. scarlett_ctl_resume, 0x0a, 0x01,
  515. 2*index+1, USB_MIXER_S16, 2, mx, NULL, &elem);
  516. if (err < 0)
  517. return err;
  518. /* Add volume control and initialize to 0 */
  519. snprintf(mx, sizeof(mx), "Master %d (%s) Playback Volume",
  520. index + 1, name);
  521. err = add_new_ctl(mixer, &usb_scarlett_ctl_master,
  522. scarlett_ctl_resume, 0x0a, 0x02,
  523. 2*index+1, USB_MIXER_S16, 2, mx, NULL, &elem);
  524. if (err < 0)
  525. return err;
  526. /* Add L channel source playback enumeration */
  527. snprintf(mx, sizeof(mx), "Master %dL (%s) Source Playback Enum",
  528. index + 1, name);
  529. err = add_new_ctl(mixer, &usb_scarlett_ctl_dynamic_enum,
  530. scarlett_ctl_enum_resume, 0x33, 0x00,
  531. 2*index, USB_MIXER_S16, 1, mx, &info->opt_master,
  532. &elem);
  533. if (err < 0)
  534. return err;
  535. /* Add R channel source playback enumeration */
  536. snprintf(mx, sizeof(mx), "Master %dR (%s) Source Playback Enum",
  537. index + 1, name);
  538. err = add_new_ctl(mixer, &usb_scarlett_ctl_dynamic_enum,
  539. scarlett_ctl_enum_resume, 0x33, 0x00,
  540. 2*index+1, USB_MIXER_S16, 1, mx, &info->opt_master,
  541. &elem);
  542. if (err < 0)
  543. return err;
  544. return 0;
  545. }
  546. /********************** device-specific config *************************/
  547. /* untested... */
  548. static struct scarlett_device_info s6i6_info = {
  549. .matrix_in = 18,
  550. .matrix_out = 8,
  551. .input_len = 6,
  552. .output_len = 6,
  553. .opt_master = {
  554. .start = -1,
  555. .len = 27,
  556. .offsets = {0, 12, 16, 18, 18},
  557. .names = NULL
  558. },
  559. .opt_matrix = {
  560. .start = -1,
  561. .len = 19,
  562. .offsets = {0, 12, 16, 18, 18},
  563. .names = NULL
  564. },
  565. .num_controls = 9,
  566. .controls = {
  567. { .num = 0, .type = SCARLETT_OUTPUTS, .name = "Monitor" },
  568. { .num = 1, .type = SCARLETT_OUTPUTS, .name = "Headphone" },
  569. { .num = 2, .type = SCARLETT_OUTPUTS, .name = "SPDIF" },
  570. { .num = 1, .type = SCARLETT_SWITCH_IMPEDANCE, .name = NULL},
  571. { .num = 1, .type = SCARLETT_SWITCH_PAD, .name = NULL},
  572. { .num = 2, .type = SCARLETT_SWITCH_IMPEDANCE, .name = NULL},
  573. { .num = 2, .type = SCARLETT_SWITCH_PAD, .name = NULL},
  574. { .num = 3, .type = SCARLETT_SWITCH_PAD, .name = NULL},
  575. { .num = 4, .type = SCARLETT_SWITCH_PAD, .name = NULL},
  576. },
  577. .matrix_mux_init = {
  578. 12, 13, 14, 15, /* Analog -> 1..4 */
  579. 16, 17, /* SPDIF -> 5,6 */
  580. 0, 1, 2, 3, 4, 5, 6, 7, /* PCM[1..12] -> 7..18 */
  581. 8, 9, 10, 11
  582. }
  583. };
  584. /* untested... */
  585. static struct scarlett_device_info s8i6_info = {
  586. .matrix_in = 18,
  587. .matrix_out = 6,
  588. .input_len = 8,
  589. .output_len = 6,
  590. .opt_master = {
  591. .start = -1,
  592. .len = 25,
  593. .offsets = {0, 12, 16, 18, 18},
  594. .names = NULL
  595. },
  596. .opt_matrix = {
  597. .start = -1,
  598. .len = 19,
  599. .offsets = {0, 12, 16, 18, 18},
  600. .names = NULL
  601. },
  602. .num_controls = 7,
  603. .controls = {
  604. { .num = 0, .type = SCARLETT_OUTPUTS, .name = "Monitor" },
  605. { .num = 1, .type = SCARLETT_OUTPUTS, .name = "Headphone" },
  606. { .num = 2, .type = SCARLETT_OUTPUTS, .name = "SPDIF" },
  607. { .num = 1, .type = SCARLETT_SWITCH_IMPEDANCE, .name = NULL},
  608. { .num = 2, .type = SCARLETT_SWITCH_IMPEDANCE, .name = NULL},
  609. { .num = 3, .type = SCARLETT_SWITCH_PAD, .name = NULL},
  610. { .num = 4, .type = SCARLETT_SWITCH_PAD, .name = NULL},
  611. },
  612. .matrix_mux_init = {
  613. 12, 13, 14, 15, /* Analog -> 1..4 */
  614. 16, 17, /* SPDIF -> 5,6 */
  615. 0, 1, 2, 3, 4, 5, 6, 7, /* PCM[1..12] -> 7..18 */
  616. 8, 9, 10, 11
  617. }
  618. };
  619. static struct scarlett_device_info s18i6_info = {
  620. .matrix_in = 18,
  621. .matrix_out = 6,
  622. .input_len = 18,
  623. .output_len = 6,
  624. .opt_master = {
  625. .start = -1,
  626. .len = 31,
  627. .offsets = {0, 6, 14, 16, 24},
  628. .names = NULL,
  629. },
  630. .opt_matrix = {
  631. .start = -1,
  632. .len = 25,
  633. .offsets = {0, 6, 14, 16, 24},
  634. .names = NULL,
  635. },
  636. .num_controls = 5,
  637. .controls = {
  638. { .num = 0, .type = SCARLETT_OUTPUTS, .name = "Monitor" },
  639. { .num = 1, .type = SCARLETT_OUTPUTS, .name = "Headphone" },
  640. { .num = 2, .type = SCARLETT_OUTPUTS, .name = "SPDIF" },
  641. { .num = 1, .type = SCARLETT_SWITCH_IMPEDANCE, .name = NULL},
  642. { .num = 2, .type = SCARLETT_SWITCH_IMPEDANCE, .name = NULL},
  643. },
  644. .matrix_mux_init = {
  645. 6, 7, 8, 9, 10, 11, 12, 13, /* Analog -> 1..8 */
  646. 16, 17, 18, 19, 20, 21, /* ADAT[1..6] -> 9..14 */
  647. 14, 15, /* SPDIF -> 15,16 */
  648. 0, 1 /* PCM[1,2] -> 17,18 */
  649. }
  650. };
  651. static struct scarlett_device_info s18i8_info = {
  652. .matrix_in = 18,
  653. .matrix_out = 8,
  654. .input_len = 18,
  655. .output_len = 8,
  656. .opt_master = {
  657. .start = -1,
  658. .len = 35,
  659. .offsets = {0, 8, 16, 18, 26},
  660. .names = NULL
  661. },
  662. .opt_matrix = {
  663. .start = -1,
  664. .len = 27,
  665. .offsets = {0, 8, 16, 18, 26},
  666. .names = NULL
  667. },
  668. .num_controls = 10,
  669. .controls = {
  670. { .num = 0, .type = SCARLETT_OUTPUTS, .name = "Monitor" },
  671. { .num = 1, .type = SCARLETT_OUTPUTS, .name = "Headphone 1" },
  672. { .num = 2, .type = SCARLETT_OUTPUTS, .name = "Headphone 2" },
  673. { .num = 3, .type = SCARLETT_OUTPUTS, .name = "SPDIF" },
  674. { .num = 1, .type = SCARLETT_SWITCH_IMPEDANCE, .name = NULL},
  675. { .num = 1, .type = SCARLETT_SWITCH_PAD, .name = NULL},
  676. { .num = 2, .type = SCARLETT_SWITCH_IMPEDANCE, .name = NULL},
  677. { .num = 2, .type = SCARLETT_SWITCH_PAD, .name = NULL},
  678. { .num = 3, .type = SCARLETT_SWITCH_PAD, .name = NULL},
  679. { .num = 4, .type = SCARLETT_SWITCH_PAD, .name = NULL},
  680. },
  681. .matrix_mux_init = {
  682. 8, 9, 10, 11, 12, 13, 14, 15, /* Analog -> 1..8 */
  683. 18, 19, 20, 21, 22, 23, /* ADAT[1..6] -> 9..14 */
  684. 16, 17, /* SPDIF -> 15,16 */
  685. 0, 1 /* PCM[1,2] -> 17,18 */
  686. }
  687. };
  688. static struct scarlett_device_info s18i20_info = {
  689. .matrix_in = 18,
  690. .matrix_out = 8,
  691. .input_len = 18,
  692. .output_len = 20,
  693. .opt_master = {
  694. .start = -1,
  695. .len = 47,
  696. .offsets = {0, 20, 28, 30, 38},
  697. .names = NULL
  698. },
  699. .opt_matrix = {
  700. .start = -1,
  701. .len = 39,
  702. .offsets = {0, 20, 28, 30, 38},
  703. .names = NULL
  704. },
  705. .num_controls = 10,
  706. .controls = {
  707. { .num = 0, .type = SCARLETT_OUTPUTS, .name = "Monitor" },
  708. { .num = 1, .type = SCARLETT_OUTPUTS, .name = "Line 3/4" },
  709. { .num = 2, .type = SCARLETT_OUTPUTS, .name = "Line 5/6" },
  710. { .num = 3, .type = SCARLETT_OUTPUTS, .name = "Line 7/8" },
  711. { .num = 4, .type = SCARLETT_OUTPUTS, .name = "Line 9/10" },
  712. { .num = 5, .type = SCARLETT_OUTPUTS, .name = "SPDIF" },
  713. { .num = 6, .type = SCARLETT_OUTPUTS, .name = "ADAT 1/2" },
  714. { .num = 7, .type = SCARLETT_OUTPUTS, .name = "ADAT 3/4" },
  715. { .num = 8, .type = SCARLETT_OUTPUTS, .name = "ADAT 5/6" },
  716. { .num = 9, .type = SCARLETT_OUTPUTS, .name = "ADAT 7/8" },
  717. /*{ .num = 1, .type = SCARLETT_SWITCH_IMPEDANCE, .name = NULL},
  718. { .num = 1, .type = SCARLETT_SWITCH_PAD, .name = NULL},
  719. { .num = 2, .type = SCARLETT_SWITCH_IMPEDANCE, .name = NULL},
  720. { .num = 2, .type = SCARLETT_SWITCH_PAD, .name = NULL},
  721. { .num = 3, .type = SCARLETT_SWITCH_PAD, .name = NULL},
  722. { .num = 4, .type = SCARLETT_SWITCH_PAD, .name = NULL},*/
  723. },
  724. .matrix_mux_init = {
  725. 20, 21, 22, 23, 24, 25, 26, 27, /* Analog -> 1..8 */
  726. 30, 31, 32, 33, 34, 35, /* ADAT[1..6] -> 9..14 */
  727. 28, 29, /* SPDIF -> 15,16 */
  728. 0, 1 /* PCM[1,2] -> 17,18 */
  729. }
  730. };
  731. static int scarlett_controls_create_generic(struct usb_mixer_interface *mixer,
  732. struct scarlett_device_info *info)
  733. {
  734. int i, err;
  735. char mx[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
  736. const struct scarlett_mixer_control *ctl;
  737. struct usb_mixer_elem_info *elem;
  738. /* create master switch and playback volume */
  739. err = add_new_ctl(mixer, &usb_scarlett_ctl_switch,
  740. scarlett_ctl_resume, 0x0a, 0x01, 0,
  741. USB_MIXER_S16, 1, "Master Playback Switch", NULL,
  742. &elem);
  743. if (err < 0)
  744. return err;
  745. err = add_new_ctl(mixer, &usb_scarlett_ctl_master,
  746. scarlett_ctl_resume, 0x0a, 0x02, 0,
  747. USB_MIXER_S16, 1, "Master Playback Volume", NULL,
  748. &elem);
  749. if (err < 0)
  750. return err;
  751. /* iterate through controls in info struct and create each one */
  752. for (i = 0; i < info->num_controls; i++) {
  753. ctl = &info->controls[i];
  754. switch (ctl->type) {
  755. case SCARLETT_OUTPUTS:
  756. err = add_output_ctls(mixer, ctl->num, ctl->name, info);
  757. if (err < 0)
  758. return err;
  759. break;
  760. case SCARLETT_SWITCH_IMPEDANCE:
  761. sprintf(mx, "Input %d Impedance Switch", ctl->num);
  762. err = add_new_ctl(mixer, &usb_scarlett_ctl_enum,
  763. scarlett_ctl_enum_resume, 0x01,
  764. 0x09, ctl->num, USB_MIXER_S16, 1, mx,
  765. &opt_impedance, &elem);
  766. if (err < 0)
  767. return err;
  768. break;
  769. case SCARLETT_SWITCH_PAD:
  770. sprintf(mx, "Input %d Pad Switch", ctl->num);
  771. err = add_new_ctl(mixer, &usb_scarlett_ctl_enum,
  772. scarlett_ctl_enum_resume, 0x01,
  773. 0x0b, ctl->num, USB_MIXER_S16, 1, mx,
  774. &opt_pad, &elem);
  775. if (err < 0)
  776. return err;
  777. break;
  778. }
  779. }
  780. return 0;
  781. }
  782. /*
  783. * Create and initialize a mixer for the Focusrite(R) Scarlett
  784. */
  785. int snd_scarlett_controls_create(struct usb_mixer_interface *mixer)
  786. {
  787. int err, i, o;
  788. char mx[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
  789. struct scarlett_device_info *info;
  790. struct usb_mixer_elem_info *elem;
  791. static char sample_rate_buffer[4] = { '\x80', '\xbb', '\x00', '\x00' };
  792. /* only use UAC_VERSION_2 */
  793. if (!mixer->protocol)
  794. return 0;
  795. switch (mixer->chip->usb_id) {
  796. case USB_ID(0x1235, 0x8012):
  797. info = &s6i6_info;
  798. break;
  799. case USB_ID(0x1235, 0x8002):
  800. info = &s8i6_info;
  801. break;
  802. case USB_ID(0x1235, 0x8004):
  803. info = &s18i6_info;
  804. break;
  805. case USB_ID(0x1235, 0x8014):
  806. info = &s18i8_info;
  807. break;
  808. case USB_ID(0x1235, 0x800c):
  809. info = &s18i20_info;
  810. break;
  811. default: /* device not (yet) supported */
  812. return -EINVAL;
  813. }
  814. /* generic function to create controls */
  815. err = scarlett_controls_create_generic(mixer, info);
  816. if (err < 0)
  817. return err;
  818. /* setup matrix controls */
  819. for (i = 0; i < info->matrix_in; i++) {
  820. snprintf(mx, sizeof(mx), "Matrix %02d Input Playback Route",
  821. i+1);
  822. err = add_new_ctl(mixer, &usb_scarlett_ctl_dynamic_enum,
  823. scarlett_ctl_enum_resume, 0x32,
  824. 0x06, i, USB_MIXER_S16, 1, mx,
  825. &info->opt_matrix, &elem);
  826. if (err < 0)
  827. return err;
  828. for (o = 0; o < info->matrix_out; o++) {
  829. sprintf(mx, "Matrix %02d Mix %c Playback Volume", i+1,
  830. o+'A');
  831. err = add_new_ctl(mixer, &usb_scarlett_ctl,
  832. scarlett_ctl_resume, 0x3c, 0x00,
  833. (i << 3) + (o & 0x07), USB_MIXER_S16,
  834. 1, mx, NULL, &elem);
  835. if (err < 0)
  836. return err;
  837. }
  838. }
  839. for (i = 0; i < info->input_len; i++) {
  840. snprintf(mx, sizeof(mx), "Input Source %02d Capture Route",
  841. i+1);
  842. err = add_new_ctl(mixer, &usb_scarlett_ctl_dynamic_enum,
  843. scarlett_ctl_enum_resume, 0x34,
  844. 0x00, i, USB_MIXER_S16, 1, mx,
  845. &info->opt_master, &elem);
  846. if (err < 0)
  847. return err;
  848. }
  849. /* val_len == 1 needed here */
  850. err = add_new_ctl(mixer, &usb_scarlett_ctl_enum,
  851. scarlett_ctl_enum_resume, 0x28, 0x01, 0,
  852. USB_MIXER_U8, 1, "Sample Clock Source",
  853. &opt_clock, &elem);
  854. if (err < 0)
  855. return err;
  856. /* val_len == 1 and UAC2_CS_MEM */
  857. err = add_new_ctl(mixer, &usb_scarlett_ctl_sync, NULL, 0x3c, 0x00, 2,
  858. USB_MIXER_U8, 1, "Sample Clock Sync Status",
  859. &opt_sync, &elem);
  860. if (err < 0)
  861. return err;
  862. /* initialize sampling rate to 48000 */
  863. err = snd_usb_ctl_msg(mixer->chip->dev,
  864. usb_sndctrlpipe(mixer->chip->dev, 0), UAC2_CS_CUR,
  865. USB_RECIP_INTERFACE | USB_TYPE_CLASS |
  866. USB_DIR_OUT, 0x0100, snd_usb_ctrl_intf(mixer->chip) |
  867. (0x29 << 8), sample_rate_buffer, 4);
  868. if (err < 0)
  869. return err;
  870. return err;
  871. }