linux-old/drivers/usb/hcd.c
<<
>>
Prefs
   1/*
   2 * Copyright (c) 2001-2002 by David Brownell
   3 * 
   4 * This program is free software; you can redistribute it and/or modify it
   5 * under the terms of the GNU General Public License as published by the
   6 * Free Software Foundation; either version 2 of the License, or (at your
   7 * option) any later version.
   8 *
   9 * This program is distributed in the hope that it will be useful, but
  10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12 * for more details.
  13 *
  14 * You should have received a copy of the GNU General Public License
  15 * along with this program; if not, write to the Free Software Foundation,
  16 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17 */
  18
  19#include <linux/config.h>
  20#include <linux/module.h>
  21#include <linux/pci.h>
  22#include <linux/kernel.h>
  23#include <linux/delay.h>
  24#include <linux/ioport.h>
  25#include <linux/sched.h>
  26#include <linux/slab.h>
  27#include <linux/smp_lock.h>
  28#include <linux/errno.h>
  29#include <linux/kmod.h>
  30#include <linux/init.h>
  31#include <linux/timer.h>
  32#include <linux/list.h>
  33#include <linux/interrupt.h>
  34#include <linux/completion.h>
  35#include <linux/uts.h>                  /* for UTS_SYSNAME */
  36
  37
  38#ifdef CONFIG_USB_DEBUG
  39        #define DEBUG
  40#else
  41        #undef DEBUG
  42#endif
  43
  44#include <linux/usb.h>
  45#include "hcd.h"
  46
  47#include <asm/io.h>
  48#include <asm/irq.h>
  49#include <asm/system.h>
  50#include <asm/unaligned.h>
  51
  52
  53/*-------------------------------------------------------------------------*/
  54
  55/*
  56 * USB Host Controller Driver framework
  57 *
  58 * Plugs into usbcore (usb_bus) and lets HCDs share code, minimizing
  59 * HCD-specific behaviors/bugs.  Think of it as the "upper level" of
  60 * some drivers, where the "lower level" is hardware-specific.
  61 *
  62 * This does error checks, tracks devices and urbs, and delegates to a
  63 * "hc_driver" only for code (and data) that really needs to know about
  64 * hardware differences.  That includes root hub registers, i/o queues,
  65 * and so on ... but as little else as possible.
  66 *
  67 * Shared code includes most of the "root hub" code (these are emulated,
  68 * though each HC's hardware works differently) and PCI glue, plus request
  69 * tracking overhead.  The HCD code should only block on spinlocks or on
  70 * hardware handshaking; blocking on software events (such as other kernel
  71 * threads releasing resources, or completing actions) is all generic.
  72 *
  73 * Happens the USB 2.0 spec says this would be invisible inside the "USBD",
  74 * and includes mostly a "HCDI" (HCD Interface) along with some APIs used
  75 * only by the hub driver ... and that neither should be seen or used by
  76 * usb client device drivers.
  77 *
  78 * Contributors of ideas or unattributed patches include: David Brownell,
  79 * Roman Weissgaerber, Rory Bolt, ...
  80 *
  81 * HISTORY:
  82 * 2002-sept    Merge some 2.5 updates so we can share hardware level HCD
  83 *      code between the 2.4.20+ and 2.5 trees.
  84 * 2002-feb     merge to 2.4.19
  85 * 2001-12-12   Initial patch version for Linux 2.5.1 kernel.
  86 */
  87
  88/*-------------------------------------------------------------------------*/
  89
  90/* host controllers we manage */
  91static LIST_HEAD (hcd_list);
  92
  93/* used when updating list of hcds */
  94static DECLARE_MUTEX (hcd_list_lock);
  95
  96/* used when updating hcd data */
  97static spinlock_t hcd_data_lock = SPIN_LOCK_UNLOCKED;
  98
  99static struct usb_operations hcd_operations;
 100
 101/*-------------------------------------------------------------------------*/
 102
 103/*
 104 * Sharable chunks of root hub code.
 105 */
 106
 107/*-------------------------------------------------------------------------*/
 108
 109#define KERNEL_REL      ((LINUX_VERSION_CODE >> 16) & 0x0ff)
 110#define KERNEL_VER      ((LINUX_VERSION_CODE >> 8) & 0x0ff)
 111
 112/* usb 2.0 root hub device descriptor */
 113static const u8 usb2_rh_dev_descriptor [18] = {
 114        0x12,       /*  __u8  bLength; */
 115        0x01,       /*  __u8  bDescriptorType; Device */
 116        0x00, 0x02, /*  __u16 bcdUSB; v2.0 */
 117
 118        0x09,       /*  __u8  bDeviceClass; HUB_CLASSCODE */
 119        0x00,       /*  __u8  bDeviceSubClass; */
 120        0x01,       /*  __u8  bDeviceProtocol; [ usb 2.0 single TT ]*/
 121        0x08,       /*  __u8  bMaxPacketSize0; 8 Bytes */
 122
 123        0x00, 0x00, /*  __u16 idVendor; */
 124        0x00, 0x00, /*  __u16 idProduct; */
 125        KERNEL_VER, KERNEL_REL, /*  __u16 bcdDevice */
 126
 127        0x03,       /*  __u8  iManufacturer; */
 128        0x02,       /*  __u8  iProduct; */
 129        0x01,       /*  __u8  iSerialNumber; */
 130        0x01        /*  __u8  bNumConfigurations; */
 131};
 132
 133/* no usb 2.0 root hub "device qualifier" descriptor: one speed only */
 134
 135/* usb 1.1 root hub device descriptor */
 136static const u8 usb11_rh_dev_descriptor [18] = {
 137        0x12,       /*  __u8  bLength; */
 138        0x01,       /*  __u8  bDescriptorType; Device */
 139        0x10, 0x01, /*  __u16 bcdUSB; v1.1 */
 140
 141        0x09,       /*  __u8  bDeviceClass; HUB_CLASSCODE */
 142        0x00,       /*  __u8  bDeviceSubClass; */
 143        0x00,       /*  __u8  bDeviceProtocol; [ low/full speeds only ] */
 144        0x08,       /*  __u8  bMaxPacketSize0; 8 Bytes */
 145
 146        0x00, 0x00, /*  __u16 idVendor; */
 147        0x00, 0x00, /*  __u16 idProduct; */
 148        KERNEL_VER, KERNEL_REL, /*  __u16 bcdDevice */
 149
 150        0x03,       /*  __u8  iManufacturer; */
 151        0x02,       /*  __u8  iProduct; */
 152        0x01,       /*  __u8  iSerialNumber; */
 153        0x01        /*  __u8  bNumConfigurations; */
 154};
 155
 156
 157/*-------------------------------------------------------------------------*/
 158
 159/* Configuration descriptors for our root hubs */
 160
 161static const u8 fs_rh_config_descriptor [] = {
 162
 163        /* one configuration */
 164        0x09,       /*  __u8  bLength; */
 165        0x02,       /*  __u8  bDescriptorType; Configuration */
 166        0x19, 0x00, /*  __u16 wTotalLength; */
 167        0x01,       /*  __u8  bNumInterfaces; (1) */
 168        0x01,       /*  __u8  bConfigurationValue; */
 169        0x00,       /*  __u8  iConfiguration; */
 170        0x40,       /*  __u8  bmAttributes; 
 171                                 Bit 7: Bus-powered,
 172                                     6: Self-powered,
 173                                     5 Remote-wakwup,
 174                                     4..0: resvd */
 175        0x00,       /*  __u8  MaxPower; */
 176      
 177        /* USB 1.1:
 178         * USB 2.0, single TT organization (mandatory):
 179         *      one interface, protocol 0
 180         *
 181         * USB 2.0, multiple TT organization (optional):
 182         *      two interfaces, protocols 1 (like single TT)
 183         *      and 2 (multiple TT mode) ... config is
 184         *      sometimes settable
 185         *      NOT IMPLEMENTED
 186         */
 187
 188        /* one interface */
 189        0x09,       /*  __u8  if_bLength; */
 190        0x04,       /*  __u8  if_bDescriptorType; Interface */
 191        0x00,       /*  __u8  if_bInterfaceNumber; */
 192        0x00,       /*  __u8  if_bAlternateSetting; */
 193        0x01,       /*  __u8  if_bNumEndpoints; */
 194        0x09,       /*  __u8  if_bInterfaceClass; HUB_CLASSCODE */
 195        0x00,       /*  __u8  if_bInterfaceSubClass; */
 196        0x00,       /*  __u8  if_bInterfaceProtocol; [usb1.1 or single tt] */
 197        0x00,       /*  __u8  if_iInterface; */
 198     
 199        /* one endpoint (status change endpoint) */
 200        0x07,       /*  __u8  ep_bLength; */
 201        0x05,       /*  __u8  ep_bDescriptorType; Endpoint */
 202        0x81,       /*  __u8  ep_bEndpointAddress; IN Endpoint 1 */
 203        0x03,       /*  __u8  ep_bmAttributes; Interrupt */
 204        0x02, 0x00, /*  __u16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8) */
 205        0xff        /*  __u8  ep_bInterval; (255ms -- usb 2.0 spec) */
 206};
 207
 208static const u8 hs_rh_config_descriptor [] = {
 209
 210        /* one configuration */
 211        0x09,       /*  __u8  bLength; */
 212        0x02,       /*  __u8  bDescriptorType; Configuration */
 213        0x19, 0x00, /*  __u16 wTotalLength; */
 214        0x01,       /*  __u8  bNumInterfaces; (1) */
 215        0x01,       /*  __u8  bConfigurationValue; */
 216        0x00,       /*  __u8  iConfiguration; */
 217        0x40,       /*  __u8  bmAttributes; 
 218                                 Bit 7: Bus-powered,
 219                                     6: Self-powered,
 220                                     5 Remote-wakwup,
 221                                     4..0: resvd */
 222        0x00,       /*  __u8  MaxPower; */
 223      
 224        /* USB 1.1:
 225         * USB 2.0, single TT organization (mandatory):
 226         *      one interface, protocol 0
 227         *
 228         * USB 2.0, multiple TT organization (optional):
 229         *      two interfaces, protocols 1 (like single TT)
 230         *      and 2 (multiple TT mode) ... config is
 231         *      sometimes settable
 232         *      NOT IMPLEMENTED
 233         */
 234
 235        /* one interface */
 236        0x09,       /*  __u8  if_bLength; */
 237        0x04,       /*  __u8  if_bDescriptorType; Interface */
 238        0x00,       /*  __u8  if_bInterfaceNumber; */
 239        0x00,       /*  __u8  if_bAlternateSetting; */
 240        0x01,       /*  __u8  if_bNumEndpoints; */
 241        0x09,       /*  __u8  if_bInterfaceClass; HUB_CLASSCODE */
 242        0x00,       /*  __u8  if_bInterfaceSubClass; */
 243        0x00,       /*  __u8  if_bInterfaceProtocol; [usb1.1 or single tt] */
 244        0x00,       /*  __u8  if_iInterface; */
 245     
 246        /* one endpoint (status change endpoint) */
 247        0x07,       /*  __u8  ep_bLength; */
 248        0x05,       /*  __u8  ep_bDescriptorType; Endpoint */
 249        0x81,       /*  __u8  ep_bEndpointAddress; IN Endpoint 1 */
 250        0x03,       /*  __u8  ep_bmAttributes; Interrupt */
 251        0x02, 0x00, /*  __u16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8) */
 252        0x0c        /*  __u8  ep_bInterval; (256ms -- usb 2.0 spec) */
 253};
 254
 255/*-------------------------------------------------------------------------*/
 256
 257/*
 258 * helper routine for returning string descriptors in UTF-16LE
 259 * input can actually be ISO-8859-1; ASCII is its 7-bit subset
 260 */
 261static int ascii2utf (char *s, u8 *utf, int utfmax)
 262{
 263        int retval;
 264
 265        for (retval = 0; *s && utfmax > 1; utfmax -= 2, retval += 2) {
 266                *utf++ = *s++;
 267                *utf++ = 0;
 268        }
 269        return retval;
 270}
 271
 272/*
 273 * rh_string - provides manufacturer, product and serial strings for root hub
 274 * @id: the string ID number (1: serial number, 2: product, 3: vendor)
 275 * @pci_desc: PCI device descriptor for the relevant HC
 276 * @type: string describing our driver 
 277 * @data: return packet in UTF-16 LE
 278 * @len: length of the return packet
 279 *
 280 * Produces either a manufacturer, product or serial number string for the
 281 * virtual root hub device.
 282 */
 283static int rh_string (
 284        int             id,
 285        struct usb_hcd  *hcd,
 286        u8              *data,
 287        int             len
 288) {
 289        char buf [100];
 290
 291        // language ids
 292        if (id == 0) {
 293                *data++ = 4; *data++ = 3;       /* 4 bytes string data */
 294                *data++ = 0; *data++ = 0;       /* some language id */
 295                return 4;
 296
 297        // serial number
 298        } else if (id == 1) {
 299                strcpy (buf, hcd->bus->bus_name);
 300
 301        // product description
 302        } else if (id == 2) {
 303                strcpy (buf, hcd->product_desc);
 304
 305        // id 3 == vendor description
 306        } else if (id == 3) {
 307                sprintf (buf, "%s %s %s", UTS_SYSNAME, UTS_RELEASE,
 308                        hcd->description);
 309
 310        // unsupported IDs --> "protocol stall"
 311        } else
 312            return 0;
 313
 314        data [0] = 2 * (strlen (buf) + 1);
 315        data [1] = 3;   /* type == string */
 316        return 2 + ascii2utf (buf, data + 2, len - 2);
 317}
 318
 319
 320/* Root hub control transfers execute synchronously */
 321static int rh_call_control (struct usb_hcd *hcd, struct urb *urb)
 322{
 323        struct usb_ctrlrequest *cmd = (struct usb_ctrlrequest *) urb->setup_packet;
 324        u16             typeReq, wValue, wIndex, wLength;
 325        const u8        *bufp = 0;
 326        u8              *ubuf = urb->transfer_buffer;
 327        int             len = 0;
 328
 329        typeReq  = (cmd->bRequestType << 8) | cmd->bRequest;
 330        wValue   = le16_to_cpu (cmd->wValue);
 331        wIndex   = le16_to_cpu (cmd->wIndex);
 332        wLength  = le16_to_cpu (cmd->wLength);
 333
 334        if (wLength > urb->transfer_buffer_length)
 335                goto error;
 336
 337        /* set up for success */
 338        urb->status = 0;
 339        urb->actual_length = wLength;
 340        switch (typeReq) {
 341
 342        /* DEVICE REQUESTS */
 343
 344        case DeviceRequest | USB_REQ_GET_STATUS:
 345                // DEVICE_REMOTE_WAKEUP
 346                ubuf [0] = 1; // selfpowered
 347                ubuf [1] = 0;
 348                        /* FALLTHROUGH */
 349        case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
 350        case DeviceOutRequest | USB_REQ_SET_FEATURE:
 351                dbg ("no device features yet yet");
 352                break;
 353        case DeviceRequest | USB_REQ_GET_CONFIGURATION:
 354                ubuf [0] = 1;
 355                        /* FALLTHROUGH */
 356        case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
 357                break;
 358        case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
 359                switch (wValue & 0xff00) {
 360                case USB_DT_DEVICE << 8:
 361                        if (hcd->driver->flags & HCD_USB2)
 362                                bufp = usb2_rh_dev_descriptor;
 363                        else if (hcd->driver->flags & HCD_USB11)
 364                                bufp = usb11_rh_dev_descriptor;
 365                        else
 366                                goto error;
 367                        len = 18;
 368                        break;
 369                case USB_DT_CONFIG << 8:
 370                        if (hcd->driver->flags & HCD_USB2) {
 371                                bufp = hs_rh_config_descriptor;
 372                                len = sizeof hs_rh_config_descriptor;
 373                        } else {
 374                                bufp = fs_rh_config_descriptor;
 375                                len = sizeof fs_rh_config_descriptor;
 376                        }
 377                        break;
 378                case USB_DT_STRING << 8:
 379                        urb->actual_length = rh_string (
 380                                wValue & 0xff, hcd,
 381                                ubuf, wLength);
 382                        break;
 383                default:
 384                        goto error;
 385                }
 386                break;
 387        case DeviceRequest | USB_REQ_GET_INTERFACE:
 388                ubuf [0] = 0;
 389                        /* FALLTHROUGH */
 390        case DeviceOutRequest | USB_REQ_SET_INTERFACE:
 391                break;
 392        case DeviceOutRequest | USB_REQ_SET_ADDRESS:
 393                // wValue == urb->dev->devaddr
 394                dbg ("%s root hub device address %d",
 395                        hcd->bus->bus_name, wValue);
 396                break;
 397
 398        /* INTERFACE REQUESTS (no defined feature/status flags) */
 399
 400        /* ENDPOINT REQUESTS */
 401
 402        case EndpointRequest | USB_REQ_GET_STATUS:
 403                // ENDPOINT_HALT flag
 404                ubuf [0] = 0;
 405                ubuf [1] = 0;
 406                        /* FALLTHROUGH */
 407        case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
 408        case EndpointOutRequest | USB_REQ_SET_FEATURE:
 409                dbg ("no endpoint features yet");
 410                break;
 411
 412        /* CLASS REQUESTS (and errors) */
 413
 414        default:
 415                /* non-generic request */
 416                urb->status = hcd->driver->hub_control (hcd,
 417                        typeReq, wValue, wIndex,
 418                        ubuf, wLength);
 419                break;
 420error:
 421                /* "protocol stall" on error */
 422                urb->status = -EPIPE;
 423                dbg ("unsupported hub control message (maxchild %d)",
 424                                urb->dev->maxchild);
 425        }
 426        if (urb->status) {
 427                urb->actual_length = 0;
 428                dbg ("CTRL: TypeReq=0x%x val=0x%x idx=0x%x len=%d ==> %d",
 429                        typeReq, wValue, wIndex, wLength, urb->status);
 430        }
 431        if (bufp) {
 432                if (urb->transfer_buffer_length < len)
 433                        len = urb->transfer_buffer_length;
 434                urb->actual_length = len;
 435                // always USB_DIR_IN, toward host
 436                memcpy (ubuf, bufp, len);
 437        }
 438
 439        /* any errors get returned through the urb completion */
 440        usb_hcd_giveback_urb (hcd, urb, 0);
 441        return 0;
 442}
 443
 444/*-------------------------------------------------------------------------*/
 445
 446/*
 447 * Root Hub interrupt transfers are synthesized with a timer.
 448 * Completions are called in_interrupt() but not in_irq().
 449 */
 450
 451static void rh_report_status (unsigned long ptr);
 452
 453static int rh_status_urb (struct usb_hcd *hcd, struct urb *urb) 
 454{
 455        int     len = 1 + (urb->dev->maxchild / 8);
 456
 457        /* rh_timer protected by hcd_data_lock */
 458        if (timer_pending (&hcd->rh_timer)
 459                        || urb->status != -EINPROGRESS
 460                        || !HCD_IS_RUNNING (hcd->state)
 461                        || urb->transfer_buffer_length < len) {
 462                dbg ("not queuing status urb, stat %d", urb->status);
 463                return -EINVAL;
 464        }
 465
 466        urb->hcpriv = hcd;      /* nonzero to indicate it's queued */
 467        init_timer (&hcd->rh_timer);
 468        hcd->rh_timer.function = rh_report_status;
 469        hcd->rh_timer.data = (unsigned long) urb;
 470        /* USB 2.0 spec says 256msec; this is close enough */
 471        hcd->rh_timer.expires = jiffies + HZ/4;
 472        add_timer (&hcd->rh_timer);
 473        return 0;
 474}
 475
 476/* timer callback */
 477
 478static void rh_report_status (unsigned long ptr)
 479{
 480        struct urb      *urb;
 481        struct usb_hcd  *hcd;
 482        int             length;
 483        unsigned long   flags;
 484
 485        urb = (struct urb *) ptr;
 486        spin_lock_irqsave (&urb->lock, flags);
 487        if (!urb->dev) {
 488                spin_unlock_irqrestore (&urb->lock, flags);
 489                return;
 490        }
 491
 492        hcd = urb->dev->bus->hcpriv;
 493        if (urb->status == -EINPROGRESS) {
 494                if (HCD_IS_RUNNING (hcd->state)) {
 495                        length = hcd->driver->hub_status_data (hcd,
 496                                        urb->transfer_buffer);
 497                        spin_unlock_irqrestore (&urb->lock, flags);
 498                        if (length > 0) {
 499                                urb->actual_length = length;
 500                                urb->status = 0;
 501                                urb->complete (urb);
 502                        }
 503                        spin_lock_irqsave (&hcd_data_lock, flags);
 504                        urb->status = -EINPROGRESS;
 505                        if (HCD_IS_RUNNING (hcd->state)
 506                                        && rh_status_urb (hcd, urb) != 0) {
 507                                /* another driver snuck in? */
 508                                dbg ("%s, can't resubmit roothub status urb?",
 509                                        hcd->bus->bus_name);
 510                                spin_unlock_irqrestore (&hcd_data_lock, flags);
 511                                BUG ();
 512                        }
 513                        spin_unlock_irqrestore (&hcd_data_lock, flags);
 514                } else
 515                        spin_unlock_irqrestore (&urb->lock, flags);
 516        } else {
 517                /* this urb's been unlinked */
 518                urb->hcpriv = 0;
 519                spin_unlock_irqrestore (&urb->lock, flags);
 520
 521                usb_hcd_giveback_urb (hcd, urb, 0);
 522        }
 523}
 524
 525/*-------------------------------------------------------------------------*/
 526
 527static int rh_urb_enqueue (struct usb_hcd *hcd, struct urb *urb)
 528{
 529        if (usb_pipeint (urb->pipe)) {
 530                int             retval;
 531                unsigned long   flags;
 532
 533                spin_lock_irqsave (&hcd_data_lock, flags);
 534                retval = rh_status_urb (hcd, urb);
 535                spin_unlock_irqrestore (&hcd_data_lock, flags);
 536                return retval;
 537        }
 538        if (usb_pipecontrol (urb->pipe))
 539                return rh_call_control (hcd, urb);
 540        else
 541                return -EINVAL;
 542}
 543
 544/*-------------------------------------------------------------------------*/
 545
 546static void rh_status_dequeue (struct usb_hcd *hcd, struct urb *urb)
 547{
 548        unsigned long   flags;
 549
 550        spin_lock_irqsave (&hcd_data_lock, flags);
 551        del_timer_sync (&hcd->rh_timer);
 552        hcd->rh_timer.data = 0;
 553        spin_unlock_irqrestore (&hcd_data_lock, flags);
 554
 555        /* we rely on RH callback code not unlinking its URB! */
 556        usb_hcd_giveback_urb (hcd, urb, 0);
 557}
 558
 559/*-------------------------------------------------------------------------*/
 560
 561#ifdef CONFIG_PCI
 562
 563/* PCI-based HCs are normal, but custom bus glue should be ok */
 564
 565static void hcd_irq (int irq, void *__hcd, struct pt_regs *r);
 566static void hc_died (struct usb_hcd *hcd);
 567
 568/*-------------------------------------------------------------------------*/
 569
 570/* configure so an HC device and id are always provided */
 571/* always called with process context; sleeping is OK */
 572
 573/**
 574 * usb_hcd_pci_probe - initialize PCI-based HCDs
 575 * @dev: USB Host Controller being probed
 576 * @id: pci hotplug id connecting controller to HCD framework
 577 * Context: !in_interrupt()
 578 *
 579 * Allocates basic PCI resources for this USB host controller, and
 580 * then invokes the start() method for the HCD associated with it
 581 * through the hotplug entry's driver_data.
 582 *
 583 * Store this function in the HCD's struct pci_driver as probe().
 584 */
 585int usb_hcd_pci_probe (struct pci_dev *dev, const struct pci_device_id *id)
 586{
 587        struct hc_driver        *driver;
 588        unsigned long           resource, len;
 589        void                    *base;
 590        struct usb_bus          *bus;
 591        struct usb_hcd          *hcd;
 592        int                     retval, region;
 593        char                    buf [8], *bufp = buf;
 594
 595        if (!id || !(driver = (struct hc_driver *) id->driver_data))
 596                return -EINVAL;
 597
 598        if (pci_enable_device (dev) < 0)
 599                return -ENODEV;
 600        
 601        if (!dev->irq) {
 602                err ("Found HC with no IRQ.  Check BIOS/PCI %s setup!",
 603                        dev->slot_name);
 604                return -ENODEV;
 605        }
 606        
 607        if (driver->flags & HCD_MEMORY) {       // EHCI, OHCI
 608                region = 0;
 609                resource = pci_resource_start (dev, 0);
 610                len = pci_resource_len (dev, 0);
 611                if (!request_mem_region (resource, len, driver->description)) {
 612                        dbg ("controller already in use");
 613                        return -EBUSY;
 614                }
 615                base = ioremap_nocache (resource, len);
 616                if (base == NULL) {
 617                        dbg ("error mapping memory");
 618                        retval = -EFAULT;
 619clean_1:
 620                        release_mem_region (resource, len);
 621                        err ("init %s fail, %d", dev->slot_name, retval);
 622                        return retval;
 623                }
 624
 625        } else {                                // UHCI
 626                resource = len = 0;
 627                for (region = 0; region < PCI_ROM_RESOURCE; region++) {
 628                        if (!(pci_resource_flags (dev, region) & IORESOURCE_IO))
 629                                continue;
 630
 631                        resource = pci_resource_start (dev, region);
 632                        len = pci_resource_len (dev, region);
 633                        if (request_region (resource, len,
 634                                        driver->description))
 635                                break;
 636                }
 637                if (region == PCI_ROM_RESOURCE) {
 638                        dbg ("no i/o regions available");
 639                        return -EBUSY;
 640                }
 641                base = (void *) resource;
 642        }
 643
 644        // driver->start(), later on, will transfer device from
 645        // control by SMM/BIOS to control by Linux (if needed)
 646
 647        pci_set_master (dev);
 648        hcd = driver->hcd_alloc ();
 649        if (hcd == NULL){
 650                dbg ("hcd alloc fail");
 651                retval = -ENOMEM;
 652clean_2:
 653                if (driver->flags & HCD_MEMORY) {
 654                        iounmap (base);
 655                        goto clean_1;
 656                } else {
 657                        release_region (resource, len);
 658                        err ("init %s fail, %d", dev->slot_name, retval);
 659                        return retval;
 660                }
 661        }
 662        pci_set_drvdata(dev, hcd);
 663        hcd->driver = driver;
 664        hcd->description = driver->description;
 665        hcd->pdev = dev;
 666        printk (KERN_INFO "%s %s: %s\n",
 667                        hcd->description,  dev->slot_name, dev->name);
 668
 669#ifndef __sparc__
 670        sprintf (buf, "%d", dev->irq);
 671#else
 672        bufp = __irq_itoa(dev->irq);
 673#endif
 674        if (request_irq (dev->irq, hcd_irq, SA_SHIRQ, hcd->description, hcd)
 675                        != 0) {
 676                err ("request interrupt %s failed", bufp);
 677                retval = -EBUSY;
 678clean_3:
 679                driver->hcd_free (hcd);
 680                goto clean_2;
 681        }
 682        hcd->irq = dev->irq;
 683
 684        hcd->regs = base;
 685        hcd->region = region;
 686        printk (KERN_INFO "%s %s: irq %s, %s %p\n",
 687                hcd->description,  dev->slot_name, bufp,
 688                (driver->flags & HCD_MEMORY) ? "pci mem" : "io base",
 689                base);
 690
 691// FIXME simpler: make "bus" be that data, not pointer to it.
 692// (fixed in 2.5)
 693        bus = usb_alloc_bus (&hcd_operations);
 694        if (bus == NULL) {
 695                dbg ("usb_alloc_bus fail");
 696                retval = -ENOMEM;
 697                free_irq (dev->irq, hcd);
 698                goto clean_3;
 699        }
 700        hcd->bus = bus;
 701        bus->bus_name = dev->slot_name;
 702        hcd->product_desc = dev->name;
 703        bus->hcpriv = (void *) hcd;
 704
 705        INIT_LIST_HEAD (&hcd->dev_list);
 706        INIT_LIST_HEAD (&hcd->hcd_list);
 707
 708        down (&hcd_list_lock);
 709        list_add (&hcd->hcd_list, &hcd_list);
 710        up (&hcd_list_lock);
 711
 712        usb_register_bus (bus);
 713
 714        if ((retval = driver->start (hcd)) < 0)
 715                usb_hcd_pci_remove (dev);
 716
 717        return retval;
 718} 
 719EXPORT_SYMBOL (usb_hcd_pci_probe);
 720
 721
 722/* may be called without controller electrically present */
 723/* may be called with controller, bus, and devices active */
 724
 725/**
 726 * usb_hcd_pci_remove - shutdown processing for PCI-based HCDs
 727 * @dev: USB Host Controller being removed
 728 * Context: !in_interrupt()
 729 *
 730 * Reverses the effect of usb_hcd_pci_probe(), first invoking
 731 * the HCD's stop() method.  It is always called from a thread
 732 * context, normally "rmmod", "apmd", or something similar.
 733 *
 734 * Store this function in the HCD's struct pci_driver as remove().
 735 */
 736void usb_hcd_pci_remove (struct pci_dev *dev)
 737{
 738        struct usb_hcd          *hcd;
 739        struct usb_device       *hub;
 740
 741        hcd = pci_get_drvdata(dev);
 742        if (!hcd)
 743                return;
 744        printk (KERN_INFO "%s %s: remove state %x\n",
 745                hcd->description,  dev->slot_name, hcd->state);
 746
 747        if (in_interrupt ()) BUG ();
 748
 749        hub = hcd->bus->root_hub;
 750        hcd->state = USB_STATE_QUIESCING;
 751
 752        dbg ("%s: roothub graceful disconnect", hcd->bus->bus_name);
 753        usb_disconnect (&hub);
 754        // usb_disconnect (&hcd->bus->root_hub);
 755
 756        hcd->driver->stop (hcd);
 757        hcd->state = USB_STATE_HALT;
 758
 759        free_irq (hcd->irq, hcd);
 760        if (hcd->driver->flags & HCD_MEMORY) {
 761                iounmap (hcd->regs);
 762                release_mem_region (pci_resource_start (dev, 0),
 763                        pci_resource_len (dev, 0));
 764        } else {
 765                release_region (pci_resource_start (dev, hcd->region),
 766                        pci_resource_len (dev, hcd->region));
 767        }
 768
 769        down (&hcd_list_lock);
 770        list_del (&hcd->hcd_list);
 771        up (&hcd_list_lock);
 772
 773        usb_deregister_bus (hcd->bus);
 774        usb_free_bus (hcd->bus);
 775        hcd->bus = NULL;
 776
 777        hcd->driver->hcd_free (hcd);
 778}
 779EXPORT_SYMBOL (usb_hcd_pci_remove);
 780
 781
 782#ifdef  CONFIG_PM
 783
 784/*
 785 * Some "sleep" power levels imply updating struct usb_driver
 786 * to include a callback asking hcds to do their bit by checking
 787 * if all the drivers can suspend.  Gets involved with remote wakeup.
 788 *
 789 * If there are pending urbs, then HCs will need to access memory,
 790 * causing extra power drain.  New sleep()/wakeup() PM calls might
 791 * be needed, beyond PCI suspend()/resume().  The root hub timer
 792 * still be accessing memory though ...
 793 *
 794 * FIXME:  USB should have some power budgeting support working with
 795 * all kinds of hubs.
 796 *
 797 * FIXME:  This assumes only D0->D3 suspend and D3->D0 resume.
 798 * D1 and D2 states should do something, yes?
 799 *
 800 * FIXME:  Should provide generic enable_wake(), calling pci_enable_wake()
 801 * for all supported states, so that USB remote wakeup can work for any
 802 * devices that support it (and are connected via powered hubs).
 803 *
 804 * FIXME:  resume doesn't seem to work right any more...
 805 */
 806
 807
 808// 2.4 kernels have issued concurrent resumes (w/APM)
 809// we defend against that error; PCI doesn't yet.
 810
 811/**
 812 * usb_hcd_pci_suspend - power management suspend of a PCI-based HCD
 813 * @dev: USB Host Controller being suspended
 814 *
 815 * Store this function in the HCD's struct pci_driver as suspend().
 816 */
 817int usb_hcd_pci_suspend (struct pci_dev *dev, u32 state)
 818{
 819        struct usb_hcd          *hcd;
 820        int                     retval;
 821
 822        hcd = pci_get_drvdata(dev);
 823        printk (KERN_INFO "%s %s: suspend to state %d\n",
 824                hcd->description,  dev->slot_name, state);
 825
 826        pci_save_state (dev, hcd->pci_state);
 827
 828        // FIXME for all connected devices, leaf-to-root:
 829        // driver->suspend()
 830        // proposed "new 2.5 driver model" will automate that
 831
 832        /* driver may want to disable DMA etc */
 833        retval = hcd->driver->suspend (hcd, state);
 834        hcd->state = USB_STATE_SUSPENDED;
 835
 836        pci_set_power_state (dev, state);
 837        return retval;
 838}
 839EXPORT_SYMBOL (usb_hcd_pci_suspend);
 840
 841/**
 842 * usb_hcd_pci_resume - power management resume of a PCI-based HCD
 843 * @dev: USB Host Controller being resumed
 844 *
 845 * Store this function in the HCD's struct pci_driver as resume().
 846 */
 847int usb_hcd_pci_resume (struct pci_dev *dev)
 848{
 849        struct usb_hcd          *hcd;
 850        int                     retval;
 851
 852        hcd = pci_get_drvdata(dev);
 853        printk (KERN_INFO "%s %s: resume\n",
 854                hcd->description,  dev->slot_name);
 855
 856        /* guard against multiple resumes (APM bug?) */
 857        atomic_inc (&hcd->resume_count);
 858        if (atomic_read (&hcd->resume_count) != 1) {
 859                err ("concurrent PCI resumes for %s", hcd->bus->bus_name);
 860                retval = 0;
 861                goto done;
 862        }
 863
 864        retval = -EBUSY;
 865        if (hcd->state != USB_STATE_SUSPENDED) {
 866                dbg ("can't resume, not suspended!");
 867                goto done;
 868        }
 869        hcd->state = USB_STATE_RESUMING;
 870
 871        pci_set_power_state (dev, 0);
 872        pci_restore_state (dev, hcd->pci_state);
 873
 874        retval = hcd->driver->resume (hcd);
 875        if (!HCD_IS_RUNNING (hcd->state)) {
 876                dbg ("resume %s failure, retval %d",
 877                        hcd->bus->bus_name, retval);
 878                hc_died (hcd);
 879// FIXME:  recover, reset etc.
 880        } else {
 881                // FIXME for all connected devices, root-to-leaf:
 882                // driver->resume ();
 883                // proposed "new 2.5 driver model" will automate that
 884        }
 885
 886done:
 887        atomic_dec (&hcd->resume_count);
 888        return retval;
 889}
 890EXPORT_SYMBOL (usb_hcd_pci_resume);
 891
 892#endif  /* CONFIG_PM */
 893
 894#endif
 895
 896/*-------------------------------------------------------------------------*/
 897
 898/*
 899 * Generic HC operations.
 900 */
 901
 902/*-------------------------------------------------------------------------*/
 903
 904/* called from khubd, or root hub init threads for hcd-private init */
 905static int hcd_alloc_dev (struct usb_device *udev)
 906{
 907        struct hcd_dev          *dev;
 908        struct usb_hcd          *hcd;
 909        unsigned long           flags;
 910
 911        if (!udev || udev->hcpriv)
 912                return -EINVAL;
 913        if (!udev->bus || !udev->bus->hcpriv)
 914                return -ENODEV;
 915        hcd = udev->bus->hcpriv;
 916        if (hcd->state == USB_STATE_QUIESCING)
 917                return -ENOLINK;
 918
 919        dev = (struct hcd_dev *) kmalloc (sizeof *dev, GFP_KERNEL);
 920        if (dev == NULL)
 921                return -ENOMEM;
 922        memset (dev, 0, sizeof *dev);
 923
 924        INIT_LIST_HEAD (&dev->dev_list);
 925        INIT_LIST_HEAD (&dev->urb_list);
 926
 927        spin_lock_irqsave (&hcd_data_lock, flags);
 928        list_add (&dev->dev_list, &hcd->dev_list);
 929        // refcount is implicit
 930        udev->hcpriv = dev;
 931        spin_unlock_irqrestore (&hcd_data_lock, flags);
 932
 933        return 0;
 934}
 935
 936/*-------------------------------------------------------------------------*/
 937
 938static void hcd_panic (void *_hcd)
 939{
 940        struct usb_hcd *hcd = _hcd;
 941        hcd->driver->stop (hcd);
 942}
 943
 944static void hc_died (struct usb_hcd *hcd)
 945{
 946        struct list_head        *devlist, *urblist;
 947        struct hcd_dev          *dev;
 948        struct urb              *urb;
 949        unsigned long           flags;
 950        
 951        /* flag every pending urb as done */
 952        spin_lock_irqsave (&hcd_data_lock, flags);
 953        list_for_each (devlist, &hcd->dev_list) {
 954                dev = list_entry (devlist, struct hcd_dev, dev_list);
 955                list_for_each (urblist, &dev->urb_list) {
 956                        urb = list_entry (urblist, struct urb, urb_list);
 957                        dbg ("shutdown %s urb %p pipe %x, current status %d",
 958                                hcd->bus->bus_name,
 959                                urb, urb->pipe, urb->status);
 960                        if (urb->status == -EINPROGRESS)
 961                                urb->status = -ESHUTDOWN;
 962                }
 963        }
 964        urb = (struct urb *) hcd->rh_timer.data;
 965        if (urb)
 966                urb->status = -ESHUTDOWN;
 967        spin_unlock_irqrestore (&hcd_data_lock, flags);
 968
 969        if (urb)
 970                rh_status_dequeue (hcd, urb);
 971
 972        /* hcd->stop() needs a task context */
 973        INIT_TQUEUE (&hcd->work, hcd_panic, hcd);
 974        (void) schedule_task (&hcd->work);
 975}
 976
 977/*-------------------------------------------------------------------------*/
 978
 979static void urb_unlink (struct urb *urb)
 980{
 981        unsigned long           flags;
 982        struct usb_device       *dev;
 983
 984        /* Release any periodic transfer bandwidth */
 985        if (urb->bandwidth)
 986                usb_release_bandwidth (urb->dev, urb,
 987                        usb_pipeisoc (urb->pipe));
 988
 989        /* clear all state linking urb to this dev (and hcd) */
 990
 991        spin_lock_irqsave (&hcd_data_lock, flags);
 992        list_del_init (&urb->urb_list);
 993        dev = urb->dev;
 994        urb->dev = NULL;
 995        usb_dec_dev_use (dev);
 996        spin_unlock_irqrestore (&hcd_data_lock, flags);
 997}
 998
 999
