xref: /openbmc/qemu/hw/usb/u2f.h (revision a6caeee8)
1 /*
2  * U2F USB device.
3  *
4  * Copyright (c) 2020 César Belley <cesar.belley@lse.epita.fr>
5  * Written by César Belley <cesar.belley@lse.epita.fr>
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23  * THE SOFTWARE.
24  */
25 
26 #ifndef U2F_H
27 #define U2F_H
28 
29 #include "hw/qdev-core.h"
30 
31 #define U2FHID_PACKET_SIZE 64
32 #define U2FHID_PENDING_IN_NUM 32
33 
34 typedef struct U2FKeyState U2FKeyState;
35 typedef struct U2FKeyInfo U2FKeyInfo;
36 
37 #define TYPE_U2F_KEY "u2f-key"
38 #define U2F_KEY(obj) \
39     OBJECT_CHECK(U2FKeyState, (obj), TYPE_U2F_KEY)
40 #define U2F_KEY_CLASS(klass) \
41     OBJECT_CLASS_CHECK(U2FKeyClass, (klass), TYPE_U2F_KEY)
42 #define U2F_KEY_GET_CLASS(obj) \
43     OBJECT_GET_CLASS(U2FKeyClass, (obj), TYPE_U2F_KEY)
44 
45 /*
46  * Callbacks to be used by the U2F key base device (i.e. hw/u2f.c)
47  * to interact with its variants (i.e. hw/u2f-*.c)
48  */
49 typedef struct U2FKeyClass {
50     /*< private >*/
51     USBDeviceClass parent_class;
52 
53     /*< public >*/
54     void (*recv_from_guest)(U2FKeyState *key,
55                             const uint8_t packet[U2FHID_PACKET_SIZE]);
56     void (*realize)(U2FKeyState *key, Error **errp);
57     void (*unrealize)(U2FKeyState *key);
58 } U2FKeyClass;
59 
60 /*
61  * State of the U2F key base device (i.e. hw/u2f.c)
62  */
63 typedef struct U2FKeyState {
64     USBDevice dev;
65     USBEndpoint *ep;
66     uint8_t idle;
67 
68     /* Pending packets to be send to the guest */
69     uint8_t pending_in[U2FHID_PENDING_IN_NUM][U2FHID_PACKET_SIZE];
70     uint8_t pending_in_start;
71     uint8_t pending_in_end;
72     uint8_t pending_in_num;
73 } U2FKeyState;
74 
75 /*
76  * API to be used by the U2F key device variants (i.e. hw/u2f-*.c)
77  * to interact with the the U2F key base device (i.e. hw/u2f.c)
78  */
79 void u2f_send_to_guest(U2FKeyState *key,
80                        const uint8_t packet[U2FHID_PACKET_SIZE]);
81 
82 extern const VMStateDescription vmstate_u2f_key;
83 
84 #define VMSTATE_U2F_KEY(_field, _state) {                            \
85     .name       = (stringify(_field)),                               \
86     .size       = sizeof(U2FKeyState),                               \
87     .vmsd       = &vmstate_u2f_key,                                  \
88     .flags      = VMS_STRUCT,                                        \
89     .offset     = vmstate_offset_value(_state, _field, U2FKeyState), \
90 }
91 
92 #endif /* U2F_H */
93