mixer_scarlett.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  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. }