diff --git a/meson.build b/meson.build index 54893455..7b78a618 100644 --- a/meson.build +++ b/meson.build @@ -234,6 +234,15 @@ executable('hidpp20-dump-page', install : false, ) +#### hidpp20-reset #### +src_hidpp20_reset = [ 'tools/hidpp20-reset.c' ] +executable('hidpp20-reset', + src_hidpp20_reset, + dependencies : [ dep_libhidpp ], + include_directories : include_directories('src'), + install : false, +) + #### lur-command #### src_lur_command = [ 'tools/lur-command.c' ] executable('lur-command', diff --git a/src/hidpp20.c b/src/hidpp20.c index 388d01c3..99917d49 100644 --- a/src/hidpp20.c +++ b/src/hidpp20.c @@ -1232,11 +1232,12 @@ hidpp20_onboard_profiles_write_data(struct hidpp20_device *device, return 0; } -static int +int hidpp20_onboard_profiles_write_sector(struct hidpp20_device *device, uint16_t sector, uint16_t sector_size, - uint8_t *data) + uint8_t *data, + bool write_crc) { uint8_t feature_index; uint16_t crc; @@ -1247,8 +1248,10 @@ hidpp20_onboard_profiles_write_sector(struct hidpp20_device *device, if (feature_index == 0) return -ENOTSUP; - crc = hidpp_crc_ccitt(data, sector_size - 2); - hidpp_set_unaligned_be_u16(&data[sector_size - 2], crc); + if (write_crc) { + crc = hidpp_crc_ccitt(data, sector_size - 2); + hidpp_set_unaligned_be_u16(&data[sector_size - 2], crc); + } rc = hidpp20_onboard_profiles_write_start(device, sector, @@ -1753,7 +1756,8 @@ hidpp20_onboard_profiles_write_dict(struct hidpp20_device *device, rc = hidpp20_onboard_profiles_write_sector(device, 0x0000, sector_size, - data); + data, + true); return rc; } @@ -2154,7 +2158,7 @@ hidpp20_onboard_profiles_write_profile(struct hidpp20_device *device, memcpy(pdata->profile.name.txt, profile->name, sizeof(profile->name)); - rc = hidpp20_onboard_profiles_write_sector(device, sector, sector_size, data); + rc = hidpp20_onboard_profiles_write_sector(device, sector, sector_size, data, true); if (rc < 0) return rc; diff --git a/src/hidpp20.h b/src/hidpp20.h index db816ede..8f2ae794 100644 --- a/src/hidpp20.h +++ b/src/hidpp20.h @@ -592,6 +592,13 @@ hidpp20_onboard_profiles_read_sector(struct hidpp20_device *device, uint16_t sector_size, uint8_t *data); +int +hidpp20_onboard_profiles_write_sector(struct hidpp20_device *device, + uint16_t sector, + uint16_t sector_size, + uint8_t *data, + bool write_crc); + static inline uint8_t * hidpp20_onboard_profiles_allocate_sector(struct hidpp20_profiles *profiles) { diff --git a/tools/hidpp20-reset.c b/tools/hidpp20-reset.c new file mode 100644 index 00000000..1a579fa3 --- /dev/null +++ b/tools/hidpp20-reset.c @@ -0,0 +1,110 @@ +/* + * Copyright © 2017 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 +#include + +static inline int +reset_sector(struct hidpp20_device *dev, uint16_t sector_size, uint16_t sector) +{ + int rc = 0; + _cleanup_free_ uint8_t *data = NULL; + + data = zalloc(sector_size); + + /* this returns error 4: hardware error and is expected */ + rc = hidpp20_onboard_profiles_write_sector(dev, sector, sector_size, data, false); + if (rc == 4) + rc = 0; + + return rc; +} + +static int +reset_all_sectors(struct hidpp20_device *dev, uint16_t sector_size) +{ + uint8_t sector; + int rc = 0; + + for (sector = 1; sector < 31; sector++) { + rc = reset_sector(dev, sector_size, sector); + if (rc != 0) + break; + } + + if (!rc) + rc = reset_sector(dev, sector_size, 0); + + return rc; +} + +static void +usage(void) +{ + printf("Usage: %s [sector] /dev/hidraw0\n", program_invocation_short_name); +} + +int +main(int argc, char **argv) +{ + _cleanup_close_ int fd = -1; + const char *path; + size_t sector = 0; + struct hidpp20_device *dev = NULL; + struct hidpp_device base; + struct hidpp20_onboard_profiles_info info = { 0 }; + int rc; + + if (argc < 2 || argc > 3) { + usage(); + return 1; + } + + path = argv[argc - 1]; + fd = open(path, O_RDWR); + if (fd < 0) + error(1, errno, "Failed to open path %s", path); + + hidpp_device_init(&base, fd); + dev = hidpp20_device_new(&base, 0xff); + if (!dev) + error(1, 0, "Failed to open %s as a HID++ 2.0 device", path); + + hidpp20_onboard_profiles_get_profiles_desc(dev, &info); + + if (argc == 2) + rc = reset_all_sectors(dev, info.sector_size); + else { + sector = atoi(argv[1]); + rc = reset_sector(dev, info.sector_size, sector); + } + + hidpp20_device_destroy(dev); + + return rc; +}