Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / SLOF / lib / libusb / usb-core.h
1 /*****************************************************************************
2  * Copyright (c) 2013 IBM Corporation
3  * All rights reserved.
4  * This program and the accompanying materials
5  * are made available under the terms of the BSD License
6  * which accompanies this distribution, and is available at
7  * http://www.opensource.org/licenses/bsd-license.php
8  *
9  * Contributors:
10  *     IBM Corporation - initial implementation
11  *****************************************************************************/
12
13 #ifndef __USB_CORE_H
14 #define __USB_CORE_H
15
16 #include <stdio.h>
17 #include <stdbool.h>
18 #include "helpers.h"
19 #include "usb.h"
20 #include "tools.h"
21
22 enum usb_hcd_type {
23         USB_OHCI = 1,
24         USB_EHCI = 2,
25         USB_XHCI = 3,
26 };
27
28 struct usb_hcd_dev;
29
30 struct usb_hcd_dev {
31         void *base;
32         long type;
33         long num;
34         struct usb_hcd_ops *ops;
35         void *priv; /* hcd owned structure */
36         long nextaddr; /* address for devices */
37 };
38
39 struct usb_pipe;
40
41 /*******************************************/
42 /* Standard Endpoint Descriptor            */
43 /*******************************************/
44 /* bmAttributes */
45 #define USB_EP_TYPE_MASK          0x03
46 #define USB_EP_TYPE_CONTROL       0
47 #define USB_EP_TYPE_ISOC          1
48 #define USB_EP_TYPE_BULK          2
49 #define USB_EP_TYPE_INTR          3
50
51 struct usb_ep_descr {
52         uint8_t         bLength;                /* size of descriptor */
53         uint8_t         bDescriptorType;        /* Type = 5 */
54         uint8_t         bEndpointAddress;
55         uint8_t         bmAttributes;
56         uint16_t        wMaxPacketSize;
57         uint8_t         bInterval;
58 } __attribute__((packed));
59
60 #define DEV_HID_KEYB            0x030101        /* class=HIB,   protocol=Keyboard */
61 #define DEV_HID_MOUSE           0x030102        /* class=HIB,   protocol=Mouse */
62 #define DEV_HUB                 0x090000        /* class=HUB, subclass, protocol */
63 #define DEV_MASS_RBC            0x080150        /* MassStorage, RBC, Bulk */
64 #define DEV_CDROM_ATAPI         0x080250        /* MassStorage, SFF-8020i , Bulk */
65 #define DEV_MASS_FLOPPY         0x080450        /* MassStorage, UFI, Bulk */
66 #define DEV_MASS_ATAPI          0x080550        /* MassStorage, SFF-8070i , Bulk */
67 #define DEV_MASS_SCSI           0x080650        /* MassStorage, SCSI, Bulk */
68
69 enum USB_SPEED_TYPE {
70         USB_LOW_SPEED = 0,
71         USB_FULL_SPEED = 1,
72         USB_HIGH_SPEED = 2,
73         USB_SUPER_SPEED = 3,
74 };
75
76 /* Max number of endpoints supported in a device */
77 #define USB_DEV_EP_MAX 4
78 #define USB_TIMEOUT    5000 /* 5 sec usb timeout */
79
80 struct usb_dev {
81         struct usb_dev     *next;
82         struct usb_hcd_dev *hcidev;
83         struct usb_pipe    *intr;
84         struct usb_pipe    *control;
85         struct usb_pipe    *bulk_in;
86         struct usb_pipe    *bulk_out;
87         struct usb_ep_descr ep[USB_DEV_EP_MAX];
88         void *priv;
89         uint32_t ep_cnt;
90         uint32_t class;
91         uint32_t speed;
92         uint32_t addr;
93         uint32_t mps0;
94         uint32_t port;
95         uint16_t intf_num;
96 };
97
98 #define DEVICE_KEYBOARD    1
99 #define DEVICE_MOUSE       2
100 #define DEVICE_DISK        3
101 #define DEVICE_HUB         4
102
103 /* Structure in sync with FORTH code */
104 struct slof_usb_dev {
105         void     *udev;
106         uint32_t port;
107         uint32_t addr;
108         uint32_t hcitype;
109         uint32_t num;
110         uint32_t devtype;
111 } __attribute__((packed));
112
113 enum USB_PIPE_DIR {
114         USB_PIPE_OUT = 0,
115         USB_PIPE_IN,
116 };
117
118 struct usb_pipe {
119         struct usb_dev *dev;
120         struct usb_pipe *next;
121         uint32_t type;
122         uint32_t speed;
123         uint32_t dir;
124         uint16_t epno;
125         uint16_t mps;
126 } __attribute__((packed));
127
128 #define REQ_GET_STATUS               0  /* see Table 9-4 */
129 #define REQ_CLEAR_FEATURE            1
130 #define REQ_GET_STATE                2  /* HUB specific */
131 #define REQ_SET_FEATURE              3
132 #define REQ_SET_ADDRESS              5
133 #define REQ_GET_DESCRIPTOR           6
134 #define REQ_SET_DESCRIPTOR           7
135 #define REQ_GET_CONFIGURATION        8
136 #define REQ_SET_CONFIGURATION        9
137 #define REQ_GET_INTERFACE            10
138 #define REQ_SET_INTERFACE            11
139 #define REQ_SYNCH_FRAME              12
140
141 #define FEATURE_DEVICE_REMOTE_WAKEUP 1
142 #define FEATURE_ENDPOINT_HALT        0
143
144 #define REQT_REC_DEVICE              0
145 #define REQT_REC_INTERFACE           1
146 #define REQT_REC_EP                  2
147 #define REQT_REC_OTHER               3
148 #define REQT_TYPE_STANDARD           (0 << 5)
149 #define REQT_TYPE_CLASS              (1 << 5)
150 #define REQT_TYPE_VENDOR             (2 << 5)
151 #define REQT_TYPE_RSRVD              (3 << 5)
152 #define REQT_DIR_OUT                 (0 << 7) /* host -> device */
153 #define REQT_DIR_IN                  (1 << 7) /* device -> host */
154
155 #define DESCR_TYPE_DEVICE               1       /* see Table 9-5 */
156 #define DESCR_TYPE_CONFIGURATION        2
157 #define DESCR_TYPE_STRING               3
158 #define DESCR_TYPE_INTERFACE            4
159 #define DESCR_TYPE_ENDPOINT             5
160 #define DESCR_TYPE_HUB                  0x29    /* Class Descriptor HUB */
161 #define DESCR_TYPE_HID                  0x21    /* Class Descriptor HID */
162 #define DESCR_TYPE_REPORT               0x22    /* Class Descriptor HID */
163 #define DESCR_TYPE_PHYSICAL             0x23    /* Class Descriptor HID */
164
165 struct usb_dev_req {
166         uint8_t         bmRequestType;          /* direction, recipient */
167         uint8_t         bRequest;               /* see spec: Table 9-3 */
168         uint16_t        wValue;
169         uint16_t        wIndex;
170         uint16_t        wLength;                /* number of bytes to transfer */
171 } __attribute__((packed));
172
173 /* Standard Device Descriptor (18 Bytes)   */
174 /*******************************************/
175 struct usb_dev_descr {
176         uint8_t         bLength;
177         uint8_t         bDescriptorType;
178         uint16_t        bcdUSB;
179         uint8_t         bDeviceClass;
180         uint8_t         bDeviceSubClass;
181         uint8_t         bDeviceProtocol;
182         uint8_t         bMaxPacketSize0;
183         uint16_t        idVendor;
184         uint16_t        idProduct;
185         uint16_t        bcdDevice;
186         uint8_t         iManufacturer;
187         uint8_t         iProduct;
188         uint8_t         iSerialNumber;
189         uint8_t         bNumConfigurations;
190 } __attribute__((packed));
191
192 /*******************************************/
193 /* Standard Configuration Descriptor       */
194 /*******************************************/
195 struct usb_dev_config_descr {
196         uint8_t         bLength;                /* size of descriptor */
197         uint8_t         bDescriptorType;        /* Type = 2 */
198         uint16_t        wTotalLength;           /* total returned data */
199         uint8_t         bNumInterfaces;         /* interfaces supported by this config */
200         uint8_t         bConfigurationValue;    /* Configuration-ID for SetConfiguration */
201         uint8_t         iConfiguration;         /* index of string descriptor */
202         uint8_t         bmAttributes;           /* configuration characteristics */
203         uint8_t         bMaxPower;              /* in 2mA units */
204 } __attribute__((packed));
205
206 /*******************************************/
207 /* Standard Interface Descriptor */
208 /*******************************************/
209 struct usb_dev_intf_descr {
210         uint8_t         bLength;                /* size of descriptor */
211         uint8_t         bDescriptorType;        /* Type = 4 */
212         uint8_t         bInterfaceNumber;
213         uint8_t         bAlternateSetting;
214         uint8_t         bNumEndpoints;
215         uint8_t         bInterfaceClass;
216         uint8_t         bInterfaceSubClass;
217         uint8_t         bInterfaceProtocol;     /* protocol code */
218         uint8_t         iInterface;             /* index to string descriptor */
219 } __attribute__((packed));
220
221 /*******************************************/
222 /* HUB-Class Descriptor                    */
223 /*******************************************/
224 struct usb_dev_hub_descr {
225         uint8_t         bLength;                /* size of complete descriptor */
226         uint8_t         bDescriptorType;        /* type = 0x29 for HUB */
227         uint8_t         bNbrPorts;              /* number of downstream ports */
228         uint8_t         wHubCharacteristics;    /* mode bits    7..0 */
229         uint8_t         reserved;               /* mode bits 15..8 */
230         uint8_t         bPwrOn2PwrGood;         /* in 2ms units */
231         uint8_t         bHubContrCurrent;       /* current requirement in mA */
232         uint8_t         DeviceTable;            /* length depends on number of ports */
233 } __attribute__((packed));
234
235 /*******************************************/
236 /* HID-Class Descriptor                    */
237 /*******************************************/
238 struct usb_dev_hid_descr {
239         uint8_t         bLength;                /* size of this descriptor */
240         uint8_t         bDescriptorType;        /* type = 0x21 for HID     */
241         uint16_t        bcdHID;                 /* Sample: 0x0102 for 2.01  */
242         uint8_t         bCountryCode;           /* Hardware target country */
243         uint8_t         bNumDescriptors;        /* Number of HID class descr. */
244         uint8_t         bReportType;            /* Report Descriptor Type */
245         uint16_t        wReportLength;          /* Total Length of Report Descr. */
246 } __attribute__((packed));
247
248 struct usb_hcd_ops {
249         const char *name;
250         void (*init)(struct usb_hcd_dev *);
251         void (*exit)(struct usb_hcd_dev *);
252         void (*detect)(void);
253         void (*disconnect)(void);
254         int  (*send_ctrl)(struct usb_pipe *pipe, struct usb_dev_req *req, void *data);
255         struct usb_pipe* (*get_pipe)(struct usb_dev *dev, struct usb_ep_descr *ep,
256                                 char *buf, size_t len);
257         int  (*transfer_bulk)(struct usb_pipe *pipe, void *td, void *td_phys, void *data, int size);
258         void (*put_pipe)(struct usb_pipe *);
259         int (*poll_intr)(struct usb_pipe *, uint8_t *);
260         struct usb_hcd_ops *next;
261         unsigned int usb_type;
262 };
263
264 extern void usb_hcd_register(struct usb_hcd_ops *ops);
265 extern struct usb_pipe *usb_get_pipe(struct usb_dev *dev, struct usb_ep_descr *ep,
266                                 char *buf, size_t len);
267 extern void usb_put_pipe(struct usb_pipe *pipe);
268 extern int usb_poll_intr(struct usb_pipe *pipe, uint8_t *buf);
269 extern int usb_send_ctrl(struct usb_pipe *pipe, struct usb_dev_req *req, void *data);
270 extern struct usb_dev *usb_devpool_get(void);
271 extern void usb_devpool_put(struct usb_dev *);
272 extern int setup_new_device(struct usb_dev *dev, unsigned int port);
273 extern int slof_usb_handle(struct usb_dev *dev);
274 extern int usb_dev_populate_pipe(struct usb_dev *dev, struct usb_ep_descr *ep,
275                                 void *buf, size_t len);
276 extern int usb_hid_kbd_init(struct usb_dev *dev);
277 extern int usb_hid_kbd_exit(struct usb_dev *dev);
278 extern void usb_msc_resetrecovery(struct usb_dev *dev);
279 #endif