linux/drivers/usb/gadget/pxa25x_udc.c
<<
>>
Prefs
   1/*
   2 * Intel PXA25x and IXP4xx on-chip full speed USB device controllers
   3 *
   4 * Copyright (C) 2002 Intrinsyc, Inc. (Frank Becker)
   5 * Copyright (C) 2003 Robert Schwebel, Pengutronix
   6 * Copyright (C) 2003 Benedikt Spranger, Pengutronix
   7 * Copyright (C) 2003 David Brownell
   8 * Copyright (C) 2003 Joshua Wise
   9 *
  10 * This program is free software; you can redistribute it and/or modify
  11 * it under the terms of the GNU General Public License as published by
  12 * the Free Software Foundation; either version 2 of the License, or
  13 * (at your option) any later version.
  14 */
  15
  16/* #define VERBOSE_DEBUG */
  17
  18#include <linux/device.h>
  19#include <linux/module.h>
  20#include <linux/kernel.h>
  21#include <linux/ioport.h>
  22#include <linux/types.h>
  23#include <linux/errno.h>
  24#include <linux/err.h>
  25#include <linux/delay.h>
  26#include <linux/slab.h>
  27#include <linux/timer.h>
  28#include <linux/list.h>
  29#include <linux/interrupt.h>
  30#include <linux/mm.h>
  31#include <linux/platform_data/pxa2xx_udc.h>
  32#include <linux/platform_device.h>
  33#include <linux/dma-mapping.h>
  34#include <linux/irq.h>
  35#include <linux/clk.h>
  36#include <linux/seq_file.h>
  37#include <linux/debugfs.h>
  38#include <linux/io.h>
  39#include <linux/prefetch.h>
  40
  41#include <asm/byteorder.h>
  42#include <asm/dma.h>
  43#include <asm/gpio.h>
  44#include <asm/mach-types.h>
  45#include <asm/unaligned.h>
  46
  47#include <linux/usb/ch9.h>
  48#include <linux/usb/gadget.h>
  49#include <linux/usb/otg.h>
  50
  51/*
  52 * This driver is PXA25x only.  Grab the right register definitions.
  53 */
  54#ifdef CONFIG_ARCH_PXA
  55#include <mach/pxa25x-udc.h>
  56#endif
  57
  58#ifdef CONFIG_ARCH_LUBBOCK
  59#include <mach/lubbock.h>
  60#endif
  61
  62/*
  63 * This driver handles the USB Device Controller (UDC) in Intel's PXA 25x
  64 * series processors.  The UDC for the IXP 4xx series is very similar.
  65 * There are fifteen endpoints, in addition to ep0.
  66 *
  67 * Such controller drivers work with a gadget driver.  The gadget driver
  68 * returns descriptors, implements configuration and data protocols used
  69 * by the host to interact with this device, and allocates endpoints to
  70 * the different protocol interfaces.  The controller driver virtualizes
  71 * usb hardware so that the gadget drivers will be more portable.
  72 *
  73 * This UDC hardware wants to implement a bit too much USB protocol, so
  74 * it constrains the sorts of USB configuration change events that work.
  75 * The errata for these chips are misleading; some "fixed" bugs from
  76 * pxa250 a0/a1 b0/b1/b2 sure act like they're still there.
  77 *
  78 * Note that the UDC hardware supports DMA (except on IXP) but that's
  79 * not used here.  IN-DMA (to host) is simple enough, when the data is
  80 * suitably aligned (16 bytes) ... the network stack doesn't do that,
  81 * other software can.  OUT-DMA is buggy in most chip versions, as well
  82 * as poorly designed (data toggle not automatic).  So this driver won't
  83 * bother using DMA.  (Mostly-working IN-DMA support was available in
  84 * kernels before 2.6.23, but was never enabled or well tested.)
  85 */
  86
  87#define DRIVER_VERSION  "30-June-2007"
  88#define DRIVER_DESC     "PXA 25x USB Device Controller driver"
  89
  90
  91static const char driver_name [] = "pxa25x_udc";
  92
  93static const char ep0name [] = "ep0";
  94
  95
  96#ifdef CONFIG_ARCH_IXP4XX
  97
  98/* cpu-specific register addresses are compiled in to this code */
  99#ifdef CONFIG_ARCH_PXA
 100#error "Can't configure both IXP and PXA"
 101#endif
 102
 103/* IXP doesn't yet support <linux/clk.h> */
 104#define clk_get(dev,name)       NULL
 105#define clk_enable(clk)         do { } while (0)
 106#define clk_disable(clk)        do { } while (0)
 107#define clk_put(clk)            do { } while (0)
 108
 109#endif
 110
 111#include "pxa25x_udc.h"
 112
 113
 114#ifdef  CONFIG_USB_PXA25X_SMALL
 115#define SIZE_STR        " (small)"
 116#else
 117#define SIZE_STR        ""
 118#endif
 119
 120/* ---------------------------------------------------------------------------
 121 *      endpoint related parts of the api to the usb controller hardware,
 122 *      used by gadget driver; and the inner talker-to-hardware core.
 123 * ---------------------------------------------------------------------------
 124 */
 125
 126static void pxa25x_ep_fifo_flush (struct usb_ep *ep);
 127static void nuke (struct pxa25x_ep *, int status);
 128
 129/* one GPIO should control a D+ pullup, so host sees this device (or not) */
 130static void pullup_off(void)
 131{
 132        struct pxa2xx_udc_mach_info             *mach = the_controller->mach;
 133        int off_level = mach->gpio_pullup_inverted;
 134
 135        if (gpio_is_valid(mach->gpio_pullup))
 136                gpio_set_value(mach->gpio_pullup, off_level);
 137        else if (mach->udc_command)
 138                mach->udc_command(PXA2XX_UDC_CMD_DISCONNECT);
 139}
 140
 141static void pullup_on(void)
 142{
 143        struct pxa2xx_udc_mach_info             *mach = the_controller->mach;
 144        int on_level = !mach->gpio_pullup_inverted;
 145
 146        if (gpio_is_valid(mach->gpio_pullup))
 147                gpio_set_value(mach->gpio_pullup, on_level);
 148        else if (mach->udc_command)
 149                mach->udc_command(PXA2XX_UDC_CMD_CONNECT);
 150}
 151
 152static void pio_irq_enable(int bEndpointAddress)
 153{
 154        bEndpointAddress &= 0xf;
 155        if (bEndpointAddress < 8)
 156                UICR0 &= ~(1 << bEndpointAddress);
 157        else {
 158                bEndpointAddress -= 8;
 159                UICR1 &= ~(1 << bEndpointAddress);
 160        }
 161}
 162
 163static void pio_irq_disable(int bEndpointAddress)
 164{
 165        bEndpointAddress &= 0xf;
 166        if (bEndpointAddress < 8)
 167                UICR0 |= 1 << bEndpointAddress;
 168        else {
 169                bEndpointAddress -= 8;
 170                UICR1 |= 1 << bEndpointAddress;
 171        }
 172}
 173
 174/* The UDCCR reg contains mask and interrupt status bits,
 175 * so using '|=' isn't safe as it may ack an interrupt.
 176 */
 177#define UDCCR_MASK_BITS         (UDCCR_REM | UDCCR_SRM | UDCCR_UDE)
 178
 179static inline void udc_set_mask_UDCCR(int mask)
 180{
 181        UDCCR = (UDCCR & UDCCR_MASK_BITS) | (mask & UDCCR_MASK_BITS);
 182}
 183
 184static inline void udc_clear_mask_UDCCR(int mask)
 185{
 186        UDCCR = (UDCCR & UDCCR_MASK_BITS) & ~(mask & UDCCR_MASK_BITS);
 187}
 188
 189static inline void udc_ack_int_UDCCR(int mask)
 190{
 191        /* udccr contains the bits we dont want to change */
 192        __u32 udccr = UDCCR & UDCCR_MASK_BITS;
 193
 194        UDCCR = udccr | (mask & ~UDCCR_MASK_BITS);
 195}
 196
 197/*
 198 * endpoint enable/disable
 199 *
 200 * we need to verify the descriptors used to enable endpoints.  since pxa25x
 201 * endpoint configurations are fixed, and are pretty much always enabled,
 202 * there's not a lot to manage here.
 203 *
 204 * because pxa25x can't selectively initialize bulk (or interrupt) endpoints,
 205 * (resetting endpoint halt and toggle), SET_INTERFACE is unusable except
 206 * for a single interface (with only the default altsetting) and for gadget
 207 * drivers that don't halt endpoints (not reset by set_interface).  that also
 208 * means that if you use ISO, you must violate the USB spec rule that all
 209 * iso endpoints must be in non-default altsettings.
 210 */
 211static int pxa25x_ep_enable (struct usb_ep *_ep,
 212                const struct usb_endpoint_descriptor *desc)
 213{
 214        struct pxa25x_ep        *ep;
 215        struct pxa25x_udc       *dev;
 216
 217        ep = container_of (_ep, struct pxa25x_ep, ep);
 218        if (!_ep || !desc || _ep->name == ep0name
 219                        || desc->bDescriptorType != USB_DT_ENDPOINT
 220                        || ep->bEndpointAddress != desc->bEndpointAddress
 221                        || ep->fifo_size < usb_endpoint_maxp (desc)) {
 222                DMSG("%s, bad ep or descriptor\n", __func__);
 223                return -EINVAL;
 224        }
 225
 226        /* xfer types must match, except that interrupt ~= bulk */
 227        if (ep->bmAttributes != desc->bmAttributes
 228                        && ep->bmAttributes != USB_ENDPOINT_XFER_BULK
 229                        && desc->bmAttributes != USB_ENDPOINT_XFER_INT) {
 230                DMSG("%s, %s type mismatch\n", __func__, _ep->name);
 231                return -EINVAL;
 232        }
 233
 234        /* hardware _could_ do smaller, but driver doesn't */
 235        if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK
 236                                && usb_endpoint_maxp (desc)
 237                                                != BULK_FIFO_SIZE)
 238                        || !desc->wMaxPacketSize) {
 239                DMSG("%s, bad %s maxpacket\n", __func__, _ep->name);
 240                return -ERANGE;
 241        }
 242
 243        dev = ep->dev;
 244        if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
 245                DMSG("%s, bogus device state\n", __func__);
 246                return -ESHUTDOWN;
 247        }
 248
 249        ep->ep.desc = desc;
 250        ep->stopped = 0;
 251        ep->pio_irqs = 0;
 252        ep->ep.maxpacket = usb_endpoint_maxp (desc);
 253
 254        /* flush fifo (mostly for OUT buffers) */
 255        pxa25x_ep_fifo_flush (_ep);
 256
 257        /* ... reset halt state too, if we could ... */
 258
 259        DBG(DBG_VERBOSE, "enabled %s\n", _ep->name);
 260        return 0;
 261}
 262
 263static int pxa25x_ep_disable (struct usb_ep *_ep)
 264{
 265        struct pxa25x_ep        *ep;
 266        unsigned long           flags;
 267
 268        ep = container_of (_ep, struct pxa25x_ep, ep);
 269        if (!_ep || !ep->ep.desc) {
 270                DMSG("%s, %s not enabled\n", __func__,
 271                        _ep ? ep->ep.name : NULL);
 272                return -EINVAL;
 273        }
 274        local_irq_save(flags);
 275
 276        nuke (ep, -ESHUTDOWN);
 277
 278        /* flush fifo (mostly for IN buffers) */
 279        pxa25x_ep_fifo_flush (_ep);
 280
 281        ep->ep.desc = NULL;
 282        ep->stopped = 1;
 283
 284        local_irq_restore(flags);
 285        DBG(DBG_VERBOSE, "%s disabled\n", _ep->name);
 286        return 0;
 287}
 288
 289/*-------------------------------------------------------------------------*/
 290
 291/* for the pxa25x, these can just wrap kmalloc/kfree.  gadget drivers
 292 * must still pass correctly initialized endpoints, since other controller
 293 * drivers may care about how it's currently set up (dma issues etc).
 294 */
 295
 296/*
 297 *      pxa25x_ep_alloc_request - allocate a request data structure
 298 */
 299static struct usb_request *
 300pxa25x_ep_alloc_request (struct usb_ep *_ep, gfp_t gfp_flags)
 301{
 302        struct pxa25x_request *req;
 303
 304        req = kzalloc(sizeof(*req), gfp_flags);
 305        if (!req)
 306                return NULL;
 307
 308        INIT_LIST_HEAD (&req->queue);
 309        return &req->req;
 310}
 311
 312
 313/*
 314 *      pxa25x_ep_free_request - deallocate a request data structure
 315 */
 316static void
 317pxa25x_ep_free_request (struct usb_ep *_ep, struct usb_request *_req)
 318{
 319        struct pxa25x_request   *req;
 320
 321        req = container_of (_req, struct pxa25x_request, req);
 322        WARN_ON(!list_empty (&req->queue));
 323        kfree(req);
 324}
 325
 326/*-------------------------------------------------------------------------*/
 327
 328/*
 329 *      done - retire a request; caller blocked irqs
 330 */
 331static void done(struct pxa25x_ep *ep, struct pxa25x_request *req, int status)
 332{
 333        unsigned                stopped = ep->stopped;
 334
 335        list_del_init(&req->queue);
 336
 337        if (likely (req->req.status == -EINPROGRESS))
 338                req->req.status = status;
 339        else
 340                status = req->req.status;
 341
 342        if (status && status != -ESHUTDOWN)
 343                DBG(DBG_VERBOSE, "complete %s req %p stat %d len %u/%u\n",
 344                        ep->ep.name, &req->req, status,
 345                        req->req.actual, req->req.length);
 346
 347        /* don't modify queue heads during completion callback */
 348        ep->stopped = 1;
 349        req->req.complete(&ep->ep, &req->req);
 350        ep->stopped = stopped;
 351}
 352
 353
 354static inline void ep0_idle (struct pxa25x_udc *dev)
 355{
 356        dev->ep0state = EP0_IDLE;
 357}
 358
 359static int
 360write_packet(volatile u32 *uddr, struct pxa25x_request *req, unsigned max)
 361{
 362        u8              *buf;
 363        unsigned        length, count;
 364
 365        buf = req->req.buf + req->req.actual;
 366        prefetch(buf);
 367
 368        /* how big will this packet be? */
 369        length = min(req->req.length - req->req.actual, max);
 370        req->req.actual += length;
 371
 372        count = length;
 373        while (likely(count--))
 374                *uddr = *buf++;
 375
 376        return length;
 377}
 378
 379/*
 380 * write to an IN endpoint fifo, as many packets as possible.
 381 * irqs will use this to write the rest later.
 382 * caller guarantees at least one packet buffer is ready (or a zlp).
 383 */
 384static int
 385write_fifo (struct pxa25x_ep *ep, struct pxa25x_request *req)
 386{
 387        unsigned                max;
 388
 389        max = usb_endpoint_maxp(ep->ep.desc);
 390        do {
 391                unsigned        count;
 392                int             is_last, is_short;
 393
 394                count = write_packet(ep->reg_uddr, req, max);
 395
 396                /* last packet is usually short (or a zlp) */
 397                if (unlikely (count != max))
 398                        is_last = is_short = 1;
 399                else {
 400                        if (likely(req->req.length != req->req.actual)
 401                                        || req->req.zero)
 402                                is_last = 0;
 403                        else
 404                                is_last = 1;
 405                        /* interrupt/iso maxpacket may not fill the fifo */
 406                        is_short = unlikely (max < ep->fifo_size);
 407                }
 408
 409                DBG(DBG_VERY_NOISY, "wrote %s %d bytes%s%s %d left %p\n",
 410                        ep->ep.name, count,
 411                        is_last ? "/L" : "", is_short ? "/S" : "",
 412                        req->req.length - req->req.actual, req);
 413
 414                /* let loose that packet. maybe try writing another one,
 415                 * double buffering might work.  TSP, TPC, and TFS
 416                 * bit values are the same for all normal IN endpoints.
 417                 */
 418                *ep->reg_udccs = UDCCS_BI_TPC;
 419                if (is_short)
 420                        *ep->reg_udccs = UDCCS_BI_TSP;
 421
 422                /* requests complete when all IN data is in the FIFO */
 423                if (is_last) {
 424                        done (ep, req, 0);
 425                        if (list_empty(&ep->queue))
 426                                pio_irq_disable (ep->bEndpointAddress);
 427                        return 1;
 428                }
 429
 430                // TODO experiment: how robust can fifo mode tweaking be?
 431                // double buffering is off in the default fifo mode, which
 432                // prevents TFS from being set here.
 433
 434        } while (*ep->reg_udccs & UDCCS_BI_TFS);
 435        return 0;
 436}
 437
 438/* caller asserts req->pending (ep0 irq status nyet cleared); starts
 439 * ep0 data stage.  these chips want very simple state transitions.
 440 */
 441static inline
 442void ep0start(struct pxa25x_udc *dev, u32 flags, const char *tag)
 443{
 444        UDCCS0 = flags|UDCCS0_SA|UDCCS0_OPR;
 445        USIR0 = USIR0_IR0;
 446        dev->req_pending = 0;
 447        DBG(DBG_VERY_NOISY, "%s %s, %02x/%02x\n",
 448                __func__, tag, UDCCS0, flags);
 449}
 450
 451static int
 452write_ep0_fifo (struct pxa25x_ep *ep, struct pxa25x_request *req)
 453{
 454        unsigned        count;
 455        int             is_short;
 456
 457        count = write_packet(&UDDR0, req, EP0_FIFO_SIZE);
 458        ep->dev->stats.write.bytes += count;
 459
 460        /* last packet "must be" short (or a zlp) */
 461        is_short = (count != EP0_FIFO_SIZE);
 462
 463        DBG(DBG_VERY_NOISY, "ep0in %d bytes %d left %p\n", count,
 464                req->req.length - req->req.actual, req);
 465
 466        if (unlikely (is_short)) {
 467                if (ep->dev->req_pending)
 468                        ep0start(ep->dev, UDCCS0_IPR, "short IN");
 469                else
 470                        UDCCS0 = UDCCS0_IPR;
 471
 472                count = req->req.length;
 473                done (ep, req, 0);
 474                ep0_idle(ep->dev);
 475#ifndef CONFIG_ARCH_IXP4XX
 476#if 1
 477                /* This seems to get rid of lost status irqs in some cases:
 478                 * host responds quickly, or next request involves config
 479                 * change automagic, or should have been hidden, or ...
 480                 *
 481                 * FIXME get rid of all udelays possible...
 482                 */
 483                if (count >= EP0_FIFO_SIZE) {
 484                        count = 100;
 485                        do {
 486                                if ((UDCCS0 & UDCCS0_OPR) != 0) {
 487                                        /* clear OPR, generate ack */
 488                                        UDCCS0 = UDCCS0_OPR;
 489                                        break;
 490                                }
 491                                count--;
 492                                udelay(1);
 493                        } while (count);
 494                }
 495#endif
 496#endif
 497        } else if (ep->dev->req_pending)
 498                ep0start(ep->dev, 0, "IN");
 499        return is_short;
 500}
 501
 502
 503/*
 504 * read_fifo -  unload packet(s) from the fifo we use for usb OUT
 505 * transfers and put them into the request.  caller should have made
 506 * sure there's at least one packet ready.
 507 *
 508 * returns true if the request completed because of short packet or the
 509 * request buffer having filled (and maybe overran till end-of-packet).
 510 */
 511static int
 512read_fifo (struct pxa25x_ep *ep, struct pxa25x_request *req)
 513{
 514        for (;;) {
 515                u32             udccs;
 516                u8              *buf;
 517                unsigned        bufferspace, count, is_short;
 518
 519                /* make sure there's a packet in the FIFO.
 520                 * UDCCS_{BO,IO}_RPC are all the same bit value.
 521                 * UDCCS_{BO,IO}_RNE are all the same bit value.
 522                 */
 523                udccs = *ep->reg_udccs;
 524                if (unlikely ((udccs & UDCCS_BO_RPC) == 0))
 525                        break;
 526                buf = req->req.buf + req->req.actual;
 527                prefetchw(buf);
 528                bufferspace = req->req.length - req->req.actual;
 529
 530                /* read all bytes from this packet */
 531                if (likely (udccs & UDCCS_BO_RNE)) {
 532                        count = 1 + (0x0ff & *ep->reg_ubcr);
 533                        req->req.actual += min (count, bufferspace);
 534                } else /* zlp */
 535                        count = 0;
 536                is_short = (count < ep->ep.maxpacket);
 537                DBG(DBG_VERY_NOISY, "read %s %02x, %d bytes%s req %p %d/%d\n",
 538                        ep->ep.name, udccs, count,
 539                        is_short ? "/S" : "",
 540                        req, req->req.actual, req->req.length);
 541                while (likely (count-- != 0)) {
 542                        u8      byte = (u8) *ep->reg_uddr;
 543
 544                        if (unlikely (bufferspace == 0)) {
 545                                /* this happens when the driver's buffer
 546                                 * is smaller than what the host sent.
 547                                 * discard the extra data.
 548                                 */
 549                                if (req->req.status != -EOVERFLOW)
 550                                        DMSG("%s overflow %d\n",
 551                                                ep->ep.name, count);
 552                                req->req.status = -EOVERFLOW;
 553                        } else {
 554                                *buf++ = byte;
 555                                bufferspace--;
 556                        }
 557                }
 558                *ep->reg_udccs =  UDCCS_BO_RPC;
 559                /* RPC/RSP/RNE could now reflect the other packet buffer */
 560
 561                /* iso is one request per packet */
 562                if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
 563                        if (udccs & UDCCS_IO_ROF)
 564                                req->req.status = -EHOSTUNREACH;
 565                        /* more like "is_done" */
 566                        is_short = 1;
 567                }
 568
 569                /* completion */
 570                if (is_short || req->req.actual == req->req.length) {
 571                        done (ep, req, 0);
 572                        if (list_empty(&ep->queue))
 573                                pio_irq_disable (ep->bEndpointAddress);
 574                        return 1;
 575                }
 576
 577                /* finished that packet.  the next one may be waiting... */
 578        }
 579        return 0;
 580}
 581
 582/*
 583 * special ep0 version of the above.  no UBCR0 or double buffering; status
 584 * handshaking is magic.  most device protocols don't need control-OUT.
 585 * CDC vendor commands (and RNDIS), mass storage CB/CBI, and some other
 586 * protocols do use them.
 587 */
 588static int
 589read_ep0_fifo (struct pxa25x_ep *ep, struct pxa25x_request *req)
 590{
 591        u8              *buf, byte;
 592        unsigned        bufferspace;
 593
 594        buf = req->req.buf + req->req.actual;
 595        bufferspace = req->req.length - req->req.actual;
 596
 597        while (UDCCS0 & UDCCS0_RNE) {
 598                byte = (u8) UDDR0;
 599
 600                if (unlikely (bufferspace == 0)) {
 601                        /* this happens when the driver's buffer
 602                         * is smaller than what the host sent.
 603                         * discard the extra data.
 604                         */
 605                        if (req->req.status != -EOVERFLOW)
 606                                DMSG("%s overflow\n", ep->ep.name);
 607                        req->req.status = -EOVERFLOW;
 608                } else {
 609                        *buf++ = byte;
 610                        req->req.actual++;
 611                        bufferspace--;
 612                }
 613        }
 614
 615        UDCCS0 = UDCCS0_OPR | UDCCS0_IPR;
 616
 617        /* completion */
 618        if (req->req.actual >= req->req.length)
 619                return 1;
 620
 621        /* finished that packet.  the next one may be waiting... */
 622        return 0;
 623}
 624
 625/*-------------------------------------------------------------------------*/
 626
 627static int
 628pxa25x_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
 629{
 630        struct pxa25x_request   *req;
 631        struct pxa25x_ep        *ep;
 632        struct pxa25x_udc       *dev;
 633        unsigned long           flags;
 634
 635        req = container_of(_req, struct pxa25x_request, req);
 636        if (unlikely (!_req || !_req->complete || !_req->buf
 637                        || !list_empty(&req->queue))) {
 638                DMSG("%s, bad params\n", __func__);
 639                return -EINVAL;
 640        }
 641
 642        ep = container_of(_ep, struct pxa25x_ep, ep);
 643        if (unlikely(!_ep || (!ep->ep.desc && ep->ep.name != ep0name))) {
 644                DMSG("%s, bad ep\n", __func__);
 645                return -EINVAL;
 646        }
 647
 648        dev = ep->dev;
 649        if (unlikely (!dev->driver
 650                        || dev->gadget.speed == USB_SPEED_UNKNOWN)) {
 651                DMSG("%s, bogus device state\n", __func__);
 652                return -ESHUTDOWN;
 653        }
 654
 655        /* iso is always one packet per request, that's the only way
 656         * we can report per-packet status.  that also helps with dma.
 657         */
 658        if (unlikely (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
 659                        && req->req.length > usb_endpoint_maxp(ep->ep.desc)))
 660                return -EMSGSIZE;
 661
 662        DBG(DBG_NOISY, "%s queue req %p, len %d buf %p\n",
 663                _ep->name, _req, _req->length, _req->buf);
 664
 665        local_irq_save(flags);
 666
 667        _req->status = -EINPROGRESS;
 668        _req->actual = 0;
 669
 670        /* kickstart this i/o queue? */
 671        if (list_empty(&ep->queue) && !ep->stopped) {
 672                if (ep->ep.desc == NULL/* ep0 */) {
 673                        unsigned        length = _req->length;
 674
 675                        switch (dev->ep0state) {
 676                        case EP0_IN_DATA_PHASE:
 677                                dev->stats.write.ops++;
 678                                if (write_ep0_fifo(ep, req))
 679                                        req = NULL;
 680                                break;
 681
 682                        case EP0_OUT_DATA_PHASE:
 683                                dev->stats.read.ops++;
 684                                /* messy ... */
 685                                if (dev->req_config) {
 686                                        DBG(DBG_VERBOSE, "ep0 config ack%s\n",
 687                                                dev->has_cfr ?  "" : " raced");
 688                                        if (dev->has_cfr)
 689                                                UDCCFR = UDCCFR_AREN|UDCCFR_ACM
 690                                                        |UDCCFR_MB1;
 691                                        done(ep, req, 0);
 692                                        dev->ep0state = EP0_END_XFER;
 693                                        local_irq_restore (flags);
 694                                        return 0;
 695                                }
 696                                if (dev->req_pending)
 697                                        ep0start(dev, UDCCS0_IPR, "OUT");
 698                                if (length == 0 || ((UDCCS0 & UDCCS0_RNE) != 0
 699                                                && read_ep0_fifo(ep, req))) {
 700                                        ep0_idle(dev);
 701                                        done(ep, req, 0);
 702                                        req = NULL;
 703                                }
 704                                break;
 705
 706                        default:
 707                                DMSG("ep0 i/o, odd state %d\n", dev->ep0state);
 708                                local_irq_restore (flags);
 709                                return -EL2HLT;
 710                        }
 711                /* can the FIFO can satisfy the request immediately? */
 712                } else if ((ep->bEndpointAddress & USB_DIR_IN) != 0) {
 713                        if ((*ep->reg_udccs & UDCCS_BI_TFS) != 0
 714                                        && write_fifo(ep, req))
 715                                req = NULL;
 716                } else if ((*ep->reg_udccs & UDCCS_BO_RFS) != 0
 717                                && read_fifo(ep, req)) {
 718                        req = NULL;
 719                }
 720
 721                if (likely(req && ep->ep.desc))
 722                        pio_irq_enable(ep->bEndpointAddress);
 723        }
 724
 725        /* pio or dma irq handler advances the queue. */
 726        if (likely(req != NULL))
 727                list_add_tail(&req->queue, &ep->queue);
 728        local_irq_restore(flags);
 729
 730        return 0;
 731}
 732
 733
 734/*
 735 *      nuke - dequeue ALL requests
 736 */
 737static void nuke(struct pxa25x_ep *ep, int status)
 738{
 739        struct pxa25x_request *req;
 740
 741        /* called with irqs blocked */
 742        while (!list_empty(&ep->queue)) {
 743                req = list_entry(ep->queue.next,
 744                                struct pxa25x_request,
 745                                queue);
 746                done(ep, req, status);
 747        }
 748        if (ep->ep.desc)
 749                pio_irq_disable (ep->bEndpointAddress);
 750}
 751
 752
 753/* dequeue JUST ONE request */
 754static int pxa25x_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
 755{
 756        struct pxa25x_ep        *ep;
 757        struct pxa25x_request   *req;
 758        unsigned long           flags;
 759
 760        ep = container_of(_ep, struct pxa25x_ep, ep);
 761        if (!_ep || ep->ep.name == ep0name)
 762                return -EINVAL;
 763
 764        local_irq_save(flags);
 765
 766        /* make sure it's actually queued on this endpoint */
 767        list_for_each_entry (req, &ep->queue, queue) {
 768                if (&req->req == _req)
 769                        break;
 770        }
 771        if (&req->req != _req) {
 772                local_irq_restore(flags);
 773                return -EINVAL;
 774        }
 775
 776        done(ep, req, -ECONNRESET);
 777
 778        local_irq_restore(flags);
 779        return 0;
 780}
 781
 782/*-------------------------------------------------------------------------*/
 783
 784static int pxa25x_ep_set_halt(struct usb_ep *_ep, int value)
 785{
 786        struct pxa25x_ep        *ep;
 787        unsigned long           flags;
 788
 789        ep = container_of(_ep, struct pxa25x_ep, ep);
 790        if (unlikely (!_ep
 791                        || (!ep->ep.desc && ep->ep.name != ep0name))
 792                        || ep->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
 793                DMSG("%s, bad ep\n", __func__);
 794                return -EINVAL;
 795        }
 796        if (value == 0) {
 797                /* this path (reset toggle+halt) is needed to implement
 798                 * SET_INTERFACE on normal hardware.  but it can't be
 799                 * done from software on the PXA UDC, and the hardware
 800                 * forgets to do it as part of SET_INTERFACE automagic.
 801                 */
 802                DMSG("only host can clear %s halt\n", _ep->name);
 803                return -EROFS;
 804        }
 805
 806        local_irq_save(flags);
 807
 808        if ((ep->bEndpointAddress & USB_DIR_IN) != 0
 809                        && ((*ep->reg_udccs & UDCCS_BI_TFS) == 0
 810                           || !list_empty(&ep->queue))) {
 811                local_irq_restore(flags);
 812                return -EAGAIN;
 813        }
 814
 815        /* FST bit is the same for control, bulk in, bulk out, interrupt in */
 816        *ep->reg_udccs = UDCCS_BI_FST|UDCCS_BI_FTF;
 817
 818        /* ep0 needs special care */
 819        if (!ep->ep.desc) {
 820                start_watchdog(ep->dev);
 821                ep->dev->req_pending = 0;
 822                ep->dev->ep0state = EP0_STALL;
 823
 824        /* and bulk/intr endpoints like dropping stalls too */
 825        } else {
 826                unsigned i;
 827                for (i = 0; i < 1000; i += 20) {
 828                        if (*ep->reg_udccs & UDCCS_BI_SST)
 829                                break;
 830                        udelay(20);
 831                }
 832        }
 833        local_irq_restore(flags);
 834
 835        DBG(DBG_VERBOSE, "%s halt\n", _ep->name);
 836        return 0;
 837}
 838
 839static int pxa25x_ep_fifo_status(struct usb_ep *_ep)
 840{
 841        struct pxa25x_ep        *ep;
 842
 843        ep = container_of(_ep, struct pxa25x_ep, ep);
 844        if (!_ep) {
 845                DMSG("%s, bad ep\n", __func__);
 846                return -ENODEV;
 847        }
 848        /* pxa can't report unclaimed bytes from IN fifos */
 849        if ((ep->bEndpointAddress & USB_DIR_IN) != 0)
 850                return -EOPNOTSUPP;
 851        if (ep->dev->gadget.speed == USB_SPEED_UNKNOWN
 852                        || (*ep->reg_udccs & UDCCS_BO_RFS) == 0)
 853                return 0;
 854        else
 855                return (*ep->reg_ubcr & 0xfff) + 1;
 856}
 857
 858static void pxa25x_ep_fifo_flush(struct usb_ep *_ep)
 859{
 860        struct pxa25x_ep        *ep;
 861
 862        ep = container_of(_ep, struct pxa25x_ep, ep);
 863        if (!_ep || ep->ep.name == ep0name || !list_empty(&ep->queue)) {
 864                DMSG("%s, bad ep\n", __func__);
 865                return;
 866        }
 867
 868        /* toggle and halt bits stay unchanged */
 869
 870        /* for OUT, just read and discard the FIFO contents. */
 871        if ((ep->bEndpointAddress & USB_DIR_IN) == 0) {
 872                while (((*ep->reg_udccs) & UDCCS_BO_RNE) != 0)
 873                        (void) *ep->reg_uddr;
 874                return;
 875        }
 876
 877        /* most IN status is the same, but ISO can't stall */
 878        *ep->reg_udccs = UDCCS_BI_TPC|UDCCS_BI_FTF|UDCCS_BI_TUR
 879                | (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
 880                        ? 0 : UDCCS_BI_SST);
 881}
 882
 883
 884static struct usb_ep_ops pxa25x_ep_ops = {
 885        .enable         = pxa25x_ep_enable,
 886        .disable        = pxa25x_ep_disable,
 887
 888        .alloc_request  = pxa25x_ep_alloc_request,
 889        .free_request   = pxa25x_ep_free_request,
 890
 891        .queue          = pxa25x_ep_queue,
 892        .dequeue        = pxa25x_ep_dequeue,
 893
 894        .set_halt       = pxa25x_ep_set_halt,
 895        .fifo_status    = pxa25x_ep_fifo_status,
 896        .fifo_flush     = pxa25x_ep_fifo_flush,
 897};
 898
 899
 900/* ---------------------------------------------------------------------------
 901 *      device-scoped parts of the api to the usb controller hardware
 902 * ---------------------------------------------------------------------------
 903 */
 904
 905static int pxa25x_udc_get_frame(struct usb_gadget *_gadget)
 906{
 907        return ((UFNRH & 0x07) << 8) | (UFNRL & 0xff);
 908}
 909
 910static int pxa25x_udc_wakeup(struct usb_gadget *_gadget)
 911{
 912        /* host may not have enabled remote wakeup */
 913        if ((UDCCS0 & UDCCS0_DRWF) == 0)
 914                return -EHOSTUNREACH;
 915        udc_set_mask_UDCCR(UDCCR_RSM);
 916        return 0;
 917}
 918
 919static void stop_activity(struct pxa25x_udc *, struct usb_gadget_driver *);
 920static void udc_enable (struct pxa25x_udc *);
 921static void udc_disable(struct pxa25x_udc *);
 922
 923/* We disable the UDC -- and its 48 MHz clock -- whenever it's not
 924 * in active use.
 925 */
 926static int pullup(struct pxa25x_udc *udc)
 927{
 928        int is_active = udc->vbus && udc->pullup && !udc->suspended;
 929        DMSG("%s\n", is_active ? "active" : "inactive");
 930        if (is_active) {
 931                if (!udc->active) {
 932                        udc->active = 1;
 933                        /* Enable clock for USB device */
 934                        clk_enable(udc->clk);
 935                        udc_enable(udc);
 936                }
 937        } else {
 938                if (udc->active) {
 939                        if (udc->gadget.speed != USB_SPEED_UNKNOWN) {
 940                                DMSG("disconnect %s\n", udc->driver
 941                                        ? udc->driver->driver.name
 942                                        : "(no driver)");
 943                                stop_activity(udc, udc->driver);
 944                        }
 945                        udc_disable(udc);
 946                        /* Disable clock for USB device */
 947                        clk_disable(udc->clk);
 948                        udc->active = 0;
 949                }
 950
 951        }
 952        return 0;
 953}
 954
 955/* VBUS reporting logically comes from a transceiver */
 956static int pxa25x_udc_vbus_session(struct usb_gadget *_gadget, int is_active)
 957{
 958        struct pxa25x_udc       *udc;
 959
 960        udc = container_of(_gadget, struct pxa25x_udc, gadget);
 961        udc->vbus = is_active;
 962        DMSG("vbus %s\n", is_active ? "supplied" : "inactive");
 963        pullup(udc);
 964        return 0;
 965}
 966
 967/* drivers may have software control over D+ pullup */
 968static int pxa25x_udc_pullup(struct usb_gadget *_gadget, int is_active)
 969{
 970        struct pxa25x_udc       *udc;
 971
 972        udc = container_of(_gadget, struct pxa25x_udc, gadget);
 973
 974        /* not all boards support pullup control */
 975        if (!gpio_is_valid(udc->mach->gpio_pullup) && !udc->mach->udc_command)
 976                return -EOPNOTSUPP;
 977
 978        udc->pullup = (is_active != 0);
 979        pullup(udc);
 980        return 0;
 981}
 982
 983/* boards may consume current from VBUS, up to 100-500mA based on config.
 984 * the 500uA suspend ceiling means that exclusively vbus-powered PXA designs
 985 * violate USB specs.
 986 */
 987static int pxa25x_udc_vbus_draw(struct usb_gadget *_gadget, unsigned mA)
 988{
 989        struct pxa25x_udc       *udc;
 990
 991        udc = container_of(_gadget, struct pxa25x_udc, gadget);
 992
 993        if (!IS_ERR_OR_NULL(udc->transceiver))
 994                return usb_phy_set_power(udc->transceiver, mA);
 995        return -EOPNOTSUPP;
 996}
 997
 998static int pxa25x_udc_start(struct usb_gadget *g,
 999                struct usb_gadget_driver *driver);
