Software APIs
usbdev_int.h
1 // Copyright lowRISC contributors (OpenTitan project).
2 // Licensed under the Apache License, Version 2.0, see LICENSE for details.
3 // SPDX-License-Identifier: Apache-2.0
4 #ifndef OPENTITAN_SW_HOST_TESTS_USBDEV_USBDEV_STREAM_USBDEV_INT_H_
5 #define OPENTITAN_SW_HOST_TESTS_USBDEV_USBDEV_STREAM_USBDEV_INT_H_
6 #include <queue>
7 
8 #include "usb_device.h"
9 #include "usbdev_stream.h"
10 
11 // Interrupt and Bulk Transfer-based streams to usbdev; as far as the host-
12 // and device-side code at application level is concerned, these are the same.
13 //
14 // The differences are at the service/delivery level offered by the lower USB
15 // layers.
16 class USBDevInt : public USBDevStream {
17  public:
18  USBDevInt(USBDevice *dev, bool bulk, unsigned id, uint32_t transfer_bytes,
19  bool retrieve, bool check, bool send, bool verbose)
20  : USBDevStream(id, transfer_bytes, retrieve, check, send, verbose),
21  dev_(dev),
22  bulk_(bulk),
23  failed_(false),
24  inActive_(false),
25  outActive_(false),
26  xfrIn_(nullptr),
27  xfrOut_(nullptr) {}
28  /**
29  * Open an Interrupt connection to specified device interface.
30  *
31  * @param interface Interface number.
32  * @return The success of the operation.
33  */
34  bool Open(unsigned interface);
35  /**
36  * Finalize the stream, prior to shutting down.
37  */
38  virtual void Stop();
39  /**
40  * Pause the stream, prior to suspending the device.
41  */
42  virtual void Pause();
43  /**
44  * Resume stremaing.
45  */
46  virtual bool Resume();
47  /**
48  * Return a summary report of the stream settings or status.
49  *
50  * @param status Indicates whether settings or status requested.
51  * @param verbose true iff a more verbose report is required.
52  * @return Status report
53  */
54  virtual std::string Report(bool status = false, bool verbose = false) const;
55  /**
56  * Service this Interrupt stream.
57  *
58  * @return true iff the stream is still operational.
59  */
60  virtual bool Service();
61 
62  private:
63  /**
64  * Diagnostic utility function to display the content of libusb Bulk/Interrupt
65  * transfer.
66  *
67  * @param xfr The Interrupt transfer to be displayed.
68  */
69  void DumpIntTransfer(struct libusb_transfer *xfr) const;
70  /**
71  * Retrieving of IN traffic from device.
72  *
73  * @return true iff the stream is still operational.
74  */
75  bool ServiceIN();
76  /**
77  * Sending of OUT traffic to device.
78  *
79  * @return true iff the stream is still operational.
80  */
81  bool ServiceOUT();
82  /**
83  * Callback function supplied to libusb for IN transfers; transfer has
84  * completed and requires attention.
85  *
86  * @param xfr The transfer that has completed.
87  */
88  void CallbackIN(struct libusb_transfer *xfr);
89  /**
90  * Callback function supplied to libusb for OUT transfers; transfer has
91  * completed and requires attention.
92  *
93  * @param xfr The transfer that has completed.
94  */
95  void CallbackOUT(struct libusb_transfer *xfr);
96  /**
97  * Stub callback function supplied to libusb for IN transfers.
98  *
99  * @param xfr The transfer that has completed.
100  */
101  static void LIBUSB_CALL CbStubIN(struct libusb_transfer *xfr);
102  /**
103  * Stub callback function supplied to libusb for OUT transfers.
104  *
105  * @param xfr The transfer that has completed.
106  */
107  static void LIBUSB_CALL CbStubOUT(struct libusb_transfer *xfr);
108 
109  // USB device.
110  USBDevice *dev_;
111 
112  // Bulk Transfer Type?
113  bool bulk_;
114 
115  // The number of the interface being used by this stream.
116  unsigned interface_;
117 
118  // Has this stream experienced a failure?
119  bool failed_;
120 
121  // Is an IN transfer in progress?
122  bool inActive_;
123 
124  // Is an OUT transfer in progress?
125  bool outActive_;
126 
127  // Do we currently have an IN transfer?
128  struct libusb_transfer *xfrIn_;
129 
130  // Do we currently have an OUT transfer?
131  struct libusb_transfer *xfrOut_;
132 
133  // Maximum packet size for this stream.
134  uint8_t maxPacketSize_;
135 
136  // Endpoint numbers used by this stream.
137  uint8_t epIn_;
138  uint8_t epOut_;
139 
140  // Stream signature.
141  // Note: this is constructed piecemeal as the bytes are received from the
142  // device.
143  usbdev_stream_sig_t sig_;
144 
145  // No timeout at present; the device-side code is responsible for signaling
146  // test completion/failure. This may need to change for CI tests.
147  static constexpr unsigned kDataTimeout = 0U;
148 };
149 
150 #endif // OPENTITAN_SW_HOST_TESTS_USBDEV_USBDEV_STREAM_USBDEV_INT_H_