6 #include <kernel/list.h>
8 #define USB_HOST_ERR_NONE 0
9 #define USB_HOST_ERR_BITSTUFF 1
10 #define USB_HOST_ERR_TIMEOUT 2
11 #define USB_HOST_ERR_NAK 3
12 #define USB_HOST_ERR_BABBLE 4
13 #define USB_HOST_ERR_BUFFER 5
14 #define USB_HOST_ERR_STALL 6
15 #define USB_HOST_ERR_NODEV 7
17 #define make_usb_pid(x) ((x) | ((~(x)) << 4))
19 /* token packets... */
20 #define USB_PID_OUT make_usb_pid(1)
21 #define USB_PID_IN make_usb_pid(9)
22 #define USB_PID_SOF make_usb_pid(5)
23 #define USB_PID_SETUP make_usb_pid(13)
25 #define USB_PID_DATA0 make_usb_pid(3)
26 #define USB_PID_DATA1 make_usb_pid(11)
27 #define USB_PID_DATA2 make_usb_pid(7)
28 #define USB_PID_MDATA make_usb_pid(15)
29 /* handshake packets.. */
30 #define USB_PID_ACK make_usb_pid(2)
31 #define USB_PID_NAK make_usb_pid(10)
32 #define USB_PID_STALL make_usb_pid(14)
33 #define USB_PID_NYET make_usb_pid(6)
35 #define USB_PID_PRE make_usb_pid(12)
36 #define USB_PID_ERR make_usb_pid(12)
37 #define USB_PID_SPLIT make_usb_pid(8)
38 #define USB_PID_PING make_usb_pid(4)
40 /* the standard setup requests */
41 #define REQ_STD_GET_STATUS 0
42 #define REQ_STD_CLR_FEAT 1
43 #define REQ_STD_SET_FEAT 3
44 #define REQ_STD_SET_ADDRESS 5
45 #define REQ_STD_GET_DESC 6
46 #define REQ_STD_SET_DESC 7
47 #define REQ_STD_GET_CONFIG 8
48 #define REQ_STD_SET_CONFIG 9
49 #define REQ_STD_GET_IFACE 10
50 #define REQ_STD_SET_IFACE 11
51 #define REQ_STD_SYNCH_FRAME 12
54 #define USB_TOKEN_SETUP 0x00
55 #define USB_TOKEN_IN 0x80
56 #define USB_TOKEN_OUT 0x90
60 typedef void *host_info;
61 typedef void *host_eop_info;
62 typedef void *host_dev_info;
63 typedef void *class_info;
69 int class_id, subclass_id;
77 struct list_elem class_peers; /* peers on class */
78 struct list endpoints;
80 struct list_elem peers; /* peers on device */
84 #define USB_SPEED_1_1 1
90 int (*detect_change) (host_info);
91 int (*tx_pkt) (host_eop_info, int pid, void *pkt,
92 int min_sz, int max_sz, int *in_sz, bool wait);
94 host_eop_info (*create_eop)(host_dev_info, int eop, int maxpkt);
95 void (*remove_eop)(host_eop_info);
97 host_dev_info (*create_dev_channel) (host_info, int dev_addr, int ver);
98 void (*modify_dev_channel) (host_dev_info, int dev_addr, int ver);
99 void (*remove_dev_channel) (host_dev_info);
101 void (*set_toggle) (host_eop_info, int toggle);
110 /* when a device of this class is attached, the device is passed in */
111 /* returns private info on device */
112 void *(*attached) (struct usb_iface *);
114 /* device is detached -> detached(dev_info) */
115 void (*detached) (class_info info);
118 #define USB_VERSION_1_0 0x100
119 #define USB_VERSION_1_1 0x110
120 #define USB_VERSION_2 0x200
122 #define USB_EOP_ATTR_CTL 0 /* control */
123 #define USB_EOP_ATTR_ISO 1 /* isochronous */
124 #define USB_EOP_ATTR_BULK 2 /* bulk */
125 #define USB_EOP_ATTR_INT 3 /* interrupt */
128 int eop; /* end point address */
130 int direction; /* 0 = host->dev, 1=dev->host */
133 struct usb_iface *iface;
135 struct list_elem peers;
142 int class_id, subclass_id;
143 uint16_t product_id, vendor_id, device_id;
149 int pwr; /* power draw for this config */
151 struct list interfaces;
153 struct list_elem host_peers; /* peers on host */
154 struct list_elem sys_peers; /* list for all devices */
156 struct usb_iface default_iface;
157 struct usb_endpoint cfg_eop;
160 host_eop_info h_cfg_eop; /* configuration EOP */
164 /* pg276 usb_20.pdf */
165 #define USB_SETUP_TYPE_STD 0
166 #define USB_SETUP_TYPE_CLASS 1
167 #define USB_SETUP_TYPE_VENDOR 2
169 #define USB_SETUP_RECIP_DEV 0
170 #define USB_SETUP_RECIP_IFACE 1
171 #define USB_SETUP_RECIP_ENDPT 2
172 #define USB_SETUP_RECIP_OTHER 3
179 uint8_t direction:1; /* 0 = host->dev, 1 = dev->host */
187 void usb_init (void);
188 void usb_storage_init (void);
190 void usb_register_host (struct usb_host *, host_info info);
191 int usb_unregister_host (struct usb_host *, host_info info);
192 int usb_register_class (struct usb_class *);
193 int usb_unregister_class (struct usb_class *);
195 int usb_dev_bulk (struct usb_endpoint *eop, void *buf, int sz, int *tx);
196 int usb_dev_setup (struct usb_endpoint *eop, bool in,
197 struct usb_setup_pkt *s, void *buf, int sz);
198 int usb_dev_wait_int (struct usb_dev *);