4 * Copyright (c) 2005 Fabrice Bellard
6 * 2008 Generic packet handler rewrite by Max Krasnyansky
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:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
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
26 #include "qemu-common.h"
30 void usb_attach(USBPort
*port
)
32 USBDevice
*dev
= port
->dev
;
35 assert(dev
->attached
);
36 assert(dev
->state
== USB_STATE_NOTATTACHED
);
37 port
->ops
->attach(port
);
38 usb_send_msg(dev
, USB_MSG_ATTACH
);
41 void usb_detach(USBPort
*port
)
43 USBDevice
*dev
= port
->dev
;
46 assert(dev
->state
!= USB_STATE_NOTATTACHED
);
47 port
->ops
->detach(port
);
48 usb_send_msg(dev
, USB_MSG_DETACH
);
51 void usb_reset(USBPort
*port
)
53 USBDevice
*dev
= port
->dev
;
58 usb_send_msg(dev
, USB_MSG_RESET
);
61 void usb_wakeup(USBDevice
*dev
)
63 if (dev
->remote_wakeup
&& dev
->port
&& dev
->port
->ops
->wakeup
) {
64 dev
->port
->ops
->wakeup(dev
->port
);
68 /**********************/
70 /* generic USB device helpers (you are not forced to use them when
71 writing your USB device driver, but they help handling the
75 #define SETUP_STATE_IDLE 0
76 #define SETUP_STATE_SETUP 1
77 #define SETUP_STATE_DATA 2
78 #define SETUP_STATE_ACK 3
80 static int do_token_setup(USBDevice
*s
, USBPacket
*p
)
82 int request
, value
, index
;
85 if (p
->iov
.size
!= 8) {
89 usb_packet_copy(p
, s
->setup_buf
, p
->iov
.size
);
90 s
->setup_len
= (s
->setup_buf
[7] << 8) | s
->setup_buf
[6];
93 request
= (s
->setup_buf
[0] << 8) | s
->setup_buf
[1];
94 value
= (s
->setup_buf
[3] << 8) | s
->setup_buf
[2];
95 index
= (s
->setup_buf
[5] << 8) | s
->setup_buf
[4];
97 if (s
->setup_buf
[0] & USB_DIR_IN
) {
98 ret
= s
->info
->handle_control(s
, p
, request
, value
, index
,
99 s
->setup_len
, s
->data_buf
);
100 if (ret
== USB_RET_ASYNC
) {
101 s
->setup_state
= SETUP_STATE_SETUP
;
102 return USB_RET_ASYNC
;
107 if (ret
< s
->setup_len
)
109 s
->setup_state
= SETUP_STATE_DATA
;
111 if (s
->setup_len
> sizeof(s
->data_buf
)) {
113 "usb_generic_handle_packet: ctrl buffer too small (%d > %zu)\n",
114 s
->setup_len
, sizeof(s
->data_buf
));
115 return USB_RET_STALL
;
117 if (s
->setup_len
== 0)
118 s
->setup_state
= SETUP_STATE_ACK
;
120 s
->setup_state
= SETUP_STATE_DATA
;
126 static int do_token_in(USBDevice
*s
, USBPacket
*p
)
128 int request
, value
, index
;
132 return s
->info
->handle_data(s
, p
);
134 request
= (s
->setup_buf
[0] << 8) | s
->setup_buf
[1];
135 value
= (s
->setup_buf
[3] << 8) | s
->setup_buf
[2];
136 index
= (s
->setup_buf
[5] << 8) | s
->setup_buf
[4];
138 switch(s
->setup_state
) {
139 case SETUP_STATE_ACK
:
140 if (!(s
->setup_buf
[0] & USB_DIR_IN
)) {
141 ret
= s
->info
->handle_control(s
, p
, request
, value
, index
,
142 s
->setup_len
, s
->data_buf
);
143 if (ret
== USB_RET_ASYNC
) {
144 return USB_RET_ASYNC
;
146 s
->setup_state
= SETUP_STATE_IDLE
;
155 case SETUP_STATE_DATA
:
156 if (s
->setup_buf
[0] & USB_DIR_IN
) {
157 int len
= s
->setup_len
- s
->setup_index
;
158 if (len
> p
->iov
.size
) {
161 usb_packet_copy(p
, s
->data_buf
+ s
->setup_index
, len
);
162 s
->setup_index
+= len
;
163 if (s
->setup_index
>= s
->setup_len
)
164 s
->setup_state
= SETUP_STATE_ACK
;
168 s
->setup_state
= SETUP_STATE_IDLE
;
169 return USB_RET_STALL
;
172 return USB_RET_STALL
;
176 static int do_token_out(USBDevice
*s
, USBPacket
*p
)
179 return s
->info
->handle_data(s
, p
);
181 switch(s
->setup_state
) {
182 case SETUP_STATE_ACK
:
183 if (s
->setup_buf
[0] & USB_DIR_IN
) {
184 s
->setup_state
= SETUP_STATE_IDLE
;
187 /* ignore additional output */
191 case SETUP_STATE_DATA
:
192 if (!(s
->setup_buf
[0] & USB_DIR_IN
)) {
193 int len
= s
->setup_len
- s
->setup_index
;
194 if (len
> p
->iov
.size
) {
197 usb_packet_copy(p
, s
->data_buf
+ s
->setup_index
, len
);
198 s
->setup_index
+= len
;
199 if (s
->setup_index
>= s
->setup_len
)
200 s
->setup_state
= SETUP_STATE_ACK
;
204 s
->setup_state
= SETUP_STATE_IDLE
;
205 return USB_RET_STALL
;
208 return USB_RET_STALL
;
213 * Generic packet handler.
214 * Called by the HC (host controller).
216 * Returns length of the transaction or one of the USB_RET_XXX codes.
218 int usb_generic_handle_packet(USBDevice
*s
, USBPacket
*p
)
222 s
->state
= USB_STATE_ATTACHED
;
223 if (s
->info
->handle_attach
) {
224 s
->info
->handle_attach(s
);
229 s
->state
= USB_STATE_NOTATTACHED
;
233 s
->remote_wakeup
= 0;
235 s
->state
= USB_STATE_DEFAULT
;
236 if (s
->info
->handle_reset
) {
237 s
->info
->handle_reset(s
);
242 /* Rest of the PIDs must match our address */
243 if (s
->state
< USB_STATE_DEFAULT
|| p
->devaddr
!= s
->addr
)
244 return USB_RET_NODEV
;
247 case USB_TOKEN_SETUP
:
248 return do_token_setup(s
, p
);
251 return do_token_in(s
, p
);
254 return do_token_out(s
, p
);
257 return USB_RET_STALL
;
261 /* ctrl complete function for devices which use usb_generic_handle_packet and
262 may return USB_RET_ASYNC from their handle_control callback. Device code
263 which does this *must* call this function instead of the normal
264 usb_packet_complete to complete their async control packets. */
265 void usb_generic_async_ctrl_complete(USBDevice
*s
, USBPacket
*p
)
268 s
->setup_state
= SETUP_STATE_IDLE
;
271 switch (s
->setup_state
) {
272 case SETUP_STATE_SETUP
:
273 if (p
->result
< s
->setup_len
) {
274 s
->setup_len
= p
->result
;
276 s
->setup_state
= SETUP_STATE_DATA
;
280 case SETUP_STATE_ACK
:
281 s
->setup_state
= SETUP_STATE_IDLE
;
288 usb_packet_complete(s
, p
);
291 /* XXX: fix overflow */
292 int set_usb_string(uint8_t *buf
, const char *str
)
301 for(i
= 0; i
< len
; i
++) {
308 /* Send an internal message to a USB device. */
309 void usb_send_msg(USBDevice
*dev
, int msg
)
314 memset(&p
, 0, sizeof(p
));
316 ret
= usb_handle_packet(dev
, &p
);
317 /* This _must_ be synchronous */
318 assert(ret
!= USB_RET_ASYNC
);
321 /* Hand over a packet to a device for processing. Return value
322 USB_RET_ASYNC indicates the processing isn't finished yet, the
323 driver will call usb_packet_complete() when done processing it. */
324 int usb_handle_packet(USBDevice
*dev
, USBPacket
*p
)
328 assert(p
->owner
== NULL
);
329 ret
= dev
->info
->handle_packet(dev
, p
);
330 if (ret
== USB_RET_ASYNC
) {
331 if (p
->owner
== NULL
) {
332 p
->owner
= usb_ep_get(dev
, p
->pid
, p
->devep
);
334 /* We'll end up here when usb_handle_packet is called
335 * recursively due to a hub being in the chain. Nothing
336 * to do. Leave p->owner pointing to the device, not the
343 /* Notify the controller that an async packet is complete. This should only
344 be called for packets previously deferred by returning USB_RET_ASYNC from
346 void usb_packet_complete(USBDevice
*dev
, USBPacket
*p
)
348 /* Note: p->owner != dev is possible in case dev is a hub */
349 assert(p
->owner
!= NULL
);
351 dev
->port
->ops
->complete(dev
->port
, p
);
354 /* Cancel an active packet. The packed must have been deferred by
355 returning USB_RET_ASYNC from handle_packet, and not yet
357 void usb_cancel_packet(USBPacket
* p
)
359 assert(p
->owner
!= NULL
);
360 p
->owner
->dev
->info
->cancel_packet(p
->owner
->dev
, p
);
365 void usb_packet_init(USBPacket
*p
)
367 qemu_iovec_init(&p
->iov
, 1);
370 void usb_packet_setup(USBPacket
*p
, int pid
, uint8_t addr
, uint8_t ep
)
376 qemu_iovec_reset(&p
->iov
);
379 void usb_packet_addbuf(USBPacket
*p
, void *ptr
, size_t len
)
381 qemu_iovec_add(&p
->iov
, ptr
, len
);
384 void usb_packet_copy(USBPacket
*p
, void *ptr
, size_t bytes
)
386 assert(p
->result
>= 0);
387 assert(p
->result
+ bytes
<= p
->iov
.size
);
389 case USB_TOKEN_SETUP
:
391 iov_to_buf(p
->iov
.iov
, p
->iov
.niov
, ptr
, p
->result
, bytes
);
394 iov_from_buf(p
->iov
.iov
, p
->iov
.niov
, ptr
, p
->result
, bytes
);
397 fprintf(stderr
, "%s: invalid pid: %x\n", __func__
, p
->pid
);
403 void usb_packet_skip(USBPacket
*p
, size_t bytes
)
405 assert(p
->result
>= 0);
406 assert(p
->result
+ bytes
<= p
->iov
.size
);
407 if (p
->pid
== USB_TOKEN_IN
) {
408 iov_clear(p
->iov
.iov
, p
->iov
.niov
, p
->result
, bytes
);
413 void usb_packet_cleanup(USBPacket
*p
)
415 qemu_iovec_destroy(&p
->iov
);
418 void usb_ep_init(USBDevice
*dev
)
422 dev
->ep_ctl
.type
= USB_ENDPOINT_XFER_CONTROL
;
423 dev
->ep_ctl
.ifnum
= 0;
424 dev
->ep_ctl
.dev
= dev
;
425 for (ep
= 0; ep
< USB_MAX_ENDPOINTS
; ep
++) {
426 dev
->ep_in
[ep
].type
= USB_ENDPOINT_XFER_INVALID
;
427 dev
->ep_out
[ep
].type
= USB_ENDPOINT_XFER_INVALID
;
428 dev
->ep_in
[ep
].ifnum
= 0;
429 dev
->ep_out
[ep
].ifnum
= 0;
430 dev
->ep_in
[ep
].dev
= dev
;
431 dev
->ep_out
[ep
].dev
= dev
;
435 void usb_ep_dump(USBDevice
*dev
)
437 static const char *tname
[] = {
438 [USB_ENDPOINT_XFER_CONTROL
] = "control",
439 [USB_ENDPOINT_XFER_ISOC
] = "isoc",
440 [USB_ENDPOINT_XFER_BULK
] = "bulk",
441 [USB_ENDPOINT_XFER_INT
] = "int",
443 int ifnum
, ep
, first
;
445 fprintf(stderr
, "Device \"%s\", config %d\n",
446 dev
->product_desc
, dev
->configuration
);
447 for (ifnum
= 0; ifnum
< 16; ifnum
++) {
449 for (ep
= 0; ep
< USB_MAX_ENDPOINTS
; ep
++) {
450 if (dev
->ep_in
[ep
].type
!= USB_ENDPOINT_XFER_INVALID
&&
451 dev
->ep_in
[ep
].ifnum
== ifnum
) {
454 fprintf(stderr
, " Interface %d, alternative %d\n",
455 ifnum
, dev
->altsetting
[ifnum
]);
457 fprintf(stderr
, " Endpoint %d, IN, %s, %d max\n", ep
,
458 tname
[dev
->ep_in
[ep
].type
],
459 dev
->ep_in
[ep
].max_packet_size
);
461 if (dev
->ep_out
[ep
].type
!= USB_ENDPOINT_XFER_INVALID
&&
462 dev
->ep_out
[ep
].ifnum
== ifnum
) {
465 fprintf(stderr
, " Interface %d, alternative %d\n",
466 ifnum
, dev
->altsetting
[ifnum
]);
468 fprintf(stderr
, " Endpoint %d, OUT, %s, %d max\n", ep
,
469 tname
[dev
->ep_out
[ep
].type
],
470 dev
->ep_out
[ep
].max_packet_size
);
474 fprintf(stderr
, "--\n");
477 struct USBEndpoint
*usb_ep_get(USBDevice
*dev
, int pid
, int ep
)
479 struct USBEndpoint
*eps
= pid
== USB_TOKEN_IN
? dev
->ep_in
: dev
->ep_out
;
483 assert(pid
== USB_TOKEN_IN
|| pid
== USB_TOKEN_OUT
);
484 assert(ep
> 0 && ep
<= USB_MAX_ENDPOINTS
);
488 uint8_t usb_ep_get_type(USBDevice
*dev
, int pid
, int ep
)
490 struct USBEndpoint
*uep
= usb_ep_get(dev
, pid
, ep
);
494 void usb_ep_set_type(USBDevice
*dev
, int pid
, int ep
, uint8_t type
)
496 struct USBEndpoint
*uep
= usb_ep_get(dev
, pid
, ep
);
500 uint8_t usb_ep_get_ifnum(USBDevice
*dev
, int pid
, int ep
)
502 struct USBEndpoint
*uep
= usb_ep_get(dev
, pid
, ep
);
506 void usb_ep_set_ifnum(USBDevice
*dev
, int pid
, int ep
, uint8_t ifnum
)
508 struct USBEndpoint
*uep
= usb_ep_get(dev
, pid
, ep
);
512 void usb_ep_set_max_packet_size(USBDevice
*dev
, int pid
, int ep
,
515 struct USBEndpoint
*uep
= usb_ep_get(dev
, pid
, ep
);
516 int size
, microframes
;
519 switch ((raw
>> 11) & 3) {
530 uep
->max_packet_size
= size
* microframes
;
533 int usb_ep_get_max_packet_size(USBDevice
*dev
, int pid
, int ep
)
535 struct USBEndpoint
*uep
= usb_ep_get(dev
, pid
, ep
);
536 return uep
->max_packet_size
;