2 * xen paravirt usb device backend
4 * (c) Juergen Gross <jgross@suse.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; under version 2 of the License.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, see <http://www.gnu.org/licenses/>.
18 * Contributions after 2012-01-13 are licensed under the terms of the
19 * GNU GPL, version 2 or (at your option) any later version.
22 #include "qemu/osdep.h"
26 #include "qemu/config-file.h"
27 #include "qemu/main-loop.h"
28 #include "qemu/option.h"
29 #include "hw/sysbus.h"
31 #include "hw/xen/xen-legacy-backend.h"
32 #include "monitor/qdev.h"
33 #include "qapi/error.h"
34 #include "qapi/qmp/qdict.h"
35 #include "qapi/qmp/qstring.h"
37 #include "hw/xen/interface/io/usbif.h"
40 * Check for required support of usbif.h: USBIF_SHORT_NOT_OK was the last
41 * macro added we rely on.
43 #ifdef USBIF_SHORT_NOT_OK
45 #define TR(xendev, lvl, fmt, args...) \
49 gettimeofday(&tv, NULL); \
50 xen_pv_printf(xendev, lvl, "%8ld.%06ld xen-usb(%s):" fmt, \
51 tv.tv_sec, tv.tv_usec, __func__, ##args); \
53 #define TR_BUS(xendev, fmt, args...) TR(xendev, 2, fmt, ##args)
54 #define TR_REQ(xendev, fmt, args...) TR(xendev, 3, fmt, ##args)
56 #define USBBACK_MAXPORTS USBIF_PIPE_PORT_MASK
57 #define USB_DEV_ADDR_SIZE (USBIF_PIPE_DEV_MASK + 1)
59 /* USB wire protocol: structure describing control request parameter. */
60 struct usbif_ctrlrequest
{
76 QTAILQ_HEAD(, usbback_req
) submit_q
;
80 struct usbback_info
*usbif
;
81 struct usbback_stub
*stub
;
82 struct usbif_urb_request req
;
85 unsigned int nr_buffer_segs
; /* # of transfer_buffer segments */
86 unsigned int nr_extra_segs
; /* # of iso_frame_desc segments */
88 QTAILQ_ENTRY(usbback_req
) q
;
92 struct libusb_transfer
*xfer
;
97 struct usbback_hotplug
{
98 QSIMPLEQ_ENTRY(usbback_hotplug
) q
;
102 struct usbback_info
{
103 struct XenLegacyDevice xendev
; /* must be first */
107 struct usbif_urb_back_ring urb_ring
;
108 struct usbif_conn_back_ring conn_ring
;
112 QTAILQ_HEAD(, usbback_req
) req_free_q
;
113 QSIMPLEQ_HEAD(, usbback_hotplug
) hotplug_q
;
114 struct usbback_stub ports
[USBBACK_MAXPORTS
];
115 struct usbback_stub
*addr_table
[USB_DEV_ADDR_SIZE
];
119 static struct usbback_req
*usbback_get_req(struct usbback_info
*usbif
)
121 struct usbback_req
*usbback_req
;
123 if (QTAILQ_EMPTY(&usbif
->req_free_q
)) {
124 usbback_req
= g_new0(struct usbback_req
, 1);
126 usbback_req
= QTAILQ_FIRST(&usbif
->req_free_q
);
127 QTAILQ_REMOVE(&usbif
->req_free_q
, usbback_req
, q
);
132 static void usbback_put_req(struct usbback_req
*usbback_req
)
134 struct usbback_info
*usbif
;
136 usbif
= usbback_req
->usbif
;
137 memset(usbback_req
, 0, sizeof(*usbback_req
));
138 QTAILQ_INSERT_HEAD(&usbif
->req_free_q
, usbback_req
, q
);
141 static int usbback_gnttab_map(struct usbback_req
*usbback_req
)
143 unsigned int nr_segs
, i
, prot
;
144 uint32_t ref
[USBIF_MAX_SEGMENTS_PER_REQUEST
];
145 struct usbback_info
*usbif
= usbback_req
->usbif
;
146 struct XenLegacyDevice
*xendev
= &usbif
->xendev
;
147 struct usbif_request_segment
*seg
;
150 nr_segs
= usbback_req
->nr_buffer_segs
+ usbback_req
->nr_extra_segs
;
155 if (nr_segs
> USBIF_MAX_SEGMENTS_PER_REQUEST
) {
156 xen_pv_printf(xendev
, 0, "bad number of segments in request (%d)\n",
161 for (i
= 0; i
< nr_segs
; i
++) {
162 if ((unsigned)usbback_req
->req
.seg
[i
].offset
+
163 (unsigned)usbback_req
->req
.seg
[i
].length
> XC_PAGE_SIZE
) {
164 xen_pv_printf(xendev
, 0, "segment crosses page boundary\n");
169 if (usbback_req
->nr_buffer_segs
) {
171 if (usbif_pipein(usbback_req
->req
.pipe
)) {
174 for (i
= 0; i
< usbback_req
->nr_buffer_segs
; i
++) {
175 ref
[i
] = usbback_req
->req
.seg
[i
].gref
;
177 usbback_req
->buffer
=
178 xen_be_map_grant_refs(xendev
, ref
, usbback_req
->nr_buffer_segs
,
181 if (!usbback_req
->buffer
) {
185 for (i
= 0; i
< usbback_req
->nr_buffer_segs
; i
++) {
186 seg
= usbback_req
->req
.seg
+ i
;
187 addr
= usbback_req
->buffer
+ i
* XC_PAGE_SIZE
+ seg
->offset
;
188 qemu_iovec_add(&usbback_req
->packet
.iov
, addr
, seg
->length
);
192 if (!usbif_pipeisoc(usbback_req
->req
.pipe
)) {
197 * Right now isoc requests are not supported.
198 * Prepare supporting those by doing the work needed on the guest
202 if (!usbback_req
->nr_extra_segs
) {
203 xen_pv_printf(xendev
, 0, "iso request without descriptor segments\n");
207 prot
= PROT_READ
| PROT_WRITE
;
208 for (i
= 0; i
< usbback_req
->nr_extra_segs
; i
++) {
209 ref
[i
] = usbback_req
->req
.seg
[i
+ usbback_req
->req
.nr_buffer_segs
].gref
;
211 usbback_req
->isoc_buffer
=
212 xen_be_map_grant_refs(xendev
, ref
, usbback_req
->nr_extra_segs
,
215 if (!usbback_req
->isoc_buffer
) {
222 static int usbback_init_packet(struct usbback_req
*usbback_req
)
224 struct XenLegacyDevice
*xendev
= &usbback_req
->usbif
->xendev
;
225 USBPacket
*packet
= &usbback_req
->packet
;
226 USBDevice
*dev
= usbback_req
->stub
->dev
;
228 unsigned int pid
, ep_nr
;
232 qemu_iovec_init(&packet
->iov
, USBIF_MAX_SEGMENTS_PER_REQUEST
);
233 pid
= usbif_pipein(usbback_req
->req
.pipe
) ? USB_TOKEN_IN
: USB_TOKEN_OUT
;
234 ep_nr
= usbif_pipeendpoint(usbback_req
->req
.pipe
);
235 sok
= !!(usbback_req
->req
.transfer_flags
& USBIF_SHORT_NOT_OK
);
236 if (usbif_pipectrl(usbback_req
->req
.pipe
)) {
240 ep
= usb_ep_get(dev
, pid
, ep_nr
);
241 usb_packet_setup(packet
, pid
, ep
, 0, 1, sok
, true);
243 switch (usbif_pipetype(usbback_req
->req
.pipe
)) {
244 case USBIF_PIPE_TYPE_ISOC
:
245 TR_REQ(xendev
, "iso transfer %s: buflen: %x, %d frames\n",
246 (pid
== USB_TOKEN_IN
) ? "in" : "out",
247 usbback_req
->req
.buffer_length
,
248 usbback_req
->req
.u
.isoc
.nr_frame_desc_segs
);
249 ret
= -EINVAL
; /* isoc not implemented yet */
252 case USBIF_PIPE_TYPE_INT
:
253 TR_REQ(xendev
, "int transfer %s: buflen: %x\n",
254 (pid
== USB_TOKEN_IN
) ? "in" : "out",
255 usbback_req
->req
.buffer_length
);
258 case USBIF_PIPE_TYPE_CTRL
:
259 packet
->parameter
= *(uint64_t *)usbback_req
->req
.u
.ctrl
;
260 TR_REQ(xendev
, "ctrl parameter: %"PRIx64
", buflen: %x\n",
262 usbback_req
->req
.buffer_length
);
265 case USBIF_PIPE_TYPE_BULK
:
266 TR_REQ(xendev
, "bulk transfer %s: buflen: %x\n",
267 (pid
== USB_TOKEN_IN
) ? "in" : "out",
268 usbback_req
->req
.buffer_length
);
278 static void usbback_do_response(struct usbback_req
*usbback_req
, int32_t status
,
279 int32_t actual_length
, int32_t error_count
)
281 struct usbback_info
*usbif
;
282 struct usbif_urb_response
*res
;
283 struct XenLegacyDevice
*xendev
;
286 usbif
= usbback_req
->usbif
;
287 xendev
= &usbif
->xendev
;
289 TR_REQ(xendev
, "id %d, status %d, length %d, errcnt %d\n",
290 usbback_req
->req
.id
, status
, actual_length
, error_count
);
292 if (usbback_req
->packet
.iov
.iov
) {
293 qemu_iovec_destroy(&usbback_req
->packet
.iov
);
296 if (usbback_req
->buffer
) {
297 xen_be_unmap_grant_refs(xendev
, usbback_req
->buffer
,
298 usbback_req
->nr_buffer_segs
);
299 usbback_req
->buffer
= NULL
;
302 if (usbback_req
->isoc_buffer
) {
303 xen_be_unmap_grant_refs(xendev
, usbback_req
->isoc_buffer
,
304 usbback_req
->nr_extra_segs
);
305 usbback_req
->isoc_buffer
= NULL
;
308 if (usbif
->urb_sring
) {
309 res
= RING_GET_RESPONSE(&usbif
->urb_ring
, usbif
->urb_ring
.rsp_prod_pvt
);
310 res
->id
= usbback_req
->req
.id
;
311 res
->status
= status
;
312 res
->actual_length
= actual_length
;
313 res
->error_count
= error_count
;
314 res
->start_frame
= 0;
315 usbif
->urb_ring
.rsp_prod_pvt
++;
316 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&usbif
->urb_ring
, notify
);
319 xen_pv_send_notify(xendev
);
323 if (!usbback_req
->cancelled
)
324 usbback_put_req(usbback_req
);
327 static void usbback_do_response_ret(struct usbback_req
*usbback_req
,
330 usbback_do_response(usbback_req
, status
, 0, 0);
333 static int32_t usbback_xlat_status(int status
)
336 case USB_RET_SUCCESS
:
344 case USB_RET_IOERROR
:
351 static void usbback_packet_complete(struct usbback_req
*usbback_req
)
353 USBPacket
*packet
= &usbback_req
->packet
;
356 QTAILQ_REMOVE(&usbback_req
->stub
->submit_q
, usbback_req
, q
);
358 status
= usbback_xlat_status(packet
->status
);
359 usbback_do_response(usbback_req
, status
, packet
->actual_length
, 0);
362 static void usbback_set_address(struct usbback_info
*usbif
,
363 struct usbback_stub
*stub
,
364 unsigned int cur_addr
, unsigned int new_addr
)
367 usbif
->addr_table
[cur_addr
] = NULL
;
370 usbif
->addr_table
[new_addr
] = stub
;
374 static void usbback_cancel_req(struct usbback_req
*usbback_req
)
376 if (usb_packet_is_inflight(&usbback_req
->packet
)) {
377 usb_cancel_packet(&usbback_req
->packet
);
378 QTAILQ_REMOVE(&usbback_req
->stub
->submit_q
, usbback_req
, q
);
379 usbback_req
->cancelled
= true;
380 usbback_do_response_ret(usbback_req
, -EPROTO
);
384 static void usbback_process_unlink_req(struct usbback_req
*usbback_req
)
386 struct usbback_info
*usbif
;
387 struct usbback_req
*unlink_req
;
388 unsigned int id
, devnum
;
391 usbif
= usbback_req
->usbif
;
393 id
= usbback_req
->req
.u
.unlink
.unlink_id
;
394 TR_REQ(&usbif
->xendev
, "unlink id %d\n", id
);
395 devnum
= usbif_pipedevice(usbback_req
->req
.pipe
);
396 if (unlikely(devnum
== 0)) {
397 usbback_req
->stub
= usbif
->ports
+
398 usbif_pipeportnum(usbback_req
->req
.pipe
) - 1;
399 if (unlikely(!usbback_req
->stub
)) {
404 if (unlikely(!usbif
->addr_table
[devnum
])) {
408 usbback_req
->stub
= usbif
->addr_table
[devnum
];
411 QTAILQ_FOREACH(unlink_req
, &usbback_req
->stub
->submit_q
, q
) {
412 if (unlink_req
->req
.id
== id
) {
413 usbback_cancel_req(unlink_req
);
419 usbback_do_response_ret(usbback_req
, ret
);
423 * Checks whether a request can be handled at once or should be forwarded
424 * to the usb framework.
426 * 0 in case of usb framework is needed
427 * 1 in case of local handling (no error)
428 * The request response has been queued already if return value not 0.
430 static int usbback_check_and_submit(struct usbback_req
*usbback_req
)
432 struct usbback_info
*usbif
;
434 struct usbback_stub
*stub
;
435 struct usbif_ctrlrequest
*ctrl
;
439 usbif
= usbback_req
->usbif
;
441 devnum
= usbif_pipedevice(usbback_req
->req
.pipe
);
442 ctrl
= (struct usbif_ctrlrequest
*)usbback_req
->req
.u
.ctrl
;
443 wValue
= le16_to_cpu(ctrl
->wValue
);
446 * When the device is first connected or resetted, USB device has no
447 * address. In this initial state, following requests are sent to device
450 * 1. GET_DESCRIPTOR (with Descriptor Type is "DEVICE") is sent,
451 * and OS knows what device is connected to.
453 * 2. SET_ADDRESS is sent, and then device has its address.
455 * In the next step, SET_CONFIGURATION is sent to addressed device, and
456 * then the device is finally ready to use.
458 if (unlikely(devnum
== 0)) {
459 stub
= usbif
->ports
+ usbif_pipeportnum(usbback_req
->req
.pipe
) - 1;
460 if (!stub
->dev
|| !stub
->attached
) {
465 switch (ctrl
->bRequest
) {
466 case USB_REQ_GET_DESCRIPTOR
:
468 * GET_DESCRIPTOR request to device #0.
469 * through normal transfer.
471 TR_REQ(&usbif
->xendev
, "devnum 0 GET_DESCRIPTOR\n");
472 usbback_req
->stub
= stub
;
474 case USB_REQ_SET_ADDRESS
:
476 * SET_ADDRESS request to device #0.
477 * add attached device to addr_table.
479 TR_REQ(&usbif
->xendev
, "devnum 0 SET_ADDRESS\n");
480 usbback_set_address(usbif
, stub
, 0, wValue
);
490 if (unlikely(!usbif
->addr_table
[devnum
])) {
494 usbback_req
->stub
= usbif
->addr_table
[devnum
];
497 * Check special request
499 if (ctrl
->bRequest
!= USB_REQ_SET_ADDRESS
) {
504 * SET_ADDRESS request to addressed device.
505 * change addr or remove from addr_table.
507 usbback_set_address(usbif
, usbback_req
->stub
, devnum
, wValue
);
511 usbback_do_response_ret(usbback_req
, ret
);
515 static void usbback_dispatch(struct usbback_req
*usbback_req
)
519 struct usbback_info
*usbif
;
521 usbif
= usbback_req
->usbif
;
523 TR_REQ(&usbif
->xendev
, "start req_id %d pipe %08x\n", usbback_req
->req
.id
,
524 usbback_req
->req
.pipe
);
527 if (unlikely(usbif_pipeunlink(usbback_req
->req
.pipe
))) {
528 usbback_process_unlink_req(usbback_req
);
532 if (usbif_pipectrl(usbback_req
->req
.pipe
)) {
533 if (usbback_check_and_submit(usbback_req
)) {
537 devnum
= usbif_pipedevice(usbback_req
->req
.pipe
);
538 usbback_req
->stub
= usbif
->addr_table
[devnum
];
540 if (!usbback_req
->stub
|| !usbback_req
->stub
->attached
) {
546 QTAILQ_INSERT_TAIL(&usbback_req
->stub
->submit_q
, usbback_req
, q
);
548 usbback_req
->nr_buffer_segs
= usbback_req
->req
.nr_buffer_segs
;
549 usbback_req
->nr_extra_segs
= usbif_pipeisoc(usbback_req
->req
.pipe
) ?
550 usbback_req
->req
.u
.isoc
.nr_frame_desc_segs
: 0;
552 ret
= usbback_init_packet(usbback_req
);
554 xen_pv_printf(&usbif
->xendev
, 0, "invalid request\n");
559 ret
= usbback_gnttab_map(usbback_req
);
561 xen_pv_printf(&usbif
->xendev
, 0, "invalid buffer, ret=%d\n", ret
);
566 usb_handle_packet(usbback_req
->stub
->dev
, &usbback_req
->packet
);
567 if (usbback_req
->packet
.status
!= USB_RET_ASYNC
) {
568 usbback_packet_complete(usbback_req
);
573 QTAILQ_REMOVE(&usbback_req
->stub
->submit_q
, usbback_req
, q
);
576 usbback_do_response_ret(usbback_req
, ret
);
579 static void usbback_hotplug_notify(struct usbback_info
*usbif
)
581 struct usbif_conn_back_ring
*ring
= &usbif
->conn_ring
;
582 struct usbif_conn_request req
;
583 struct usbif_conn_response
*res
;
584 struct usbback_hotplug
*usb_hp
;
587 if (!usbif
->conn_sring
) {
591 /* Check for full ring. */
592 if ((RING_SIZE(ring
) - ring
->rsp_prod_pvt
- ring
->req_cons
) == 0) {
593 xen_pv_send_notify(&usbif
->xendev
);
597 usb_hp
= QSIMPLEQ_FIRST(&usbif
->hotplug_q
);
598 QSIMPLEQ_REMOVE_HEAD(&usbif
->hotplug_q
, q
);
600 RING_COPY_REQUEST(ring
, ring
->req_cons
, &req
);
602 ring
->sring
->req_event
= ring
->req_cons
+ 1;
604 res
= RING_GET_RESPONSE(ring
, ring
->rsp_prod_pvt
);
606 res
->portnum
= usb_hp
->port
;
607 res
->speed
= usbif
->ports
[usb_hp
->port
- 1].speed
;
608 ring
->rsp_prod_pvt
++;
609 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(ring
, notify
);
612 xen_pv_send_notify(&usbif
->xendev
);
615 TR_BUS(&usbif
->xendev
, "hotplug port %d speed %d\n", usb_hp
->port
,
620 if (!QSIMPLEQ_EMPTY(&usbif
->hotplug_q
)) {
621 qemu_bh_schedule(usbif
->bh
);
625 static void usbback_bh(void *opaque
)
627 struct usbback_info
*usbif
;
628 struct usbif_urb_back_ring
*urb_ring
;
629 struct usbback_req
*usbback_req
;
631 unsigned int more_to_do
;
634 if (usbif
->ring_error
) {
638 if (!QSIMPLEQ_EMPTY(&usbif
->hotplug_q
)) {
639 usbback_hotplug_notify(usbif
);
642 urb_ring
= &usbif
->urb_ring
;
643 rc
= urb_ring
->req_cons
;
644 rp
= urb_ring
->sring
->req_prod
;
645 xen_rmb(); /* Ensure we see queued requests up to 'rp'. */
647 if (RING_REQUEST_PROD_OVERFLOW(urb_ring
, rp
)) {
648 rc
= urb_ring
->rsp_prod_pvt
;
649 xen_pv_printf(&usbif
->xendev
, 0, "domU provided bogus ring requests "
650 "(%#x - %#x = %u). Halting ring processing.\n",
652 usbif
->ring_error
= true;
657 if (RING_REQUEST_CONS_OVERFLOW(urb_ring
, rc
)) {
660 usbback_req
= usbback_get_req(usbif
);
662 RING_COPY_REQUEST(urb_ring
, rc
, &usbback_req
->req
);
663 usbback_req
->usbif
= usbif
;
665 usbback_dispatch(usbback_req
);
667 urb_ring
->req_cons
= ++rc
;
670 RING_FINAL_CHECK_FOR_REQUESTS(urb_ring
, more_to_do
);
672 qemu_bh_schedule(usbif
->bh
);
676 static void usbback_hotplug_enq(struct usbback_info
*usbif
, unsigned port
)
678 struct usbback_hotplug
*usb_hp
;
680 usb_hp
= g_new0(struct usbback_hotplug
, 1);
682 QSIMPLEQ_INSERT_TAIL(&usbif
->hotplug_q
, usb_hp
, q
);
683 usbback_hotplug_notify(usbif
);
686 static void usbback_portid_drain(struct usbback_info
*usbif
, unsigned port
)
688 struct usbback_req
*req
, *tmp
;
691 QTAILQ_FOREACH_SAFE(req
, &usbif
->ports
[port
- 1].submit_q
, q
, tmp
) {
692 usbback_cancel_req(req
);
697 qemu_bh_schedule(usbif
->bh
);
701 static void usbback_portid_detach(struct usbback_info
*usbif
, unsigned port
)
703 if (!usbif
->ports
[port
- 1].attached
) {
707 usbif
->ports
[port
- 1].speed
= USBIF_SPEED_NONE
;
708 usbif
->ports
[port
- 1].attached
= false;
709 usbback_portid_drain(usbif
, port
);
710 usbback_hotplug_enq(usbif
, port
);
713 static void usbback_portid_remove(struct usbback_info
*usbif
, unsigned port
)
715 if (!usbif
->ports
[port
- 1].dev
) {
719 object_unparent(OBJECT(usbif
->ports
[port
- 1].dev
));
720 usbif
->ports
[port
- 1].dev
= NULL
;
721 usbback_portid_detach(usbif
, port
);
723 TR_BUS(&usbif
->xendev
, "port %d removed\n", port
);
726 static void usbback_portid_add(struct usbback_info
*usbif
, unsigned port
,
731 Error
*local_err
= NULL
;
736 if (usbif
->ports
[port
- 1].dev
) {
740 portname
= strchr(busid
, '-');
742 xen_pv_printf(&usbif
->xendev
, 0, "device %s illegal specification\n",
749 qdict_put_str(qdict
, "driver", "usb-host");
750 tmp
= g_strdup_printf("%s.0", usbif
->xendev
.qdev
.id
);
751 qdict_put_str(qdict
, "bus", tmp
);
753 tmp
= g_strdup_printf("%s-%u", usbif
->xendev
.qdev
.id
, port
);
754 qdict_put_str(qdict
, "id", tmp
);
756 qdict_put_int(qdict
, "port", port
);
757 qdict_put_int(qdict
, "hostbus", atoi(busid
));
758 qdict_put_str(qdict
, "hostport", portname
);
759 opts
= qemu_opts_from_qdict(qemu_find_opts("device"), qdict
,
761 usbif
->ports
[port
- 1].dev
= USB_DEVICE(qdev_device_add(opts
, &local_err
));
762 if (!usbif
->ports
[port
- 1].dev
) {
763 qobject_unref(qdict
);
764 xen_pv_printf(&usbif
->xendev
, 0,
765 "device %s could not be opened: %s\n",
766 busid
, error_get_pretty(local_err
));
767 error_free(local_err
);
770 qobject_unref(qdict
);
771 speed
= usbif
->ports
[port
- 1].dev
->speed
;
774 speed
= USBIF_SPEED_LOW
;
777 speed
= USBIF_SPEED_FULL
;
780 speed
= (usbif
->usb_ver
< USB_VER_USB20
) ?
781 USBIF_SPEED_NONE
: USBIF_SPEED_HIGH
;
784 speed
= USBIF_SPEED_NONE
;
787 if (speed
== USBIF_SPEED_NONE
) {
788 xen_pv_printf(&usbif
->xendev
, 0, "device %s wrong speed\n", busid
);
789 object_unparent(OBJECT(usbif
->ports
[port
- 1].dev
));
790 usbif
->ports
[port
- 1].dev
= NULL
;
793 usb_device_reset(usbif
->ports
[port
- 1].dev
);
794 usbif
->ports
[port
- 1].speed
= speed
;
795 usbif
->ports
[port
- 1].attached
= true;
796 QTAILQ_INIT(&usbif
->ports
[port
- 1].submit_q
);
797 usbback_hotplug_enq(usbif
, port
);
799 TR_BUS(&usbif
->xendev
, "port %d attached\n", port
);
802 static void usbback_process_port(struct usbback_info
*usbif
, unsigned port
)
807 snprintf(node
, sizeof(node
), "port/%d", port
);
808 busid
= xenstore_read_be_str(&usbif
->xendev
, node
);
810 xen_pv_printf(&usbif
->xendev
, 0, "xenstore_read %s failed\n", node
);
814 /* Remove portid, if the port is not connected. */
815 if (strlen(busid
) == 0) {
816 usbback_portid_remove(usbif
, port
);
818 usbback_portid_add(usbif
, port
, busid
);
824 static void usbback_disconnect(struct XenLegacyDevice
*xendev
)
826 struct usbback_info
*usbif
;
829 TR_BUS(xendev
, "start\n");
831 usbif
= container_of(xendev
, struct usbback_info
, xendev
);
833 xen_pv_unbind_evtchn(xendev
);
835 if (usbif
->urb_sring
) {
836 xen_be_unmap_grant_ref(xendev
, usbif
->urb_sring
);
837 usbif
->urb_sring
= NULL
;
839 if (usbif
->conn_sring
) {
840 xen_be_unmap_grant_ref(xendev
, usbif
->conn_sring
);
841 usbif
->conn_sring
= NULL
;
844 for (i
= 0; i
< usbif
->num_ports
; i
++) {
845 if (usbif
->ports
[i
].dev
) {
846 usbback_portid_drain(usbif
, i
+ 1);
850 TR_BUS(xendev
, "finished\n");
853 static int usbback_connect(struct XenLegacyDevice
*xendev
)
855 struct usbback_info
*usbif
;
856 struct usbif_urb_sring
*urb_sring
;
857 struct usbif_conn_sring
*conn_sring
;
860 unsigned int i
, max_grants
;
862 TR_BUS(xendev
, "start\n");
864 /* max_grants: for each request and for the rings (request and connect). */
865 max_grants
= USBIF_MAX_SEGMENTS_PER_REQUEST
* USB_URB_RING_SIZE
+ 2;
866 xen_be_set_max_grant_refs(xendev
, max_grants
);
868 usbif
= container_of(xendev
, struct usbback_info
, xendev
);
870 if (xenstore_read_fe_int(xendev
, "urb-ring-ref", &urb_ring_ref
)) {
871 xen_pv_printf(xendev
, 0, "error reading urb-ring-ref\n");
874 if (xenstore_read_fe_int(xendev
, "conn-ring-ref", &conn_ring_ref
)) {
875 xen_pv_printf(xendev
, 0, "error reading conn-ring-ref\n");
878 if (xenstore_read_fe_int(xendev
, "event-channel", &xendev
->remote_port
)) {
879 xen_pv_printf(xendev
, 0, "error reading event-channel\n");
883 usbif
->urb_sring
= xen_be_map_grant_ref(xendev
, urb_ring_ref
,
884 PROT_READ
| PROT_WRITE
);
885 usbif
->conn_sring
= xen_be_map_grant_ref(xendev
, conn_ring_ref
,
886 PROT_READ
| PROT_WRITE
);
887 if (!usbif
->urb_sring
|| !usbif
->conn_sring
) {
888 xen_pv_printf(xendev
, 0, "error mapping rings\n");
889 usbback_disconnect(xendev
);
893 urb_sring
= usbif
->urb_sring
;
894 conn_sring
= usbif
->conn_sring
;
895 BACK_RING_INIT(&usbif
->urb_ring
, urb_sring
, XC_PAGE_SIZE
);
896 BACK_RING_INIT(&usbif
->conn_ring
, conn_sring
, XC_PAGE_SIZE
);
898 xen_be_bind_evtchn(xendev
);
900 xen_pv_printf(xendev
, 1, "urb-ring-ref %d, conn-ring-ref %d, "
901 "remote port %d, local port %d\n", urb_ring_ref
,
902 conn_ring_ref
, xendev
->remote_port
, xendev
->local_port
);
904 for (i
= 1; i
<= usbif
->num_ports
; i
++) {
905 if (usbif
->ports
[i
- 1].dev
) {
906 usbback_hotplug_enq(usbif
, i
);
913 static void usbback_backend_changed(struct XenLegacyDevice
*xendev
,
916 struct usbback_info
*usbif
;
919 TR_BUS(xendev
, "path %s\n", node
);
921 usbif
= container_of(xendev
, struct usbback_info
, xendev
);
922 for (i
= 1; i
<= usbif
->num_ports
; i
++) {
923 usbback_process_port(usbif
, i
);
927 static int usbback_init(struct XenLegacyDevice
*xendev
)
929 struct usbback_info
*usbif
;
931 TR_BUS(xendev
, "start\n");
933 usbif
= container_of(xendev
, struct usbback_info
, xendev
);
935 if (xenstore_read_be_int(xendev
, "num-ports", &usbif
->num_ports
) ||
936 usbif
->num_ports
< 1 || usbif
->num_ports
> USBBACK_MAXPORTS
) {
937 xen_pv_printf(xendev
, 0, "num-ports not readable or out of bounds\n");
940 if (xenstore_read_be_int(xendev
, "usb-ver", &usbif
->usb_ver
) ||
941 (usbif
->usb_ver
!= USB_VER_USB11
&& usbif
->usb_ver
!= USB_VER_USB20
)) {
942 xen_pv_printf(xendev
, 0, "usb-ver not readable or out of bounds\n");
946 usbback_backend_changed(xendev
, "port");
948 TR_BUS(xendev
, "finished\n");
953 static void xen_bus_attach(USBPort
*port
)
955 struct usbback_info
*usbif
;
957 usbif
= port
->opaque
;
958 TR_BUS(&usbif
->xendev
, "\n");
959 usbif
->ports
[port
->index
].attached
= true;
960 usbback_hotplug_enq(usbif
, port
->index
+ 1);
963 static void xen_bus_detach(USBPort
*port
)
965 struct usbback_info
*usbif
;
967 usbif
= port
->opaque
;
968 TR_BUS(&usbif
->xendev
, "\n");
969 usbback_portid_detach(usbif
, port
->index
+ 1);
972 static void xen_bus_child_detach(USBPort
*port
, USBDevice
*child
)
974 struct usbback_info
*usbif
;
976 usbif
= port
->opaque
;
977 TR_BUS(&usbif
->xendev
, "\n");
980 static void xen_bus_complete(USBPort
*port
, USBPacket
*packet
)
982 struct usbback_req
*usbback_req
;
983 struct usbback_info
*usbif
;
985 usbback_req
= container_of(packet
, struct usbback_req
, packet
);
986 if (usbback_req
->cancelled
) {
991 usbif
= usbback_req
->usbif
;
992 TR_REQ(&usbif
->xendev
, "\n");
993 usbback_packet_complete(usbback_req
);
996 static USBPortOps xen_usb_port_ops
= {
997 .attach
= xen_bus_attach
,
998 .detach
= xen_bus_detach
,
999 .child_detach
= xen_bus_child_detach
,
1000 .complete
= xen_bus_complete
,
1003 static USBBusOps xen_usb_bus_ops
= {
1006 static void usbback_alloc(struct XenLegacyDevice
*xendev
)
1008 struct usbback_info
*usbif
;
1012 usbif
= container_of(xendev
, struct usbback_info
, xendev
);
1014 usb_bus_new(&usbif
->bus
, sizeof(usbif
->bus
), &xen_usb_bus_ops
,
1015 DEVICE(&xendev
->qdev
));
1016 for (i
= 0; i
< USBBACK_MAXPORTS
; i
++) {
1017 p
= &(usbif
->ports
[i
].port
);
1018 usb_register_port(&usbif
->bus
, p
, usbif
, i
, &xen_usb_port_ops
,
1019 USB_SPEED_MASK_LOW
| USB_SPEED_MASK_FULL
|
1020 USB_SPEED_MASK_HIGH
);
1023 QTAILQ_INIT(&usbif
->req_free_q
);
1024 QSIMPLEQ_INIT(&usbif
->hotplug_q
);
1025 usbif
->bh
= qemu_bh_new(usbback_bh
, usbif
);
1028 static int usbback_free(struct XenLegacyDevice
*xendev
)
1030 struct usbback_info
*usbif
;
1031 struct usbback_req
*usbback_req
;
1032 struct usbback_hotplug
*usb_hp
;
1035 TR_BUS(xendev
, "start\n");
1037 usbback_disconnect(xendev
);
1038 usbif
= container_of(xendev
, struct usbback_info
, xendev
);
1039 for (i
= 1; i
<= usbif
->num_ports
; i
++) {
1040 usbback_portid_remove(usbif
, i
);
1043 while (!QTAILQ_EMPTY(&usbif
->req_free_q
)) {
1044 usbback_req
= QTAILQ_FIRST(&usbif
->req_free_q
);
1045 QTAILQ_REMOVE(&usbif
->req_free_q
, usbback_req
, q
);
1046 g_free(usbback_req
);
1048 while (!QSIMPLEQ_EMPTY(&usbif
->hotplug_q
)) {
1049 usb_hp
= QSIMPLEQ_FIRST(&usbif
->hotplug_q
);
1050 QSIMPLEQ_REMOVE_HEAD(&usbif
->hotplug_q
, q
);
1054 qemu_bh_delete(usbif
->bh
);
1056 for (i
= 0; i
< USBBACK_MAXPORTS
; i
++) {
1057 usb_unregister_port(&usbif
->bus
, &(usbif
->ports
[i
].port
));
1060 usb_bus_release(&usbif
->bus
);
1062 TR_BUS(xendev
, "finished\n");
1067 static void usbback_event(struct XenLegacyDevice
*xendev
)
1069 struct usbback_info
*usbif
;
1071 usbif
= container_of(xendev
, struct usbback_info
, xendev
);
1072 qemu_bh_schedule(usbif
->bh
);
1075 struct XenDevOps xen_usb_ops
= {
1076 .size
= sizeof(struct usbback_info
),
1077 .flags
= DEVOPS_FLAG_NEED_GNTDEV
,
1078 .init
= usbback_init
,
1079 .alloc
= usbback_alloc
,
1080 .free
= usbback_free
,
1081 .backend_changed
= usbback_backend_changed
,
1082 .initialise
= usbback_connect
,
1083 .disconnect
= usbback_disconnect
,
1084 .event
= usbback_event
,
1087 #else /* USBIF_SHORT_NOT_OK */
1089 static int usbback_not_supported(void)
1094 struct XenDevOps xen_usb_ops
= {
1095 .backend_register
= usbback_not_supported
,