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.
10 #include <dev/usb/usb.h>
25 size_t report_out_len;
29 fido_hid_manifest(fido_dev_info_t *devlist, size_t ilen, size_t *olen)
34 struct usb_device_info udi;
38 return (FIDO_OK); /* nothing to do */
40 if (devlist == NULL || olen == NULL)
41 return (FIDO_ERR_INVALID_ARGUMENT);
43 for (i = *olen = 0; i < MAX_UHID && *olen < ilen; i++) {
44 snprintf(path, sizeof(path), "/dev/fido/%zu", i);
45 if ((fd = fido_hid_unix_open(path)) == -1)
47 memset(&udi, 0, sizeof(udi));
48 if (ioctl(fd, IOCTL_REQ(USB_GET_DEVICEINFO), &udi) == -1) {
49 fido_log_error(errno, "%s: get device info %s",
52 fido_log_error(errno, "%s: close", __func__);
56 fido_log_error(errno, "%s: close", __func__);
58 fido_log_debug("%s: %s: bus = 0x%02x, addr = 0x%02x",
59 __func__, path, udi.udi_bus, udi.udi_addr);
60 fido_log_debug("%s: %s: vendor = \"%s\", product = \"%s\"",
61 __func__, path, udi.udi_vendor, udi.udi_product);
62 fido_log_debug("%s: %s: productNo = 0x%04x, vendorNo = 0x%04x, "
63 "releaseNo = 0x%04x", __func__, path, udi.udi_productNo,
64 udi.udi_vendorNo, udi.udi_releaseNo);
67 memset(di, 0, sizeof(*di));
68 di->io = (fido_dev_io_t) {
74 if ((di->path = strdup(path)) == NULL ||
75 (di->manufacturer = strdup(udi.udi_vendor)) == NULL ||
76 (di->product = strdup(udi.udi_product)) == NULL) {
78 free(di->manufacturer);
80 explicit_bzero(di, sizeof(*di));
81 return FIDO_ERR_INTERNAL;
83 di->vendor_id = (int16_t)udi.udi_vendorNo;
84 di->product_id = (int16_t)udi.udi_productNo;
92 * Workaround for OpenBSD <=6.6-current (as of 201910) bug that loses
93 * sync of DATA0/DATA1 sequence bit across uhid open/close.
94 * Send pings until we get a response - early pings with incorrect
95 * sequence bits will be ignored as duplicate packets by the device.
98 terrible_ping_kludge(struct hid_openbsd *ctx)
104 if (sizeof(data) < ctx->report_out_len + 1)
106 for (i = 0; i < 4; i++) {
107 memset(data, 0, sizeof(data));
108 /* broadcast channel ID */
115 /* One byte ping only, Vasili */
118 fido_log_debug("%s: send ping %d", __func__, i);
119 if (fido_hid_write(ctx, data, ctx->report_out_len + 1) == -1)
121 fido_log_debug("%s: wait reply", __func__);
122 memset(&pfd, 0, sizeof(pfd));
125 if ((n = poll(&pfd, 1, 100)) == -1) {
126 fido_log_error(errno, "%s: poll", __func__);
129 fido_log_debug("%s: timed out", __func__);
132 if (fido_hid_read(ctx, data, ctx->report_out_len, 250) == -1)
135 * Ping isn't always supported on the broadcast channel,
136 * so we might get an error, but we don't care - we're
139 fido_log_xxd(data, ctx->report_out_len, "%s: got reply",
143 fido_log_debug("%s: no response", __func__);
148 fido_hid_open(const char *path)
150 struct hid_openbsd *ret = NULL;
152 if ((ret = calloc(1, sizeof(*ret))) == NULL ||
153 (ret->fd = fido_hid_unix_open(path)) == -1) {
157 ret->report_in_len = ret->report_out_len = CTAP_MAX_REPORT_LEN;
158 fido_log_debug("%s: inlen = %zu outlen = %zu", __func__,
159 ret->report_in_len, ret->report_out_len);
162 * OpenBSD (as of 201910) has a bug that causes it to lose
163 * track of the DATA0/DATA1 sequence toggle across uhid device
164 * open and close. This is a terrible hack to work around it.
166 if (terrible_ping_kludge(ret) != 0) {
175 fido_hid_close(void *handle)
177 struct hid_openbsd *ctx = (struct hid_openbsd *)handle;
179 if (close(ctx->fd) == -1)
180 fido_log_error(errno, "%s: close", __func__);
186 fido_hid_set_sigmask(void *handle, const fido_sigset_t *sigmask)
191 return (FIDO_ERR_INTERNAL);
195 fido_hid_read(void *handle, unsigned char *buf, size_t len, int ms)
197 struct hid_openbsd *ctx = (struct hid_openbsd *)handle;
202 if (len != ctx->report_in_len) {
203 fido_log_debug("%s: invalid len: got %zu, want %zu", __func__,
204 len, ctx->report_in_len);
208 if ((r = read(ctx->fd, buf, len)) == -1) {
209 fido_log_error(errno, "%s: read", __func__);
213 if (r < 0 || (size_t)r != len) {
214 fido_log_debug("%s: %zd != %zu", __func__, r, len);
222 fido_hid_write(void *handle, const unsigned char *buf, size_t len)
224 struct hid_openbsd *ctx = (struct hid_openbsd *)handle;
227 if (len != ctx->report_out_len + 1) {
228 fido_log_debug("%s: invalid len: got %zu, want %zu", __func__,
229 len, ctx->report_out_len);
233 if ((r = write(ctx->fd, buf + 1, len - 1)) == -1) {
234 fido_log_error(errno, "%s: write", __func__);
238 if (r < 0 || (size_t)r != len - 1) {
239 fido_log_debug("%s: %zd != %zu", __func__, r, len - 1);
247 fido_hid_report_in_len(void *handle)
249 struct hid_openbsd *ctx = handle;
251 return (ctx->report_in_len);
255 fido_hid_report_out_len(void *handle)
257 struct hid_openbsd *ctx = handle;
259 return (ctx->report_out_len);