xref: /qemu/hw/usb/canokey.h (revision 1fe8ac35)
1 /*
2  * CanoKey QEMU device header.
3  *
4  * Copyright (c) 2021-2022 Canokeys.org <contact@canokeys.org>
5  * Written by Hongren (Zenithal) Zheng <i@zenithal.me>
6  *
7  * This code is licensed under the Apache-2.0.
8  */
9 
10 #ifndef CANOKEY_H
11 #define CANOKEY_H
12 
13 #include "hw/qdev-core.h"
14 
15 #define TYPE_CANOKEY "canokey"
16 #define CANOKEY(obj) \
17     OBJECT_CHECK(CanoKeyState, (obj), TYPE_CANOKEY)
18 
19 /*
20  * State of Canokey (i.e. hw/canokey.c)
21  */
22 
23 /* CTRL INTR BULK */
24 #define CANOKEY_EP_NUM 3
25 /* BULK/INTR IN can be up to 1352 bytes, e.g. get key info */
26 #define CANOKEY_EP_IN_BUFFER_SIZE 2048
27 /* BULK OUT can be up to 270 bytes, e.g. PIV import cert */
28 #define CANOKEY_EP_OUT_BUFFER_SIZE 512
29 
30 typedef enum {
31     CANOKEY_EP_IN_WAIT,
32     CANOKEY_EP_IN_READY,
33     CANOKEY_EP_IN_STALL
34 } CanoKeyEPState;
35 
36 typedef struct CanoKeyState {
37     USBDevice dev;
38 
39     /* IN packets from canokey device loop */
40     uint8_t ep_in[CANOKEY_EP_NUM][CANOKEY_EP_IN_BUFFER_SIZE];
41     /*
42      * See canokey_emu_transmit
43      *
44      * For large INTR IN, receive multiple data from canokey device loop
45      * in this case ep_in_size would increase with every call
46      */
47     uint32_t ep_in_size[CANOKEY_EP_NUM];
48     /*
49      * Used in canokey_handle_data
50      * for IN larger than p->iov.size, we would do multiple handle_data()
51      *
52      * The difference between ep_in_pos and ep_in_size:
53      * We first increase ep_in_size to fill ep_in buffer in device_loop,
54      * then use ep_in_pos to submit data from ep_in buffer in handle_data
55      */
56     uint32_t ep_in_pos[CANOKEY_EP_NUM];
57     CanoKeyEPState ep_in_state[CANOKEY_EP_NUM];
58 
59     /* OUT pointer to canokey recv buffer */
60     uint8_t *ep_out[CANOKEY_EP_NUM];
61     uint32_t ep_out_size[CANOKEY_EP_NUM];
62     /* For large BULK OUT, multiple write to ep_out is needed */
63     uint8_t ep_out_buffer[CANOKEY_EP_NUM][CANOKEY_EP_OUT_BUFFER_SIZE];
64 
65     /* Properties */
66     char *file; /* canokey-file */
67 } CanoKeyState;
68 
69 #endif /* CANOKEY_H */
70