usb.patch, with conflicts and some warnings fixed
[pintos-anon] / src / devices / usb.h
1 #ifndef USB_H
2 #define USB_H
3
4 #include <stdint.h>
5 #include <stdbool.h>
6 #include <kernel/list.h>
7
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
17 #define make_usb_pid(x)         ((x) | ((~(x)) << 4))
18
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)
24 /* data packets... */
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)
34 /* special */
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)
39
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
52
53
54 #define USB_TOKEN_SETUP         0x00
55 #define USB_TOKEN_IN            0x80
56 #define USB_TOKEN_OUT           0x90
57
58 struct class;
59 struct host;
60 typedef void *host_info;
61 typedef void *host_eop_info;
62 typedef void *host_dev_info;
63 typedef void *class_info;
64
65
66 struct usb_iface
67 {
68   int iface_num;
69   int class_id, subclass_id;
70   int proto;
71
72   struct usb_dev *dev;
73
74   struct class *class;
75   class_info c_info;
76
77   struct list_elem class_peers; /* peers on class */
78   struct list endpoints;
79
80   struct list_elem peers;       /* peers on device */
81 };
82
83 #define USB_SPEED_1             0
84 #define USB_SPEED_1_1           1
85 #define USB_SPEED_2             2
86
87 struct usb_host
88 {
89   const char *name;
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);
93
94   host_eop_info (*create_eop)(host_dev_info, int eop, int maxpkt);
95   void (*remove_eop)(host_eop_info);
96
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);
100 };
101
102 struct usb_dev;
103 struct usb_class
104 {
105   const int class_id;
106   const char *name;
107
108   /* when a device of this class is attached, the device is passed in */
109   /* returns private info on device */
110   void *(*attached) (struct usb_iface *);
111
112   /* device is detached -> detached(dev_info) */
113   void (*detached) (class_info info);
114 };
115
116 #define USB_VERSION_1_0 0x100
117 #define USB_VERSION_1_1 0x110
118 #define USB_VERSION_2   0x200
119
120 #define USB_EOP_ATTR_CTL        0       /* control */
121 #define USB_EOP_ATTR_ISO        1       /* isochronous */
122 #define USB_EOP_ATTR_BULK       2       /* bulk */
123 #define USB_EOP_ATTR_INT        3       /* interrupt */
124 struct usb_endpoint
125 {
126   int eop;                      /* end point address */
127   int attr;
128   int direction;                /* 0 = host->dev, 1=dev->host */
129   int max_pkt;
130   int interval;
131   struct usb_iface *iface;
132   host_eop_info h_eop;
133   struct list_elem peers;
134 };
135
136 struct usb_dev
137 {
138   uint8_t addr;
139   int usb_version;
140   int class_id, subclass_id;
141   uint16_t product_id, vendor_id, device_id;
142   int interface;
143   int max_pkt_len;
144   int int_period;
145   char *manufacturer;
146   char *product;
147   int pwr;                      /* power draw for this config */
148   bool ignore_device;
149   struct list interfaces;
150
151   struct list_elem host_peers;  /* peers on host */
152   struct list_elem sys_peers;   /* list for all devices */
153
154   struct usb_iface default_iface;
155   struct usb_endpoint cfg_eop;
156
157   host_dev_info h_dev;
158   host_eop_info h_cfg_eop;      /* configuration EOP */
159   struct host *host;
160 };
161
162 /* pg276 usb_20.pdf */
163 #define USB_SETUP_TYPE_STD      0
164 #define USB_SETUP_TYPE_CLASS    1
165 #define USB_SETUP_TYPE_VENDOR   2
166
167 #define USB_SETUP_RECIP_DEV     0
168 #define USB_SETUP_RECIP_IFACE   1
169 #define USB_SETUP_RECIP_ENDPT   2
170 #define USB_SETUP_RECIP_OTHER   3
171
172 #pragma pack(1)
173 struct usb_setup_pkt
174 {
175   uint8_t recipient:5;
176   uint8_t type:2;
177   uint8_t direction:1;          /* 0 = host->dev, 1 = dev->host */
178   uint8_t request;
179   uint16_t value;
180   uint16_t index;
181   uint16_t length;
182 };
183 #pragma pack()
184
185 void usb_init (void);
186
187 void usb_register_host (struct usb_host *, host_info info);
188 int usb_unregister_host (struct usb_host *, host_info info);
189 int usb_register_class (struct usb_class *);
190 int usb_unregister_class (struct usb_class *);
191
192 int usb_dev_bulk (struct usb_endpoint *eop, void *buf, int sz, int *tx);
193 int usb_dev_setup (struct usb_endpoint *eop, bool in,
194                    struct usb_setup_pkt *s, void *buf, int sz);
195 int usb_dev_wait_int (struct usb_dev *);
196
197 #endif