linux/drivers/usb/gadget/net2280.c
<<
>>
Prefs
   1/*
   2 * Driver for the PLX NET2280 USB device controller.
   3 * Specs and errata are available from <http://www.plxtech.com>.
   4 *
   5 * PLX Technology Inc. (formerly NetChip Technology) supported the
   6 * development of this driver.
   7 *
   8 *
   9 * CODE STATUS HIGHLIGHTS
  10 *
  11 * This driver should work well with most "gadget" drivers, including
  12 * the Mass Storage, Serial, and Ethernet/RNDIS gadget drivers
  13 * as well as Gadget Zero and Gadgetfs.
  14 *
  15 * DMA is enabled by default.  Drivers using transfer queues might use
  16 * DMA chaining to remove IRQ latencies between transfers.  (Except when
  17 * short OUT transfers happen.)  Drivers can use the req->no_interrupt
  18 * hint to completely eliminate some IRQs, if a later IRQ is guaranteed
  19 * and DMA chaining is enabled.
  20 *
  21 * Note that almost all the errata workarounds here are only needed for
  22 * rev1 chips.  Rev1a silicon (0110) fixes almost all of them.
  23 */
  24
  25/*
  26 * Copyright (C) 2003 David Brownell
  27 * Copyright (C) 2003-2005 PLX Technology, Inc.
  28 *
  29 * Modified Seth Levy 2005 PLX Technology, Inc. to provide compatibility
  30 *      with 2282 chip
  31 *
  32 * This program is free software; you can redistribute it and/or modify
  33 * it under the terms of the GNU General Public License as published by
  34 * the Free Software Foundation; either version 2 of the License, or
  35 * (at your option) any later version.
  36 */
  37
  38#undef  DEBUG           /* messages on error and most fault paths */
  39#undef  VERBOSE         /* extra debug messages (success too) */
  40
  41#include <linux/module.h>
  42#include <linux/pci.h>
  43#include <linux/dma-mapping.h>
  44#include <linux/kernel.h>
  45#include <linux/delay.h>
  46#include <linux/ioport.h>
  47#include <linux/slab.h>
  48#include <linux/errno.h>
  49#include <linux/init.h>
  50#include <linux/timer.h>
  51#include <linux/list.h>
  52#include <linux/interrupt.h>
  53#include <linux/moduleparam.h>
  54#include <linux/device.h>
  55#include <linux/usb/ch9.h>
  56#include <linux/usb/gadget.h>
  57#include <linux/prefetch.h>
  58
  59#include <asm/byteorder.h>
  60#include <asm/io.h>
  61#include <asm/irq.h>
  62#include <asm/unaligned.h>
  63
  64
  65#define DRIVER_DESC             "PLX NET228x USB Peripheral Controller"
  66#define DRIVER_VERSION          "2005 Sept 27"
  67
  68#define DMA_ADDR_INVALID        (~(dma_addr_t)0)
  69#define EP_DONTUSE              13      /* nonzero */
  70
  71#define USE_RDK_LEDS            /* GPIO pins control three LEDs */
  72
  73
  74static const char driver_name [] = "net2280";
  75static const char driver_desc [] = DRIVER_DESC;
  76
  77static const char ep0name [] = "ep0";
  78static const char *const ep_name [] = {
  79        ep0name,
  80        "ep-a", "ep-b", "ep-c", "ep-d",
  81        "ep-e", "ep-f",
  82};
  83
  84/* use_dma -- general goodness, fewer interrupts, less cpu load (vs PIO)
  85 * use_dma_chaining -- dma descriptor queueing gives even more irq reduction
  86 *
  87 * The net2280 DMA engines are not tightly integrated with their FIFOs;
  88 * not all cases are (yet) handled well in this driver or the silicon.
  89 * Some gadget drivers work better with the dma support here than others.
  90 * These two parameters let you use PIO or more aggressive DMA.
  91 */
  92static bool use_dma = 1;
  93static bool use_dma_chaining = 0;
  94
  95/* "modprobe net2280 use_dma=n" etc */
  96module_param (use_dma, bool, S_IRUGO);
  97module_param (use_dma_chaining, bool, S_IRUGO);
  98
  99
 100/* mode 0 == ep-{a,b,c,d} 1K fifo each
 101 * mode 1 == ep-{a,b} 2K fifo each, ep-{c,d} unavailable
 102 * mode 2 == ep-a 2K fifo, ep-{b,c} 1K each, ep-d unavailable
 103 */
 104static ushort fifo_mode = 0;
 105
 106/* "modprobe net2280 fifo_mode=1" etc */
 107module_param (fifo_mode, ushort, 0644);
 108
 109/* enable_suspend -- When enabled, the driver will respond to
 110 * USB suspend requests by powering down the NET2280.  Otherwise,
 111 * USB suspend requests will be ignored.  This is acceptable for
 112 * self-powered devices
 113 */
 114static bool enable_suspend = 0;
 115
 116/* "modprobe net2280 enable_suspend=1" etc */
 117module_param (enable_suspend, bool, S_IRUGO);
 118
 119/* force full-speed operation */
 120static bool full_speed;
 121module_param(full_speed, bool, 0444);
 122MODULE_PARM_DESC(full_speed, "force full-speed mode -- for testing only!");
 123
 124#define DIR_STRING(bAddress) (((bAddress) & USB_DIR_IN) ? "in" : "out")
 125
 126#if defined(CONFIG_USB_GADGET_DEBUG_FILES) || defined (DEBUG)
 127static char *type_string (u8 bmAttributes)
 128{
 129        switch ((bmAttributes) & USB_ENDPOINT_XFERTYPE_MASK) {
 130        case USB_ENDPOINT_XFER_BULK:    return "bulk";
 131        case USB_ENDPOINT_XFER_ISOC:    return "iso";
 132        case USB_ENDPOINT_XFER_INT:     return "intr";
 133        };
 134        return "control";
 135}
 136#endif
 137
 138#include "net2280.h"
 139
 140#define valid_bit       cpu_to_le32 (1 << VALID_BIT)
 141#define dma_done_ie     cpu_to_le32 (1 << DMA_DONE_INTERRUPT_ENABLE)
 142
 143/*-------------------------------------------------------------------------*/
 144
 145static int
 146net2280_enable (struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
 147{
 148        struct net2280          *dev;
 149        struct net2280_ep       *ep;
 150        u32                     max, tmp;
 151        unsigned long           flags;
 152
 153        ep = container_of (_ep, struct net2280_ep, ep);
 154        if (!_ep || !desc || ep->desc || _ep->name == ep0name
 155                        || desc->bDescriptorType != USB_DT_ENDPOINT)
 156                return -EINVAL;
 157        dev = ep->dev;
 158        if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
 159                return -ESHUTDOWN;
 160
 161        /* erratum 0119 workaround ties up an endpoint number */
 162        if ((desc->bEndpointAddress & 0x0f) == EP_DONTUSE)
 163                return -EDOM;
 164
 165        /* sanity check ep-e/ep-f since their fifos are small */
 166        max = usb_endpoint_maxp (desc) & 0x1fff;
 167        if (ep->num > 4 && max > 64)
 168                return -ERANGE;
 169
 170        spin_lock_irqsave (&dev->lock, flags);
 171        _ep->maxpacket = max & 0x7ff;
 172        ep->desc = desc;
 173
 174        /* ep_reset() has already been called */
 175        ep->stopped = 0;
 176        ep->wedged = 0;
 177        ep->out_overflow = 0;
 178
 179        /* set speed-dependent max packet; may kick in high bandwidth */
 180        set_idx_reg (dev->regs, REG_EP_MAXPKT (dev, ep->num), max);
 181
 182        /* FIFO lines can't go to different packets.  PIO is ok, so
 183         * use it instead of troublesome (non-bulk) multi-packet DMA.
 184         */
 185        if (ep->dma && (max % 4) != 0 && use_dma_chaining) {
 186                DEBUG (ep->dev, "%s, no dma for maxpacket %d\n",
 187                        ep->ep.name, ep->ep.maxpacket);
 188                ep->dma = NULL;
 189        }
 190
 191        /* set type, direction, address; reset fifo counters */
 192        writel ((1 << FIFO_FLUSH), &ep->regs->ep_stat);
 193        tmp = (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK);
 194        if (tmp == USB_ENDPOINT_XFER_INT) {
 195                /* erratum 0105 workaround prevents hs NYET */
 196                if (dev->chiprev == 0100
 197                                && dev->gadget.speed == USB_SPEED_HIGH
 198                                && !(desc->bEndpointAddress & USB_DIR_IN))
 199                        writel ((1 << CLEAR_NAK_OUT_PACKETS_MODE),
 200                                &ep->regs->ep_rsp);
 201        } else if (tmp == USB_ENDPOINT_XFER_BULK) {
 202                /* catch some particularly blatant driver bugs */
 203                if ((dev->gadget.speed == USB_SPEED_HIGH
 204                                        && max != 512)
 205                                || (dev->gadget.speed == USB_SPEED_FULL
 206                                        && max > 64)) {
 207                        spin_unlock_irqrestore (&dev->lock, flags);
 208                        return -ERANGE;
 209                }
 210        }
 211        ep->is_iso = (tmp == USB_ENDPOINT_XFER_ISOC) ? 1 : 0;
 212        tmp <<= ENDPOINT_TYPE;
 213        tmp |= desc->bEndpointAddress;
 214        tmp |= (4 << ENDPOINT_BYTE_COUNT);      /* default full fifo lines */
 215        tmp |= 1 << ENDPOINT_ENABLE;
 216        wmb ();
 217
 218        /* for OUT transfers, block the rx fifo until a read is posted */
 219        ep->is_in = (tmp & USB_DIR_IN) != 0;
 220        if (!ep->is_in)
 221                writel ((1 << SET_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
 222        else if (dev->pdev->device != 0x2280) {
 223                /* Added for 2282, Don't use nak packets on an in endpoint,
 224                 * this was ignored on 2280
 225                 */
 226                writel ((1 << CLEAR_NAK_OUT_PACKETS)
 227                        | (1 << CLEAR_NAK_OUT_PACKETS_MODE), &ep->regs->ep_rsp);
 228        }
 229
 230        writel (tmp, &ep->regs->ep_cfg);
 231
 232        /* enable irqs */
 233        if (!ep->dma) {                         /* pio, per-packet */
 234                tmp = (1 << ep->num) | readl (&dev->regs->pciirqenb0);
 235                writel (tmp, &dev->regs->pciirqenb0);
 236
 237                tmp = (1 << DATA_PACKET_RECEIVED_INTERRUPT_ENABLE)
 238                        | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT_ENABLE);
 239                if (dev->pdev->device == 0x2280)
 240                        tmp |= readl (&ep->regs->ep_irqenb);
 241                writel (tmp, &ep->regs->ep_irqenb);
 242        } else {                                /* dma, per-request */
 243                tmp = (1 << (8 + ep->num));     /* completion */
 244                tmp |= readl (&dev->regs->pciirqenb1);
 245                writel (tmp, &dev->regs->pciirqenb1);
 246
 247                /* for short OUT transfers, dma completions can't
 248                 * advance the queue; do it pio-style, by hand.
 249                 * NOTE erratum 0112 workaround #2
 250                 */
 251                if ((desc->bEndpointAddress & USB_DIR_IN) == 0) {
 252                        tmp = (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT_ENABLE);
 253                        writel (tmp, &ep->regs->ep_irqenb);
 254
 255                        tmp = (1 << ep->num) | readl (&dev->regs->pciirqenb0);
 256                        writel (tmp, &dev->regs->pciirqenb0);
 257                }
 258        }
 259
 260        tmp = desc->bEndpointAddress;
 261        DEBUG (dev, "enabled %s (ep%d%s-%s) %s max %04x\n",
 262                _ep->name, tmp & 0x0f, DIR_STRING (tmp),
 263                type_string (desc->bmAttributes),
 264                ep->dma ? "dma" : "pio", max);
 265
 266        /* pci writes may still be posted */
 267        spin_unlock_irqrestore (&dev->lock, flags);
 268        return 0;
 269}
 270
 271static int handshake (u32 __iomem *ptr, u32 mask, u32 done, int usec)
 272{
 273        u32     result;
 274
 275        do {
 276                result = readl (ptr);
 277                if (result == ~(u32)0)          /* "device unplugged" */
 278                        return -ENODEV;
 279                result &= mask;
 280                if (result == done)
 281                        return 0;
 282                udelay (1);
 283                usec--;
 284        } while (usec > 0);
 285        return -ETIMEDOUT;
 286}
 287
 288static const struct usb_ep_ops net2280_ep_ops;
 289
 290static void ep_reset (struct net2280_regs __iomem *regs, struct net2280_ep *ep)
 291{
 292        u32             tmp;
 293
 294        ep->desc = NULL;
 295        INIT_LIST_HEAD (&ep->queue);
 296
 297        ep->ep.maxpacket = ~0;
 298        ep->ep.ops = &net2280_ep_ops;
 299
 300        /* disable the dma, irqs, endpoint... */
 301        if (ep->dma) {
 302                writel (0, &ep->dma->dmactl);
 303                writel (  (1 << DMA_SCATTER_GATHER_DONE_INTERRUPT)
 304                        | (1 << DMA_TRANSACTION_DONE_INTERRUPT)
 305                        | (1 << DMA_ABORT)
 306                        , &ep->dma->dmastat);
 307
 308                tmp = readl (&regs->pciirqenb0);
 309                tmp &= ~(1 << ep->num);
 310                writel (tmp, &regs->pciirqenb0);
 311        } else {
 312                tmp = readl (&regs->pciirqenb1);
 313                tmp &= ~(1 << (8 + ep->num));   /* completion */
 314                writel (tmp, &regs->pciirqenb1);
 315        }
 316        writel (0, &ep->regs->ep_irqenb);
 317
 318        /* init to our chosen defaults, notably so that we NAK OUT
 319         * packets until the driver queues a read (+note erratum 0112)
 320         */
 321        if (!ep->is_in || ep->dev->pdev->device == 0x2280) {
 322                tmp = (1 << SET_NAK_OUT_PACKETS_MODE)
 323                | (1 << SET_NAK_OUT_PACKETS)
 324                | (1 << CLEAR_EP_HIDE_STATUS_PHASE)
 325                | (1 << CLEAR_INTERRUPT_MODE);
 326        } else {
 327                /* added for 2282 */
 328                tmp = (1 << CLEAR_NAK_OUT_PACKETS_MODE)
 329                | (1 << CLEAR_NAK_OUT_PACKETS)
 330                | (1 << CLEAR_EP_HIDE_STATUS_PHASE)
 331                | (1 << CLEAR_INTERRUPT_MODE);
 332        }
 333
 334        if (ep->num != 0) {
 335                tmp |= (1 << CLEAR_ENDPOINT_TOGGLE)
 336                        | (1 << CLEAR_ENDPOINT_HALT);
 337        }
 338        writel (tmp, &ep->regs->ep_rsp);
 339
 340        /* scrub most status bits, and flush any fifo state */
 341        if (ep->dev->pdev->device == 0x2280)
 342                tmp = (1 << FIFO_OVERFLOW)
 343                        | (1 << FIFO_UNDERFLOW);
 344        else
 345                tmp = 0;
 346
 347        writel (tmp | (1 << TIMEOUT)
 348                | (1 << USB_STALL_SENT)
 349                | (1 << USB_IN_NAK_SENT)
 350                | (1 << USB_IN_ACK_RCVD)
 351                | (1 << USB_OUT_PING_NAK_SENT)
 352                | (1 << USB_OUT_ACK_SENT)
 353                | (1 << FIFO_FLUSH)
 354                | (1 << SHORT_PACKET_OUT_DONE_INTERRUPT)
 355                | (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)
 356                | (1 << DATA_PACKET_RECEIVED_INTERRUPT)
 357                | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
 358                | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
 359                | (1 << DATA_IN_TOKEN_INTERRUPT)
 360                , &ep->regs->ep_stat);
 361
 362        /* fifo size is handled separately */
 363}
 364
 365static void nuke (struct net2280_ep *);
 366
 367static int net2280_disable (struct usb_ep *_ep)
 368{
 369        struct net2280_ep       *ep;
 370        unsigned long           flags;
 371
 372        ep = container_of (_ep, struct net2280_ep, ep);
 373        if (!_ep || !ep->desc || _ep->name == ep0name)
 374                return -EINVAL;
 375
 376        spin_lock_irqsave (&ep->dev->lock, flags);
 377        nuke (ep);
 378        ep_reset (ep->dev->regs, ep);
 379
 380        VDEBUG (ep->dev, "disabled %s %s\n",
 381                        ep->dma ? "dma" : "pio", _ep->name);
 382
 383        /* synch memory views with the device */
 384        (void) readl (&ep->regs->ep_cfg);
 385
 386        if (use_dma && !ep->dma && ep->num >= 1 && ep->num <= 4)
 387                ep->dma = &ep->dev->dma [ep->num - 1];
 388
 389        spin_unlock_irqrestore (&ep->dev->lock, flags);
 390        return 0;
 391}
 392
 393/*-------------------------------------------------------------------------*/
 394
 395static struct usb_request *
 396net2280_alloc_request (struct usb_ep *_ep, gfp_t gfp_flags)
 397{
 398        struct net2280_ep       *ep;
 399        struct net2280_request  *req;
 400
 401        if (!_ep)
 402                return NULL;
 403        ep = container_of (_ep, struct net2280_ep, ep);
 404
 405        req = kzalloc(sizeof(*req), gfp_flags);
 406        if (!req)
 407                return NULL;
 408
 409        req->req.dma = DMA_ADDR_INVALID;
 410        INIT_LIST_HEAD (&req->queue);
 411
 412        /* this dma descriptor may be swapped with the previous dummy */
 413        if (ep->dma) {
 414                struct net2280_dma      *td;
 415
 416                td = pci_pool_alloc (ep->dev->requests, gfp_flags,
 417                                &req->td_dma);
 418                if (!td) {
 419                        kfree (req);
 420                        return NULL;
 421                }
 422                td->dmacount = 0;       /* not VALID */
 423                td->dmaaddr = cpu_to_le32 (DMA_ADDR_INVALID);
 424                td->dmadesc = td->dmaaddr;
 425                req->td = td;
 426        }
 427        return &req->req;
 428}
 429
 430static void
 431net2280_free_request (struct usb_ep *_ep, struct usb_request *_req)
 432{
 433        struct net2280_ep       *ep;
 434        struct net2280_request  *req;
 435
 436        ep = container_of (_ep, struct net2280_ep, ep);
 437        if (!_ep || !_req)
 438                return;
 439
 440        req = container_of (_req, struct net2280_request, req);
 441        WARN_ON (!list_empty (&req->queue));
 442        if (req->td)
 443                pci_pool_free (ep->dev->requests, req->td, req->td_dma);
 444        kfree (req);
 445}
 446
 447/*-------------------------------------------------------------------------*/
 448
 449/* load a packet into the fifo we use for usb IN transfers.
 450 * works for all endpoints.
 451 *
 452 * NOTE: pio with ep-a..ep-d could stuff multiple packets into the fifo
 453 * at a time, but this code is simpler because it knows it only writes
 454 * one packet.  ep-a..ep-d should use dma instead.
 455 */
 456static void
 457write_fifo (struct net2280_ep *ep, struct usb_request *req)
 458{
 459        struct net2280_ep_regs  __iomem *regs = ep->regs;
 460        u8                      *buf;
 461        u32                     tmp;
 462        unsigned                count, total;
 463
 464        /* INVARIANT:  fifo is currently empty. (testable) */
 465
 466        if (req) {
 467                buf = req->buf + req->actual;
 468                prefetch (buf);
 469                total = req->length - req->actual;
 470        } else {
 471                total = 0;
 472                buf = NULL;
 473        }
 474
 475        /* write just one packet at a time */
 476        count = ep->ep.maxpacket;
 477        if (count > total)      /* min() cannot be used on a bitfield */
 478                count = total;
 479
 480        VDEBUG (ep->dev, "write %s fifo (IN) %d bytes%s req %p\n",
 481                        ep->ep.name, count,
 482                        (count != ep->ep.maxpacket) ? " (short)" : "",
 483                        req);
 484        while (count >= 4) {
 485                /* NOTE be careful if you try to align these. fifo lines
 486                 * should normally be full (4 bytes) and successive partial
 487                 * lines are ok only in certain cases.
 488                 */
 489                tmp = get_unaligned ((u32 *)buf);
 490                cpu_to_le32s (&tmp);
 491                writel (tmp, &regs->ep_data);
 492                buf += 4;
 493                count -= 4;
 494        }
 495
 496        /* last fifo entry is "short" unless we wrote a full packet.
 497         * also explicitly validate last word in (periodic) transfers
 498         * when maxpacket is not a multiple of 4 bytes.
 499         */
 500        if (count || total < ep->ep.maxpacket) {
 501                tmp = count ? get_unaligned ((u32 *)buf) : count;
 502                cpu_to_le32s (&tmp);
 503                set_fifo_bytecount (ep, count & 0x03);
 504                writel (tmp, &regs->ep_data);
 505        }
 506
 507        /* pci writes may still be posted */
 508}
 509
 510/* work around erratum 0106: PCI and USB race over the OUT fifo.
 511 * caller guarantees chiprev 0100, out endpoint is NAKing, and
 512 * there's no real data in the fifo.
 513 *
 514 * NOTE:  also used in cases where that erratum doesn't apply:
 515 * where the host wrote "too much" data to us.
 516 */
 517static void out_flush (struct net2280_ep *ep)
 518{
 519        u32     __iomem *statp;
 520        u32     tmp;
 521
 522        ASSERT_OUT_NAKING (ep);
 523
 524        statp = &ep->regs->ep_stat;
 525        writel (  (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
 526                | (1 << DATA_PACKET_RECEIVED_INTERRUPT)
 527                , statp);
 528        writel ((1 << FIFO_FLUSH), statp);
 529        mb ();
 530        tmp = readl (statp);
 531        if (tmp & (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
 532                        /* high speed did bulk NYET; fifo isn't filling */
 533                        && ep->dev->gadget.speed == USB_SPEED_FULL) {
 534                unsigned        usec;
 535
 536                usec = 50;              /* 64 byte bulk/interrupt */
 537                handshake (statp, (1 << USB_OUT_PING_NAK_SENT),
 538                                (1 << USB_OUT_PING_NAK_SENT), usec);
 539                /* NAK done; now CLEAR_NAK_OUT_PACKETS is safe */
 540        }
 541}
 542
 543/* unload packet(s) from the fifo we use for usb OUT transfers.
 544 * returns true iff the request completed, because of short packet
 545 * or the request buffer having filled with full packets.
 546 *
 547 * for ep-a..ep-d this will read multiple packets out when they
 548 * have been accepted.
 549 */
 550static int
 551read_fifo (struct net2280_ep *ep, struct net2280_request *req)
 552{
 553        struct net2280_ep_regs  __iomem *regs = ep->regs;
 554        u8                      *buf = req->req.buf + req->req.actual;
 555        unsigned                count, tmp, is_short;
 556        unsigned                cleanup = 0, prevent = 0;
 557
 558        /* erratum 0106 ... packets coming in during fifo reads might
 559         * be incompletely rejected.  not all cases have workarounds.
 560         */
 561        if (ep->dev->chiprev == 0x0100
 562                        && ep->dev->gadget.speed == USB_SPEED_FULL) {
 563                udelay (1);
 564                tmp = readl (&ep->regs->ep_stat);
 565                if ((tmp & (1 << NAK_OUT_PACKETS)))
 566                        cleanup = 1;
 567                else if ((tmp & (1 << FIFO_FULL))) {
 568                        start_out_naking (ep);
 569                        prevent = 1;
 570                }
 571                /* else: hope we don't see the problem */
 572        }
 573
 574        /* never overflow the rx buffer. the fifo reads packets until
 575         * it sees a short one; we might not be ready for them all.
 576         */
 577        prefetchw (buf);
 578        count = readl (&regs->ep_avail);
 579        if (unlikely (count == 0)) {
 580                udelay (1);
 581                tmp = readl (&ep->regs->ep_stat);
 582                count = readl (&regs->ep_avail);
 583                /* handled that data already? */
 584                if (count == 0 && (tmp & (1 << NAK_OUT_PACKETS)) == 0)
 585                        return 0;
 586        }
 587
 588        tmp = req->req.length - req->req.actual;
 589        if (count > tmp) {
 590                /* as with DMA, data overflow gets flushed */
 591                if ((tmp % ep->ep.maxpacket) != 0) {
 592                        ERROR (ep->dev,
 593                                "%s out fifo %d bytes, expected %d\n",
 594                                ep->ep.name, count, tmp);
 595                        req->req.status = -EOVERFLOW;
 596                        cleanup = 1;
 597                        /* NAK_OUT_PACKETS will be set, so flushing is safe;
 598                         * the next read will start with the next packet
 599                         */
 600                } /* else it's a ZLP, no worries */
 601                count = tmp;
 602        }
 603        req->req.actual += count;
 604
 605        is_short = (count == 0) || ((count % ep->ep.maxpacket) != 0);
 606
 607        VDEBUG (ep->dev, "read %s fifo (OUT) %d bytes%s%s%s req %p %d/%d\n",
 608                        ep->ep.name, count, is_short ? " (short)" : "",
 609                        cleanup ? " flush" : "", prevent ? " nak" : "",
 610                        req, req->req.actual, req->req.length);
 611
 612        while (count >= 4) {
 613                tmp = readl (&regs->ep_data);
 614                cpu_to_le32s (&tmp);
 615                put_unaligned (tmp, (u32 *)buf);
 616                buf += 4;
 617                count -= 4;
 618        }
 619        if (count) {
 620                tmp = readl (&regs->ep_data);
 621                /* LE conversion is implicit here: */
 622                do {
 623                        *buf++ = (u8) tmp;
 624                        tmp >>= 8;
 625                } while (--count);
 626        }
 627        if (cleanup)
 628                out_flush (ep);
 629        if (prevent) {
 630                writel ((1 << CLEAR_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
 631                (void) readl (&ep->regs->ep_rsp);
 632        }
 633
 634        return is_short || ((req->req.actual == req->req.length)
 635                                && !req->req.zero);
 636}
 637
 638/* fill out dma descriptor to match a given request */
 639static void
 640fill_dma_desc (struct net2280_ep *ep, struct net2280_request *req, int valid)
 641{
 642        struct net2280_dma      *td = req->td;
 643        u32                     dmacount = req->req.length;
 644
 645        /* don't let DMA continue after a short OUT packet,
 646         * so overruns can't affect the next transfer.
 647         * in case of overruns on max-size packets, we can't
 648         * stop the fifo from filling but we can flush it.
 649         */
 650        if (ep->is_in)
 651                dmacount |= (1 << DMA_DIRECTION);
 652        if ((!ep->is_in && (dmacount % ep->ep.maxpacket) != 0)
 653                        || ep->dev->pdev->device != 0x2280)
 654                dmacount |= (1 << END_OF_CHAIN);
 655
 656        req->valid = valid;
 657        if (valid)
 658                dmacount |= (1 << VALID_BIT);
 659        if (likely(!req->req.no_interrupt || !use_dma_chaining))
 660                dmacount |= (1 << DMA_DONE_INTERRUPT_ENABLE);
 661
 662        /* td->dmadesc = previously set by caller */
 663        td->dmaaddr = cpu_to_le32 (req->req.dma);
 664
 665        /* 2280 may be polling VALID_BIT through ep->dma->dmadesc */
 666        wmb ();
 667        td->dmacount = cpu_to_le32(dmacount);
 668}
 669
 670static const u32 dmactl_default =
 671                  (1 << DMA_SCATTER_GATHER_DONE_INTERRUPT)
 672                | (1 << DMA_CLEAR_COUNT_ENABLE)
 673                /* erratum 0116 workaround part 1 (use POLLING) */
 674                | (POLL_100_USEC << DESCRIPTOR_POLLING_RATE)
 675                | (1 << DMA_VALID_BIT_POLLING_ENABLE)
 676                | (1 << DMA_VALID_BIT_ENABLE)
 677                | (1 << DMA_SCATTER_GATHER_ENABLE)
 678                /* erratum 0116 workaround part 2 (no AUTOSTART) */
 679                | (1 << DMA_ENABLE);
 680
 681static inline void spin_stop_dma (struct net2280_dma_regs __iomem *dma)
 682{
 683        handshake (&dma->dmactl, (1 << DMA_ENABLE), 0, 50);
 684}
 685
 686static inline void stop_dma (struct net2280_dma_regs __iomem *dma)
 687{
 688        writel (readl (&dma->dmactl) & ~(1 << DMA_ENABLE), &dma->dmactl);
 689        spin_stop_dma (dma);
 690}
 691
 692static void start_queue (struct net2280_ep *ep, u32 dmactl, u32 td_dma)
 693{
 694        struct net2280_dma_regs __iomem *dma = ep->dma;
 695        unsigned int tmp = (1 << VALID_BIT) | (ep->is_in << DMA_DIRECTION);
 696
 697        if (ep->dev->pdev->device != 0x2280)
 698                tmp |= (1 << END_OF_CHAIN);
 699
 700        writel (tmp, &dma->dmacount);
 701        writel (readl (&dma->dmastat), &dma->dmastat);
 702
 703        writel (td_dma, &dma->dmadesc);
 704        writel (dmactl, &dma->dmactl);
 705
 706        /* erratum 0116 workaround part 3:  pci arbiter away from net2280 */
 707        (void) readl (&ep->dev->pci->pcimstctl);
 708
 709        writel ((1 << DMA_START), &dma->dmastat);
 710
 711        if (!ep->is_in)
 712                stop_out_naking (ep);
 713}
 714
 715static void start_dma (struct net2280_ep *ep, struct net2280_request *req)
 716{
 717        u32                     tmp;
 718        struct net2280_dma_regs __iomem *dma = ep->dma;
 719
 720        /* FIXME can't use DMA for ZLPs */
 721
 722        /* on this path we "know" there's no dma active (yet) */
 723        WARN_ON (readl (&dma->dmactl) & (1 << DMA_ENABLE));
 724        writel (0, &ep->dma->dmactl);
 725
 726        /* previous OUT packet might have been short */
 727        if (!ep->is_in && ((tmp = readl (&ep->regs->ep_stat))
 728                                & (1 << NAK_OUT_PACKETS)) != 0) {
 729                writel ((1 << SHORT_PACKET_TRANSFERRED_INTERRUPT),
 730                        &ep->regs->ep_stat);
 731
 732                tmp = readl (&ep->regs->ep_avail);
 733                if (tmp) {
 734                        writel (readl (&dma->dmastat), &dma->dmastat);
 735
 736                        /* transfer all/some fifo data */
 737                        writel (req->req.dma, &dma->dmaaddr);
 738                        tmp = min (tmp, req->req.length);
 739
 740                        /* dma irq, faking scatterlist status */
 741                        req->td->dmacount = cpu_to_le32 (req->req.length - tmp);
 742                        writel ((1 << DMA_DONE_INTERRUPT_ENABLE)
 743                                | tmp, &dma->dmacount);
 744                        req->td->dmadesc = 0;
 745                        req->valid = 1;
 746
 747                        writel ((1 << DMA_ENABLE), &dma->dmactl);
 748                        writel ((1 << DMA_START), &dma->dmastat);
 749                        return;
 750                }
 751        }
 752
 753        tmp = dmactl_default;
 754
 755        /* force packet boundaries between dma requests, but prevent the
 756         * controller from automagically writing a last "short" packet
 757         * (zero length) unless the driver explicitly said to do that.
 758         */
 759        if (ep->is_in) {
 760                if (likely ((req->req.length % ep->ep.maxpacket) != 0
 761                                || req->req.zero)) {
 762                        tmp |= (1 << DMA_FIFO_VALIDATE);
 763                        ep->in_fifo_validate = 1;
 764                } else
 765                        ep->in_fifo_validate = 0;
 766        }
 767
 768        /* init req->td, pointing to the current dummy */
 769        req->td->dmadesc = cpu_to_le32 (ep->td_dma);
 770        fill_dma_desc (ep, req, 1);
 771
 772        if (!use_dma_chaining)
 773                req->td->dmacount |= cpu_to_le32 (1 << END_OF_CHAIN);
 774
 775        start_queue (ep, tmp, req->td_dma);
 776}
 777
 778static inline void
 779queue_dma (struct net2280_ep *ep, struct net2280_request *req, int valid)
 780{
 781        struct net2280_dma      *end;
 782        dma_addr_t              tmp;
 783
 784        /* swap new dummy for old, link; fill and maybe activate */
 785        end = ep->dummy;
 786        ep->dummy = req->td;
 787        req->td = end;
 788
 789        tmp = ep->td_dma;
 790        ep->td_dma = req->td_dma;
 791        req->td_dma = tmp;
 792
 793        end->dmadesc = cpu_to_le32 (ep->td_dma);
 794
 795        fill_dma_desc (ep, req, valid);
 796}
 797
 798static void
 799done (struct net2280_ep *ep, struct net2280_request *req, int status)
 800{
 801        struct net2280          *dev;
 802        unsigned                stopped = ep->stopped;
 803
 804        list_del_init (&req->queue);
 805
 806        if (req->req.status == -EINPROGRESS)
 807                req->req.status = status;
 808        else
 809                status = req->req.status;
 810
 811        dev = ep->dev;
 812        if (ep->dma)
 813                usb_gadget_unmap_request(&dev->gadget, &req->req, ep->is_in);
 814
 815        if (status && status != -ESHUTDOWN)
 816                VDEBUG (dev, "complete %s req %p stat %d len %u/%u\n",
 817                        ep->ep.name, &req->req, status,
 818                        req->req.actual, req->req.length);
 819
 820        /* don't modify queue heads during completion callback */
 821        ep->stopped = 1;
 822        spin_unlock (&dev->lock);
 823        req->req.complete (&ep->ep, &req->req);
 824        spin_lock (&dev->lock);
 825        ep->stopped = stopped;
 826}
 827
 828/*-------------------------------------------------------------------------*/
 829
 830static int
 831net2280_queue (struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
 832{
 833        struct net2280_request  *req;
 834        struct net2280_ep       *ep;
 835        struct net2280          *dev;
 836        unsigned long           flags;
 837
 838        /* we always require a cpu-view buffer, so that we can
 839         * always use pio (as fallback or whatever).
 840         */
 841        req = container_of (_req, struct net2280_request, req);
 842        if (!_req || !_req->complete || !_req->buf
 843                        || !list_empty (&req->queue))
 844                return -EINVAL;
 845        if (_req->length > (~0 & DMA_BYTE_COUNT_MASK))
 846                return -EDOM;
 847        ep = container_of (_ep, struct net2280_ep, ep);
 848        if (!_ep || (!ep->desc && ep->num != 0))
 849                return -EINVAL;
 850        dev = ep->dev;
 851        if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
 852                return -ESHUTDOWN;
 853
 854        /* FIXME implement PIO fallback for ZLPs with DMA */
 855        if (ep->dma && _req->length == 0)
 856                return -EOPNOTSUPP;
 857
 858        /* set up dma mapping in case the caller didn't */
 859        if (ep->dma) {
 860                int ret;
 861
 862                ret = usb_gadget_map_request(&dev->gadget, _req,
 863                                ep->is_in);
 864                if (ret)
 865                        return ret;
 866        }
 867
 868#if 0
 869        VDEBUG (dev, "%s queue req %p, len %d buf %p\n",
 870                        _ep->name, _req, _req->length, _req->buf);
 871#endif
 872
 873        spin_lock_irqsave (&dev->lock, flags);
 874
 875        _req->status = -EINPROGRESS;
 876        _req->actual = 0;
 877
 878        /* kickstart this i/o queue? */
 879        if (list_empty (&ep->queue) && !ep->stopped) {
 880                /* use DMA if the endpoint supports it, else pio */
 881                if (ep->dma)
 882                        start_dma (ep, req);
 883                else {
 884                        /* maybe there's no control data, just status ack */
 885                        if (ep->num == 0 && _req->length == 0) {
 886                                allow_status (ep);
 887                                done (ep, req, 0);
 888                                VDEBUG (dev, "%s status ack\n", ep->ep.name);
 889                                goto done;
 890                        }
 891
 892                        /* PIO ... stuff the fifo, or unblock it.  */
 893                        if (ep->is_in)
 894                                write_fifo (ep, _req);
 895                        else if (list_empty (&ep->queue)) {
 896                                u32     s;
 897
 898                                /* OUT FIFO might have packet(s) buffered */
 899                                s = readl (&ep->regs->ep_stat);
 900                                if ((s & (1 << FIFO_EMPTY)) == 0) {
 901                                        /* note:  _req->short_not_ok is
 902                                         * ignored here since PIO _always_
 903                                         * stops queue advance here, and
 904                                         * _req->status doesn't change for
 905                                         * short reads (only _req->actual)
 906                                         */
 907                                        if (read_fifo (ep, req)) {
 908                                                done (ep, req, 0);
 909                                                if (ep->num == 0)
 910                                                        allow_status (ep);
 911                                                /* don't queue it */
 912                                                req = NULL;
 913                                        } else
 914                                                s = readl (&ep->regs->ep_stat);
 915                                }
 916
 917                                /* don't NAK, let the fifo fill */
 918                                if (req && (s & (1 << NAK_OUT_PACKETS)))
 919                                        writel ((1 << CLEAR_NAK_OUT_PACKETS),
 920                                                        &ep->regs->ep_rsp);
 921                        }
 922                }
 923
 924        } else if (ep->dma) {
 925                int     valid = 1;
 926
 927                if (ep->is_in) {
 928                        int     expect;
 929
 930                        /* preventing magic zlps is per-engine state, not
 931                         * per-transfer; irq logic must recover hiccups.
 932                         */
 933                        expect = likely (req->req.zero
 934                                || (req->req.length % ep->ep.maxpacket) != 0);
 935                        if (expect != ep->in_fifo_validate)
 936                                valid = 0;
 937                }
 938                queue_dma (ep, req, valid);
 939
 940        } /* else the irq handler advances the queue. */
 941
 942        ep->responded = 1;
 943        if (req)
 944                list_add_tail (&req->queue, &ep->queue);
 945done:
 946        spin_unlock_irqrestore (&dev->lock, flags);
 947
 948        /* pci writes may still be posted */
 949        return 0;
 950}
 951
 952static inline void
 953dma_done (
 954        struct net2280_ep *ep,
 955        struct net2280_request *req,
 956        u32 dmacount,
 957        int status
 958)
 959{
 960        req->req.actual = req->req.length - (DMA_BYTE_COUNT_MASK & dmacount);
 961        done (ep, req, status);
 962}
 963
 964static void restart_dma (struct net2280_ep *ep);
 965
 966static void scan_dma_completions (struct net2280_ep *ep)
 967{
 968        /* only look at descriptors that were "naturally" retired,
 969         * so fifo and list head state won't matter
 970         */
 971        while (!list_empty (&ep->queue)) {
 972                struct net2280_request  *req;
 973                u32                     tmp;
 974
 975                req = list_entry (ep->queue.next,
 976                                struct net2280_request, queue);
 977                if (!req->valid)
 978                        break;
 979                rmb ();
 980                tmp = le32_to_cpup (&req->td->dmacount);
 981                if ((tmp & (1 << VALID_BIT)) != 0)
 982                        break;
 983
 984                /* SHORT_PACKET_TRANSFERRED_INTERRUPT handles "usb-short"
 985                 * cases where DMA must be aborted; this code handles
 986                 * all non-abort DMA completions.
 987                 */
 988                if (unlikely (req->td->dmadesc == 0)) {
 989                        /* paranoia */
 990                        tmp = readl (&ep->dma->dmacount);
 991                        if (tmp & DMA_BYTE_COUNT_MASK)
 992                                break;
 993                        /* single transfer mode */
 994                        dma_done (ep, req, tmp, 0);
 995                        break;
 996                } else if (!ep->is_in
 997                                && (req->req.length % ep->ep.maxpacket) != 0) {
 998                        tmp = readl (&ep->regs->ep_stat);
 999
1000                        /* AVOID TROUBLE HERE by not issuing short reads from
1001                         * your gadget driver.  That helps avoids errata 0121,
1002                         * 0122, and 0124; not all cases trigger the warning.
1003                         */
1004                        if ((tmp & (1 << NAK_OUT_PACKETS)) == 0) {
1005                                WARNING (ep->dev, "%s lost packet sync!\n",
1006                                                ep->ep.name);
1007                                req->req.status = -EOVERFLOW;
1008                        } else if ((tmp = readl (&ep->regs->ep_avail)) != 0) {
1009                                /* fifo gets flushed later */
1010                                ep->out_overflow = 1;
1011                                DEBUG (ep->dev, "%s dma, discard %d len %d\n",
1012                                                ep->ep.name, tmp,
1013                                                req->req.length);
1014                                req->req.status = -EOVERFLOW;
1015                        }
1016                }
1017                dma_done (ep, req, tmp, 0);
1018        }
1019}
1020
1021static void restart_dma (struct net2280_ep *ep)
1022{
1023        struct net2280_request  *req;
1024        u32                     dmactl = dmactl_default;
1025
1026        if (ep->stopped)
1027                return;
1028        req = list_entry (ep->queue.next, struct net2280_request, queue);
1029
1030        if (!use_dma_chaining) {
1031                start_dma (ep, req);
1032                return;
1033        }
1034
1035        /* the 2280 will be processing the queue unless queue hiccups after
1036         * the previous transfer:
1037         *  IN:   wanted automagic zlp, head doesn't (or vice versa)
1038         *        DMA_FIFO_VALIDATE doesn't init from dma descriptors.
1039         *  OUT:  was "usb-short", we must restart.
1040         */
1041        if (ep->is_in && !req->valid) {
1042                struct net2280_request  *entry, *prev = NULL;
1043                int                     reqmode, done = 0;
1044
1045                DEBUG (ep->dev, "%s dma hiccup td %p\n", ep->ep.name, req->td);
1046                ep->in_fifo_validate = likely (req->req.zero
1047                        || (req->req.length % ep->ep.maxpacket) != 0);
1048                if (ep->in_fifo_validate)
1049                        dmactl |= (1 << DMA_FIFO_VALIDATE);
1050                list_for_each_entry (entry, &ep->queue, queue) {
1051                        __le32          dmacount;
1052
1053                        if (entry == req)
1054                                continue;
1055                        dmacount = entry->td->dmacount;
1056                        if (!done) {
1057                                reqmode = likely (entry->req.zero
1058                                        || (entry->req.length
1059                                                % ep->ep.maxpacket) != 0);
1060                                if (reqmode == ep->in_fifo_validate) {
1061                                        entry->valid = 1;
1062                                        dmacount |= valid_bit;
1063                                        entry->td->dmacount = dmacount;
1064                                        prev = entry;
1065                                        continue;
1066                                } else {
1067                                        /* force a hiccup */
1068                                        prev->td->dmacount |= dma_done_ie;
1069                                        done = 1;
1070                                }
1071                        }
1072
1073                        /* walk the rest of the queue so unlinks behave */
1074                        entry->valid = 0;
1075                        dmacount &= ~valid_bit;
1076                        entry->td->dmacount = dmacount;
1077                        prev = entry;
1078                }
1079        }
1080
1081        writel (0, &ep->dma->dmactl);
1082        start_queue (ep, dmactl, req->td_dma);
1083}
1084
1085static void abort_dma (struct net2280_ep *ep)
1086{
1087        /* abort the current transfer */
1088        if (likely (!list_empty (&ep->queue))) {
1089                /* FIXME work around errata 0121, 0122, 0124 */
1090                writel ((1 << DMA_ABORT), &ep->dma->dmastat);
1091                spin_stop_dma (ep->dma);
1092        } else
1093                stop_dma (ep->dma);
1094        scan_dma_completions (ep);
1095}
1096
1097/* dequeue ALL requests */
1098static void nuke (struct net2280_ep *ep)
1099{
1100        struct net2280_request  *req;
1101
1102        /* called with spinlock held */
1103        ep->stopped = 1;
1104        if (ep->dma)
1105                abort_dma (ep);
1106        while (!list_empty (&ep->queue)) {
1107                req = list_entry (ep->queue.next,
1108                                struct net2280_request,
1109                                queue);
1110                done (ep, req, -ESHUTDOWN);
1111        }
1112}
1113
1114/* dequeue JUST ONE request */
1115static int net2280_dequeue (struct usb_ep *_ep, struct usb_request *_req)
1116{
1117        struct net2280_ep       *ep;
1118        struct net2280_request  *req;
1119        unsigned long           flags;
1120        u32                     dmactl;
1121        int                     stopped;
1122
1123        ep = container_of (_ep, struct net2280_ep, ep);
1124        if (!_ep || (!ep->desc && ep->num != 0) || !_req)
1125                return -EINVAL;
1126
1127        spin_lock_irqsave (&ep->dev->lock, flags);
1128        stopped = ep->stopped;
1129
1130        /* quiesce dma while we patch the queue */
1131        dmactl = 0;
1132        ep->stopped = 1;
1133        if (ep->dma) {
1134                dmactl = readl (&ep->dma->dmactl);
1135                /* WARNING erratum 0127 may kick in ... */
1136                stop_dma (ep->dma);
1137                scan_dma_completions (ep);
1138        }
1139
1140        /* make sure it's still queued on this endpoint */
1141        list_for_each_entry (req, &ep->queue, queue) {
1142                if (&req->req == _req)
1143                        break;
1144        }
1145        if (&req->req != _req) {
1146                spin_unlock_irqrestore (&ep->dev->lock, flags);
1147                return -EINVAL;
1148        }
1149
1150        /* queue head may be partially complete. */
1151        if (ep->queue.next == &req->queue) {
1152                if (ep->dma) {
1153                        DEBUG (ep->dev, "unlink (%s) dma\n", _ep->name);
1154                        _req->status = -ECONNRESET;
1155                        abort_dma (ep);
1156                        if (likely (ep->queue.next == &req->queue)) {
1157                                // NOTE: misreports single-transfer mode
1158                                req->td->dmacount = 0;  /* invalidate */
1159                                dma_done (ep, req,
1160                                        readl (&ep->dma->dmacount),
1161                                        -ECONNRESET);
1162                        }
1163                } else {
1164                        DEBUG (ep->dev, "unlink (%s) pio\n", _ep->name);
1165                        done (ep, req, -ECONNRESET);
1166                }
1167                req = NULL;
1168
1169        /* patch up hardware chaining data */
1170        } else if (ep->dma && use_dma_chaining) {
1171                if (req->queue.prev == ep->queue.next) {
1172                        writel (le32_to_cpu (req->td->dmadesc),
1173                                &ep->dma->dmadesc);
1174                        if (req->td->dmacount & dma_done_ie)
1175                                writel (readl (&ep->dma->dmacount)
1176                                                | le32_to_cpu(dma_done_ie),
1177                                        &ep->dma->dmacount);
1178                } else {
1179                        struct net2280_request  *prev;
1180
1181                        prev = list_entry (req->queue.prev,
1182                                struct net2280_request, queue);
1183                        prev->td->dmadesc = req->td->dmadesc;
1184                        if (req->td->dmacount & dma_done_ie)
1185                                prev->td->dmacount |= dma_done_ie;
1186                }
1187        }
1188
1189        if (req)
1190                done (ep, req, -ECONNRESET);
1191        ep->stopped = stopped;
1192
1193        if (ep->dma) {
1194                /* turn off dma on inactive queues */
1195                if (list_empty (&ep->queue))
1196                        stop_dma (ep->dma);
1197                else if (!ep->stopped) {
1198                        /* resume current request, or start new one */
1199                        if (req)
1200                                writel (dmactl, &ep->dma->dmactl);
1201                        else
1202                                start_dma (ep, list_entry (ep->queue.next,
1203                                        struct net2280_request, queue));
1204                }
1205        }
1206
1207        spin_unlock_irqrestore (&ep->dev->lock, flags);
1208        return 0;
1209}
1210
1211/*-------------------------------------------------------------------------*/
1212
1213static int net2280_fifo_status (struct usb_ep *_ep);
1214
1215static int
1216net2280_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedged)
1217{
1218        struct net2280_ep       *ep;
1219        unsigned long           flags;
1220        int                     retval = 0;
1221
1222        ep = container_of (_ep, struct net2280_ep, ep);
1223        if (!_ep || (!ep->desc && ep->num != 0))
1224                return -EINVAL;
1225        if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1226                return -ESHUTDOWN;
1227        if (ep->desc /* not ep0 */ && (ep->desc->bmAttributes & 0x03)
1228                                                == USB_ENDPOINT_XFER_ISOC)
1229                return -EINVAL;
1230
1231        spin_lock_irqsave (&ep->dev->lock, flags);
1232        if (!list_empty (&ep->queue))
1233                retval = -EAGAIN;
1234        else if (ep->is_in && value && net2280_fifo_status (_ep) != 0)
1235                retval = -EAGAIN;
1236        else {
1237                VDEBUG (ep->dev, "%s %s %s\n", _ep->name,
1238                                value ? "set" : "clear",
1239                                wedged ? "wedge" : "halt");
1240                /* set/clear, then synch memory views with the device */
1241                if (value) {
1242                        if (ep->num == 0)
1243                                ep->dev->protocol_stall = 1;
1244                        else
1245                                set_halt (ep);
1246                        if (wedged)
1247                                ep->wedged = 1;
1248                } else {
1249                        clear_halt (ep);
1250                        ep->wedged = 0;
1251                }
1252                (void) readl (&ep->regs->ep_rsp);
1253        }
1254        spin_unlock_irqrestore (&ep->dev->lock, flags);
1255
1256        return retval;
1257}
1258
1259static int
1260net2280_set_halt(struct usb_ep *_ep, int value)
1261{
1262        return net2280_set_halt_and_wedge(_ep, value, 0);
1263}
1264
1265static int
1266net2280_set_wedge(struct usb_ep *_ep)
1267{
1268        if (!_ep || _ep->name == ep0name)
1269                return -EINVAL;
1270        return net2280_set_halt_and_wedge(_ep, 1, 1);
1271}
1272
1273static int
1274net2280_fifo_status (struct usb_ep *_ep)
1275{
1276        struct net2280_ep       *ep;
1277        u32                     avail;
1278
1279        ep = container_of (_ep, struct net2280_ep, ep);
1280        if (!_ep || (!ep->desc && ep->num != 0))
1281                return -ENODEV;
1282        if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1283                return -ESHUTDOWN;
1284
1285        avail = readl (&ep->regs->ep_avail) & ((1 << 12) - 1);
1286        if (avail > ep->fifo_size)
1287                return -EOVERFLOW;
1288        if (ep->is_in)
1289                avail = ep->fifo_size - avail;
1290        return avail;
1291}
1292
1293static void
1294net2280_fifo_flush (struct usb_ep *_ep)
1295{
1296        struct net2280_ep       *ep;
1297
1298        ep = container_of (_ep, struct net2280_ep, ep);
1299        if (!_ep || (!ep->desc && ep->num != 0))
1300                return;
1301        if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1302                return;
1303
1304        writel ((1 << FIFO_FLUSH), &ep->regs->ep_stat);
1305        (void) readl (&ep->regs->ep_rsp);
1306}
1307
1308static const struct usb_ep_ops net2280_ep_ops = {
1309        .enable         = net2280_enable,
1310        .disable        = net2280_disable,
1311
1312        .alloc_request  = net2280_alloc_request,
1313        .free_request   = net2280_free_request,
1314
1315        .queue          = net2280_queue,
1316        .dequeue        = net2280_dequeue,
1317
1318        .set_halt       = net2280_set_halt,
1319        .set_wedge      = net2280_set_wedge,
1320        .fifo_status    = net2280_fifo_status,
1321        .fifo_flush     = net2280_fifo_flush,
1322};
1323
1324/*-------------------------------------------------------------------------*/
1325
1326static int net2280_get_frame (struct usb_gadget *_gadget)
1327{
1328        struct net2280          *dev;
1329        unsigned long           flags;
1330        u16                     retval;
1331
1332        if (!_gadget)
1333                return -ENODEV;
1334        dev = container_of (_gadget, struct net2280, gadget);
1335        spin_lock_irqsave (&dev->lock, flags);
1336        retval = get_idx_reg (dev->regs, REG_FRAME) & 0x03ff;
1337        spin_unlock_irqrestore (&dev->lock, flags);
1338        return retval;
1339}
1340
1341static int net2280_wakeup (struct usb_gadget *_gadget)
1342{
1343        struct net2280          *dev;
1344        u32                     tmp;
1345        unsigned long           flags;
1346
1347        if (!_gadget)
1348                return 0;
1349        dev = container_of (_gadget, struct net2280, gadget);
1350
1351        spin_lock_irqsave (&dev->lock, flags);
1352        tmp = readl (&dev->usb->usbctl);
1353        if (tmp & (1 << DEVICE_REMOTE_WAKEUP_ENABLE))
1354                writel (1 << GENERATE_RESUME, &dev->usb->usbstat);
1355        spin_unlock_irqrestore (&dev->lock, flags);
1356
1357        /* pci writes may still be posted */
1358        return 0;
1359}
1360
1361static int net2280_set_selfpowered (struct usb_gadget *_gadget, int value)
1362{
1363        struct net2280          *dev;
1364        u32                     tmp;
1365        unsigned long           flags;
1366
1367        if (!_gadget)
1368                return 0;
1369        dev = container_of (_gadget, struct net2280, gadget);
1370
1371        spin_lock_irqsave (&dev->lock, flags);
1372        tmp = readl (&dev->usb->usbctl);
1373        if (value)
1374                tmp |= (1 << SELF_POWERED_STATUS);
1375        else
1376                tmp &= ~(1 << SELF_POWERED_STATUS);
1377        writel (tmp, &dev->usb->usbctl);
1378        spin_unlock_irqrestore (&dev->lock, flags);
1379
1380        return 0;
1381}
1382
1383static int net2280_pullup(struct usb_gadget *_gadget, int is_on)
1384{
1385        struct net2280  *dev;
1386        u32             tmp;
1387        unsigned long   flags;
1388
1389        if (!_gadget)
1390                return -ENODEV;
1391        dev = container_of (_gadget, struct net2280, gadget);
1392
1393        spin_lock_irqsave (&dev->lock, flags);
1394        tmp = readl (&dev->usb->usbctl);
1395        dev->softconnect = (is_on != 0);
1396        if (is_on)
1397                tmp |= (1 << USB_DETECT_ENABLE);
1398        else
1399                tmp &= ~(1 << USB_DETECT_ENABLE);
1400        writel (tmp, &dev->usb->usbctl);
1401        spin_unlock_irqrestore (&dev->lock, flags);
1402
1403        return 0;
1404}
1405
1406static int net2280_start(struct usb_gadget *_gadget,
1407                struct usb_gadget_driver *driver);
1408static int net2280_stop(struct usb_gadget *_gadget,
1409                struct usb_gadget_driver *driver);
1410
1411static const struct usb_gadget_ops net2280_ops = {
1412        .get_frame      = net2280_get_frame,
1413        .wakeup         = net2280_wakeup,
1414        .set_selfpowered = net2280_set_selfpowered,
1415        .pullup         = net2280_pullup,
1416        .udc_start      = net2280_start,
1417        .udc_stop       = net2280_stop,
1418};
1419
1420/*-------------------------------------------------------------------------*/
1421
1422#ifdef  CONFIG_USB_GADGET_DEBUG_FILES
1423
1424/* FIXME move these into procfs, and use seq_file.
1425 * Sysfs _still_ doesn't behave for arbitrarily sized files,
1426 * and also doesn't help products using this with 2.4 kernels.
1427 */
1428
1429/* "function" sysfs attribute */
1430static ssize_t
1431show_function (struct device *_dev, struct device_attribute *attr, char *buf)
1432{
1433        struct net2280  *dev = dev_get_drvdata (_dev);
1434
1435        if (!dev->driver
1436                        || !dev->driver->function
1437                        || strlen (dev->driver->function) > PAGE_SIZE)
1438                return 0;
1439        return scnprintf (buf, PAGE_SIZE, "%s\n", dev->driver->function);
1440}
1441static DEVICE_ATTR (function, S_IRUGO, show_function, NULL);
1442
1443static ssize_t net2280_show_registers(struct device *_dev,
1444                                struct device_attribute *attr, char *buf)
1445{
1446        struct net2280          *dev;
1447        char                    *next;
1448        unsigned                size, t;
1449        unsigned long           flags;
1450        int                     i;
1451        u32                     t1, t2;
1452        const char              *s;
1453
1454        dev = dev_get_drvdata (_dev);
1455        next = buf;
1456        size = PAGE_SIZE;
1457        spin_lock_irqsave (&dev->lock, flags);
1458
1459        if (dev->driver)
1460                s = dev->driver->driver.name;
1461        else
1462                s = "(none)";
1463
1464        /* Main Control Registers */
1465        t = scnprintf (next, size, "%s version " DRIVER_VERSION
1466                        ", chiprev %04x, dma %s\n\n"
1467                        "devinit %03x fifoctl %08x gadget '%s'\n"
1468                        "pci irqenb0 %02x irqenb1 %08x "
1469                        "irqstat0 %04x irqstat1 %08x\n",
1470                        driver_name, dev->chiprev,
1471                        use_dma
1472                                ? (use_dma_chaining ? "chaining" : "enabled")
1473                                : "disabled",
1474                        readl (&dev->regs->devinit),
1475                        readl (&dev->regs->fifoctl),
1476                        s,
1477                        readl (&dev->regs->pciirqenb0),
1478                        readl (&dev->regs->pciirqenb1),
1479                        readl (&dev->regs->irqstat0),
1480                        readl (&dev->regs->irqstat1));
1481        size -= t;
1482        next += t;
1483
1484        /* USB Control Registers */
1485        t1 = readl (&dev->usb->usbctl);
1486        t2 = readl (&dev->usb->usbstat);
1487        if (t1 & (1 << VBUS_PIN)) {
1488                if (t2 & (1 << HIGH_SPEED))
1489                        s = "high speed";
1490                else if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1491                        s = "powered";
1492                else
1493                        s = "full speed";
1494                /* full speed bit (6) not working?? */
1495        } else
1496                        s = "not attached";
1497        t = scnprintf (next, size,
1498                        "stdrsp %08x usbctl %08x usbstat %08x "
1499                                "addr 0x%02x (%s)\n",
1500                        readl (&dev->usb->stdrsp), t1, t2,
1501                        readl (&dev->usb->ouraddr), s);
1502        size -= t;
1503        next += t;
1504
1505        /* PCI Master Control Registers */
1506
1507        /* DMA Control Registers */
1508
1509        /* Configurable EP Control Registers */
1510        for (i = 0; i < 7; i++) {
1511                struct net2280_ep       *ep;
1512
1513                ep = &dev->ep [i];
1514                if (i && !ep->desc)
1515                        continue;
1516
1517                t1 = readl (&ep->regs->ep_cfg);
1518                t2 = readl (&ep->regs->ep_rsp) & 0xff;
1519                t = scnprintf (next, size,
1520                                "\n%s\tcfg %05x rsp (%02x) %s%s%s%s%s%s%s%s"
1521                                        "irqenb %02x\n",
1522                                ep->ep.name, t1, t2,
1523                                (t2 & (1 << CLEAR_NAK_OUT_PACKETS))
1524                                        ? "NAK " : "",
1525                                (t2 & (1 << CLEAR_EP_HIDE_STATUS_PHASE))
1526                                        ? "hide " : "",
1527                                (t2 & (1 << CLEAR_EP_FORCE_CRC_ERROR))
1528                                        ? "CRC " : "",
1529                                (t2 & (1 << CLEAR_INTERRUPT_MODE))
1530                                        ? "interrupt " : "",
1531                                (t2 & (1<<CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE))
1532                                        ? "status " : "",
1533                                (t2 & (1 << CLEAR_NAK_OUT_PACKETS_MODE))
1534                                        ? "NAKmode " : "",
1535                                (t2 & (1 << CLEAR_ENDPOINT_TOGGLE))
1536                                        ? "DATA1 " : "DATA0 ",
1537                                (t2 & (1 << CLEAR_ENDPOINT_HALT))
1538                                        ? "HALT " : "",
1539                                readl (&ep->regs->ep_irqenb));
1540                size -= t;
1541                next += t;
1542
1543                t = scnprintf (next, size,
1544                                "\tstat %08x avail %04x "
1545                                "(ep%d%s-%s)%s\n",
1546                                readl (&ep->regs->ep_stat),
1547                                readl (&ep->regs->ep_avail),
1548                                t1 & 0x0f, DIR_STRING (t1),
1549                                type_string (t1 >> 8),
1550                                ep->stopped ? "*" : "");
1551                size -= t;
1552                next += t;
1553
1554                if (!ep->dma)
1555                        continue;
1556
1557                t = scnprintf (next, size,
1558                                "  dma\tctl %08x stat %08x count %08x\n"
1559                                "\taddr %08x desc %08x\n",
1560                                readl (&ep->dma->dmactl),
1561                                readl (&ep->dma->dmastat),
1562                                readl (&ep->dma->dmacount),
1563                                readl (&ep->dma->dmaaddr),
1564                                readl (&ep->dma->dmadesc));
1565                size -= t;
1566                next += t;
1567
1568        }
1569
1570        /* Indexed Registers */
1571                // none yet
1572
1573        /* Statistics */
1574        t = scnprintf (next, size, "\nirqs:  ");
1575        size -= t;
1576        next += t;
1577        for (i = 0; i < 7; i++) {
1578                struct net2280_ep       *ep;
1579
1580                ep = &dev->ep [i];
1581                if (i && !ep->irqs)
1582                        continue;
1583                t = scnprintf (next, size, " %s/%lu", ep->ep.name, ep->irqs);
1584                size -= t;
1585                next += t;
1586
1587        }
1588        t = scnprintf (next, size, "\n");
1589        size -= t;
1590        next += t;
1591
1592        spin_unlock_irqrestore (&dev->lock, flags);
1593
1594        return PAGE_SIZE - size;
1595}
1596static DEVICE_ATTR(registers, S_IRUGO, net2280_show_registers, NULL);
1597
1598static ssize_t
1599show_queues (struct device *_dev, struct device_attribute *attr, char *buf)
1600{
1601        struct net2280          *dev;
1602        char                    *next;
1603        unsigned                size;
1604        unsigned long           flags;
1605        int                     i;
1606
1607        dev = dev_get_drvdata (_dev);
1608        next = buf;
1609        size = PAGE_SIZE;
1610        spin_lock_irqsave (&dev->lock, flags);
1611
1612        for (i = 0; i < 7; i++) {
1613                struct net2280_ep               *ep = &dev->ep [i];
1614                struct net2280_request          *req;
1615                int                             t;
1616
1617                if (i != 0) {
1618                        const struct usb_endpoint_descriptor    *d;
1619
1620                        d = ep->desc;
1621                        if (!d)
1622                                continue;
1623                        t = d->bEndpointAddress;
1624                        t = scnprintf (next, size,
1625                                "\n%s (ep%d%s-%s) max %04x %s fifo %d\n",
1626                                ep->ep.name, t & USB_ENDPOINT_NUMBER_MASK,
1627                                (t & USB_DIR_IN) ? "in" : "out",
1628                                ({ char *val;
1629                                 switch (d->bmAttributes & 0x03) {
1630                                 case USB_ENDPOINT_XFER_BULK:
1631                                        val = "bulk"; break;
1632                                 case USB_ENDPOINT_XFER_INT:
1633                                        val = "intr"; break;
1634                                 default:
1635                                        val = "iso"; break;
1636                                 }; val; }),
1637                                usb_endpoint_maxp (d) & 0x1fff,
1638                                ep->dma ? "dma" : "pio", ep->fifo_size
1639                                );
1640                } else /* ep0 should only have one transfer queued */
1641                        t = scnprintf (next, size, "ep0 max 64 pio %s\n",
1642                                        ep->is_in ? "in" : "out");
1643                if (t <= 0 || t > size)
1644                        goto done;
1645                size -= t;
1646                next += t;
1647
1648                if (list_empty (&ep->queue)) {
1649                        t = scnprintf (next, size, "\t(nothing queued)\n");
1650                        if (t <= 0 || t > size)
1651                                goto done;
1652                        size -= t;
1653                        next += t;
1654                        continue;
1655                }
1656                list_for_each_entry (req, &ep->queue, queue) {
1657                        if (ep->dma && req->td_dma == readl (&ep->dma->dmadesc))
1658                                t = scnprintf (next, size,
1659                                        "\treq %p len %d/%d "
1660                                        "buf %p (dmacount %08x)\n",
1661                                        &req->req, req->req.actual,
1662                                        req->req.length, req->req.buf,
1663                                        readl (&ep->dma->dmacount));
1664                        else
1665                                t = scnprintf (next, size,
1666                                        "\treq %p len %d/%d buf %p\n",
1667                                        &req->req, req->req.actual,
1668                                        req->req.length, req->req.buf);
1669                        if (t <= 0 || t > size)
1670                                goto done;
1671                        size -= t;
1672                        next += t;
1673
1674                        if (ep->dma) {
1675                                struct net2280_dma      *td;
1676
1677                                td = req->td;
1678                                t = scnprintf (next, size, "\t    td %08x "
1679                                        " count %08x buf %08x desc %08x\n",
1680                                        (u32) req->td_dma,
1681                                        le32_to_cpu (td->dmacount),
1682                                        le32_to_cpu (td->dmaaddr),
1683                                        le32_to_cpu (td->dmadesc));
1684                                if (t <= 0 || t > size)
1685                                        goto done;
1686                                size -= t;
1687                                next += t;
1688                        }
1689                }
1690        }
1691
1692done:
1693        spin_unlock_irqrestore (&dev->lock, flags);
1694        return PAGE_SIZE - size;
1695}
1696static DEVICE_ATTR (queues, S_IRUGO, show_queues, NULL);
1697
1698
1699#else
1700
1701#define device_create_file(a,b) (0)
1702#define device_remove_file(a,b) do { } while (0)
1703
1704#endif
1705
1706/*-------------------------------------------------------------------------*/
1707
1708/* another driver-specific mode might be a request type doing dma
1709 * to/from another device fifo instead of to/from memory.
1710 */
1711
1712static void set_fifo_mode (struct net2280 *dev, int mode)
1713{
1714        /* keeping high bits preserves BAR2 */
1715        writel ((0xffff << PCI_BASE2_RANGE) | mode, &dev->regs->fifoctl);
1716
1717        /* always ep-{a,b,e,f} ... maybe not ep-c or ep-d */
1718        INIT_LIST_HEAD (&dev->gadget.ep_list);
1719        list_add_tail (&dev->ep [1].ep.ep_list, &dev->gadget.ep_list);
1720        list_add_tail (&dev->ep [2].ep.ep_list, &dev->gadget.ep_list);
1721        switch (mode) {
1722        case 0:
1723                list_add_tail (&dev->ep [3].ep.ep_list, &dev->gadget.ep_list);
1724                list_add_tail (&dev->ep [4].ep.ep_list, &dev->gadget.ep_list);
1725                dev->ep [1].fifo_size = dev->ep [2].fifo_size = 1024;
1726                break;
1727        case 1:
1728                dev->ep [1].fifo_size = dev->ep [2].fifo_size = 2048;
1729                break;
1730        case 2:
1731                list_add_tail (&dev->ep [3].ep.ep_list, &dev->gadget.ep_list);
1732                dev->ep [1].fifo_size = 2048;
1733                dev->ep [2].fifo_size = 1024;
1734                break;
1735        }
1736        /* fifo sizes for ep0, ep-c, ep-d, ep-e, and ep-f never change */
1737        list_add_tail (&dev->ep [5].ep.ep_list, &dev->gadget.ep_list);
1738        list_add_tail (&dev->ep [6].ep.ep_list, &dev->gadget.ep_list);
1739}
1740
1741/* keeping it simple:
1742 * - one bus driver, initted first;
1743 * - one function driver, initted second
1744 *
1745 * most of the work to support multiple net2280 controllers would
1746 * be to associate this gadget driver (yes?) with all of them, or
1747 * perhaps to bind specific drivers to specific devices.
1748 */
1749
1750static void usb_reset (struct net2280 *dev)
1751{
1752        u32     tmp;
1753
1754        dev->gadget.speed = USB_SPEED_UNKNOWN;
1755        (void) readl (&dev->usb->usbctl);
1756
1757        net2280_led_init (dev);
1758
1759        /* disable automatic responses, and irqs */
1760        writel (0, &dev->usb->stdrsp);
1761        writel (0, &dev->regs->pciirqenb0);
1762        writel (0, &dev->regs->pciirqenb1);
1763
1764        /* clear old dma and irq state */
1765        for (tmp = 0; tmp < 4; tmp++) {
1766                struct net2280_ep       *ep = &dev->ep [tmp + 1];
1767
1768                if (ep->dma)
1769                        abort_dma (ep);
1770        }
1771        writel (~0, &dev->regs->irqstat0),
1772        writel (~(1 << SUSPEND_REQUEST_INTERRUPT), &dev->regs->irqstat1),
1773
1774        /* reset, and enable pci */
1775        tmp = readl (&dev->regs->devinit)
1776                | (1 << PCI_ENABLE)
1777                | (1 << FIFO_SOFT_RESET)
1778                | (1 << USB_SOFT_RESET)
1779                | (1 << M8051_RESET);
1780        writel (tmp, &dev->regs->devinit);
1781
1782        /* standard fifo and endpoint allocations */
1783        set_fifo_mode (dev, (fifo_mode <= 2) ? fifo_mode : 0);
1784}
1785
1786static void usb_reinit (struct net2280 *dev)
1787{
1788        u32     tmp;
1789        int     init_dma;
1790
1791        /* use_dma changes are ignored till next device re-init */
1792        init_dma = use_dma;
1793
1794        /* basic endpoint init */
1795        for (tmp = 0; tmp < 7; tmp++) {
1796                struct net2280_ep       *ep = &dev->ep [tmp];
1797
1798                ep->ep.name = ep_name [tmp];
1799                ep->dev = dev;
1800                ep->num = tmp;
1801
1802                if (tmp > 0 && tmp <= 4) {
1803                        ep->fifo_size = 1024;
1804                        if (init_dma)
1805                                ep->dma = &dev->dma [tmp - 1];
1806                } else
1807                        ep->fifo_size = 64;
1808                ep->regs = &dev->epregs [tmp];
1809                ep_reset (dev->regs, ep);
1810        }
1811        dev->ep [0].ep.maxpacket = 64;
1812        dev->ep [5].ep.maxpacket = 64;
1813        dev->ep [6].ep.maxpacket = 64;
1814
1815        dev->gadget.ep0 = &dev->ep [0].ep;
1816        dev->ep [0].stopped = 0;
1817        INIT_LIST_HEAD (&dev->gadget.ep0->ep_list);
1818
1819        /* we want to prevent lowlevel/insecure access from the USB host,
1820         * but erratum 0119 means this enable bit is ignored
1821         */
1822        for (tmp = 0; tmp < 5; tmp++)
1823                writel (EP_DONTUSE, &dev->dep [tmp].dep_cfg);
1824}
1825
1826static void ep0_start (struct net2280 *dev)
1827{
1828        writel (  (1 << CLEAR_EP_HIDE_STATUS_PHASE)
1829                | (1 << CLEAR_NAK_OUT_PACKETS)
1830                | (1 << CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE)
1831                , &dev->epregs [0].ep_rsp);
1832
1833        /*
1834         * hardware optionally handles a bunch of standard requests
1835         * that the API hides from drivers anyway.  have it do so.
1836         * endpoint status/features are handled in software, to
1837         * help pass tests for some dubious behavior.
1838         */
1839        writel (  (1 << SET_TEST_MODE)
1840                | (1 << SET_ADDRESS)
1841                | (1 << DEVICE_SET_CLEAR_DEVICE_REMOTE_WAKEUP)
1842                | (1 << GET_DEVICE_STATUS)
1843                | (1 << GET_INTERFACE_STATUS)
1844                , &dev->usb->stdrsp);
1845        writel (  (1 << USB_ROOT_PORT_WAKEUP_ENABLE)
1846                | (1 << SELF_POWERED_USB_DEVICE)
1847                | (1 << REMOTE_WAKEUP_SUPPORT)
1848                | (dev->softconnect << USB_DETECT_ENABLE)
1849                | (1 << SELF_POWERED_STATUS)
1850                , &dev->usb->usbctl);
1851
1852        /* enable irqs so we can see ep0 and general operation  */
1853        writel (  (1 << SETUP_PACKET_INTERRUPT_ENABLE)
1854                | (1 << ENDPOINT_0_INTERRUPT_ENABLE)
1855                , &dev->regs->pciirqenb0);
1856        writel (  (1 << PCI_INTERRUPT_ENABLE)
1857                | (1 << PCI_MASTER_ABORT_RECEIVED_INTERRUPT_ENABLE)
1858                | (1 << PCI_TARGET_ABORT_RECEIVED_INTERRUPT_ENABLE)
1859                | (1 << PCI_RETRY_ABORT_INTERRUPT_ENABLE)
1860                | (1 << VBUS_INTERRUPT_ENABLE)
1861                | (1 << ROOT_PORT_RESET_INTERRUPT_ENABLE)
1862                | (1 << SUSPEND_REQUEST_CHANGE_INTERRUPT_ENABLE)
1863                , &dev->regs->pciirqenb1);
1864
1865        /* don't leave any writes posted */
1866        (void) readl (&dev->usb->usbctl);
1867}
1868
1869/* when a driver is successfully registered, it will receive
1870 * control requests including set_configuration(), which enables
1871 * non-control requests.  then usb traffic follows until a
1872 * disconnect is reported.  then a host may connect again, or
1873 * the driver might get unbound.
1874 */
1875static int net2280_start(struct usb_gadget *_gadget,
1876                struct usb_gadget_driver *driver)
1877{
1878        struct net2280          *dev;
1879        int                     retval;
1880        unsigned                i;
1881
1882        /* insist on high speed support from the driver, since
1883         * (dev->usb->xcvrdiag & FORCE_FULL_SPEED_MODE)
1884         * "must not be used in normal operation"
1885         */
1886        if (!driver || driver->max_speed < USB_SPEED_HIGH
1887                        || !driver->setup)
1888                return -EINVAL;
1889
1890        dev = container_of (_gadget, struct net2280, gadget);
1891
1892        for (i = 0; i < 7; i++)
1893                dev->ep [i].irqs = 0;
1894
1895        /* hook up the driver ... */
1896        dev->softconnect = 1;
1897        driver->driver.bus = NULL;
1898        dev->driver = driver;
1899        dev->gadget.dev.driver = &driver->driver;
1900
1901        retval = device_create_file (&dev->pdev->dev, &dev_attr_function);
1902        if (retval) goto err_unbind;
1903        retval = device_create_file (&dev->pdev->dev, &dev_attr_queues);
1904        if (retval) goto err_func;
1905
1906        /* Enable force-full-speed testing mode, if desired */
1907        if (full_speed)
1908                writel(1 << FORCE_FULL_SPEED_MODE, &dev->usb->xcvrdiag);
1909
1910        /* ... then enable host detection and ep0; and we're ready
1911         * for set_configuration as well as eventual disconnect.
1912         */
1913        net2280_led_active (dev, 1);
1914        ep0_start (dev);
1915
1916        DEBUG (dev, "%s ready, usbctl %08x stdrsp %08x\n",
1917                        driver->driver.name,
1918                        readl (&dev->usb->usbctl),
1919                        readl (&dev->usb->stdrsp));
1920
1921        /* pci writes may still be posted */
1922        return 0;
1923
1924err_func:
1925        device_remove_file (&dev->pdev->dev, &dev_attr_function);
1926err_unbind:
1927        dev->gadget.dev.driver = NULL;
1928        dev->driver = NULL;
1929        return retval;
1930}
1931
1932static void
1933stop_activity (struct net2280 *dev, struct usb_gadget_driver *driver)
1934{
1935        int                     i;
1936
1937        /* don't disconnect if it's not connected */
1938        if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1939                driver = NULL;
1940
1941        /* stop hardware; prevent new request submissions;
1942         * and kill any outstanding requests.
1943         */
1944        usb_reset (dev);
1945        for (i = 0; i < 7; i++)
1946                nuke (&dev->ep [i]);
1947
1948        /* report disconnect; the driver is already quiesced */
1949        if (driver) {
1950                spin_unlock(&dev->lock);
1951                driver->disconnect(&dev->gadget);
1952                spin_lock(&dev->lock);
1953        }
1954
1955        usb_reinit (dev);
1956}
1957
1958static int net2280_stop(struct usb_gadget *_gadget,
1959                struct usb_gadget_driver *driver)
1960{
1961        struct net2280  *dev;
1962        unsigned long   flags;
1963
1964        dev = container_of (_gadget, struct net2280, gadget);
1965
1966        spin_lock_irqsave (&dev->lock, flags);
1967        stop_activity (dev, driver);
1968        spin_unlock_irqrestore (&dev->lock, flags);
1969
1970        dev->gadget.dev.driver = NULL;
1971        dev->driver = NULL;
1972
1973        net2280_led_active (dev, 0);
1974
1975        /* Disable full-speed test mode */
1976        writel(0, &dev->usb->xcvrdiag);
1977
1978        device_remove_file (&dev->pdev->dev, &dev_attr_function);
1979        device_remove_file (&dev->pdev->dev, &dev_attr_queues);
1980
1981        DEBUG (dev, "unregistered driver '%s'\n", driver->driver.name);
1982        return 0;
1983}
1984
1985/*-------------------------------------------------------------------------*/
1986
1987/* handle ep0, ep-e, ep-f with 64 byte packets: packet per irq.
1988 * also works for dma-capable endpoints, in pio mode or just
1989 * to manually advance the queue after short OUT transfers.
1990 */
1991static void handle_ep_small (struct net2280_ep *ep)
1992{
1993        struct net2280_request  *req;
1994        u32                     t;
1995        /* 0 error, 1 mid-data, 2 done */
1996        int                     mode = 1;
1997
1998        if (!list_empty (&ep->queue))
1999                req = list_entry (ep->queue.next,
2000                        struct net2280_request, queue);
2001        else
2002                req = NULL;
2003
2004        /* ack all, and handle what we care about */
2005        t = readl (&ep->regs->ep_stat);
2006        ep->irqs++;
2007#if 0
2008        VDEBUG (ep->dev, "%s ack ep_stat %08x, req %p\n",
2009                        ep->ep.name, t, req ? &req->req : 0);
2010#endif
2011        if (!ep->is_in || ep->dev->pdev->device == 0x2280)
2012                writel (t & ~(1 << NAK_OUT_PACKETS), &ep->regs->ep_stat);
2013        else
2014                /* Added for 2282 */
2015                writel (t, &ep->regs->ep_stat);
2016
2017        /* for ep0, monitor token irqs to catch data stage length errors
2018         * and to synchronize on status.
2019         *
2020         * also, to defer reporting of protocol stalls ... here's where
2021         * data or status first appears, handling stalls here should never
2022         * cause trouble on the host side..
2023         *
2024         * control requests could be slightly faster without token synch for
2025         * status, but status can jam up that way.
2026         */
2027        if (unlikely (ep->num == 0)) {
2028                if (ep->is_in) {
2029                        /* status; stop NAKing */
2030                        if (t & (1 << DATA_OUT_PING_TOKEN_INTERRUPT)) {
2031                                if (ep->dev->protocol_stall) {
2032                                        ep->stopped = 1;
2033                                        set_halt (ep);
2034                                }
2035                                if (!req)
2036                                        allow_status (ep);
2037                                mode = 2;
2038                        /* reply to extra IN data tokens with a zlp */
2039                        } else if (t & (1 << DATA_IN_TOKEN_INTERRUPT)) {
2040                                if (ep->dev->protocol_stall) {
2041                                        ep->stopped = 1;
2042                                        set_halt (ep);
2043                                        mode = 2;
2044                                } else if (ep->responded &&
2045                                                !req && !ep->stopped)
2046                                        write_fifo (ep, NULL);
2047                        }
2048                } else {
2049                        /* status; stop NAKing */
2050                        if (t & (1 << DATA_IN_TOKEN_INTERRUPT)) {
2051                                if (ep->dev->protocol_stall) {
2052                                        ep->stopped = 1;
2053                                        set_halt (ep);
2054                                }
2055                                mode = 2;
2056                        /* an extra OUT token is an error */
2057                        } else if (((t & (1 << DATA_OUT_PING_TOKEN_INTERRUPT))
2058                                        && req
2059                                        && req->req.actual == req->req.length)
2060                                        || (ep->responded && !req)) {
2061                                ep->dev->protocol_stall = 1;
2062                                set_halt (ep);
2063                                ep->stopped = 1;
2064                                if (req)
2065                                        done (ep, req, -EOVERFLOW);
2066                                req = NULL;
2067                        }
2068                }
2069        }
2070
2071        if (unlikely (!req))
2072                return;
2073
2074        /* manual DMA queue advance after short OUT */
2075        if (likely (ep->dma != 0)) {
2076                if (t & (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)) {
2077                        u32     count;
2078                        int     stopped = ep->stopped;
2079
2080                        /* TRANSFERRED works around OUT_DONE erratum 0112.
2081                         * we expect (N <= maxpacket) bytes; host wrote M.
2082                         * iff (M < N) we won't ever see a DMA interrupt.
2083                         */
2084                        ep->stopped = 1;
2085                        for (count = 0; ; t = readl (&ep->regs->ep_stat)) {
2086
2087                                /* any preceding dma transfers must finish.
2088                                 * dma handles (M >= N), may empty the queue
2089                                 */
2090                                scan_dma_completions (ep);
2091                                if (unlikely (list_empty (&ep->queue)
2092                                                || ep->out_overflow)) {
2093                                        req = NULL;
2094                                        break;
2095                                }
2096                                req = list_entry (ep->queue.next,
2097                                        struct net2280_request, queue);
2098
2099                                /* here either (M < N), a "real" short rx;
2100                                 * or (M == N) and the queue didn't empty
2101                                 */
2102                                if (likely (t & (1 << FIFO_EMPTY))) {
2103                                        count = readl (&ep->dma->dmacount);
2104                                        count &= DMA_BYTE_COUNT_MASK;
2105                                        if (readl (&ep->dma->dmadesc)
2106                                                        != req->td_dma)
2107                                                req = NULL;
2108                                        break;
2109                                }
2110                                udelay(1);
2111                        }
2112
2113                        /* stop DMA, leave ep NAKing */
2114                        writel ((1 << DMA_ABORT), &ep->dma->dmastat);
2115                        spin_stop_dma (ep->dma);
2116
2117                        if (likely (req)) {
2118                                req->td->dmacount = 0;
2119                                t = readl (&ep->regs->ep_avail);
2120                                dma_done (ep, req, count,
2121                                        (ep->out_overflow || t)
2122                                                ? -EOVERFLOW : 0);
2123                        }
2124
2125                        /* also flush to prevent erratum 0106 trouble */
2126                        if (unlikely (ep->out_overflow
2127                                        || (ep->dev->chiprev == 0x0100
2128                                                && ep->dev->gadget.speed
2129                                                        == USB_SPEED_FULL))) {
2130                                out_flush (ep);
2131                                ep->out_overflow = 0;
2132                        }
2133
2134                        /* (re)start dma if needed, stop NAKing */
2135                        ep->stopped = stopped;
2136                        if (!list_empty (&ep->queue))
2137                                restart_dma (ep);
2138                } else
2139                        DEBUG (ep->dev, "%s dma ep_stat %08x ??\n",
2140                                        ep->ep.name, t);
2141                return;
2142
2143        /* data packet(s) received (in the fifo, OUT) */
2144        } else if (t & (1 << DATA_PACKET_RECEIVED_INTERRUPT)) {
2145                if (read_fifo (ep, req) && ep->num != 0)
2146                        mode = 2;
2147
2148        /* data packet(s) transmitted (IN) */
2149        } else if (t & (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)) {
2150                unsigned        len;
2151
2152                len = req->req.length - req->req.actual;
2153                if (len > ep->ep.maxpacket)
2154                        len = ep->ep.maxpacket;
2155                req->req.actual += len;
2156
2157                /* if we wrote it all, we're usually done */
2158                if (req->req.actual == req->req.length) {
2159                        if (ep->num == 0) {
2160                                /* send zlps until the status stage */
2161                        } else if (!req->req.zero || len != ep->ep.maxpacket)
2162                                mode = 2;
2163                }
2164
2165        /* there was nothing to do ...  */
2166        } else if (mode == 1)
2167                return;
2168
2169        /* done */
2170        if (mode == 2) {
2171                /* stream endpoints often resubmit/unlink in completion */
2172                done (ep, req, 0);
2173
2174                /* maybe advance queue to next request */
2175                if (ep->num == 0) {
2176                        /* NOTE:  net2280 could let gadget driver start the
2177                         * status stage later. since not all controllers let
2178                         * them control that, the api doesn't (yet) allow it.
2179                         */
2180                        if (!ep->stopped)
2181                                allow_status (ep);
2182                        req = NULL;
2183                } else {
2184                        if (!list_empty (&ep->queue) && !ep->stopped)
2185                                req = list_entry (ep->queue.next,
2186                                        struct net2280_request, queue);
2187                        else
2188                                req = NULL;
2189                        if (req && !ep->is_in)
2190                                stop_out_naking (ep);
2191                }
2192        }
2193
2194        /* is there a buffer for the next packet?
2195         * for best streaming performance, make sure there is one.
2196         */
2197        if (req && !ep->stopped) {
2198
2199                /* load IN fifo with next packet (may be zlp) */
2200                if (t & (1 << DATA_PACKET_TRANSMITTED_INTERRUPT))
2201                        write_fifo (ep, &req->req);
2202        }
2203}
2204
2205static struct net2280_ep *
2206get_ep_by_addr (struct net2280 *dev, u16 wIndex)
2207{
2208        struct net2280_ep       *ep;
2209
2210        if ((wIndex & USB_ENDPOINT_NUMBER_MASK) == 0)
2211                return &dev->ep [0];
2212        list_for_each_entry (ep, &dev->gadget.ep_list, ep.ep_list) {
2213                u8      bEndpointAddress;
2214
2215                if (!ep->desc)
2216                        continue;
2217                bEndpointAddress = ep->desc->bEndpointAddress;
2218                if ((wIndex ^ bEndpointAddress) & USB_DIR_IN)
2219                        continue;
2220                if ((wIndex & 0x0f) == (bEndpointAddress & 0x0f))
2221                        return ep;
2222        }
2223        return NULL;
2224}
2225
2226static void handle_stat0_irqs (struct net2280 *dev, u32 stat)
2227{
2228        struct net2280_ep       *ep;
2229        u32                     num, scratch;
2230
2231        /* most of these don't need individual acks */
2232        stat &= ~(1 << INTA_ASSERTED);
2233        if (!stat)
2234                return;
2235        // DEBUG (dev, "irqstat0 %04x\n", stat);
2236
2237        /* starting a control request? */
2238        if (unlikely (stat & (1 << SETUP_PACKET_INTERRUPT))) {
2239                union {
2240                        u32                     raw [2];
2241                        struct usb_ctrlrequest  r;
2242                } u;
2243                int                             tmp;
2244                struct net2280_request          *req;
2245
2246                if (dev->gadget.speed == USB_SPEED_UNKNOWN) {
2247                        if (readl (&dev->usb->usbstat) & (1 << HIGH_SPEED))
2248                                dev->gadget.speed = USB_SPEED_HIGH;
2249                        else
2250                                dev->gadget.speed = USB_SPEED_FULL;
2251                        net2280_led_speed (dev, dev->gadget.speed);
2252                        DEBUG(dev, "%s\n", usb_speed_string(dev->gadget.speed));
2253                }
2254
2255                ep = &dev->ep [0];
2256                ep->irqs++;
2257
2258                /* make sure any leftover request state is cleared */
2259                stat &= ~(1 << ENDPOINT_0_INTERRUPT);
2260                while (!list_empty (&ep->queue)) {
2261                        req = list_entry (ep->queue.next,
2262                                        struct net2280_request, queue);
2263                        done (ep, req, (req->req.actual == req->req.length)
2264                                                ? 0 : -EPROTO);
2265                }
2266                ep->stopped = 0;
2267                dev->protocol_stall = 0;
2268
2269                if (ep->dev->pdev->device == 0x2280)
2270                        tmp = (1 << FIFO_OVERFLOW)
2271                                | (1 << FIFO_UNDERFLOW);
2272                else
2273                        tmp = 0;
2274
2275                writel (tmp | (1 << TIMEOUT)
2276                        | (1 << USB_STALL_SENT)
2277                        | (1 << USB_IN_NAK_SENT)
2278                        | (1 << USB_IN_ACK_RCVD)
2279                        | (1 << USB_OUT_PING_NAK_SENT)
2280                        | (1 << USB_OUT_ACK_SENT)
2281                        | (1 << SHORT_PACKET_OUT_DONE_INTERRUPT)
2282                        | (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)
2283                        | (1 << DATA_PACKET_RECEIVED_INTERRUPT)
2284                        | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
2285                        | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
2286                        | (1 << DATA_IN_TOKEN_INTERRUPT)
2287                        , &ep->regs->ep_stat);
2288                u.raw [0] = readl (&dev->usb->setup0123);
2289                u.raw [1] = readl (&dev->usb->setup4567);
2290
2291                cpu_to_le32s (&u.raw [0]);
2292                cpu_to_le32s (&u.raw [1]);
2293
2294                tmp = 0;
2295
2296#define w_value         le16_to_cpu(u.r.wValue)
2297#define w_index         le16_to_cpu(u.r.wIndex)
2298#define w_length        le16_to_cpu(u.r.wLength)
2299
2300                /* ack the irq */
2301                writel (1 << SETUP_PACKET_INTERRUPT, &dev->regs->irqstat0);
2302                stat ^= (1 << SETUP_PACKET_INTERRUPT);
2303
2304                /* watch control traffic at the token level, and force
2305                 * synchronization before letting the status stage happen.
2306                 * FIXME ignore tokens we'll NAK, until driver responds.
2307                 * that'll mean a lot less irqs for some drivers.
2308                 */
2309                ep->is_in = (u.r.bRequestType & USB_DIR_IN) != 0;
2310                if (ep->is_in) {
2311                        scratch = (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
2312                                | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
2313                                | (1 << DATA_IN_TOKEN_INTERRUPT);
2314                        stop_out_naking (ep);
2315                } else
2316                        scratch = (1 << DATA_PACKET_RECEIVED_INTERRUPT)
2317                                | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
2318                                | (1 << DATA_IN_TOKEN_INTERRUPT);
2319                writel (scratch, &dev->epregs [0].ep_irqenb);
2320
2321                /* we made the hardware handle most lowlevel requests;
2322                 * everything else goes uplevel to the gadget code.
2323                 */
2324                ep->responded = 1;
2325                switch (u.r.bRequest) {
2326                case USB_REQ_GET_STATUS: {
2327                        struct net2280_ep       *e;
2328                        __le32                  status;
2329
2330                        /* hw handles device and interface status */
2331                        if (u.r.bRequestType != (USB_DIR_IN|USB_RECIP_ENDPOINT))
2332                                goto delegate;
2333                        if ((e = get_ep_by_addr (dev, w_index)) == 0
2334                                        || w_length > 2)
2335                                goto do_stall;
2336
2337                        if (readl (&e->regs->ep_rsp)
2338                                        & (1 << SET_ENDPOINT_HALT))
2339                                status = cpu_to_le32 (1);
2340                        else
2341                                status = cpu_to_le32 (0);
2342
2343                        /* don't bother with a request object! */
2344                        writel (0, &dev->epregs [0].ep_irqenb);
2345                        set_fifo_bytecount (ep, w_length);
2346                        writel ((__force u32)status, &dev->epregs [0].ep_data);
2347                        allow_status (ep);
2348                        VDEBUG (dev, "%s stat %02x\n", ep->ep.name, status);
2349                        goto next_endpoints;
2350                        }
2351                        break;
2352                case USB_REQ_CLEAR_FEATURE: {
2353                        struct net2280_ep       *e;
2354
2355                        /* hw handles device features */
2356                        if (u.r.bRequestType != USB_RECIP_ENDPOINT)
2357                                goto delegate;
2358                        if (w_value != USB_ENDPOINT_HALT
2359                                        || w_length != 0)
2360                                goto do_stall;
2361                        if ((e = get_ep_by_addr (dev, w_index)) == 0)
2362                                goto do_stall;
2363                        if (e->wedged) {
2364                                VDEBUG(dev, "%s wedged, halt not cleared\n",
2365                                                ep->ep.name);
2366                        } else {
2367                                VDEBUG(dev, "%s clear halt\n", ep->ep.name);
2368                                clear_halt(e);
2369                        }
2370                        allow_status (ep);
2371                        goto next_endpoints;
2372                        }
2373                        break;
2374                case USB_REQ_SET_FEATURE: {
2375                        struct net2280_ep       *e;
2376
2377                        /* hw handles device features */
2378                        if (u.r.bRequestType != USB_RECIP_ENDPOINT)
2379                                goto delegate;
2380                        if (w_value != USB_ENDPOINT_HALT
2381                                        || w_length != 0)
2382                                goto do_stall;
2383                        if ((e = get_ep_by_addr (dev, w_index)) == 0)
2384                                goto do_stall;
2385                        if (e->ep.name == ep0name)
2386                                goto do_stall;
2387                        set_halt (e);
2388                        allow_status (ep);
2389                        VDEBUG (dev, "%s set halt\n", ep->ep.name);
2390                        goto next_endpoints;
2391                        }
2392                        break;
2393                default:
2394delegate:
2395                        VDEBUG (dev, "setup %02x.%02x v%04x i%04x l%04x "
2396                                "ep_cfg %08x\n",
2397                                u.r.bRequestType, u.r.bRequest,
2398                                w_value, w_index, w_length,
2399                                readl (&ep->regs->ep_cfg));
2400                        ep->responded = 0;
2401                        spin_unlock (&dev->lock);
2402                        tmp = dev->driver->setup (&dev->gadget, &u.r);
2403                        spin_lock (&dev->lock);
2404                }
2405
2406                /* stall ep0 on error */
2407                if (tmp < 0) {
2408do_stall:
2409                        VDEBUG (dev, "req %02x.%02x protocol STALL; stat %d\n",
2410                                        u.r.bRequestType, u.r.bRequest, tmp);
2411                        dev->protocol_stall = 1;
2412                }
2413
2414                /* some in/out token irq should follow; maybe stall then.
2415                 * driver must queue a request (even zlp) or halt ep0
2416                 * before the host times out.
2417                 */
2418        }
2419
2420#undef  w_value
2421#undef  w_index
2422#undef  w_length
2423
2424next_endpoints:
2425        /* endpoint data irq ? */
2426        scratch = stat & 0x7f;
2427        stat &= ~0x7f;
2428        for (num = 0; scratch; num++) {
2429                u32             t;
2430
2431                /* do this endpoint's FIFO and queue need tending? */
2432                t = 1 << num;
2433                if ((scratch & t) == 0)
2434                        continue;
2435                scratch ^= t;
2436
2437                ep = &dev->ep [num];
2438                handle_ep_small (ep);
2439        }
2440
2441        if (stat)
2442                DEBUG (dev, "unhandled irqstat0 %08x\n", stat);
2443}
2444
2445#define DMA_INTERRUPTS ( \
2446                  (1 << DMA_D_INTERRUPT) \
2447                | (1 << DMA_C_INTERRUPT) \
2448                | (1 << DMA_B_INTERRUPT) \
2449                | (1 << DMA_A_INTERRUPT))
2450#define PCI_ERROR_INTERRUPTS ( \
2451                  (1 << PCI_MASTER_ABORT_RECEIVED_INTERRUPT) \
2452                | (1 << PCI_TARGET_ABORT_RECEIVED_INTERRUPT) \
2453                | (1 << PCI_RETRY_ABORT_INTERRUPT))
2454
2455static void handle_stat1_irqs (struct net2280 *dev, u32 stat)
2456{
2457        struct net2280_ep       *ep;
2458        u32                     tmp, num, mask, scratch;
2459
2460        /* after disconnect there's nothing else to do! */
2461        tmp = (1 << VBUS_INTERRUPT) | (1 << ROOT_PORT_RESET_INTERRUPT);
2462        mask = (1 << HIGH_SPEED) | (1 << FULL_SPEED);
2463
2464        /* VBUS disconnect is indicated by VBUS_PIN and VBUS_INTERRUPT set.
2465         * Root Port Reset is indicated by ROOT_PORT_RESET_INTERRUPT set and
2466         * both HIGH_SPEED and FULL_SPEED clear (as ROOT_PORT_RESET_INTERRUPT
2467         * only indicates a change in the reset state).
2468         */
2469        if (stat & tmp) {
2470                writel (tmp, &dev->regs->irqstat1);
2471                if ((((stat & (1 << ROOT_PORT_RESET_INTERRUPT))
2472                                        && ((readl (&dev->usb->usbstat) & mask)
2473                                                        == 0))
2474                                || ((readl (&dev->usb->usbctl)
2475                                        & (1 << VBUS_PIN)) == 0)
2476                            ) && ( dev->gadget.speed != USB_SPEED_UNKNOWN)) {
2477                        DEBUG (dev, "disconnect %s\n",
2478                                        dev->driver->driver.name);
2479                        stop_activity (dev, dev->driver);
2480                        ep0_start (dev);
2481                        return;
2482                }
2483                stat &= ~tmp;
2484
2485                /* vBUS can bounce ... one of many reasons to ignore the
2486                 * notion of hotplug events on bus connect/disconnect!
2487                 */
2488                if (!stat)
2489                        return;
2490        }
2491
2492        /* NOTE: chip stays in PCI D0 state for now, but it could
2493         * enter D1 to save more power
2494         */
2495        tmp = (1 << SUSPEND_REQUEST_CHANGE_INTERRUPT);
2496        if (stat & tmp) {
2497                writel (tmp, &dev->regs->irqstat1);
2498                if (stat & (1 << SUSPEND_REQUEST_INTERRUPT)) {
2499                        if (dev->driver->suspend)
2500                                dev->driver->suspend (&dev->gadget);
2501                        if (!enable_suspend)
2502                                stat &= ~(1 << SUSPEND_REQUEST_INTERRUPT);
2503                } else {
2504                        if (dev->driver->resume)
2505                                dev->driver->resume (&dev->gadget);
2506                        /* at high speed, note erratum 0133 */
2507                }
2508                stat &= ~tmp;
2509        }
2510
2511        /* clear any other status/irqs */
2512        if (stat)
2513                writel (stat, &dev->regs->irqstat1);
2514
2515        /* some status we can just ignore */
2516        if (dev->pdev->device == 0x2280)
2517                stat &= ~((1 << CONTROL_STATUS_INTERRUPT)
2518                          | (1 << SUSPEND_REQUEST_INTERRUPT)
2519                          | (1 << RESUME_INTERRUPT)
2520                          | (1 << SOF_INTERRUPT));
2521        else
2522                stat &= ~((1 << CONTROL_STATUS_INTERRUPT)
2523                          | (1 << RESUME_INTERRUPT)
2524                          | (1 << SOF_DOWN_INTERRUPT)
2525                          | (1 << SOF_INTERRUPT));
2526
2527        if (!stat)
2528                return;
2529        // DEBUG (dev, "irqstat1 %08x\n", stat);
2530
2531        /* DMA status, for ep-{a,b,c,d} */
2532        scratch = stat & DMA_INTERRUPTS;
2533        stat &= ~DMA_INTERRUPTS;
2534        scratch >>= 9;
2535        for (num = 0; scratch; num++) {
2536                struct net2280_dma_regs __iomem *dma;
2537
2538                tmp = 1 << num;
2539                if ((tmp & scratch) == 0)
2540                        continue;
2541                scratch ^= tmp;
2542
2543                ep = &dev->ep [num + 1];
2544                dma = ep->dma;
2545
2546                if (!dma)
2547                        continue;
2548
2549                /* clear ep's dma status */
2550                tmp = readl (&dma->dmastat);
2551                writel (tmp, &dma->dmastat);
2552
2553                /* chaining should stop on abort, short OUT from fifo,
2554                 * or (stat0 codepath) short OUT transfer.
2555                 */
2556                if (!use_dma_chaining) {
2557                        if ((tmp & (1 << DMA_TRANSACTION_DONE_INTERRUPT))
2558                                        == 0) {
2559                                DEBUG (ep->dev, "%s no xact done? %08x\n",
2560                                        ep->ep.name, tmp);
2561                                continue;
2562                        }
2563                        stop_dma (ep->dma);
2564                }
2565
2566                /* OUT transfers terminate when the data from the
2567                 * host is in our memory.  Process whatever's done.
2568                 * On this path, we know transfer's last packet wasn't
2569                 * less than req->length. NAK_OUT_PACKETS may be set,
2570                 * or the FIFO may already be holding new packets.
2571                 *
2572                 * IN transfers can linger in the FIFO for a very
2573                 * long time ... we ignore that for now, accounting
2574                 * precisely (like PIO does) needs per-packet irqs
2575                 */
2576                scan_dma_completions (ep);
2577
2578                /* disable dma on inactive queues; else maybe restart */
2579                if (list_empty (&ep->queue)) {
2580                        if (use_dma_chaining)
2581                                stop_dma (ep->dma);
2582                } else {
2583                        tmp = readl (&dma->dmactl);
2584                        if (!use_dma_chaining
2585                                        || (tmp & (1 << DMA_ENABLE)) == 0)
2586                                restart_dma (ep);
2587                        else if (ep->is_in && use_dma_chaining) {
2588                                struct net2280_request  *req;
2589                                __le32                  dmacount;
2590
2591                                /* the descriptor at the head of the chain
2592                                 * may still have VALID_BIT clear; that's
2593                                 * used to trigger changing DMA_FIFO_VALIDATE
2594                                 * (affects automagic zlp writes).
2595                                 */
2596                                req = list_entry (ep->queue.next,
2597                                                struct net2280_request, queue);
2598                                dmacount = req->td->dmacount;
2599                                dmacount &= cpu_to_le32 (
2600                                                (1 << VALID_BIT)
2601                                                | DMA_BYTE_COUNT_MASK);
2602                                if (dmacount && (dmacount & valid_bit) == 0)
2603                                        restart_dma (ep);
2604                        }
2605                }
2606                ep->irqs++;
2607        }
2608
2609        /* NOTE:  there are other PCI errors we might usefully notice.
2610         * if they appear very often, here's where to try recovering.
2611         */
2612        if (stat & PCI_ERROR_INTERRUPTS) {
2613                ERROR (dev, "pci dma error; stat %08x\n", stat);
2614                stat &= ~PCI_ERROR_INTERRUPTS;
2615                /* these are fatal errors, but "maybe" they won't
2616                 * happen again ...
2617                 */
2618                stop_activity (dev, dev->driver);
2619                ep0_start (dev);
2620                stat = 0;
2621        }
2622
2623        if (stat)
2624                DEBUG (dev, "unhandled irqstat1 %08x\n", stat);
2625}
2626
2627static irqreturn_t net2280_irq (int irq, void *_dev)
2628{
2629        struct net2280          *dev = _dev;
2630
2631        /* shared interrupt, not ours */
2632        if (!(readl(&dev->regs->irqstat0) & (1 << INTA_ASSERTED)))
2633                return IRQ_NONE;
2634
2635        spin_lock (&dev->lock);
2636
2637        /* handle disconnect, dma, and more */
2638        handle_stat1_irqs (dev, readl (&dev->regs->irqstat1));
2639
2640        /* control requests and PIO */
2641        handle_stat0_irqs (dev, readl (&dev->regs->irqstat0));
2642
2643        spin_unlock (&dev->lock);
2644
2645        return IRQ_HANDLED;
2646}
2647
2648/*-------------------------------------------------------------------------*/
2649
2650static void gadget_release (struct device *_dev)
2651{
2652        struct net2280  *dev = dev_get_drvdata (_dev);
2653
2654        kfree (dev);
2655}
2656
2657/* tear down the binding between this driver and the pci device */
2658
2659static void net2280_remove (struct pci_dev *pdev)
2660{
2661        struct net2280          *dev = pci_get_drvdata (pdev);
2662
2663        usb_del_gadget_udc(&dev->gadget);
2664
2665        BUG_ON(dev->driver);
2666
2667        /* then clean up the resources we allocated during probe() */
2668        net2280_led_shutdown (dev);
2669        if (dev->requests) {
2670                int             i;
2671                for (i = 1; i < 5; i++) {
2672                        if (!dev->ep [i].dummy)
2673                                continue;
2674                        pci_pool_free (dev->requests, dev->ep [i].dummy,
2675                                        dev->ep [i].td_dma);
2676                }
2677                pci_pool_destroy (dev->requests);
2678        }
2679        if (dev->got_irq)
2680                free_irq (pdev->irq, dev);
2681        if (dev->regs)
2682                iounmap (dev->regs);
2683        if (dev->region)
2684                release_mem_region (pci_resource_start (pdev, 0),
2685                                pci_resource_len (pdev, 0));
2686        if (dev->enabled)
2687                pci_disable_device (pdev);
2688        device_unregister (&dev->gadget.dev);
2689        device_remove_file (&pdev->dev, &dev_attr_registers);
2690        pci_set_drvdata (pdev, NULL);
2691
2692        INFO (dev, "unbind\n");
2693}
2694
2695/* wrap this driver around the specified device, but
2696 * don't respond over USB until a gadget driver binds to us.
2697 */
2698
2699static int net2280_probe (struct pci_dev *pdev, const struct pci_device_id *id)
2700{
2701        struct net2280          *dev;
2702        unsigned long           resource, len;
2703        void                    __iomem *base = NULL;
2704        int                     retval, i;
2705
2706        /* alloc, and start init */
2707        dev = kzalloc (sizeof *dev, GFP_KERNEL);
2708        if (dev == NULL){
2709                retval = -ENOMEM;
2710                goto done;
2711        }
2712
2713        pci_set_drvdata (pdev, dev);
2714        spin_lock_init (&dev->lock);
2715        dev->pdev = pdev;
2716        dev->gadget.ops = &net2280_ops;
2717        dev->gadget.max_speed = USB_SPEED_HIGH;
2718
2719        /* the "gadget" abstracts/virtualizes the controller */
2720        dev_set_name(&dev->gadget.dev, "gadget");
2721        dev->gadget.dev.parent = &pdev->dev;
2722        dev->gadget.dev.dma_mask = pdev->dev.dma_mask;
2723        dev->gadget.dev.release = gadget_release;
2724        dev->gadget.name = driver_name;
2725
2726        /* now all the pci goodies ... */
2727        if (pci_enable_device (pdev) < 0) {
2728                retval = -ENODEV;
2729                goto done;
2730        }
2731        dev->enabled = 1;
2732
2733        /* BAR 0 holds all the registers
2734         * BAR 1 is 8051 memory; unused here (note erratum 0103)
2735         * BAR 2 is fifo memory; unused here
2736         */
2737        resource = pci_resource_start (pdev, 0);
2738        len = pci_resource_len (pdev, 0);
2739        if (!request_mem_region (resource, len, driver_name)) {
2740                DEBUG (dev, "controller already in use\n");
2741                retval = -EBUSY;
2742                goto done;
2743        }
2744        dev->region = 1;
2745
2746        /* FIXME provide firmware download interface to put
2747         * 8051 code into the chip, e.g. to turn on PCI PM.
2748         */
2749
2750        base = ioremap_nocache (resource, len);
2751        if (base == NULL) {
2752                DEBUG (dev, "can't map memory\n");
2753                retval = -EFAULT;
2754                goto done;
2755        }
2756        dev->regs = (struct net2280_regs __iomem *) base;
2757        dev->usb = (struct net2280_usb_regs __iomem *) (base + 0x0080);
2758        dev->pci = (struct net2280_pci_regs __iomem *) (base + 0x0100);
2759        dev->dma = (struct net2280_dma_regs __iomem *) (base + 0x0180);
2760        dev->dep = (struct net2280_dep_regs __iomem *) (base + 0x0200);
2761        dev->epregs = (struct net2280_ep_regs __iomem *) (base + 0x0300);
2762
2763        /* put into initial config, link up all endpoints */
2764        writel (0, &dev->usb->usbctl);
2765        usb_reset (dev);
2766        usb_reinit (dev);
2767
2768        /* irq setup after old hardware is cleaned up */
2769        if (!pdev->irq) {
2770                ERROR (dev, "No IRQ.  Check PCI setup!\n");
2771                retval = -ENODEV;
2772                goto done;
2773        }
2774
2775        if (request_irq (pdev->irq, net2280_irq, IRQF_SHARED, driver_name, dev)
2776                        != 0) {
2777                ERROR (dev, "request interrupt %d failed\n", pdev->irq);
2778                retval = -EBUSY;
2779                goto done;
2780        }
2781        dev->got_irq = 1;
2782
2783        /* DMA setup */
2784        /* NOTE:  we know only the 32 LSBs of dma addresses may be nonzero */
2785        dev->requests = pci_pool_create ("requests", pdev,
2786                sizeof (struct net2280_dma),
2787                0 /* no alignment requirements */,
2788                0 /* or page-crossing issues */);
2789        if (!dev->requests) {
2790                DEBUG (dev, "can't get request pool\n");
2791                retval = -ENOMEM;
2792                goto done;
2793        }
2794        for (i = 1; i < 5; i++) {
2795                struct net2280_dma      *td;
2796
2797                td = pci_pool_alloc (dev->requests, GFP_KERNEL,
2798                                &dev->ep [i].td_dma);
2799                if (!td) {
2800                        DEBUG (dev, "can't get dummy %d\n", i);
2801                        retval = -ENOMEM;
2802                        goto done;
2803                }
2804                td->dmacount = 0;       /* not VALID */
2805                td->dmaaddr = cpu_to_le32 (DMA_ADDR_INVALID);
2806                td->dmadesc = td->dmaaddr;
2807                dev->ep [i].dummy = td;
2808        }
2809
2810        /* enable lower-overhead pci memory bursts during DMA */
2811        writel ( (1 << DMA_MEMORY_WRITE_AND_INVALIDATE_ENABLE)
2812                        // 256 write retries may not be enough...
2813                        // | (1 << PCI_RETRY_ABORT_ENABLE)
2814                        | (1 << DMA_READ_MULTIPLE_ENABLE)
2815                        | (1 << DMA_READ_LINE_ENABLE)
2816                        , &dev->pci->pcimstctl);
2817        /* erratum 0115 shouldn't appear: Linux inits PCI_LATENCY_TIMER */
2818        pci_set_master (pdev);
2819        pci_try_set_mwi (pdev);
2820
2821        /* ... also flushes any posted pci writes */
2822        dev->chiprev = get_idx_reg (dev->regs, REG_CHIPREV) & 0xffff;
2823
2824        /* done */
2825        INFO (dev, "%s\n", driver_desc);
2826        INFO (dev, "irq %d, pci mem %p, chip rev %04x\n",
2827                        pdev->irq, base, dev->chiprev);
2828        INFO (dev, "version: " DRIVER_VERSION "; dma %s\n",
2829                        use_dma
2830                                ? (use_dma_chaining ? "chaining" : "enabled")
2831                                : "disabled");
2832        retval = device_register (&dev->gadget.dev);
2833        if (retval) goto done;
2834        retval = device_create_file (&pdev->dev, &dev_attr_registers);
2835        if (retval) goto done;
2836
2837        retval = usb_add_gadget_udc(&pdev->dev, &dev->gadget);
2838        if (retval)
2839                goto done;
2840        return 0;
2841
2842done:
2843        if (dev)
2844                net2280_remove (pdev);
2845        return retval;
2846}
2847
2848/* make sure the board is quiescent; otherwise it will continue
2849 * generating IRQs across the upcoming reboot.
2850 */
2851
2852static void net2280_shutdown (struct pci_dev *pdev)
2853{
2854        struct net2280          *dev = pci_get_drvdata (pdev);
2855
2856        /* disable IRQs */
2857        writel (0, &dev->regs->pciirqenb0);
2858        writel (0, &dev->regs->pciirqenb1);
2859
2860        /* disable the pullup so the host will think we're gone */
2861        writel (0, &dev->usb->usbctl);
2862
2863        /* Disable full-speed test mode */
2864        writel(0, &dev->usb->xcvrdiag);
2865}
2866
2867
2868/*-------------------------------------------------------------------------*/
2869
2870static const struct pci_device_id pci_ids [] = { {
2871        .class =        ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
2872        .class_mask =   ~0,
2873        .vendor =       0x17cc,
2874        .device =       0x2280,
2875        .subvendor =    PCI_ANY_ID,
2876        .subdevice =    PCI_ANY_ID,
2877}, {
2878        .class =        ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
2879        .class_mask =   ~0,
2880        .vendor =       0x17cc,
2881        .device =       0x2282,
2882        .subvendor =    PCI_ANY_ID,
2883        .subdevice =    PCI_ANY_ID,
2884
2885}, { /* end: all zeroes */ }
2886};
2887MODULE_DEVICE_TABLE (pci, pci_ids);
2888
2889/* pci driver glue; this is a "new style" PCI driver module */
2890static struct pci_driver net2280_pci_driver = {
2891        .name =         (char *) driver_name,
2892        .id_table =     pci_ids,
2893
2894        .probe =        net2280_probe,
2895        .remove =       net2280_remove,
2896        .shutdown =     net2280_shutdown,
2897
2898        /* FIXME add power management support */
2899};
2900
2901MODULE_DESCRIPTION (DRIVER_DESC);
2902MODULE_AUTHOR ("David Brownell");
2903MODULE_LICENSE ("GPL");
2904
2905static int __init init (void)
2906{
2907        if (!use_dma)
2908                use_dma_chaining = 0;
2909        return pci_register_driver (&net2280_pci_driver);
2910}
2911module_init (init);
2912
2913static void __exit cleanup (void)
2914{
2915        pci_unregister_driver (&net2280_pci_driver);
2916}
2917module_exit (cleanup);
2918
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.