/
usb_host.h
232 lines (212 loc) · 6.93 KB
/
usb_host.h
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
/**
* @file drivers/usb_host.h
*
* @section License
* Copyright (C) 2014-2016, Erik Moqvist
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* This file is part of the Simba project.
*/
#ifndef __DRIVERS_USB_HOST_H__
#define __DRIVERS_USB_HOST_H__
#include "simba.h"
/* States of a device. */
#define USB_HOST_DEVICE_STATE_NONE 0
#define USB_HOST_DEVICE_STATE_ATTACHED 1
/* Pipe types. */
#define USB_PIPE_TYPE_CONTROL 0
#define USB_PIPE_TYPE_INTERRUPT 1
#define USB_PIPE_TYPE_ISOCHRONOUS 2
#define USB_PIPE_TYPE_BULK 3
/**
* An USB device as seen by the host.
*/
struct usb_host_device_t {
int id;
int state;
int address; /* Device address on the USB bus. */
int vid;
int pid;
char *description_p;
size_t max_packet_size;
struct {
uint8_t configuration; /* Current configuration. */
struct {
struct usb_descriptor_device_t *dev_p;
struct usb_descriptor_configuration_t *conf_p;
} descriptor;
} current;
struct usb_host_driver_t *self_p;
struct usb_pipe_t *pipes[32];
/* A list of descriptors. */
struct {
size_t size;
uint8_t buf[128];
} descriptors;
};
/**
* Used to find a device driver.
*/
struct usb_host_device_driver_t {
int (*supports)(struct usb_host_device_t *);
int (*enumerate)(struct usb_host_device_t *);
struct usb_host_device_driver_t *next_p;
};
#include "usb_host_port.h"
/**
* Initialize the USB host module. This function must be called before
* calling any other function in this module.
*
* The module will only be initialized once even if this function is
* called multiple times.
*
* @return zero(0) or negative error code.
*/
int usb_host_module_init(void);
/**
* Initialize the USB host driver object from given configuration.
*
* @param[in] self_p Driver object to be initialized.
* @param[in] dev_p USB device to use.
* @param[in] devices_p An array of devices. One entry in this array
* is allocated for each USB device that is
* connected to the host.
* @param[in] length Length of the devices array.
*
* @return zero(0) or negative error code.
*/
int usb_host_init(struct usb_host_driver_t *self_p,
struct usb_device_t *dev_p,
struct usb_host_device_t *devices_p,
size_t length);
/**
* Start the USB host device using given driver object.
*
* @param[in] self_p Initialized driver object.
*
* @return zero(0) or negative error code.
*/
int usb_host_start(struct usb_host_driver_t *self_p);
/**
* Stop the USB host device referenced by driver object.
*
* @param[in] self_p Initialized driver object.
*
* @return zero(0) or negative error code.
*/
int usb_host_stop(struct usb_host_driver_t *self_p);
/**
* Add given class/vendor driver to the USB host driver.
*
* When a USB device is plugged in, its class and vendor information
* is read by the host. Those values are used to find the device
* driver for this particular device. If there is no driver, the
* device cannot be configured and will not work.
*
* @param[in] self_p Initialized driver object.
* @param[in] driver_p USB device driver to add.
*
* @return zero(0) or negative error code.
*/
int usb_host_driver_add(struct usb_host_driver_t *self_p,
struct usb_host_device_driver_t *driver_p,
void *arg_p);
/**
* Remove given class/vendor driver from the USB host driver.
*
* @param[in] self_p Initialized driver object.
* @param[in] driver_p USB device driver to remove.
*
* @return zero(0) or negative error code.
*/
int usb_host_driver_remove(struct usb_host_driver_t *self_p,
struct usb_host_device_driver_t *driver_p);
/**
* Open given device in given driver. Open a device before reading and
* writing data to it with usb_host_device_read() or
* usb_host_device_write().
*
* @param[in] self_p Initialized driver.
* @param[in] device Device to open.
*
* @return Opened device or NULL on failure.
*/
struct usb_host_device_t *
usb_host_device_open(struct usb_host_driver_t *self_p,
int device);
/**
* Close given device in given driver.
*
* @param[in] self_p Initialized driver.
* @param[in] device Device to close.
*
* @return zero(0) or negative error code.
*/
int usb_host_device_close(struct usb_host_driver_t *self_p,
int device);
/**
* Read data from given endpoint for given device.
*
* @param[in] device_p Device to read from.
* @param[in] endpoint Endpoint to read data from.
* @param[in] buf_p Buffer to read into.
* @param[in] size Number of bytes to read.
*
* @return Number of bytes read or negative error code.
*/
ssize_t usb_host_device_read(struct usb_host_device_t *device_p,
int endpoint,
void *buf_p,
size_t size);
/**
* Write data to given endpoint for given device.
*
* @param[in] device_p Device to write to.
* @param[in] endpoint Endpoint to write to.
* @param[in] buf_p Buffer to write.
* @param[in] size Number of bytes to write.
*
* @return Number of bytes written or negative error code.
*/
ssize_t usb_host_device_write(struct usb_host_device_t *device_p,
int endpoint,
const void *buf_p,
size_t size);
/**
* Perform a control transfer on endpoint zero(0).
*
* A control transfer can have up to three stages. First the setup
* stage, then an optional data stage, and at last a status stage.
*
* @param[in] device_p Device to write to.
* @param[in] setup_p Setup packet to write.
* @param[in] buf_p Buffer to read/write. May be NULL if no data shall
* be transferred.
* @param[in] size Number of bytes to read/write.
*
* @return Number of bytes read/written or negative error code.
*/
ssize_t usb_host_device_control_transfer(struct usb_host_device_t *device_p,
struct usb_setup_t *setup_p,
void *buf_p,
size_t size);
/**
* Set configuration for given device.
*
* @param[in] device_p Device to use.
* @param[in] configuration Configuration to set.
*
* @return zero(0) or negative error code.
*/
int usb_host_device_set_configuration(struct usb_host_device_t *device_p,
uint8_t configuration);
#endif