2 * Copyright (c) 2020 Yubico AB. 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
11 #include <dev/usb/usb.h>
12 #include <dev/usb/usbhid.h>
30 size_t report_out_len;
32 const sigset_t *sigmaskp;
35 /* Hack to make this work with newer kernels even if /usr/include is old. */
36 #if __NetBSD_Version__ < 901000000 /* 9.1 */
37 #define USB_HID_GET_RAW _IOR('h', 1, int)
38 #define USB_HID_SET_RAW _IOW('h', 2, int)
44 struct usb_ctl_report_desc ucrd;
45 uint32_t usage_page = 0;
48 memset(&ucrd, 0, sizeof(ucrd));
50 if (ioctl(fd, IOCTL_REQ(USB_GET_REPORT_DESC), &ucrd) == -1) {
51 fido_log_error(errno, "%s: ioctl", __func__);
55 if (ucrd.ucrd_size < 0 ||
56 (size_t)ucrd.ucrd_size > sizeof(ucrd.ucrd_data) ||
57 fido_hid_get_usage(ucrd.ucrd_data, (size_t)ucrd.ucrd_size,
59 fido_log_debug("%s: fido_hid_get_usage", __func__);
63 if (usage_page != 0xf1d0)
67 * This step is not strictly necessary -- NetBSD puts fido
68 * devices into raw mode automatically by default, but in
69 * principle that might change, and this serves as a test to
70 * verify that we're running on a kernel with support for raw
71 * mode at all so we don't get confused issuing writes that try
72 * to set the report descriptor rather than transfer data on
73 * the output interrupt pipe as we need.
75 if (ioctl(fd, IOCTL_REQ(USB_HID_SET_RAW), &raw) == -1) {
76 fido_log_error(errno, "%s: unable to set raw", __func__);
84 copy_info(fido_dev_info_t *di, const char *path)
88 struct usb_device_info udi;
90 memset(di, 0, sizeof(*di));
91 memset(&udi, 0, sizeof(udi));
93 if ((fd = fido_hid_unix_open(path)) == -1 || is_fido(fd) == 0)
96 if (ioctl(fd, IOCTL_REQ(USB_GET_DEVICEINFO), &udi) == -1) {
97 fido_log_error(errno, "%s: ioctl", __func__);
101 if ((di->path = strdup(path)) == NULL ||
102 (di->manufacturer = strdup(udi.udi_vendor)) == NULL ||
103 (di->product = strdup(udi.udi_product)) == NULL)
106 di->vendor_id = (int16_t)udi.udi_vendorNo;
107 di->product_id = (int16_t)udi.udi_productNo;
111 if (fd != -1 && close(fd) == -1)
112 fido_log_error(errno, "%s: close", __func__);
116 free(di->manufacturer);
118 explicit_bzero(di, sizeof(*di));
125 fido_hid_manifest(fido_dev_info_t *devlist, size_t ilen, size_t *olen)
133 return (FIDO_OK); /* nothing to do */
135 if (devlist == NULL || olen == NULL)
136 return (FIDO_ERR_INVALID_ARGUMENT);
138 for (i = *olen = 0; i < MAX_UHID && *olen < ilen; i++) {
139 snprintf(path, sizeof(path), "/dev/uhid%zu", i);
140 if (copy_info(&devlist[*olen], path) == 0) {
141 devlist[*olen].io = (fido_dev_io_t) {
155 * Workaround for NetBSD (as of 201910) bug that loses
156 * sync of DATA0/DATA1 sequence bit across uhid open/close.
157 * Send pings until we get a response - early pings with incorrect
158 * sequence bits will be ignored as duplicate packets by the device.
161 terrible_ping_kludge(struct hid_netbsd *ctx)
167 if (sizeof(data) < ctx->report_out_len + 1)
169 for (i = 0; i < 4; i++) {
170 memset(data, 0, sizeof(data));
171 /* broadcast channel ID */
178 /* One byte ping only, Vasili */
181 fido_log_debug("%s: send ping %d", __func__, i);
182 if (fido_hid_write(ctx, data, ctx->report_out_len + 1) == -1)
184 fido_log_debug("%s: wait reply", __func__);
185 memset(&pfd, 0, sizeof(pfd));
188 if ((n = poll(&pfd, 1, 100)) == -1) {
189 fido_log_error(errno, "%s: poll", __func__);
192 fido_log_debug("%s: timed out", __func__);
195 if (fido_hid_read(ctx, data, ctx->report_out_len, 250) == -1)
198 * Ping isn't always supported on the broadcast channel,
199 * so we might get an error, but we don't care - we're
202 fido_log_xxd(data, ctx->report_out_len, "%s: got reply",
206 fido_log_debug("%s: no response", __func__);
211 fido_hid_open(const char *path)
213 struct hid_netbsd *ctx;
214 struct usb_ctl_report_desc ucrd;
217 memset(&ucrd, 0, sizeof(ucrd));
219 if ((ctx = calloc(1, sizeof(*ctx))) == NULL ||
220 (ctx->fd = fido_hid_unix_open(path)) == -1) {
225 if ((r = ioctl(ctx->fd, IOCTL_REQ(USB_GET_REPORT_DESC), &ucrd)) == -1 ||
226 ucrd.ucrd_size < 0 ||
227 (size_t)ucrd.ucrd_size > sizeof(ucrd.ucrd_data) ||
228 fido_hid_get_report_len(ucrd.ucrd_data, (size_t)ucrd.ucrd_size,
229 &ctx->report_in_len, &ctx->report_out_len) < 0) {
231 fido_log_error(errno, "%s: ioctl", __func__);
232 fido_log_debug("%s: using default report sizes", __func__);
233 ctx->report_in_len = CTAP_MAX_REPORT_LEN;
234 ctx->report_out_len = CTAP_MAX_REPORT_LEN;
238 * NetBSD has a bug that causes it to lose
239 * track of the DATA0/DATA1 sequence toggle across uhid device
240 * open and close. This is a terrible hack to work around it.
242 if (!is_fido(ctx->fd) || terrible_ping_kludge(ctx) != 0) {
251 fido_hid_close(void *handle)
253 struct hid_netbsd *ctx = handle;
255 if (close(ctx->fd) == -1)
256 fido_log_error(errno, "%s: close", __func__);
262 fido_hid_set_sigmask(void *handle, const fido_sigset_t *sigmask)
264 struct hid_netbsd *ctx = handle;
266 ctx->sigmask = *sigmask;
267 ctx->sigmaskp = &ctx->sigmask;
273 fido_hid_read(void *handle, unsigned char *buf, size_t len, int ms)
275 struct hid_netbsd *ctx = handle;
278 if (len != ctx->report_in_len) {
279 fido_log_debug("%s: len %zu", __func__, len);
283 if (fido_hid_unix_wait(ctx->fd, ms, ctx->sigmaskp) < 0) {
284 fido_log_debug("%s: fd not ready", __func__);
288 if ((r = read(ctx->fd, buf, len)) == -1) {
289 fido_log_error(errno, "%s: read", __func__);
293 if (r < 0 || (size_t)r != len) {
294 fido_log_error(errno, "%s: %zd != %zu", __func__, r, len);
302 fido_hid_write(void *handle, const unsigned char *buf, size_t len)
304 struct hid_netbsd *ctx = handle;
307 if (len != ctx->report_out_len + 1) {
308 fido_log_debug("%s: len %zu", __func__, len);
312 if ((r = write(ctx->fd, buf + 1, len - 1)) == -1) {
313 fido_log_error(errno, "%s: write", __func__);
317 if (r < 0 || (size_t)r != len - 1) {
318 fido_log_error(errno, "%s: %zd != %zu", __func__, r, len - 1);
326 fido_hid_report_in_len(void *handle)
328 struct hid_netbsd *ctx = handle;
330 return (ctx->report_in_len);
334 fido_hid_report_out_len(void *handle)
336 struct hid_netbsd *ctx = handle;
338 return (ctx->report_out_len);