[PATCH v3] HID: sony: support for ghlive ps3/wii u dongles

2020-11-25 Thread Pascal Giard
This commit adds support for the Guitar Hero Live PS3 and Wii U dongles.

These dongles require a "magic" USB control message [1] to be sent
approximately every 10 seconds otherwise the dongle will not report
events where the strumbar is hit while a fret is being held.

Also, inspired by a patch sent on linux-input by Sanjay Govind [2], the
accelerometer is mapped to ABS_RY for tilt.

Interestingly, the Wii U and PS3 dongles share the same VID and PID.

[1] https://github.com/ghlre/GHLtarUtility/
[2] https://marc.info/?l=linux-input&m=157242835928542&w=2

Signed-off-by: Pascal Giard 
---

Changes in v3:
* Only use one quirk as for now only the PS3/WiiU dongle is supported.

Changes in v2:
* Patches hid-sony instead of creating a new driver
* Changed memory allocation scheme in case of fail
---
 drivers/hid/Kconfig|   1 +
 drivers/hid/hid-ids.h  |   3 ++
 drivers/hid/hid-sony.c | 112 +
 3 files changed, 116 insertions(+)

diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig
index 05315b434276..3386f13e06d1 100644
--- a/drivers/hid/Kconfig
+++ b/drivers/hid/Kconfig
@@ -898,6 +898,7 @@ config HID_SONY
  * Buzz controllers
  * Sony PS3 Blue-ray Disk Remote Control (Bluetooth)
  * Logitech Harmony adapter for Sony Playstation 3 (Bluetooth)
+ * Guitar Hero Live PS3 and Wii U guitar dongles
 
 config SONY_FF
bool "Sony PS2/3/4 accessories force feedback support" 
diff --git a/drivers/hid/hid-ids.h b/drivers/hid/hid-ids.h
index 74fc1df6e3c2..75603262dd4e 100644
--- a/drivers/hid/hid-ids.h
+++ b/drivers/hid/hid-ids.h
@@ -1064,6 +1064,9 @@
 #define USB_DEVICE_ID_SONY_BUZZ_CONTROLLER 0x0002
 #define USB_DEVICE_ID_SONY_WIRELESS_BUZZ_CONTROLLER0x1000
 
+#define USB_VENDOR_ID_SONY_GHLIVE  0x12ba
+#define USB_DEVICE_ID_SONY_PS3WIIU_GHLIVE_DONGLE   0x074b
+
 #define USB_VENDOR_ID_SINO_LITE0x1345
 #define USB_DEVICE_ID_SINO_LITE_CONTROLLER 0x3008
 
diff --git a/drivers/hid/hid-sony.c b/drivers/hid/hid-sony.c
index 2f073f536070..08efe45dccbc 100644
--- a/drivers/hid/hid-sony.c
+++ b/drivers/hid/hid-sony.c
@@ -11,6 +11,7 @@
  *  Copyright (c) 2013 Colin Leitner 
  *  Copyright (c) 2014-2016 Frank Praznik 
  *  Copyright (c) 2018 Todd Kelner
