Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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