From a51cf764775979a8c3ac4b4da5118495cae4116f Mon Sep 17 00:00:00 2001 From: Peter Hutterer Date: Thu, 15 Oct 2015 10:09:45 +1000 Subject: [PATCH] hidpp: move read/write to hidpp into the generic headers Part 2 of disentangling libratbag from the hidpp10 library. Copy the hidraw_fd into the hidpp_device struct and use that during read/write. This temporarily disables logging, will be re-enabled with an upcoming commit. Signed-off-by: Peter Hutterer --- src/hidpp-generic.c | 54 +++++++++++++++++++++++++++++++++++++++++++ src/hidpp-generic.h | 11 +++++++++ src/hidpp10.c | 56 ++++----------------------------------------- src/hidpp10.h | 1 + 4 files changed, 70 insertions(+), 52 deletions(-) diff --git a/src/hidpp-generic.c b/src/hidpp-generic.c index 15ee135c..2f1a7940 100644 --- a/src/hidpp-generic.c +++ b/src/hidpp-generic.c @@ -30,6 +30,8 @@ #include "hidpp-generic.h" +#include +#include #include #include "libratbag-private.h" @@ -201,3 +203,55 @@ hidpp20_1b04_get_physical_mapping_name(uint16_t value) return "UNKNOWN"; } +int +hidpp_write_command(struct hidpp_device *dev, uint8_t *cmd, int size) +{ + int fd = dev->hidraw_fd; + int res; + + if (size < 1 || !cmd || fd < 0) + return -EINVAL; + +#if 0 + log_buf_raw(device->ratbag, "hidpp10 write: ", cmd, size); +#endif + res = write(fd, cmd, size); + if (res < 0) { + res = -errno; +#if 0 + log_error(device->ratbag, "Error: %s (%d)\n", strerror(-res), -res); +#endif + } + + return res < 0 ? res : 0; +} + +int +hidpp_read_response(struct hidpp_device *dev, uint8_t *buf, size_t size) +{ + int fd = dev->hidraw_fd; + struct pollfd fds; + int rc; + + if (size < 1 || !buf || fd < 0) + return -EINVAL; + + fds.fd = fd; + fds.events = POLLIN; + + rc = poll(&fds, 1, 1000); + if (rc == -1) + return -errno; + + if (rc == 0) + return -ETIMEDOUT; + + rc = read(fd, buf, size); + +#if 0 + if (rc > 0) + log_buf_raw(device->ratbag, "input report: ", buf, rc); +#endif + + return rc >= 0 ? rc : -errno; +} diff --git a/src/hidpp-generic.h b/src/hidpp-generic.h index 9d3908d7..8d16a7bf 100644 --- a/src/hidpp-generic.h +++ b/src/hidpp-generic.h @@ -27,6 +27,7 @@ */ #include +#include #ifndef HIDPP_GENERIC_H #define HIDPP_GENERIC_H @@ -64,6 +65,10 @@ #define ERR_INVALID_PARAM_VALUE 0x0B #define ERR_WRONG_PIN_CODE 0x0C +struct hidpp_device { + int hidraw_fd; +}; + extern const char *hidpp_errors[0xFF]; const char * @@ -81,4 +86,10 @@ hidpp20_1b04_get_logical_mapping(uint16_t value); uint16_t hidpp20_1b04_get_logical_control_id(const struct ratbag_button_action *action); +int +hidpp_write_command(struct hidpp_device *dev, uint8_t *cmd, int size); + +int +hidpp_read_response(struct hidpp_device *dev, uint8_t *buf, size_t size); + #endif /* HIDPP_GENERIC_H */ diff --git a/src/hidpp10.c b/src/hidpp10.c index da2b8a5d..7bbdf724 100644 --- a/src/hidpp10.c +++ b/src/hidpp10.c @@ -87,55 +87,6 @@ const char *device_types[0xFF] = { [0x0A ... 0xFE] = NULL, }; -static int -hidpp10_write_command(struct hidpp10_device *dev, uint8_t *cmd, int size) -{ - struct ratbag_device *device = dev->ratbag_device; - int fd = device->hidraw.fd; - int res; - - if (size < 1 || !cmd || fd < 0) - return -EINVAL; - - log_buf_raw(device->ratbag, "hidpp10 write: ", cmd, size); - res = write(fd, cmd, size); - if (res < 0) { - res = -errno; - log_error(device->ratbag, "Error: %s (%d)\n", strerror(-res), -res); - } - - return res < 0 ? res : 0; -} - -static int -hidpp10_read_response(struct hidpp10_device *dev, uint8_t *buf, size_t size) -{ - struct ratbag_device *device = dev->ratbag_device; - int fd = device->hidraw.fd; - struct pollfd fds; - int rc; - - if (size < 1 || !buf || fd < 0) - return -EINVAL; - - fds.fd = fd; - fds.events = POLLIN; - - rc = poll(&fds, 1, 1000); - if (rc == -1) - return -errno; - - if (rc == 0) - return -ETIMEDOUT; - - rc = read(fd, buf, size); - - if (rc > 0) - log_buf_raw(device->ratbag, "input report: ", buf, rc); - - return rc >= 0 ? rc : -errno; -} - int hidpp10_request_command(struct hidpp10_device *dev, union hidpp10_message *msg) { @@ -167,7 +118,7 @@ hidpp10_request_command(struct hidpp10_device *dev, union hidpp10_message *msg) log_buf_raw(ratbag, " expected_error_dev: ", expected_error_dev.data, SHORT_MESSAGE_LENGTH); /* Send the message to the Device */ - ret = hidpp10_write_command(dev, msg->data, command_size); + ret = hidpp_write_command(&dev->base, msg->data, command_size); if (ret) goto out_err; @@ -176,12 +127,12 @@ hidpp10_request_command(struct hidpp10_device *dev, union hidpp10_message *msg) * loop until we get the actual answer or an error code. */ do { - ret = hidpp10_read_response(dev, read_buffer.data, LONG_MESSAGE_LENGTH); + ret = hidpp_read_response(&dev->base, read_buffer.data, LONG_MESSAGE_LENGTH); /* Wait and retry if the USB timed out */ if (ret == -ETIMEDOUT) { msleep(10); - ret = hidpp10_read_response(dev, read_buffer.data, LONG_MESSAGE_LENGTH); + ret = hidpp_read_response(&dev->base, read_buffer.data, LONG_MESSAGE_LENGTH); } /* Overwrite the return device index with ours. The kernel @@ -1286,6 +1237,7 @@ hidpp10_device_new(struct ratbag_device *device, int index) return NULL; dev->index = index; + dev->base.hidraw_fd = device->hidraw.fd; dev->ratbag_device = device; return dev; diff --git a/src/hidpp10.h b/src/hidpp10.h index 03f5ad35..e470c75d 100644 --- a/src/hidpp10.h +++ b/src/hidpp10.h @@ -507,6 +507,7 @@ hidpp10_get_firmare_information(struct hidpp10_device *dev, * 0 is zeroed, 1 and 2 are garbage, all above 6 is garbage */ #define HIDPP10_NUM_PROFILES 3 struct hidpp10_device { + struct hidpp_device base; struct ratbag_device *ratbag_device; unsigned index; char name[15];