ALSA: usb-audio: Scarlett Gen 2 mixer interface
PATCH v4 1/1 ALSA: usb-audio: Scarlett Gen 2 mixer interface
Add mixer quirk for the Focusrite Scarlett 6i6, 18i8, and 18i20 Gen 2 audio interfaces. Although the interfaces are USB compliant, additional input/output level controls and hardware routing/mixing functionality are available using proprietary USB requests.
---
Hi all,
This patch adds the following controls for the Scarlett 6i6, 18i8, and 18i20 Gen 2:
- Master volume knob indicator (18i20 only)
- Mute and dim switches (18i20 only)
- Volume controls for the analogue HW outputs
- HW/SW volume switches for the 10 analogue HW outputs (18i20 only)
- Line Level/Instrument Level and Pad controls (6i6 and 18i8 only)
- Output mux (where the sound for the HW outputs comes from; defaults to PCM outputs)
- Capture mux (where the sound for PCM recording comes from; defaults to HW inputs)
- Matrix mux (where the sound going into the mixer comes from; 18 inputs default off)
- Mixer matrix (18 inputs * 10 outputs = 180 controls)
- Level meters
Changes since v1:
- Add support for the Scarlett 18i8 Gen 2
- Save configuration parameters to NVRAM
- Implemented feedback from Takashi's email 24/Apr/2019
- Moved private field from struct snd_usb_audio to struct usb_mixer_interface
- Added timer and buffer fields to struct usb_mixer_interface
- Other small code fixes/cleanups/improvements
Changes since v2:
- Add support for the Scarlett 6i6 Gen 2
- Add support for controlling the mute/dim switches (18i20 only)
- Implemented feedback from Takashi's email 29/Apr/2019: One pointer field private_data and one function pointer private_free. Replaced timer with delayed work (thanks for that suggestion; it makes the code a *lot* simpler!).
- The new functionality is disabled by default as there were reports of some Gen 2 devices not responding to the initialisation sequence and hanging. Added module parameter "scarlett_gen2_mixer_enable". If this parameter is not set, logs a message "Focusrite Scarlett Gen 2 Mixer Driver disabled; use options snd_usb_audio scarlett_gen2_mixer_enable=1 to enable and report any issues to [email protected]".
Changes since v3:
- No functional changes; fix checkpatch.pl error and typo in declaration.
Only outstanding question I believe:
- Takashi, you wrote last time "Also, the delayed work needs to be canceled or flushed at PM suspend callback as well as disconnecting." I'm not sure why it should be cancelled; is it not okay to do on resume? But I tested suspending while the delayed work was waiting and it seemed like the delayed work was cancelled anyway. Probably better to do a flush on suspend though; should this be done through a hook like:
diff --git a/sound/usb/mixer.c b/sound/usb/mixer.c
index 2444737a14b2..1572a89267c6 100644
--- a/sound/usb/mixer.c
+++ b/sound/usb/mixer.c
@@ -3547,6 +3547,8 @@ static int snd_usb_mixer_activate(struct usb_mixer_interface *mixer)
int snd_usb_mixer_suspend(struct usb_mixer_interface *mixer)
{
snd_usb_mixer_inactivate(mixer);
+ if (mixer->private_suspend)
+ mixer->private_suspend(mixer);
return 0;
}
diff --git a/sound/usb/mixer.h b/sound/usb/mixer.h
index fa6e216a06f0..d94c688c65f7 100644
--- a/sound/usb/mixer.h
+++ b/sound/usb/mixer.h
@@ -28,6 +28,7 @@ struct usb_mixer_interface { void *private_data;
void (*private_free)(struct usb_mixer_interface *private_data); + void (*private_suspend)(struct usb_mixer_interface *);
};
#define MAX_CHANNELS 16 /* max logical channels */
...or just keep the existing behaviour where it seems to get cancelled?
Thanks,
Geoffrey.
sound/usb/Makefile +1 -0
@@ -11,6 +11,7 @@ snd-usb-audio-objs := card.o \ mixer.o \ mixer_quirks.o \ mixer_scarlett.o \ + mixer_scarlett_gen2.o \ mixer_us16x08.o \ pcm.o \ power.o \
sound/usb/mixer.c +2 -0
@@ -3505,6 +3505,8 @@ void snd_usb_mixer_disconnect(struct usb_mixer_interface *mixer)usb_kill_urb (mixer->urb);if (mixer->rc_urb)usb_kill_urb (mixer->rc_urb); +if (mixer->private_free) + mixer->private_free (mixer); mixer->disconnected = true; }
sound/usb/mixer.h +3 -0
@@ -28,6 +28,9 @@ struct usb_mixer_interface {struct media_mixer_ctl *media_mixer_ctl;bool disconnected; + +void *private_data; +void (*private_free)(struct usb_mixer_interface *mixer); };#define MAX_CHANNELS 16/* max logical channels */
sound/usb/mixer_quirks.c +7 -0
@@ -32,6 +32,7 @@#include "mixer.h" #include "mixer_quirks.h" #include "mixer_scarlett.h" +#include "mixer_scarlett_gen2.h" #include "mixer_us16x08.h" #include "helper.h"
@@ -2258,6 +2259,12 @@ int snd_usb_mixer_apply_create_quirk(struct usb_mixer_interface *mixer) err =snd_scarlett_controls_create (mixer);break ; +case USB_ID (0x1235 ,0x8203 ):/* Focusrite Scarlett 6i6 2nd Gen */ +case USB_ID (0x1235 ,0x8204 ):/* Focusrite Scarlett 18i8 2nd Gen */ +case USB_ID (0x1235 ,0x8201 ):/* Focusrite Scarlett 18i20 2nd Gen */ + err =snd_scarlett_gen2_controls_create (mixer); +break ; +case USB_ID (0x041e ,0x323b ):/* Creative Sound Blaster E1 */ err =snd_soundblaster_e1_switch_create (mixer);break ;
sound/usb/mixer_scarlett_gen2.c new file +2078 -0
2,078 lines not on this page · raw message
sound/usb/mixer_scarlett_gen2.h new file +7 -0
@@ -0,0 +1,7 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __USB_MIXER_SCARLETT_GEN2_H +#define __USB_MIXER_SCARLETT_GEN2_H + +int snd_scarlett_gen2_controls_create (struct usb_mixer_interface *mixer); + +#endif /* __USB_MIXER_SCARLETT_GEN2_H */
_______________________________________________
Alsa-devel mailing list
[email protected]
https://mailman.alsa-project.org/mailman/listinfo/alsa-devel
Quoted 7 lines · Geoffrey D. Bennett
Only outstanding question I believe:
- Takashi, you wrote last time "Also, the delayed work needs to be canceled or flushed at PM suspend callback as well as disconnecting." I'm not sure why it should be cancelled; is it not okay to do on resume?
Not really. The standard idiom is to cancel the pending task immediately at suspend, so that the whole PM procedure won't be interfered, then reschedule the work at resume again.
Quoted 3 lines
But I tested suspending while the delayed work
was waiting and it seemed like the delayed work was cancelled anyway. Probably better to do a flush on suspend though;
The flush would block, hence it wastes lots of time at suspend. Ideally the suspend should be performed immediately.
Quoted 29 lines
should this
be done through a hook like:diff --git a/sound/usb/mixer.c b/sound/usb/mixer.c index 2444737a14b2..1572a89267c6 100644 --- a/sound/usb/mixer.c +++ b/sound/usb/mixer.c @@ -3547,6 +3547,8 @@ static int snd_usb_mixer_activate(struct usb_mixer_interface *mixer) int snd_usb_mixer_suspend(struct usb_mixer_interface *mixer) { snd_usb_mixer_inactivate(mixer); + if (mixer->private_suspend) + mixer->private_suspend(mixer); return 0; } diff --git a/sound/usb/mixer.h b/sound/usb/mixer.h index fa6e216a06f0..d94c688c65f7 100644 --- a/sound/usb/mixer.h +++ b/sound/usb/mixer.h @@ -28,6 +28,7 @@ struct usb_mixer_interface {void *private_data; void (*private_free)(struct usb_mixer_interface *private_data); + void (*private_suspend)(struct usb_mixer_interface *); }; #define MAX_CHANNELS 16 /* max logical channels */
Quoted 2 lines
...or just keep the existing behaviour where it seems to get cancelled?
I'm fine to add the new suspend/resume callbacks, but maybe the corresponding resume would be needed.
Other than that, the patch looks almost good.
Quoted 22 lines
sound/usb/mixer_scarlett_gen2.c new file +19
-0@@ -0,0 +1,2078 @@ +// 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>
sound/usb/mixer_scarlett_gen2.c line 29
Quoted 10 lines
sound/usb/mixer_scarlett_gen2.c new file +10
-0+* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details.
Drop these texts. The SPDX identifier should suffice.
sound/usb/mixer_scarlett_gen2.c line 122
Quoted 4 lines
sound/usb/mixer_scarlett_gen2.c new file +4
-0+static int scarlett_gen2_mixer_enable; +module_param (scarlett_gen2_mixer_enable, int,0444 ); +MODULE_PARM_DESC (scarlett_gen2_mixer_enable, +"Focusrite Scarlett Gen 2 Mixer Driver Enable" );
Do we need this? If disabling the quirk is really required, it should be implemented rather in a generic option, instead.
sound/usb/mixer_scarlett_gen2.c line 612
Quoted 13 lines
sound/usb/mixer_scarlett_gen2.c new file +13
-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 =0 , *resp =0 ; + +int err =0 ;
Avoid unnecessary blank lines.
sound/usb/mixer_scarlett_gen2.c line 622
Quoted 10 lines
sound/usb/mixer_scarlett_gen2.c new file +10
-0+ +mutex_lock (&private->usb_mutex); + +/* build request message and send it */ + + req =kmalloc (req_buf_size,GFP_KERNEL ); +if (!req) { + err = -ENOMEM ; + goto unlock; + }
Better to run kmalloc outside the mutex. It's not necessarily exclusive.
sound/usb/mixer_scarlett_gen2.c line 653
Quoted 7 lines
sound/usb/mixer_scarlett_gen2.c new file +7
-0+/* send a second message to get the response */ + + resp =kmalloc (resp_buf_size,GFP_KERNEL ); +if (!resp) { + err = -ENOMEM ; + goto unlock; + }
Ditto, this could be better allocated before the critical section.
thanks,
Takashi
On Tue, Jul 09, 2019 at 03:57:28PM +0200, Takashi Iwai wrote:
[...]
Quoted 34 lines
Quoted 3 lines
But I tested suspending while the delayed work
was waiting and it seemed like the delayed work was cancelled anyway. Probably better to do a flush on suspend though;The flush would block, hence it wastes lots of time at suspend. Ideally the suspend should be performed immediately.
Quoted 27 lines
should this
be done through a hook like:diff --git a/sound/usb/mixer.c b/sound/usb/mixer.c index 2444737a14b2..1572a89267c6 100644 --- a/sound/usb/mixer.c +++ b/sound/usb/mixer.c @@ -3547,6 +3547,8 @@ static int snd_usb_mixer_activate(struct usb_mixer_interface *mixer) int snd_usb_mixer_suspend(struct usb_mixer_interface *mixer) { snd_usb_mixer_inactivate(mixer); + if (mixer->private_suspend) + mixer->private_suspend(mixer); return 0; } diff --git a/sound/usb/mixer.h b/sound/usb/mixer.h index fa6e216a06f0..d94c688c65f7 100644 --- a/sound/usb/mixer.h +++ b/sound/usb/mixer.h @@ -28,6 +28,7 @@ struct usb_mixer_interface {void *private_data; void (*private_free)(struct usb_mixer_interface *private_data); + void (*private_suspend)(struct usb_mixer_interface *); };
Quoted 7 lines
Quoted 4 lines
#define MAX_CHANNELS 16 /* max logical channels */...or just keep the existing behaviour where it seems to get cancelled?
I'm fine to add the new suspend/resume callbacks, but maybe the corresponding resume would be needed.
Did you mean "wouldn't be needed"?
[...]
Quoted 7 lines
sound/usb/mixer_scarlett_gen2.c line 122
Quoted 4 lines
sound/usb/mixer_scarlett_gen2.c new file +4
-0+static int scarlett_gen2_mixer_enable; +module_param (scarlett_gen2_mixer_enable, int,0444 ); +MODULE_PARM_DESC (scarlett_gen2_mixer_enable, +"Focusrite Scarlett Gen 2 Mixer Driver Enable" );Do we need this? If disabling the quirk is really required, it should be implemented rather in a generic option, instead.
Actually it would be best to have it disabled by default as I have had two reports from people who tried this mixer driver and it broke audio for them.
The failure looks like this... the first vendor-specific USB message sent to the interface to initialise it results in an error:
e2d87480 3146602314 S Ci:1:007:0 s a1 03 0000 0005 0010 16 < e2d87480 3152107418 C Ci:1:007:0 -2 0
My mixer driver gives up initialising, and subsequent USB messages fail:
e2d87480 3152110424 S Ci:1:007:0 s 80 06 0304 0409 00ff 255 < e2d87480 3152121815 C Ci:1:007:0 -71 0e2d87480 3152121955 S Ci:1:007:0 s 80 06 0304 0409 0002 2 < e2d87480 3152131554 C Ci:1:007:0 -71 0e2d87480 3152133420 S Ci:1:007:0 s 80 06 0305 0409 00ff 255 < e2d87480 3157224469 C Ci:1:007:0 -2 0
I don't see any way to distinguish between devices that do and don't work before sending the initialisation message. lsusb -v output is identical. I need someone to do a USB capture to see what the vendor driver does differently, but I haven't found someone who can do this yet.
I have 9 success reports so far and 2 failure reports. I hope that by getting this driver out to a wider audience I can get the data I need to make it work in all cases and then we can remove the scarlett_gen2_mixer_enable quirk and have the driver always enabled.
Thanks for your feedback again. The other things which I didn't comment on I agree with and will fix.
Regards,
Geoffrey.
Quoted 39 lines · Geoffrey D. Bennett
On Tue, Jul 09, 2019 at 03:57:28PM +0200, Takashi Iwai wrote:
[...]Quoted 36 lines
Quoted 3 lines
But I tested suspending while the delayed work
was waiting and it seemed like the delayed work was cancelled anyway. Probably better to do a flush on suspend though;The flush would block, hence it wastes lots of time at suspend. Ideally the suspend should be performed immediately.
Quoted 29 lines
should this
be done through a hook like:diff --git a/sound/usb/mixer.c b/sound/usb/mixer.c index 2444737a14b2..1572a89267c6 100644 --- a/sound/usb/mixer.c +++ b/sound/usb/mixer.c @@ -3547,6 +3547,8 @@ static int snd_usb_mixer_activate(struct usb_mixer_interface *mixer) int snd_usb_mixer_suspend(struct usb_mixer_interface *mixer) { snd_usb_mixer_inactivate(mixer); + if (mixer->private_suspend) + mixer->private_suspend(mixer); return 0; } diff --git a/sound/usb/mixer.h b/sound/usb/mixer.h index fa6e216a06f0..d94c688c65f7 100644 --- a/sound/usb/mixer.h +++ b/sound/usb/mixer.h @@ -28,6 +28,7 @@ struct usb_mixer_interface {void *private_data; void (*private_free)(struct usb_mixer_interface *private_data); + void (*private_suspend)(struct usb_mixer_interface *); }; #define MAX_CHANNELS 16 /* max logical channels */
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
...or just keep the existing behaviour where it seems to get cancelled?
I'm fine to add the new suspend/resume callbacks, but maybe the corresponding resume would be needed.
Did you mean "wouldn't be needed"?
No, it's needed because at resume you'd reschedule the work that was canceled at suspend. It's specific.
Quoted 9 lines
[...]
Quoted 7 lines
sound/usb/mixer_scarlett_gen2.c line 122
Quoted 4 lines
sound/usb/mixer_scarlett_gen2.c new file +4
-0+static int scarlett_gen2_mixer_enable; +module_param (scarlett_gen2_mixer_enable, int,0444 ); +MODULE_PARM_DESC (scarlett_gen2_mixer_enable, +"Focusrite Scarlett Gen 2 Mixer Driver Enable" );Do we need this? If disabling the quirk is really required, it should be implemented rather in a generic option, instead.
Quoted 3 lines
Actually it would be best to have it disabled by default as I have had two reports from people who tried this mixer driver and it broke audio for them.
Hm, and these have the same USB device ID?
Quoted 22 lines
The failure looks like this... the first vendor-specific USB message sent to the interface to initialise it results in an error:
e2d874803146602314 S Ci:1:007:0 s a1 03 0000 0005 0010 16 <e2d874803152107418 C Ci:1:007:0 -2 0My mixer driver gives up initialising, and subsequent USB messages fail:
e2d874803152110424 S Ci:1:007:0 s 80 06 0304 0409 00ff 255 <e2d874803152121815 C Ci:1:007:0 -71 0e2d874803152121955 S Ci:1:007:0 s 80 06 0304 0409 0002 2 <e2d874803152131554 C Ci:1:007:0 -71 0e2d874803152133420 S Ci:1:007:0 s 80 06 0305 0409 00ff 255 <e2d874803157224469 C Ci:1:007:0 -2 0I don't see any way to distinguish between devices that do and don't work before sending the initialisation message. lsusb -v output is identical. I need someone to do a USB capture to see what the vendor driver does differently, but I haven't found someone who can do this yet.
Quoted 7 lines
I have 9 success reports so far and 2 failure reports. I hope that by getting this driver out to a wider audience I can get the data I need to make it work in all cases and then we can remove the
scarlett_gen2_mixer_enablequirk and have the driver always enabled.Thanks for your feedback again. The other things which I didn't comment on I agree with and will fix.
I guess you can use chip->setup value for the quirk-specific configuration as a start. It's a bit hackish but the parameter is already there for long time, hence you don't need to add any extra stuff for that.
thanks,
Takashi
Quoted 2 lines · Takashi Iwai
On Tue, 09 Jul 2019 18:24:30 +0200,
Geoffrey D. Bennett wrote:
[...]
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
sound/usb/mixer_scarlett_gen2.c line 122
Quoted 4 lines
sound/usb/mixer_scarlett_gen2.c new file +4
-0+static int scarlett_gen2_mixer_enable; +module_param (scarlett_gen2_mixer_enable, int,0444 ); +MODULE_PARM_DESC (scarlett_gen2_mixer_enable, +"Focusrite Scarlett Gen 2 Mixer Driver Enable" );Do we need this? If disabling the quirk is really required, it should be implemented rather in a generic option, instead.
Quoted 5 lines
Quoted 3 lines
Actually it would be best to have it disabled by default as I have had two reports from people who tried this mixer driver and it broke audio for them.
Hm, and these have the same USB device ID?
Yes, same device ID. I couldn't see any significant difference between mine and theirs usbmon trace and lsusb -v output.
[...]
Quoted 4 lines
I guess you can use
chip->setupvalue for the quirk-specific configuration as a start. It's a bit hackish but the parameter is already there for long time, hence you don't need to add any extra stuff for that.
Got it. Have implemented and will resend soon.
Quoted 11 lines · In your other email you
Canceling the pending work is the right thing for suspend, but we need reconsidering the side effect. For example, the device might be removed while the machine is in sleep. And the purpose of the scheduled work is to sync the config to NVRAM. That is, it should have been done before going to suspend. Then we don't have to resume the task at resume, too.
That said, the best would be:
- cancel the pending work, check the return value
- re-execute the work immediately (delay=0) if the work was canceled in the above, and do flush -- or just call the function directly
I agree and have implemented that as well.
Thanks,
Geoffrey.