aboutsummaryrefslogtreecommitdiff
path: root/hw/usb/canokey.h
blob: 24cf304203466d4189ccaab558d61fdb7c2b7956 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
 * CanoKey QEMU device header.
 *
 * Copyright (c) 2021-2022 Canokeys.org <contact@canokeys.org>
 * Written by Hongren (Zenithal) Zheng <i@zenithal.me>
 *
 * This code is licensed under the Apache-2.0.
 */

#ifndef CANOKEY_H
#define CANOKEY_H

#include "hw/qdev-core.h"

#define TYPE_CANOKEY "canokey"
#define CANOKEY(obj) \
    OBJECT_CHECK(CanoKeyState, (obj), TYPE_CANOKEY)

/*
 * State of Canokey (i.e. hw/canokey.c)
 */

/* CTRL INTR BULK */
#define CANOKEY_EP_NUM 3
/* BULK/INTR IN can be up to 1352 bytes, e.g. get key info */
#define CANOKEY_EP_IN_BUFFER_SIZE 2048
/* BULK OUT can be up to 270 bytes, e.g. PIV import cert */
#define CANOKEY_EP_OUT_BUFFER_SIZE 512

typedef enum {
    CANOKEY_EP_IN_WAIT,
    CANOKEY_EP_IN_READY,
    CANOKEY_EP_IN_STALL
} CanoKeyEPState;

typedef struct CanoKeyState {
    USBDevice dev;

    /* IN packets from canokey device loop */
    uint8_t ep_in[CANOKEY_EP_NUM][CANOKEY_EP_IN_BUFFER_SIZE];
    /*
     * See canokey_emu_transmit
     *
     * For large INTR IN, receive multiple data from canokey device loop
     * in this case ep_in_size would increase with every call
     */
    uint32_t ep_in_size[CANOKEY_EP_NUM];
    /*
     * Used in canokey_handle_data
     * for IN larger than p->iov.size, we would do multiple handle_data()
     *
     * The difference between ep_in_pos and ep_in_size:
     * We first increase ep_in_size to fill ep_in buffer in device_loop,
     * then use ep_in_pos to submit data from ep_in buffer in handle_data
     */
    uint32_t ep_in_pos[CANOKEY_EP_NUM];
    CanoKeyEPState ep_in_state[CANOKEY_EP_NUM];

    /* OUT pointer to canokey recv buffer */
    uint8_t *ep_out[CANOKEY_EP_NUM];
    uint32_t ep_out_size[CANOKEY_EP_NUM];
    /* For large BULK OUT, multiple write to ep_out is needed */
    uint8_t ep_out_buffer[CANOKEY_EP_NUM][CANOKEY_EP_OUT_BUFFER_SIZE];

    /* Properties */
    char *file; /* canokey-file */
} CanoKeyState;

#endif /* CANOKEY_H */