canokey.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. #ifndef CANOKEY_H
  10. #define CANOKEY_H
  11. #include "hw/qdev-core.h"
  12. #define TYPE_CANOKEY "canokey"
  13. #define CANOKEY(obj) \
  14. OBJECT_CHECK(CanoKeyState, (obj), TYPE_CANOKEY)
  15. /*
  16. * State of Canokey (i.e. hw/canokey.c)
  17. */
  18. /* CTRL INTR BULK */
  19. #define CANOKEY_EP_NUM 3
  20. /* BULK/INTR IN can be up to 1352 bytes, e.g. get key info */
  21. #define CANOKEY_EP_IN_BUFFER_SIZE 2048
  22. /* BULK OUT can be up to 270 bytes, e.g. PIV import cert */
  23. #define CANOKEY_EP_OUT_BUFFER_SIZE 512
  24. typedef enum {
  25. CANOKEY_EP_IN_WAIT,
  26. CANOKEY_EP_IN_READY,
  27. CANOKEY_EP_IN_STALL
  28. } CanoKeyEPState;
  29. typedef struct CanoKeyState {
  30. USBDevice dev;
  31. /* IN packets from canokey device loop */
  32. uint8_t ep_in[CANOKEY_EP_NUM][CANOKEY_EP_IN_BUFFER_SIZE];
  33. /*
  34. * See canokey_emu_transmit
  35. *
  36. * For large INTR IN, receive multiple data from canokey device loop
  37. * in this case ep_in_size would increase with every call
  38. */
  39. uint32_t ep_in_size[CANOKEY_EP_NUM];
  40. /*
  41. * Used in canokey_handle_data
  42. * for IN larger than p->iov.size, we would do multiple handle_data()
  43. *
  44. * The difference between ep_in_pos and ep_in_size:
  45. * We first increase ep_in_size to fill ep_in buffer in device_loop,
  46. * then use ep_in_pos to submit data from ep_in buffer in handle_data
  47. */
  48. uint32_t ep_in_pos[CANOKEY_EP_NUM];
  49. CanoKeyEPState ep_in_state[CANOKEY_EP_NUM];
  50. /* OUT pointer to canokey recv buffer */
  51. uint8_t *ep_out[CANOKEY_EP_NUM];
  52. uint32_t ep_out_size[CANOKEY_EP_NUM];
  53. /* For large BULK OUT, multiple write to ep_out is needed */
  54. uint8_t ep_out_buffer[CANOKEY_EP_NUM][CANOKEY_EP_OUT_BUFFER_SIZE];
  55. /* Properties */
  56. char *file; /* canokey-file */
  57. } CanoKeyState;
  58. #endif /* CANOKEY_H */