+ *  Copyright (c) 2020 Pascal Giard 
  */
 
 /*
@@ -35,6 +36,8 @@
 #include 
 #include 
 #include 
+#include 
+#include 
 #include 
 
 #include "hid-ids.h"
@@ -56,6 +59,7 @@
 #define NSG_MR5U_REMOTE_BTBIT(14)
 #define NSG_MR7U_REMOTE_BTBIT(15)
 #define SHANWAN_GAMEPAD   BIT(16)
+#define GHL_GUITAR_PS3WIIUBIT(17)
 
 #define SIXAXIS_CONTROLLER (SIXAXIS_CONTROLLER_USB | SIXAXIS_CONTROLLER_BT)
 #define MOTION_CONTROLLER (MOTION_CONTROLLER_USB | MOTION_CONTROLLER_BT)
@@ -79,6 +83,17 @@
 #define NSG_MRXU_MAX_X 1667
 #define NSG_MRXU_MAX_Y 1868
 
+#define GHL_GUITAR_POKE_INTERVAL 10 /* In seconds */
+#define GHL_GUITAR_TILT_USAGE 44
+
+/* Magic value and data taken from GHLtarUtility:
+ * https://github.com/ghlre/GHLtarUtility/blob/master/PS3Guitar.cs
+ * Note: The Wii U and PS3 dongles happen to share the same!
+ */
+static const u16 ghl_ps3wiiu_magic_value = 0x201;
+static const char ghl_ps3wiiu_magic_data[] = {
+   0x02, 0x08, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00
+};
 
 /* PS/3 Motion controller */
 static u8 motion_rdesc[] = {
@@ -578,6 +593,10 @@ struct sony_sc {
enum ds4_dongle_state ds4_dongle_state;
/* DS4 calibration data */
struct ds4_calibration_data ds4_calib_data[6];
+   /* GH Live */
+   struct timer_list ghl_poke_timer;
+   struct usb_ctrlrequest *ghl_cr;
+   u8 *ghl_databuf;
 };
 
 static void sony_set_leds(struct sony_sc *sc);
@@ -601,6 +620,85 @@ static inline void sony_schedule_work(struct sony_sc *sc,
}
 }
 
