From 729c219f1acf7c5c671014d08dbae462e885ddc5 Mon Sep 17 00:00:00 2001 From: Nathan Strong Date: Fri, 15 Oct 2021 16:02:34 -0700 Subject: [PATCH] Rewrite DS3 driver == DETAILS The DS3 driver previously only worked with the Wii U HID implementation. I adapted this driver from the Linux driver for the DS3. It's not quite 100%--I haven't got the LEDs to work properly--but it's functional. Going to continue tweaking it to see if I can get the LEDs to work. --- input/common/input_hid_common.c | 16 ++- input/connect/connect_ps3.c | 187 +++++++++++++++++++++++++-- input/connect/joypad_connection.c | 5 +- input/drivers_hid/iohidmanager_hid.c | 53 +++++++- input/drivers_hid/wiiu_hid.c | 27 ++++ input/include/gamepad.h | 2 +- input/include/hid_driver.h | 5 +- 7 files changed, 273 insertions(+), 22 deletions(-) diff --git a/input/common/input_hid_common.c b/input/common/input_hid_common.c index 18a7669bb4..64780d307b 100644 --- a/input/common/input_hid_common.c +++ b/input/common/input_hid_common.c @@ -43,6 +43,11 @@ void gamepad_read_axis_data(uint32_t axis, axis_data *data) } int16_t gamepad_get_axis_value(int16_t state[3][2], axis_data *data) +{ + return gamepad_get_axis_value_raw(state, data, true); +} + +int16_t gamepad_get_axis_value_raw(int16_t state[3][2], axis_data *data, bool do_clamp) { int16_t value = 0; @@ -64,11 +69,12 @@ int16_t gamepad_get_axis_value(int16_t state[3][2], axis_data *data) value = state[RETRO_DEVICE_INDEX_ANALOG_RIGHT][0]; break; } - - if (data->is_negative && value > 0) - return 0; - if (!data->is_negative && value < 0) - return 0; + if(do_clamp) { + if (data->is_negative && value > 0) + return 0; + if (!data->is_negative && value < 0) + return 0; + } return value; } diff --git a/input/connect/connect_ps3.c b/input/connect/connect_ps3.c index 73d4fa0c2e..83b02c84f2 100644 --- a/input/connect/connect_ps3.c +++ b/input/connect/connect_ps3.c @@ -39,30 +39,30 @@ typedef struct ds3_instance uint8_t led_state[4]; } ds3_instance_t; -struct sixaxis_led { +struct __attribute__((__packed__)) sixaxis_led { uint8_t time_enabled; /* the total time the led is active (0xff means forever) */ uint8_t duty_length; /* how long a cycle is in deciseconds (0 means "really fast") */ uint8_t enabled; uint8_t duty_off; /* % of duty_length the led is off (0xff means 100%) */ uint8_t duty_on; /* % of duty_length the led is on (0xff mean 100%) */ -} __packed; +}; -struct sixaxis_rumble { +struct __attribute__((__packed__)) sixaxis_rumble { uint8_t padding; uint8_t right_duration; /* Right motor duration (0xff means forever) */ uint8_t right_motor_on; /* Right (small) motor on/off, only supports values of 0 or 1 (off/on) */ uint8_t left_duration; /* Left motor duration (0xff means forever) */ uint8_t left_motor_force; /* left (large) motor, supports force values from 0 to 255 */ -} __packed; +}; -struct sixaxis_output_report { +struct __attribute__((__packed__)) sixaxis_output_report { uint8_t report_id; struct sixaxis_rumble rumble; uint8_t padding[4]; uint8_t leds_bitmap; /* bitmap of enabled LEDs: LED_1 = 0x02, LED_2 = 0x04, ... */ struct sixaxis_led led[4]; /* LEDx at (4 - x) */ struct sixaxis_led _reserved; /* LED5, not actually soldered */ -} __packed; +}; union sixaxis_output_report_01 { struct sixaxis_output_report data; @@ -85,9 +85,13 @@ static const union sixaxis_output_report_01 default_report = { /* forward declarations */ static void set_leds_from_id(ds3_instance_t *instance); static int ds3_set_operational(ds3_instance_t *instance); +static int ds3_send_output_report(ds3_instance_t *instance); +static void ds3_update_pad_state(ds3_instance_t *instance); +static void ds3_update_analog_state(ds3_instance_t *instance); static void *ds3_init(void *handle, uint32_t slot, hid_driver_t *driver) { ds3_instance_t *instance = (ds3_instance_t *)malloc(sizeof(ds3_instance_t)); + int ret; if(!instance) { return NULL; } @@ -97,15 +101,56 @@ static void *ds3_init(void *handle, uint32_t slot, hid_driver_t *driver) { instance->slot = slot; set_leds_from_id(instance); + if((ret = ds3_set_operational(instance)) < 0) { + RARCH_LOG("Failed to set operational mode\n"); + goto error; + } + if((ret = ds3_send_output_report(instance)) < 0) { + RARCH_LOG("Failed to send output report\n"); + goto error; + } + return instance; +error: + free(instance); + return NULL; } static void ds3_deinit(void *device_data) { - + if(device_data) { + free(device_data); + } } static void ds3_packet_handler(void *device_data, uint8_t *packet, uint16_t size) { + ds3_instance_t *device = (ds3_instance_t *)device_data; + static long packet_count = 0; + if(!device) + return; + + if (!device->led_set) + { + ds3_send_output_report(device); + device->led_set = true; + } + + if (size > sizeof(device->data)) + { + RARCH_ERR("[ds3]: Expecting packet to be %ld but was %d\n", + (long)sizeof(device->data), size); + return; + } + packet_count++; + +#if defined(__APPLE__) && defined(HAVE_IOHIDMANAGER) + packet++; + size -= 2; +#endif + + memcpy(device->data, packet, size); + ds3_update_pad_state(device); + ds3_update_analog_state(device); } static void ds3_set_rumble(void *device_data, enum retro_rumble_effect effect, uint16_t strength) { @@ -113,11 +158,35 @@ static void ds3_set_rumble(void *device_data, enum retro_rumble_effect effect, u } static void ds3_get_buttons(void *device_data, input_bits_t *state) { - + ds3_instance_t *device = (ds3_instance_t *)device_data; + if (device) + { + /* copy 32 bits : needed for PS button? */ + BITS_COPY32_PTR(state, device->buttons); + } + else + BIT256_CLEAR_ALL_PTR(state); } static int16_t ds3_get_axis(void *device_data, unsigned axis) { - return 0; + union joyaxis { + uint32_t encoded; + int16_t axis[2]; + } joyaxis; + axis_data axis_data = {0}; + ds3_instance_t *device = (ds3_instance_t *)device_data; + + joyaxis.encoded = axis; + gamepad_read_axis_data(axis, &axis_data); + + if (!device || axis_data.axis >= 4) + return 0; + + if(joyaxis.axis[0] < 0 || joyaxis.axis[1] < 0) { + return gamepad_get_axis_value(device->analog_state, &axis_data); + } else { + return gamepad_get_axis_value_raw(device->analog_state, &axis_data, false); + } } static const char *ds3_get_name(void *device_data) { @@ -125,7 +194,11 @@ static const char *ds3_get_name(void *device_data) { } static int32_t ds3_button(void *device_data, uint16_t joykey) { - return 0; + ds3_instance_t *device = (ds3_instance_t *)device_data; + + if (!device || joykey > 31) + return 0; + return device->buttons & (1 << joykey); } static void set_leds_from_id(ds3_instance_t *instance) @@ -134,7 +207,7 @@ static void set_leds_from_id(ds3_instance_t *instance) /* for higher pads, we sum up the numbers on the LEDs */ /* themselves, so e.g. pad 5 is 4 + 1, pad 6 is 4 + 2, */ /* and so on. We max out at 10 because 4+3+2+1 = 10 */ - static const u8 sixaxis_leds[10][4] = { + static const uint8_t sixaxis_leds[10][4] = { { 0x01, 0x00, 0x00, 0x00 }, { 0x00, 0x01, 0x00, 0x00 }, { 0x00, 0x00, 0x01, 0x00 }, @@ -159,12 +232,102 @@ static void set_leds_from_id(ds3_instance_t *instance) static int ds3_set_operational(ds3_instance_t *instance) { const int buf_size = SIXAXIS_REPORT_0xF2_SIZE; uint8_t *buf = (uint8_t *)malloc(buf_size); + int ret; if(!buf) { return -1; } - instance->hid_driver->send_control(instance->handle,); + ret = instance->hid_driver->get_report(instance->handle, HID_REPORT_FEATURE, 0xf2, buf, SIXAXIS_REPORT_0xF2_SIZE); + if(ret < 0) { + RARCH_LOG("Failed to set operational mode step 1\n"); + goto out; + } + + ret = instance->hid_driver->get_report(instance->handle, HID_REPORT_FEATURE, 0xf5, buf, SIXAXIS_REPORT_0xF5_SIZE); + if(ret < 0) { + RARCH_LOG("Failed to set operational mode step 2\n"); + goto out; + } + + ret = instance->hid_driver->set_report(instance->handle, HID_REPORT_OUTPUT, buf[0], buf, 1); + if(ret < 0) { + RARCH_LOG("Failed to set operational mode step 3, ignoring\n"); + ret = 0; + } +out: + free(buf); + return ret; +} + +static int ds3_send_output_report(ds3_instance_t *instance) { + struct sixaxis_output_report report = {0}; + int n; + + /* Initialize the report with default values */ + memcpy(&report, &default_report, sizeof(struct sixaxis_output_report)); + report.leds_bitmap |= instance->led_state[0] << 1; + report.leds_bitmap |= instance->led_state[1] << 2; + report.leds_bitmap |= instance->led_state[2] << 3; + report.leds_bitmap |= instance->led_state[3] << 4; + + return instance->hid_driver->set_report(instance->handle, HID_REPORT_OUTPUT, report.report_id, (uint8_t *)&report, sizeof(report)); +} + +static void ds3_update_pad_state(ds3_instance_t *instance) +{ + uint32_t i, pressed_keys; + + static const uint32_t button_mapping[17] = + { + RETRO_DEVICE_ID_JOYPAD_SELECT, + RETRO_DEVICE_ID_JOYPAD_L3, + RETRO_DEVICE_ID_JOYPAD_R3, + RETRO_DEVICE_ID_JOYPAD_START, + RETRO_DEVICE_ID_JOYPAD_UP, + RETRO_DEVICE_ID_JOYPAD_RIGHT, + RETRO_DEVICE_ID_JOYPAD_DOWN, + RETRO_DEVICE_ID_JOYPAD_LEFT, + RETRO_DEVICE_ID_JOYPAD_L2, + RETRO_DEVICE_ID_JOYPAD_R2, + RETRO_DEVICE_ID_JOYPAD_L, + RETRO_DEVICE_ID_JOYPAD_R, + RETRO_DEVICE_ID_JOYPAD_X, + RETRO_DEVICE_ID_JOYPAD_A, + RETRO_DEVICE_ID_JOYPAD_B, + RETRO_DEVICE_ID_JOYPAD_Y, + 16 /* PS button */ + }; + + instance->buttons = 0; + + pressed_keys = instance->data[2] | + (instance->data[3] << 8) | + ((instance->data[4] & 0x01) << 16); + + for (i = 0; i < 17; i++) + instance->buttons |= (pressed_keys & (1 << i)) ? + (1 << button_mapping[i]) : 0; +} + +static void ds3_update_analog_state(ds3_instance_t *instance) +{ + int pad_axis; + int16_t interpolated; + unsigned stick, axis; + + for (pad_axis = 0; pad_axis < 4; pad_axis++) + { + axis = (pad_axis % 2) ? 0 : 1; + stick = pad_axis / 2; + interpolated = instance->data[6 + pad_axis]; + + /* libretro requires "up" to be negative, so we invert the y axis */ + interpolated = (axis) ? + ((interpolated - 128) * 256) : + ((interpolated - 128) * -256); + instance->analog_state[stick][axis] = interpolated; + } } pad_connection_interface_t pad_connection_ps3 = { diff --git a/input/connect/joypad_connection.c b/input/connect/joypad_connection.c index 37731bb75c..5f42e649c5 100644 --- a/input/connect/joypad_connection.c +++ b/input/connect/joypad_connection.c @@ -406,8 +406,11 @@ void pad_connection_packet(joypad_connection_t *joyconn, uint32_t pad, if ( joyconn->connection && joyconn->iface - && joyconn->iface->packet_handler) + && joyconn->iface->packet_handler) { + joyconn->iface->packet_handler(joyconn->connection, data, length); + + } } void pad_connection_get_buttons(joypad_connection_t *joyconn, diff --git a/input/drivers_hid/iohidmanager_hid.c b/input/drivers_hid/iohidmanager_hid.c index 78f68d651e..3fc0602c75 100644 --- a/input/drivers_hid/iohidmanager_hid.c +++ b/input/drivers_hid/iohidmanager_hid.c @@ -59,6 +59,19 @@ struct iohidmanager_hid_adapter uint8_t data[2048]; }; +enum IOHIDReportType translate_hid_report_type(int report_type) { + switch(report_type) { + case HID_REPORT_FEATURE: + return kIOHIDReportTypeFeature; + case HID_REPORT_INPUT: + return kIOHIDReportTypeInput; + case HID_REPORT_OUTPUT: + return kIOHIDReportTypeOutput; + case HID_REPORT_COUNT: + return kIOHIDReportTypeCount; + } +} + CFComparisonResult iohidmanager_sort_elements(const void *val1, const void *val2, void *context) { uint32_t page1 = (uint32_t)IOHIDElementGetUsagePage((IOHIDElementRef)val1); @@ -683,13 +696,15 @@ static void iohidmanager_hid_device_add(IOHIDDeviceRef device, iohidmanager_hid_ if (string_is_empty(adapter->name)) strcpy(adapter->name, "Unknown Controller With No Name"); - if (pad_connection_has_interface(hid->slots, adapter->slot)) + if (pad_connection_has_interface(hid->slots, adapter->slot)) { IOHIDDeviceRegisterInputReportCallback(device, adapter->data + 1, sizeof(adapter->data) - 1, iohidmanager_hid_device_report, adapter); - else + } + else { IOHIDDeviceRegisterInputValueCallback(device, iohidmanager_hid_device_input_callback, adapter); + } /* scan for buttons, axis, hats */ elements_raw = IOHIDDeviceCopyMatchingElements(device, NULL, kIOHIDOptionsTypeNone); @@ -1081,6 +1096,35 @@ static void iohidmanager_hid_poll(void *data) (void)data; } +static int32_t iohidmanager_set_report(void *handle, uint8_t report_type, uint8_t report_id, void *data_buf, uint32_t size) +{ + struct iohidmanager_hid_adapter *adapter = + (struct iohidmanager_hid_adapter*)handle; + + int retval = -1; + + if (adapter) { + retval = IOHIDDeviceSetReport(adapter->handle, translate_hid_report_type(report_type), report_type, data_buf + 1, size - 1); + } + + return retval; +} + +static int32_t iohidmanager_get_report(void *handle, uint8_t report_type, uint8_t report_id, void *data_buf, size_t size) +{ + struct iohidmanager_hid_adapter *adapter = + (struct iohidmanager_hid_adapter*)handle; + int retval = -1; + + if (adapter) { + CFIndex length = size; + + retval = IOHIDDeviceGetReport(adapter->handle, translate_hid_report_type(report_type), report_id, data_buf+1, &length); + } + + return retval; +} + hid_driver_t iohidmanager_hid = { iohidmanager_hid_init, iohidmanager_hid_joypad_query, @@ -1094,4 +1138,9 @@ hid_driver_t iohidmanager_hid = { iohidmanager_hid_joypad_name, "iohidmanager", iohidmanager_hid_device_send_control, + iohidmanager_set_report, + iohidmanager_get_report, + NULL, /* set_idle */ + NULL, /* set_protocol */ + NULL /* read */ }; diff --git a/input/drivers_hid/wiiu_hid.c b/input/drivers_hid/wiiu_hid.c index 844cbef651..bf152f27b4 100644 --- a/input/drivers_hid/wiiu_hid.c +++ b/input/drivers_hid/wiiu_hid.c @@ -239,6 +239,23 @@ static int32_t wiiu_hid_set_report(void *data, uint8_t report_type, NULL, NULL); } +static int32_t wiiu_hid_get_report(void *handle, uint8_t report_type, uint8_t report_id, void *data_buf, size_t size) +{ + wiiu_adapter_t *adapter = (wiiu_adapter_t *)handle; + if (!adapter || size > adapter->tx_size) + return -1; + + memset(adapter->tx_buffer, 0, adapter->tx_size); + memcpy(adapter->tx_buffer, data_buf, size); + + return HIDGetReport(adapter->handle, + report_type, + report_id, + adapter->tx_buffer, + adapter->tx_size, + NULL, NULL); +} + static int32_t wiiu_hid_set_idle(void *data, uint8_t duration) { wiiu_adapter_t *adapter = (wiiu_adapter_t *)data; @@ -910,6 +927,15 @@ void *alloc_zeroed(size_t alignment, size_t size) return result; } +/* + void (*send_control)(void *handle, uint8_t *buf, size_t size); + int32_t (*set_report)(void *handle, uint8_t, uint8_t, void *data, uint32_t size); + int32_t (*get_report)(void *handle, uint8_t report_type, uint8_t report_id, void *buffer, size_t length); + int32_t (*set_idle)(void *handle, uint8_t amount); + int32_t (*set_protocol)(void *handle, uint8_t protocol); + int32_t (*read)(void *handle, void *buf, size_t size); +*/ + hid_driver_t wiiu_hid = { wiiu_hid_init, wiiu_hid_joypad_query, @@ -924,6 +950,7 @@ hid_driver_t wiiu_hid = { "wiiu", wiiu_hid_send_control, wiiu_hid_set_report, + wiiu_hid_get_report, wiiu_hid_set_idle, wiiu_hid_set_protocol, wiiu_hid_read, diff --git a/input/include/gamepad.h b/input/include/gamepad.h index 802f0b5718..c6170594e7 100644 --- a/input/include/gamepad.h +++ b/input/include/gamepad.h @@ -28,5 +28,5 @@ typedef struct _axis_data void gamepad_read_axis_data(uint32_t axis, axis_data *data); int16_t gamepad_get_axis_value(int16_t state[3][2], axis_data *data); - +int16_t gamepad_get_axis_value_raw(int16_t state[3][2], axis_data *data, bool do_clamp); #endif /* GAMEPAD_H__ */ diff --git a/input/include/hid_driver.h b/input/include/hid_driver.h index 127b477d94..64f21a1acd 100644 --- a/input/include/hid_driver.h +++ b/input/include/hid_driver.h @@ -22,8 +22,10 @@ #include "../input_driver.h" /* what is 1? */ -#define HID_REPORT_OUTPUT 2 +#define HID_REPORT_INPUT 1 +#define HID_REPORT_OUTPUT 2 #define HID_REPORT_FEATURE 3 +#define HID_REPORT_COUNT 4 /* are there more? */ /* @@ -49,6 +51,7 @@ struct hid_driver const char *ident; void (*send_control)(void *handle, uint8_t *buf, size_t size); int32_t (*set_report)(void *handle, uint8_t, uint8_t, void *data, uint32_t size); + int32_t (*get_report)(void *handle, uint8_t report_type, uint8_t report_id, void *buffer, size_t length); int32_t (*set_idle)(void *handle, uint8_t amount); int32_t (*set_protocol)(void *handle, uint8_t protocol); int32_t (*read)(void *handle, void *buf, size_t size);