Software APIs
usbdev_iso.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_ISO_H_
5 #define OPENTITAN_SW_HOST_TESTS_USBDEV_USBDEV_STREAM_USBDEV_ISO_H_
6 #include <queue>
7 
8 #include "usb_device.h"
9 #include "usbdev_stream.h"
10 
11 class USBDevIso : public USBDevStream {
12  public:
13  USBDevIso(USBDevice *dev, unsigned id, uint32_t transfer_bytes, bool retrieve,
14  bool check, bool send, bool verbose)
15  : USBDevStream(id, transfer_bytes, retrieve, check, send, verbose),
16  dev_(dev),
17  failed_(false),
18  inActive_(false),
19  outActive_(false),
20  xfrIn_(nullptr),
21  xfrOut_(nullptr) {}
22  /**
23  * Open an Isochronous connection to specified device interface.
24  *
25  * @param interface Interface number.
26  * @return The success of the operation.
27  */
28  bool Open(unsigned interface);
29  /**
30  * Finalize the stream, prior to shutting down.
31  */
32  virtual void Stop();
33  /**
34  * Pause the stream, prior to suspending the device.
35  */
36  virtual void Pause();
37  /**
38  * Resume stremaing.
39  */
40  virtual bool Resume();
41  /**
42  * Return a summary report of the stream settings or status.
43  *
44  * @param status Indicates whether settings or status requested.
45  * @param verbose true iff a more verbose report is required.
46  * @return Status report.
47  */
48  virtual std::string Report(bool status = false, bool verbose = false) const;
49  /**
50  * Indicates whether this stream has completed its transfer.
51  *
52  * @return true iff this stream has nothing more to do.
53  */
54  virtual bool Completed() const;
55  /**
56  * Service this Isochronous 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 Iso transfer.
65  *
66  * @param xfr The Isochronous transfer to be displayed.
67  */
68  void DumpIsoTransfer(struct libusb_transfer *xfr) const;
69  /**
70  * Retrieving of IN traffic from device.
71  *
72  * @return true iff the stream is still operational.
73  */
74  bool ServiceIN();
75  /**
76  * Sending of OUT traffic to device.
77  *
78  * @return true iff the stream is still operational.
79  */
80  bool ServiceOUT();
81  /**
82  * Callback function supplied to libusb for IN transfers; transfer has
83  * completed and requires attention.
84  *
85  * @param xfr The transfer that has completed.
86  */
87  void CallbackIN(struct libusb_transfer *xfr);
88  /**
89  * Callback function supplied to libusb for OUT transfers; transfer has
90  * completed and requires attention.
91  *
92  * @param xfr The transfer that has completed.
93  */
94  void CallbackOUT(struct libusb_transfer *xfr);
95  /**
96  * Stub callback function supplied to libusb for IN transfers.
97  *
98  * @param xfr The transfer that has completed.
99  */
100  static void LIBUSB_CALL CbStubIN(struct libusb_transfer *xfr);
101  /**
102  * Stub callback function supplied to libusb for OUT transfers.
103  *
104  * @param xfr The transfer that has completed.
105  */
106  static void LIBUSB_CALL CbStubOUT(struct libusb_transfer *xfr);
107 
108  // USB device.
109  USBDevice *dev_;
110 
111  // The number of the interface being used by this stream.
112  unsigned interface_;
113 
114  // Has this stream experienced a failure?
115  bool failed_;
116 
117  // Is an IN transfer in progress?
118  bool inActive_;
119 
120  // Is an OUT transfer in progress?
121  bool outActive_;
122 
123  // Do we currently have an IN transfer?
124  struct libusb_transfer *xfrIn_;
125 
126  // Do we currently have an OUT transfer?
127  struct libusb_transfer *xfrOut_;
128 
129  // Maximum packet size for this stream.
130  uint8_t maxPacketSize_;
131 
132  // Endpoint numbers used by this stream.
133  uint8_t epIn_;
134  uint8_t epOut_;
135 
136  // Expected device-side sequence number of next IN packet.
137  uint16_t tst_seq_;
138 
139  // Lengths of packets (in bytes) of the Isochronous Data packets held in the
140  // circular buffer.
141  std::queue<uint32_t> pktLen_;
142 
143  // No timeout at present; the device-side code is responsible for signaling
144  // test completion/failure. This may need to change for CI tests.
145  static constexpr unsigned kIsoTimeout = 0U;
146 
147  // Since the USB device is Full Speed it supports only one Isochronous
148  // Data packet per bus frame.
149  static constexpr unsigned kNumIsoPackets = 1U;
150 };
151 
152 #endif // OPENTITAN_SW_HOST_TESTS_USBDEV_USBDEV_STREAM_USBDEV_ISO_H_