From 29b72c1a94f0f315a32d2d3a0fd114d7b32e4709 Mon Sep 17 00:00:00 2001 From: Benjamin Tissoires Date: Thu, 20 Aug 2015 10:46:21 -0400 Subject: [PATCH] hidpp10: use ratbag_hidraw* commands, not plain access to the fd Signed-off-by: Benjamin Tissoires --- src/driver-logitech-unifying.c | 4 +-- src/hidpp10.c | 53 ++++++++++++++++------------------ src/hidpp10.h | 16 +++++----- 3 files changed, 36 insertions(+), 37 deletions(-) diff --git a/src/driver-logitech-unifying.c b/src/driver-logitech-unifying.c index d8fab88a..c3d1b07f 100644 --- a/src/driver-logitech-unifying.c +++ b/src/driver-logitech-unifying.c @@ -64,7 +64,7 @@ unifying_write_button(struct ratbag_button *button) if (dev->fw_major == 0x17 && dev->fw_minor == 0x01 && dev->build == 0x0015) - hidpp10_toggle_individual_feature(device->hidraw_fd, + hidpp10_toggle_individual_feature(device, dev, FEATURE_BIT_R0_SPECIAL_BUTTON_FUNCTION, -1); @@ -120,7 +120,7 @@ unifying_probe(struct ratbag_device *device, const struct ratbag_id id) if (!drv_data) return -ENODEV; - rc = hidpp10_get_device_from_wpid(device->hidraw_fd, + rc = hidpp10_get_device_from_wpid(device, id.id.product, &dev); if (rc) { diff --git a/src/hidpp10.c b/src/hidpp10.c index 8fe7ab2c..92b31a3a 100644 --- a/src/hidpp10.c +++ b/src/hidpp10.c @@ -41,6 +41,7 @@ #include #include +#include #if DEBUG_LVL > 0 const char *hidpp_errors[0xFF] = { @@ -76,25 +77,21 @@ const char *device_types[0xFF] = { }; -static int hidpp10_write_command(int fd, __u8 *cmd, int size) { - int res = write(fd, cmd, size); +static int hidpp10_write_command(struct ratbag_device *device, __u8 *cmd, int size) { + int res = ratbag_hidraw_output_report(device, cmd, size); - if (res == size) + if (res == 0) return 0; if (res < 0) { printf("Error: %d\n", errno); perror("write"); - } else { - errno = ENOMEM; - printf("write: %d were written instead of %d.\n", res, size); - perror("write"); } return res; } -int hidpp10_request_command(int fd, union hidpp10_message *msg) { +int hidpp10_request_command(struct ratbag_device *device, union hidpp10_message *msg) { union hidpp10_message read_buffer; union hidpp10_message expected_header; union hidpp10_message expected_error_recv = ERROR_MSG(msg, RECEIVER_IDX); @@ -127,7 +124,7 @@ int hidpp10_request_command(int fd, union hidpp10_message *msg) { #endif /* Send the message to the Device */ - ret = hidpp10_write_command(fd, msg->data, SHORT_MESSAGE_LENGTH); + ret = hidpp10_write_command(device, msg->data, SHORT_MESSAGE_LENGTH); if (ret) goto out_err; @@ -136,7 +133,7 @@ int hidpp10_request_command(int fd, union hidpp10_message *msg) { * loop until we get the actual answer or an error code. */ do { - ret = read(fd, read_buffer.data, LONG_MESSAGE_LENGTH); + ret = ratbag_hidraw_read_input_report(device, read_buffer.data, LONG_MESSAGE_LENGTH); #if DEBUG_LVL > 2 printf(" *** received: "); pr_buffer(read_buffer.data, ret); #endif @@ -175,13 +172,13 @@ out_err: return ret; } -int hidpp10_toggle_individual_feature(int fd, struct hidpp10_device *dev, int feature_bit_r0, int feature_bit_r2) { +int hidpp10_toggle_individual_feature(struct ratbag_device *device, struct hidpp10_device *dev, int feature_bit_r0, int feature_bit_r2) { unsigned idx = dev->index; union hidpp10_message mode = CMD_ENABLE_INDIVIDUAL_FEATURES(idx, GET_REGISTER_REQ); int res; /* first read the current values */ - res = hidpp10_request_command(fd, &mode); + res = hidpp10_request_command(device, &mode); if (res) return -1; @@ -195,28 +192,28 @@ int hidpp10_toggle_individual_feature(int fd, struct hidpp10_device *dev, int fe /* now write back the change */ mode.msg.sub_id = SET_REGISTER_REQ; - res = hidpp10_request_command(fd, &mode); + res = hidpp10_request_command(device, &mode); return res; } -int hidpp10_open_lock(int fd) { +int hidpp10_open_lock(struct ratbag_device *device) { union hidpp10_message open_lock = CMD_DEVICE_CONNECTION_DISCONNECTION(0x00, CONNECT_DEVICES_OPEN_LOCK, 0x08); - return hidpp10_request_command(fd, &open_lock); + return hidpp10_request_command(device, &open_lock); } -int hidpp10_disconnect(int fd, int idx) { +int hidpp10_disconnect(struct ratbag_device *device, int idx) { union hidpp10_message disconnect = CMD_DEVICE_CONNECTION_DISCONNECTION(idx + 1, CONNECT_DEVICES_DISCONNECT, 0x00); - return hidpp10_request_command(fd, &disconnect); + return hidpp10_request_command(device, &disconnect); } -void hidpp10_list_devices(int fd) { +void hidpp10_list_devices(struct ratbag_device *device) { struct hidpp10_device dev; int i, res; for (i = 0; i < 6; ++i) { - res = hidpp10_get_device_from_idx(fd, i, &dev); + res = hidpp10_get_device_from_idx(device, i, &dev); if (res) continue; @@ -224,7 +221,7 @@ void hidpp10_list_devices(int fd) { } } -static int hidpp10_get_device_info(int fd, struct hidpp10_device *dev) { +static int hidpp10_get_device_info(struct ratbag_device *device, struct hidpp10_device *dev) { unsigned idx = dev->index; union hidpp10_message pairing_information = CMD_PAIRING_INFORMATION(idx, DEVICE_PAIRING_INFORMATION); union hidpp10_message device_name = CMD_PAIRING_INFORMATION(idx, DEVICE_NAME); @@ -233,7 +230,7 @@ static int hidpp10_get_device_info(int fd, struct hidpp10_device *dev) { size_t name_size; int res; - res = hidpp10_request_command(fd, &pairing_information); + res = hidpp10_request_command(device, &pairing_information); if (res) return -1; @@ -242,7 +239,7 @@ static int hidpp10_get_device_info(int fd, struct hidpp10_device *dev) { pairing_information.msg.string[4]; dev->device_type = pairing_information.msg.string[7]; - res = hidpp10_request_command(fd, &device_name); + res = hidpp10_request_command(device, &device_name); if (res) return -1; @@ -254,14 +251,14 @@ static int hidpp10_get_device_info(int fd, struct hidpp10_device *dev) { * This may fail on some devices * => we can not retrieve their FW version through HID++ 1.0. */ - res = hidpp10_request_command(fd, &firmware_information); + res = hidpp10_request_command(device, &firmware_information); if (res) return 0; dev->fw_major = firmware_information.msg.string[1]; dev->fw_minor = firmware_information.msg.string[2]; - res = hidpp10_request_command(fd, &build_information); + res = hidpp10_request_command(device, &build_information); if (res) return 0; @@ -271,11 +268,11 @@ static int hidpp10_get_device_info(int fd, struct hidpp10_device *dev) { return 0; } -int hidpp10_get_device_from_wpid(int fd, __u16 wpid, struct hidpp10_device *dev) { +int hidpp10_get_device_from_wpid(struct ratbag_device *device, __u16 wpid, struct hidpp10_device *dev) { int i, res; for (i = 0; i < 6; i++) { - res = hidpp10_get_device_from_idx(fd, i, dev); + res = hidpp10_get_device_from_idx(device, i, dev); if (res) continue; @@ -288,7 +285,7 @@ int hidpp10_get_device_from_wpid(int fd, __u16 wpid, struct hidpp10_device *dev) return res; } -int hidpp10_get_device_from_idx(int fd, int idx, struct hidpp10_device *dev) { +int hidpp10_get_device_from_idx(struct ratbag_device *device, int idx, struct hidpp10_device *dev) { dev->index = idx; - return hidpp10_get_device_info(fd, dev); + return hidpp10_get_device_info(device, dev); } diff --git a/src/hidpp10.h b/src/hidpp10.h index a37b2a4d..514bba1e 100644 --- a/src/hidpp10.h +++ b/src/hidpp10.h @@ -31,6 +31,8 @@ #ifndef HIDPP_10_H #define HIDPP_10_H +#include "libratbag.h" + #define RECEIVER_IDX 0xFF #define REPORT_ID_SHORT 0x10 @@ -167,11 +169,11 @@ union hidpp10_message { }; -int hidpp10_request_command(int fd, union hidpp10_message *msg); -int hidpp10_toggle_individual_feature(int fd, struct hidpp10_device *dev, int feature_bit_r0, int feature_bit_r2); -int hidpp10_open_lock(int fd); -int hidpp10_disconnect(int fd, int idx); -void hidpp10_list_devices(int fd); -int hidpp10_get_device_from_wpid(int fd, __u16 wpid, struct hidpp10_device *dev); -int hidpp10_get_device_from_idx(int fd, int idx, struct hidpp10_device *dev); +int hidpp10_request_command(struct ratbag_device *device, union hidpp10_message *msg); +int hidpp10_toggle_individual_feature(struct ratbag_device *device, struct hidpp10_device *dev, int feature_bit_r0, int feature_bit_r2); +int hidpp10_open_lock(struct ratbag_device *device); +int hidpp10_disconnect(struct ratbag_device *device, int idx); +void hidpp10_list_devices(struct ratbag_device *device); +int hidpp10_get_device_from_wpid(struct ratbag_device *device, __u16 wpid, struct hidpp10_device *dev); +int hidpp10_get_device_from_idx(struct ratbag_device *device, int idx, struct hidpp10_device *dev); #endif /* HIDPP_10_H */