forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmixer_scarlett_gen2.c
2075 lines (1744 loc) · 54.9 KB
/
mixer_scarlett_gen2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-2.0
/*
* Focusrite Scarlett 6i6/18i8/18i20 Gen 2 Driver for ALSA
*
* Copyright (c) 2018-2019 by Geoffrey D. Bennett <g at b4.vu>
*
* Based on the Scarlett (Gen 1) Driver for ALSA:
*
* Copyright (c) 2013 by Tobias Hoffmann
* Copyright (c) 2013 by Robin Gareus <robin at gareus.org>
* Copyright (c) 2002 by Takashi Iwai <tiwai at suse.de>
* Copyright (c) 2014 by Chris J Arges <chris.j.arges at canonical.com>
*
* Many codes borrowed from audio.c by
* Alan Cox (alan at lxorguk.ukuu.org.uk)
* Thomas Sailer (sailer at ife.ee.ethz.ch)
*
* Code cleanup:
* David Henningsson <david.henningsson at canonical.com>
*/
/* Mixer Interface for the Focusrite Scarlett 6i6/18i8/18i20 Gen 2 audio
* interface. Based on the Gen 1 driver and rewritten.
*/
/* The protocol was reverse engineered by looking at the communication
* between Focusrite Control 2.3.4 and the Focusrite(R) Scarlett 18i20
* (firmware 1083) using usbmon in July-August 2018.
*
* Scarlett 18i8 support added in April 2019.
*
* Scarlett 6i6 support added in June 2019 (thanks to Martin Wittmann
* for providing usbmon output and testing).
*
* This ALSA mixer gives access to:
* - input, output, mixer-matrix muxes
* - 18x10 mixer-matrix gain stages
* - gain/volume controls
* - level meters
* - line/inst level and pad controls
*
* <ditaa>
* /--------------\ 18chn 20chn /--------------\
* | Hardware in +--+------\ /-------------+--+ ALSA PCM out |
* \--------------/ | | | | \--------------/
* | | | /-----\ |
* | | | | | |
* | v v v | |
* | +---------------+ | |
* | \ Matrix Mux / | |
* | +-----+-----+ | |
* | | | |
* | |18chn | |
* | | | |
* | | 10chn| |
* | v | |
* | +------------+ | |
* | | Mixer | | |
* | | Matrix | | |
* | | | | |
* | | 18x10 Gain | | |
* | | stages | | |
* | +-----+------+ | |
* | | | |
* |18chn |10chn | |20chn
* | | | |
* | +----------/ |
* | | |
* v v v
* ===========================
* +---------------+ +--—------------+
* \ Output Mux / \ Capture Mux /
* +---+---+---+ +-----+-----+
* | | |
* 10chn| | |18chn
* | | |
* /--------------\ | | | /--------------\
* | S/PDIF, ADAT |<--/ |10chn \-->| ALSA PCM in |
* | Hardware out | | \--------------/
* \--------------/ |
* v
* +-------------+ Software gain per channel.
* | Master Gain |<-- 18i20 only: Switch per channel
* +------+------+ to select HW or SW gain control.
* |
* |10chn
* /--------------\ |
* | Analogue |<------/
* | Hardware out |
* \--------------/
* </ditaa>
*
*/
#include <linux/slab.h>
#include <linux/usb.h>
#include <linux/moduleparam.h>
#include <sound/control.h>
#include <sound/tlv.h>
#include "usbaudio.h"
#include "mixer.h"
#include "helper.h"
#include "mixer_scarlett_gen2.h"
/* device_setup value to enable */
#define SCARLETT2_ENABLE 0x01
/* some gui mixers can't handle negative ctl values */
#define SCARLETT2_VOLUME_BIAS 127
/* mixer range from -80dB to +6dB in 0.5dB steps */
#define SCARLETT2_MIXER_MIN_DB -80
#define SCARLETT2_MIXER_BIAS (-SCARLETT2_MIXER_MIN_DB * 2)
#define SCARLETT2_MIXER_MAX_DB 6
#define SCARLETT2_MIXER_MAX_VALUE \
((SCARLETT2_MIXER_MAX_DB - SCARLETT2_MIXER_MIN_DB) * 2)
/* map from (dB + 80) * 2 to mixer value
* for dB in 0 .. 172: int(8192 * pow(10, ((dB - 160) / 2 / 20)))
*/
static const u16 scarlett2_mixer_values[173] = {
0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2,
2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 8, 8,
9, 9, 10, 10, 11, 12, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
23, 24, 25, 27, 29, 30, 32, 34, 36, 38, 41, 43, 46, 48, 51,
54, 57, 61, 65, 68, 73, 77, 81, 86, 91, 97, 103, 109, 115,
122, 129, 137, 145, 154, 163, 173, 183, 194, 205, 217, 230,
244, 259, 274, 290, 307, 326, 345, 365, 387, 410, 434, 460,
487, 516, 547, 579, 614, 650, 689, 730, 773, 819, 867, 919,
973, 1031, 1092, 1157, 1225, 1298, 1375, 1456, 1543, 1634,
1731, 1833, 1942, 2057, 2179, 2308, 2445, 2590, 2744, 2906,
3078, 3261, 3454, 3659, 3876, 4105, 4349, 4606, 4879, 5168,
5475, 5799, 6143, 6507, 6892, 7301, 7733, 8192, 8677, 9191,
9736, 10313, 10924, 11571, 12257, 12983, 13752, 14567, 15430,
16345
};
/* Maximum number of analogue outputs */
#define SCARLETT2_ANALOGUE_MAX 10
/* Maximum number of level and pad switches */
#define SCARLETT2_LEVEL_SWITCH_MAX 2
#define SCARLETT2_PAD_SWITCH_MAX 4
/* Maximum number of inputs to the mixer */
#define SCARLETT2_INPUT_MIX_MAX 18
/* Maximum number of outputs from the mixer */
#define SCARLETT2_OUTPUT_MIX_MAX 10
/* Maximum size of the data in the USB mux assignment message:
* 18 inputs, 20 outputs, 18 matrix inputs, 8 spare
*/
#define SCARLETT2_MUX_MAX 64
/* Number of meters:
* 18 inputs, 20 outputs, 18 matrix inputs
*/
#define SCARLETT2_NUM_METERS 56
/* Hardware port types:
* - None (no input to mux)
* - Analogue I/O
* - S/PDIF I/O
* - ADAT I/O
* - Mixer I/O
* - PCM I/O
*/
enum {
SCARLETT2_PORT_TYPE_NONE = 0,
SCARLETT2_PORT_TYPE_ANALOGUE = 1,
SCARLETT2_PORT_TYPE_SPDIF = 2,
SCARLETT2_PORT_TYPE_ADAT = 3,
SCARLETT2_PORT_TYPE_MIX = 4,
SCARLETT2_PORT_TYPE_PCM = 5,
SCARLETT2_PORT_TYPE_COUNT = 6,
};
/* Count of total I/O and number available at each sample rate */
enum {
SCARLETT2_PORT_IN = 0,
SCARLETT2_PORT_OUT = 1,
SCARLETT2_PORT_OUT_44 = 2,
SCARLETT2_PORT_OUT_88 = 3,
SCARLETT2_PORT_OUT_176 = 4,
SCARLETT2_PORT_DIRECTIONS = 5,
};
/* Hardware buttons on the 18i20 */
#define SCARLETT2_BUTTON_MAX 2
static const char *const scarlett2_button_names[SCARLETT2_BUTTON_MAX] = {
"Mute", "Dim"
};
/* Description of each hardware port type:
* - id: hardware ID for this port type
* - num: number of sources/destinations of this port type
* - src_descr: printf format string for mux input selections
* - src_num_offset: added to channel number for the fprintf
* - dst_descr: printf format string for mixer controls
*/
struct scarlett2_ports {
u16 id;
int num[SCARLETT2_PORT_DIRECTIONS];
const char * const src_descr;
int src_num_offset;
const char * const dst_descr;
};
struct scarlett2_device_info {
u8 line_out_hw_vol; /* line out hw volume is sw controlled */
u8 button_count; /* number of buttons */
u8 level_input_count; /* inputs with level selectable */
u8 pad_input_count; /* inputs with pad selectable */
const char * const line_out_descrs[SCARLETT2_ANALOGUE_MAX];
struct scarlett2_ports ports[SCARLETT2_PORT_TYPE_COUNT];
};
struct scarlett2_mixer_data {
struct usb_mixer_interface *mixer;
struct mutex usb_mutex; /* prevent sending concurrent USB requests */
struct mutex data_mutex; /* lock access to this data */
struct delayed_work work;
const struct scarlett2_device_info *info;
int num_mux_srcs;
u16 scarlett2_seq;
u8 vol_updated;
u8 master_vol;
u8 vol[SCARLETT2_ANALOGUE_MAX];
u8 vol_sw_hw_switch[SCARLETT2_ANALOGUE_MAX];
u8 level_switch[SCARLETT2_LEVEL_SWITCH_MAX];
u8 pad_switch[SCARLETT2_PAD_SWITCH_MAX];
u8 buttons[SCARLETT2_BUTTON_MAX];
struct snd_kcontrol *master_vol_ctl;
struct snd_kcontrol *vol_ctls[SCARLETT2_ANALOGUE_MAX];
struct snd_kcontrol *button_ctls[SCARLETT2_BUTTON_MAX];
u8 mux[SCARLETT2_MUX_MAX];
u8 mix[SCARLETT2_INPUT_MIX_MAX * SCARLETT2_OUTPUT_MIX_MAX];
};
/*** Model-specific data ***/
static const struct scarlett2_device_info s6i6_gen2_info = {
/* The first two analogue inputs can be switched between line
* and instrument levels.
*/
.level_input_count = 2,
/* The first two analogue inputs have an optional pad. */
.pad_input_count = 2,
.line_out_descrs = {
"Monitor L",
"Monitor R",
"Headphones L",
"Headphones R",
},
.ports = {
[SCARLETT2_PORT_TYPE_NONE] = {
.id = 0x000,
.num = { 1, 0, 8, 8, 8 },
.src_descr = "Off",
.src_num_offset = 0,
},
[SCARLETT2_PORT_TYPE_ANALOGUE] = {
.id = 0x080,
.num = { 4, 4, 4, 4, 4 },
.src_descr = "Analogue %d",
.src_num_offset = 1,
.dst_descr = "Analogue Output %02d Playback"
},
[SCARLETT2_PORT_TYPE_SPDIF] = {
.id = 0x180,
.num = { 2, 2, 2, 2, 2 },
.src_descr = "S/PDIF %d",
.src_num_offset = 1,
.dst_descr = "S/PDIF Output %d Playback"
},
[SCARLETT2_PORT_TYPE_MIX] = {
.id = 0x300,
.num = { 10, 18, 18, 18, 18 },
.src_descr = "Mix %c",
.src_num_offset = 65,
.dst_descr = "Mixer Input %02d Capture"
},
[SCARLETT2_PORT_TYPE_PCM] = {
.id = 0x600,
.num = { 6, 6, 6, 6, 6 },
.src_descr = "PCM %d",
.src_num_offset = 1,
.dst_descr = "PCM %02d Capture"
},
},
};
static const struct scarlett2_device_info s18i8_gen2_info = {
/* The first two analogue inputs can be switched between line
* and instrument levels.
*/
.level_input_count = 2,
/* The first four analogue inputs have an optional pad. */
.pad_input_count = 4,
.line_out_descrs = {
"Monitor L",
"Monitor R",
"Headphones 1 L",
"Headphones 1 R",
"Headphones 2 L",
"Headphones 2 R",
},
.ports = {
[SCARLETT2_PORT_TYPE_NONE] = {
.id = 0x000,
.num = { 1, 0, 8, 8, 4 },
.src_descr = "Off",
.src_num_offset = 0,
},
[SCARLETT2_PORT_TYPE_ANALOGUE] = {
.id = 0x080,
.num = { 8, 6, 6, 6, 6 },
.src_descr = "Analogue %d",
.src_num_offset = 1,
.dst_descr = "Analogue Output %02d Playback"
},
[SCARLETT2_PORT_TYPE_SPDIF] = {
.id = 0x180,
/* S/PDIF outputs aren't available at 192KHz
* but are included in the USB mux I/O
* assignment message anyway
*/
.num = { 2, 2, 2, 2, 2 },
.src_descr = "S/PDIF %d",
.src_num_offset = 1,
.dst_descr = "S/PDIF Output %d Playback"
},
[SCARLETT2_PORT_TYPE_ADAT] = {
.id = 0x200,
.num = { 8, 0, 0, 0, 0 },
.src_descr = "ADAT %d",
.src_num_offset = 1,
},
[SCARLETT2_PORT_TYPE_MIX] = {
.id = 0x300,
.num = { 10, 18, 18, 18, 18 },
.src_descr = "Mix %c",
.src_num_offset = 65,
.dst_descr = "Mixer Input %02d Capture"
},
[SCARLETT2_PORT_TYPE_PCM] = {
.id = 0x600,
.num = { 20, 18, 18, 14, 10 },
.src_descr = "PCM %d",
.src_num_offset = 1,
.dst_descr = "PCM %02d Capture"
},
},
};
static const struct scarlett2_device_info s18i20_gen2_info = {
/* The analogue line outputs on the 18i20 can be switched
* between software and hardware volume control
*/
.line_out_hw_vol = 1,
/* Mute and dim buttons */
.button_count = 2,
.line_out_descrs = {
"Monitor L",
"Monitor R",
NULL,
NULL,
NULL,
NULL,
"Headphones 1 L",
"Headphones 1 R",
"Headphones 2 L",
"Headphones 2 R",
},
.ports = {
[SCARLETT2_PORT_TYPE_NONE] = {
.id = 0x000,
.num = { 1, 0, 8, 8, 6 },
.src_descr = "Off",
.src_num_offset = 0,
},
[SCARLETT2_PORT_TYPE_ANALOGUE] = {
.id = 0x080,
.num = { 8, 10, 10, 10, 10 },
.src_descr = "Analogue %d",
.src_num_offset = 1,
.dst_descr = "Analogue Output %02d Playback"
},
[SCARLETT2_PORT_TYPE_SPDIF] = {
/* S/PDIF outputs aren't available at 192KHz
* but are included in the USB mux I/O
* assignment message anyway
*/
.id = 0x180,
.num = { 2, 2, 2, 2, 2 },
.src_descr = "S/PDIF %d",
.src_num_offset = 1,
.dst_descr = "S/PDIF Output %d Playback"
},
[SCARLETT2_PORT_TYPE_ADAT] = {
.id = 0x200,
.num = { 8, 8, 8, 4, 0 },
.src_descr = "ADAT %d",
.src_num_offset = 1,
.dst_descr = "ADAT Output %d Playback"
},
[SCARLETT2_PORT_TYPE_MIX] = {
.id = 0x300,
.num = { 10, 18, 18, 18, 18 },
.src_descr = "Mix %c",
.src_num_offset = 65,
.dst_descr = "Mixer Input %02d Capture"
},
[SCARLETT2_PORT_TYPE_PCM] = {
.id = 0x600,
.num = { 20, 18, 18, 14, 10 },
.src_descr = "PCM %d",
.src_num_offset = 1,
.dst_descr = "PCM %02d Capture"
},
},
};
/* get the starting port index number for a given port type/direction */
static int scarlett2_get_port_start_num(const struct scarlett2_ports *ports,
int direction, int port_type)
{
int i, num = 0;
for (i = 0; i < port_type; i++)
num += ports[i].num[direction];
return num;
}
/*** USB Interactions ***/
/* Vendor-Specific Interface, Endpoint, MaxPacketSize, Interval */
#define SCARLETT2_USB_VENDOR_SPECIFIC_INTERFACE 5
#define SCARLETT2_USB_INTERRUPT_ENDPOINT 4
#define SCARLETT2_USB_INTERRUPT_MAX_DATA 64
#define SCARLETT2_USB_INTERRUPT_INTERVAL 3
/* Interrupt flags for volume and mute/dim button changes */
#define SCARLETT2_USB_INTERRUPT_VOL_CHANGE 0x400000
#define SCARLETT2_USB_INTERRUPT_BUTTON_CHANGE 0x200000
/* Commands for sending/receiving requests/responses */
#define SCARLETT2_USB_VENDOR_SPECIFIC_CMD_REQ 2
#define SCARLETT2_USB_VENDOR_SPECIFIC_CMD_RESP 3
#define SCARLETT2_USB_INIT_SEQ 0x00000000
#define SCARLETT2_USB_GET_METER_LEVELS 0x00001001
#define SCARLETT2_USB_SET_MIX 0x00002002
#define SCARLETT2_USB_SET_MUX 0x00003002
#define SCARLETT2_USB_GET_DATA 0x00800000
#define SCARLETT2_USB_SET_DATA 0x00800001
#define SCARLETT2_USB_DATA_CMD 0x00800002
#define SCARLETT2_USB_CONFIG_SAVE 6
#define SCARLETT2_USB_VOLUME_STATUS_OFFSET 0x31
#define SCARLETT2_USB_METER_LEVELS_GET_MAGIC 1
/* volume status is read together (matches scarlett2_config_items[]) */
struct scarlett2_usb_volume_status {
/* mute & dim buttons */
u8 buttons[SCARLETT2_BUTTON_MAX];
u8 pad1;
/* software volume setting */
s16 sw_vol[SCARLETT2_ANALOGUE_MAX];
/* actual volume of output inc. dim (-18dB) */
s16 hw_vol[SCARLETT2_ANALOGUE_MAX];
u8 pad2[SCARLETT2_ANALOGUE_MAX];
/* sw (0) or hw (1) controlled */
u8 sw_hw_switch[SCARLETT2_ANALOGUE_MAX];
u8 pad3[6];
/* front panel volume knob */
s16 master_vol;
} __packed;
/* Configuration parameters that can be read and written */
enum {
SCARLETT2_CONFIG_BUTTONS = 0,
SCARLETT2_CONFIG_LINE_OUT_VOLUME = 1,
SCARLETT2_CONFIG_SW_HW_SWITCH = 2,
SCARLETT2_CONFIG_LEVEL_SWITCH = 3,
SCARLETT2_CONFIG_PAD_SWITCH = 4,
SCARLETT2_CONFIG_COUNT = 5
};
/* Location, size, and activation command number for the configuration
* parameters
*/
struct scarlett2_config {
u8 offset;
u8 size;
u8 activate;
};
static const struct scarlett2_config
scarlett2_config_items[SCARLETT2_CONFIG_COUNT] = {
/* Mute/Dim Buttons */
{
.offset = 0x31,
.size = 1,
.activate = 2
},
/* Line Out Volume */
{
.offset = 0x34,
.size = 2,
.activate = 1
},
/* SW/HW Volume Switch */
{
.offset = 0x66,
.size = 1,
.activate = 3
},
/* Level Switch */
{
.offset = 0x7c,
.size = 1,
.activate = 7
},
/* Pad Switch */
{
.offset = 0x84,
.size = 1,
.activate = 8
}
};
/* proprietary request/response format */
struct scarlett2_usb_packet {
__le32 cmd;
__le16 size;
__le16 seq;
__le32 error;
__le32 pad;
u8 data[];
};
#define SCARLETT2_USB_PACKET_LEN (sizeof(struct scarlett2_usb_packet))
static void scarlett2_fill_request_header(struct scarlett2_mixer_data *private,
struct scarlett2_usb_packet *req,
u32 cmd, u16 req_size)
{
/* sequence must go up by 1 for each request */
u16 seq = private->scarlett2_seq++;
req->cmd = cpu_to_le32(cmd);
req->size = cpu_to_le16(req_size);
req->seq = cpu_to_le16(seq);
req->error = 0;
req->pad = 0;
}
/* Send a proprietary format request to the Scarlett interface */
static int scarlett2_usb(
struct usb_mixer_interface *mixer, u32 cmd,
void *req_data, u16 req_size, void *resp_data, u16 resp_size)
{
struct scarlett2_mixer_data *private = mixer->private_data;
u16 req_buf_size = sizeof(struct scarlett2_usb_packet) + req_size;
u16 resp_buf_size = sizeof(struct scarlett2_usb_packet) + resp_size;
struct scarlett2_usb_packet *req = NULL, *resp = NULL;
int err = 0;
req = kmalloc(req_buf_size, GFP_KERNEL);
if (!req) {
err = -ENOMEM;
goto error;
}
resp = kmalloc(resp_buf_size, GFP_KERNEL);
if (!resp) {
err = -ENOMEM;
goto error;
}
mutex_lock(&private->usb_mutex);
/* build request message and send it */
scarlett2_fill_request_header(private, req, cmd, req_size);
if (req_size)
memcpy(req->data, req_data, req_size);
err = snd_usb_ctl_msg(mixer->chip->dev,
usb_sndctrlpipe(mixer->chip->dev, 0),
SCARLETT2_USB_VENDOR_SPECIFIC_CMD_REQ,
USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
0,
SCARLETT2_USB_VENDOR_SPECIFIC_INTERFACE,
req,
req_buf_size);
if (err != req_buf_size) {
usb_audio_err(
mixer->chip,
"Scarlett Gen 2 USB request result cmd %x was %d\n",
cmd, err);
err = -EINVAL;
goto unlock;
}
/* send a second message to get the response */
err = snd_usb_ctl_msg(mixer->chip->dev,
usb_sndctrlpipe(mixer->chip->dev, 0),
SCARLETT2_USB_VENDOR_SPECIFIC_CMD_RESP,
USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
0,
SCARLETT2_USB_VENDOR_SPECIFIC_INTERFACE,
resp,
resp_buf_size);
/* validate the response */
if (err != resp_buf_size) {
usb_audio_err(
mixer->chip,
"Scarlett Gen 2 USB response result cmd %x was %d\n",
cmd, err);
err = -EINVAL;
goto unlock;
}
if (resp->cmd != req->cmd ||
resp->seq != req->seq ||
resp_size != le16_to_cpu(resp->size) ||
resp->error ||
resp->pad) {
usb_audio_err(
mixer->chip,
"Scarlett Gen 2 USB invalid response; "
"cmd tx/rx %d/%d seq %d/%d size %d/%d "
"error %d pad %d\n",
le32_to_cpu(req->cmd), le32_to_cpu(resp->cmd),
le16_to_cpu(req->seq), le16_to_cpu(resp->seq),
resp_size, le16_to_cpu(resp->size),
le32_to_cpu(resp->error),
le32_to_cpu(resp->pad));
err = -EINVAL;
goto unlock;
}
if (resp_size > 0)
memcpy(resp_data, resp->data, resp_size);
unlock:
mutex_unlock(&private->usb_mutex);
error:
kfree(req);
kfree(resp);
return err;
}
/* Send SCARLETT2_USB_DATA_CMD SCARLETT2_USB_CONFIG_SAVE */
static void scarlett2_config_save(struct usb_mixer_interface *mixer)
{
__le32 req = cpu_to_le32(SCARLETT2_USB_CONFIG_SAVE);
scarlett2_usb(mixer, SCARLETT2_USB_DATA_CMD,
&req, sizeof(u32),
NULL, 0);
}
/* Delayed work to save config */
static void scarlett2_config_save_work(struct work_struct *work)
{
struct scarlett2_mixer_data *private =
container_of(work, struct scarlett2_mixer_data, work.work);
scarlett2_config_save(private->mixer);
}
/* Send a USB message to set a configuration parameter (volume level,
* sw/hw volume switch, line/inst level switch, or pad switch)
*/
static int scarlett2_usb_set_config(
struct usb_mixer_interface *mixer,
int config_item_num, int index, int value)
{
const struct scarlett2_config config_item =
scarlett2_config_items[config_item_num];
struct {
__le32 offset;
__le32 bytes;
__le32 value;
} __packed req;
__le32 req2;
int err;
struct scarlett2_mixer_data *private = mixer->private_data;
/* Cancel any pending NVRAM save */
cancel_delayed_work_sync(&private->work);
/* Send the configuration parameter data */
req.offset = cpu_to_le32(config_item.offset + index * config_item.size);
req.bytes = cpu_to_le32(config_item.size);
req.value = cpu_to_le32(value);
err = scarlett2_usb(mixer, SCARLETT2_USB_SET_DATA,
&req, sizeof(u32) * 2 + config_item.size,
NULL, 0);
if (err < 0)
return err;
/* Activate the change */
req2 = cpu_to_le32(config_item.activate);
err = scarlett2_usb(mixer, SCARLETT2_USB_DATA_CMD,
&req2, sizeof(req2), NULL, 0);
if (err < 0)
return err;
/* Schedule the change to be written to NVRAM */
schedule_delayed_work(&private->work, msecs_to_jiffies(2000));
return 0;
}
/* Send a USB message to get data; result placed in *buf */
static int scarlett2_usb_get(
struct usb_mixer_interface *mixer,
int offset, void *buf, int size)
{
struct {
__le32 offset;
__le32 size;
} __packed req;
req.offset = cpu_to_le32(offset);
req.size = cpu_to_le32(size);
return scarlett2_usb(mixer, SCARLETT2_USB_GET_DATA,
&req, sizeof(req), buf, size);
}
/* Send a USB message to get configuration parameters; result placed in *buf */
static int scarlett2_usb_get_config(
struct usb_mixer_interface *mixer,
int config_item_num, int count, void *buf)
{
const struct scarlett2_config config_item =
scarlett2_config_items[config_item_num];
int size = config_item.size * count;
return scarlett2_usb_get(mixer, config_item.offset, buf, size);
}
/* Send a USB message to get volume status; result placed in *buf */
static int scarlett2_usb_get_volume_status(
struct usb_mixer_interface *mixer,
struct scarlett2_usb_volume_status *buf)
{
return scarlett2_usb_get(mixer, SCARLETT2_USB_VOLUME_STATUS_OFFSET,
buf, sizeof(*buf));
}
/* Send a USB message to set the volumes for all inputs of one mix
* (values obtained from private->mix[])
*/
static int scarlett2_usb_set_mix(struct usb_mixer_interface *mixer,
int mix_num)
{
struct scarlett2_mixer_data *private = mixer->private_data;
const struct scarlett2_device_info *info = private->info;
struct {
__le16 mix_num;
__le16 data[SCARLETT2_INPUT_MIX_MAX];
} __packed req;
int i, j;
int num_mixer_in =
info->ports[SCARLETT2_PORT_TYPE_MIX].num[SCARLETT2_PORT_OUT];
req.mix_num = cpu_to_le16(mix_num);
for (i = 0, j = mix_num * num_mixer_in; i < num_mixer_in; i++, j++)
req.data[i] = cpu_to_le16(
scarlett2_mixer_values[private->mix[j]]
);
return scarlett2_usb(mixer, SCARLETT2_USB_SET_MIX,
&req, (num_mixer_in + 1) * sizeof(u16),
NULL, 0);
}
/* Convert a port number index (per info->ports) to a hardware ID */
static u32 scarlett2_mux_src_num_to_id(const struct scarlett2_ports *ports,
int num)
{
int port_type;
for (port_type = 0;
port_type < SCARLETT2_PORT_TYPE_COUNT;
port_type++) {
if (num < ports[port_type].num[SCARLETT2_PORT_IN])
return ports[port_type].id | num;
num -= ports[port_type].num[SCARLETT2_PORT_IN];
}
/* Oops */
return 0;
}
/* Send USB messages to set mux inputs */
static int scarlett2_usb_set_mux(struct usb_mixer_interface *mixer)
{
struct scarlett2_mixer_data *private = mixer->private_data;
const struct scarlett2_device_info *info = private->info;
const struct scarlett2_ports *ports = info->ports;
int rate, port_dir_rate;
static const int assignment_order[SCARLETT2_PORT_TYPE_COUNT] = {
SCARLETT2_PORT_TYPE_PCM,
SCARLETT2_PORT_TYPE_ANALOGUE,
SCARLETT2_PORT_TYPE_SPDIF,
SCARLETT2_PORT_TYPE_ADAT,
SCARLETT2_PORT_TYPE_MIX,
SCARLETT2_PORT_TYPE_NONE,
};
struct {
__le16 pad;
__le16 num;
__le32 data[SCARLETT2_MUX_MAX];
} __packed req;
req.pad = 0;
/* mux settings for each rate */
for (rate = 0, port_dir_rate = SCARLETT2_PORT_OUT_44;
port_dir_rate <= SCARLETT2_PORT_OUT_176;
rate++, port_dir_rate++) {
int order_num, i, err;
req.num = cpu_to_le16(rate);
for (order_num = 0, i = 0;
order_num < SCARLETT2_PORT_TYPE_COUNT;
order_num++) {
int port_type = assignment_order[order_num];
int j = scarlett2_get_port_start_num(ports,
SCARLETT2_PORT_OUT,
port_type);
int port_id = ports[port_type].id;
int channel;
for (channel = 0;
channel < ports[port_type].num[port_dir_rate];
channel++, i++, j++)
/* lower 12 bits for the destination and
* next 12 bits for the source
*/
req.data[i] = !port_id
? 0
: cpu_to_le32(
port_id |
channel |
scarlett2_mux_src_num_to_id(
ports, private->mux[j]
) << 12
);
/* skip private->mux[j] entries not output */
j += ports[port_type].num[SCARLETT2_PORT_OUT] -
ports[port_type].num[port_dir_rate];
}
err = scarlett2_usb(mixer, SCARLETT2_USB_SET_MUX,
&req, (i + 1) * sizeof(u32),
NULL, 0);
if (err < 0)
return err;
}
return 0;
}
/* Send USB message to get meter levels */
static int scarlett2_usb_get_meter_levels(struct usb_mixer_interface *mixer,
u16 *levels)
{
struct {
__le16 pad;
__le16 num_meters;
__le32 magic;
} __packed req;
u32 resp[SCARLETT2_NUM_METERS];
int i, err;
req.pad = 0;
req.num_meters = cpu_to_le16(SCARLETT2_NUM_METERS);
req.magic = cpu_to_le32(SCARLETT2_USB_METER_LEVELS_GET_MAGIC);
err = scarlett2_usb(mixer, SCARLETT2_USB_GET_METER_LEVELS,
&req, sizeof(req), resp, sizeof(resp));
if (err < 0)
return err;
/* copy, convert to u16 */
for (i = 0; i < SCARLETT2_NUM_METERS; i++)
levels[i] = resp[i];
return 0;
}
/*** Control Functions ***/
/* helper function to create a new control */
static int scarlett2_add_new_ctl(struct usb_mixer_interface *mixer,
const struct snd_kcontrol_new *ncontrol,
int index, int channels, const char *name,
struct snd_kcontrol **kctl_return)
{
struct snd_kcontrol *kctl;
struct usb_mixer_elem_info *elem;
int err;
elem = kzalloc(sizeof(*elem), GFP_KERNEL);
if (!elem)
return -ENOMEM;
elem->head.mixer = mixer;
elem->control = index;
elem->head.id = index;
elem->channels = channels;
kctl = snd_ctl_new1(ncontrol, elem);
if (!kctl) {
kfree(elem);
return -ENOMEM;
}
kctl->private_free = snd_usb_mixer_elem_free;
strlcpy(kctl->id.name, name, sizeof(kctl->id.name));
err = snd_usb_mixer_add_control(&elem->head, kctl);
if (err < 0)
return err;
if (kctl_return)
*kctl_return = kctl;
return 0;
}
/*** Analogue Line Out Volume Controls ***/
/* Update hardware volume controls after receiving notification that
* they have changed
*/
static int scarlett2_update_volumes(struct usb_mixer_interface *mixer)
{
struct scarlett2_mixer_data *private = mixer->private_data;
const struct scarlett2_ports *ports = private->info->ports;
struct scarlett2_usb_volume_status volume_status;
int num_line_out =
ports[SCARLETT2_PORT_TYPE_ANALOGUE].num[SCARLETT2_PORT_OUT];
int err, i;
private->vol_updated = 0;
err = scarlett2_usb_get_volume_status(mixer, &volume_status);
if (err < 0)
return err;
private->master_vol = clamp(
volume_status.master_vol + SCARLETT2_VOLUME_BIAS,
0, SCARLETT2_VOLUME_BIAS);
for (i = 0; i < num_line_out; i++) {