1000/* may be called in any context with a valid urb->dev usecount */
1001/* caller surrenders "ownership" of urb */
1002
1003static int hcd_submit_urb (struct urb *urb)
1004{
1005        int                     status;
1006        struct usb_hcd          *hcd;
1007        struct hcd_dev          *dev;
1008        unsigned long           flags;
1009        int                     pipe, temp, max;
1010        int                     mem_flags;
1011
1012        if (!urb || urb->hcpriv || !urb->complete)
1013                return -EINVAL;
1014
1015        urb->status = -EINPROGRESS;
1016        urb->actual_length = 0;
1017        urb->bandwidth = 0;
1018        INIT_LIST_HEAD (&urb->urb_list);
1019
1020        if (!urb->dev || !urb->dev->bus || urb->dev->devnum <= 0)
1021                return -ENODEV;
1022        hcd = urb->dev->bus->hcpriv;
1023        dev = urb->dev->hcpriv;
1024        if (!hcd || !dev)
1025                return -ENODEV;
1026
1027        /* can't submit new urbs when quiescing, halted, ... */
1028        if (hcd->state == USB_STATE_QUIESCING || !HCD_IS_RUNNING (hcd->state))
1029                return -ESHUTDOWN;
1030        pipe = urb->pipe;
1031        temp = usb_pipetype (urb->pipe);
1032        if (usb_endpoint_halted (urb->dev, usb_pipeendpoint (pipe),
1033                        usb_pipeout (pipe)))
1034                return -EPIPE;
1035
1036        /* NOTE: 2.5 passes this value explicitly in submit() */
1037        mem_flags = GFP_ATOMIC;
1038
1039        /* FIXME there should be a sharable lock protecting us against
1040         * config/altsetting changes and disconnects, kicking in here.
1041         */
1042
1043        /* Sanity check, so HCDs can rely on clean data */
1044        max = usb_maxpacket (urb->dev, pipe, usb_pipeout (pipe));
1045        if (max <= 0) {
1046                err ("bogus endpoint (bad maxpacket)");
1047                return -EINVAL;
1048        }
1049
1050        /* "high bandwidth" mode, 1-3 packets/uframe? */
1051        if (urb->dev->speed == USB_SPEED_HIGH) {
1052                int     mult;
1053                switch (temp) {
1054                case PIPE_ISOCHRONOUS:
1055                case PIPE_INTERRUPT:
1056                        mult = 1 + ((max >> 11) & 0x03);
1057                        max &= 0x03ff;
1058                        max *= mult;
1059                }
1060        }
1061
1062        /* periodic transfers limit size per frame/uframe */
1063        switch (temp) {
1064        case PIPE_ISOCHRONOUS: {
1065                int     n, len;
1066
1067                if (urb->number_of_packets <= 0)                    
1068                        return -EINVAL;
1069                for (n = 0; n < urb->number_of_packets; n++) {
1070                        len = urb->iso_frame_desc [n].length;
1071                        if (len < 0 || len > max) 
1072                                return -EINVAL;
1073                }
1074
1075                }
1076                break;
1077        case PIPE_INTERRUPT:
1078                if (urb->transfer_buffer_length > max)
1079                        return -EINVAL;
1080        }
1081
1082        /* the I/O buffer must usually be mapped/unmapped */
1083        if (urb->transfer_buffer_length < 0)
1084                return -EINVAL;
1085
1086        if (urb->next) {
1087                warn ("use explicit queuing not urb->next");
1088                return -EINVAL;
1089        }
1090
1091#ifdef DEBUG
1092        /* stuff that drivers shouldn't do, but which shouldn't
1093         * cause problems in HCDs if they get it wrong.
1094         */
1095        {
1096        unsigned int    orig_flags = urb->transfer_flags;
1097        unsigned int    allowed;
1098
1099        /* enforce simple/standard policy */
1100        allowed = USB_ASYNC_UNLINK;     // affects later unlinks
1101        allowed |= USB_NO_FSBR;         // only affects UHCI
1102        switch (temp) {
1103        case PIPE_CONTROL:
1104                allowed |= USB_DISABLE_SPD;
1105                break;
1106        case PIPE_BULK:
1107                allowed |= USB_DISABLE_SPD | USB_QUEUE_BULK
1108                                | USB_ZERO_PACKET | URB_NO_INTERRUPT;
1109                break;
1110        case PIPE_INTERRUPT:
1111                allowed |= USB_DISABLE_SPD;
1112                break;
1113        case PIPE_ISOCHRONOUS:
1114                allowed |= USB_ISO_ASAP;
1115                break;
1116        }
1117        urb->transfer_flags &= allowed;
1118
1119        /* fail if submitter gave bogus flags */
1120        if (urb->transfer_flags != orig_flags) {
1121                err ("BOGUS urb flags, %x --> %x",
1122                        orig_flags, urb->transfer_flags);
1123                return -EINVAL;
1124        }
1125        }
1126#endif
1127        /*
1128         * Force periodic transfer intervals to be legal values that are
1129         * a power of two (so HCDs don't need to).
1130         *
1131         * FIXME want bus->{intr,iso}_sched_horizon values here.  Each HC
1132         * supports different values... this uses EHCI/UHCI defaults (and
1133         * EHCI can use smaller non-default values).
1134         */
1135        switch (temp) {
1136        case PIPE_ISOCHRONOUS:
1137        case PIPE_INTERRUPT:
1138                /* too small? */
1139                if (urb->interval <= 0)
1140                        return -EINVAL;
1141                /* too big? */
1142                switch (urb->dev->speed) {
1143                case USB_SPEED_HIGH:    /* units are microframes */
1144                        // NOTE usb handles 2^15
1145                        if (urb->interval > (1024 * 8))
1146                                urb->interval = 1024 * 8;
1147                        temp = 1024 * 8;
1148                        break;
1149                case USB_SPEED_FULL:    /* units are frames/msec */
1150                case USB_SPEED_LOW:
1151                        if (temp == PIPE_INTERRUPT) {
1152                                if (urb->interval > 255)
1153                                        return -EINVAL;
1154                                // NOTE ohci only handles up to 32
1155                                temp = 128;
1156                        } else {
1157                                if (urb->interval > 1024)
1158                                        urb->interval = 1024;
1159                                // NOTE usb and ohci handle up to 2^15
1160                                temp = 1024;
1161                        }
1162                        break;
1163                default:
1164                        return -EINVAL;
1165                }
1166                /* power of two? */
1167                while (temp > urb->interval)
1168                        temp >>= 1;
1169                urb->interval = temp;
1170        }
1171
1172
1173        /*
1174         * FIXME:  make urb timeouts be generic, keeping the HCD cores
1175         * as simple as possible.
1176         */
1177
1178        // NOTE:  a generic device/urb monitoring hook would go here.
1179        // hcd_monitor_hook(MONITOR_URB_SUBMIT, urb)
1180        // It would catch submission paths for all urbs.
1181
1182        /*
1183         * Atomically queue the urb,  first to our records, then to the HCD.
1184         * Access to urb->status is controlled by urb->lock ... changes on
1185         * i/o completion (normal or fault) or unlinking.
1186         */
1187
1188        // FIXME:  verify that quiescing hc works right (RH cleans up)
1189
1190        spin_lock_irqsave (&hcd_data_lock, flags);
1191        if (HCD_IS_RUNNING (hcd->state) && hcd->state != USB_STATE_QUIESCING) {
1192                usb_inc_dev_use (urb->dev);
1193                list_add (&urb->urb_list, &dev->urb_list);
1194                status = 0;
1195        } else {
1196                INIT_LIST_HEAD (&urb->urb_list);
1197                status = -ESHUTDOWN;
1198        }
1199        spin_unlock_irqrestore (&hcd_data_lock, flags);
1200        if (status)
1201                return status;
1202
1203        // NOTE:  2.5 does this if !URB_NO_DMA_MAP transfer flag
1204        
1205        /* For 2.4, don't map bounce buffer if it's a root hub operation. */
1206        if (urb->dev == hcd->bus->root_hub) {
1207                status = rh_urb_enqueue (hcd, urb);
1208        } else {
1209                if (usb_pipecontrol (urb->pipe))
1210                        urb->setup_dma = pci_map_single (
1211                                        hcd->pdev,
1212                                        urb->setup_packet,
1213                                        sizeof (struct usb_ctrlrequest),
1214                                        PCI_DMA_TODEVICE);
1215                if (urb->transfer_buffer_length != 0)
1216                        urb->transfer_dma = pci_map_single (
1217                                        hcd->pdev,
1218                                        urb->transfer_buffer,
1219                                        urb->transfer_buffer_length,
1220                                        usb_pipein (urb->pipe)
1221                                            ? PCI_DMA_FROMDEVICE
1222                                            : PCI_DMA_TODEVICE);
1223                status = hcd->driver->urb_enqueue (hcd, urb, mem_flags);
1224        }
1225        return status;
1226}
1227
1228/*-------------------------------------------------------------------------*/
1229
1230/* called in any context */
1231static int hcd_get_frame_number (struct usb_device *udev)
1232{
1233        struct usb_hcd  *hcd = (struct usb_hcd *)udev->bus->hcpriv;
1234        return hcd->driver->get_frame_number (hcd);
1235}
1236
1237/*-------------------------------------------------------------------------*/
1238
1239struct completion_splice {              // modified urb context:
1240        /* did we complete? */
1241        struct completion       done;
1242
1243        /* original urb data */
1244        void                    (*complete)(struct urb *);
1245        void                    *context;
1246};
1247
1248static void unlink_complete (struct urb *urb)
1249{
1250        struct completion_splice        *splice;
1251
1252        splice = (struct completion_splice *) urb->context;
1253
1254        /* issue original completion call */
1255        urb->complete = splice->complete;
1256        urb->context = splice->context;
1257        urb->complete (urb);
1258
1259        /* then let the synchronous unlink call complete */
1260        complete (&splice->done);
1261}
1262
1263/*
1264 * called in any context; note ASYNC_UNLINK restrictions
1265 *
1266 * caller guarantees urb won't be recycled till both unlink()
1267 * and the urb's completion function return
1268 */
1269static int hcd_unlink_urb (struct urb *urb)
1270{
1271        struct hcd_dev                  *dev;
1272        struct usb_hcd                  *hcd = 0;
1273        unsigned long                   flags;
1274        struct completion_splice        splice;
1275        int                             retval;
1276
1277        if (!urb)
1278                return -EINVAL;
1279
1280        /*
1281         * we contend for urb->status with the hcd core,
1282         * which changes it while returning the urb.
1283         *
1284         * Caller guaranteed that the urb pointer hasn't been freed, and
1285         * that it was submitted.  But as a rule it can't know whether or
1286         * not it's already been unlinked ... so we respect the reversed
1287         * lock sequence needed for the usb_hcd_giveback_urb() code paths
1288         * (urb lock, then hcd_data_lock) in case some other CPU is now
1289         * unlinking it.
1290         */
1291        spin_lock_irqsave (&urb->lock, flags);
1292        spin_lock (&hcd_data_lock);
1293        if (!urb->hcpriv || urb->transfer_flags & USB_TIMEOUT_KILLED) {
1294                retval = -EINVAL;
1295                goto done;
1296        }
1297
1298        if (!urb->dev || !urb->dev->bus) {
1299                retval = -ENODEV;
1300                goto done;
1301        }
1302
1303        /* giveback clears dev; non-null means it's linked at this level */
1304        dev = urb->dev->hcpriv;
1305        hcd = urb->dev->bus->hcpriv;
1306        if (!dev || !hcd) {
1307                retval = -ENODEV;
1308                goto done;
1309        }
1310
1311        /* Any status except -EINPROGRESS means the HCD has already started
1312         * to return this URB to the driver.  In that case, there's no
1313         * more work for us to do.
1314         *
1315         * There's much magic because of "automagic resubmit" of interrupt
1316         * transfers, stopped only by explicit unlinking.  We won't issue
1317         * an "it's unlinked" callback more than once, but device drivers
1318         * can need to retry (SMP, -EAGAIN) an unlink request as well as
1319         * fake out the "not yet completed" state (set -EINPROGRESS) if
1320         * unlinking from complete().  Automagic eventually vanishes.
1321         *
1322         * FIXME use an URB_UNLINKED flag to match URB_TIMEOUT_KILLED
1323         */
1324        if (urb->status != -EINPROGRESS) {
1325                if (usb_pipetype (urb->pipe) == PIPE_INTERRUPT)
1326                        retval = -EAGAIN;
1327                else
1328                        retval = -EBUSY;
1329                goto done;
1330        }
1331
1332        /* maybe set up to block on completion notification */
1333        if ((urb->transfer_flags & USB_TIMEOUT_KILLED))
1334                urb->status = -ETIMEDOUT;
1335        else if (!(urb->transfer_flags & USB_ASYNC_UNLINK)) {
1336                if (in_interrupt ()) {
1337                        dbg ("non-async unlink in_interrupt");
1338                        retval = -EWOULDBLOCK;
1339                        goto done;
1340                }
1341                /* synchronous unlink: block till we see the completion */
1342                init_completion (&splice.done);
1343                splice.complete = urb->complete;
1344                splice.context = urb->context;
1345                urb->complete = unlink_complete;
1346                urb->context = &splice;
1347                urb->status = -ENOENT;
1348        } else {
1349                /* asynchronous unlink */
1350                urb->status = -ECONNRESET;
1351        }
1352        spin_unlock (&hcd_data_lock);
1353        spin_unlock_irqrestore (&urb->lock, flags);
1354
1355        if (urb == (struct urb *) hcd->rh_timer.data) {
1356                rh_status_dequeue (hcd, urb);
1357                retval = 0;
1358        } else {
1359                retval = hcd->driver->urb_dequeue (hcd, urb);
1360// FIXME:  if retval and we tried to splice, whoa!!
1361if (retval && urb->status == -ENOENT) err ("whoa! retval %d", retval);
1362        }
1363
1364        /* block till giveback, if needed */
1365        if (!(urb->transfer_flags & (USB_ASYNC_UNLINK|USB_TIMEOUT_KILLED))
1366                        && HCD_IS_RUNNING (hcd->state)
1367                        && !retval) {
1368                wait_for_completion (&splice.done);
1369        } else if ((urb->transfer_flags & USB_ASYNC_UNLINK) && retval == 0) {
1370                return -EINPROGRESS;
1371        }
1372        goto bye;
1373done:
1374        spin_unlock (&hcd_data_lock);
1375        spin_unlock_irqrestore (&urb->lock, flags);
1376bye:
1377        if (retval)
1378                dbg ("%s: hcd_unlink_urb fail %d",
1379                    hcd ? hcd->bus->bus_name : "(no bus?)",
1380                    retval);
1381        return retval;
1382}
1383
1384/*-------------------------------------------------------------------------*/
1385
1386/* called by khubd, rmmod, apmd, or other thread for hcd-private cleanup */
1387
1388// FIXME:  likely best to have explicit per-setting (config+alt)
1389// setup primitives in the usbcore-to-hcd driver API, so nothing
1390// is implicit.  kernel 2.5 needs a bunch of config cleanup...
1391
1392static int hcd_free_dev (struct usb_device *udev)
1393{
1394        struct hcd_dev          *dev;
1395        struct usb_hcd          *hcd;
1396        unsigned long           flags;
1397
1398        if (!udev || !udev->hcpriv)
1399                return -EINVAL;
1400
1401        if (!udev->bus || !udev->bus->hcpriv)
1402                return -ENODEV;
1403
1404        // should udev->devnum == -1 ??
1405
1406        dev = udev->hcpriv;
1407        hcd = udev->bus->hcpriv;
1408
1409        /* device driver problem with refcounts? */
1410        if (!list_empty (&dev->urb_list)) {
1411                dbg ("free busy dev, %s devnum %d (bug!)",
1412                        hcd->bus->bus_name, udev->devnum);
1413                return -EINVAL;
1414        }
1415
1416        hcd->driver->free_config (hcd, udev);
1417
1418        spin_lock_irqsave (&hcd_data_lock, flags);
1419        list_del (&dev->dev_list);
1420        udev->hcpriv = NULL;
1421        spin_unlock_irqrestore (&hcd_data_lock, flags);
1422
1423        kfree (dev);
1424        return 0;
1425}
1426
1427static struct usb_operations hcd_operations = {
1428        allocate:               hcd_alloc_dev,
1429        get_frame_number:       hcd_get_frame_number,
1430        submit_urb:             hcd_submit_urb,
1431        unlink_urb:             hcd_unlink_urb,
1432        deallocate:             hcd_free_dev,
1433};
1434
1435/*-------------------------------------------------------------------------*/
1436
1437static void hcd_irq (int irq, void *__hcd, struct pt_regs * r)
1438{
1439        struct usb_hcd          *hcd = __hcd;
1440        int                     start = hcd->state;
1441
1442        if (unlikely (hcd->state == USB_STATE_HALT))    /* irq sharing? */
1443                return;
1444
1445        hcd->driver->irq (hcd, r);
1446        if (hcd->state != start && hcd->state == USB_STATE_HALT)
1447                hc_died (hcd);
1448}
1449
1450/*-------------------------------------------------------------------------*/
1451
1452/**
1453 * usb_hcd_giveback_urb - return URB from HCD to device driver
1454 * @hcd: host controller returning the URB
1455 * @urb: urb being returned to the USB device driver.
1456 * @regs: saved hardware registers (ignored on 2.4 kernels)
1457 * Context: in_interrupt()
1458 *
1459 * This hands the URB from HCD to its USB device driver, using its
1460 * completion function.  The HCD has freed all per-urb resources
1461 * (and is done using urb->hcpriv).  It also released all HCD locks;
1462 * the device driver won't cause deadlocks if it resubmits this URB,
1463 * and won't confuse things by modifying and resubmitting this one.
1464 * Bandwidth and other resources will be deallocated.
1465 *
1466 * HCDs must not use this for periodic URBs that are still scheduled
1467 * and will be reissued.  They should just call their completion handlers
1468 * until the urb is returned to the device driver by unlinking.
1469 *
1470 * NOTE that no urb->next processing is done, even for isochronous URBs.
1471 * ISO streaming functionality can be achieved by having completion handlers
1472 * re-queue URBs.  Such explicit queuing doesn't discard error reports.
1473 */
1474void usb_hcd_giveback_urb (struct usb_hcd *hcd, struct urb *urb, struct pt_regs *regs)
1475{
1476        int is_root_hub_operation;
1477
1478        /* Work this out here as urb_unlink clears urb->dev */
1479        is_root_hub_operation = (urb->dev == hcd->bus->root_hub);
1480
1481        urb_unlink (urb);
1482
1483        // NOTE:  a generic device/urb monitoring hook would go here.
1484        // hcd_monitor_hook(MONITOR_URB_FINISH, urb, dev)
1485        // It would catch exit/unlink paths for all urbs, but non-exit
1486        // completions for periodic urbs need hooks inside the HCD.
1487        // hcd_monitor_hook(MONITOR_URB_UPDATE, urb, dev)
1488
1489        // NOTE:  2.5 does this if !URB_NO_DMA_MAP transfer flag
1490        
1491        /* For 2.4, don't unmap bounce buffer if it's a root hub operation. */
1492        if (usb_pipecontrol (urb->pipe) && !is_root_hub_operation)
1493                pci_unmap_single (hcd->pdev, urb->setup_dma,
1494                                sizeof (struct usb_ctrlrequest),
1495                                PCI_DMA_TODEVICE);
1496
1497        if ((urb->transfer_buffer_length != 0) && !is_root_hub_operation)
1498                pci_unmap_single (hcd->pdev, urb->transfer_dma,
1499                                urb->transfer_buffer_length,
1500                                usb_pipein (urb->pipe)
1501                                    ? PCI_DMA_FROMDEVICE
1502                                    : PCI_DMA_TODEVICE);
1503
1504        /* pass ownership to the completion handler */
1505        urb->complete (urb);
1506}
1507EXPORT_SYMBOL (usb_hcd_giveback_urb);
1508
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.