mirror of
git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2025-08-05 16:54:27 +00:00
ALSA: usb-audio: Add mixer quirk for Sony DualSense PS5
The Sony DualSense wireless controller (PS5) features an internal mono speaker, but it also provides a 3.5mm jack socket for headphone output and headset microphone input. Since this is a UAC1 device, it doesn't advertise any jack detection capability. However, the controller is able to report HP & MIC insert events via HID, i.e. through a dedicated input device managed by the hid-playstation driver. Add a quirk to create the jack controls for headphone and headset mic, respectively, and setup an input handler for each of them in order to intercept the related hotplug events. Signed-off-by: Cristian Ciocaltea <cristian.ciocaltea@collabora.com> Signed-off-by: Takashi Iwai <tiwai@suse.de> Link: https://patch.msgid.link/20250526-dualsense-alsa-jack-v1-9-1a821463b632@collabora.com
This commit is contained in:
parent
9cea742559
commit
79d561c4ec
1 changed files with 263 additions and 0 deletions
|
@ -17,6 +17,7 @@
|
|||
#include <linux/bitfield.h>
|
||||
#include <linux/hid.h>
|
||||
#include <linux/init.h>
|
||||
#include <linux/input.h>
|
||||
#include <linux/math64.h>
|
||||
#include <linux/slab.h>
|
||||
#include <linux/usb.h>
|
||||
|
@ -531,6 +532,263 @@ static int snd_emu0204_controls_create(struct usb_mixer_interface *mixer)
|
|||
&snd_emu0204_control, NULL);
|
||||
}
|
||||
|
||||
/*
|
||||
* Sony DualSense controller (PS5) jack detection
|
||||
*
|
||||
* Since this is an UAC 1 device, it doesn't support jack detection.
|
||||
* However, the controller hid-playstation driver reports HP & MIC
|
||||
* insert events through a dedicated input device.
|
||||
*/
|
||||
|
||||
#define SND_DUALSENSE_JACK_OUT_TERM_ID 3
|
||||
#define SND_DUALSENSE_JACK_IN_TERM_ID 4
|
||||
|
||||
struct dualsense_mixer_elem_info {
|
||||
struct usb_mixer_elem_info info;
|
||||
struct input_handler ih;
|
||||
struct input_device_id id_table[2];
|
||||
bool connected;
|
||||
};
|
||||
|
||||
static void snd_dualsense_ih_event(struct input_handle *handle,
|
||||
unsigned int type, unsigned int code,
|
||||
int value)
|
||||
{
|
||||
struct dualsense_mixer_elem_info *mei;
|
||||
struct usb_mixer_elem_list *me;
|
||||
|
||||
if (type != EV_SW)
|
||||
return;
|
||||
|
||||
mei = container_of(handle->handler, struct dualsense_mixer_elem_info, ih);
|
||||
me = &mei->info.head;
|
||||
|
||||
if ((me->id == SND_DUALSENSE_JACK_OUT_TERM_ID && code == SW_HEADPHONE_INSERT) ||
|
||||
(me->id == SND_DUALSENSE_JACK_IN_TERM_ID && code == SW_MICROPHONE_INSERT)) {
|
||||
mei->connected = !!value;
|
||||
snd_ctl_notify(me->mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
|
||||
&me->kctl->id);
|
||||
}
|
||||
}
|
||||
|
||||
static bool snd_dualsense_ih_match(struct input_handler *handler,
|
||||
struct input_dev *dev)
|
||||
{
|
||||
struct dualsense_mixer_elem_info *mei;
|
||||
struct usb_device *snd_dev;
|
||||
char *input_dev_path, *usb_dev_path;
|
||||
size_t usb_dev_path_len;
|
||||
bool match = false;
|
||||
|
||||
mei = container_of(handler, struct dualsense_mixer_elem_info, ih);
|
||||
snd_dev = mei->info.head.mixer->chip->dev;
|
||||
|
||||
input_dev_path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
|
||||
if (!input_dev_path) {
|
||||
dev_warn(&snd_dev->dev, "Failed to get input dev path\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
usb_dev_path = kobject_get_path(&snd_dev->dev.kobj, GFP_KERNEL);
|
||||
if (!usb_dev_path) {
|
||||
dev_warn(&snd_dev->dev, "Failed to get USB dev path\n");
|
||||
goto free_paths;
|
||||
}
|
||||
|
||||
/*
|
||||
* Ensure the VID:PID matched input device supposedly owned by the
|
||||
* hid-playstation driver belongs to the actual hardware handled by
|
||||
* the current USB audio device, which implies input_dev_path being
|
||||
* a subpath of usb_dev_path.
|
||||
*
|
||||
* This verification is necessary when there is more than one identical
|
||||
* controller attached to the host system.
|
||||
*/
|
||||
usb_dev_path_len = strlen(usb_dev_path);
|
||||
if (usb_dev_path_len >= strlen(input_dev_path))
|
||||
goto free_paths;
|
||||
|
||||
usb_dev_path[usb_dev_path_len] = '/';
|
||||
match = !memcmp(input_dev_path, usb_dev_path, usb_dev_path_len + 1);
|
||||
|
||||
free_paths:
|
||||
kfree(input_dev_path);
|
||||
kfree(usb_dev_path);
|
||||
|
||||
return match;
|
||||
}
|
||||
|
||||
static int snd_dualsense_ih_connect(struct input_handler *handler,
|
||||
struct input_dev *dev,
|
||||
const struct input_device_id *id)
|
||||
{
|
||||
struct input_handle *handle;
|
||||
int err;
|
||||
|
||||
handle = kzalloc(sizeof(*handle), GFP_KERNEL);
|
||||
if (!handle)
|
||||
return -ENOMEM;
|
||||
|
||||
handle->dev = dev;
|
||||
handle->handler = handler;
|
||||
handle->name = handler->name;
|
||||
|
||||
err = input_register_handle(handle);
|
||||
if (err)
|
||||
goto err_free;
|
||||
|
||||
err = input_open_device(handle);
|
||||
if (err)
|
||||
goto err_unregister;
|
||||
|
||||
return 0;
|
||||
|
||||
err_unregister:
|
||||
input_unregister_handle(handle);
|
||||
err_free:
|
||||
kfree(handle);
|
||||
return err;
|
||||
}
|
||||
|
||||
static void snd_dualsense_ih_disconnect(struct input_handle *handle)
|
||||
{
|
||||
input_close_device(handle);
|
||||
input_unregister_handle(handle);
|
||||
kfree(handle);
|
||||
}
|
||||
|
||||
static void snd_dualsense_ih_start(struct input_handle *handle)
|
||||
{
|
||||
struct dualsense_mixer_elem_info *mei;
|
||||
struct usb_mixer_elem_list *me;
|
||||
int status = -1;
|
||||
|
||||
mei = container_of(handle->handler, struct dualsense_mixer_elem_info, ih);
|
||||
me = &mei->info.head;
|
||||
|
||||
if (me->id == SND_DUALSENSE_JACK_OUT_TERM_ID &&
|
||||
test_bit(SW_HEADPHONE_INSERT, handle->dev->swbit))
|
||||
status = test_bit(SW_HEADPHONE_INSERT, handle->dev->sw);
|
||||
else if (me->id == SND_DUALSENSE_JACK_IN_TERM_ID &&
|
||||
test_bit(SW_MICROPHONE_INSERT, handle->dev->swbit))
|
||||
status = test_bit(SW_MICROPHONE_INSERT, handle->dev->sw);
|
||||
|
||||
if (status >= 0) {
|
||||
mei->connected = !!status;
|
||||
snd_ctl_notify(me->mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
|
||||
&me->kctl->id);
|
||||
}
|
||||
}
|
||||
|
||||
static int snd_dualsense_jack_get(struct snd_kcontrol *kctl,
|
||||
struct snd_ctl_elem_value *ucontrol)
|
||||
{
|
||||
struct dualsense_mixer_elem_info *mei = snd_kcontrol_chip(kctl);
|
||||
|
||||
ucontrol->value.integer.value[0] = mei->connected;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const struct snd_kcontrol_new snd_dualsense_jack_control = {
|
||||
.iface = SNDRV_CTL_ELEM_IFACE_CARD,
|
||||
.access = SNDRV_CTL_ELEM_ACCESS_READ,
|
||||
.info = snd_ctl_boolean_mono_info,
|
||||
.get = snd_dualsense_jack_get,
|
||||
};
|
||||
|
||||
static int snd_dualsense_resume_jack(struct usb_mixer_elem_list *list)
|
||||
{
|
||||
snd_ctl_notify(list->mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
|
||||
&list->kctl->id);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void snd_dualsense_mixer_elem_free(struct snd_kcontrol *kctl)
|
||||
{
|
||||
struct dualsense_mixer_elem_info *mei = snd_kcontrol_chip(kctl);
|
||||
|
||||
if (mei->ih.event)
|
||||
input_unregister_handler(&mei->ih);
|
||||
|
||||
snd_usb_mixer_elem_free(kctl);
|
||||
}
|
||||
|
||||
static int snd_dualsense_jack_create(struct usb_mixer_interface *mixer,
|
||||
const char *name, bool is_output)
|
||||
{
|
||||
struct dualsense_mixer_elem_info *mei;
|
||||
struct input_device_id *idev_id;
|
||||
struct snd_kcontrol *kctl;
|
||||
int err;
|
||||
|
||||
mei = kzalloc(sizeof(*mei), GFP_KERNEL);
|
||||
if (!mei)
|
||||
return -ENOMEM;
|
||||
|
||||
snd_usb_mixer_elem_init_std(&mei->info.head, mixer,
|
||||
is_output ? SND_DUALSENSE_JACK_OUT_TERM_ID :
|
||||
SND_DUALSENSE_JACK_IN_TERM_ID);
|
||||
|
||||
mei->info.head.resume = snd_dualsense_resume_jack;
|
||||
mei->info.val_type = USB_MIXER_BOOLEAN;
|
||||
mei->info.channels = 1;
|
||||
mei->info.min = 0;
|
||||
mei->info.max = 1;
|
||||
|
||||
kctl = snd_ctl_new1(&snd_dualsense_jack_control, mei);
|
||||
if (!kctl) {
|
||||
kfree(mei);
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
strscpy(kctl->id.name, name, sizeof(kctl->id.name));
|
||||
kctl->private_free = snd_dualsense_mixer_elem_free;
|
||||
|
||||
err = snd_usb_mixer_add_control(&mei->info.head, kctl);
|
||||
if (err)
|
||||
return err;
|
||||
|
||||
idev_id = &mei->id_table[0];
|
||||
idev_id->flags = INPUT_DEVICE_ID_MATCH_VENDOR | INPUT_DEVICE_ID_MATCH_PRODUCT |
|
||||
INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_SWBIT;
|
||||
idev_id->vendor = USB_ID_VENDOR(mixer->chip->usb_id);
|
||||
idev_id->product = USB_ID_PRODUCT(mixer->chip->usb_id);
|
||||
idev_id->evbit[BIT_WORD(EV_SW)] = BIT_MASK(EV_SW);
|
||||
if (is_output)
|
||||
idev_id->swbit[BIT_WORD(SW_HEADPHONE_INSERT)] = BIT_MASK(SW_HEADPHONE_INSERT);
|
||||
else
|
||||
idev_id->swbit[BIT_WORD(SW_MICROPHONE_INSERT)] = BIT_MASK(SW_MICROPHONE_INSERT);
|
||||
|
||||
mei->ih.event = snd_dualsense_ih_event;
|
||||
mei->ih.match = snd_dualsense_ih_match;
|
||||
mei->ih.connect = snd_dualsense_ih_connect,
|
||||
mei->ih.disconnect = snd_dualsense_ih_disconnect,
|
||||
mei->ih.start = snd_dualsense_ih_start,
|
||||
mei->ih.name = name;
|
||||
mei->ih.id_table = mei->id_table;
|
||||
|
||||
err = input_register_handler(&mei->ih);
|
||||
if (err) {
|
||||
dev_warn(&mixer->chip->dev->dev,
|
||||
"Could not register input handler: %d\n", err);
|
||||
mei->ih.event = NULL;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int snd_dualsense_controls_create(struct usb_mixer_interface *mixer)
|
||||
{
|
||||
int err;
|
||||
|
||||
err = snd_dualsense_jack_create(mixer, "Headphone Jack", true);
|
||||
if (err < 0)
|
||||
return err;
|
||||
|
||||
return snd_dualsense_jack_create(mixer, "Headset Mic Jack", false);
|
||||
}
|
||||
|
||||
/* ASUS Xonar U1 / U3 controls */
|
||||
|
||||
static int snd_xonar_u1_switch_get(struct snd_kcontrol *kcontrol,
|
||||
|
@ -4073,6 +4331,11 @@ int snd_usb_mixer_apply_create_quirk(struct usb_mixer_interface *mixer)
|
|||
err = snd_emu0204_controls_create(mixer);
|
||||
break;
|
||||
|
||||
case USB_ID(0x054c, 0x0ce6): /* Sony DualSense controller (PS5) */
|
||||
case USB_ID(0x054c, 0x0df2): /* Sony DualSense Edge controller (PS5) */
|
||||
err = snd_dualsense_controls_create(mixer);
|
||||
break;
|
||||
|
||||
case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */
|
||||
case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C400 */
|
||||
err = snd_c400_create_mixer(mixer);
|
||||
|
|
Loading…
Add table
Reference in a new issue