xref: /openbmc/qemu/hw/usb/u2f.h (revision 7a21bee2)
180e267f1SCésar Belley /*
280e267f1SCésar Belley  * U2F USB device.
380e267f1SCésar Belley  *
480e267f1SCésar Belley  * Copyright (c) 2020 César Belley <cesar.belley@lse.epita.fr>
580e267f1SCésar Belley  * Written by César Belley <cesar.belley@lse.epita.fr>
680e267f1SCésar Belley  *
780e267f1SCésar Belley  * Permission is hereby granted, free of charge, to any person obtaining a copy
880e267f1SCésar Belley  * of this software and associated documentation files (the "Software"), to deal
980e267f1SCésar Belley  * in the Software without restriction, including without limitation the rights
1080e267f1SCésar Belley  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1180e267f1SCésar Belley  * copies of the Software, and to permit persons to whom the Software is
1280e267f1SCésar Belley  * furnished to do so, subject to the following conditions:
1380e267f1SCésar Belley  *
1480e267f1SCésar Belley  * The above copyright notice and this permission notice shall be included in
1580e267f1SCésar Belley  * all copies or substantial portions of the Software.
1680e267f1SCésar Belley  *
1780e267f1SCésar Belley  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1880e267f1SCésar Belley  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1980e267f1SCésar Belley  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
2080e267f1SCésar Belley  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2180e267f1SCésar Belley  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2280e267f1SCésar Belley  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2380e267f1SCésar Belley  * THE SOFTWARE.
2480e267f1SCésar Belley  */
2580e267f1SCésar Belley 
2680e267f1SCésar Belley #ifndef U2F_H
2780e267f1SCésar Belley #define U2F_H
2880e267f1SCésar Belley 
2980e267f1SCésar Belley #include "hw/qdev-core.h"
3080e267f1SCésar Belley 
3180e267f1SCésar Belley #define U2FHID_PACKET_SIZE 64
3280e267f1SCésar Belley #define U2FHID_PENDING_IN_NUM 32
3380e267f1SCésar Belley 
3480e267f1SCésar Belley typedef struct U2FKeyState U2FKeyState;
3580e267f1SCésar Belley typedef struct U2FKeyInfo U2FKeyInfo;
3680e267f1SCésar Belley 
3780e267f1SCésar Belley #define TYPE_U2F_KEY "u2f-key"
3880e267f1SCésar Belley #define U2F_KEY(obj) \
3980e267f1SCésar Belley     OBJECT_CHECK(U2FKeyState, (obj), TYPE_U2F_KEY)
4080e267f1SCésar Belley #define U2F_KEY_CLASS(klass) \
4180e267f1SCésar Belley     OBJECT_CLASS_CHECK(U2FKeyClass, (klass), TYPE_U2F_KEY)
4280e267f1SCésar Belley #define U2F_KEY_GET_CLASS(obj) \
4380e267f1SCésar Belley     OBJECT_GET_CLASS(U2FKeyClass, (obj), TYPE_U2F_KEY)
4480e267f1SCésar Belley 
4580e267f1SCésar Belley /*
4680e267f1SCésar Belley  * Callbacks to be used by the U2F key base device (i.e. hw/u2f.c)
4780e267f1SCésar Belley  * to interact with its variants (i.e. hw/u2f-*.c)
4880e267f1SCésar Belley  */
4980e267f1SCésar Belley typedef struct U2FKeyClass {
5080e267f1SCésar Belley     /*< private >*/
5180e267f1SCésar Belley     USBDeviceClass parent_class;
5280e267f1SCésar Belley 
5380e267f1SCésar Belley     /*< public >*/
5480e267f1SCésar Belley     void (*recv_from_guest)(U2FKeyState *key,
5580e267f1SCésar Belley                             const uint8_t packet[U2FHID_PACKET_SIZE]);
5680e267f1SCésar Belley     void (*realize)(U2FKeyState *key, Error **errp);
5780e267f1SCésar Belley     void (*unrealize)(U2FKeyState *key);
5880e267f1SCésar Belley } U2FKeyClass;
5980e267f1SCésar Belley 
6080e267f1SCésar Belley /*
6180e267f1SCésar Belley  * State of the U2F key base device (i.e. hw/u2f.c)
6280e267f1SCésar Belley  */
6380e267f1SCésar Belley typedef struct U2FKeyState {
6480e267f1SCésar Belley     USBDevice dev;
6580e267f1SCésar Belley     USBEndpoint *ep;
6680e267f1SCésar Belley     uint8_t idle;
6780e267f1SCésar Belley 
6880e267f1SCésar Belley     /* Pending packets to be send to the guest */
6980e267f1SCésar Belley     uint8_t pending_in[U2FHID_PENDING_IN_NUM][U2FHID_PACKET_SIZE];
7080e267f1SCésar Belley     uint8_t pending_in_start;
7180e267f1SCésar Belley     uint8_t pending_in_end;
7280e267f1SCésar Belley     uint8_t pending_in_num;
7380e267f1SCésar Belley } U2FKeyState;
7480e267f1SCésar Belley 
7580e267f1SCésar Belley /*
7680e267f1SCésar Belley  * API to be used by the U2F key device variants (i.e. hw/u2f-*.c)
77*7a21bee2SDaniel P. Berrangé  * to interact with the U2F key base device (i.e. hw/u2f.c)
7880e267f1SCésar Belley  */
7980e267f1SCésar Belley void u2f_send_to_guest(U2FKeyState *key,
8080e267f1SCésar Belley                        const uint8_t packet[U2FHID_PACKET_SIZE]);
8180e267f1SCésar Belley 
8280e267f1SCésar Belley extern const VMStateDescription vmstate_u2f_key;
8380e267f1SCésar Belley 
8480e267f1SCésar Belley #define VMSTATE_U2F_KEY(_field, _state) {                            \
8580e267f1SCésar Belley     .name       = (stringify(_field)),                               \
8680e267f1SCésar Belley     .size       = sizeof(U2FKeyState),                               \
8780e267f1SCésar Belley     .vmsd       = &vmstate_u2f_key,                                  \
8880e267f1SCésar Belley     .flags      = VMS_STRUCT,                                        \
8980e267f1SCésar Belley     .offset     = vmstate_offset_value(_state, _field, U2FKeyState), \
9080e267f1SCésar Belley }
9180e267f1SCésar Belley 
9280e267f1SCésar Belley #endif /* U2F_H */
93