linux/drivers/usb/gadget/goku_udc.c
<<
>>
Prefs
   1/*
   2 * Toshiba TC86C001 ("Goku-S") USB Device Controller driver
   3 *
   4 * Copyright (C) 2000-2002 Lineo
   5 *      by Stuart Lynne, Tom Rushworth, and Bruce Balden
   6 * Copyright (C) 2002 Toshiba Corporation
   7 * Copyright (C) 2003 MontaVista Software (source@mvista.com)
   8 *
   9 * This file is licensed under the terms of the GNU General Public
  10 * License version 2.  This program is licensed "as is" without any
  11 * warranty of any kind, whether express or implied.
  12 */
  13
  14/*
  15 * This device has ep0 and three semi-configurable bulk/interrupt endpoints.
  16 *
  17 *  - Endpoint numbering is fixed: ep{1,2,3}-bulk
  18 *  - Gadget drivers can choose ep maxpacket (8/16/32/64)
  19 *  - Gadget drivers can choose direction (IN, OUT)
  20 *  - DMA works with ep1 (OUT transfers) and ep2 (IN transfers).
  21 */
  22
  23// #define      VERBOSE         /* extra debug messages (success too) */
  24// #define      USB_TRACE       /* packet-level success messages */
  25
  26#include <linux/kernel.h>
  27#include <linux/module.h>
  28#include <linux/pci.h>
  29#include <linux/delay.h>
  30#include <linux/ioport.h>
  31#include <linux/slab.h>
  32#include <linux/errno.h>
  33#include <linux/init.h>
  34#include <linux/timer.h>
  35#include <linux/list.h>
  36#include <linux/interrupt.h>
  37#include <linux/proc_fs.h>
  38#include <linux/device.h>
  39#include <linux/usb/ch9.h>
  40#include <linux/usb/gadget.h>
  41#include <linux/prefetch.h>
  42
  43#include <asm/byteorder.h>
  44#include <asm/io.h>
  45#include <asm/irq.h>
  46#include <asm/system.h>
  47#include <asm/unaligned.h>
  48
  49
  50#include "goku_udc.h"
  51
  52#define DRIVER_DESC             "TC86C001 USB Device Controller"
  53#define DRIVER_VERSION          "30-Oct 2003"
  54
  55#define DMA_ADDR_INVALID        (~(dma_addr_t)0)
  56
  57static const char driver_name [] = "goku_udc";
  58static const char driver_desc [] = DRIVER_DESC;
  59
  60MODULE_AUTHOR("source@mvista.com");
  61MODULE_DESCRIPTION(DRIVER_DESC);
  62MODULE_LICENSE("GPL");
  63
  64
  65/*
  66 * IN dma behaves ok under testing, though the IN-dma abort paths don't
  67 * seem to behave quite as expected.  Used by default.
  68 *
  69 * OUT dma documents design problems handling the common "short packet"
  70 * transfer termination policy; it couldn't be enabled by default, even
  71 * if the OUT-dma abort problems had a resolution.
  72 */
  73static unsigned use_dma = 1;
  74
  75#if 0
  76//#include <linux/moduleparam.h>
  77/* "modprobe goku_udc use_dma=1" etc
  78 *      0 to disable dma
  79 *      1 to use IN dma only (normal operation)
  80 *      2 to use IN and OUT dma
  81 */
  82module_param(use_dma, uint, S_IRUGO);
  83#endif
  84
  85/*-------------------------------------------------------------------------*/
  86
  87static void nuke(struct goku_ep *, int status);
  88
  89static inline void
  90command(struct goku_udc_regs __iomem *regs, int command, unsigned epnum)
  91{
  92        writel(COMMAND_EP(epnum) | command, &regs->Command);
  93        udelay(300);
  94}
  95
  96static int
  97goku_ep_enable(struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
  98{
  99        struct goku_udc *dev;
 100        struct goku_ep  *ep;
 101        u32             mode;
 102        u16             max;
 103        unsigned long   flags;
 104
 105        ep = container_of(_ep, struct goku_ep, ep);
 106        if (!_ep || !desc || ep->desc
 107                        || desc->bDescriptorType != USB_DT_ENDPOINT)
 108                return -EINVAL;
 109        dev = ep->dev;
 110        if (ep == &dev->ep[0])
 111                return -EINVAL;
 112        if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
 113                return -ESHUTDOWN;
 114        if (ep->num != usb_endpoint_num(desc))
 115                return -EINVAL;
 116
 117        switch (usb_endpoint_type(desc)) {
 118        case USB_ENDPOINT_XFER_BULK:
 119        case USB_ENDPOINT_XFER_INT:
 120                break;
 121        default:
 122                return -EINVAL;
 123        }
 124
 125        if ((readl(ep->reg_status) & EPxSTATUS_EP_MASK)
 126                        != EPxSTATUS_EP_INVALID)
 127                return -EBUSY;
 128
 129        /* enabling the no-toggle interrupt mode would need an api hook */
 130        mode = 0;
 131        max = get_unaligned_le16(&desc->wMaxPacketSize);
 132        switch (max) {
 133        case 64:        mode++;
 134        case 32:        mode++;
 135        case 16:        mode++;
 136        case 8:         mode <<= 3;
 137                        break;
 138        default:
 139                return -EINVAL;
 140        }
 141        mode |= 2 << 1;         /* bulk, or intr-with-toggle */
 142
 143        /* ep1/ep2 dma direction is chosen early; it works in the other
 144         * direction, with pio.  be cautious with out-dma.
 145         */
 146        ep->is_in = usb_endpoint_dir_in(desc);
 147        if (ep->is_in) {
 148                mode |= 1;
 149                ep->dma = (use_dma != 0) && (ep->num == UDC_MSTRD_ENDPOINT);
 150        } else {
 151                ep->dma = (use_dma == 2) && (ep->num == UDC_MSTWR_ENDPOINT);
 152                if (ep->dma)
 153                        DBG(dev, "%s out-dma hides short packets\n",
 154                                ep->ep.name);
 155        }
 156
 157        spin_lock_irqsave(&ep->dev->lock, flags);
 158
 159        /* ep1 and ep2 can do double buffering and/or dma */
 160        if (ep->num < 3) {
 161                struct goku_udc_regs __iomem    *regs = ep->dev->regs;
 162                u32                             tmp;
 163
 164                /* double buffer except (for now) with pio in */
 165                tmp = ((ep->dma || !ep->is_in)
 166                                ? 0x10  /* double buffered */
 167                                : 0x11  /* single buffer */
 168                        ) << ep->num;
 169                tmp |= readl(&regs->EPxSingle);
 170                writel(tmp, &regs->EPxSingle);
 171
 172                tmp = (ep->dma ? 0x10/*dma*/ : 0x11/*pio*/) << ep->num;
 173                tmp |= readl(&regs->EPxBCS);
 174                writel(tmp, &regs->EPxBCS);
 175        }
 176        writel(mode, ep->reg_mode);
 177        command(ep->dev->regs, COMMAND_RESET, ep->num);
 178        ep->ep.maxpacket = max;
 179        ep->stopped = 0;
 180        ep->desc = desc;
 181        spin_unlock_irqrestore(&ep->dev->lock, flags);
 182
 183        DBG(dev, "enable %s %s %s maxpacket %u\n", ep->ep.name,
 184                ep->is_in ? "IN" : "OUT",
 185                ep->dma ? "dma" : "pio",
 186                max);
 187
 188        return 0;
 189}
 190
 191static void ep_reset(struct goku_udc_regs __iomem *regs, struct goku_ep *ep)
 192{
 193        struct goku_udc         *dev = ep->dev;
 194
 195        if (regs) {
 196                command(regs, COMMAND_INVALID, ep->num);
 197                if (ep->num) {
 198                        if (ep->num == UDC_MSTWR_ENDPOINT)
 199                                dev->int_enable &= ~(INT_MSTWREND
 200                                                        |INT_MSTWRTMOUT);
 201                        else if (ep->num == UDC_MSTRD_ENDPOINT)
 202                                dev->int_enable &= ~INT_MSTRDEND;
 203                        dev->int_enable &= ~INT_EPxDATASET (ep->num);
 204                } else
 205                        dev->int_enable &= ~INT_EP0;
 206                writel(dev->int_enable, &regs->int_enable);
 207                readl(&regs->int_enable);
 208                if (ep->num < 3) {
 209                        struct goku_udc_regs __iomem    *r = ep->dev->regs;
 210                        u32                             tmp;
 211
 212                        tmp = readl(&r->EPxSingle);
 213                        tmp &= ~(0x11 << ep->num);
 214                        writel(tmp, &r->EPxSingle);
 215
 216                        tmp = readl(&r->EPxBCS);
 217                        tmp &= ~(0x11 << ep->num);
 218                        writel(tmp, &r->EPxBCS);
 219                }
 220                /* reset dma in case we're still using it */
 221                if (ep->dma) {
 222                        u32     master;
 223
 224                        master = readl(&regs->dma_master) & MST_RW_BITS;
 225                        if (ep->num == UDC_MSTWR_ENDPOINT) {
 226                                master &= ~MST_W_BITS;
 227                                master |= MST_WR_RESET;
 228                        } else {
 229                                master &= ~MST_R_BITS;
 230                                master |= MST_RD_RESET;
 231                        }
 232                        writel(master, &regs->dma_master);
 233                }
 234        }
 235
 236        ep->ep.maxpacket = MAX_FIFO_SIZE;
 237        ep->desc = NULL;
 238        ep->stopped = 1;
 239        ep->irqs = 0;
 240        ep->dma = 0;
 241}
 242
 243static int goku_ep_disable(struct usb_ep *_ep)
 244{
 245        struct goku_ep  *ep;
 246        struct goku_udc *dev;
 247        unsigned long   flags;
 248
 249        ep = container_of(_ep, struct goku_ep, ep);
 250        if (!_ep || !ep->desc)
 251                return -ENODEV;
 252        dev = ep->dev;
 253        if (dev->ep0state == EP0_SUSPEND)
 254                return -EBUSY;
 255
 256        VDBG(dev, "disable %s\n", _ep->name);
 257
 258        spin_lock_irqsave(&dev->lock, flags);
 259        nuke(ep, -ESHUTDOWN);
 260        ep_reset(dev->regs, ep);
 261        spin_unlock_irqrestore(&dev->lock, flags);
 262
 263        return 0;
 264}
 265
 266/*-------------------------------------------------------------------------*/
 267
 268static struct usb_request *
 269goku_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
 270{
 271        struct goku_request     *req;
 272
 273        if (!_ep)
 274                return NULL;
 275        req = kzalloc(sizeof *req, gfp_flags);
 276        if (!req)
 277                return NULL;
 278
 279        req->req.dma = DMA_ADDR_INVALID;
 280        INIT_LIST_HEAD(&req->queue);
 281        return &req->req;
 282}
 283
 284static void
 285goku_free_request(struct usb_ep *_ep, struct usb_request *_req)
 286{
 287        struct goku_request     *req;
 288
 289        if (!_ep || !_req)
 290                return;
 291
 292        req = container_of(_req, struct goku_request, req);
 293        WARN_ON(!list_empty(&req->queue));
 294        kfree(req);
 295}
 296
 297/*-------------------------------------------------------------------------*/
 298
 299static void
 300done(struct goku_ep *ep, struct goku_request *req, int status)
 301{
 302        struct goku_udc         *dev;
 303        unsigned                stopped = ep->stopped;
 304
 305        list_del_init(&req->queue);
 306
 307        if (likely(req->req.status == -EINPROGRESS))
 308                req->req.status = status;
 309        else
 310                status = req->req.status;
 311
 312        dev = ep->dev;
 313        if (req->mapped) {
 314                pci_unmap_single(dev->pdev, req->req.dma, req->req.length,
 315                        ep->is_in ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
 316                req->req.dma = DMA_ADDR_INVALID;
 317                req->mapped = 0;
 318        }
 319
 320#ifndef USB_TRACE
 321        if (status && status != -ESHUTDOWN)
 322#endif
 323                VDBG(dev, "complete %s req %p stat %d len %u/%u\n",
 324                        ep->ep.name, &req->req, status,
 325                        req->req.actual, req->req.length);
 326
 327        /* don't modify queue heads during completion callback */
 328        ep->stopped = 1;
 329        spin_unlock(&dev->lock);
 330        req->req.complete(&ep->ep, &req->req);
 331        spin_lock(&dev->lock);
 332        ep->stopped = stopped;
 333}
 334
 335/*-------------------------------------------------------------------------*/
 336
 337static inline int
 338write_packet(u32 __iomem *fifo, u8 *buf, struct goku_request *req, unsigned max)
 339{
 340        unsigned        length, count;
 341
 342        length = min(req->req.length - req->req.actual, max);
 343        req->req.actual += length;
 344
 345        count = length;
 346        while (likely(count--))
 347                writel(*buf++, fifo);
 348        return length;
 349}
 350
 351// return:  0 = still running, 1 = completed, negative = errno
 352static int write_fifo(struct goku_ep *ep, struct goku_request *req)
 353{
 354        struct goku_udc *dev = ep->dev;
 355        u32             tmp;
 356        u8              *buf;
 357        unsigned        count;
 358        int             is_last;
 359
 360        tmp = readl(&dev->regs->DataSet);
 361        buf = req->req.buf + req->req.actual;
 362        prefetch(buf);
 363
 364        dev = ep->dev;
 365        if (unlikely(ep->num == 0 && dev->ep0state != EP0_IN))
 366                return -EL2HLT;
 367
 368        /* NOTE:  just single-buffered PIO-IN for now.  */
 369        if (unlikely((tmp & DATASET_A(ep->num)) != 0))
 370                return 0;
 371
 372        /* clear our "packet available" irq */
 373        if (ep->num != 0)
 374                writel(~INT_EPxDATASET(ep->num), &dev->regs->int_status);
 375
 376        count = write_packet(ep->reg_fifo, buf, req, ep->ep.maxpacket);
 377
 378        /* last packet often short (sometimes a zlp, especially on ep0) */
 379        if (unlikely(count != ep->ep.maxpacket)) {
 380                writel(~(1<<ep->num), &dev->regs->EOP);
 381                if (ep->num == 0) {
 382                        dev->ep[0].stopped = 1;
 383                        dev->ep0state = EP0_STATUS;
 384                }
 385                is_last = 1;
 386        } else {
 387                if (likely(req->req.length != req->req.actual)
 388                                || req->req.zero)
 389                        is_last = 0;
 390                else
 391                        is_last = 1;
 392        }
 393#if 0           /* printk seemed to trash is_last...*/
 394//#ifdef USB_TRACE
 395        VDBG(dev, "wrote %s %u bytes%s IN %u left %p\n",
 396                ep->ep.name, count, is_last ? "/last" : "",
 397                req->req.length - req->req.actual, req);
 398#endif
 399
 400        /* requests complete when all IN data is in the FIFO,
 401         * or sometimes later, if a zlp was needed.
 402         */
 403        if (is_last) {
 404                done(ep, req, 0);
 405                return 1;
 406        }
 407
 408        return 0;
 409}
 410
 411static int read_fifo(struct goku_ep *ep, struct goku_request *req)
 412{
 413        struct goku_udc_regs __iomem    *regs;
 414        u32                             size, set;
 415        u8                              *buf;
 416        unsigned                        bufferspace, is_short, dbuff;
 417
 418        regs = ep->dev->regs;
 419top:
 420        buf = req->req.buf + req->req.actual;
 421        prefetchw(buf);
 422
 423        if (unlikely(ep->num == 0 && ep->dev->ep0state != EP0_OUT))
 424                return -EL2HLT;
 425
 426        dbuff = (ep->num == 1 || ep->num == 2);
 427        do {
 428                /* ack dataset irq matching the status we'll handle */
 429                if (ep->num != 0)
 430                        writel(~INT_EPxDATASET(ep->num), &regs->int_status);
 431
 432                set = readl(&regs->DataSet) & DATASET_AB(ep->num);
 433                size = readl(&regs->EPxSizeLA[ep->num]);
 434                bufferspace = req->req.length - req->req.actual;
 435
 436                /* usually do nothing without an OUT packet */
 437                if (likely(ep->num != 0 || bufferspace != 0)) {
 438                        if (unlikely(set == 0))
 439                                break;
 440                        /* use ep1/ep2 double-buffering for OUT */
 441                        if (!(size & PACKET_ACTIVE))
 442                                size = readl(&regs->EPxSizeLB[ep->num]);
 443                        if (!(size & PACKET_ACTIVE))    /* "can't happen" */
 444                                break;
 445                        size &= DATASIZE;       /* EPxSizeH == 0 */
 446
 447                /* ep0out no-out-data case for set_config, etc */
 448                } else
 449                        size = 0;
 450
 451                /* read all bytes from this packet */
 452                req->req.actual += size;
 453                is_short = (size < ep->ep.maxpacket);
 454#ifdef USB_TRACE
 455                VDBG(ep->dev, "read %s %u bytes%s OUT req %p %u/%u\n",
 456                        ep->ep.name, size, is_short ? "/S" : "",
 457                        req, req->req.actual, req->req.length);
 458#endif
 459                while (likely(size-- != 0)) {
 460                        u8      byte = (u8) readl(ep->reg_fifo);
 461
 462                        if (unlikely(bufferspace == 0)) {
 463                                /* this happens when the driver's buffer
 464                                 * is smaller than what the host sent.
 465                                 * discard the extra data in this packet.
 466                                 */
 467                                if (req->req.status != -EOVERFLOW)
 468                                        DBG(ep->dev, "%s overflow %u\n",
 469                                                ep->ep.name, size);
 470                                req->req.status = -EOVERFLOW;
 471                        } else {
 472                                *buf++ = byte;
 473                                bufferspace--;
 474                        }
 475                }
 476
 477                /* completion */
 478                if (unlikely(is_short || req->req.actual == req->req.length)) {
 479                        if (unlikely(ep->num == 0)) {
 480                                /* non-control endpoints now usable? */
 481                                if (ep->dev->req_config)
 482                                        writel(ep->dev->configured
 483                                                        ? USBSTATE_CONFIGURED
 484                                                        : 0,
 485                                                &regs->UsbState);
 486                                /* ep0out status stage */
 487                                writel(~(1<<0), &regs->EOP);
 488                                ep->stopped = 1;
 489                                ep->dev->ep0state = EP0_STATUS;
 490                        }
 491                        done(ep, req, 0);
 492
 493                        /* empty the second buffer asap */
 494                        if (dbuff && !list_empty(&ep->queue)) {
 495                                req = list_entry(ep->queue.next,
 496                                                struct goku_request, queue);
 497                                goto top;
 498                        }
 499                        return 1;
 500                }
 501        } while (dbuff);
 502        return 0;
 503}
 504
 505static inline void
 506pio_irq_enable(struct goku_udc *dev,
 507                struct goku_udc_regs __iomem *regs, int epnum)
 508{
 509        dev->int_enable |= INT_EPxDATASET (epnum);
 510        writel(dev->int_enable, &regs->int_enable);
 511        /* write may still be posted */
 512}
 513
 514static inline void
 515pio_irq_disable(struct goku_udc *dev,
 516                struct goku_udc_regs __iomem *regs, int epnum)
 517{
 518        dev->int_enable &= ~INT_EPxDATASET (epnum);
 519        writel(dev->int_enable, &regs->int_enable);
 520        /* write may still be posted */
 521}
 522
 523static inline void
 524pio_advance(struct goku_ep *ep)
 525{
 526        struct goku_request     *req;
 527
 528        if (unlikely(list_empty (&ep->queue)))
 529                return;
 530        req = list_entry(ep->queue.next, struct goku_request, queue);
 531        (ep->is_in ? write_fifo : read_fifo)(ep, req);
 532}
 533
 534
 535/*-------------------------------------------------------------------------*/
 536
 537// return:  0 = q running, 1 = q stopped, negative = errno
 538static int start_dma(struct goku_ep *ep, struct goku_request *req)
 539{
 540        struct goku_udc_regs __iomem    *regs = ep->dev->regs;
 541        u32                             master;
 542        u32                             start = req->req.dma;
 543        u32                             end = start + req->req.length - 1;
 544
 545        master = readl(&regs->dma_master) & MST_RW_BITS;
 546
 547        /* re-init the bits affecting IN dma; careful with zlps */
 548        if (likely(ep->is_in)) {
 549                if (unlikely(master & MST_RD_ENA)) {
 550                        DBG (ep->dev, "start, IN active dma %03x!!\n",
 551                                master);
 552//                      return -EL2HLT;
 553                }
 554                writel(end, &regs->in_dma_end);
 555                writel(start, &regs->in_dma_start);
 556
 557                master &= ~MST_R_BITS;
 558                if (unlikely(req->req.length == 0))
 559                        master = MST_RD_ENA | MST_RD_EOPB;
 560                else if ((req->req.length % ep->ep.maxpacket) != 0
 561                                        || req->req.zero)
 562                        master = MST_RD_ENA | MST_EOPB_ENA;
 563                else
 564                        master = MST_RD_ENA | MST_EOPB_DIS;
 565
 566                ep->dev->int_enable |= INT_MSTRDEND;
 567
 568        /* Goku DMA-OUT merges short packets, which plays poorly with
 569         * protocols where short packets mark the transfer boundaries.
 570         * The chip supports a nonstandard policy with INT_MSTWRTMOUT,
 571         * ending transfers after 3 SOFs; we don't turn it on.
 572         */
 573        } else {
 574                if (unlikely(master & MST_WR_ENA)) {
 575                        DBG (ep->dev, "start, OUT active dma %03x!!\n",
 576                                master);
 577//                      return -EL2HLT;
 578                }
 579                writel(end, &regs->out_dma_end);
 580                writel(start, &regs->out_dma_start);
 581
 582                master &= ~MST_W_BITS;
 583                master |= MST_WR_ENA | MST_TIMEOUT_DIS;
 584
 585                ep->dev->int_enable |= INT_MSTWREND|INT_MSTWRTMOUT;
 586        }
 587
 588        writel(master, &regs->dma_master);
 589        writel(ep->dev->int_enable, &regs->int_enable);
 590        return 0;
 591}
 592
 593static void dma_advance(struct goku_udc *dev, struct goku_ep *ep)
 594{
 595        struct goku_request             *req;
 596        struct goku_udc_regs __iomem    *regs = ep->dev->regs;
 597        u32                             master;
 598
 599        master = readl(&regs->dma_master);
 600
 601        if (unlikely(list_empty(&ep->queue))) {
 602stop:
 603                if (ep->is_in)
 604                        dev->int_enable &= ~INT_MSTRDEND;
 605                else
 606                        dev->int_enable &= ~(INT_MSTWREND|INT_MSTWRTMOUT);
 607                writel(dev->int_enable, &regs->int_enable);
 608                return;
 609        }
 610        req = list_entry(ep->queue.next, struct goku_request, queue);
 611
 612        /* normal hw dma completion (not abort) */
 613        if (likely(ep->is_in)) {
 614                if (unlikely(master & MST_RD_ENA))
 615                        return;
 616                req->req.actual = readl(&regs->in_dma_current);
 617        } else {
 618                if (unlikely(master & MST_WR_ENA))
 619                        return;
 620
 621                /* hardware merges short packets, and also hides packet
 622                 * overruns.  a partial packet MAY be in the fifo here.
 623                 */
 624                req->req.actual = readl(&regs->out_dma_current);
 625        }
 626        req->req.actual -= req->req.dma;
 627        req->req.actual++;
 628
 629#ifdef USB_TRACE
 630        VDBG(dev, "done %s %s dma, %u/%u bytes, req %p\n",
 631                ep->ep.name, ep->is_in ? "IN" : "OUT",
 632                req->req.actual, req->req.length, req);
 633#endif
 634        done(ep, req, 0);
 635        if (list_empty(&ep->queue))
 636                goto stop;
 637        req = list_entry(ep->queue.next, struct goku_request, queue);
 638        (void) start_dma(ep, req);
 639}
 640
 641static void abort_dma(struct goku_ep *ep, int status)
 642{
 643        struct goku_udc_regs __iomem    *regs = ep->dev->regs;
 644        struct goku_request             *req;
 645        u32                             curr, master;
 646
 647        /* NAK future host requests, hoping the implicit delay lets the
 648         * dma engine finish reading (or writing) its latest packet and
 649         * empty the dma buffer (up to 16 bytes).
 650         *
 651         * This avoids needing to clean up a partial packet in the fifo;
 652         * we can't do that for IN without side effects to HALT and TOGGLE.
 653         */
 654        command(regs, COMMAND_FIFO_DISABLE, ep->num);
 655        req = list_entry(ep->queue.next, struct goku_request, queue);
 656        master = readl(&regs->dma_master) & MST_RW_BITS;
 657
 658        /* FIXME using these resets isn't usably documented. this may
 659         * not work unless it's followed by disabling the endpoint.
 660         *
 661         * FIXME the OUT reset path doesn't even behave consistently.
 662         */
 663        if (ep->is_in) {
 664                if (unlikely((readl(&regs->dma_master) & MST_RD_ENA) == 0))
 665                        goto finished;
 666                curr = readl(&regs->in_dma_current);
 667
 668                writel(curr, &regs->in_dma_end);
 669                writel(curr, &regs->in_dma_start);
 670
 671                master &= ~MST_R_BITS;
 672                master |= MST_RD_RESET;
 673                writel(master, &regs->dma_master);
 674
 675                if (readl(&regs->dma_master) & MST_RD_ENA)
 676                        DBG(ep->dev, "IN dma active after reset!\n");
 677
 678        } else {
 679                if (unlikely((readl(&regs->dma_master) & MST_WR_ENA) == 0))
 680                        goto finished;
 681                curr = readl(&regs->out_dma_current);
 682
 683                writel(curr, &regs->out_dma_end);
 684                writel(curr, &regs->out_dma_start);
 685
 686                master &= ~MST_W_BITS;
 687                master |= MST_WR_RESET;
 688                writel(master, &regs->dma_master);
 689
 690                if (readl(&regs->dma_master) & MST_WR_ENA)
 691                        DBG(ep->dev, "OUT dma active after reset!\n");
 692        }
 693        req->req.actual = (curr - req->req.dma) + 1;
 694        req->req.status = status;
 695
 696        VDBG(ep->dev, "%s %s %s %d/%d\n", __func__, ep->ep.name,
 697                ep->is_in ? "IN" : "OUT",
 698                req->req.actual, req->req.length);
 699
 700        command(regs, COMMAND_FIFO_ENABLE, ep->num);
 701
 702        return;
 703
 704finished:
 705        /* dma already completed; no abort needed */
 706        command(regs, COMMAND_FIFO_ENABLE, ep->num);
 707        req->req.actual = req->req.length;
 708        req->req.status = 0;
 709}
 710
 711/*-------------------------------------------------------------------------*/
 712
 713static int
 714goku_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
 715{
 716        struct goku_request     *req;
 717        struct goku_ep          *ep;
 718        struct goku_udc         *dev;
 719        unsigned long           flags;
 720        int                     status;
 721
 722        /* always require a cpu-view buffer so pio works */
 723        req = container_of(_req, struct goku_request, req);
 724        if (unlikely(!_req || !_req->complete
 725                        || !_req->buf || !list_empty(&req->queue)))
 726                return -EINVAL;
 727        ep = container_of(_ep, struct goku_ep, ep);
 728        if (unlikely(!_ep || (!ep->desc && ep->num != 0)))
 729                return -EINVAL;
 730        dev = ep->dev;
 731        if (unlikely(!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN))
 732                return -ESHUTDOWN;
 733
 734        /* can't touch registers when suspended */
 735        if (dev->ep0state == EP0_SUSPEND)
 736                return -EBUSY;
 737
 738        /* set up dma mapping in case the caller didn't */
 739        if (ep->dma && _req->dma == DMA_ADDR_INVALID) {
 740                _req->dma = pci_map_single(dev->pdev, _req->buf, _req->length,
 741                        ep->is_in ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
 742                req->mapped = 1;
 743        }
 744
 745#ifdef USB_TRACE
 746        VDBG(dev, "%s queue req %p, len %u buf %p\n",
 747                        _ep->name, _req, _req->length, _req->buf);
 748#endif
 749
 750        spin_lock_irqsave(&dev->lock, flags);
 751
 752        _req->status = -EINPROGRESS;
 753        _req->actual = 0;
 754
 755        /* for ep0 IN without premature status, zlp is required and
 756         * writing EOP starts the status stage (OUT).
 757         */
 758        if (unlikely(ep->num == 0 && ep->is_in))
 759                _req->zero = 1;
 760
 761        /* kickstart this i/o queue? */
 762        status = 0;
 763        if (list_empty(&ep->queue) && likely(!ep->stopped)) {
 764                /* dma:  done after dma completion IRQ (or error)
 765                 * pio:  done after last fifo operation
 766                 */
 767                if (ep->dma)
 768                        status = start_dma(ep, req);
 769                else
 770                        status = (ep->is_in ? write_fifo : read_fifo)(ep, req);
 771
 772                if (unlikely(status != 0)) {
 773                        if (status > 0)
 774                                status = 0;
 775                        req = NULL;
 776                }
 777
 778        } /* else pio or dma irq handler advances the queue. */
 779
 780        if (likely(req != 0))
 781                list_add_tail(&req->queue, &ep->queue);
 782
 783        if (likely(!list_empty(&ep->queue))
 784                        && likely(ep->num != 0)
 785                        && !ep->dma
 786                        && !(dev->int_enable & INT_EPxDATASET (ep->num)))
 787                pio_irq_enable(dev, dev->regs, ep->num);
 788
 789        spin_unlock_irqrestore(&dev->lock, flags);
 790
 791        /* pci writes may still be posted */
 792        return status;
 793}
 794
 795/* dequeue ALL requests */
 796static void nuke(struct goku_ep *ep, int status)
 797{
 798        struct goku_request     *req;
 799
 800        ep->stopped = 1;
 801        if (list_empty(&ep->queue))
 802                return;
 803        if (ep->dma)
 804                abort_dma(ep, status);
 805        while (!list_empty(&ep->queue)) {
 806                req = list_entry(ep->queue.next, struct goku_request, queue);
 807                done(ep, req, status);
 808        }
 809}
 810
 811/* dequeue JUST ONE request */
 812static int goku_dequeue(struct usb_ep *_ep, struct usb_request *_req)
 813{
 814        struct goku_request     *req;
 815        struct goku_ep          *ep;
 816        struct goku_udc         *dev;
 817        unsigned long           flags;
 818
 819        ep = container_of(_ep, struct goku_ep, ep);
 820        if (!_ep || !_req || (!ep->desc && ep->num != 0))
 821                return -EINVAL;
 822        dev = ep->dev;
 823        if (!dev->driver)
 824                return -ESHUTDOWN;
 825
 826        /* we can't touch (dma) registers when suspended */
 827        if (dev->ep0state == EP0_SUSPEND)
 828                return -EBUSY;
 829
 830        VDBG(dev, "%s %s %s %s %p\n", __func__, _ep->name,
 831                ep->is_in ? "IN" : "OUT",
 832                ep->dma ? "dma" : "pio",
 833                _req);
 834
 835        spin_lock_irqsave(&dev->lock, flags);
 836
 837        /* make sure it's actually queued on this endpoint */
 838        list_for_each_entry (req, &ep->queue, queue) {
 839                if (&req->req == _req)
 840                        break;
 841        }
 842        if (&req->req != _req) {
 843                spin_unlock_irqrestore (&dev->lock, flags);
 844                return -EINVAL;
 845        }
 846
 847        if (ep->dma && ep->queue.next == &req->queue && !ep->stopped) {
 848                abort_dma(ep, -ECONNRESET);
 849                done(ep, req, -ECONNRESET);
 850                dma_advance(dev, ep);
 851        } else if (!list_empty(&req->queue))
 852                done(ep, req, -ECONNRESET);
 853        else
 854                req = NULL;
 855        spin_unlock_irqrestore(&dev->lock, flags);
 856
 857        return req ? 0 : -EOPNOTSUPP;
 858}
 859
 860/*-------------------------------------------------------------------------*/
 861
 862static void goku_clear_halt(struct goku_ep *ep)
 863{
 864        // assert (ep->num !=0)
 865        VDBG(ep->dev, "%s clear halt\n", ep->ep.name);
 866        command(ep->dev->regs, COMMAND_SETDATA0, ep->num);
 867        command(ep->dev->regs, COMMAND_STALL_CLEAR, ep->num);
 868        if (ep->stopped) {
 869                ep->stopped = 0;
 870                if (ep->dma) {
 871                        struct goku_request     *req;
 872
 873                        if (list_empty(&ep->queue))
 874                                return;
 875                        req = list_entry(ep->queue.next, struct goku_request,
 876                                                queue);
 877                        (void) start_dma(ep, req);
 878                } else
 879                        pio_advance(ep);
 880        }
 881}
 882
 883static int goku_set_halt(struct usb_ep *_ep, int value)
 884{
 885        struct goku_ep  *ep;
 886        unsigned long   flags;
 887        int             retval = 0;
 888
 889        if (!_ep)
 890                return -ENODEV;
 891        ep = container_of (_ep, struct goku_ep, ep);
 892
 893        if (ep->num == 0) {
 894                if (value) {
 895                        ep->dev->ep0state = EP0_STALL;
 896                        ep->dev->ep[0].stopped = 1;
 897                } else
 898                        return -EINVAL;
 899
 900        /* don't change EPxSTATUS_EP_INVALID to READY */
 901        } else if (!ep->desc) {
 902                DBG(ep->dev, "%s %s inactive?\n", __func__, ep->ep.name);
 903                return -EINVAL;
 904        }
 905
 906        spin_lock_irqsave(&ep->dev->lock, flags);
 907        if (!list_empty(&ep->queue))
 908                retval = -EAGAIN;
 909        else if (ep->is_in && value
 910                        /* data in (either) packet buffer? */
 911                        && (readl(&ep->dev->regs->DataSet)
 912                                        & DATASET_AB(ep->num)))
 913                retval = -EAGAIN;
 914        else if (!value)
 915                goku_clear_halt(ep);
 916        else {
 917                ep->stopped = 1;
 918                VDBG(ep->dev, "%s set halt\n", ep->ep.name);
 919                command(ep->dev->regs, COMMAND_STALL, ep->num);
 920                readl(ep->reg_status);
 921        }
 922        spin_unlock_irqrestore(&ep->dev->lock, flags);
 923        return retval;
 924}
 925
 926static int goku_fifo_status(struct usb_ep *_ep)
 927{
 928        struct goku_ep                  *ep;
 929        struct goku_udc_regs __iomem    *regs;
 930        u32                             size;
 931
 932        if (!_ep)
 933                return -ENODEV;
 934        ep = container_of(_ep, struct goku_ep, ep);
 935
 936        /* size is only reported sanely for OUT */
 937        if (ep->is_in)
 938                return -EOPNOTSUPP;
 939
 940        /* ignores 16-byte dma buffer; SizeH == 0 */
 941        regs = ep->dev->regs;
 942        size = readl(&regs->EPxSizeLA[ep->num]) & DATASIZE;
 943        size += readl(&regs->EPxSizeLB[ep->num]) & DATASIZE;
 944        VDBG(ep->dev, "%s %s %u\n", __func__, ep->ep.name, size);
 945        return size;
 946}
 947
 948static void goku_fifo_flush(struct usb_ep *_ep)
 949{
 950        struct goku_ep                  *ep;
 951        struct goku_udc_regs __iomem    *regs;
 952        u32                             size;
 953
 954        if (!_ep)
 955                return;
 956        ep = container_of(_ep, struct goku_ep, ep);
 957        VDBG(ep->dev, "%s %s\n", __func__, ep->ep.name);
 958
 959        /* don't change EPxSTATUS_EP_INVALID to READY */
 960        if (!ep->desc && ep->num != 0) {
 961                DBG(ep->dev, "%s %s inactive?\n", __func__, ep->ep.name);
 962                return;
 963        }
 964
 965        regs = ep->dev->regs;
 966        size = readl(&regs->EPxSizeLA[ep->num]);
 967        size &= DATASIZE;
 968
 969        /* Non-desirable behavior:  FIFO_CLEAR also clears the
 970         * endpoint halt feature.  For OUT, we _could_ just read
 971         * the bytes out (PIO, if !ep->dma); for in, no choice.
 972         */
 973        if (size)
 974                command(regs, COMMAND_FIFO_CLEAR, ep->num);
 975}
 976
 977static struct usb_ep_ops goku_ep_ops = {
 978        .enable         = goku_ep_enable,
 979        .disable        = goku_ep_disable,
 980
 981        .alloc_request  = goku_alloc_request,
 982        .free_request   = goku_free_request,
 983
 984        .queue          = goku_queue,
 985        .dequeue        = goku_dequeue,
 986
 987        .set_halt       = goku_set_halt,
 988        .fifo_status    = goku_fifo_status,
 989        .fifo_flush     = goku_fifo_flush,
 990};
 991
 992/*-------------------------------------------------------------------------*/
 993
 994static int goku_get_frame(struct usb_gadget *_gadget)
 995{
 996        return -EOPNOTSUPP;
 997}
 998
 999static const struct usb_gadget_ops goku_ops = {
1000        .get_frame      = goku_get_frame,
1001        // no remote wakeup
1002        // not selfpowered
1003};
1004
1005/*-------------------------------------------------------------------------*/
1006
1007static inline char *dmastr(void)
1008{
1009        if (use_dma == 0)
1010                return "(dma disabled)";
1011        else if (use_dma == 2)
1012                return "(dma IN and OUT)";
1013        else
1014                return "(dma IN)";
1015}
1016
1017#ifdef CONFIG_USB_GADGET_DEBUG_FILES
1018
1019static const char proc_node_name [] = "driver/udc";
1020
1021#define FOURBITS "%s%s%s%s"
1022#define EIGHTBITS FOURBITS FOURBITS
1023
1024static void
1025dump_intmask(const char *label, u32 mask, char **next, unsigned *size)
1026{
1027        int t;
1028
1029        /* int_status is the same format ... */
1030        t = scnprintf(*next, *size,
1031                "%s %05X =" FOURBITS EIGHTBITS EIGHTBITS "\n",
1032                label, mask,
1033                (mask & INT_PWRDETECT) ? " power" : "",
1034                (mask & INT_SYSERROR) ? " sys" : "",
1035                (mask & INT_MSTRDEND) ? " in-dma" : "",
1036                (mask & INT_MSTWRTMOUT) ? " wrtmo" : "",
1037
1038                (mask & INT_MSTWREND) ? " out-dma" : "",
1039                (mask & INT_MSTWRSET) ? " wrset" : "",
1040                (mask & INT_ERR) ? " err" : "",
1041                (mask & INT_SOF) ? " sof" : "",
1042
1043                (mask & INT_EP3NAK) ? " ep3nak" : "",
1044                (mask & INT_EP2NAK) ? " ep2nak" : "",
1045                (mask & INT_EP1NAK) ? " ep1nak" : "",
1046                (mask & INT_EP3DATASET) ? " ep3" : "",
1047
1048                (mask & INT_EP2DATASET) ? " ep2" : "",
1049                (mask & INT_EP1DATASET) ? " ep1" : "",
1050                (mask & INT_STATUSNAK) ? " ep0snak" : "",
1051                (mask & INT_STATUS) ? " ep0status" : "",
1052
1053                (mask & INT_SETUP) ? " setup" : "",
1054                (mask & INT_ENDPOINT0) ? " ep0" : "",
1055                (mask & INT_USBRESET) ? " reset" : "",
1056                (mask & INT_SUSPEND) ? " suspend" : "");
1057        *size -= t;
1058        *next += t;
1059}
1060
1061
1062static int
1063udc_proc_read(char *buffer, char **start, off_t off, int count,
1064                int *eof, void *_dev)
1065{
1066        char                            *buf = buffer;
1067        struct goku_udc                 *dev = _dev;
1068        struct goku_udc_regs __iomem    *regs = dev->regs;
1069        char                            *next = buf;
1070        unsigned                        size = count;
1071        unsigned long                   flags;
1072        int                             i, t, is_usb_connected;
1073        u32                             tmp;
1074
1075        if (off != 0)
1076                return 0;
1077
1078        local_irq_save(flags);
1079
1080        /* basic device status */
1081        tmp = readl(&regs->power_detect);
1082        is_usb_connected = tmp & PW_DETECT;
1083        t = scnprintf(next, size,
1084                "%s - %s\n"
1085                "%s version: %s %s\n"
1086                "Gadget driver: %s\n"
1087                "Host %s, %s\n"
1088                "\n",
1089                pci_name(dev->pdev), driver_desc,
1090                driver_name, DRIVER_VERSION, dmastr(),
1091                dev->driver ? dev->driver->driver.name : "(none)",
1092                is_usb_connected
1093                        ? ((tmp & PW_PULLUP) ? "full speed" : "powered")
1094                        : "disconnected",
1095                ({char *state;
1096                switch(dev->ep0state){
1097                case EP0_DISCONNECT:    state = "ep0_disconnect"; break;
1098                case EP0_IDLE:          state = "ep0_idle"; break;
1099                case EP0_IN:            state = "ep0_in"; break;
1100                case EP0_OUT:           state = "ep0_out"; break;
1101                case EP0_STATUS:        state = "ep0_status"; break;
1102                case EP0_STALL:         state = "ep0_stall"; break;
1103                case EP0_SUSPEND:       state = "ep0_suspend"; break;
1104                default:                state = "ep0_?"; break;
1105                } state; })
1106                );
1107        size -= t;
1108        next += t;
1109
1110        dump_intmask("int_status", readl(&regs->int_status), &next, &size);
1111        dump_intmask("int_enable", readl(&regs->int_enable), &next, &size);
1112
1113        if (!is_usb_connected || !dev->driver || (tmp & PW_PULLUP) == 0)
1114                goto done;
1115
1116        /* registers for (active) device and ep0 */
1117        t = scnprintf(next, size, "\nirqs %lu\ndataset %02x "
1118                        "single.bcs %02x.%02x state %x addr %u\n",
1119                        dev->irqs, readl(&regs->DataSet),
1120                        readl(&regs->EPxSingle), readl(&regs->EPxBCS),
1121                        readl(&regs->UsbState),
1122                        readl(&regs->address));
1123        size -= t;
1124        next += t;
1125
1126        tmp = readl(&regs->dma_master);
1127        t = scnprintf(next, size,
1128                "dma %03X =" EIGHTBITS "%s %s\n", tmp,
1129                (tmp & MST_EOPB_DIS) ? " eopb-" : "",
1130                (tmp & MST_EOPB_ENA) ? " eopb+" : "",
1131                (tmp & MST_TIMEOUT_DIS) ? " tmo-" : "",
1132                (tmp & MST_TIMEOUT_ENA) ? " tmo+" : "",
1133
1134                (tmp & MST_RD_EOPB) ? " eopb" : "",
1135                (tmp & MST_RD_RESET) ? " in_reset" : "",
1136                (tmp & MST_WR_RESET) ? " out_reset" : "",
1137                (tmp & MST_RD_ENA) ? " IN" : "",
1138
1139                (tmp & MST_WR_ENA) ? " OUT" : "",
1140                (tmp & MST_CONNECTION)
1141                        ? "ep1in/ep2out"
1142                        : "ep1out/ep2in");
1143        size -= t;
1144        next += t;
1145
1146        /* dump endpoint queues */
1147        for (i = 0; i < 4; i++) {
1148                struct goku_ep          *ep = &dev->ep [i];
1149                struct goku_request     *req;
1150
1151                if (i && !ep->desc)
1152                        continue;
1153
1154                tmp = readl(ep->reg_status);
1155                t = scnprintf(next, size,
1156                        "%s %s max %u %s, irqs %lu, "
1157                        "status %02x (%s) " FOURBITS "\n",
1158                        ep->ep.name,
1159                        ep->is_in ? "in" : "out",
1160                        ep->ep.maxpacket,
1161                        ep->dma ? "dma" : "pio",
1162                        ep->irqs,
1163                        tmp, ({ char *s;
1164                        switch (tmp & EPxSTATUS_EP_MASK) {
1165                        case EPxSTATUS_EP_READY:
1166                                s = "ready"; break;
1167                        case EPxSTATUS_EP_DATAIN:
1168                                s = "packet"; break;
1169                        case EPxSTATUS_EP_FULL:
1170                                s = "full"; break;
1171                        case EPxSTATUS_EP_TX_ERR:       // host will retry
1172                                s = "tx_err"; break;
1173                        case EPxSTATUS_EP_RX_ERR:
1174                                s = "rx_err"; break;
1175                        case EPxSTATUS_EP_BUSY:         /* ep0 only */
1176                                s = "busy"; break;
1177                        case EPxSTATUS_EP_STALL:
1178                                s = "stall"; break;
1179                        case EPxSTATUS_EP_INVALID:      // these "can't happen"
1180                                s = "invalid"; break;
1181                        default:
1182                                s = "?"; break;
1183                        }; s; }),
1184                        (tmp & EPxSTATUS_TOGGLE) ? "data1" : "data0",
1185                        (tmp & EPxSTATUS_SUSPEND) ? " suspend" : "",
1186                        (tmp & EPxSTATUS_FIFO_DISABLE) ? " disable" : "",
1187                        (tmp & EPxSTATUS_STAGE_ERROR) ? " ep0stat" : ""
1188                        );
1189                if (t <= 0 || t > size)
1190                        goto done;
1191                size -= t;
1192                next += t;
1193
1194                if (list_empty(&ep->queue)) {
1195                        t = scnprintf(next, size, "\t(nothing queued)\n");
1196                        if (t <= 0 || t > size)
1197                                goto done;
1198                        size -= t;
1199                        next += t;
1200                        continue;
1201                }
1202                list_for_each_entry(req, &ep->queue, queue) {
1203                        if (ep->dma && req->queue.prev == &ep->queue) {
1204                                if (i == UDC_MSTRD_ENDPOINT)
1205                                        tmp = readl(&regs->in_dma_current);
1206                                else
1207                                        tmp = readl(&regs->out_dma_current);
1208                                tmp -= req->req.dma;
1209                                tmp++;
1210                        } else
1211                                tmp = req->req.actual;
1212
1213                        t = scnprintf(next, size,
1214                                "\treq %p len %u/%u buf %p\n",
1215                                &req->req, tmp, req->req.length,
1216                                req->req.buf);
1217                        if (t <= 0 || t > size)
1218                                goto done;
1219                        size -= t;
1220                        next += t;
1221                }
1222        }
1223
1224done:
1225        local_irq_restore(flags);
1226        *eof = 1;
1227        return count - size;
1228}
1229
1230#endif  /* CONFIG_USB_GADGET_DEBUG_FILES */
1231
1232/*-------------------------------------------------------------------------*/
1233
1234static void udc_reinit (struct goku_udc *dev)
1235{
1236        static char *names [] = { "ep0", "ep1-bulk", "ep2-bulk", "ep3-bulk" };
1237
1238        unsigned i;
1239
1240        INIT_LIST_HEAD (&dev->gadget.ep_list);
1241        dev->gadget.ep0 = &dev->ep [0].ep;
1242        dev->gadget.speed = USB_SPEED_UNKNOWN;
1243        dev->ep0state = EP0_DISCONNECT;
1244        dev->irqs = 0;
1245
1246        for (i = 0; i < 4; i++) {
1247                struct goku_ep  *ep = &dev->ep[i];
1248
1249                ep->num = i;
1250                ep->ep.name = names[i];
1251                ep->reg_fifo = &dev->regs->ep_fifo [i];
1252                ep->reg_status = &dev->regs->ep_status [i];
1253                ep->reg_mode = &dev->regs->ep_mode[i];
1254
1255                ep->ep.ops = &goku_ep_ops;
1256                list_add_tail (&ep->ep.ep_list, &dev->gadget.ep_list);
1257                ep->dev = dev;
1258                INIT_LIST_HEAD (&ep->queue);
1259
1260                ep_reset(NULL, ep);
1261        }
1262
1263        dev->ep[0].reg_mode = NULL;
1264        dev->ep[0].ep.maxpacket = MAX_EP0_SIZE;
1265        list_del_init (&dev->ep[0].ep.ep_list);
1266}
1267
1268static void udc_reset(struct goku_udc *dev)
1269{
1270        struct goku_udc_regs __iomem    *regs = dev->regs;
1271
1272        writel(0, &regs->power_detect);
1273        writel(0, &regs->int_enable);
1274        readl(&regs->int_enable);
1275        dev->int_enable = 0;
1276
1277        /* deassert reset, leave USB D+ at hi-Z (no pullup)
1278         * don't let INT_PWRDETECT sequence begin
1279         */
1280        udelay(250);
1281        writel(PW_RESETB, &regs->power_detect);
1282        readl(&regs->int_enable);
1283}
1284
1285static void ep0_start(struct goku_udc *dev)
1286{
1287        struct goku_udc_regs __iomem    *regs = dev->regs;
1288        unsigned                        i;
1289
1290        VDBG(dev, "%s\n", __func__);
1291
1292        udc_reset(dev);
1293        udc_reinit (dev);
1294        //writel(MST_EOPB_ENA | MST_TIMEOUT_ENA, &regs->dma_master);
1295
1296        /* hw handles set_address, set_feature, get_status; maybe more */
1297        writel(   G_REQMODE_SET_INTF | G_REQMODE_GET_INTF
1298                | G_REQMODE_SET_CONF | G_REQMODE_GET_CONF
1299                | G_REQMODE_GET_DESC
1300                | G_REQMODE_CLEAR_FEAT
1301                , &regs->reqmode);
1302
1303        for (i = 0; i < 4; i++)
1304                dev->ep[i].irqs = 0;
1305
1306        /* can't modify descriptors after writing UsbReady */
1307        for (i = 0; i < DESC_LEN; i++)
1308                writel(0, &regs->descriptors[i]);
1309        writel(0, &regs->UsbReady);
1310
1311        /* expect ep0 requests when the host drops reset */
1312        writel(PW_RESETB | PW_PULLUP, &regs->power_detect);
1313        dev->int_enable = INT_DEVWIDE | INT_EP0;
1314        writel(dev->int_enable, &dev->regs->int_enable);
1315        readl(&regs->int_enable);
1316        dev->gadget.speed = USB_SPEED_FULL;
1317        dev->ep0state = EP0_IDLE;
1318}
1319
1320static void udc_enable(struct goku_udc *dev)
1321{
1322        /* start enumeration now, or after power detect irq */
1323        if (readl(&dev->regs->power_detect) & PW_DETECT)
1324                ep0_start(dev);
1325        else {
1326                DBG(dev, "%s\n", __func__);
1327                dev->int_enable = INT_PWRDETECT;
1328                writel(dev->int_enable, &dev->regs->int_enable);
1329        }
1330}
1331
1332/*-------------------------------------------------------------------------*/
1333
1334/* keeping it simple:
1335 * - one bus driver, initted first;
1336 * - one function driver, initted second
1337 */
1338
1339static struct goku_udc  *the_controller;
1340
1341/* when a driver is successfully registered, it will receive
1342 * control requests including set_configuration(), which enables
1343 * non-control requests.  then usb traffic follows until a
1344 * disconnect is reported.  then a host may connect again, or
1345 * the driver might get unbound.
1346 */
1347int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
1348                int (*bind)(struct usb_gadget *))
1349{
1350        struct goku_udc *dev = the_controller;
1351        int                     retval;
1352
1353        if (!driver
1354                        || driver->speed < USB_SPEED_FULL
1355                        || !bind
1356                        || !driver->disconnect
1357                        || !driver->setup)
1358                return -EINVAL;
1359        if (!dev)
1360                return -ENODEV;
1361        if (dev->driver)
1362                return -EBUSY;
1363
1364        /* hook up the driver */
1365        driver->driver.bus = NULL;
1366        dev->driver = driver;
1367        dev->gadget.dev.driver = &driver->driver;
1368        retval = bind(&dev->gadget);
1369        if (retval) {
1370                DBG(dev, "bind to driver %s --> error %d\n",
1371                                driver->driver.name, retval);
1372                dev->driver = NULL;
1373                dev->gadget.dev.driver = NULL;
1374                return retval;
1375        }
1376
1377        /* then enable host detection and ep0; and we're ready
1378         * for set_configuration as well as eventual disconnect.
1379         */
1380        udc_enable(dev);
1381
1382        DBG(dev, "registered gadget driver '%s'\n", driver->driver.name);
1383        return 0;
1384}
1385EXPORT_SYMBOL(usb_gadget_probe_driver);
1386
1387static void
1388stop_activity(struct goku_udc *dev, struct usb_gadget_driver *driver)
1389{
1390        unsigned        i;
1391
1392        DBG (dev, "%s\n", __func__);
1393
1394        if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1395                driver = NULL;
1396
1397        /* disconnect gadget driver after quiesceing hw and the driver */
1398        udc_reset (dev);
1399        for (i = 0; i < 4; i++)
1400                nuke(&dev->ep [i], -ESHUTDOWN);
1401        if (driver) {
1402                spin_unlock(&dev->lock);
1403                driver->disconnect(&dev->gadget);
1404                spin_lock(&dev->lock);
1405        }
1406
1407        if (dev->driver)
1408                udc_enable(dev);
1409}
1410
1411int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1412{
1413        struct goku_udc *dev = the_controller;
1414        unsigned long   flags;
1415
1416        if (!dev)
1417                return -ENODEV;
1418        if (!driver || driver != dev->driver || !driver->unbind)
1419                return -EINVAL;
1420
1421        spin_lock_irqsave(&dev->lock, flags);
1422        dev->driver = NULL;
1423        stop_activity(dev, driver);
1424        spin_unlock_irqrestore(&dev->lock, flags);
1425
1426        driver->unbind(&dev->gadget);
1427        dev->gadget.dev.driver = NULL;
1428
1429        DBG(dev, "unregistered driver '%s'\n", driver->driver.name);
1430        return 0;
1431}
1432EXPORT_SYMBOL(usb_gadget_unregister_driver);
1433
1434
1435/*-------------------------------------------------------------------------*/
1436
1437static void ep0_setup(struct goku_udc *dev)
1438{
1439        struct goku_udc_regs __iomem    *regs = dev->regs;
1440        struct usb_ctrlrequest          ctrl;
1441        int                             tmp;
1442
1443        /* read SETUP packet and enter DATA stage */
1444        ctrl.bRequestType = readl(&regs->bRequestType);
1445        ctrl.bRequest = readl(&regs->bRequest);
1446        ctrl.wValue  = cpu_to_le16((readl(&regs->wValueH)  << 8)
1447                                        | readl(&regs->wValueL));
1448        ctrl.wIndex  = cpu_to_le16((readl(&regs->wIndexH)  << 8)
1449                                        | readl(&regs->wIndexL));
1450        ctrl.wLength = cpu_to_le16((readl(&regs->wLengthH) << 8)
1451                                        | readl(&regs->wLengthL));
1452        writel(0, &regs->SetupRecv);
1453
1454        nuke(&dev->ep[0], 0);
1455        dev->ep[0].stopped = 0;
1456        if (likely(ctrl.bRequestType & USB_DIR_IN)) {
1457                dev->ep[0].is_in = 1;
1458                dev->ep0state = EP0_IN;
1459                /* detect early status stages */
1460                writel(ICONTROL_STATUSNAK, &dev->regs->IntControl);
1461        } else {
1462                dev->ep[0].is_in = 0;
1463                dev->ep0state = EP0_OUT;
1464
1465                /* NOTE:  CLEAR_FEATURE is done in software so that we can
1466                 * synchronize transfer restarts after bulk IN stalls.  data
1467                 * won't even enter the fifo until the halt is cleared.
1468                 */
1469                switch (ctrl.bRequest) {
1470                case USB_REQ_CLEAR_FEATURE:
1471                        switch (ctrl.bRequestType) {
1472                        case USB_RECIP_ENDPOINT:
1473                                tmp = le16_to_cpu(ctrl.wIndex) & 0x0f;
1474                                /* active endpoint */
1475                                if (tmp > 3 || (!dev->ep[tmp].desc && tmp != 0))
1476                                        goto stall;
1477                                if (ctrl.wIndex & cpu_to_le16(
1478                                                USB_DIR_IN)) {
1479                                        if (!dev->ep[tmp].is_in)
1480                                                goto stall;
1481                                } else {
1482                                        if (dev->ep[tmp].is_in)
1483                                                goto stall;
1484                                }
1485                                if (ctrl.wValue != cpu_to_le16(
1486                                                USB_ENDPOINT_HALT))
1487                                        goto stall;
1488                                if (tmp)
1489                                        goku_clear_halt(&dev->ep[tmp]);
1490succeed:
1491                                /* start ep0out status stage */
1492                                writel(~(1<<0), &regs->EOP);
1493                                dev->ep[0].stopped = 1;
1494                                dev->ep0state = EP0_STATUS;
1495                                return;
1496                        case USB_RECIP_DEVICE:
1497                                /* device remote wakeup: always clear */
1498                                if (ctrl.wValue != cpu_to_le16(1))
1499                                        goto stall;
1500                                VDBG(dev, "clear dev remote wakeup\n");
1501                                goto succeed;
1502                        case USB_RECIP_INTERFACE:
1503                                goto stall;
1504                        default:                /* pass to gadget driver */
1505                                break;
1506                        }
1507                        break;
1508                default:
1509                        break;
1510                }
1511        }
1512
1513#ifdef USB_TRACE
1514        VDBG(dev, "SETUP %02x.%02x v%04x i%04x l%04x\n",
1515                ctrl.bRequestType, ctrl.bRequest,
1516                le16_to_cpu(ctrl.wValue), le16_to_cpu(ctrl.wIndex),
1517                le16_to_cpu(ctrl.wLength));
1518#endif
1519
1520        /* hw wants to know when we're configured (or not) */
1521        dev->req_config = (ctrl.bRequest == USB_REQ_SET_CONFIGURATION
1522                                && ctrl.bRequestType == USB_RECIP_DEVICE);
1523        if (unlikely(dev->req_config))
1524                dev->configured = (ctrl.wValue != cpu_to_le16(0));
1525
1526        /* delegate everything to the gadget driver.
1527         * it may respond after this irq handler returns.
1528         */
1529        spin_unlock (&dev->lock);
1530        tmp = dev->driver->setup(&dev->gadget, &ctrl);
1531        spin_lock (&dev->lock);
1532        if (unlikely(tmp < 0)) {
1533stall:
1534#ifdef USB_TRACE
1535                VDBG(dev, "req %02x.%02x protocol STALL; err %d\n",
1536                                ctrl.bRequestType, ctrl.bRequest, tmp);
1537#endif
1538                command(regs, COMMAND_STALL, 0);
1539                dev->ep[0].stopped = 1;
1540                dev->ep0state = EP0_STALL;
1541        }
1542
1543        /* expect at least one data or status stage irq */
1544}
1545
1546#define ACK(irqbit) { \
1547                stat &= ~irqbit; \
1548                writel(~irqbit, &regs->int_status); \
1549                handled = 1; \
1550                }
1551
1552static irqreturn_t goku_irq(int irq, void *_dev)
1553{
1554        struct goku_udc                 *dev = _dev;
1555        struct goku_udc_regs __iomem    *regs = dev->regs;
1556        struct goku_ep                  *ep;
1557        u32                             stat, handled = 0;
1558        unsigned                        i, rescans = 5;
1559
1560        spin_lock(&dev->lock);
1561
1562rescan:
1563        stat = readl(&regs->int_status) & dev->int_enable;
1564        if (!stat)
1565                goto done;
1566        dev->irqs++;
1567
1568        /* device-wide irqs */
1569        if (unlikely(stat & INT_DEVWIDE)) {
1570                if (stat & INT_SYSERROR) {
1571                        ERROR(dev, "system error\n");
1572                        stop_activity(dev, dev->driver);
1573                        stat = 0;
1574                        handled = 1;
1575                        // FIXME have a neater way to prevent re-enumeration
1576                        dev->driver = NULL;
1577                        goto done;
1578                }
1579                if (stat & INT_PWRDETECT) {
1580                        writel(~stat, &regs->int_status);
1581                        if (readl(&dev->regs->power_detect) & PW_DETECT) {
1582                                VDBG(dev, "connect\n");
1583                                ep0_start(dev);
1584                        } else {
1585                                DBG(dev, "disconnect\n");
1586                                if (dev->gadget.speed == USB_SPEED_FULL)
1587                                        stop_activity(dev, dev->driver);
1588                                dev->ep0state = EP0_DISCONNECT;
1589                                dev->int_enable = INT_DEVWIDE;
1590                                writel(dev->int_enable, &dev->regs->int_enable);
1591                        }
1592                        stat = 0;
1593                        handled = 1;
1594                        goto done;
1595                }
1596                if (stat & INT_SUSPEND) {
1597                        ACK(INT_SUSPEND);
1598                        if (readl(&regs->ep_status[0]) & EPxSTATUS_SUSPEND) {
1599                                switch (dev->ep0state) {
1600                                case EP0_DISCONNECT:
1601                                case EP0_SUSPEND:
1602                                        goto pm_next;
1603                                default:
1604                                        break;
1605                                }
1606                                DBG(dev, "USB suspend\n");
1607                                dev->ep0state = EP0_SUSPEND;
1608                                if (dev->gadget.speed != USB_SPEED_UNKNOWN
1609                                                && dev->driver
1610                                                && dev->driver->suspend) {
1611                                        spin_unlock(&dev->lock);
1612                                        dev->driver->suspend(&dev->gadget);
1613                                        spin_lock(&dev->lock);
1614                                }
1615                        } else {
1616                                if (dev->ep0state != EP0_SUSPEND) {
1617                                        DBG(dev, "bogus USB resume %d\n",
1618                                                dev->ep0state);
1619                                        goto pm_next;
1620                                }
1621                                DBG(dev, "USB resume\n");
1622                                dev->ep0state = EP0_IDLE;
1623                                if (dev->gadget.speed != USB_SPEED_UNKNOWN
1624                                                && dev->driver
1625                                                && dev->driver->resume) {
1626                                        spin_unlock(&dev->lock);
1627                                        dev->driver->resume(&dev->gadget);
1628                                        spin_lock(&dev->lock);
1629                                }
1630                        }
1631                }
1632pm_next:
1633                if (stat & INT_USBRESET) {              /* hub reset done */
1634                        ACK(INT_USBRESET);
1635                        INFO(dev, "USB reset done, gadget %s\n",
1636                                dev->driver->driver.name);
1637                }
1638                // and INT_ERR on some endpoint's crc/bitstuff/... problem
1639        }
1640
1641        /* progress ep0 setup, data, or status stages.
1642         * no transition {EP0_STATUS, EP0_STALL} --> EP0_IDLE; saves irqs
1643         */
1644        if (stat & INT_SETUP) {
1645                ACK(INT_SETUP);
1646                dev->ep[0].irqs++;
1647                ep0_setup(dev);
1648        }
1649        if (stat & INT_STATUSNAK) {
1650                ACK(INT_STATUSNAK|INT_ENDPOINT0);
1651                if (dev->ep0state == EP0_IN) {
1652                        ep = &dev->ep[0];
1653                        ep->irqs++;
1654                        nuke(ep, 0);
1655                        writel(~(1<<0), &regs->EOP);
1656                        dev->ep0state = EP0_STATUS;
1657                }
1658        }
1659        if (stat & INT_ENDPOINT0) {
1660                ACK(INT_ENDPOINT0);
1661                ep = &dev->ep[0];
1662                ep->irqs++;
1663                pio_advance(ep);
1664        }
1665
1666        /* dma completion */
1667        if (stat & INT_MSTRDEND) {      /* IN */
1668                ACK(INT_MSTRDEND);
1669                ep = &dev->ep[UDC_MSTRD_ENDPOINT];
1670                ep->irqs++;
1671                dma_advance(dev, ep);
1672        }
1673        if (stat & INT_MSTWREND) {      /* OUT */
1674                ACK(INT_MSTWREND);
1675                ep = &dev->ep[UDC_MSTWR_ENDPOINT];
1676                ep->irqs++;
1677                dma_advance(dev, ep);
1678        }
1679        if (stat & INT_MSTWRTMOUT) {    /* OUT */
1680                ACK(INT_MSTWRTMOUT);
1681                ep = &dev->ep[UDC_MSTWR_ENDPOINT];
1682                ep->irqs++;
1683                ERROR(dev, "%s write timeout ?\n", ep->ep.name);
1684                // reset dma? then dma_advance()
1685        }
1686
1687        /* pio */
1688        for (i = 1; i < 4; i++) {
1689                u32             tmp = INT_EPxDATASET(i);
1690
1691                if (!(stat & tmp))
1692                        continue;
1693                ep = &dev->ep[i];
1694                pio_advance(ep);
1695                if (list_empty (&ep->queue))
1696                        pio_irq_disable(dev, regs, i);
1697                stat &= ~tmp;
1698                handled = 1;
1699                ep->irqs++;
1700        }
1701
1702        if (rescans--)
1703                goto rescan;
1704
1705done:
1706        (void)readl(&regs->int_enable);
1707        spin_unlock(&dev->lock);
1708        if (stat)
1709                DBG(dev, "unhandled irq status: %05x (%05x, %05x)\n", stat,
1710                                readl(&regs->int_status), dev->int_enable);
1711        return IRQ_RETVAL(handled);
1712}
1713
1714#undef ACK
1715
1716/*-------------------------------------------------------------------------*/
1717
1718static void gadget_release(struct device *_dev)
1719{
1720        struct goku_udc *dev = dev_get_drvdata(_dev);
1721
1722        kfree(dev);
1723}
1724
1725/* tear down the binding between this driver and the pci device */
1726
1727static void goku_remove(struct pci_dev *pdev)
1728{
1729        struct goku_udc         *dev = pci_get_drvdata(pdev);
1730
1731        DBG(dev, "%s\n", __func__);
1732
1733        BUG_ON(dev->driver);
1734
1735#ifdef CONFIG_USB_GADGET_DEBUG_FILES
1736        remove_proc_entry(proc_node_name, NULL);
1737#endif
1738        if (dev->regs)
1739                udc_reset(dev);
1740        if (dev->got_irq)
1741                free_irq(pdev->irq, dev);
1742        if (dev->regs)
1743                iounmap(dev->regs);
1744        if (dev->got_region)
1745                release_mem_region(pci_resource_start (pdev, 0),
1746                                pci_resource_len (pdev, 0));
1747        if (dev->enabled)
1748                pci_disable_device(pdev);
1749        if (dev->registered)
1750                device_unregister(&dev->gadget.dev);
1751
1752        pci_set_drvdata(pdev, NULL);
1753        dev->regs = NULL;
1754        the_controller = NULL;
1755
1756        INFO(dev, "unbind\n");
1757}
1758
1759/* wrap this driver around the specified pci device, but
1760 * don't respond over USB until a gadget driver binds to us.
1761 */
1762
1763static int goku_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1764{
1765        struct goku_udc         *dev = NULL;
1766        unsigned long           resource, len;
1767        void __iomem            *base = NULL;
1768        int                     retval;
1769
1770        /* if you want to support more than one controller in a system,
1771         * usb_gadget_driver_{register,unregister}() must change.
1772         */
1773        if (the_controller) {
1774                pr_warning("ignoring %s\n", pci_name(pdev));
1775                return -EBUSY;
1776        }
1777        if (!pdev->irq) {
1778                printk(KERN_ERR "Check PCI %s IRQ setup!\n", pci_name(pdev));
1779                retval = -ENODEV;
1780                goto err;
1781        }
1782
1783        /* alloc, and start init */
1784        dev = kzalloc (sizeof *dev, GFP_KERNEL);
1785        if (dev == NULL){
1786                pr_debug("enomem %s\n", pci_name(pdev));
1787                retval = -ENOMEM;
1788                goto err;
1789        }
1790
1791        spin_lock_init(&dev->lock);
1792        dev->pdev = pdev;
1793        dev->gadget.ops = &goku_ops;
1794
1795        /* the "gadget" abstracts/virtualizes the controller */
1796        dev_set_name(&dev->gadget.dev, "gadget");
1797        dev->gadget.dev.parent = &pdev->dev;
1798        dev->gadget.dev.dma_mask = pdev->dev.dma_mask;
1799        dev->gadget.dev.release = gadget_release;
1800        dev->gadget.name = driver_name;
1801
1802        /* now all the pci goodies ... */
1803        retval = pci_enable_device(pdev);
1804        if (retval < 0) {
1805                DBG(dev, "can't enable, %d\n", retval);
1806                goto err;
1807        }
1808        dev->enabled = 1;
1809
1810        resource = pci_resource_start(pdev, 0);
1811        len = pci_resource_len(pdev, 0);
1812        if (!request_mem_region(resource, len, driver_name)) {
1813                DBG(dev, "controller already in use\n");
1814                retval = -EBUSY;
1815                goto err;
1816        }
1817        dev->got_region = 1;
1818
1819        base = ioremap_nocache(resource, len);
1820        if (base == NULL) {
1821                DBG(dev, "can't map memory\n");
1822                retval = -EFAULT;
1823                goto err;
1824        }
1825        dev->regs = (struct goku_udc_regs __iomem *) base;
1826
1827        pci_set_drvdata(pdev, dev);
1828        INFO(dev, "%s\n", driver_desc);
1829        INFO(dev, "version: " DRIVER_VERSION " %s\n", dmastr());
1830        INFO(dev, "irq %d, pci mem %p\n", pdev->irq, base);
1831
1832        /* init to known state, then setup irqs */
1833        udc_reset(dev);
1834        udc_reinit (dev);
1835        if (request_irq(pdev->irq, goku_irq, IRQF_SHARED/*|IRQF_SAMPLE_RANDOM*/,
1836                        driver_name, dev) != 0) {
1837                DBG(dev, "request interrupt %d failed\n", pdev->irq);
1838                retval = -EBUSY;
1839                goto err;
1840        }
1841        dev->got_irq = 1;
1842        if (use_dma)
1843                pci_set_master(pdev);
1844
1845
1846#ifdef CONFIG_USB_GADGET_DEBUG_FILES
1847        create_proc_read_entry(proc_node_name, 0, NULL, udc_proc_read, dev);
1848#endif
1849
1850        the_controller = dev;
1851        retval = device_register(&dev->gadget.dev);
1852        if (retval) {
1853                put_device(&dev->gadget.dev);
1854                goto err;
1855        }
1856        dev->registered = 1;
1857        return 0;
1858
1859err:
1860        if (dev)
1861                goku_remove (pdev);
1862        return retval;
1863}
1864
1865
1866/*-------------------------------------------------------------------------*/
1867
1868static const struct pci_device_id pci_ids[] = { {
1869        .class =        ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
1870        .class_mask =   ~0,
1871        .vendor =       0x102f,         /* Toshiba */
1872        .device =       0x0107,         /* this UDC */
1873        .subvendor =    PCI_ANY_ID,
1874        .subdevice =    PCI_ANY_ID,
1875
1876}, { /* end: all zeroes */ }
1877};
1878MODULE_DEVICE_TABLE (pci, pci_ids);
1879
1880static struct pci_driver goku_pci_driver = {
1881        .name =         (char *) driver_name,
1882        .id_table =     pci_ids,
1883
1884        .probe =        goku_probe,
1885        .remove =       goku_remove,
1886
1887        /* FIXME add power management support */
1888};
1889
1890static int __init init (void)
1891{
1892        return pci_register_driver (&goku_pci_driver);
1893}
1894module_init (init);
1895
1896static void __exit cleanup (void)
1897{
1898        pci_unregister_driver (&goku_pci_driver);
1899}
1900module_exit (cleanup);
1901