2 * Copyright (c) 2019 Google LLC. All rights reserved.
3 * Use of this source code is governed by a BSD-style
4 * license that can be found in the LICENSE file.
5 * SPDX-License-Identifier: BSD-2-Clause
10 #include <linux/hidraw.h>
11 #include <linux/input.h>
24 size_t report_out_len;
28 fido_wcslen(const wchar_t *wcs)
31 while (*wcs++ != L'\0')
37 wcs_to_cs(const wchar_t *wcs)
42 if (wcs == NULL || (cs = calloc(fido_wcslen(wcs) + 1, 1)) == NULL)
45 for (i = 0; i < fido_wcslen(wcs); i++) {
47 /* give up on parsing non-ASCII text */
49 return strdup("hidapi device");
58 copy_info(fido_dev_info_t *di, const struct hid_device_info *d)
60 memset(di, 0, sizeof(*di));
63 di->path = strdup(d->path);
65 di->path = strdup("");
67 if (d->manufacturer_string != NULL)
68 di->manufacturer = wcs_to_cs(d->manufacturer_string);
70 di->manufacturer = strdup("");
72 if (d->product_string != NULL)
73 di->product = wcs_to_cs(d->product_string);
75 di->product = strdup("");
77 if (di->path == NULL ||
78 di->manufacturer == NULL ||
79 di->product == NULL) {
81 free(di->manufacturer);
83 explicit_bzero(di, sizeof(*di));
87 di->product_id = (int16_t)d->product_id;
88 di->vendor_id = (int16_t)d->vendor_id;
89 di->io = (fido_dev_io_t) {
101 get_report_descriptor(const char *path, struct hidraw_report_descriptor *hrd)
107 if ((fd = fido_hid_unix_open(path)) == -1) {
108 fido_log_debug("%s: fido_hid_unix_open", __func__);
112 if (ioctl(fd, IOCTL_REQ(HIDIOCGRDESCSIZE), &s) < 0 || s < 0 ||
113 (unsigned)s > HID_MAX_DESCRIPTOR_SIZE) {
114 fido_log_error(errno, "%s: ioctl HIDIOCGRDESCSIZE", __func__);
118 hrd->size = (unsigned)s;
120 if (ioctl(fd, IOCTL_REQ(HIDIOCGRDESC), hrd) < 0) {
121 fido_log_error(errno, "%s: ioctl HIDIOCGRDESC", __func__);
134 is_fido(const struct hid_device_info *hdi)
136 uint32_t usage_page = 0;
137 struct hidraw_report_descriptor *hrd;
139 if ((hrd = calloc(1, sizeof(*hrd))) == NULL ||
140 get_report_descriptor(hdi->path, hrd) < 0 ||
141 fido_hid_get_usage(hrd->value, hrd->size, &usage_page) < 0)
146 return usage_page == 0xf1d0;
148 #elif defined(_WIN32) || defined(__APPLE__)
150 is_fido(const struct hid_device_info *hdi)
152 return hdi->usage_page == 0xf1d0;
156 is_fido(const struct hid_device_info *hdi)
159 fido_log_debug("%s: assuming FIDO HID", __func__);
165 fido_hid_open(const char *path)
167 struct hid_hidapi *ctx;
169 if ((ctx = calloc(1, sizeof(*ctx))) == NULL) {
173 if ((ctx->handle = hid_open_path(path)) == NULL) {
178 ctx->report_in_len = ctx->report_out_len = CTAP_MAX_REPORT_LEN;
184 fido_hid_close(void *handle)
186 struct hid_hidapi *ctx = handle;
188 hid_close(ctx->handle);
193 fido_hid_set_sigmask(void *handle, const fido_sigset_t *sigmask)
198 return (FIDO_ERR_INTERNAL);
202 fido_hid_read(void *handle, unsigned char *buf, size_t len, int ms)
204 struct hid_hidapi *ctx = handle;
206 if (len != ctx->report_in_len) {
207 fido_log_debug("%s: len %zu", __func__, len);
211 return hid_read_timeout(ctx->handle, buf, len, ms);
215 fido_hid_write(void *handle, const unsigned char *buf, size_t len)
217 struct hid_hidapi *ctx = handle;
219 if (len != ctx->report_out_len + 1) {
220 fido_log_debug("%s: len %zu", __func__, len);
224 return hid_write(ctx->handle, buf, len);
228 fido_hid_manifest(fido_dev_info_t *devlist, size_t ilen, size_t *olen)
230 struct hid_device_info *hdi;
235 return FIDO_OK; /* nothing to do */
237 return FIDO_ERR_INVALID_ARGUMENT;
238 if ((hdi = hid_enumerate(0, 0)) == NULL)
239 return FIDO_OK; /* nothing to do */
241 for (struct hid_device_info *d = hdi; d != NULL; d = d->next) {
242 if (is_fido(d) == false)
244 if (copy_info(&devlist[*olen], d) == 0) {
245 if (++(*olen) == ilen)
250 hid_free_enumeration(hdi);
256 fido_hid_report_in_len(void *handle)
258 struct hid_hidapi *ctx = handle;
260 return (ctx->report_in_len);
264 fido_hid_report_out_len(void *handle)
266 struct hid_hidapi *ctx = handle;
268 return (ctx->report_out_len);