diff --git a/src/driver-hidpp10.c b/src/driver-hidpp10.c index d544485b..15c5fc9c 100644 --- a/src/driver-hidpp10.c +++ b/src/driver-hidpp10.c @@ -189,6 +189,8 @@ hidpp10drv_probe(struct ratbag_device *device, const struct ratbag_id id) struct hidpp10_device dev; bool is_unifying_receiver; + hidpp10_device_init(device, &dev); + /* check if the device is a unifying receiver first so we can update * the hidraw path before we open it */ is_unifying_receiver = hidpp10drv_is_unifying_receiver(device); @@ -201,12 +203,15 @@ hidpp10drv_probe(struct ratbag_device *device, const struct ratbag_id id) "Can't open corresponding hidraw node: '%s' (%d)\n", strerror(-rc), rc); - return -ENODEV; + rc = -ENODEV; + goto err; } drv_data = zalloc(sizeof(*drv_data)); - if (!drv_data) - return -ENODEV; + if (!drv_data) { + rc = -ENODEV; + goto err; + } if (is_unifying_receiver) rc = hidpp10_get_device_from_wpid(device, @@ -232,6 +237,7 @@ hidpp10drv_probe(struct ratbag_device *device, const struct ratbag_id id) err: free(drv_data); ratbag_set_drv_data(device, NULL); + hidpp10_device_cleanup(&dev); return rc; } @@ -239,7 +245,15 @@ err: static void hidpp10drv_remove(struct ratbag_device *device) { - free(ratbag_get_drv_data(device)); + struct hidpp10drv_data *drv_data; + struct hidpp10_device *dev; + + drv_data = ratbag_get_drv_data(device); + dev = &drv_data->dev; + + hidpp10_device_cleanup(dev); + + free(drv_data); } #define LOGITECH_DEVICE(_bus, _pid) \ diff --git a/src/hidpp10.c b/src/hidpp10.c index d9db7678..c7f3f5ca 100644 --- a/src/hidpp10.c +++ b/src/hidpp10.c @@ -672,3 +672,15 @@ hidpp10_get_device_from_idx(struct ratbag_device *device, int idx, struct hidpp1 return hidpp10_get_device_info(device, dev); } +void +hidpp10_device_init(struct ratbag_device *device, struct hidpp10_device *dev) +{ + dev->index = 0x00; /* default for wired */ + dev->ratbag_device = ratbag_device_ref(device); +} + +void +hidpp10_device_cleanup(struct hidpp10_device *dev) +{ + ratbag_device_unref(dev->ratbag_device); +} diff --git a/src/hidpp10.h b/src/hidpp10.h index 38bf0c7c..9ca3f7d9 100644 --- a/src/hidpp10.h +++ b/src/hidpp10.h @@ -33,9 +33,11 @@ #include #include "hidpp-generic.h" +#include "libratbag-private.h" #include "libratbag.h" struct hidpp10_device { + struct ratbag_device *ratbag_device; unsigned index; char name[15]; uint16_t wpid; @@ -65,6 +67,9 @@ union hidpp10_message { uint8_t data[LONG_MESSAGE_LENGTH]; }; +void hidpp10_device_init(struct ratbag_device *device, struct hidpp10_device *dev); +void hidpp10_device_cleanup(struct hidpp10_device *dev); + int hidpp10_request_command(struct ratbag_device *device, union hidpp10_message *msg); int hidpp10_open_lock(struct ratbag_device *device); int hidpp10_disconnect(struct ratbag_device *device, int idx);