1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Xen USB Virtual Host Controller driver
7 * Copyright (C) 2009, FUJITSU LABORATORIES LTD.
8 * Author: Noboru Iwamatsu <n_iwamatsu@jp.fujitsu.com>
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;
58 /* Virtual Host Controller has 4 urb queues */
59 struct list_head pending_submit_list;
60 struct list_head pending_unlink_list;
61 struct list_head in_progress_list;
62 struct list_head giveback_waiting_list;
66 /* timer that kick pending and giveback waiting urbs */
67 struct timer_list watchdog;
68 unsigned long actions;
70 /* virtual root hub */
72 struct rhport_status ports[XENUSB_MAX_PORTNR];
73 struct vdevice_status devices[XENUSB_MAX_PORTNR];
75 /* Xen related staff */
76 struct xenbus_device *xbdev;
79 struct xenusb_urb_front_ring urb_ring;
80 struct xenusb_conn_front_ring conn_ring;
84 struct usb_shadow shadow[XENUSB_URB_RING_SIZE];
85 unsigned int shadow_free;
90 #define XENHCD_RING_JIFFIES (HZ/200)
91 #define XENHCD_SCAN_JIFFIES 1
93 enum xenhcd_timer_action {
95 TIMER_SCAN_PENDING_URBS,
98 static struct kmem_cache *xenhcd_urbp_cachep;
100 static inline struct xenhcd_info *xenhcd_hcd_to_info(struct usb_hcd *hcd)
102 return (struct xenhcd_info *)hcd->hcd_priv;
105 static inline struct usb_hcd *xenhcd_info_to_hcd(struct xenhcd_info *info)
107 return container_of((void *)info, struct usb_hcd, hcd_priv);
110 static void xenhcd_set_error(struct xenhcd_info *info, const char *msg)
114 pr_alert("xen-hcd: protocol error: %s!\n", msg);
117 static inline void xenhcd_timer_action_done(struct xenhcd_info *info,
118 enum xenhcd_timer_action action)
120 clear_bit(action, &info->actions);
123 static void xenhcd_timer_action(struct xenhcd_info *info,
124 enum xenhcd_timer_action action)
126 if (timer_pending(&info->watchdog) &&
127 test_bit(TIMER_SCAN_PENDING_URBS, &info->actions))
130 if (!test_and_set_bit(action, &info->actions)) {
134 case TIMER_RING_WATCHDOG:
135 t = XENHCD_RING_JIFFIES;
138 t = XENHCD_SCAN_JIFFIES;
141 mod_timer(&info->watchdog, t + jiffies);
146 * set virtual port connection status
148 static void xenhcd_set_connect_state(struct xenhcd_info *info, int portnum)
153 if (info->ports[port].status & USB_PORT_STAT_POWER) {
154 switch (info->devices[port].speed) {
155 case XENUSB_SPEED_NONE:
156 info->ports[port].status &=
157 ~(USB_PORT_STAT_CONNECTION |
158 USB_PORT_STAT_ENABLE |
159 USB_PORT_STAT_LOW_SPEED |
160 USB_PORT_STAT_HIGH_SPEED |
161 USB_PORT_STAT_SUSPEND);
163 case XENUSB_SPEED_LOW:
164 info->ports[port].status |= USB_PORT_STAT_CONNECTION;
165 info->ports[port].status |= USB_PORT_STAT_LOW_SPEED;
167 case XENUSB_SPEED_FULL:
168 info->ports[port].status |= USB_PORT_STAT_CONNECTION;
170 case XENUSB_SPEED_HIGH:
171 info->ports[port].status |= USB_PORT_STAT_CONNECTION;
172 info->ports[port].status |= USB_PORT_STAT_HIGH_SPEED;
177 info->ports[port].status |= (USB_PORT_STAT_C_CONNECTION << 16);
182 * set virtual device connection status
184 static int xenhcd_rhport_connect(struct xenhcd_info *info, __u8 portnum,
189 if (portnum < 1 || portnum > info->rh_numports)
190 return -EINVAL; /* invalid port number */
193 if (info->devices[port].speed != speed) {
195 case XENUSB_SPEED_NONE: /* disconnect */
196 info->devices[port].status = USB_STATE_NOTATTACHED;
198 case XENUSB_SPEED_LOW:
199 case XENUSB_SPEED_FULL:
200 case XENUSB_SPEED_HIGH:
201 info->devices[port].status = USB_STATE_ATTACHED;
206 info->devices[port].speed = speed;
207 info->ports[port].c_connection = true;
209 xenhcd_set_connect_state(info, portnum);
216 * SetPortFeature(PORT_SUSPENDED)
218 static void xenhcd_rhport_suspend(struct xenhcd_info *info, int portnum)
223 info->ports[port].status |= USB_PORT_STAT_SUSPEND;
224 info->devices[port].status = USB_STATE_SUSPENDED;
228 * ClearPortFeature(PORT_SUSPENDED)
230 static void xenhcd_rhport_resume(struct xenhcd_info *info, int portnum)
235 if (info->ports[port].status & USB_PORT_STAT_SUSPEND) {
236 info->ports[port].resuming = true;
237 info->ports[port].timeout = jiffies + msecs_to_jiffies(20);
242 * SetPortFeature(PORT_POWER)
244 static void xenhcd_rhport_power_on(struct xenhcd_info *info, int portnum)
249 if ((info->ports[port].status & USB_PORT_STAT_POWER) == 0) {
250 info->ports[port].status |= USB_PORT_STAT_POWER;
251 if (info->devices[port].status != USB_STATE_NOTATTACHED)
252 info->devices[port].status = USB_STATE_POWERED;
253 if (info->ports[port].c_connection)
254 xenhcd_set_connect_state(info, portnum);
259 * ClearPortFeature(PORT_POWER)
260 * SetConfiguration(non-zero)
264 static void xenhcd_rhport_power_off(struct xenhcd_info *info, int portnum)
269 if (info->ports[port].status & USB_PORT_STAT_POWER) {
270 info->ports[port].status = 0;
271 if (info->devices[port].status != USB_STATE_NOTATTACHED)
272 info->devices[port].status = USB_STATE_ATTACHED;
277 * ClearPortFeature(PORT_ENABLE)
279 static void xenhcd_rhport_disable(struct xenhcd_info *info, int portnum)
284 info->ports[port].status &= ~USB_PORT_STAT_ENABLE;
285 info->ports[port].status &= ~USB_PORT_STAT_SUSPEND;
286 info->ports[port].resuming = false;
287 if (info->devices[port].status != USB_STATE_NOTATTACHED)
288 info->devices[port].status = USB_STATE_POWERED;
292 * SetPortFeature(PORT_RESET)
294 static void xenhcd_rhport_reset(struct xenhcd_info *info, int portnum)
299 info->ports[port].status &= ~(USB_PORT_STAT_ENABLE |
300 USB_PORT_STAT_LOW_SPEED |
301 USB_PORT_STAT_HIGH_SPEED);
302 info->ports[port].status |= USB_PORT_STAT_RESET;
304 if (info->devices[port].status != USB_STATE_NOTATTACHED)
305 info->devices[port].status = USB_STATE_ATTACHED;
307 /* 10msec reset signaling */
308 info->ports[port].timeout = jiffies + msecs_to_jiffies(10);
312 static int xenhcd_bus_suspend(struct usb_hcd *hcd)
314 struct xenhcd_info *info = xenhcd_hcd_to_info(hcd);
318 ports = info->rh_numports;
320 spin_lock_irq(&info->lock);
321 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
324 /* suspend any active ports*/
325 for (i = 1; i <= ports; i++)
326 xenhcd_rhport_suspend(info, i);
328 spin_unlock_irq(&info->lock);
330 del_timer_sync(&info->watchdog);
335 static int xenhcd_bus_resume(struct usb_hcd *hcd)
337 struct xenhcd_info *info = xenhcd_hcd_to_info(hcd);
341 ports = info->rh_numports;
343 spin_lock_irq(&info->lock);
344 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
347 /* resume any suspended ports*/
348 for (i = 1; i <= ports; i++)
349 xenhcd_rhport_resume(info, i);
351 spin_unlock_irq(&info->lock);
357 static void xenhcd_hub_descriptor(struct xenhcd_info *info,
358 struct usb_hub_descriptor *desc)
361 int ports = info->rh_numports;
363 desc->bDescriptorType = 0x29;
364 desc->bPwrOn2PwrGood = 10; /* EHCI says 20ms max */
365 desc->bHubContrCurrent = 0;
366 desc->bNbrPorts = ports;
368 /* size of DeviceRemovable and PortPwrCtrlMask fields */
369 temp = 1 + (ports / 8);
370 desc->bDescLength = 7 + 2 * temp;
372 /* bitmaps for DeviceRemovable and PortPwrCtrlMask */
373 memset(&desc->u.hs.DeviceRemovable[0], 0, temp);
374 memset(&desc->u.hs.DeviceRemovable[temp], 0xff, temp);
376 /* per-port over current reporting and no power switching */
378 desc->wHubCharacteristics = cpu_to_le16(temp);
381 /* port status change mask for hub_status_data */
382 #define PORT_C_MASK ((USB_PORT_STAT_C_CONNECTION | \
383 USB_PORT_STAT_C_ENABLE | \
384 USB_PORT_STAT_C_SUSPEND | \
385 USB_PORT_STAT_C_OVERCURRENT | \
386 USB_PORT_STAT_C_RESET) << 16)
389 * See USB 2.0 Spec, 11.12.4 Hub and Port Status Change Bitmap.
390 * If port status changed, writes the bitmap to buf and return
391 * that length(number of bytes).
392 * If Nothing changed, return 0.
394 static int xenhcd_hub_status_data(struct usb_hcd *hcd, char *buf)
396 struct xenhcd_info *info = xenhcd_hcd_to_info(hcd);
403 /* initialize the status to no-changes */
404 ports = info->rh_numports;
405 ret = 1 + (ports / 8);
408 spin_lock_irqsave(&info->lock, flags);
410 for (i = 0; i < ports; i++) {
411 /* check status for each port */
412 if (info->ports[i].status & PORT_C_MASK) {
413 buf[(i + 1) / 8] |= 1 << (i + 1) % 8;
418 if ((hcd->state == HC_STATE_SUSPENDED) && (changed == 1))
419 usb_hcd_resume_root_hub(hcd);
421 spin_unlock_irqrestore(&info->lock, flags);
423 return changed ? ret : 0;
426 static int xenhcd_hub_control(struct usb_hcd *hcd, __u16 typeReq, __u16 wValue,
427 __u16 wIndex, char *buf, __u16 wLength)
429 struct xenhcd_info *info = xenhcd_hcd_to_info(hcd);
430 int ports = info->rh_numports;
436 spin_lock_irqsave(&info->lock, flags);
438 case ClearHubFeature:
439 /* ignore this request */
441 case ClearPortFeature:
442 if (!wIndex || wIndex > ports)
446 case USB_PORT_FEAT_SUSPEND:
447 xenhcd_rhport_resume(info, wIndex);
449 case USB_PORT_FEAT_POWER:
450 xenhcd_rhport_power_off(info, wIndex);
452 case USB_PORT_FEAT_ENABLE:
453 xenhcd_rhport_disable(info, wIndex);
455 case USB_PORT_FEAT_C_CONNECTION:
456 info->ports[wIndex - 1].c_connection = false;
459 info->ports[wIndex - 1].status &= ~(1 << wValue);
463 case GetHubDescriptor:
464 xenhcd_hub_descriptor(info, (struct usb_hub_descriptor *)buf);
467 /* always local power supply good and no over-current exists. */
468 *(__le32 *)buf = cpu_to_le32(0);
471 if (!wIndex || wIndex > ports)
476 /* resume completion */
477 if (info->ports[wIndex].resuming &&
478 time_after_eq(jiffies, info->ports[wIndex].timeout)) {
479 info->ports[wIndex].status |=
480 USB_PORT_STAT_C_SUSPEND << 16;
481 info->ports[wIndex].status &= ~USB_PORT_STAT_SUSPEND;
484 /* reset completion */
485 if ((info->ports[wIndex].status & USB_PORT_STAT_RESET) != 0 &&
486 time_after_eq(jiffies, info->ports[wIndex].timeout)) {
487 info->ports[wIndex].status |=
488 USB_PORT_STAT_C_RESET << 16;
489 info->ports[wIndex].status &= ~USB_PORT_STAT_RESET;
491 if (info->devices[wIndex].status !=
492 USB_STATE_NOTATTACHED) {
493 info->ports[wIndex].status |=
494 USB_PORT_STAT_ENABLE;
495 info->devices[wIndex].status =
499 switch (info->devices[wIndex].speed) {
500 case XENUSB_SPEED_LOW:
501 info->ports[wIndex].status |=
502 USB_PORT_STAT_LOW_SPEED;
504 case XENUSB_SPEED_HIGH:
505 info->ports[wIndex].status |=
506 USB_PORT_STAT_HIGH_SPEED;
513 *(__le32 *)buf = cpu_to_le32(info->ports[wIndex].status);
516 if (!wIndex || wIndex > ports)
520 case USB_PORT_FEAT_POWER:
521 xenhcd_rhport_power_on(info, wIndex);
523 case USB_PORT_FEAT_RESET:
524 xenhcd_rhport_reset(info, wIndex);
526 case USB_PORT_FEAT_SUSPEND:
527 xenhcd_rhport_suspend(info, wIndex);
530 if (info->ports[wIndex-1].status & USB_PORT_STAT_POWER)
531 info->ports[wIndex-1].status |= (1 << wValue);
541 spin_unlock_irqrestore(&info->lock, flags);
543 /* check status for each port */
544 for (i = 0; i < ports; i++) {
545 if (info->ports[i].status & PORT_C_MASK)
549 usb_hcd_poll_rh_status(hcd);
554 static void xenhcd_free_urb_priv(struct urb_priv *urbp)
556 urbp->urb->hcpriv = NULL;
557 kmem_cache_free(xenhcd_urbp_cachep, urbp);
560 static inline unsigned int xenhcd_get_id_from_freelist(struct xenhcd_info *info)
564 free = info->shadow_free;
565 info->shadow_free = info->shadow[free].req.id;
566 info->shadow[free].req.id = 0x0fff; /* debug */
570 static inline void xenhcd_add_id_to_freelist(struct xenhcd_info *info,
573 info->shadow[id].req.id = info->shadow_free;
574 info->shadow[id].urb = NULL;
575 info->shadow_free = id;
578 static inline int xenhcd_count_pages(void *addr, int length)
580 unsigned long vaddr = (unsigned long)addr;
582 return PFN_UP(vaddr + length) - PFN_DOWN(vaddr);
585 static void xenhcd_gnttab_map(struct xenhcd_info *info, void *addr, int length,
586 grant_ref_t *gref_head,
587 struct xenusb_request_segment *seg,
588 int nr_pages, int flags)
592 unsigned int len = length;
596 for (i = 0; i < nr_pages; i++) {
597 offset = offset_in_page(addr);
599 bytes = PAGE_SIZE - offset;
603 ref = gnttab_claim_grant_reference(gref_head);
604 gnttab_grant_foreign_access_ref(ref, info->xbdev->otherend_id,
605 virt_to_gfn(addr), flags);
607 seg[i].offset = (__u16)offset;
608 seg[i].length = (__u16)bytes;
615 static __u32 xenhcd_pipe_urb_to_xenusb(__u32 urb_pipe, __u8 port)
619 pipe = usb_pipedevice(urb_pipe) << XENUSB_PIPE_DEV_SHIFT;
620 pipe |= usb_pipeendpoint(urb_pipe) << XENUSB_PIPE_EP_SHIFT;
621 if (usb_pipein(urb_pipe))
622 pipe |= XENUSB_PIPE_DIR;
623 switch (usb_pipetype(urb_pipe)) {
624 case PIPE_ISOCHRONOUS:
625 pipe |= XENUSB_PIPE_TYPE_ISOC << XENUSB_PIPE_TYPE_SHIFT;
628 pipe |= XENUSB_PIPE_TYPE_INT << XENUSB_PIPE_TYPE_SHIFT;
631 pipe |= XENUSB_PIPE_TYPE_CTRL << XENUSB_PIPE_TYPE_SHIFT;
634 pipe |= XENUSB_PIPE_TYPE_BULK << XENUSB_PIPE_TYPE_SHIFT;
637 pipe = xenusb_setportnum_pipe(pipe, port);
642 static int xenhcd_map_urb_for_request(struct xenhcd_info *info, struct urb *urb,
643 struct xenusb_urb_request *req)
645 grant_ref_t gref_head;
646 int nr_buff_pages = 0;
647 int nr_isodesc_pages = 0;
650 if (urb->transfer_buffer_length) {
651 nr_buff_pages = xenhcd_count_pages(urb->transfer_buffer,
652 urb->transfer_buffer_length);
654 if (usb_pipeisoc(urb->pipe))
655 nr_isodesc_pages = xenhcd_count_pages(
656 &urb->iso_frame_desc[0],
657 sizeof(struct usb_iso_packet_descriptor) *
658 urb->number_of_packets);
660 nr_grants = nr_buff_pages + nr_isodesc_pages;
661 if (nr_grants > XENUSB_MAX_SEGMENTS_PER_REQUEST) {
662 pr_err("xenhcd: error: %d grants\n", nr_grants);
666 if (gnttab_alloc_grant_references(nr_grants, &gref_head)) {
667 pr_err("xenhcd: gnttab_alloc_grant_references() error\n");
671 xenhcd_gnttab_map(info, urb->transfer_buffer,
672 urb->transfer_buffer_length, &gref_head,
673 &req->seg[0], nr_buff_pages,
674 usb_pipein(urb->pipe) ? 0 : GTF_readonly);
677 req->pipe = xenhcd_pipe_urb_to_xenusb(urb->pipe, urb->dev->portnum);
678 req->transfer_flags = 0;
679 if (urb->transfer_flags & URB_SHORT_NOT_OK)
680 req->transfer_flags |= XENUSB_SHORT_NOT_OK;
681 req->buffer_length = urb->transfer_buffer_length;
682 req->nr_buffer_segs = nr_buff_pages;
684 switch (usb_pipetype(urb->pipe)) {
685 case PIPE_ISOCHRONOUS:
686 req->u.isoc.interval = urb->interval;
687 req->u.isoc.start_frame = urb->start_frame;
688 req->u.isoc.number_of_packets = urb->number_of_packets;
689 req->u.isoc.nr_frame_desc_segs = nr_isodesc_pages;
691 xenhcd_gnttab_map(info, &urb->iso_frame_desc[0],
692 sizeof(struct usb_iso_packet_descriptor) *
693 urb->number_of_packets,
694 &gref_head, &req->seg[nr_buff_pages],
695 nr_isodesc_pages, 0);
698 req->u.intr.interval = urb->interval;
701 if (urb->setup_packet)
702 memcpy(req->u.ctrl, urb->setup_packet, 8);
711 gnttab_free_grant_references(gref_head);
716 static void xenhcd_gnttab_done(struct xenhcd_info *info, unsigned int id)
718 struct usb_shadow *shadow = info->shadow + id;
722 if (!shadow->in_flight) {
723 xenhcd_set_error(info, "Illegal request id");
726 shadow->in_flight = false;
728 nr_segs = shadow->req.nr_buffer_segs;
730 if (xenusb_pipeisoc(shadow->req.pipe))
731 nr_segs += shadow->req.u.isoc.nr_frame_desc_segs;
733 for (i = 0; i < nr_segs; i++) {
734 if (!gnttab_try_end_foreign_access(shadow->req.seg[i].gref))
735 xenhcd_set_error(info, "backend didn't release grant");
738 shadow->req.nr_buffer_segs = 0;
739 shadow->req.u.isoc.nr_frame_desc_segs = 0;
742 static int xenhcd_translate_status(int status)
745 case XENUSB_STATUS_OK:
747 case XENUSB_STATUS_NODEV:
749 case XENUSB_STATUS_INVAL:
751 case XENUSB_STATUS_STALL:
753 case XENUSB_STATUS_IOERROR:
755 case XENUSB_STATUS_BABBLE:
762 static void xenhcd_giveback_urb(struct xenhcd_info *info, struct urb *urb,
765 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
766 int priv_status = urbp->status;
768 list_del_init(&urbp->list);
769 xenhcd_free_urb_priv(urbp);
771 if (urb->status == -EINPROGRESS)
772 urb->status = xenhcd_translate_status(status);
774 spin_unlock(&info->lock);
775 usb_hcd_giveback_urb(xenhcd_info_to_hcd(info), urb,
776 priv_status <= 0 ? priv_status : urb->status);
777 spin_lock(&info->lock);
780 static int xenhcd_do_request(struct xenhcd_info *info, struct urb_priv *urbp)
782 struct xenusb_urb_request *req;
783 struct urb *urb = urbp->urb;
788 id = xenhcd_get_id_from_freelist(info);
789 req = &info->shadow[id].req;
792 if (unlikely(urbp->unlinked)) {
793 req->u.unlink.unlink_id = urbp->req_id;
794 req->pipe = xenusb_setunlink_pipe(xenhcd_pipe_urb_to_xenusb(
795 urb->pipe, urb->dev->portnum));
796 urbp->unlink_req_id = id;
798 ret = xenhcd_map_urb_for_request(info, urb, req);
800 xenhcd_add_id_to_freelist(info, id);
806 req = RING_GET_REQUEST(&info->urb_ring, info->urb_ring.req_prod_pvt);
807 *req = info->shadow[id].req;
809 info->urb_ring.req_prod_pvt++;
810 info->shadow[id].urb = urb;
811 info->shadow[id].in_flight = true;
813 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&info->urb_ring, notify);
815 notify_remote_via_irq(info->irq);
820 static void xenhcd_kick_pending_urbs(struct xenhcd_info *info)
822 struct urb_priv *urbp;
824 while (!list_empty(&info->pending_submit_list)) {
825 if (RING_FULL(&info->urb_ring)) {
826 xenhcd_timer_action(info, TIMER_RING_WATCHDOG);
830 urbp = list_entry(info->pending_submit_list.next,
831 struct urb_priv, list);
832 if (!xenhcd_do_request(info, urbp))
833 list_move_tail(&urbp->list, &info->in_progress_list);
835 xenhcd_giveback_urb(info, urbp->urb, -ESHUTDOWN);
837 xenhcd_timer_action_done(info, TIMER_SCAN_PENDING_URBS);
841 * caller must lock info->lock
843 static void xenhcd_cancel_all_enqueued_urbs(struct xenhcd_info *info)
845 struct urb_priv *urbp, *tmp;
848 list_for_each_entry_safe(urbp, tmp, &info->in_progress_list, list) {
849 req_id = urbp->req_id;
850 if (!urbp->unlinked) {
851 xenhcd_gnttab_done(info, req_id);
854 if (urbp->urb->status == -EINPROGRESS)
856 xenhcd_giveback_urb(info, urbp->urb,
859 xenhcd_giveback_urb(info, urbp->urb,
862 info->shadow[req_id].urb = NULL;
865 list_for_each_entry_safe(urbp, tmp, &info->pending_submit_list, list)
866 xenhcd_giveback_urb(info, urbp->urb, -ESHUTDOWN);
870 * caller must lock info->lock
872 static void xenhcd_giveback_unlinked_urbs(struct xenhcd_info *info)
874 struct urb_priv *urbp, *tmp;
876 list_for_each_entry_safe(urbp, tmp, &info->giveback_waiting_list, list)
877 xenhcd_giveback_urb(info, urbp->urb, urbp->urb->status);
880 static int xenhcd_submit_urb(struct xenhcd_info *info, struct urb_priv *urbp)
884 if (RING_FULL(&info->urb_ring)) {
885 list_add_tail(&urbp->list, &info->pending_submit_list);
886 xenhcd_timer_action(info, TIMER_RING_WATCHDOG);
890 if (!list_empty(&info->pending_submit_list)) {
891 list_add_tail(&urbp->list, &info->pending_submit_list);
892 xenhcd_timer_action(info, TIMER_SCAN_PENDING_URBS);
896 ret = xenhcd_do_request(info, urbp);
898 list_add_tail(&urbp->list, &info->in_progress_list);
903 static int xenhcd_unlink_urb(struct xenhcd_info *info, struct urb_priv *urbp)
907 /* already unlinked? */
911 urbp->unlinked = true;
913 /* the urb is still in pending_submit queue */
914 if (urbp->req_id == ~0) {
915 list_move_tail(&urbp->list, &info->giveback_waiting_list);
916 xenhcd_timer_action(info, TIMER_SCAN_PENDING_URBS);
920 /* send unlink request to backend */
921 if (RING_FULL(&info->urb_ring)) {
922 list_move_tail(&urbp->list, &info->pending_unlink_list);
923 xenhcd_timer_action(info, TIMER_RING_WATCHDOG);
927 if (!list_empty(&info->pending_unlink_list)) {
928 list_move_tail(&urbp->list, &info->pending_unlink_list);
929 xenhcd_timer_action(info, TIMER_SCAN_PENDING_URBS);
933 ret = xenhcd_do_request(info, urbp);
935 list_move_tail(&urbp->list, &info->in_progress_list);
940 static void xenhcd_res_to_urb(struct xenhcd_info *info,
941 struct xenusb_urb_response *res, struct urb *urb)
946 if (res->actual_length > urb->transfer_buffer_length)
947 urb->actual_length = urb->transfer_buffer_length;
948 else if (res->actual_length < 0)
949 urb->actual_length = 0;
951 urb->actual_length = res->actual_length;
952 urb->error_count = res->error_count;
953 urb->start_frame = res->start_frame;
954 xenhcd_giveback_urb(info, urb, res->status);
957 static int xenhcd_urb_request_done(struct xenhcd_info *info,
958 unsigned int *eoiflag)
960 struct xenusb_urb_response res;
966 spin_lock_irqsave(&info->lock, flags);
968 rp = info->urb_ring.sring->rsp_prod;
969 if (RING_RESPONSE_PROD_OVERFLOW(&info->urb_ring, rp)) {
970 xenhcd_set_error(info, "Illegal index on urb-ring");
973 rmb(); /* ensure we see queued responses up to "rp" */
975 for (i = info->urb_ring.rsp_cons; i != rp; i++) {
976 RING_COPY_RESPONSE(&info->urb_ring, i, &res);
978 if (id >= XENUSB_URB_RING_SIZE) {
979 xenhcd_set_error(info, "Illegal data on urb-ring");
983 if (likely(xenusb_pipesubmit(info->shadow[id].req.pipe))) {
984 xenhcd_gnttab_done(info, id);
987 xenhcd_res_to_urb(info, &res, info->shadow[id].urb);
990 xenhcd_add_id_to_freelist(info, id);
994 info->urb_ring.rsp_cons = i;
996 if (i != info->urb_ring.req_prod_pvt)
997 RING_FINAL_CHECK_FOR_RESPONSES(&info->urb_ring, more_to_do);
999 info->urb_ring.sring->rsp_event = i + 1;
1001 spin_unlock_irqrestore(&info->lock, flags);
1006 spin_unlock_irqrestore(&info->lock, flags);
1010 static int xenhcd_conn_notify(struct xenhcd_info *info, unsigned int *eoiflag)
1012 struct xenusb_conn_response res;
1013 struct xenusb_conn_request *req;
1016 __u8 portnum, speed;
1019 int port_changed = 0;
1020 unsigned long flags;
1022 spin_lock_irqsave(&info->lock, flags);
1024 rc = info->conn_ring.rsp_cons;
1025 rp = info->conn_ring.sring->rsp_prod;
1026 if (RING_RESPONSE_PROD_OVERFLOW(&info->conn_ring, rp)) {
1027 xenhcd_set_error(info, "Illegal index on conn-ring");
1028 spin_unlock_irqrestore(&info->lock, flags);
1031 rmb(); /* ensure we see queued responses up to "rp" */
1034 RING_COPY_RESPONSE(&info->conn_ring, rc, &res);
1036 portnum = res.portnum;
1038 info->conn_ring.rsp_cons = ++rc;
1040 if (xenhcd_rhport_connect(info, portnum, speed)) {
1041 xenhcd_set_error(info, "Illegal data on conn-ring");
1042 spin_unlock_irqrestore(&info->lock, flags);
1046 if (info->ports[portnum - 1].c_connection)
1051 req = RING_GET_REQUEST(&info->conn_ring,
1052 info->conn_ring.req_prod_pvt);
1054 info->conn_ring.req_prod_pvt++;
1059 if (rc != info->conn_ring.req_prod_pvt)
1060 RING_FINAL_CHECK_FOR_RESPONSES(&info->conn_ring, more_to_do);
1062 info->conn_ring.sring->rsp_event = rc + 1;
1064 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&info->conn_ring, notify);
1066 notify_remote_via_irq(info->irq);
1068 spin_unlock_irqrestore(&info->lock, flags);
1071 usb_hcd_poll_rh_status(xenhcd_info_to_hcd(info));
1076 static irqreturn_t xenhcd_int(int irq, void *dev_id)
1078 struct xenhcd_info *info = (struct xenhcd_info *)dev_id;
1079 unsigned int eoiflag = XEN_EOI_FLAG_SPURIOUS;
1081 if (unlikely(info->error)) {
1082 xen_irq_lateeoi(irq, XEN_EOI_FLAG_SPURIOUS);
1086 while (xenhcd_urb_request_done(info, &eoiflag) |
1087 xenhcd_conn_notify(info, &eoiflag))
1088 /* Yield point for this unbounded loop. */
1091 xen_irq_lateeoi(irq, eoiflag);
1095 static void xenhcd_destroy_rings(struct xenhcd_info *info)
1098 unbind_from_irqhandler(info->irq, info);
1101 xenbus_teardown_ring((void **)&info->urb_ring.sring, 1,
1102 &info->urb_ring_ref);
1103 xenbus_teardown_ring((void **)&info->conn_ring.sring, 1,
1104 &info->conn_ring_ref);
1107 static int xenhcd_setup_rings(struct xenbus_device *dev,
1108 struct xenhcd_info *info)
1110 struct xenusb_urb_sring *urb_sring;
1111 struct xenusb_conn_sring *conn_sring;
1114 info->conn_ring_ref = INVALID_GRANT_REF;
1115 err = xenbus_setup_ring(dev, GFP_NOIO | __GFP_HIGH,
1116 (void **)&urb_sring, 1, &info->urb_ring_ref);
1118 xenbus_dev_fatal(dev, err, "allocating urb ring");
1121 XEN_FRONT_RING_INIT(&info->urb_ring, urb_sring, PAGE_SIZE);
1123 err = xenbus_setup_ring(dev, GFP_NOIO | __GFP_HIGH,
1124 (void **)&conn_sring, 1, &info->conn_ring_ref);
1126 xenbus_dev_fatal(dev, err, "allocating conn ring");
1129 XEN_FRONT_RING_INIT(&info->conn_ring, conn_sring, PAGE_SIZE);
1131 err = xenbus_alloc_evtchn(dev, &info->evtchn);
1133 xenbus_dev_fatal(dev, err, "xenbus_alloc_evtchn");
1137 err = bind_evtchn_to_irq_lateeoi(info->evtchn);
1139 xenbus_dev_fatal(dev, err, "bind_evtchn_to_irq_lateeoi");
1145 err = request_threaded_irq(info->irq, NULL, xenhcd_int,
1146 IRQF_ONESHOT, "xenhcd", info);
1148 xenbus_dev_fatal(dev, err, "request_threaded_irq");
1155 unbind_from_irqhandler(info->irq, info);
1157 xenhcd_destroy_rings(info);
1161 static int xenhcd_talk_to_backend(struct xenbus_device *dev,
1162 struct xenhcd_info *info)
1164 const char *message;
1165 struct xenbus_transaction xbt;
1168 err = xenhcd_setup_rings(dev, info);
1173 err = xenbus_transaction_start(&xbt);
1175 xenbus_dev_fatal(dev, err, "starting transaction");
1179 err = xenbus_printf(xbt, dev->nodename, "urb-ring-ref", "%u",
1180 info->urb_ring_ref);
1182 message = "writing urb-ring-ref";
1183 goto abort_transaction;
1186 err = xenbus_printf(xbt, dev->nodename, "conn-ring-ref", "%u",
1187 info->conn_ring_ref);
1189 message = "writing conn-ring-ref";
1190 goto abort_transaction;
1193 err = xenbus_printf(xbt, dev->nodename, "event-channel", "%u",
1196 message = "writing event-channel";
1197 goto abort_transaction;
1200 err = xenbus_transaction_end(xbt, 0);
1204 xenbus_dev_fatal(dev, err, "completing transaction");
1211 xenbus_transaction_end(xbt, 1);
1212 xenbus_dev_fatal(dev, err, "%s", message);
1215 xenhcd_destroy_rings(info);
1220 static int xenhcd_connect(struct xenbus_device *dev)
1222 struct xenhcd_info *info = dev_get_drvdata(&dev->dev);
1223 struct xenusb_conn_request *req;
1226 char name[TASK_COMM_LEN];
1227 struct usb_hcd *hcd;
1229 hcd = xenhcd_info_to_hcd(info);
1230 snprintf(name, TASK_COMM_LEN, "xenhcd.%d", hcd->self.busnum);
1232 err = xenhcd_talk_to_backend(dev, info);
1236 /* prepare ring for hotplug notification */
1237 for (idx = 0; idx < XENUSB_CONN_RING_SIZE; idx++) {
1238 req = RING_GET_REQUEST(&info->conn_ring, idx);
1241 info->conn_ring.req_prod_pvt = idx;
1243 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&info->conn_ring, notify);
1245 notify_remote_via_irq(info->irq);
1250 static void xenhcd_disconnect(struct xenbus_device *dev)
1252 struct xenhcd_info *info = dev_get_drvdata(&dev->dev);
1253 struct usb_hcd *hcd = xenhcd_info_to_hcd(info);
1255 usb_remove_hcd(hcd);
1256 xenbus_frontend_closed(dev);
1259 static void xenhcd_watchdog(struct timer_list *timer)
1261 struct xenhcd_info *info = from_timer(info, timer, watchdog);
1262 unsigned long flags;
1264 spin_lock_irqsave(&info->lock, flags);
1265 if (likely(HC_IS_RUNNING(xenhcd_info_to_hcd(info)->state))) {
1266 xenhcd_timer_action_done(info, TIMER_RING_WATCHDOG);
1267 xenhcd_giveback_unlinked_urbs(info);
1268 xenhcd_kick_pending_urbs(info);
1270 spin_unlock_irqrestore(&info->lock, flags);
1276 static int xenhcd_setup(struct usb_hcd *hcd)
1278 struct xenhcd_info *info = xenhcd_hcd_to_info(hcd);
1280 spin_lock_init(&info->lock);
1281 INIT_LIST_HEAD(&info->pending_submit_list);
1282 INIT_LIST_HEAD(&info->pending_unlink_list);
1283 INIT_LIST_HEAD(&info->in_progress_list);
1284 INIT_LIST_HEAD(&info->giveback_waiting_list);
1285 timer_setup(&info->watchdog, xenhcd_watchdog, 0);
1287 hcd->has_tt = (hcd->driver->flags & HCD_MASK) != HCD_USB11;
1295 static int xenhcd_run(struct usb_hcd *hcd)
1297 hcd->uses_new_polling = 1;
1298 clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
1299 hcd->state = HC_STATE_RUNNING;
1306 static void xenhcd_stop(struct usb_hcd *hcd)
1308 struct xenhcd_info *info = xenhcd_hcd_to_info(hcd);
1310 del_timer_sync(&info->watchdog);
1311 spin_lock_irq(&info->lock);
1312 /* cancel all urbs */
1313 hcd->state = HC_STATE_HALT;
1314 xenhcd_cancel_all_enqueued_urbs(info);
1315 xenhcd_giveback_unlinked_urbs(info);
1316 spin_unlock_irq(&info->lock);
1320 * called as .urb_enqueue()
1321 * non-error returns are promise to giveback the urb later
1323 static int xenhcd_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
1326 struct xenhcd_info *info = xenhcd_hcd_to_info(hcd);
1327 struct urb_priv *urbp;
1328 unsigned long flags;
1331 if (unlikely(info->error))
1334 urbp = kmem_cache_zalloc(xenhcd_urbp_cachep, mem_flags);
1338 spin_lock_irqsave(&info->lock, flags);
1343 urbp->unlink_req_id = ~0;
1344 INIT_LIST_HEAD(&urbp->list);
1346 urb->unlinked = false;
1348 ret = xenhcd_submit_urb(info, urbp);
1351 xenhcd_free_urb_priv(urbp);
1353 spin_unlock_irqrestore(&info->lock, flags);
1359 * called as .urb_dequeue()
1361 static int xenhcd_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
1363 struct xenhcd_info *info = xenhcd_hcd_to_info(hcd);
1364 struct urb_priv *urbp;
1365 unsigned long flags;
1368 spin_lock_irqsave(&info->lock, flags);
1372 urbp->status = status;
1373 ret = xenhcd_unlink_urb(info, urbp);
1376 spin_unlock_irqrestore(&info->lock, flags);
1382 * called from usb_get_current_frame_number(),
1383 * but, almost all drivers not use such function.
1385 static int xenhcd_get_frame(struct usb_hcd *hcd)
1387 /* it means error, but probably no problem :-) */
1391 static struct hc_driver xenhcd_usb20_hc_driver = {
1392 .description = "xen-hcd",
1393 .product_desc = "Xen USB2.0 Virtual Host Controller",
1394 .hcd_priv_size = sizeof(struct xenhcd_info),
1397 /* basic HC lifecycle operations */
1398 .reset = xenhcd_setup,
1399 .start = xenhcd_run,
1400 .stop = xenhcd_stop,
1402 /* managing urb I/O */
1403 .urb_enqueue = xenhcd_urb_enqueue,
1404 .urb_dequeue = xenhcd_urb_dequeue,
1405 .get_frame_number = xenhcd_get_frame,
1407 /* root hub operations */
1408 .hub_status_data = xenhcd_hub_status_data,
1409 .hub_control = xenhcd_hub_control,
1411 .bus_suspend = xenhcd_bus_suspend,
1412 .bus_resume = xenhcd_bus_resume,
1416 static struct hc_driver xenhcd_usb11_hc_driver = {
1417 .description = "xen-hcd",
1418 .product_desc = "Xen USB1.1 Virtual Host Controller",
1419 .hcd_priv_size = sizeof(struct xenhcd_info),
1422 /* basic HC lifecycle operations */
1423 .reset = xenhcd_setup,
1424 .start = xenhcd_run,
1425 .stop = xenhcd_stop,
1427 /* managing urb I/O */
1428 .urb_enqueue = xenhcd_urb_enqueue,
1429 .urb_dequeue = xenhcd_urb_dequeue,
1430 .get_frame_number = xenhcd_get_frame,
1432 /* root hub operations */
1433 .hub_status_data = xenhcd_hub_status_data,
1434 .hub_control = xenhcd_hub_control,
1436 .bus_suspend = xenhcd_bus_suspend,
1437 .bus_resume = xenhcd_bus_resume,
1441 static struct usb_hcd *xenhcd_create_hcd(struct xenbus_device *dev)
1447 struct usb_hcd *hcd = NULL;
1448 struct xenhcd_info *info;
1450 err = xenbus_scanf(XBT_NIL, dev->otherend, "num-ports", "%d",
1453 xenbus_dev_fatal(dev, err, "reading num-ports");
1454 return ERR_PTR(-EINVAL);
1456 if (num_ports < 1 || num_ports > XENUSB_MAX_PORTNR) {
1457 xenbus_dev_fatal(dev, err, "invalid num-ports");
1458 return ERR_PTR(-EINVAL);
1461 err = xenbus_scanf(XBT_NIL, dev->otherend, "usb-ver", "%d", &usb_ver);
1463 xenbus_dev_fatal(dev, err, "reading usb-ver");
1464 return ERR_PTR(-EINVAL);
1467 case XENUSB_VER_USB11:
1468 hcd = usb_create_hcd(&xenhcd_usb11_hc_driver, &dev->dev,
1469 dev_name(&dev->dev));
1471 case XENUSB_VER_USB20:
1472 hcd = usb_create_hcd(&xenhcd_usb20_hc_driver, &dev->dev,
1473 dev_name(&dev->dev));
1476 xenbus_dev_fatal(dev, err, "invalid usb-ver");
1477 return ERR_PTR(-EINVAL);
1480 xenbus_dev_fatal(dev, err,
1481 "fail to allocate USB host controller");
1482 return ERR_PTR(-ENOMEM);
1485 info = xenhcd_hcd_to_info(hcd);
1487 info->rh_numports = num_ports;
1489 for (i = 0; i < XENUSB_URB_RING_SIZE; i++) {
1490 info->shadow[i].req.id = i + 1;
1491 info->shadow[i].urb = NULL;
1492 info->shadow[i].in_flight = false;
1494 info->shadow[XENUSB_URB_RING_SIZE - 1].req.id = 0x0fff;
1499 static void xenhcd_backend_changed(struct xenbus_device *dev,
1500 enum xenbus_state backend_state)
1502 switch (backend_state) {
1503 case XenbusStateInitialising:
1504 case XenbusStateReconfiguring:
1505 case XenbusStateReconfigured:
1506 case XenbusStateUnknown:
1509 case XenbusStateInitWait:
1510 case XenbusStateInitialised:
1511 case XenbusStateConnected:
1512 if (dev->state != XenbusStateInitialising)
1514 if (!xenhcd_connect(dev))
1515 xenbus_switch_state(dev, XenbusStateConnected);
1518 case XenbusStateClosed:
1519 if (dev->state == XenbusStateClosed)
1521 fallthrough; /* Missed the backend's Closing state. */
1522 case XenbusStateClosing:
1523 xenhcd_disconnect(dev);
1527 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
1533 static void xenhcd_remove(struct xenbus_device *dev)
1535 struct xenhcd_info *info = dev_get_drvdata(&dev->dev);
1536 struct usb_hcd *hcd = xenhcd_info_to_hcd(info);
1538 xenhcd_destroy_rings(info);
1542 static int xenhcd_probe(struct xenbus_device *dev,
1543 const struct xenbus_device_id *id)
1546 struct usb_hcd *hcd;
1547 struct xenhcd_info *info;
1552 hcd = xenhcd_create_hcd(dev);
1555 xenbus_dev_fatal(dev, err,
1556 "fail to create usb host controller");
1560 info = xenhcd_hcd_to_info(hcd);
1561 dev_set_drvdata(&dev->dev, info);
1563 err = usb_add_hcd(hcd, 0, 0);
1565 xenbus_dev_fatal(dev, err, "fail to add USB host controller");
1567 dev_set_drvdata(&dev->dev, NULL);
1573 static const struct xenbus_device_id xenhcd_ids[] = {
1578 static struct xenbus_driver xenhcd_driver = {
1580 .probe = xenhcd_probe,
1581 .otherend_changed = xenhcd_backend_changed,
1582 .remove = xenhcd_remove,
1585 static int __init xenhcd_init(void)
1590 xenhcd_urbp_cachep = kmem_cache_create("xenhcd_urb_priv",
1591 sizeof(struct urb_priv), 0, 0, NULL);
1592 if (!xenhcd_urbp_cachep) {
1593 pr_err("xenhcd failed to create kmem cache\n");
1597 return xenbus_register_frontend(&xenhcd_driver);
1599 module_init(xenhcd_init);
1601 static void __exit xenhcd_exit(void)
1603 kmem_cache_destroy(xenhcd_urbp_cachep);
1604 xenbus_unregister_driver(&xenhcd_driver);
1606 module_exit(xenhcd_exit);
1608 MODULE_ALIAS("xen:vusb");
1609 MODULE_AUTHOR("Juergen Gross <jgross@suse.com>");
1610 MODULE_DESCRIPTION("Xen USB Virtual Host Controller driver (xen-hcd)");
1611 MODULE_LICENSE("Dual BSD/GPL");