Commit | Line | Data |
---|---|---|
9e4d5c1b GB |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | /* | |
3 | * Focusrite Scarlett 6i6/18i8/18i20 Gen 2 Driver for ALSA | |
4 | * | |
5 | * Copyright (c) 2018-2019 by Geoffrey D. Bennett <g at b4.vu> | |
6 | * | |
7 | * Based on the Scarlett (Gen 1) Driver for ALSA: | |
8 | * | |
9 | * Copyright (c) 2013 by Tobias Hoffmann | |
10 | * Copyright (c) 2013 by Robin Gareus <robin at gareus.org> | |
11 | * Copyright (c) 2002 by Takashi Iwai <tiwai at suse.de> | |
12 | * Copyright (c) 2014 by Chris J Arges <chris.j.arges at canonical.com> | |
13 | * | |
14 | * Many codes borrowed from audio.c by | |
15 | * Alan Cox (alan at lxorguk.ukuu.org.uk) | |
16 | * Thomas Sailer (sailer at ife.ee.ethz.ch) | |
17 | * | |
18 | * Code cleanup: | |
19 | * David Henningsson <david.henningsson at canonical.com> | |
20 | */ | |
21 | ||
22 | /* Mixer Interface for the Focusrite Scarlett 6i6/18i8/18i20 Gen 2 audio | |
23 | * interface. Based on the Gen 1 driver and rewritten. | |
24 | */ | |
25 | ||
26 | /* The protocol was reverse engineered by looking at the communication | |
27 | * between Focusrite Control 2.3.4 and the Focusrite(R) Scarlett 18i20 | |
28 | * (firmware 1083) using usbmon in July-August 2018. | |
29 | * | |
30 | * Scarlett 18i8 support added in April 2019. | |
31 | * | |
32 | * Scarlett 6i6 support added in June 2019 (thanks to Martin Wittmann | |
33 | * for providing usbmon output and testing). | |
34 | * | |
35 | * This ALSA mixer gives access to: | |
36 | * - input, output, mixer-matrix muxes | |
37 | * - 18x10 mixer-matrix gain stages | |
38 | * - gain/volume controls | |
39 | * - level meters | |
40 | * - line/inst level and pad controls | |
41 | * | |
42 | * <ditaa> | |
43 | * /--------------\ 18chn 20chn /--------------\ | |
44 | * | Hardware in +--+------\ /-------------+--+ ALSA PCM out | | |
45 | * \--------------/ | | | | \--------------/ | |
46 | * | | | /-----\ | | |
47 | * | | | | | | | |
48 | * | v v v | | | |
49 | * | +---------------+ | | | |
50 | * | \ Matrix Mux / | | | |
51 | * | +-----+-----+ | | | |
52 | * | | | | | |
53 | * | |18chn | | | |
54 | * | | | | | |
55 | * | | 10chn| | | |
56 | * | v | | | |
57 | * | +------------+ | | | |
58 | * | | Mixer | | | | |
59 | * | | Matrix | | | | |
60 | * | | | | | | |
61 | * | | 18x10 Gain | | | | |
62 | * | | stages | | | | |
63 | * | +-----+------+ | | | |
64 | * | | | | | |
65 | * |18chn |10chn | |20chn | |
66 | * | | | | | |
67 | * | +----------/ | | |
68 | * | | | | |
69 | * v v v | |
70 | * =========================== | |
71 | * +---------------+ +--—------------+ | |
72 | * \ Output Mux / \ Capture Mux / | |
73 | * +---+---+---+ +-----+-----+ | |
74 | * | | | | |
75 | * 10chn| | |18chn | |
76 | * | | | | |
77 | * /--------------\ | | | /--------------\ | |
78 | * | S/PDIF, ADAT |<--/ |10chn \-->| ALSA PCM in | | |
79 | * | Hardware out | | \--------------/ | |
80 | * \--------------/ | | |
81 | * v | |
82 | * +-------------+ Software gain per channel. | |
83 | * | Master Gain |<-- 18i20 only: Switch per channel | |
84 | * +------+------+ to select HW or SW gain control. | |
85 | * | | |
86 | * |10chn | |
87 | * /--------------\ | | |
88 | * | Analogue |<------/ | |
89 | * | Hardware out | | |
90 | * \--------------/ | |
91 | * </ditaa> | |
92 | * | |
93 | */ | |
94 | ||
95 | #include <linux/slab.h> | |
96 | #include <linux/usb.h> | |
97 | #include <linux/moduleparam.h> | |
98 | ||
99 | #include <sound/control.h> | |
100 | #include <sound/tlv.h> | |
101 | ||
102 | #include "usbaudio.h" | |
103 | #include "mixer.h" | |
104 | #include "helper.h" | |
105 | ||
106 | #include "mixer_scarlett_gen2.h" | |
107 | ||
108 | /* device_setup value to enable */ | |
109 | #define SCARLETT2_ENABLE 0x01 | |
110 | ||
111 | /* some gui mixers can't handle negative ctl values */ | |
112 | #define SCARLETT2_VOLUME_BIAS 127 | |
113 | ||
114 | /* mixer range from -80dB to +6dB in 0.5dB steps */ | |
115 | #define SCARLETT2_MIXER_MIN_DB -80 | |
116 | #define SCARLETT2_MIXER_BIAS (-SCARLETT2_MIXER_MIN_DB * 2) | |
117 | #define SCARLETT2_MIXER_MAX_DB 6 | |
118 | #define SCARLETT2_MIXER_MAX_VALUE \ | |
119 | ((SCARLETT2_MIXER_MAX_DB - SCARLETT2_MIXER_MIN_DB) * 2) | |
120 | ||
121 | /* map from (dB + 80) * 2 to mixer value | |
122 | * for dB in 0 .. 172: int(8192 * pow(10, ((dB - 160) / 2 / 20))) | |
123 | */ | |
124 | static const u16 scarlett2_mixer_values[173] = { | |
125 | 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, | |
126 | 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 8, 8, | |
127 | 9, 9, 10, 10, 11, 12, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, | |
128 | 23, 24, 25, 27, 29, 30, 32, 34, 36, 38, 41, 43, 46, 48, 51, | |
129 | 54, 57, 61, 65, 68, 73, 77, 81, 86, 91, 97, 103, 109, 115, | |
130 | 122, 129, 137, 145, 154, 163, 173, 183, 194, 205, 217, 230, | |
131 | 244, 259, 274, 290, 307, 326, 345, 365, 387, 410, 434, 460, | |
132 | 487, 516, 547, 579, 614, 650, 689, 730, 773, 819, 867, 919, | |
133 | 973, 1031, 1092, 1157, 1225, 1298, 1375, 1456, 1543, 1634, | |
134 | 1731, 1833, 1942, 2057, 2179, 2308, 2445, 2590, 2744, 2906, | |
135 | 3078, 3261, 3454, 3659, 3876, 4105, 4349, 4606, 4879, 5168, | |
136 | 5475, 5799, 6143, 6507, 6892, 7301, 7733, 8192, 8677, 9191, | |
137 | 9736, 10313, 10924, 11571, 12257, 12983, 13752, 14567, 15430, | |
138 | 16345 | |
139 | }; | |
140 | ||
141 | /* Maximum number of analogue outputs */ | |
142 | #define SCARLETT2_ANALOGUE_MAX 10 | |
143 | ||
144 | /* Maximum number of level and pad switches */ | |
145 | #define SCARLETT2_LEVEL_SWITCH_MAX 2 | |
146 | #define SCARLETT2_PAD_SWITCH_MAX 4 | |
147 | ||
148 | /* Maximum number of inputs to the mixer */ | |
149 | #define SCARLETT2_INPUT_MIX_MAX 18 | |
150 | ||
151 | /* Maximum number of outputs from the mixer */ | |
152 | #define SCARLETT2_OUTPUT_MIX_MAX 10 | |
153 | ||
154 | /* Maximum size of the data in the USB mux assignment message: | |
155 | * 18 inputs, 20 outputs, 18 matrix inputs, 8 spare | |
156 | */ | |
157 | #define SCARLETT2_MUX_MAX 64 | |
158 | ||
159 | /* Number of meters: | |
160 | * 18 inputs, 20 outputs, 18 matrix inputs | |
161 | */ | |
162 | #define SCARLETT2_NUM_METERS 56 | |
163 | ||
164 | /* Hardware port types: | |
165 | * - None (no input to mux) | |
166 | * - Analogue I/O | |
167 | * - S/PDIF I/O | |
168 | * - ADAT I/O | |
169 | * - Mixer I/O | |
170 | * - PCM I/O | |
171 | */ | |
172 | enum { | |
173 | SCARLETT2_PORT_TYPE_NONE = 0, | |
174 | SCARLETT2_PORT_TYPE_ANALOGUE = 1, | |
175 | SCARLETT2_PORT_TYPE_SPDIF = 2, | |
176 | SCARLETT2_PORT_TYPE_ADAT = 3, | |
177 | SCARLETT2_PORT_TYPE_MIX = 4, | |
178 | SCARLETT2_PORT_TYPE_PCM = 5, | |
179 | SCARLETT2_PORT_TYPE_COUNT = 6, | |
180 | }; | |
181 | ||
182 | /* Count of total I/O and number available at each sample rate */ | |
183 | enum { | |
184 | SCARLETT2_PORT_IN = 0, | |
185 | SCARLETT2_PORT_OUT = 1, | |
186 | SCARLETT2_PORT_OUT_44 = 2, | |
187 | SCARLETT2_PORT_OUT_88 = 3, | |
188 | SCARLETT2_PORT_OUT_176 = 4, | |
189 | SCARLETT2_PORT_DIRECTIONS = 5, | |
190 | }; | |
191 | ||
192 | /* Hardware buttons on the 18i20 */ | |
193 | #define SCARLETT2_BUTTON_MAX 2 | |
194 | ||
195 | static const char *const scarlett2_button_names[SCARLETT2_BUTTON_MAX] = { | |
196 | "Mute", "Dim" | |
197 | }; | |
198 | ||
199 | /* Description of each hardware port type: | |
200 | * - id: hardware ID for this port type | |
201 | * - num: number of sources/destinations of this port type | |
202 | * - src_descr: printf format string for mux input selections | |
203 | * - src_num_offset: added to channel number for the fprintf | |
204 | * - dst_descr: printf format string for mixer controls | |
205 | */ | |
206 | struct scarlett2_ports { | |
207 | u16 id; | |
208 | int num[SCARLETT2_PORT_DIRECTIONS]; | |
209 | const char * const src_descr; | |
210 | int src_num_offset; | |
211 | const char * const dst_descr; | |
212 | }; | |
213 | ||
214 | struct scarlett2_device_info { | |
215 | u8 line_out_hw_vol; /* line out hw volume is sw controlled */ | |
216 | u8 button_count; /* number of buttons */ | |
217 | u8 level_input_count; /* inputs with level selectable */ | |
218 | u8 pad_input_count; /* inputs with pad selectable */ | |
219 | const char * const line_out_descrs[SCARLETT2_ANALOGUE_MAX]; | |
220 | struct scarlett2_ports ports[SCARLETT2_PORT_TYPE_COUNT]; | |
221 | }; | |
222 | ||
223 | struct scarlett2_mixer_data { | |
224 | struct usb_mixer_interface *mixer; | |
225 | struct mutex usb_mutex; /* prevent sending concurrent USB requests */ | |
226 | struct mutex data_mutex; /* lock access to this data */ | |
227 | struct delayed_work work; | |
228 | const struct scarlett2_device_info *info; | |
229 | int num_mux_srcs; | |
230 | u16 scarlett2_seq; | |
231 | u8 vol_updated; | |
232 | u8 master_vol; | |
233 | u8 vol[SCARLETT2_ANALOGUE_MAX]; | |
234 | u8 vol_sw_hw_switch[SCARLETT2_ANALOGUE_MAX]; | |
235 | u8 level_switch[SCARLETT2_LEVEL_SWITCH_MAX]; | |
236 | u8 pad_switch[SCARLETT2_PAD_SWITCH_MAX]; | |
237 | u8 buttons[SCARLETT2_BUTTON_MAX]; | |
238 | struct snd_kcontrol *master_vol_ctl; | |
239 | struct snd_kcontrol *vol_ctls[SCARLETT2_ANALOGUE_MAX]; | |
240 | struct snd_kcontrol *button_ctls[SCARLETT2_BUTTON_MAX]; | |
241 | u8 mux[SCARLETT2_MUX_MAX]; | |
242 | u8 mix[SCARLETT2_INPUT_MIX_MAX * SCARLETT2_OUTPUT_MIX_MAX]; | |
243 | }; | |
244 | ||
245 | /*** Model-specific data ***/ | |
246 | ||
247 | static const struct scarlett2_device_info s6i6_gen2_info = { | |
248 | /* The first two analogue inputs can be switched between line | |
249 | * and instrument levels. | |
250 | */ | |
251 | .level_input_count = 2, | |
252 | ||
253 | /* The first two analogue inputs have an optional pad. */ | |
254 | .pad_input_count = 2, | |
255 | ||
256 | .line_out_descrs = { | |
257 | "Monitor L", | |
258 | "Monitor R", | |
259 | "Headphones L", | |
260 | "Headphones R", | |
261 | }, | |
262 | ||
263 | .ports = { | |
264 | { | |
265 | .id = 0x000, | |
266 | .num = { 1, 0, 8, 8, 8 }, | |
267 | .src_descr = "Off", | |
268 | .src_num_offset = 0, | |
269 | }, | |
270 | { | |
271 | .id = 0x080, | |
272 | .num = { 4, 4, 4, 4, 4 }, | |
273 | .src_descr = "Analogue %d", | |
274 | .src_num_offset = 1, | |
275 | .dst_descr = "Analogue Output %02d Playback" | |
276 | }, | |
277 | { | |
278 | .id = 0x180, | |
279 | .num = { 2, 2, 2, 2, 2 }, | |
280 | .src_descr = "S/PDIF %d", | |
281 | .src_num_offset = 1, | |
282 | .dst_descr = "S/PDIF Output %d Playback" | |
283 | }, | |
284 | { | |
285 | .id = 0x300, | |
286 | .num = { 10, 18, 18, 18, 18 }, | |
287 | .src_descr = "Mix %c", | |
288 | .src_num_offset = 65, | |
289 | .dst_descr = "Mixer Input %02d Capture" | |
290 | }, | |
291 | { | |
292 | .id = 0x600, | |
293 | .num = { 6, 6, 6, 6, 6 }, | |
294 | .src_descr = "PCM %d", | |
295 | .src_num_offset = 1, | |
296 | .dst_descr = "PCM %02d Capture" | |
297 | }, | |
298 | }, | |
299 | }; | |
300 | ||
301 | static const struct scarlett2_device_info s18i8_gen2_info = { | |
302 | /* The first two analogue inputs can be switched between line | |
303 | * and instrument levels. | |
304 | */ | |
305 | .level_input_count = 2, | |
306 | ||
307 | /* The first four analogue inputs have an optional pad. */ | |
308 | .pad_input_count = 4, | |
309 | ||
310 | .line_out_descrs = { | |
311 | "Monitor L", | |
312 | "Monitor R", | |
313 | "Headphones 1 L", | |
314 | "Headphones 1 R", | |
315 | "Headphones 2 L", | |
316 | "Headphones 2 R", | |
317 | }, | |
318 | ||
319 | .ports = { | |
320 | { | |
321 | .id = 0x000, | |
322 | .num = { 1, 0, 8, 8, 4 }, | |
323 | .src_descr = "Off", | |
324 | .src_num_offset = 0, | |
325 | }, | |
326 | { | |
327 | .id = 0x080, | |
328 | .num = { 8, 6, 6, 6, 6 }, | |
329 | .src_descr = "Analogue %d", | |
330 | .src_num_offset = 1, | |
331 | .dst_descr = "Analogue Output %02d Playback" | |
332 | }, | |
333 | { | |
334 | /* S/PDIF outputs aren't available at 192KHz | |
335 | * but are included in the USB mux I/O | |
336 | * assignment message anyway | |
337 | */ | |
338 | .id = 0x180, | |
339 | .num = { 2, 2, 2, 2, 2 }, | |
340 | .src_descr = "S/PDIF %d", | |
341 | .src_num_offset = 1, | |
342 | .dst_descr = "S/PDIF Output %d Playback" | |
343 | }, | |
344 | { | |
345 | .id = 0x200, | |
346 | .num = { 8, 0, 0, 0, 0 }, | |
347 | .src_descr = "ADAT %d", | |
348 | .src_num_offset = 1, | |
349 | }, | |
350 | { | |
351 | .id = 0x300, | |
352 | .num = { 10, 18, 18, 18, 18 }, | |
353 | .src_descr = "Mix %c", | |
354 | .src_num_offset = 65, | |
355 | .dst_descr = "Mixer Input %02d Capture" | |
356 | }, | |
357 | { | |
358 | .id = 0x600, | |
359 | .num = { 20, 18, 18, 14, 10 }, | |
360 | .src_descr = "PCM %d", | |
361 | .src_num_offset = 1, | |
362 | .dst_descr = "PCM %02d Capture" | |
363 | }, | |
364 | }, | |
365 | }; | |
366 | ||
367 | static const struct scarlett2_device_info s18i20_gen2_info = { | |
368 | /* The analogue line outputs on the 18i20 can be switched | |
369 | * between software and hardware volume control | |
370 | */ | |
371 | .line_out_hw_vol = 1, | |
372 | ||
373 | /* Mute and dim buttons */ | |
374 | .button_count = 2, | |
375 | ||
376 | .line_out_descrs = { | |
377 | "Monitor L", | |
378 | "Monitor R", | |
379 | NULL, | |
380 | NULL, | |
381 | NULL, | |
382 | NULL, | |
383 | "Headphones 1 L", | |
384 | "Headphones 1 R", | |
385 | "Headphones 2 L", | |
386 | "Headphones 2 R", | |
387 | }, | |
388 | ||
389 | .ports = { | |
390 | { | |
391 | .id = 0x000, | |
392 | .num = { 1, 0, 8, 8, 6 }, | |
393 | .src_descr = "Off", | |
394 | .src_num_offset = 0, | |
395 | }, | |
396 | { | |
397 | .id = 0x080, | |
398 | .num = { 8, 10, 10, 10, 10 }, | |
399 | .src_descr = "Analogue %d", | |
400 | .src_num_offset = 1, | |
401 | .dst_descr = "Analogue Output %02d Playback" | |
402 | }, | |
403 | { | |
404 | /* S/PDIF outputs aren't available at 192KHz | |
405 | * but are included in the USB mux I/O | |
406 | * assignment message anyway | |
407 | */ | |
408 | .id = 0x180, | |
409 | .num = { 2, 2, 2, 2, 2 }, | |
410 | .src_descr = "S/PDIF %d", | |
411 | .src_num_offset = 1, | |
412 | .dst_descr = "S/PDIF Output %d Playback" | |
413 | }, | |
414 | { | |
415 | .id = 0x200, | |
416 | .num = { 8, 8, 8, 4, 0 }, | |
417 | .src_descr = "ADAT %d", | |
418 | .src_num_offset = 1, | |
419 | .dst_descr = "ADAT Output %d Playback" | |
420 | }, | |
421 | { | |
422 | .id = 0x300, | |
423 | .num = { 10, 18, 18, 18, 18 }, | |
424 | .src_descr = "Mix %c", | |
425 | .src_num_offset = 65, | |
426 | .dst_descr = "Mixer Input %02d Capture" | |
427 | }, | |
428 | { | |
429 | .id = 0x600, | |
430 | .num = { 20, 18, 18, 14, 10 }, | |
431 | .src_descr = "PCM %d", | |
432 | .src_num_offset = 1, | |
433 | .dst_descr = "PCM %02d Capture" | |
434 | }, | |
435 | }, | |
436 | }; | |
437 | ||
438 | /* get the starting port index number for a given port type/direction */ | |
439 | static int scarlett2_get_port_start_num(const struct scarlett2_ports *ports, | |
440 | int direction, int port_type) | |
441 | { | |
442 | int i, num = 0; | |
443 | ||
444 | for (i = 0; i < port_type; i++) | |
445 | num += ports[i].num[direction]; | |
446 | ||
447 | return num; | |
448 | } | |
449 | ||
450 | /*** USB Interactions ***/ | |
451 | ||
452 | /* Vendor-Specific Interface, Endpoint, MaxPacketSize, Interval */ | |
453 | #define SCARLETT2_USB_VENDOR_SPECIFIC_INTERFACE 5 | |
454 | #define SCARLETT2_USB_INTERRUPT_ENDPOINT 4 | |
455 | #define SCARLETT2_USB_INTERRUPT_MAX_DATA 64 | |
456 | #define SCARLETT2_USB_INTERRUPT_INTERVAL 3 | |
457 | ||
458 | /* Interrupt flags for volume and mute/dim button changes */ | |
459 | #define SCARLETT2_USB_INTERRUPT_VOL_CHANGE 0x400000 | |
460 | #define SCARLETT2_USB_INTERRUPT_BUTTON_CHANGE 0x200000 | |
461 | ||
462 | /* Commands for sending/receiving requests/responses */ | |
463 | #define SCARLETT2_USB_VENDOR_SPECIFIC_CMD_REQ 2 | |
464 | #define SCARLETT2_USB_VENDOR_SPECIFIC_CMD_RESP 3 | |
465 | ||
466 | #define SCARLETT2_USB_INIT_SEQ 0x00000000 | |
467 | #define SCARLETT2_USB_GET_METER_LEVELS 0x00001001 | |
468 | #define SCARLETT2_USB_SET_MIX 0x00002002 | |
469 | #define SCARLETT2_USB_SET_MUX 0x00003002 | |
470 | #define SCARLETT2_USB_GET_DATA 0x00800000 | |
471 | #define SCARLETT2_USB_SET_DATA 0x00800001 | |
472 | #define SCARLETT2_USB_DATA_CMD 0x00800002 | |
473 | #define SCARLETT2_USB_CONFIG_SAVE 6 | |
474 | ||
475 | #define SCARLETT2_USB_VOLUME_STATUS_OFFSET 0x31 | |
476 | #define SCARLETT2_USB_METER_LEVELS_GET_MAGIC 1 | |
477 | ||
478 | /* volume status is read together (matches scarlett2_config_items[]) */ | |
479 | struct scarlett2_usb_volume_status { | |
480 | /* mute & dim buttons */ | |
481 | u8 buttons[SCARLETT2_BUTTON_MAX]; | |
482 | ||
483 | u8 pad1; | |
484 | ||
485 | /* software volume setting */ | |
486 | s16 sw_vol[SCARLETT2_ANALOGUE_MAX]; | |
487 | ||
488 | /* actual volume of output inc. dim (-18dB) */ | |
489 | s16 hw_vol[SCARLETT2_ANALOGUE_MAX]; | |
490 | ||
491 | u8 pad2[SCARLETT2_ANALOGUE_MAX]; | |
492 | ||
493 | /* sw (0) or hw (1) controlled */ | |
494 | u8 sw_hw_switch[SCARLETT2_ANALOGUE_MAX]; | |
495 | ||
496 | u8 pad3[6]; | |
497 | ||
498 | /* front panel volume knob */ | |
499 | s16 master_vol; | |
500 | } __packed; | |
501 | ||
502 | /* Configuration parameters that can be read and written */ | |
503 | enum { | |
504 | SCARLETT2_CONFIG_BUTTONS = 0, | |
505 | SCARLETT2_CONFIG_LINE_OUT_VOLUME = 1, | |
506 | SCARLETT2_CONFIG_SW_HW_SWITCH = 2, | |
507 | SCARLETT2_CONFIG_LEVEL_SWITCH = 3, | |
508 | SCARLETT2_CONFIG_PAD_SWITCH = 4, | |
509 | SCARLETT2_CONFIG_COUNT = 5 | |
510 | }; | |
511 | ||
512 | /* Location, size, and activation command number for the configuration | |
513 | * parameters | |
514 | */ | |
515 | struct scarlett2_config { | |
516 | u8 offset; | |
517 | u8 size; | |
518 | u8 activate; | |
519 | }; | |
520 | ||
521 | static const struct scarlett2_config | |
522 | scarlett2_config_items[SCARLETT2_CONFIG_COUNT] = { | |
523 | /* Mute/Dim Buttons */ | |
524 | { | |
525 | .offset = 0x31, | |
526 | .size = 1, | |
527 | .activate = 2 | |
528 | }, | |
529 | ||
530 | /* Line Out Volume */ | |
531 | { | |
532 | .offset = 0x34, | |
533 | .size = 2, | |
534 | .activate = 1 | |
535 | }, | |
536 | ||
537 | /* SW/HW Volume Switch */ | |
538 | { | |
539 | .offset = 0x66, | |
540 | .size = 1, | |
541 | .activate = 3 | |
542 | }, | |
543 | ||
544 | /* Level Switch */ | |
545 | { | |
546 | .offset = 0x7c, | |
547 | .size = 1, | |
548 | .activate = 7 | |
549 | }, | |
550 | ||
551 | /* Pad Switch */ | |
552 | { | |
553 | .offset = 0x84, | |
554 | .size = 1, | |
555 | .activate = 8 | |
556 | } | |
557 | }; | |
558 | ||
559 | /* proprietary request/response format */ | |
560 | struct scarlett2_usb_packet { | |
561 | u32 cmd; | |
562 | u16 size; | |
563 | u16 seq; | |
564 | u32 error; | |
565 | u32 pad; | |
566 | u8 data[]; | |
567 | }; | |
568 | ||
569 | #define SCARLETT2_USB_PACKET_LEN (sizeof(struct scarlett2_usb_packet)) | |
570 | ||
571 | static void scarlett2_fill_request_header(struct scarlett2_mixer_data *private, | |
572 | struct scarlett2_usb_packet *req, | |
573 | u32 cmd, u16 req_size) | |
574 | { | |
575 | /* sequence must go up by 1 for each request */ | |
576 | u16 seq = private->scarlett2_seq++; | |
577 | ||
578 | req->cmd = cpu_to_le32(cmd); | |
579 | req->size = cpu_to_le16(req_size); | |
580 | req->seq = cpu_to_le16(seq); | |
581 | req->error = 0; | |
582 | req->pad = 0; | |
583 | } | |
584 | ||
585 | /* Send a proprietary format request to the Scarlett interface */ | |
586 | static int scarlett2_usb( | |
587 | struct usb_mixer_interface *mixer, u32 cmd, | |
588 | void *req_data, u16 req_size, void *resp_data, u16 resp_size) | |
589 | { | |
590 | struct scarlett2_mixer_data *private = mixer->private_data; | |
591 | u16 req_buf_size = sizeof(struct scarlett2_usb_packet) + req_size; | |
592 | u16 resp_buf_size = sizeof(struct scarlett2_usb_packet) + resp_size; | |
593 | struct scarlett2_usb_packet *req = NULL, *resp = NULL; | |
594 | int err = 0; | |
595 | ||
596 | req = kmalloc(req_buf_size, GFP_KERNEL); | |
597 | if (!req) { | |
598 | err = -ENOMEM; | |
599 | goto error; | |
600 | } | |
601 | ||
602 | resp = kmalloc(resp_buf_size, GFP_KERNEL); | |
603 | if (!resp) { | |
604 | err = -ENOMEM; | |
605 | goto error; | |
606 | } | |
607 | ||
608 | mutex_lock(&private->usb_mutex); | |
609 | ||
610 | /* build request message and send it */ | |
611 | ||
612 | scarlett2_fill_request_header(private, req, cmd, req_size); | |
613 | ||
614 | if (req_size) | |
615 | memcpy(req->data, req_data, req_size); | |
616 | ||
617 | err = snd_usb_ctl_msg(mixer->chip->dev, | |
618 | usb_sndctrlpipe(mixer->chip->dev, 0), | |
619 | SCARLETT2_USB_VENDOR_SPECIFIC_CMD_REQ, | |
620 | USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT, | |
621 | 0, | |
622 | SCARLETT2_USB_VENDOR_SPECIFIC_INTERFACE, | |
623 | req, | |
624 | req_buf_size); | |
625 | ||
626 | if (err != req_buf_size) { | |
627 | usb_audio_err( | |
628 | mixer->chip, | |
629 | "Scarlett Gen 2 USB request result cmd %x was %d\n", | |
630 | cmd, err); | |
631 | err = -EINVAL; | |
632 | goto unlock; | |
633 | } | |
634 | ||
635 | /* send a second message to get the response */ | |
636 | ||
637 | err = snd_usb_ctl_msg(mixer->chip->dev, | |
638 | usb_sndctrlpipe(mixer->chip->dev, 0), | |
639 | SCARLETT2_USB_VENDOR_SPECIFIC_CMD_RESP, | |
640 | USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN, | |
641 | 0, | |
642 | SCARLETT2_USB_VENDOR_SPECIFIC_INTERFACE, | |
643 | resp, | |
644 | resp_buf_size); | |
645 | ||
646 | /* validate the response */ | |
647 | ||
648 | if (err != resp_buf_size) { | |
649 | usb_audio_err( | |
650 | mixer->chip, | |
651 | "Scarlett Gen 2 USB response result cmd %x was %d\n", | |
652 | cmd, err); | |
653 | err = -EINVAL; | |
654 | goto unlock; | |
655 | } | |
656 | ||
657 | if (resp->cmd != req->cmd || | |
658 | resp->seq != req->seq || | |
659 | resp_size != le16_to_cpu(resp->size) || | |
660 | resp->error || | |
661 | resp->pad) { | |
662 | usb_audio_err( | |
663 | mixer->chip, | |
664 | "Scarlett Gen 2 USB invalid response; " | |
665 | "cmd tx/rx %d/%d seq %d/%d size %d/%d " | |
666 | "error %d pad %d\n", | |
667 | le16_to_cpu(req->cmd), le16_to_cpu(resp->cmd), | |
668 | le16_to_cpu(req->seq), le16_to_cpu(resp->seq), | |
669 | resp_size, le16_to_cpu(resp->size), | |
670 | le16_to_cpu(resp->error), | |
671 | le16_to_cpu(resp->pad)); | |
672 | err = -EINVAL; | |
673 | goto unlock; | |
674 | } | |
675 | ||
676 | if (resp_size > 0) | |
677 | memcpy(resp_data, resp->data, resp_size); | |
678 | ||
679 | unlock: | |
680 | mutex_unlock(&private->usb_mutex); | |
681 | error: | |
682 | kfree(req); | |
683 | kfree(resp); | |
684 | return err; | |
685 | } | |
686 | ||
687 | /* Send SCARLETT2_USB_DATA_CMD SCARLETT2_USB_CONFIG_SAVE */ | |
688 | static void scarlett2_config_save(struct usb_mixer_interface *mixer) | |
689 | { | |
690 | u32 req = cpu_to_le32(SCARLETT2_USB_CONFIG_SAVE); | |
691 | ||
692 | scarlett2_usb(mixer, SCARLETT2_USB_DATA_CMD, | |
693 | &req, sizeof(u32), | |
694 | NULL, 0); | |
695 | } | |
696 | ||
697 | /* Delayed work to save config */ | |
698 | static void scarlett2_config_save_work(struct work_struct *work) | |
699 | { | |
700 | struct scarlett2_mixer_data *private = | |
701 | container_of(work, struct scarlett2_mixer_data, work.work); | |
702 | ||
703 | scarlett2_config_save(private->mixer); | |
704 | } | |
705 | ||
706 | /* Send a USB message to set a configuration parameter (volume level, | |
707 | * sw/hw volume switch, line/inst level switch, or pad switch) | |
708 | */ | |
709 | static int scarlett2_usb_set_config( | |
710 | struct usb_mixer_interface *mixer, | |
711 | int config_item_num, int index, int value) | |
712 | { | |
713 | const struct scarlett2_config config_item = | |
714 | scarlett2_config_items[config_item_num]; | |
715 | struct { | |
716 | u32 offset; | |
717 | u32 bytes; | |
718 | s32 value; | |
719 | } __packed req; | |
720 | u32 req2; | |
721 | int err; | |
722 | struct scarlett2_mixer_data *private = mixer->private_data; | |
723 | ||
724 | /* Cancel any pending NVRAM save */ | |
725 | cancel_delayed_work_sync(&private->work); | |
726 | ||
727 | /* Send the configuration parameter data */ | |
728 | req.offset = cpu_to_le32(config_item.offset + index * config_item.size); | |
729 | req.bytes = cpu_to_le32(config_item.size); | |
730 | req.value = cpu_to_le32(value); | |
731 | err = scarlett2_usb(mixer, SCARLETT2_USB_SET_DATA, | |
732 | &req, sizeof(u32) * 2 + config_item.size, | |
733 | NULL, 0); | |
734 | if (err < 0) | |
735 | return err; | |
736 | ||
737 | /* Activate the change */ | |
738 | req2 = cpu_to_le32(config_item.activate); | |
739 | err = scarlett2_usb(mixer, SCARLETT2_USB_DATA_CMD, | |
740 | &req2, sizeof(req2), NULL, 0); | |
741 | if (err < 0) | |
742 | return err; | |
743 | ||
744 | /* Schedule the change to be written to NVRAM */ | |
745 | schedule_delayed_work(&private->work, msecs_to_jiffies(2000)); | |
746 | ||
747 | return 0; | |
748 | } | |
749 | ||
750 | /* Send a USB message to get data; result placed in *buf */ | |
751 | static int scarlett2_usb_get( | |
752 | struct usb_mixer_interface *mixer, | |
753 | int offset, void *buf, int size) | |
754 | { | |
755 | struct { | |
756 | u32 offset; | |
757 | u32 size; | |
758 | } __packed req; | |
759 | ||
760 | req.offset = cpu_to_le32(offset); | |
761 | req.size = cpu_to_le32(size); | |
762 | return scarlett2_usb(mixer, SCARLETT2_USB_GET_DATA, | |
763 | &req, sizeof(req), buf, size); | |
764 | } | |
765 | ||
766 | /* Send a USB message to get configuration parameters; result placed in *buf */ | |
767 | static int scarlett2_usb_get_config( | |
768 | struct usb_mixer_interface *mixer, | |
769 | int config_item_num, int count, void *buf) | |
770 | { | |
771 | const struct scarlett2_config config_item = | |
772 | scarlett2_config_items[config_item_num]; | |
773 | int size = config_item.size * count; | |
774 | ||
775 | return scarlett2_usb_get(mixer, config_item.offset, buf, size); | |
776 | } | |
777 | ||
778 | /* Send a USB message to get volume status; result placed in *buf */ | |
779 | static int scarlett2_usb_get_volume_status( | |
780 | struct usb_mixer_interface *mixer, | |
781 | struct scarlett2_usb_volume_status *buf) | |
782 | { | |
783 | return scarlett2_usb_get(mixer, SCARLETT2_USB_VOLUME_STATUS_OFFSET, | |
784 | buf, sizeof(*buf)); | |
785 | } | |
786 | ||
787 | /* Send a USB message to set the volumes for all inputs of one mix | |
788 | * (values obtained from private->mix[]) | |
789 | */ | |
790 | static int scarlett2_usb_set_mix(struct usb_mixer_interface *mixer, | |
791 | int mix_num) | |
792 | { | |
793 | struct scarlett2_mixer_data *private = mixer->private_data; | |
794 | const struct scarlett2_device_info *info = private->info; | |
795 | ||
796 | struct { | |
797 | u16 mix_num; | |
798 | u16 data[SCARLETT2_INPUT_MIX_MAX]; | |
799 | } __packed req; | |
800 | ||
801 | int i, j; | |
802 | int num_mixer_in = | |
803 | info->ports[SCARLETT2_PORT_TYPE_MIX].num[SCARLETT2_PORT_OUT]; | |
804 | ||
805 | req.mix_num = cpu_to_le16(mix_num); | |
806 | ||
807 | for (i = 0, j = mix_num * num_mixer_in; i < num_mixer_in; i++, j++) | |
808 | req.data[i] = cpu_to_le16( | |
809 | scarlett2_mixer_values[private->mix[j]] | |
810 | ); | |
811 | ||
812 | return scarlett2_usb(mixer, SCARLETT2_USB_SET_MIX, | |
813 | &req, (num_mixer_in + 1) * sizeof(u16), | |
814 | NULL, 0); | |
815 | } | |
816 | ||
817 | /* Convert a port number index (per info->ports) to a hardware ID */ | |
818 | static u32 scarlett2_mux_src_num_to_id(const struct scarlett2_ports *ports, | |
819 | int num) | |
820 | { | |
821 | int port_type; | |
822 | ||
823 | for (port_type = 0; | |
824 | port_type < SCARLETT2_PORT_TYPE_COUNT; | |
825 | port_type++) { | |
826 | if (num < ports[port_type].num[SCARLETT2_PORT_IN]) | |
827 | return ports[port_type].id | num; | |
828 | num -= ports[port_type].num[SCARLETT2_PORT_IN]; | |
829 | } | |
830 | ||
831 | /* Oops */ | |
832 | return 0; | |
833 | } | |
834 | ||
835 | /* Send USB messages to set mux inputs */ | |
836 | static int scarlett2_usb_set_mux(struct usb_mixer_interface *mixer) | |
837 | { | |
838 | struct scarlett2_mixer_data *private = mixer->private_data; | |
839 | const struct scarlett2_device_info *info = private->info; | |
840 | const struct scarlett2_ports *ports = info->ports; | |
841 | int rate, port_dir_rate; | |
842 | ||
843 | static const int assignment_order[SCARLETT2_PORT_TYPE_COUNT] = { | |
844 | SCARLETT2_PORT_TYPE_PCM, | |
845 | SCARLETT2_PORT_TYPE_ANALOGUE, | |
846 | SCARLETT2_PORT_TYPE_SPDIF, | |
847 | SCARLETT2_PORT_TYPE_ADAT, | |
848 | SCARLETT2_PORT_TYPE_MIX, | |
849 | SCARLETT2_PORT_TYPE_NONE, | |
850 | }; | |
851 | ||
852 | struct { | |
853 | u16 pad; | |
854 | u16 num; | |
855 | u32 data[SCARLETT2_MUX_MAX]; | |
856 | } __packed req; | |
857 | ||
858 | req.pad = 0; | |
859 | ||
860 | /* mux settings for each rate */ | |
861 | for (rate = 0, port_dir_rate = SCARLETT2_PORT_OUT_44; | |
862 | port_dir_rate <= SCARLETT2_PORT_OUT_176; | |
863 | rate++, port_dir_rate++) { | |
864 | int order_num, i, err; | |
865 | ||
866 | req.num = cpu_to_le16(rate); | |
867 | ||
868 | for (order_num = 0, i = 0; | |
869 | order_num < SCARLETT2_PORT_TYPE_COUNT; | |
870 | order_num++) { | |
871 | int port_type = assignment_order[order_num]; | |
872 | int j = scarlett2_get_port_start_num(ports, | |
873 | SCARLETT2_PORT_OUT, | |
874 | port_type); | |
875 | int port_id = ports[port_type].id; | |
876 | int channel; | |
877 | ||
878 | for (channel = 0; | |
879 | channel < ports[port_type].num[port_dir_rate]; | |
880 | channel++, i++, j++) | |
881 | /* lower 12 bits for the destination and | |
882 | * next 12 bits for the source | |
883 | */ | |
884 | req.data[i] = !port_id | |
885 | ? 0 | |
886 | : cpu_to_le32( | |
887 | port_id | | |
888 | channel | | |
889 | scarlett2_mux_src_num_to_id( | |
890 | ports, private->mux[j] | |
891 | ) << 12 | |
892 | ); | |
893 | ||
894 | /* skip private->mux[j] entries not output */ | |
895 | j += ports[port_type].num[SCARLETT2_PORT_OUT] - | |
896 | ports[port_type].num[port_dir_rate]; | |
897 | } | |
898 | ||
899 | err = scarlett2_usb(mixer, SCARLETT2_USB_SET_MUX, | |
900 | &req, (i + 1) * sizeof(u32), | |
901 | NULL, 0); | |
902 | if (err < 0) | |
903 | return err; | |
904 | } | |
905 | ||
906 | return 0; | |
907 | } | |
908 | ||
909 | /* Send USB message to get meter levels */ | |
910 | static int scarlett2_usb_get_meter_levels(struct usb_mixer_interface *mixer, | |
911 | u16 *levels) | |
912 | { | |
913 | struct { | |
914 | u16 pad; | |
915 | u16 num_meters; | |
916 | u32 magic; | |
917 | } __packed req; | |
918 | u32 resp[SCARLETT2_NUM_METERS]; | |
919 | int i, err; | |
920 | ||
921 | req.pad = 0; | |
922 | req.num_meters = cpu_to_le16(SCARLETT2_NUM_METERS); | |
923 | req.magic = cpu_to_le32(SCARLETT2_USB_METER_LEVELS_GET_MAGIC); | |
924 | err = scarlett2_usb(mixer, SCARLETT2_USB_GET_METER_LEVELS, | |
925 | &req, sizeof(req), resp, sizeof(resp)); | |
926 | if (err < 0) | |
927 | return err; | |
928 | ||
929 | /* copy, convert to u16 */ | |
930 | for (i = 0; i < SCARLETT2_NUM_METERS; i++) | |
931 | levels[i] = resp[i]; | |
932 | ||
933 | return 0; | |
934 | } | |
935 | ||
936 | /*** Control Functions ***/ | |
937 | ||
938 | /* helper function to create a new control */ | |
939 | static int scarlett2_add_new_ctl(struct usb_mixer_interface *mixer, | |
940 | const struct snd_kcontrol_new *ncontrol, | |
941 | int index, int channels, const char *name, | |
942 | struct snd_kcontrol **kctl_return) | |
943 | { | |
944 | struct snd_kcontrol *kctl; | |
945 | struct usb_mixer_elem_info *elem; | |
946 | int err; | |
947 | ||
948 | elem = kzalloc(sizeof(*elem), GFP_KERNEL); | |
949 | if (!elem) | |
950 | return -ENOMEM; | |
951 | ||
952 | elem->head.mixer = mixer; | |
953 | elem->control = index; | |
954 | elem->head.id = index; | |
955 | elem->channels = channels; | |
956 | ||
957 | kctl = snd_ctl_new1(ncontrol, elem); | |
958 | if (!kctl) { | |
959 | kfree(elem); | |
960 | return -ENOMEM; | |
961 | } | |
962 | kctl->private_free = snd_usb_mixer_elem_free; | |
963 | ||
964 | strlcpy(kctl->id.name, name, sizeof(kctl->id.name)); | |
965 | ||
966 | err = snd_usb_mixer_add_control(&elem->head, kctl); | |
967 | if (err < 0) | |
968 | return err; | |
969 | ||
970 | if (kctl_return) | |
971 | *kctl_return = kctl; | |
972 | ||
973 | return 0; | |
974 | } | |
975 | ||
976 | /*** Analogue Line Out Volume Controls ***/ | |
977 | ||
978 | /* Update hardware volume controls after receiving notification that | |
979 | * they have changed | |
980 | */ | |
981 | static int scarlett2_update_volumes(struct usb_mixer_interface *mixer) | |
982 | { | |
983 | struct scarlett2_mixer_data *private = mixer->private_data; | |
984 | const struct scarlett2_ports *ports = private->info->ports; | |
985 | struct scarlett2_usb_volume_status volume_status; | |
986 | int num_line_out = | |
987 | ports[SCARLETT2_PORT_TYPE_ANALOGUE].num[SCARLETT2_PORT_OUT]; | |
988 | int err, i; | |
989 | ||
990 | private->vol_updated = 0; | |
991 | ||
992 | err = scarlett2_usb_get_volume_status(mixer, &volume_status); | |
993 | if (err < 0) | |
994 | return err; | |
995 | ||
996 | private->master_vol = clamp( | |
997 | volume_status.master_vol + SCARLETT2_VOLUME_BIAS, | |
998 | 0, SCARLETT2_VOLUME_BIAS); | |
999 | ||
1000 | for (i = 0; i < num_line_out; i++) { | |
1001 | if (private->vol_sw_hw_switch[i]) | |
1002 | private->vol[i] = private->master_vol; | |
1003 | } | |
1004 | ||
1005 | for (i = 0; i < private->info->button_count; i++) | |
1006 | private->buttons[i] = !!volume_status.buttons[i]; | |
1007 | ||
1008 | return 0; | |
1009 | } | |
1010 | ||
1011 | static int scarlett2_volume_ctl_info(struct snd_kcontrol *kctl, | |
1012 | struct snd_ctl_elem_info *uinfo) | |
1013 | { | |
1014 | struct usb_mixer_elem_info *elem = kctl->private_data; | |
1015 | ||
1016 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; | |
1017 | uinfo->count = elem->channels; | |
1018 | uinfo->value.integer.min = 0; | |
1019 | uinfo->value.integer.max = SCARLETT2_VOLUME_BIAS; | |
1020 | uinfo->value.integer.step = 1; | |
1021 | return 0; | |
1022 | } | |
1023 | ||
1024 | static int scarlett2_master_volume_ctl_get(struct snd_kcontrol *kctl, | |
1025 | struct snd_ctl_elem_value *ucontrol) | |
1026 | { | |
1027 | struct usb_mixer_elem_info *elem = kctl->private_data; | |
1028 | struct usb_mixer_interface *mixer = elem->head.mixer; | |
1029 | struct scarlett2_mixer_data *private = mixer->private_data; | |
1030 | ||
1031 | if (private->vol_updated) { | |
1032 | mutex_lock(&private->data_mutex); | |
1033 | scarlett2_update_volumes(mixer); | |
1034 | mutex_unlock(&private->data_mutex); | |
1035 | } | |
1036 | ||
1037 | ucontrol->value.integer.value[0] = private->master_vol; | |
1038 | return 0; | |
1039 | } | |
1040 | ||
1041 | static int scarlett2_volume_ctl_get(struct snd_kcontrol *kctl, | |
1042 | struct snd_ctl_elem_value *ucontrol) | |
1043 | { | |
1044 | struct usb_mixer_elem_info *elem = kctl->private_data; | |
1045 | struct usb_mixer_interface *mixer = elem->head.mixer; | |
1046 | struct scarlett2_mixer_data *private = mixer->private_data; | |
1047 | int index = elem->control; | |
1048 | ||
1049 | if (private->vol_updated) { | |
1050 | mutex_lock(&private->data_mutex); | |
1051 | scarlett2_update_volumes(mixer); | |
1052 | mutex_unlock(&private->data_mutex); | |
1053 | } | |
1054 | ||
1055 | ucontrol->value.integer.value[0] = private->vol[index]; | |
1056 | return 0; | |
1057 | } | |
1058 | ||
1059 | static int scarlett2_volume_ctl_put(struct snd_kcontrol *kctl, | |
1060 | struct snd_ctl_elem_value *ucontrol) | |
1061 | { | |
1062 | struct usb_mixer_elem_info *elem = kctl->private_data; | |
1063 | struct usb_mixer_interface *mixer = elem->head.mixer; | |
1064 | struct scarlett2_mixer_data *private = mixer->private_data; | |
1065 | int index = elem->control; | |
1066 | int oval, val, err = 0; | |
1067 | ||
1068 | mutex_lock(&private->data_mutex); | |
1069 | ||
1070 | oval = private->vol[index]; | |
1071 | val = ucontrol->value.integer.value[0]; | |
1072 | ||
1073 | if (oval == val) | |
1074 | goto unlock; | |
1075 | ||
1076 | private->vol[index] = val; | |
1077 | err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_LINE_OUT_VOLUME, | |
1078 | index, val - SCARLETT2_VOLUME_BIAS); | |
1079 | if (err == 0) | |
1080 | err = 1; | |
1081 | ||
1082 | unlock: | |
1083 | mutex_unlock(&private->data_mutex); | |
1084 | return err; | |
1085 | } | |
1086 | ||
1087 | static const DECLARE_TLV_DB_MINMAX( | |
1088 | db_scale_scarlett2_gain, -SCARLETT2_VOLUME_BIAS * 100, 0 | |
1089 | ); | |
1090 | ||
1091 | static const struct snd_kcontrol_new scarlett2_master_volume_ctl = { | |
1092 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, | |
1093 | .access = SNDRV_CTL_ELEM_ACCESS_READ | | |
1094 | SNDRV_CTL_ELEM_ACCESS_TLV_READ, | |
1095 | .name = "", | |
1096 | .info = scarlett2_volume_ctl_info, | |
1097 | .get = scarlett2_master_volume_ctl_get, | |
1098 | .private_value = 0, /* max value */ | |
1099 | .tlv = { .p = db_scale_scarlett2_gain } | |
1100 | }; | |
1101 | ||
1102 | static const struct snd_kcontrol_new scarlett2_line_out_volume_ctl = { | |
1103 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, | |
1104 | .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | | |
1105 | SNDRV_CTL_ELEM_ACCESS_TLV_READ, | |
1106 | .name = "", | |
1107 | .info = scarlett2_volume_ctl_info, | |
1108 | .get = scarlett2_volume_ctl_get, | |
1109 | .put = scarlett2_volume_ctl_put, | |
1110 | .private_value = 0, /* max value */ | |
1111 | .tlv = { .p = db_scale_scarlett2_gain } | |
1112 | }; | |
1113 | ||
1114 | /*** HW/SW Volume Switch Controls ***/ | |
1115 | ||
1116 | static int scarlett2_sw_hw_enum_ctl_info(struct snd_kcontrol *kctl, | |
1117 | struct snd_ctl_elem_info *uinfo) | |
1118 | { | |
1119 | static const char *const values[2] = { | |
1120 | "SW", "HW" | |
1121 | }; | |
1122 | ||
1123 | return snd_ctl_enum_info(uinfo, 1, 2, values); | |
1124 | } | |
1125 | ||
1126 | static int scarlett2_sw_hw_enum_ctl_get(struct snd_kcontrol *kctl, | |
1127 | struct snd_ctl_elem_value *ucontrol) | |
1128 | { | |
1129 | struct usb_mixer_elem_info *elem = kctl->private_data; | |
1130 | struct scarlett2_mixer_data *private = elem->head.mixer->private_data; | |
1131 | ||
1132 | ucontrol->value.enumerated.item[0] = | |
1133 | private->vol_sw_hw_switch[elem->control]; | |
1134 | return 0; | |
1135 | } | |
1136 | ||
1137 | static int scarlett2_sw_hw_enum_ctl_put(struct snd_kcontrol *kctl, | |
1138 | struct snd_ctl_elem_value *ucontrol) | |
1139 | { | |
1140 | struct usb_mixer_elem_info *elem = kctl->private_data; | |
1141 | struct usb_mixer_interface *mixer = elem->head.mixer; | |
1142 | struct scarlett2_mixer_data *private = mixer->private_data; | |
1143 | ||
1144 | int index = elem->control; | |
1145 | int oval, val, err = 0; | |
1146 | ||
1147 | mutex_lock(&private->data_mutex); | |
1148 | ||
1149 | oval = private->vol_sw_hw_switch[index]; | |
1150 | val = !!ucontrol->value.integer.value[0]; | |
1151 | ||
1152 | if (oval == val) | |
1153 | goto unlock; | |
1154 | ||
1155 | private->vol_sw_hw_switch[index] = val; | |
1156 | ||
1157 | /* Change access mode to RO (hardware controlled volume) | |
1158 | * or RW (software controlled volume) | |
1159 | */ | |
1160 | if (val) | |
1161 | private->vol_ctls[index]->vd[0].access &= | |
1162 | ~SNDRV_CTL_ELEM_ACCESS_WRITE; | |
1163 | else | |
1164 | private->vol_ctls[index]->vd[0].access |= | |
1165 | SNDRV_CTL_ELEM_ACCESS_WRITE; | |
1166 | ||
1167 | /* Reset volume to master volume */ | |
1168 | private->vol[index] = private->master_vol; | |
1169 | ||
1170 | /* Set SW volume to current HW volume */ | |
1171 | err = scarlett2_usb_set_config( | |
1172 | mixer, SCARLETT2_CONFIG_LINE_OUT_VOLUME, | |
1173 | index, private->master_vol - SCARLETT2_VOLUME_BIAS); | |
1174 | if (err < 0) | |
1175 | goto unlock; | |
1176 | ||
1177 | /* Notify of RO/RW change */ | |
1178 | snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_INFO, | |
1179 | &private->vol_ctls[index]->id); | |
1180 | ||
1181 | /* Send SW/HW switch change to the device */ | |
1182 | err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_SW_HW_SWITCH, | |
1183 | index, val); | |
1184 | ||
1185 | unlock: | |
1186 | mutex_unlock(&private->data_mutex); | |
1187 | return err; | |
1188 | } | |
1189 | ||
1190 | static const struct snd_kcontrol_new scarlett2_sw_hw_enum_ctl = { | |
1191 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, | |
1192 | .name = "", | |
1193 | .info = scarlett2_sw_hw_enum_ctl_info, | |
1194 | .get = scarlett2_sw_hw_enum_ctl_get, | |
1195 | .put = scarlett2_sw_hw_enum_ctl_put, | |
1196 | }; | |
1197 | ||
1198 | /*** Line Level/Instrument Level Switch Controls ***/ | |
1199 | ||
1200 | static int scarlett2_level_enum_ctl_info(struct snd_kcontrol *kctl, | |
1201 | struct snd_ctl_elem_info *uinfo) | |
1202 | { | |
1203 | static const char *const values[2] = { | |
1204 | "Line", "Inst" | |
1205 | }; | |
1206 | ||
1207 | return snd_ctl_enum_info(uinfo, 1, 2, values); | |
1208 | } | |
1209 | ||
1210 | static int scarlett2_level_enum_ctl_get(struct snd_kcontrol *kctl, | |
1211 | struct snd_ctl_elem_value *ucontrol) | |
1212 | { | |
1213 | struct usb_mixer_elem_info *elem = kctl->private_data; | |
1214 | struct scarlett2_mixer_data *private = elem->head.mixer->private_data; | |
1215 | ||
1216 | ucontrol->value.enumerated.item[0] = | |
1217 | private->level_switch[elem->control]; | |
1218 | return 0; | |
1219 | } | |
1220 | ||
1221 | static int scarlett2_level_enum_ctl_put(struct snd_kcontrol *kctl, | |
1222 | struct snd_ctl_elem_value *ucontrol) | |
1223 | { | |
1224 | struct usb_mixer_elem_info *elem = kctl->private_data; | |
1225 | struct usb_mixer_interface *mixer = elem->head.mixer; | |
1226 | struct scarlett2_mixer_data *private = mixer->private_data; | |
1227 | ||
1228 | int index = elem->control; | |
1229 | int oval, val, err = 0; | |
1230 | ||
1231 | mutex_lock(&private->data_mutex); | |
1232 | ||
1233 | oval = private->level_switch[index]; | |
1234 | val = !!ucontrol->value.integer.value[0]; | |
1235 | ||
1236 | if (oval == val) | |
1237 | goto unlock; | |
1238 | ||
1239 | private->level_switch[index] = val; | |
1240 | ||
1241 | /* Send switch change to the device */ | |
1242 | err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_LEVEL_SWITCH, | |
1243 | index, val); | |
1244 | ||
1245 | unlock: | |
1246 | mutex_unlock(&private->data_mutex); | |
1247 | return err; | |
1248 | } | |
1249 | ||
1250 | static const struct snd_kcontrol_new scarlett2_level_enum_ctl = { | |
1251 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, | |
1252 | .name = "", | |
1253 | .info = scarlett2_level_enum_ctl_info, | |
1254 | .get = scarlett2_level_enum_ctl_get, | |
1255 | .put = scarlett2_level_enum_ctl_put, | |
1256 | }; | |
1257 | ||
1258 | /*** Pad Switch Controls ***/ | |
1259 | ||
1260 | static int scarlett2_pad_ctl_get(struct snd_kcontrol *kctl, | |
1261 | struct snd_ctl_elem_value *ucontrol) | |
1262 | { | |
1263 | struct usb_mixer_elem_info *elem = kctl->private_data; | |
1264 | struct scarlett2_mixer_data *private = elem->head.mixer->private_data; | |
1265 | ||
1266 | ucontrol->value.enumerated.item[0] = | |
1267 | private->pad_switch[elem->control]; | |
1268 | return 0; | |
1269 | } | |
1270 | ||
1271 | static int scarlett2_pad_ctl_put(struct snd_kcontrol *kctl, | |
1272 | struct snd_ctl_elem_value *ucontrol) | |
1273 | { | |
1274 | struct usb_mixer_elem_info *elem = kctl->private_data; | |
1275 | struct usb_mixer_interface *mixer = elem->head.mixer; | |
1276 | struct scarlett2_mixer_data *private = mixer->private_data; | |
1277 | ||
1278 | int index = elem->control; | |
1279 | int oval, val, err = 0; | |
1280 | ||
1281 | mutex_lock(&private->data_mutex); | |
1282 | ||
1283 | oval = private->pad_switch[index]; | |
1284 | val = !!ucontrol->value.integer.value[0]; | |
1285 | ||
1286 | if (oval == val) | |
1287 | goto unlock; | |
1288 | ||
1289 | private->pad_switch[index] = val; | |
1290 | ||
1291 | /* Send switch change to the device */ | |
1292 | err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_PAD_SWITCH, | |
1293 | index, val); | |
1294 | ||
1295 | unlock: | |
1296 | mutex_unlock(&private->data_mutex); | |
1297 | return err; | |
1298 | } | |
1299 | ||
1300 | static const struct snd_kcontrol_new scarlett2_pad_ctl = { | |
1301 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, | |
1302 | .name = "", | |
1303 | .info = snd_ctl_boolean_mono_info, | |
1304 | .get = scarlett2_pad_ctl_get, | |
1305 | .put = scarlett2_pad_ctl_put, | |
1306 | }; | |
1307 | ||
1308 | /*** Mute/Dim Controls ***/ | |
1309 | ||
1310 | static int scarlett2_button_ctl_get(struct snd_kcontrol *kctl, | |
1311 | struct snd_ctl_elem_value *ucontrol) | |
1312 | { | |
1313 | struct usb_mixer_elem_info *elem = kctl->private_data; | |
1314 | struct usb_mixer_interface *mixer = elem->head.mixer; | |
1315 | struct scarlett2_mixer_data *private = mixer->private_data; | |
1316 | ||
1317 | if (private->vol_updated) { | |
1318 | mutex_lock(&private->data_mutex); | |
1319 | scarlett2_update_volumes(mixer); | |
1320 | mutex_unlock(&private->data_mutex); | |
1321 | } | |
1322 | ||
1323 | ucontrol->value.enumerated.item[0] = private->buttons[elem->control]; | |
1324 | return 0; | |
1325 | } | |
1326 | ||
1327 | static int scarlett2_button_ctl_put(struct snd_kcontrol *kctl, | |
1328 | struct snd_ctl_elem_value *ucontrol) | |
1329 | { | |
1330 | struct usb_mixer_elem_info *elem = kctl->private_data; | |
1331 | struct usb_mixer_interface *mixer = elem->head.mixer; | |
1332 | struct scarlett2_mixer_data *private = mixer->private_data; | |
1333 | ||
1334 | int index = elem->control; | |
1335 | int oval, val, err = 0; | |
1336 | ||
1337 | mutex_lock(&private->data_mutex); | |
1338 | ||
1339 | oval = private->buttons[index]; | |
1340 | val = !!ucontrol->value.integer.value[0]; | |
1341 | ||
1342 | if (oval == val) | |
1343 | goto unlock; | |
1344 | ||
1345 | private->buttons[index] = val; | |
1346 | ||
1347 | /* Send switch change to the device */ | |
1348 | err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_BUTTONS, | |
1349 | index, val); | |
1350 | ||
1351 | unlock: | |
1352 | mutex_unlock(&private->data_mutex); | |
1353 | return err; | |
1354 | } | |
1355 | ||
1356 | static const struct snd_kcontrol_new scarlett2_button_ctl = { | |
1357 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, | |
1358 | .name = "", | |
1359 | .info = snd_ctl_boolean_mono_info, | |
1360 | .get = scarlett2_button_ctl_get, | |
1361 | .put = scarlett2_button_ctl_put | |
1362 | }; | |
1363 | ||
1364 | /*** Create the analogue output controls ***/ | |
1365 | ||
1366 | static int scarlett2_add_line_out_ctls(struct usb_mixer_interface *mixer) | |
1367 | { | |
1368 | struct scarlett2_mixer_data *private = mixer->private_data; | |
1369 | const struct scarlett2_device_info *info = private->info; | |
1370 | const struct scarlett2_ports *ports = info->ports; | |
1371 | int num_line_out = | |
1372 | ports[SCARLETT2_PORT_TYPE_ANALOGUE].num[SCARLETT2_PORT_OUT]; | |
1373 | int err, i; | |
1374 | char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN]; | |
1375 | ||
1376 | /* Add R/O HW volume control */ | |
1377 | if (info->line_out_hw_vol) { | |
1378 | snprintf(s, sizeof(s), "Master HW Playback Volume"); | |
1379 | err = scarlett2_add_new_ctl(mixer, | |
1380 | &scarlett2_master_volume_ctl, | |
1381 | 0, 1, s, &private->master_vol_ctl); | |
1382 | if (err < 0) | |
1383 | return err; | |
1384 | } | |
1385 | ||
1386 | /* Add volume controls */ | |
1387 | for (i = 0; i < num_line_out; i++) { | |
1388 | ||
1389 | /* Fader */ | |
1390 | if (info->line_out_descrs[i]) | |
1391 | snprintf(s, sizeof(s), | |
1392 | "Line %02d (%s) Playback Volume", | |
1393 | i + 1, info->line_out_descrs[i]); | |
1394 | else | |
1395 | snprintf(s, sizeof(s), | |
1396 | "Line %02d Playback Volume", | |
1397 | i + 1); | |
1398 | err = scarlett2_add_new_ctl(mixer, | |
1399 | &scarlett2_line_out_volume_ctl, | |
1400 | i, 1, s, &private->vol_ctls[i]); | |
1401 | if (err < 0) | |
1402 | return err; | |
1403 | ||
1404 | /* Make the fader read-only if the SW/HW switch is set to HW */ | |
1405 | if (private->vol_sw_hw_switch[i]) | |
1406 | private->vol_ctls[i]->vd[0].access &= | |
1407 | ~SNDRV_CTL_ELEM_ACCESS_WRITE; | |
1408 | ||
1409 | /* SW/HW Switch */ | |
1410 | if (info->line_out_hw_vol) { | |
1411 | snprintf(s, sizeof(s), | |
1412 | "Line Out %02d Volume Control Playback Enum", | |
1413 | i + 1); | |
1414 | err = scarlett2_add_new_ctl(mixer, | |
1415 | &scarlett2_sw_hw_enum_ctl, | |
1416 | i, 1, s, NULL); | |
1417 | if (err < 0) | |
1418 | return err; | |
1419 | } | |
1420 | } | |
1421 | ||
1422 | /* Add HW button controls */ | |
1423 | for (i = 0; i < private->info->button_count; i++) { | |
1424 | err = scarlett2_add_new_ctl(mixer, &scarlett2_button_ctl, | |
1425 | i, 1, scarlett2_button_names[i], | |
1426 | &private->button_ctls[i]); | |
1427 | if (err < 0) | |
1428 | return err; | |
1429 | } | |
1430 | ||
1431 | return 0; | |
1432 | } | |
1433 | ||
1434 | /*** Create the analogue input controls ***/ | |
1435 | ||
1436 | static int scarlett2_add_line_in_ctls(struct usb_mixer_interface *mixer) | |
1437 | { | |
1438 | struct scarlett2_mixer_data *private = mixer->private_data; | |
1439 | const struct scarlett2_device_info *info = private->info; | |
1440 | int err, i; | |
1441 | char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN]; | |
1442 | ||
1443 | /* Add input level (line/inst) controls */ | |
1444 | for (i = 0; i < info->level_input_count; i++) { | |
1445 | snprintf(s, sizeof(s), "Line In %d Level Capture Enum", i + 1); | |
1446 | err = scarlett2_add_new_ctl(mixer, &scarlett2_level_enum_ctl, | |
1447 | i, 1, s, NULL); | |
1448 | if (err < 0) | |
1449 | return err; | |
1450 | } | |
1451 | ||
1452 | /* Add input pad controls */ | |
1453 | for (i = 0; i < info->pad_input_count; i++) { | |
1454 | snprintf(s, sizeof(s), "Line In %d Pad Capture Switch", i + 1); | |
1455 | err = scarlett2_add_new_ctl(mixer, &scarlett2_pad_ctl, | |
1456 | i, 1, s, NULL); | |
1457 | if (err < 0) | |
1458 | return err; | |
1459 | } | |
1460 | ||
1461 | return 0; | |
1462 | } | |
1463 | ||
1464 | /*** Mixer Volume Controls ***/ | |
1465 | ||
1466 | static int scarlett2_mixer_ctl_info(struct snd_kcontrol *kctl, | |
1467 | struct snd_ctl_elem_info *uinfo) | |
1468 | { | |
1469 | struct usb_mixer_elem_info *elem = kctl->private_data; | |
1470 | ||
1471 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; | |
1472 | uinfo->count = elem->channels; | |
1473 | uinfo->value.integer.min = 0; | |
1474 | uinfo->value.integer.max = SCARLETT2_MIXER_MAX_VALUE; | |
1475 | uinfo->value.integer.step = 1; | |
1476 | return 0; | |
1477 | } | |
1478 | ||
1479 | static int scarlett2_mixer_ctl_get(struct snd_kcontrol *kctl, | |
1480 | struct snd_ctl_elem_value *ucontrol) | |
1481 | { | |
1482 | struct usb_mixer_elem_info *elem = kctl->private_data; | |
1483 | struct scarlett2_mixer_data *private = elem->head.mixer->private_data; | |
1484 | ||
1485 | ucontrol->value.integer.value[0] = private->mix[elem->control]; | |
1486 | return 0; | |
1487 | } | |
1488 | ||
1489 | static int scarlett2_mixer_ctl_put(struct snd_kcontrol *kctl, | |
1490 | struct snd_ctl_elem_value *ucontrol) | |
1491 | { | |
1492 | struct usb_mixer_elem_info *elem = kctl->private_data; | |
1493 | struct usb_mixer_interface *mixer = elem->head.mixer; | |
1494 | struct scarlett2_mixer_data *private = mixer->private_data; | |
1495 | const struct scarlett2_device_info *info = private->info; | |
1496 | const struct scarlett2_ports *ports = info->ports; | |
1497 | int oval, val, num_mixer_in, mix_num, err = 0; | |
1498 | ||
1499 | mutex_lock(&private->data_mutex); | |
1500 | ||
1501 | oval = private->mix[elem->control]; | |
1502 | val = ucontrol->value.integer.value[0]; | |
1503 | num_mixer_in = ports[SCARLETT2_PORT_TYPE_MIX].num[SCARLETT2_PORT_OUT]; | |
1504 | mix_num = elem->control / num_mixer_in; | |
1505 | ||
1506 | if (oval == val) | |
1507 | goto unlock; | |
1508 | ||
1509 | private->mix[elem->control] = val; | |
1510 | err = scarlett2_usb_set_mix(mixer, mix_num); | |
1511 | if (err == 0) | |
1512 | err = 1; | |
1513 | ||
1514 | unlock: | |
1515 | mutex_unlock(&private->data_mutex); | |
1516 | return err; | |
1517 | } | |
1518 | ||
1519 | static const DECLARE_TLV_DB_MINMAX( | |
1520 | db_scale_scarlett2_mixer, | |
1521 | SCARLETT2_MIXER_MIN_DB * 100, | |
1522 | SCARLETT2_MIXER_MAX_DB * 100 | |
1523 | ); | |
1524 | ||
1525 | static const struct snd_kcontrol_new scarlett2_mixer_ctl = { | |
1526 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, | |
1527 | .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | | |
1528 | SNDRV_CTL_ELEM_ACCESS_TLV_READ, | |
1529 | .name = "", | |
1530 | .info = scarlett2_mixer_ctl_info, | |
1531 | .get = scarlett2_mixer_ctl_get, | |
1532 | .put = scarlett2_mixer_ctl_put, | |
1533 | .private_value = SCARLETT2_MIXER_MAX_DB, /* max value */ | |
1534 | .tlv = { .p = db_scale_scarlett2_mixer } | |
1535 | }; | |
1536 | ||
1537 | static int scarlett2_add_mixer_ctls(struct usb_mixer_interface *mixer) | |
1538 | { | |
1539 | struct scarlett2_mixer_data *private = mixer->private_data; | |
1540 | const struct scarlett2_ports *ports = private->info->ports; | |
1541 | int err, i, j; | |
1542 | int index; | |
1543 | char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN]; | |
1544 | ||
1545 | int num_inputs = ports[SCARLETT2_PORT_TYPE_MIX].num[SCARLETT2_PORT_OUT]; | |
1546 | int num_outputs = ports[SCARLETT2_PORT_TYPE_MIX].num[SCARLETT2_PORT_IN]; | |
1547 | ||
1548 | for (i = 0, index = 0; i < num_outputs; i++) { | |
1549 | for (j = 0; j < num_inputs; j++, index++) { | |
1550 | snprintf(s, sizeof(s), | |
1551 | "Mix %c Input %02d Playback Volume", | |
1552 | 'A' + i, j + 1); | |
1553 | err = scarlett2_add_new_ctl(mixer, &scarlett2_mixer_ctl, | |
1554 | index, 1, s, NULL); | |
1555 | if (err < 0) | |
1556 | return err; | |
1557 | } | |
1558 | } | |
1559 | ||
1560 | return 0; | |
1561 | } | |
1562 | ||
1563 | /*** Mux Source Selection Controls ***/ | |
1564 | ||
1565 | static int scarlett2_mux_src_enum_ctl_info(struct snd_kcontrol *kctl, | |
1566 | struct snd_ctl_elem_info *uinfo) | |
1567 | { | |
1568 | struct usb_mixer_elem_info *elem = kctl->private_data; | |
1569 | struct scarlett2_mixer_data *private = elem->head.mixer->private_data; | |
1570 | const struct scarlett2_ports *ports = private->info->ports; | |
1571 | unsigned int item = uinfo->value.enumerated.item; | |
1572 | int items = private->num_mux_srcs; | |
1573 | int port_type; | |
1574 | ||
1575 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; | |
1576 | uinfo->count = elem->channels; | |
1577 | uinfo->value.enumerated.items = items; | |
1578 | ||
1579 | if (item >= items) | |
1580 | item = uinfo->value.enumerated.item = items - 1; | |
1581 | ||
1582 | for (port_type = 0; | |
1583 | port_type < SCARLETT2_PORT_TYPE_COUNT; | |
1584 | port_type++) { | |
1585 | if (item < ports[port_type].num[SCARLETT2_PORT_IN]) { | |
1586 | sprintf(uinfo->value.enumerated.name, | |
1587 | ports[port_type].src_descr, | |
1588 | item + ports[port_type].src_num_offset); | |
1589 | return 0; | |
1590 | } | |
1591 | item -= ports[port_type].num[SCARLETT2_PORT_IN]; | |
1592 | } | |
1593 | ||
1594 | return -EINVAL; | |
1595 | } | |
1596 | ||
1597 | static int scarlett2_mux_src_enum_ctl_get(struct snd_kcontrol *kctl, | |
1598 | struct snd_ctl_elem_value *ucontrol) | |
1599 | { | |
1600 | struct usb_mixer_elem_info *elem = kctl->private_data; | |
1601 | struct scarlett2_mixer_data *private = elem->head.mixer->private_data; | |
1602 | ||
1603 | ucontrol->value.enumerated.item[0] = private->mux[elem->control]; | |
1604 | return 0; | |
1605 | } | |
1606 | ||
1607 | static int scarlett2_mux_src_enum_ctl_put(struct snd_kcontrol *kctl, | |
1608 | struct snd_ctl_elem_value *ucontrol) | |
1609 | { | |
1610 | struct usb_mixer_elem_info *elem = kctl->private_data; | |
1611 | struct usb_mixer_interface *mixer = elem->head.mixer; | |
1612 | struct scarlett2_mixer_data *private = mixer->private_data; | |
1613 | int index = elem->control; | |
1614 | int oval, val, err = 0; | |
1615 | ||
1616 | mutex_lock(&private->data_mutex); | |
1617 | ||
1618 | oval = private->mux[index]; | |
1619 | val = clamp(ucontrol->value.integer.value[0], | |
1620 | 0L, private->num_mux_srcs - 1L); | |
1621 | ||
1622 | if (oval == val) | |
1623 | goto unlock; | |
1624 | ||
1625 | private->mux[index] = val; | |
1626 | err = scarlett2_usb_set_mux(mixer); | |
1627 | if (err == 0) | |
1628 | err = 1; | |
1629 | ||
1630 | unlock: | |
1631 | mutex_unlock(&private->data_mutex); | |
1632 | return err; | |
1633 | } | |
1634 | ||
1635 | static const struct snd_kcontrol_new scarlett2_mux_src_enum_ctl = { | |
1636 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, | |
1637 | .name = "", | |
1638 | .info = scarlett2_mux_src_enum_ctl_info, | |
1639 | .get = scarlett2_mux_src_enum_ctl_get, | |
1640 | .put = scarlett2_mux_src_enum_ctl_put, | |
1641 | }; | |
1642 | ||
1643 | static int scarlett2_add_mux_enums(struct usb_mixer_interface *mixer) | |
1644 | { | |
1645 | struct scarlett2_mixer_data *private = mixer->private_data; | |
1646 | const struct scarlett2_ports *ports = private->info->ports; | |
1647 | int port_type, channel, i; | |
1648 | ||
1649 | for (i = 0, port_type = 0; | |
1650 | port_type < SCARLETT2_PORT_TYPE_COUNT; | |
1651 | port_type++) { | |
1652 | for (channel = 0; | |
1653 | channel < ports[port_type].num[SCARLETT2_PORT_OUT]; | |
1654 | channel++, i++) { | |
1655 | int err; | |
1656 | char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN]; | |
1657 | const char *const descr = ports[port_type].dst_descr; | |
1658 | ||
1659 | snprintf(s, sizeof(s) - 5, descr, channel + 1); | |
1660 | strcat(s, " Enum"); | |
1661 | ||
1662 | err = scarlett2_add_new_ctl(mixer, | |
1663 | &scarlett2_mux_src_enum_ctl, | |
1664 | i, 1, s, NULL); | |
1665 | if (err < 0) | |
1666 | return err; | |
1667 | } | |
1668 | } | |
1669 | ||
1670 | return 0; | |
1671 | } | |
1672 | ||
1673 | /*** Meter Controls ***/ | |
1674 | ||
1675 | static int scarlett2_meter_ctl_info(struct snd_kcontrol *kctl, | |
1676 | struct snd_ctl_elem_info *uinfo) | |
1677 | { | |
1678 | struct usb_mixer_elem_info *elem = kctl->private_data; | |
1679 | ||
1680 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; | |
1681 | uinfo->count = elem->channels; | |
1682 | uinfo->value.integer.min = 0; | |
1683 | uinfo->value.integer.max = 4095; | |
1684 | uinfo->value.integer.step = 1; | |
1685 | return 0; | |
1686 | } | |
1687 | ||
1688 | static int scarlett2_meter_ctl_get(struct snd_kcontrol *kctl, | |
1689 | struct snd_ctl_elem_value *ucontrol) | |
1690 | { | |
1691 | struct usb_mixer_elem_info *elem = kctl->private_data; | |
1692 | u16 meter_levels[SCARLETT2_NUM_METERS]; | |
1693 | int i, err; | |
1694 | ||
1695 | err = scarlett2_usb_get_meter_levels(elem->head.mixer, meter_levels); | |
1696 | if (err < 0) | |
1697 | return err; | |
1698 | ||
1699 | for (i = 0; i < elem->channels; i++) | |
1700 | ucontrol->value.integer.value[i] = meter_levels[i]; | |
1701 | ||
1702 | return 0; | |
1703 | } | |
1704 | ||
1705 | static const struct snd_kcontrol_new scarlett2_meter_ctl = { | |
1706 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, | |
1707 | .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, | |
1708 | .name = "", | |
1709 | .info = scarlett2_meter_ctl_info, | |
1710 | .get = scarlett2_meter_ctl_get | |
1711 | }; | |
1712 | ||
1713 | static int scarlett2_add_meter_ctl(struct usb_mixer_interface *mixer) | |
1714 | { | |
1715 | return scarlett2_add_new_ctl(mixer, &scarlett2_meter_ctl, | |
1716 | 0, SCARLETT2_NUM_METERS, | |
1717 | "Level Meter", NULL); | |
1718 | } | |
1719 | ||
1720 | /*** Cleanup/Suspend Callbacks ***/ | |
1721 | ||
1722 | static void scarlett2_private_free(struct usb_mixer_interface *mixer) | |
1723 | { | |
1724 | struct scarlett2_mixer_data *private = mixer->private_data; | |
1725 | ||
1726 | cancel_delayed_work_sync(&private->work); | |
1727 | kfree(private); | |
1728 | mixer->private_data = NULL; | |
1729 | } | |
1730 | ||
1731 | static void scarlett2_private_suspend(struct usb_mixer_interface *mixer) | |
1732 | { | |
1733 | struct scarlett2_mixer_data *private = mixer->private_data; | |
1734 | ||
1735 | if (cancel_delayed_work_sync(&private->work)) | |
1736 | scarlett2_config_save(private->mixer); | |
1737 | } | |
1738 | ||
1739 | /*** Initialisation ***/ | |
1740 | ||
1741 | static int scarlett2_count_mux_srcs(const struct scarlett2_ports *ports) | |
1742 | { | |
1743 | int port_type, count = 0; | |
1744 | ||
1745 | for (port_type = 0; | |
1746 | port_type < SCARLETT2_PORT_TYPE_COUNT; | |
1747 | port_type++) | |
1748 | count += ports[port_type].num[SCARLETT2_PORT_IN]; | |
1749 | ||
1750 | return count; | |
1751 | } | |
1752 | ||
1753 | /* Default routing connects PCM outputs and inputs to Analogue, | |
1754 | * S/PDIF, then ADAT | |
1755 | */ | |
1756 | static void scarlett2_init_routing(u8 *mux, | |
1757 | const struct scarlett2_ports *ports) | |
1758 | { | |
1759 | int i, input_num, input_count, port_type; | |
1760 | int output_num, output_count, port_type_connect_num; | |
1761 | ||
1762 | static const int connect_order[] = { | |
1763 | SCARLETT2_PORT_TYPE_ANALOGUE, | |
1764 | SCARLETT2_PORT_TYPE_SPDIF, | |
1765 | SCARLETT2_PORT_TYPE_ADAT, | |
1766 | -1 | |
1767 | }; | |
1768 | ||
1769 | /* Assign PCM inputs (routing outputs) */ | |
1770 | output_num = scarlett2_get_port_start_num(ports, | |
1771 | SCARLETT2_PORT_OUT, | |
1772 | SCARLETT2_PORT_TYPE_PCM); | |
1773 | output_count = ports[SCARLETT2_PORT_TYPE_PCM].num[SCARLETT2_PORT_OUT]; | |
1774 | ||
1775 | for (port_type = connect_order[port_type_connect_num = 0]; | |
1776 | port_type >= 0; | |
1777 | port_type = connect_order[++port_type_connect_num]) { | |
1778 | input_num = scarlett2_get_port_start_num( | |
1779 | ports, SCARLETT2_PORT_IN, port_type); | |
1780 | input_count = ports[port_type].num[SCARLETT2_PORT_IN]; | |
1781 | for (i = 0; | |
1782 | i < input_count && output_count; | |
1783 | i++, output_count--) | |
1784 | mux[output_num++] = input_num++; | |
1785 | } | |
1786 | ||
1787 | /* Assign PCM outputs (routing inputs) */ | |
1788 | input_num = scarlett2_get_port_start_num(ports, | |
1789 | SCARLETT2_PORT_IN, | |
1790 | SCARLETT2_PORT_TYPE_PCM); | |
1791 | input_count = ports[SCARLETT2_PORT_TYPE_PCM].num[SCARLETT2_PORT_IN]; | |
1792 | ||
1793 | for (port_type = connect_order[port_type_connect_num = 0]; | |
1794 | port_type >= 0; | |
1795 | port_type = connect_order[++port_type_connect_num]) { | |
1796 | output_num = scarlett2_get_port_start_num( | |
1797 | ports, SCARLETT2_PORT_OUT, port_type); | |
1798 | output_count = ports[port_type].num[SCARLETT2_PORT_OUT]; | |
1799 | for (i = 0; | |
1800 | i < output_count && input_count; | |
1801 | i++, input_count--) | |
1802 | mux[output_num++] = input_num++; | |
1803 | } | |
1804 | } | |
1805 | ||
1806 | /* Initialise private data, routing, sequence number */ | |
1807 | static int scarlett2_init_private(struct usb_mixer_interface *mixer, | |
1808 | const struct scarlett2_device_info *info) | |
1809 | { | |
1810 | struct scarlett2_mixer_data *private = | |
1811 | kzalloc(sizeof(struct scarlett2_mixer_data), GFP_KERNEL); | |
1812 | ||
1813 | if (!private) | |
1814 | return -ENOMEM; | |
1815 | ||
1816 | mutex_init(&private->usb_mutex); | |
1817 | mutex_init(&private->data_mutex); | |
1818 | INIT_DELAYED_WORK(&private->work, scarlett2_config_save_work); | |
1819 | private->info = info; | |
1820 | private->num_mux_srcs = scarlett2_count_mux_srcs(info->ports); | |
1821 | private->scarlett2_seq = 0; | |
1822 | private->mixer = mixer; | |
1823 | mixer->private_data = private; | |
1824 | mixer->private_free = scarlett2_private_free; | |
1825 | mixer->private_suspend = scarlett2_private_suspend; | |
1826 | ||
1827 | /* Setup default routing */ | |
1828 | scarlett2_init_routing(private->mux, info->ports); | |
1829 | ||
1830 | /* Initialise the sequence number used for the proprietary commands */ | |
1831 | return scarlett2_usb(mixer, SCARLETT2_USB_INIT_SEQ, NULL, 0, NULL, 0); | |
1832 | } | |
1833 | ||
1834 | /* Read line-in config and line-out volume settings on start */ | |
1835 | static int scarlett2_read_configs(struct usb_mixer_interface *mixer) | |
1836 | { | |
1837 | struct scarlett2_mixer_data *private = mixer->private_data; | |
1838 | const struct scarlett2_device_info *info = private->info; | |
1839 | const struct scarlett2_ports *ports = info->ports; | |
1840 | int num_line_out = | |
1841 | ports[SCARLETT2_PORT_TYPE_ANALOGUE].num[SCARLETT2_PORT_OUT]; | |
1842 | u8 level_switches[SCARLETT2_LEVEL_SWITCH_MAX]; | |
1843 | u8 pad_switches[SCARLETT2_PAD_SWITCH_MAX]; | |
1844 | struct scarlett2_usb_volume_status volume_status; | |
1845 | int err, i; | |
1846 | ||
1847 | if (info->level_input_count) { | |
1848 | err = scarlett2_usb_get_config( | |
1849 | mixer, | |
1850 | SCARLETT2_CONFIG_LEVEL_SWITCH, | |
1851 | info->level_input_count, | |
1852 | level_switches); | |
1853 | if (err < 0) | |
1854 | return err; | |
1855 | for (i = 0; i < info->level_input_count; i++) | |
1856 | private->level_switch[i] = level_switches[i]; | |
1857 | } | |
1858 | ||
1859 | if (info->pad_input_count) { | |
1860 | err = scarlett2_usb_get_config( | |
1861 | mixer, | |
1862 | SCARLETT2_CONFIG_PAD_SWITCH, | |
1863 | info->pad_input_count, | |
1864 | pad_switches); | |
1865 | if (err < 0) | |
1866 | return err; | |
1867 | for (i = 0; i < info->pad_input_count; i++) | |
1868 | private->pad_switch[i] = pad_switches[i]; | |
1869 | } | |
1870 | ||
1871 | err = scarlett2_usb_get_volume_status(mixer, &volume_status); | |
1872 | if (err < 0) | |
1873 | return err; | |
1874 | ||
1875 | private->master_vol = clamp( | |
1876 | volume_status.master_vol + SCARLETT2_VOLUME_BIAS, | |
1877 | 0, SCARLETT2_VOLUME_BIAS); | |
1878 | ||
1879 | for (i = 0; i < num_line_out; i++) { | |
1880 | int volume; | |
1881 | ||
1882 | private->vol_sw_hw_switch[i] = | |
1883 | info->line_out_hw_vol | |
1884 | && volume_status.sw_hw_switch[i]; | |
1885 | ||
1886 | volume = private->vol_sw_hw_switch[i] | |
1887 | ? volume_status.master_vol | |
1888 | : volume_status.sw_vol[i]; | |
1889 | volume = clamp(volume + SCARLETT2_VOLUME_BIAS, | |
1890 | 0, SCARLETT2_VOLUME_BIAS); | |
1891 | private->vol[i] = volume; | |
1892 | } | |
1893 | ||
1894 | for (i = 0; i < info->button_count; i++) | |
1895 | private->buttons[i] = !!volume_status.buttons[i]; | |
1896 | ||
1897 | return 0; | |
1898 | } | |
1899 | ||
1900 | /* Notify on volume change */ | |
1901 | static void scarlett2_mixer_interrupt_vol_change( | |
1902 | struct usb_mixer_interface *mixer) | |
1903 | { | |
1904 | struct scarlett2_mixer_data *private = mixer->private_data; | |
1905 | const struct scarlett2_ports *ports = private->info->ports; | |
1906 | int num_line_out = | |
1907 | ports[SCARLETT2_PORT_TYPE_ANALOGUE].num[SCARLETT2_PORT_OUT]; | |
1908 | int i; | |
1909 | ||
1910 | private->vol_updated = 1; | |
1911 | ||
1912 | snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE, | |
1913 | &private->master_vol_ctl->id); | |
1914 | ||
1915 | for (i = 0; i < num_line_out; i++) { | |
1916 | if (!private->vol_sw_hw_switch[i]) | |
1917 | continue; | |
1918 | snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE, | |
1919 | &private->vol_ctls[i]->id); | |
1920 | } | |
1921 | } | |
1922 | ||
1923 | /* Notify on button change */ | |
1924 | static void scarlett2_mixer_interrupt_button_change( | |
1925 | struct usb_mixer_interface *mixer) | |
1926 | { | |
1927 | struct scarlett2_mixer_data *private = mixer->private_data; | |
1928 | int i; | |
1929 | ||
1930 | private->vol_updated = 1; | |
1931 | ||
1932 | for (i = 0; i < private->info->button_count; i++) | |
1933 | snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE, | |
1934 | &private->button_ctls[i]->id); | |
1935 | } | |
1936 | ||
1937 | /* Interrupt callback */ | |
1938 | static void scarlett2_mixer_interrupt(struct urb *urb) | |
1939 | { | |
1940 | struct usb_mixer_interface *mixer = urb->context; | |
1941 | int len = urb->actual_length; | |
1942 | int ustatus = urb->status; | |
1943 | u32 data; | |
1944 | ||
1945 | if (ustatus != 0) | |
1946 | goto requeue; | |
1947 | ||
1948 | if (len == 8) { | |
1949 | data = le32_to_cpu(*(u32 *)urb->transfer_buffer); | |
1950 | if (data & SCARLETT2_USB_INTERRUPT_VOL_CHANGE) | |
1951 | scarlett2_mixer_interrupt_vol_change(mixer); | |
1952 | if (data & SCARLETT2_USB_INTERRUPT_BUTTON_CHANGE) | |
1953 | scarlett2_mixer_interrupt_button_change(mixer); | |
1954 | } else { | |
1955 | usb_audio_err(mixer->chip, | |
1956 | "scarlett mixer interrupt length %d\n", len); | |
1957 | } | |
1958 | ||
1959 | requeue: | |
1960 | if (ustatus != -ENOENT && | |
1961 | ustatus != -ECONNRESET && | |
1962 | ustatus != -ESHUTDOWN) { | |
1963 | urb->dev = mixer->chip->dev; | |
1964 | usb_submit_urb(urb, GFP_ATOMIC); | |
1965 | } | |
1966 | } | |
1967 | ||
1968 | static int scarlett2_mixer_status_create(struct usb_mixer_interface *mixer) | |
1969 | { | |
1970 | struct usb_device *dev = mixer->chip->dev; | |
1971 | unsigned int pipe = usb_rcvintpipe(dev, | |
1972 | SCARLETT2_USB_INTERRUPT_ENDPOINT); | |
1973 | void *transfer_buffer; | |
1974 | ||
1975 | if (mixer->urb) { | |
1976 | usb_audio_err(mixer->chip, | |
1977 | "%s: mixer urb already in use!\n", __func__); | |
1978 | return 0; | |
1979 | } | |
1980 | ||
1981 | if (snd_usb_pipe_sanity_check(dev, pipe)) | |
1982 | return -EINVAL; | |
1983 | ||
1984 | mixer->urb = usb_alloc_urb(0, GFP_KERNEL); | |
1985 | if (!mixer->urb) | |
1986 | return -ENOMEM; | |
1987 | ||
1988 | transfer_buffer = kmalloc(SCARLETT2_USB_INTERRUPT_MAX_DATA, GFP_KERNEL); | |
1989 | if (!transfer_buffer) | |
1990 | return -ENOMEM; | |
1991 | ||
1992 | usb_fill_int_urb(mixer->urb, dev, pipe, | |
1993 | transfer_buffer, SCARLETT2_USB_INTERRUPT_MAX_DATA, | |
1994 | scarlett2_mixer_interrupt, mixer, | |
1995 | SCARLETT2_USB_INTERRUPT_INTERVAL); | |
1996 | ||
1997 | return usb_submit_urb(mixer->urb, GFP_KERNEL); | |
1998 | } | |
1999 | ||
2000 | /* Entry point */ | |
2001 | int snd_scarlett_gen2_controls_create(struct usb_mixer_interface *mixer) | |
2002 | { | |
2003 | const struct scarlett2_device_info *info; | |
2004 | int err; | |
2005 | ||
2006 | /* only use UAC_VERSION_2 */ | |
2007 | if (!mixer->protocol) | |
2008 | return 0; | |
2009 | ||
2010 | switch (mixer->chip->usb_id) { | |
2011 | case USB_ID(0x1235, 0x8203): | |
2012 | info = &s6i6_gen2_info; | |
2013 | break; | |
2014 | case USB_ID(0x1235, 0x8204): | |
2015 | info = &s18i8_gen2_info; | |
2016 | break; | |
2017 | case USB_ID(0x1235, 0x8201): | |
2018 | info = &s18i20_gen2_info; | |
2019 | break; | |
2020 | default: /* device not (yet) supported */ | |
2021 | return -EINVAL; | |
2022 | } | |
2023 | ||
2024 | if (!(mixer->chip->setup & SCARLETT2_ENABLE)) { | |
2025 | usb_audio_err(mixer->chip, | |
2026 | "Focusrite Scarlett Gen 2 Mixer Driver disabled; " | |
2027 | "use options snd_usb_audio device_setup=1 " | |
2028 | "to enable and report any issues to g@b4.vu"); | |
2029 | return 0; | |
2030 | } | |
2031 | ||
2032 | /* Initialise private data, routing, sequence number */ | |
2033 | err = scarlett2_init_private(mixer, info); | |
2034 | if (err < 0) | |
2035 | return err; | |
2036 | ||
2037 | /* Read volume levels and controls from the interface */ | |
2038 | err = scarlett2_read_configs(mixer); | |
2039 | if (err < 0) | |
2040 | return err; | |
2041 | ||
2042 | /* Create the analogue output controls */ | |
2043 | err = scarlett2_add_line_out_ctls(mixer); | |
2044 | if (err < 0) | |
2045 | return err; | |
2046 | ||
2047 | /* Create the analogue input controls */ | |
2048 | err = scarlett2_add_line_in_ctls(mixer); | |
2049 | if (err < 0) | |
2050 | return err; | |
2051 | ||
2052 | /* Create the input, output, and mixer mux input selections */ | |
2053 | err = scarlett2_add_mux_enums(mixer); | |
2054 | if (err < 0) | |
2055 | return err; | |
2056 | ||
2057 | /* Create the matrix mixer controls */ | |
2058 | err = scarlett2_add_mixer_ctls(mixer); | |
2059 | if (err < 0) | |
2060 | return err; | |
2061 | ||
2062 | /* Create the level meter controls */ | |
2063 | err = scarlett2_add_meter_ctl(mixer); | |
2064 | if (err < 0) | |
2065 | return err; | |
2066 | ||
2067 | /* Set up the interrupt polling if there are hardware buttons */ | |
2068 | if (info->button_count) { | |
2069 | err = scarlett2_mixer_status_create(mixer); | |
2070 | if (err < 0) | |
2071 | return err; | |
2072 | } | |
2073 | ||
2074 | return 0; | |
2075 | } |