From 6ac92f84792d80be14a2fa5aa815041e4c3a206e Mon Sep 17 00:00:00 2001 From: Benjamin Tissoires Date: Tue, 20 Oct 2015 16:49:13 -0400 Subject: [PATCH] hidpp20: retrieve and store the list of features once for all Signed-off-by: Benjamin Tissoires --- src/driver-hidpp20.c | 27 ++++++++++++--------------- src/hidpp20.c | 17 +++++++++++++++-- src/hidpp20.h | 22 ++++++++-------------- 3 files changed, 35 insertions(+), 31 deletions(-) diff --git a/src/driver-hidpp20.c b/src/driver-hidpp20.c index a84f5496..bceba30c 100644 --- a/src/driver-hidpp20.c +++ b/src/driver-hidpp20.c @@ -424,25 +424,22 @@ static int hidpp20drv_20_probe(struct ratbag_device *device) { struct hidpp20drv_data *drv_data = ratbag_get_drv_data(device); - struct hidpp20_feature *feature_list; - int rc, i; + struct hidpp20_device *dev = drv_data->dev; + struct hidpp20_feature *feature_list = dev->feature_list; + unsigned int i; - rc = hidpp20_feature_set_get(drv_data->dev, &feature_list); - if (rc < 0) - return rc; + log_raw(device->ratbag, + "'%s' has %d features\n", + ratbag_device_get_name(device), + dev->feature_count); - if (rc > 0) { - log_raw(device->ratbag, "'%s' has %d features\n", ratbag_device_get_name(device), rc); - for (i = 0; i < rc; i++) { - log_raw(device->ratbag, "Init feature %s (0x%04x) \n", - hidpp20_feature_get_name(feature_list[i].feature), - feature_list[i].feature); - hidpp20drv_init_feature(device, feature_list[i].feature); - } + for (i = 0; i < dev->feature_count; i++) { + log_raw(device->ratbag, "Init feature %s (0x%04x) \n", + hidpp20_feature_get_name(feature_list[i].feature), + feature_list[i].feature); + hidpp20drv_init_feature(device, feature_list[i].feature); } - free(feature_list); - return 0; } diff --git a/src/hidpp20.c b/src/hidpp20.c index f02aa704..92c8ce67 100644 --- a/src/hidpp20.c +++ b/src/hidpp20.c @@ -289,8 +289,14 @@ hidpp20_feature_set_get_feature_id(struct hidpp20_device *device, return 0; } -int hidpp20_feature_set_get(struct hidpp20_device *device, - struct hidpp20_feature **feature_list) +/** + * allocates a list of features that has to be freed by the caller. + * + * returns the elements in the list or a negative error + */ +static int +hidpp20_feature_set_get(struct hidpp20_device *device, + struct hidpp20_feature **feature_list) { uint8_t feature_index, feature_type, feature_version; struct hidpp20_feature *flist; @@ -931,6 +937,12 @@ hidpp20_device_new(const struct hidpp_device *base, unsigned int idx) goto err; } + rc = hidpp20_feature_set_get(dev, &dev->feature_list); + if (rc <= 0) + goto err; + + dev->feature_count = rc; + return dev; err: free(dev); @@ -940,5 +952,6 @@ err: void hidpp20_device_destroy(struct hidpp20_device *device) { + free(device->feature_list); free(device); } diff --git a/src/hidpp20.h b/src/hidpp20.h index 2abc41bc..4645cc04 100644 --- a/src/hidpp20.h +++ b/src/hidpp20.h @@ -46,11 +46,18 @@ union hidpp20_message { uint8_t data[LONG_MESSAGE_LENGTH]; }; +struct hidpp20_feature { + uint16_t feature; + uint8_t type; +}; + struct hidpp20_device { struct hidpp_device base; unsigned int index; unsigned proto_major; unsigned proto_minor; + unsigned feature_count; + struct hidpp20_feature *feature_list; }; int hidpp20_request_command(struct hidpp20_device *dev, union hidpp20_message *msg); @@ -87,21 +94,8 @@ int hidpp20_root_get_protocol_version(struct hidpp20_device *dev, #define HIDPP_PAGE_FEATURE_SET 0x0001 -struct hidpp20_feature { - uint16_t feature; - uint8_t type; -}; - -/** - * allocates a list of features that has to be freed by the caller. - * - * returns the elements in the list or a negative error - */ -int hidpp20_feature_set_get(struct hidpp20_device *device, - struct hidpp20_feature **feature_list); - /* -------------------------------------------------------------------------- */ -/* 0x0001: Device Info */ +/* 0x0003: Device Info */ /* -------------------------------------------------------------------------- */ #define HIDPP_PAGE_DEVICE_INFO 0x0003