root/drivers/usb/gadget/udc/goku_udc.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. command
  2. goku_ep_enable
  3. ep_reset
  4. goku_ep_disable
  5. goku_alloc_request
  6. goku_free_request
  7. done
  8. write_packet
  9. write_fifo
  10. read_fifo
  11. pio_irq_enable
  12. pio_irq_disable
  13. pio_advance
  14. start_dma
  15. dma_advance
  16. abort_dma
  17. goku_queue
  18. nuke
  19. goku_dequeue
  20. goku_clear_halt
  21. goku_set_halt
  22. goku_fifo_status
  23. goku_fifo_flush
  24. goku_get_frame
  25. goku_match_ep
  26. dmastr
  27. dump_intmask
  28. udc_ep_state
  29. udc_ep_status
  30. udc_proc_read
  31. udc_reinit
  32. udc_reset
  33. ep0_start
  34. udc_enable
  35. goku_udc_start
  36. stop_activity
  37. goku_udc_stop
  38. ep0_setup
  39. goku_irq
  40. gadget_release
  41. goku_remove
  42. goku_probe

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

/* [<][>][^][v][top][bottom][index][help] */