HID:nintendo
Subsystems (2)
Daniel J. Ogorchock
maintainerv10 cover HID:nintendo
Version 10 changes:
- Removed duplicate reporting of one of the triggers that Billy noticed
- The joy-cons now only report having the buttons they actually have (they used to register the input devices with the buttons of the other joy-con as well).
- The input device is now created after the LEDs/power supply.
- The removed state handling bool has been removed, instead opting to add a new controller state (removed).
- Eliminated a 1 second delay when probing a USB controller.
- Added support for the IMU. This mostly consisted of merging in some work provided by Carl. I'm not incredibly familiar with proper gyro/accelerometer handling in linux, so this might need some tweaking. Preliminary tests in evtest show the gyro/accel values being reported.
- Added support for the joy-con USB charging grip.
Version 9 changes:
- Fixed compiler errors on gcc versions older than 8.2
- Set input device's uniq value to the controller's MAC address
Version 8 changes:
- Corrected the handshaking protocol with USB pro controllers. A handshake now occurs both prior and after the baudrate set. This doesn't appear to have a noticeable difference, but it more accurately follows documentation found online.
- Fixed potential race condition which could lead to a slightly longer delay sending subcommands in rare circumstances.
- Moved the rumble worker to its own workqueue, since it can block. This prevents it from having a negative impact on the default kernel workqueue. It also prevents dropped subcommands due to something else blocking the kernel workqueue. The benefit is most obvious when using multiple controllers at once, since the controller subcommand timings are very picky.
- Added a patch to set the most significant bit of the hid hw version. Roderick had mentioned needing to probably do this awhile ago, but I had forgotten about it until now. This is the same thing hid-sony does. It allows SDL2 to have different mappings for the hid-nintendo driver vs the default hid mappings.
Version 7 changes:
- Changed name to hid-nintendo to fit modern naming conventions
- Removed joycon_ctlr_destroy(), since it wasn't needed an could potentially invalidate a mutex while it could be in use on other threads
- Implemented minor code improvements suggested by Silvan
- The driver now waits to send subcommands until after receiving an input report. This significantly reduces dropped subcommands.
- Reduced the number of error messages when disconnecting a controller.
Version 6 changes:
- Improved subcommand sending reliabilty
- Decreased rumble period to 50ms
- Added rumble queue to avoid missing ff_effects if sent too quickly
- Code cleanup and minor refactoring
- Added default analog stick calibration
Version 5 changes:
- Removed sysfs interface to control motor frequencies.
- Improved rumble reliability by using subcommands to set it.
- Changed mapping of the SL/SR triggers on the joy-cons to map to whichever triggers they lack (e.g. a left joycon's sl/sr map to TR and TR2). This allows userspace to distinguish between the normal and S triggers.
- Minor refactors
Version 4 changes:
- Added support for the Home button LED for the pro controller and right joy-con
- Changed name from hid-switchcon to hid-joycon
- Added rumble support
- Removed ctlr->type and use hdev->product instead
- Use POWER_SUPPLY_CAPACITY_LEVEL enum instead of manually translating to capacity percentages
- Misc. minor refactors based on v3 feedback
Version 3 changes:
- Added led_classdev support for the 4 player LEDs
- Added power_supply support for the controller's battery
- Made the controller number mutex static
- Minor refactoring/style fixes based on Roderick's feedback from v2
Version 2 changes:
- Switched to using a synchronous method for configuring the controller.
- Removed any pairing/orientation logic in the driver. Every controller now corresponds to its own input device.
- Store controller button data as a single u32.
- Style corrections
Daniel J. Ogorchock (12):
HID: nintendo: add nintendo switch controller driver
HID: nintendo: add player led support
HID: nintendo: add power supply support
HID: nintendo: add home led support
HID: nintendo: add rumble support
HID: nintendo: improve subcommand reliability
HID: nintendo: send subcommands after receiving input report
HID: nintendo: reduce device removal subcommand errors
HID: nintendo: patch hw version for userspace HID mappings
HID: nintendo: set controller uniq to MAC
HID: nintendo: add IMU support
HID: nintendo: add support for charging grip
MAINTAINERS | 6 + drivers/hid/Kconfig | 24 + drivers/hid/Makefile | 1 + drivers/hid/hid-ids.h | 4 + drivers/hid/hid-nintendo.c | 1867 ++++++++++++++++++++++++++++++++++++ 5 files changed, 1902 insertions(+) create mode 100644 drivers/hid/hid-nintendo.c
Signature · 1 line
2.24.1
Daniel J. Ogorchock
maintainerPATCH v10 1/12 HID: nintendo: add nintendo switch controller driver
The hid-nintendo driver supports the Nintendo Switch Pro Controllers and the Joy-Cons. The Pro Controllers can be used over USB or Bluetooth.
The Joy-Cons each create their own, independent input devices, so it is up to userspace to combine them if desired.
--- MAINTAINERS | 6 + drivers/hid/Kconfig | 11 + drivers/hid/Makefile | 1 + drivers/hid/hid-ids.h | 3 + drivers/hid/hid-nintendo.c | 822 +++++++++++++++++++++++++++++++++++++ 5 files changed, 843 insertions(+) create mode 100644 drivers/hid/hid-nintendo.c
MAINTAINERS +6 -0
@@ -11504,6 +11504,12 @@ S: Maintained F: Documentation/scsi/NinjaSCSI.txt F: drivers/scsi/nsp32* +NINTENDO HID DRIVER +M: Daniel J. Ogorchock <[email protected]> +L: [email protected] +S: Maintained +F: drivers/hid/hid-nintendo* + NIOS2 ARCHITECTURE M: Ley Foon Tan <[email protected]> L: [email protected] (moderated for non-subscribers)
drivers/hid/Kconfig +11 -0
@@ -701,6 +701,17 @@ config HID_MULTITOUCH To compile this driver as a module, choose M here: the module will be called hid-multitouch. +config HID_NINTENDO + tristate "Nintendo Joy-Con and Pro Controller support" + depends on HID + help + Adds support for the Nintendo Switch Joy-Cons and Pro Controller. + All controllers support bluetooth, and the Pro Controller also supports + its USB mode. + + To compile this driver as a module, choose M here: the + module will be called hid-nintendo. + config HID_NTI tristate "NTI keyboard adapters" ---help---
drivers/hid/Makefile +1 -0
@@ -73,6 +73,7 @@ obj-$(CONFIG_HID_MAYFLASH) += hid-mf.o obj-$(CONFIG_HID_MICROSOFT) += hid-microsoft.o obj-$(CONFIG_HID_MONTEREY) += hid-monterey.o obj-$(CONFIG_HID_MULTITOUCH) += hid-multitouch.o +obj-$(CONFIG_HID_NINTENDO) += hid-nintendo.o obj-$(CONFIG_HID_NTI) += hid-nti.o obj-$(CONFIG_HID_NTRIG) += hid-ntrig.o obj-$(CONFIG_HID_ORTEK) += hid-ortek.o
drivers/hid/hid-ids.h +3 -0
@@ -868,6 +868,9 @@#define USB_VENDOR_ID_NINTENDO 0x057e#define USB_DEVICE_ID_NINTENDO_WIIMOTE 0x0306#define USB_DEVICE_ID_NINTENDO_WIIMOTE2 0x0330 +#define USB_DEVICE_ID_NINTENDO_JOYCONL 0x2006 +#define USB_DEVICE_ID_NINTENDO_JOYCONR 0x2007 +#define USB_DEVICE_ID_NINTENDO_PROCON 0x2009#define USB_VENDOR_ID_NOVATEK 0x0603#define USB_DEVICE_ID_NOVATEK_PCT 0x0600
drivers/hid/hid-nintendo.c new file +822 -0
822 lines not on this page · raw message
Signature · 1 line
2.24.1
Re: [PATCH v10 01/12] HID: nintendo: add nintendo switch controller driver
Quoted 7 lines · Daniel J. Ogorchock
The hid-nintendo driver supports the Nintendo Switch Pro Controllers and the Joy-Cons. The Pro Controllers can be used over USB or Bluetooth.
The Joy-Cons each create their own, independent input devices, so it is up to userspace to combine them if desired.
Signed-off-by Daniel J. Ogorchock <[email protected]>
Quoted 8 lines
--- MAINTAINERS | 6 + drivers/hid/Kconfig | 11 + drivers/hid/Makefile | 1 + drivers/hid/hid-ids.h | 3 + drivers/hid/hid-nintendo.c | 822 +++++++++++++++++++++++++++++++++++++ 5 files changed, 843 insertions(+) create mode 100644 drivers/hid/hid-nintendo.c
Looks like a lot of effort was put into this.
What is the current status? Are you still pursuing?
Signature · 4 lines
Lee Jones [李琼斯] Senior Technical Lead - Developer Services Linaro.org │ Open source software for Arm SoCs Follow Linaro: Facebook | Twitter | Blog
Re: [PATCH v10 01/12] HID: nintendo: add nintendo switch controller driver
Hi
2021. május 24., hétfő 18:34 keltezéssel, Lee Jones írta:
Quoted 17 lines · Daniel J. Ogorchock
Quoted 7 lines
The hid-nintendo driver supports the Nintendo Switch Pro Controllers and the Joy-Cons. The Pro Controllers can be used over USB or Bluetooth. The Joy-Cons each create their own, independent input devices, so it is up to userspace to combine them if desired.
Signed-off-by Daniel J. Ogorchock [email protected]Quoted 8 lines
---
MAINTAINERS| 6 +drivers/hid/Kconfig| 11 +drivers/hid/Makefile| 1 +drivers/hid/hid-ids.h| 3 +drivers/hid/hid-nintendo.c| 822 +++++++++++++++++++++++++++++++++++++5 files changed, 843 insertions(+)create mode 100644
drivers/hid/hid-nintendo.c
Quoted 3 lines
Looks like a lot of effort was put into this.
What is the current status? Are you still pursuing?
The thirteenth version was submitted just four days ago:
https://lore.kernel.org/linux-input/[email protected]/
Regards,
Barnabás Pőcze
Daniel J. Ogorchock
maintainerPATCH v10 2/12 HID: nintendo: add player led support
In reply to Daniel J. OgorchockThis patch adds led_classdev functionality to the switch controller driver. It adds support for the 4 player LEDs. The Home Button LED still needs to be supported on the pro controllers and right joy-con.
--- drivers/hid/Kconfig | 2 + drivers/hid/hid-nintendo.c | 95 +++++++++++++++++++++++++++++++++++++- 2 files changed, 95 insertions(+), 2 deletions(-)
drivers/hid/Kconfig +2 -0
@@ -704,6 +704,8 @@ config HID_MULTITOUCH config HID_NINTENDO tristate "Nintendo Joy-Con and Pro Controller support" depends on HID + depends on NEW_LEDS + depends on LEDS_CLASS help Adds support for the Nintendo Switch Joy-Cons and Pro Controller. All controllers support bluetooth, and the Pro Controller also supports
drivers/hid/hid-nintendo.c +93 -2
139 lines not on this page · raw message
Signature · 1 line
2.24.1
Daniel J. Ogorchock
maintainerPATCH v10 3/12 HID: nintendo: add power supply support
In reply to Daniel J. OgorchockThis patch adds power_supply functionality to the switch controller driver for its battery.
--- drivers/hid/Kconfig | 1 + drivers/hid/hid-nintendo.c | 133 +++++++++++++++++++++++++++++++++++++ 2 files changed, 134 insertions(+)
drivers/hid/Kconfig +1 -0
@@ -706,6 +706,7 @@ config HID_NINTENDO depends on HID depends on NEW_LEDS depends on LEDS_CLASS + select POWER_SUPPLY help Adds support for the Nintendo Switch Joy-Cons and Pro Controller. All controllers support bluetooth, and the Pro Controller also supports
drivers/hid/hid-nintendo.c +133 -0
184 lines not on this page · raw message
Signature · 1 line
2.24.1
Daniel J. Ogorchock
maintainerPATCH v10 4/12 HID: nintendo: add home led support
In reply to Daniel J. OgorchockThis patch adds the ability to set the intensity level of the home button's LED.
--- drivers/hid/hid-nintendo.c | 69 +++++++++++++++++++++++++++++++++++--- 1 file changed, 65 insertions(+), 4 deletions(-)
drivers/hid/hid-nintendo.c +65 -4
@@ -187,7 +187,8 @@ struct joycon_input_report {struct joycon_ctlr {struct hid_device *hdev;struct input_dev *input; -struct led_classdev leds[JC_NUM_LEDS ]; +struct led_classdev leds[JC_NUM_LEDS ];/* player leds */ +struct led_classdev home_led;enum joycon_ctlr_state ctlr_state;spinlock_t lock;
@@ -675,6 +676,40 @@ static int joycon_player_led_brightness_set(struct led_classdev *led,return ret; } +static int joycon_home_led_brightness_set (struct led_classdev *led, +enum led_brightness brightness) +{ +struct device *dev = led->dev->parent; +struct hid_device *hdev =to_hid_device (dev); +struct joycon_ctlr *ctlr; +struct joycon_subcmd_request *req; +u8 buffer[sizeof (*req) +5 ] = {0 }; +u8 *data; +int ret; + + ctlr =hid_get_drvdata (hdev); +if (!ctlr) { +hid_err (hdev,"No controller data\n" ); +return -ENODEV ; + } + + req = (struct joycon_subcmd_request *)buffer; + req->subcmd_id =JC_SUBCMD_SET_HOME_LIGHT ; + data = req->data; + data[0 ] =0x01 ; + data[1 ] = brightness <<4 ; + data[2 ] = brightness | (brightness <<4 ); + data[3 ] =0x11 ; + data[4 ] =0x11 ; + +hid_dbg (hdev,"setting home led brightness\n" ); +mutex_lock (&ctlr->output_mutex); + ret =joycon_send_subcmd (ctlr, req,5 ); +mutex_unlock (&ctlr->output_mutex); + +return ret; +} +static const char *const joycon_player_led_names[] = {"player1" ,"player2" ,
@@ -683,7 +718,7 @@ static const char * const joycon_player_led_names[] = { };static DEFINE_MUTEX (joycon_input_num_mutex); -static int joycon_player_leds_create (struct joycon_ctlr *ctlr) +static int joycon_leds_create (struct joycon_ctlr *ctlr) {struct hid_device *hdev = ctlr->hdev;struct device *dev = &hdev->dev;
@@ -720,7 +755,7 @@ static int joycon_player_leds_create(struct joycon_ctlr *ctlr) ret =devm_led_classdev_register (&hdev->dev, led);if (ret) {hid_err (hdev,"Failed registering %s LED\n" , led->name); -break ; +return ret; } }
@@ -728,6 +763,32 @@ static int joycon_player_leds_create(struct joycon_ctlr *ctlr) input_num =1 ;mutex_unlock (&joycon_input_num_mutex); +/* configure the home LED */ +if (ctlr->hdev->product !=USB_DEVICE_ID_NINTENDO_JOYCONL ) { + name =devm_kasprintf (dev,GFP_KERNEL ,"%s:%s" , d_name,"home" ); +if (!name) +return ret; + + led = &ctlr->home_led; + led->name = name; + led->brightness =0 ; + led->max_brightness =0xF ; + led->brightness_set_blocking = joycon_home_led_brightness_set; + led->flags =LED_CORE_SUSPENDRESUME |LED_HW_PLUGGABLE ; + ret =devm_led_classdev_register (&hdev->dev, led); +if (ret) { +hid_err (hdev,"Failed registering home led\n" ); +return ret; + } +/* Set the home LED to 0 as default state */ + ret =joycon_home_led_brightness_set (led,0 ); +if (ret) { +hid_err (hdev,"Failed to set home LED dflt; ret=%d\n" , + ret); +return ret; + } + } +return 0 ; }
@@ -975,7 +1036,7 @@ static int nintendo_hid_probe(struct hid_device *hdev,mutex_unlock (&ctlr->output_mutex);/* Initialize the leds */ - ret =joycon_player_leds_create (ctlr); + ret =joycon_leds_create (ctlr);if (ret) {hid_err (hdev,"Failed to create leds; ret=%d\n" , ret); goto err_close;
Signature · 1 line
2.24.1
Daniel J. Ogorchock
maintainerPATCH v10 5/12 HID: nintendo: add rumble support
In reply to Daniel J. OgorchockThis patch adds support for controller rumble.
The ff_effect weak magnitude is associated with the pro controller's right motor (or with a right joy-con). The strong magnitude is associated with the pro's left motor (or a left joy-con).
The rumble data is sent periodically (currently configured for every 50 milliseconds). If the controller receives no rumble data for too long a time period, it will stop vibrating. The data is also sent every time joycon_set_rumble is called to avoid latency of up to 50ms.
Because the rumble subcommands are sent in a deferred workqueue (they can't be sent in the play_effect function due to the hid send sleeping), the effects are queued. This ensures that no rumble effect is missed due to them arriving in too quick of succession.
--- drivers/hid/Kconfig | 10 ++ drivers/hid/hid-nintendo.c | 347 ++++++++++++++++++++++++++++++++++++- 2 files changed, 354 insertions(+), 3 deletions(-)
drivers/hid/Kconfig +10 -0
@@ -715,6 +715,16 @@ config HID_NINTENDO To compile this driver as a module, choose M here: the module will be called hid-nintendo. +config NINTENDO_FF + bool "Nintendo Switch controller force feedback support" + depends on HID_NINTENDO + select INPUT_FF_MEMLESS + help + Say Y here if you have a Nintendo Switch controller and want to enable + force feedback support for it. This works for both joy-cons and the pro + controller. For the pro controller, both rumble motors can be controlled + individually. + config HID_NTI tristate "NTI keyboard adapters" ---help---
drivers/hid/hid-nintendo.c +344 -3
447 lines not on this page · raw message
Signature · 1 line
2.24.1
Daniel J. Ogorchock
maintainerPATCH v10 6/12 HID: nintendo: improve subcommand reliability
In reply to Daniel J. OgorchockThe controller occasionally doesn't respond to subcommands. It appears that it's dropping them. To improve reliability, this patch attempts one retry in the case of a synchronous send timeout. In testing, this has resolved all timeout failures (most common for LED setting and rumble setting subcommands).
The 1 second timeout is excessively long for rumble and LED subcommands, so the timeout has been made a param for joycon_hid_send_sync. Most subcommands continue to use the 1s timeout, since they can result in long response times. Rumble and LED setting subcommands have been reduced to 250ms, since response times for them are much quicker (and this significantly reduces the observable impact in the case of a retry being required).
--- drivers/hid/hid-nintendo.c | 66 ++++++++++++++++++++++++-------------- 1 file changed, 42 insertions(+), 24 deletions(-)
drivers/hid/hid-nintendo.c +42 -24
@@ -365,27 +365,45 @@ static int __joycon_hid_send(struct hid_device *hdev, u8 *data, size_t len)return ret; } -static int joycon_hid_send_sync (struct joycon_ctlr *ctlr,u8 *data,size_t len) +static int joycon_hid_send_sync (struct joycon_ctlr *ctlr,u8 *data,size_t len, +u32 timeout) {int ret; +int tries =2 ; - ret =__joycon_hid_send (ctlr->hdev, data, len); -if (ret <0 ) { -memset (ctlr->input_buf,0 ,JC_MAX_RESP_SIZE ); -return ret; - } +/* +* The controller occasionally seems to drop subcommands. In testing, +* doing one retry after a timeout appears to always work. +*/ +while (tries--) { + ret =__joycon_hid_send (ctlr->hdev, data, len); +if (ret <0 ) { +memset (ctlr->input_buf,0 ,JC_MAX_RESP_SIZE ); +return ret; + } -if (!wait_event_timeout (ctlr->wait, ctlr->received_resp,HZ )) { -hid_dbg (ctlr->hdev,"synchronous send/receive timed out\n" ); -memset (ctlr->input_buf,0 ,JC_MAX_RESP_SIZE ); -return -ETIMEDOUT ; + ret =wait_event_timeout (ctlr->wait, ctlr->received_resp, + timeout); +if (!ret) { +hid_dbg (ctlr->hdev, +"synchronous send/receive timed out\n" ); +if (tries) { +hid_dbg (ctlr->hdev, +"retrying sync send after timeout\n" ); + } +memset (ctlr->input_buf,0 ,JC_MAX_RESP_SIZE ); + ret = -ETIMEDOUT ; + }else { + ret =0 ; +break ; + } } ctlr->received_resp = false; -return 0 ; +return ret; } -static int joycon_send_usb (struct joycon_ctlr *ctlr,u8 cmd) +static int joycon_send_usb (struct joycon_ctlr *ctlr,u8 cmd,u32 timeout) {int ret;u8 buf[2 ] = {JC_OUTPUT_USB_CMD };
@@ -393,7 +411,7 @@ static int joycon_send_usb(struct joycon_ctlr *ctlr, u8 cmd) buf[1 ] = cmd; ctlr->usb_ack_match = cmd; ctlr->msg_type =JOYCON_MSG_TYPE_USB ; - ret =joycon_hid_send_sync (ctlr, buf,sizeof (buf)); + ret =joycon_hid_send_sync (ctlr, buf,sizeof (buf), timeout);if (ret)hid_dbg (ctlr->hdev,"send usb command failed; ret=%d\n" , ret);return ret;
@@ -401,7 +419,7 @@ static int joycon_send_usb(struct joycon_ctlr *ctlr, u8 cmd)static int joycon_send_subcmd (struct joycon_ctlr *ctlr,struct joycon_subcmd_request *subcmd, -size_t data_len) +size_t data_len,u32 timeout) {int ret;unsigned long flags;
@@ -419,7 +437,7 @@ static int joycon_send_subcmd(struct joycon_ctlr *ctlr, ctlr->msg_type =JOYCON_MSG_TYPE_SUBCMD ; ret =joycon_hid_send_sync (ctlr, (u8 *)subcmd, -sizeof (*subcmd) + data_len); +sizeof (*subcmd) + data_len, timeout);if (ret <0 )hid_dbg (ctlr->hdev,"send subcommand failed; ret=%d\n" , ret);else
@@ -438,7 +456,7 @@ static int joycon_set_player_leds(struct joycon_ctlr *ctlr, u8 flash, u8 on) req->data[0 ] = (flash <<4 ) | on;hid_dbg (ctlr->hdev,"setting player leds\n" ); -return joycon_send_subcmd (ctlr, req,1 ); +return joycon_send_subcmd (ctlr, req,1 ,HZ /4 ); }static const u16 DFLT_STICK_CAL_CEN =2000 ;
@@ -469,7 +487,7 @@ static int joycon_request_calibration(struct joycon_ctlr *ctlr) data[4 ] =JC_CAL_DATA_SIZE ;hid_dbg (ctlr->hdev,"requesting cal data\n" ); - ret =joycon_send_subcmd (ctlr, req,5 ); + ret =joycon_send_subcmd (ctlr, req,5 ,HZ );if (ret) { hid_warn(ctlr->hdev, "Failed to read stick cal, using defaults; ret=%d\n",
@@ -559,7 +577,7 @@ static int joycon_set_report_mode(struct joycon_ctlr *ctlr) req->data[0 ] =0x30 ;/* standard, full report mode */ hid_dbg (ctlr->hdev,"setting controller report mode\n" ); -return joycon_send_subcmd (ctlr, req,1 ); +return joycon_send_subcmd (ctlr, req,1 ,HZ ); }static int joycon_enable_rumble (struct joycon_ctlr *ctlr,bool enable)
@@ -572,7 +590,7 @@ static int joycon_enable_rumble(struct joycon_ctlr *ctlr, bool enable) req->data[0 ] = enable ?0x01 :0x00 ;hid_dbg (ctlr->hdev,"%s rumble\n" , enable ?"enabling" :"disabling" ); -return joycon_send_subcmd (ctlr, req,1 ); +return joycon_send_subcmd (ctlr, req,1 ,HZ /4 ); }static s32 joycon_map_stick_val (struct joycon_stick_cal *cal,s32 val)
@@ -1030,7 +1048,7 @@ static int joycon_home_led_brightness_set(struct led_classdev *led,hid_dbg (hdev,"setting home led brightness\n" );mutex_lock (&ctlr->output_mutex); - ret =joycon_send_subcmd (ctlr, req,5 ); + ret =joycon_send_subcmd (ctlr, req,5 ,HZ /4 );mutex_unlock (&ctlr->output_mutex);return ret;
@@ -1326,16 +1344,16 @@ static int nintendo_hid_probe(struct hid_device *hdev,mutex_lock (&ctlr->output_mutex);/* if handshake command fails, assume ble pro controller */ if (hdev->product ==USB_DEVICE_ID_NINTENDO_PROCON && - !joycon_send_usb (ctlr,JC_USB_CMD_HANDSHAKE )) { + !joycon_send_usb (ctlr,JC_USB_CMD_HANDSHAKE ,HZ )) {hid_dbg (hdev,"detected USB controller\n" );/* set baudrate for improved latency */ - ret =joycon_send_usb (ctlr,JC_USB_CMD_BAUDRATE_3M ); + ret =joycon_send_usb (ctlr,JC_USB_CMD_BAUDRATE_3M ,HZ );if (ret) {hid_err (hdev,"Failed to set baudrate; ret=%d\n" , ret); goto err_mutex; }/* handshake */ - ret =joycon_send_usb (ctlr,JC_USB_CMD_HANDSHAKE ); + ret =joycon_send_usb (ctlr,JC_USB_CMD_HANDSHAKE ,HZ );if (ret) {hid_err (hdev,"Failed handshake; ret=%d\n" , ret); goto err_mutex;
@@ -1344,7 +1362,7 @@ static int nintendo_hid_probe(struct hid_device *hdev,* Set no timeout (to keep controller in USB mode). * This doesn't send a response, so ignore the timeout. */ -joycon_send_usb (ctlr,JC_USB_CMD_NO_TIMEOUT ); +joycon_send_usb (ctlr,JC_USB_CMD_NO_TIMEOUT ,HZ /10 ); }/* get controller calibration data, and parse it */
Signature · 1 line
2.24.1
Daniel J. Ogorchock
maintainerPATCH v10 7/12 HID: nintendo: send subcommands after receiving input report
In reply to Daniel J. OgorchockWaiting to send subcommands until right after receiving an input report drastically improves subcommand reliability. If the driver has finished initial controller configuration, it now waits until receiving an input report for all subcommands.
--- drivers/hid/hid-nintendo.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+)
drivers/hid/hid-nintendo.c +33 -0
@@ -323,6 +323,7 @@ struct joycon_ctlr {bool received_resp;u8 usb_ack_match;u8 subcmd_ack_match; +bool received_input_report;/* factory calibration data */ struct joycon_stick_cal left_stick_cal_x;
@@ -376,6 +377,26 @@ static int joycon_hid_send_sync(struct joycon_ctlr *ctlr, u8 *data, size_t len,* doing one retry after a timeout appears to always work. */ while (tries--) { +/* +* If we are in the proper reporting mode, wait for an input +* report prior to sending the subcommand. This improves +* reliability considerably. +*/ +if (ctlr->ctlr_state ==JOYCON_CTLR_STATE_READ ) { +unsigned long flags; + +spin_lock_irqsave (&ctlr->lock, flags); + ctlr->received_input_report = false; +spin_unlock_irqrestore (&ctlr->lock, flags); + ret =wait_event_timeout (ctlr->wait, + ctlr->received_input_report, +HZ /4 ); +/* We will still proceed, even with a timeout here */ +if (!ret) +hid_warn (ctlr->hdev, +"timeout waiting for input report\n" ); + } + ret =__joycon_hid_send (ctlr->hdev, data, len);if (ret <0 ) {memset (ctlr->input_buf,0 ,JC_MAX_RESP_SIZE );
@@ -725,6 +746,18 @@ static void joycon_parse_report(struct joycon_ctlr *ctlr, }input_sync (dev); + +/* +* Immediately after receiving a report is the most reliable time to +* send a subcommand to the controller. Wake any subcommand senders +* waiting for a report. +*/ +if (unlikely (mutex_is_locked (&ctlr->output_mutex))) { +spin_lock_irqsave (&ctlr->lock, flags); + ctlr->received_input_report = true; +spin_unlock_irqrestore (&ctlr->lock, flags); +wake_up (&ctlr->wait); + } }static void joycon_rumble_worker (struct work_struct *work)
Signature · 1 line
2.24.1
Daniel J. Ogorchock
maintainerPATCH v10 8/12 HID: nintendo: reduce device removal subcommand errors
In reply to Daniel J. OgorchockThis patch fixes meaningless error output from trying to send subcommands immediately after controller removal. It now disables subcommands as soon as possible on removal.
--- drivers/hid/hid-nintendo.c | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-)
drivers/hid/hid-nintendo.c +24 -2
@@ -225,6 +225,7 @@ static const struct joycon_rumble_amp_data joycon_rumble_amplitudes[] = {enum joycon_ctlr_state {JOYCON_CTLR_STATE_INIT ,JOYCON_CTLR_STATE_READ , +JOYCON_CTLR_STATE_REMOVED , };struct joycon_stick_cal {
@@ -391,10 +392,12 @@ static int joycon_hid_send_sync(struct joycon_ctlr *ctlr, u8 *data, size_t len, ret =wait_event_timeout (ctlr->wait, ctlr->received_input_report,HZ /4 ); +spin_lock_irqsave (&ctlr->lock, flags);/* We will still proceed, even with a timeout here */ if (!ret)hid_warn (ctlr->hdev,"timeout waiting for input report\n" ); +spin_unlock_irqrestore (&ctlr->lock, flags); } ret =__joycon_hid_send (ctlr->hdev, data, len);
@@ -446,6 +449,14 @@ static int joycon_send_subcmd(struct joycon_ctlr *ctlr,unsigned long flags;spin_lock_irqsave (&ctlr->lock, flags); +/* +* If the controller has been removed, just return ENODEV so the LED +* subsystem doesn't print invalid errors on removal. +*/ +if (ctlr->ctlr_state ==JOYCON_CTLR_STATE_REMOVED ) { +spin_unlock_irqrestore (&ctlr->lock, flags); +return -ENODEV ; + }memcpy (subcmd->rumble_data, ctlr->rumble_data[ctlr->rumble_queue_tail],JC_RUMBLE_DATA_SIZE );spin_unlock_irqrestore (&ctlr->lock, flags);
@@ -772,10 +783,13 @@ static void joycon_rumble_worker(struct work_struct *work)mutex_lock (&ctlr->output_mutex); ret =joycon_enable_rumble (ctlr, true);mutex_unlock (&ctlr->output_mutex); -if (ret <0 ) -hid_warn (ctlr->hdev,"Failed to set rumble; e=%d" , ret); +/* -ENODEV means the controller was just unplugged */ spin_lock_irqsave (&ctlr->lock, flags); +if (ret <0 && ret != -ENODEV && + ctlr->ctlr_state !=JOYCON_CTLR_STATE_REMOVED ) +hid_warn (ctlr->hdev,"Failed to set rumble; e=%d" , ret); + ctlr->rumble_msecs =jiffies_to_msecs (jiffies);if (ctlr->rumble_queue_tail != ctlr->rumble_queue_head) {if (++ctlr->rumble_queue_tail >=JC_RUMBLE_QUEUE_SIZE )
@@ -1465,9 +1479,17 @@ static int nintendo_hid_probe(struct hid_device *hdev,static void nintendo_hid_remove (struct hid_device *hdev) {struct joycon_ctlr *ctlr =hid_get_drvdata (hdev); +unsigned long flags;hid_dbg (hdev,"remove\n" ); + +/* Prevent further attempts at sending subcommands. */ +spin_lock_irqsave (&ctlr->lock, flags); + ctlr->ctlr_state =JOYCON_CTLR_STATE_REMOVED ; +spin_unlock_irqrestore (&ctlr->lock, flags); +destroy_workqueue (ctlr->rumble_queue); +hid_hw_close (hdev);hid_hw_stop (hdev); }
Signature · 1 line
2.24.1
Daniel J. Ogorchock
maintainerPATCH v10 9/12 HID: nintendo: patch hw version for userspace HID mappings
In reply to Daniel J. OgorchockThis patch sets the most significant bit of the hid hw version to allow userspace to distinguish between this driver's input mappings vs. the default hid mappings. This prevents breaking userspace applications that use SDL2 for gamepad input, allowing them to distinguish the mappings based on the version.
--- drivers/hid/hid-nintendo.c | 9 +++++++++ 1 file changed, 9 insertions(+)
drivers/hid/hid-nintendo.c +9 -0
@@ -1373,6 +1373,15 @@ static int nintendo_hid_probe(struct hid_device *hdev, goto err_wq; } +/* +* Patch the hw version of pro controller/joycons, so applications can +* distinguish between the default HID mappings and the mappings defined +* by the Linux game controller spec. This is important for the SDL2 +* library, which has a game controller database, which uses device ids +* in combination with version as a key. +*/ + hdev->version |=0x8000 ; + ret =hid_hw_start (hdev,HID_CONNECT_HIDRAW );if (ret) {hid_err (hdev,"HW start failed\n" );
Signature · 1 line
2.24.1
Daniel J. Ogorchock
maintainerPATCH v10 10/12 HID: nintendo: set controller uniq to MAC
In reply to Daniel J. OgorchockThis patch sets the input device's uniq identifier to the controller's MAC address. This is useful for future association between an IMU input device with the normal input device as well as associating the controller with any serial joy-con driver.
--- drivers/hid/hid-nintendo.c | 45 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+)
drivers/hid/hid-nintendo.c +45 -0
@@ -314,6 +314,8 @@ struct joycon_ctlr {struct led_classdev home_led;enum joycon_ctlr_state ctlr_state;spinlock_t lock; +u8 mac_addr[6 ]; +char *mac_addr_str;/* The following members are used for synchronous sends/receives */ enum joycon_msg_type msg_type;
@@ -975,6 +977,7 @@ static int joycon_input_create(struct joycon_ctlr *ctlr) ctlr->input->id.vendor = hdev->vendor; ctlr->input->id.product = hdev->product; ctlr->input->id.version = hdev->version; + ctlr->input->uniq = ctlr->mac_addr_str; ctlr->input->name = name;input_set_drvdata (ctlr->input, ctlr);
@@ -1267,6 +1270,41 @@ static int joycon_power_supply_create(struct joycon_ctlr *ctlr)return 0 ; } +static int joycon_read_mac (struct joycon_ctlr *ctlr) +{ +int ret; +int i; +int j; +struct joycon_subcmd_request req = {0 }; +struct joycon_input_report *report; + + req.subcmd_id =JC_SUBCMD_REQ_DEV_INFO ; + ret =joycon_send_subcmd (ctlr, &req,0 ,HZ ); +if (ret) { +hid_err (ctlr->hdev,"Failed to get joycon info; ret=%d\n" , ret); +return ret; + } + + report = (struct joycon_input_report *)ctlr->input_buf; + +for (i =4 , j =0 ; j <6 ; i++, j++) + ctlr->mac_addr[j] = report->reply.data[i]; + + ctlr->mac_addr_str =devm_kasprintf (&ctlr->hdev->dev,GFP_KERNEL , +"%02X:%02X:%02X:%02X:%02X:%02X" , + ctlr->mac_addr[0 ], + ctlr->mac_addr[1 ], + ctlr->mac_addr[2 ], + ctlr->mac_addr[3 ], + ctlr->mac_addr[4 ], + ctlr->mac_addr[5 ]); +if (!ctlr->mac_addr_str) +return -ENOMEM ; +hid_info (ctlr->hdev,"controller MAC = %s\n" , ctlr->mac_addr_str); + +return 0 ; +} +/* Common handler for parsing inputs */ static int joycon_ctlr_read_handler (struct joycon_ctlr *ctlr,u8 *data,int size)
@@ -1445,6 +1483,13 @@ static int nintendo_hid_probe(struct hid_device *hdev, goto err_mutex; } + ret =joycon_read_mac (ctlr); +if (ret) { +hid_err (hdev,"Failed to retrieve controller MAC; ret=%d\n" , + ret); + goto err_close; + } +mutex_unlock (&ctlr->output_mutex);/* Initialize the leds */
Signature · 1 line
2.24.1
Daniel J. Ogorchock
maintainerPATCH v10 11/12 HID: nintendo: add IMU support
In reply to Daniel J. OgorchockThis patch adds support for the controller's IMU. The accelerometer and gyro data are both provided to userspace using a second input device. The devices can be associated using their uniq value (set to the controller's MAC address).
The majority of this patch's functinoality was provided by Carl Mueller.
--- drivers/hid/hid-nintendo.c | 267 +++++++++++++++++++++++++++++++++++-- 1 file changed, 259 insertions(+), 8 deletions(-)
drivers/hid/hid-nintendo.c +259 -8
@@ -101,12 +101,29 @@ static const u16 JC_CAL_DATA_START = 0x603d;static const u16 JC_CAL_DATA_END =0x604e ;#define JC_CAL_DATA_SIZE (JC_CAL_DATA_END - JC_CAL_DATA_START + 1) +/* SPI storage addresses of IMU factory calibration data */ +static const u16 JC_IMU_CAL_DATA_START =0x6020 ; +static const u16 JC_IMU_CAL_DATA_END =0x6037 ; +#define JC_IMU_CAL_DATA_SIZE \ + (JC_IMU_CAL_DATA_END - JC_IMU_CAL_DATA_START + 1)/* The raw analog joystick values will be mapped in terms of this magnitude */ static const u16 JC_MAX_STICK_MAG =32767 ;static const u16 JC_STICK_FUZZ =250 ;static const u16 JC_STICK_FLAT =500 ; +/* The accel axes will be mapped in terms of this magnitude */ +static const u16 JC_MAX_ACCEL_MAG =32767 ; +static const u16 JC_ACCEL_RES =4096 ; +static const u16 JC_ACCEL_FUZZ =10 ; +static const u16 JC_ACCEL_FLAT /*= 0*/ ; + +/* The gyro axes will be mapped in terms of this magnitude */ +static const u16 JC_MAX_GYRO_MAG =32767 ; +static const u16 JC_GYRO_RES =13371 /936 ;/* 14 (14.285) */ +static const u16 JC_GYRO_FUZZ =10 ; +static const u16 JC_GYRO_FLAT /*= 0*/ ; +/* frequency/amplitude tables for rumble */ struct joycon_rumble_freq_data {u16 high;
@@ -234,6 +251,11 @@ struct joycon_stick_cal {s32 center; }; +struct joycon_imu_cal { +s16 offset[3 ]; +s16 scale[3 ]; +}; +/* * All the controller's button values are stored in a u32. * They can be accessed with bitwise ANDs.
@@ -281,6 +303,19 @@ struct joycon_subcmd_reply {u8 data[0 ];/* will be at most 35 bytes */ } __packed; +struct joycon_imu_data { +s16 accel_x; +s16 accel_y; +s16 accel_z; +s16 gyro_x; +s16 gyro_y; +s16 gyro_z; +} __packed; + +struct joycon_imu_report { +struct joycon_imu_data data[3 ]; +} __packed; +struct joycon_input_report {u8 id;u8 timer;
@@ -290,11 +325,10 @@ struct joycon_input_report {u8 right_stick[3 ];u8 vibrator_report; -/* -* If support for firmware updates, gyroscope data, and/or NFC/IR -* are added in the future, this can be swapped for a union. -*/ -struct joycon_subcmd_reply reply; +union { +struct joycon_subcmd_reply subcmd_reply; +struct joycon_imu_report imu_report; + }; } __packed;#define JC_MAX_RESP_SIZE (sizeof(struct joycon_input_report) + 35)
@@ -334,6 +368,9 @@ struct joycon_ctlr {struct joycon_stick_cal right_stick_cal_x;struct joycon_stick_cal right_stick_cal_y; +struct joycon_imu_cal accel_cal; +struct joycon_imu_cal gyro_cal; +/* power supply data */ struct power_supply *battery;struct power_supply_desc battery_desc;
@@ -352,6 +389,10 @@ struct joycon_ctlr {u16 rumble_lh_freq;u16 rumble_rl_freq;u16 rumble_rh_freq; + +/* imu */ +struct input_dev *imu_input; +int timestamp; };static int __joycon_hid_send (struct hid_device *hdev,u8 *data,size_t len)
@@ -547,7 +588,7 @@ static int joycon_request_calibration(struct joycon_ctlr *ctlr) } report = (struct joycon_input_report *)ctlr->input_buf; - raw_cal = &report->reply.data[5 ]; + raw_cal = &report->subcmd_reply.data[5 ];/* left stick calibration parsing */ cal_x = &ctlr->left_stick_cal_x;
@@ -601,6 +642,85 @@ static int joycon_request_calibration(struct joycon_ctlr *ctlr)return 0 ; } +static const s16 DFLT_ACCEL_OFFSET /*= 0*/ ; +static const s16 DFLT_ACCEL_SCALE =16384 ; +static const s16 DFLT_GYRO_OFFSET /*= 0*/ ; +static const s16 DFLT_GYRO_SCALE =13371 ; +static int joycon_request_imu_calibration (struct joycon_ctlr *ctlr) +{ +struct joycon_subcmd_request *req; +u8 buffer[sizeof (*req) +5 ] = {0 }; +struct joycon_input_report *report; +u8 *data; +u8 *raw_cal; +int ret; +int i; + +/* request IMU calibration data */ + req = (struct joycon_subcmd_request *)buffer; + req->subcmd_id =JC_SUBCMD_SPI_FLASH_READ ; + data = req->data; + data[0 ] =0xFF &JC_IMU_CAL_DATA_START ; + data[1 ] =0xFF & (JC_IMU_CAL_DATA_START >>8 ); + data[2 ] =0xFF & (JC_IMU_CAL_DATA_START >>16 ); + data[3 ] =0xFF & (JC_IMU_CAL_DATA_START >>24 ); + data[4 ] =JC_IMU_CAL_DATA_SIZE ; + +hid_dbg (ctlr->hdev,"requesting IMU cal data\n" ); + ret =joycon_send_subcmd (ctlr, req,5 ,HZ ); + +if (ret) { +hid_warn (ctlr->hdev, +"Failed to read IMU cal, using defaults; ret=%d\n" , + ret); + +for (i =0 ; i <3 ; i++) { + ctlr->accel_cal.offset[i] =DFLT_ACCEL_OFFSET ; + ctlr->accel_cal.scale[i] =DFLT_ACCEL_SCALE ; + ctlr->gyro_cal.offset[i] =DFLT_GYRO_OFFSET ; + ctlr->gyro_cal.scale[i] =DFLT_GYRO_SCALE ; + } +return ret; + } + + report = (struct joycon_input_report *)ctlr->input_buf; + raw_cal = &report->subcmd_reply.data[5 ]; + +/* IMU calibration parsing */ +for (i =0 ; i <3 ; i++) { +int j = i *2 ; + + ctlr->accel_cal.offset[i] = raw_cal[j +0 ] | + ((s16 )raw_cal[j +1 ] <<8 ); + ctlr->accel_cal.scale[i] = raw_cal[j +6 ] | + ((s16 )raw_cal[j +7 ] <<8 ); + ctlr->gyro_cal.offset[i] = raw_cal[j +12 ] | + ((s16 )raw_cal[j +13 ] <<8 ); + ctlr->gyro_cal.scale[i] = raw_cal[j +18 ] | + ((s16 )raw_cal[j +19 ] <<8 ); + } + +hid_dbg (ctlr->hdev,"IMU calibration:\n" +"a_o[0]=%d a_o[1]=%d a_o[2]=%d\n" +"a_s[0]=%d a_s[1]=%d a_s[2]=%d\n" +"g_o[0]=%d g_o[1]=%d g_o[2]=%d\n" +"g_s[0]=%d g_s[1]=%d g_s[2]=%d\n" , + ctlr->accel_cal.offset[0 ], + ctlr->accel_cal.offset[1 ], + ctlr->accel_cal.offset[2 ], + ctlr->accel_cal.scale[0 ], + ctlr->accel_cal.scale[1 ], + ctlr->accel_cal.scale[2 ], + ctlr->gyro_cal.offset[0 ], + ctlr->gyro_cal.offset[1 ], + ctlr->gyro_cal.offset[2 ], + ctlr->gyro_cal.scale[0 ], + ctlr->gyro_cal.scale[1 ], + ctlr->gyro_cal.scale[2 ]); + +return 0 ; +} +static int joycon_set_report_mode (struct joycon_ctlr *ctlr) {struct joycon_subcmd_request *req;
@@ -627,6 +747,19 @@ static int joycon_enable_rumble(struct joycon_ctlr *ctlr, bool enable)return joycon_send_subcmd (ctlr, req,1 ,HZ /4 ); } +static int joycon_enable_imu (struct joycon_ctlr *ctlr,bool enable) +{ +struct joycon_subcmd_request *req; +u8 buffer[sizeof (*req) +1 ] = {0 }; + + req = (struct joycon_subcmd_request *)buffer; + req->subcmd_id =JC_SUBCMD_ENABLE_IMU ; + req->data[0 ] = enable ?0x01 :0x00 ; + +hid_dbg (ctlr->hdev,"%s IMU\n" , enable ?"enabling" :"disabling" ); +return joycon_send_subcmd (ctlr, req,1 ,HZ ); +} +static s32 joycon_map_stick_val (struct joycon_stick_cal *cal,s32 val) {s32 center = cal->center;
@@ -771,6 +904,54 @@ static void joycon_parse_report(struct joycon_ctlr *ctlr,spin_unlock_irqrestore (&ctlr->lock, flags);wake_up (&ctlr->wait); } + +/* parse IMU data if present */ +if (rep->id ==JC_INPUT_IMU_DATA ) { +struct joycon_imu_data *imu_data = rep->imu_report.data; +struct input_dev *idev = ctlr->imu_input; +int i; +int value[6 ]; + +for (i =0 ; i <3 ; i++) {/* there are 3 reports */ + ctlr->timestamp +=5000 ;/* each is 5 ms apart */ +input_event (idev,EV_MSC ,MSC_TIMESTAMP , + ctlr->timestamp); + +/* +* Rather than convert to floats, we adjust by +* calibration factors and then multiply by the default +* scaling values. This way, the consumer only needs to +* divide by the default scale values. +*/ + value[0 ] = (imu_data[i].gyro_x - + ctlr->gyro_cal.offset[0 ]) * +DFLT_GYRO_SCALE / ctlr->gyro_cal.scale[0 ]; + value[1 ] = (imu_data[i].gyro_y - + ctlr->gyro_cal.offset[1 ]) * +DFLT_GYRO_SCALE / ctlr->gyro_cal.scale[1 ]; + value[2 ] = (imu_data[i].gyro_z - + ctlr->gyro_cal.offset[2 ]) * +DFLT_GYRO_SCALE / ctlr->gyro_cal.scale[2 ]; + + value[3 ] = (imu_data[i].accel_x - + ctlr->accel_cal.offset[0 ]) * +DFLT_ACCEL_SCALE / ctlr->accel_cal.scale[0 ]; + value[4 ] = (imu_data[i].accel_y - + ctlr->accel_cal.offset[1 ]) * +DFLT_ACCEL_SCALE / ctlr->accel_cal.scale[1 ]; + value[5 ] = (imu_data[i].accel_z - + ctlr->accel_cal.offset[2 ]) * +DFLT_ACCEL_SCALE / ctlr->accel_cal.scale[2 ]; + +input_report_abs (idev,ABS_RX , value[0 ]); +input_report_abs (idev,ABS_RY , value[1 ]); +input_report_abs (idev,ABS_RZ , value[2 ]); +input_report_abs (idev,ABS_X , value[3 ]); +input_report_abs (idev,ABS_Y , value[4 ]); +input_report_abs (idev,ABS_Z , value[5 ]); +input_sync (idev); + } + } }static void joycon_rumble_worker (struct work_struct *work)
@@ -950,6 +1131,7 @@ static int joycon_input_create(struct joycon_ctlr *ctlr) {struct hid_device *hdev;const char *name; +const char *imu_name;int ret;int i;
@@ -958,12 +1140,15 @@ static int joycon_input_create(struct joycon_ctlr *ctlr)switch (hdev->product) {case USB_DEVICE_ID_NINTENDO_PROCON : name ="Nintendo Switch Pro Controller" ; + imu_name ="Nintendo Switch Pro Controller IMU" ;break ;case USB_DEVICE_ID_NINTENDO_JOYCONL : name ="Nintendo Switch Left Joy-Con" ; + imu_name ="Nintendo Switch Left Joy-Con IMU" ;break ;case USB_DEVICE_ID_NINTENDO_JOYCONR : name ="Nintendo Switch Right Joy-Con" ; + imu_name ="Nintendo Switch Right Joy-Con IMU" ;break ;default :/* Should be impossible */ hid_err (hdev,"Invalid hid product\n" );
@@ -1029,6 +1214,55 @@ static int joycon_input_create(struct joycon_ctlr *ctlr)if (ret)return ret; +/* configure the imu input device */ + ctlr->imu_input =devm_input_allocate_device (&hdev->dev); +if (!ctlr->imu_input) +return -ENOMEM ; + + ctlr->imu_input->id.bustype = hdev->bus; + ctlr->imu_input->id.vendor = hdev->vendor; + ctlr->imu_input->id.product = hdev->product; + ctlr->imu_input->id.version = hdev->version; + ctlr->imu_input->uniq = ctlr->mac_addr_str; + ctlr->imu_input->name = imu_name; +input_set_drvdata (ctlr->imu_input, ctlr); + +/* configure imu axes */ +input_set_abs_params (ctlr->imu_input,ABS_X , + -JC_MAX_ACCEL_MAG ,JC_MAX_ACCEL_MAG , +JC_ACCEL_FUZZ ,JC_ACCEL_FLAT ); +input_set_abs_params (ctlr->imu_input,ABS_Y , + -JC_MAX_ACCEL_MAG ,JC_MAX_ACCEL_MAG , +JC_ACCEL_FUZZ ,JC_ACCEL_FLAT ); +input_set_abs_params (ctlr->imu_input,ABS_Z , + -JC_MAX_ACCEL_MAG ,JC_MAX_ACCEL_MAG , +JC_ACCEL_FUZZ ,JC_ACCEL_FLAT ); +input_abs_set_res (ctlr->imu_input,ABS_X ,JC_ACCEL_RES ); +input_abs_set_res (ctlr->imu_input,ABS_Y ,JC_ACCEL_RES ); +input_abs_set_res (ctlr->imu_input,ABS_Z ,JC_ACCEL_RES ); + +input_set_abs_params (ctlr->imu_input,ABS_RX , + -JC_MAX_GYRO_MAG ,JC_MAX_GYRO_MAG , +JC_GYRO_FUZZ ,JC_GYRO_FLAT ); +input_set_abs_params (ctlr->imu_input,ABS_RY , + -JC_MAX_GYRO_MAG ,JC_MAX_GYRO_MAG , +JC_GYRO_FUZZ ,JC_GYRO_FLAT ); +input_set_abs_params (ctlr->imu_input,ABS_RZ , + -JC_MAX_GYRO_MAG ,JC_MAX_GYRO_MAG , +JC_GYRO_FUZZ ,JC_GYRO_FLAT ); + +input_abs_set_res (ctlr->imu_input,ABS_RX ,JC_GYRO_RES ); +input_abs_set_res (ctlr->imu_input,ABS_RY ,JC_GYRO_RES ); +input_abs_set_res (ctlr->imu_input,ABS_RZ ,JC_GYRO_RES ); + +__set_bit (EV_MSC , ctlr->imu_input->evbit); +__set_bit (MSC_TIMESTAMP , ctlr->imu_input->mscbit); +__set_bit (INPUT_PROP_ACCELEROMETER , ctlr->imu_input->propbit); + + ret =input_register_device (ctlr->imu_input); +if (ret) +return ret; +return 0 ; }
@@ -1288,7 +1522,7 @@ static int joycon_read_mac(struct joycon_ctlr *ctlr) report = (struct joycon_input_report *)ctlr->input_buf;for (i =4 , j =0 ; j <6 ; i++, j++) - ctlr->mac_addr[j] = report->reply.data[i]; + ctlr->mac_addr[j] = report->subcmd_reply.data[i]; ctlr->mac_addr_str = devm_kasprintf(&ctlr->hdev->dev, GFP_KERNEL, "%02X:%02X:%02X:%02X:%02X:%02X",
@@ -1343,7 +1577,7 @@ static int joycon_ctlr_handle_event(struct joycon_ctlr *ctlr, u8 *data, data[0] != JC_INPUT_SUBCMD_REPLY)break ; report = (struct joycon_input_report *)data; -if (report->reply.id == ctlr->subcmd_ack_match) +if (report->subcmd_reply.id == ctlr->subcmd_ack_match) match = true;break ;default :
@@ -1469,6 +1703,16 @@ static int nintendo_hid_probe(struct hid_device *hdev,hid_warn (hdev,"Analog stick positions may be inaccurate\n" ); } +/* get IMU calibration data, and parse it */ + ret =joycon_request_imu_calibration (ctlr); +if (ret) { +/* +* We can function with default calibration, but it may be +* inaccurate. Provide a warning, and continue on. +*/ +hid_warn (hdev,"Unable to read IMU calibration data\n" ); + } +/* Set the reporting mode to 0x30, which is the full report mode */ ret =joycon_set_report_mode (ctlr);if (ret) {
@@ -1483,6 +1727,13 @@ static int nintendo_hid_probe(struct hid_device *hdev, goto err_mutex; } +/* Enable the IMU */ + ret =joycon_enable_imu (ctlr, true); +if (ret) { +hid_err (hdev,"Failed to enable the IMU; ret=%d\n" , ret); + goto err_mutex; + } + ret =joycon_read_mac (ctlr);if (ret) { hid_err(hdev, "Failed to retrieve controller MAC; ret=%d\n",
Signature · 1 line
2.24.1
Re: [PATCH v10 11/12] HID: nintendo: add IMU support
Hi Daniel,
I had some time to review the motion sensors patch. I have added some feedback inline with your patch below.
Aside from standard feedback on the code, I wanted to have a close look at the accelerometer / gyro data. My team has been doing a lot of work on this (and still is) and I want to make sure any work we do on "user space" software (e.g. Android) automatically works for Joycon as well. The accuracy of the data is very important else applications will make bad decisions. Userspace applications often combine the data of both sensors to calculate a position for position tracking. Inaccurate axes ranges or wrong precision can cause major issues. I may be a bit strict below, but it will just be to prevent headaches for others later on. I use the DS4 as a reference point as it was the first device (aside from Wii early on) where we properly report acceleration and gyro axes with INPUT_PROP_ACCELEROMETER and we should be consistent with it here else applications could be confused, so we should use similar orientation of axes and resolutions.
Thanks,
Roderick
Quoted 25 lines · Daniel J. Ogorchock
This patch adds support for the controller's IMU. The accelerometer and gyro data are both provided to userspace using a second input device. The devices can be associated using their uniq value (set to the controller's MAC address).
The majority of this patch's functinoality was provided by Carl Mueller.
Signed-off-by Daniel J. Ogorchock <[email protected]>--- drivers/hid/hid-nintendo.c | 267 +++++++++++++++++++++++++++++++++++-- 1 file changed, 259 insertions(+), 8 deletions(-)drivers/hid/hid-nintendo.c +4
-0@@ -101,12 +101,29 @@ static const u16 JC_CAL_DATA_START = 0x603d;static const u16 JC_CAL_DATA_END =0x604e ;#define JC_CAL_DATA_SIZE (JC_CAL_DATA_END - JC_CAL_DATA_START + 1) +/* SPI storage addresses of IMU factory calibration data */ +static const u16 JC_IMU_CAL_DATA_START =0x6020 ; +static const u16 JC_IMU_CAL_DATA_END =0x6037 ; +#define JC_IMU_CAL_DATA_SIZE \
drivers/hid/hid-nintendo.c static const u16 JC_CAL_DATA_START = 0x603d; · line 117
Quoted 10 lines
drivers/hid/hid-nintendo.c +4
-0+ (JC_IMU_CAL_DATA_END -JC_IMU_CAL_DATA_START +1 ) /* The raw analog joystick values will be mapped in terms of this magnitude */static const u16 JC_MAX_STICK_MAG =32767 ;static const u16 JC_STICK_FUZZ =250 ;static const u16 JC_STICK_FLAT =500 ; +/* The accel axes will be mapped in terms of this magnitude */ +static const u16 JC_MAX_ACCEL_MAG =32767 ; +static const u16 JC_ACCEL_RES =4096 ;
What does the acceleration resolution equate to? For DS4 we use multiples of 'g'. (We don't know the position on earth, so can't report in m/s^2).
drivers/hid/hid-nintendo.c static const u16 JC_CAL_DATA_START = 0x603d; · line 123
Quoted 6 lines
drivers/hid/hid-nintendo.c +6
-0+static const u16 JC_ACCEL_FUZZ =10 ; +static const u16 JC_ACCEL_FLAT /*= 0*/ ; + +/* The gyro axes will be mapped in terms of this magnitude */ +static const u16 JC_MAX_GYRO_MAG =32767 ; +static const u16 JC_GYRO_RES =13371 /936 ;/* 14 (14.285) */
What does the gyro resolution equate to? For DS4 we report "degrees per second". We should do the same for the joycons, but I don't know how you guys figured out the exact resolution. As I mentioned if it is not accurate, applications will make wrong calculations.
Quoted 24 lines
drivers/hid/hid-nintendo.c +10
-0+static const u16 JC_GYRO_FUZZ =10 ; +static const u16 JC_GYRO_FLAT /*= 0*/ ; +/* frequency/amplitude tables for rumble */ struct joycon_rumble_freq_data {u16 high;@@ -234,6 +251,11 @@ struct joycon_stick_cal {s32 center; }; +struct joycon_imu_cal { +s16 offset[3 ]; +s16 scale[3 ]; +}; +/* * All the controller's button values are stored in a u32. * They can be accessed with bitwise ANDs. @@ -281,6 +303,19 @@ struct joycon_subcmd_reply {u8 data[0 ];/* will be at most 35 bytes */ } __packed; +struct joycon_imu_data { +s16 accel_x;
drivers/hid/hid-nintendo.c struct joycon_subcmd_reply { · line 317
Quoted 10 lines
drivers/hid/hid-nintendo.c +10
-0+s16 accel_y; +s16 accel_z; +s16 gyro_x; +s16 gyro_y; +s16 gyro_z; +} __packed; + +struct joycon_imu_report { +struct joycon_imu_data data[3 ]; +} __packed;
See comments later on about imu_data, but you can't directly cast your data buffer to this data type due to endian issues. It may not really make sense to keep the data structure as you need to do manual data fetching.
Quoted 6 lines
drivers/hid/hid-nintendo.c +1
-0+struct joycon_input_report {u8 id;u8 timer;@@ -290,11 +325,10 @@ struct joycon_input_report {u8 right_stick[3 ];
drivers/hid/hid-nintendo.c struct joycon_input_report { · line 330
Quoted 10 lines
drivers/hid/hid-nintendo.c +3
-5u8 vibrator_report; -/* -* If support for firmware updates, gyroscope data, and/or NFC/IR -* are added in the future, this can be swapped for a union. -*/ -struct joycon_subcmd_reply reply; +union { +struct joycon_subcmd_reply subcmd_reply; +struct joycon_imu_report imu_report;
maybe add a raw byte array to this union. Could help for parsing the imu data.
Quoted 50 lines
drivers/hid/hid-nintendo.c +21
-1+ }; } __packed;#define JC_MAX_RESP_SIZE (sizeof(struct joycon_input_report) + 35)@@ -334,6 +368,9 @@ struct joycon_ctlr {struct joycon_stick_cal right_stick_cal_x;struct joycon_stick_cal right_stick_cal_y; +struct joycon_imu_cal accel_cal; +struct joycon_imu_cal gyro_cal; +/* power supply data */ struct power_supply *battery;struct power_supply_desc battery_desc;@@ -352,6 +389,10 @@ struct joycon_ctlr {u16 rumble_lh_freq;u16 rumble_rl_freq;u16 rumble_rh_freq; + +/* imu */ +struct input_dev *imu_input; +int timestamp; };static int __joycon_hid_send (struct hid_device *hdev,u8 *data,size_t len)@@ -547,7 +588,7 @@ static int joycon_request_calibration(struct joycon_ctlr *ctlr) } report = (struct joycon_input_report *)ctlr->input_buf; - raw_cal = &report->reply.data[5 ]; + raw_cal = &report->subcmd_reply.data[5 ];/* left stick calibration parsing */ cal_x = &ctlr->left_stick_cal_x;@@ -601,6 +642,85 @@ static int joycon_request_calibration(struct joycon_ctlr *ctlr)return 0 ; } +static const s16 DFLT_ACCEL_OFFSET /*= 0*/ ; +static const s16 DFLT_ACCEL_SCALE =16384 ; +static const s16 DFLT_GYRO_OFFSET /*= 0*/ ; +static const s16 DFLT_GYRO_SCALE =13371 ; +static int joycon_request_imu_calibration (struct joycon_ctlr *ctlr) +{ +struct joycon_subcmd_request *req; +u8 buffer[sizeof (*req) +5 ] = {0 }; +struct joycon_input_report *report; +u8 *data; +u8 *raw_cal; +int ret;
drivers/hid/hid-nintendo.c static int joycon_request_calibration(struct joycon_ctlr *ctlr) · line 666
Quoted 10 lines
drivers/hid/hid-nintendo.c +10
-0+int i; + +/* request IMU calibration data */ + req = (struct joycon_subcmd_request *)buffer; + req->subcmd_id =JC_SUBCMD_SPI_FLASH_READ ; + data = req->data; + data[0 ] =0xFF &JC_IMU_CAL_DATA_START ; + data[1 ] =0xFF & (JC_IMU_CAL_DATA_START >>8 ); + data[2 ] =0xFF & (JC_IMU_CAL_DATA_START >>16 ); + data[3 ] =0xFF & (JC_IMU_CAL_DATA_START >>24 );
Maybe use put_unaligned_le32? I think it allows you to avoid doing all these calculations yourself:put_unaligned_le32(JC_IMU_CAL_DATA_START, data);
Quoted 24 lines
drivers/hid/hid-nintendo.c +24
-0+ data[4 ] =JC_IMU_CAL_DATA_SIZE ; + +hid_dbg (ctlr->hdev,"requesting IMU cal data\n" ); + ret =joycon_send_subcmd (ctlr, req,5 ,HZ ); + +if (ret) { +hid_warn (ctlr->hdev, +"Failed to read IMU cal, using defaults; ret=%d\n" , + ret); + +for (i =0 ; i <3 ; i++) { + ctlr->accel_cal.offset[i] =DFLT_ACCEL_OFFSET ; + ctlr->accel_cal.scale[i] =DFLT_ACCEL_SCALE ; + ctlr->gyro_cal.offset[i] =DFLT_GYRO_OFFSET ; + ctlr->gyro_cal.scale[i] =DFLT_GYRO_SCALE ; + } +return ret; + } + + report = (struct joycon_input_report *)ctlr->input_buf; + raw_cal = &report->subcmd_reply.data[5 ]; + +/* IMU calibration parsing */ +for (i =0 ; i <3 ; i++) {
drivers/hid/hid-nintendo.c static int joycon_request_calibration(struct joycon_ctlr *ctlr) · line 700
Quoted 10 lines
drivers/hid/hid-nintendo.c +10
-0+int j = i *2 ; + + ctlr->accel_cal.offset[i] = raw_cal[j +0 ] | + ((s16 )raw_cal[j +1 ] <<8 ); + ctlr->accel_cal.scale[i] = raw_cal[j +6 ] | + ((s16 )raw_cal[j +7 ] <<8 ); + ctlr->gyro_cal.offset[i] = raw_cal[j +12 ] | + ((s16 )raw_cal[j +13 ] <<8 ); + ctlr->gyro_cal.scale[i] = raw_cal[j +18 ] | + ((s16 )raw_cal[j +19 ] <<8 );
get_unaligned_le16 instead of doing your own bitshifts?
Quoted 47 lines
drivers/hid/hid-nintendo.c +36
-0+ } + +hid_dbg (ctlr->hdev,"IMU calibration:\n" +"a_o[0]=%d a_o[1]=%d a_o[2]=%d\n" +"a_s[0]=%d a_s[1]=%d a_s[2]=%d\n" +"g_o[0]=%d g_o[1]=%d g_o[2]=%d\n" +"g_s[0]=%d g_s[1]=%d g_s[2]=%d\n" , + ctlr->accel_cal.offset[0 ], + ctlr->accel_cal.offset[1 ], + ctlr->accel_cal.offset[2 ], + ctlr->accel_cal.scale[0 ], + ctlr->accel_cal.scale[1 ], + ctlr->accel_cal.scale[2 ], + ctlr->gyro_cal.offset[0 ], + ctlr->gyro_cal.offset[1 ], + ctlr->gyro_cal.offset[2 ], + ctlr->gyro_cal.scale[0 ], + ctlr->gyro_cal.scale[1 ], + ctlr->gyro_cal.scale[2 ]); + +return 0 ; +} +static int joycon_set_report_mode (struct joycon_ctlr *ctlr) {struct joycon_subcmd_request *req;@@ -627,6 +747,19 @@ static int joycon_enable_rumble(struct joycon_ctlr *ctlr, bool enable)return joycon_send_subcmd (ctlr, req,1 ,HZ /4 ); } +static int joycon_enable_imu (struct joycon_ctlr *ctlr,bool enable) +{ +struct joycon_subcmd_request *req; +u8 buffer[sizeof (*req) +1 ] = {0 }; + + req = (struct joycon_subcmd_request *)buffer; + req->subcmd_id =JC_SUBCMD_ENABLE_IMU ; + req->data[0 ] = enable ?0x01 :0x00 ; + +hid_dbg (ctlr->hdev,"%s IMU\n" , enable ?"enabling" :"disabling" ); +return joycon_send_subcmd (ctlr, req,1 ,HZ ); +} +static s32 joycon_map_stick_val (struct joycon_stick_cal *cal,s32 val) {s32 center = cal->center;
drivers/hid/hid-nintendo.c static void joycon_parse_report(struct joycon_ctlr *ctlr, · line 910
Quoted 7 lines
drivers/hid/hid-nintendo.c +4
-0@@ -771,6 +904,54 @@ static void joycon_parse_report(struct joycon_ctlr *ctlr,spin_unlock_irqrestore (&ctlr->lock, flags);wake_up (&ctlr->wait); } + +/* parse IMU data if present */ +if (rep->id ==JC_INPUT_IMU_DATA ) { +struct joycon_imu_data *imu_data = rep->imu_report.data;
I don't think you can directly covert your input report data to imu_data. Until now you have been lucky enough (based on a quick glance of the the other patches) that your data are single bytes. However, this data seems to be a bunch of s16's. In other words you have to deal with endianess issues. You need to use get_unaligned_le16 to retrieve data from your raw byte buffer. See other HID drivers for reference.
Since you will have to use get_unaligned_le16, it probably won't make much sense to really have a joycon_imu_data structure. Maybe extend your input_buffer union with raw bytes and in this case just use raw bytes. Each of your loop iterations below just grab the values from the buffer and store them in a variable.
drivers/hid/hid-nintendo.c static void joycon_parse_report(struct joycon_ctlr *ctlr, · line 918
Quoted 8 lines
drivers/hid/hid-nintendo.c +8
-0+struct input_dev *idev = ctlr->imu_input; +int i; +int value[6 ]; + +for (i =0 ; i <3 ; i++) {/* there are 3 reports */ + ctlr->timestamp +=5000 ;/* each is 5 ms apart */ +input_event (idev,EV_MSC ,MSC_TIMESTAMP , + ctlr->timestamp);
How sure are you that this is always 5ms? Is there a hardware timestamp somewhere? If I look at our DS4 the timing isn't guaranteed (Bluetooth is lossy) and not all packets even make it.
Quoted 16 lines
drivers/hid/hid-nintendo.c +16
-0+ +/* +* Rather than convert to floats, we adjust by +* calibration factors and then multiply by the default +* scaling values. This way, the consumer only needs to +* divide by the default scale values. +*/ + value[0 ] = (imu_data[i].gyro_x - + ctlr->gyro_cal.offset[0 ]) * +DFLT_GYRO_SCALE / ctlr->gyro_cal.scale[0 ]; + value[1 ] = (imu_data[i].gyro_y - + ctlr->gyro_cal.offset[1 ]) * +DFLT_GYRO_SCALE / ctlr->gyro_cal.scale[1 ]; + value[2 ] = (imu_data[i].gyro_z - + ctlr->gyro_cal.offset[2 ]) * +DFLT_GYRO_SCALE / ctlr->gyro_cal.scale[2 ];
drivers/hid/hid-nintendo.c static void joycon_parse_report(struct joycon_ctlr *ctlr, · line 944
Quoted 10 lines
drivers/hid/hid-nintendo.c +10
-0+ + value[3 ] = (imu_data[i].accel_x - + ctlr->accel_cal.offset[0 ]) * +DFLT_ACCEL_SCALE / ctlr->accel_cal.scale[0 ]; + value[4 ] = (imu_data[i].accel_y - + ctlr->accel_cal.offset[1 ]) * +DFLT_ACCEL_SCALE / ctlr->accel_cal.scale[1 ]; + value[5 ] = (imu_data[i].accel_z - + ctlr->accel_cal.offset[2 ]) * +DFLT_ACCEL_SCALE / ctlr->accel_cal.scale[2 ];
Just in case I would double check the precision of your calculations. For DS4 we had similar calculations, but we had a small loss of precision, which ultimately caused major calculation errors. (Applications often use accelerometer + gyro data to calculate an absolute position. This involves integration.. and small errors become big). We had to use the "mult_frac" macro to restore some of the precision during the calculations. It might be something to double check.
In addition what orientation are you using for the axes? I need to double check the DS4 datasheets, but I think we were using a "right handed" coordinate system. (So if you make a fist of your right hand with thumb up, the gyro curls around it counter clockwise along the direction of your fingers).
Quoted 145 lines
drivers/hid/hid-nintendo.c +82
-3+ +input_report_abs (idev,ABS_RX , value[0 ]); +input_report_abs (idev,ABS_RY , value[1 ]); +input_report_abs (idev,ABS_RZ , value[2 ]); +input_report_abs (idev,ABS_X , value[3 ]); +input_report_abs (idev,ABS_Y , value[4 ]); +input_report_abs (idev,ABS_Z , value[5 ]); +input_sync (idev); + } + } }static void joycon_rumble_worker (struct work_struct *work)@@ -950,6 +1131,7 @@ static int joycon_input_create(struct joycon_ctlr *ctlr) {struct hid_device *hdev;const char *name; +const char *imu_name;int ret;int i;@@ -958,12 +1140,15 @@ static int joycon_input_create(struct joycon_ctlr *ctlr)switch (hdev->product) {case USB_DEVICE_ID_NINTENDO_PROCON : name ="Nintendo Switch Pro Controller" ; + imu_name ="Nintendo Switch Pro Controller IMU" ;break ;case USB_DEVICE_ID_NINTENDO_JOYCONL : name ="Nintendo Switch Left Joy-Con" ; + imu_name ="Nintendo Switch Left Joy-Con IMU" ;break ;case USB_DEVICE_ID_NINTENDO_JOYCONR : name ="Nintendo Switch Right Joy-Con" ; + imu_name ="Nintendo Switch Right Joy-Con IMU" ;break ;default :/* Should be impossible */ hid_err (hdev,"Invalid hid product\n" );@@ -1029,6 +1214,55 @@ static int joycon_input_create(struct joycon_ctlr *ctlr)if (ret)return ret; +/* configure the imu input device */ + ctlr->imu_input =devm_input_allocate_device (&hdev->dev); +if (!ctlr->imu_input) +return -ENOMEM ; + + ctlr->imu_input->id.bustype = hdev->bus; + ctlr->imu_input->id.vendor = hdev->vendor; + ctlr->imu_input->id.product = hdev->product; + ctlr->imu_input->id.version = hdev->version; + ctlr->imu_input->uniq = ctlr->mac_addr_str; + ctlr->imu_input->name = imu_name; +input_set_drvdata (ctlr->imu_input, ctlr); + +/* configure imu axes */ +input_set_abs_params (ctlr->imu_input,ABS_X , + -JC_MAX_ACCEL_MAG ,JC_MAX_ACCEL_MAG , +JC_ACCEL_FUZZ ,JC_ACCEL_FLAT ); +input_set_abs_params (ctlr->imu_input,ABS_Y , + -JC_MAX_ACCEL_MAG ,JC_MAX_ACCEL_MAG , +JC_ACCEL_FUZZ ,JC_ACCEL_FLAT ); +input_set_abs_params (ctlr->imu_input,ABS_Z , + -JC_MAX_ACCEL_MAG ,JC_MAX_ACCEL_MAG , +JC_ACCEL_FUZZ ,JC_ACCEL_FLAT ); +input_abs_set_res (ctlr->imu_input,ABS_X ,JC_ACCEL_RES ); +input_abs_set_res (ctlr->imu_input,ABS_Y ,JC_ACCEL_RES ); +input_abs_set_res (ctlr->imu_input,ABS_Z ,JC_ACCEL_RES ); + +input_set_abs_params (ctlr->imu_input,ABS_RX , + -JC_MAX_GYRO_MAG ,JC_MAX_GYRO_MAG , +JC_GYRO_FUZZ ,JC_GYRO_FLAT ); +input_set_abs_params (ctlr->imu_input,ABS_RY , + -JC_MAX_GYRO_MAG ,JC_MAX_GYRO_MAG , +JC_GYRO_FUZZ ,JC_GYRO_FLAT ); +input_set_abs_params (ctlr->imu_input,ABS_RZ , + -JC_MAX_GYRO_MAG ,JC_MAX_GYRO_MAG , +JC_GYRO_FUZZ ,JC_GYRO_FLAT ); + +input_abs_set_res (ctlr->imu_input,ABS_RX ,JC_GYRO_RES ); +input_abs_set_res (ctlr->imu_input,ABS_RY ,JC_GYRO_RES ); +input_abs_set_res (ctlr->imu_input,ABS_RZ ,JC_GYRO_RES ); + +__set_bit (EV_MSC , ctlr->imu_input->evbit); +__set_bit (MSC_TIMESTAMP , ctlr->imu_input->mscbit); +__set_bit (INPUT_PROP_ACCELEROMETER , ctlr->imu_input->propbit); + + ret =input_register_device (ctlr->imu_input); +if (ret) +return ret; +return 0 ; }@@ -1288,7 +1522,7 @@ static int joycon_read_mac(struct joycon_ctlr *ctlr) report = (struct joycon_input_report *)ctlr->input_buf;for (i =4 , j =0 ; j <6 ; i++, j++) - ctlr->mac_addr[j] = report->reply.data[i]; + ctlr->mac_addr[j] = report->subcmd_reply.data[i]; ctlr->mac_addr_str = devm_kasprintf(&ctlr->hdev->dev, GFP_KERNEL, "%02X:%02X:%02X:%02X:%02X:%02X",@@ -1343,7 +1577,7 @@ static int joycon_ctlr_handle_event(struct joycon_ctlr *ctlr, u8 *data, data[0] != JC_INPUT_SUBCMD_REPLY)break ; report = (struct joycon_input_report *)data; -if (report->reply.id == ctlr->subcmd_ack_match) +if (report->subcmd_reply.id == ctlr->subcmd_ack_match) match = true;break ;default :@@ -1469,6 +1703,16 @@ static int nintendo_hid_probe(struct hid_device *hdev,hid_warn (hdev,"Analog stick positions may be inaccurate\n" ); } +/* get IMU calibration data, and parse it */ + ret =joycon_request_imu_calibration (ctlr); +if (ret) { +/* +* We can function with default calibration, but it may be +* inaccurate. Provide a warning, and continue on. +*/ +hid_warn (hdev,"Unable to read IMU calibration data\n" ); + } +/* Set the reporting mode to 0x30, which is the full report mode */ ret =joycon_set_report_mode (ctlr);if (ret) {@@ -1483,6 +1727,13 @@ static int nintendo_hid_probe(struct hid_device *hdev, goto err_mutex; } +/* Enable the IMU */ + ret =joycon_enable_imu (ctlr, true); +if (ret) { +hid_err (hdev,"Failed to enable the IMU; ret=%d\n" , ret); + goto err_mutex; + } + ret =joycon_read_mac (ctlr);if (ret) { hid_err(hdev, "Failed to retrieve controller MAC; ret=%d\n", --2.24.1
Daniel Ogorchock
maintainerRe: [PATCH v10 11/12] HID: nintendo: add IMU support
Hi Roderick,
Thanks for the feedback. In addition to the inline comments below, do you have any recommendations for test programs that you know work well with hid-sony's gyro implementation? Up to this point I've just been using evtest, which obviously isn't too useful for testing actual functionality of the gyro in an intuitive way.
Quoted 292 lines · Roderick Colenbrander
Hi Daniel,
I had some time to review the motion sensors patch. I have added some feedback inline with your patch below.
Aside from standard feedback on the code, I wanted to have a close look at the accelerometer / gyro data. My team has been doing a lot of work on this (and still is) and I want to make sure any work we do on "user space" software (e.g. Android) automatically works for Joycon as well. The accuracy of the data is very important else applications will make bad decisions. Userspace applications often combine the data of both sensors to calculate a position for position tracking. Inaccurate axes ranges or wrong precision can cause major issues. I may be a bit strict below, but it will just be to prevent headaches for others later on. I use the DS4 as a reference point as it was the first device (aside from Wii early on) where we properly report acceleration and gyro axes with
INPUT_PROP_ACCELEROMETERand we should be consistent with it here else applications could be confused, so we should use similar orientation of axes and resolutions.Thanks,
RoderickQuoted 25 lines · Daniel J. Ogorchock
This patch adds support for the controller's IMU. The accelerometer and gyro data are both provided to userspace using a second input device. The devices can be associated using their uniq value (set to the controller's MAC address).
The majority of this patch's functinoality was provided by Carl Mueller.
Signed-off-by Daniel J. Ogorchock <[email protected]>--- drivers/hid/hid-nintendo.c | 267 +++++++++++++++++++++++++++++++++++-- 1 file changed, 259 insertions(+), 8 deletions(-)drivers/hid/hid-nintendo.c +4
-0@@ -101,12 +101,29 @@ static const u16 JC_CAL_DATA_START = 0x603d;static const u16 JC_CAL_DATA_END =0x604e ;#define JC_CAL_DATA_SIZE (JC_CAL_DATA_END - JC_CAL_DATA_START + 1) +/* SPI storage addresses of IMU factory calibration data */ +static const u16 JC_IMU_CAL_DATA_START =0x6020 ; +static const u16 JC_IMU_CAL_DATA_END =0x6037 ; +#define JC_IMU_CAL_DATA_SIZE \drivers/hid/hid-nintendo.c static const u16 JC_CAL_DATA_START = 0x603d; · line 117
Quoted 10 lines
drivers/hid/hid-nintendo.c +4
-0+ (JC_IMU_CAL_DATA_END -JC_IMU_CAL_DATA_START +1 ) /* The raw analog joystick values will be mapped in terms of this magnitude */static const u16 JC_MAX_STICK_MAG =32767 ;static const u16 JC_STICK_FUZZ =250 ;static const u16 JC_STICK_FLAT =500 ; +/* The accel axes will be mapped in terms of this magnitude */ +static const u16 JC_MAX_ACCEL_MAG =32767 ; +static const u16 JC_ACCEL_RES =4096 ;What does the acceleration resolution equate to? For DS4 we use multiples of 'g'. (We don't know the position on earth, so can't report in m/s^2).
drivers/hid/hid-nintendo.c static const u16 JC_CAL_DATA_START = 0x603d; · line 123
Quoted 6 lines
drivers/hid/hid-nintendo.c +6
-0+static const u16 JC_ACCEL_FUZZ =10 ; +static const u16 JC_ACCEL_FLAT /*= 0*/ ; + +/* The gyro axes will be mapped in terms of this magnitude */ +static const u16 JC_MAX_GYRO_MAG =32767 ; +static const u16 JC_GYRO_RES =13371 /936 ;/* 14 (14.285) */ What does the gyro resolution equate to? For DS4 we report "degrees per second". We should do the same for the joycons, but I don't know how you guys figured out the exact resolution. As I mentioned if it is not accurate, applications will make wrong calculations.
Quoted 24 lines
drivers/hid/hid-nintendo.c +10
-0+static const u16 JC_GYRO_FUZZ =10 ; +static const u16 JC_GYRO_FLAT /*= 0*/ ; +/* frequency/amplitude tables for rumble */ struct joycon_rumble_freq_data {u16 high;@@ -234,6 +251,11 @@ struct joycon_stick_cal {s32 center; }; +struct joycon_imu_cal { +s16 offset[3 ]; +s16 scale[3 ]; +}; +/* * All the controller's button values are stored in a u32. * They can be accessed with bitwise ANDs. @@ -281,6 +303,19 @@ struct joycon_subcmd_reply {u8 data[0 ];/* will be at most 35 bytes */ } __packed; +struct joycon_imu_data { +s16 accel_x;drivers/hid/hid-nintendo.c struct joycon_subcmd_reply { · line 317
Quoted 10 lines
drivers/hid/hid-nintendo.c +10
-0+s16 accel_y; +s16 accel_z; +s16 gyro_x; +s16 gyro_y; +s16 gyro_z; +} __packed; + +struct joycon_imu_report { +struct joycon_imu_data data[3 ]; +} __packed;See comments later on about
imu_data, but you can't directly cast your data buffer to this data type due to endian issues. It may not really make sense to keep the data structure as you need to do manual data fetching.Quoted 6 lines
drivers/hid/hid-nintendo.c +1
-0+struct joycon_input_report {u8 id;u8 timer;@@ -290,11 +325,10 @@ struct joycon_input_report {u8 right_stick[3 ];drivers/hid/hid-nintendo.c struct joycon_input_report { · line 330
Quoted 10 lines
drivers/hid/hid-nintendo.c +3
-5u8 vibrator_report; -/* -* If support for firmware updates, gyroscope data, and/or NFC/IR -* are added in the future, this can be swapped for a union. -*/ -struct joycon_subcmd_reply reply; +union { +struct joycon_subcmd_reply subcmd_reply; +struct joycon_imu_report imu_report;maybe add a raw byte array to this union. Could help for parsing the imu data.
Quoted 50 lines
drivers/hid/hid-nintendo.c +21
-1+ }; } __packed;#define JC_MAX_RESP_SIZE (sizeof(struct joycon_input_report) + 35)@@ -334,6 +368,9 @@ struct joycon_ctlr {struct joycon_stick_cal right_stick_cal_x;struct joycon_stick_cal right_stick_cal_y; +struct joycon_imu_cal accel_cal; +struct joycon_imu_cal gyro_cal; +/* power supply data */ struct power_supply *battery;struct power_supply_desc battery_desc;@@ -352,6 +389,10 @@ struct joycon_ctlr {u16 rumble_lh_freq;u16 rumble_rl_freq;u16 rumble_rh_freq; + +/* imu */ +struct input_dev *imu_input; +int timestamp; };static int __joycon_hid_send (struct hid_device *hdev,u8 *data,size_t len)@@ -547,7 +588,7 @@ static int joycon_request_calibration(struct joycon_ctlr *ctlr) } report = (struct joycon_input_report *)ctlr->input_buf; - raw_cal = &report->reply.data[5 ]; + raw_cal = &report->subcmd_reply.data[5 ];/* left stick calibration parsing */ cal_x = &ctlr->left_stick_cal_x;@@ -601,6 +642,85 @@ static int joycon_request_calibration(struct joycon_ctlr *ctlr)return 0 ; } +static const s16 DFLT_ACCEL_OFFSET /*= 0*/ ; +static const s16 DFLT_ACCEL_SCALE =16384 ; +static const s16 DFLT_GYRO_OFFSET /*= 0*/ ; +static const s16 DFLT_GYRO_SCALE =13371 ; +static int joycon_request_imu_calibration (struct joycon_ctlr *ctlr) +{ +struct joycon_subcmd_request *req; +u8 buffer[sizeof (*req) +5 ] = {0 }; +struct joycon_input_report *report; +u8 *data; +u8 *raw_cal; +int ret;drivers/hid/hid-nintendo.c static int joycon_request_calibration(struct joycon_ctlr *ctlr) · line 666
Quoted 10 lines
drivers/hid/hid-nintendo.c +10
-0+int i; + +/* request IMU calibration data */ + req = (struct joycon_subcmd_request *)buffer; + req->subcmd_id =JC_SUBCMD_SPI_FLASH_READ ; + data = req->data; + data[0 ] =0xFF &JC_IMU_CAL_DATA_START ; + data[1 ] =0xFF & (JC_IMU_CAL_DATA_START >>8 ); + data[2 ] =0xFF & (JC_IMU_CAL_DATA_START >>16 ); + data[3 ] =0xFF & (JC_IMU_CAL_DATA_START >>24 );Maybe use
put_unaligned_le32? I think it allows you to avoid doing all these calculations yourself:put_unaligned_le32(JC_IMU_CAL_DATA_START, data);Quoted 24 lines
drivers/hid/hid-nintendo.c +24
-0+ data[4 ] =JC_IMU_CAL_DATA_SIZE ; + +hid_dbg (ctlr->hdev,"requesting IMU cal data\n" ); + ret =joycon_send_subcmd (ctlr, req,5 ,HZ ); + +if (ret) { +hid_warn (ctlr->hdev, +"Failed to read IMU cal, using defaults; ret=%d\n" , + ret); + +for (i =0 ; i <3 ; i++) { + ctlr->accel_cal.offset[i] =DFLT_ACCEL_OFFSET ; + ctlr->accel_cal.scale[i] =DFLT_ACCEL_SCALE ; + ctlr->gyro_cal.offset[i] =DFLT_GYRO_OFFSET ; + ctlr->gyro_cal.scale[i] =DFLT_GYRO_SCALE ; + } +return ret; + } + + report = (struct joycon_input_report *)ctlr->input_buf; + raw_cal = &report->subcmd_reply.data[5 ]; + +/* IMU calibration parsing */ +for (i =0 ; i <3 ; i++) {drivers/hid/hid-nintendo.c static int joycon_request_calibration(struct joycon_ctlr *ctlr) · line 700
Quoted 10 lines
drivers/hid/hid-nintendo.c +10
-0+int j = i *2 ; + + ctlr->accel_cal.offset[i] = raw_cal[j +0 ] | + ((s16 )raw_cal[j +1 ] <<8 ); + ctlr->accel_cal.scale[i] = raw_cal[j +6 ] | + ((s16 )raw_cal[j +7 ] <<8 ); + ctlr->gyro_cal.offset[i] = raw_cal[j +12 ] | + ((s16 )raw_cal[j +13 ] <<8 ); + ctlr->gyro_cal.scale[i] = raw_cal[j +18 ] | + ((s16 )raw_cal[j +19 ] <<8 );
get_unaligned_le16instead of doing your own bitshifts?Quoted 47 lines
drivers/hid/hid-nintendo.c +36
-0+ } + +hid_dbg (ctlr->hdev,"IMU calibration:\n" +"a_o[0]=%d a_o[1]=%d a_o[2]=%d\n" +"a_s[0]=%d a_s[1]=%d a_s[2]=%d\n" +"g_o[0]=%d g_o[1]=%d g_o[2]=%d\n" +"g_s[0]=%d g_s[1]=%d g_s[2]=%d\n" , + ctlr->accel_cal.offset[0 ], + ctlr->accel_cal.offset[1 ], + ctlr->accel_cal.offset[2 ], + ctlr->accel_cal.scale[0 ], + ctlr->accel_cal.scale[1 ], + ctlr->accel_cal.scale[2 ], + ctlr->gyro_cal.offset[0 ], + ctlr->gyro_cal.offset[1 ], + ctlr->gyro_cal.offset[2 ], + ctlr->gyro_cal.scale[0 ], + ctlr->gyro_cal.scale[1 ], + ctlr->gyro_cal.scale[2 ]); + +return 0 ; +} +static int joycon_set_report_mode (struct joycon_ctlr *ctlr) {struct joycon_subcmd_request *req;@@ -627,6 +747,19 @@ static int joycon_enable_rumble(struct joycon_ctlr *ctlr, bool enable)return joycon_send_subcmd (ctlr, req,1 ,HZ /4 ); } +static int joycon_enable_imu (struct joycon_ctlr *ctlr,bool enable) +{ +struct joycon_subcmd_request *req; +u8 buffer[sizeof (*req) +1 ] = {0 }; + + req = (struct joycon_subcmd_request *)buffer; + req->subcmd_id =JC_SUBCMD_ENABLE_IMU ; + req->data[0 ] = enable ?0x01 :0x00 ; + +hid_dbg (ctlr->hdev,"%s IMU\n" , enable ?"enabling" :"disabling" ); +return joycon_send_subcmd (ctlr, req,1 ,HZ ); +} +static s32 joycon_map_stick_val (struct joycon_stick_cal *cal,s32 val) {s32 center = cal->center;drivers/hid/hid-nintendo.c static void joycon_parse_report(struct joycon_ctlr *ctlr, · line 910
Quoted 7 lines
drivers/hid/hid-nintendo.c +4
-0@@ -771,6 +904,54 @@ static void joycon_parse_report(struct joycon_ctlr *ctlr,spin_unlock_irqrestore (&ctlr->lock, flags);wake_up (&ctlr->wait); } + +/* parse IMU data if present */ +if (rep->id ==JC_INPUT_IMU_DATA ) { +struct joycon_imu_data *imu_data = rep->imu_report.data;
Quoted 7 lines
I don't think you can directly covert your input report data to
imu_data. Until now you have been lucky enough (based on a quick glance of the the other patches) that your data are single bytes. However, this data seems to be a bunch of s16's. In other words you have to deal with endianess issues. You need to useget_unaligned_le16to retrieve data from your raw byte buffer. See other HID drivers for reference.
Ah, good point. I'd overlooked that entirely.
Quoted 16 lines
Since you will have to use
get_unaligned_le16, it probably won't make much sense to really have ajoycon_imu_datastructure. Maybe extend yourinput_bufferunion with raw bytes and in this case just use raw bytes. Each of your loop iterations below just grab the values from the buffer and store them in a variable.drivers/hid/hid-nintendo.c static void joycon_parse_report(struct joycon_ctlr *ctlr, · line 918
Quoted 8 lines
drivers/hid/hid-nintendo.c +8
-0+struct input_dev *idev = ctlr->imu_input; +int i; +int value[6 ]; + +for (i =0 ; i <3 ; i++) {/* there are 3 reports */ + ctlr->timestamp +=5000 ;/* each is 5 ms apart */ +input_event (idev,EV_MSC ,MSC_TIMESTAMP , + ctlr->timestamp);
Quoted 4 lines
How sure are you that this is always 5ms? Is there a hardware timestamp somewhere? If I look at our DS4 the timing isn't guaranteed (Bluetooth is lossy) and not all packets even make it.
It appears that the closest thing to a hardware timestamp available is a quickly incrementing 1-byte timer sent with every report. It's only really useful for latency estimation. Each input report includes 3 IMU samples which are 5ms apart for the joy-cons. It's recently come to my attention that the samples may be spaced differently for the pro controller, so I'm going to need to dive into this more anyway. I'm not sure what a great way would be to handle lost reports.
Quoted 27 lines
Quoted 16 lines
drivers/hid/hid-nintendo.c +16
-0+ +/* +* Rather than convert to floats, we adjust by +* calibration factors and then multiply by the default +* scaling values. This way, the consumer only needs to +* divide by the default scale values. +*/ + value[0 ] = (imu_data[i].gyro_x - + ctlr->gyro_cal.offset[0 ]) * +DFLT_GYRO_SCALE / ctlr->gyro_cal.scale[0 ]; + value[1 ] = (imu_data[i].gyro_y - + ctlr->gyro_cal.offset[1 ]) * +DFLT_GYRO_SCALE / ctlr->gyro_cal.scale[1 ]; + value[2 ] = (imu_data[i].gyro_z - + ctlr->gyro_cal.offset[2 ]) * +DFLT_GYRO_SCALE / ctlr->gyro_cal.scale[2 ];drivers/hid/hid-nintendo.c static void joycon_parse_report(struct joycon_ctlr *ctlr, · line 944
Quoted 10 lines
drivers/hid/hid-nintendo.c +10
-0+ + value[3 ] = (imu_data[i].accel_x - + ctlr->accel_cal.offset[0 ]) * +DFLT_ACCEL_SCALE / ctlr->accel_cal.scale[0 ]; + value[4 ] = (imu_data[i].accel_y - + ctlr->accel_cal.offset[1 ]) * +DFLT_ACCEL_SCALE / ctlr->accel_cal.scale[1 ]; + value[5 ] = (imu_data[i].accel_z - + ctlr->accel_cal.offset[2 ]) * +DFLT_ACCEL_SCALE / ctlr->accel_cal.scale[2 ];
Quoted 9 lines
Just in case I would double check the precision of your calculations. For DS4 we had similar calculations, but we had a small loss of precision, which ultimately caused major calculation errors. (Applications often use accelerometer + gyro data to calculate an absolute position. This involves integration.. and small errors become big). We had to use the "
mult_frac" macro to restore some of the precision during the calculations. It might be something to double check.
That's good to know. I'll look more closely at how it's implemented in hid-sony.
Quoted 6 lines
In addition what orientation are you using for the axes? I need to double check the DS4 datasheets, but I think we were using a "right handed" coordinate system. (So if you make a fist of your right hand with thumb up, the gyro curls around it counter clockwise along the direction of your fingers).
The orientation of the axes (and much more) are documented here:
https://github.com/dekuNukem/Nintendo_Switch_Reverse_Engineering/blob/master/imu_sensor_notes.md
Since the the right vs. left joy-cons have different axes orientations, I assume it's preferable to standardize it all in software to match the orientation of the DS4.
Quoted 146 lines
Quoted 146 lines
drivers/hid/hid-nintendo.c +82
-3+ +input_report_abs (idev,ABS_RX , value[0 ]); +input_report_abs (idev,ABS_RY , value[1 ]); +input_report_abs (idev,ABS_RZ , value[2 ]); +input_report_abs (idev,ABS_X , value[3 ]); +input_report_abs (idev,ABS_Y , value[4 ]); +input_report_abs (idev,ABS_Z , value[5 ]); +input_sync (idev); + } + } }static void joycon_rumble_worker (struct work_struct *work)@@ -950,6 +1131,7 @@ static int joycon_input_create(struct joycon_ctlr *ctlr) {struct hid_device *hdev;const char *name; +const char *imu_name;int ret;int i;@@ -958,12 +1140,15 @@ static int joycon_input_create(struct joycon_ctlr *ctlr)switch (hdev->product) {case USB_DEVICE_ID_NINTENDO_PROCON : name ="Nintendo Switch Pro Controller" ; + imu_name ="Nintendo Switch Pro Controller IMU" ;break ;case USB_DEVICE_ID_NINTENDO_JOYCONL : name ="Nintendo Switch Left Joy-Con" ; + imu_name ="Nintendo Switch Left Joy-Con IMU" ;break ;case USB_DEVICE_ID_NINTENDO_JOYCONR : name ="Nintendo Switch Right Joy-Con" ; + imu_name ="Nintendo Switch Right Joy-Con IMU" ;break ;default :/* Should be impossible */ hid_err (hdev,"Invalid hid product\n" );@@ -1029,6 +1214,55 @@ static int joycon_input_create(struct joycon_ctlr *ctlr)if (ret)return ret; +/* configure the imu input device */ + ctlr->imu_input =devm_input_allocate_device (&hdev->dev); +if (!ctlr->imu_input) +return -ENOMEM ; + + ctlr->imu_input->id.bustype = hdev->bus; + ctlr->imu_input->id.vendor = hdev->vendor; + ctlr->imu_input->id.product = hdev->product; + ctlr->imu_input->id.version = hdev->version; + ctlr->imu_input->uniq = ctlr->mac_addr_str; + ctlr->imu_input->name = imu_name; +input_set_drvdata (ctlr->imu_input, ctlr); + +/* configure imu axes */ +input_set_abs_params (ctlr->imu_input,ABS_X , + -JC_MAX_ACCEL_MAG ,JC_MAX_ACCEL_MAG , +JC_ACCEL_FUZZ ,JC_ACCEL_FLAT ); +input_set_abs_params (ctlr->imu_input,ABS_Y , + -JC_MAX_ACCEL_MAG ,JC_MAX_ACCEL_MAG , +JC_ACCEL_FUZZ ,JC_ACCEL_FLAT ); +input_set_abs_params (ctlr->imu_input,ABS_Z , + -JC_MAX_ACCEL_MAG ,JC_MAX_ACCEL_MAG , +JC_ACCEL_FUZZ ,JC_ACCEL_FLAT ); +input_abs_set_res (ctlr->imu_input,ABS_X ,JC_ACCEL_RES ); +input_abs_set_res (ctlr->imu_input,ABS_Y ,JC_ACCEL_RES ); +input_abs_set_res (ctlr->imu_input,ABS_Z ,JC_ACCEL_RES ); + +input_set_abs_params (ctlr->imu_input,ABS_RX , + -JC_MAX_GYRO_MAG ,JC_MAX_GYRO_MAG , +JC_GYRO_FUZZ ,JC_GYRO_FLAT ); +input_set_abs_params (ctlr->imu_input,ABS_RY , + -JC_MAX_GYRO_MAG ,JC_MAX_GYRO_MAG , +JC_GYRO_FUZZ ,JC_GYRO_FLAT ); +input_set_abs_params (ctlr->imu_input,ABS_RZ , + -JC_MAX_GYRO_MAG ,JC_MAX_GYRO_MAG , +JC_GYRO_FUZZ ,JC_GYRO_FLAT ); + +input_abs_set_res (ctlr->imu_input,ABS_RX ,JC_GYRO_RES ); +input_abs_set_res (ctlr->imu_input,ABS_RY ,JC_GYRO_RES ); +input_abs_set_res (ctlr->imu_input,ABS_RZ ,JC_GYRO_RES ); + +__set_bit (EV_MSC , ctlr->imu_input->evbit); +__set_bit (MSC_TIMESTAMP , ctlr->imu_input->mscbit); +__set_bit (INPUT_PROP_ACCELEROMETER , ctlr->imu_input->propbit); + + ret =input_register_device (ctlr->imu_input); +if (ret) +return ret; +return 0 ; }@@ -1288,7 +1522,7 @@ static int joycon_read_mac(struct joycon_ctlr *ctlr) report = (struct joycon_input_report *)ctlr->input_buf;for (i =4 , j =0 ; j <6 ; i++, j++) - ctlr->mac_addr[j] = report->reply.data[i]; + ctlr->mac_addr[j] = report->subcmd_reply.data[i]; ctlr->mac_addr_str = devm_kasprintf(&ctlr->hdev->dev, GFP_KERNEL, "%02X:%02X:%02X:%02X:%02X:%02X",@@ -1343,7 +1577,7 @@ static int joycon_ctlr_handle_event(struct joycon_ctlr *ctlr, u8 *data, data[0] != JC_INPUT_SUBCMD_REPLY)break ; report = (struct joycon_input_report *)data; -if (report->reply.id == ctlr->subcmd_ack_match) +if (report->subcmd_reply.id == ctlr->subcmd_ack_match) match = true;break ;default :@@ -1469,6 +1703,16 @@ static int nintendo_hid_probe(struct hid_device *hdev,hid_warn (hdev,"Analog stick positions may be inaccurate\n" ); } +/* get IMU calibration data, and parse it */ + ret =joycon_request_imu_calibration (ctlr); +if (ret) { +/* +* We can function with default calibration, but it may be +* inaccurate. Provide a warning, and continue on. +*/ +hid_warn (hdev,"Unable to read IMU calibration data\n" ); + } +/* Set the reporting mode to 0x30, which is the full report mode */ ret =joycon_set_report_mode (ctlr);if (ret) {@@ -1483,6 +1727,13 @@ static int nintendo_hid_probe(struct hid_device *hdev, goto err_mutex; } +/* Enable the IMU */ + ret =joycon_enable_imu (ctlr, true); +if (ret) { +hid_err (hdev,"Failed to enable the IMU; ret=%d\n" , ret); + goto err_mutex; + } + ret =joycon_read_mac (ctlr);if (ret) { hid_err(hdev, "Failed to retrieve controller MAC; ret=%d\n", --2.24.1
Re: [PATCH v10 11/12] HID: nintendo: add IMU support
Hi Daniel,
Unfortunately there is no public test application at the moment to illustrate these features. I agree something is definitely needed.
I need to see if we can come up with something. One of the test apps we have internally is a 3D cube, which is controllable by controller motion. It of course shows the gyro / accelerometer values, but the position of the cube is tied to the calculated orientation. If something is off you will see weird movements in the cube or drift building up over time etcetera.
Though it would take some time to prepare something. The rest of the patch series looked fine I think, so the IMU parts may need to wait for a next kernel cycle, but all the other plumbing could go in (if others are okay of course).
Thanks,
Roderick
Quoted 541 lines · Daniel Ogorchock
Hi Roderick,
Thanks for the feedback. In addition to the inline comments below, do you have any recommendations for test programs that you know work well with hid-sony's gyro implementation? Up to this point I've just been using evtest, which obviously isn't too useful for testing actual functionality of the gyro in an intuitive way.
Quoted 292 lines · Roderick Colenbrander
Hi Daniel,
I had some time to review the motion sensors patch. I have added some feedback inline with your patch below.
Aside from standard feedback on the code, I wanted to have a close look at the accelerometer / gyro data. My team has been doing a lot of work on this (and still is) and I want to make sure any work we do on "user space" software (e.g. Android) automatically works for Joycon as well. The accuracy of the data is very important else applications will make bad decisions. Userspace applications often combine the data of both sensors to calculate a position for position tracking. Inaccurate axes ranges or wrong precision can cause major issues. I may be a bit strict below, but it will just be to prevent headaches for others later on. I use the DS4 as a reference point as it was the first device (aside from Wii early on) where we properly report acceleration and gyro axes with
INPUT_PROP_ACCELEROMETERand we should be consistent with it here else applications could be confused, so we should use similar orientation of axes and resolutions.Thanks,
RoderickQuoted 25 lines · Daniel J. Ogorchock
This patch adds support for the controller's IMU. The accelerometer and gyro data are both provided to userspace using a second input device. The devices can be associated using their uniq value (set to the controller's MAC address).
The majority of this patch's functinoality was provided by Carl Mueller.
Signed-off-by Daniel J. Ogorchock <[email protected]>--- drivers/hid/hid-nintendo.c | 267 +++++++++++++++++++++++++++++++++++-- 1 file changed, 259 insertions(+), 8 deletions(-)drivers/hid/hid-nintendo.c +4
-0@@ -101,12 +101,29 @@ static const u16 JC_CAL_DATA_START = 0x603d;static const u16 JC_CAL_DATA_END =0x604e ;#define JC_CAL_DATA_SIZE (JC_CAL_DATA_END - JC_CAL_DATA_START + 1) +/* SPI storage addresses of IMU factory calibration data */ +static const u16 JC_IMU_CAL_DATA_START =0x6020 ; +static const u16 JC_IMU_CAL_DATA_END =0x6037 ; +#define JC_IMU_CAL_DATA_SIZE \drivers/hid/hid-nintendo.c static const u16 JC_CAL_DATA_START = 0x603d; · line 117
Quoted 10 lines
drivers/hid/hid-nintendo.c +4
-0+ (JC_IMU_CAL_DATA_END -JC_IMU_CAL_DATA_START +1 ) /* The raw analog joystick values will be mapped in terms of this magnitude */static const u16 JC_MAX_STICK_MAG =32767 ;static const u16 JC_STICK_FUZZ =250 ;static const u16 JC_STICK_FLAT =500 ; +/* The accel axes will be mapped in terms of this magnitude */ +static const u16 JC_MAX_ACCEL_MAG =32767 ; +static const u16 JC_ACCEL_RES =4096 ;What does the acceleration resolution equate to? For DS4 we use multiples of 'g'. (We don't know the position on earth, so can't report in m/s^2).
drivers/hid/hid-nintendo.c static const u16 JC_CAL_DATA_START = 0x603d; · line 123
Quoted 6 lines
drivers/hid/hid-nintendo.c +6
-0+static const u16 JC_ACCEL_FUZZ =10 ; +static const u16 JC_ACCEL_FLAT /*= 0*/ ; + +/* The gyro axes will be mapped in terms of this magnitude */ +static const u16 JC_MAX_GYRO_MAG =32767 ; +static const u16 JC_GYRO_RES =13371 /936 ;/* 14 (14.285) */ What does the gyro resolution equate to? For DS4 we report "degrees per second". We should do the same for the joycons, but I don't know how you guys figured out the exact resolution. As I mentioned if it is not accurate, applications will make wrong calculations.
Quoted 24 lines
drivers/hid/hid-nintendo.c +10
-0+static const u16 JC_GYRO_FUZZ =10 ; +static const u16 JC_GYRO_FLAT /*= 0*/ ; +/* frequency/amplitude tables for rumble */ struct joycon_rumble_freq_data {u16 high;@@ -234,6 +251,11 @@ struct joycon_stick_cal {s32 center; }; +struct joycon_imu_cal { +s16 offset[3 ]; +s16 scale[3 ]; +}; +/* * All the controller's button values are stored in a u32. * They can be accessed with bitwise ANDs. @@ -281,6 +303,19 @@ struct joycon_subcmd_reply {u8 data[0 ];/* will be at most 35 bytes */ } __packed; +struct joycon_imu_data { +s16 accel_x;drivers/hid/hid-nintendo.c struct joycon_subcmd_reply { · line 317
Quoted 10 lines
drivers/hid/hid-nintendo.c +10
-0+s16 accel_y; +s16 accel_z; +s16 gyro_x; +s16 gyro_y; +s16 gyro_z; +} __packed; + +struct joycon_imu_report { +struct joycon_imu_data data[3 ]; +} __packed;See comments later on about
imu_data, but you can't directly cast your data buffer to this data type due to endian issues. It may not really make sense to keep the data structure as you need to do manual data fetching.Quoted 6 lines
drivers/hid/hid-nintendo.c +1
-0+struct joycon_input_report {u8 id;u8 timer;@@ -290,11 +325,10 @@ struct joycon_input_report {u8 right_stick[3 ];drivers/hid/hid-nintendo.c struct joycon_input_report { · line 330
Quoted 10 lines
drivers/hid/hid-nintendo.c +3
-5u8 vibrator_report; -/* -* If support for firmware updates, gyroscope data, and/or NFC/IR -* are added in the future, this can be swapped for a union. -*/ -struct joycon_subcmd_reply reply; +union { +struct joycon_subcmd_reply subcmd_reply; +struct joycon_imu_report imu_report;maybe add a raw byte array to this union. Could help for parsing the imu data.
Quoted 50 lines
drivers/hid/hid-nintendo.c +21
-1+ }; } __packed;#define JC_MAX_RESP_SIZE (sizeof(struct joycon_input_report) + 35)@@ -334,6 +368,9 @@ struct joycon_ctlr {struct joycon_stick_cal right_stick_cal_x;struct joycon_stick_cal right_stick_cal_y; +struct joycon_imu_cal accel_cal; +struct joycon_imu_cal gyro_cal; +/* power supply data */ struct power_supply *battery;struct power_supply_desc battery_desc;@@ -352,6 +389,10 @@ struct joycon_ctlr {u16 rumble_lh_freq;u16 rumble_rl_freq;u16 rumble_rh_freq; + +/* imu */ +struct input_dev *imu_input; +int timestamp; };static int __joycon_hid_send (struct hid_device *hdev,u8 *data,size_t len)@@ -547,7 +588,7 @@ static int joycon_request_calibration(struct joycon_ctlr *ctlr) } report = (struct joycon_input_report *)ctlr->input_buf; - raw_cal = &report->reply.data[5 ]; + raw_cal = &report->subcmd_reply.data[5 ];/* left stick calibration parsing */ cal_x = &ctlr->left_stick_cal_x;@@ -601,6 +642,85 @@ static int joycon_request_calibration(struct joycon_ctlr *ctlr)return 0 ; } +static const s16 DFLT_ACCEL_OFFSET /*= 0*/ ; +static const s16 DFLT_ACCEL_SCALE =16384 ; +static const s16 DFLT_GYRO_OFFSET /*= 0*/ ; +static const s16 DFLT_GYRO_SCALE =13371 ; +static int joycon_request_imu_calibration (struct joycon_ctlr *ctlr) +{ +struct joycon_subcmd_request *req; +u8 buffer[sizeof (*req) +5 ] = {0 }; +struct joycon_input_report *report; +u8 *data; +u8 *raw_cal; +int ret;drivers/hid/hid-nintendo.c static int joycon_request_calibration(struct joycon_ctlr *ctlr) · line 666
Quoted 10 lines
drivers/hid/hid-nintendo.c +10
-0+int i; + +/* request IMU calibration data */ + req = (struct joycon_subcmd_request *)buffer; + req->subcmd_id =JC_SUBCMD_SPI_FLASH_READ ; + data = req->data; + data[0 ] =0xFF &JC_IMU_CAL_DATA_START ; + data[1 ] =0xFF & (JC_IMU_CAL_DATA_START >>8 ); + data[2 ] =0xFF & (JC_IMU_CAL_DATA_START >>16 ); + data[3 ] =0xFF & (JC_IMU_CAL_DATA_START >>24 );Maybe use
put_unaligned_le32? I think it allows you to avoid doing all these calculations yourself:put_unaligned_le32(JC_IMU_CAL_DATA_START, data);Quoted 24 lines
drivers/hid/hid-nintendo.c +24
-0+ data[4 ] =JC_IMU_CAL_DATA_SIZE ; + +hid_dbg (ctlr->hdev,"requesting IMU cal data\n" ); + ret =joycon_send_subcmd (ctlr, req,5 ,HZ ); + +if (ret) { +hid_warn (ctlr->hdev, +"Failed to read IMU cal, using defaults; ret=%d\n" , + ret); + +for (i =0 ; i <3 ; i++) { + ctlr->accel_cal.offset[i] =DFLT_ACCEL_OFFSET ; + ctlr->accel_cal.scale[i] =DFLT_ACCEL_SCALE ; + ctlr->gyro_cal.offset[i] =DFLT_GYRO_OFFSET ; + ctlr->gyro_cal.scale[i] =DFLT_GYRO_SCALE ; + } +return ret; + } + + report = (struct joycon_input_report *)ctlr->input_buf; + raw_cal = &report->subcmd_reply.data[5 ]; + +/* IMU calibration parsing */ +for (i =0 ; i <3 ; i++) {drivers/hid/hid-nintendo.c static int joycon_request_calibration(struct joycon_ctlr *ctlr) · line 700
Quoted 10 lines
drivers/hid/hid-nintendo.c +10
-0+int j = i *2 ; + + ctlr->accel_cal.offset[i] = raw_cal[j +0 ] | + ((s16 )raw_cal[j +1 ] <<8 ); + ctlr->accel_cal.scale[i] = raw_cal[j +6 ] | + ((s16 )raw_cal[j +7 ] <<8 ); + ctlr->gyro_cal.offset[i] = raw_cal[j +12 ] | + ((s16 )raw_cal[j +13 ] <<8 ); + ctlr->gyro_cal.scale[i] = raw_cal[j +18 ] | + ((s16 )raw_cal[j +19 ] <<8 );
get_unaligned_le16instead of doing your own bitshifts?Quoted 47 lines
drivers/hid/hid-nintendo.c +36
-0+ } + +hid_dbg (ctlr->hdev,"IMU calibration:\n" +"a_o[0]=%d a_o[1]=%d a_o[2]=%d\n" +"a_s[0]=%d a_s[1]=%d a_s[2]=%d\n" +"g_o[0]=%d g_o[1]=%d g_o[2]=%d\n" +"g_s[0]=%d g_s[1]=%d g_s[2]=%d\n" , + ctlr->accel_cal.offset[0 ], + ctlr->accel_cal.offset[1 ], + ctlr->accel_cal.offset[2 ], + ctlr->accel_cal.scale[0 ], + ctlr->accel_cal.scale[1 ], + ctlr->accel_cal.scale[2 ], + ctlr->gyro_cal.offset[0 ], + ctlr->gyro_cal.offset[1 ], + ctlr->gyro_cal.offset[2 ], + ctlr->gyro_cal.scale[0 ], + ctlr->gyro_cal.scale[1 ], + ctlr->gyro_cal.scale[2 ]); + +return 0 ; +} +static int joycon_set_report_mode (struct joycon_ctlr *ctlr) {struct joycon_subcmd_request *req;@@ -627,6 +747,19 @@ static int joycon_enable_rumble(struct joycon_ctlr *ctlr, bool enable)return joycon_send_subcmd (ctlr, req,1 ,HZ /4 ); } +static int joycon_enable_imu (struct joycon_ctlr *ctlr,bool enable) +{ +struct joycon_subcmd_request *req; +u8 buffer[sizeof (*req) +1 ] = {0 }; + + req = (struct joycon_subcmd_request *)buffer; + req->subcmd_id =JC_SUBCMD_ENABLE_IMU ; + req->data[0 ] = enable ?0x01 :0x00 ; + +hid_dbg (ctlr->hdev,"%s IMU\n" , enable ?"enabling" :"disabling" ); +return joycon_send_subcmd (ctlr, req,1 ,HZ ); +} +static s32 joycon_map_stick_val (struct joycon_stick_cal *cal,s32 val) {s32 center = cal->center;drivers/hid/hid-nintendo.c static void joycon_parse_report(struct joycon_ctlr *ctlr, · line 910
Quoted 7 lines
drivers/hid/hid-nintendo.c +4
-0@@ -771,6 +904,54 @@ static void joycon_parse_report(struct joycon_ctlr *ctlr,spin_unlock_irqrestore (&ctlr->lock, flags);wake_up (&ctlr->wait); } + +/* parse IMU data if present */ +if (rep->id ==JC_INPUT_IMU_DATA ) { +struct joycon_imu_data *imu_data = rep->imu_report.data;Quoted 7 lines
I don't think you can directly covert your input report data to
imu_data. Until now you have been lucky enough (based on a quick glance of the the other patches) that your data are single bytes. However, this data seems to be a bunch of s16's. In other words you have to deal with endianess issues. You need to useget_unaligned_le16to retrieve data from your raw byte buffer. See other HID drivers for reference.Ah, good point. I'd overlooked that entirely.
Quoted 16 lines
Since you will have to use
get_unaligned_le16, it probably won't make much sense to really have ajoycon_imu_datastructure. Maybe extend yourinput_bufferunion with raw bytes and in this case just use raw bytes. Each of your loop iterations below just grab the values from the buffer and store them in a variable.drivers/hid/hid-nintendo.c static void joycon_parse_report(struct joycon_ctlr *ctlr, · line 918
Quoted 8 lines
drivers/hid/hid-nintendo.c +8
-0+struct input_dev *idev = ctlr->imu_input; +int i; +int value[6 ]; + +for (i =0 ; i <3 ; i++) {/* there are 3 reports */ + ctlr->timestamp +=5000 ;/* each is 5 ms apart */ +input_event (idev,EV_MSC ,MSC_TIMESTAMP , + ctlr->timestamp);Quoted 4 lines
How sure are you that this is always 5ms? Is there a hardware timestamp somewhere? If I look at our DS4 the timing isn't guaranteed (Bluetooth is lossy) and not all packets even make it.
It appears that the closest thing to a hardware timestamp available is a quickly incrementing 1-byte timer sent with every report. It's only really useful for latency estimation. Each input report includes 3 IMU samples which are 5ms apart for the joy-cons. It's recently come to my attention that the samples may be spaced differently for the pro controller, so I'm going to need to dive into this more anyway. I'm not sure what a great way would be to handle lost reports.
Quoted 27 lines
Quoted 16 lines
drivers/hid/hid-nintendo.c +16
-0+ +/* +* Rather than convert to floats, we adjust by +* calibration factors and then multiply by the default +* scaling values. This way, the consumer only needs to +* divide by the default scale values. +*/ + value[0 ] = (imu_data[i].gyro_x - + ctlr->gyro_cal.offset[0 ]) * +DFLT_GYRO_SCALE / ctlr->gyro_cal.scale[0 ]; + value[1 ] = (imu_data[i].gyro_y - + ctlr->gyro_cal.offset[1 ]) * +DFLT_GYRO_SCALE / ctlr->gyro_cal.scale[1 ]; + value[2 ] = (imu_data[i].gyro_z - + ctlr->gyro_cal.offset[2 ]) * +DFLT_GYRO_SCALE / ctlr->gyro_cal.scale[2 ];drivers/hid/hid-nintendo.c static void joycon_parse_report(struct joycon_ctlr *ctlr, · line 944
Quoted 10 lines
drivers/hid/hid-nintendo.c +10
-0+ + value[3 ] = (imu_data[i].accel_x - + ctlr->accel_cal.offset[0 ]) * +DFLT_ACCEL_SCALE / ctlr->accel_cal.scale[0 ]; + value[4 ] = (imu_data[i].accel_y - + ctlr->accel_cal.offset[1 ]) * +DFLT_ACCEL_SCALE / ctlr->accel_cal.scale[1 ]; + value[5 ] = (imu_data[i].accel_z - + ctlr->accel_cal.offset[2 ]) * +DFLT_ACCEL_SCALE / ctlr->accel_cal.scale[2 ];Quoted 9 lines
Just in case I would double check the precision of your calculations. For DS4 we had similar calculations, but we had a small loss of precision, which ultimately caused major calculation errors. (Applications often use accelerometer + gyro data to calculate an absolute position. This involves integration.. and small errors become big). We had to use the "
mult_frac" macro to restore some of the precision during the calculations. It might be something to double check.That's good to know. I'll look more closely at how it's implemented in hid-sony.
Quoted 6 lines
In addition what orientation are you using for the axes? I need to double check the DS4 datasheets, but I think we were using a "right handed" coordinate system. (So if you make a fist of your right hand with thumb up, the gyro curls around it counter clockwise along the direction of your fingers).
The orientation of the axes (and much more) are documented here:
https://github.com/dekuNukem/Nintendo_Switch_Reverse_Engineering/blob/master/imu_sensor_notes.md
Since the the right vs. left joy-cons have different axes orientations, I assume it's preferable to standardize it all in software to match the orientation of the DS4.Quoted 146 lines
Quoted 146 lines
drivers/hid/hid-nintendo.c +82
-3+ +input_report_abs (idev,ABS_RX , value[0 ]); +input_report_abs (idev,ABS_RY , value[1 ]); +input_report_abs (idev,ABS_RZ , value[2 ]); +input_report_abs (idev,ABS_X , value[3 ]); +input_report_abs (idev,ABS_Y , value[4 ]); +input_report_abs (idev,ABS_Z , value[5 ]); +input_sync (idev); + } + } }static void joycon_rumble_worker (struct work_struct *work)@@ -950,6 +1131,7 @@ static int joycon_input_create(struct joycon_ctlr *ctlr) {struct hid_device *hdev;const char *name; +const char *imu_name;int ret;int i;@@ -958,12 +1140,15 @@ static int joycon_input_create(struct joycon_ctlr *ctlr)switch (hdev->product) {case USB_DEVICE_ID_NINTENDO_PROCON : name ="Nintendo Switch Pro Controller" ; + imu_name ="Nintendo Switch Pro Controller IMU" ;break ;case USB_DEVICE_ID_NINTENDO_JOYCONL : name ="Nintendo Switch Left Joy-Con" ; + imu_name ="Nintendo Switch Left Joy-Con IMU" ;break ;case USB_DEVICE_ID_NINTENDO_JOYCONR : name ="Nintendo Switch Right Joy-Con" ; + imu_name ="Nintendo Switch Right Joy-Con IMU" ;break ;default :/* Should be impossible */ hid_err (hdev,"Invalid hid product\n" );@@ -1029,6 +1214,55 @@ static int joycon_input_create(struct joycon_ctlr *ctlr)if (ret)return ret; +/* configure the imu input device */ + ctlr->imu_input =devm_input_allocate_device (&hdev->dev); +if (!ctlr->imu_input) +return -ENOMEM ; + + ctlr->imu_input->id.bustype = hdev->bus; + ctlr->imu_input->id.vendor = hdev->vendor; + ctlr->imu_input->id.product = hdev->product; + ctlr->imu_input->id.version = hdev->version; + ctlr->imu_input->uniq = ctlr->mac_addr_str; + ctlr->imu_input->name = imu_name; +input_set_drvdata (ctlr->imu_input, ctlr); + +/* configure imu axes */ +input_set_abs_params (ctlr->imu_input,ABS_X , + -JC_MAX_ACCEL_MAG ,JC_MAX_ACCEL_MAG , +JC_ACCEL_FUZZ ,JC_ACCEL_FLAT ); +input_set_abs_params (ctlr->imu_input,ABS_Y , + -JC_MAX_ACCEL_MAG ,JC_MAX_ACCEL_MAG , +JC_ACCEL_FUZZ ,JC_ACCEL_FLAT ); +input_set_abs_params (ctlr->imu_input,ABS_Z , + -JC_MAX_ACCEL_MAG ,JC_MAX_ACCEL_MAG , +JC_ACCEL_FUZZ ,JC_ACCEL_FLAT ); +input_abs_set_res (ctlr->imu_input,ABS_X ,JC_ACCEL_RES ); +input_abs_set_res (ctlr->imu_input,ABS_Y ,JC_ACCEL_RES ); +input_abs_set_res (ctlr->imu_input,ABS_Z ,JC_ACCEL_RES ); + +input_set_abs_params (ctlr->imu_input,ABS_RX , + -JC_MAX_GYRO_MAG ,JC_MAX_GYRO_MAG , +JC_GYRO_FUZZ ,JC_GYRO_FLAT ); +input_set_abs_params (ctlr->imu_input,ABS_RY , + -JC_MAX_GYRO_MAG ,JC_MAX_GYRO_MAG , +JC_GYRO_FUZZ ,JC_GYRO_FLAT ); +input_set_abs_params (ctlr->imu_input,ABS_RZ , + -JC_MAX_GYRO_MAG ,JC_MAX_GYRO_MAG , +JC_GYRO_FUZZ ,JC_GYRO_FLAT ); + +input_abs_set_res (ctlr->imu_input,ABS_RX ,JC_GYRO_RES ); +input_abs_set_res (ctlr->imu_input,ABS_RY ,JC_GYRO_RES ); +input_abs_set_res (ctlr->imu_input,ABS_RZ ,JC_GYRO_RES ); + +__set_bit (EV_MSC , ctlr->imu_input->evbit); +__set_bit (MSC_TIMESTAMP , ctlr->imu_input->mscbit); +__set_bit (INPUT_PROP_ACCELEROMETER , ctlr->imu_input->propbit); + + ret =input_register_device (ctlr->imu_input); +if (ret) +return ret; +return 0 ; }@@ -1288,7 +1522,7 @@ static int joycon_read_mac(struct joycon_ctlr *ctlr) report = (struct joycon_input_report *)ctlr->input_buf;for (i =4 , j =0 ; j <6 ; i++, j++) - ctlr->mac_addr[j] = report->reply.data[i]; + ctlr->mac_addr[j] = report->subcmd_reply.data[i]; ctlr->mac_addr_str = devm_kasprintf(&ctlr->hdev->dev, GFP_KERNEL, "%02X:%02X:%02X:%02X:%02X:%02X",@@ -1343,7 +1577,7 @@ static int joycon_ctlr_handle_event(struct joycon_ctlr *ctlr, u8 *data, data[0] != JC_INPUT_SUBCMD_REPLY)break ; report = (struct joycon_input_report *)data; -if (report->reply.id == ctlr->subcmd_ack_match) +if (report->subcmd_reply.id == ctlr->subcmd_ack_match) match = true;break ;default :@@ -1469,6 +1703,16 @@ static int nintendo_hid_probe(struct hid_device *hdev,hid_warn (hdev,"Analog stick positions may be inaccurate\n" ); } +/* get IMU calibration data, and parse it */ + ret =joycon_request_imu_calibration (ctlr); +if (ret) { +/* +* We can function with default calibration, but it may be +* inaccurate. Provide a warning, and continue on. +*/ +hid_warn (hdev,"Unable to read IMU calibration data\n" ); + } +/* Set the reporting mode to 0x30, which is the full report mode */ ret =joycon_set_report_mode (ctlr);if (ret) {@@ -1483,6 +1727,13 @@ static int nintendo_hid_probe(struct hid_device *hdev, goto err_mutex; } +/* Enable the IMU */ + ret =joycon_enable_imu (ctlr, true); +if (ret) { +hid_err (hdev,"Failed to enable the IMU; ret=%d\n" , ret); + goto err_mutex; + } + ret =joycon_read_mac (ctlr);if (ret) { hid_err(hdev, "Failed to retrieve controller MAC; ret=%d\n", --2.24.1
Re: [PATCH v10 11/12] HID: nintendo: add IMU support
(3rd/final attempt at reply due to list server rejecting message with HTML subpart; sorry about the spam!)
Hi Everyone,
In trying out this driver to use with the Switch Pro Controller, the biggest difficulty I had was that the D-pad is reported as buttons instead of as a Hat. All other controllers that are similar in structure (such as the PS3 and PS4 controllers, the Xbox controllers, and many others) report the D-pad as a Hat. This kind of consistency is needed to avoid lots of software compatibility issues.
I mentioned this to Daniel, and he indicated that he wanted the Switch Pro Controller to behave like the JoyCons, which have buttons instead of a D-pad. Having the JoyCons report the buttons as buttons makes sense, since it's possible to push opposite directions at the same time. However, I don't think that the argument carries over to the Switch Pro Controller, since it has a physically different control.
Again, the consistency with the other controllers that have all the same physical structure and the same types of controls seems more important to me than consistency with a controller that is physically much different. Additionally, many games are already written for use with controllers that look like the Switch Pro Controller, whereas fewer are written for use with Nintendo JoyCons. If we have to cause trouble for one side or the other, let's not cause trouble with the more established side.
Quoted 563 lines · Roderick Colenbrander
Hi Daniel,
Unfortunately there is no public test application at the moment to illustrate these features. I agree something is definitely needed.
I need to see if we can come up with something. One of the test apps we have internally is a 3D cube, which is controllable by controller motion. It of course shows the gyro / accelerometer values, but the position of the cube is tied to the calculated orientation. If something is off you will see weird movements in the cube or drift building up over time etcetera.
Though it would take some time to prepare something. The rest of the patch series looked fine I think, so the IMU parts may need to wait for a next kernel cycle, but all the other plumbing could go in (if others are okay of course).
Thanks,
RoderickQuoted 541 lines · Daniel Ogorchock
Hi Roderick,
Thanks for the feedback. In addition to the inline comments below, do you have any recommendations for test programs that you know work well with hid-sony's gyro implementation? Up to this point I've just been using evtest, which obviously isn't too useful for testing actual functionality of the gyro in an intuitive way.
Quoted 292 lines · Roderick Colenbrander
Hi Daniel,
I had some time to review the motion sensors patch. I have added some feedback inline with your patch below.
Aside from standard feedback on the code, I wanted to have a close look at the accelerometer / gyro data. My team has been doing a lot of work on this (and still is) and I want to make sure any work we do on "user space" software (e.g. Android) automatically works for Joycon as well. The accuracy of the data is very important else applications will make bad decisions. Userspace applications often combine the data of both sensors to calculate a position for position tracking. Inaccurate axes ranges or wrong precision can cause major issues. I may be a bit strict below, but it will just be to prevent headaches for others later on. I use the DS4 as a reference point as it was the first device (aside from Wii early on) where we properly report acceleration and gyro axes with
INPUT_PROP_ACCELEROMETERand we should be consistent with it here else applications could be confused, so we should use similar orientation of axes and resolutions.Thanks,
RoderickQuoted 25 lines · Daniel J. Ogorchock
This patch adds support for the controller's IMU. The accelerometer and gyro data are both provided to userspace using a second input device. The devices can be associated using their uniq value (set to the controller's MAC address).
The majority of this patch's functinoality was provided by Carl Mueller.
Signed-off-by Daniel J. Ogorchock <[email protected]>--- drivers/hid/hid-nintendo.c | 267 +++++++++++++++++++++++++++++++++++-- 1 file changed, 259 insertions(+), 8 deletions(-)drivers/hid/hid-nintendo.c +4
-0@@ -101,12 +101,29 @@ static const u16 JC_CAL_DATA_START = 0x603d;static const u16 JC_CAL_DATA_END =0x604e ;#define JC_CAL_DATA_SIZE (JC_CAL_DATA_END - JC_CAL_DATA_START + 1) +/* SPI storage addresses of IMU factory calibration data */ +static const u16 JC_IMU_CAL_DATA_START =0x6020 ; +static const u16 JC_IMU_CAL_DATA_END =0x6037 ; +#define JC_IMU_CAL_DATA_SIZE \drivers/hid/hid-nintendo.c static const u16 JC_CAL_DATA_START = 0x603d; · line 117
Quoted 10 lines
drivers/hid/hid-nintendo.c +4
-0+ (JC_IMU_CAL_DATA_END -JC_IMU_CAL_DATA_START +1 ) /* The raw analog joystick values will be mapped in terms of this magnitude */static const u16 JC_MAX_STICK_MAG =32767 ;static const u16 JC_STICK_FUZZ =250 ;static const u16 JC_STICK_FLAT =500 ; +/* The accel axes will be mapped in terms of this magnitude */ +static const u16 JC_MAX_ACCEL_MAG =32767 ; +static const u16 JC_ACCEL_RES =4096 ;What does the acceleration resolution equate to? For DS4 we use multiples of 'g'. (We don't know the position on earth, so can't report in m/s^2).
drivers/hid/hid-nintendo.c static const u16 JC_CAL_DATA_START = 0x603d; · line 123
Quoted 6 lines
drivers/hid/hid-nintendo.c +6
-0+static const u16 JC_ACCEL_FUZZ =10 ; +static const u16 JC_ACCEL_FLAT /*= 0*/ ; + +/* The gyro axes will be mapped in terms of this magnitude */ +static const u16 JC_MAX_GYRO_MAG =32767 ; +static const u16 JC_GYRO_RES =13371 /936 ;/* 14 (14.285) */ What does the gyro resolution equate to? For DS4 we report "degrees per second". We should do the same for the joycons, but I don't know how you guys figured out the exact resolution. As I mentioned if it is not accurate, applications will make wrong calculations.
Quoted 24 lines
drivers/hid/hid-nintendo.c +10
-0+static const u16 JC_GYRO_FUZZ =10 ; +static const u16 JC_GYRO_FLAT /*= 0*/ ; +/* frequency/amplitude tables for rumble */ struct joycon_rumble_freq_data {u16 high;@@ -234,6 +251,11 @@ struct joycon_stick_cal {s32 center; }; +struct joycon_imu_cal { +s16 offset[3 ]; +s16 scale[3 ]; +}; +/* * All the controller's button values are stored in a u32. * They can be accessed with bitwise ANDs. @@ -281,6 +303,19 @@ struct joycon_subcmd_reply {u8 data[0 ];/* will be at most 35 bytes */ } __packed; +struct joycon_imu_data { +s16 accel_x;drivers/hid/hid-nintendo.c struct joycon_subcmd_reply { · line 317
Quoted 10 lines
drivers/hid/hid-nintendo.c +10
-0+s16 accel_y; +s16 accel_z; +s16 gyro_x; +s16 gyro_y; +s16 gyro_z; +} __packed; + +struct joycon_imu_report { +struct joycon_imu_data data[3 ]; +} __packed;See comments later on about
imu_data, but you can't directly cast your data buffer to this data type due to endian issues. It may not really make sense to keep the data structure as you need to do manual data fetching.Quoted 6 lines
drivers/hid/hid-nintendo.c +1
-0+struct joycon_input_report {u8 id;u8 timer;@@ -290,11 +325,10 @@ struct joycon_input_report {u8 right_stick[3 ];drivers/hid/hid-nintendo.c struct joycon_input_report { · line 330
Quoted 10 lines
drivers/hid/hid-nintendo.c +3
-5u8 vibrator_report; -/* -* If support for firmware updates, gyroscope data, and/or NFC/IR -* are added in the future, this can be swapped for a union. -*/ -struct joycon_subcmd_reply reply; +union { +struct joycon_subcmd_reply subcmd_reply; +struct joycon_imu_report imu_report;maybe add a raw byte array to this union. Could help for parsing the imu data.
Quoted 50 lines
drivers/hid/hid-nintendo.c +21
-1+ }; } __packed;#define JC_MAX_RESP_SIZE (sizeof(struct joycon_input_report) + 35)@@ -334,6 +368,9 @@ struct joycon_ctlr {struct joycon_stick_cal right_stick_cal_x;struct joycon_stick_cal right_stick_cal_y; +struct joycon_imu_cal accel_cal; +struct joycon_imu_cal gyro_cal; +/* power supply data */ struct power_supply *battery;struct power_supply_desc battery_desc;@@ -352,6 +389,10 @@ struct joycon_ctlr {u16 rumble_lh_freq;u16 rumble_rl_freq;u16 rumble_rh_freq; + +/* imu */ +struct input_dev *imu_input; +int timestamp; };static int __joycon_hid_send (struct hid_device *hdev,u8 *data,size_t len)@@ -547,7 +588,7 @@ static int joycon_request_calibration(struct joycon_ctlr *ctlr) } report = (struct joycon_input_report *)ctlr->input_buf; - raw_cal = &report->reply.data[5 ]; + raw_cal = &report->subcmd_reply.data[5 ];/* left stick calibration parsing */ cal_x = &ctlr->left_stick_cal_x;@@ -601,6 +642,85 @@ static int joycon_request_calibration(struct joycon_ctlr *ctlr)return 0 ; } +static const s16 DFLT_ACCEL_OFFSET /*= 0*/ ; +static const s16 DFLT_ACCEL_SCALE =16384 ; +static const s16 DFLT_GYRO_OFFSET /*= 0*/ ; +static const s16 DFLT_GYRO_SCALE =13371 ; +static int joycon_request_imu_calibration (struct joycon_ctlr *ctlr) +{ +struct joycon_subcmd_request *req; +u8 buffer[sizeof (*req) +5 ] = {0 }; +struct joycon_input_report *report; +u8 *data; +u8 *raw_cal; +int ret;drivers/hid/hid-nintendo.c static int joycon_request_calibration(struct joycon_ctlr *ctlr) · line 666
Quoted 10 lines
drivers/hid/hid-nintendo.c +10
-0+int i; + +/* request IMU calibration data */ + req = (struct joycon_subcmd_request *)buffer; + req->subcmd_id =JC_SUBCMD_SPI_FLASH_READ ; + data = req->data; + data[0 ] =0xFF &JC_IMU_CAL_DATA_START ; + data[1 ] =0xFF & (JC_IMU_CAL_DATA_START >>8 ); + data[2 ] =0xFF & (JC_IMU_CAL_DATA_START >>16 ); + data[3 ] =0xFF & (JC_IMU_CAL_DATA_START >>24 );Maybe use
put_unaligned_le32? I think it allows you to avoid doing all these calculations yourself:put_unaligned_le32(JC_IMU_CAL_DATA_START, data);Quoted 24 lines
drivers/hid/hid-nintendo.c +24
-0+ data[4 ] =JC_IMU_CAL_DATA_SIZE ; + +hid_dbg (ctlr->hdev,"requesting IMU cal data\n" ); + ret =joycon_send_subcmd (ctlr, req,5 ,HZ ); + +if (ret) { +hid_warn (ctlr->hdev, +"Failed to read IMU cal, using defaults; ret=%d\n" , + ret); + +for (i =0 ; i <3 ; i++) { + ctlr->accel_cal.offset[i] =DFLT_ACCEL_OFFSET ; + ctlr->accel_cal.scale[i] =DFLT_ACCEL_SCALE ; + ctlr->gyro_cal.offset[i] =DFLT_GYRO_OFFSET ; + ctlr->gyro_cal.scale[i] =DFLT_GYRO_SCALE ; + } +return ret; + } + + report = (struct joycon_input_report *)ctlr->input_buf; + raw_cal = &report->subcmd_reply.data[5 ]; + +/* IMU calibration parsing */ +for (i =0 ; i <3 ; i++) {drivers/hid/hid-nintendo.c static int joycon_request_calibration(struct joycon_ctlr *ctlr) · line 700
Quoted 10 lines
drivers/hid/hid-nintendo.c +10
-0+int j = i *2 ; + + ctlr->accel_cal.offset[i] = raw_cal[j +0 ] | + ((s16 )raw_cal[j +1 ] <<8 ); + ctlr->accel_cal.scale[i] = raw_cal[j +6 ] | + ((s16 )raw_cal[j +7 ] <<8 ); + ctlr->gyro_cal.offset[i] = raw_cal[j +12 ] | + ((s16 )raw_cal[j +13 ] <<8 ); + ctlr->gyro_cal.scale[i] = raw_cal[j +18 ] | + ((s16 )raw_cal[j +19 ] <<8 );
get_unaligned_le16instead of doing your own bitshifts?Quoted 47 lines
drivers/hid/hid-nintendo.c +36
-0+ } + +hid_dbg (ctlr->hdev,"IMU calibration:\n" +"a_o[0]=%d a_o[1]=%d a_o[2]=%d\n" +"a_s[0]=%d a_s[1]=%d a_s[2]=%d\n" +"g_o[0]=%d g_o[1]=%d g_o[2]=%d\n" +"g_s[0]=%d g_s[1]=%d g_s[2]=%d\n" , + ctlr->accel_cal.offset[0 ], + ctlr->accel_cal.offset[1 ], + ctlr->accel_cal.offset[2 ], + ctlr->accel_cal.scale[0 ], + ctlr->accel_cal.scale[1 ], + ctlr->accel_cal.scale[2 ], + ctlr->gyro_cal.offset[0 ], + ctlr->gyro_cal.offset[1 ], + ctlr->gyro_cal.offset[2 ], + ctlr->gyro_cal.scale[0 ], + ctlr->gyro_cal.scale[1 ], + ctlr->gyro_cal.scale[2 ]); + +return 0 ; +} +static int joycon_set_report_mode (struct joycon_ctlr *ctlr) {struct joycon_subcmd_request *req;@@ -627,6 +747,19 @@ static int joycon_enable_rumble(struct joycon_ctlr *ctlr, bool enable)return joycon_send_subcmd (ctlr, req,1 ,HZ /4 ); } +static int joycon_enable_imu (struct joycon_ctlr *ctlr,bool enable) +{ +struct joycon_subcmd_request *req; +u8 buffer[sizeof (*req) +1 ] = {0 }; + + req = (struct joycon_subcmd_request *)buffer; + req->subcmd_id =JC_SUBCMD_ENABLE_IMU ; + req->data[0 ] = enable ?0x01 :0x00 ; + +hid_dbg (ctlr->hdev,"%s IMU\n" , enable ?"enabling" :"disabling" ); +return joycon_send_subcmd (ctlr, req,1 ,HZ ); +} +static s32 joycon_map_stick_val (struct joycon_stick_cal *cal,s32 val) {s32 center = cal->center;drivers/hid/hid-nintendo.c static void joycon_parse_report(struct joycon_ctlr *ctlr, · line 910
Quoted 7 lines
drivers/hid/hid-nintendo.c +4
-0@@ -771,6 +904,54 @@ static void joycon_parse_report(struct joycon_ctlr *ctlr,spin_unlock_irqrestore (&ctlr->lock, flags);wake_up (&ctlr->wait); } + +/* parse IMU data if present */ +if (rep->id ==JC_INPUT_IMU_DATA ) { +struct joycon_imu_data *imu_data = rep->imu_report.data;Quoted 7 lines
I don't think you can directly covert your input report data to
imu_data. Until now you have been lucky enough (based on a quick glance of the the other patches) that your data are single bytes. However, this data seems to be a bunch of s16's. In other words you have to deal with endianess issues. You need to useget_unaligned_le16to retrieve data from your raw byte buffer. See other HID drivers for reference.Ah, good point. I'd overlooked that entirely.
Quoted 16 lines
Since you will have to use
get_unaligned_le16, it probably won't make much sense to really have ajoycon_imu_datastructure. Maybe extend yourinput_bufferunion with raw bytes and in this case just use raw bytes. Each of your loop iterations below just grab the values from the buffer and store them in a variable.drivers/hid/hid-nintendo.c static void joycon_parse_report(struct joycon_ctlr *ctlr, · line 918
Quoted 8 lines
drivers/hid/hid-nintendo.c +8
-0+struct input_dev *idev = ctlr->imu_input; +int i; +int value[6 ]; + +for (i =0 ; i <3 ; i++) {/* there are 3 reports */ + ctlr->timestamp +=5000 ;/* each is 5 ms apart */ +input_event (idev,EV_MSC ,MSC_TIMESTAMP , + ctlr->timestamp);Quoted 4 lines
How sure are you that this is always 5ms? Is there a hardware timestamp somewhere? If I look at our DS4 the timing isn't guaranteed (Bluetooth is lossy) and not all packets even make it.
It appears that the closest thing to a hardware timestamp available is a quickly incrementing 1-byte timer sent with every report. It's only really useful for latency estimation. Each input report includes 3 IMU samples which are 5ms apart for the joy-cons. It's recently come to my attention that the samples may be spaced differently for the pro controller, so I'm going to need to dive into this more anyway. I'm not sure what a great way would be to handle lost reports.
Quoted 27 lines
Quoted 16 lines
drivers/hid/hid-nintendo.c +16
-0+ +/* +* Rather than convert to floats, we adjust by +* calibration factors and then multiply by the default +* scaling values. This way, the consumer only needs to +* divide by the default scale values. +*/ + value[0 ] = (imu_data[i].gyro_x - + ctlr->gyro_cal.offset[0 ]) * +DFLT_GYRO_SCALE / ctlr->gyro_cal.scale[0 ]; + value[1 ] = (imu_data[i].gyro_y - + ctlr->gyro_cal.offset[1 ]) * +DFLT_GYRO_SCALE / ctlr->gyro_cal.scale[1 ]; + value[2 ] = (imu_data[i].gyro_z - + ctlr->gyro_cal.offset[2 ]) * +DFLT_GYRO_SCALE / ctlr->gyro_cal.scale[2 ];drivers/hid/hid-nintendo.c static void joycon_parse_report(struct joycon_ctlr *ctlr, · line 944
Quoted 10 lines
drivers/hid/hid-nintendo.c +10
-0+ + value[3 ] = (imu_data[i].accel_x - + ctlr->accel_cal.offset[0 ]) * +DFLT_ACCEL_SCALE / ctlr->accel_cal.scale[0 ]; + value[4 ] = (imu_data[i].accel_y - + ctlr->accel_cal.offset[1 ]) * +DFLT_ACCEL_SCALE / ctlr->accel_cal.scale[1 ]; + value[5 ] = (imu_data[i].accel_z - + ctlr->accel_cal.offset[2 ]) * +DFLT_ACCEL_SCALE / ctlr->accel_cal.scale[2 ];Quoted 9 lines
Just in case I would double check the precision of your calculations. For DS4 we had similar calculations, but we had a small loss of precision, which ultimately caused major calculation errors. (Applications often use accelerometer + gyro data to calculate an absolute position. This involves integration.. and small errors become big). We had to use the "
mult_frac" macro to restore some of the precision during the calculations. It might be something to double check.That's good to know. I'll look more closely at how it's implemented in hid-sony.
Quoted 6 lines
In addition what orientation are you using for the axes? I need to double check the DS4 datasheets, but I think we were using a "right handed" coordinate system. (So if you make a fist of your right hand with thumb up, the gyro curls around it counter clockwise along the direction of your fingers).
The orientation of the axes (and much more) are documented here:
https://github.com/dekuNukem/Nintendo_Switch_Reverse_Engineering/blob/master/imu_sensor_notes.md
Since the the right vs. left joy-cons have different axes orientations, I assume it's preferable to standardize it all in software to match the orientation of the DS4.Quoted 146 lines
Quoted 146 lines
drivers/hid/hid-nintendo.c +82
-3+ +input_report_abs (idev,ABS_RX , value[0 ]); +input_report_abs (idev,ABS_RY , value[1 ]); +input_report_abs (idev,ABS_RZ , value[2 ]); +input_report_abs (idev,ABS_X , value[3 ]); +input_report_abs (idev,ABS_Y , value[4 ]); +input_report_abs (idev,ABS_Z , value[5 ]); +input_sync (idev); + } + } }static void joycon_rumble_worker (struct work_struct *work)@@ -950,6 +1131,7 @@ static int joycon_input_create(struct joycon_ctlr *ctlr) {struct hid_device *hdev;const char *name; +const char *imu_name;int ret;int i;@@ -958,12 +1140,15 @@ static int joycon_input_create(struct joycon_ctlr *ctlr)switch (hdev->product) {case USB_DEVICE_ID_NINTENDO_PROCON : name ="Nintendo Switch Pro Controller" ; + imu_name ="Nintendo Switch Pro Controller IMU" ;break ;case USB_DEVICE_ID_NINTENDO_JOYCONL : name ="Nintendo Switch Left Joy-Con" ; + imu_name ="Nintendo Switch Left Joy-Con IMU" ;break ;case USB_DEVICE_ID_NINTENDO_JOYCONR : name ="Nintendo Switch Right Joy-Con" ; + imu_name ="Nintendo Switch Right Joy-Con IMU" ;break ;default :/* Should be impossible */ hid_err (hdev,"Invalid hid product\n" );@@ -1029,6 +1214,55 @@ static int joycon_input_create(struct joycon_ctlr *ctlr)if (ret)return ret; +/* configure the imu input device */ + ctlr->imu_input =devm_input_allocate_device (&hdev->dev); +if (!ctlr->imu_input) +return -ENOMEM ; + + ctlr->imu_input->id.bustype = hdev->bus; + ctlr->imu_input->id.vendor = hdev->vendor; + ctlr->imu_input->id.product = hdev->product; + ctlr->imu_input->id.version = hdev->version; + ctlr->imu_input->uniq = ctlr->mac_addr_str; + ctlr->imu_input->name = imu_name; +input_set_drvdata (ctlr->imu_input, ctlr); + +/* configure imu axes */ +input_set_abs_params (ctlr->imu_input,ABS_X , + -JC_MAX_ACCEL_MAG ,JC_MAX_ACCEL_MAG , +JC_ACCEL_FUZZ ,JC_ACCEL_FLAT ); +input_set_abs_params (ctlr->imu_input,ABS_Y , + -JC_MAX_ACCEL_MAG ,JC_MAX_ACCEL_MAG , +JC_ACCEL_FUZZ ,JC_ACCEL_FLAT ); +input_set_abs_params (ctlr->imu_input,ABS_Z , + -JC_MAX_ACCEL_MAG ,JC_MAX_ACCEL_MAG , +JC_ACCEL_FUZZ ,JC_ACCEL_FLAT ); +input_abs_set_res (ctlr->imu_input,ABS_X ,JC_ACCEL_RES ); +input_abs_set_res (ctlr->imu_input,ABS_Y ,JC_ACCEL_RES ); +input_abs_set_res (ctlr->imu_input,ABS_Z ,JC_ACCEL_RES ); + +input_set_abs_params (ctlr->imu_input,ABS_RX , + -JC_MAX_GYRO_MAG ,JC_MAX_GYRO_MAG , +JC_GYRO_FUZZ ,JC_GYRO_FLAT ); +input_set_abs_params (ctlr->imu_input,ABS_RY , + -JC_MAX_GYRO_MAG ,JC_MAX_GYRO_MAG , +JC_GYRO_FUZZ ,JC_GYRO_FLAT ); +input_set_abs_params (ctlr->imu_input,ABS_RZ , + -JC_MAX_GYRO_MAG ,JC_MAX_GYRO_MAG , +JC_GYRO_FUZZ ,JC_GYRO_FLAT ); + +input_abs_set_res (ctlr->imu_input,ABS_RX ,JC_GYRO_RES ); +input_abs_set_res (ctlr->imu_input,ABS_RY ,JC_GYRO_RES ); +input_abs_set_res (ctlr->imu_input,ABS_RZ ,JC_GYRO_RES ); + +__set_bit (EV_MSC , ctlr->imu_input->evbit); +__set_bit (MSC_TIMESTAMP , ctlr->imu_input->mscbit); +__set_bit (INPUT_PROP_ACCELEROMETER , ctlr->imu_input->propbit); + + ret =input_register_device (ctlr->imu_input); +if (ret) +return ret; +return 0 ; }@@ -1288,7 +1522,7 @@ static int joycon_read_mac(struct joycon_ctlr *ctlr) report = (struct joycon_input_report *)ctlr->input_buf;for (i =4 , j =0 ; j <6 ; i++, j++) - ctlr->mac_addr[j] = report->reply.data[i]; + ctlr->mac_addr[j] = report->subcmd_reply.data[i]; ctlr->mac_addr_str = devm_kasprintf(&ctlr->hdev->dev, GFP_KERNEL, "%02X:%02X:%02X:%02X:%02X:%02X",@@ -1343,7 +1577,7 @@ static int joycon_ctlr_handle_event(struct joycon_ctlr *ctlr, u8 *data, data[0] != JC_INPUT_SUBCMD_REPLY)break ; report = (struct joycon_input_report *)data; -if (report->reply.id == ctlr->subcmd_ack_match) +if (report->subcmd_reply.id == ctlr->subcmd_ack_match) match = true;break ;default :@@ -1469,6 +1703,16 @@ static int nintendo_hid_probe(struct hid_device *hdev,hid_warn (hdev,"Analog stick positions may be inaccurate\n" ); } +/* get IMU calibration data, and parse it */ + ret =joycon_request_imu_calibration (ctlr); +if (ret) { +/* +* We can function with default calibration, but it may be +* inaccurate. Provide a warning, and continue on. +*/ +hid_warn (hdev,"Unable to read IMU calibration data\n" ); + } +/* Set the reporting mode to 0x30, which is the full report mode */ ret =joycon_set_report_mode (ctlr);if (ret) {@@ -1483,6 +1727,13 @@ static int nintendo_hid_probe(struct hid_device *hdev, goto err_mutex; } +/* Enable the IMU */ + ret =joycon_enable_imu (ctlr, true); +if (ret) { +hid_err (hdev,"Failed to enable the IMU; ret=%d\n" , ret); + goto err_mutex; + } + ret =joycon_read_mac (ctlr);if (ret) { hid_err(hdev, "Failed to retrieve controller MAC; ret=%d\n", --2.24.1
Daniel Ogorchock
maintainerRe: [PATCH v10 11/12] HID: nintendo: add IMU support
Hi Carl,
When I was initially implementing the button mappings, I referred to the standard described here:
https://www.kernel.org/doc/html/latest/input/gamepad.html
It mentions under the d-pad section that digital inputs should be reported using the BTN_DPAD_* variant. Do the other controllers mentioned report their d-pads as analog values, or has using the ABS_HAT* values become the de facto format for digital inputs as well?
If the latter, I guess it would make sense to go with the crowd for the pro. It just seems a bit odd to report digital inputs as absolute axes, but I'm open to changing it if that's the consensus.
Thanks,
Daniel
Quoted 593 lines · Carl Mueller
(3rd/final attempt at reply due to list server rejecting message with HTML subpart; sorry about the spam!)
Hi Everyone,
In trying out this driver to use with the Switch Pro Controller, the biggest difficulty I had was that the D-pad is reported as buttons instead of as a Hat. All other controllers that are similar in structure (such as the PS3 and PS4 controllers, the Xbox controllers, and many others) report the D-pad as a Hat. This kind of consistency is needed to avoid lots of software compatibility issues.
I mentioned this to Daniel, and he indicated that he wanted the Switch Pro Controller to behave like the JoyCons, which have buttons instead of a D-pad. Having the JoyCons report the buttons as buttons makes sense, since it's possible to push opposite directions at the same time. However, I don't think that the argument carries over to the Switch Pro Controller, since it has a physically different control.
Again, the consistency with the other controllers that have all the same physical structure and the same types of controls seems more important to me than consistency with a controller that is physically much different. Additionally, many games are already written for use with controllers that look like the Switch Pro Controller, whereas fewer are written for use with Nintendo JoyCons. If we have to cause trouble for one side or the other, let's not cause trouble with the more established side.
Quoted 563 lines · Roderick Colenbrander
Hi Daniel,
Unfortunately there is no public test application at the moment to illustrate these features. I agree something is definitely needed.
I need to see if we can come up with something. One of the test apps we have internally is a 3D cube, which is controllable by controller motion. It of course shows the gyro / accelerometer values, but the position of the cube is tied to the calculated orientation. If something is off you will see weird movements in the cube or drift building up over time etcetera.
Though it would take some time to prepare something. The rest of the patch series looked fine I think, so the IMU parts may need to wait for a next kernel cycle, but all the other plumbing could go in (if others are okay of course).
Thanks,
RoderickQuoted 541 lines · Daniel Ogorchock
Hi Roderick,
Thanks for the feedback. In addition to the inline comments below, do you have any recommendations for test programs that you know work well with hid-sony's gyro implementation? Up to this point I've just been using evtest, which obviously isn't too useful for testing actual functionality of the gyro in an intuitive way.
Quoted 292 lines · Roderick Colenbrander
Hi Daniel,
I had some time to review the motion sensors patch. I have added some feedback inline with your patch below.
Aside from standard feedback on the code, I wanted to have a close look at the accelerometer / gyro data. My team has been doing a lot of work on this (and still is) and I want to make sure any work we do on "user space" software (e.g. Android) automatically works for Joycon as well. The accuracy of the data is very important else applications will make bad decisions. Userspace applications often combine the data of both sensors to calculate a position for position tracking. Inaccurate axes ranges or wrong precision can cause major issues. I may be a bit strict below, but it will just be to prevent headaches for others later on. I use the DS4 as a reference point as it was the first device (aside from Wii early on) where we properly report acceleration and gyro axes with
INPUT_PROP_ACCELEROMETERand we should be consistent with it here else applications could be confused, so we should use similar orientation of axes and resolutions.Thanks,
RoderickQuoted 25 lines · Daniel J. Ogorchock
This patch adds support for the controller's IMU. The accelerometer and gyro data are both provided to userspace using a second input device. The devices can be associated using their uniq value (set to the controller's MAC address).
The majority of this patch's functinoality was provided by Carl Mueller.
Signed-off-by Daniel J. Ogorchock <[email protected]>--- drivers/hid/hid-nintendo.c | 267 +++++++++++++++++++++++++++++++++++-- 1 file changed, 259 insertions(+), 8 deletions(-)drivers/hid/hid-nintendo.c +4
-0@@ -101,12 +101,29 @@ static const u16 JC_CAL_DATA_START = 0x603d;static const u16 JC_CAL_DATA_END =0x604e ;#define JC_CAL_DATA_SIZE (JC_CAL_DATA_END - JC_CAL_DATA_START + 1) +/* SPI storage addresses of IMU factory calibration data */ +static const u16 JC_IMU_CAL_DATA_START =0x6020 ; +static const u16 JC_IMU_CAL_DATA_END =0x6037 ; +#define JC_IMU_CAL_DATA_SIZE \drivers/hid/hid-nintendo.c static const u16 JC_CAL_DATA_START = 0x603d; · line 117
Quoted 10 lines
drivers/hid/hid-nintendo.c +4
-0+ (JC_IMU_CAL_DATA_END -JC_IMU_CAL_DATA_START +1 ) /* The raw analog joystick values will be mapped in terms of this magnitude */static const u16 JC_MAX_STICK_MAG =32767 ;static const u16 JC_STICK_FUZZ =250 ;static const u16 JC_STICK_FLAT =500 ; +/* The accel axes will be mapped in terms of this magnitude */ +static const u16 JC_MAX_ACCEL_MAG =32767 ; +static const u16 JC_ACCEL_RES =4096 ;What does the acceleration resolution equate to? For DS4 we use multiples of 'g'. (We don't know the position on earth, so can't report in m/s^2).
drivers/hid/hid-nintendo.c static const u16 JC_CAL_DATA_START = 0x603d; · line 123
Quoted 6 lines
drivers/hid/hid-nintendo.c +6
-0+static const u16 JC_ACCEL_FUZZ =10 ; +static const u16 JC_ACCEL_FLAT /*= 0*/ ; + +/* The gyro axes will be mapped in terms of this magnitude */ +static const u16 JC_MAX_GYRO_MAG =32767 ; +static const u16 JC_GYRO_RES =13371 /936 ;/* 14 (14.285) */ What does the gyro resolution equate to? For DS4 we report "degrees per second". We should do the same for the joycons, but I don't know how you guys figured out the exact resolution. As I mentioned if it is not accurate, applications will make wrong calculations.
Quoted 24 lines
drivers/hid/hid-nintendo.c +10
-0+static const u16 JC_GYRO_FUZZ =10 ; +static const u16 JC_GYRO_FLAT /*= 0*/ ; +/* frequency/amplitude tables for rumble */ struct joycon_rumble_freq_data {u16 high;@@ -234,6 +251,11 @@ struct joycon_stick_cal {s32 center; }; +struct joycon_imu_cal { +s16 offset[3 ]; +s16 scale[3 ]; +}; +/* * All the controller's button values are stored in a u32. * They can be accessed with bitwise ANDs. @@ -281,6 +303,19 @@ struct joycon_subcmd_reply {u8 data[0 ];/* will be at most 35 bytes */ } __packed; +struct joycon_imu_data { +s16 accel_x;drivers/hid/hid-nintendo.c struct joycon_subcmd_reply { · line 317
Quoted 10 lines
drivers/hid/hid-nintendo.c +10
-0+s16 accel_y; +s16 accel_z; +s16 gyro_x; +s16 gyro_y; +s16 gyro_z; +} __packed; + +struct joycon_imu_report { +struct joycon_imu_data data[3 ]; +} __packed;See comments later on about
imu_data, but you can't directly cast your data buffer to this data type due to endian issues. It may not really make sense to keep the data structure as you need to do manual data fetching.Quoted 6 lines
drivers/hid/hid-nintendo.c +1
-0+struct joycon_input_report {u8 id;u8 timer;@@ -290,11 +325,10 @@ struct joycon_input_report {u8 right_stick[3 ];drivers/hid/hid-nintendo.c struct joycon_input_report { · line 330
Quoted 10 lines
drivers/hid/hid-nintendo.c +3
-5u8 vibrator_report; -/* -* If support for firmware updates, gyroscope data, and/or NFC/IR -* are added in the future, this can be swapped for a union. -*/ -struct joycon_subcmd_reply reply; +union { +struct joycon_subcmd_reply subcmd_reply; +struct joycon_imu_report imu_report;maybe add a raw byte array to this union. Could help for parsing the imu data.
Quoted 50 lines
drivers/hid/hid-nintendo.c +21
-1+ }; } __packed;#define JC_MAX_RESP_SIZE (sizeof(struct joycon_input_report) + 35)@@ -334,6 +368,9 @@ struct joycon_ctlr {struct joycon_stick_cal right_stick_cal_x;struct joycon_stick_cal right_stick_cal_y; +struct joycon_imu_cal accel_cal; +struct joycon_imu_cal gyro_cal; +/* power supply data */ struct power_supply *battery;struct power_supply_desc battery_desc;@@ -352,6 +389,10 @@ struct joycon_ctlr {u16 rumble_lh_freq;u16 rumble_rl_freq;u16 rumble_rh_freq; + +/* imu */ +struct input_dev *imu_input; +int timestamp; };static int __joycon_hid_send (struct hid_device *hdev,u8 *data,size_t len)@@ -547,7 +588,7 @@ static int joycon_request_calibration(struct joycon_ctlr *ctlr) } report = (struct joycon_input_report *)ctlr->input_buf; - raw_cal = &report->reply.data[5 ]; + raw_cal = &report->subcmd_reply.data[5 ];/* left stick calibration parsing */ cal_x = &ctlr->left_stick_cal_x;@@ -601,6 +642,85 @@ static int joycon_request_calibration(struct joycon_ctlr *ctlr)return 0 ; } +static const s16 DFLT_ACCEL_OFFSET /*= 0*/ ; +static const s16 DFLT_ACCEL_SCALE =16384 ; +static const s16 DFLT_GYRO_OFFSET /*= 0*/ ; +static const s16 DFLT_GYRO_SCALE =13371 ; +static int joycon_request_imu_calibration (struct joycon_ctlr *ctlr) +{ +struct joycon_subcmd_request *req; +u8 buffer[sizeof (*req) +5 ] = {0 }; +struct joycon_input_report *report; +u8 *data; +u8 *raw_cal; +int ret;drivers/hid/hid-nintendo.c static int joycon_request_calibration(struct joycon_ctlr *ctlr) · line 666
Quoted 10 lines
drivers/hid/hid-nintendo.c +10
-0+int i; + +/* request IMU calibration data */ + req = (struct joycon_subcmd_request *)buffer; + req->subcmd_id =JC_SUBCMD_SPI_FLASH_READ ; + data = req->data; + data[0 ] =0xFF &JC_IMU_CAL_DATA_START ; + data[1 ] =0xFF & (JC_IMU_CAL_DATA_START >>8 ); + data[2 ] =0xFF & (JC_IMU_CAL_DATA_START >>16 ); + data[3 ] =0xFF & (JC_IMU_CAL_DATA_START >>24 );Maybe use
put_unaligned_le32? I think it allows you to avoid doing all these calculations yourself:put_unaligned_le32(JC_IMU_CAL_DATA_START, data);Quoted 24 lines
drivers/hid/hid-nintendo.c +24
-0+ data[4 ] =JC_IMU_CAL_DATA_SIZE ; + +hid_dbg (ctlr->hdev,"requesting IMU cal data\n" ); + ret =joycon_send_subcmd (ctlr, req,5 ,HZ ); + +if (ret) { +hid_warn (ctlr->hdev, +"Failed to read IMU cal, using defaults; ret=%d\n" , + ret); + +for (i =0 ; i <3 ; i++) { + ctlr->accel_cal.offset[i] =DFLT_ACCEL_OFFSET ; + ctlr->accel_cal.scale[i] =DFLT_ACCEL_SCALE ; + ctlr->gyro_cal.offset[i] =DFLT_GYRO_OFFSET ; + ctlr->gyro_cal.scale[i] =DFLT_GYRO_SCALE ; + } +return ret; + } + + report = (struct joycon_input_report *)ctlr->input_buf; + raw_cal = &report->subcmd_reply.data[5 ]; + +/* IMU calibration parsing */ +for (i =0 ; i <3 ; i++) {drivers/hid/hid-nintendo.c static int joycon_request_calibration(struct joycon_ctlr *ctlr) · line 700
Quoted 10 lines
drivers/hid/hid-nintendo.c +10
-0+int j = i *2 ; + + ctlr->accel_cal.offset[i] = raw_cal[j +0 ] | + ((s16 )raw_cal[j +1 ] <<8 ); + ctlr->accel_cal.scale[i] = raw_cal[j +6 ] | + ((s16 )raw_cal[j +7 ] <<8 ); + ctlr->gyro_cal.offset[i] = raw_cal[j +12 ] | + ((s16 )raw_cal[j +13 ] <<8 ); + ctlr->gyro_cal.scale[i] = raw_cal[j +18 ] | + ((s16 )raw_cal[j +19 ] <<8 );
get_unaligned_le16instead of doing your own bitshifts?Quoted 47 lines
drivers/hid/hid-nintendo.c +36
-0+ } + +hid_dbg (ctlr->hdev,"IMU calibration:\n" +"a_o[0]=%d a_o[1]=%d a_o[2]=%d\n" +"a_s[0]=%d a_s[1]=%d a_s[2]=%d\n" +"g_o[0]=%d g_o[1]=%d g_o[2]=%d\n" +"g_s[0]=%d g_s[1]=%d g_s[2]=%d\n" , + ctlr->accel_cal.offset[0 ], + ctlr->accel_cal.offset[1 ], + ctlr->accel_cal.offset[2 ], + ctlr->accel_cal.scale[0 ], + ctlr->accel_cal.scale[1 ], + ctlr->accel_cal.scale[2 ], + ctlr->gyro_cal.offset[0 ], + ctlr->gyro_cal.offset[1 ], + ctlr->gyro_cal.offset[2 ], + ctlr->gyro_cal.scale[0 ], + ctlr->gyro_cal.scale[1 ], + ctlr->gyro_cal.scale[2 ]); + +return 0 ; +} +static int joycon_set_report_mode (struct joycon_ctlr *ctlr) {struct joycon_subcmd_request *req;@@ -627,6 +747,19 @@ static int joycon_enable_rumble(struct joycon_ctlr *ctlr, bool enable)return joycon_send_subcmd (ctlr, req,1 ,HZ /4 ); } +static int joycon_enable_imu (struct joycon_ctlr *ctlr,bool enable) +{ +struct joycon_subcmd_request *req; +u8 buffer[sizeof (*req) +1 ] = {0 }; + + req = (struct joycon_subcmd_request *)buffer; + req->subcmd_id =JC_SUBCMD_ENABLE_IMU ; + req->data[0 ] = enable ?0x01 :0x00 ; + +hid_dbg (ctlr->hdev,"%s IMU\n" , enable ?"enabling" :"disabling" ); +return joycon_send_subcmd (ctlr, req,1 ,HZ ); +} +static s32 joycon_map_stick_val (struct joycon_stick_cal *cal,s32 val) {s32 center = cal->center;drivers/hid/hid-nintendo.c static void joycon_parse_report(struct joycon_ctlr *ctlr, · line 910
Quoted 7 lines
drivers/hid/hid-nintendo.c +4
-0@@ -771,6 +904,54 @@ static void joycon_parse_report(struct joycon_ctlr *ctlr,spin_unlock_irqrestore (&ctlr->lock, flags);wake_up (&ctlr->wait); } + +/* parse IMU data if present */ +if (rep->id ==JC_INPUT_IMU_DATA ) { +struct joycon_imu_data *imu_data = rep->imu_report.data;Quoted 7 lines
I don't think you can directly covert your input report data to
imu_data. Until now you have been lucky enough (based on a quick glance of the the other patches) that your data are single bytes. However, this data seems to be a bunch of s16's. In other words you have to deal with endianess issues. You need to useget_unaligned_le16to retrieve data from your raw byte buffer. See other HID drivers for reference.Ah, good point. I'd overlooked that entirely.
Quoted 16 lines
Since you will have to use
get_unaligned_le16, it probably won't make much sense to really have ajoycon_imu_datastructure. Maybe extend yourinput_bufferunion with raw bytes and in this case just use raw bytes. Each of your loop iterations below just grab the values from the buffer and store them in a variable.drivers/hid/hid-nintendo.c static void joycon_parse_report(struct joycon_ctlr *ctlr, · line 918
Quoted 8 lines
drivers/hid/hid-nintendo.c +8
-0+struct input_dev *idev = ctlr->imu_input; +int i; +int value[6 ]; + +for (i =0 ; i <3 ; i++) {/* there are 3 reports */ + ctlr->timestamp +=5000 ;/* each is 5 ms apart */ +input_event (idev,EV_MSC ,MSC_TIMESTAMP , + ctlr->timestamp);Quoted 4 lines
How sure are you that this is always 5ms? Is there a hardware timestamp somewhere? If I look at our DS4 the timing isn't guaranteed (Bluetooth is lossy) and not all packets even make it.
It appears that the closest thing to a hardware timestamp available is a quickly incrementing 1-byte timer sent with every report. It's only really useful for latency estimation. Each input report includes 3 IMU samples which are 5ms apart for the joy-cons. It's recently come to my attention that the samples may be spaced differently for the pro controller, so I'm going to need to dive into this more anyway. I'm not sure what a great way would be to handle lost reports.
Quoted 27 lines
Quoted 16 lines
drivers/hid/hid-nintendo.c +16
-0+ +/* +* Rather than convert to floats, we adjust by +* calibration factors and then multiply by the default +* scaling values. This way, the consumer only needs to +* divide by the default scale values. +*/ + value[0 ] = (imu_data[i].gyro_x - + ctlr->gyro_cal.offset[0 ]) * +DFLT_GYRO_SCALE / ctlr->gyro_cal.scale[0 ]; + value[1 ] = (imu_data[i].gyro_y - + ctlr->gyro_cal.offset[1 ]) * +DFLT_GYRO_SCALE / ctlr->gyro_cal.scale[1 ]; + value[2 ] = (imu_data[i].gyro_z - + ctlr->gyro_cal.offset[2 ]) * +DFLT_GYRO_SCALE / ctlr->gyro_cal.scale[2 ];drivers/hid/hid-nintendo.c static void joycon_parse_report(struct joycon_ctlr *ctlr, · line 944
Quoted 10 lines
drivers/hid/hid-nintendo.c +10
-0+ + value[3 ] = (imu_data[i].accel_x - + ctlr->accel_cal.offset[0 ]) * +DFLT_ACCEL_SCALE / ctlr->accel_cal.scale[0 ]; + value[4 ] = (imu_data[i].accel_y - + ctlr->accel_cal.offset[1 ]) * +DFLT_ACCEL_SCALE / ctlr->accel_cal.scale[1 ]; + value[5 ] = (imu_data[i].accel_z - + ctlr->accel_cal.offset[2 ]) * +DFLT_ACCEL_SCALE / ctlr->accel_cal.scale[2 ];Quoted 9 lines
Just in case I would double check the precision of your calculations. For DS4 we had similar calculations, but we had a small loss of precision, which ultimately caused major calculation errors. (Applications often use accelerometer + gyro data to calculate an absolute position. This involves integration.. and small errors become big). We had to use the "
mult_frac" macro to restore some of the precision during the calculations. It might be something to double check.That's good to know. I'll look more closely at how it's implemented in hid-sony.
Quoted 6 lines
In addition what orientation are you using for the axes? I need to double check the DS4 datasheets, but I think we were using a "right handed" coordinate system. (So if you make a fist of your right hand with thumb up, the gyro curls around it counter clockwise along the direction of your fingers).
The orientation of the axes (and much more) are documented here:
https://github.com/dekuNukem/Nintendo_Switch_Reverse_Engineering/blob/master/imu_sensor_notes.md
Since the the right vs. left joy-cons have different axes orientations, I assume it's preferable to standardize it all in software to match the orientation of the DS4.Quoted 146 lines
Quoted 146 lines
drivers/hid/hid-nintendo.c +82
-3+ +input_report_abs (idev,ABS_RX , value[0 ]); +input_report_abs (idev,ABS_RY , value[1 ]); +input_report_abs (idev,ABS_RZ , value[2 ]); +input_report_abs (idev,ABS_X , value[3 ]); +input_report_abs (idev,ABS_Y , value[4 ]); +input_report_abs (idev,ABS_Z , value[5 ]); +input_sync (idev); + } + } }static void joycon_rumble_worker (struct work_struct *work)@@ -950,6 +1131,7 @@ static int joycon_input_create(struct joycon_ctlr *ctlr) {struct hid_device *hdev;const char *name; +const char *imu_name;int ret;int i;@@ -958,12 +1140,15 @@ static int joycon_input_create(struct joycon_ctlr *ctlr)switch (hdev->product) {case USB_DEVICE_ID_NINTENDO_PROCON : name ="Nintendo Switch Pro Controller" ; + imu_name ="Nintendo Switch Pro Controller IMU" ;break ;case USB_DEVICE_ID_NINTENDO_JOYCONL : name ="Nintendo Switch Left Joy-Con" ; + imu_name ="Nintendo Switch Left Joy-Con IMU" ;break ;case USB_DEVICE_ID_NINTENDO_JOYCONR : name ="Nintendo Switch Right Joy-Con" ; + imu_name ="Nintendo Switch Right Joy-Con IMU" ;break ;default :/* Should be impossible */ hid_err (hdev,"Invalid hid product\n" );@@ -1029,6 +1214,55 @@ static int joycon_input_create(struct joycon_ctlr *ctlr)if (ret)return ret; +/* configure the imu input device */ + ctlr->imu_input =devm_input_allocate_device (&hdev->dev); +if (!ctlr->imu_input) +return -ENOMEM ; + + ctlr->imu_input->id.bustype = hdev->bus; + ctlr->imu_input->id.vendor = hdev->vendor; + ctlr->imu_input->id.product = hdev->product; + ctlr->imu_input->id.version = hdev->version; + ctlr->imu_input->uniq = ctlr->mac_addr_str; + ctlr->imu_input->name = imu_name; +input_set_drvdata (ctlr->imu_input, ctlr); + +/* configure imu axes */ +input_set_abs_params (ctlr->imu_input,ABS_X , + -JC_MAX_ACCEL_MAG ,JC_MAX_ACCEL_MAG , +JC_ACCEL_FUZZ ,JC_ACCEL_FLAT ); +input_set_abs_params (ctlr->imu_input,ABS_Y , + -JC_MAX_ACCEL_MAG ,JC_MAX_ACCEL_MAG , +JC_ACCEL_FUZZ ,JC_ACCEL_FLAT ); +input_set_abs_params (ctlr->imu_input,ABS_Z , + -JC_MAX_ACCEL_MAG ,JC_MAX_ACCEL_MAG , +JC_ACCEL_FUZZ ,JC_ACCEL_FLAT ); +input_abs_set_res (ctlr->imu_input,ABS_X ,JC_ACCEL_RES ); +input_abs_set_res (ctlr->imu_input,ABS_Y ,JC_ACCEL_RES ); +input_abs_set_res (ctlr->imu_input,ABS_Z ,JC_ACCEL_RES ); + +input_set_abs_params (ctlr->imu_input,ABS_RX , + -JC_MAX_GYRO_MAG ,JC_MAX_GYRO_MAG , +JC_GYRO_FUZZ ,JC_GYRO_FLAT ); +input_set_abs_params (ctlr->imu_input,ABS_RY , + -JC_MAX_GYRO_MAG ,JC_MAX_GYRO_MAG , +JC_GYRO_FUZZ ,JC_GYRO_FLAT ); +input_set_abs_params (ctlr->imu_input,ABS_RZ , + -JC_MAX_GYRO_MAG ,JC_MAX_GYRO_MAG , +JC_GYRO_FUZZ ,JC_GYRO_FLAT ); + +input_abs_set_res (ctlr->imu_input,ABS_RX ,JC_GYRO_RES ); +input_abs_set_res (ctlr->imu_input,ABS_RY ,JC_GYRO_RES ); +input_abs_set_res (ctlr->imu_input,ABS_RZ ,JC_GYRO_RES ); + +__set_bit (EV_MSC , ctlr->imu_input->evbit); +__set_bit (MSC_TIMESTAMP , ctlr->imu_input->mscbit); +__set_bit (INPUT_PROP_ACCELEROMETER , ctlr->imu_input->propbit); + + ret =input_register_device (ctlr->imu_input); +if (ret) +return ret; +return 0 ; }@@ -1288,7 +1522,7 @@ static int joycon_read_mac(struct joycon_ctlr *ctlr) report = (struct joycon_input_report *)ctlr->input_buf;for (i =4 , j =0 ; j <6 ; i++, j++) - ctlr->mac_addr[j] = report->reply.data[i]; + ctlr->mac_addr[j] = report->subcmd_reply.data[i]; ctlr->mac_addr_str = devm_kasprintf(&ctlr->hdev->dev, GFP_KERNEL, "%02X:%02X:%02X:%02X:%02X:%02X",@@ -1343,7 +1577,7 @@ static int joycon_ctlr_handle_event(struct joycon_ctlr *ctlr, u8 *data, data[0] != JC_INPUT_SUBCMD_REPLY)break ; report = (struct joycon_input_report *)data; -if (report->reply.id == ctlr->subcmd_ack_match) +if (report->subcmd_reply.id == ctlr->subcmd_ack_match) match = true;break ;default :@@ -1469,6 +1703,16 @@ static int nintendo_hid_probe(struct hid_device *hdev,hid_warn (hdev,"Analog stick positions may be inaccurate\n" ); } +/* get IMU calibration data, and parse it */ + ret =joycon_request_imu_calibration (ctlr); +if (ret) { +/* +* We can function with default calibration, but it may be +* inaccurate. Provide a warning, and continue on. +*/ +hid_warn (hdev,"Unable to read IMU calibration data\n" ); + } +/* Set the reporting mode to 0x30, which is the full report mode */ ret =joycon_set_report_mode (ctlr);if (ret) {@@ -1483,6 +1727,13 @@ static int nintendo_hid_probe(struct hid_device *hdev, goto err_mutex; } +/* Enable the IMU */ + ret =joycon_enable_imu (ctlr, true); +if (ret) { +hid_err (hdev,"Failed to enable the IMU; ret=%d\n" , ret); + goto err_mutex; + } + ret =joycon_read_mac (ctlr);if (ret) { hid_err(hdev, "Failed to retrieve controller MAC; ret=%d\n", --2.24.1
Signature · 1 line
Daniel Ogorchock
Re: [PATCH v10 11/12] HID: nintendo: add IMU support
Hi Daniel,
Whether to report as HAT or BTN_DPAD depends on how the hardware works internally. You see the HAT vs buton at the HID report layer (it is quite different), but if you just touch the controller sticks you will also notice. A HAT axis is really like a joystick e.g. with a range -1 to +1. If you hold down e.g. "left" and "right" on a d-pad, you would get "0" back as you are unable to press both left and right at the same time. With d-pad buttons you could press left and right at the same time (depending on design).
When I just tried my Switch Pro controller it really felt like the d-pad were buttons. Various other controllers using BTN_DPAD as well for example DualShock 3. Most applications these days should use SDL2, which will hide that.
Thanks,
Roderick
Quoted 619 lines · Daniel Ogorchock
Hi Carl,
When I was initially implementing the button mappings, I referred to the standard described here:
https://www.kernel.org/doc/html/latest/input/gamepad.htmlIt mentions under the d-pad section that digital inputs should be reported using the BTN_DPAD_* variant. Do the other controllers mentioned report their d-pads as analog values, or has using the ABS_HAT* values become the de facto format for digital inputs as well?
If the latter, I guess it would make sense to go with the crowd for the pro. It just seems a bit odd to report digital inputs as absolute axes, but I'm open to changing it if that's the consensus.
Thanks,
DanielQuoted 580 lines · Carl Mueller
(3rd/final attempt at reply due to list server rejecting message with HTML subpart; sorry about the spam!)
Hi Everyone,
In trying out this driver to use with the Switch Pro Controller, the biggest difficulty I had was that the D-pad is reported as buttons instead of as a Hat. All other controllers that are similar in structure (such as the PS3 and PS4 controllers, the Xbox controllers, and many others) report the D-pad as a Hat. This kind of consistency is needed to avoid lots of software compatibility issues.
I mentioned this to Daniel, and he indicated that he wanted the Switch Pro Controller to behave like the JoyCons, which have buttons instead of a D-pad. Having the JoyCons report the buttons as buttons makes sense, since it's possible to push opposite directions at the same time. However, I don't think that the argument carries over to the Switch Pro Controller, since it has a physically different control.
Again, the consistency with the other controllers that have all the same physical structure and the same types of controls seems more important to me than consistency with a controller that is physically much different. Additionally, many games are already written for use with controllers that look like the Switch Pro Controller, whereas fewer are written for use with Nintendo JoyCons. If we have to cause trouble for one side or the other, let's not cause trouble with the more established side.
Quoted 550 lines · Roderick Colenbrander
Hi Daniel,
Unfortunately there is no public test application at the moment to illustrate these features. I agree something is definitely needed.
I need to see if we can come up with something. One of the test apps we have internally is a 3D cube, which is controllable by controller motion. It of course shows the gyro / accelerometer values, but the position of the cube is tied to the calculated orientation. If something is off you will see weird movements in the cube or drift building up over time etcetera.
Though it would take some time to prepare something. The rest of the patch series looked fine I think, so the IMU parts may need to wait for a next kernel cycle, but all the other plumbing could go in (if others are okay of course).
Thanks,
RoderickQuoted 528 lines · Daniel Ogorchock
Hi Roderick,
Thanks for the feedback. In addition to the inline comments below, do you have any recommendations for test programs that you know work well with hid-sony's gyro implementation? Up to this point I've just been using evtest, which obviously isn't too useful for testing actual functionality of the gyro in an intuitive way.
Quoted 292 lines · Roderick Colenbrander
Hi Daniel,
I had some time to review the motion sensors patch. I have added some feedback inline with your patch below.
Aside from standard feedback on the code, I wanted to have a close look at the accelerometer / gyro data. My team has been doing a lot of work on this (and still is) and I want to make sure any work we do on "user space" software (e.g. Android) automatically works for Joycon as well. The accuracy of the data is very important else applications will make bad decisions. Userspace applications often combine the data of both sensors to calculate a position for position tracking. Inaccurate axes ranges or wrong precision can cause major issues. I may be a bit strict below, but it will just be to prevent headaches for others later on. I use the DS4 as a reference point as it was the first device (aside from Wii early on) where we properly report acceleration and gyro axes with
INPUT_PROP_ACCELEROMETERand we should be consistent with it here else applications could be confused, so we should use similar orientation of axes and resolutions.Thanks,
RoderickQuoted 25 lines · Daniel J. Ogorchock
This patch adds support for the controller's IMU. The accelerometer and gyro data are both provided to userspace using a second input device. The devices can be associated using their uniq value (set to the controller's MAC address).
The majority of this patch's functinoality was provided by Carl Mueller.
Signed-off-by Daniel J. Ogorchock <[email protected]>--- drivers/hid/hid-nintendo.c | 267 +++++++++++++++++++++++++++++++++++-- 1 file changed, 259 insertions(+), 8 deletions(-)drivers/hid/hid-nintendo.c +4
-0@@ -101,12 +101,29 @@ static const u16 JC_CAL_DATA_START = 0x603d;static const u16 JC_CAL_DATA_END =0x604e ;#define JC_CAL_DATA_SIZE (JC_CAL_DATA_END - JC_CAL_DATA_START + 1) +/* SPI storage addresses of IMU factory calibration data */ +static const u16 JC_IMU_CAL_DATA_START =0x6020 ; +static const u16 JC_IMU_CAL_DATA_END =0x6037 ; +#define JC_IMU_CAL_DATA_SIZE \drivers/hid/hid-nintendo.c static const u16 JC_CAL_DATA_START = 0x603d; · line 117
Quoted 10 lines
drivers/hid/hid-nintendo.c +4
-0+ (JC_IMU_CAL_DATA_END -JC_IMU_CAL_DATA_START +1 ) /* The raw analog joystick values will be mapped in terms of this magnitude */static const u16 JC_MAX_STICK_MAG =32767 ;static const u16 JC_STICK_FUZZ =250 ;static const u16 JC_STICK_FLAT =500 ; +/* The accel axes will be mapped in terms of this magnitude */ +static const u16 JC_MAX_ACCEL_MAG =32767 ; +static const u16 JC_ACCEL_RES =4096 ;What does the acceleration resolution equate to? For DS4 we use multiples of 'g'. (We don't know the position on earth, so can't report in m/s^2).
drivers/hid/hid-nintendo.c static const u16 JC_CAL_DATA_START = 0x603d; · line 123
Quoted 6 lines
drivers/hid/hid-nintendo.c +6
-0+static const u16 JC_ACCEL_FUZZ =10 ; +static const u16 JC_ACCEL_FLAT /*= 0*/ ; + +/* The gyro axes will be mapped in terms of this magnitude */ +static const u16 JC_MAX_GYRO_MAG =32767 ; +static const u16 JC_GYRO_RES =13371 /936 ;/* 14 (14.285) */ What does the gyro resolution equate to? For DS4 we report "degrees per second". We should do the same for the joycons, but I don't know how you guys figured out the exact resolution. As I mentioned if it is not accurate, applications will make wrong calculations.
Quoted 24 lines
drivers/hid/hid-nintendo.c +10
-0+static const u16 JC_GYRO_FUZZ =10 ; +static const u16 JC_GYRO_FLAT /*= 0*/ ; +/* frequency/amplitude tables for rumble */ struct joycon_rumble_freq_data {u16 high;@@ -234,6 +251,11 @@ struct joycon_stick_cal {s32 center; }; +struct joycon_imu_cal { +s16 offset[3 ]; +s16 scale[3 ]; +}; +/* * All the controller's button values are stored in a u32. * They can be accessed with bitwise ANDs. @@ -281,6 +303,19 @@ struct joycon_subcmd_reply {u8 data[0 ];/* will be at most 35 bytes */ } __packed; +struct joycon_imu_data { +s16 accel_x;drivers/hid/hid-nintendo.c struct joycon_subcmd_reply { · line 317
Quoted 10 lines
drivers/hid/hid-nintendo.c +10
-0+s16 accel_y; +s16 accel_z; +s16 gyro_x; +s16 gyro_y; +s16 gyro_z; +} __packed; + +struct joycon_imu_report { +struct joycon_imu_data data[3 ]; +} __packed;See comments later on about
imu_data, but you can't directly cast your data buffer to this data type due to endian issues. It may not really make sense to keep the data structure as you need to do manual data fetching.Quoted 6 lines
drivers/hid/hid-nintendo.c +1
-0+struct joycon_input_report {u8 id;u8 timer;@@ -290,11 +325,10 @@ struct joycon_input_report {u8 right_stick[3 ];drivers/hid/hid-nintendo.c struct joycon_input_report { · line 330
Quoted 10 lines
drivers/hid/hid-nintendo.c +3
-5u8 vibrator_report; -/* -* If support for firmware updates, gyroscope data, and/or NFC/IR -* are added in the future, this can be swapped for a union. -*/ -struct joycon_subcmd_reply reply; +union { +struct joycon_subcmd_reply subcmd_reply; +struct joycon_imu_report imu_report;maybe add a raw byte array to this union. Could help for parsing the imu data.
Quoted 50 lines
drivers/hid/hid-nintendo.c +21
-1+ }; } __packed;#define JC_MAX_RESP_SIZE (sizeof(struct joycon_input_report) + 35)@@ -334,6 +368,9 @@ struct joycon_ctlr {struct joycon_stick_cal right_stick_cal_x;struct joycon_stick_cal right_stick_cal_y; +struct joycon_imu_cal accel_cal; +struct joycon_imu_cal gyro_cal; +/* power supply data */ struct power_supply *battery;struct power_supply_desc battery_desc;@@ -352,6 +389,10 @@ struct joycon_ctlr {u16 rumble_lh_freq;u16 rumble_rl_freq;u16 rumble_rh_freq; + +/* imu */ +struct input_dev *imu_input; +int timestamp; };static int __joycon_hid_send (struct hid_device *hdev,u8 *data,size_t len)@@ -547,7 +588,7 @@ static int joycon_request_calibration(struct joycon_ctlr *ctlr) } report = (struct joycon_input_report *)ctlr->input_buf; - raw_cal = &report->reply.data[5 ]; + raw_cal = &report->subcmd_reply.data[5 ];/* left stick calibration parsing */ cal_x = &ctlr->left_stick_cal_x;@@ -601,6 +642,85 @@ static int joycon_request_calibration(struct joycon_ctlr *ctlr)return 0 ; } +static const s16 DFLT_ACCEL_OFFSET /*= 0*/ ; +static const s16 DFLT_ACCEL_SCALE =16384 ; +static const s16 DFLT_GYRO_OFFSET /*= 0*/ ; +static const s16 DFLT_GYRO_SCALE =13371 ; +static int joycon_request_imu_calibration (struct joycon_ctlr *ctlr) +{ +struct joycon_subcmd_request *req; +u8 buffer[sizeof (*req) +5 ] = {0 }; +struct joycon_input_report *report; +u8 *data; +u8 *raw_cal; +int ret;drivers/hid/hid-nintendo.c static int joycon_request_calibration(struct joycon_ctlr *ctlr) · line 666
Quoted 10 lines
drivers/hid/hid-nintendo.c +10
-0+int i; + +/* request IMU calibration data */ + req = (struct joycon_subcmd_request *)buffer; + req->subcmd_id =JC_SUBCMD_SPI_FLASH_READ ; + data = req->data; + data[0 ] =0xFF &JC_IMU_CAL_DATA_START ; + data[1 ] =0xFF & (JC_IMU_CAL_DATA_START >>8 ); + data[2 ] =0xFF & (JC_IMU_CAL_DATA_START >>16 ); + data[3 ] =0xFF & (JC_IMU_CAL_DATA_START >>24 );Maybe use
put_unaligned_le32? I think it allows you to avoid doing all these calculations yourself:put_unaligned_le32(JC_IMU_CAL_DATA_START, data);Quoted 24 lines
drivers/hid/hid-nintendo.c +24
-0+ data[4 ] =JC_IMU_CAL_DATA_SIZE ; + +hid_dbg (ctlr->hdev,"requesting IMU cal data\n" ); + ret =joycon_send_subcmd (ctlr, req,5 ,HZ ); + +if (ret) { +hid_warn (ctlr->hdev, +"Failed to read IMU cal, using defaults; ret=%d\n" , + ret); + +for (i =0 ; i <3 ; i++) { + ctlr->accel_cal.offset[i] =DFLT_ACCEL_OFFSET ; + ctlr->accel_cal.scale[i] =DFLT_ACCEL_SCALE ; + ctlr->gyro_cal.offset[i] =DFLT_GYRO_OFFSET ; + ctlr->gyro_cal.scale[i] =DFLT_GYRO_SCALE ; + } +return ret; + } + + report = (struct joycon_input_report *)ctlr->input_buf; + raw_cal = &report->subcmd_reply.data[5 ]; + +/* IMU calibration parsing */ +for (i =0 ; i <3 ; i++) {drivers/hid/hid-nintendo.c static int joycon_request_calibration(struct joycon_ctlr *ctlr) · line 700
Quoted 10 lines
drivers/hid/hid-nintendo.c +10
-0+int j = i *2 ; + + ctlr->accel_cal.offset[i] = raw_cal[j +0 ] | + ((s16 )raw_cal[j +1 ] <<8 ); + ctlr->accel_cal.scale[i] = raw_cal[j +6 ] | + ((s16 )raw_cal[j +7 ] <<8 ); + ctlr->gyro_cal.offset[i] = raw_cal[j +12 ] | + ((s16 )raw_cal[j +13 ] <<8 ); + ctlr->gyro_cal.scale[i] = raw_cal[j +18 ] | + ((s16 )raw_cal[j +19 ] <<8 );
get_unaligned_le16instead of doing your own bitshifts?Quoted 47 lines
drivers/hid/hid-nintendo.c +36
-0+ } + +hid_dbg (ctlr->hdev,"IMU calibration:\n" +"a_o[0]=%d a_o[1]=%d a_o[2]=%d\n" +"a_s[0]=%d a_s[1]=%d a_s[2]=%d\n" +"g_o[0]=%d g_o[1]=%d g_o[2]=%d\n" +"g_s[0]=%d g_s[1]=%d g_s[2]=%d\n" , + ctlr->accel_cal.offset[0 ], + ctlr->accel_cal.offset[1 ], + ctlr->accel_cal.offset[2 ], + ctlr->accel_cal.scale[0 ], + ctlr->accel_cal.scale[1 ], + ctlr->accel_cal.scale[2 ], + ctlr->gyro_cal.offset[0 ], + ctlr->gyro_cal.offset[1 ], + ctlr->gyro_cal.offset[2 ], + ctlr->gyro_cal.scale[0 ], + ctlr->gyro_cal.scale[1 ], + ctlr->gyro_cal.scale[2 ]); + +return 0 ; +} +static int joycon_set_report_mode (struct joycon_ctlr *ctlr) {struct joycon_subcmd_request *req;@@ -627,6 +747,19 @@ static int joycon_enable_rumble(struct joycon_ctlr *ctlr, bool enable)return joycon_send_subcmd (ctlr, req,1 ,HZ /4 ); } +static int joycon_enable_imu (struct joycon_ctlr *ctlr,bool enable) +{ +struct joycon_subcmd_request *req; +u8 buffer[sizeof (*req) +1 ] = {0 }; + + req = (struct joycon_subcmd_request *)buffer; + req->subcmd_id =JC_SUBCMD_ENABLE_IMU ; + req->data[0 ] = enable ?0x01 :0x00 ; + +hid_dbg (ctlr->hdev,"%s IMU\n" , enable ?"enabling" :"disabling" ); +return joycon_send_subcmd (ctlr, req,1 ,HZ ); +} +static s32 joycon_map_stick_val (struct joycon_stick_cal *cal,s32 val) {s32 center = cal->center;drivers/hid/hid-nintendo.c static void joycon_parse_report(struct joycon_ctlr *ctlr, · line 910
Quoted 7 lines
drivers/hid/hid-nintendo.c +4
-0@@ -771,6 +904,54 @@ static void joycon_parse_report(struct joycon_ctlr *ctlr,spin_unlock_irqrestore (&ctlr->lock, flags);wake_up (&ctlr->wait); } + +/* parse IMU data if present */ +if (rep->id ==JC_INPUT_IMU_DATA ) { +struct joycon_imu_data *imu_data = rep->imu_report.data;Quoted 7 lines
I don't think you can directly covert your input report data to
imu_data. Until now you have been lucky enough (based on a quick glance of the the other patches) that your data are single bytes. However, this data seems to be a bunch of s16's. In other words you have to deal with endianess issues. You need to useget_unaligned_le16to retrieve data from your raw byte buffer. See other HID drivers for reference.Ah, good point. I'd overlooked that entirely.
Quoted 16 lines
Since you will have to use
get_unaligned_le16, it probably won't make much sense to really have ajoycon_imu_datastructure. Maybe extend yourinput_bufferunion with raw bytes and in this case just use raw bytes. Each of your loop iterations below just grab the values from the buffer and store them in a variable.drivers/hid/hid-nintendo.c static void joycon_parse_report(struct joycon_ctlr *ctlr, · line 918
Quoted 8 lines
drivers/hid/hid-nintendo.c +8
-0+struct input_dev *idev = ctlr->imu_input; +int i; +int value[6 ]; + +for (i =0 ; i <3 ; i++) {/* there are 3 reports */ + ctlr->timestamp +=5000 ;/* each is 5 ms apart */ +input_event (idev,EV_MSC ,MSC_TIMESTAMP , + ctlr->timestamp);Quoted 4 lines
How sure are you that this is always 5ms? Is there a hardware timestamp somewhere? If I look at our DS4 the timing isn't guaranteed (Bluetooth is lossy) and not all packets even make it.
It appears that the closest thing to a hardware timestamp available is a quickly incrementing 1-byte timer sent with every report. It's only really useful for latency estimation. Each input report includes 3 IMU samples which are 5ms apart for the joy-cons. It's recently come to my attention that the samples may be spaced differently for the pro controller, so I'm going to need to dive into this more anyway. I'm not sure what a great way would be to handle lost reports.
Quoted 27 lines
Quoted 16 lines
drivers/hid/hid-nintendo.c +16
-0+ +/* +* Rather than convert to floats, we adjust by +* calibration factors and then multiply by the default +* scaling values. This way, the consumer only needs to +* divide by the default scale values. +*/ + value[0 ] = (imu_data[i].gyro_x - + ctlr->gyro_cal.offset[0 ]) * +DFLT_GYRO_SCALE / ctlr->gyro_cal.scale[0 ]; + value[1 ] = (imu_data[i].gyro_y - + ctlr->gyro_cal.offset[1 ]) * +DFLT_GYRO_SCALE / ctlr->gyro_cal.scale[1 ]; + value[2 ] = (imu_data[i].gyro_z - + ctlr->gyro_cal.offset[2 ]) * +DFLT_GYRO_SCALE / ctlr->gyro_cal.scale[2 ];drivers/hid/hid-nintendo.c static void joycon_parse_report(struct joycon_ctlr *ctlr, · line 944
Quoted 10 lines
drivers/hid/hid-nintendo.c +10
-0+ + value[3 ] = (imu_data[i].accel_x - + ctlr->accel_cal.offset[0 ]) * +DFLT_ACCEL_SCALE / ctlr->accel_cal.scale[0 ]; + value[4 ] = (imu_data[i].accel_y - + ctlr->accel_cal.offset[1 ]) * +DFLT_ACCEL_SCALE / ctlr->accel_cal.scale[1 ]; + value[5 ] = (imu_data[i].accel_z - + ctlr->accel_cal.offset[2 ]) * +DFLT_ACCEL_SCALE / ctlr->accel_cal.scale[2 ];Quoted 9 lines
Just in case I would double check the precision of your calculations. For DS4 we had similar calculations, but we had a small loss of precision, which ultimately caused major calculation errors. (Applications often use accelerometer + gyro data to calculate an absolute position. This involves integration.. and small errors become big). We had to use the "
mult_frac" macro to restore some of the precision during the calculations. It might be something to double check.That's good to know. I'll look more closely at how it's implemented in hid-sony.
Quoted 6 lines
In addition what orientation are you using for the axes? I need to double check the DS4 datasheets, but I think we were using a "right handed" coordinate system. (So if you make a fist of your right hand with thumb up, the gyro curls around it counter clockwise along the direction of your fingers).
The orientation of the axes (and much more) are documented here:
https://github.com/dekuNukem/Nintendo_Switch_Reverse_Engineering/blob/master/imu_sensor_notes.md
Since the the right vs. left joy-cons have different axes orientations, I assume it's preferable to standardize it all in software to match the orientation of the DS4.Quoted 133 lines
Quoted 133 lines
drivers/hid/hid-nintendo.c +75
-2+ +input_report_abs (idev,ABS_RX , value[0 ]); +input_report_abs (idev,ABS_RY , value[1 ]); +input_report_abs (idev,ABS_RZ , value[2 ]); +input_report_abs (idev,ABS_X , value[3 ]); +input_report_abs (idev,ABS_Y , value[4 ]); +input_report_abs (idev,ABS_Z , value[5 ]); +input_sync (idev); + } + } }static void joycon_rumble_worker (struct work_struct *work)@@ -950,6 +1131,7 @@ static int joycon_input_create(struct joycon_ctlr *ctlr) {struct hid_device *hdev;const char *name; +const char *imu_name;int ret;int i;@@ -958,12 +1140,15 @@ static int joycon_input_create(struct joycon_ctlr *ctlr)switch (hdev->product) {case USB_DEVICE_ID_NINTENDO_PROCON : name ="Nintendo Switch Pro Controller" ; + imu_name ="Nintendo Switch Pro Controller IMU" ;break ;case USB_DEVICE_ID_NINTENDO_JOYCONL : name ="Nintendo Switch Left Joy-Con" ; + imu_name ="Nintendo Switch Left Joy-Con IMU" ;break ;case USB_DEVICE_ID_NINTENDO_JOYCONR : name ="Nintendo Switch Right Joy-Con" ; + imu_name ="Nintendo Switch Right Joy-Con IMU" ;break ;default :/* Should be impossible */ hid_err (hdev,"Invalid hid product\n" );@@ -1029,6 +1214,55 @@ static int joycon_input_create(struct joycon_ctlr *ctlr)if (ret)return ret; +/* configure the imu input device */ + ctlr->imu_input =devm_input_allocate_device (&hdev->dev); +if (!ctlr->imu_input) +return -ENOMEM ; + + ctlr->imu_input->id.bustype = hdev->bus; + ctlr->imu_input->id.vendor = hdev->vendor; + ctlr->imu_input->id.product = hdev->product; + ctlr->imu_input->id.version = hdev->version; + ctlr->imu_input->uniq = ctlr->mac_addr_str; + ctlr->imu_input->name = imu_name; +input_set_drvdata (ctlr->imu_input, ctlr); + +/* configure imu axes */ +input_set_abs_params (ctlr->imu_input,ABS_X , + -JC_MAX_ACCEL_MAG ,JC_MAX_ACCEL_MAG , +JC_ACCEL_FUZZ ,JC_ACCEL_FLAT ); +input_set_abs_params (ctlr->imu_input,ABS_Y , + -JC_MAX_ACCEL_MAG ,JC_MAX_ACCEL_MAG , +JC_ACCEL_FUZZ ,JC_ACCEL_FLAT ); +input_set_abs_params (ctlr->imu_input,ABS_Z , + -JC_MAX_ACCEL_MAG ,JC_MAX_ACCEL_MAG , +JC_ACCEL_FUZZ ,JC_ACCEL_FLAT ); +input_abs_set_res (ctlr->imu_input,ABS_X ,JC_ACCEL_RES ); +input_abs_set_res (ctlr->imu_input,ABS_Y ,JC_ACCEL_RES ); +input_abs_set_res (ctlr->imu_input,ABS_Z ,JC_ACCEL_RES ); + +input_set_abs_params (ctlr->imu_input,ABS_RX , + -JC_MAX_GYRO_MAG ,JC_MAX_GYRO_MAG , +JC_GYRO_FUZZ ,JC_GYRO_FLAT ); +input_set_abs_params (ctlr->imu_input,ABS_RY , + -JC_MAX_GYRO_MAG ,JC_MAX_GYRO_MAG , +JC_GYRO_FUZZ ,JC_GYRO_FLAT ); +input_set_abs_params (ctlr->imu_input,ABS_RZ , + -JC_MAX_GYRO_MAG ,JC_MAX_GYRO_MAG , +JC_GYRO_FUZZ ,JC_GYRO_FLAT ); + +input_abs_set_res (ctlr->imu_input,ABS_RX ,JC_GYRO_RES ); +input_abs_set_res (ctlr->imu_input,ABS_RY ,JC_GYRO_RES ); +input_abs_set_res (ctlr->imu_input,ABS_RZ ,JC_GYRO_RES ); + +__set_bit (EV_MSC , ctlr->imu_input->evbit); +__set_bit (MSC_TIMESTAMP , ctlr->imu_input->mscbit); +__set_bit (INPUT_PROP_ACCELEROMETER , ctlr->imu_input->propbit); + + ret =input_register_device (ctlr->imu_input); +if (ret) +return ret; +return 0 ; }@@ -1288,7 +1522,7 @@ static int joycon_read_mac(struct joycon_ctlr *ctlr) report = (struct joycon_input_report *)ctlr->input_buf;for (i =4 , j =0 ; j <6 ; i++, j++) - ctlr->mac_addr[j] = report->reply.data[i]; + ctlr->mac_addr[j] = report->subcmd_reply.data[i]; ctlr->mac_addr_str = devm_kasprintf(&ctlr->hdev->dev, GFP_KERNEL, "%02X:%02X:%02X:%02X:%02X:%02X",@@ -1343,7 +1577,7 @@ static int joycon_ctlr_handle_event(struct joycon_ctlr *ctlr, u8 *data, data[0] != JC_INPUT_SUBCMD_REPLY)break ; report = (struct joycon_input_report *)data; -if (report->reply.id == ctlr->subcmd_ack_match) +if (report->subcmd_reply.id == ctlr->subcmd_ack_match) match = true;break ;default :@@ -1469,6 +1703,16 @@ static int nintendo_hid_probe(struct hid_device *hdev,hid_warn (hdev,"Analog stick positions may be inaccurate\n" ); } +/* get IMU calibration data, and parse it */ + ret =joycon_request_imu_calibration (ctlr); +if (ret) { +/* +* We can function with default calibration, but it may be +* inaccurate. Provide a warning, and continue on. +*/ +hid_warn (hdev,"Unable to read IMU calibration data\n" ); + } +/* Set the reporting mode to 0x30, which is the full report mode */ ret =joycon_set_report_mode (ctlr);if (ret) {@@ -1483,6 +1727,13 @@ static int nintendo_hid_probe(struct hid_device *hdev, goto err_mutex; }Quoted 13 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
drivers/hid/hid-nintendo.c +5
-0+/* Enable the IMU */ + ret =joycon_enable_imu (ctlr, true); +if (ret) { +hid_err (hdev,"Failed to enable the IMU; ret=%d\n" , ret); + goto err_mutex;Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
drivers/hid/hid-nintendo.c +2
-1+ } + ret =joycon_read_mac (ctlr);if (ret) { hid_err(hdev, "Failed to retrieve controller MAC; ret=%d\n", --2.24.1
--
Daniel Ogorchock
Re: [PATCH v10 11/12] HID: nintendo: add IMU support
Hi Roderick,
The Switch Pro Controller D-pad functions as a D-pad. You cannot push left and right at the same time.
Also, the Switch Pro Controller already functions as a controller when connected with Bluetooth and
using the standard Linux device driver. In this case, the D-pad is registered as a Hat.
I'd prefer if we didn't change this.
Quoted 640 lines · Roderick Colenbrander
Hi Daniel,
Whether to report as HAT or
BTN_DPADdepends on how the hardware works internally. You see the HAT vs buton at the HID report layer (it is quite different), but if you just touch the controller sticks you will also notice. A HAT axis is really like a joystick e.g. with a range -1 to +1. If you hold down e.g. "left" and "right" on a d-pad, you would get "0" back as you are unable to press both left and right at the same time. With d-pad buttons you could press left and right at the same time (depending on design).When I just tried my Switch Pro controller it really felt like the d-pad were buttons. Various other controllers using
BTN_DPADas well for example DualShock 3. Most applications these days should use SDL2, which will hide that.Thanks,
RoderickQuoted 619 lines · Daniel Ogorchock
Hi Carl,
When I was initially implementing the button mappings, I referred to the standard described here:
https://www.kernel.org/doc/html/latest/input/gamepad.htmlIt mentions under the d-pad section that digital inputs should be reported using the BTN_DPAD_* variant. Do the other controllers mentioned report their d-pads as analog values, or has using the ABS_HAT* values become the de facto format for digital inputs as well?
If the latter, I guess it would make sense to go with the crowd for the pro. It just seems a bit odd to report digital inputs as absolute axes, but I'm open to changing it if that's the consensus.
Thanks,
DanielQuoted 580 lines · Carl Mueller
(3rd/final attempt at reply due to list server rejecting message with HTML subpart; sorry about the spam!)
Hi Everyone,
In trying out this driver to use with the Switch Pro Controller, the biggest difficulty I had was that the D-pad is reported as buttons instead of as a Hat. All other controllers that are similar in structure (such as the PS3 and PS4 controllers, the Xbox controllers, and many others) report the D-pad as a Hat. This kind of consistency is needed to avoid lots of software compatibility issues.
I mentioned this to Daniel, and he indicated that he wanted the Switch Pro Controller to behave like the JoyCons, which have buttons instead of a D-pad. Having the JoyCons report the buttons as buttons makes sense, since it's possible to push opposite directions at the same time. However, I don't think that the argument carries over to the Switch Pro Controller, since it has a physically different control.
Again, the consistency with the other controllers that have all the same physical structure and the same types of controls seems more important to me than consistency with a controller that is physically much different. Additionally, many games are already written for use with controllers that look like the Switch Pro Controller, whereas fewer are written for use with Nintendo JoyCons. If we have to cause trouble for one side or the other, let's not cause trouble with the more established side.
Quoted 550 lines · Roderick Colenbrander
Hi Daniel,
Unfortunately there is no public test application at the moment to illustrate these features. I agree something is definitely needed.
I need to see if we can come up with something. One of the test apps we have internally is a 3D cube, which is controllable by controller motion. It of course shows the gyro / accelerometer values, but the position of the cube is tied to the calculated orientation. If something is off you will see weird movements in the cube or drift building up over time etcetera.
Though it would take some time to prepare something. The rest of the patch series looked fine I think, so the IMU parts may need to wait for a next kernel cycle, but all the other plumbing could go in (if others are okay of course).
Thanks,
RoderickQuoted 528 lines · Daniel Ogorchock
Hi Roderick,
Thanks for the feedback. In addition to the inline comments below, do you have any recommendations for test programs that you know work well with hid-sony's gyro implementation? Up to this point I've just been using evtest, which obviously isn't too useful for testing actual functionality of the gyro in an intuitive way.
Quoted 292 lines · Roderick Colenbrander
Hi Daniel,
I had some time to review the motion sensors patch. I have added some feedback inline with your patch below.
Aside from standard feedback on the code, I wanted to have a close look at the accelerometer / gyro data. My team has been doing a lot of work on this (and still is) and I want to make sure any work we do on "user space" software (e.g. Android) automatically works for Joycon as well. The accuracy of the data is very important else applications will make bad decisions. Userspace applications often combine the data of both sensors to calculate a position for position tracking. Inaccurate axes ranges or wrong precision can cause major issues. I may be a bit strict below, but it will just be to prevent headaches for others later on. I use the DS4 as a reference point as it was the first device (aside from Wii early on) where we properly report acceleration and gyro axes with
INPUT_PROP_ACCELEROMETERand we should be consistent with it here else applications could be confused, so we should use similar orientation of axes and resolutions.Thanks,
RoderickQuoted 25 lines · Daniel J. Ogorchock
This patch adds support for the controller's IMU. The accelerometer and gyro data are both provided to userspace using a second input device. The devices can be associated using their uniq value (set to the controller's MAC address).
The majority of this patch's functinoality was provided by Carl Mueller.
Signed-off-by Daniel J. Ogorchock <[email protected]>--- drivers/hid/hid-nintendo.c | 267 +++++++++++++++++++++++++++++++++++-- 1 file changed, 259 insertions(+), 8 deletions(-)drivers/hid/hid-nintendo.c +4
-0@@ -101,12 +101,29 @@ static const u16 JC_CAL_DATA_START = 0x603d;static const u16 JC_CAL_DATA_END =0x604e ;#define JC_CAL_DATA_SIZE (JC_CAL_DATA_END - JC_CAL_DATA_START + 1) +/* SPI storage addresses of IMU factory calibration data */ +static const u16 JC_IMU_CAL_DATA_START =0x6020 ; +static const u16 JC_IMU_CAL_DATA_END =0x6037 ; +#define JC_IMU_CAL_DATA_SIZE \drivers/hid/hid-nintendo.c static const u16 JC_CAL_DATA_START = 0x603d; · line 117
Quoted 10 lines
drivers/hid/hid-nintendo.c +4
-0+ (JC_IMU_CAL_DATA_END -JC_IMU_CAL_DATA_START +1 ) /* The raw analog joystick values will be mapped in terms of this magnitude */static const u16 JC_MAX_STICK_MAG =32767 ;static const u16 JC_STICK_FUZZ =250 ;static const u16 JC_STICK_FLAT =500 ; +/* The accel axes will be mapped in terms of this magnitude */ +static const u16 JC_MAX_ACCEL_MAG =32767 ; +static const u16 JC_ACCEL_RES =4096 ;What does the acceleration resolution equate to? For DS4 we use multiples of 'g'. (We don't know the position on earth, so can't report in m/s^2).
drivers/hid/hid-nintendo.c static const u16 JC_CAL_DATA_START = 0x603d; · line 123
Quoted 6 lines
drivers/hid/hid-nintendo.c +6
-0+static const u16 JC_ACCEL_FUZZ =10 ; +static const u16 JC_ACCEL_FLAT /*= 0*/ ; + +/* The gyro axes will be mapped in terms of this magnitude */ +static const u16 JC_MAX_GYRO_MAG =32767 ; +static const u16 JC_GYRO_RES =13371 /936 ;/* 14 (14.285) */ What does the gyro resolution equate to? For DS4 we report "degrees per second". We should do the same for the joycons, but I don't know how you guys figured out the exact resolution. As I mentioned if it is not accurate, applications will make wrong calculations.
Quoted 24 lines
drivers/hid/hid-nintendo.c +10
-0+static const u16 JC_GYRO_FUZZ =10 ; +static const u16 JC_GYRO_FLAT /*= 0*/ ; +/* frequency/amplitude tables for rumble */ struct joycon_rumble_freq_data {u16 high;@@ -234,6 +251,11 @@ struct joycon_stick_cal {s32 center; }; +struct joycon_imu_cal { +s16 offset[3 ]; +s16 scale[3 ]; +}; +/* * All the controller's button values are stored in a u32. * They can be accessed with bitwise ANDs. @@ -281,6 +303,19 @@ struct joycon_subcmd_reply {u8 data[0 ];/* will be at most 35 bytes */ } __packed; +struct joycon_imu_data { +s16 accel_x;drivers/hid/hid-nintendo.c struct joycon_subcmd_reply { · line 317
Quoted 10 lines
drivers/hid/hid-nintendo.c +10
-0+s16 accel_y; +s16 accel_z; +s16 gyro_x; +s16 gyro_y; +s16 gyro_z; +} __packed; + +struct joycon_imu_report { +struct joycon_imu_data data[3 ]; +} __packed;See comments later on about
imu_data, but you can't directly cast your data buffer to this data type due to endian issues. It may not really make sense to keep the data structure as you need to do manual data fetching.Quoted 6 lines
drivers/hid/hid-nintendo.c +1
-0+struct joycon_input_report {u8 id;u8 timer;@@ -290,11 +325,10 @@ struct joycon_input_report {u8 right_stick[3 ];drivers/hid/hid-nintendo.c struct joycon_input_report { · line 330
Quoted 10 lines
drivers/hid/hid-nintendo.c +3
-5u8 vibrator_report; -/* -* If support for firmware updates, gyroscope data, and/or NFC/IR -* are added in the future, this can be swapped for a union. -*/ -struct joycon_subcmd_reply reply; +union { +struct joycon_subcmd_reply subcmd_reply; +struct joycon_imu_report imu_report;maybe add a raw byte array to this union. Could help for parsing the imu data.
Quoted 50 lines
drivers/hid/hid-nintendo.c +21
-1+ }; } __packed;#define JC_MAX_RESP_SIZE (sizeof(struct joycon_input_report) + 35)@@ -334,6 +368,9 @@ struct joycon_ctlr {struct joycon_stick_cal right_stick_cal_x;struct joycon_stick_cal right_stick_cal_y; +struct joycon_imu_cal accel_cal; +struct joycon_imu_cal gyro_cal; +/* power supply data */ struct power_supply *battery;struct power_supply_desc battery_desc;@@ -352,6 +389,10 @@ struct joycon_ctlr {u16 rumble_lh_freq;u16 rumble_rl_freq;u16 rumble_rh_freq; + +/* imu */ +struct input_dev *imu_input; +int timestamp; };static int __joycon_hid_send (struct hid_device *hdev,u8 *data,size_t len)@@ -547,7 +588,7 @@ static int joycon_request_calibration(struct joycon_ctlr *ctlr) } report = (struct joycon_input_report *)ctlr->input_buf; - raw_cal = &report->reply.data[5 ]; + raw_cal = &report->subcmd_reply.data[5 ];/* left stick calibration parsing */ cal_x = &ctlr->left_stick_cal_x;@@ -601,6 +642,85 @@ static int joycon_request_calibration(struct joycon_ctlr *ctlr)return 0 ; } +static const s16 DFLT_ACCEL_OFFSET /*= 0*/ ; +static const s16 DFLT_ACCEL_SCALE =16384 ; +static const s16 DFLT_GYRO_OFFSET /*= 0*/ ; +static const s16 DFLT_GYRO_SCALE =13371 ; +static int joycon_request_imu_calibration (struct joycon_ctlr *ctlr) +{ +struct joycon_subcmd_request *req; +u8 buffer[sizeof (*req) +5 ] = {0 }; +struct joycon_input_report *report; +u8 *data; +u8 *raw_cal; +int ret;drivers/hid/hid-nintendo.c static int joycon_request_calibration(struct joycon_ctlr *ctlr) · line 666
Quoted 10 lines
drivers/hid/hid-nintendo.c +10
-0+int i; + +/* request IMU calibration data */ + req = (struct joycon_subcmd_request *)buffer; + req->subcmd_id =JC_SUBCMD_SPI_FLASH_READ ; + data = req->data; + data[0 ] =0xFF &JC_IMU_CAL_DATA_START ; + data[1 ] =0xFF & (JC_IMU_CAL_DATA_START >>8 ); + data[2 ] =0xFF & (JC_IMU_CAL_DATA_START >>16 ); + data[3 ] =0xFF & (JC_IMU_CAL_DATA_START >>24 );Maybe use
put_unaligned_le32? I think it allows you to avoid doing all these calculations yourself:put_unaligned_le32(JC_IMU_CAL_DATA_START, data);Quoted 24 lines
drivers/hid/hid-nintendo.c +24
-0+ data[4 ] =JC_IMU_CAL_DATA_SIZE ; + +hid_dbg (ctlr->hdev,"requesting IMU cal data\n" ); + ret =joycon_send_subcmd (ctlr, req,5 ,HZ ); + +if (ret) { +hid_warn (ctlr->hdev, +"Failed to read IMU cal, using defaults; ret=%d\n" , + ret); + +for (i =0 ; i <3 ; i++) { + ctlr->accel_cal.offset[i] =DFLT_ACCEL_OFFSET ; + ctlr->accel_cal.scale[i] =DFLT_ACCEL_SCALE ; + ctlr->gyro_cal.offset[i] =DFLT_GYRO_OFFSET ; + ctlr->gyro_cal.scale[i] =DFLT_GYRO_SCALE ; + } +return ret; + } + + report = (struct joycon_input_report *)ctlr->input_buf; + raw_cal = &report->subcmd_reply.data[5 ]; + +/* IMU calibration parsing */ +for (i =0 ; i <3 ; i++) {drivers/hid/hid-nintendo.c static int joycon_request_calibration(struct joycon_ctlr *ctlr) · line 700
Quoted 10 lines
drivers/hid/hid-nintendo.c +10
-0+int j = i *2 ; + + ctlr->accel_cal.offset[i] = raw_cal[j +0 ] | + ((s16 )raw_cal[j +1 ] <<8 ); + ctlr->accel_cal.scale[i] = raw_cal[j +6 ] | + ((s16 )raw_cal[j +7 ] <<8 ); + ctlr->gyro_cal.offset[i] = raw_cal[j +12 ] | + ((s16 )raw_cal[j +13 ] <<8 ); + ctlr->gyro_cal.scale[i] = raw_cal[j +18 ] | + ((s16 )raw_cal[j +19 ] <<8 );
get_unaligned_le16instead of doing your own bitshifts?Quoted 47 lines
drivers/hid/hid-nintendo.c +36
-0+ } + +hid_dbg (ctlr->hdev,"IMU calibration:\n" +"a_o[0]=%d a_o[1]=%d a_o[2]=%d\n" +"a_s[0]=%d a_s[1]=%d a_s[2]=%d\n" +"g_o[0]=%d g_o[1]=%d g_o[2]=%d\n" +"g_s[0]=%d g_s[1]=%d g_s[2]=%d\n" , + ctlr->accel_cal.offset[0 ], + ctlr->accel_cal.offset[1 ], + ctlr->accel_cal.offset[2 ], + ctlr->accel_cal.scale[0 ], + ctlr->accel_cal.scale[1 ], + ctlr->accel_cal.scale[2 ], + ctlr->gyro_cal.offset[0 ], + ctlr->gyro_cal.offset[1 ], + ctlr->gyro_cal.offset[2 ], + ctlr->gyro_cal.scale[0 ], + ctlr->gyro_cal.scale[1 ], + ctlr->gyro_cal.scale[2 ]); + +return 0 ; +} +static int joycon_set_report_mode (struct joycon_ctlr *ctlr) {struct joycon_subcmd_request *req;@@ -627,6 +747,19 @@ static int joycon_enable_rumble(struct joycon_ctlr *ctlr, bool enable)return joycon_send_subcmd (ctlr, req,1 ,HZ /4 ); } +static int joycon_enable_imu (struct joycon_ctlr *ctlr,bool enable) +{ +struct joycon_subcmd_request *req; +u8 buffer[sizeof (*req) +1 ] = {0 }; + + req = (struct joycon_subcmd_request *)buffer; + req->subcmd_id =JC_SUBCMD_ENABLE_IMU ; + req->data[0 ] = enable ?0x01 :0x00 ; + +hid_dbg (ctlr->hdev,"%s IMU\n" , enable ?"enabling" :"disabling" ); +return joycon_send_subcmd (ctlr, req,1 ,HZ ); +} +static s32 joycon_map_stick_val (struct joycon_stick_cal *cal,s32 val) {s32 center = cal->center;drivers/hid/hid-nintendo.c static void joycon_parse_report(struct joycon_ctlr *ctlr, · line 910
Quoted 7 lines
drivers/hid/hid-nintendo.c +4
-0@@ -771,6 +904,54 @@ static void joycon_parse_report(struct joycon_ctlr *ctlr,spin_unlock_irqrestore (&ctlr->lock, flags);wake_up (&ctlr->wait); } + +/* parse IMU data if present */ +if (rep->id ==JC_INPUT_IMU_DATA ) { +struct joycon_imu_data *imu_data = rep->imu_report.data;Quoted 7 lines
I don't think you can directly covert your input report data to
imu_data. Until now you have been lucky enough (based on a quick glance of the the other patches) that your data are single bytes. However, this data seems to be a bunch of s16's. In other words you have to deal with endianess issues. You need to useget_unaligned_le16to retrieve data from your raw byte buffer. See other HID drivers for reference.Ah, good point. I'd overlooked that entirely.
Quoted 16 lines
Since you will have to use
get_unaligned_le16, it probably won't make much sense to really have ajoycon_imu_datastructure. Maybe extend yourinput_bufferunion with raw bytes and in this case just use raw bytes. Each of your loop iterations below just grab the values from the buffer and store them in a variable.drivers/hid/hid-nintendo.c static void joycon_parse_report(struct joycon_ctlr *ctlr, · line 918
Quoted 8 lines
drivers/hid/hid-nintendo.c +8
-0+struct input_dev *idev = ctlr->imu_input; +int i; +int value[6 ]; + +for (i =0 ; i <3 ; i++) {/* there are 3 reports */ + ctlr->timestamp +=5000 ;/* each is 5 ms apart */ +input_event (idev,EV_MSC ,MSC_TIMESTAMP , + ctlr->timestamp);Quoted 4 lines
How sure are you that this is always 5ms? Is there a hardware timestamp somewhere? If I look at our DS4 the timing isn't guaranteed (Bluetooth is lossy) and not all packets even make it.
It appears that the closest thing to a hardware timestamp available is a quickly incrementing 1-byte timer sent with every report. It's only really useful for latency estimation. Each input report includes 3 IMU samples which are 5ms apart for the joy-cons. It's recently come to my attention that the samples may be spaced differently for the pro controller, so I'm going to need to dive into this more anyway. I'm not sure what a great way would be to handle lost reports.
Quoted 27 lines
Quoted 16 lines
drivers/hid/hid-nintendo.c +16
-0+ +/* +* Rather than convert to floats, we adjust by +* calibration factors and then multiply by the default +* scaling values. This way, the consumer only needs to +* divide by the default scale values. +*/ + value[0 ] = (imu_data[i].gyro_x - + ctlr->gyro_cal.offset[0 ]) * +DFLT_GYRO_SCALE / ctlr->gyro_cal.scale[0 ]; + value[1 ] = (imu_data[i].gyro_y - + ctlr->gyro_cal.offset[1 ]) * +DFLT_GYRO_SCALE / ctlr->gyro_cal.scale[1 ]; + value[2 ] = (imu_data[i].gyro_z - + ctlr->gyro_cal.offset[2 ]) * +DFLT_GYRO_SCALE / ctlr->gyro_cal.scale[2 ];drivers/hid/hid-nintendo.c static void joycon_parse_report(struct joycon_ctlr *ctlr, · line 944
Quoted 10 lines
drivers/hid/hid-nintendo.c +10
-0+ + value[3 ] = (imu_data[i].accel_x - + ctlr->accel_cal.offset[0 ]) * +DFLT_ACCEL_SCALE / ctlr->accel_cal.scale[0 ]; + value[4 ] = (imu_data[i].accel_y - + ctlr->accel_cal.offset[1 ]) * +DFLT_ACCEL_SCALE / ctlr->accel_cal.scale[1 ]; + value[5 ] = (imu_data[i].accel_z - + ctlr->accel_cal.offset[2 ]) * +DFLT_ACCEL_SCALE / ctlr->accel_cal.scale[2 ];Quoted 9 lines
Just in case I would double check the precision of your calculations. For DS4 we had similar calculations, but we had a small loss of precision, which ultimately caused major calculation errors. (Applications often use accelerometer + gyro data to calculate an absolute position. This involves integration.. and small errors become big). We had to use the "
mult_frac" macro to restore some of the precision during the calculations. It might be something to double check.That's good to know. I'll look more closely at how it's implemented in hid-sony.
Quoted 6 lines
In addition what orientation are you using for the axes? I need to double check the DS4 datasheets, but I think we were using a "right handed" coordinate system. (So if you make a fist of your right hand with thumb up, the gyro curls around it counter clockwise along the direction of your fingers).
The orientation of the axes (and much more) are documented here:
https://github.com/dekuNukem/Nintendo_Switch_Reverse_Engineering/blob/master/imu_sensor_notes.md
Since the the right vs. left joy-cons have different axes orientations, I assume it's preferable to standardize it all in software to match the orientation of the DS4.Quoted 133 lines
Quoted 133 lines
drivers/hid/hid-nintendo.c +75
-2+ +input_report_abs (idev,ABS_RX , value[0 ]); +input_report_abs (idev,ABS_RY , value[1 ]); +input_report_abs (idev,ABS_RZ , value[2 ]); +input_report_abs (idev,ABS_X , value[3 ]); +input_report_abs (idev,ABS_Y , value[4 ]); +input_report_abs (idev,ABS_Z , value[5 ]); +input_sync (idev); + } + } }static void joycon_rumble_worker (struct work_struct *work)@@ -950,6 +1131,7 @@ static int joycon_input_create(struct joycon_ctlr *ctlr) {struct hid_device *hdev;const char *name; +const char *imu_name;int ret;int i;@@ -958,12 +1140,15 @@ static int joycon_input_create(struct joycon_ctlr *ctlr)switch (hdev->product) {case USB_DEVICE_ID_NINTENDO_PROCON : name ="Nintendo Switch Pro Controller" ; + imu_name ="Nintendo Switch Pro Controller IMU" ;break ;case USB_DEVICE_ID_NINTENDO_JOYCONL : name ="Nintendo Switch Left Joy-Con" ; + imu_name ="Nintendo Switch Left Joy-Con IMU" ;break ;case USB_DEVICE_ID_NINTENDO_JOYCONR : name ="Nintendo Switch Right Joy-Con" ; + imu_name ="Nintendo Switch Right Joy-Con IMU" ;break ;default :/* Should be impossible */ hid_err (hdev,"Invalid hid product\n" );@@ -1029,6 +1214,55 @@ static int joycon_input_create(struct joycon_ctlr *ctlr)if (ret)return ret; +/* configure the imu input device */ + ctlr->imu_input =devm_input_allocate_device (&hdev->dev); +if (!ctlr->imu_input) +return -ENOMEM ; + + ctlr->imu_input->id.bustype = hdev->bus; + ctlr->imu_input->id.vendor = hdev->vendor; + ctlr->imu_input->id.product = hdev->product; + ctlr->imu_input->id.version = hdev->version; + ctlr->imu_input->uniq = ctlr->mac_addr_str; + ctlr->imu_input->name = imu_name; +input_set_drvdata (ctlr->imu_input, ctlr); + +/* configure imu axes */ +input_set_abs_params (ctlr->imu_input,ABS_X , + -JC_MAX_ACCEL_MAG ,JC_MAX_ACCEL_MAG , +JC_ACCEL_FUZZ ,JC_ACCEL_FLAT ); +input_set_abs_params (ctlr->imu_input,ABS_Y , + -JC_MAX_ACCEL_MAG ,JC_MAX_ACCEL_MAG , +JC_ACCEL_FUZZ ,JC_ACCEL_FLAT ); +input_set_abs_params (ctlr->imu_input,ABS_Z , + -JC_MAX_ACCEL_MAG ,JC_MAX_ACCEL_MAG , +JC_ACCEL_FUZZ ,JC_ACCEL_FLAT ); +input_abs_set_res (ctlr->imu_input,ABS_X ,JC_ACCEL_RES ); +input_abs_set_res (ctlr->imu_input,ABS_Y ,JC_ACCEL_RES ); +input_abs_set_res (ctlr->imu_input,ABS_Z ,JC_ACCEL_RES ); + +input_set_abs_params (ctlr->imu_input,ABS_RX , + -JC_MAX_GYRO_MAG ,JC_MAX_GYRO_MAG , +JC_GYRO_FUZZ ,JC_GYRO_FLAT ); +input_set_abs_params (ctlr->imu_input,ABS_RY , + -JC_MAX_GYRO_MAG ,JC_MAX_GYRO_MAG , +JC_GYRO_FUZZ ,JC_GYRO_FLAT ); +input_set_abs_params (ctlr->imu_input,ABS_RZ , + -JC_MAX_GYRO_MAG ,JC_MAX_GYRO_MAG , +JC_GYRO_FUZZ ,JC_GYRO_FLAT ); + +input_abs_set_res (ctlr->imu_input,ABS_RX ,JC_GYRO_RES ); +input_abs_set_res (ctlr->imu_input,ABS_RY ,JC_GYRO_RES ); +input_abs_set_res (ctlr->imu_input,ABS_RZ ,JC_GYRO_RES ); + +__set_bit (EV_MSC , ctlr->imu_input->evbit); +__set_bit (MSC_TIMESTAMP , ctlr->imu_input->mscbit); +__set_bit (INPUT_PROP_ACCELEROMETER , ctlr->imu_input->propbit); + + ret =input_register_device (ctlr->imu_input); +if (ret) +return ret; +return 0 ; }@@ -1288,7 +1522,7 @@ static int joycon_read_mac(struct joycon_ctlr *ctlr) report = (struct joycon_input_report *)ctlr->input_buf;for (i =4 , j =0 ; j <6 ; i++, j++) - ctlr->mac_addr[j] = report->reply.data[i]; + ctlr->mac_addr[j] = report->subcmd_reply.data[i]; ctlr->mac_addr_str = devm_kasprintf(&ctlr->hdev->dev, GFP_KERNEL, "%02X:%02X:%02X:%02X:%02X:%02X",@@ -1343,7 +1577,7 @@ static int joycon_ctlr_handle_event(struct joycon_ctlr *ctlr, u8 *data, data[0] != JC_INPUT_SUBCMD_REPLY)break ; report = (struct joycon_input_report *)data; -if (report->reply.id == ctlr->subcmd_ack_match) +if (report->subcmd_reply.id == ctlr->subcmd_ack_match) match = true;break ;default :@@ -1469,6 +1703,16 @@ static int nintendo_hid_probe(struct hid_device *hdev,hid_warn (hdev,"Analog stick positions may be inaccurate\n" ); } +/* get IMU calibration data, and parse it */ + ret =joycon_request_imu_calibration (ctlr); +if (ret) { +/* +* We can function with default calibration, but it may be +* inaccurate. Provide a warning, and continue on. +*/ +hid_warn (hdev,"Unable to read IMU calibration data\n" ); + } +/* Set the reporting mode to 0x30, which is the full report mode */ ret =joycon_set_report_mode (ctlr);if (ret) {@@ -1483,6 +1727,13 @@ static int nintendo_hid_probe(struct hid_device *hdev, goto err_mutex; }Quoted 13 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
drivers/hid/hid-nintendo.c +5
-0+/* Enable the IMU */ + ret =joycon_enable_imu (ctlr, true); +if (ret) { +hid_err (hdev,"Failed to enable the IMU; ret=%d\n" , ret); + goto err_mutex;Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
drivers/hid/hid-nintendo.c +2
-1+ } + ret =joycon_read_mac (ctlr);if (ret) { hid_err(hdev, "Failed to retrieve controller MAC; ret=%d\n", --2.24.1
--
Daniel Ogorchock
A message that is not in the archive (sent off-list or not yet received): CAKF84v26=X8OLPavdE52tprm=WOynUXRz2aDjz5Bvqw6rdTZQg@mail.gmail.com
Daniel Ogorchock
maintainerRe: [PATCH v10 11/12] HID: nintendo: add IMU support
Hi Siarhei,
Thanks for catching that. I'll fix it for the next rev. I'll also do a sanity check compilation for each patch in the series.
-Daniel
Quoted 6 lines · Siarhei Vishniakou
Hi Daniel,
I just tried to build the first patch in isolation, and I'm hitting a build error: in the function
nintendo_hid_remove, the variable "ctrl" is unused. It looks like it's first used in the rumble patch, 'add rumble support'. Could you please move that variable declaration there?Also, in the same rumble patch, I'm seeing that the function '
joycon_enable_rumble' is always called with the second parameter bool enable = true. Maybe that parameter can be removed from the function definition?
Re: [PATCH v10 11/12] HID: nintendo: add IMU support
Hi Daniel,
I wonder if it would make sense to fold some of the patches into the first patch?
As an example, the first patch essentially adds the basic joystick functionality, and at the same time it uses subcommands to communicate with the device, for things like retrieving the calibration data. Somewhere later in the patch series, there is a patch that improves subcommand reliability. Maybe the 'improve subcommands' patch can be combined with the first patch?
Daniel Ogorchock
maintainerRe: [PATCH v10 11/12] HID: nintendo: add IMU support
In reply to Carl MuellerDoes anyone else have an opinion on this? I agree with Carl that the D-pad cannot register opposite direction inputs simultaneously.
Thanks,
Daniel
Quoted 655 lines · Carl Mueller
Hi Roderick,
The Switch Pro Controller D-pad functions as a D-pad. You cannot push left and right at the same time.
Also, the Switch Pro Controller already functions as a controller when connected with Bluetooth and
using the standard Linux device driver. In this case, the D-pad is registered as a Hat.
I'd prefer if we didn't change this.Quoted 640 lines · Roderick Colenbrander
Hi Daniel,
Whether to report as HAT or
BTN_DPADdepends on how the hardware works internally. You see the HAT vs buton at the HID report layer (it is quite different), but if you just touch the controller sticks you will also notice. A HAT axis is really like a joystick e.g. with a range -1 to +1. If you hold down e.g. "left" and "right" on a d-pad, you would get "0" back as you are unable to press both left and right at the same time. With d-pad buttons you could press left and right at the same time (depending on design).When I just tried my Switch Pro controller it really felt like the d-pad were buttons. Various other controllers using
BTN_DPADas well for example DualShock 3. Most applications these days should use SDL2, which will hide that.Thanks,
RoderickQuoted 619 lines · Daniel Ogorchock
Hi Carl,
When I was initially implementing the button mappings, I referred to the standard described here:
https://www.kernel.org/doc/html/latest/input/gamepad.htmlIt mentions under the d-pad section that digital inputs should be reported using the BTN_DPAD_* variant. Do the other controllers mentioned report their d-pads as analog values, or has using the ABS_HAT* values become the de facto format for digital inputs as well?
If the latter, I guess it would make sense to go with the crowd for the pro. It just seems a bit odd to report digital inputs as absolute axes, but I'm open to changing it if that's the consensus.
Thanks,
DanielQuoted 580 lines · Carl Mueller
(3rd/final attempt at reply due to list server rejecting message with HTML subpart; sorry about the spam!)
Hi Everyone,
In trying out this driver to use with the Switch Pro Controller, the biggest difficulty I had was that the D-pad is reported as buttons instead of as a Hat. All other controllers that are similar in structure (such as the PS3 and PS4 controllers, the Xbox controllers, and many others) report the D-pad as a Hat. This kind of consistency is needed to avoid lots of software compatibility issues.
I mentioned this to Daniel, and he indicated that he wanted the Switch Pro Controller to behave like the JoyCons, which have buttons instead of a D-pad. Having the JoyCons report the buttons as buttons makes sense, since it's possible to push opposite directions at the same time. However, I don't think that the argument carries over to the Switch Pro Controller, since it has a physically different control.
Again, the consistency with the other controllers that have all the same physical structure and the same types of controls seems more important to me than consistency with a controller that is physically much different. Additionally, many games are already written for use with controllers that look like the Switch Pro Controller, whereas fewer are written for use with Nintendo JoyCons. If we have to cause trouble for one side or the other, let's not cause trouble with the more established side.
Quoted 550 lines · Roderick Colenbrander
Hi Daniel,
Unfortunately there is no public test application at the moment to illustrate these features. I agree something is definitely needed.
I need to see if we can come up with something. One of the test apps we have internally is a 3D cube, which is controllable by controller motion. It of course shows the gyro / accelerometer values, but the position of the cube is tied to the calculated orientation. If something is off you will see weird movements in the cube or drift building up over time etcetera.
Though it would take some time to prepare something. The rest of the patch series looked fine I think, so the IMU parts may need to wait for a next kernel cycle, but all the other plumbing could go in (if others are okay of course).
Thanks,
RoderickQuoted 528 lines · Daniel Ogorchock
Hi Roderick,
Thanks for the feedback. In addition to the inline comments below, do you have any recommendations for test programs that you know work well with hid-sony's gyro implementation? Up to this point I've just been using evtest, which obviously isn't too useful for testing actual functionality of the gyro in an intuitive way.
Quoted 292 lines · Roderick Colenbrander
Hi Daniel,
I had some time to review the motion sensors patch. I have added some feedback inline with your patch below.
Aside from standard feedback on the code, I wanted to have a close look at the accelerometer / gyro data. My team has been doing a lot of work on this (and still is) and I want to make sure any work we do on "user space" software (e.g. Android) automatically works for Joycon as well. The accuracy of the data is very important else applications will make bad decisions. Userspace applications often combine the data of both sensors to calculate a position for position tracking. Inaccurate axes ranges or wrong precision can cause major issues. I may be a bit strict below, but it will just be to prevent headaches for others later on. I use the DS4 as a reference point as it was the first device (aside from Wii early on) where we properly report acceleration and gyro axes with
INPUT_PROP_ACCELEROMETERand we should be consistent with it here else applications could be confused, so we should use similar orientation of axes and resolutions.Thanks,
RoderickQuoted 25 lines · Daniel J. Ogorchock
This patch adds support for the controller's IMU. The accelerometer and gyro data are both provided to userspace using a second input device. The devices can be associated using their uniq value (set to the controller's MAC address).
The majority of this patch's functinoality was provided by Carl Mueller.
Signed-off-by Daniel J. Ogorchock <[email protected]>--- drivers/hid/hid-nintendo.c | 267 +++++++++++++++++++++++++++++++++++-- 1 file changed, 259 insertions(+), 8 deletions(-)drivers/hid/hid-nintendo.c +4
-0@@ -101,12 +101,29 @@ static const u16 JC_CAL_DATA_START = 0x603d;static const u16 JC_CAL_DATA_END =0x604e ;#define JC_CAL_DATA_SIZE (JC_CAL_DATA_END - JC_CAL_DATA_START + 1) +/* SPI storage addresses of IMU factory calibration data */ +static const u16 JC_IMU_CAL_DATA_START =0x6020 ; +static const u16 JC_IMU_CAL_DATA_END =0x6037 ; +#define JC_IMU_CAL_DATA_SIZE \drivers/hid/hid-nintendo.c static const u16 JC_CAL_DATA_START = 0x603d; · line 117
Quoted 10 lines
drivers/hid/hid-nintendo.c +4
-0+ (JC_IMU_CAL_DATA_END -JC_IMU_CAL_DATA_START +1 ) /* The raw analog joystick values will be mapped in terms of this magnitude */static const u16 JC_MAX_STICK_MAG =32767 ;static const u16 JC_STICK_FUZZ =250 ;static const u16 JC_STICK_FLAT =500 ; +/* The accel axes will be mapped in terms of this magnitude */ +static const u16 JC_MAX_ACCEL_MAG =32767 ; +static const u16 JC_ACCEL_RES =4096 ;What does the acceleration resolution equate to? For DS4 we use multiples of 'g'. (We don't know the position on earth, so can't report in m/s^2).
drivers/hid/hid-nintendo.c static const u16 JC_CAL_DATA_START = 0x603d; · line 123
Quoted 6 lines
drivers/hid/hid-nintendo.c +6
-0+static const u16 JC_ACCEL_FUZZ =10 ; +static const u16 JC_ACCEL_FLAT /*= 0*/ ; + +/* The gyro axes will be mapped in terms of this magnitude */ +static const u16 JC_MAX_GYRO_MAG =32767 ; +static const u16 JC_GYRO_RES =13371 /936 ;/* 14 (14.285) */ What does the gyro resolution equate to? For DS4 we report "degrees per second". We should do the same for the joycons, but I don't know how you guys figured out the exact resolution. As I mentioned if it is not accurate, applications will make wrong calculations.
Quoted 24 lines
drivers/hid/hid-nintendo.c +10
-0+static const u16 JC_GYRO_FUZZ =10 ; +static const u16 JC_GYRO_FLAT /*= 0*/ ; +/* frequency/amplitude tables for rumble */ struct joycon_rumble_freq_data {u16 high;@@ -234,6 +251,11 @@ struct joycon_stick_cal {s32 center; }; +struct joycon_imu_cal { +s16 offset[3 ]; +s16 scale[3 ]; +}; +/* * All the controller's button values are stored in a u32. * They can be accessed with bitwise ANDs. @@ -281,6 +303,19 @@ struct joycon_subcmd_reply {u8 data[0 ];/* will be at most 35 bytes */ } __packed; +struct joycon_imu_data { +s16 accel_x;drivers/hid/hid-nintendo.c struct joycon_subcmd_reply { · line 317
Quoted 10 lines
drivers/hid/hid-nintendo.c +10
-0+s16 accel_y; +s16 accel_z; +s16 gyro_x; +s16 gyro_y; +s16 gyro_z; +} __packed; + +struct joycon_imu_report { +struct joycon_imu_data data[3 ]; +} __packed;See comments later on about
imu_data, but you can't directly cast your data buffer to this data type due to endian issues. It may not really make sense to keep the data structure as you need to do manual data fetching.Quoted 6 lines
drivers/hid/hid-nintendo.c +1
-0+struct joycon_input_report {u8 id;u8 timer;@@ -290,11 +325,10 @@ struct joycon_input_report {u8 right_stick[3 ];drivers/hid/hid-nintendo.c struct joycon_input_report { · line 330
Quoted 10 lines
drivers/hid/hid-nintendo.c +3
-5u8 vibrator_report; -/* -* If support for firmware updates, gyroscope data, and/or NFC/IR -* are added in the future, this can be swapped for a union. -*/ -struct joycon_subcmd_reply reply; +union { +struct joycon_subcmd_reply subcmd_reply; +struct joycon_imu_report imu_report;maybe add a raw byte array to this union. Could help for parsing the imu data.
Quoted 50 lines
drivers/hid/hid-nintendo.c +21
-1+ }; } __packed;#define JC_MAX_RESP_SIZE (sizeof(struct joycon_input_report) + 35)@@ -334,6 +368,9 @@ struct joycon_ctlr {struct joycon_stick_cal right_stick_cal_x;struct joycon_stick_cal right_stick_cal_y; +struct joycon_imu_cal accel_cal; +struct joycon_imu_cal gyro_cal; +/* power supply data */ struct power_supply *battery;struct power_supply_desc battery_desc;@@ -352,6 +389,10 @@ struct joycon_ctlr {u16 rumble_lh_freq;u16 rumble_rl_freq;u16 rumble_rh_freq; + +/* imu */ +struct input_dev *imu_input; +int timestamp; };static int __joycon_hid_send (struct hid_device *hdev,u8 *data,size_t len)@@ -547,7 +588,7 @@ static int joycon_request_calibration(struct joycon_ctlr *ctlr) } report = (struct joycon_input_report *)ctlr->input_buf; - raw_cal = &report->reply.data[5 ]; + raw_cal = &report->subcmd_reply.data[5 ];/* left stick calibration parsing */ cal_x = &ctlr->left_stick_cal_x;@@ -601,6 +642,85 @@ static int joycon_request_calibration(struct joycon_ctlr *ctlr)return 0 ; } +static const s16 DFLT_ACCEL_OFFSET /*= 0*/ ; +static const s16 DFLT_ACCEL_SCALE =16384 ; +static const s16 DFLT_GYRO_OFFSET /*= 0*/ ; +static const s16 DFLT_GYRO_SCALE =13371 ; +static int joycon_request_imu_calibration (struct joycon_ctlr *ctlr) +{ +struct joycon_subcmd_request *req; +u8 buffer[sizeof (*req) +5 ] = {0 }; +struct joycon_input_report *report; +u8 *data; +u8 *raw_cal; +int ret;drivers/hid/hid-nintendo.c static int joycon_request_calibration(struct joycon_ctlr *ctlr) · line 666
Quoted 10 lines
drivers/hid/hid-nintendo.c +10
-0+int i; + +/* request IMU calibration data */ + req = (struct joycon_subcmd_request *)buffer; + req->subcmd_id =JC_SUBCMD_SPI_FLASH_READ ; + data = req->data; + data[0 ] =0xFF &JC_IMU_CAL_DATA_START ; + data[1 ] =0xFF & (JC_IMU_CAL_DATA_START >>8 ); + data[2 ] =0xFF & (JC_IMU_CAL_DATA_START >>16 ); + data[3 ] =0xFF & (JC_IMU_CAL_DATA_START >>24 );Maybe use
put_unaligned_le32? I think it allows you to avoid doing all these calculations yourself:put_unaligned_le32(JC_IMU_CAL_DATA_START, data);Quoted 24 lines
drivers/hid/hid-nintendo.c +24
-0+ data[4 ] =JC_IMU_CAL_DATA_SIZE ; + +hid_dbg (ctlr->hdev,"requesting IMU cal data\n" ); + ret =joycon_send_subcmd (ctlr, req,5 ,HZ ); + +if (ret) { +hid_warn (ctlr->hdev, +"Failed to read IMU cal, using defaults; ret=%d\n" , + ret); + +for (i =0 ; i <3 ; i++) { + ctlr->accel_cal.offset[i] =DFLT_ACCEL_OFFSET ; + ctlr->accel_cal.scale[i] =DFLT_ACCEL_SCALE ; + ctlr->gyro_cal.offset[i] =DFLT_GYRO_OFFSET ; + ctlr->gyro_cal.scale[i] =DFLT_GYRO_SCALE ; + } +return ret; + } + + report = (struct joycon_input_report *)ctlr->input_buf; + raw_cal = &report->subcmd_reply.data[5 ]; + +/* IMU calibration parsing */ +for (i =0 ; i <3 ; i++) {drivers/hid/hid-nintendo.c static int joycon_request_calibration(struct joycon_ctlr *ctlr) · line 700
Quoted 10 lines
drivers/hid/hid-nintendo.c +10
-0+int j = i *2 ; + + ctlr->accel_cal.offset[i] = raw_cal[j +0 ] | + ((s16 )raw_cal[j +1 ] <<8 ); + ctlr->accel_cal.scale[i] = raw_cal[j +6 ] | + ((s16 )raw_cal[j +7 ] <<8 ); + ctlr->gyro_cal.offset[i] = raw_cal[j +12 ] | + ((s16 )raw_cal[j +13 ] <<8 ); + ctlr->gyro_cal.scale[i] = raw_cal[j +18 ] | + ((s16 )raw_cal[j +19 ] <<8 );
get_unaligned_le16instead of doing your own bitshifts?Quoted 47 lines
drivers/hid/hid-nintendo.c +36
-0+ } + +hid_dbg (ctlr->hdev,"IMU calibration:\n" +"a_o[0]=%d a_o[1]=%d a_o[2]=%d\n" +"a_s[0]=%d a_s[1]=%d a_s[2]=%d\n" +"g_o[0]=%d g_o[1]=%d g_o[2]=%d\n" +"g_s[0]=%d g_s[1]=%d g_s[2]=%d\n" , + ctlr->accel_cal.offset[0 ], + ctlr->accel_cal.offset[1 ], + ctlr->accel_cal.offset[2 ], + ctlr->accel_cal.scale[0 ], + ctlr->accel_cal.scale[1 ], + ctlr->accel_cal.scale[2 ], + ctlr->gyro_cal.offset[0 ], + ctlr->gyro_cal.offset[1 ], + ctlr->gyro_cal.offset[2 ], + ctlr->gyro_cal.scale[0 ], + ctlr->gyro_cal.scale[1 ], + ctlr->gyro_cal.scale[2 ]); + +return 0 ; +} +static int joycon_set_report_mode (struct joycon_ctlr *ctlr) {struct joycon_subcmd_request *req;@@ -627,6 +747,19 @@ static int joycon_enable_rumble(struct joycon_ctlr *ctlr, bool enable)return joycon_send_subcmd (ctlr, req,1 ,HZ /4 ); } +static int joycon_enable_imu (struct joycon_ctlr *ctlr,bool enable) +{ +struct joycon_subcmd_request *req; +u8 buffer[sizeof (*req) +1 ] = {0 }; + + req = (struct joycon_subcmd_request *)buffer; + req->subcmd_id =JC_SUBCMD_ENABLE_IMU ; + req->data[0 ] = enable ?0x01 :0x00 ; + +hid_dbg (ctlr->hdev,"%s IMU\n" , enable ?"enabling" :"disabling" ); +return joycon_send_subcmd (ctlr, req,1 ,HZ ); +} +static s32 joycon_map_stick_val (struct joycon_stick_cal *cal,s32 val) {s32 center = cal->center;drivers/hid/hid-nintendo.c static void joycon_parse_report(struct joycon_ctlr *ctlr, · line 910
Quoted 7 lines
drivers/hid/hid-nintendo.c +4
-0@@ -771,6 +904,54 @@ static void joycon_parse_report(struct joycon_ctlr *ctlr,spin_unlock_irqrestore (&ctlr->lock, flags);wake_up (&ctlr->wait); } + +/* parse IMU data if present */ +if (rep->id ==JC_INPUT_IMU_DATA ) { +struct joycon_imu_data *imu_data = rep->imu_report.data;Quoted 7 lines
I don't think you can directly covert your input report data to
imu_data. Until now you have been lucky enough (based on a quick glance of the the other patches) that your data are single bytes. However, this data seems to be a bunch of s16's. In other words you have to deal with endianess issues. You need to useget_unaligned_le16to retrieve data from your raw byte buffer. See other HID drivers for reference.Ah, good point. I'd overlooked that entirely.
Quoted 16 lines
Since you will have to use
get_unaligned_le16, it probably won't make much sense to really have ajoycon_imu_datastructure. Maybe extend yourinput_bufferunion with raw bytes and in this case just use raw bytes. Each of your loop iterations below just grab the values from the buffer and store them in a variable.drivers/hid/hid-nintendo.c static void joycon_parse_report(struct joycon_ctlr *ctlr, · line 918
Quoted 8 lines
drivers/hid/hid-nintendo.c +8
-0+struct input_dev *idev = ctlr->imu_input; +int i; +int value[6 ]; + +for (i =0 ; i <3 ; i++) {/* there are 3 reports */ + ctlr->timestamp +=5000 ;/* each is 5 ms apart */ +input_event (idev,EV_MSC ,MSC_TIMESTAMP , + ctlr->timestamp);Quoted 4 lines
How sure are you that this is always 5ms? Is there a hardware timestamp somewhere? If I look at our DS4 the timing isn't guaranteed (Bluetooth is lossy) and not all packets even make it.
It appears that the closest thing to a hardware timestamp available is a quickly incrementing 1-byte timer sent with every report. It's only really useful for latency estimation. Each input report includes 3 IMU samples which are 5ms apart for the joy-cons. It's recently come to my attention that the samples may be spaced differently for the pro controller, so I'm going to need to dive into this more anyway. I'm not sure what a great way would be to handle lost reports.
Quoted 27 lines
Quoted 16 lines
drivers/hid/hid-nintendo.c +16
-0+ +/* +* Rather than convert to floats, we adjust by +* calibration factors and then multiply by the default +* scaling values. This way, the consumer only needs to +* divide by the default scale values. +*/ + value[0 ] = (imu_data[i].gyro_x - + ctlr->gyro_cal.offset[0 ]) * +DFLT_GYRO_SCALE / ctlr->gyro_cal.scale[0 ]; + value[1 ] = (imu_data[i].gyro_y - + ctlr->gyro_cal.offset[1 ]) * +DFLT_GYRO_SCALE / ctlr->gyro_cal.scale[1 ]; + value[2 ] = (imu_data[i].gyro_z - + ctlr->gyro_cal.offset[2 ]) * +DFLT_GYRO_SCALE / ctlr->gyro_cal.scale[2 ];drivers/hid/hid-nintendo.c static void joycon_parse_report(struct joycon_ctlr *ctlr, · line 944
Quoted 10 lines
drivers/hid/hid-nintendo.c +10
-0+ + value[3 ] = (imu_data[i].accel_x - + ctlr->accel_cal.offset[0 ]) * +DFLT_ACCEL_SCALE / ctlr->accel_cal.scale[0 ]; + value[4 ] = (imu_data[i].accel_y - + ctlr->accel_cal.offset[1 ]) * +DFLT_ACCEL_SCALE / ctlr->accel_cal.scale[1 ]; + value[5 ] = (imu_data[i].accel_z - + ctlr->accel_cal.offset[2 ]) * +DFLT_ACCEL_SCALE / ctlr->accel_cal.scale[2 ];Quoted 9 lines
Just in case I would double check the precision of your calculations. For DS4 we had similar calculations, but we had a small loss of precision, which ultimately caused major calculation errors. (Applications often use accelerometer + gyro data to calculate an absolute position. This involves integration.. and small errors become big). We had to use the "
mult_frac" macro to restore some of the precision during the calculations. It might be something to double check.That's good to know. I'll look more closely at how it's implemented in hid-sony.
Quoted 6 lines
In addition what orientation are you using for the axes? I need to double check the DS4 datasheets, but I think we were using a "right handed" coordinate system. (So if you make a fist of your right hand with thumb up, the gyro curls around it counter clockwise along the direction of your fingers).
The orientation of the axes (and much more) are documented here:
https://github.com/dekuNukem/Nintendo_Switch_Reverse_Engineering/blob/master/imu_sensor_notes.md
Since the the right vs. left joy-cons have different axes orientations, I assume it's preferable to standardize it all in software to match the orientation of the DS4.Quoted 133 lines
Quoted 133 lines
drivers/hid/hid-nintendo.c +75
-2+ +input_report_abs (idev,ABS_RX , value[0 ]); +input_report_abs (idev,ABS_RY , value[1 ]); +input_report_abs (idev,ABS_RZ , value[2 ]); +input_report_abs (idev,ABS_X , value[3 ]); +input_report_abs (idev,ABS_Y , value[4 ]); +input_report_abs (idev,ABS_Z , value[5 ]); +input_sync (idev); + } + } }static void joycon_rumble_worker (struct work_struct *work)@@ -950,6 +1131,7 @@ static int joycon_input_create(struct joycon_ctlr *ctlr) {struct hid_device *hdev;const char *name; +const char *imu_name;int ret;int i;@@ -958,12 +1140,15 @@ static int joycon_input_create(struct joycon_ctlr *ctlr)switch (hdev->product) {case USB_DEVICE_ID_NINTENDO_PROCON : name ="Nintendo Switch Pro Controller" ; + imu_name ="Nintendo Switch Pro Controller IMU" ;break ;case USB_DEVICE_ID_NINTENDO_JOYCONL : name ="Nintendo Switch Left Joy-Con" ; + imu_name ="Nintendo Switch Left Joy-Con IMU" ;break ;case USB_DEVICE_ID_NINTENDO_JOYCONR : name ="Nintendo Switch Right Joy-Con" ; + imu_name ="Nintendo Switch Right Joy-Con IMU" ;break ;default :/* Should be impossible */ hid_err (hdev,"Invalid hid product\n" );@@ -1029,6 +1214,55 @@ static int joycon_input_create(struct joycon_ctlr *ctlr)if (ret)return ret; +/* configure the imu input device */ + ctlr->imu_input =devm_input_allocate_device (&hdev->dev); +if (!ctlr->imu_input) +return -ENOMEM ; + + ctlr->imu_input->id.bustype = hdev->bus; + ctlr->imu_input->id.vendor = hdev->vendor; + ctlr->imu_input->id.product = hdev->product; + ctlr->imu_input->id.version = hdev->version; + ctlr->imu_input->uniq = ctlr->mac_addr_str; + ctlr->imu_input->name = imu_name; +input_set_drvdata (ctlr->imu_input, ctlr); + +/* configure imu axes */ +input_set_abs_params (ctlr->imu_input,ABS_X , + -JC_MAX_ACCEL_MAG ,JC_MAX_ACCEL_MAG , +JC_ACCEL_FUZZ ,JC_ACCEL_FLAT ); +input_set_abs_params (ctlr->imu_input,ABS_Y , + -JC_MAX_ACCEL_MAG ,JC_MAX_ACCEL_MAG , +JC_ACCEL_FUZZ ,JC_ACCEL_FLAT ); +input_set_abs_params (ctlr->imu_input,ABS_Z , + -JC_MAX_ACCEL_MAG ,JC_MAX_ACCEL_MAG , +JC_ACCEL_FUZZ ,JC_ACCEL_FLAT ); +input_abs_set_res (ctlr->imu_input,ABS_X ,JC_ACCEL_RES ); +input_abs_set_res (ctlr->imu_input,ABS_Y ,JC_ACCEL_RES ); +input_abs_set_res (ctlr->imu_input,ABS_Z ,JC_ACCEL_RES ); + +input_set_abs_params (ctlr->imu_input,ABS_RX , + -JC_MAX_GYRO_MAG ,JC_MAX_GYRO_MAG , +JC_GYRO_FUZZ ,JC_GYRO_FLAT ); +input_set_abs_params (ctlr->imu_input,ABS_RY , + -JC_MAX_GYRO_MAG ,JC_MAX_GYRO_MAG , +JC_GYRO_FUZZ ,JC_GYRO_FLAT ); +input_set_abs_params (ctlr->imu_input,ABS_RZ , + -JC_MAX_GYRO_MAG ,JC_MAX_GYRO_MAG , +JC_GYRO_FUZZ ,JC_GYRO_FLAT ); + +input_abs_set_res (ctlr->imu_input,ABS_RX ,JC_GYRO_RES ); +input_abs_set_res (ctlr->imu_input,ABS_RY ,JC_GYRO_RES ); +input_abs_set_res (ctlr->imu_input,ABS_RZ ,JC_GYRO_RES ); + +__set_bit (EV_MSC , ctlr->imu_input->evbit); +__set_bit (MSC_TIMESTAMP , ctlr->imu_input->mscbit); +__set_bit (INPUT_PROP_ACCELEROMETER , ctlr->imu_input->propbit); + + ret =input_register_device (ctlr->imu_input); +if (ret) +return ret; +return 0 ; }@@ -1288,7 +1522,7 @@ static int joycon_read_mac(struct joycon_ctlr *ctlr) report = (struct joycon_input_report *)ctlr->input_buf;for (i =4 , j =0 ; j <6 ; i++, j++) - ctlr->mac_addr[j] = report->reply.data[i]; + ctlr->mac_addr[j] = report->subcmd_reply.data[i]; ctlr->mac_addr_str = devm_kasprintf(&ctlr->hdev->dev, GFP_KERNEL, "%02X:%02X:%02X:%02X:%02X:%02X",@@ -1343,7 +1577,7 @@ static int joycon_ctlr_handle_event(struct joycon_ctlr *ctlr, u8 *data, data[0] != JC_INPUT_SUBCMD_REPLY)break ; report = (struct joycon_input_report *)data; -if (report->reply.id == ctlr->subcmd_ack_match) +if (report->subcmd_reply.id == ctlr->subcmd_ack_match) match = true;break ;default :@@ -1469,6 +1703,16 @@ static int nintendo_hid_probe(struct hid_device *hdev,hid_warn (hdev,"Analog stick positions may be inaccurate\n" ); } +/* get IMU calibration data, and parse it */ + ret =joycon_request_imu_calibration (ctlr); +if (ret) { +/* +* We can function with default calibration, but it may be +* inaccurate. Provide a warning, and continue on. +*/ +hid_warn (hdev,"Unable to read IMU calibration data\n" ); + } +/* Set the reporting mode to 0x30, which is the full report mode */ ret =joycon_set_report_mode (ctlr);if (ret) {@@ -1483,6 +1727,13 @@ static int nintendo_hid_probe(struct hid_device *hdev, goto err_mutex; }Quoted 13 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
drivers/hid/hid-nintendo.c +5
-0+/* Enable the IMU */ + ret =joycon_enable_imu (ctlr, true); +if (ret) { +hid_err (hdev,"Failed to enable the IMU; ret=%d\n" , ret); + goto err_mutex;Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
drivers/hid/hid-nintendo.c +2
-1+ } + ret =joycon_read_mac (ctlr);if (ret) { hid_err(hdev, "Failed to retrieve controller MAC; ret=%d\n", --2.24.1
--
Daniel Ogorchock
Signature · 1 line
Daniel Ogorchock
Re: [PATCH v10 11/12] HID: nintendo: add IMU support
Hi Daniel,
I tried a Switch Pro controller as well. It indeed is a real HAT, so I think it should be HAT axes. The joycons are really buttons, so use the button d-pad codes there.
Thanks,
Roderick
Quoted 668 lines · Daniel Ogorchock
Does anyone else have an opinion on this? I agree with Carl that the D-pad cannot register opposite direction inputs simultaneously.
Thanks,
DanielQuoted 13 lines · Carl Mueller
Hi Roderick,
The Switch Pro Controller D-pad functions as a D-pad. You cannot push left and right at the same time.
Also, the Switch Pro Controller already functions as a controller when connected with Bluetooth and
using the standard Linux device driver. In this case, the D-pad is registered as a Hat.
I'd prefer if we didn't change this.Quoted 642 lines
Quoted 640 lines · Roderick Colenbrander
Hi Daniel,
Whether to report as HAT or
BTN_DPADdepends on how the hardware works internally. You see the HAT vs buton at the HID report layer (it is quite different), but if you just touch the controller sticks you will also notice. A HAT axis is really like a joystick e.g. with a range -1 to +1. If you hold down e.g. "left" and "right" on a d-pad, you would get "0" back as you are unable to press both left and right at the same time. With d-pad buttons you could press left and right at the same time (depending on design).When I just tried my Switch Pro controller it really felt like the d-pad were buttons. Various other controllers using
BTN_DPADas well for example DualShock 3. Most applications these days should use SDL2, which will hide that.Thanks,
RoderickQuoted 619 lines · Daniel Ogorchock
Hi Carl,
When I was initially implementing the button mappings, I referred to the standard described here:
https://www.kernel.org/doc/html/latest/input/gamepad.htmlIt mentions under the d-pad section that digital inputs should be reported using the BTN_DPAD_* variant. Do the other controllers mentioned report their d-pads as analog values, or has using the ABS_HAT* values become the de facto format for digital inputs as well?
If the latter, I guess it would make sense to go with the crowd for the pro. It just seems a bit odd to report digital inputs as absolute axes, but I'm open to changing it if that's the consensus.
Thanks,
DanielQuoted 580 lines · Carl Mueller
(3rd/final attempt at reply due to list server rejecting message with HTML subpart; sorry about the spam!)
Hi Everyone,
In trying out this driver to use with the Switch Pro Controller, the biggest difficulty I had was that the D-pad is reported as buttons instead of as a Hat. All other controllers that are similar in structure (such as the PS3 and PS4 controllers, the Xbox controllers, and many others) report the D-pad as a Hat. This kind of consistency is needed to avoid lots of software compatibility issues.
I mentioned this to Daniel, and he indicated that he wanted the Switch Pro Controller to behave like the JoyCons, which have buttons instead of a D-pad. Having the JoyCons report the buttons as buttons makes sense, since it's possible to push opposite directions at the same time. However, I don't think that the argument carries over to the Switch Pro Controller, since it has a physically different control.
Again, the consistency with the other controllers that have all the same physical structure and the same types of controls seems more important to me than consistency with a controller that is physically much different. Additionally, many games are already written for use with controllers that look like the Switch Pro Controller, whereas fewer are written for use with Nintendo JoyCons. If we have to cause trouble for one side or the other, let's not cause trouble with the more established side.
Quoted 550 lines · Roderick Colenbrander
Hi Daniel,
Unfortunately there is no public test application at the moment to illustrate these features. I agree something is definitely needed.
I need to see if we can come up with something. One of the test apps we have internally is a 3D cube, which is controllable by controller motion. It of course shows the gyro / accelerometer values, but the position of the cube is tied to the calculated orientation. If something is off you will see weird movements in the cube or drift building up over time etcetera.
Though it would take some time to prepare something. The rest of the patch series looked fine I think, so the IMU parts may need to wait for a next kernel cycle, but all the other plumbing could go in (if others are okay of course).
Thanks,
RoderickQuoted 528 lines · Daniel Ogorchock
Hi Roderick,
Thanks for the feedback. In addition to the inline comments below, do you have any recommendations for test programs that you know work well with hid-sony's gyro implementation? Up to this point I've just been using evtest, which obviously isn't too useful for testing actual functionality of the gyro in an intuitive way.
Quoted 292 lines · Roderick Colenbrander
Hi Daniel,
I had some time to review the motion sensors patch. I have added some feedback inline with your patch below.
Aside from standard feedback on the code, I wanted to have a close look at the accelerometer / gyro data. My team has been doing a lot of work on this (and still is) and I want to make sure any work we do on "user space" software (e.g. Android) automatically works for Joycon as well. The accuracy of the data is very important else applications will make bad decisions. Userspace applications often combine the data of both sensors to calculate a position for position tracking. Inaccurate axes ranges or wrong precision can cause major issues. I may be a bit strict below, but it will just be to prevent headaches for others later on. I use the DS4 as a reference point as it was the first device (aside from Wii early on) where we properly report acceleration and gyro axes with
INPUT_PROP_ACCELEROMETERand we should be consistent with it here else applications could be confused, so we should use similar orientation of axes and resolutions.Thanks,
RoderickQuoted 25 lines · Daniel J. Ogorchock
This patch adds support for the controller's IMU. The accelerometer and gyro data are both provided to userspace using a second input device. The devices can be associated using their uniq value (set to the controller's MAC address).
The majority of this patch's functinoality was provided by Carl Mueller.
Signed-off-by Daniel J. Ogorchock <[email protected]>--- drivers/hid/hid-nintendo.c | 267 +++++++++++++++++++++++++++++++++++-- 1 file changed, 259 insertions(+), 8 deletions(-)drivers/hid/hid-nintendo.c +4
-0@@ -101,12 +101,29 @@ static const u16 JC_CAL_DATA_START = 0x603d;static const u16 JC_CAL_DATA_END =0x604e ;#define JC_CAL_DATA_SIZE (JC_CAL_DATA_END - JC_CAL_DATA_START + 1) +/* SPI storage addresses of IMU factory calibration data */ +static const u16 JC_IMU_CAL_DATA_START =0x6020 ; +static const u16 JC_IMU_CAL_DATA_END =0x6037 ; +#define JC_IMU_CAL_DATA_SIZE \drivers/hid/hid-nintendo.c static const u16 JC_CAL_DATA_START = 0x603d; · line 117
Quoted 10 lines
drivers/hid/hid-nintendo.c +4
-0+ (JC_IMU_CAL_DATA_END -JC_IMU_CAL_DATA_START +1 ) /* The raw analog joystick values will be mapped in terms of this magnitude */static const u16 JC_MAX_STICK_MAG =32767 ;static const u16 JC_STICK_FUZZ =250 ;static const u16 JC_STICK_FLAT =500 ; +/* The accel axes will be mapped in terms of this magnitude */ +static const u16 JC_MAX_ACCEL_MAG =32767 ; +static const u16 JC_ACCEL_RES =4096 ;What does the acceleration resolution equate to? For DS4 we use multiples of 'g'. (We don't know the position on earth, so can't report in m/s^2).
drivers/hid/hid-nintendo.c static const u16 JC_CAL_DATA_START = 0x603d; · line 123
Quoted 6 lines
drivers/hid/hid-nintendo.c +6
-0+static const u16 JC_ACCEL_FUZZ =10 ; +static const u16 JC_ACCEL_FLAT /*= 0*/ ; + +/* The gyro axes will be mapped in terms of this magnitude */ +static const u16 JC_MAX_GYRO_MAG =32767 ; +static const u16 JC_GYRO_RES =13371 /936 ;/* 14 (14.285) */ What does the gyro resolution equate to? For DS4 we report "degrees per second". We should do the same for the joycons, but I don't know how you guys figured out the exact resolution. As I mentioned if it is not accurate, applications will make wrong calculations.
Quoted 24 lines
drivers/hid/hid-nintendo.c +10
-0+static const u16 JC_GYRO_FUZZ =10 ; +static const u16 JC_GYRO_FLAT /*= 0*/ ; +/* frequency/amplitude tables for rumble */ struct joycon_rumble_freq_data {u16 high;@@ -234,6 +251,11 @@ struct joycon_stick_cal {s32 center; }; +struct joycon_imu_cal { +s16 offset[3 ]; +s16 scale[3 ]; +}; +/* * All the controller's button values are stored in a u32. * They can be accessed with bitwise ANDs. @@ -281,6 +303,19 @@ struct joycon_subcmd_reply {u8 data[0 ];/* will be at most 35 bytes */ } __packed; +struct joycon_imu_data { +s16 accel_x;drivers/hid/hid-nintendo.c struct joycon_subcmd_reply { · line 317
Quoted 10 lines
drivers/hid/hid-nintendo.c +10
-0+s16 accel_y; +s16 accel_z; +s16 gyro_x; +s16 gyro_y; +s16 gyro_z; +} __packed; + +struct joycon_imu_report { +struct joycon_imu_data data[3 ]; +} __packed;See comments later on about
imu_data, but you can't directly cast your data buffer to this data type due to endian issues. It may not really make sense to keep the data structure as you need to do manual data fetching.Quoted 6 lines
drivers/hid/hid-nintendo.c +1
-0+struct joycon_input_report {u8 id;u8 timer;@@ -290,11 +325,10 @@ struct joycon_input_report {u8 right_stick[3 ];drivers/hid/hid-nintendo.c struct joycon_input_report { · line 330
Quoted 10 lines
drivers/hid/hid-nintendo.c +3
-5u8 vibrator_report; -/* -* If support for firmware updates, gyroscope data, and/or NFC/IR -* are added in the future, this can be swapped for a union. -*/ -struct joycon_subcmd_reply reply; +union { +struct joycon_subcmd_reply subcmd_reply; +struct joycon_imu_report imu_report;maybe add a raw byte array to this union. Could help for parsing the imu data.
Quoted 50 lines
drivers/hid/hid-nintendo.c +21
-1+ }; } __packed;#define JC_MAX_RESP_SIZE (sizeof(struct joycon_input_report) + 35)@@ -334,6 +368,9 @@ struct joycon_ctlr {struct joycon_stick_cal right_stick_cal_x;struct joycon_stick_cal right_stick_cal_y; +struct joycon_imu_cal accel_cal; +struct joycon_imu_cal gyro_cal; +/* power supply data */ struct power_supply *battery;struct power_supply_desc battery_desc;@@ -352,6 +389,10 @@ struct joycon_ctlr {u16 rumble_lh_freq;u16 rumble_rl_freq;u16 rumble_rh_freq; + +/* imu */ +struct input_dev *imu_input; +int timestamp; };static int __joycon_hid_send (struct hid_device *hdev,u8 *data,size_t len)@@ -547,7 +588,7 @@ static int joycon_request_calibration(struct joycon_ctlr *ctlr) } report = (struct joycon_input_report *)ctlr->input_buf; - raw_cal = &report->reply.data[5 ]; + raw_cal = &report->subcmd_reply.data[5 ];/* left stick calibration parsing */ cal_x = &ctlr->left_stick_cal_x;@@ -601,6 +642,85 @@ static int joycon_request_calibration(struct joycon_ctlr *ctlr)return 0 ; } +static const s16 DFLT_ACCEL_OFFSET /*= 0*/ ; +static const s16 DFLT_ACCEL_SCALE =16384 ; +static const s16 DFLT_GYRO_OFFSET /*= 0*/ ; +static const s16 DFLT_GYRO_SCALE =13371 ; +static int joycon_request_imu_calibration (struct joycon_ctlr *ctlr) +{ +struct joycon_subcmd_request *req; +u8 buffer[sizeof (*req) +5 ] = {0 }; +struct joycon_input_report *report; +u8 *data; +u8 *raw_cal; +int ret;drivers/hid/hid-nintendo.c static int joycon_request_calibration(struct joycon_ctlr *ctlr) · line 666
Quoted 10 lines
drivers/hid/hid-nintendo.c +10
-0+int i; + +/* request IMU calibration data */ + req = (struct joycon_subcmd_request *)buffer; + req->subcmd_id =JC_SUBCMD_SPI_FLASH_READ ; + data = req->data; + data[0 ] =0xFF &JC_IMU_CAL_DATA_START ; + data[1 ] =0xFF & (JC_IMU_CAL_DATA_START >>8 ); + data[2 ] =0xFF & (JC_IMU_CAL_DATA_START >>16 ); + data[3 ] =0xFF & (JC_IMU_CAL_DATA_START >>24 );Maybe use
put_unaligned_le32? I think it allows you to avoid doing all these calculations yourself:put_unaligned_le32(JC_IMU_CAL_DATA_START, data);Quoted 24 lines
drivers/hid/hid-nintendo.c +24
-0+ data[4 ] =JC_IMU_CAL_DATA_SIZE ; + +hid_dbg (ctlr->hdev,"requesting IMU cal data\n" ); + ret =joycon_send_subcmd (ctlr, req,5 ,HZ ); + +if (ret) { +hid_warn (ctlr->hdev, +"Failed to read IMU cal, using defaults; ret=%d\n" , + ret); + +for (i =0 ; i <3 ; i++) { + ctlr->accel_cal.offset[i] =DFLT_ACCEL_OFFSET ; + ctlr->accel_cal.scale[i] =DFLT_ACCEL_SCALE ; + ctlr->gyro_cal.offset[i] =DFLT_GYRO_OFFSET ; + ctlr->gyro_cal.scale[i] =DFLT_GYRO_SCALE ; + } +return ret; + } + + report = (struct joycon_input_report *)ctlr->input_buf; + raw_cal = &report->subcmd_reply.data[5 ]; + +/* IMU calibration parsing */ +for (i =0 ; i <3 ; i++) {drivers/hid/hid-nintendo.c static int joycon_request_calibration(struct joycon_ctlr *ctlr) · line 700
Quoted 10 lines
drivers/hid/hid-nintendo.c +10
-0+int j = i *2 ; + + ctlr->accel_cal.offset[i] = raw_cal[j +0 ] | + ((s16 )raw_cal[j +1 ] <<8 ); + ctlr->accel_cal.scale[i] = raw_cal[j +6 ] | + ((s16 )raw_cal[j +7 ] <<8 ); + ctlr->gyro_cal.offset[i] = raw_cal[j +12 ] | + ((s16 )raw_cal[j +13 ] <<8 ); + ctlr->gyro_cal.scale[i] = raw_cal[j +18 ] | + ((s16 )raw_cal[j +19 ] <<8 );
get_unaligned_le16instead of doing your own bitshifts?Quoted 47 lines
drivers/hid/hid-nintendo.c +36
-0+ } + +hid_dbg (ctlr->hdev,"IMU calibration:\n" +"a_o[0]=%d a_o[1]=%d a_o[2]=%d\n" +"a_s[0]=%d a_s[1]=%d a_s[2]=%d\n" +"g_o[0]=%d g_o[1]=%d g_o[2]=%d\n" +"g_s[0]=%d g_s[1]=%d g_s[2]=%d\n" , + ctlr->accel_cal.offset[0 ], + ctlr->accel_cal.offset[1 ], + ctlr->accel_cal.offset[2 ], + ctlr->accel_cal.scale[0 ], + ctlr->accel_cal.scale[1 ], + ctlr->accel_cal.scale[2 ], + ctlr->gyro_cal.offset[0 ], + ctlr->gyro_cal.offset[1 ], + ctlr->gyro_cal.offset[2 ], + ctlr->gyro_cal.scale[0 ], + ctlr->gyro_cal.scale[1 ], + ctlr->gyro_cal.scale[2 ]); + +return 0 ; +} +static int joycon_set_report_mode (struct joycon_ctlr *ctlr) {struct joycon_subcmd_request *req;@@ -627,6 +747,19 @@ static int joycon_enable_rumble(struct joycon_ctlr *ctlr, bool enable)return joycon_send_subcmd (ctlr, req,1 ,HZ /4 ); } +static int joycon_enable_imu (struct joycon_ctlr *ctlr,bool enable) +{ +struct joycon_subcmd_request *req; +u8 buffer[sizeof (*req) +1 ] = {0 }; + + req = (struct joycon_subcmd_request *)buffer; + req->subcmd_id =JC_SUBCMD_ENABLE_IMU ; + req->data[0 ] = enable ?0x01 :0x00 ; + +hid_dbg (ctlr->hdev,"%s IMU\n" , enable ?"enabling" :"disabling" ); +return joycon_send_subcmd (ctlr, req,1 ,HZ ); +} +static s32 joycon_map_stick_val (struct joycon_stick_cal *cal,s32 val) {s32 center = cal->center;drivers/hid/hid-nintendo.c static void joycon_parse_report(struct joycon_ctlr *ctlr, · line 910
Quoted 7 lines
drivers/hid/hid-nintendo.c +4
-0@@ -771,6 +904,54 @@ static void joycon_parse_report(struct joycon_ctlr *ctlr,spin_unlock_irqrestore (&ctlr->lock, flags);wake_up (&ctlr->wait); } + +/* parse IMU data if present */ +if (rep->id ==JC_INPUT_IMU_DATA ) { +struct joycon_imu_data *imu_data = rep->imu_report.data;Quoted 7 lines
I don't think you can directly covert your input report data to
imu_data. Until now you have been lucky enough (based on a quick glance of the the other patches) that your data are single bytes. However, this data seems to be a bunch of s16's. In other words you have to deal with endianess issues. You need to useget_unaligned_le16to retrieve data from your raw byte buffer. See other HID drivers for reference.Ah, good point. I'd overlooked that entirely.
Quoted 16 lines
Since you will have to use
get_unaligned_le16, it probably won't make much sense to really have ajoycon_imu_datastructure. Maybe extend yourinput_bufferunion with raw bytes and in this case just use raw bytes. Each of your loop iterations below just grab the values from the buffer and store them in a variable.drivers/hid/hid-nintendo.c static void joycon_parse_report(struct joycon_ctlr *ctlr, · line 918
Quoted 8 lines
drivers/hid/hid-nintendo.c +8
-0+struct input_dev *idev = ctlr->imu_input; +int i; +int value[6 ]; + +for (i =0 ; i <3 ; i++) {/* there are 3 reports */ + ctlr->timestamp +=5000 ;/* each is 5 ms apart */ +input_event (idev,EV_MSC ,MSC_TIMESTAMP , + ctlr->timestamp);Quoted 4 lines
How sure are you that this is always 5ms? Is there a hardware timestamp somewhere? If I look at our DS4 the timing isn't guaranteed (Bluetooth is lossy) and not all packets even make it.
It appears that the closest thing to a hardware timestamp available is a quickly incrementing 1-byte timer sent with every report. It's only really useful for latency estimation. Each input report includes 3 IMU samples which are 5ms apart for the joy-cons. It's recently come to my attention that the samples may be spaced differently for the pro controller, so I'm going to need to dive into this more anyway. I'm not sure what a great way would be to handle lost reports.
Quoted 27 lines
Quoted 16 lines
drivers/hid/hid-nintendo.c +16
-0+ +/* +* Rather than convert to floats, we adjust by +* calibration factors and then multiply by the default +* scaling values. This way, the consumer only needs to +* divide by the default scale values. +*/ + value[0 ] = (imu_data[i].gyro_x - + ctlr->gyro_cal.offset[0 ]) * +DFLT_GYRO_SCALE / ctlr->gyro_cal.scale[0 ]; + value[1 ] = (imu_data[i].gyro_y - + ctlr->gyro_cal.offset[1 ]) * +DFLT_GYRO_SCALE / ctlr->gyro_cal.scale[1 ]; + value[2 ] = (imu_data[i].gyro_z - + ctlr->gyro_cal.offset[2 ]) * +DFLT_GYRO_SCALE / ctlr->gyro_cal.scale[2 ];drivers/hid/hid-nintendo.c static void joycon_parse_report(struct joycon_ctlr *ctlr, · line 944
Quoted 10 lines
drivers/hid/hid-nintendo.c +10
-0+ + value[3 ] = (imu_data[i].accel_x - + ctlr->accel_cal.offset[0 ]) * +DFLT_ACCEL_SCALE / ctlr->accel_cal.scale[0 ]; + value[4 ] = (imu_data[i].accel_y - + ctlr->accel_cal.offset[1 ]) * +DFLT_ACCEL_SCALE / ctlr->accel_cal.scale[1 ]; + value[5 ] = (imu_data[i].accel_z - + ctlr->accel_cal.offset[2 ]) * +DFLT_ACCEL_SCALE / ctlr->accel_cal.scale[2 ];Quoted 9 lines
Just in case I would double check the precision of your calculations. For DS4 we had similar calculations, but we had a small loss of precision, which ultimately caused major calculation errors. (Applications often use accelerometer + gyro data to calculate an absolute position. This involves integration.. and small errors become big). We had to use the "
mult_frac" macro to restore some of the precision during the calculations. It might be something to double check.That's good to know. I'll look more closely at how it's implemented in hid-sony.
Quoted 6 lines
In addition what orientation are you using for the axes? I need to double check the DS4 datasheets, but I think we were using a "right handed" coordinate system. (So if you make a fist of your right hand with thumb up, the gyro curls around it counter clockwise along the direction of your fingers).
The orientation of the axes (and much more) are documented here:
https://github.com/dekuNukem/Nintendo_Switch_Reverse_Engineering/blob/master/imu_sensor_notes.md
Since the the right vs. left joy-cons have different axes orientations, I assume it's preferable to standardize it all in software to match the orientation of the DS4.Quoted 133 lines
Quoted 133 lines
drivers/hid/hid-nintendo.c +75
-2+ +input_report_abs (idev,ABS_RX , value[0 ]); +input_report_abs (idev,ABS_RY , value[1 ]); +input_report_abs (idev,ABS_RZ , value[2 ]); +input_report_abs (idev,ABS_X , value[3 ]); +input_report_abs (idev,ABS_Y , value[4 ]); +input_report_abs (idev,ABS_Z , value[5 ]); +input_sync (idev); + } + } }static void joycon_rumble_worker (struct work_struct *work)@@ -950,6 +1131,7 @@ static int joycon_input_create(struct joycon_ctlr *ctlr) {struct hid_device *hdev;const char *name; +const char *imu_name;int ret;int i;@@ -958,12 +1140,15 @@ static int joycon_input_create(struct joycon_ctlr *ctlr)switch (hdev->product) {case USB_DEVICE_ID_NINTENDO_PROCON : name ="Nintendo Switch Pro Controller" ; + imu_name ="Nintendo Switch Pro Controller IMU" ;break ;case USB_DEVICE_ID_NINTENDO_JOYCONL : name ="Nintendo Switch Left Joy-Con" ; + imu_name ="Nintendo Switch Left Joy-Con IMU" ;break ;case USB_DEVICE_ID_NINTENDO_JOYCONR : name ="Nintendo Switch Right Joy-Con" ; + imu_name ="Nintendo Switch Right Joy-Con IMU" ;break ;default :/* Should be impossible */ hid_err (hdev,"Invalid hid product\n" );@@ -1029,6 +1214,55 @@ static int joycon_input_create(struct joycon_ctlr *ctlr)if (ret)return ret; +/* configure the imu input device */ + ctlr->imu_input =devm_input_allocate_device (&hdev->dev); +if (!ctlr->imu_input) +return -ENOMEM ; + + ctlr->imu_input->id.bustype = hdev->bus; + ctlr->imu_input->id.vendor = hdev->vendor; + ctlr->imu_input->id.product = hdev->product; + ctlr->imu_input->id.version = hdev->version; + ctlr->imu_input->uniq = ctlr->mac_addr_str; + ctlr->imu_input->name = imu_name; +input_set_drvdata (ctlr->imu_input, ctlr); + +/* configure imu axes */ +input_set_abs_params (ctlr->imu_input,ABS_X , + -JC_MAX_ACCEL_MAG ,JC_MAX_ACCEL_MAG , +JC_ACCEL_FUZZ ,JC_ACCEL_FLAT ); +input_set_abs_params (ctlr->imu_input,ABS_Y , + -JC_MAX_ACCEL_MAG ,JC_MAX_ACCEL_MAG , +JC_ACCEL_FUZZ ,JC_ACCEL_FLAT ); +input_set_abs_params (ctlr->imu_input,ABS_Z , + -JC_MAX_ACCEL_MAG ,JC_MAX_ACCEL_MAG , +JC_ACCEL_FUZZ ,JC_ACCEL_FLAT ); +input_abs_set_res (ctlr->imu_input,ABS_X ,JC_ACCEL_RES ); +input_abs_set_res (ctlr->imu_input,ABS_Y ,JC_ACCEL_RES ); +input_abs_set_res (ctlr->imu_input,ABS_Z ,JC_ACCEL_RES ); + +input_set_abs_params (ctlr->imu_input,ABS_RX , + -JC_MAX_GYRO_MAG ,JC_MAX_GYRO_MAG , +JC_GYRO_FUZZ ,JC_GYRO_FLAT ); +input_set_abs_params (ctlr->imu_input,ABS_RY , + -JC_MAX_GYRO_MAG ,JC_MAX_GYRO_MAG , +JC_GYRO_FUZZ ,JC_GYRO_FLAT ); +input_set_abs_params (ctlr->imu_input,ABS_RZ , + -JC_MAX_GYRO_MAG ,JC_MAX_GYRO_MAG , +JC_GYRO_FUZZ ,JC_GYRO_FLAT ); + +input_abs_set_res (ctlr->imu_input,ABS_RX ,JC_GYRO_RES ); +input_abs_set_res (ctlr->imu_input,ABS_RY ,JC_GYRO_RES ); +input_abs_set_res (ctlr->imu_input,ABS_RZ ,JC_GYRO_RES ); + +__set_bit (EV_MSC , ctlr->imu_input->evbit); +__set_bit (MSC_TIMESTAMP , ctlr->imu_input->mscbit); +__set_bit (INPUT_PROP_ACCELEROMETER , ctlr->imu_input->propbit); + + ret =input_register_device (ctlr->imu_input); +if (ret) +return ret; +return 0 ; }@@ -1288,7 +1522,7 @@ static int joycon_read_mac(struct joycon_ctlr *ctlr) report = (struct joycon_input_report *)ctlr->input_buf;for (i =4 , j =0 ; j <6 ; i++, j++) - ctlr->mac_addr[j] = report->reply.data[i]; + ctlr->mac_addr[j] = report->subcmd_reply.data[i]; ctlr->mac_addr_str = devm_kasprintf(&ctlr->hdev->dev, GFP_KERNEL, "%02X:%02X:%02X:%02X:%02X:%02X",@@ -1343,7 +1577,7 @@ static int joycon_ctlr_handle_event(struct joycon_ctlr *ctlr, u8 *data, data[0] != JC_INPUT_SUBCMD_REPLY)break ; report = (struct joycon_input_report *)data; -if (report->reply.id == ctlr->subcmd_ack_match) +if (report->subcmd_reply.id == ctlr->subcmd_ack_match) match = true;break ;default :@@ -1469,6 +1703,16 @@ static int nintendo_hid_probe(struct hid_device *hdev,hid_warn (hdev,"Analog stick positions may be inaccurate\n" ); } +/* get IMU calibration data, and parse it */ + ret =joycon_request_imu_calibration (ctlr); +if (ret) { +/* +* We can function with default calibration, but it may be +* inaccurate. Provide a warning, and continue on. +*/ +hid_warn (hdev,"Unable to read IMU calibration data\n" ); + } +/* Set the reporting mode to 0x30, which is the full report mode */ ret =joycon_set_report_mode (ctlr);if (ret) {@@ -1483,6 +1727,13 @@ static int nintendo_hid_probe(struct hid_device *hdev, goto err_mutex; }Quoted 13 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
drivers/hid/hid-nintendo.c +5
-0+/* Enable the IMU */ + ret =joycon_enable_imu (ctlr, true); +if (ret) { +hid_err (hdev,"Failed to enable the IMU; ret=%d\n" , ret); + goto err_mutex;Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
drivers/hid/hid-nintendo.c +2
-1+ } + ret =joycon_read_mac (ctlr);if (ret) { hid_err(hdev, "Failed to retrieve controller MAC; ret=%d\n", --2.24.1
--
Daniel Ogorchock--
Daniel Ogorchock
Daniel Ogorchock
maintainerRe: [PATCH v10 11/12] HID: nintendo: add IMU support
In reply to Roderick ColenbranderHi Roderick,
Quoted 13 lines · Roderick Colenbrander
Hi Daniel,
Unfortunately there is no public test application at the moment to illustrate these features. I agree something is definitely needed.
I need to see if we can come up with something. One of the test apps we have internally is a 3D cube, which is controllable by controller motion. It of course shows the gyro / accelerometer values, but the position of the cube is tied to the calculated orientation. If something is off you will see weird movements in the cube or drift building up over time etcetera.
Quoted 4 lines
Though it would take some time to prepare something. The rest of the patch series looked fine I think, so the IMU parts may need to wait for a next kernel cycle, but all the other plumbing could go in (if others are okay of course).
I agree with postponing the IMU patch for later. I'll probably rebase things so support for the charging grip can be added independent of IMU support.
Thanks,
Daniel
Quoted 546 lines
Thanks,
RoderickQuoted 541 lines · Daniel Ogorchock
Hi Roderick,
Thanks for the feedback. In addition to the inline comments below, do you have any recommendations for test programs that you know work well with hid-sony's gyro implementation? Up to this point I've just been using evtest, which obviously isn't too useful for testing actual functionality of the gyro in an intuitive way.
Quoted 292 lines · Roderick Colenbrander
Hi Daniel,
I had some time to review the motion sensors patch. I have added some feedback inline with your patch below.
Aside from standard feedback on the code, I wanted to have a close look at the accelerometer / gyro data. My team has been doing a lot of work on this (and still is) and I want to make sure any work we do on "user space" software (e.g. Android) automatically works for Joycon as well. The accuracy of the data is very important else applications will make bad decisions. Userspace applications often combine the data of both sensors to calculate a position for position tracking. Inaccurate axes ranges or wrong precision can cause major issues. I may be a bit strict below, but it will just be to prevent headaches for others later on. I use the DS4 as a reference point as it was the first device (aside from Wii early on) where we properly report acceleration and gyro axes with
INPUT_PROP_ACCELEROMETERand we should be consistent with it here else applications could be confused, so we should use similar orientation of axes and resolutions.Thanks,
RoderickQuoted 25 lines · Daniel J. Ogorchock
This patch adds support for the controller's IMU. The accelerometer and gyro data are both provided to userspace using a second input device. The devices can be associated using their uniq value (set to the controller's MAC address).
The majority of this patch's functinoality was provided by Carl Mueller.
Signed-off-by Daniel J. Ogorchock <[email protected]>--- drivers/hid/hid-nintendo.c | 267 +++++++++++++++++++++++++++++++++++-- 1 file changed, 259 insertions(+), 8 deletions(-)drivers/hid/hid-nintendo.c +4
-0@@ -101,12 +101,29 @@ static const u16 JC_CAL_DATA_START = 0x603d;static const u16 JC_CAL_DATA_END =0x604e ;#define JC_CAL_DATA_SIZE (JC_CAL_DATA_END - JC_CAL_DATA_START + 1) +/* SPI storage addresses of IMU factory calibration data */ +static const u16 JC_IMU_CAL_DATA_START =0x6020 ; +static const u16 JC_IMU_CAL_DATA_END =0x6037 ; +#define JC_IMU_CAL_DATA_SIZE \drivers/hid/hid-nintendo.c static const u16 JC_CAL_DATA_START = 0x603d; · line 117
Quoted 10 lines
drivers/hid/hid-nintendo.c +4
-0+ (JC_IMU_CAL_DATA_END -JC_IMU_CAL_DATA_START +1 ) /* The raw analog joystick values will be mapped in terms of this magnitude */static const u16 JC_MAX_STICK_MAG =32767 ;static const u16 JC_STICK_FUZZ =250 ;static const u16 JC_STICK_FLAT =500 ; +/* The accel axes will be mapped in terms of this magnitude */ +static const u16 JC_MAX_ACCEL_MAG =32767 ; +static const u16 JC_ACCEL_RES =4096 ;What does the acceleration resolution equate to? For DS4 we use multiples of 'g'. (We don't know the position on earth, so can't report in m/s^2).
drivers/hid/hid-nintendo.c static const u16 JC_CAL_DATA_START = 0x603d; · line 123
Quoted 6 lines
drivers/hid/hid-nintendo.c +6
-0+static const u16 JC_ACCEL_FUZZ =10 ; +static const u16 JC_ACCEL_FLAT /*= 0*/ ; + +/* The gyro axes will be mapped in terms of this magnitude */ +static const u16 JC_MAX_GYRO_MAG =32767 ; +static const u16 JC_GYRO_RES =13371 /936 ;/* 14 (14.285) */ What does the gyro resolution equate to? For DS4 we report "degrees per second". We should do the same for the joycons, but I don't know how you guys figured out the exact resolution. As I mentioned if it is not accurate, applications will make wrong calculations.
Quoted 24 lines
drivers/hid/hid-nintendo.c +10
-0+static const u16 JC_GYRO_FUZZ =10 ; +static const u16 JC_GYRO_FLAT /*= 0*/ ; +/* frequency/amplitude tables for rumble */ struct joycon_rumble_freq_data {u16 high;@@ -234,6 +251,11 @@ struct joycon_stick_cal {s32 center; }; +struct joycon_imu_cal { +s16 offset[3 ]; +s16 scale[3 ]; +}; +/* * All the controller's button values are stored in a u32. * They can be accessed with bitwise ANDs. @@ -281,6 +303,19 @@ struct joycon_subcmd_reply {u8 data[0 ];/* will be at most 35 bytes */ } __packed; +struct joycon_imu_data { +s16 accel_x;drivers/hid/hid-nintendo.c struct joycon_subcmd_reply { · line 317
Quoted 10 lines
drivers/hid/hid-nintendo.c +10
-0+s16 accel_y; +s16 accel_z; +s16 gyro_x; +s16 gyro_y; +s16 gyro_z; +} __packed; + +struct joycon_imu_report { +struct joycon_imu_data data[3 ]; +} __packed;See comments later on about
imu_data, but you can't directly cast your data buffer to this data type due to endian issues. It may not really make sense to keep the data structure as you need to do manual data fetching.Quoted 6 lines
drivers/hid/hid-nintendo.c +1
-0+struct joycon_input_report {u8 id;u8 timer;@@ -290,11 +325,10 @@ struct joycon_input_report {u8 right_stick[3 ];drivers/hid/hid-nintendo.c struct joycon_input_report { · line 330
Quoted 10 lines
drivers/hid/hid-nintendo.c +3
-5u8 vibrator_report; -/* -* If support for firmware updates, gyroscope data, and/or NFC/IR -* are added in the future, this can be swapped for a union. -*/ -struct joycon_subcmd_reply reply; +union { +struct joycon_subcmd_reply subcmd_reply; +struct joycon_imu_report imu_report;maybe add a raw byte array to this union. Could help for parsing the imu data.
Quoted 50 lines
drivers/hid/hid-nintendo.c +21
-1+ }; } __packed;#define JC_MAX_RESP_SIZE (sizeof(struct joycon_input_report) + 35)@@ -334,6 +368,9 @@ struct joycon_ctlr {struct joycon_stick_cal right_stick_cal_x;struct joycon_stick_cal right_stick_cal_y; +struct joycon_imu_cal accel_cal; +struct joycon_imu_cal gyro_cal; +/* power supply data */ struct power_supply *battery;struct power_supply_desc battery_desc;@@ -352,6 +389,10 @@ struct joycon_ctlr {u16 rumble_lh_freq;u16 rumble_rl_freq;u16 rumble_rh_freq; + +/* imu */ +struct input_dev *imu_input; +int timestamp; };static int __joycon_hid_send (struct hid_device *hdev,u8 *data,size_t len)@@ -547,7 +588,7 @@ static int joycon_request_calibration(struct joycon_ctlr *ctlr) } report = (struct joycon_input_report *)ctlr->input_buf; - raw_cal = &report->reply.data[5 ]; + raw_cal = &report->subcmd_reply.data[5 ];/* left stick calibration parsing */ cal_x = &ctlr->left_stick_cal_x;@@ -601,6 +642,85 @@ static int joycon_request_calibration(struct joycon_ctlr *ctlr)return 0 ; } +static const s16 DFLT_ACCEL_OFFSET /*= 0*/ ; +static const s16 DFLT_ACCEL_SCALE =16384 ; +static const s16 DFLT_GYRO_OFFSET /*= 0*/ ; +static const s16 DFLT_GYRO_SCALE =13371 ; +static int joycon_request_imu_calibration (struct joycon_ctlr *ctlr) +{ +struct joycon_subcmd_request *req; +u8 buffer[sizeof (*req) +5 ] = {0 }; +struct joycon_input_report *report; +u8 *data; +u8 *raw_cal; +int ret;drivers/hid/hid-nintendo.c static int joycon_request_calibration(struct joycon_ctlr *ctlr) · line 666
Quoted 10 lines
drivers/hid/hid-nintendo.c +10
-0+int i; + +/* request IMU calibration data */ + req = (struct joycon_subcmd_request *)buffer; + req->subcmd_id =JC_SUBCMD_SPI_FLASH_READ ; + data = req->data; + data[0 ] =0xFF &JC_IMU_CAL_DATA_START ; + data[1 ] =0xFF & (JC_IMU_CAL_DATA_START >>8 ); + data[2 ] =0xFF & (JC_IMU_CAL_DATA_START >>16 ); + data[3 ] =0xFF & (JC_IMU_CAL_DATA_START >>24 );Maybe use
put_unaligned_le32? I think it allows you to avoid doing all these calculations yourself:put_unaligned_le32(JC_IMU_CAL_DATA_START, data);Quoted 24 lines
drivers/hid/hid-nintendo.c +24
-0+ data[4 ] =JC_IMU_CAL_DATA_SIZE ; + +hid_dbg (ctlr->hdev,"requesting IMU cal data\n" ); + ret =joycon_send_subcmd (ctlr, req,5 ,HZ ); + +if (ret) { +hid_warn (ctlr->hdev, +"Failed to read IMU cal, using defaults; ret=%d\n" , + ret); + +for (i =0 ; i <3 ; i++) { + ctlr->accel_cal.offset[i] =DFLT_ACCEL_OFFSET ; + ctlr->accel_cal.scale[i] =DFLT_ACCEL_SCALE ; + ctlr->gyro_cal.offset[i] =DFLT_GYRO_OFFSET ; + ctlr->gyro_cal.scale[i] =DFLT_GYRO_SCALE ; + } +return ret; + } + + report = (struct joycon_input_report *)ctlr->input_buf; + raw_cal = &report->subcmd_reply.data[5 ]; + +/* IMU calibration parsing */ +for (i =0 ; i <3 ; i++) {drivers/hid/hid-nintendo.c static int joycon_request_calibration(struct joycon_ctlr *ctlr) · line 700
Quoted 10 lines
drivers/hid/hid-nintendo.c +10
-0+int j = i *2 ; + + ctlr->accel_cal.offset[i] = raw_cal[j +0 ] | + ((s16 )raw_cal[j +1 ] <<8 ); + ctlr->accel_cal.scale[i] = raw_cal[j +6 ] | + ((s16 )raw_cal[j +7 ] <<8 ); + ctlr->gyro_cal.offset[i] = raw_cal[j +12 ] | + ((s16 )raw_cal[j +13 ] <<8 ); + ctlr->gyro_cal.scale[i] = raw_cal[j +18 ] | + ((s16 )raw_cal[j +19 ] <<8 );
get_unaligned_le16instead of doing your own bitshifts?Quoted 47 lines
drivers/hid/hid-nintendo.c +36
-0+ } + +hid_dbg (ctlr->hdev,"IMU calibration:\n" +"a_o[0]=%d a_o[1]=%d a_o[2]=%d\n" +"a_s[0]=%d a_s[1]=%d a_s[2]=%d\n" +"g_o[0]=%d g_o[1]=%d g_o[2]=%d\n" +"g_s[0]=%d g_s[1]=%d g_s[2]=%d\n" , + ctlr->accel_cal.offset[0 ], + ctlr->accel_cal.offset[1 ], + ctlr->accel_cal.offset[2 ], + ctlr->accel_cal.scale[0 ], + ctlr->accel_cal.scale[1 ], + ctlr->accel_cal.scale[2 ], + ctlr->gyro_cal.offset[0 ], + ctlr->gyro_cal.offset[1 ], + ctlr->gyro_cal.offset[2 ], + ctlr->gyro_cal.scale[0 ], + ctlr->gyro_cal.scale[1 ], + ctlr->gyro_cal.scale[2 ]); + +return 0 ; +} +static int joycon_set_report_mode (struct joycon_ctlr *ctlr) {struct joycon_subcmd_request *req;@@ -627,6 +747,19 @@ static int joycon_enable_rumble(struct joycon_ctlr *ctlr, bool enable)return joycon_send_subcmd (ctlr, req,1 ,HZ /4 ); } +static int joycon_enable_imu (struct joycon_ctlr *ctlr,bool enable) +{ +struct joycon_subcmd_request *req; +u8 buffer[sizeof (*req) +1 ] = {0 }; + + req = (struct joycon_subcmd_request *)buffer; + req->subcmd_id =JC_SUBCMD_ENABLE_IMU ; + req->data[0 ] = enable ?0x01 :0x00 ; + +hid_dbg (ctlr->hdev,"%s IMU\n" , enable ?"enabling" :"disabling" ); +return joycon_send_subcmd (ctlr, req,1 ,HZ ); +} +static s32 joycon_map_stick_val (struct joycon_stick_cal *cal,s32 val) {s32 center = cal->center;drivers/hid/hid-nintendo.c static void joycon_parse_report(struct joycon_ctlr *ctlr, · line 910
Quoted 7 lines
drivers/hid/hid-nintendo.c +4
-0@@ -771,6 +904,54 @@ static void joycon_parse_report(struct joycon_ctlr *ctlr,spin_unlock_irqrestore (&ctlr->lock, flags);wake_up (&ctlr->wait); } + +/* parse IMU data if present */ +if (rep->id ==JC_INPUT_IMU_DATA ) { +struct joycon_imu_data *imu_data = rep->imu_report.data;Quoted 7 lines
I don't think you can directly covert your input report data to
imu_data. Until now you have been lucky enough (based on a quick glance of the the other patches) that your data are single bytes. However, this data seems to be a bunch of s16's. In other words you have to deal with endianess issues. You need to useget_unaligned_le16to retrieve data from your raw byte buffer. See other HID drivers for reference.Ah, good point. I'd overlooked that entirely.
Quoted 16 lines
Since you will have to use
get_unaligned_le16, it probably won't make much sense to really have ajoycon_imu_datastructure. Maybe extend yourinput_bufferunion with raw bytes and in this case just use raw bytes. Each of your loop iterations below just grab the values from the buffer and store them in a variable.drivers/hid/hid-nintendo.c static void joycon_parse_report(struct joycon_ctlr *ctlr, · line 918
Quoted 8 lines
drivers/hid/hid-nintendo.c +8
-0+struct input_dev *idev = ctlr->imu_input; +int i; +int value[6 ]; + +for (i =0 ; i <3 ; i++) {/* there are 3 reports */ + ctlr->timestamp +=5000 ;/* each is 5 ms apart */ +input_event (idev,EV_MSC ,MSC_TIMESTAMP , + ctlr->timestamp);Quoted 4 lines
How sure are you that this is always 5ms? Is there a hardware timestamp somewhere? If I look at our DS4 the timing isn't guaranteed (Bluetooth is lossy) and not all packets even make it.
It appears that the closest thing to a hardware timestamp available is a quickly incrementing 1-byte timer sent with every report. It's only really useful for latency estimation. Each input report includes 3 IMU samples which are 5ms apart for the joy-cons. It's recently come to my attention that the samples may be spaced differently for the pro controller, so I'm going to need to dive into this more anyway. I'm not sure what a great way would be to handle lost reports.
Quoted 27 lines
Quoted 16 lines
drivers/hid/hid-nintendo.c +16
-0+ +/* +* Rather than convert to floats, we adjust by +* calibration factors and then multiply by the default +* scaling values. This way, the consumer only needs to +* divide by the default scale values. +*/ + value[0 ] = (imu_data[i].gyro_x - + ctlr->gyro_cal.offset[0 ]) * +DFLT_GYRO_SCALE / ctlr->gyro_cal.scale[0 ]; + value[1 ] = (imu_data[i].gyro_y - + ctlr->gyro_cal.offset[1 ]) * +DFLT_GYRO_SCALE / ctlr->gyro_cal.scale[1 ]; + value[2 ] = (imu_data[i].gyro_z - + ctlr->gyro_cal.offset[2 ]) * +DFLT_GYRO_SCALE / ctlr->gyro_cal.scale[2 ];drivers/hid/hid-nintendo.c static void joycon_parse_report(struct joycon_ctlr *ctlr, · line 944
Quoted 10 lines
drivers/hid/hid-nintendo.c +10
-0+ + value[3 ] = (imu_data[i].accel_x - + ctlr->accel_cal.offset[0 ]) * +DFLT_ACCEL_SCALE / ctlr->accel_cal.scale[0 ]; + value[4 ] = (imu_data[i].accel_y - + ctlr->accel_cal.offset[1 ]) * +DFLT_ACCEL_SCALE / ctlr->accel_cal.scale[1 ]; + value[5 ] = (imu_data[i].accel_z - + ctlr->accel_cal.offset[2 ]) * +DFLT_ACCEL_SCALE / ctlr->accel_cal.scale[2 ];Quoted 9 lines
Just in case I would double check the precision of your calculations. For DS4 we had similar calculations, but we had a small loss of precision, which ultimately caused major calculation errors. (Applications often use accelerometer + gyro data to calculate an absolute position. This involves integration.. and small errors become big). We had to use the "
mult_frac" macro to restore some of the precision during the calculations. It might be something to double check.That's good to know. I'll look more closely at how it's implemented in hid-sony.
Quoted 6 lines
In addition what orientation are you using for the axes? I need to double check the DS4 datasheets, but I think we were using a "right handed" coordinate system. (So if you make a fist of your right hand with thumb up, the gyro curls around it counter clockwise along the direction of your fingers).
The orientation of the axes (and much more) are documented here:
https://github.com/dekuNukem/Nintendo_Switch_Reverse_Engineering/blob/master/imu_sensor_notes.md
Since the the right vs. left joy-cons have different axes orientations, I assume it's preferable to standardize it all in software to match the orientation of the DS4.Quoted 146 lines
Quoted 146 lines
drivers/hid/hid-nintendo.c +82
-3+ +input_report_abs (idev,ABS_RX , value[0 ]); +input_report_abs (idev,ABS_RY , value[1 ]); +input_report_abs (idev,ABS_RZ , value[2 ]); +input_report_abs (idev,ABS_X , value[3 ]); +input_report_abs (idev,ABS_Y , value[4 ]); +input_report_abs (idev,ABS_Z , value[5 ]); +input_sync (idev); + } + } }static void joycon_rumble_worker (struct work_struct *work)@@ -950,6 +1131,7 @@ static int joycon_input_create(struct joycon_ctlr *ctlr) {struct hid_device *hdev;const char *name; +const char *imu_name;int ret;int i;@@ -958,12 +1140,15 @@ static int joycon_input_create(struct joycon_ctlr *ctlr)switch (hdev->product) {case USB_DEVICE_ID_NINTENDO_PROCON : name ="Nintendo Switch Pro Controller" ; + imu_name ="Nintendo Switch Pro Controller IMU" ;break ;case USB_DEVICE_ID_NINTENDO_JOYCONL : name ="Nintendo Switch Left Joy-Con" ; + imu_name ="Nintendo Switch Left Joy-Con IMU" ;break ;case USB_DEVICE_ID_NINTENDO_JOYCONR : name ="Nintendo Switch Right Joy-Con" ; + imu_name ="Nintendo Switch Right Joy-Con IMU" ;break ;default :/* Should be impossible */ hid_err (hdev,"Invalid hid product\n" );@@ -1029,6 +1214,55 @@ static int joycon_input_create(struct joycon_ctlr *ctlr)if (ret)return ret; +/* configure the imu input device */ + ctlr->imu_input =devm_input_allocate_device (&hdev->dev); +if (!ctlr->imu_input) +return -ENOMEM ; + + ctlr->imu_input->id.bustype = hdev->bus; + ctlr->imu_input->id.vendor = hdev->vendor; + ctlr->imu_input->id.product = hdev->product; + ctlr->imu_input->id.version = hdev->version; + ctlr->imu_input->uniq = ctlr->mac_addr_str; + ctlr->imu_input->name = imu_name; +input_set_drvdata (ctlr->imu_input, ctlr); + +/* configure imu axes */ +input_set_abs_params (ctlr->imu_input,ABS_X , + -JC_MAX_ACCEL_MAG ,JC_MAX_ACCEL_MAG , +JC_ACCEL_FUZZ ,JC_ACCEL_FLAT ); +input_set_abs_params (ctlr->imu_input,ABS_Y , + -JC_MAX_ACCEL_MAG ,JC_MAX_ACCEL_MAG , +JC_ACCEL_FUZZ ,JC_ACCEL_FLAT ); +input_set_abs_params (ctlr->imu_input,ABS_Z , + -JC_MAX_ACCEL_MAG ,JC_MAX_ACCEL_MAG , +JC_ACCEL_FUZZ ,JC_ACCEL_FLAT ); +input_abs_set_res (ctlr->imu_input,ABS_X ,JC_ACCEL_RES ); +input_abs_set_res (ctlr->imu_input,ABS_Y ,JC_ACCEL_RES ); +input_abs_set_res (ctlr->imu_input,ABS_Z ,JC_ACCEL_RES ); + +input_set_abs_params (ctlr->imu_input,ABS_RX , + -JC_MAX_GYRO_MAG ,JC_MAX_GYRO_MAG , +JC_GYRO_FUZZ ,JC_GYRO_FLAT ); +input_set_abs_params (ctlr->imu_input,ABS_RY , + -JC_MAX_GYRO_MAG ,JC_MAX_GYRO_MAG , +JC_GYRO_FUZZ ,JC_GYRO_FLAT ); +input_set_abs_params (ctlr->imu_input,ABS_RZ , + -JC_MAX_GYRO_MAG ,JC_MAX_GYRO_MAG , +JC_GYRO_FUZZ ,JC_GYRO_FLAT ); + +input_abs_set_res (ctlr->imu_input,ABS_RX ,JC_GYRO_RES ); +input_abs_set_res (ctlr->imu_input,ABS_RY ,JC_GYRO_RES ); +input_abs_set_res (ctlr->imu_input,ABS_RZ ,JC_GYRO_RES ); + +__set_bit (EV_MSC , ctlr->imu_input->evbit); +__set_bit (MSC_TIMESTAMP , ctlr->imu_input->mscbit); +__set_bit (INPUT_PROP_ACCELEROMETER , ctlr->imu_input->propbit); + + ret =input_register_device (ctlr->imu_input); +if (ret) +return ret; +return 0 ; }@@ -1288,7 +1522,7 @@ static int joycon_read_mac(struct joycon_ctlr *ctlr) report = (struct joycon_input_report *)ctlr->input_buf;for (i =4 , j =0 ; j <6 ; i++, j++) - ctlr->mac_addr[j] = report->reply.data[i]; + ctlr->mac_addr[j] = report->subcmd_reply.data[i]; ctlr->mac_addr_str = devm_kasprintf(&ctlr->hdev->dev, GFP_KERNEL, "%02X:%02X:%02X:%02X:%02X:%02X",@@ -1343,7 +1577,7 @@ static int joycon_ctlr_handle_event(struct joycon_ctlr *ctlr, u8 *data, data[0] != JC_INPUT_SUBCMD_REPLY)break ; report = (struct joycon_input_report *)data; -if (report->reply.id == ctlr->subcmd_ack_match) +if (report->subcmd_reply.id == ctlr->subcmd_ack_match) match = true;break ;default :@@ -1469,6 +1703,16 @@ static int nintendo_hid_probe(struct hid_device *hdev,hid_warn (hdev,"Analog stick positions may be inaccurate\n" ); } +/* get IMU calibration data, and parse it */ + ret =joycon_request_imu_calibration (ctlr); +if (ret) { +/* +* We can function with default calibration, but it may be +* inaccurate. Provide a warning, and continue on. +*/ +hid_warn (hdev,"Unable to read IMU calibration data\n" ); + } +/* Set the reporting mode to 0x30, which is the full report mode */ ret =joycon_set_report_mode (ctlr);if (ret) {@@ -1483,6 +1727,13 @@ static int nintendo_hid_probe(struct hid_device *hdev, goto err_mutex; } +/* Enable the IMU */ + ret =joycon_enable_imu (ctlr, true); +if (ret) { +hid_err (hdev,"Failed to enable the IMU; ret=%d\n" , ret); + goto err_mutex; + } + ret =joycon_read_mac (ctlr);if (ret) { hid_err(hdev, "Failed to retrieve controller MAC; ret=%d\n", --2.24.1
Signature · 1 line
Daniel Ogorchock
Daniel J. Ogorchock
maintainerPATCH v10 12/12 HID: nintendo: add support for charging grip
In reply to Daniel J. OgorchockThis patch adds support for the joy-con charging grip. The peripheral essentially behaves the same as a pro controller, but with two joy-cons attached to the grip. However the grip exposes the two joy-cons as separate hid devices, so extra handling is required. The joy-con is queried to check if it is a right or left joy-con (since the product ID is identical between left/right when using the grip).
Since controller model detection is now more complicated, the various checks for hid product values have been replaced with helper macros to reduce code duplication.
--- drivers/hid/hid-ids.h | 1 + drivers/hid/hid-nintendo.c | 87 ++++++++++++++++++++++++++++---------- 2 files changed, 65 insertions(+), 23 deletions(-)
drivers/hid/hid-ids.h +1 -0
@@ -871,6 +871,7 @@#define USB_DEVICE_ID_NINTENDO_JOYCONL 0x2006#define USB_DEVICE_ID_NINTENDO_JOYCONR 0x2007#define USB_DEVICE_ID_NINTENDO_PROCON 0x2009 +#define USB_DEVICE_ID_NINTENDO_CHRGGRIP 0x200E#define USB_VENDOR_ID_NOVATEK 0x0603#define USB_DEVICE_ID_NOVATEK_PCT 0x0600
drivers/hid/hid-nintendo.c +64 -23
203 lines not on this page · raw message
Signature · 1 line
2.24.1