xref: /openbsd/sys/dev/usb/fido.c (revision d415bd75)
1 /*	$OpenBSD: fido.c,v 1.5 2022/07/02 08:50:42 visa Exp $	*/
2 
3 /*
4  * Copyright (c) 2019 Reyk Floeter <reyk@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include "fido.h"
20 
21 #include <sys/param.h>
22 #include <sys/systm.h>
23 #include <sys/kernel.h>
24 #include <sys/malloc.h>
25 #include <sys/signalvar.h>
26 #include <sys/device.h>
27 #include <sys/ioctl.h>
28 #include <sys/conf.h>
29 #include <sys/tty.h>
30 #include <sys/proc.h>
31 #include <sys/vnode.h>
32 
33 #include <dev/usb/usb.h>
34 #include <dev/usb/usbhid.h>
35 
36 #include <dev/usb/usbdevs.h>
37 #include <dev/usb/usbdi.h>
38 #include <dev/usb/usbdi_util.h>
39 
40 #include <dev/usb/uhidev.h>
41 #include <dev/usb/uhid.h>
42 
43 int fido_match(struct device *, void *, void *);
44 
45 struct cfdriver fido_cd = {
46 	NULL, "fido", DV_DULL
47 };
48 
49 const struct cfattach fido_ca = {
50 	sizeof(struct uhid_softc),
51 	fido_match,
52 	uhid_attach,
53 	uhid_detach,
54 };
55 
56 int
57 fido_match(struct device *parent, void *match, void *aux)
58 {
59 	struct uhidev_attach_arg *uha = (struct uhidev_attach_arg *)aux;
60 	int			  size;
61 	void			 *desc;
62 	int			  ret = UMATCH_NONE;
63 
64 	if (UHIDEV_CLAIM_MULTIPLE_REPORTID(uha))
65 		return (ret);
66 
67 	/* Find the FIDO usage page and U2F collection */
68 	uhidev_get_report_desc(uha->parent, &desc, &size);
69 	if (hid_is_collection(desc, size, uha->reportid,
70 	    HID_USAGE2(HUP_FIDO, HUF_U2FHID)))
71 		ret = UMATCH_IFACECLASS;
72 
73 	return (ret);
74 }
75 
76 int
77 fidoopen(dev_t dev, int flag, int mode, struct proc *p)
78 {
79 	return (uhid_do_open(dev, flag, mode, p));
80 }
81 
82 int
83 fidoioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
84 {
85 	int	 error;
86 
87 	switch (cmd) {
88 	case FIONBIO:
89 	case FIOASYNC:
90 	case USB_GET_DEVICEINFO:
91 		break;
92 	default:
93 		/*
94 		 * Users don't need USB/HID ioctl access to fido(4) devices
95 		 * but it can still be useful for debugging by root.
96 		 */
97 		if ((error = suser(p)) != 0)
98 			return (error);
99 		break;
100 	}
101 
102 	return (uhidioctl(dev, cmd, addr, flag, p));
103 }
104