Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * xen-hcd.c
0004  *
0005  * Xen USB Virtual Host Controller driver
0006  *
0007  * Copyright (C) 2009, FUJITSU LABORATORIES LTD.
0008  * Author: Noboru Iwamatsu <n_iwamatsu@jp.fujitsu.com>
0009  */
0010 
0011 #include <linux/module.h>
0012 #include <linux/usb.h>
0013 #include <linux/list.h>
0014 #include <linux/usb/hcd.h>
0015 #include <linux/io.h>
0016 
0017 #include <xen/xen.h>
0018 #include <xen/xenbus.h>
0019 #include <xen/grant_table.h>
0020 #include <xen/events.h>
0021 #include <xen/page.h>
0022 
0023 #include <xen/interface/io/usbif.h>
0024 
0025 /* Private per-URB data */
0026 struct urb_priv {
0027     struct list_head list;
0028     struct urb *urb;
0029     int req_id;     /* RING_REQUEST id for submitting */
0030     int unlink_req_id;  /* RING_REQUEST id for unlinking */
0031     int status;
0032     bool unlinked;      /* dequeued marker */
0033 };
0034 
0035 /* virtual roothub port status */
0036 struct rhport_status {
0037     __u32 status;
0038     bool resuming;      /* in resuming */
0039     bool c_connection;  /* connection changed */
0040     unsigned long timeout;
0041 };
0042 
0043 /* status of attached device */
0044 struct vdevice_status {
0045     int devnum;
0046     enum usb_device_state status;
0047     enum usb_device_speed speed;
0048 };
0049 
0050 /* RING request shadow */
0051 struct usb_shadow {
0052     struct xenusb_urb_request req;
0053     struct urb *urb;
0054     bool in_flight;
0055 };
0056 
0057 struct xenhcd_info {
0058     /* Virtual Host Controller has 4 urb queues */
0059     struct list_head pending_submit_list;
0060     struct list_head pending_unlink_list;
0061     struct list_head in_progress_list;
0062     struct list_head giveback_waiting_list;
0063 
0064     spinlock_t lock;
0065 
0066     /* timer that kick pending and giveback waiting urbs */
0067     struct timer_list watchdog;
0068     unsigned long actions;
0069 
0070     /* virtual root hub */
0071     int rh_numports;
0072     struct rhport_status ports[XENUSB_MAX_PORTNR];
0073     struct vdevice_status devices[XENUSB_MAX_PORTNR];
0074 
0075     /* Xen related staff */
0076     struct xenbus_device *xbdev;
0077     int urb_ring_ref;
0078     int conn_ring_ref;
0079     struct xenusb_urb_front_ring urb_ring;
0080     struct xenusb_conn_front_ring conn_ring;
0081 
0082     unsigned int evtchn;
0083     unsigned int irq;
0084     struct usb_shadow shadow[XENUSB_URB_RING_SIZE];
0085     unsigned int shadow_free;
0086 
0087     bool error;
0088 };
0089 
0090 #define XENHCD_RING_JIFFIES (HZ/200)
0091 #define XENHCD_SCAN_JIFFIES 1
0092 
0093 enum xenhcd_timer_action {
0094     TIMER_RING_WATCHDOG,
0095     TIMER_SCAN_PENDING_URBS,
0096 };
0097 
0098 static struct kmem_cache *xenhcd_urbp_cachep;
0099 
0100 static inline struct xenhcd_info *xenhcd_hcd_to_info(struct usb_hcd *hcd)
0101 {
0102     return (struct xenhcd_info *)hcd->hcd_priv;
0103 }
0104 
0105 static inline struct usb_hcd *xenhcd_info_to_hcd(struct xenhcd_info *info)
0106 {
0107     return container_of((void *)info, struct usb_hcd, hcd_priv);
0108 }
0109 
0110 static void xenhcd_set_error(struct xenhcd_info *info, const char *msg)
0111 {
0112     info->error = true;
0113 
0114     pr_alert("xen-hcd: protocol error: %s!\n", msg);
0115 }
0116 
0117 static inline void xenhcd_timer_action_done(struct xenhcd_info *info,
0118                         enum xenhcd_timer_action action)
0119 {
0120     clear_bit(action, &info->actions);
0121 }
0122 
0123 static void xenhcd_timer_action(struct xenhcd_info *info,
0124                 enum xenhcd_timer_action action)
0125 {
0126     if (timer_pending(&info->watchdog) &&
0127         test_bit(TIMER_SCAN_PENDING_URBS, &info->actions))
0128         return;
0129 
0130     if (!test_and_set_bit(action, &info->actions)) {
0131         unsigned long t;
0132 
0133         switch (action) {
0134         case TIMER_RING_WATCHDOG:
0135             t = XENHCD_RING_JIFFIES;
0136             break;
0137         default:
0138             t = XENHCD_SCAN_JIFFIES;
0139             break;
0140         }
0141         mod_timer(&info->watchdog, t + jiffies);
0142     }
0143 }
0144 
0145 /*
0146  * set virtual port connection status
0147  */
0148 static void xenhcd_set_connect_state(struct xenhcd_info *info, int portnum)
0149 {
0150     int port;
0151 
0152     port = portnum - 1;
0153     if (info->ports[port].status & USB_PORT_STAT_POWER) {
0154         switch (info->devices[port].speed) {
0155         case XENUSB_SPEED_NONE:
0156             info->ports[port].status &=
0157                 ~(USB_PORT_STAT_CONNECTION |
0158                   USB_PORT_STAT_ENABLE |
0159                   USB_PORT_STAT_LOW_SPEED |
0160                   USB_PORT_STAT_HIGH_SPEED |
0161                   USB_PORT_STAT_SUSPEND);
0162             break;
0163         case XENUSB_SPEED_LOW:
0164             info->ports[port].status |= USB_PORT_STAT_CONNECTION;
0165             info->ports[port].status |= USB_PORT_STAT_LOW_SPEED;
0166             break;
0167         case XENUSB_SPEED_FULL:
0168             info->ports[port].status |= USB_PORT_STAT_CONNECTION;
0169             break;
0170         case XENUSB_SPEED_HIGH:
0171             info->ports[port].status |= USB_PORT_STAT_CONNECTION;
0172             info->ports[port].status |= USB_PORT_STAT_HIGH_SPEED;
0173             break;
0174         default: /* error */
0175             return;
0176         }
0177         info->ports[port].status |= (USB_PORT_STAT_C_CONNECTION << 16);
0178     }
0179 }
0180 
0181 /*
0182  * set virtual device connection status
0183  */
0184 static int xenhcd_rhport_connect(struct xenhcd_info *info, __u8 portnum,
0185                  __u8 speed)
0186 {
0187     int port;
0188 
0189     if (portnum < 1 || portnum > info->rh_numports)
0190         return -EINVAL; /* invalid port number */
0191 
0192     port = portnum - 1;
0193     if (info->devices[port].speed != speed) {
0194         switch (speed) {
0195         case XENUSB_SPEED_NONE: /* disconnect */
0196             info->devices[port].status = USB_STATE_NOTATTACHED;
0197             break;
0198         case XENUSB_SPEED_LOW:
0199         case XENUSB_SPEED_FULL:
0200         case XENUSB_SPEED_HIGH:
0201             info->devices[port].status = USB_STATE_ATTACHED;
0202             break;
0203         default: /* error */
0204             return -EINVAL;
0205         }
0206         info->devices[port].speed = speed;
0207         info->ports[port].c_connection = true;
0208 
0209         xenhcd_set_connect_state(info, portnum);
0210     }
0211 
0212     return 0;
0213 }
0214 
0215 /*
0216  * SetPortFeature(PORT_SUSPENDED)
0217  */
0218 static void xenhcd_rhport_suspend(struct xenhcd_info *info, int portnum)
0219 {
0220     int port;
0221 
0222     port = portnum - 1;
0223     info->ports[port].status |= USB_PORT_STAT_SUSPEND;
0224     info->devices[port].status = USB_STATE_SUSPENDED;
0225 }
0226 
0227 /*
0228  * ClearPortFeature(PORT_SUSPENDED)
0229  */
0230 static void xenhcd_rhport_resume(struct xenhcd_info *info, int portnum)
0231 {
0232     int port;
0233 
0234     port = portnum - 1;
0235     if (info->ports[port].status & USB_PORT_STAT_SUSPEND) {
0236         info->ports[port].resuming = true;
0237         info->ports[port].timeout = jiffies + msecs_to_jiffies(20);
0238     }
0239 }
0240 
0241 /*
0242  * SetPortFeature(PORT_POWER)
0243  */
0244 static void xenhcd_rhport_power_on(struct xenhcd_info *info, int portnum)
0245 {
0246     int port;
0247 
0248     port = portnum - 1;
0249     if ((info->ports[port].status & USB_PORT_STAT_POWER) == 0) {
0250         info->ports[port].status |= USB_PORT_STAT_POWER;
0251         if (info->devices[port].status != USB_STATE_NOTATTACHED)
0252             info->devices[port].status = USB_STATE_POWERED;
0253         if (info->ports[port].c_connection)
0254             xenhcd_set_connect_state(info, portnum);
0255     }
0256 }
0257 
0258 /*
0259  * ClearPortFeature(PORT_POWER)
0260  * SetConfiguration(non-zero)
0261  * Power_Source_Off
0262  * Over-current
0263  */
0264 static void xenhcd_rhport_power_off(struct xenhcd_info *info, int portnum)
0265 {
0266     int port;
0267 
0268     port = portnum - 1;
0269     if (info->ports[port].status & USB_PORT_STAT_POWER) {
0270         info->ports[port].status = 0;
0271         if (info->devices[port].status != USB_STATE_NOTATTACHED)
0272             info->devices[port].status = USB_STATE_ATTACHED;
0273     }
0274 }
0275 
0276 /*
0277  * ClearPortFeature(PORT_ENABLE)
0278  */
0279 static void xenhcd_rhport_disable(struct xenhcd_info *info, int portnum)
0280 {
0281     int port;
0282 
0283     port = portnum - 1;
0284     info->ports[port].status &= ~USB_PORT_STAT_ENABLE;
0285     info->ports[port].status &= ~USB_PORT_STAT_SUSPEND;
0286     info->ports[port].resuming = false;
0287     if (info->devices[port].status != USB_STATE_NOTATTACHED)
0288         info->devices[port].status = USB_STATE_POWERED;
0289 }
0290 
0291 /*
0292  * SetPortFeature(PORT_RESET)
0293  */
0294 static void xenhcd_rhport_reset(struct xenhcd_info *info, int portnum)
0295 {
0296     int port;
0297 
0298     port = portnum - 1;
0299     info->ports[port].status &= ~(USB_PORT_STAT_ENABLE |
0300                       USB_PORT_STAT_LOW_SPEED |
0301                       USB_PORT_STAT_HIGH_SPEED);
0302     info->ports[port].status |= USB_PORT_STAT_RESET;
0303 
0304     if (info->devices[port].status != USB_STATE_NOTATTACHED)
0305         info->devices[port].status = USB_STATE_ATTACHED;
0306 
0307     /* 10msec reset signaling */
0308     info->ports[port].timeout = jiffies + msecs_to_jiffies(10);
0309 }
0310 
0311 #ifdef CONFIG_PM
0312 static int xenhcd_bus_suspend(struct usb_hcd *hcd)
0313 {
0314     struct xenhcd_info *info = xenhcd_hcd_to_info(hcd);
0315     int ret = 0;
0316     int i, ports;
0317 
0318     ports = info->rh_numports;
0319 
0320     spin_lock_irq(&info->lock);
0321     if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
0322         ret = -ESHUTDOWN;
0323     } else {
0324         /* suspend any active ports*/
0325         for (i = 1; i <= ports; i++)
0326             xenhcd_rhport_suspend(info, i);
0327     }
0328     spin_unlock_irq(&info->lock);
0329 
0330     del_timer_sync(&info->watchdog);
0331 
0332     return ret;
0333 }
0334 
0335 static int xenhcd_bus_resume(struct usb_hcd *hcd)
0336 {
0337     struct xenhcd_info *info = xenhcd_hcd_to_info(hcd);
0338     int ret = 0;
0339     int i, ports;
0340 
0341     ports = info->rh_numports;
0342 
0343     spin_lock_irq(&info->lock);
0344     if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
0345         ret = -ESHUTDOWN;
0346     } else {
0347         /* resume any suspended ports*/
0348         for (i = 1; i <= ports; i++)
0349             xenhcd_rhport_resume(info, i);
0350     }
0351     spin_unlock_irq(&info->lock);
0352 
0353     return ret;
0354 }
0355 #endif
0356 
0357 static void xenhcd_hub_descriptor(struct xenhcd_info *info,
0358                   struct usb_hub_descriptor *desc)
0359 {
0360     __u16 temp;
0361     int ports = info->rh_numports;
0362 
0363     desc->bDescriptorType = 0x29;
0364     desc->bPwrOn2PwrGood = 10; /* EHCI says 20ms max */
0365     desc->bHubContrCurrent = 0;
0366     desc->bNbrPorts = ports;
0367 
0368     /* size of DeviceRemovable and PortPwrCtrlMask fields */
0369     temp = 1 + (ports / 8);
0370     desc->bDescLength = 7 + 2 * temp;
0371 
0372     /* bitmaps for DeviceRemovable and PortPwrCtrlMask */
0373     memset(&desc->u.hs.DeviceRemovable[0], 0, temp);
0374     memset(&desc->u.hs.DeviceRemovable[temp], 0xff, temp);
0375 
0376     /* per-port over current reporting and no power switching */
0377     temp = 0x000a;
0378     desc->wHubCharacteristics = cpu_to_le16(temp);
0379 }
0380 
0381 /* port status change mask for hub_status_data */
0382 #define PORT_C_MASK ((USB_PORT_STAT_C_CONNECTION |      \
0383               USB_PORT_STAT_C_ENABLE |      \
0384               USB_PORT_STAT_C_SUSPEND |     \
0385               USB_PORT_STAT_C_OVERCURRENT |     \
0386               USB_PORT_STAT_C_RESET) << 16)
0387 
0388 /*
0389  * See USB 2.0 Spec, 11.12.4 Hub and Port Status Change Bitmap.
0390  * If port status changed, writes the bitmap to buf and return
0391  * that length(number of bytes).
0392  * If Nothing changed, return 0.
0393  */
0394 static int xenhcd_hub_status_data(struct usb_hcd *hcd, char *buf)
0395 {
0396     struct xenhcd_info *info = xenhcd_hcd_to_info(hcd);
0397     int ports;
0398     int i;
0399     unsigned long flags;
0400     int ret;
0401     int changed = 0;
0402 
0403     /* initialize the status to no-changes */
0404     ports = info->rh_numports;
0405     ret = 1 + (ports / 8);
0406     memset(buf, 0, ret);
0407 
0408     spin_lock_irqsave(&info->lock, flags);
0409 
0410     for (i = 0; i < ports; i++) {
0411         /* check status for each port */
0412         if (info->ports[i].status & PORT_C_MASK) {
0413             buf[(i + 1) / 8] |= 1 << (i + 1) % 8;
0414             changed = 1;
0415         }
0416     }
0417 
0418     if ((hcd->state == HC_STATE_SUSPENDED) && (changed == 1))
0419         usb_hcd_resume_root_hub(hcd);
0420 
0421     spin_unlock_irqrestore(&info->lock, flags);
0422 
0423     return changed ? ret : 0;
0424 }
0425 
0426 static int xenhcd_hub_control(struct usb_hcd *hcd, __u16 typeReq, __u16 wValue,
0427                   __u16 wIndex, char *buf, __u16 wLength)
0428 {
0429     struct xenhcd_info *info = xenhcd_hcd_to_info(hcd);
0430     int ports = info->rh_numports;
0431     unsigned long flags;
0432     int ret = 0;
0433     int i;
0434     int changed = 0;
0435 
0436     spin_lock_irqsave(&info->lock, flags);
0437     switch (typeReq) {
0438     case ClearHubFeature:
0439         /* ignore this request */
0440         break;
0441     case ClearPortFeature:
0442         if (!wIndex || wIndex > ports)
0443             goto error;
0444 
0445         switch (wValue) {
0446         case USB_PORT_FEAT_SUSPEND:
0447             xenhcd_rhport_resume(info, wIndex);
0448             break;
0449         case USB_PORT_FEAT_POWER:
0450             xenhcd_rhport_power_off(info, wIndex);
0451             break;
0452         case USB_PORT_FEAT_ENABLE:
0453             xenhcd_rhport_disable(info, wIndex);
0454             break;
0455         case USB_PORT_FEAT_C_CONNECTION:
0456             info->ports[wIndex - 1].c_connection = false;
0457             fallthrough;
0458         default:
0459             info->ports[wIndex - 1].status &= ~(1 << wValue);
0460             break;
0461         }
0462         break;
0463     case GetHubDescriptor:
0464         xenhcd_hub_descriptor(info, (struct usb_hub_descriptor *)buf);
0465         break;
0466     case GetHubStatus:
0467         /* always local power supply good and no over-current exists. */
0468         *(__le32 *)buf = cpu_to_le32(0);
0469         break;
0470     case GetPortStatus:
0471         if (!wIndex || wIndex > ports)
0472             goto error;
0473 
0474         wIndex--;
0475 
0476         /* resume completion */
0477         if (info->ports[wIndex].resuming &&
0478             time_after_eq(jiffies, info->ports[wIndex].timeout)) {
0479             info->ports[wIndex].status |=
0480                 USB_PORT_STAT_C_SUSPEND << 16;
0481             info->ports[wIndex].status &= ~USB_PORT_STAT_SUSPEND;
0482         }
0483 
0484         /* reset completion */
0485         if ((info->ports[wIndex].status & USB_PORT_STAT_RESET) != 0 &&
0486             time_after_eq(jiffies, info->ports[wIndex].timeout)) {
0487             info->ports[wIndex].status |=
0488                 USB_PORT_STAT_C_RESET << 16;
0489             info->ports[wIndex].status &= ~USB_PORT_STAT_RESET;
0490 
0491             if (info->devices[wIndex].status !=
0492                 USB_STATE_NOTATTACHED) {
0493                 info->ports[wIndex].status |=
0494                     USB_PORT_STAT_ENABLE;
0495                 info->devices[wIndex].status =
0496                     USB_STATE_DEFAULT;
0497             }
0498 
0499             switch (info->devices[wIndex].speed) {
0500             case XENUSB_SPEED_LOW:
0501                 info->ports[wIndex].status |=
0502                     USB_PORT_STAT_LOW_SPEED;
0503                 break;
0504             case XENUSB_SPEED_HIGH:
0505                 info->ports[wIndex].status |=
0506                     USB_PORT_STAT_HIGH_SPEED;
0507                 break;
0508             default:
0509                 break;
0510             }
0511         }
0512 
0513         *(__le32 *)buf = cpu_to_le32(info->ports[wIndex].status);
0514         break;
0515     case SetPortFeature:
0516         if (!wIndex || wIndex > ports)
0517             goto error;
0518 
0519         switch (wValue) {
0520         case USB_PORT_FEAT_POWER:
0521             xenhcd_rhport_power_on(info, wIndex);
0522             break;
0523         case USB_PORT_FEAT_RESET:
0524             xenhcd_rhport_reset(info, wIndex);
0525             break;
0526         case USB_PORT_FEAT_SUSPEND:
0527             xenhcd_rhport_suspend(info, wIndex);
0528             break;
0529         default:
0530             if (info->ports[wIndex-1].status & USB_PORT_STAT_POWER)
0531                 info->ports[wIndex-1].status |= (1 << wValue);
0532         }
0533         break;
0534 
0535     case SetHubFeature:
0536         /* not supported */
0537     default:
0538 error:
0539         ret = -EPIPE;
0540     }
0541     spin_unlock_irqrestore(&info->lock, flags);
0542 
0543     /* check status for each port */
0544     for (i = 0; i < ports; i++) {
0545         if (info->ports[i].status & PORT_C_MASK)
0546             changed = 1;
0547     }
0548     if (changed)
0549         usb_hcd_poll_rh_status(hcd);
0550 
0551     return ret;
0552 }
0553 
0554 static void xenhcd_free_urb_priv(struct urb_priv *urbp)
0555 {
0556     urbp->urb->hcpriv = NULL;
0557     kmem_cache_free(xenhcd_urbp_cachep, urbp);
0558 }
0559 
0560 static inline unsigned int xenhcd_get_id_from_freelist(struct xenhcd_info *info)
0561 {
0562     unsigned int free;
0563 
0564     free = info->shadow_free;
0565     info->shadow_free = info->shadow[free].req.id;
0566     info->shadow[free].req.id = 0x0fff; /* debug */
0567     return free;
0568 }
0569 
0570 static inline void xenhcd_add_id_to_freelist(struct xenhcd_info *info,
0571                          unsigned int id)
0572 {
0573     info->shadow[id].req.id = info->shadow_free;
0574     info->shadow[id].urb = NULL;
0575     info->shadow_free = id;
0576 }
0577 
0578 static inline int xenhcd_count_pages(void *addr, int length)
0579 {
0580     unsigned long vaddr = (unsigned long)addr;
0581 
0582     return PFN_UP(vaddr + length) - PFN_DOWN(vaddr);
0583 }
0584 
0585 static void xenhcd_gnttab_map(struct xenhcd_info *info, void *addr, int length,
0586                   grant_ref_t *gref_head,
0587                   struct xenusb_request_segment *seg,
0588                   int nr_pages, int flags)
0589 {
0590     grant_ref_t ref;
0591     unsigned int offset;
0592     unsigned int len = length;
0593     unsigned int bytes;
0594     int i;
0595 
0596     for (i = 0; i < nr_pages; i++) {
0597         offset = offset_in_page(addr);
0598 
0599         bytes = PAGE_SIZE - offset;
0600         if (bytes > len)
0601             bytes = len;
0602 
0603         ref = gnttab_claim_grant_reference(gref_head);
0604         gnttab_grant_foreign_access_ref(ref, info->xbdev->otherend_id,
0605                         virt_to_gfn(addr), flags);
0606         seg[i].gref = ref;
0607         seg[i].offset = (__u16)offset;
0608         seg[i].length = (__u16)bytes;
0609 
0610         addr += bytes;
0611         len -= bytes;
0612     }
0613 }
0614 
0615 static __u32 xenhcd_pipe_urb_to_xenusb(__u32 urb_pipe, __u8 port)
0616 {
0617     static __u32 pipe;
0618 
0619     pipe = usb_pipedevice(urb_pipe) << XENUSB_PIPE_DEV_SHIFT;
0620     pipe |= usb_pipeendpoint(urb_pipe) << XENUSB_PIPE_EP_SHIFT;
0621     if (usb_pipein(urb_pipe))
0622         pipe |= XENUSB_PIPE_DIR;
0623     switch (usb_pipetype(urb_pipe)) {
0624     case PIPE_ISOCHRONOUS:
0625         pipe |= XENUSB_PIPE_TYPE_ISOC << XENUSB_PIPE_TYPE_SHIFT;
0626         break;
0627     case PIPE_INTERRUPT:
0628         pipe |= XENUSB_PIPE_TYPE_INT << XENUSB_PIPE_TYPE_SHIFT;
0629         break;
0630     case PIPE_CONTROL:
0631         pipe |= XENUSB_PIPE_TYPE_CTRL << XENUSB_PIPE_TYPE_SHIFT;
0632         break;
0633     case PIPE_BULK:
0634         pipe |= XENUSB_PIPE_TYPE_BULK << XENUSB_PIPE_TYPE_SHIFT;
0635         break;
0636     }
0637     pipe = xenusb_setportnum_pipe(pipe, port);
0638 
0639     return pipe;
0640 }
0641 
0642 static int xenhcd_map_urb_for_request(struct xenhcd_info *info, struct urb *urb,
0643                       struct xenusb_urb_request *req)
0644 {
0645     grant_ref_t gref_head;
0646     int nr_buff_pages = 0;
0647     int nr_isodesc_pages = 0;
0648     int nr_grants = 0;
0649 
0650     if (urb->transfer_buffer_length) {
0651         nr_buff_pages = xenhcd_count_pages(urb->transfer_buffer,
0652                         urb->transfer_buffer_length);
0653 
0654         if (usb_pipeisoc(urb->pipe))
0655             nr_isodesc_pages = xenhcd_count_pages(
0656                 &urb->iso_frame_desc[0],
0657                 sizeof(struct usb_iso_packet_descriptor) *
0658                 urb->number_of_packets);
0659 
0660         nr_grants = nr_buff_pages + nr_isodesc_pages;
0661         if (nr_grants > XENUSB_MAX_SEGMENTS_PER_REQUEST) {
0662             pr_err("xenhcd: error: %d grants\n", nr_grants);
0663             return -E2BIG;
0664         }
0665 
0666         if (gnttab_alloc_grant_references(nr_grants, &gref_head)) {
0667             pr_err("xenhcd: gnttab_alloc_grant_references() error\n");
0668             return -ENOMEM;
0669         }
0670 
0671         xenhcd_gnttab_map(info, urb->transfer_buffer,
0672                   urb->transfer_buffer_length, &gref_head,
0673                   &req->seg[0], nr_buff_pages,
0674                   usb_pipein(urb->pipe) ? 0 : GTF_readonly);
0675     }
0676 
0677     req->pipe = xenhcd_pipe_urb_to_xenusb(urb->pipe, urb->dev->portnum);
0678     req->transfer_flags = 0;
0679     if (urb->transfer_flags & URB_SHORT_NOT_OK)
0680         req->transfer_flags |= XENUSB_SHORT_NOT_OK;
0681     req->buffer_length = urb->transfer_buffer_length;
0682     req->nr_buffer_segs = nr_buff_pages;
0683 
0684     switch (usb_pipetype(urb->pipe)) {
0685     case PIPE_ISOCHRONOUS:
0686         req->u.isoc.interval = urb->interval;
0687         req->u.isoc.start_frame = urb->start_frame;
0688         req->u.isoc.number_of_packets = urb->number_of_packets;
0689         req->u.isoc.nr_frame_desc_segs = nr_isodesc_pages;
0690 
0691         xenhcd_gnttab_map(info, &urb->iso_frame_desc[0],
0692                   sizeof(struct usb_iso_packet_descriptor) *
0693                   urb->number_of_packets,
0694                   &gref_head, &req->seg[nr_buff_pages],
0695                   nr_isodesc_pages, 0);
0696         break;
0697     case PIPE_INTERRUPT:
0698         req->u.intr.interval = urb->interval;
0699         break;
0700     case PIPE_CONTROL:
0701         if (urb->setup_packet)
0702             memcpy(req->u.ctrl, urb->setup_packet, 8);
0703         break;
0704     case PIPE_BULK:
0705         break;
0706     default:
0707         break;
0708     }
0709 
0710     if (nr_grants)
0711         gnttab_free_grant_references(gref_head);
0712 
0713     return 0;
0714 }
0715 
0716 static void xenhcd_gnttab_done(struct xenhcd_info *info, unsigned int id)
0717 {
0718     struct usb_shadow *shadow = info->shadow + id;
0719     int nr_segs = 0;
0720     int i;
0721 
0722     if (!shadow->in_flight) {
0723         xenhcd_set_error(info, "Illegal request id");
0724         return;
0725     }
0726     shadow->in_flight = false;
0727 
0728     nr_segs = shadow->req.nr_buffer_segs;
0729 
0730     if (xenusb_pipeisoc(shadow->req.pipe))
0731         nr_segs += shadow->req.u.isoc.nr_frame_desc_segs;
0732 
0733     for (i = 0; i < nr_segs; i++) {
0734         if (!gnttab_try_end_foreign_access(shadow->req.seg[i].gref))
0735             xenhcd_set_error(info, "backend didn't release grant");
0736     }
0737 
0738     shadow->req.nr_buffer_segs = 0;
0739     shadow->req.u.isoc.nr_frame_desc_segs = 0;
0740 }
0741 
0742 static int xenhcd_translate_status(int status)
0743 {
0744     switch (status) {
0745     case XENUSB_STATUS_OK:
0746         return 0;
0747     case XENUSB_STATUS_NODEV:
0748         return -ENODEV;
0749     case XENUSB_STATUS_INVAL:
0750         return -EINVAL;
0751     case XENUSB_STATUS_STALL:
0752         return -EPIPE;
0753     case XENUSB_STATUS_IOERROR:
0754         return -EPROTO;
0755     case XENUSB_STATUS_BABBLE:
0756         return -EOVERFLOW;
0757     default:
0758         return -ESHUTDOWN;
0759     }
0760 }
0761 
0762 static void xenhcd_giveback_urb(struct xenhcd_info *info, struct urb *urb,
0763                 int status)
0764 {
0765     struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
0766     int priv_status = urbp->status;
0767 
0768     list_del_init(&urbp->list);
0769     xenhcd_free_urb_priv(urbp);
0770 
0771     if (urb->status == -EINPROGRESS)
0772         urb->status = xenhcd_translate_status(status);
0773 
0774     spin_unlock(&info->lock);
0775     usb_hcd_giveback_urb(xenhcd_info_to_hcd(info), urb,
0776                  priv_status <= 0 ? priv_status : urb->status);
0777     spin_lock(&info->lock);
0778 }
0779 
0780 static int xenhcd_do_request(struct xenhcd_info *info, struct urb_priv *urbp)
0781 {
0782     struct xenusb_urb_request *req;
0783     struct urb *urb = urbp->urb;
0784     unsigned int id;
0785     int notify;
0786     int ret;
0787 
0788     id = xenhcd_get_id_from_freelist(info);
0789     req = &info->shadow[id].req;
0790     req->id = id;
0791 
0792     if (unlikely(urbp->unlinked)) {
0793         req->u.unlink.unlink_id = urbp->req_id;
0794         req->pipe = xenusb_setunlink_pipe(xenhcd_pipe_urb_to_xenusb(
0795                          urb->pipe, urb->dev->portnum));
0796         urbp->unlink_req_id = id;
0797     } else {
0798         ret = xenhcd_map_urb_for_request(info, urb, req);
0799         if (ret) {
0800             xenhcd_add_id_to_freelist(info, id);
0801             return ret;
0802         }
0803         urbp->req_id = id;
0804     }
0805 
0806     req = RING_GET_REQUEST(&info->urb_ring, info->urb_ring.req_prod_pvt);
0807     *req = info->shadow[id].req;
0808 
0809     info->urb_ring.req_prod_pvt++;
0810     info->shadow[id].urb = urb;
0811     info->shadow[id].in_flight = true;
0812 
0813     RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&info->urb_ring, notify);
0814     if (notify)
0815         notify_remote_via_irq(info->irq);
0816 
0817     return 0;
0818 }
0819 
0820 static void xenhcd_kick_pending_urbs(struct xenhcd_info *info)
0821 {
0822     struct urb_priv *urbp;
0823 
0824     while (!list_empty(&info->pending_submit_list)) {
0825         if (RING_FULL(&info->urb_ring)) {
0826             xenhcd_timer_action(info, TIMER_RING_WATCHDOG);
0827             return;
0828         }
0829 
0830         urbp = list_entry(info->pending_submit_list.next,
0831                   struct urb_priv, list);
0832         if (!xenhcd_do_request(info, urbp))
0833             list_move_tail(&urbp->list, &info->in_progress_list);
0834         else
0835             xenhcd_giveback_urb(info, urbp->urb, -ESHUTDOWN);
0836     }
0837     xenhcd_timer_action_done(info, TIMER_SCAN_PENDING_URBS);
0838 }
0839 
0840 /*
0841  * caller must lock info->lock
0842  */
0843 static void xenhcd_cancel_all_enqueued_urbs(struct xenhcd_info *info)
0844 {
0845     struct urb_priv *urbp, *tmp;
0846     int req_id;
0847 
0848     list_for_each_entry_safe(urbp, tmp, &info->in_progress_list, list) {
0849         req_id = urbp->req_id;
0850         if (!urbp->unlinked) {
0851             xenhcd_gnttab_done(info, req_id);
0852             if (info->error)
0853                 return;
0854             if (urbp->urb->status == -EINPROGRESS)
0855                 /* not dequeued */
0856                 xenhcd_giveback_urb(info, urbp->urb,
0857                             -ESHUTDOWN);
0858             else    /* dequeued */
0859                 xenhcd_giveback_urb(info, urbp->urb,
0860                             urbp->urb->status);
0861         }
0862         info->shadow[req_id].urb = NULL;
0863     }
0864 
0865     list_for_each_entry_safe(urbp, tmp, &info->pending_submit_list, list)
0866         xenhcd_giveback_urb(info, urbp->urb, -ESHUTDOWN);
0867 }
0868 
0869 /*
0870  * caller must lock info->lock
0871  */
0872 static void xenhcd_giveback_unlinked_urbs(struct xenhcd_info *info)
0873 {
0874     struct urb_priv *urbp, *tmp;
0875 
0876     list_for_each_entry_safe(urbp, tmp, &info->giveback_waiting_list, list)
0877         xenhcd_giveback_urb(info, urbp->urb, urbp->urb->status);
0878 }
0879 
0880 static int xenhcd_submit_urb(struct xenhcd_info *info, struct urb_priv *urbp)
0881 {
0882     int ret;
0883 
0884     if (RING_FULL(&info->urb_ring)) {
0885         list_add_tail(&urbp->list, &info->pending_submit_list);
0886         xenhcd_timer_action(info, TIMER_RING_WATCHDOG);
0887         return 0;
0888     }
0889 
0890     if (!list_empty(&info->pending_submit_list)) {
0891         list_add_tail(&urbp->list, &info->pending_submit_list);
0892         xenhcd_timer_action(info, TIMER_SCAN_PENDING_URBS);
0893         return 0;
0894     }
0895 
0896     ret = xenhcd_do_request(info, urbp);
0897     if (ret == 0)
0898         list_add_tail(&urbp->list, &info->in_progress_list);
0899 
0900     return ret;
0901 }
0902 
0903 static int xenhcd_unlink_urb(struct xenhcd_info *info, struct urb_priv *urbp)
0904 {
0905     int ret;
0906 
0907     /* already unlinked? */
0908     if (urbp->unlinked)
0909         return -EBUSY;
0910 
0911     urbp->unlinked = true;
0912 
0913     /* the urb is still in pending_submit queue */
0914     if (urbp->req_id == ~0) {
0915         list_move_tail(&urbp->list, &info->giveback_waiting_list);
0916         xenhcd_timer_action(info, TIMER_SCAN_PENDING_URBS);
0917         return 0;
0918     }
0919 
0920     /* send unlink request to backend */
0921     if (RING_FULL(&info->urb_ring)) {
0922         list_move_tail(&urbp->list, &info->pending_unlink_list);
0923         xenhcd_timer_action(info, TIMER_RING_WATCHDOG);
0924         return 0;
0925     }
0926 
0927     if (!list_empty(&info->pending_unlink_list)) {
0928         list_move_tail(&urbp->list, &info->pending_unlink_list);
0929         xenhcd_timer_action(info, TIMER_SCAN_PENDING_URBS);
0930         return 0;
0931     }
0932 
0933     ret = xenhcd_do_request(info, urbp);
0934     if (ret == 0)
0935         list_move_tail(&urbp->list, &info->in_progress_list);
0936 
0937     return ret;
0938 }
0939 
0940 static void xenhcd_res_to_urb(struct xenhcd_info *info,
0941                   struct xenusb_urb_response *res, struct urb *urb)
0942 {
0943     if (unlikely(!urb))
0944         return;
0945 
0946     if (res->actual_length > urb->transfer_buffer_length)
0947         urb->actual_length = urb->transfer_buffer_length;
0948     else if (res->actual_length < 0)
0949         urb->actual_length = 0;
0950     else
0951         urb->actual_length = res->actual_length;
0952     urb->error_count = res->error_count;
0953     urb->start_frame = res->start_frame;
0954     xenhcd_giveback_urb(info, urb, res->status);
0955 }
0956 
0957 static int xenhcd_urb_request_done(struct xenhcd_info *info,
0958                    unsigned int *eoiflag)
0959 {
0960     struct xenusb_urb_response res;
0961     RING_IDX i, rp;
0962     __u16 id;
0963     int more_to_do = 0;
0964     unsigned long flags;
0965 
0966     spin_lock_irqsave(&info->lock, flags);
0967 
0968     rp = info->urb_ring.sring->rsp_prod;
0969     if (RING_RESPONSE_PROD_OVERFLOW(&info->urb_ring, rp)) {
0970         xenhcd_set_error(info, "Illegal index on urb-ring");
0971         goto err;
0972     }
0973     rmb(); /* ensure we see queued responses up to "rp" */
0974 
0975     for (i = info->urb_ring.rsp_cons; i != rp; i++) {
0976         RING_COPY_RESPONSE(&info->urb_ring, i, &res);
0977         id = res.id;
0978         if (id >= XENUSB_URB_RING_SIZE) {
0979             xenhcd_set_error(info, "Illegal data on urb-ring");
0980             goto err;
0981         }
0982 
0983         if (likely(xenusb_pipesubmit(info->shadow[id].req.pipe))) {
0984             xenhcd_gnttab_done(info, id);
0985             if (info->error)
0986                 goto err;
0987             xenhcd_res_to_urb(info, &res, info->shadow[id].urb);
0988         }
0989 
0990         xenhcd_add_id_to_freelist(info, id);
0991 
0992         *eoiflag = 0;
0993     }
0994     info->urb_ring.rsp_cons = i;
0995 
0996     if (i != info->urb_ring.req_prod_pvt)
0997         RING_FINAL_CHECK_FOR_RESPONSES(&info->urb_ring, more_to_do);
0998     else
0999         info->urb_ring.sring->rsp_event = i + 1;
1000 
1001     spin_unlock_irqrestore(&info->lock, flags);
1002 
1003     return more_to_do;
1004 
1005  err:
1006     spin_unlock_irqrestore(&info->lock, flags);
1007     return 0;
1008 }
1009 
1010 static int xenhcd_conn_notify(struct xenhcd_info *info, unsigned int *eoiflag)
1011 {
1012     struct xenusb_conn_response res;
1013     struct xenusb_conn_request *req;
1014     RING_IDX rc, rp;
1015     __u16 id;
1016     __u8 portnum, speed;
1017     int more_to_do = 0;
1018     int notify;
1019     int port_changed = 0;
1020     unsigned long flags;
1021 
1022     spin_lock_irqsave(&info->lock, flags);
1023 
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);
1029         return 0;
1030     }
1031     rmb(); /* ensure we see queued responses up to "rp" */
1032 
1033     while (rc != rp) {
1034         RING_COPY_RESPONSE(&info->conn_ring, rc, &res);
1035         id = res.id;
1036         portnum = res.portnum;
1037         speed = res.speed;
1038         info->conn_ring.rsp_cons = ++rc;
1039 
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);
1043             return 0;
1044         }
1045 
1046         if (info->ports[portnum - 1].c_connection)
1047             port_changed = 1;
1048 
1049         barrier();
1050 
1051         req = RING_GET_REQUEST(&info->conn_ring,
1052                        info->conn_ring.req_prod_pvt);
1053         req->id = id;
1054         info->conn_ring.req_prod_pvt++;
1055 
1056         *eoiflag = 0;
1057     }
1058 
1059     if (rc != info->conn_ring.req_prod_pvt)
1060         RING_FINAL_CHECK_FOR_RESPONSES(&info->conn_ring, more_to_do);
1061     else
1062         info->conn_ring.sring->rsp_event = rc + 1;
1063 
1064     RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&info->conn_ring, notify);
1065     if (notify)
1066         notify_remote_via_irq(info->irq);
1067 
1068     spin_unlock_irqrestore(&info->lock, flags);
1069 
1070     if (port_changed)
1071         usb_hcd_poll_rh_status(xenhcd_info_to_hcd(info));
1072 
1073     return more_to_do;
1074 }
1075 
1076 static irqreturn_t xenhcd_int(int irq, void *dev_id)
1077 {
1078     struct xenhcd_info *info = (struct xenhcd_info *)dev_id;
1079     unsigned int eoiflag = XEN_EOI_FLAG_SPURIOUS;
1080 
1081     if (unlikely(info->error)) {
1082         xen_irq_lateeoi(irq, XEN_EOI_FLAG_SPURIOUS);
1083         return IRQ_HANDLED;
1084     }
1085 
1086     while (xenhcd_urb_request_done(info, &eoiflag) |
1087            xenhcd_conn_notify(info, &eoiflag))
1088         /* Yield point for this unbounded loop. */
1089         cond_resched();
1090 
1091     xen_irq_lateeoi(irq, eoiflag);
1092     return IRQ_HANDLED;
1093 }
1094 
1095 static void xenhcd_destroy_rings(struct xenhcd_info *info)
1096 {
1097     if (info->irq)
1098         unbind_from_irqhandler(info->irq, info);
1099     info->irq = 0;
1100 
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);
1105 }
1106 
1107 static int xenhcd_setup_rings(struct xenbus_device *dev,
1108                   struct xenhcd_info *info)
1109 {
1110     struct xenusb_urb_sring *urb_sring;
1111     struct xenusb_conn_sring *conn_sring;
1112     int err;
1113 
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);
1117     if (err) {
1118         xenbus_dev_fatal(dev, err, "allocating urb ring");
1119         return err;
1120     }
1121     XEN_FRONT_RING_INIT(&info->urb_ring, urb_sring, PAGE_SIZE);
1122 
1123     err = xenbus_setup_ring(dev, GFP_NOIO | __GFP_HIGH,
1124                 (void **)&conn_sring, 1, &info->conn_ring_ref);
1125     if (err) {
1126         xenbus_dev_fatal(dev, err, "allocating conn ring");
1127         goto fail;
1128     }
1129     XEN_FRONT_RING_INIT(&info->conn_ring, conn_sring, PAGE_SIZE);
1130 
1131     err = xenbus_alloc_evtchn(dev, &info->evtchn);
1132     if (err) {
1133         xenbus_dev_fatal(dev, err, "xenbus_alloc_evtchn");
1134         goto fail;
1135     }
1136 
1137     err = bind_evtchn_to_irq_lateeoi(info->evtchn);
1138     if (err <= 0) {
1139         xenbus_dev_fatal(dev, err, "bind_evtchn_to_irq_lateeoi");
1140         goto fail;
1141     }
1142 
1143     info->irq = err;
1144 
1145     err = request_threaded_irq(info->irq, NULL, xenhcd_int,
1146                    IRQF_ONESHOT, "xenhcd", info);
1147     if (err) {
1148         xenbus_dev_fatal(dev, err, "request_threaded_irq");
1149         goto free_irq;
1150     }
1151 
1152     return 0;
1153 
1154 free_irq:
1155     unbind_from_irqhandler(info->irq, info);
1156 fail:
1157     xenhcd_destroy_rings(info);
1158     return err;
1159 }
1160 
1161 static int xenhcd_talk_to_backend(struct xenbus_device *dev,
1162                   struct xenhcd_info *info)
1163 {
1164     const char *message;
1165     struct xenbus_transaction xbt;
1166     int err;
1167 
1168     err = xenhcd_setup_rings(dev, info);
1169     if (err)
1170         return err;
1171 
1172 again:
1173     err = xenbus_transaction_start(&xbt);
1174     if (err) {
1175         xenbus_dev_fatal(dev, err, "starting transaction");
1176         goto destroy_ring;
1177     }
1178 
1179     err = xenbus_printf(xbt, dev->nodename, "urb-ring-ref", "%u",
1180                 info->urb_ring_ref);
1181     if (err) {
1182         message = "writing urb-ring-ref";
1183         goto abort_transaction;
1184     }
1185 
1186     err = xenbus_printf(xbt, dev->nodename, "conn-ring-ref", "%u",
1187                 info->conn_ring_ref);
1188     if (err) {
1189         message = "writing conn-ring-ref";
1190         goto abort_transaction;
1191     }
1192 
1193     err = xenbus_printf(xbt, dev->nodename, "event-channel", "%u",
1194                 info->evtchn);
1195     if (err) {
1196         message = "writing event-channel";
1197         goto abort_transaction;
1198     }
1199 
1200     err = xenbus_transaction_end(xbt, 0);
1201     if (err) {
1202         if (err == -EAGAIN)
1203             goto again;
1204         xenbus_dev_fatal(dev, err, "completing transaction");
1205         goto destroy_ring;
1206     }
1207 
1208     return 0;
1209 
1210 abort_transaction:
1211     xenbus_transaction_end(xbt, 1);
1212     xenbus_dev_fatal(dev, err, "%s", message);
1213 
1214 destroy_ring:
1215     xenhcd_destroy_rings(info);
1216 
1217     return err;
1218 }
1219 
1220 static int xenhcd_connect(struct xenbus_device *dev)
1221 {
1222     struct xenhcd_info *info = dev_get_drvdata(&dev->dev);
1223     struct xenusb_conn_request *req;
1224     int idx, err;
1225     int notify;
1226     char name[TASK_COMM_LEN];
1227     struct usb_hcd *hcd;
1228 
1229     hcd = xenhcd_info_to_hcd(info);
1230     snprintf(name, TASK_COMM_LEN, "xenhcd.%d", hcd->self.busnum);
1231 
1232     err = xenhcd_talk_to_backend(dev, info);
1233     if (err)
1234         return err;
1235 
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);
1239         req->id = idx;
1240     }
1241     info->conn_ring.req_prod_pvt = idx;
1242 
1243     RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&info->conn_ring, notify);
1244     if (notify)
1245         notify_remote_via_irq(info->irq);
1246 
1247     return 0;
1248 }
1249 
1250 static void xenhcd_disconnect(struct xenbus_device *dev)
1251 {
1252     struct xenhcd_info *info = dev_get_drvdata(&dev->dev);
1253     struct usb_hcd *hcd = xenhcd_info_to_hcd(info);
1254 
1255     usb_remove_hcd(hcd);
1256     xenbus_frontend_closed(dev);
1257 }
1258 
1259 static void xenhcd_watchdog(struct timer_list *timer)
1260 {
1261     struct xenhcd_info *info = from_timer(info, timer, watchdog);
1262     unsigned long flags;
1263 
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);
1269     }
1270     spin_unlock_irqrestore(&info->lock, flags);
1271 }
1272 
1273 /*
1274  * one-time HC init
1275  */
1276 static int xenhcd_setup(struct usb_hcd *hcd)
1277 {
1278     struct xenhcd_info *info = xenhcd_hcd_to_info(hcd);
1279 
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);
1286 
1287     hcd->has_tt = (hcd->driver->flags & HCD_MASK) != HCD_USB11;
1288 
1289     return 0;
1290 }
1291 
1292 /*
1293  * start HC running
1294  */
1295 static int xenhcd_run(struct usb_hcd *hcd)
1296 {
1297     hcd->uses_new_polling = 1;
1298     clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
1299     hcd->state = HC_STATE_RUNNING;
1300     return 0;
1301 }
1302 
1303 /*
1304  * stop running HC
1305  */
1306 static void xenhcd_stop(struct usb_hcd *hcd)
1307 {
1308     struct xenhcd_info *info = xenhcd_hcd_to_info(hcd);
1309 
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);
1317 }
1318 
1319 /*
1320  * called as .urb_enqueue()
1321  * non-error returns are promise to giveback the urb later
1322  */
1323 static int xenhcd_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
1324                   gfp_t mem_flags)
1325 {
1326     struct xenhcd_info *info = xenhcd_hcd_to_info(hcd);
1327     struct urb_priv *urbp;
1328     unsigned long flags;
1329     int ret;
1330 
1331     if (unlikely(info->error))
1332         return -ESHUTDOWN;
1333 
1334     urbp = kmem_cache_zalloc(xenhcd_urbp_cachep, mem_flags);
1335     if (!urbp)
1336         return -ENOMEM;
1337 
1338     spin_lock_irqsave(&info->lock, flags);
1339 
1340     urbp->urb = urb;
1341     urb->hcpriv = urbp;
1342     urbp->req_id = ~0;
1343     urbp->unlink_req_id = ~0;
1344     INIT_LIST_HEAD(&urbp->list);
1345     urbp->status = 1;
1346     urb->unlinked = false;
1347 
1348     ret = xenhcd_submit_urb(info, urbp);
1349 
1350     if (ret)
1351         xenhcd_free_urb_priv(urbp);
1352 
1353     spin_unlock_irqrestore(&info->lock, flags);
1354 
1355     return ret;
1356 }
1357 
1358 /*
1359  * called as .urb_dequeue()
1360  */
1361 static int xenhcd_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
1362 {
1363     struct xenhcd_info *info = xenhcd_hcd_to_info(hcd);
1364     struct urb_priv *urbp;
1365     unsigned long flags;
1366     int ret = 0;
1367 
1368     spin_lock_irqsave(&info->lock, flags);
1369 
1370     urbp = urb->hcpriv;
1371     if (urbp) {
1372         urbp->status = status;
1373         ret = xenhcd_unlink_urb(info, urbp);
1374     }
1375 
1376     spin_unlock_irqrestore(&info->lock, flags);
1377 
1378     return ret;
1379 }
1380 
1381 /*
1382  * called from usb_get_current_frame_number(),
1383  * but, almost all drivers not use such function.
1384  */
1385 static int xenhcd_get_frame(struct usb_hcd *hcd)
1386 {
1387     /* it means error, but probably no problem :-) */
1388     return 0;
1389 }
1390 
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),
1395     .flags = HCD_USB2,
1396 
1397     /* basic HC lifecycle operations */
1398     .reset = xenhcd_setup,
1399     .start = xenhcd_run,
1400     .stop = xenhcd_stop,
1401 
1402     /* managing urb I/O */
1403     .urb_enqueue = xenhcd_urb_enqueue,
1404     .urb_dequeue = xenhcd_urb_dequeue,
1405     .get_frame_number = xenhcd_get_frame,
1406 
1407     /* root hub operations */
1408     .hub_status_data = xenhcd_hub_status_data,
1409     .hub_control = xenhcd_hub_control,
1410 #ifdef CONFIG_PM
1411     .bus_suspend = xenhcd_bus_suspend,
1412     .bus_resume = xenhcd_bus_resume,
1413 #endif
1414 };
1415 
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),
1420     .flags = HCD_USB11,
1421 
1422     /* basic HC lifecycle operations */
1423     .reset = xenhcd_setup,
1424     .start = xenhcd_run,
1425     .stop = xenhcd_stop,
1426 
1427     /* managing urb I/O */
1428     .urb_enqueue = xenhcd_urb_enqueue,
1429     .urb_dequeue = xenhcd_urb_dequeue,
1430     .get_frame_number = xenhcd_get_frame,
1431 
1432     /* root hub operations */
1433     .hub_status_data = xenhcd_hub_status_data,
1434     .hub_control = xenhcd_hub_control,
1435 #ifdef CONFIG_PM
1436     .bus_suspend = xenhcd_bus_suspend,
1437     .bus_resume = xenhcd_bus_resume,
1438 #endif
1439 };
1440 
1441 static struct usb_hcd *xenhcd_create_hcd(struct xenbus_device *dev)
1442 {
1443     int i;
1444     int err = 0;
1445     int num_ports;
1446     int usb_ver;
1447     struct usb_hcd *hcd = NULL;
1448     struct xenhcd_info *info;
1449 
1450     err = xenbus_scanf(XBT_NIL, dev->otherend, "num-ports", "%d",
1451                &num_ports);
1452     if (err != 1) {
1453         xenbus_dev_fatal(dev, err, "reading num-ports");
1454         return ERR_PTR(-EINVAL);
1455     }
1456     if (num_ports < 1 || num_ports > XENUSB_MAX_PORTNR) {
1457         xenbus_dev_fatal(dev, err, "invalid num-ports");
1458         return ERR_PTR(-EINVAL);
1459     }
1460 
1461     err = xenbus_scanf(XBT_NIL, dev->otherend, "usb-ver", "%d", &usb_ver);
1462     if (err != 1) {
1463         xenbus_dev_fatal(dev, err, "reading usb-ver");
1464         return ERR_PTR(-EINVAL);
1465     }
1466     switch (usb_ver) {
1467     case XENUSB_VER_USB11:
1468         hcd = usb_create_hcd(&xenhcd_usb11_hc_driver, &dev->dev,
1469                      dev_name(&dev->dev));
1470         break;
1471     case XENUSB_VER_USB20:
1472         hcd = usb_create_hcd(&xenhcd_usb20_hc_driver, &dev->dev,
1473                      dev_name(&dev->dev));
1474         break;
1475     default:
1476         xenbus_dev_fatal(dev, err, "invalid usb-ver");
1477         return ERR_PTR(-EINVAL);
1478     }
1479     if (!hcd) {
1480         xenbus_dev_fatal(dev, err,
1481                  "fail to allocate USB host controller");
1482         return ERR_PTR(-ENOMEM);
1483     }
1484 
1485     info = xenhcd_hcd_to_info(hcd);
1486     info->xbdev = dev;
1487     info->rh_numports = num_ports;
1488 
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;
1493     }
1494     info->shadow[XENUSB_URB_RING_SIZE - 1].req.id = 0x0fff;
1495 
1496     return hcd;
1497 }
1498 
1499 static void xenhcd_backend_changed(struct xenbus_device *dev,
1500                    enum xenbus_state backend_state)
1501 {
1502     switch (backend_state) {
1503     case XenbusStateInitialising:
1504     case XenbusStateReconfiguring:
1505     case XenbusStateReconfigured:
1506     case XenbusStateUnknown:
1507         break;
1508 
1509     case XenbusStateInitWait:
1510     case XenbusStateInitialised:
1511     case XenbusStateConnected:
1512         if (dev->state != XenbusStateInitialising)
1513             break;
1514         if (!xenhcd_connect(dev))
1515             xenbus_switch_state(dev, XenbusStateConnected);
1516         break;
1517 
1518     case XenbusStateClosed:
1519         if (dev->state == XenbusStateClosed)
1520             break;
1521         fallthrough;    /* Missed the backend's Closing state. */
1522     case XenbusStateClosing:
1523         xenhcd_disconnect(dev);
1524         break;
1525 
1526     default:
1527         xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
1528                  backend_state);
1529         break;
1530     }
1531 }
1532 
1533 static int xenhcd_remove(struct xenbus_device *dev)
1534 {
1535     struct xenhcd_info *info = dev_get_drvdata(&dev->dev);
1536     struct usb_hcd *hcd = xenhcd_info_to_hcd(info);
1537 
1538     xenhcd_destroy_rings(info);
1539     usb_put_hcd(hcd);
1540 
1541     return 0;
1542 }
1543 
1544 static int xenhcd_probe(struct xenbus_device *dev,
1545             const struct xenbus_device_id *id)
1546 {
1547     int err;
1548     struct usb_hcd *hcd;
1549     struct xenhcd_info *info;
1550 
1551     if (usb_disabled())
1552         return -ENODEV;
1553 
1554     hcd = xenhcd_create_hcd(dev);
1555     if (IS_ERR(hcd)) {
1556         err = PTR_ERR(hcd);
1557         xenbus_dev_fatal(dev, err,
1558                  "fail to create usb host controller");
1559         return err;
1560     }
1561 
1562     info = xenhcd_hcd_to_info(hcd);
1563     dev_set_drvdata(&dev->dev, info);
1564 
1565     err = usb_add_hcd(hcd, 0, 0);
1566     if (err) {
1567         xenbus_dev_fatal(dev, err, "fail to add USB host controller");
1568         usb_put_hcd(hcd);
1569         dev_set_drvdata(&dev->dev, NULL);
1570     }
1571 
1572     return err;
1573 }
1574 
1575 static const struct xenbus_device_id xenhcd_ids[] = {
1576     { "vusb" },
1577     { "" },
1578 };
1579 
1580 static struct xenbus_driver xenhcd_driver = {
1581     .ids            = xenhcd_ids,
1582     .probe          = xenhcd_probe,
1583     .otherend_changed   = xenhcd_backend_changed,
1584     .remove         = xenhcd_remove,
1585 };
1586 
1587 static int __init xenhcd_init(void)
1588 {
1589     if (!xen_domain())
1590         return -ENODEV;
1591 
1592     xenhcd_urbp_cachep = kmem_cache_create("xenhcd_urb_priv",
1593                     sizeof(struct urb_priv), 0, 0, NULL);
1594     if (!xenhcd_urbp_cachep) {
1595         pr_err("xenhcd failed to create kmem cache\n");
1596         return -ENOMEM;
1597     }
1598 
1599     return xenbus_register_frontend(&xenhcd_driver);
1600 }
1601 module_init(xenhcd_init);
1602 
1603 static void __exit xenhcd_exit(void)
1604 {
1605     kmem_cache_destroy(xenhcd_urbp_cachep);
1606     xenbus_unregister_driver(&xenhcd_driver);
1607 }
1608 module_exit(xenhcd_exit);
1609 
1610 MODULE_ALIAS("xen:vusb");
1611 MODULE_AUTHOR("Juergen Gross <jgross@suse.com>");
1612 MODULE_DESCRIPTION("Xen USB Virtual Host Controller driver (xen-hcd)");
1613 MODULE_LICENSE("Dual BSD/GPL");