diff --git a/src/Makefile.am b/src/Makefile.am index 21c0d4d5..7e4ed90a 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -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 diff --git a/src/etekcity.c b/src/etekcity.c index ad3429e6..9dc32e7b 100644 --- a/src/etekcity.c +++ b/src/etekcity.c @@ -22,16 +22,30 @@ */ #include "config.h" +#include #include #include +#include #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; } diff --git a/src/libratbag-hidraw.c b/src/libratbag-hidraw.c new file mode 100644 index 00000000..d8c2ebc6 --- /dev/null +++ b/src/libratbag-hidraw.c @@ -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 +#include +#include +#include + +#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; +} diff --git a/src/libratbag-hidraw.h b/src/libratbag-hidraw.h new file mode 100644 index 00000000..358b6a99 --- /dev/null +++ b/src/libratbag-hidraw.h @@ -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 */ diff --git a/src/libratbag-private.h b/src/libratbag-private.h index 0ba4f491..59b46444 100644 --- a/src/libratbag-private.h +++ b/src/libratbag-private.h @@ -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; diff --git a/src/libratbag.c b/src/libratbag.c index 34955764..6fcd7a05 100644 --- a/src/libratbag.c +++ b/src/libratbag.c @@ -24,7 +24,9 @@ #include "config.h" #include #include +#include #include +#include #include #include #include @@ -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);