1000static int pxa25x_udc_stop(struct usb_gadget *g,
1001                struct usb_gadget_driver *driver);
1002
1003static const struct usb_gadget_ops pxa25x_udc_ops = {
1004        .get_frame      = pxa25x_udc_get_frame,
1005        .wakeup         = pxa25x_udc_wakeup,
1006        .vbus_session   = pxa25x_udc_vbus_session,
1007        .pullup         = pxa25x_udc_pullup,
1008        .vbus_draw      = pxa25x_udc_vbus_draw,
1009        .udc_start      = pxa25x_udc_start,
1010        .udc_stop       = pxa25x_udc_stop,
1011};
1012
1013/*-------------------------------------------------------------------------*/
1014
1015#ifdef CONFIG_USB_GADGET_DEBUG_FS
1016
1017static int
1018udc_seq_show(struct seq_file *m, void *_d)
1019{
1020        struct pxa25x_udc       *dev = m->private;
1021        unsigned long           flags;
1022        int                     i;
1023        u32                     tmp;
1024
1025        local_irq_save(flags);
1026
1027        /* basic device status */
1028        seq_printf(m, DRIVER_DESC "\n"
1029                "%s version: %s\nGadget driver: %s\nHost %s\n\n",
1030                driver_name, DRIVER_VERSION SIZE_STR "(pio)",
1031                dev->driver ? dev->driver->driver.name : "(none)",
1032                dev->gadget.speed == USB_SPEED_FULL ? "full speed" : "disconnected");
1033
1034        /* registers for device and ep0 */
1035        seq_printf(m,
1036                "uicr %02X.%02X, usir %02X.%02x, ufnr %02X.%02X\n",
1037                UICR1, UICR0, USIR1, USIR0, UFNRH, UFNRL);
1038
1039        tmp = UDCCR;
1040        seq_printf(m,
1041                "udccr %02X =%s%s%s%s%s%s%s%s\n", tmp,
1042                (tmp & UDCCR_REM) ? " rem" : "",
1043                (tmp & UDCCR_RSTIR) ? " rstir" : "",
1044                (tmp & UDCCR_SRM) ? " srm" : "",
1045                (tmp & UDCCR_SUSIR) ? " susir" : "",
1046                (tmp & UDCCR_RESIR) ? " resir" : "",
1047                (tmp & UDCCR_RSM) ? " rsm" : "",
1048                (tmp & UDCCR_UDA) ? " uda" : "",
1049                (tmp & UDCCR_UDE) ? " ude" : "");
1050
1051        tmp = UDCCS0;
1052        seq_printf(m,
1053                "udccs0 %02X =%s%s%s%s%s%s%s%s\n", tmp,
1054                (tmp & UDCCS0_SA) ? " sa" : "",
1055                (tmp & UDCCS0_RNE) ? " rne" : "",
1056                (tmp & UDCCS0_FST) ? " fst" : "",
1057                (tmp & UDCCS0_SST) ? " sst" : "",
1058                (tmp & UDCCS0_DRWF) ? " dwrf" : "",
1059                (tmp & UDCCS0_FTF) ? " ftf" : "",
1060                (tmp & UDCCS0_IPR) ? " ipr" : "",
1061                (tmp & UDCCS0_OPR) ? " opr" : "");
1062
1063        if (dev->has_cfr) {
1064                tmp = UDCCFR;
1065                seq_printf(m,
1066                        "udccfr %02X =%s%s\n", tmp,
1067                        (tmp & UDCCFR_AREN) ? " aren" : "",
1068                        (tmp & UDCCFR_ACM) ? " acm" : "");
1069        }
1070
1071        if (dev->gadget.speed != USB_SPEED_FULL || !dev->driver)
1072                goto done;
1073
1074        seq_printf(m, "ep0 IN %lu/%lu, OUT %lu/%lu\nirqs %lu\n\n",
1075                dev->stats.write.bytes, dev->stats.write.ops,
1076                dev->stats.read.bytes, dev->stats.read.ops,
1077                dev->stats.irqs);
1078
1079        /* dump endpoint queues */
1080        for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
1081                struct pxa25x_ep        *ep = &dev->ep [i];
1082                struct pxa25x_request   *req;
1083
1084                if (i != 0) {
1085                        const struct usb_endpoint_descriptor    *desc;
1086
1087                        desc = ep->ep.desc;
1088                        if (!desc)
1089                                continue;
1090                        tmp = *dev->ep [i].reg_udccs;
1091                        seq_printf(m,
1092                                "%s max %d %s udccs %02x irqs %lu\n",
1093                                ep->ep.name, usb_endpoint_maxp(desc),
1094                                "pio", tmp, ep->pio_irqs);
1095                        /* TODO translate all five groups of udccs bits! */
1096
1097                } else /* ep0 should only have one transfer queued */
1098                        seq_printf(m, "ep0 max 16 pio irqs %lu\n",
1099                                ep->pio_irqs);
1100
1101                if (list_empty(&ep->queue)) {
1102                        seq_printf(m, "\t(nothing queued)\n");
1103                        continue;
1104                }
1105                list_for_each_entry(req, &ep->queue, queue) {
1106                        seq_printf(m,
1107                                        "\treq %p len %d/%d buf %p\n",
1108                                        &req->req, req->req.actual,
1109                                        req->req.length, req->req.buf);
1110                }
1111        }
1112
1113done:
1114        local_irq_restore(flags);
1115        return 0;
1116}
1117
1118static int
1119udc_debugfs_open(struct inode *inode, struct file *file)
1120{
1121        return single_open(file, udc_seq_show, inode->i_private);
1122}
1123
1124static const struct file_operations debug_fops = {
1125        .open           = udc_debugfs_open,
1126        .read           = seq_read,
1127        .llseek         = seq_lseek,
1128        .release        = single_release,
1129        .owner          = THIS_MODULE,
1130};
1131
1132#define create_debug_files(dev) \
1133        do { \
1134                dev->debugfs_udc = debugfs_create_file(dev->gadget.name, \
1135                        S_IRUGO, NULL, dev, &debug_fops); \
1136        } while (0)
1137#define remove_debug_files(dev) \
1138        do { \
1139                if (dev->debugfs_udc) \
1140                        debugfs_remove(dev->debugfs_udc); \
1141        } while (0)
1142
1143#else   /* !CONFIG_USB_GADGET_DEBUG_FILES */
1144
1145#define create_debug_files(dev) do {} while (0)
1146#define remove_debug_files(dev) do {} while (0)
1147
1148#endif  /* CONFIG_USB_GADGET_DEBUG_FILES */
1149
1150/*-------------------------------------------------------------------------*/
1151
1152/*
1153 *      udc_disable - disable USB device controller
1154 */
1155static void udc_disable(struct pxa25x_udc *dev)
1156{
1157        /* block all irqs */
1158        udc_set_mask_UDCCR(UDCCR_SRM|UDCCR_REM);
1159        UICR0 = UICR1 = 0xff;
1160        UFNRH = UFNRH_SIM;
1161
1162        /* if hardware supports it, disconnect from usb */
1163        pullup_off();
1164
1165        udc_clear_mask_UDCCR(UDCCR_UDE);
1166
1167        ep0_idle (dev);
1168        dev->gadget.speed = USB_SPEED_UNKNOWN;
1169}
1170
1171
1172/*
1173 *      udc_reinit - initialize software state
1174 */
1175static void udc_reinit(struct pxa25x_udc *dev)
1176{
1177        u32     i;
1178
1179        /* device/ep0 records init */
1180        INIT_LIST_HEAD (&dev->gadget.ep_list);
1181        INIT_LIST_HEAD (&dev->gadget.ep0->ep_list);
1182        dev->ep0state = EP0_IDLE;
1183
1184        /* basic endpoint records init */
1185        for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
1186                struct pxa25x_ep *ep = &dev->ep[i];
1187
1188                if (i != 0)
1189                        list_add_tail (&ep->ep.ep_list, &dev->gadget.ep_list);
1190
1191                ep->ep.desc = NULL;
1192                ep->stopped = 0;
1193                INIT_LIST_HEAD (&ep->queue);
1194                ep->pio_irqs = 0;
1195        }
1196
1197        /* the rest was statically initialized, and is read-only */
1198}
1199
1200/* until it's enabled, this UDC should be completely invisible
1201 * to any USB host.
1202 */
1203static void udc_enable (struct pxa25x_udc *dev)
1204{
1205        udc_clear_mask_UDCCR(UDCCR_UDE);
1206
1207        /* try to clear these bits before we enable the udc */
1208        udc_ack_int_UDCCR(UDCCR_SUSIR|/*UDCCR_RSTIR|*/UDCCR_RESIR);
1209
1210        ep0_idle(dev);
1211        dev->gadget.speed = USB_SPEED_UNKNOWN;
1212        dev->stats.irqs = 0;
1213
1214        /*
1215         * sequence taken from chapter 12.5.10, PXA250 AppProcDevManual:
1216         * - enable UDC
1217         * - if RESET is already in progress, ack interrupt
1218         * - unmask reset interrupt
1219         */
1220        udc_set_mask_UDCCR(UDCCR_UDE);
1221        if (!(UDCCR & UDCCR_UDA))
1222                udc_ack_int_UDCCR(UDCCR_RSTIR);
1223
1224        if (dev->has_cfr /* UDC_RES2 is defined */) {
1225                /* pxa255 (a0+) can avoid a set_config race that could
1226                 * prevent gadget drivers from configuring correctly
1227                 */
1228                UDCCFR = UDCCFR_ACM | UDCCFR_MB1;
1229        } else {
1230                /* "USB test mode" for pxa250 errata 40-42 (stepping a0, a1)
1231                 * which could result in missing packets and interrupts.
1232                 * supposedly one bit per endpoint, controlling whether it
1233                 * double buffers or not; ACM/AREN bits fit into the holes.
1234                 * zero bits (like USIR0_IRx) disable double buffering.
1235                 */
1236                UDC_RES1 = 0x00;
1237                UDC_RES2 = 0x00;
1238        }
1239
1240        /* enable suspend/resume and reset irqs */
1241        udc_clear_mask_UDCCR(UDCCR_SRM | UDCCR_REM);
1242
1243        /* enable ep0 irqs */
1244        UICR0 &= ~UICR0_IM0;
1245
1246        /* if hardware supports it, pullup D+ and wait for reset */
1247        pullup_on();
1248}
1249
1250
1251/* when a driver is successfully registered, it will receive
1252 * control requests including set_configuration(), which enables
1253 * non-control requests.  then usb traffic follows until a
1254 * disconnect is reported.  then a host may connect again, or
1255 * the driver might get unbound.
1256 */
1257static int pxa25x_udc_start(struct usb_gadget *g,
1258                struct usb_gadget_driver *driver)
1259{
1260        struct pxa25x_udc       *dev = to_pxa25x(g);
1261        int                     retval;
1262
1263        /* first hook up the driver ... */
1264        dev->driver = driver;
1265        dev->pullup = 1;
1266
1267        /* ... then enable host detection and ep0; and we're ready
1268         * for set_configuration as well as eventual disconnect.
1269         */
1270        /* connect to bus through transceiver */
1271        if (!IS_ERR_OR_NULL(dev->transceiver)) {
1272                retval = otg_set_peripheral(dev->transceiver->otg,
1273                                                &dev->gadget);
1274                if (retval)
1275                        goto bind_fail;
1276        }
1277
1278        pullup(dev);
1279        dump_state(dev);
1280        return 0;
1281bind_fail:
1282        return retval;
1283}
1284
1285static void
1286stop_activity(struct pxa25x_udc *dev, struct usb_gadget_driver *driver)
1287{
1288        int i;
1289
1290        /* don't disconnect drivers more than once */
1291        if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1292                driver = NULL;
1293        dev->gadget.speed = USB_SPEED_UNKNOWN;
1294
1295        /* prevent new request submissions, kill any outstanding requests  */
1296        for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
1297                struct pxa25x_ep *ep = &dev->ep[i];
1298
1299                ep->stopped = 1;
1300                nuke(ep, -ESHUTDOWN);
1301        }
1302        del_timer_sync(&dev->timer);
1303
1304        /* report disconnect; the driver is already quiesced */
1305        if (driver)
1306                driver->disconnect(&dev->gadget);
1307
1308        /* re-init driver-visible data structures */
1309        udc_reinit(dev);
1310}
1311
1312static int pxa25x_udc_stop(struct usb_gadget*g,
1313                struct usb_gadget_driver *driver)
1314{
1315        struct pxa25x_udc       *dev = to_pxa25x(g);
1316
1317        local_irq_disable();
1318        dev->pullup = 0;
1319        pullup(dev);
1320        stop_activity(dev, driver);
1321        local_irq_enable();
1322
1323        if (!IS_ERR_OR_NULL(dev->transceiver))
1324                (void) otg_set_peripheral(dev->transceiver->otg, NULL);
1325
1326        dev->driver = NULL;
1327
1328        dump_state(dev);
1329
1330        return 0;
1331}
1332
1333/*-------------------------------------------------------------------------*/
1334
1335#ifdef CONFIG_ARCH_LUBBOCK
1336
1337/* Lubbock has separate connect and disconnect irqs.  More typical designs
1338 * use one GPIO as the VBUS IRQ, and another to control the D+ pullup.
1339 */
1340
1341static irqreturn_t
1342lubbock_vbus_irq(int irq, void *_dev)
1343{
1344        struct pxa25x_udc       *dev = _dev;
1345        int                     vbus;
1346
1347        dev->stats.irqs++;
1348        switch (irq) {
1349        case LUBBOCK_USB_IRQ:
1350                vbus = 1;
1351                disable_irq(LUBBOCK_USB_IRQ);
1352                enable_irq(LUBBOCK_USB_DISC_IRQ);
1353                break;
1354        case LUBBOCK_USB_DISC_IRQ:
1355                vbus = 0;
1356                disable_irq(LUBBOCK_USB_DISC_IRQ);
1357                enable_irq(LUBBOCK_USB_IRQ);
1358                break;
1359        default:
1360                return IRQ_NONE;
1361        }
1362
1363        pxa25x_udc_vbus_session(&dev->gadget, vbus);
1364        return IRQ_HANDLED;
1365}
1366
1367#endif
1368
1369
1370/*-------------------------------------------------------------------------*/
1371
1372static inline void clear_ep_state (struct pxa25x_udc *dev)
1373{
1374        unsigned i;
1375
1376        /* hardware SET_{CONFIGURATION,INTERFACE} automagic resets endpoint
1377         * fifos, and pending transactions mustn't be continued in any case.
1378         */
1379        for (i = 1; i < PXA_UDC_NUM_ENDPOINTS; i++)
1380                nuke(&dev->ep[i], -ECONNABORTED);
1381}
1382
1383static void udc_watchdog(unsigned long _dev)
1384{
1385        struct pxa25x_udc       *dev = (void *)_dev;
1386
1387        local_irq_disable();
1388        if (dev->ep0state == EP0_STALL
1389                        && (UDCCS0 & UDCCS0_FST) == 0
1390                        && (UDCCS0 & UDCCS0_SST) == 0) {
1391                UDCCS0 = UDCCS0_FST|UDCCS0_FTF;
1392                DBG(DBG_VERBOSE, "ep0 re-stall\n");
1393                start_watchdog(dev);
1394        }
1395        local_irq_enable();
1396}
1397
1398static void handle_ep0 (struct pxa25x_udc *dev)
1399{
1400        u32                     udccs0 = UDCCS0;
1401        struct pxa25x_ep        *ep = &dev->ep [0];
1402        struct pxa25x_request   *req;
1403        union {
1404                struct usb_ctrlrequest  r;
1405                u8                      raw [8];
1406                u32                     word [2];
1407        } u;
1408
1409        if (list_empty(&ep->queue))
1410                req = NULL;
1411        else
1412                req = list_entry(ep->queue.next, struct pxa25x_request, queue);
1413
1414        /* clear stall status */
1415        if (udccs0 & UDCCS0_SST) {
1416                nuke(ep, -EPIPE);
1417                UDCCS0 = UDCCS0_SST;
1418                del_timer(&dev->timer);
1419                ep0_idle(dev);
1420        }
1421
1422        /* previous request unfinished?  non-error iff back-to-back ... */
1423        if ((udccs0 & UDCCS0_SA) != 0 && dev->ep0state != EP0_IDLE) {
1424                nuke(ep, 0);
1425                del_timer(&dev->timer);
1426                ep0_idle(dev);
1427        }
1428
1429        switch (dev->ep0state) {
1430        case EP0_IDLE:
1431                /* late-breaking status? */
1432                udccs0 = UDCCS0;
1433
1434                /* start control request? */
1435                if (likely((udccs0 & (UDCCS0_OPR|UDCCS0_SA|UDCCS0_RNE))
1436                                == (UDCCS0_OPR|UDCCS0_SA|UDCCS0_RNE))) {
1437                        int i;
1438
1439                        nuke (ep, -EPROTO);
1440
1441                        /* read SETUP packet */
1442                        for (i = 0; i < 8; i++) {
1443                                if (unlikely(!(UDCCS0 & UDCCS0_RNE))) {
1444bad_setup:
1445                                        DMSG("SETUP %d!\n", i);
1446                                        goto stall;
1447                                }
1448                                u.raw [i] = (u8) UDDR0;
1449                        }
1450                        if (unlikely((UDCCS0 & UDCCS0_RNE) != 0))
1451                                goto bad_setup;
1452
1453got_setup:
1454                        DBG(DBG_VERBOSE, "SETUP %02x.%02x v%04x i%04x l%04x\n",
1455                                u.r.bRequestType, u.r.bRequest,
1456                                le16_to_cpu(u.r.wValue),
1457                                le16_to_cpu(u.r.wIndex),
1458                                le16_to_cpu(u.r.wLength));
1459
1460                        /* cope with automagic for some standard requests. */
1461                        dev->req_std = (u.r.bRequestType & USB_TYPE_MASK)
1462                                                == USB_TYPE_STANDARD;
1463                        dev->req_config = 0;
1464                        dev->req_pending = 1;
1465                        switch (u.r.bRequest) {
1466                        /* hardware restricts gadget drivers here! */
1467                        case USB_REQ_SET_CONFIGURATION:
1468                                if (u.r.bRequestType == USB_RECIP_DEVICE) {
1469                                        /* reflect hardware's automagic
1470                                         * up to the gadget driver.
1471                                         */
1472config_change:
1473                                        dev->req_config = 1;
1474                                        clear_ep_state(dev);
1475                                        /* if !has_cfr, there's no synch
1476                                         * else use AREN (later) not SA|OPR
1477                                         * USIR0_IR0 acts edge sensitive
1478                                         */
1479                                }
1480                                break;
1481                        /* ... and here, even more ... */
1482                        case USB_REQ_SET_INTERFACE:
1483                                if (u.r.bRequestType == USB_RECIP_INTERFACE) {
1484                                        /* udc hardware is broken by design:
1485                                         *  - altsetting may only be zero;
1486                                         *  - hw resets all interfaces' eps;
1487                                         *  - ep reset doesn't include halt(?).
1488                                         */
1489                                        DMSG("broken set_interface (%d/%d)\n",
1490                                                le16_to_cpu(u.r.wIndex),
1491                                                le16_to_cpu(u.r.wValue));
1492                                        goto config_change;
1493                                }
1494                                break;
1495                        /* hardware was supposed to hide this */
1496                        case USB_REQ_SET_ADDRESS:
1497                                if (u.r.bRequestType == USB_RECIP_DEVICE) {
1498                                        ep0start(dev, 0, "address");
1499                                        return;
1500                                }
1501                                break;
1502                        }
1503
1504                        if (u.r.bRequestType & USB_DIR_IN)
1505                                dev->ep0state = EP0_IN_DATA_PHASE;
1506                        else
1507                                dev->ep0state = EP0_OUT_DATA_PHASE;
1508
1509                        i = dev->driver->setup(&dev->gadget, &u.r);
1510                        if (i < 0) {
1511                                /* hardware automagic preventing STALL... */
1512                                if (dev->req_config) {
1513                                        /* hardware sometimes neglects to tell
1514                                         * tell us about config change events,
1515                                         * so later ones may fail...
1516                                         */
1517                                        WARNING("config change %02x fail %d?\n",
1518                                                u.r.bRequest, i);
1519                                        return;
1520                                        /* TODO experiment:  if has_cfr,
1521                                         * hardware didn't ACK; maybe we
1522                                         * could actually STALL!
1523                                         */
1524                                }
1525                                DBG(DBG_VERBOSE, "protocol STALL, "
1526                                        "%02x err %d\n", UDCCS0, i);
1527stall:
1528                                /* the watchdog timer helps deal with cases
1529                                 * where udc seems to clear FST wrongly, and
1530                                 * then NAKs instead of STALLing.
1531                                 */
1532                                ep0start(dev, UDCCS0_FST|UDCCS0_FTF, "stall");
1533                                start_watchdog(dev);
1534                                dev->ep0state = EP0_STALL;
1535
1536                        /* deferred i/o == no response yet */
1537                        } else if (dev->req_pending) {
1538                                if (likely(dev->ep0state == EP0_IN_DATA_PHASE
1539                                                || dev->req_std || u.r.wLength))
1540                                        ep0start(dev, 0, "defer");
1541                                else
1542                                        ep0start(dev, UDCCS0_IPR, "defer/IPR");
1543                        }
1544
1545                        /* expect at least one data or status stage irq */
1546                        return;
1547
1548                } else if (likely((udccs0 & (UDCCS0_OPR|UDCCS0_SA))
1549                                == (UDCCS0_OPR|UDCCS0_SA))) {
1550                        unsigned i;
1551
1552                        /* pxa210/250 erratum 131 for B0/B1 says RNE lies.
1553                         * still observed on a pxa255 a0.
1554                         */
1555                        DBG(DBG_VERBOSE, "e131\n");
1556                        nuke(ep, -EPROTO);
1557
1558                        /* read SETUP data, but don't trust it too much */
1559                        for (i = 0; i < 8; i++)
1560                                u.raw [i] = (u8) UDDR0;
1561                        if ((u.r.bRequestType & USB_RECIP_MASK)
1562                                        > USB_RECIP_OTHER)
1563                                goto stall;
1564                        if (u.word [0] == 0 && u.word [1] == 0)
1565                                goto stall;
1566                        goto got_setup;
1567                } else {
1568                        /* some random early IRQ:
1569                         * - we acked FST
1570                         * - IPR cleared
1571                         * - OPR got set, without SA (likely status stage)
1572                         */
1573                        UDCCS0 = udccs0 & (UDCCS0_SA|UDCCS0_OPR);
1574                }
1575                break;
1576        case EP0_IN_DATA_PHASE:                 /* GET_DESCRIPTOR etc */
1577                if (udccs0 & UDCCS0_OPR) {
1578                        UDCCS0 = UDCCS0_OPR|UDCCS0_FTF;
1579                        DBG(DBG_VERBOSE, "ep0in premature status\n");
1580                        if (req)
1581                                done(ep, req, 0);
1582                        ep0_idle(dev);
1583                } else /* irq was IPR clearing */ {
1584                        if (req) {
1585                                /* this IN packet might finish the request */
1586                                (void) write_ep0_fifo(ep, req);
1587                        } /* else IN token before response was written */
1588                }
1589                break;
1590        case EP0_OUT_DATA_PHASE:                /* SET_DESCRIPTOR etc */
1591                if (udccs0 & UDCCS0_OPR) {
1592                        if (req) {
1593                                /* this OUT packet might finish the request */
1594                                if (read_ep0_fifo(ep, req))
1595                                        done(ep, req, 0);
1596                                /* else more OUT packets expected */
1597                        } /* else OUT token before read was issued */
1598                } else /* irq was IPR clearing */ {
1599                        DBG(DBG_VERBOSE, "ep0out premature status\n");
1600                        if (req)
1601                                done(ep, req, 0);
1602                        ep0_idle(dev);
1603                }
1604                break;
1605        case EP0_END_XFER:
1606                if (req)
1607                        done(ep, req, 0);
1608                /* ack control-IN status (maybe in-zlp was skipped)
1609                 * also appears after some config change events.
1610                 */
1611                if (udccs0 & UDCCS0_OPR)
1612                        UDCCS0 = UDCCS0_OPR;
1613                ep0_idle(dev);
1614                break;
1615        case EP0_STALL:
1616                UDCCS0 = UDCCS0_FST;
1617                break;
1618        }
1619        USIR0 = USIR0_IR0;
1620}
1621
1622static void handle_ep(struct pxa25x_ep *ep)
1623{
1624        struct pxa25x_request   *req;
1625        int                     is_in = ep->bEndpointAddress & USB_DIR_IN;
1626        int                     completed;
1627        u32                     udccs, tmp;
1628
1629        do {
1630                completed = 0;
1631                if (likely (!list_empty(&ep->queue)))
1632                        req = list_entry(ep->queue.next,
1633                                        struct pxa25x_request, queue);
1634                else
1635                        req = NULL;
1636
1637                // TODO check FST handling
1638
1639                udccs = *ep->reg_udccs;
1640                if (unlikely(is_in)) {  /* irq from TPC, SST, or (ISO) TUR */
1641                        tmp = UDCCS_BI_TUR;
1642                        if (likely(ep->bmAttributes == USB_ENDPOINT_XFER_BULK))
1643                                tmp |= UDCCS_BI_SST;
1644                        tmp &= udccs;
1645                        if (likely (tmp))
1646                                *ep->reg_udccs = tmp;
1647                        if (req && likely ((udccs & UDCCS_BI_TFS) != 0))
1648                                completed = write_fifo(ep, req);
1649
1650                } else {        /* irq from RPC (or for ISO, ROF) */
1651                        if (likely(ep->bmAttributes == USB_ENDPOINT_XFER_BULK))
1652                                tmp = UDCCS_BO_SST | UDCCS_BO_DME;
1653                        else
1654                                tmp = UDCCS_IO_ROF | UDCCS_IO_DME;
1655                        tmp &= udccs;
1656                        if (likely(tmp))
1657                                *ep->reg_udccs = tmp;
1658
1659                        /* fifos can hold packets, ready for reading... */
1660                        if (likely(req)) {
1661                                completed = read_fifo(ep, req);
1662                        } else
1663                                pio_irq_disable (ep->bEndpointAddress);
1664                }
1665                ep->pio_irqs++;
1666        } while (completed);
1667}
1668
1669/*
1670 *      pxa25x_udc_irq - interrupt handler
1671 *
1672 * avoid delays in ep0 processing. the control handshaking isn't always
1673 * under software control (pxa250c0 and the pxa255 are better), and delays
1674 * could cause usb protocol errors.
1675 */
1676static irqreturn_t
1677pxa25x_udc_irq(int irq, void *_dev)
1678{
1679        struct pxa25x_udc       *dev = _dev;
1680        int                     handled;
1681
1682        dev->stats.irqs++;
1683        do {
1684                u32             udccr = UDCCR;
1685
1686                handled = 0;
1687
1688                /* SUSpend Interrupt Request */
1689                if (unlikely(udccr & UDCCR_SUSIR)) {
1690                        udc_ack_int_UDCCR(UDCCR_SUSIR);
1691                        handled = 1;
1692                        DBG(DBG_VERBOSE, "USB suspend\n");
1693
1694                        if (dev->gadget.speed != USB_SPEED_UNKNOWN
1695                                        && dev->driver
1696                                        && dev->driver->suspend)
1697                                dev->driver->suspend(&dev->gadget);
1698                        ep0_idle (dev);
1699                }
1700
1701                /* RESume Interrupt Request */
1702                if (unlikely(udccr & UDCCR_RESIR)) {
1703                        udc_ack_int_UDCCR(UDCCR_RESIR);
1704                        handled = 1;
1705                        DBG(DBG_VERBOSE, "USB resume\n");
1706
1707                        if (dev->gadget.speed != USB_SPEED_UNKNOWN
1708                                        && dev->driver
1709                                        && dev->driver->resume)
1710                                dev->driver->resume(&dev->gadget);
1711                }
1712
1713                /* ReSeT Interrupt Request - USB reset */
1714                if (unlikely(udccr & UDCCR_RSTIR)) {
1715                        udc_ack_int_UDCCR(UDCCR_RSTIR);
1716                        handled = 1;
1717
1718                        if ((UDCCR & UDCCR_UDA) == 0) {
1719                                DBG(DBG_VERBOSE, "USB reset start\n");
1720
1721                                /* reset driver and endpoints,
1722                                 * in case that's not yet done
1723                                 */
1724                                stop_activity (dev, dev->driver);
1725
1726                        } else {
1727                                DBG(DBG_VERBOSE, "USB reset end\n");
1728                                dev->gadget.speed = USB_SPEED_FULL;
1729                                memset(&dev->stats, 0, sizeof dev->stats);
1730                                /* driver and endpoints are still reset */
1731                        }
1732
1733                } else {
1734                        u32     usir0 = USIR0 & ~UICR0;
1735                        u32     usir1 = USIR1 & ~UICR1;
1736                        int     i;
1737
1738                        if (unlikely (!usir0 && !usir1))
1739                                continue;
1740
1741                        DBG(DBG_VERY_NOISY, "irq %02x.%02x\n", usir1, usir0);
1742
1743                        /* control traffic */
1744                        if (usir0 & USIR0_IR0) {
1745                                dev->ep[0].pio_irqs++;
1746                                handle_ep0(dev);
1747                                handled = 1;
1748                        }
1749
1750                        /* endpoint data transfers */
1751                        for (i = 0; i < 8; i++) {
1752                                u32     tmp = 1 << i;
1753
1754                                if (i && (usir0 & tmp)) {
1755                                        handle_ep(&dev->ep[i]);
1756                                        USIR0 |= tmp;
1757                                        handled = 1;
1758                                }
1759#ifndef CONFIG_USB_PXA25X_SMALL
1760                                if (usir1 & tmp) {
1761                                        handle_ep(&dev->ep[i+8]);
1762                                        USIR1 |= tmp;
1763                                        handled = 1;
1764                                }
1765#endif
1766                        }
1767                }
1768
1769                /* we could also ask for 1 msec SOF (SIR) interrupts */
1770
1771        } while (handled);
1772        return IRQ_HANDLED;
1773}
1774
1775/*-------------------------------------------------------------------------*/
1776
1777static void nop_release (struct device *dev)
1778{
1779        DMSG("%s %s\n", __func__, dev_name(dev));
1780}
1781
1782/* this uses load-time allocation and initialization (instead of
1783 * doing it at run-time) to save code, eliminate fault paths, and
1784 * be more obviously correct.
1785 */
1786static struct pxa25x_udc memory = {
1787        .gadget = {
1788                .ops            = &pxa25x_udc_ops,
1789                .ep0            = &memory.ep[0].ep,
1790                .name           = driver_name,
1791                .dev = {
1792                        .init_name      = "gadget",
1793                        .release        = nop_release,
1794                },
1795        },
1796
1797        /* control endpoint */
1798        .ep[0] = {
1799                .ep = {
1800                        .name           = ep0name,
1801                        .ops            = &pxa25x_ep_ops,
1802                        .maxpacket      = EP0_FIFO_SIZE,
1803                },
1804                .dev            = &memory,
1805                .reg_udccs      = &UDCCS0,
1806                .reg_uddr       = &UDDR0,
1807        },
1808
1809        /* first group of endpoints */
1810        .ep[1] = {
1811                .ep = {
1812                        .name           = "ep1in-bulk",
1813                        .ops            = &pxa25x_ep_ops,
1814                        .maxpacket      = BULK_FIFO_SIZE,
1815                },
1816                .dev            = &memory,
1817                .fifo_size      = BULK_FIFO_SIZE,
1818                .bEndpointAddress = USB_DIR_IN | 1,
1819                .bmAttributes   = USB_ENDPOINT_XFER_BULK,
1820                .reg_udccs      = &UDCCS1,
1821                .reg_uddr       = &UDDR1,
1822        },
1823        .ep[2] = {
1824                .ep = {
1825                        .name           = "ep2out-bulk",
1826                        .ops            = &pxa25x_ep_ops,
1827                        .maxpacket      = BULK_FIFO_SIZE,
1828                },
1829                .dev            = &memory,
1830                .fifo_size      = BULK_FIFO_SIZE,
1831                .bEndpointAddress = 2,
1832                .bmAttributes   = USB_ENDPOINT_XFER_BULK,
1833                .reg_udccs      = &UDCCS2,
1834                .reg_ubcr       = &UBCR2,
1835                .reg_uddr       = &UDDR2,
1836        },
1837#ifndef CONFIG_USB_PXA25X_SMALL
1838        .ep[3] = {
1839                .ep = {
1840                        .name           = "ep3in-iso",
1841                        .ops            = &pxa25x_ep_ops,
1842                        .maxpacket      = ISO_FIFO_SIZE,
1843                },
1844                .dev            = &memory,
1845                .fifo_size      = ISO_FIFO_SIZE,
1846                .bEndpointAddress = USB_DIR_IN | 3,
1847                .bmAttributes   = USB_ENDPOINT_XFER_ISOC,
1848                .reg_udccs      = &UDCCS3,
1849                .reg_uddr       = &UDDR3,
1850        },
1851        .ep[4] = {
1852                .ep = {
1853                        .name           = "ep4out-iso",
1854                        .ops            = &pxa25x_ep_ops,
1855                        .maxpacket      = ISO_FIFO_SIZE,
1856                },
1857                .dev            = &memory,
1858                .fifo_size      = ISO_FIFO_SIZE,
1859                .bEndpointAddress = 4,
1860                .bmAttributes   = USB_ENDPOINT_XFER_ISOC,
1861                .reg_udccs      = &UDCCS4,
1862                .reg_ubcr       = &UBCR4,
1863                .reg_uddr       = &UDDR4,
1864        },
1865        .ep[5] = {
1866                .ep = {
1867                        .name           = "ep5in-int",
1868                        .ops            = &pxa25x_ep_ops,
1869                        .maxpacket      = INT_FIFO_SIZE,
1870                },
1871                .dev            = &memory,
1872                .fifo_size      = INT_FIFO_SIZE,
1873                .bEndpointAddress = USB_DIR_IN | 5,
1874                .bmAttributes   = USB_ENDPOINT_XFER_INT,
1875                .reg_udccs      = &UDCCS5,
1876                .reg_uddr       = &UDDR5,
1877        },
1878
1879        /* second group of endpoints */
1880        .ep[6] = {
1881                .ep = {
1882                        .name           = "ep6in-bulk",
1883                        .ops            = &pxa25x_ep_ops,
1884                        .maxpacket      = BULK_FIFO_SIZE,
1885                },
1886                .dev            = &memory,
1887                .fifo_size      = BULK_FIFO_SIZE,
1888                .bEndpointAddress = USB_DIR_IN | 6,
1889                .bmAttributes   = USB_ENDPOINT_XFER_BULK,
1890                .reg_udccs      = &UDCCS6,
1891                .reg_uddr       = &UDDR6,
1892        },
1893        .ep[7] = {
1894                .ep = {
1895                        .name           = "ep7out-bulk",
1896                        .ops            = &pxa25x_ep_ops,
1897                        .maxpacket      = BULK_FIFO_SIZE,
1898                },
1899                .dev            = &memory,
1900                .fifo_size      = BULK_FIFO_SIZE,
1901                .bEndpointAddress = 7,
1902                .bmAttributes   = USB_ENDPOINT_XFER_BULK,
1903                .reg_udccs      = &UDCCS7,
1904                .reg_ubcr       = &UBCR7,
1905                .reg_uddr       = &UDDR7,
1906        },
1907        .ep[8] = {
1908                .ep = {
1909                        .name           = "ep8in-iso",
1910                        .ops            = &pxa25x_ep_ops,
1911                        .maxpacket      = ISO_FIFO_SIZE,
1912                },
1913                .dev            = &memory,
1914                .fifo_size      = ISO_FIFO_SIZE,
1915                .bEndpointAddress = USB_DIR_IN | 8,
1916                .bmAttributes   = USB_ENDPOINT_XFER_ISOC,
1917                .reg_udccs      = &UDCCS8,
1918                .reg_uddr       = &UDDR8,
1919        },
1920        .ep[9] = {
1921                .ep = {
1922                        .name           = "ep9out-iso",
1923                        .ops            = &pxa25x_ep_ops,
1924                        .maxpacket      = ISO_FIFO_SIZE,
1925                },
1926                .dev            = &memory,
1927                .fifo_size      = ISO_FIFO_SIZE,
1928                .bEndpointAddress = 9,
1929                .bmAttributes   = USB_ENDPOINT_XFER_ISOC,
1930                .reg_udccs      = &UDCCS9,
1931                .reg_ubcr       = &UBCR9,
1932                .reg_uddr       = &UDDR9,
1933        },
1934        .ep[10] = {
1935                .ep = {
1936                        .name           = "ep10in-int",
1937                        .ops            = &pxa25x_ep_ops,
1938                        .maxpacket      = INT_FIFO_SIZE,
1939                },
1940                .dev            = &memory,
1941                .fifo_size      = INT_FIFO_SIZE,
1942                .bEndpointAddress = USB_DIR_IN | 10,
1943                .bmAttributes   = USB_ENDPOINT_XFER_INT,
1944                .reg_udccs      = &UDCCS10,
1945                .reg_uddr       = &UDDR10,
1946        },
1947
1948        /* third group of endpoints */
1949        .ep[11] = {
1950                .ep = {
1951                        .name           = "ep11in-bulk",
1952                        .ops            = &pxa25x_ep_ops,
1953                        .maxpacket      = BULK_FIFO_SIZE,
1954                },
1955                .dev            = &memory,
1956                .fifo_size      = BULK_FIFO_SIZE,
1957                .bEndpointAddress = USB_DIR_IN | 11,
1958                .bmAttributes   = USB_ENDPOINT_XFER_BULK,
1959                .reg_udccs      = &UDCCS11,
1960                .reg_uddr       = &UDDR11,
1961        },
1962        .ep[12] = {
1963                .ep = {
1964                        .name           = "ep12out-bulk",
1965                        .ops            = &pxa25x_ep_ops,
1966                        .maxpacket      = BULK_FIFO_SIZE,
1967                },
1968                .dev            = &memory,
1969                .fifo_size      = BULK_FIFO_SIZE,
1970                .bEndpointAddress = 12,
1971                .bmAttributes   = USB_ENDPOINT_XFER_BULK,
1972                .reg_udccs      = &UDCCS12,
1973                .reg_ubcr       = &UBCR12,
1974                .reg_uddr       = &UDDR12,
1975        },
1976        .ep[13] = {
1977                .ep = {
1978                        .name           = "ep13in-iso",
1979                        .ops            = &pxa25x_ep_ops,
1980                        .maxpacket      = ISO_FIFO_SIZE,
1981                },
1982                .dev            = &memory,
1983                .fifo_size      = ISO_FIFO_SIZE,
1984                .bEndpointAddress = USB_DIR_IN | 13,
1985                .bmAttributes   = USB_ENDPOINT_XFER_ISOC,
1986                .reg_udccs      = &UDCCS13,
1987                .reg_uddr       = &UDDR13,
1988        },
1989        .ep[14] = {
1990                .ep = {
1991                        .name           = "ep14out-iso",
1992                        .ops            = &pxa25x_ep_ops,
1993                        .maxpacket      = ISO_FIFO_SIZE,
1994                },
1995                .dev            = &memory,
1996                .fifo_size      = ISO_FIFO_SIZE,
1997                .bEndpointAddress = 14,
1998                .bmAttributes   = USB_ENDPOINT_XFER_ISOC,
1999                .reg_udccs      = &UDCCS14,
2000                .reg_ubcr       = &UBCR14,
2001                .reg_uddr       = &UDDR14,
2002        },
2003        .ep[15] = {
2004                .ep = {
2005                        .name           = "ep15in-int",
2006                        .ops            = &pxa25x_ep_ops,
2007                        .maxpacket      = INT_FIFO_SIZE,
2008                },
2009                .dev            = &memory,
2010                .fifo_size      = INT_FIFO_SIZE,
2011                .bEndpointAddress = USB_DIR_IN | 15,
2012                .bmAttributes   = USB_ENDPOINT_XFER_INT,
2013                .reg_udccs      = &UDCCS15,
2014                .reg_uddr       = &UDDR15,
2015        },
2016#endif /* !CONFIG_USB_PXA25X_SMALL */
2017};
2018
2019#define CP15R0_VENDOR_MASK      0xffffe000
2020
2021#if     defined(CONFIG_ARCH_PXA)
2022#define CP15R0_XSCALE_VALUE     0x69052000      /* intel/arm/xscale */
2023
2024#elif   defined(CONFIG_ARCH_IXP4XX)
2025#define CP15R0_XSCALE_VALUE     0x69054000      /* intel/arm/ixp4xx */
2026
2027#endif
2028
2029#define CP15R0_PROD_MASK        0x000003f0
2030#define PXA25x                  0x00000100      /* and PXA26x */
2031#define PXA210                  0x00000120
2032
2033#define CP15R0_REV_MASK         0x0000000f
2034
2035#define CP15R0_PRODREV_MASK     (CP15R0_PROD_MASK | CP15R0_REV_MASK)
2036
2037#define PXA255_A0               0x00000106      /* or PXA260_B1 */
2038#define PXA250_C0               0x00000105      /* or PXA26x_B0 */
2039#define PXA250_B2               0x00000104
2040#define PXA250_B1               0x00000103      /* or PXA260_A0 */
2041#define PXA250_B0               0x00000102
2042#define PXA250_A1               0x00000101
2043#define PXA250_A0               0x00000100
2044
2045#define PXA210_C0               0x00000125
2046#define PXA210_B2               0x00000124
2047#define PXA210_B1               0x00000123
2048#define PXA210_B0               0x00000122
2049#define IXP425_A0               0x000001c1
2050#define IXP425_B0               0x000001f1
2051#define IXP465_AD               0x00000200
2052
2053/*
2054 *      probe - binds to the platform device
2055 */
2056static int __init pxa25x_udc_probe(struct platform_device *pdev)
2057{
2058        struct pxa25x_udc *dev = &memory;
2059        int retval, irq;
2060        u32 chiprev;
2061
2062        pr_info("%s: version %s\n", driver_name, DRIVER_VERSION);
2063
2064        /* insist on Intel/ARM/XScale */
2065        asm("mrc%? p15, 0, %0, c0, c0" : "=r" (chiprev));
2066        if ((chiprev & CP15R0_VENDOR_MASK) != CP15R0_XSCALE_VALUE) {
2067                pr_err("%s: not XScale!\n", driver_name);
2068                return -ENODEV;
2069        }
2070
2071        /* trigger chiprev-specific logic */
2072        switch (chiprev & CP15R0_PRODREV_MASK) {
2073#if     defined(CONFIG_ARCH_PXA)
2074        case PXA255_A0:
2075                dev->has_cfr = 1;
2076                break;
2077        case PXA250_A0:
2078        case PXA250_A1:
2079                /* A0/A1 "not released"; ep 13, 15 unusable */
2080                /* fall through */
2081        case PXA250_B2: case PXA210_B2:
2082        case PXA250_B1: case PXA210_B1:
2083        case PXA250_B0: case PXA210_B0:
2084                /* OUT-DMA is broken ... */
2085                /* fall through */
2086        case PXA250_C0: case PXA210_C0:
2087                break;
2088#elif   defined(CONFIG_ARCH_IXP4XX)
2089        case IXP425_A0:
2090        case IXP425_B0:
2091        case IXP465_AD:
2092                dev->has_cfr = 1;
2093                break;
2094#endif
2095        default:
2096                pr_err("%s: unrecognized processor: %08x\n",
2097                        driver_name, chiprev);
2098                /* iop3xx, ixp4xx, ... */
2099                return -ENODEV;
2100        }
2101
2102        irq = platform_get_irq(pdev, 0);
2103        if (irq < 0)
2104                return -ENODEV;
2105
2106        dev->clk = clk_get(&pdev->dev, NULL);
2107        if (IS_ERR(dev->clk)) {
2108                retval = PTR_ERR(dev->clk);
2109                goto err_clk;
2110        }
2111
2112        pr_debug("%s: IRQ %d%s%s\n", driver_name, irq,
2113                dev->has_cfr ? "" : " (!cfr)",
2114                SIZE_STR "(pio)"
2115                );
2116
2117        /* other non-static parts of init */
2118        dev->dev = &pdev->dev;
2119        dev->mach = pdev->dev.platform_data;
2120
2121        dev->transceiver = usb_get_phy(USB_PHY_TYPE_USB2);
2122
2123        if (gpio_is_valid(dev->mach->gpio_pullup)) {
2124                if ((retval = gpio_request(dev->mach->gpio_pullup,
2125                                "pca25x_udc GPIO PULLUP"))) {
2126                        dev_dbg(&pdev->dev,
2127                                "can't get pullup gpio %d, err: %d\n",
2128                                dev->mach->gpio_pullup, retval);
2129                        goto err_gpio_pullup;
2130                }
2131                gpio_direction_output(dev->mach->gpio_pullup, 0);
2132        }
2133
2134        init_timer(&dev->timer);
2135        dev->timer.function = udc_watchdog;
2136        dev->timer.data = (unsigned long) dev;
2137
2138        the_controller = dev;
2139        platform_set_drvdata(pdev, dev);
2140
2141        udc_disable(dev);
2142        udc_reinit(dev);
2143
2144        dev->vbus = 0;
2145
2146        /* irq setup after old hardware state is cleaned up */
2147        retval = request_irq(irq, pxa25x_udc_irq,
2148                        0, driver_name, dev);
2149        if (retval != 0) {
2150                pr_err("%s: can't get irq %d, err %d\n",
2151                        driver_name, irq, retval);
2152                goto err_irq1;
2153        }
2154        dev->got_irq = 1;
2155
2156#ifdef CONFIG_ARCH_LUBBOCK
2157        if (machine_is_lubbock()) {
2158                retval = request_irq(LUBBOCK_USB_DISC_IRQ, lubbock_vbus_irq,
2159                                     0, driver_name, dev);
2160                if (retval != 0) {
2161                        pr_err("%s: can't get irq %i, err %d\n",
2162                                driver_name, LUBBOCK_USB_DISC_IRQ, retval);
2163                        goto err_irq_lub;
2164                }
2165                retval = request_irq(LUBBOCK_USB_IRQ, lubbock_vbus_irq,
2166                                     0, driver_name, dev);
2167                if (retval != 0) {
2168                        pr_err("%s: can't get irq %i, err %d\n",
2169                                driver_name, LUBBOCK_USB_IRQ, retval);
2170                        goto lubbock_fail0;
2171                }
2172        } else
2173#endif
2174        create_debug_files(dev);
2175
2176        retval = usb_add_gadget_udc(&pdev->dev, &dev->gadget);
2177        if (!retval)
2178                return retval;
2179
2180        remove_debug_files(dev);
2181#ifdef  CONFIG_ARCH_LUBBOCK
2182lubbock_fail0:
2183        free_irq(LUBBOCK_USB_DISC_IRQ, dev);
2184 err_irq_lub:
2185        free_irq(irq, dev);
2186#endif
2187 err_irq1:
2188        if (gpio_is_valid(dev->mach->gpio_pullup))
2189                gpio_free(dev->mach->gpio_pullup);
2190 err_gpio_pullup:
2191        if (!IS_ERR_OR_NULL(dev->transceiver)) {
2192                usb_put_phy(dev->transceiver);
2193                dev->transceiver = NULL;
2194        }
2195        clk_put(dev->clk);
2196 err_clk:
2197        return retval;
2198}
2199
2200static void pxa25x_udc_shutdown(struct platform_device *_dev)
2201{
2202        pullup_off();
2203}
2204
2205static int __exit pxa25x_udc_remove(struct platform_device *pdev)
2206{
2207        struct pxa25x_udc *dev = platform_get_drvdata(pdev);
2208
2209        if (dev->driver)
2210                return -EBUSY;
2211
2212        usb_del_gadget_udc(&dev->gadget);
2213        dev->pullup = 0;
2214        pullup(dev);
2215
2216        remove_debug_files(dev);
2217
2218        if (dev->got_irq) {
2219                free_irq(platform_get_irq(pdev, 0), dev);
2220                dev->got_irq = 0;
2221        }
2222#ifdef CONFIG_ARCH_LUBBOCK
2223        if (machine_is_lubbock()) {
2224                free_irq(LUBBOCK_USB_DISC_IRQ, dev);
2225                free_irq(LUBBOCK_USB_IRQ, dev);
2226        }
2227#endif
2228        if (gpio_is_valid(dev->mach->gpio_pullup))
2229                gpio_free(dev->mach->gpio_pullup);
2230
2231        clk_put(dev->clk);
2232
2233        if (!IS_ERR_OR_NULL(dev->transceiver)) {
2234                usb_put_phy(dev->transceiver);
2235                dev->transceiver = NULL;
2236        }
2237
2238        the_controller = NULL;
2239        return 0;
2240}
2241
2242/*-------------------------------------------------------------------------*/
2243
2244#ifdef  CONFIG_PM
2245
2246/* USB suspend (controlled by the host) and system suspend (controlled
2247 * by the PXA) don't necessarily work well together.  If USB is active,
2248 * the 48 MHz clock is required; so the system can't enter 33 MHz idle
2249 * mode, or any deeper PM saving state.
2250 *
2251 * For now, we punt and forcibly disconnect from the USB host when PXA
2252 * enters any suspend state.  While we're disconnected, we always disable
2253 * the 48MHz USB clock ... allowing PXA sleep and/or 33 MHz idle states.
2254 * Boards without software pullup control shouldn't use those states.
2255 * VBUS IRQs should probably be ignored so that the PXA device just acts
2256 * "dead" to USB hosts until system resume.
2257 */
2258static int pxa25x_udc_suspend(struct platform_device *dev, pm_message_t state)
2259{
2260        struct pxa25x_udc       *udc = platform_get_drvdata(dev);
2261        unsigned long flags;
2262
2263        if (!gpio_is_valid(udc->mach->gpio_pullup) && !udc->mach->udc_command)
2264                WARNING("USB host won't detect disconnect!\n");
2265        udc->suspended = 1;
2266
2267        local_irq_save(flags);
2268        pullup(udc);
2269        local_irq_restore(flags);
2270
2271        return 0;
2272}
2273
2274static int pxa25x_udc_resume(struct platform_device *dev)
2275{
2276        struct pxa25x_udc       *udc = platform_get_drvdata(dev);
2277        unsigned long flags;
2278
2279        udc->suspended = 0;
2280        local_irq_save(flags);
2281        pullup(udc);
2282        local_irq_restore(flags);
2283
2284        return 0;
2285}
2286
2287#else
2288#define pxa25x_udc_suspend      NULL
2289#define pxa25x_udc_resume       NULL
2290#endif
2291
2292/*-------------------------------------------------------------------------*/
2293
2294static struct platform_driver udc_driver = {
2295        .shutdown       = pxa25x_udc_shutdown,
2296        .remove         = __exit_p(pxa25x_udc_remove),
2297        .suspend        = pxa25x_udc_suspend,
2298        .resume         = pxa25x_udc_resume,
2299        .driver         = {
2300                .owner  = THIS_MODULE,
2301                .name   = "pxa25x-udc",
2302        },
2303};
2304
2305module_platform_driver_probe(udc_driver, pxa25x_udc_probe);
2306
2307MODULE_DESCRIPTION(DRIVER_DESC);
2308MODULE_AUTHOR("Frank Becker, Robert Schwebel, David Brownell");
2309MODULE_LICENSE("GPL");
2310MODULE_ALIAS("platform:pxa25x-udc");
2311