root / hw / usb.c @ d47e59b8
History | View | Annotate | Download (9.5 kB)
1 |
/*
|
---|---|
2 |
* QEMU USB emulation
|
3 |
*
|
4 |
* Copyright (c) 2005 Fabrice Bellard
|
5 |
*
|
6 |
* 2008 Generic packet handler rewrite by Max Krasnyansky
|
7 |
*
|
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 |
*/
|
26 |
#include "qemu-common.h" |
27 |
#include "usb.h" |
28 |
|
29 |
void usb_attach(USBPort *port, USBDevice *dev)
|
30 |
{ |
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 |
} |
50 |
} |
51 |
|
52 |
void usb_wakeup(USBDevice *dev)
|
53 |
{ |
54 |
if (dev->remote_wakeup && dev->port && dev->port->ops->wakeup) {
|
55 |
dev->port->ops->wakeup(dev->port); |
56 |
} |
57 |
} |
58 |
|
59 |
/**********************/
|
60 |
|
61 |
/* generic USB device helpers (you are not forced to use them when
|
62 |
writing your USB device driver, but they help handling the
|
63 |
protocol)
|
64 |
*/
|
65 |
|
66 |
#define SETUP_STATE_IDLE 0 |
67 |
#define SETUP_STATE_SETUP 1 |
68 |
#define SETUP_STATE_DATA 2 |
69 |
#define SETUP_STATE_ACK 3 |
70 |
|
71 |
static 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]; |
86 |
|
87 |
if (s->setup_buf[0] & USB_DIR_IN) { |
88 |
ret = s->info->handle_control(s, p, request, value, index, |
89 |
s->setup_len, s->data_buf); |
90 |
if (ret == USB_RET_ASYNC) {
|
91 |
s->setup_state = SETUP_STATE_SETUP; |
92 |
return USB_RET_ASYNC;
|
93 |
} |
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 {
|
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 |
} |
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 |
|
116 |
static int do_token_in(USBDevice *s, USBPacket *p) |
117 |
{ |
118 |
int request, value, index;
|
119 |
int ret = 0; |
120 |
|
121 |
if (p->devep != 0) |
122 |
return s->info->handle_data(s, p);
|
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)) { |
131 |
ret = s->info->handle_control(s, p, request, value, index, |
132 |
s->setup_len, s->data_buf); |
133 |
if (ret == USB_RET_ASYNC) {
|
134 |
return USB_RET_ASYNC;
|
135 |
} |
136 |
s->setup_state = SETUP_STATE_IDLE; |
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 |
|
165 |
static int do_token_out(USBDevice *s, USBPacket *p) |
166 |
{ |
167 |
if (p->devep != 0) |
168 |
return s->info->handle_data(s, p);
|
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 |
} |
199 |
|
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 |
*/
|
206 |
int usb_generic_handle_packet(USBDevice *s, USBPacket *p)
|
207 |
{ |
208 |
switch(p->pid) {
|
209 |
case USB_MSG_ATTACH:
|
210 |
s->state = USB_STATE_ATTACHED; |
211 |
if (s->info->handle_attach) {
|
212 |
s->info->handle_attach(s); |
213 |
} |
214 |
return 0; |
215 |
|
216 |
case USB_MSG_DETACH:
|
217 |
s->state = USB_STATE_NOTATTACHED; |
218 |
return 0; |
219 |
|
220 |
case USB_MSG_RESET:
|
221 |
s->remote_wakeup = 0;
|
222 |
s->addr = 0;
|
223 |
s->state = USB_STATE_DEFAULT; |
224 |
if (s->info->handle_reset) {
|
225 |
s->info->handle_reset(s); |
226 |
} |
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) {
|
235 |
case USB_TOKEN_SETUP:
|
236 |
return do_token_setup(s, p);
|
237 |
|
238 |
case USB_TOKEN_IN:
|
239 |
return do_token_in(s, p);
|
240 |
|
241 |
case USB_TOKEN_OUT:
|
242 |
return do_token_out(s, p);
|
243 |
|
244 |
default:
|
245 |
return USB_RET_STALL;
|
246 |
} |
247 |
} |
248 |
|
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. */
|
253 |
void 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 |
|
279 |
/* XXX: fix overflow */
|
280 |
int 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); |
287 |
*q++ = 2 * len + 2; |
288 |
*q++ = 3;
|
289 |
for(i = 0; i < len; i++) { |
290 |
*q++ = str[i]; |
291 |
*q++ = 0;
|
292 |
} |
293 |
return q - buf;
|
294 |
} |
295 |
|
296 |
/* Send an internal message to a USB device. */
|
297 |
void usb_send_msg(USBDevice *dev, int msg) |
298 |
{ |
299 |
USBPacket p; |
300 |
int ret;
|
301 |
|
302 |
memset(&p, 0, sizeof(p)); |
303 |
p.pid = msg; |
304 |
ret = usb_handle_packet(dev, &p); |
305 |
/* This _must_ be synchronous */
|
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. */
|
312 |
int usb_handle_packet(USBDevice *dev, USBPacket *p)
|
313 |
{ |
314 |
int ret;
|
315 |
|
316 |
assert(p->owner == NULL);
|
317 |
ret = dev->info->handle_packet(dev, p); |
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 |
} |
328 |
return ret;
|
329 |
} |
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. */
|
334 |
void 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->port, 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. */
|
345 |
void usb_cancel_packet(USBPacket * p)
|
346 |
{ |
347 |
assert(p->owner != NULL);
|
348 |
p->owner->info->cancel_packet(p->owner, p); |
349 |
p->owner = NULL;
|
350 |
} |