Initial interrupt URBs support
[minix3.git] / drivers / usbd / include / usb / hcd_common.h
blob7d3f251fbfe530c40b27adf880396df23052346f
1 /*
2 * Contains commonly used types and procedures, for HCD handling/initialization
3 * If possible, everything OS specific (IPC, virtual memory...) should be here
4 */
6 #ifndef _HCD_COMMON_H_
7 #define _HCD_COMMON_H_
9 #include <ddekit/thread.h>
10 #include <ddekit/semaphore.h>
11 #include <ddekit/usb.h>
13 #include <minix/usb.h> /* for setup structures */
14 #include <minix/usb_ch9.h> /* for descriptor structures */
17 /*===========================================================================*
18 * USB register handling defines *
19 *===========================================================================*/
20 /* Helper type used for register bitwise access */
21 #define HCD_BIT(num) (0x01u << (num))
23 /* Unsigned type that can hold all possible addresses */
24 typedef unsigned long hcd_addr;
26 /* Register types */
27 typedef unsigned long hcd_reg4;
28 typedef unsigned short hcd_reg2;
29 typedef unsigned char hcd_reg1;
31 /* For register dereferencing */
32 #define _HCD_REG4 volatile hcd_reg4 *
33 #define _HCD_REG2 volatile hcd_reg2 *
34 #define _HCD_REG1 volatile hcd_reg1 *
36 /* Scalar address to dereference */
37 #define _HCD_ADDR(base, off) (((hcd_addr)(base))+(off))
39 /* Defines for fixed size register access
40 * May cause unaligned memory access */
41 #define HCD_WR4(base, off, val) (*((_HCD_REG4)_HCD_ADDR(base, off)) = (val))
42 #define HCD_WR2(base, off, val) (*((_HCD_REG2)_HCD_ADDR(base, off)) = (val))
43 #define HCD_WR1(base, off, val) (*((_HCD_REG1)_HCD_ADDR(base, off)) = (val))
44 #define HCD_RD4(base, off) (*((_HCD_REG4)_HCD_ADDR(base, off)))
45 #define HCD_RD2(base, off) (*((_HCD_REG2)_HCD_ADDR(base, off)))
46 #define HCD_RD1(base, off) (*((_HCD_REG1)_HCD_ADDR(base, off)))
48 /* Other useful defines */
49 #define HCD_SET(val, bits) ((val)|=(bits))
50 #define HCD_CLR(val, bits) ((val)&=~(bits))
52 /* Alignment safe conversion from 'bytes' array to a word */
53 #define HCD_8TO32(bytes) (((bytes)[0]) | \
54 (((bytes)[1])<<8) | \
55 (((bytes)[2])<<16) | \
56 (((bytes)[3])<<24))
58 /* Convert type's 'sizeof' to 4-byte words count */
59 #define HCD_SIZEOF_TO_4(type) ((sizeof(type)+3)/4)
62 /*===========================================================================*
63 * USB descriptor types *
64 *===========================================================================*/
65 typedef usb_descriptor_t hcd_descriptor;
66 typedef usb_device_descriptor_t hcd_device_descriptor;
67 typedef usb_config_descriptor_t hcd_config_descriptor;
68 typedef usb_interface_descriptor_t hcd_interface_descriptor;
69 typedef usb_endpoint_descriptor_t hcd_endpoint_descriptor;
70 typedef usb_string_descriptor_t hcd_string_descriptor;
73 /*===========================================================================*
74 * HCD descriptor tree types *
75 *===========================================================================*/
76 typedef struct hcd_endpoint {
78 hcd_endpoint_descriptor descriptor;
80 hcd_endpoint;
82 typedef struct hcd_interface {
84 hcd_interface_descriptor descriptor;
85 hcd_endpoint * endpoint;
86 int num_endpoints;
88 hcd_interface;
90 typedef struct hcd_configuration {
92 hcd_config_descriptor descriptor;
93 hcd_interface * interface;
94 int num_interfaces;
96 hcd_configuration;
99 /*===========================================================================*
100 * HCD device helper types *
101 *===========================================================================*/
102 typedef void (*hcd_thread_function)(void *);
103 typedef ddekit_thread_t hcd_thread;
104 typedef ddekit_sem_t hcd_lock;
105 typedef struct hcd_driver_state hcd_driver_state;
106 typedef struct ddekit_usb_urb hcd_urb;
108 typedef enum {
110 HCD_STATE_DISCONNECTED = 0, /* default for initialization */
111 HCD_STATE_CONNECTION_PENDING,
112 HCD_STATE_CONNECTED
114 hcd_state;
116 typedef enum {
118 HCD_SPEED_LOW,
119 HCD_SPEED_FULL,
120 HCD_SPEED_HIGH,
122 hcd_speed;
124 /* Largest value that can be transfered by this driver at a time
125 * see MAXPAYLOAD in TXMAXP/RXMAXP */
126 #define MAX_WTOTALLENGTH 1024
128 typedef struct hcd_device_state {
130 hcd_driver_state * driver; /* Specific HCD driver object */
131 hcd_thread * thread;
132 hcd_lock * lock;
133 hcd_urb * urb;
134 void * data;
136 hcd_device_descriptor device_desc;
137 hcd_configuration config_tree;
138 hcd_reg1 max_packet_size;
139 hcd_speed speed;
140 hcd_state state;
141 int address;
143 /* Number of bytes received/transmitted in last transfer */
144 int data_len;
146 /* TODO: forcefully align buffer to make things clear? */
147 /* Buffer for each device to hold transfered data */
148 hcd_reg1 buffer[MAX_WTOTALLENGTH];
150 hcd_device_state;
153 /*===========================================================================*
154 * HCD event handling *
155 *===========================================================================*/
156 /* Possible USB transfer types */
157 typedef enum {
159 HCD_TRANSFER_CONTROL = UE_CONTROL,
160 HCD_TRANSFER_ISOCHRONOUS = UE_ISOCHRONOUS,
161 HCD_TRANSFER_BULK = UE_BULK,
162 HCD_TRANSFER_INTERRUPT = UE_INTERRUPT
164 hcd_transfer;
166 /* Possible USB transfer directions */
167 typedef enum {
169 HCD_DIRECTION_OUT = 0,
170 HCD_DIRECTION_IN = 1,
171 HCD_DIRECTION_UNUSED = 0xFF
173 hcd_direction;
175 /* Possible asynchronous HCD events */
176 typedef enum {
178 HCD_EVENT_CONNECTED,
179 HCD_EVENT_DISCONNECTED,
180 HCD_EVENT_ENDPOINT,
181 HCD_EVENT_URB
183 hcd_event;
185 /* EP event constants */
186 #define HCD_NO_ENDPOINT -1
187 #define HCD_ENDPOINT_0 0
190 /*===========================================================================*
191 * HCD transfer requests *
192 *===========================================================================*/
193 struct hcd_datarequest {
195 char * data;
196 int size;
197 int endpoint;
198 int direction;
199 unsigned int max_packet_size;
200 unsigned int interval;
201 hcd_speed speed;
202 hcd_transfer type;
205 typedef struct usb_ctrlrequest hcd_ctrlrequest;
206 typedef struct hcd_datarequest hcd_datarequest;
209 /*===========================================================================*
210 * Other definitions *
211 *===========================================================================*/
212 #define HCD_MILI 1000
213 #define HCD_MICRO 1000000
214 #define HCD_NANO 1000000000
215 #define HCD_NANOSLEEP_SEC(sec) ((sec) * HCD_NANO)
216 #define HCD_NANOSLEEP_MSEC(msec) ((msec) * HCD_MICRO)
217 #define HCD_NANOSLEEP_USEC(usec) ((usec) * HCD_MILI)
219 /* Default USB communication parameters */
220 #define HCD_DEFAULT_EP 0x00
221 #define HCD_DEFAULT_ADDR 0x00
223 /* TODO: one device */
224 #define HCD_ATTACHED_ADDR 0x01
227 /*===========================================================================*
228 * Operating system specific *
229 *===========================================================================*/
230 /* Generic method for registering interrupts */
231 int hcd_os_interrupt_attach(int irq, void (*init)(void *),
232 void (*isr)(void *), void *priv);
234 /* Generic method for unregistering interrupts */
235 void hcd_os_interrupt_detach(int);
237 /* Generic method for enabling interrupts */
238 void hcd_os_interrupt_enable(int);
240 /* Generic method for disabling interrupts */
241 void hcd_os_interrupt_disable(int);
243 /* Returns pointer to memory mapped for given arguments */
244 void * hcd_os_regs_init(unsigned long, unsigned long);
246 /* Unregisters mapped memory */
247 int hcd_os_regs_deinit(unsigned long, unsigned long);
249 /* Configure clocking */
250 int hcd_os_clkconf(unsigned long, unsigned long, unsigned long);
252 /* Release clocking */
253 int hcd_os_clkconf_release(void);
255 /* OS's sleep wrapper */
256 void hcd_os_nanosleep(int);
259 /*===========================================================================*
260 * Device handling calls *
261 *===========================================================================*/
262 /* Initializes device threading on connection */
263 int hcd_connect_device(hcd_device_state *, hcd_thread_function);
265 /* Cleans after device disconnection */
266 void hcd_disconnect_device(hcd_device_state *);
268 /* Locks device thread until 'hcd_device_continue' */
269 void hcd_device_wait(hcd_device_state *, hcd_event, int);
271 /* Unlocks device thread halted by 'hcd_device_wait' */
272 void hcd_device_continue(hcd_device_state *);
275 /*===========================================================================*
276 * Descriptor tree calls *
277 *===========================================================================*/
278 /* Creates descriptor tree based on given buffer */
279 int hcd_buffer_to_tree(hcd_reg1 *, int, hcd_configuration *);
281 /* Frees descriptor tree */
282 void hcd_tree_cleanup(hcd_configuration *);
284 /* Find EP in a tree */
285 hcd_endpoint * hcd_tree_find_ep(hcd_configuration *, int);
288 #endif /* !_HCD_COMMON_H_ */