Skip to content

Commit 104ad9b

Browse files
cristiccgregkh
authored andcommitted
ALSA: usb-audio: Add mixer quirk for Sony DualSense PS5
[ Upstream commit 79d561c ] 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 Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 731e676 commit 104ad9b

1 file changed

Lines changed: 263 additions & 0 deletions

File tree

sound/usb/mixer_quirks.c

Lines changed: 263 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include <linux/hid.h>
1818
#include <linux/init.h>
19+
#include <linux/input.h>
1920
#include <linux/math64.h>
2021
#include <linux/slab.h>
2122
#include <linux/usb.h>
@@ -526,6 +527,263 @@ static int snd_emu0204_controls_create(struct usb_mixer_interface *mixer)
526527
&snd_emu0204_control, NULL);
527528
}
528529

530+
/*
531+
* Sony DualSense controller (PS5) jack detection
532+
*
533+
* Since this is an UAC 1 device, it doesn't support jack detection.
534+
* However, the controller hid-playstation driver reports HP & MIC
535+
* insert events through a dedicated input device.
536+
*/
537+
538+
#define SND_DUALSENSE_JACK_OUT_TERM_ID 3
539+
#define SND_DUALSENSE_JACK_IN_TERM_ID 4
540+
541+
struct dualsense_mixer_elem_info {
542+
struct usb_mixer_elem_info info;
543+
struct input_handler ih;
544+
struct input_device_id id_table[2];
545+
bool connected;
546+
};
547+
548+
static void snd_dualsense_ih_event(struct input_handle *handle,
549+
unsigned int type, unsigned int code,
550+
int value)
551+
{
552+
struct dualsense_mixer_elem_info *mei;
553+
struct usb_mixer_elem_list *me;
554+
555+
if (type != EV_SW)
556+
return;
557+
558+
mei = container_of(handle->handler, struct dualsense_mixer_elem_info, ih);
559+
me = &mei->info.head;
560+
561+
if ((me->id == SND_DUALSENSE_JACK_OUT_TERM_ID && code == SW_HEADPHONE_INSERT) ||
562+
(me->id == SND_DUALSENSE_JACK_IN_TERM_ID && code == SW_MICROPHONE_INSERT)) {
563+
mei->connected = !!value;
564+
snd_ctl_notify(me->mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
565+
&me->kctl->id);
566+
}
567+
}
568+
569+
static bool snd_dualsense_ih_match(struct input_handler *handler,
570+
struct input_dev *dev)
571+
{
572+
struct dualsense_mixer_elem_info *mei;
573+
struct usb_device *snd_dev;
574+
char *input_dev_path, *usb_dev_path;
575+
size_t usb_dev_path_len;
576+
bool match = false;
577+
578+
mei = container_of(handler, struct dualsense_mixer_elem_info, ih);
579+
snd_dev = mei->info.head.mixer->chip->dev;
580+
581+
input_dev_path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
582+
if (!input_dev_path) {
583+
dev_warn(&snd_dev->dev, "Failed to get input dev path\n");
584+
return false;
585+
}
586+
587+
usb_dev_path = kobject_get_path(&snd_dev->dev.kobj, GFP_KERNEL);
588+
if (!usb_dev_path) {
589+
dev_warn(&snd_dev->dev, "Failed to get USB dev path\n");
590+
goto free_paths;
591+
}
592+
593+
/*
594+
* Ensure the VID:PID matched input device supposedly owned by the
595+
* hid-playstation driver belongs to the actual hardware handled by
596+
* the current USB audio device, which implies input_dev_path being
597+
* a subpath of usb_dev_path.
598+
*
599+
* This verification is necessary when there is more than one identical
600+
* controller attached to the host system.
601+
*/
602+
usb_dev_path_len = strlen(usb_dev_path);
603+
if (usb_dev_path_len >= strlen(input_dev_path))
604+
goto free_paths;
605+
606+
usb_dev_path[usb_dev_path_len] = '/';
607+
match = !memcmp(input_dev_path, usb_dev_path, usb_dev_path_len + 1);
608+
609+
free_paths:
610+
kfree(input_dev_path);
611+
kfree(usb_dev_path);
612+
613+
return match;
614+
}
615+
616+
static int snd_dualsense_ih_connect(struct input_handler *handler,
617+
struct input_dev *dev,
618+
const struct input_device_id *id)
619+
{
620+
struct input_handle *handle;
621+
int err;
622+
623+
handle = kzalloc(sizeof(*handle), GFP_KERNEL);
624+
if (!handle)
625+
return -ENOMEM;
626+
627+
handle->dev = dev;
628+
handle->handler = handler;
629+
handle->name = handler->name;
630+
631+
err = input_register_handle(handle);
632+
if (err)
633+
goto err_free;
634+
635+
err = input_open_device(handle);
636+
if (err)
637+
goto err_unregister;
638+
639+
return 0;
640+
641+
err_unregister:
642+
input_unregister_handle(handle);
643+
err_free:
644+
kfree(handle);
645+
return err;
646+
}
647+
648+
static void snd_dualsense_ih_disconnect(struct input_handle *handle)
649+
{
650+
input_close_device(handle);
651+
input_unregister_handle(handle);
652+
kfree(handle);
653+
}
654+
655+
static void snd_dualsense_ih_start(struct input_handle *handle)
656+
{
657+
struct dualsense_mixer_elem_info *mei;
658+
struct usb_mixer_elem_list *me;
659+
int status = -1;
660+
661+
mei = container_of(handle->handler, struct dualsense_mixer_elem_info, ih);
662+
me = &mei->info.head;
663+
664+
if (me->id == SND_DUALSENSE_JACK_OUT_TERM_ID &&
665+
test_bit(SW_HEADPHONE_INSERT, handle->dev->swbit))
666+
status = test_bit(SW_HEADPHONE_INSERT, handle->dev->sw);
667+
else if (me->id == SND_DUALSENSE_JACK_IN_TERM_ID &&
668+
test_bit(SW_MICROPHONE_INSERT, handle->dev->swbit))
669+
status = test_bit(SW_MICROPHONE_INSERT, handle->dev->sw);
670+
671+
if (status >= 0) {
672+
mei->connected = !!status;
673+
snd_ctl_notify(me->mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
674+
&me->kctl->id);
675+
}
676+
}
677+
678+
static int snd_dualsense_jack_get(struct snd_kcontrol *kctl,
679+
struct snd_ctl_elem_value *ucontrol)
680+
{
681+
struct dualsense_mixer_elem_info *mei = snd_kcontrol_chip(kctl);
682+
683+
ucontrol->value.integer.value[0] = mei->connected;
684+
685+
return 0;
686+
}
687+
688+
static const struct snd_kcontrol_new snd_dualsense_jack_control = {
689+
.iface = SNDRV_CTL_ELEM_IFACE_CARD,
690+
.access = SNDRV_CTL_ELEM_ACCESS_READ,
691+
.info = snd_ctl_boolean_mono_info,
692+
.get = snd_dualsense_jack_get,
693+
};
694+
695+
static int snd_dualsense_resume_jack(struct usb_mixer_elem_list *list)
696+
{
697+
snd_ctl_notify(list->mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
698+
&list->kctl->id);
699+
return 0;
700+
}
701+
702+
static void snd_dualsense_mixer_elem_free(struct snd_kcontrol *kctl)
703+
{
704+
struct dualsense_mixer_elem_info *mei = snd_kcontrol_chip(kctl);
705+
706+
if (mei->ih.event)
707+
input_unregister_handler(&mei->ih);
708+
709+
snd_usb_mixer_elem_free(kctl);
710+
}
711+
712+
static int snd_dualsense_jack_create(struct usb_mixer_interface *mixer,
713+
const char *name, bool is_output)
714+
{
715+
struct dualsense_mixer_elem_info *mei;
716+
struct input_device_id *idev_id;
717+
struct snd_kcontrol *kctl;
718+
int err;
719+
720+
mei = kzalloc(sizeof(*mei), GFP_KERNEL);
721+
if (!mei)
722+
return -ENOMEM;
723+
724+
snd_usb_mixer_elem_init_std(&mei->info.head, mixer,
725+
is_output ? SND_DUALSENSE_JACK_OUT_TERM_ID :
726+
SND_DUALSENSE_JACK_IN_TERM_ID);
727+
728+
mei->info.head.resume = snd_dualsense_resume_jack;
729+
mei->info.val_type = USB_MIXER_BOOLEAN;
730+
mei->info.channels = 1;
731+
mei->info.min = 0;
732+
mei->info.max = 1;
733+
734+
kctl = snd_ctl_new1(&snd_dualsense_jack_control, mei);
735+
if (!kctl) {
736+
kfree(mei);
737+
return -ENOMEM;
738+
}
739+
740+
strscpy(kctl->id.name, name, sizeof(kctl->id.name));
741+
kctl->private_free = snd_dualsense_mixer_elem_free;
742+
743+
err = snd_usb_mixer_add_control(&mei->info.head, kctl);
744+
if (err)
745+
return err;
746+
747+
idev_id = &mei->id_table[0];
748+
idev_id->flags = INPUT_DEVICE_ID_MATCH_VENDOR | INPUT_DEVICE_ID_MATCH_PRODUCT |
749+
INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_SWBIT;
750+
idev_id->vendor = USB_ID_VENDOR(mixer->chip->usb_id);
751+
idev_id->product = USB_ID_PRODUCT(mixer->chip->usb_id);
752+
idev_id->evbit[BIT_WORD(EV_SW)] = BIT_MASK(EV_SW);
753+
if (is_output)
754+
idev_id->swbit[BIT_WORD(SW_HEADPHONE_INSERT)] = BIT_MASK(SW_HEADPHONE_INSERT);
755+
else
756+
idev_id->swbit[BIT_WORD(SW_MICROPHONE_INSERT)] = BIT_MASK(SW_MICROPHONE_INSERT);
757+
758+
mei->ih.event = snd_dualsense_ih_event;
759+
mei->ih.match = snd_dualsense_ih_match;
760+
mei->ih.connect = snd_dualsense_ih_connect,
761+
mei->ih.disconnect = snd_dualsense_ih_disconnect,
762+
mei->ih.start = snd_dualsense_ih_start,
763+
mei->ih.name = name;
764+
mei->ih.id_table = mei->id_table;
765+
766+
err = input_register_handler(&mei->ih);
767+
if (err) {
768+
dev_warn(&mixer->chip->dev->dev,
769+
"Could not register input handler: %d\n", err);
770+
mei->ih.event = NULL;
771+
}
772+
773+
return 0;
774+
}
775+
776+
static int snd_dualsense_controls_create(struct usb_mixer_interface *mixer)
777+
{
778+
int err;
779+
780+
err = snd_dualsense_jack_create(mixer, "Headphone Jack", true);
781+
if (err < 0)
782+
return err;
783+
784+
return snd_dualsense_jack_create(mixer, "Headset Mic Jack", false);
785+
}
786+
529787
/* ASUS Xonar U1 / U3 controls */
530788

531789
static int snd_xonar_u1_switch_get(struct snd_kcontrol *kcontrol,
@@ -2381,6 +2639,11 @@ int snd_usb_mixer_apply_create_quirk(struct usb_mixer_interface *mixer)
23812639
err = snd_emu0204_controls_create(mixer);
23822640
break;
23832641

2642+
case USB_ID(0x054c, 0x0ce6): /* Sony DualSense controller (PS5) */
2643+
case USB_ID(0x054c, 0x0df2): /* Sony DualSense Edge controller (PS5) */
2644+
err = snd_dualsense_controls_create(mixer);
2645+
break;
2646+
23842647
case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */
23852648
case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C400 */
23862649
err = snd_c400_create_mixer(mixer);

0 commit comments

Comments
 (0)