summaryrefslogtreecommitdiff
path: root/docs/en/api/api_host.rst
blob: 7499d188dd784c049fecc09d51a521cad6929462 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
Host Protocol Stack
=======================================

For the naming, classification, and member composition of structures in the host protocol stack, refer to the following two diagrams:

.. figure:: img/api_host1.png
.. figure:: img/api_host2.png

CORE
-----------------

CLASS Driver Information Structure
""""""""""""""""""""""""""""""""""""

.. code-block:: C

    struct usbh_class_info {
        uint8_t match_flags;           /* Used for product specific matches; range is inclusive */
        uint8_t bInterfaceClass;       /* Base device class code */
        uint8_t bInterfaceSubClass;    /* Sub-class, depends on base class. Eg. */
        uint8_t bInterfaceProtocol;    /* Protocol, depends on base class. Eg. */
        const uint16_t (*id_table)[2]; /* List of Vendor/Product ID pairs */
        const struct usbh_class_driver *class_driver;
    };

Endpoint Structure
""""""""""""""""""""""""""""""""""""

.. code-block:: C

    struct usbh_endpoint {
        struct usb_endpoint_descriptor ep_desc;
    };

Interface Altsetting Structure
""""""""""""""""""""""""""""""""""""

.. code-block:: C

    struct usbh_interface_altsetting {
        struct usb_interface_descriptor intf_desc;
        struct usbh_endpoint ep[CONFIG_USBHOST_MAX_ENDPOINTS];
    };

Interface Structure
""""""""""""""""""""""""""""""""""""

.. code-block:: C

    struct usbh_interface {
        char devname[CONFIG_USBHOST_DEV_NAMELEN];
        struct usbh_class_driver *class_driver;
        void *priv;
        struct usbh_interface_altsetting altsetting[CONFIG_USBHOST_MAX_INTF_ALTSETTINGS];
        uint8_t altsetting_num;
    };

Configuration Structure
""""""""""""""""""""""""""""""""""""

.. code-block:: C

    struct usbh_configuration {
        struct usb_configuration_descriptor config_desc;
        struct usbh_interface intf[CONFIG_USBHOST_MAX_INTERFACES];
    };

hubport Structure
""""""""""""""""""""""""""""""""""""

.. code-block:: C

    struct usbh_hubport {
        bool connected;   /* True: device connected; false: disconnected */
        uint8_t port;     /* Hub port index */
        uint8_t dev_addr; /* device address */
        uint8_t speed;    /* device speed */
        uint8_t depth;    /* distance from root hub */
        uint8_t route;    /* route string */
        uint8_t slot_id;  /* slot id */
        struct usb_device_descriptor device_desc;
        struct usbh_configuration config;
        const char *iManufacturer;
        const char *iProduct;
        const char *iSerialNumber;
        uint8_t *raw_config_desc;
        struct usb_setup_packet *setup;
        struct usbh_hub *parent;
        struct usbh_hub *self; /* if this hubport is a hub */
        struct usbh_bus *bus;
        struct usb_endpoint_descriptor ep0;
        struct usbh_urb ep0_urb;
        usb_osal_mutex_t mutex;
    };

hub Structure
""""""""""""""""""""""""""""""""""""

.. code-block:: C

    struct usbh_hub {
        bool connected;
        bool is_roothub;
        uint8_t index;
        uint8_t hub_addr;
        uint8_t speed;
        uint8_t nports;
        uint8_t powerdelay;
        uint8_t tt_think;
        bool ismtt;
        struct usb_hub_descriptor hub_desc; /* USB 2.0 only */
        struct usb_hub_ss_descriptor hub_ss_desc; /* USB 3.0 only */
        struct usbh_hubport child[CONFIG_USBHOST_MAX_EHPORTS];
        struct usbh_hubport *parent;
        struct usbh_bus *bus;
        struct usb_endpoint_descriptor *intin;
        struct usbh_urb intin_urb;
        uint8_t *int_buffer;
        struct usb_osal_timer *int_timer;
    };

usbh_initialize
""""""""""""""""""""""""""""""""""""

``usbh_initialize`` is used to initialize the USB host protocol stack, including: initializing the USB host controller, creating roothub device, creating hub detection thread.

.. code-block:: C

    int usbh_initialize(uint8_t busid, uint32_t reg_base, usbh_event_handler_t event_handler);

- **busid**  bus id, starting from 0, cannot exceed `CONFIG_USBHOST_MAX_BUS`
- **reg_base**  hcd register base address
- **event_handler**  host event callback function, can be NULL
- **return**  0 indicates normal, other values indicate error

usbh_find_class_instance
""""""""""""""""""""""""""""""""""""

``usbh_find_class_instance`` finds the corresponding class structure handle based on the registered class name.

.. code-block:: C

    void *usbh_find_class_instance(const char *devname);

- **devname**  class name
- **return**  class structure handle

lsusb
""""""""""""""""""""""""""""""""""""

``lsusb`` is used to view and operate device information on the hub. Requires shell plugin to use.

.. code-block:: C

    int lsusb(int argc, char **argv);

SERIAL
-----------------

usbh_serial_open
""""""""""""""""""""""""""""""""""""

``usbh_serial_open`` opens a serial device according to the path.

.. code-block:: C

    struct usbh_serial *usbh_serial_open(const char *devname, uint32_t open_flags);

