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
16 #define USB_HOST_ERR_NOMEM 8
18 #define make_usb_pid(x) ((x) | ((~(x)) << 4))
20 /* token packets... */
21 #define USB_PID_OUT make_usb_pid(1)
22 #define USB_PID_IN make_usb_pid(9)
23 #define USB_PID_SOF make_usb_pid(5)
24 #define USB_PID_SETUP make_usb_pid(13)
26 #define USB_PID_DATA0 make_usb_pid(3)
27 #define USB_PID_DATA1 make_usb_pid(11)
28 #define USB_PID_DATA2 make_usb_pid(7)
29 #define USB_PID_MDATA make_usb_pid(15)
30 /* handshake packets.. */
31 #define USB_PID_ACK make_usb_pid(2)
32 #define USB_PID_NAK make_usb_pid(10)
33 #define USB_PID_STALL make_usb_pid(14)
34 #define USB_PID_NYET make_usb_pid(6)
36 #define USB_PID_PRE make_usb_pid(12)
37 #define USB_PID_ERR make_usb_pid(12)
38 #define USB_PID_SPLIT make_usb_pid(8)
39 #define USB_PID_PING make_usb_pid(4)
41 /* the standard setup requests */
42 #define REQ_STD_GET_STATUS 0
43 #define REQ_STD_CLR_FEAT 1
44 #define REQ_STD_SET_FEAT 3
45 #define REQ_STD_SET_ADDRESS 5
46 #define REQ_STD_GET_DESC 6
47 #define REQ_STD_SET_DESC 7
48 #define REQ_STD_GET_CONFIG 8
49 #define REQ_STD_SET_CONFIG 9
50 #define REQ_STD_GET_IFACE 10
51 #define REQ_STD_SET_IFACE 11
52 #define REQ_STD_SYNCH_FRAME 12
55 #define USB_TOKEN_SETUP 0x00
56 #define USB_TOKEN_IN 0x80
57 #define USB_TOKEN_OUT 0x90
62 typedef void *host_info;
63 typedef void *host_eop_info;
64 typedef void *host_dev_info;
65 typedef void *class_info;
71 int class_id, subclass_id;
79 struct list_elem class_peers; /* peers on class */
80 struct list endpoints;
82 struct list_elem peers; /* peers on device */
86 #define USB_SPEED_1_1 1
92 int (*detect_change) (host_info);
93 int (*dev_control) (host_eop_info, struct usb_setup_pkt *,
94 void *data, size_t *size);
95 int (*dev_bulk) (host_eop_info, bool out, void *data, size_t *size);
96 int (*tx_pkt) (host_eop_info, int pid, void *pkt,
97 int min_sz, int max_sz, int *in_sz, bool wait);
99 host_eop_info (*create_eop)(host_dev_info, int eop, int maxpkt);
100 void (*remove_eop)(host_eop_info);
102 host_dev_info (*create_dev_channel) (host_info, int dev_addr, int ver);
103 void (*modify_dev_channel) (host_dev_info, int dev_addr, int ver);
104 void (*remove_dev_channel) (host_dev_info);
113 /* when a device of this class is attached, the device is passed in */
114 /* returns private info on device */
115 void *(*attached) (struct usb_iface *);
117 /* device is detached -> detached(dev_info) */
118 void (*detached) (class_info info);
121 #define USB_VERSION_1_0 0x100
122 #define USB_VERSION_1_1 0x110
123 #define USB_VERSION_2 0x200
125 #define USB_EOP_ATTR_CTL 0 /* control */
126 #define USB_EOP_ATTR_ISO 1 /* isochronous */
127 #define USB_EOP_ATTR_BULK 2 /* bulk */
128 #define USB_EOP_ATTR_INT 3 /* interrupt */
131 int eop; /* end point address */
133 int direction; /* 0 = host->dev, 1=dev->host */
136 struct usb_iface *iface;
138 struct list_elem peers;
145 int class_id, subclass_id;
146 uint16_t product_id, vendor_id, device_id;
152 int pwr; /* power draw for this config */
154 struct list interfaces;
156 struct list_elem host_peers; /* peers on host */
157 struct list_elem sys_peers; /* list for all devices */
159 struct usb_iface default_iface;
160 struct usb_endpoint cfg_eop;
163 host_eop_info h_cfg_eop; /* configuration EOP */
167 /* pg276 usb_20.pdf */
168 #define USB_SETUP_TYPE_STD 0
169 #define USB_SETUP_TYPE_CLASS 1
170 #define USB_SETUP_TYPE_VENDOR 2
172 #define USB_SETUP_RECIP_DEV 0
173 #define USB_SETUP_RECIP_IFACE 1
174 #define USB_SETUP_RECIP_ENDPT 2
175 #define USB_SETUP_RECIP_OTHER 3
182 uint8_t direction:1; /* 0 = host->dev, 1 = dev->host */
190 void usb_init (void);
192 void usb_register_host (struct usb_host *, host_info info);
193 int usb_unregister_host (struct usb_host *, host_info info);
194 int usb_register_class (struct usb_class *);
195 int usb_unregister_class (struct usb_class *);
197 int usb_dev_control (struct usb_endpoint *, struct usb_setup_pkt *,
198 void *data, size_t *size);
199 int usb_dev_bulk (struct usb_endpoint *eop, void *buf, int sz, int *tx);
200 int usb_dev_wait_int (struct usb_dev *);
202 void usb_storage_init (void);