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 <peter.hutterer@who-t.net>
This commit is contained in:
Peter Hutterer
2015-10-15 10:09:45 +10:00
parent 2314437e05
commit a51cf76477
4 changed files with 70 additions and 52 deletions

View File

@@ -30,6 +30,8 @@
#include "hidpp-generic.h"
#include <errno.h>
#include <poll.h>
#include <stddef.h>
#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;
}

View File

@@ -27,6 +27,7 @@
*/
#include <stdint.h>
#include <stddef.h>
#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 */

View File

@@ -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;

View File

@@ -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];