From 9cc323d80f98c8cd37e2bef50884a040e598d03c Mon Sep 17 00:00:00 2001 From: Benjamin Tissoires Date: Wed, 25 Nov 2015 18:16:36 +0100 Subject: [PATCH] hidpp: extract hidpp20_crc_ccitt into hidpp_generic Looks like the hid++ 1.0 devices are also using the same crc computation, so put it in the generic part. Signed-off-by: Benjamin Tissoires --- src/hidpp-generic.c | 27 +++++++++++++++++++++++++++ src/hidpp-generic.h | 2 ++ src/hidpp20.c | 31 ++----------------------------- 3 files changed, 31 insertions(+), 29 deletions(-) diff --git a/src/hidpp-generic.c b/src/hidpp-generic.c index f7c5f985..04b7eacb 100644 --- a/src/hidpp-generic.c +++ b/src/hidpp-generic.c @@ -317,3 +317,30 @@ hidpp_device_set_log_handler(struct hidpp_device *dev, dev->log_priority = priority; dev->userdata = userdata; } + +/* + * The following crc computation has been provided by Logitech + */ +#define CRC_CCITT_SEED 0xFFFF + +uint16_t +hidpp_crc_ccitt(uint8_t *data, unsigned int length) +{ + uint16_t crc, temp, quick; + unsigned int i; + + crc = CRC_CCITT_SEED; + + for (i = 0; i < length; i++) { + temp = (crc >> 8) ^ (*data++); + crc <<= 8; + quick = temp ^ (temp >> 4); + crc ^= quick; + quick <<= 5; + crc ^= quick; + quick <<= 7; + crc ^= quick; + } + + return crc; +} diff --git a/src/hidpp-generic.h b/src/hidpp-generic.h index 3f16fb6e..1ac36fee 100644 --- a/src/hidpp-generic.h +++ b/src/hidpp-generic.h @@ -144,6 +144,8 @@ hidpp_log_buffer(struct hidpp_device *dev, #define hidpp_log_buf_info(li_, h_, buf_, len_) hidpp_log_buffer(li_, HIDPP_LOG_PRIORITY_INFO, h_, buf_, len_) #define hidpp_log_buf_error(li_, h_, buf_, len_) hidpp_log_buffer(li_, HIDPP_LOG_PRIORITY_ERROR, h_, buf_, len_) +uint16_t hidpp_crc_ccitt(uint8_t *data, unsigned int length); + static inline uint16_t hidpp_get_unaligned_be_u16(uint8_t *buf) { diff --git a/src/hidpp20.c b/src/hidpp20.c index 0140a6f3..6ab00c17 100644 --- a/src/hidpp20.c +++ b/src/hidpp20.c @@ -920,33 +920,6 @@ int hidpp20_adjustable_dpi_set_sensor_dpi(struct hidpp20_device *device, #define HIDPP20_PROFILE_SIZE HIDPP20_PAGE_SIZE #define HIDPP20_BUTTON_HID 0x80 -/* - * The following crc computation has been provided by Logitech - */ -#define CRC_CCITT_SEED 0xFFFF - -static uint16_t -hidpp20_crc_ccitt(uint8_t *data, unsigned int length) -{ - uint16_t crc, temp, quick; - unsigned int i; - - crc = CRC_CCITT_SEED; - - for (i = 0; i < length; i++) { - temp = (crc >> 8) ^ (*data++); - crc <<= 8; - quick = temp ^ (temp >> 4); - crc ^= quick; - quick <<= 5; - crc ^= quick; - quick <<= 7; - crc ^= quick; - } - - return crc; -} - int hidpp20_onboard_profiles_read_memory(struct hidpp20_device *device, uint8_t read_rom, @@ -1097,7 +1070,7 @@ hidpp20_onboard_profiles_write_page(struct hidpp20_device *device, { uint16_t crc; - crc = hidpp20_crc_ccitt(data, HIDPP20_PAGE_SIZE - 2); + crc = hidpp_crc_ccitt(data, HIDPP20_PAGE_SIZE - 2); hidpp_set_unaligned_be_u16(&data[HIDPP20_PROFILE_SIZE - 2], crc); return hidpp20_onboard_profiles_write_data(device, page, 0x00, data, HIDPP20_PAGE_SIZE); @@ -1228,7 +1201,7 @@ hidpp20_onboard_profiles_find_and_read_profile(struct hidpp20_device *device, return rc; } - crc = hidpp20_crc_ccitt(data, HIDPP20_PROFILE_SIZE - 2); + crc = hidpp_crc_ccitt(data, HIDPP20_PROFILE_SIZE - 2); read_crc = hidpp_get_unaligned_be_u16(&data[HIDPP20_PROFILE_SIZE - 2]); if (crc == read_crc)