ia64/xen-unstable

changeset 3837:a5092d2c64ea

bitkeeper revision 1.1201.1.3 (421297bd6K0RFSf9_qOCU30jMEudxg)

USB Frontend cleanups.

Signed-off-by: mark.williamson@cl.cam.ac.uk
author mwilli2@equilibrium.research
date Wed Feb 16 00:45:49 2005 +0000 (2005-02-16)
parents 3b3ed38bd02b
children a58b3f082755
files linux-2.6.10-xen-sparse/drivers/xen/usbfront/usbfront.c
line diff
     1.1 --- a/linux-2.6.10-xen-sparse/drivers/xen/usbfront/usbfront.c	Mon Feb 14 20:57:49 2005 +0000
     1.2 +++ b/linux-2.6.10-xen-sparse/drivers/xen/usbfront/usbfront.c	Wed Feb 16 00:45:49 2005 +0000
     1.3 @@ -6,21 +6,14 @@
     1.4   *
     1.5   * Based on Linux's uhci.c, original copyright notices are displayed
     1.6   * below.  Portions also (c) 2004 Intel Research Cambridge
     1.7 - * and (c) 2004 Mark Williamson
     1.8 + * and (c) 2004, 2005 Mark Williamson
     1.9   *
    1.10   * Contact <mark.williamson@cl.cam.ac.uk> or
    1.11   * <xen-devel@lists.sourceforge.net> regarding this code.
    1.12   *
    1.13   * Still to be (maybe) implemented:
    1.14 - * - multiple port
    1.15 - * - multiple interfaces
    1.16   * - migration / backend restart support?
    1.17 - * - unloading support
    1.18 - *
    1.19 - * Differences to a normal host controller:
    1.20 - * - the backend does most of the mucky stuff so we don't have to do various
    1.21 - *   things that are necessary for a normal host controller (e.g. FSBR).
    1.22 - * - we don't have any hardware, so status registers are simulated in software.
    1.23 + * - support for building / using as a module
    1.24   */
    1.25  
    1.26  /*
    1.27 @@ -55,13 +48,11 @@
    1.28  #include <linux/module.h>
    1.29  #include <linux/kernel.h>
    1.30  #include <linux/init.h>
    1.31 +#include <linux/sched.h>
    1.32  #include <linux/delay.h>
    1.33 -#include <linux/ioport.h>
    1.34 -#include <linux/sched.h>
    1.35  #include <linux/slab.h>
    1.36  #include <linux/smp_lock.h>
    1.37  #include <linux/errno.h>
    1.38 -#include <linux/unistd.h>
    1.39  #include <linux/interrupt.h>
    1.40  #include <linux/spinlock.h>
    1.41  #ifdef CONFIG_USB_DEBUG
    1.42 @@ -71,14 +62,11 @@
    1.43  #endif
    1.44  #include <linux/usb.h>
    1.45  
    1.46 -#include <asm/uaccess.h>
    1.47  #include <asm/irq.h>
    1.48  #include <asm/system.h>
    1.49  
    1.50  #include "xhci.h"
    1.51  
    1.52 -#include <linux/pm.h>
    1.53 -
    1.54  #include "../../../../../drivers/usb/hcd.h"
    1.55  
    1.56  #include <asm-xen/xen-public/io/usbif.h>
    1.57 @@ -89,8 +77,10 @@
    1.58   * Version Information
    1.59   */
    1.60  #define DRIVER_VERSION "v1.0"
    1.61 -#define DRIVER_AUTHOR "Linus 'Frodo Rabbit' Torvalds, Johannes Erdfelt, Randy Dunlap, Georg Acher, Deti Fliegl, Thomas Sailer, Roman Weissgaerber, Mark Williamson"
    1.62 -#define DRIVER_DESC "Xen Virtual USB Host Controller Interface driver"
    1.63 +#define DRIVER_AUTHOR "Linus 'Frodo Rabbit' Torvalds, Johannes Erdfelt, " \
    1.64 +                      "Randy Dunlap, Georg Acher, Deti Fliegl, " \
    1.65 +                      "Thomas Sailer, Roman Weissgaerber, Mark Williamson"
    1.66 +#define DRIVER_DESC "Xen Virtual USB Host Controller Interface"
    1.67  
    1.68  /*
    1.69   * debug = 0, no debugging messages
    1.70 @@ -107,26 +97,22 @@ MODULE_PARM_DESC(debug, "Debug level");
    1.71  static char *errbuf;
    1.72  #define ERRBUF_LEN    (PAGE_SIZE * 8)
    1.73  
    1.74 -static kmem_cache_t *xhci_up_cachep;	/* urb_priv */
    1.75 -
    1.76  static int rh_submit_urb(struct urb *urb);
    1.77  static int rh_unlink_urb(struct urb *urb);
    1.78 -//static int xhci_get_current_frame_number(struct usb_device *dev);
    1.79  static int xhci_unlink_urb(struct urb *urb);
    1.80 -static void xhci_unlink_generic(struct urb *urb);
    1.81  static void xhci_call_completion(struct urb *urb);
    1.82  static void xhci_drain_ring(void);
    1.83 +static void xhci_transfer_result(struct xhci *xhci, struct urb *urb);
    1.84 +static void xhci_finish_completion(void);
    1.85  
    1.86  #define MAX_URB_LOOP	2048		/* Maximum number of linked URB's */
    1.87  
    1.88 -static struct xhci *xhci;
    1.89 +static kmem_cache_t *xhci_up_cachep;	/* urb_priv cache */
    1.90 +static struct xhci *xhci;               /* XHCI structure for the interface */
    1.91  
    1.92 -enum { USBIF_STATE_CONNECTED = 2,
    1.93 -       USBIF_STATE_DISCONNECTED = 1,
    1.94 -       USBIF_STATE_CLOSED =0
    1.95 -};
    1.96 -
    1.97 -static int awaiting_reset = 0;
    1.98 +/******************************************************************************
    1.99 + * DEBUGGING
   1.100 + */
   1.101  
   1.102  #ifdef DEBUG
   1.103  
   1.104 @@ -145,13 +131,16 @@ static void dump_urb(struct urb *urb)
   1.105             "  bandwidth = %d\n"
   1.106             "  setup_packet = %p\n",
   1.107             urb, urb->hcpriv, urb->next, urb->dev, urb->pipe, urb->status,
   1.108 -           urb->transfer_flags, urb->transfer_buffer, urb->transfer_buffer_length,
   1.109 -           urb->actual_length, urb->bandwidth, urb->setup_packet);
   1.110 +           urb->transfer_flags, urb->transfer_buffer,
   1.111 +           urb->transfer_buffer_length, urb->actual_length, urb->bandwidth,
   1.112 +           urb->setup_packet);
   1.113      if ( urb->setup_packet != NULL )
   1.114          printk(KERN_DEBUG
   1.115                 "setup = { 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x }\n",
   1.116 -               urb->setup_packet[0], urb->setup_packet[1], urb->setup_packet[2], urb->setup_packet[3],
   1.117 -               urb->setup_packet[4], urb->setup_packet[5], urb->setup_packet[6], urb->setup_packet[7]);
   1.118 +               urb->setup_packet[0], urb->setup_packet[1],
   1.119 +               urb->setup_packet[2], urb->setup_packet[3],
   1.120 +               urb->setup_packet[4], urb->setup_packet[5],
   1.121 +               urb->setup_packet[6], urb->setup_packet[7]);
   1.122      printk(KERN_DEBUG "complete = %p\n"
   1.123             "interval = %d\n", urb->complete, urb->interval);
   1.124          
   1.125 @@ -178,6 +167,10 @@ static void xhci_show_resp(usbif_respons
   1.126  
   1.127  #endif /* DEBUG */
   1.128  
   1.129 +/******************************************************************************
   1.130 + * RING REQUEST HANDLING
   1.131 + */
   1.132 +
   1.133  /**
   1.134   * xhci_construct_isoc - add isochronous information to a request
   1.135   */
   1.136 @@ -205,6 +198,9 @@ static int xhci_construct_isoc(usbif_req
   1.137          return 0;
   1.138  }
   1.139  
   1.140 +/**
   1.141 + * xhci_queue_req - construct and queue request for an URB
   1.142 + */
   1.143  static int xhci_queue_req(struct urb *urb)
   1.144  {
   1.145          usbif_request_t *req;
   1.146 @@ -265,6 +261,9 @@ static int xhci_queue_req(struct urb *ur
   1.147          return -EINPROGRESS;
   1.148  }
   1.149  
   1.150 +/**
   1.151 + * xhci_queue_probe - queue a probe request for a particular port
   1.152 + */
   1.153  static inline usbif_request_t *xhci_queue_probe(usbif_vdev_t port)
   1.154  {
   1.155          usbif_request_t *req;
   1.156 @@ -272,33 +271,26 @@ static inline usbif_request_t *xhci_queu
   1.157  
   1.158  #if DEBUG
   1.159  	printk(KERN_DEBUG
   1.160 -               "queuing probe: req_prod = %d (@ 0x%lx), resp_prod = %d, resp_cons = %d\n",
   1.161 -	       usbif->req_prod, virt_to_machine(&usbif->req_prod),
   1.162 +               "queuing probe: req_prod = %d (@ 0x%lx), resp_prod = %d, "
   1.163 +               "resp_cons = %d\n", usbif->req_prod,
   1.164 +               virt_to_machine(&usbif->req_prod),
   1.165  	       usbif->resp_prod, xhci->usb_resp_cons);
   1.166  #endif
   1.167          
   1.168          if ( RING_FULL(USBIF_RING, usb_ring) )
   1.169          {
   1.170                  printk(KERN_WARNING
   1.171 -                       "xhci_queue_probe(): USB ring full, not queuing request\n");
   1.172 +                       "xhci_queue_probe(): ring full, not queuing request\n");
   1.173                  return NULL;
   1.174          }
   1.175  
   1.176          /* Stick something in the shared communications ring. */
   1.177          req = RING_GET_REQUEST(USBIF_RING, usb_ring, usb_ring->req_prod_pvt);
   1.178  
   1.179 +        memset(req, sizeof(*req), 0);
   1.180 +
   1.181          req->operation       = USBIF_OP_PROBE;
   1.182          req->port            = port;
   1.183 -        req->id              = 0;
   1.184 -        req->transfer_buffer = 0;
   1.185 -	req->devnum          = 0;
   1.186 -        req->direction       = 0;
   1.187 -	req->speed           = 0;
   1.188 -        req->pipe_type       = 0;
   1.189 -        req->length          = 0;
   1.190 -        req->transfer_flags  = 0;
   1.191 -	req->endpoint        = 0;
   1.192 -	req->speed           = 0;
   1.193  
   1.194          usb_ring->req_prod_pvt++;
   1.195          RING_PUSH_REQUESTS(USBIF_RING, usb_ring);
   1.196 @@ -308,6 +300,9 @@ static inline usbif_request_t *xhci_queu
   1.197          return req;
   1.198  }
   1.199  
   1.200 +/**
   1.201 + * xhci_port_reset - queue a reset request for a particular port
   1.202 + */
   1.203  static int xhci_port_reset(usbif_vdev_t port)
   1.204  {
   1.205          usbif_request_t *req;
   1.206 @@ -315,11 +310,13 @@ static int xhci_port_reset(usbif_vdev_t 
   1.207  
   1.208          /* We only reset one port at a time, so we only need one variable per
   1.209           * hub. */
   1.210 -        awaiting_reset = 1;
   1.211 +        xhci->awaiting_reset = 1;
   1.212          
   1.213          /* Stick something in the shared communications ring. */
   1.214  	req = RING_GET_REQUEST(USBIF_RING, usb_ring, usb_ring->req_prod_pvt);
   1.215  
   1.216 +        memset(req, sizeof(*req), 0);
   1.217 +
   1.218          req->operation       = USBIF_OP_RESET;
   1.219          req->port            = port;
   1.220          
   1.221 @@ -328,25 +325,167 @@ static int xhci_port_reset(usbif_vdev_t 
   1.222  
   1.223  	notify_via_evtchn(xhci->evtchn);
   1.224  
   1.225 -        while ( awaiting_reset > 0 )
   1.226 +        while ( xhci->awaiting_reset > 0 )
   1.227          {
   1.228                  mdelay(1);
   1.229                  xhci_drain_ring();
   1.230          }
   1.231  
   1.232 -        return awaiting_reset;
   1.233 +        return xhci->awaiting_reset;
   1.234  }
   1.235  
   1.236  
   1.237 -/*
   1.238 - * Only the USB core should call xhci_alloc_dev and xhci_free_dev
   1.239 +/******************************************************************************
   1.240 + * RING RESPONSE HANDLING
   1.241   */
   1.242 -static int xhci_alloc_dev(struct usb_device *dev)
   1.243 +
   1.244 +static void receive_usb_reset(usbif_response_t *resp)
   1.245  {
   1.246 -	return 0;
   1.247 +    xhci->awaiting_reset = resp->status;
   1.248 +    rmb();
   1.249 +    
   1.250  }
   1.251  
   1.252 -static int xhci_free_dev(struct usb_device *dev)
   1.253 +static void receive_usb_probe(usbif_response_t *resp)
   1.254 +{
   1.255 +    spin_lock(&xhci->rh.port_state_lock);
   1.256 +
   1.257 +    if ( resp->status > 0 )
   1.258 +    {
   1.259 +        if ( resp->status == 1 )
   1.260 +        {
   1.261 +            /* If theres a device there and there wasn't one before there must
   1.262 +             * have been a connection status change. */
   1.263 +            if( xhci->rh.ports[resp->data].cs == 0 )
   1.264 +	    {
   1.265 +                xhci->rh.ports[resp->data].cs = 1;
   1.266 +                xhci->rh.ports[resp->data].ccs = 1;
   1.267 +                xhci->rh.ports[resp->data].cs_chg = 1;
   1.268 +	    }
   1.269 +        }
   1.270 +        else
   1.271 +            printk(KERN_WARNING "receive_usb_probe(): unexpected status %d "
   1.272 +                   "for port %d\n", resp->status, resp->data);
   1.273 +    }
   1.274 +    else if ( resp->status < 0)
   1.275 +        printk(KERN_WARNING "receive_usb_probe(): got error status %d\n",
   1.276 +               resp->status);
   1.277 +
   1.278 +    spin_unlock(&xhci->rh.port_state_lock);
   1.279 +}
   1.280 +
   1.281 +static void receive_usb_io(usbif_response_t *resp)
   1.282 +{
   1.283 +        struct urb_priv *urbp = (struct urb_priv *)resp->id;
   1.284 +        struct urb *urb = urbp->urb;
   1.285 +
   1.286 +        urb->actual_length = resp->length;
   1.287 +	urb->status = resp->status;
   1.288 +	urbp->status = resp->status;
   1.289 +        urbp->in_progress = 0;
   1.290 +
   1.291 +        if( usb_pipetype(urb->pipe) == 0 ) /* ISO */
   1.292 +        {
   1.293 +                int i;
   1.294 +              
   1.295 +                /* Copy ISO schedule results back in. */
   1.296 +                for ( i = 0; i < urb->number_of_packets; i++ )
   1.297 +                {
   1.298 +                        urb->iso_frame_desc[i].status
   1.299 +                                = urbp->schedule[i].status;
   1.300 +                        urb->iso_frame_desc[i].actual_length
   1.301 +                                = urbp->schedule[i].length;
   1.302 +                }
   1.303 +                free_page((unsigned long)urbp->schedule);
   1.304 +        }
   1.305 +}
   1.306 +
   1.307 +/**
   1.308 + * xhci_drain_ring - drain responses from the ring, calling handlers
   1.309 + *
   1.310 + * This may be called from interrupt context when an event is received from the
   1.311 + * backend domain, or sometimes in process context whilst waiting for a port
   1.312 + * reset or URB completion.
   1.313 + */
   1.314 +static void xhci_drain_ring(void)
   1.315 +{
   1.316 +	struct list_head *tmp, *head;
   1.317 +	usbif_front_ring_t *usb_ring = &xhci->usb_ring;
   1.318 +	usbif_response_t *resp;
   1.319 +        RING_IDX i, rp;
   1.320 +
   1.321 +        /* Walk the ring here to get responses, updating URBs to show what
   1.322 +         * completed. */
   1.323 +        
   1.324 +        rp = usb_ring->sring->rsp_prod;
   1.325 +        rmb(); /* Ensure we see queued requests up to 'rp'. */
   1.326 +
   1.327 +        /* Take items off the comms ring, taking care not to overflow. */
   1.328 +        for ( i = usb_ring->rsp_cons; i != rp; i++ )
   1.329 +        {
   1.330 +            resp = RING_GET_RESPONSE(USBIF_RING, usb_ring, i);
   1.331 +            
   1.332 +            /* May need to deal with batching and with putting a ceiling on
   1.333 +               the number dispatched for performance and anti-dos reasons */
   1.334 +
   1.335 +            xhci_show_resp(resp);
   1.336 +
   1.337 +            switch ( resp->operation )
   1.338 +            {
   1.339 +            case USBIF_OP_PROBE:
   1.340 +                receive_usb_probe(resp);
   1.341 +                break;
   1.342 +                
   1.343 +            case USBIF_OP_IO:
   1.344 +                receive_usb_io(resp);
   1.345 +                break;
   1.346 +
   1.347 +            case USBIF_OP_RESET:
   1.348 +                receive_usb_reset(resp);
   1.349 +                break;
   1.350 +
   1.351 +            default:
   1.352 +                printk(KERN_WARNING
   1.353 +                       "error: unknown USB io operation response [%d]\n",
   1.354 +                       resp->operation);
   1.355 +                break;
   1.356 +            }
   1.357 +        }
   1.358 +
   1.359 +        usb_ring->rsp_cons = i;
   1.360 +
   1.361 +	/* Walk the list of pending URB's to see which ones completed and do
   1.362 +         * callbacks, etc. */
   1.363 +	spin_lock(&xhci->urb_list_lock);
   1.364 +	head = &xhci->urb_list;
   1.365 +	tmp = head->next;
   1.366 +	while (tmp != head) {
   1.367 +		struct urb *urb = list_entry(tmp, struct urb, urb_list);
   1.368 +
   1.369 +		tmp = tmp->next;
   1.370 +
   1.371 +		/* Checks the status and does all of the magic necessary */
   1.372 +		xhci_transfer_result(xhci, urb);
   1.373 +	}
   1.374 +	spin_unlock(&xhci->urb_list_lock);
   1.375 +
   1.376 +	xhci_finish_completion();
   1.377 +}
   1.378 +
   1.379 +
   1.380 +static void xhci_interrupt(int irq, void *__xhci, struct pt_regs *regs)
   1.381 +{
   1.382 +        xhci_drain_ring();
   1.383 +}
   1.384 +
   1.385 +/******************************************************************************
   1.386 + * HOST CONTROLLER FUNCTIONALITY
   1.387 + */
   1.388 +
   1.389 +/**
   1.390 + * no-op implementation of private device alloc / free routines
   1.391 + */
   1.392 +static int xhci_do_nothing_dev(struct usb_device *dev)
   1.393  {
   1.394  	return 0;
   1.395  }
   1.396 @@ -365,6 +504,8 @@ static inline void xhci_add_complete(str
   1.397   * storage.
   1.398   *
   1.399   * We spin and wait for the URB to complete before returning.
   1.400 + *
   1.401 + * Call with urb->lock acquired.
   1.402   */
   1.403  static void xhci_delete_urb(struct urb *urb)
   1.404  {
   1.405 @@ -526,8 +667,10 @@ static int xhci_submit_urb(struct urb *u
   1.406  
   1.407  	switch (usb_pipetype(urb->pipe)) {
   1.408  	case PIPE_CONTROL:
   1.409 +	case PIPE_BULK:
   1.410  		ret = xhci_queue_req(urb);
   1.411  		break;
   1.412 +
   1.413  	case PIPE_INTERRUPT:
   1.414  		if (urb->bandwidth == 0) {	/* not yet checked/allocated */
   1.415  			bustime = usb_check_bandwidth(urb->dev, urb);
   1.416 @@ -536,14 +679,13 @@ static int xhci_submit_urb(struct urb *u
   1.417  			else {
   1.418  				ret = xhci_queue_req(urb);
   1.419  				if (ret == -EINPROGRESS)
   1.420 -					usb_claim_bandwidth(urb->dev, urb, bustime, 0);
   1.421 +					usb_claim_bandwidth(urb->dev, urb,
   1.422 +                                                            bustime, 0);
   1.423  			}
   1.424  		} else		/* bandwidth is already set */
   1.425  			ret = xhci_queue_req(urb);
   1.426  		break;
   1.427 -	case PIPE_BULK:
   1.428 -		ret = xhci_queue_req(urb);
   1.429 -		break;
   1.430 +
   1.431  	case PIPE_ISOCHRONOUS:
   1.432  		if (urb->bandwidth == 0) {	/* not yet checked/allocated */
   1.433  			if (urb->number_of_packets <= 0) {
   1.434 @@ -563,7 +705,6 @@ static int xhci_submit_urb(struct urb *u
   1.435  			ret = xhci_queue_req(urb);
   1.436  		break;
   1.437  	}
   1.438 -
   1.439  out:
   1.440  	urb->status = ret;
   1.441  
   1.442 @@ -577,7 +718,7 @@ out:
   1.443  		return 0;
   1.444  	}
   1.445  
   1.446 -	xhci_unlink_generic(urb);
   1.447 +	xhci_delete_urb(urb);
   1.448  
   1.449  	spin_unlock(&urb->lock);
   1.450  	spin_unlock_irqrestore(&xhci->urb_list_lock, flags);
   1.451 @@ -643,7 +784,7 @@ static void xhci_transfer_result(struct 
   1.452  		/* Spinlock needed ? */
   1.453  		if (urb->bandwidth)
   1.454  			usb_release_bandwidth(urb->dev, urb, 1);
   1.455 -		xhci_unlink_generic(urb);
   1.456 +		xhci_delete_urb(urb);
   1.457  		break;
   1.458  	case PIPE_INTERRUPT:
   1.459  		/* Interrupts are an exception */
   1.460 @@ -654,11 +795,11 @@ static void xhci_transfer_result(struct 
   1.461  		/* Spinlock needed ? */
   1.462  		if (urb->bandwidth)
   1.463  			usb_release_bandwidth(urb->dev, urb, 0);
   1.464 -		xhci_unlink_generic(urb);
   1.465 +		xhci_delete_urb(urb);
   1.466  		break;
   1.467  	default:
   1.468  		info("xhci_transfer_result: unknown pipe type %d for urb %p\n",
   1.469 -			usb_pipetype(urb->pipe), urb);
   1.470 +                     usb_pipetype(urb->pipe), urb);
   1.471  	}
   1.472  
   1.473  	/* Remove it from xhci->urb_list */
   1.474 @@ -671,23 +812,6 @@ out:
   1.475  	spin_unlock_irqrestore(&urb->lock, flags);
   1.476  }
   1.477  
   1.478 -/*
   1.479 - * MUST be called with urb->lock acquired
   1.480 - */
   1.481 -static void xhci_unlink_generic(struct urb *urb)
   1.482 -{
   1.483 -	struct urb_priv *urbp = urb->hcpriv;
   1.484 -
   1.485 -	/* We can get called when urbp allocation fails, so check */
   1.486 -	if (!urbp)
   1.487 -		return;
   1.488 -
   1.489 -        /* ??? This function is now so minimal it doesn't do much.  Do we really
   1.490 -         * need it? */
   1.491 -
   1.492 -	xhci_delete_urb(urb);
   1.493 -}
   1.494 -
   1.495  static int xhci_unlink_urb(struct urb *urb)
   1.496  {
   1.497  	unsigned long flags;
   1.498 @@ -725,7 +849,7 @@ static int xhci_unlink_urb(struct urb *u
   1.499  
   1.500  	list_del_init(&urb->urb_list);
   1.501  
   1.502 -	xhci_unlink_generic(urb);
   1.503 +	xhci_delete_urb(urb);
   1.504  
   1.505  	/* Short circuit the virtual root hub */
   1.506  	if (urb->dev == xhci->rh.dev) {
   1.507 @@ -770,10 +894,114 @@ static int xhci_unlink_urb(struct urb *u
   1.508  	return 0;
   1.509  }
   1.510  
   1.511 +static void xhci_call_completion(struct urb *urb)
   1.512 +{
   1.513 +	struct urb_priv *urbp;
   1.514 +	struct usb_device *dev = urb->dev;
   1.515 +	int is_ring = 0, killed, resubmit_interrupt, status;
   1.516 +	struct urb *nurb;
   1.517 +	unsigned long flags;
   1.518 +
   1.519 +	spin_lock_irqsave(&urb->lock, flags);
   1.520 +
   1.521 +	urbp = (struct urb_priv *)urb->hcpriv;
   1.522 +	if (!urbp || !urb->dev) {
   1.523 +		spin_unlock_irqrestore(&urb->lock, flags);
   1.524 +		return;
   1.525 +	}
   1.526 +
   1.527 +	killed = (urb->status == -ENOENT || urb->status == -ECONNABORTED ||
   1.528 +			urb->status == -ECONNRESET);
   1.529 +	resubmit_interrupt = (usb_pipetype(urb->pipe) == PIPE_INTERRUPT &&
   1.530 +			urb->interval);
   1.531 +
   1.532 +	nurb = urb->next;
   1.533 +	if (nurb && !killed) {
   1.534 +		int count = 0;
   1.535 +
   1.536 +		while (nurb && nurb != urb && count < MAX_URB_LOOP) {
   1.537 +			if (nurb->status == -ENOENT ||
   1.538 +			    nurb->status == -ECONNABORTED ||
   1.539 +			    nurb->status == -ECONNRESET) {
   1.540 +				killed = 1;
   1.541 +				break;
   1.542 +			}
   1.543 +
   1.544 +			nurb = nurb->next;
   1.545 +			count++;
   1.546 +		}
   1.547 +
   1.548 +		if (count == MAX_URB_LOOP)
   1.549 +			err("xhci_call_completion: too many linked URB's, loop? (first loop)");
   1.550 +
   1.551 +		/* Check to see if chain is a ring */
   1.552 +		is_ring = (nurb == urb);
   1.553 +	}
   1.554 +
   1.555 +	status = urbp->status;
   1.556 +	if (!resubmit_interrupt || killed)
   1.557 +		/* We don't need urb_priv anymore */
   1.558 +		xhci_destroy_urb_priv(urb);
   1.559 +
   1.560 +	if (!killed)
   1.561 +		urb->status = status;
   1.562 +
   1.563 +	spin_unlock_irqrestore(&urb->lock, flags);
   1.564 +
   1.565 +	if (urb->complete)
   1.566 +		urb->complete(urb);
   1.567 +
   1.568 +	if (resubmit_interrupt)
   1.569 +		/* Recheck the status. The completion handler may have */
   1.570 +		/*  unlinked the resubmitting interrupt URB */
   1.571 +		killed = (urb->status == -ENOENT ||
   1.572 +			  urb->status == -ECONNABORTED ||
   1.573 +			  urb->status == -ECONNRESET);
   1.574 +
   1.575 +	if (resubmit_interrupt && !killed) {
   1.576 +                if ( urb->dev != xhci->rh.dev )
   1.577 +                        xhci_queue_req(urb); /* XXX What if this fails? */
   1.578 +                /* Don't need to resubmit URBs for the virtual root dev. */
   1.579 +	} else {
   1.580 +		if (is_ring && !killed) {
   1.581 +			urb->dev = dev;
   1.582 +			xhci_submit_urb(urb);
   1.583 +		} else {
   1.584 +			/* We decrement the usage count after we're done */
   1.585 +			/*  with everything */
   1.586 +			usb_dec_dev_use(dev);
   1.587 +		}
   1.588 +	}
   1.589 +}
   1.590 +
   1.591 +static void xhci_finish_completion(void)
   1.592 +{
   1.593 +	struct list_head *tmp, *head;
   1.594 +	unsigned long flags;
   1.595 +
   1.596 +	spin_lock_irqsave(&xhci->complete_list_lock, flags);
   1.597 +	head = &xhci->complete_list;
   1.598 +	tmp = head->next;
   1.599 +	while (tmp != head) {
   1.600 +		struct urb_priv *urbp = list_entry(tmp, struct urb_priv,
   1.601 +                                                   complete_list);
   1.602 +		struct urb *urb = urbp->urb;
   1.603 +
   1.604 +		list_del_init(&urbp->complete_list);
   1.605 +		spin_unlock_irqrestore(&xhci->complete_list_lock, flags);
   1.606 +
   1.607 +		xhci_call_completion(urb);
   1.608 +
   1.609 +		spin_lock_irqsave(&xhci->complete_list_lock, flags);
   1.610 +		head = &xhci->complete_list;
   1.611 +		tmp = head->next;
   1.612 +	}
   1.613 +	spin_unlock_irqrestore(&xhci->complete_list_lock, flags);
   1.614 +}
   1.615  
   1.616  static struct usb_operations xhci_device_operations = {
   1.617 -	.allocate = xhci_alloc_dev,
   1.618 -	.deallocate = xhci_free_dev,
   1.619 +	.allocate = xhci_do_nothing_dev,
   1.620 +	.deallocate = xhci_do_nothing_dev,
   1.621          /* It doesn't look like any drivers actually care what the frame number
   1.622  	 * is at the moment!  If necessary, we could approximate the current
   1.623  	 * frame nubmer by passing it from the backend in response messages. */
   1.624 @@ -782,7 +1010,9 @@ static struct usb_operations xhci_device
   1.625  	.unlink_urb = xhci_unlink_urb
   1.626  };
   1.627  
   1.628 -/* Virtual Root Hub */
   1.629 +/******************************************************************************
   1.630 + * VIRTUAL ROOT HUB EMULATION
   1.631 + */
   1.632  
   1.633  static __u8 root_hub_dev_des[] =
   1.634  {
   1.635 @@ -867,9 +1097,8 @@ static int rh_send_irq(struct urb *urb)
   1.636  
   1.637  	spin_lock_irqsave(&urb->lock, flags);
   1.638  	for (i = 0; i < xhci->rh.numports; i++) {
   1.639 -                /* MAW: No idea what the old code was doing here or why it worked.
   1.640 -		 * This implementation sets a bit if anything at all has changed on the 
   1.641 -		 * port, as per USB spec 11.12 */
   1.642 +                /* Set a bit if anything at all has changed on the port, as per
   1.643 +		 * USB spec 11.12 */
   1.644  		data |= (ports[i].cs_chg || ports[i].pe_chg )
   1.645                          ? (1 << (i + 1))
   1.646                          : 0;
   1.647 @@ -921,7 +1150,8 @@ static void rh_int_timer_do(unsigned lon
   1.648  		spin_lock(&u->lock);
   1.649  
   1.650  		/* Check if the URB timed out */
   1.651 -		if (u->timeout && time_after_eq(jiffies, up->inserttime + u->timeout)) {
   1.652 +		if (u->timeout && time_after_eq(jiffies,
   1.653 +                                                up->inserttime + u->timeout)) {
   1.654  			list_del(&u->urb_list);
   1.655  			list_add_tail(&u->urb_list, &list);
   1.656  		}
   1.657 @@ -951,7 +1181,8 @@ static int rh_init_int_timer(struct urb 
   1.658  	init_timer(&xhci->rh.rh_int_timer);
   1.659  	xhci->rh.rh_int_timer.function = rh_int_timer_do;
   1.660  	xhci->rh.rh_int_timer.data = (unsigned long)urb;
   1.661 -	xhci->rh.rh_int_timer.expires = jiffies + (HZ * (urb->interval < 30 ? 30 : urb->interval)) / 1000;
   1.662 +	xhci->rh.rh_int_timer.expires = jiffies
   1.663 +                + (HZ * (urb->interval < 30 ? 30 : urb->interval)) / 1000;
   1.664  	add_timer(&xhci->rh.rh_int_timer);
   1.665  
   1.666  	return 0;
   1.667 @@ -963,7 +1194,8 @@ static int rh_init_int_timer(struct urb 
   1.668  static int rh_submit_urb(struct urb *urb)
   1.669  {
   1.670  	unsigned int pipe = urb->pipe;
   1.671 -	struct usb_ctrlrequest *cmd = (struct usb_ctrlrequest *)urb->setup_packet;
   1.672 +	struct usb_ctrlrequest *cmd =
   1.673 +                (struct usb_ctrlrequest *)urb->setup_packet;
   1.674  	void *data = urb->transfer_buffer;
   1.675  	int leni = urb->transfer_buffer_length;
   1.676  	int len = 0;
   1.677 @@ -1164,250 +1396,12 @@ static int rh_unlink_urb(struct urb *urb
   1.678  	return 0;
   1.679  }
   1.680  
   1.681 -static void xhci_call_completion(struct urb *urb)
   1.682 -{
   1.683 -	struct urb_priv *urbp;
   1.684 -	struct usb_device *dev = urb->dev;
   1.685 -	int is_ring = 0, killed, resubmit_interrupt, status;
   1.686 -	struct urb *nurb;
   1.687 -	unsigned long flags;
   1.688 -
   1.689 -	spin_lock_irqsave(&urb->lock, flags);
   1.690 -
   1.691 -	urbp = (struct urb_priv *)urb->hcpriv;
   1.692 -	if (!urbp || !urb->dev) {
   1.693 -		spin_unlock_irqrestore(&urb->lock, flags);
   1.694 -		return;
   1.695 -	}
   1.696 -
   1.697 -	killed = (urb->status == -ENOENT || urb->status == -ECONNABORTED ||
   1.698 -			urb->status == -ECONNRESET);
   1.699 -	resubmit_interrupt = (usb_pipetype(urb->pipe) == PIPE_INTERRUPT &&
   1.700 -			urb->interval);
   1.701 -
   1.702 -	nurb = urb->next;
   1.703 -	if (nurb && !killed) {
   1.704 -		int count = 0;
   1.705 -
   1.706 -		while (nurb && nurb != urb && count < MAX_URB_LOOP) {
   1.707 -			if (nurb->status == -ENOENT ||
   1.708 -			    nurb->status == -ECONNABORTED ||
   1.709 -			    nurb->status == -ECONNRESET) {
   1.710 -				killed = 1;
   1.711 -				break;
   1.712 -			}
   1.713 -
   1.714 -			nurb = nurb->next;
   1.715 -			count++;
   1.716 -		}
   1.717 -
   1.718 -		if (count == MAX_URB_LOOP)
   1.719 -			err("xhci_call_completion: too many linked URB's, loop? (first loop)");
   1.720 -
   1.721 -		/* Check to see if chain is a ring */
   1.722 -		is_ring = (nurb == urb);
   1.723 -	}
   1.724 -
   1.725 -	status = urbp->status;
   1.726 -	if (!resubmit_interrupt || killed)
   1.727 -		/* We don't need urb_priv anymore */
   1.728 -		xhci_destroy_urb_priv(urb);
   1.729 -
   1.730 -	if (!killed)
   1.731 -		urb->status = status;
   1.732 -
   1.733 -	spin_unlock_irqrestore(&urb->lock, flags);
   1.734 -
   1.735 -	if (urb->complete)
   1.736 -		urb->complete(urb);
   1.737 -
   1.738 -	if (resubmit_interrupt)
   1.739 -		/* Recheck the status. The completion handler may have */
   1.740 -		/*  unlinked the resubmitting interrupt URB */
   1.741 -		killed = (urb->status == -ENOENT ||
   1.742 -			  urb->status == -ECONNABORTED ||
   1.743 -			  urb->status == -ECONNRESET);
   1.744 -
   1.745 -	if (resubmit_interrupt && !killed) {
   1.746 -                if ( urb->dev != xhci->rh.dev )
   1.747 -                        xhci_queue_req(urb); /* XXX What if this fails? */
   1.748 -                /* Don't need to resubmit URBs for the virtual root dev. */
   1.749 -	} else {
   1.750 -		if (is_ring && !killed) {
   1.751 -			urb->dev = dev;
   1.752 -			xhci_submit_urb(urb);
   1.753 -		} else {
   1.754 -			/* We decrement the usage count after we're done */
   1.755 -			/*  with everything */
   1.756 -			usb_dec_dev_use(dev);
   1.757 -		}
   1.758 -	}
   1.759 -}
   1.760 -
   1.761 -static void xhci_finish_completion(void)
   1.762 -{
   1.763 -	struct list_head *tmp, *head;
   1.764 -	unsigned long flags;
   1.765 -
   1.766 -	spin_lock_irqsave(&xhci->complete_list_lock, flags);
   1.767 -	head = &xhci->complete_list;
   1.768 -	tmp = head->next;
   1.769 -	while (tmp != head) {
   1.770 -		struct urb_priv *urbp = list_entry(tmp, struct urb_priv, complete_list);
   1.771 -		struct urb *urb = urbp->urb;
   1.772 -
   1.773 -		list_del_init(&urbp->complete_list);
   1.774 -		spin_unlock_irqrestore(&xhci->complete_list_lock, flags);
   1.775 -
   1.776 -		xhci_call_completion(urb);
   1.777 -
   1.778 -		spin_lock_irqsave(&xhci->complete_list_lock, flags);
   1.779 -		head = &xhci->complete_list;
   1.780 -		tmp = head->next;
   1.781 -	}
   1.782 -	spin_unlock_irqrestore(&xhci->complete_list_lock, flags);
   1.783 -}
   1.784 -
   1.785 -static void receive_usb_reset(usbif_response_t *resp)
   1.786 -{
   1.787 -    awaiting_reset = resp->status;
   1.788 -    rmb();
   1.789 -    
   1.790 -}
   1.791 -
   1.792 -static void receive_usb_probe(usbif_response_t *resp)
   1.793 -{
   1.794 -    spin_lock(&xhci->rh.port_state_lock);
   1.795 -
   1.796 -    if ( resp->status > 0 )
   1.797 -    {
   1.798 -        if ( resp->status == 1 )
   1.799 -        {
   1.800 -            /* If theres a device there and there wasn't one before there must
   1.801 -             * have been a connection status change. */
   1.802 -            if( xhci->rh.ports[resp->data].cs == 0 )
   1.803 -	    {
   1.804 -                xhci->rh.ports[resp->data].cs = 1;
   1.805 -                xhci->rh.ports[resp->data].ccs = 1;
   1.806 -                xhci->rh.ports[resp->data].cs_chg = 1;
   1.807 -	    }
   1.808 -        }
   1.809 -        else
   1.810 -            printk(KERN_WARNING "receive_usb_probe(): unexpected status %d for port %d\n",
   1.811 -                   resp->status, resp->data);
   1.812 -    }
   1.813 -    else if ( resp->status < 0)
   1.814 -        printk(KERN_WARNING "receive_usb_probe(): got error status %d\n", resp->status);
   1.815 -
   1.816 -    spin_unlock(&xhci->rh.port_state_lock);
   1.817 -}
   1.818 -
   1.819 -static void receive_usb_io(usbif_response_t *resp)
   1.820 -{
   1.821 -        struct urb_priv *urbp = (struct urb_priv *)resp->id;
   1.822 -        struct urb *urb = urbp->urb;
   1.823 -
   1.824 -        urb->actual_length = resp->length;
   1.825 -	urb->status = resp->status;
   1.826 -	urbp->status = resp->status;
   1.827 -        urbp->in_progress = 0;
   1.828 -
   1.829 -        if( usb_pipetype(urb->pipe) == 0 ) /* ISO */
   1.830 -        {
   1.831 -                int i;
   1.832 -              
   1.833 -                /* Copy ISO schedule results back in. */
   1.834 -
   1.835 -                for ( i = 0; i < urb->number_of_packets; i++ )
   1.836 -                {
   1.837 -                        urb->iso_frame_desc[i].status
   1.838 -			  = urbp->schedule[i].status;
   1.839 -                        urb->iso_frame_desc[i].actual_length
   1.840 -                                = urbp->schedule[i].length;
   1.841 -                }
   1.842 -                free_page((unsigned long)urbp->schedule);
   1.843 -        }
   1.844 -}
   1.845 -
   1.846 -static void xhci_drain_ring(void)
   1.847 -{
   1.848 -	struct list_head *tmp, *head;
   1.849 -	usbif_front_ring_t *usb_ring = &xhci->usb_ring;
   1.850 -	usbif_response_t *resp;
   1.851 -        RING_IDX i, rp;
   1.852 -
   1.853 -        /* Walk the ring here to get responses, updating URBs to show what
   1.854 -         * completed. */
   1.855 -        
   1.856 -        rp = usb_ring->sring->rsp_prod;
   1.857 -        rmb(); /* Ensure we see queued requests up to 'rp'. */
   1.858 -
   1.859 -        /* Take items off the comms ring, taking care not to overflow. */
   1.860 -        for ( i = usb_ring->rsp_cons; i != rp; i++ )
   1.861 -        {
   1.862 -            resp = RING_GET_RESPONSE(USBIF_RING, usb_ring, i);
   1.863 -            
   1.864 -            /* May need to deal with batching and with putting a ceiling on
   1.865 -               the number dispatched for performance and anti-dos reasons */
   1.866 -
   1.867 -            xhci_show_resp(resp);
   1.868 -
   1.869 -            switch ( resp->operation )
   1.870 -            {
   1.871 -            case USBIF_OP_PROBE:
   1.872 -                receive_usb_probe(resp);
   1.873 -                break;
   1.874 -                
   1.875 -            case USBIF_OP_IO:
   1.876 -                receive_usb_io(resp);
   1.877 -                break;
   1.878 -
   1.879 -            case USBIF_OP_RESET:
   1.880 -                receive_usb_reset(resp);
   1.881 -                break;
   1.882 -
   1.883 -            default:
   1.884 -                printk(KERN_WARNING
   1.885 -                       "error: unknown USB io operation response [%d]\n",
   1.886 -                       resp->operation);
   1.887 -                break;
   1.888 -            }
   1.889 -        }
   1.890 -
   1.891 -        usb_ring->rsp_cons = i;
   1.892 -
   1.893 -	/* Walk the list of pending URB's to see which ones completed and do
   1.894 -         * callbacks, etc. */
   1.895 -	spin_lock(&xhci->urb_list_lock);
   1.896 -	head = &xhci->urb_list;
   1.897 -	tmp = head->next;
   1.898 -	while (tmp != head) {
   1.899 -                
   1.900 -		struct urb *urb = list_entry(tmp, struct urb, urb_list);
   1.901 -
   1.902 -		tmp = tmp->next;
   1.903 -
   1.904 -		/* Checks the status and does all of the magic necessary */
   1.905 -		xhci_transfer_result(xhci, urb);
   1.906 -	}
   1.907 -	spin_unlock(&xhci->urb_list_lock);
   1.908 -
   1.909 -	xhci_finish_completion();
   1.910 -}
   1.911 -
   1.912 -
   1.913 -static void xhci_interrupt(int irq, void *__xhci, struct pt_regs *regs)
   1.914 -{
   1.915 -        xhci_drain_ring();
   1.916 -}
   1.917 -
   1.918 -static void free_xhci(struct xhci *xhci)
   1.919 -{
   1.920 -	kfree(xhci);
   1.921 -}
   1.922 +/******************************************************************************
   1.923 + * CONTROL PLANE FUNCTIONALITY
   1.924 + */
   1.925  
   1.926  /**
   1.927 - * Initialise a new virtual root hub for a new USB device channel.
   1.928 + * alloc_xhci - initialise a new virtual root hub for a new USB device channel
   1.929   */
   1.930  static int alloc_xhci(void)
   1.931  {
   1.932 @@ -1442,12 +1436,8 @@ static int alloc_xhci(void)
   1.933  
   1.934  	spin_lock_init(&xhci->frame_list_lock);
   1.935  
   1.936 -	/* We need exactly one page (per XHCI specs), how convenient */
   1.937 -	/* We assume that one page is atleast 4k (1024 frames * 4 bytes) */
   1.938 -#if PAGE_SIZE < (4 * 1024)
   1.939 -#error PAGE_SIZE is not atleast 4k
   1.940 -#endif
   1.941  	bus = usb_alloc_bus(&xhci_device_operations);
   1.942 +
   1.943  	if (!bus) {
   1.944  		err("unable to allocate bus");
   1.945  		goto err_alloc_bus;
   1.946 @@ -1477,16 +1467,20 @@ static int alloc_xhci(void)
   1.947   * error exits:
   1.948   */
   1.949  err_alloc_root_hub:
   1.950 +        usb_deregister_bus(xhci->bus);
   1.951  	usb_free_bus(xhci->bus);
   1.952  	xhci->bus = NULL;
   1.953  
   1.954  err_alloc_bus:
   1.955 -	free_xhci(xhci);
   1.956 +	kfree(xhci);
   1.957  
   1.958  err_alloc_xhci:
   1.959  	return retval;
   1.960  }
   1.961  
   1.962 +/**
   1.963 + * usbif_status_change - deal with an incoming USB_INTERFACE_STATUS_ message
   1.964 + */
   1.965  static void usbif_status_change(usbif_fe_interface_status_changed_t *status)
   1.966  {
   1.967      ctrl_msg_t                   cmsg;
   1.968 @@ -1551,8 +1545,8 @@ static void usbif_status_change(usbif_fe
   1.969  	}
   1.970  
   1.971  	/* Allocate the appropriate USB bandwidth here...  Need to
   1.972 -	* somehow know what the total available is thought to be so we
   1.973 -	* can calculate the reservation correctly. */
   1.974 +         * somehow know what the total available is thought to be so we
   1.975 +         * can calculate the reservation correctly. */
   1.976   	usb_claim_bandwidth(xhci->rh.dev, xhci->rh.urb,
   1.977   			    1000 - xhci->bandwidth, 0);
   1.978  
   1.979 @@ -1560,8 +1554,10 @@ static void usbif_status_change(usbif_fe
   1.980                                 SA_SAMPLE_RANDOM, "usbif", xhci)) )
   1.981                  printk(KERN_ALERT"usbfront request_irq failed (%ld)\n",rc);
   1.982  
   1.983 -	DPRINTK(KERN_INFO __FILE__ ": USB XHCI: SHM at %p (0x%lx), EVTCHN %d IRQ %d\n",
   1.984 -               xhci->usb_ring.sring, virt_to_machine(xhci->usbif), xhci->evtchn, xhci->irq);
   1.985 +	DPRINTK(KERN_INFO __FILE__
   1.986 +                ": USB XHCI: SHM at %p (0x%lx), EVTCHN %d IRQ %d\n",
   1.987 +                xhci->usb_ring.sring, virt_to_machine(xhci->usbif),
   1.988 +                xhci->evtchn, xhci->irq);
   1.989  
   1.990          xhci->state = USBIF_STATE_CONNECTED;
   1.991          
   1.992 @@ -1574,7 +1570,9 @@ static void usbif_status_change(usbif_fe
   1.993      }
   1.994  }
   1.995  
   1.996 -
   1.997 +/**
   1.998 + * usbif_ctrlif_rx - demux control messages by subtype
   1.999 + */
  1.1000  static void usbif_ctrlif_rx(ctrl_msg_t *msg, unsigned long id)
  1.1001  {
  1.1002      switch ( msg->subtype )
  1.1003 @@ -1623,9 +1621,6 @@ static int __init xhci_hcd_init(void)
  1.1004  	if (!xhci_up_cachep)
  1.1005  		goto up_failed;
  1.1006  
  1.1007 -        /* Lazily avoid unloading issues for now. ;-)*/
  1.1008 -	MOD_INC_USE_COUNT;
  1.1009 -
  1.1010          /* Let the domain controller know we're here.  For now we wait until
  1.1011           * connection, as for the block and net drivers.  This is only strictly
  1.1012           * necessary if we're going to boot off a USB device. */
  1.1013 @@ -1661,29 +1656,14 @@ static int __init xhci_hcd_init(void)
  1.1014  	return 0;
  1.1015  
  1.1016  up_failed:
  1.1017 -
  1.1018  	if (errbuf)
  1.1019  		kfree(errbuf);
  1.1020  
  1.1021  errbuf_failed:
  1.1022 -
  1.1023  	return retval;
  1.1024  }
  1.1025  
  1.1026 -static void __exit xhci_hcd_cleanup(void) 
  1.1027 -{
  1.1028 -	if (kmem_cache_destroy(xhci_up_cachep))
  1.1029 -		printk(KERN_WARNING "xhci: not all urb_priv's were freed\n");
  1.1030 -
  1.1031 -//        release_xhci(); do some calls here
  1.1032 -
  1.1033 -
  1.1034 -	if (errbuf)
  1.1035 -		kfree(errbuf);
  1.1036 -}
  1.1037 -
  1.1038  module_init(xhci_hcd_init);
  1.1039 -module_exit(xhci_hcd_cleanup);
  1.1040  
  1.1041  MODULE_AUTHOR(DRIVER_AUTHOR);
  1.1042  MODULE_DESCRIPTION(DRIVER_DESC);