+static void ghl_magic_poke_cb(struct urb *urb)
+{
+   if (urb) {
+   /* Free sc->ghl_cr and sc->ghl_databuf allocated in
+* ghl_magic_poke()
+*/
+   kfree(urb->setup_packet);
+   kfree(urb->transfer_buffer);
+   }
+}
+
+static void ghl_magic_poke(struct timer_list *t)
+{
+   struct sony_sc *sc = from_timer(sc, t, ghl_poke_timer);
+
+   int ret;
+   unsigned int pipe;
+   struct urb *urb;
+   struct usb_device *usbdev = to_usb_device(sc->hdev->dev.parent->parent);
+   const u16 poke_size =
+   ARRAY_SIZE(ghl_ps3wiiu_magic_data);
+
+   pipe = usb_sndctrlpipe(usbdev, 0);
+
+   if (!sc->ghl_cr) {
+   sc->ghl_cr = kzalloc(sizeof(*sc->ghl_cr), GFP_ATOMIC);
+   if (!sc->ghl_cr)
+   goto resched;
+   }
+
+   if (!sc->ghl_databuf) {
+   sc->ghl_databuf = kzalloc(poke_size, GFP_ATOMIC);
+   if (!sc->ghl_databuf)
+   goto resched;
+   }
+
+   ur

Re: [PATCH v2] HID: sony: support for ghlive ps3/wii u dongles

2020-11-25 Thread Pascal Giard
Apologies for the resend. I had not realized that gmail's app for
smartphones didn't really support plaintext.

On Wed, Nov 25, 2020 at 8:41 AM Jiri Kosina  wrote:
>
> On Wed, 25 Nov 2020, pascal.gi...@etsmtl.ca wrote:
>
> > Thank you for reviewing this new version.
> >
> > You are right, we could totally do without GHL_GUITAR_CONTROLLER.
> >
> > This can be seen as premature generalization or an excess of optimism;
> > I'm assuming that the PS4 also needs magic control messages to behave
> > correctly, and that I will figure those sooner than later. But I may be
> > assuming too much and this will be trivial to add when the time comes.
>
> Yeah, let's extend this only when really needed.

Fair.

> > Do you want me to submit a v3?
>
> Please do, thanks. I'll merge that one, I promise :) Sorry for not
> having catched this in v1 already.

No worries, will do.

Thanks again!

-Pascal


[PATCH v2] HID: sony: support for ghlive ps3/wii u dongles

2020-11-07 Thread Pascal Giard
This commit adds support for the Guitar Hero Live PS3 and Wii U dongles.

These dongles require a "magic" USB control message [1] to be sent
approximately every 10 seconds otherwise the dongle will not report
events where the strumbar is hit while a fret is being held.

Also, inspired by a patch sent on linux-input by Sanjay Govind [2], the
accelerometer is mapped to ABS_RY for tilt.

Interestingly, the Wii U and PS3 dongles share the same VID and PID.

[1] https://github.com/ghlre/GHLtarUtility/
[2] https://marc.info/?l=linux-input&m=157242835928542&w=2

Signed-off-by: Pascal Giard 
---
differences from v1:
* Patches hid-sony instead of creating a new driver
* Changed memory allocation scheme in case of fail
---
 drivers/hid/Kconfig|   1 +
 drivers/hid/hid-ids.h  |   3 ++
 drivers/hid/hid-sony.c | 115 +
 3 files changed, 119 insertions(+)

diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig
index 34f07371716d..e2df2ae112a5 100644
--- a/drivers/hid/Kconfig
+++ b/drivers/hid/Kconfig
@@ -897,6 +897,7 @@ config HID_SONY
  * Buzz controllers
  * Sony PS3 Blue-ray Disk Remote Control (Bluetooth)
  * Logitech Harmony adapter for Sony Playstation 3 (Bluetooth)
+ * Guitar Hero Live PS3 and Wii U guitar dongles
 
 config SONY_FF
bool "Sony PS2/3/4 accessories force feedback support" 
diff --git a/drivers/hid/hid-ids.h b/drivers/hid/hid-ids.h
index 1c71a1aa76b2..e3a3942079cf 100644
--- a/drivers/hid/hid-ids.h
+++ b/drivers/hid/hid-ids.h
@@ -1060,6 +1060,9 @@
 #define USB_DEVICE_ID_SONY_BUZZ_CONTROLLER 0x0002
 #define USB_DEVICE_ID_SONY_WIRELESS_BUZZ_CONTROLLER0x1000
 
+#define USB_VENDOR_ID_SONY_GHLIVE  0x12ba
+#define USB_DEVICE_ID_SONY_PS3WIIU_GHLIVE_DONGLE   0x074b
+
 #define USB_VENDOR_ID_SINO_LITE0x1345
 #define USB_DEVICE_ID_SINO_LITE_CONTROLLER 0x3008
 
diff --git a/drivers/hid/hid-sony.c b/drivers/hid/hid-sony.c
index 4c6ed6ef31f1..700bea6239f6 100644
--- a/drivers/hid/hid-sony.c
+++ b/drivers/hid/hid-sony.c
@@ -11,6 +11,7 @@
  *  Copyright (c) 2013 Colin Leitner 
  *  Copyright (c) 2014-2016 Frank Praznik 
  *  Copyright (c) 2018 Todd Kelner
+ *  Copyright (c) 2020 Pascal Giard 
  */
 
 /*
@@ -35,6 +36,8 @@
 #include 
 #include 
 #include 
+#include 
+#include 
 #include 
 
 #include "hid-ids.h"
@@ -56,6 +59,8 @@
 #define NSG_MR5U_REMOTE_BTBIT(14)
 #define NSG_MR7U_REMOTE_BTBIT(15)
 #define SHANWAN_GAMEPAD   BIT(16)
+#define GHL_GUITAR_PS3WIIUBIT(17)
+#define GHL_GUITAR_CONTROLLER BIT(18)
 
 #define SIXAXIS_CONTROLLER (SIXAXIS_CONTROLLER_USB | SIXAXIS_CONTROLLER_BT)
 #define MOTION_CONTROLLER (MOTION_CONTROLLER_USB | MOTION_CONTROLLER_BT)
@@ -79,6 +84,17 @@
 #define NSG_MRXU_MAX_X 1667
 #define NSG_MRXU_MAX_Y 1868
 
+#define GHL_GUITAR_POKE_INTERVAL 10 /* In seconds */
+#define GHL_GUITAR_TILT_USAGE 44
+
+/* Magic value and data taken from GHLtarUtility:
+ * https://github.com/ghlre/GHLtarUtility/blob/master/PS3Guitar.cs
+ * Note: The Wii U and PS3 dongles happen to share the same!
+ */
+static const u16 ghl_ps3wiiu_magic_value = 0x201;
+static const char ghl_ps3wiiu_magic_data[] = {
+   0x02, 0x08, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00
+};
 
 /* PS/3 Motion controller */
 static u8 motion_rdesc[] = {
@@ -578,6 +594,10 @@ struct sony_sc {
enum ds4_dongle_state ds4_dongle_state;
/* DS4 calibration data */
struct ds4_calibration_data ds4_calib_data[6];
+   /* GH Live */
+   struct timer_list ghl_poke_timer;
+   struct usb_ctrlrequest *ghl_cr;
+   u8 *ghl_databuf;
 };
 
 static void sony_set_leds(struct sony_sc *sc);
@@ -601,6 +621,87 @@ static inline void sony_schedule_work(struct sony_sc *sc,
}
 }
 
+static void ghl_magic_poke_cb(struct urb *urb)
+{
+   if (urb) {
+   /* Free sc->ghl_cr and sc->ghl_databuf allocated in
+* ghl_magic_poke()
+*/
+   kfree(urb->setup_packet);
+   kfree(urb->transfer_buffer);
+   }
+}
+
+static void ghl_magic_poke(struct timer_list *t)
+{
+   struct sony_sc *sc = from_timer(sc, t, ghl_poke_timer);
+
+   int ret;
+   unsigned int pipe;
+   struct urb *urb;
+   struct usb_device *usbdev = to_usb_device(sc->hdev->dev.parent->parent);
+   const u16 poke_size =
+   ARRAY_SIZE(ghl_ps3wiiu_magic_data);
+
+   pipe = usb_sndctrlpipe(usbdev, 0);
+
+   if (!sc->ghl_cr) {
+   sc->ghl_cr = kzalloc(sizeof(*sc->ghl_cr), GFP_ATOMIC);
+   if (!sc->ghl_cr)
+   goto resched;
+   }
+
+   if (!sc->ghl_databuf) {
+   sc->ghl_databuf = kzalloc(poke_size, GFP_ATOMIC);
+   if (!sc->ghl_databuf)
+   goto resched;
+   }
+
+   urb = usb_alloc_urb(0, GFP_A

Re: [PATCH] HID: ghlive: support for ghlive ps3/wii u dongles

2020-10-29 Thread Pascal Giard
On Thu, Oct 29, 2020 at 11:26 AM Jiri Kosina  wrote:
>
> On Thu, 8 Oct 2020, Pascal Giard wrote:
>
> > This commit introduces the Guitar Hero Live driver which adds support
> > for the PS3 and Wii U dongles.
>
> Pascal,
>
> thanks for the patch.
>

Dear Jiri,

thank you for reviewing my patch.

> [ ... snip ... ]
>
> > ---
> >  drivers/hid/Kconfig  |   6 ++
> >  drivers/hid/Makefile |   1 +
> >  drivers/hid/hid-ghlive.c | 220 +++
>
> Would it make more sense (with respect to how we are structuring/naming
> the hid drivers) to incorporate this into hid-sony (irrespective of
> currently ongoing discussions about actually splitting that driver :) )?
>

I think it would be most appropriate, yes.

Note that there are 3 other dongles out there:
- the xbox360 dongle does not need any special treatment, it just
works with hid-generic;
- the ps4 dongle obviously makes sense to go into hid-sony (although
no one has reversed engineered that one (yet));
- the xboxone dongle: that's an unknown one to me. I don't have any
information about that one unfortunately and do not own one.

I wrote this as a separate hid driver as I saw that email [1] from
Roderick Colenbrander in which he expressed a preference for a
seperate driver in cases where the device is not from Sony proper.

> > +static void ghl_magic_poke(struct timer_list *t)
> > +{
> > + struct ghlive_sc *sc = from_timer(sc, t, poke_timer);
> > +
> > + int ret;
> > + unsigned int pipe;
> > + struct urb *urb;
> > + struct usb_ctrlrequest *cr;
> > + const u16 poke_size =
> > + ARRAY_SIZE(ghl_ps3wiiu_magic_data);
> > + u8 *databuf;
> > +
> > + pipe = usb_sndctrlpipe(sc->usbdev, 0);
> > +
> > + cr = kzalloc(sizeof(*cr), GFP_ATOMIC);
> > + if (!cr)
> > + goto resched;
> > +
> > + databuf = kzalloc(poke_size, GFP_ATOMIC);
> > + if (!databuf) {
> > + kfree(cr);
> > + goto resched;
> > + }
> > +
> > + urb = usb_alloc_urb(0, GFP_ATOMIC);
> > + if (!urb) {
> > + kfree(databuf);
> > + kfree(cr);
> > + goto resched;
>
>
> So if one of the allocations above succeeds and a subsequent one fails,
> you're going to try re-allocate all of them next time again, leaking the
> ones that previously succeeded, right?
>

I attempted to avoid such a case. IIUC there are 4 possible scenarios
tied to those 3 allocs (cr, databuf, and urb):
1) alloc of cr fails. nothing to be freed, we reschedule;
2) alloc of cr succeeds, alloc of databuf fails. we free cr and we reschedule;
3) allocs of cr and databuf succeed, alloc of urb fails. we free cr
and databuf, and we reschedule;
4) all allocs succeed, we submit the urb, and free urb. once the
control packet is sent, the callback is called and we free cr and
databuf.