- **devname**  serial path
- **open_flags**  open flags, refer to `USBH_SERIAL_OFLAG_*` definitions
- **return**  serial structure handle

usbh_serial_close
""""""""""""""""""""""""""""""""""""

``usbh_serial_close`` closes the serial device.

.. code-block:: C

    void usbh_serial_close(struct usbh_serial *serial);

- **serial**  serial structure handle

usbh_serial_control
""""""""""""""""""""""""""""""""""""

``usbh_serial_control`` configures the serial port.

.. code-block:: C

    int usbh_serial_control(struct usbh_serial *serial, int cmd, void *arg);

- **serial**  serial structure handle
- **cmd**  control command, refer to `USBH_SERIAL_CMD_*` definitions
- **arg**  control parameter pointer
- **return**  0 indicates normal, other values indicate error

usbh_serial_write
""""""""""""""""""""""""""""""""""""

``usbh_serial_write`` writes data to the serial port.

.. code-block:: C

    int usbh_serial_write(struct usbh_serial *serial, const void *buffer, uint32_t buflen);

- **serial**  serial structure handle
- **buffer**  data buffer pointer
- **buflen**  length of data to write
- **return**  actual length of data written or error code

.. note:: If CONFIG_USB_DCACHE_ENABLE is not enabled, buffer needs to be in nocache area, otherwise it needs to be aligned to CONFIG_USB_ALIGN_SIZE area.

usbh_serial_read
""""""""""""""""""""""""""""""""""""

``usbh_serial_read`` reads data from the serial port. **If baud rate is not set, this API is not allowed to be used. After setting baud rate, rx reception will be enabled internally and data will be written to ringbuf**.

.. code-block:: C

    int usbh_serial_read(struct usbh_serial *serial, void *buffer, uint32_t buflen);

- **serial**  serial structure handle
- **buffer**  data buffer pointer
- **buflen**  maximum length of data to read
- **return**  actual length of data read or error code

.. note:: Since ringbuffer is used internally, there are no restrictions on user buffer attributes.

usbh_serial_cdc_write_async
""""""""""""""""""""""""""""""""""""

``usbh_serial_cdc_write_async`` asynchronously writes data to the serial port. **If baud rate is set, this API is not allowed to be used**.

.. code-block:: C

    int usbh_serial_cdc_write_async(struct usbh_serial *serial, uint8_t *buffer, uint32_t buflen, usbh_complete_callback_t complete, void *arg);

- **serial**  serial structure handle
- **buffer**  data buffer pointer
- **buflen**  length of data to send
- **complete**  data write completion callback function
- **arg**  callback function parameter
- **return**  0 indicates normal, other values indicate error

.. note:: If CONFIG_USB_DCACHE_ENABLE is not enabled, buffer needs to be in nocache area, otherwise it needs to be aligned to CONFIG_USB_ALIGN_SIZE area.

usbh_serial_cdc_read_async
""""""""""""""""""""""""""""""""""""

``usbh_serial_cdc_read_async`` asynchronously reads data from the serial port. **If baud rate is set, this API is not allowed to be used. After setting baud rate, rx reception will be enabled internally and data will be written to ringbuf**.

.. code-block:: C

    int usbh_serial_cdc_read_async(struct usbh_serial *serial, uint8_t *buffer, uint32_t buflen, usbh_complete_callback_t complete, void *arg);

- **serial**  serial structure handle
- **buffer**  data buffer pointer
- **buflen**  maximum length of data to read, up to 16K at a time. Must be a multiple of wMaxPacketSize
- **complete**  data read completion callback function
- **arg**  callback function parameter
- **return**  0 indicates normal, other values indicate error

.. note:: If CONFIG_USB_DCACHE_ENABLE is not enabled, buffer needs to be in nocache area, otherwise it needs to be aligned to CONFIG_USB_ALIGN_SIZE area.

HID
-----------------

MSC
-----------------

usbh_msc_scsi_init
""""""""""""""""""""""""""""""""""""

``usbh_msc_scsi_init`` initializes msc scsi device. Gets MSC status and capacity information.

.. code-block:: C

    int usbh_msc_scsi_init(struct usbh_msc *msc_class);

- **msc_class**  msc structure handle
- **return**  0 indicates normal, other values indicate error

usbh_msc_scsi_write10
""""""""""""""""""""""""""""""""""""

``usbh_msc_scsi_write10`` writes data to msc device.

.. code-block:: C

    int usbh_msc_scsi_write10(struct usbh_msc *msc_class, uint32_t start_sector, const uint8_t *buffer, uint32_t nsectors);

- **msc_class**  msc structure handle
- **start_sector**  starting sector
- **buffer**  data buffer pointer
- **nsectors**  number of sectors to write
- **return**  returns 0 for normal, other values indicate error

usbh_msc_scsi_read10
""""""""""""""""""""""""""""""""""""

``usbh_msc_scsi_read10`` reads data from msc device.

.. code-block:: C

    int usbh_msc_scsi_read10(struct usbh_msc *msc_class, uint32_t start_sector, uint8_t *buffer, uint32_t nsectors);

- **msc_class**  msc structure handle
- **start_sector**  starting sector
- **buffer**  data buffer pointer
- **nsectors**  number of sectors to read
- **return**  returns 0 for normal, other values indicate error

NETWORK
-----------------

Already integrated with lwIP protocol stack or other network protocol stacks, use socket API.