1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Xen USB Virtual Host Controller driver
7 * Copyright (C) 2009, FUJITSU LABORATORIES LTD.
11 #include <linux/module.h>
12 #include <linux/usb.h>
13 #include <linux/list.h>
14 #include <linux/usb/hcd.h>
18 #include <xen/xenbus.h>
19 #include <xen/grant_table.h>
20 #include <xen/events.h>
23 #include <xen/interface/io/usbif.h>
25 /* Private per-URB data */
27 struct list_head list;
29 int req_id; /* RING_REQUEST id for submitting */
30 int unlink_req_id; /* RING_REQUEST id for unlinking */
32 bool unlinked; /* dequeued marker */
35 /* virtual roothub port status */
36 struct rhport_status {
38 bool resuming; /* in resuming */
39 bool c_connection; /* connection changed */
40 unsigned long timeout;
43 /* status of attached device */
44 struct vdevice_status {
46 enum usb_device_state status;
47 enum usb_device_speed speed;
50 /* RING request shadow */
52 struct xenusb_urb_request req;
57 /* Virtual Host Controller has 4 urb queues */
58 struct list_head pending_submit_list;
59 struct list_head pending_unlink_list;
60 struct list_head in_progress_list;
61 struct list_head giveback_waiting_list;
65 /* timer that kick pending and giveback waiting urbs */
66 struct timer_list watchdog;
67 unsigned long actions;
69 /* virtual root hub */
71 struct rhport_status ports[XENUSB_MAX_PORTNR];
72 struct vdevice_status devices[XENUSB_MAX_PORTNR];
74 /* Xen related staff */
75 struct xenbus_device *xbdev;
78 struct xenusb_urb_front_ring urb_ring;
79 struct xenusb_conn_front_ring conn_ring;
83 struct usb_shadow shadow[XENUSB_URB_RING_SIZE];
84 unsigned int shadow_free;
89 #define GRANT_INVALID_REF 0
91 #define XENHCD_RING_JIFFIES (HZ/200)
92 #define XENHCD_SCAN_JIFFIES 1
94 enum xenhcd_timer_action {
96 TIMER_SCAN_PENDING_URBS,
99 static struct kmem_cache *xenhcd_urbp_cachep;
101 static inline struct xenhcd_info *xenhcd_hcd_to_info(struct usb_hcd *hcd)
103 return (struct xenhcd_info *)hcd->hcd_priv;
106 static inline struct usb_hcd *xenhcd_info_to_hcd(struct xenhcd_info *info)
108 return container_of((void *)info, struct usb_hcd, hcd_priv);
111 static void xenhcd_set_error(struct xenhcd_info *info, const char *msg)
115 pr_alert("xen-hcd: protocol error: %s!\n", msg);
118 static inline void xenhcd_timer_action_done(struct xenhcd_info *info,
119 enum xenhcd_timer_action action)
121 clear_bit(action, &info->actions);
124 static void xenhcd_timer_action(struct xenhcd_info *info,
125 enum xenhcd_timer_action action)
127 if (timer_pending(&info->watchdog) &&
128 test_bit(TIMER_SCAN_PENDING_URBS, &info->actions))
131 if (!test_and_set_bit(action, &info->actions)) {
135 case TIMER_RING_WATCHDOG:
136 t = XENHCD_RING_JIFFIES;
139 t = XENHCD_SCAN_JIFFIES;
142 mod_timer(&info->watchdog, t + jiffies);
147 * set virtual port connection status
149 static void xenhcd_set_connect_state(struct xenhcd_info *info, int portnum)
154 if (info->ports[port].status & USB_PORT_STAT_POWER) {
155 switch (info->devices[port].speed) {
156 case XENUSB_SPEED_NONE:
157 info->ports[port].status &=
158 ~(USB_PORT_STAT_CONNECTION |
159 USB_PORT_STAT_ENABLE |
160 USB_PORT_STAT_LOW_SPEED |
161 USB_PORT_STAT_HIGH_SPEED |
162 USB_PORT_STAT_SUSPEND);
164 case XENUSB_SPEED_LOW:
165 info->ports[port].status |= USB_PORT_STAT_CONNECTION;
166 info->ports[port].status |= USB_PORT_STAT_LOW_SPEED;
168 case XENUSB_SPEED_FULL:
169 info->ports[port].status |= USB_PORT_STAT_CONNECTION;
171 case XENUSB_SPEED_HIGH:
172 info->ports[port].status |= USB_PORT_STAT_CONNECTION;
173 info->ports[port].status |= USB_PORT_STAT_HIGH_SPEED;
178 info->ports[port].status |= (USB_PORT_STAT_C_CONNECTION << 16);
183 * set virtual device connection status
185 static int xenhcd_rhport_connect(struct xenhcd_info *info, __u8 portnum,
190 if (portnum < 1 || portnum > info->rh_numports)
191 return -EINVAL; /* invalid port number */
194 if (info->devices[port].speed != speed) {
196 case XENUSB_SPEED_NONE: /* disconnect */
197 info->devices[port].status = USB_STATE_NOTATTACHED;
199 case XENUSB_SPEED_LOW:
200 case XENUSB_SPEED_FULL:
201 case XENUSB_SPEED_HIGH:
202 info->devices[port].status = USB_STATE_ATTACHED;
207 info->devices[port].speed = speed;
208 info->ports[port].c_connection = true;
210 xenhcd_set_connect_state(info, portnum);
217 * SetPortFeature(PORT_SUSPENDED)
219 static void xenhcd_rhport_suspend(struct xenhcd_info *info, int portnum)
224 info->ports[port].status |= USB_PORT_STAT_SUSPEND;
225 info->devices[port].status = USB_STATE_SUSPENDED;
229 * ClearPortFeature(PORT_SUSPENDED)
231 static void xenhcd_rhport_resume(struct xenhcd_info *info, int portnum)
236 if (info->ports[port].status & USB_PORT_STAT_SUSPEND) {
237 info->ports[port].resuming = true;
238 info->ports[port].timeout = jiffies + msecs_to_jiffies(20);
243 * SetPortFeature(PORT_POWER)
245 static void xenhcd_rhport_power_on(struct xenhcd_info *info, int portnum)
250 if ((info->ports[port].status & USB_PORT_STAT_POWER) == 0) {
251 info->ports[port].status |= USB_PORT_STAT_POWER;
252 if (info->devices[port].status != USB_STATE_NOTATTACHED)
253 info->devices[port].status = USB_STATE_POWERED;
254 if (info->ports[port].c_connection)
255 xenhcd_set_connect_state(info, portnum);
260 * ClearPortFeature(PORT_POWER)
261 * SetConfiguration(non-zero)
265 static void xenhcd_rhport_power_off(struct xenhcd_info *info, int portnum)
270 if (info->ports[port].status & USB_PORT_STAT_POWER) {
271 info->ports[port].status = 0;
272 if (info->devices[port].status != USB_STATE_NOTATTACHED)
273 info->devices[port].status = USB_STATE_ATTACHED;
278 * ClearPortFeature(PORT_ENABLE)
280 static void xenhcd_rhport_disable(struct xenhcd_info *info, int portnum)
285 info->ports[port].status &= ~USB_PORT_STAT_ENABLE;
286 info->ports[port].status &= ~USB_PORT_STAT_SUSPEND;
287 info->ports[port].resuming = false;
288 if (info->devices[port].status != USB_STATE_NOTATTACHED)
289 info->devices[port].status = USB_STATE_POWERED;
293 * SetPortFeature(PORT_RESET)
295 static void xenhcd_rhport_reset(struct xenhcd_info *info, int portnum)
300 info->ports[port].status &= ~(USB_PORT_STAT_ENABLE |
301 USB_PORT_STAT_LOW_SPEED |
302 USB_PORT_STAT_HIGH_SPEED);
303 info->ports[port].status |= USB_PORT_STAT_RESET;
305 if (info->devices[port].status != USB_STATE_NOTATTACHED)
306 info->devices[port].status = USB_STATE_ATTACHED;
308 /* 10msec reset signaling */
309 info->ports[port].timeout = jiffies + msecs_to_jiffies(10);
313 static int xenhcd_bus_suspend(struct usb_hcd *hcd)
315 struct xenhcd_info *info = xenhcd_hcd_to_info(hcd);
319 ports = info->rh_numports;
321 spin_lock_irq(&info->lock);
322 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
325 /* suspend any active ports*/
326 for (i = 1; i <= ports; i++)
327 xenhcd_rhport_suspend(info, i);
329 spin_unlock_irq(&info->lock);
331 del_timer_sync(&info->watchdog);
336 static int xenhcd_bus_resume(struct usb_hcd *hcd)
338 struct xenhcd_info *info = xenhcd_hcd_to_info(hcd);
342 ports = info->rh_numports;
344 spin_lock_irq(&info->lock);
345 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
348 /* resume any suspended ports*/
349 for (i = 1; i <= ports; i++)
350 xenhcd_rhport_resume(info, i);
352 spin_unlock_irq(&info->lock);
358 static void xenhcd_hub_descriptor(struct xenhcd_info *info,
359 struct usb_hub_descriptor *desc)
362 int ports = info->rh_numports;
364 desc->bDescriptorType = 0x29;
365 desc->bPwrOn2PwrGood = 10; /* EHCI says 20ms max */
366 desc->bHubContrCurrent = 0;
367 desc->bNbrPorts = ports;
369 /* size of DeviceRemovable and PortPwrCtrlMask fields */
370 temp = 1 + (ports / 8);
371 desc->bDescLength = 7 + 2 * temp;
373 /* bitmaps for DeviceRemovable and PortPwrCtrlMask */
374 memset(&desc->u.hs.DeviceRemovable[0], 0, temp);
375 memset(&desc->u.hs.DeviceRemovable[temp], 0xff, temp);
377 /* per-port over current reporting and no power switching */
379 desc->wHubCharacteristics = cpu_to_le16(temp);
382 /* port status change mask for hub_status_data */
383 #define PORT_C_MASK ((USB_PORT_STAT_C_CONNECTION | \
384 USB_PORT_STAT_C_ENABLE | \
385 USB_PORT_STAT_C_SUSPEND | \
386 USB_PORT_STAT_C_OVERCURRENT | \
387 USB_PORT_STAT_C_RESET) << 16)
390 * See USB 2.0 Spec, 11.12.4 Hub and Port Status Change Bitmap.
391 * If port status changed, writes the bitmap to buf and return
392 * that length(number of bytes).
393 * If Nothing changed, return 0.
395 static int xenhcd_hub_status_data(struct usb_hcd *hcd, char *buf)
397 struct xenhcd_info *info = xenhcd_hcd_to_info(hcd);
404 /* initialize the status to no-changes */
405 ports = info->rh_numports;
406 ret = 1 + (ports / 8);
409 spin_lock_irqsave(&info->lock, flags);
411 for (i = 0; i < ports; i++) {
412 /* check status for each port */
413 if (info->ports[i].status & PORT_C_MASK) {
414 buf[(i + 1) / 8] |= 1 << (i + 1) % 8;
419 if ((hcd->state == HC_STATE_SUSPENDED) && (changed == 1))
420 usb_hcd_resume_root_hub(hcd);
422 spin_unlock_irqrestore(&info->lock, flags);
424 return changed ? ret : 0;
427 static int xenhcd_hub_control(struct usb_hcd *hcd, __u16 typeReq, __u16 wValue,
428 __u16 wIndex, char *buf, __u16 wLength)
430 struct xenhcd_info *info = xenhcd_hcd_to_info(hcd);
431 int ports = info->rh_numports;
437 spin_lock_irqsave(&info->lock, flags);
439 case ClearHubFeature:
440 /* ignore this request */
442 case ClearPortFeature:
443 if (!wIndex || wIndex > ports)
447 case USB_PORT_FEAT_SUSPEND:
448 xenhcd_rhport_resume(info, wIndex);
450 case USB_PORT_FEAT_POWER:
451 xenhcd_rhport_power_off(info, wIndex);
453 case USB_PORT_FEAT_ENABLE:
454 xenhcd_rhport_disable(info, wIndex);
456 case USB_PORT_FEAT_C_CONNECTION:
457 info->ports[wIndex - 1].c_connection = false;
460 info->ports[wIndex - 1].status &= ~(1 << wValue);
464 case GetHubDescriptor:
465 xenhcd_hub_descriptor(info, (struct usb_hub_descriptor *)buf);
468 /* always local power supply good and no over-current exists. */
469 *(__le32 *)buf = cpu_to_le32(0);
472 if (!wIndex || wIndex > ports)
477 /* resume completion */
478 if (info->ports[wIndex].resuming &&
479 time_after_eq(jiffies, info->ports[wIndex].timeout)) {
480 info->ports[wIndex].status |=
481 USB_PORT_STAT_C_SUSPEND << 16;
482 info->ports[wIndex].status &= ~USB_PORT_STAT_SUSPEND;
485 /* reset completion */
486 if ((info->ports[wIndex].status & USB_PORT_STAT_RESET) != 0 &&
487 time_after_eq(jiffies, info->ports[wIndex].timeout)) {
488 info->ports[wIndex].status |=
489 USB_PORT_STAT_C_RESET << 16;
490 info->ports[wIndex].status &= ~USB_PORT_STAT_RESET;
492 if (info->devices[wIndex].status !=
493 USB_STATE_NOTATTACHED) {
494 info->ports[wIndex].status |=
495 USB_PORT_STAT_ENABLE;
496 info->devices[wIndex].status =
500 switch (info->devices[wIndex].speed) {
501 case XENUSB_SPEED_LOW:
502 info->ports[wIndex].status |=
503 USB_PORT_STAT_LOW_SPEED;
505 case XENUSB_SPEED_HIGH:
506 info->ports[wIndex].status |=
507 USB_PORT_STAT_HIGH_SPEED;
514 *(__le32 *)buf = cpu_to_le32(info->ports[wIndex].status);
517 if (!wIndex || wIndex > ports)
521 case USB_PORT_FEAT_POWER:
522 xenhcd_rhport_power_on(info, wIndex);
524 case USB_PORT_FEAT_RESET:
525 xenhcd_rhport_reset(info, wIndex);
527 case USB_PORT_FEAT_SUSPEND:
528 xenhcd_rhport_suspend(info, wIndex);
531 if (info->ports[wIndex-1].status & USB_PORT_STAT_POWER)
532 info->ports[wIndex-1].status |= (1 << wValue);
542 spin_unlock_irqrestore(&info->lock, flags);
544 /* check status for each port */
545 for (i = 0; i < ports; i++) {
546 if (info->ports[i].status & PORT_C_MASK)
550 usb_hcd_poll_rh_status(hcd);
555 static void xenhcd_free_urb_priv(struct urb_priv *urbp)
557 urbp->urb->hcpriv = NULL;
558 kmem_cache_free(xenhcd_urbp_cachep, urbp);
561 static inline unsigned int xenhcd_get_id_from_freelist(struct xenhcd_info *info)
565 free = info->shadow_free;
566 info->shadow_free = info->shadow[free].req.id;
567 info->shadow[free].req.id = 0x0fff; /* debug */
571 static inline void xenhcd_add_id_to_freelist(struct xenhcd_info *info,
574 info->shadow[id].req.id = info->shadow_free;
575 info->shadow[id].urb = NULL;
576 info->shadow_free = id;
579 static inline int xenhcd_count_pages(void *addr, int length)
581 unsigned long vaddr = (unsigned long)addr;
583 return PFN_UP(vaddr + length) - PFN_DOWN(vaddr);
586 static void xenhcd_gnttab_map(struct xenhcd_info *info, void *addr, int length,
587 grant_ref_t *gref_head,
588 struct xenusb_request_segment *seg,
589 int nr_pages, int flags)
592 unsigned long buffer_mfn;
594 unsigned int len = length;
598 for (i = 0; i < nr_pages; i++) {
599 buffer_mfn = PFN_DOWN(arbitrary_virt_to_machine(addr).maddr);
600 offset = offset_in_page(addr);
602 bytes = PAGE_SIZE - offset;
606 ref = gnttab_claim_grant_reference(gref_head);
607 gnttab_grant_foreign_access_ref(ref, info->xbdev->otherend_id,
610 seg[i].offset = (__u16)offset;
611 seg[i].length = (__u16)bytes;
618 static __u32 xenhcd_pipe_urb_to_xenusb(__u32 urb_pipe, __u8 port)
622 pipe = usb_pipedevice(urb_pipe) << XENUSB_PIPE_DEV_SHIFT;
623 pipe |= usb_pipeendpoint(urb_pipe) << XENUSB_PIPE_EP_SHIFT;
624 if (usb_pipein(urb_pipe))
625 pipe |= XENUSB_PIPE_DIR;
626 switch (usb_pipetype(urb_pipe)) {
627 case PIPE_ISOCHRONOUS:
628 pipe |= XENUSB_PIPE_TYPE_ISOC << XENUSB_PIPE_TYPE_SHIFT;
631 pipe |= XENUSB_PIPE_TYPE_INT << XENUSB_PIPE_TYPE_SHIFT;
634 pipe |= XENUSB_PIPE_TYPE_CTRL << XENUSB_PIPE_TYPE_SHIFT;
637 pipe |= XENUSB_PIPE_TYPE_BULK << XENUSB_PIPE_TYPE_SHIFT;
640 pipe = xenusb_setportnum_pipe(pipe, port);
645 static int xenhcd_map_urb_for_request(struct xenhcd_info *info, struct urb *urb,
646 struct xenusb_urb_request *req)
648 grant_ref_t gref_head;
649 int nr_buff_pages = 0;
650 int nr_isodesc_pages = 0;
653 if (urb->transfer_buffer_length) {
654 nr_buff_pages = xenhcd_count_pages(urb->transfer_buffer,
655 urb->transfer_buffer_length);
657 if (usb_pipeisoc(urb->pipe))
658 nr_isodesc_pages = xenhcd_count_pages(
659 &urb->iso_frame_desc[0],
660 sizeof(struct usb_iso_packet_descriptor) *
661 urb->number_of_packets);
663 nr_grants = nr_buff_pages + nr_isodesc_pages;
664 if (nr_grants > XENUSB_MAX_SEGMENTS_PER_REQUEST) {
665 pr_err("xenhcd: error: %d grants\n", nr_grants);
669 if (gnttab_alloc_grant_references(nr_grants, &gref_head)) {
670 pr_err("xenhcd: gnttab_alloc_grant_references() error\n");
674 xenhcd_gnttab_map(info, urb->transfer_buffer,
675 urb->transfer_buffer_length, &gref_head,
676 &req->seg[0], nr_buff_pages,
677 usb_pipein(urb->pipe) ? 0 : GTF_readonly);
680 req->pipe = xenhcd_pipe_urb_to_xenusb(urb->pipe, urb->dev->portnum);
681 req->transfer_flags = 0;
682 if (urb->transfer_flags & URB_SHORT_NOT_OK)
683 req->transfer_flags |= XENUSB_SHORT_NOT_OK;
684 req->buffer_length = urb->transfer_buffer_length;
685 req->nr_buffer_segs = nr_buff_pages;
687 switch (usb_pipetype(urb->pipe)) {
688 case PIPE_ISOCHRONOUS:
689 req->u.isoc.interval = urb->interval;
690 req->u.isoc.start_frame = urb->start_frame;
691 req->u.isoc.number_of_packets = urb->number_of_packets;
692 req->u.isoc.nr_frame_desc_segs = nr_isodesc_pages;
694 xenhcd_gnttab_map(info, &urb->iso_frame_desc[0],
695 sizeof(struct usb_iso_packet_descriptor) *
696 urb->number_of_packets,
697 &gref_head, &req->seg[nr_buff_pages],
698 nr_isodesc_pages, 0);
701 req->u.intr.interval = urb->interval;
704 if (urb->setup_packet)
705 memcpy(req->u.ctrl, urb->setup_packet, 8);
714 gnttab_free_grant_references(gref_head);
719 static void xenhcd_gnttab_done(struct usb_shadow *shadow)
724 nr_segs = shadow->req.nr_buffer_segs;
726 if (xenusb_pipeisoc(shadow->req.pipe))
727 nr_segs += shadow->req.u.isoc.nr_frame_desc_segs;
729 for (i = 0; i < nr_segs; i++)
730 gnttab_end_foreign_access(shadow->req.seg[i].gref, 0, 0UL);
732 shadow->req.nr_buffer_segs = 0;
733 shadow->req.u.isoc.nr_frame_desc_segs = 0;
736 static int xenhcd_translate_status(int status)
739 case XENUSB_STATUS_OK:
741 case XENUSB_STATUS_NODEV:
743 case XENUSB_STATUS_INVAL:
745 case XENUSB_STATUS_STALL:
747 case XENUSB_STATUS_IOERROR:
749 case XENUSB_STATUS_BABBLE:
756 static void xenhcd_giveback_urb(struct xenhcd_info *info, struct urb *urb,
759 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
760 int priv_status = urbp->status;
762 list_del_init(&urbp->list);
763 xenhcd_free_urb_priv(urbp);
765 if (urb->status == -EINPROGRESS)
766 urb->status = xenhcd_translate_status(status);
768 spin_unlock(&info->lock);
769 usb_hcd_giveback_urb(xenhcd_info_to_hcd(info), urb,
770 priv_status <= 0 ? priv_status : urb->status);
771 spin_lock(&info->lock);
774 static int xenhcd_do_request(struct xenhcd_info *info, struct urb_priv *urbp)
776 struct xenusb_urb_request *req;
777 struct urb *urb = urbp->urb;
782 id = xenhcd_get_id_from_freelist(info);
783 req = &info->shadow[id].req;
786 if (unlikely(urbp->unlinked)) {
787 req->u.unlink.unlink_id = urbp->req_id;
788 req->pipe = xenusb_setunlink_pipe(xenhcd_pipe_urb_to_xenusb(
789 urb->pipe, urb->dev->portnum));
790 urbp->unlink_req_id = id;
792 ret = xenhcd_map_urb_for_request(info, urb, req);
794 xenhcd_add_id_to_freelist(info, id);
800 req = RING_GET_REQUEST(&info->urb_ring, info->urb_ring.req_prod_pvt);
801 *req = info->shadow[id].req;
803 info->urb_ring.req_prod_pvt++;
804 info->shadow[id].urb = urb;
806 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&info->urb_ring, notify);
808 notify_remote_via_irq(info->irq);
813 static void xenhcd_kick_pending_urbs(struct xenhcd_info *info)
815 struct urb_priv *urbp;
817 while (!list_empty(&info->pending_submit_list)) {
818 if (RING_FULL(&info->urb_ring)) {
819 xenhcd_timer_action(info, TIMER_RING_WATCHDOG);
823 urbp = list_entry(info->pending_submit_list.next,
824 struct urb_priv, list);
825 if (!xenhcd_do_request(info, urbp))
826 list_move_tail(&urbp->list, &info->in_progress_list);
828 xenhcd_giveback_urb(info, urbp->urb, -ESHUTDOWN);
830 xenhcd_timer_action_done(info, TIMER_SCAN_PENDING_URBS);
834 * caller must lock info->lock
836 static void xenhcd_cancel_all_enqueued_urbs(struct xenhcd_info *info)
838 struct urb_priv *urbp, *tmp;
841 list_for_each_entry_safe(urbp, tmp, &info->in_progress_list, list) {
842 req_id = urbp->req_id;
843 if (!urbp->unlinked) {
844 xenhcd_gnttab_done(&info->shadow[req_id]);
845 if (urbp->urb->status == -EINPROGRESS)
847 xenhcd_giveback_urb(info, urbp->urb,
850 xenhcd_giveback_urb(info, urbp->urb,
853 info->shadow[req_id].urb = NULL;
856 list_for_each_entry_safe(urbp, tmp, &info->pending_submit_list, list)
857 xenhcd_giveback_urb(info, urbp->urb, -ESHUTDOWN);
861 * caller must lock info->lock
863 static void xenhcd_giveback_unlinked_urbs(struct xenhcd_info *info)
865 struct urb_priv *urbp, *tmp;
867 list_for_each_entry_safe(urbp, tmp, &info->giveback_waiting_list, list)
868 xenhcd_giveback_urb(info, urbp->urb, urbp->urb->status);
871 static int xenhcd_submit_urb(struct xenhcd_info *info, struct urb_priv *urbp)
875 if (RING_FULL(&info->urb_ring)) {
876 list_add_tail(&urbp->list, &info->pending_submit_list);
877 xenhcd_timer_action(info, TIMER_RING_WATCHDOG);
881 if (!list_empty(&info->pending_submit_list)) {
882 list_add_tail(&urbp->list, &info->pending_submit_list);
883 xenhcd_timer_action(info, TIMER_SCAN_PENDING_URBS);
887 ret = xenhcd_do_request(info, urbp);
889 list_add_tail(&urbp->list, &info->in_progress_list);
894 static int xenhcd_unlink_urb(struct xenhcd_info *info, struct urb_priv *urbp)
898 /* already unlinked? */
902 urbp->unlinked = true;
904 /* the urb is still in pending_submit queue */
905 if (urbp->req_id == ~0) {
906 list_move_tail(&urbp->list, &info->giveback_waiting_list);
907 xenhcd_timer_action(info, TIMER_SCAN_PENDING_URBS);
911 /* send unlink request to backend */
912 if (RING_FULL(&info->urb_ring)) {
913 list_move_tail(&urbp->list, &info->pending_unlink_list);
914 xenhcd_timer_action(info, TIMER_RING_WATCHDOG);
918 if (!list_empty(&info->pending_unlink_list)) {
919 list_move_tail(&urbp->list, &info->pending_unlink_list);
920 xenhcd_timer_action(info, TIMER_SCAN_PENDING_URBS);
924 ret = xenhcd_do_request(info, urbp);
926 list_move_tail(&urbp->list, &info->in_progress_list);
931 static int xenhcd_urb_request_done(struct xenhcd_info *info)
933 struct xenusb_urb_response res;
940 spin_lock_irqsave(&info->lock, flags);
942 rp = info->urb_ring.sring->rsp_prod;
943 if (RING_RESPONSE_PROD_OVERFLOW(&info->urb_ring, rp)) {
944 xenhcd_set_error(info, "Illegal index on urb-ring");
945 spin_unlock_irqrestore(&info->lock, flags);
948 rmb(); /* ensure we see queued responses up to "rp" */
950 for (i = info->urb_ring.rsp_cons; i != rp; i++) {
951 RING_COPY_RESPONSE(&info->urb_ring, i, &res);
953 if (id >= XENUSB_URB_RING_SIZE) {
954 xenhcd_set_error(info, "Illegal data on urb-ring");
958 if (likely(xenusb_pipesubmit(info->shadow[id].req.pipe))) {
959 xenhcd_gnttab_done(&info->shadow[id]);
960 urb = info->shadow[id].urb;
962 urb->actual_length = res.actual_length;
963 urb->error_count = res.error_count;
964 urb->start_frame = res.start_frame;
965 xenhcd_giveback_urb(info, urb, res.status);
969 xenhcd_add_id_to_freelist(info, id);
971 info->urb_ring.rsp_cons = i;
973 if (i != info->urb_ring.req_prod_pvt)
974 RING_FINAL_CHECK_FOR_RESPONSES(&info->urb_ring, more_to_do);
976 info->urb_ring.sring->rsp_event = i + 1;
978 spin_unlock_irqrestore(&info->lock, flags);
983 static int xenhcd_conn_notify(struct xenhcd_info *info)
985 struct xenusb_conn_response res;
986 struct xenusb_conn_request *req;
992 int port_changed = 0;
995 spin_lock_irqsave(&info->lock, flags);
997 rc = info->conn_ring.rsp_cons;
998 rp = info->conn_ring.sring->rsp_prod;
999 if (RING_RESPONSE_PROD_OVERFLOW(&info->conn_ring, rp)) {
1000 xenhcd_set_error(info, "Illegal index on conn-ring");
1001 spin_unlock_irqrestore(&info->lock, flags);
1004 rmb(); /* ensure we see queued responses up to "rp" */
1007 RING_COPY_RESPONSE(&info->conn_ring, rc, &res);
1009 portnum = res.portnum;
1011 info->conn_ring.rsp_cons = ++rc;
1013 if (xenhcd_rhport_connect(info, portnum, speed)) {
1014 xenhcd_set_error(info, "Illegal data on conn-ring");
1015 spin_unlock_irqrestore(&info->lock, flags);
1019 if (info->ports[portnum - 1].c_connection)
1024 req = RING_GET_REQUEST(&info->conn_ring,
1025 info->conn_ring.req_prod_pvt);
1027 info->conn_ring.req_prod_pvt++;
1030 if (rc != info->conn_ring.req_prod_pvt)
1031 RING_FINAL_CHECK_FOR_RESPONSES(&info->conn_ring, more_to_do);
1033 info->conn_ring.sring->rsp_event = rc + 1;
1035 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&info->conn_ring, notify);
1037 notify_remote_via_irq(info->irq);
1039 spin_unlock_irqrestore(&info->lock, flags);
1042 usb_hcd_poll_rh_status(xenhcd_info_to_hcd(info));
1047 static irqreturn_t xenhcd_int(int irq, void *dev_id)
1049 struct xenhcd_info *info = (struct xenhcd_info *)dev_id;
1051 if (unlikely(info->error))
1054 while (xenhcd_urb_request_done(info) | xenhcd_conn_notify(info))
1055 /* Yield point for this unbounded loop. */
1061 static void xenhcd_destroy_rings(struct xenhcd_info *info)
1064 unbind_from_irqhandler(info->irq, info);
1067 if (info->urb_ring_ref != GRANT_INVALID_REF) {
1068 gnttab_end_foreign_access(info->urb_ring_ref, 0,
1069 (unsigned long)info->urb_ring.sring);
1070 info->urb_ring_ref = GRANT_INVALID_REF;
1072 info->urb_ring.sring = NULL;
1074 if (info->conn_ring_ref != GRANT_INVALID_REF) {
1075 gnttab_end_foreign_access(info->conn_ring_ref, 0,
1076 (unsigned long)info->conn_ring.sring);
1077 info->conn_ring_ref = GRANT_INVALID_REF;
1079 info->conn_ring.sring = NULL;
1082 static int xenhcd_setup_rings(struct xenbus_device *dev,
1083 struct xenhcd_info *info)
1085 struct xenusb_urb_sring *urb_sring;
1086 struct xenusb_conn_sring *conn_sring;
1090 info->urb_ring_ref = GRANT_INVALID_REF;
1091 info->conn_ring_ref = GRANT_INVALID_REF;
1093 urb_sring = (struct xenusb_urb_sring *)get_zeroed_page(
1094 GFP_NOIO | __GFP_HIGH);
1096 xenbus_dev_fatal(dev, -ENOMEM, "allocating urb ring");
1099 SHARED_RING_INIT(urb_sring);
1100 FRONT_RING_INIT(&info->urb_ring, urb_sring, PAGE_SIZE);
1102 err = xenbus_grant_ring(dev, urb_sring, 1, &gref);
1104 free_page((unsigned long)urb_sring);
1105 info->urb_ring.sring = NULL;
1108 info->urb_ring_ref = gref;
1110 conn_sring = (struct xenusb_conn_sring *)get_zeroed_page(
1111 GFP_NOIO | __GFP_HIGH);
1113 xenbus_dev_fatal(dev, -ENOMEM, "allocating conn ring");
1117 SHARED_RING_INIT(conn_sring);
1118 FRONT_RING_INIT(&info->conn_ring, conn_sring, PAGE_SIZE);
1120 err = xenbus_grant_ring(dev, conn_sring, 1, &gref);
1122 free_page((unsigned long)conn_sring);
1123 info->conn_ring.sring = NULL;
1126 info->conn_ring_ref = gref;
1128 err = xenbus_alloc_evtchn(dev, &info->evtchn);
1130 xenbus_dev_fatal(dev, err, "xenbus_alloc_evtchn");
1134 err = bind_evtchn_to_irq(info->evtchn);
1136 xenbus_dev_fatal(dev, err, "bind_evtchn_to_irq");
1142 err = request_threaded_irq(info->irq, NULL, xenhcd_int,
1143 IRQF_ONESHOT, "xenhcd", info);
1145 xenbus_dev_fatal(dev, err, "request_threaded_irq");
1152 unbind_from_irqhandler(info->irq, info);
1154 xenhcd_destroy_rings(info);
1158 static int xenhcd_talk_to_backend(struct xenbus_device *dev,
1159 struct xenhcd_info *info)
1161 const char *message;
1162 struct xenbus_transaction xbt;
1165 err = xenhcd_setup_rings(dev, info);
1170 err = xenbus_transaction_start(&xbt);
1172 xenbus_dev_fatal(dev, err, "starting transaction");
1176 err = xenbus_printf(xbt, dev->nodename, "urb-ring-ref", "%u",
1177 info->urb_ring_ref);
1179 message = "writing urb-ring-ref";
1180 goto abort_transaction;
1183 err = xenbus_printf(xbt, dev->nodename, "conn-ring-ref", "%u",
1184 info->conn_ring_ref);
1186 message = "writing conn-ring-ref";
1187 goto abort_transaction;
1190 err = xenbus_printf(xbt, dev->nodename, "event-channel", "%u",
1193 message = "writing event-channel";
1194 goto abort_transaction;
1197 err = xenbus_transaction_end(xbt, 0);
1201 xenbus_dev_fatal(dev, err, "completing transaction");
1208 xenbus_transaction_end(xbt, 1);
1209 xenbus_dev_fatal(dev, err, "%s", message);
1212 xenhcd_destroy_rings(info);
1217 static int xenhcd_connect(struct xenbus_device *dev)
1219 struct xenhcd_info *info = dev_get_drvdata(&dev->dev);
1220 struct xenusb_conn_request *req;
1223 char name[TASK_COMM_LEN];
1224 struct usb_hcd *hcd;
1226 hcd = xenhcd_info_to_hcd(info);
1227 snprintf(name, TASK_COMM_LEN, "xenhcd.%d", hcd->self.busnum);
1229 err = xenhcd_talk_to_backend(dev, info);
1233 /* prepare ring for hotplug notification */
1234 for (idx = 0; idx < XENUSB_CONN_RING_SIZE; idx++) {
1235 req = RING_GET_REQUEST(&info->conn_ring, idx);
1238 info->conn_ring.req_prod_pvt = idx;
1240 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&info->conn_ring, notify);
1242 notify_remote_via_irq(info->irq);
1247 static void xenhcd_disconnect(struct xenbus_device *dev)
1249 struct xenhcd_info *info = dev_get_drvdata(&dev->dev);
1250 struct usb_hcd *hcd = xenhcd_info_to_hcd(info);
1252 usb_remove_hcd(hcd);
1253 xenbus_frontend_closed(dev);
1256 static void xenhcd_watchdog(struct timer_list *timer)
1258 struct xenhcd_info *info = from_timer(info, timer, watchdog);
1259 unsigned long flags;
1261 spin_lock_irqsave(&info->lock, flags);
1262 if (likely(HC_IS_RUNNING(xenhcd_info_to_hcd(info)->state))) {
1263 xenhcd_timer_action_done(info, TIMER_RING_WATCHDOG);
1264 xenhcd_giveback_unlinked_urbs(info);
1265 xenhcd_kick_pending_urbs(info);
1267 spin_unlock_irqrestore(&info->lock, flags);
1273 static int xenhcd_setup(struct usb_hcd *hcd)
1275 struct xenhcd_info *info = xenhcd_hcd_to_info(hcd);
1277 spin_lock_init(&info->lock);
1278 INIT_LIST_HEAD(&info->pending_submit_list);
1279 INIT_LIST_HEAD(&info->pending_unlink_list);
1280 INIT_LIST_HEAD(&info->in_progress_list);
1281 INIT_LIST_HEAD(&info->giveback_waiting_list);
1282 timer_setup(&info->watchdog, xenhcd_watchdog, 0);
1284 hcd->has_tt = (hcd->driver->flags & HCD_MASK) != HCD_USB11;
1292 static int xenhcd_run(struct usb_hcd *hcd)
1294 hcd->uses_new_polling = 1;
1295 clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
1296 hcd->state = HC_STATE_RUNNING;
1303 static void xenhcd_stop(struct usb_hcd *hcd)
1305 struct xenhcd_info *info = xenhcd_hcd_to_info(hcd);
1307 del_timer_sync(&info->watchdog);
1308 spin_lock_irq(&info->lock);
1309 /* cancel all urbs */
1310 hcd->state = HC_STATE_HALT;
1311 xenhcd_cancel_all_enqueued_urbs(info);
1312 xenhcd_giveback_unlinked_urbs(info);
1313 spin_unlock_irq(&info->lock);
1317 * called as .urb_enqueue()
1318 * non-error returns are promise to giveback the urb later
1320 static int xenhcd_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
1323 struct xenhcd_info *info = xenhcd_hcd_to_info(hcd);
1324 struct urb_priv *urbp;
1325 unsigned long flags;
1328 if (unlikely(info->error))
1331 urbp = kmem_cache_zalloc(xenhcd_urbp_cachep, mem_flags);
1335 spin_lock_irqsave(&info->lock, flags);
1340 urbp->unlink_req_id = ~0;
1341 INIT_LIST_HEAD(&urbp->list);
1343 urb->unlinked = false;
1345 ret = xenhcd_submit_urb(info, urbp);
1348 xenhcd_free_urb_priv(urbp);
1350 spin_unlock_irqrestore(&info->lock, flags);
1356 * called as .urb_dequeue()
1358 static int xenhcd_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
1360 struct xenhcd_info *info = xenhcd_hcd_to_info(hcd);
1361 struct urb_priv *urbp;
1362 unsigned long flags;
1365 spin_lock_irqsave(&info->lock, flags);
1369 urbp->status = status;
1370 ret = xenhcd_unlink_urb(info, urbp);
1373 spin_unlock_irqrestore(&info->lock, flags);
1379 * called from usb_get_current_frame_number(),
1380 * but, almost all drivers not use such function.
1382 static int xenhcd_get_frame(struct usb_hcd *hcd)
1384 /* it means error, but probably no problem :-) */
1388 static struct hc_driver xenhcd_usb20_hc_driver = {
1389 .description = "xen-hcd",
1390 .product_desc = "Xen USB2.0 Virtual Host Controller",
1391 .hcd_priv_size = sizeof(struct xenhcd_info),
1394 /* basic HC lifecycle operations */
1395 .reset = xenhcd_setup,
1396 .start = xenhcd_run,
1397 .stop = xenhcd_stop,
1399 /* managing urb I/O */
1400 .urb_enqueue = xenhcd_urb_enqueue,
1401 .urb_dequeue = xenhcd_urb_dequeue,
1402 .get_frame_number = xenhcd_get_frame,
1404 /* root hub operations */
1405 .hub_status_data = xenhcd_hub_status_data,
1406 .hub_control = xenhcd_hub_control,
1408 .bus_suspend = xenhcd_bus_suspend,
1409 .bus_resume = xenhcd_bus_resume,
1413 static struct hc_driver xenhcd_usb11_hc_driver = {
1414 .description = "xen-hcd",
1415 .product_desc = "Xen USB1.1 Virtual Host Controller",
1416 .hcd_priv_size = sizeof(struct xenhcd_info),
1419 /* basic HC lifecycle operations */
1420 .reset = xenhcd_setup,
1421 .start = xenhcd_run,
1422 .stop = xenhcd_stop,
1424 /* managing urb I/O */
1425 .urb_enqueue = xenhcd_urb_enqueue,
1426 .urb_dequeue = xenhcd_urb_dequeue,
1427 .get_frame_number = xenhcd_get_frame,
1429 /* root hub operations */
1430 .hub_status_data = xenhcd_hub_status_data,
1431 .hub_control = xenhcd_hub_control,
1433 .bus_suspend = xenhcd_bus_suspend,
1434 .bus_resume = xenhcd_bus_resume,
1438 static struct usb_hcd *xenhcd_create_hcd(struct xenbus_device *dev)
1444 struct usb_hcd *hcd = NULL;
1445 struct xenhcd_info *info;
1447 err = xenbus_scanf(XBT_NIL, dev->otherend, "num-ports", "%d",
1450 xenbus_dev_fatal(dev, err, "reading num-ports");
1451 return ERR_PTR(-EINVAL);
1453 if (num_ports < 1 || num_ports > XENUSB_MAX_PORTNR) {
1454 xenbus_dev_fatal(dev, err, "invalid num-ports");
1455 return ERR_PTR(-EINVAL);
1458 err = xenbus_scanf(XBT_NIL, dev->otherend, "usb-ver", "%d", &usb_ver);
1460 xenbus_dev_fatal(dev, err, "reading usb-ver");
1461 return ERR_PTR(-EINVAL);
1464 case XENUSB_VER_USB11:
1465 hcd = usb_create_hcd(&xenhcd_usb11_hc_driver, &dev->dev,
1466 dev_name(&dev->dev));
1468 case XENUSB_VER_USB20:
1469 hcd = usb_create_hcd(&xenhcd_usb20_hc_driver, &dev->dev,
1470 dev_name(&dev->dev));
1473 xenbus_dev_fatal(dev, err, "invalid usb-ver");
1474 return ERR_PTR(-EINVAL);
1477 xenbus_dev_fatal(dev, err,
1478 "fail to allocate USB host controller");
1479 return ERR_PTR(-ENOMEM);
1482 info = xenhcd_hcd_to_info(hcd);
1484 info->rh_numports = num_ports;
1486 for (i = 0; i < XENUSB_URB_RING_SIZE; i++) {
1487 info->shadow[i].req.id = i + 1;
1488 info->shadow[i].urb = NULL;
1490 info->shadow[XENUSB_URB_RING_SIZE - 1].req.id = 0x0fff;
1495 static void xenhcd_backend_changed(struct xenbus_device *dev,
1496 enum xenbus_state backend_state)
1498 switch (backend_state) {
1499 case XenbusStateInitialising:
1500 case XenbusStateReconfiguring:
1501 case XenbusStateReconfigured:
1502 case XenbusStateUnknown:
1505 case XenbusStateInitWait:
1506 case XenbusStateInitialised:
1507 case XenbusStateConnected:
1508 if (dev->state != XenbusStateInitialising)
1510 if (!xenhcd_connect(dev))
1511 xenbus_switch_state(dev, XenbusStateConnected);
1514 case XenbusStateClosed:
1515 if (dev->state == XenbusStateClosed)
1517 fallthrough; /* Missed the backend's Closing state. */
1518 case XenbusStateClosing:
1519 xenhcd_disconnect(dev);
1523 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
1529 static int xenhcd_remove(struct xenbus_device *dev)
1531 struct xenhcd_info *info = dev_get_drvdata(&dev->dev);
1532 struct usb_hcd *hcd = xenhcd_info_to_hcd(info);
1534 xenhcd_destroy_rings(info);
1540 static int xenhcd_probe(struct xenbus_device *dev,
1541 const struct xenbus_device_id *id)
1544 struct usb_hcd *hcd;
1545 struct xenhcd_info *info;
1550 hcd = xenhcd_create_hcd(dev);
1553 xenbus_dev_fatal(dev, err,
1554 "fail to create usb host controller");
1558 info = xenhcd_hcd_to_info(hcd);
1559 dev_set_drvdata(&dev->dev, info);
1561 err = usb_add_hcd(hcd, 0, 0);
1563 xenbus_dev_fatal(dev, err, "fail to add USB host controller");
1565 dev_set_drvdata(&dev->dev, NULL);
1571 static const struct xenbus_device_id xenhcd_ids[] = {
1576 static struct xenbus_driver xenhcd_driver = {
1578 .probe = xenhcd_probe,
1579 .otherend_changed = xenhcd_backend_changed,
1580 .remove = xenhcd_remove,
1583 static int __init xenhcd_init(void)
1588 xenhcd_urbp_cachep = kmem_cache_create("xenhcd_urb_priv",
1589 sizeof(struct urb_priv), 0, 0, NULL);
1590 if (!xenhcd_urbp_cachep) {
1591 pr_err("xenhcd failed to create kmem cache\n");
1595 return xenbus_register_frontend(&xenhcd_driver);
1597 module_init(xenhcd_init);
1599 static void __exit xenhcd_exit(void)
1601 kmem_cache_destroy(xenhcd_urbp_cachep);
1602 xenbus_unregister_driver(&xenhcd_driver);
1604 module_exit(xenhcd_exit);
1606 MODULE_ALIAS("xen:vusb");
1608 MODULE_DESCRIPTION("Xen USB Virtual Host Controller driver (xen-hcd)");
1609 MODULE_LICENSE("Dual BSD/GPL");