Am I missing something? It's VERY possible that I am as this is my
first patch and I wrote this by looking at other peoples' code. The
only thing that comes to mind is if poke is called again before the
control packet actually gets sent (and the callback called), in which
case I'm not sure what would happen. But with a poke interval of 10
seconds, is that probability close enough to 0 to be ignored?

Thanks again for your valuable input, :-)

-Pascal
[1] https://marc.info/?l=linux-input&m=157273970001101&w=2


[PATCH] HID: ghlive: support for ghlive ps3/wii u dongles

2020-10-08 Thread Pascal Giard
This commit introduces the Guitar Hero Live driver which adds support
for the PS3 and Wii U dongles.

These dongles require a "magic" USB control message [1] to be sent
approximately every 10 seconds otherwise the dongle will not report
events where the strumbar is hit while a fret is being held.

Also, inspired by a patch sent on linux-input by Sanjay Govind [2], the
accelerometer is mapped to ABS_RY for tilt.

Interestingly, the Wii U and PS3 dongles share the same VID and PID.

[1] https://github.com/ghlre/GHLtarUtility/
[2] https://marc.info/?l=linux-input&m=157242835928542&w=2

Signed-off-by: Pascal Giard 
---
 drivers/hid/Kconfig  |   6 ++
 drivers/hid/Makefile |   1 +
 drivers/hid/hid-ghlive.c | 220 +++
 drivers/hid/hid-ids.h|   3 +
 4 files changed, 230 insertions(+)
 create mode 100644 drivers/hid/hid-ghlive.c

diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig
index 34f07371716d..0bf8dd1629f7 100644
--- a/drivers/hid/Kconfig
+++ b/drivers/hid/Kconfig
@@ -362,6 +362,12 @@ config HID_GFRM
---help---
Support for Google Fiber TV Box remote controls
 
+config HID_GHLIVE
+   tristate "Guitar Hero Live PS3/Wii U support"
+   depends on HID
+   help
+ Support for the Guitar Hero Live PS3 and Wii U guitar devices.
+
 config HID_GLORIOUS
tristate "Glorious PC Gaming Race mice"
depends on HID
diff --git a/drivers/hid/Makefile b/drivers/hid/Makefile
index d8ea4b8c95af..6394f5bbf8a5 100644
--- a/drivers/hid/Makefile
+++ b/drivers/hid/Makefile
@@ -48,6 +48,7 @@ obj-$(CONFIG_HID_ELO) += hid-elo.o
 obj-$(CONFIG_HID_EZKEY)+= hid-ezkey.o
 obj-$(CONFIG_HID_GEMBIRD)  += hid-gembird.o
 obj-$(CONFIG_HID_GFRM) += hid-gfrm.o
+obj-$(CONFIG_HID_GHLIVE)   += hid-ghlive.o
 obj-$(CONFIG_HID_GLORIOUS)  += hid-glorious.o
 obj-$(CONFIG_HID_GOOGLE_HAMMER)+= hid-google-hammer.o
 obj-$(CONFIG_HID_GT683R)   += hid-gt683r.o
