root / hw / usb.h @ bf16cc8f
History | View | Annotate | Download (8.7 kB)
1 | bb36d470 | bellard | /*
|
---|---|---|---|
2 | bb36d470 | bellard | * QEMU USB API
|
3 | 5fafdf24 | ths | *
|
4 | bb36d470 | bellard | * Copyright (c) 2005 Fabrice Bellard
|
5 | 5fafdf24 | ths | *
|
6 | bb36d470 | bellard | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | bb36d470 | bellard | * of this software and associated documentation files (the "Software"), to deal
|
8 | bb36d470 | bellard | * in the Software without restriction, including without limitation the rights
|
9 | bb36d470 | bellard | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | bb36d470 | bellard | * copies of the Software, and to permit persons to whom the Software is
|
11 | bb36d470 | bellard | * furnished to do so, subject to the following conditions:
|
12 | bb36d470 | bellard | *
|
13 | bb36d470 | bellard | * The above copyright notice and this permission notice shall be included in
|
14 | bb36d470 | bellard | * all copies or substantial portions of the Software.
|
15 | bb36d470 | bellard | *
|
16 | bb36d470 | bellard | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | bb36d470 | bellard | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | bb36d470 | bellard | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | bb36d470 | bellard | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | bb36d470 | bellard | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | bb36d470 | bellard | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | bb36d470 | bellard | * THE SOFTWARE.
|
23 | bb36d470 | bellard | */
|
24 | c0f4ce77 | aliguori | |
25 | c0f4ce77 | aliguori | #include "block.h" |
26 | c0f4ce77 | aliguori | |
27 | bb36d470 | bellard | #define USB_TOKEN_SETUP 0x2d |
28 | bb36d470 | bellard | #define USB_TOKEN_IN 0x69 /* device -> host */ |
29 | bb36d470 | bellard | #define USB_TOKEN_OUT 0xe1 /* host -> device */ |
30 | bb36d470 | bellard | |
31 | bb36d470 | bellard | /* specific usb messages, also sent in the 'pid' parameter */
|
32 | bb36d470 | bellard | #define USB_MSG_ATTACH 0x100 |
33 | bb36d470 | bellard | #define USB_MSG_DETACH 0x101 |
34 | bb36d470 | bellard | #define USB_MSG_RESET 0x102 |
35 | bb36d470 | bellard | |
36 | 5fafdf24 | ths | #define USB_RET_NODEV (-1) |
37 | bb36d470 | bellard | #define USB_RET_NAK (-2) |
38 | bb36d470 | bellard | #define USB_RET_STALL (-3) |
39 | bb36d470 | bellard | #define USB_RET_BABBLE (-4) |
40 | 4d611c9a | pbrook | #define USB_RET_ASYNC (-5) |
41 | bb36d470 | bellard | |
42 | bb36d470 | bellard | #define USB_SPEED_LOW 0 |
43 | bb36d470 | bellard | #define USB_SPEED_FULL 1 |
44 | bb36d470 | bellard | #define USB_SPEED_HIGH 2 |
45 | bb36d470 | bellard | |
46 | bb36d470 | bellard | #define USB_STATE_NOTATTACHED 0 |
47 | bb36d470 | bellard | #define USB_STATE_ATTACHED 1 |
48 | bb36d470 | bellard | //#define USB_STATE_POWERED 2
|
49 | bb36d470 | bellard | #define USB_STATE_DEFAULT 3 |
50 | bb36d470 | bellard | //#define USB_STATE_ADDRESS 4
|
51 | bb36d470 | bellard | //#define USB_STATE_CONFIGURED 5
|
52 | bb36d470 | bellard | #define USB_STATE_SUSPENDED 6 |
53 | bb36d470 | bellard | |
54 | a594cfbf | bellard | #define USB_CLASS_AUDIO 1 |
55 | a594cfbf | bellard | #define USB_CLASS_COMM 2 |
56 | a594cfbf | bellard | #define USB_CLASS_HID 3 |
57 | a594cfbf | bellard | #define USB_CLASS_PHYSICAL 5 |
58 | a594cfbf | bellard | #define USB_CLASS_STILL_IMAGE 6 |
59 | a594cfbf | bellard | #define USB_CLASS_PRINTER 7 |
60 | a594cfbf | bellard | #define USB_CLASS_MASS_STORAGE 8 |
61 | a594cfbf | bellard | #define USB_CLASS_HUB 9 |
62 | a594cfbf | bellard | #define USB_CLASS_CDC_DATA 0x0a |
63 | a594cfbf | bellard | #define USB_CLASS_CSCID 0x0b |
64 | a594cfbf | bellard | #define USB_CLASS_CONTENT_SEC 0x0d |
65 | a594cfbf | bellard | #define USB_CLASS_APP_SPEC 0xfe |
66 | a594cfbf | bellard | #define USB_CLASS_VENDOR_SPEC 0xff |
67 | a594cfbf | bellard | |
68 | bb36d470 | bellard | #define USB_DIR_OUT 0 |
69 | bb36d470 | bellard | #define USB_DIR_IN 0x80 |
70 | bb36d470 | bellard | |
71 | bb36d470 | bellard | #define USB_TYPE_MASK (0x03 << 5) |
72 | bb36d470 | bellard | #define USB_TYPE_STANDARD (0x00 << 5) |
73 | bb36d470 | bellard | #define USB_TYPE_CLASS (0x01 << 5) |
74 | bb36d470 | bellard | #define USB_TYPE_VENDOR (0x02 << 5) |
75 | bb36d470 | bellard | #define USB_TYPE_RESERVED (0x03 << 5) |
76 | bb36d470 | bellard | |
77 | bb36d470 | bellard | #define USB_RECIP_MASK 0x1f |
78 | bb36d470 | bellard | #define USB_RECIP_DEVICE 0x00 |
79 | bb36d470 | bellard | #define USB_RECIP_INTERFACE 0x01 |
80 | bb36d470 | bellard | #define USB_RECIP_ENDPOINT 0x02 |
81 | bb36d470 | bellard | #define USB_RECIP_OTHER 0x03 |
82 | bb36d470 | bellard | |
83 | bb36d470 | bellard | #define DeviceRequest ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE)<<8) |
84 | bb36d470 | bellard | #define DeviceOutRequest ((USB_DIR_OUT|USB_TYPE_STANDARD|USB_RECIP_DEVICE)<<8) |
85 | 59ae540c | bellard | #define InterfaceRequest \
|
86 | 59ae540c | bellard | ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_INTERFACE)<<8)
|
87 | 59ae540c | bellard | #define InterfaceOutRequest \
|
88 | 59ae540c | bellard | ((USB_DIR_OUT|USB_TYPE_STANDARD|USB_RECIP_INTERFACE)<<8)
|
89 | 59ae540c | bellard | #define EndpointRequest ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_ENDPOINT)<<8) |
90 | 59ae540c | bellard | #define EndpointOutRequest \
|
91 | 59ae540c | bellard | ((USB_DIR_OUT|USB_TYPE_STANDARD|USB_RECIP_ENDPOINT)<<8)
|
92 | bb36d470 | bellard | |
93 | bb36d470 | bellard | #define USB_REQ_GET_STATUS 0x00 |
94 | bb36d470 | bellard | #define USB_REQ_CLEAR_FEATURE 0x01 |
95 | bb36d470 | bellard | #define USB_REQ_SET_FEATURE 0x03 |
96 | bb36d470 | bellard | #define USB_REQ_SET_ADDRESS 0x05 |
97 | bb36d470 | bellard | #define USB_REQ_GET_DESCRIPTOR 0x06 |
98 | bb36d470 | bellard | #define USB_REQ_SET_DESCRIPTOR 0x07 |
99 | bb36d470 | bellard | #define USB_REQ_GET_CONFIGURATION 0x08 |
100 | bb36d470 | bellard | #define USB_REQ_SET_CONFIGURATION 0x09 |
101 | bb36d470 | bellard | #define USB_REQ_GET_INTERFACE 0x0A |
102 | bb36d470 | bellard | #define USB_REQ_SET_INTERFACE 0x0B |
103 | bb36d470 | bellard | #define USB_REQ_SYNCH_FRAME 0x0C |
104 | bb36d470 | bellard | |
105 | bb36d470 | bellard | #define USB_DEVICE_SELF_POWERED 0 |
106 | bb36d470 | bellard | #define USB_DEVICE_REMOTE_WAKEUP 1 |
107 | bb36d470 | bellard | |
108 | bb36d470 | bellard | #define USB_DT_DEVICE 0x01 |
109 | bb36d470 | bellard | #define USB_DT_CONFIG 0x02 |
110 | bb36d470 | bellard | #define USB_DT_STRING 0x03 |
111 | bb36d470 | bellard | #define USB_DT_INTERFACE 0x04 |
112 | bb36d470 | bellard | #define USB_DT_ENDPOINT 0x05 |
113 | bb36d470 | bellard | |
114 | 942ac052 | balrog | #define USB_ENDPOINT_XFER_CONTROL 0 |
115 | 942ac052 | balrog | #define USB_ENDPOINT_XFER_ISOC 1 |
116 | 942ac052 | balrog | #define USB_ENDPOINT_XFER_BULK 2 |
117 | 942ac052 | balrog | #define USB_ENDPOINT_XFER_INT 3 |
118 | 942ac052 | balrog | |
119 | bb36d470 | bellard | typedef struct USBPort USBPort; |
120 | bb36d470 | bellard | typedef struct USBDevice USBDevice; |
121 | 4d611c9a | pbrook | typedef struct USBPacket USBPacket; |
122 | bb36d470 | bellard | |
123 | bb36d470 | bellard | /* definition of a USB device */
|
124 | bb36d470 | bellard | struct USBDevice {
|
125 | bb36d470 | bellard | void *opaque;
|
126 | 89b9b79f | aliguori | |
127 | 89b9b79f | aliguori | /*
|
128 | 89b9b79f | aliguori | * Process USB packet.
|
129 | 89b9b79f | aliguori | * Called by the HC (Host Controller).
|
130 | 89b9b79f | aliguori | *
|
131 | 89b9b79f | aliguori | * Returns length of the transaction
|
132 | 89b9b79f | aliguori | * or one of the USB_RET_XXX codes.
|
133 | 89b9b79f | aliguori | */
|
134 | 4d611c9a | pbrook | int (*handle_packet)(USBDevice *dev, USBPacket *p);
|
135 | 89b9b79f | aliguori | |
136 | 89b9b79f | aliguori | /*
|
137 | 89b9b79f | aliguori | * Called when device is destroyed.
|
138 | 89b9b79f | aliguori | */
|
139 | 059809e4 | bellard | void (*handle_destroy)(USBDevice *dev);
|
140 | 059809e4 | bellard | |
141 | bb36d470 | bellard | int speed;
|
142 | 3b46e624 | ths | |
143 | bb36d470 | bellard | /* The following fields are used by the generic USB device
|
144 | 89b9b79f | aliguori | layer. They are here just to avoid creating a new structure
|
145 | 89b9b79f | aliguori | for them. */
|
146 | 89b9b79f | aliguori | |
147 | 89b9b79f | aliguori | /*
|
148 | 89b9b79f | aliguori | * Reset the device
|
149 | 89b9b79f | aliguori | */
|
150 | 059809e4 | bellard | void (*handle_reset)(USBDevice *dev);
|
151 | 89b9b79f | aliguori | |
152 | 89b9b79f | aliguori | /*
|
153 | 89b9b79f | aliguori | * Process control request.
|
154 | 89b9b79f | aliguori | * Called from handle_packet().
|
155 | 89b9b79f | aliguori | *
|
156 | 89b9b79f | aliguori | * Returns length or one of the USB_RET_ codes.
|
157 | 89b9b79f | aliguori | */
|
158 | bb36d470 | bellard | int (*handle_control)(USBDevice *dev, int request, int value, |
159 | bb36d470 | bellard | int index, int length, uint8_t *data); |
160 | 89b9b79f | aliguori | |
161 | 89b9b79f | aliguori | /*
|
162 | 89b9b79f | aliguori | * Process data transfers (both BULK and ISOC).
|
163 | 89b9b79f | aliguori | * Called from handle_packet().
|
164 | 89b9b79f | aliguori | *
|
165 | 89b9b79f | aliguori | * Returns length or one of the USB_RET_ codes.
|
166 | 89b9b79f | aliguori | */
|
167 | 4d611c9a | pbrook | int (*handle_data)(USBDevice *dev, USBPacket *p);
|
168 | 89b9b79f | aliguori | |
169 | bb36d470 | bellard | uint8_t addr; |
170 | 1f6e24e7 | bellard | char devname[32]; |
171 | 3b46e624 | ths | |
172 | bb36d470 | bellard | int state;
|
173 | bb36d470 | bellard | uint8_t setup_buf[8];
|
174 | bb36d470 | bellard | uint8_t data_buf[1024];
|
175 | bb36d470 | bellard | int remote_wakeup;
|
176 | bb36d470 | bellard | int setup_state;
|
177 | bb36d470 | bellard | int setup_len;
|
178 | bb36d470 | bellard | int setup_index;
|
179 | bb36d470 | bellard | }; |
180 | bb36d470 | bellard | |
181 | 0d92ed30 | pbrook | typedef void (*usb_attachfn)(USBPort *port, USBDevice *dev); |
182 | 0d92ed30 | pbrook | |
183 | bb36d470 | bellard | /* USB port on which a device can be connected */
|
184 | bb36d470 | bellard | struct USBPort {
|
185 | a594cfbf | bellard | USBDevice *dev; |
186 | 0d92ed30 | pbrook | usb_attachfn attach; |
187 | bb36d470 | bellard | void *opaque;
|
188 | bb36d470 | bellard | int index; /* internal port index, may be used with the opaque */ |
189 | 0d92ed30 | pbrook | struct USBPort *next; /* Used internally by qemu. */ |
190 | bb36d470 | bellard | }; |
191 | bb36d470 | bellard | |
192 | 4d611c9a | pbrook | typedef void USBCallback(USBPacket * packet, void *opaque); |
193 | 4d611c9a | pbrook | |
194 | 4d611c9a | pbrook | /* Structure used to hold information about an active USB packet. */
|
195 | 4d611c9a | pbrook | struct USBPacket {
|
196 | 4d611c9a | pbrook | /* Data fields for use by the driver. */
|
197 | 4d611c9a | pbrook | int pid;
|
198 | 4d611c9a | pbrook | uint8_t devaddr; |
199 | 4d611c9a | pbrook | uint8_t devep; |
200 | 4d611c9a | pbrook | uint8_t *data; |
201 | 4d611c9a | pbrook | int len;
|
202 | 4d611c9a | pbrook | /* Internal use by the USB layer. */
|
203 | 4d611c9a | pbrook | USBCallback *complete_cb; |
204 | 4d611c9a | pbrook | void *complete_opaque;
|
205 | 4d611c9a | pbrook | USBCallback *cancel_cb; |
206 | b63891a0 | ths | void *cancel_opaque;
|
207 | 4d611c9a | pbrook | }; |
208 | 4d611c9a | pbrook | |
209 | 4d611c9a | pbrook | /* Defer completion of a USB packet. The hadle_packet routine should then
|
210 | 4d611c9a | pbrook | return USB_RET_ASYNC. Packets that complete immediately (before
|
211 | 4d611c9a | pbrook | handle_packet returns) should not call this method. */
|
212 | 4d611c9a | pbrook | static inline void usb_defer_packet(USBPacket *p, USBCallback *cancel, |
213 | 4d611c9a | pbrook | void * opaque)
|
214 | 4d611c9a | pbrook | { |
215 | 4d611c9a | pbrook | p->cancel_cb = cancel; |
216 | 4d611c9a | pbrook | p->cancel_opaque = opaque; |
217 | 4d611c9a | pbrook | } |
218 | 4d611c9a | pbrook | |
219 | 4d611c9a | pbrook | /* Notify the controller that an async packet is complete. This should only
|
220 | 4d611c9a | pbrook | be called for packets previously deferred with usb_defer_packet, and
|
221 | 4d611c9a | pbrook | should never be called from within handle_packet. */
|
222 | 4d611c9a | pbrook | static inline void usb_packet_complete(USBPacket *p) |
223 | 4d611c9a | pbrook | { |
224 | 4d611c9a | pbrook | p->complete_cb(p, p->complete_opaque); |
225 | 4d611c9a | pbrook | } |
226 | 4d611c9a | pbrook | |
227 | 4d611c9a | pbrook | /* Cancel an active packet. The packed must have been deferred with
|
228 | 4d611c9a | pbrook | usb_defer_packet, and not yet completed. */
|
229 | 4d611c9a | pbrook | static inline void usb_cancel_packet(USBPacket * p) |
230 | 4d611c9a | pbrook | { |
231 | 4d611c9a | pbrook | p->cancel_cb(p, p->cancel_opaque); |
232 | 4d611c9a | pbrook | } |
233 | 4d611c9a | pbrook | |
234 | 4b096fc9 | aliguori | int usb_device_add_dev(USBDevice *dev);
|
235 | 1f3870ab | aliguori | int usb_device_del_addr(int bus_num, int addr); |
236 | bb36d470 | bellard | void usb_attach(USBPort *port, USBDevice *dev);
|
237 | 4d611c9a | pbrook | int usb_generic_handle_packet(USBDevice *s, USBPacket *p);
|
238 | bb36d470 | bellard | int set_usb_string(uint8_t *buf, const char *str); |
239 | 4d611c9a | pbrook | void usb_send_msg(USBDevice *dev, int msg); |
240 | 4d611c9a | pbrook | |
241 | bb36d470 | bellard | /* usb hub */
|
242 | 0d92ed30 | pbrook | USBDevice *usb_hub_init(int nb_ports);
|
243 | bb36d470 | bellard | |
244 | bb36d470 | bellard | /* usb-linux.c */
|
245 | a594cfbf | bellard | USBDevice *usb_host_device_open(const char *devname); |
246 | 5d0c5750 | aliguori | int usb_host_device_close(const char *devname); |
247 | 376253ec | aliguori | void usb_host_info(Monitor *mon);
|
248 | 59ae540c | bellard | |
249 | 59ae540c | bellard | /* usb-hid.c */
|
250 | 59ae540c | bellard | USBDevice *usb_mouse_init(void);
|
251 | 09b26c5e | bellard | USBDevice *usb_tablet_init(void);
|
252 | 47b2d338 | balrog | USBDevice *usb_keyboard_init(void);
|
253 | 47e699dc | balrog | void usb_hid_datain_cb(USBDevice *dev, void *opaque, void (*datain)(void *)); |
254 | 2e5d83bb | pbrook | |
255 | 2e5d83bb | pbrook | /* usb-msd.c */
|
256 | bb5fc20f | aliguori | USBDevice *usb_msd_init(const char *filename); |
257 | bb5fc20f | aliguori | BlockDriverState *usb_msd_get_bdrv(USBDevice *dev); |
258 | f6d2a316 | balrog | |
259 | 6c9f886c | balrog | /* usb-net.c */
|
260 | 6c9f886c | balrog | USBDevice *usb_net_init(NICInfo *nd); |
261 | 6c9f886c | balrog | |
262 | e6a6d5ab | balrog | /* usb-bt.c */
|
263 | e6a6d5ab | balrog | USBDevice *usb_bt_init(HCIInfo *hci); |
264 | e6a6d5ab | balrog | |
265 | f6d2a316 | balrog | /* usb-wacom.c */
|
266 | f6d2a316 | balrog | USBDevice *usb_wacom_init(void);
|
267 | 87ecb68b | pbrook | |
268 | a7954218 | balrog | /* usb-serial.c */
|
269 | a7954218 | balrog | USBDevice *usb_serial_init(const char *filename); |
270 | a7954218 | balrog | |
271 | 87ecb68b | pbrook | /* usb ports of the VM */
|
272 | 87ecb68b | pbrook | |
273 | 87ecb68b | pbrook | void qemu_register_usb_port(USBPort *port, void *opaque, int index, |
274 | 87ecb68b | pbrook | usb_attachfn attach); |
275 | 87ecb68b | pbrook | |
276 | 87ecb68b | pbrook | #define VM_USB_HUB_SIZE 8 |
277 | 87ecb68b | pbrook | |
278 | 942ac052 | balrog | /* usb-musb.c */
|
279 | 942ac052 | balrog | enum musb_irq_source_e {
|
280 | 942ac052 | balrog | musb_irq_suspend = 0,
|
281 | 942ac052 | balrog | musb_irq_resume, |
282 | 942ac052 | balrog | musb_irq_rst_babble, |
283 | 942ac052 | balrog | musb_irq_sof, |
284 | 942ac052 | balrog | musb_irq_connect, |
285 | 942ac052 | balrog | musb_irq_disconnect, |
286 | 942ac052 | balrog | musb_irq_vbus_request, |
287 | 942ac052 | balrog | musb_irq_vbus_error, |
288 | 942ac052 | balrog | musb_irq_rx, |
289 | 942ac052 | balrog | musb_irq_tx, |
290 | 942ac052 | balrog | musb_set_vbus, |
291 | 942ac052 | balrog | musb_set_session, |
292 | 942ac052 | balrog | __musb_irq_max, |
293 | 942ac052 | balrog | }; |
294 | 942ac052 | balrog | |
295 | 942ac052 | balrog | struct musb_s;
|
296 | 942ac052 | balrog | struct musb_s *musb_init(qemu_irq *irqs);
|
297 | 942ac052 | balrog | uint32_t musb_core_intr_get(struct musb_s *s);
|
298 | 942ac052 | balrog | void musb_core_intr_clear(struct musb_s *s, uint32_t mask); |
299 | 942ac052 | balrog | void musb_set_size(struct musb_s *s, int epnum, int size, int is_tx); |