hw: ccid-card-emulated: cleanup resource when realize in error path
[qemu/ar7.git] / hw / usb / xen-usb.c
blob5b2e21ed185dccbef9233511ddc5530f520913d8
1 /*
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"
23 #include <libusb.h>
24 #include <sys/user.h>
26 #include "qemu/config-file.h"
27 #include "qemu/option.h"
28 #include "hw/sysbus.h"
29 #include "hw/usb.h"
30 #include "hw/xen/xen_backend.h"
31 #include "monitor/qdev.h"
32 #include "qapi/qmp/qdict.h"
33 #include "qapi/qmp/qstring.h"
35 #include "hw/xen/io/ring.h"
36 #include <xen/io/usbif.h>
39 * Check for required support of usbif.h: USBIF_SHORT_NOT_OK was the last
40 * macro added we rely on.
42 #ifdef USBIF_SHORT_NOT_OK
44 #define TR(xendev, lvl, fmt, args...) \
45 { \
46 struct timeval tv; \
48 gettimeofday(&tv, NULL); \
49 xen_pv_printf(xendev, lvl, "%8ld.%06ld xen-usb(%s):" fmt, \
50 tv.tv_sec, tv.tv_usec, __func__, ##args); \
52 #define TR_BUS(xendev, fmt, args...) TR(xendev, 2, fmt, ##args)
53 #define TR_REQ(xendev, fmt, args...) TR(xendev, 3, fmt, ##args)
55 #define USBBACK_MAXPORTS USBIF_PIPE_PORT_MASK
56 #define USB_DEV_ADDR_SIZE (USBIF_PIPE_DEV_MASK + 1)
58 /* USB wire protocol: structure describing control request parameter. */
59 struct usbif_ctrlrequest {
60 uint8_t bRequestType;
61 uint8_t bRequest;
62 uint16_t wValue;
63 uint16_t wIndex;
64 uint16_t wLength;
67 struct usbback_info;
68 struct usbback_req;
70 struct usbback_stub {
71 USBDevice *dev;
72 USBPort port;
73 unsigned int speed;
74 bool attached;
75 QTAILQ_HEAD(submit_q_head, usbback_req) submit_q;
78 struct usbback_req {
79 struct usbback_info *usbif;
80 struct usbback_stub *stub;
81 struct usbif_urb_request req;
82 USBPacket packet;
84 unsigned int nr_buffer_segs; /* # of transfer_buffer segments */
85 unsigned int nr_extra_segs; /* # of iso_frame_desc segments */
87 QTAILQ_ENTRY(usbback_req) q;
89 void *buffer;
90 void *isoc_buffer;
91 struct libusb_transfer *xfer;
93 bool cancelled;
96 struct usbback_hotplug {
97 QSIMPLEQ_ENTRY(usbback_hotplug) q;
98 unsigned port;
101 struct usbback_info {
102 struct XenDevice xendev; /* must be first */
103 USBBus bus;
104 void *urb_sring;
105 void *conn_sring;
106 struct usbif_urb_back_ring urb_ring;
107 struct usbif_conn_back_ring conn_ring;
108 int num_ports;
109 int usb_ver;
110 bool ring_error;
111 QTAILQ_HEAD(req_free_q_head, usbback_req) req_free_q;
112 QSIMPLEQ_HEAD(hotplug_q_head, usbback_hotplug) hotplug_q;
113 struct usbback_stub ports[USBBACK_MAXPORTS];
114 struct usbback_stub *addr_table[USB_DEV_ADDR_SIZE];
115 QEMUBH *bh;
118 static struct usbback_req *usbback_get_req(struct usbback_info *usbif)
120 struct usbback_req *usbback_req;
122 if (QTAILQ_EMPTY(&usbif->req_free_q)) {
123 usbback_req = g_new0(struct usbback_req, 1);
124 } else {
125 usbback_req = QTAILQ_FIRST(&usbif->req_free_q);
126 QTAILQ_REMOVE(&usbif->req_free_q, usbback_req, q);
128 return usbback_req;
131 static void usbback_put_req(struct usbback_req *usbback_req)
133 struct usbback_info *usbif;
135 usbif = usbback_req->usbif;
136 memset(usbback_req, 0, sizeof(*usbback_req));
137 QTAILQ_INSERT_HEAD(&usbif->req_free_q, usbback_req, q);
140 static int usbback_gnttab_map(struct usbback_req *usbback_req)
142 unsigned int nr_segs, i, prot;
143 uint32_t ref[USBIF_MAX_SEGMENTS_PER_REQUEST];
144 struct usbback_info *usbif = usbback_req->usbif;
145 struct XenDevice *xendev = &usbif->xendev;
146 struct usbif_request_segment *seg;
147 void *addr;
149 nr_segs = usbback_req->nr_buffer_segs + usbback_req->nr_extra_segs;
150 if (!nr_segs) {
151 return 0;
154 if (nr_segs > USBIF_MAX_SEGMENTS_PER_REQUEST) {
155 xen_pv_printf(xendev, 0, "bad number of segments in request (%d)\n",
156 nr_segs);
157 return -EINVAL;
160 for (i = 0; i < nr_segs; i++) {
161 if ((unsigned)usbback_req->req.seg[i].offset +
162 (unsigned)usbback_req->req.seg[i].length > XC_PAGE_SIZE) {
163 xen_pv_printf(xendev, 0, "segment crosses page boundary\n");
164 return -EINVAL;
168 if (usbback_req->nr_buffer_segs) {
169 prot = PROT_READ;
170 if (usbif_pipein(usbback_req->req.pipe)) {
171 prot |= PROT_WRITE;
173 for (i = 0; i < usbback_req->nr_buffer_segs; i++) {
174 ref[i] = usbback_req->req.seg[i].gref;
176 usbback_req->buffer =
177 xen_be_map_grant_refs(xendev, ref, usbback_req->nr_buffer_segs,
178 prot);
180 if (!usbback_req->buffer) {
181 return -ENOMEM;
184 for (i = 0; i < usbback_req->nr_buffer_segs; i++) {
185 seg = usbback_req->req.seg + i;
186 addr = usbback_req->buffer + i * XC_PAGE_SIZE + seg->offset;
187 qemu_iovec_add(&usbback_req->packet.iov, addr, seg->length);
191 if (!usbif_pipeisoc(usbback_req->req.pipe)) {
192 return 0;
196 * Right now isoc requests are not supported.
197 * Prepare supporting those by doing the work needed on the guest
198 * interface side.
201 if (!usbback_req->nr_extra_segs) {
202 xen_pv_printf(xendev, 0, "iso request without descriptor segments\n");
203 return -EINVAL;
206 prot = PROT_READ | PROT_WRITE;
207 for (i = 0; i < usbback_req->nr_extra_segs; i++) {
208 ref[i] = usbback_req->req.seg[i + usbback_req->req.nr_buffer_segs].gref;
210 usbback_req->isoc_buffer =
211 xen_be_map_grant_refs(xendev, ref, usbback_req->nr_extra_segs,
212 prot);
214 if (!usbback_req->isoc_buffer) {
215 return -ENOMEM;
218 return 0;
221 static int usbback_init_packet(struct usbback_req *usbback_req)
223 struct XenDevice *xendev = &usbback_req->usbif->xendev;
224 USBPacket *packet = &usbback_req->packet;
225 USBDevice *dev = usbback_req->stub->dev;
226 USBEndpoint *ep;
227 unsigned int pid, ep_nr;
228 bool sok;
229 int ret = 0;
231 qemu_iovec_init(&packet->iov, USBIF_MAX_SEGMENTS_PER_REQUEST);
232 pid = usbif_pipein(usbback_req->req.pipe) ? USB_TOKEN_IN : USB_TOKEN_OUT;
233 ep_nr = usbif_pipeendpoint(usbback_req->req.pipe);
234 sok = !!(usbback_req->req.transfer_flags & USBIF_SHORT_NOT_OK);
235 if (usbif_pipectrl(usbback_req->req.pipe)) {
236 ep_nr = 0;
237 sok = false;
239 ep = usb_ep_get(dev, pid, ep_nr);
240 usb_packet_setup(packet, pid, ep, 0, 1, sok, true);
242 switch (usbif_pipetype(usbback_req->req.pipe)) {
243 case USBIF_PIPE_TYPE_ISOC:
244 TR_REQ(xendev, "iso transfer %s: buflen: %x, %d frames\n",
245 (pid == USB_TOKEN_IN) ? "in" : "out",
246 usbback_req->req.buffer_length,
247 usbback_req->req.u.isoc.nr_frame_desc_segs);
248 ret = -EINVAL; /* isoc not implemented yet */
249 break;
251 case USBIF_PIPE_TYPE_INT:
252 TR_REQ(xendev, "int transfer %s: buflen: %x\n",
253 (pid == USB_TOKEN_IN) ? "in" : "out",
254 usbback_req->req.buffer_length);
255 break;
257 case USBIF_PIPE_TYPE_CTRL:
258 packet->parameter = *(uint64_t *)usbback_req->req.u.ctrl;
259 TR_REQ(xendev, "ctrl parameter: %"PRIx64", buflen: %x\n",
260 packet->parameter,
261 usbback_req->req.buffer_length);
262 break;
264 case USBIF_PIPE_TYPE_BULK:
265 TR_REQ(xendev, "bulk transfer %s: buflen: %x\n",
266 (pid == USB_TOKEN_IN) ? "in" : "out",
267 usbback_req->req.buffer_length);
268 break;
269 default:
270 ret = -EINVAL;
271 break;
274 return ret;
277 static void usbback_do_response(struct usbback_req *usbback_req, int32_t status,
278 int32_t actual_length, int32_t error_count)
280 struct usbback_info *usbif;
281 struct usbif_urb_response *res;
282 struct XenDevice *xendev;
283 unsigned int notify;
285 usbif = usbback_req->usbif;
286 xendev = &usbif->xendev;
288 TR_REQ(xendev, "id %d, status %d, length %d, errcnt %d\n",
289 usbback_req->req.id, status, actual_length, error_count);
291 if (usbback_req->packet.iov.iov) {
292 qemu_iovec_destroy(&usbback_req->packet.iov);
295 if (usbback_req->buffer) {
296 xen_be_unmap_grant_refs(xendev, usbback_req->buffer,
297 usbback_req->nr_buffer_segs);
298 usbback_req->buffer = NULL;
301 if (usbback_req->isoc_buffer) {
302 xen_be_unmap_grant_refs(xendev, usbback_req->isoc_buffer,
303 usbback_req->nr_extra_segs);
304 usbback_req->isoc_buffer = NULL;
307 if (usbif->urb_sring) {
308 res = RING_GET_RESPONSE(&usbif->urb_ring, usbif->urb_ring.rsp_prod_pvt);
309 res->id = usbback_req->req.id;
310 res->status = status;
311 res->actual_length = actual_length;
312 res->error_count = error_count;
313 res->start_frame = 0;
314 usbif->urb_ring.rsp_prod_pvt++;
315 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&usbif->urb_ring, notify);
317 if (notify) {
318 xen_pv_send_notify(xendev);
322 if (!usbback_req->cancelled)
323 usbback_put_req(usbback_req);
326 static void usbback_do_response_ret(struct usbback_req *usbback_req,
327 int32_t status)
329 usbback_do_response(usbback_req, status, 0, 0);
332 static int32_t usbback_xlat_status(int status)
334 switch (status) {
335 case USB_RET_SUCCESS:
336 return 0;
337 case USB_RET_NODEV:
338 return -ENODEV;
339 case USB_RET_STALL:
340 return -EPIPE;
341 case USB_RET_BABBLE:
342 return -EOVERFLOW;
343 case USB_RET_IOERROR:
344 return -EPROTO;
347 return -ESHUTDOWN;
350 static void usbback_packet_complete(USBPacket *packet)
352 struct usbback_req *usbback_req;
353 int32_t status;
355 usbback_req = container_of(packet, struct usbback_req, packet);
357 QTAILQ_REMOVE(&usbback_req->stub->submit_q, usbback_req, q);
359 status = usbback_xlat_status(packet->status);
360 usbback_do_response(usbback_req, status, packet->actual_length, 0);
363 static void usbback_set_address(struct usbback_info *usbif,
364 struct usbback_stub *stub,
365 unsigned int cur_addr, unsigned int new_addr)
367 if (cur_addr) {
368 usbif->addr_table[cur_addr] = NULL;
370 if (new_addr) {
371 usbif->addr_table[new_addr] = stub;
375 static void usbback_cancel_req(struct usbback_req *usbback_req)
377 if (usb_packet_is_inflight(&usbback_req->packet)) {
378 usb_cancel_packet(&usbback_req->packet);
379 QTAILQ_REMOVE(&usbback_req->stub->submit_q, usbback_req, q);
380 usbback_req->cancelled = true;
381 usbback_do_response_ret(usbback_req, -EPROTO);
385 static void usbback_process_unlink_req(struct usbback_req *usbback_req)
387 struct usbback_info *usbif;
388 struct usbback_req *unlink_req;
389 unsigned int id, devnum;
390 int ret;
392 usbif = usbback_req->usbif;
393 ret = 0;
394 id = usbback_req->req.u.unlink.unlink_id;
395 TR_REQ(&usbif->xendev, "unlink id %d\n", id);
396 devnum = usbif_pipedevice(usbback_req->req.pipe);
397 if (unlikely(devnum == 0)) {
398 usbback_req->stub = usbif->ports +
399 usbif_pipeportnum(usbback_req->req.pipe) - 1;
400 if (unlikely(!usbback_req->stub)) {
401 ret = -ENODEV;
402 goto fail_response;
404 } else {
405 if (unlikely(!usbif->addr_table[devnum])) {
406 ret = -ENODEV;
407 goto fail_response;
409 usbback_req->stub = usbif->addr_table[devnum];
412 QTAILQ_FOREACH(unlink_req, &usbback_req->stub->submit_q, q) {
413 if (unlink_req->req.id == id) {
414 usbback_cancel_req(unlink_req);
415 break;
419 fail_response:
420 usbback_do_response_ret(usbback_req, ret);
424 * Checks whether a request can be handled at once or should be forwarded
425 * to the usb framework.
426 * Return value is:
427 * 0 in case of usb framework is needed
428 * 1 in case of local handling (no error)
429 * The request response has been queued already if return value not 0.
431 static int usbback_check_and_submit(struct usbback_req *usbback_req)
433 struct usbback_info *usbif;
434 unsigned int devnum;
435 struct usbback_stub *stub;
436 struct usbif_ctrlrequest *ctrl;
437 int ret;
438 uint16_t wValue;
440 usbif = usbback_req->usbif;
441 stub = NULL;
442 devnum = usbif_pipedevice(usbback_req->req.pipe);
443 ctrl = (struct usbif_ctrlrequest *)usbback_req->req.u.ctrl;
444 wValue = le16_to_cpu(ctrl->wValue);
447 * When the device is first connected or resetted, USB device has no
448 * address. In this initial state, following requests are sent to device
449 * address (#0),
451 * 1. GET_DESCRIPTOR (with Descriptor Type is "DEVICE") is sent,
452 * and OS knows what device is connected to.
454 * 2. SET_ADDRESS is sent, and then device has its address.
456 * In the next step, SET_CONFIGURATION is sent to addressed device, and
457 * then the device is finally ready to use.
459 if (unlikely(devnum == 0)) {
460 stub = usbif->ports + usbif_pipeportnum(usbback_req->req.pipe) - 1;
461 if (!stub->dev || !stub->attached) {
462 ret = -ENODEV;
463 goto do_response;
466 switch (ctrl->bRequest) {
467 case USB_REQ_GET_DESCRIPTOR:
469 * GET_DESCRIPTOR request to device #0.
470 * through normal transfer.
472 TR_REQ(&usbif->xendev, "devnum 0 GET_DESCRIPTOR\n");
473 usbback_req->stub = stub;
474 return 0;
475 case USB_REQ_SET_ADDRESS:
477 * SET_ADDRESS request to device #0.
478 * add attached device to addr_table.
480 TR_REQ(&usbif->xendev, "devnum 0 SET_ADDRESS\n");
481 usbback_set_address(usbif, stub, 0, wValue);
482 ret = 0;
483 break;
484 default:
485 ret = -EINVAL;
486 break;
488 goto do_response;
491 if (unlikely(!usbif->addr_table[devnum])) {
492 ret = -ENODEV;
493 goto do_response;
495 usbback_req->stub = usbif->addr_table[devnum];
498 * Check special request
500 if (ctrl->bRequest != USB_REQ_SET_ADDRESS) {
501 return 0;
505 * SET_ADDRESS request to addressed device.
506 * change addr or remove from addr_table.
508 usbback_set_address(usbif, usbback_req->stub, devnum, wValue);
509 ret = 0;
511 do_response:
512 usbback_do_response_ret(usbback_req, ret);
513 return 1;
516 static void usbback_dispatch(struct usbback_req *usbback_req)
518 int ret;
519 unsigned int devnum;
520 struct usbback_info *usbif;
522 usbif = usbback_req->usbif;
524 TR_REQ(&usbif->xendev, "start req_id %d pipe %08x\n", usbback_req->req.id,
525 usbback_req->req.pipe);
527 /* unlink request */
528 if (unlikely(usbif_pipeunlink(usbback_req->req.pipe))) {
529 usbback_process_unlink_req(usbback_req);
530 return;
533 if (usbif_pipectrl(usbback_req->req.pipe)) {
534 if (usbback_check_and_submit(usbback_req)) {
535 return;
537 } else {
538 devnum = usbif_pipedevice(usbback_req->req.pipe);
539 usbback_req->stub = usbif->addr_table[devnum];
541 if (!usbback_req->stub || !usbback_req->stub->attached) {
542 ret = -ENODEV;
543 goto fail_response;
547 QTAILQ_INSERT_TAIL(&usbback_req->stub->submit_q, usbback_req, q);
549 usbback_req->nr_buffer_segs = usbback_req->req.nr_buffer_segs;
550 usbback_req->nr_extra_segs = usbif_pipeisoc(usbback_req->req.pipe) ?
551 usbback_req->req.u.isoc.nr_frame_desc_segs : 0;
553 ret = usbback_init_packet(usbback_req);
554 if (ret) {
555 xen_pv_printf(&usbif->xendev, 0, "invalid request\n");
556 ret = -ESHUTDOWN;
557 goto fail_free_urb;
560 ret = usbback_gnttab_map(usbback_req);
561 if (ret) {
562 xen_pv_printf(&usbif->xendev, 0, "invalid buffer, ret=%d\n", ret);
563 ret = -ESHUTDOWN;
564 goto fail_free_urb;
567 usb_handle_packet(usbback_req->stub->dev, &usbback_req->packet);
568 if (usbback_req->packet.status != USB_RET_ASYNC) {
569 usbback_packet_complete(&usbback_req->packet);
571 return;
573 fail_free_urb:
574 QTAILQ_REMOVE(&usbback_req->stub->submit_q, usbback_req, q);
576 fail_response:
577 usbback_do_response_ret(usbback_req, ret);
580 static void usbback_hotplug_notify(struct usbback_info *usbif)
582 struct usbif_conn_back_ring *ring = &usbif->conn_ring;
583 struct usbif_conn_request req;
584 struct usbif_conn_response *res;
585 struct usbback_hotplug *usb_hp;
586 unsigned int notify;
588 if (!usbif->conn_sring) {
589 return;
592 /* Check for full ring. */
593 if ((RING_SIZE(ring) - ring->rsp_prod_pvt - ring->req_cons) == 0) {
594 xen_pv_send_notify(&usbif->xendev);
595 return;
598 usb_hp = QSIMPLEQ_FIRST(&usbif->hotplug_q);
599 QSIMPLEQ_REMOVE_HEAD(&usbif->hotplug_q, q);
601 RING_COPY_REQUEST(ring, ring->req_cons, &req);
602 ring->req_cons++;
603 ring->sring->req_event = ring->req_cons + 1;
605 res = RING_GET_RESPONSE(ring, ring->rsp_prod_pvt);
606 res->id = req.id;
607 res->portnum = usb_hp->port;
608 res->speed = usbif->ports[usb_hp->port - 1].speed;
609 ring->rsp_prod_pvt++;
610 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(ring, notify);
612 if (notify) {
613 xen_pv_send_notify(&usbif->xendev);
616 TR_BUS(&usbif->xendev, "hotplug port %d speed %d\n", usb_hp->port,
617 res->speed);
619 g_free(usb_hp);
621 if (!QSIMPLEQ_EMPTY(&usbif->hotplug_q)) {
622 qemu_bh_schedule(usbif->bh);
626 static void usbback_bh(void *opaque)
628 struct usbback_info *usbif;
629 struct usbif_urb_back_ring *urb_ring;
630 struct usbback_req *usbback_req;
631 RING_IDX rc, rp;
632 unsigned int more_to_do;
634 usbif = opaque;
635 if (usbif->ring_error) {
636 return;
639 if (!QSIMPLEQ_EMPTY(&usbif->hotplug_q)) {
640 usbback_hotplug_notify(usbif);
643 urb_ring = &usbif->urb_ring;
644 rc = urb_ring->req_cons;
645 rp = urb_ring->sring->req_prod;
646 xen_rmb(); /* Ensure we see queued requests up to 'rp'. */
648 if (RING_REQUEST_PROD_OVERFLOW(urb_ring, rp)) {
649 rc = urb_ring->rsp_prod_pvt;
650 xen_pv_printf(&usbif->xendev, 0, "domU provided bogus ring requests "
651 "(%#x - %#x = %u). Halting ring processing.\n",
652 rp, rc, rp - rc);
653 usbif->ring_error = true;
654 return;
657 while (rc != rp) {
658 if (RING_REQUEST_CONS_OVERFLOW(urb_ring, rc)) {
659 break;
661 usbback_req = usbback_get_req(usbif);
663 RING_COPY_REQUEST(urb_ring, rc, &usbback_req->req);
664 usbback_req->usbif = usbif;
666 usbback_dispatch(usbback_req);
668 urb_ring->req_cons = ++rc;
671 RING_FINAL_CHECK_FOR_REQUESTS(urb_ring, more_to_do);
672 if (more_to_do) {
673 qemu_bh_schedule(usbif->bh);
677 static void usbback_hotplug_enq(struct usbback_info *usbif, unsigned port)
679 struct usbback_hotplug *usb_hp;
681 usb_hp = g_new0(struct usbback_hotplug, 1);
682 usb_hp->port = port;
683 QSIMPLEQ_INSERT_TAIL(&usbif->hotplug_q, usb_hp, q);
684 usbback_hotplug_notify(usbif);
687 static void usbback_portid_drain(struct usbback_info *usbif, unsigned port)
689 struct usbback_req *req, *tmp;
690 bool sched = false;
692 QTAILQ_FOREACH_SAFE(req, &usbif->ports[port - 1].submit_q, q, tmp) {
693 usbback_cancel_req(req);
694 sched = true;
697 if (sched) {
698 qemu_bh_schedule(usbif->bh);
702 static void usbback_portid_detach(struct usbback_info *usbif, unsigned port)
704 if (!usbif->ports[port - 1].attached) {
705 return;
708 usbif->ports[port - 1].speed = USBIF_SPEED_NONE;
709 usbif->ports[port - 1].attached = false;
710 usbback_portid_drain(usbif, port);
711 usbback_hotplug_enq(usbif, port);
714 static void usbback_portid_remove(struct usbback_info *usbif, unsigned port)
716 if (!usbif->ports[port - 1].dev) {
717 return;
720 object_unparent(OBJECT(usbif->ports[port - 1].dev));
721 usbif->ports[port - 1].dev = NULL;
722 usbback_portid_detach(usbif, port);
724 TR_BUS(&usbif->xendev, "port %d removed\n", port);
727 static void usbback_portid_add(struct usbback_info *usbif, unsigned port,
728 char *busid)
730 unsigned speed;
731 char *portname;
732 Error *local_err = NULL;
733 QDict *qdict;
734 QemuOpts *opts;
735 char *tmp;
737 if (usbif->ports[port - 1].dev) {
738 return;
741 portname = strchr(busid, '-');
742 if (!portname) {
743 xen_pv_printf(&usbif->xendev, 0, "device %s illegal specification\n",
744 busid);
745 return;
747 portname++;
749 qdict = qdict_new();
750 qdict_put_str(qdict, "driver", "usb-host");
751 tmp = g_strdup_printf("%s.0", usbif->xendev.qdev.id);
752 qdict_put_str(qdict, "bus", tmp);
753 g_free(tmp);
754 tmp = g_strdup_printf("%s-%u", usbif->xendev.qdev.id, port);
755 qdict_put_str(qdict, "id", tmp);
756 g_free(tmp);
757 qdict_put_int(qdict, "port", port);
758 qdict_put_int(qdict, "hostbus", atoi(busid));
759 qdict_put_str(qdict, "hostport", portname);
760 opts = qemu_opts_from_qdict(qemu_find_opts("device"), qdict, &local_err);
761 if (local_err) {
762 goto err;
764 usbif->ports[port - 1].dev = USB_DEVICE(qdev_device_add(opts, &local_err));
765 if (!usbif->ports[port - 1].dev) {
766 goto err;
768 qobject_unref(qdict);
769 speed = usbif->ports[port - 1].dev->speed;
770 switch (speed) {
771 case USB_SPEED_LOW:
772 speed = USBIF_SPEED_LOW;
773 break;
774 case USB_SPEED_FULL:
775 speed = USBIF_SPEED_FULL;
776 break;
777 case USB_SPEED_HIGH:
778 speed = (usbif->usb_ver < USB_VER_USB20) ?
779 USBIF_SPEED_NONE : USBIF_SPEED_HIGH;
780 break;
781 default:
782 speed = USBIF_SPEED_NONE;
783 break;
785 if (speed == USBIF_SPEED_NONE) {
786 xen_pv_printf(&usbif->xendev, 0, "device %s wrong speed\n", busid);
787 object_unparent(OBJECT(usbif->ports[port - 1].dev));
788 usbif->ports[port - 1].dev = NULL;
789 return;
791 usb_device_reset(usbif->ports[port - 1].dev);
792 usbif->ports[port - 1].speed = speed;
793 usbif->ports[port - 1].attached = true;
794 QTAILQ_INIT(&usbif->ports[port - 1].submit_q);
795 usbback_hotplug_enq(usbif, port);
797 TR_BUS(&usbif->xendev, "port %d attached\n", port);
798 return;
800 err:
801 qobject_unref(qdict);
802 xen_pv_printf(&usbif->xendev, 0, "device %s could not be opened\n", busid);
805 static void usbback_process_port(struct usbback_info *usbif, unsigned port)
807 char node[8];
808 char *busid;
810 snprintf(node, sizeof(node), "port/%d", port);
811 busid = xenstore_read_be_str(&usbif->xendev, node);
812 if (busid == NULL) {
813 xen_pv_printf(&usbif->xendev, 0, "xenstore_read %s failed\n", node);
814 return;
817 /* Remove portid, if the port is not connected. */
818 if (strlen(busid) == 0) {
819 usbback_portid_remove(usbif, port);
820 } else {
821 usbback_portid_add(usbif, port, busid);
824 g_free(busid);
827 static void usbback_disconnect(struct XenDevice *xendev)
829 struct usbback_info *usbif;
830 unsigned int i;
832 TR_BUS(xendev, "start\n");
834 usbif = container_of(xendev, struct usbback_info, xendev);
836 xen_pv_unbind_evtchn(xendev);
838 if (usbif->urb_sring) {
839 xen_be_unmap_grant_ref(xendev, usbif->urb_sring);
840 usbif->urb_sring = NULL;
842 if (usbif->conn_sring) {
843 xen_be_unmap_grant_ref(xendev, usbif->conn_sring);
844 usbif->conn_sring = NULL;
847 for (i = 0; i < usbif->num_ports; i++) {
848 if (usbif->ports[i].dev) {
849 usbback_portid_drain(usbif, i + 1);
853 TR_BUS(xendev, "finished\n");
856 static int usbback_connect(struct XenDevice *xendev)
858 struct usbback_info *usbif;
859 struct usbif_urb_sring *urb_sring;
860 struct usbif_conn_sring *conn_sring;
861 int urb_ring_ref;
862 int conn_ring_ref;
863 unsigned int i;
865 TR_BUS(xendev, "start\n");
867 usbif = container_of(xendev, struct usbback_info, xendev);
869 if (xenstore_read_fe_int(xendev, "urb-ring-ref", &urb_ring_ref)) {
870 xen_pv_printf(xendev, 0, "error reading urb-ring-ref\n");
871 return -1;
873 if (xenstore_read_fe_int(xendev, "conn-ring-ref", &conn_ring_ref)) {
874 xen_pv_printf(xendev, 0, "error reading conn-ring-ref\n");
875 return -1;
877 if (xenstore_read_fe_int(xendev, "event-channel", &xendev->remote_port)) {
878 xen_pv_printf(xendev, 0, "error reading event-channel\n");
879 return -1;
882 usbif->urb_sring = xen_be_map_grant_ref(xendev, urb_ring_ref,
883 PROT_READ | PROT_WRITE);
884 usbif->conn_sring = xen_be_map_grant_ref(xendev, conn_ring_ref,
885 PROT_READ | PROT_WRITE);
886 if (!usbif->urb_sring || !usbif->conn_sring) {
887 xen_pv_printf(xendev, 0, "error mapping rings\n");
888 usbback_disconnect(xendev);
889 return -1;
892 urb_sring = usbif->urb_sring;
893 conn_sring = usbif->conn_sring;
894 BACK_RING_INIT(&usbif->urb_ring, urb_sring, XC_PAGE_SIZE);
895 BACK_RING_INIT(&usbif->conn_ring, conn_sring, XC_PAGE_SIZE);
897 xen_be_bind_evtchn(xendev);
899 xen_pv_printf(xendev, 1, "urb-ring-ref %d, conn-ring-ref %d, "
900 "remote port %d, local port %d\n", urb_ring_ref,
901 conn_ring_ref, xendev->remote_port, xendev->local_port);
903 for (i = 1; i <= usbif->num_ports; i++) {
904 if (usbif->ports[i - 1].dev) {
905 usbback_hotplug_enq(usbif, i);
909 return 0;
912 static void usbback_backend_changed(struct XenDevice *xendev, const char *node)
914 struct usbback_info *usbif;
915 unsigned int i;
917 TR_BUS(xendev, "path %s\n", node);
919 usbif = container_of(xendev, struct usbback_info, xendev);
920 for (i = 1; i <= usbif->num_ports; i++) {
921 usbback_process_port(usbif, i);
925 static int usbback_init(struct XenDevice *xendev)
927 struct usbback_info *usbif;
929 TR_BUS(xendev, "start\n");
931 usbif = container_of(xendev, struct usbback_info, xendev);
933 if (xenstore_read_be_int(xendev, "num-ports", &usbif->num_ports) ||
934 usbif->num_ports < 1 || usbif->num_ports > USBBACK_MAXPORTS) {
935 xen_pv_printf(xendev, 0, "num-ports not readable or out of bounds\n");
936 return -1;
938 if (xenstore_read_be_int(xendev, "usb-ver", &usbif->usb_ver) ||
939 (usbif->usb_ver != USB_VER_USB11 && usbif->usb_ver != USB_VER_USB20)) {
940 xen_pv_printf(xendev, 0, "usb-ver not readable or out of bounds\n");
941 return -1;
944 usbback_backend_changed(xendev, "port");
946 TR_BUS(xendev, "finished\n");
948 return 0;
951 static void xen_bus_attach(USBPort *port)
953 struct usbback_info *usbif;
955 usbif = port->opaque;
956 TR_BUS(&usbif->xendev, "\n");
957 usbif->ports[port->index].attached = true;
958 usbback_hotplug_enq(usbif, port->index + 1);
961 static void xen_bus_detach(USBPort *port)
963 struct usbback_info *usbif;
965 usbif = port->opaque;
966 TR_BUS(&usbif->xendev, "\n");
967 usbback_portid_detach(usbif, port->index + 1);
970 static void xen_bus_child_detach(USBPort *port, USBDevice *child)
972 struct usbback_info *usbif;
974 usbif = port->opaque;
975 TR_BUS(&usbif->xendev, "\n");
978 static void xen_bus_complete(USBPort *port, USBPacket *packet)
980 struct usbback_req *usbback_req;
981 struct usbback_info *usbif;
983 usbback_req = container_of(packet, struct usbback_req, packet);
984 if (usbback_req->cancelled) {
985 g_free(usbback_req);
986 return;
989 usbif = usbback_req->usbif;
990 TR_REQ(&usbif->xendev, "\n");
991 usbback_packet_complete(packet);
994 static USBPortOps xen_usb_port_ops = {
995 .attach = xen_bus_attach,
996 .detach = xen_bus_detach,
997 .child_detach = xen_bus_child_detach,
998 .complete = xen_bus_complete,
1001 static USBBusOps xen_usb_bus_ops = {
1004 static void usbback_alloc(struct XenDevice *xendev)
1006 struct usbback_info *usbif;
1007 USBPort *p;
1008 unsigned int i, max_grants;
1010 usbif = container_of(xendev, struct usbback_info, xendev);
1012 usb_bus_new(&usbif->bus, sizeof(usbif->bus), &xen_usb_bus_ops,
1013 DEVICE(&xendev->qdev));
1014 for (i = 0; i < USBBACK_MAXPORTS; i++) {
1015 p = &(usbif->ports[i].port);
1016 usb_register_port(&usbif->bus, p, usbif, i, &xen_usb_port_ops,
1017 USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL |
1018 USB_SPEED_MASK_HIGH);
1021 QTAILQ_INIT(&usbif->req_free_q);
1022 QSIMPLEQ_INIT(&usbif->hotplug_q);
1023 usbif->bh = qemu_bh_new(usbback_bh, usbif);
1025 /* max_grants: for each request and for the rings (request and connect). */
1026 max_grants = USBIF_MAX_SEGMENTS_PER_REQUEST * USB_URB_RING_SIZE + 2;
1027 xen_be_set_max_grant_refs(xendev, max_grants);
1030 static int usbback_free(struct XenDevice *xendev)
1032 struct usbback_info *usbif;
1033 struct usbback_req *usbback_req;
1034 struct usbback_hotplug *usb_hp;
1035 unsigned int i;
1037 TR_BUS(xendev, "start\n");
1039 usbback_disconnect(xendev);
1040 usbif = container_of(xendev, struct usbback_info, xendev);
1041 for (i = 1; i <= usbif->num_ports; i++) {
1042 usbback_portid_remove(usbif, i);
1045 while (!QTAILQ_EMPTY(&usbif->req_free_q)) {
1046 usbback_req = QTAILQ_FIRST(&usbif->req_free_q);
1047 QTAILQ_REMOVE(&usbif->req_free_q, usbback_req, q);
1048 g_free(usbback_req);
1050 while (!QSIMPLEQ_EMPTY(&usbif->hotplug_q)) {
1051 usb_hp = QSIMPLEQ_FIRST(&usbif->hotplug_q);
1052 QSIMPLEQ_REMOVE_HEAD(&usbif->hotplug_q, q);
1053 g_free(usb_hp);
1056 qemu_bh_delete(usbif->bh);
1058 for (i = 0; i < USBBACK_MAXPORTS; i++) {
1059 usb_unregister_port(&usbif->bus, &(usbif->ports[i].port));
1062 usb_bus_release(&usbif->bus);
1064 TR_BUS(xendev, "finished\n");
1066 return 0;
1069 static void usbback_event(struct XenDevice *xendev)
1071 struct usbback_info *usbif;
1073 usbif = container_of(xendev, struct usbback_info, xendev);
1074 qemu_bh_schedule(usbif->bh);
1077 struct XenDevOps xen_usb_ops = {
1078 .size = sizeof(struct usbback_info),
1079 .flags = DEVOPS_FLAG_NEED_GNTDEV,
1080 .init = usbback_init,
1081 .alloc = usbback_alloc,
1082 .free = usbback_free,
1083 .backend_changed = usbback_backend_changed,
1084 .initialise = usbback_connect,
1085 .disconnect = usbback_disconnect,
1086 .event = usbback_event,
1089 #else /* USBIF_SHORT_NOT_OK */
1091 static int usbback_not_supported(void)
1093 return -EINVAL;
1096 struct XenDevOps xen_usb_ops = {
1097 .backend_register = usbback_not_supported,
1100 #endif