diff --git a/drivers/hid/hid-ghlive.c b/drivers/hid/hid-ghlive.c
new file mode 100644
index ..db5814aff17f
--- /dev/null
+++ b/drivers/hid/hid-ghlive.c
@@ -0,0 +1,220 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * HID driver for Guitar Hero Live PS3 and Wii U Guitar devices.
+ *
+ * Copyright (c) 2020 Pascal Giard 
+ */
+
+#include 
+#include 
+#include 
+#include 
+
+#include "hid-ids.h"
+
+MODULE_AUTHOR("Pascal Giard ");
+MODULE_LICENSE("GPL");
+MODULE_DESCRIPTION("HID driver for Activision GH Live PS3 and Wii U Guitar 
devices");
+
+#define GHL_GUITAR_PS3WIIU  BIT(2)
+#define GHL_GUITAR_CONTROLLER   BIT(1)
+
+#define GHL_GUITAR_POKE_INTERVAL 10 /* In seconds */
+
+#define GHL_GUITAR_TILT_USAGE 44
+
+/* Magic value and data taken from GHLtarUtility:
+ * https://github.com/ghlre/GHLtarUtility/blob/master/PS3Guitar.cs
+ * Note: The Wii U and PS3 dongles happen to share the same!
+ */
+static const u16 ghl_ps3wiiu_magic_value = 0x201;
+static const char ghl_ps3wiiu_magic_data[] = {
+   0x02, 0x08, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00
+};
+
+struct ghlive_sc {
+   struct hid_device *hdev;
+   struct usb_device *usbdev;
+   unsigned long quirks;
+   int device_id;
+   unsigned int poke_current;
+   struct timer_list poke_timer;
+};
+
+static void ghl_magic_poke_cb(struct urb *urb)
+{
+   if (urb) {
+   /* Free cr and databuf allocated in ghl_magic_poke() */
+   kfree(urb->setup_packet);
+   kfree(urb->transfer_buffer);
+   }
+}
+
+static void ghl_magic_poke(struct timer_list *t)
+{
+   struct ghlive_sc *sc = from_timer(sc, t, poke_timer);
+
+   int ret;
+   unsigned int pipe;
+   struct urb *urb;
+   struct usb_ctrlrequest *cr;
+   const u16 poke_size =
+   ARRAY_SIZE(ghl_ps3wiiu_magic_data);
+   u8 *databuf;
+
+   pipe = usb_sndctrlpipe(sc->usbdev, 0);
+
+   cr = kzalloc(sizeof(*cr), GFP_ATOMIC);
+   if (!cr)
+   goto resched;
+
+   databuf = kzalloc(poke_size, GFP_ATOMIC);
+   if (!databuf) {
+   kfree(cr);
+   goto resched;
+   }
+
+   urb = usb_alloc_urb(0, GFP_ATOMIC);
+   if (!urb) {
+   kfree(databuf);
+   kfree(cr);
+   goto resched;
+   }
+
+   if (sc->quirks & (GHL_GUITAR_CONTROLLER | GHL_GUITAR_PS3WIIU)) {
+   cr->bRequestType =
+   USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT;
+   cr->bRequest = USB_REQ_SET_CONFIGURATION;
+   cr->wValue = cpu_to_le16(ghl_ps3wiiu_magic_value);
+   cr->wIndex = 0;
+   cr->wLength = cpu_to_le16(poke_size);
+   memcpy(databuf, ghl_ps3wiiu_magic_data, poke_size);
+
+   usb_fill_control_urb(
+   urb, sc->usbdev, pipe,
+