xref: /openbmc/qemu/hw/usb/u2f.h (revision c3e9090c)
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 U2FKeyInfo U2FKeyInfo;
3580e267f1SCésar Belley 
3680e267f1SCésar Belley #define TYPE_U2F_KEY "u2f-key"
37*c3e9090cSPhilippe Mathieu-Daudé OBJECT_DECLARE_TYPE(U2FKeyState, U2FKeyClass, U2F_KEY)
3880e267f1SCésar Belley 
3980e267f1SCésar Belley /*
4080e267f1SCésar Belley  * Callbacks to be used by the U2F key base device (i.e. hw/u2f.c)
4180e267f1SCésar Belley  * to interact with its variants (i.e. hw/u2f-*.c)
4280e267f1SCésar Belley  */
43*c3e9090cSPhilippe Mathieu-Daudé struct U2FKeyClass {
4480e267f1SCésar Belley     /*< private >*/
4580e267f1SCésar Belley     USBDeviceClass parent_class;
4680e267f1SCésar Belley 
4780e267f1SCésar Belley     /*< public >*/
4880e267f1SCésar Belley     void (*recv_from_guest)(U2FKeyState *key,
4980e267f1SCésar Belley                             const uint8_t packet[U2FHID_PACKET_SIZE]);
5080e267f1SCésar Belley     void (*realize)(U2FKeyState *key, Error **errp);
5180e267f1SCésar Belley     void (*unrealize)(U2FKeyState *key);
52*c3e9090cSPhilippe Mathieu-Daudé };
5380e267f1SCésar Belley 
5480e267f1SCésar Belley /*
5580e267f1SCésar Belley  * State of the U2F key base device (i.e. hw/u2f.c)
5680e267f1SCésar Belley  */
57*c3e9090cSPhilippe Mathieu-Daudé struct U2FKeyState {
5880e267f1SCésar Belley     USBDevice dev;
5980e267f1SCésar Belley     USBEndpoint *ep;
6080e267f1SCésar Belley     uint8_t idle;
6180e267f1SCésar Belley 
6280e267f1SCésar Belley     /* Pending packets to be send to the guest */
6380e267f1SCésar Belley     uint8_t pending_in[U2FHID_PENDING_IN_NUM][U2FHID_PACKET_SIZE];
6480e267f1SCésar Belley     uint8_t pending_in_start;
6580e267f1SCésar Belley     uint8_t pending_in_end;
6680e267f1SCésar Belley     uint8_t pending_in_num;
67*c3e9090cSPhilippe Mathieu-Daudé };
6880e267f1SCésar Belley 
6980e267f1SCésar Belley /*
7080e267f1SCésar Belley  * API to be used by the U2F key device variants (i.e. hw/u2f-*.c)
717a21bee2SDaniel P. Berrangé  * to interact with the U2F key base device (i.e. hw/u2f.c)
7280e267f1SCésar Belley  */
7380e267f1SCésar Belley void u2f_send_to_guest(U2FKeyState *key,
7480e267f1SCésar Belley                        const uint8_t packet[U2FHID_PACKET_SIZE]);
7580e267f1SCésar Belley 
7680e267f1SCésar Belley extern const VMStateDescription vmstate_u2f_key;
7780e267f1SCésar Belley 
7880e267f1SCésar Belley #define VMSTATE_U2F_KEY(_field, _state) {                            \
7980e267f1SCésar Belley     .name       = (stringify(_field)),                               \
8080e267f1SCésar Belley     .size       = sizeof(U2FKeyState),                               \
8180e267f1SCésar Belley     .vmsd       = &vmstate_u2f_key,                                  \
8280e267f1SCésar Belley     .flags      = VMS_STRUCT,                                        \
8380e267f1SCésar Belley     .offset     = vmstate_offset_value(_state, _field, U2FKeyState), \
8480e267f1SCésar Belley }
8580e267f1SCésar Belley 
8680e267f1SCésar Belley #endif /* U2F_H */
87