]> git.proxmox.com Git - qemu.git/blame - hw/usb.c
ppc405_uc: use specific endian ld/st_phys
[qemu.git] / hw / usb.c
CommitLineData
bb36d470
FB
1/*
2 * QEMU USB emulation
3 *
4 * Copyright (c) 2005 Fabrice Bellard
5fafdf24 5 *
89b9b79f
AL
6 * 2008 Generic packet handler rewrite by Max Krasnyansky
7 *
bb36d470
FB
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
87ecb68b
PB
26#include "qemu-common.h"
27#include "usb.h"
bb36d470
FB
28
29void usb_attach(USBPort *port, USBDevice *dev)
30{
618c169b
GH
31 if (dev != NULL) {
32 /* attach */
33 if (port->dev) {
34 usb_attach(port, NULL);
35 }
36 dev->port = port;
37 port->dev = dev;
38 port->ops->attach(port);
39 usb_send_msg(dev, USB_MSG_ATTACH);
40 } else {
41 /* detach */
42 dev = port->dev;
43 port->ops->detach(port);
44 if (dev) {
45 usb_send_msg(dev, USB_MSG_DETACH);
46 dev->port = NULL;
47 port->dev = NULL;
48 }
49 }
bb36d470
FB
50}
51
01eacab6
GH
52void usb_wakeup(USBDevice *dev)
53{
54 if (dev->remote_wakeup && dev->port && dev->port->ops->wakeup) {
55 dev->port->ops->wakeup(dev);
56 }
57}
58
bb36d470 59/**********************/
89b9b79f 60
bb36d470
FB
61/* generic USB device helpers (you are not forced to use them when
62 writing your USB device driver, but they help handling the
5fafdf24 63 protocol)
bb36d470
FB
64*/
65
50b7963e
HG
66#define SETUP_STATE_IDLE 0
67#define SETUP_STATE_SETUP 1
68#define SETUP_STATE_DATA 2
69#define SETUP_STATE_ACK 3
bb36d470 70
89b9b79f
AL
71static int do_token_setup(USBDevice *s, USBPacket *p)
72{
73 int request, value, index;
74 int ret = 0;
75
76 if (p->len != 8)
77 return USB_RET_STALL;
78
79 memcpy(s->setup_buf, p->data, 8);
80 s->setup_len = (s->setup_buf[7] << 8) | s->setup_buf[6];
81 s->setup_index = 0;
82
83 request = (s->setup_buf[0] << 8) | s->setup_buf[1];
84 value = (s->setup_buf[3] << 8) | s->setup_buf[2];
85 index = (s->setup_buf[5] << 8) | s->setup_buf[4];
007fd62f 86
89b9b79f 87 if (s->setup_buf[0] & USB_DIR_IN) {
007fd62f 88 ret = s->info->handle_control(s, p, request, value, index,
806b6024 89 s->setup_len, s->data_buf);
50b7963e
HG
90 if (ret == USB_RET_ASYNC) {
91 s->setup_state = SETUP_STATE_SETUP;
92 return USB_RET_ASYNC;
93 }
89b9b79f
AL
94 if (ret < 0)
95 return ret;
96
97 if (ret < s->setup_len)
98 s->setup_len = ret;
99 s->setup_state = SETUP_STATE_DATA;
100 } else {
19f33223
HG
101 if (s->setup_len > sizeof(s->data_buf)) {
102 fprintf(stderr,
103 "usb_generic_handle_packet: ctrl buffer too small (%d > %zu)\n",
104 s->setup_len, sizeof(s->data_buf));
105 return USB_RET_STALL;
106 }
89b9b79f
AL
107 if (s->setup_len == 0)
108 s->setup_state = SETUP_STATE_ACK;
109 else
110 s->setup_state = SETUP_STATE_DATA;
111 }
112
113 return ret;
114}
115
116static int do_token_in(USBDevice *s, USBPacket *p)
bb36d470 117{
89b9b79f
AL
118 int request, value, index;
119 int ret = 0;
120
121 if (p->devep != 0)
806b6024 122 return s->info->handle_data(s, p);
89b9b79f
AL
123
124 request = (s->setup_buf[0] << 8) | s->setup_buf[1];
125 value = (s->setup_buf[3] << 8) | s->setup_buf[2];
126 index = (s->setup_buf[5] << 8) | s->setup_buf[4];
127
128 switch(s->setup_state) {
129 case SETUP_STATE_ACK:
130 if (!(s->setup_buf[0] & USB_DIR_IN)) {
007fd62f 131 ret = s->info->handle_control(s, p, request, value, index,
806b6024 132 s->setup_len, s->data_buf);
007fd62f
HG
133 if (ret == USB_RET_ASYNC) {
134 return USB_RET_ASYNC;
135 }
136 s->setup_state = SETUP_STATE_IDLE;
89b9b79f
AL
137 if (ret > 0)
138 return 0;
139 return ret;
140 }
141
142 /* return 0 byte */
143 return 0;
144
145 case SETUP_STATE_DATA:
146 if (s->setup_buf[0] & USB_DIR_IN) {
147 int len = s->setup_len - s->setup_index;
148 if (len > p->len)
149 len = p->len;
150 memcpy(p->data, s->data_buf + s->setup_index, len);
151 s->setup_index += len;
152 if (s->setup_index >= s->setup_len)
153 s->setup_state = SETUP_STATE_ACK;
154 return len;
155 }
156
157 s->setup_state = SETUP_STATE_IDLE;
158 return USB_RET_STALL;
159
160 default:
161 return USB_RET_STALL;
162 }
163}
164
165static int do_token_out(USBDevice *s, USBPacket *p)
166{
167 if (p->devep != 0)
806b6024 168 return s->info->handle_data(s, p);
89b9b79f
AL
169
170 switch(s->setup_state) {
171 case SETUP_STATE_ACK:
172 if (s->setup_buf[0] & USB_DIR_IN) {
173 s->setup_state = SETUP_STATE_IDLE;
174 /* transfer OK */
175 } else {
176 /* ignore additional output */
177 }
178 return 0;
179
180 case SETUP_STATE_DATA:
181 if (!(s->setup_buf[0] & USB_DIR_IN)) {
182 int len = s->setup_len - s->setup_index;
183 if (len > p->len)
184 len = p->len;
185 memcpy(s->data_buf + s->setup_index, p->data, len);
186 s->setup_index += len;
187 if (s->setup_index >= s->setup_len)
188 s->setup_state = SETUP_STATE_ACK;
189 return len;
190 }
191
192 s->setup_state = SETUP_STATE_IDLE;
193 return USB_RET_STALL;
194
195 default:
196 return USB_RET_STALL;
197 }
198}
bb36d470 199
89b9b79f
AL
200/*
201 * Generic packet handler.
202 * Called by the HC (host controller).
203 *
204 * Returns length of the transaction or one of the USB_RET_XXX codes.
205 */
206int usb_generic_handle_packet(USBDevice *s, USBPacket *p)
207{
4d611c9a 208 switch(p->pid) {
bb36d470
FB
209 case USB_MSG_ATTACH:
210 s->state = USB_STATE_ATTACHED;
b6f77fbe
GH
211 if (s->info->handle_attach) {
212 s->info->handle_attach(s);
213 }
89b9b79f
AL
214 return 0;
215
bb36d470
FB
216 case USB_MSG_DETACH:
217 s->state = USB_STATE_NOTATTACHED;
89b9b79f
AL
218 return 0;
219
bb36d470
FB
220 case USB_MSG_RESET:
221 s->remote_wakeup = 0;
222 s->addr = 0;
223 s->state = USB_STATE_DEFAULT;
b6f77fbe
GH
224 if (s->info->handle_reset) {
225 s->info->handle_reset(s);
226 }
89b9b79f
AL
227 return 0;
228 }
229
230 /* Rest of the PIDs must match our address */
231 if (s->state < USB_STATE_DEFAULT || p->devaddr != s->addr)
232 return USB_RET_NODEV;
233
234 switch (p->pid) {
bb36d470 235 case USB_TOKEN_SETUP:
89b9b79f
AL
236 return do_token_setup(s, p);
237
bb36d470 238 case USB_TOKEN_IN:
89b9b79f
AL
239 return do_token_in(s, p);
240
bb36d470 241 case USB_TOKEN_OUT:
89b9b79f
AL
242 return do_token_out(s, p);
243
bb36d470 244 default:
89b9b79f 245 return USB_RET_STALL;
bb36d470 246 }
bb36d470
FB
247}
248
50b7963e
HG
249/* ctrl complete function for devices which use usb_generic_handle_packet and
250 may return USB_RET_ASYNC from their handle_control callback. Device code
251 which does this *must* call this function instead of the normal
252 usb_packet_complete to complete their async control packets. */
253void usb_generic_async_ctrl_complete(USBDevice *s, USBPacket *p)
254{
255 if (p->len < 0) {
256 s->setup_state = SETUP_STATE_IDLE;
257 }
258
259 switch (s->setup_state) {
260 case SETUP_STATE_SETUP:
261 if (p->len < s->setup_len) {
262 s->setup_len = p->len;
263 }
264 s->setup_state = SETUP_STATE_DATA;
265 p->len = 8;
266 break;
267
268 case SETUP_STATE_ACK:
269 s->setup_state = SETUP_STATE_IDLE;
270 p->len = 0;
271 break;
272
273 default:
274 break;
275 }
276 usb_packet_complete(s, p);
277}
278
bb36d470
FB
279/* XXX: fix overflow */
280int set_usb_string(uint8_t *buf, const char *str)
281{
282 int len, i;
283 uint8_t *q;
284
285 q = buf;
286 len = strlen(str);
ce5c37c2 287 *q++ = 2 * len + 2;
bb36d470
FB
288 *q++ = 3;
289 for(i = 0; i < len; i++) {
290 *q++ = str[i];
291 *q++ = 0;
292 }
293 return q - buf;
294}
4d611c9a
PB
295
296/* Send an internal message to a USB device. */
297void usb_send_msg(USBDevice *dev, int msg)
298{
299 USBPacket p;
53aa8c0e
GH
300 int ret;
301
4d611c9a
PB
302 memset(&p, 0, sizeof(p));
303 p.pid = msg;
53aa8c0e 304 ret = usb_handle_packet(dev, &p);
89b9b79f 305 /* This _must_ be synchronous */
53aa8c0e
GH
306 assert(ret != USB_RET_ASYNC);
307}
308
309/* Hand over a packet to a device for processing. Return value
310 USB_RET_ASYNC indicates the processing isn't finished yet, the
311 driver will call usb_packet_complete() when done processing it. */
312int usb_handle_packet(USBDevice *dev, USBPacket *p)
313{
314 int ret;
315
4ff658fb 316 assert(p->owner == NULL);
53aa8c0e 317 ret = dev->info->handle_packet(dev, p);
4ff658fb
GH
318 if (ret == USB_RET_ASYNC) {
319 if (p->owner == NULL) {
320 p->owner = dev;
321 } else {
322 /* We'll end up here when usb_handle_packet is called
323 * recursively due to a hub being in the chain. Nothing
324 * to do. Leave p->owner pointing to the device, not the
325 * hub. */;
326 }
327 }
53aa8c0e 328 return ret;
89b9b79f 329}
4ff658fb
GH
330
331/* Notify the controller that an async packet is complete. This should only
332 be called for packets previously deferred by returning USB_RET_ASYNC from
333 handle_packet. */
334void usb_packet_complete(USBDevice *dev, USBPacket *p)
335{
336 /* Note: p->owner != dev is possible in case dev is a hub */
337 assert(p->owner != NULL);
338 dev->port->ops->complete(dev, p);
339 p->owner = NULL;
340}
341
342/* Cancel an active packet. The packed must have been deferred by
343 returning USB_RET_ASYNC from handle_packet, and not yet
344 completed. */
345void usb_cancel_packet(USBPacket * p)
346{
347 assert(p->owner != NULL);
eb5e680a 348 p->owner->info->cancel_packet(p->owner, p);
4ff658fb
GH
349 p->owner = NULL;
350}