Add ratbag-hidraw to deal with low level hidraw communication

Signed-off-by: Benjamin Tissoires <benjamin.tissoires@gmail.com>
This commit is contained in:
Benjamin Tissoires
2015-08-14 23:12:25 -04:00
parent c6cf35a5fc
commit 8d5088b9c6
6 changed files with 120 additions and 0 deletions

View File

@@ -7,6 +7,8 @@ libratbag_la_SOURCES = \
etekcity.c \
libratbag.c \
libratbag.h \
libratbag-hidraw.c \
libratbag-hidraw.h \
libratbag-util.c \
libratbag-private.h \
libratbag-util.h

View File

@@ -22,16 +22,30 @@
*/
#include "config.h"
#include <errno.h>
#include <linux/input.h>
#include <stdlib.h>
#include <string.h>
#include "libratbag-private.h"
#include "libratbag-hidraw.h"
static int
etekcity_probe(struct ratbag *ratbag, const struct ratbag_id id)
{
int rc;
log_debug(ratbag->libratbag, "data: %d\n", id.data);
rc = ratbag_open_hidraw(ratbag);
if (rc) {
log_error(ratbag->libratbag,
"Can't open corresponding hidraw node: '%s' (%d)\n",
strerror(-rc),
rc);
return -ENODEV;
}
return 0;
}

66
src/libratbag-hidraw.c Normal file
View File

@@ -0,0 +1,66 @@
/*
* Copyright © 2015 Red Hat, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include <errno.h>
#include <fcntl.h>
#include <libudev.h>
#include <linux/hidraw.h>
#include "libratbag-hidraw.h"
#include "libratbag-private.h"
int
ratbag_open_hidraw(struct ratbag *ratbag)
{
struct hidraw_devinfo info;
int fd, res;
if (!ratbag->udev_hidraw)
return -EINVAL;
fd = open(udev_device_get_devnode(ratbag->udev_hidraw), O_RDWR);
if (fd < 0)
goto err;
/* Get Raw Info */
res = ioctl(fd, HIDIOCGRAWINFO, &info);
if (res < 0) {
log_error(ratbag->libratbag,
"error while getting info from device");
goto err;
}
/* Check if the device actually matches the input node */
if (info.bustype != ratbag->ids.bustype ||
info.vendor != ratbag->ids.vendor ||
info.product != ratbag->ids.product)
goto err;
ratbag->hidraw_fd = fd;
return 0;
err:
if (fd >= 0)
close(fd);
return -errno;
}

31
src/libratbag-hidraw.h Normal file
View File

@@ -0,0 +1,31 @@
/*
* Copyright © 2015 Red Hat, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#ifndef LIBRATBAG_HIDRAW_H
#define LIBRATBAG_HIDRAW_H
#include "libratbag.h"
int ratbag_open_hidraw(struct ratbag *ratbag);
#endif /* LIBRATBAG_HIDRAW_H */

View File

@@ -44,6 +44,7 @@ struct ratbag {
struct udev_device *udev_device;
struct udev_device *udev_hidraw;
int evdev_fd;
int hidraw_fd;
int refcount;
struct input_id ids;
struct ratbag_driver *driver;

View File

@@ -24,7 +24,9 @@
#include "config.h"
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <libudev.h>
#include <linux/hidraw.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
@@ -190,6 +192,7 @@ void
ratbag_device_init(struct ratbag *rb, int fd)
{
rb->evdev_fd = fd;
rb->hidraw_fd = -1;
rb->refcount = 1;
}
@@ -311,6 +314,9 @@ ratbag_unref(struct ratbag *ratbag)
udev_device_unref(ratbag->udev_device);
udev_device_unref(ratbag->udev_hidraw);
if (ratbag->hidraw_fd >= 0)
close(ratbag->hidraw_fd);
ratbag->libratbag = libratbag_unref(ratbag->libratbag);
free(ratbag->name);
free(ratbag);