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"
29 void usb_attach(USBPort
*port
, USBDevice
*dev
)
34 usb_attach(port
, NULL
);
38 port
->ops
->attach(port
);
39 usb_send_msg(dev
, USB_MSG_ATTACH
);
44 port
->ops
->detach(port
);
45 usb_send_msg(dev
, USB_MSG_DETACH
);
51 void usb_wakeup(USBDevice
*dev
)
53 if (dev
->remote_wakeup
&& dev
->port
&& dev
->port
->ops
->wakeup
) {
54 dev
->port
->ops
->wakeup(dev
->port
);
58 /**********************/
60 /* generic USB device helpers (you are not forced to use them when
61 writing your USB device driver, but they help handling the
65 #define SETUP_STATE_IDLE 0
66 #define SETUP_STATE_SETUP 1
67 #define SETUP_STATE_DATA 2
68 #define SETUP_STATE_ACK 3
70 static int do_token_setup(USBDevice
*s
, USBPacket
*p
)
72 int request
, value
, index
;
78 memcpy(s
->setup_buf
, p
->data
, 8);
79 s
->setup_len
= (s
->setup_buf
[7] << 8) | s
->setup_buf
[6];
82 request
= (s
->setup_buf
[0] << 8) | s
->setup_buf
[1];
83 value
= (s
->setup_buf
[3] << 8) | s
->setup_buf
[2];
84 index
= (s
->setup_buf
[5] << 8) | s
->setup_buf
[4];
86 if (s
->setup_buf
[0] & USB_DIR_IN
) {
87 ret
= s
->info
->handle_control(s
, p
, request
, value
, index
,
88 s
->setup_len
, s
->data_buf
);
89 if (ret
== USB_RET_ASYNC
) {
90 s
->setup_state
= SETUP_STATE_SETUP
;
96 if (ret
< s
->setup_len
)
98 s
->setup_state
= SETUP_STATE_DATA
;
100 if (s
->setup_len
> sizeof(s
->data_buf
)) {
102 "usb_generic_handle_packet: ctrl buffer too small (%d > %zu)\n",
103 s
->setup_len
, sizeof(s
->data_buf
));
104 return USB_RET_STALL
;
106 if (s
->setup_len
== 0)
107 s
->setup_state
= SETUP_STATE_ACK
;
109 s
->setup_state
= SETUP_STATE_DATA
;
115 static int do_token_in(USBDevice
*s
, USBPacket
*p
)
117 int request
, value
, index
;
121 return s
->info
->handle_data(s
, p
);
123 request
= (s
->setup_buf
[0] << 8) | s
->setup_buf
[1];
124 value
= (s
->setup_buf
[3] << 8) | s
->setup_buf
[2];
125 index
= (s
->setup_buf
[5] << 8) | s
->setup_buf
[4];
127 switch(s
->setup_state
) {
128 case SETUP_STATE_ACK
:
129 if (!(s
->setup_buf
[0] & USB_DIR_IN
)) {
130 ret
= s
->info
->handle_control(s
, p
, request
, value
, index
,
131 s
->setup_len
, s
->data_buf
);
132 if (ret
== USB_RET_ASYNC
) {
133 return USB_RET_ASYNC
;
135 s
->setup_state
= SETUP_STATE_IDLE
;
144 case SETUP_STATE_DATA
:
145 if (s
->setup_buf
[0] & USB_DIR_IN
) {
146 int len
= s
->setup_len
- s
->setup_index
;
149 memcpy(p
->data
, s
->data_buf
+ s
->setup_index
, len
);
150 s
->setup_index
+= len
;
151 if (s
->setup_index
>= s
->setup_len
)
152 s
->setup_state
= SETUP_STATE_ACK
;
156 s
->setup_state
= SETUP_STATE_IDLE
;
157 return USB_RET_STALL
;
160 return USB_RET_STALL
;
164 static int do_token_out(USBDevice
*s
, USBPacket
*p
)
167 return s
->info
->handle_data(s
, p
);
169 switch(s
->setup_state
) {
170 case SETUP_STATE_ACK
:
171 if (s
->setup_buf
[0] & USB_DIR_IN
) {
172 s
->setup_state
= SETUP_STATE_IDLE
;
175 /* ignore additional output */
179 case SETUP_STATE_DATA
:
180 if (!(s
->setup_buf
[0] & USB_DIR_IN
)) {
181 int len
= s
->setup_len
- s
->setup_index
;
184 memcpy(s
->data_buf
+ s
->setup_index
, p
->data
, len
);
185 s
->setup_index
+= len
;
186 if (s
->setup_index
>= s
->setup_len
)
187 s
->setup_state
= SETUP_STATE_ACK
;
191 s
->setup_state
= SETUP_STATE_IDLE
;
192 return USB_RET_STALL
;
195 return USB_RET_STALL
;
200 * Generic packet handler.
201 * Called by the HC (host controller).
203 * Returns length of the transaction or one of the USB_RET_XXX codes.
205 int usb_generic_handle_packet(USBDevice
*s
, USBPacket
*p
)
209 s
->state
= USB_STATE_ATTACHED
;
210 if (s
->info
->handle_attach
) {
211 s
->info
->handle_attach(s
);
216 s
->state
= USB_STATE_NOTATTACHED
;
220 s
->remote_wakeup
= 0;
222 s
->state
= USB_STATE_DEFAULT
;
223 if (s
->info
->handle_reset
) {
224 s
->info
->handle_reset(s
);
229 /* Rest of the PIDs must match our address */
230 if (s
->state
< USB_STATE_DEFAULT
|| p
->devaddr
!= s
->addr
)
231 return USB_RET_NODEV
;
234 case USB_TOKEN_SETUP
:
235 return do_token_setup(s
, p
);
238 return do_token_in(s
, p
);
241 return do_token_out(s
, p
);
244 return USB_RET_STALL
;
248 /* ctrl complete function for devices which use usb_generic_handle_packet and
249 may return USB_RET_ASYNC from their handle_control callback. Device code
250 which does this *must* call this function instead of the normal
251 usb_packet_complete to complete their async control packets. */
252 void usb_generic_async_ctrl_complete(USBDevice
*s
, USBPacket
*p
)
255 s
->setup_state
= SETUP_STATE_IDLE
;
258 switch (s
->setup_state
) {
259 case SETUP_STATE_SETUP
:
260 if (p
->len
< s
->setup_len
) {
261 s
->setup_len
= p
->len
;
263 s
->setup_state
= SETUP_STATE_DATA
;
267 case SETUP_STATE_ACK
:
268 s
->setup_state
= SETUP_STATE_IDLE
;
275 usb_packet_complete(s
, p
);
278 /* XXX: fix overflow */
279 int set_usb_string(uint8_t *buf
, const char *str
)
288 for(i
= 0; i
< len
; i
++) {
295 /* Send an internal message to a USB device. */
296 void usb_send_msg(USBDevice
*dev
, int msg
)
301 memset(&p
, 0, sizeof(p
));
303 ret
= usb_handle_packet(dev
, &p
);
304 /* This _must_ be synchronous */
305 assert(ret
!= USB_RET_ASYNC
);
308 /* Hand over a packet to a device for processing. Return value
309 USB_RET_ASYNC indicates the processing isn't finished yet, the
310 driver will call usb_packet_complete() when done processing it. */
311 int usb_handle_packet(USBDevice
*dev
, USBPacket
*p
)
315 assert(p
->owner
== NULL
);
316 ret
= dev
->info
->handle_packet(dev
, p
);
317 if (ret
== USB_RET_ASYNC
) {
318 if (p
->owner
== NULL
) {
321 /* We'll end up here when usb_handle_packet is called
322 * recursively due to a hub being in the chain. Nothing
323 * to do. Leave p->owner pointing to the device, not the
330 /* Notify the controller that an async packet is complete. This should only
331 be called for packets previously deferred by returning USB_RET_ASYNC from
333 void usb_packet_complete(USBDevice
*dev
, USBPacket
*p
)
335 /* Note: p->owner != dev is possible in case dev is a hub */
336 assert(p
->owner
!= NULL
);
337 dev
->port
->ops
->complete(dev
->port
, p
);
341 /* Cancel an active packet. The packed must have been deferred by
342 returning USB_RET_ASYNC from handle_packet, and not yet
344 void usb_cancel_packet(USBPacket
* p
)
346 assert(p
->owner
!= NULL
);
347 p
->owner
->info
->cancel_packet(p
->owner
, p
);