2 * Toshiba TC86C001 ("Goku-S") USB Device Controller driver
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)
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.
15 * This device has ep0 and three semi-configurable bulk/interrupt endpoints.
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).
24 // #define VERBOSE /* extra debug messages (success too) */
25 // #define USB_TRACE /* packet-level success messages */
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/pci.h>
30 #include <linux/delay.h>
31 #include <linux/ioport.h>
32 #include <linux/slab.h>
33 #include <linux/smp_lock.h>
34 #include <linux/errno.h>
35 #include <linux/init.h>
36 #include <linux/timer.h>
37 #include <linux/list.h>
38 #include <linux/interrupt.h>
39 #include <linux/proc_fs.h>
40 #include <linux/device.h>
41 #include <linux/usb/ch9.h>
42 #include <linux/usb_gadget.h>
44 #include <asm/byteorder.h>
47 #include <asm/system.h>
48 #include <asm/unaligned.h>
53 #define DRIVER_DESC "TC86C001 USB Device Controller"
54 #define DRIVER_VERSION "30-Oct 2003"
56 #define DMA_ADDR_INVALID (~(dma_addr_t)0)
58 static const char driver_name [] = "goku_udc";
59 static const char driver_desc [] = DRIVER_DESC;
61 MODULE_AUTHOR("source@mvista.com");
62 MODULE_DESCRIPTION(DRIVER_DESC);
63 MODULE_LICENSE("GPL");
67 * IN dma behaves ok under testing, though the IN-dma abort paths don't
68 * seem to behave quite as expected. Used by default.
70 * OUT dma documents design problems handling the common "short packet"
71 * transfer termination policy; it couldn't be enabled by default, even
72 * if the OUT-dma abort problems had a resolution.
74 static unsigned use_dma = 1;
77 //#include <linux/moduleparam.h>
78 /* "modprobe goku_udc use_dma=1" etc
80 * 1 to use IN dma only (normal operation)
81 * 2 to use IN and OUT dma
83 module_param(use_dma, uint, S_IRUGO);
86 /*-------------------------------------------------------------------------*/
88 static void nuke(struct goku_ep *, int status);
91 command(struct goku_udc_regs __iomem *regs, int command, unsigned epnum)
93 writel(COMMAND_EP(epnum) | command, ®s->Command);
98 goku_ep_enable(struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
100 struct goku_udc *dev;
106 ep = container_of(_ep, struct goku_ep, ep);
107 if (!_ep || !desc || ep->desc
108 || desc->bDescriptorType != USB_DT_ENDPOINT)
111 if (ep == &dev->ep[0])
113 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
115 if (ep->num != (desc->bEndpointAddress & 0x0f))
118 switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
119 case USB_ENDPOINT_XFER_BULK:
120 case USB_ENDPOINT_XFER_INT:
126 if ((readl(ep->reg_status) & EPxSTATUS_EP_MASK)
127 != EPxSTATUS_EP_INVALID)
130 /* enabling the no-toggle interrupt mode would need an api hook */
132 max = le16_to_cpu(get_unaligned(&desc->wMaxPacketSize));
142 mode |= 2 << 1; /* bulk, or intr-with-toggle */
144 /* ep1/ep2 dma direction is chosen early; it works in the other
145 * direction, with pio. be cautious with out-dma.
147 ep->is_in = (USB_DIR_IN & desc->bEndpointAddress) != 0;
150 ep->dma = (use_dma != 0) && (ep->num == UDC_MSTRD_ENDPOINT);
152 ep->dma = (use_dma == 2) && (ep->num == UDC_MSTWR_ENDPOINT);
154 DBG(dev, "%s out-dma hides short packets\n",
158 spin_lock_irqsave(&ep->dev->lock, flags);
160 /* ep1 and ep2 can do double buffering and/or dma */
162 struct goku_udc_regs __iomem *regs = ep->dev->regs;
165 /* double buffer except (for now) with pio in */
166 tmp = ((ep->dma || !ep->is_in)
167 ? 0x10 /* double buffered */
168 : 0x11 /* single buffer */
170 tmp |= readl(®s->EPxSingle);
171 writel(tmp, ®s->EPxSingle);
173 tmp = (ep->dma ? 0x10/*dma*/ : 0x11/*pio*/) << ep->num;
174 tmp |= readl(®s->EPxBCS);
175 writel(tmp, ®s->EPxBCS);
177 writel(mode, ep->reg_mode);
178 command(ep->dev->regs, COMMAND_RESET, ep->num);
179 ep->ep.maxpacket = max;
182 spin_unlock_irqrestore(&ep->dev->lock, flags);
184 DBG(dev, "enable %s %s %s maxpacket %u\n", ep->ep.name,
185 ep->is_in ? "IN" : "OUT",
186 ep->dma ? "dma" : "pio",
192 static void ep_reset(struct goku_udc_regs __iomem *regs, struct goku_ep *ep)
194 struct goku_udc *dev = ep->dev;
197 command(regs, COMMAND_INVALID, ep->num);
199 if (ep->num == UDC_MSTWR_ENDPOINT)
200 dev->int_enable &= ~(INT_MSTWREND
202 else if (ep->num == UDC_MSTRD_ENDPOINT)
203 dev->int_enable &= ~INT_MSTRDEND;
204 dev->int_enable &= ~INT_EPxDATASET (ep->num);
206 dev->int_enable &= ~INT_EP0;
207 writel(dev->int_enable, ®s->int_enable);
208 readl(®s->int_enable);
210 struct goku_udc_regs __iomem *r = ep->dev->regs;
213 tmp = readl(&r->EPxSingle);
214 tmp &= ~(0x11 << ep->num);
215 writel(tmp, &r->EPxSingle);
217 tmp = readl(&r->EPxBCS);
218 tmp &= ~(0x11 << ep->num);
219 writel(tmp, &r->EPxBCS);
221 /* reset dma in case we're still using it */
225 master = readl(®s->dma_master) & MST_RW_BITS;
226 if (ep->num == UDC_MSTWR_ENDPOINT) {
227 master &= ~MST_W_BITS;
228 master |= MST_WR_RESET;
230 master &= ~MST_R_BITS;
231 master |= MST_RD_RESET;
233 writel(master, ®s->dma_master);
237 ep->ep.maxpacket = MAX_FIFO_SIZE;
244 static int goku_ep_disable(struct usb_ep *_ep)
247 struct goku_udc *dev;
250 ep = container_of(_ep, struct goku_ep, ep);
251 if (!_ep || !ep->desc)
254 if (dev->ep0state == EP0_SUSPEND)
257 VDBG(dev, "disable %s\n", _ep->name);
259 spin_lock_irqsave(&dev->lock, flags);
260 nuke(ep, -ESHUTDOWN);
261 ep_reset(dev->regs, ep);
262 spin_unlock_irqrestore(&dev->lock, flags);
267 /*-------------------------------------------------------------------------*/
269 static struct usb_request *
270 goku_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
272 struct goku_request *req;
276 req = kzalloc(sizeof *req, gfp_flags);
280 req->req.dma = DMA_ADDR_INVALID;
281 INIT_LIST_HEAD(&req->queue);
286 goku_free_request(struct usb_ep *_ep, struct usb_request *_req)
288 struct goku_request *req;
293 req = container_of(_req, struct goku_request, req);
294 WARN_ON(!list_empty(&req->queue));
298 /*-------------------------------------------------------------------------*/
302 /* many common platforms have dma-coherent caches, which means that it's
303 * safe to use kmalloc() memory for all i/o buffers without using any
304 * cache flushing calls. (unless you're trying to share cache lines
305 * between dma and non-dma activities, which is a slow idea in any case.)
307 * other platforms need more care, with 2.6 having a moderately general
308 * solution except for the common "buffer is smaller than a page" case.
310 #if defined(CONFIG_X86)
313 #elif defined(CONFIG_MIPS) && !defined(CONFIG_DMA_NONCOHERENT)
316 #elif defined(CONFIG_PPC) && !defined(CONFIG_NOT_COHERENT_CACHE)
321 /* allocating buffers this way eliminates dma mapping overhead, which
322 * on some platforms will mean eliminating a per-io buffer copy. with
323 * some kinds of system caches, further tweaks may still be needed.
326 goku_alloc_buffer(struct usb_ep *_ep, unsigned bytes,
327 dma_addr_t *dma, gfp_t gfp_flags)
332 ep = container_of(_ep, struct goku_ep, ep);
335 *dma = DMA_ADDR_INVALID;
337 #if defined(USE_KMALLOC)
338 retval = kmalloc(bytes, gfp_flags);
340 *dma = virt_to_phys(retval);
343 /* the main problem with this call is that it wastes memory
344 * on typical 1/N page allocations: it allocates 1-N pages.
346 #warning Using dma_alloc_coherent even with buffers smaller than a page.
347 retval = dma_alloc_coherent(&ep->dev->pdev->dev,
348 bytes, dma, gfp_flags);
350 retval = kmalloc(bytes, gfp_flags);
356 goku_free_buffer(struct usb_ep *_ep, void *buf, dma_addr_t dma, unsigned bytes)
358 /* free memory into the right allocator */
360 if (dma != DMA_ADDR_INVALID) {
363 ep = container_of(_ep, struct goku_ep, ep);
366 dma_free_coherent(&ep->dev->pdev->dev, bytes, buf, dma);
372 /*-------------------------------------------------------------------------*/
375 done(struct goku_ep *ep, struct goku_request *req, int status)
377 struct goku_udc *dev;
378 unsigned stopped = ep->stopped;
380 list_del_init(&req->queue);
382 if (likely(req->req.status == -EINPROGRESS))
383 req->req.status = status;
385 status = req->req.status;
389 pci_unmap_single(dev->pdev, req->req.dma, req->req.length,
390 ep->is_in ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
391 req->req.dma = DMA_ADDR_INVALID;
396 if (status && status != -ESHUTDOWN)
398 VDBG(dev, "complete %s req %p stat %d len %u/%u\n",
399 ep->ep.name, &req->req, status,
400 req->req.actual, req->req.length);
402 /* don't modify queue heads during completion callback */
404 spin_unlock(&dev->lock);
405 req->req.complete(&ep->ep, &req->req);
406 spin_lock(&dev->lock);
407 ep->stopped = stopped;
410 /*-------------------------------------------------------------------------*/
413 write_packet(u32 __iomem *fifo, u8 *buf, struct goku_request *req, unsigned max)
415 unsigned length, count;
417 length = min(req->req.length - req->req.actual, max);
418 req->req.actual += length;
421 while (likely(count--))
422 writel(*buf++, fifo);
426 // return: 0 = still running, 1 = completed, negative = errno
427 static int write_fifo(struct goku_ep *ep, struct goku_request *req)
429 struct goku_udc *dev = ep->dev;
435 tmp = readl(&dev->regs->DataSet);
436 buf = req->req.buf + req->req.actual;
440 if (unlikely(ep->num == 0 && dev->ep0state != EP0_IN))
443 /* NOTE: just single-buffered PIO-IN for now. */
444 if (unlikely((tmp & DATASET_A(ep->num)) != 0))
447 /* clear our "packet available" irq */
449 writel(~INT_EPxDATASET(ep->num), &dev->regs->int_status);
451 count = write_packet(ep->reg_fifo, buf, req, ep->ep.maxpacket);
453 /* last packet often short (sometimes a zlp, especially on ep0) */
454 if (unlikely(count != ep->ep.maxpacket)) {
455 writel(~(1<<ep->num), &dev->regs->EOP);
457 dev->ep[0].stopped = 1;
458 dev->ep0state = EP0_STATUS;
462 if (likely(req->req.length != req->req.actual)
468 #if 0 /* printk seemed to trash is_last...*/
470 VDBG(dev, "wrote %s %u bytes%s IN %u left %p\n",
471 ep->ep.name, count, is_last ? "/last" : "",
472 req->req.length - req->req.actual, req);
475 /* requests complete when all IN data is in the FIFO,
476 * or sometimes later, if a zlp was needed.
486 static int read_fifo(struct goku_ep *ep, struct goku_request *req)
488 struct goku_udc_regs __iomem *regs;
491 unsigned bufferspace, is_short, dbuff;
493 regs = ep->dev->regs;
495 buf = req->req.buf + req->req.actual;
498 if (unlikely(ep->num == 0 && ep->dev->ep0state != EP0_OUT))
501 dbuff = (ep->num == 1 || ep->num == 2);
503 /* ack dataset irq matching the status we'll handle */
505 writel(~INT_EPxDATASET(ep->num), ®s->int_status);
507 set = readl(®s->DataSet) & DATASET_AB(ep->num);
508 size = readl(®s->EPxSizeLA[ep->num]);
509 bufferspace = req->req.length - req->req.actual;
511 /* usually do nothing without an OUT packet */
512 if (likely(ep->num != 0 || bufferspace != 0)) {
513 if (unlikely(set == 0))
515 /* use ep1/ep2 double-buffering for OUT */
516 if (!(size & PACKET_ACTIVE))
517 size = readl(®s->EPxSizeLB[ep->num]);
518 if (!(size & PACKET_ACTIVE)) // "can't happen"
520 size &= DATASIZE; /* EPxSizeH == 0 */
522 /* ep0out no-out-data case for set_config, etc */
526 /* read all bytes from this packet */
527 req->req.actual += size;
528 is_short = (size < ep->ep.maxpacket);
530 VDBG(ep->dev, "read %s %u bytes%s OUT req %p %u/%u\n",
531 ep->ep.name, size, is_short ? "/S" : "",
532 req, req->req.actual, req->req.length);
534 while (likely(size-- != 0)) {
535 u8 byte = (u8) readl(ep->reg_fifo);
537 if (unlikely(bufferspace == 0)) {
538 /* this happens when the driver's buffer
539 * is smaller than what the host sent.
540 * discard the extra data in this packet.
542 if (req->req.status != -EOVERFLOW)
543 DBG(ep->dev, "%s overflow %u\n",
545 req->req.status = -EOVERFLOW;
553 if (unlikely(is_short || req->req.actual == req->req.length)) {
554 if (unlikely(ep->num == 0)) {
555 /* non-control endpoints now usable? */
556 if (ep->dev->req_config)
557 writel(ep->dev->configured
558 ? USBSTATE_CONFIGURED
561 /* ep0out status stage */
562 writel(~(1<<0), ®s->EOP);
564 ep->dev->ep0state = EP0_STATUS;
568 /* empty the second buffer asap */
569 if (dbuff && !list_empty(&ep->queue)) {
570 req = list_entry(ep->queue.next,
571 struct goku_request, queue);
581 pio_irq_enable(struct goku_udc *dev,
582 struct goku_udc_regs __iomem *regs, int epnum)
584 dev->int_enable |= INT_EPxDATASET (epnum);
585 writel(dev->int_enable, ®s->int_enable);
586 /* write may still be posted */
590 pio_irq_disable(struct goku_udc *dev,
591 struct goku_udc_regs __iomem *regs, int epnum)
593 dev->int_enable &= ~INT_EPxDATASET (epnum);
594 writel(dev->int_enable, ®s->int_enable);
595 /* write may still be posted */
599 pio_advance(struct goku_ep *ep)
601 struct goku_request *req;
603 if (unlikely(list_empty (&ep->queue)))
605 req = list_entry(ep->queue.next, struct goku_request, queue);
606 (ep->is_in ? write_fifo : read_fifo)(ep, req);
610 /*-------------------------------------------------------------------------*/
612 // return: 0 = q running, 1 = q stopped, negative = errno
613 static int start_dma(struct goku_ep *ep, struct goku_request *req)
615 struct goku_udc_regs __iomem *regs = ep->dev->regs;
617 u32 start = req->req.dma;
618 u32 end = start + req->req.length - 1;
620 master = readl(®s->dma_master) & MST_RW_BITS;
622 /* re-init the bits affecting IN dma; careful with zlps */
623 if (likely(ep->is_in)) {
624 if (unlikely(master & MST_RD_ENA)) {
625 DBG (ep->dev, "start, IN active dma %03x!!\n",
629 writel(end, ®s->in_dma_end);
630 writel(start, ®s->in_dma_start);
632 master &= ~MST_R_BITS;
633 if (unlikely(req->req.length == 0))
634 master = MST_RD_ENA | MST_RD_EOPB;
635 else if ((req->req.length % ep->ep.maxpacket) != 0
637 master = MST_RD_ENA | MST_EOPB_ENA;
639 master = MST_RD_ENA | MST_EOPB_DIS;
641 ep->dev->int_enable |= INT_MSTRDEND;
643 /* Goku DMA-OUT merges short packets, which plays poorly with
644 * protocols where short packets mark the transfer boundaries.
645 * The chip supports a nonstandard policy with INT_MSTWRTMOUT,
646 * ending transfers after 3 SOFs; we don't turn it on.
649 if (unlikely(master & MST_WR_ENA)) {
650 DBG (ep->dev, "start, OUT active dma %03x!!\n",
654 writel(end, ®s->out_dma_end);
655 writel(start, ®s->out_dma_start);
657 master &= ~MST_W_BITS;
658 master |= MST_WR_ENA | MST_TIMEOUT_DIS;
660 ep->dev->int_enable |= INT_MSTWREND|INT_MSTWRTMOUT;
663 writel(master, ®s->dma_master);
664 writel(ep->dev->int_enable, ®s->int_enable);
668 static void dma_advance(struct goku_udc *dev, struct goku_ep *ep)
670 struct goku_request *req;
671 struct goku_udc_regs __iomem *regs = ep->dev->regs;
674 master = readl(®s->dma_master);
676 if (unlikely(list_empty(&ep->queue))) {
679 dev->int_enable &= ~INT_MSTRDEND;
681 dev->int_enable &= ~(INT_MSTWREND|INT_MSTWRTMOUT);
682 writel(dev->int_enable, ®s->int_enable);
685 req = list_entry(ep->queue.next, struct goku_request, queue);
687 /* normal hw dma completion (not abort) */
688 if (likely(ep->is_in)) {
689 if (unlikely(master & MST_RD_ENA))
691 req->req.actual = readl(®s->in_dma_current);
693 if (unlikely(master & MST_WR_ENA))
696 /* hardware merges short packets, and also hides packet
697 * overruns. a partial packet MAY be in the fifo here.
699 req->req.actual = readl(®s->out_dma_current);
701 req->req.actual -= req->req.dma;
705 VDBG(dev, "done %s %s dma, %u/%u bytes, req %p\n",
706 ep->ep.name, ep->is_in ? "IN" : "OUT",
707 req->req.actual, req->req.length, req);
710 if (list_empty(&ep->queue))
712 req = list_entry(ep->queue.next, struct goku_request, queue);
713 (void) start_dma(ep, req);
716 static void abort_dma(struct goku_ep *ep, int status)
718 struct goku_udc_regs __iomem *regs = ep->dev->regs;
719 struct goku_request *req;
722 /* NAK future host requests, hoping the implicit delay lets the
723 * dma engine finish reading (or writing) its latest packet and
724 * empty the dma buffer (up to 16 bytes).
726 * This avoids needing to clean up a partial packet in the fifo;
727 * we can't do that for IN without side effects to HALT and TOGGLE.
729 command(regs, COMMAND_FIFO_DISABLE, ep->num);
730 req = list_entry(ep->queue.next, struct goku_request, queue);
731 master = readl(®s->dma_master) & MST_RW_BITS;
733 /* FIXME using these resets isn't usably documented. this may
734 * not work unless it's followed by disabling the endpoint.
736 * FIXME the OUT reset path doesn't even behave consistently.
739 if (unlikely((readl(®s->dma_master) & MST_RD_ENA) == 0))
741 curr = readl(®s->in_dma_current);
743 writel(curr, ®s->in_dma_end);
744 writel(curr, ®s->in_dma_start);
746 master &= ~MST_R_BITS;
747 master |= MST_RD_RESET;
748 writel(master, ®s->dma_master);
750 if (readl(®s->dma_master) & MST_RD_ENA)
751 DBG(ep->dev, "IN dma active after reset!\n");
754 if (unlikely((readl(®s->dma_master) & MST_WR_ENA) == 0))
756 curr = readl(®s->out_dma_current);
758 writel(curr, ®s->out_dma_end);
759 writel(curr, ®s->out_dma_start);
761 master &= ~MST_W_BITS;
762 master |= MST_WR_RESET;
763 writel(master, ®s->dma_master);
765 if (readl(®s->dma_master) & MST_WR_ENA)
766 DBG(ep->dev, "OUT dma active after reset!\n");
768 req->req.actual = (curr - req->req.dma) + 1;
769 req->req.status = status;
771 VDBG(ep->dev, "%s %s %s %d/%d\n", __FUNCTION__, ep->ep.name,
772 ep->is_in ? "IN" : "OUT",
773 req->req.actual, req->req.length);
775 command(regs, COMMAND_FIFO_ENABLE, ep->num);
780 /* dma already completed; no abort needed */
781 command(regs, COMMAND_FIFO_ENABLE, ep->num);
782 req->req.actual = req->req.length;
786 /*-------------------------------------------------------------------------*/
789 goku_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
791 struct goku_request *req;
793 struct goku_udc *dev;
797 /* always require a cpu-view buffer so pio works */
798 req = container_of(_req, struct goku_request, req);
799 if (unlikely(!_req || !_req->complete
800 || !_req->buf || !list_empty(&req->queue)))
802 ep = container_of(_ep, struct goku_ep, ep);
803 if (unlikely(!_ep || (!ep->desc && ep->num != 0)))
806 if (unlikely(!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN))
809 /* can't touch registers when suspended */
810 if (dev->ep0state == EP0_SUSPEND)
813 /* set up dma mapping in case the caller didn't */
814 if (ep->dma && _req->dma == DMA_ADDR_INVALID) {
815 _req->dma = pci_map_single(dev->pdev, _req->buf, _req->length,
816 ep->is_in ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
821 VDBG(dev, "%s queue req %p, len %u buf %p\n",
822 _ep->name, _req, _req->length, _req->buf);
825 spin_lock_irqsave(&dev->lock, flags);
827 _req->status = -EINPROGRESS;
830 /* for ep0 IN without premature status, zlp is required and
831 * writing EOP starts the status stage (OUT).
833 if (unlikely(ep->num == 0 && ep->is_in))
836 /* kickstart this i/o queue? */
838 if (list_empty(&ep->queue) && likely(!ep->stopped)) {
839 /* dma: done after dma completion IRQ (or error)
840 * pio: done after last fifo operation
843 status = start_dma(ep, req);
845 status = (ep->is_in ? write_fifo : read_fifo)(ep, req);
847 if (unlikely(status != 0)) {
853 } /* else pio or dma irq handler advances the queue. */
855 if (likely(req != 0))
856 list_add_tail(&req->queue, &ep->queue);
858 if (likely(!list_empty(&ep->queue))
859 && likely(ep->num != 0)
861 && !(dev->int_enable & INT_EPxDATASET (ep->num)))
862 pio_irq_enable(dev, dev->regs, ep->num);
864 spin_unlock_irqrestore(&dev->lock, flags);
866 /* pci writes may still be posted */
870 /* dequeue ALL requests */
871 static void nuke(struct goku_ep *ep, int status)
873 struct goku_request *req;
876 if (list_empty(&ep->queue))
879 abort_dma(ep, status);
880 while (!list_empty(&ep->queue)) {
881 req = list_entry(ep->queue.next, struct goku_request, queue);
882 done(ep, req, status);
886 /* dequeue JUST ONE request */
887 static int goku_dequeue(struct usb_ep *_ep, struct usb_request *_req)
889 struct goku_request *req;
891 struct goku_udc *dev;
894 ep = container_of(_ep, struct goku_ep, ep);
895 if (!_ep || !_req || (!ep->desc && ep->num != 0))
901 /* we can't touch (dma) registers when suspended */
902 if (dev->ep0state == EP0_SUSPEND)
905 VDBG(dev, "%s %s %s %s %p\n", __FUNCTION__, _ep->name,
906 ep->is_in ? "IN" : "OUT",
907 ep->dma ? "dma" : "pio",
910 spin_lock_irqsave(&dev->lock, flags);
912 /* make sure it's actually queued on this endpoint */
913 list_for_each_entry (req, &ep->queue, queue) {
914 if (&req->req == _req)
917 if (&req->req != _req) {
918 spin_unlock_irqrestore (&dev->lock, flags);
922 if (ep->dma && ep->queue.next == &req->queue && !ep->stopped) {
923 abort_dma(ep, -ECONNRESET);
924 done(ep, req, -ECONNRESET);
925 dma_advance(dev, ep);
926 } else if (!list_empty(&req->queue))
927 done(ep, req, -ECONNRESET);
930 spin_unlock_irqrestore(&dev->lock, flags);
932 return req ? 0 : -EOPNOTSUPP;
935 /*-------------------------------------------------------------------------*/
937 static void goku_clear_halt(struct goku_ep *ep)
939 // assert (ep->num !=0)
940 VDBG(ep->dev, "%s clear halt\n", ep->ep.name);
941 command(ep->dev->regs, COMMAND_SETDATA0, ep->num);
942 command(ep->dev->regs, COMMAND_STALL_CLEAR, ep->num);
946 struct goku_request *req;
948 if (list_empty(&ep->queue))
950 req = list_entry(ep->queue.next, struct goku_request,
952 (void) start_dma(ep, req);
958 static int goku_set_halt(struct usb_ep *_ep, int value)
966 ep = container_of (_ep, struct goku_ep, ep);
970 ep->dev->ep0state = EP0_STALL;
971 ep->dev->ep[0].stopped = 1;
975 /* don't change EPxSTATUS_EP_INVALID to READY */
976 } else if (!ep->desc) {
977 DBG(ep->dev, "%s %s inactive?\n", __FUNCTION__, ep->ep.name);
981 spin_lock_irqsave(&ep->dev->lock, flags);
982 if (!list_empty(&ep->queue))
984 else if (ep->is_in && value
985 /* data in (either) packet buffer? */
986 && (readl(&ep->dev->regs->DataSet)
987 & DATASET_AB(ep->num)))
993 VDBG(ep->dev, "%s set halt\n", ep->ep.name);
994 command(ep->dev->regs, COMMAND_STALL, ep->num);
995 readl(ep->reg_status);
997 spin_unlock_irqrestore(&ep->dev->lock, flags);
1001 static int goku_fifo_status(struct usb_ep *_ep)
1004 struct goku_udc_regs __iomem *regs;
1009 ep = container_of(_ep, struct goku_ep, ep);
1011 /* size is only reported sanely for OUT */
1015 /* ignores 16-byte dma buffer; SizeH == 0 */
1016 regs = ep->dev->regs;
1017 size = readl(®s->EPxSizeLA[ep->num]) & DATASIZE;
1018 size += readl(®s->EPxSizeLB[ep->num]) & DATASIZE;
1019 VDBG(ep->dev, "%s %s %u\n", __FUNCTION__, ep->ep.name, size);
1023 static void goku_fifo_flush(struct usb_ep *_ep)
1026 struct goku_udc_regs __iomem *regs;
1031 ep = container_of(_ep, struct goku_ep, ep);
1032 VDBG(ep->dev, "%s %s\n", __FUNCTION__, ep->ep.name);
1034 /* don't change EPxSTATUS_EP_INVALID to READY */
1035 if (!ep->desc && ep->num != 0) {
1036 DBG(ep->dev, "%s %s inactive?\n", __FUNCTION__, ep->ep.name);
1040 regs = ep->dev->regs;
1041 size = readl(®s->EPxSizeLA[ep->num]);
1044 /* Non-desirable behavior: FIFO_CLEAR also clears the
1045 * endpoint halt feature. For OUT, we _could_ just read
1046 * the bytes out (PIO, if !ep->dma); for in, no choice.
1049 command(regs, COMMAND_FIFO_CLEAR, ep->num);
1052 static struct usb_ep_ops goku_ep_ops = {
1053 .enable = goku_ep_enable,
1054 .disable = goku_ep_disable,
1056 .alloc_request = goku_alloc_request,
1057 .free_request = goku_free_request,
1059 .alloc_buffer = goku_alloc_buffer,
1060 .free_buffer = goku_free_buffer,
1062 .queue = goku_queue,
1063 .dequeue = goku_dequeue,
1065 .set_halt = goku_set_halt,
1066 .fifo_status = goku_fifo_status,
1067 .fifo_flush = goku_fifo_flush,
1070 /*-------------------------------------------------------------------------*/
1072 static int goku_get_frame(struct usb_gadget *_gadget)
1077 static const struct usb_gadget_ops goku_ops = {
1078 .get_frame = goku_get_frame,
1083 /*-------------------------------------------------------------------------*/
1085 static inline char *dmastr(void)
1088 return "(dma disabled)";
1089 else if (use_dma == 2)
1090 return "(dma IN and OUT)";
1095 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1097 static const char proc_node_name [] = "driver/udc";
1099 #define FOURBITS "%s%s%s%s"
1100 #define EIGHTBITS FOURBITS FOURBITS
1103 dump_intmask(const char *label, u32 mask, char **next, unsigned *size)
1107 /* int_status is the same format ... */
1108 t = scnprintf(*next, *size,
1109 "%s %05X =" FOURBITS EIGHTBITS EIGHTBITS "\n",
1111 (mask & INT_PWRDETECT) ? " power" : "",
1112 (mask & INT_SYSERROR) ? " sys" : "",
1113 (mask & INT_MSTRDEND) ? " in-dma" : "",
1114 (mask & INT_MSTWRTMOUT) ? " wrtmo" : "",
1116 (mask & INT_MSTWREND) ? " out-dma" : "",
1117 (mask & INT_MSTWRSET) ? " wrset" : "",
1118 (mask & INT_ERR) ? " err" : "",
1119 (mask & INT_SOF) ? " sof" : "",
1121 (mask & INT_EP3NAK) ? " ep3nak" : "",
1122 (mask & INT_EP2NAK) ? " ep2nak" : "",
1123 (mask & INT_EP1NAK) ? " ep1nak" : "",
1124 (mask & INT_EP3DATASET) ? " ep3" : "",
1126 (mask & INT_EP2DATASET) ? " ep2" : "",
1127 (mask & INT_EP1DATASET) ? " ep1" : "",
1128 (mask & INT_STATUSNAK) ? " ep0snak" : "",
1129 (mask & INT_STATUS) ? " ep0status" : "",
1131 (mask & INT_SETUP) ? " setup" : "",
1132 (mask & INT_ENDPOINT0) ? " ep0" : "",
1133 (mask & INT_USBRESET) ? " reset" : "",
1134 (mask & INT_SUSPEND) ? " suspend" : "");
1141 udc_proc_read(char *buffer, char **start, off_t off, int count,
1142 int *eof, void *_dev)
1145 struct goku_udc *dev = _dev;
1146 struct goku_udc_regs __iomem *regs = dev->regs;
1148 unsigned size = count;
1149 unsigned long flags;
1150 int i, t, is_usb_connected;
1156 local_irq_save(flags);
1158 /* basic device status */
1159 tmp = readl(®s->power_detect);
1160 is_usb_connected = tmp & PW_DETECT;
1161 t = scnprintf(next, size,
1163 "%s version: %s %s\n"
1164 "Gadget driver: %s\n"
1167 pci_name(dev->pdev), driver_desc,
1168 driver_name, DRIVER_VERSION, dmastr(),
1169 dev->driver ? dev->driver->driver.name : "(none)",
1171 ? ((tmp & PW_PULLUP) ? "full speed" : "powered")
1174 switch(dev->ep0state){
1175 case EP0_DISCONNECT: tmp = "ep0_disconnect"; break;
1176 case EP0_IDLE: tmp = "ep0_idle"; break;
1177 case EP0_IN: tmp = "ep0_in"; break;
1178 case EP0_OUT: tmp = "ep0_out"; break;
1179 case EP0_STATUS: tmp = "ep0_status"; break;
1180 case EP0_STALL: tmp = "ep0_stall"; break;
1181 case EP0_SUSPEND: tmp = "ep0_suspend"; break;
1182 default: tmp = "ep0_?"; break;
1188 dump_intmask("int_status", readl(®s->int_status), &next, &size);
1189 dump_intmask("int_enable", readl(®s->int_enable), &next, &size);
1191 if (!is_usb_connected || !dev->driver || (tmp & PW_PULLUP) == 0)
1194 /* registers for (active) device and ep0 */
1195 t = scnprintf(next, size, "\nirqs %lu\ndataset %02x "
1196 "single.bcs %02x.%02x state %x addr %u\n",
1197 dev->irqs, readl(®s->DataSet),
1198 readl(®s->EPxSingle), readl(®s->EPxBCS),
1199 readl(®s->UsbState),
1200 readl(®s->address));
1204 tmp = readl(®s->dma_master);
1205 t = scnprintf(next, size,
1206 "dma %03X =" EIGHTBITS "%s %s\n", tmp,
1207 (tmp & MST_EOPB_DIS) ? " eopb-" : "",
1208 (tmp & MST_EOPB_ENA) ? " eopb+" : "",
1209 (tmp & MST_TIMEOUT_DIS) ? " tmo-" : "",
1210 (tmp & MST_TIMEOUT_ENA) ? " tmo+" : "",
1212 (tmp & MST_RD_EOPB) ? " eopb" : "",
1213 (tmp & MST_RD_RESET) ? " in_reset" : "",
1214 (tmp & MST_WR_RESET) ? " out_reset" : "",
1215 (tmp & MST_RD_ENA) ? " IN" : "",
1217 (tmp & MST_WR_ENA) ? " OUT" : "",
1218 (tmp & MST_CONNECTION)
1224 /* dump endpoint queues */
1225 for (i = 0; i < 4; i++) {
1226 struct goku_ep *ep = &dev->ep [i];
1227 struct goku_request *req;
1233 tmp = readl(ep->reg_status);
1234 t = scnprintf(next, size,
1235 "%s %s max %u %s, irqs %lu, "
1236 "status %02x (%s) " FOURBITS "\n",
1238 ep->is_in ? "in" : "out",
1240 ep->dma ? "dma" : "pio",
1243 switch (tmp & EPxSTATUS_EP_MASK) {
1244 case EPxSTATUS_EP_READY:
1246 case EPxSTATUS_EP_DATAIN:
1247 s = "packet"; break;
1248 case EPxSTATUS_EP_FULL:
1250 case EPxSTATUS_EP_TX_ERR: // host will retry
1251 s = "tx_err"; break;
1252 case EPxSTATUS_EP_RX_ERR:
1253 s = "rx_err"; break;
1254 case EPxSTATUS_EP_BUSY: /* ep0 only */
1256 case EPxSTATUS_EP_STALL:
1258 case EPxSTATUS_EP_INVALID: // these "can't happen"
1259 s = "invalid"; break;
1263 (tmp & EPxSTATUS_TOGGLE) ? "data1" : "data0",
1264 (tmp & EPxSTATUS_SUSPEND) ? " suspend" : "",
1265 (tmp & EPxSTATUS_FIFO_DISABLE) ? " disable" : "",
1266 (tmp & EPxSTATUS_STAGE_ERROR) ? " ep0stat" : ""
1268 if (t <= 0 || t > size)
1273 if (list_empty(&ep->queue)) {
1274 t = scnprintf(next, size, "\t(nothing queued)\n");
1275 if (t <= 0 || t > size)
1281 list_for_each_entry(req, &ep->queue, queue) {
1282 if (ep->dma && req->queue.prev == &ep->queue) {
1283 if (i == UDC_MSTRD_ENDPOINT)
1284 tmp = readl(®s->in_dma_current);
1286 tmp = readl(®s->out_dma_current);
1287 tmp -= req->req.dma;
1290 tmp = req->req.actual;
1292 t = scnprintf(next, size,
1293 "\treq %p len %u/%u buf %p\n",
1294 &req->req, tmp, req->req.length,
1296 if (t <= 0 || t > size)
1304 local_irq_restore(flags);
1306 return count - size;
1309 #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
1311 /*-------------------------------------------------------------------------*/
1313 static void udc_reinit (struct goku_udc *dev)
1315 static char *names [] = { "ep0", "ep1-bulk", "ep2-bulk", "ep3-bulk" };
1319 INIT_LIST_HEAD (&dev->gadget.ep_list);
1320 dev->gadget.ep0 = &dev->ep [0].ep;
1321 dev->gadget.speed = USB_SPEED_UNKNOWN;
1322 dev->ep0state = EP0_DISCONNECT;
1325 for (i = 0; i < 4; i++) {
1326 struct goku_ep *ep = &dev->ep[i];
1329 ep->ep.name = names[i];
1330 ep->reg_fifo = &dev->regs->ep_fifo [i];
1331 ep->reg_status = &dev->regs->ep_status [i];
1332 ep->reg_mode = &dev->regs->ep_mode[i];
1334 ep->ep.ops = &goku_ep_ops;
1335 list_add_tail (&ep->ep.ep_list, &dev->gadget.ep_list);
1337 INIT_LIST_HEAD (&ep->queue);
1342 dev->ep[0].reg_mode = NULL;
1343 dev->ep[0].ep.maxpacket = MAX_EP0_SIZE;
1344 list_del_init (&dev->ep[0].ep.ep_list);
1347 static void udc_reset(struct goku_udc *dev)
1349 struct goku_udc_regs __iomem *regs = dev->regs;
1351 writel(0, ®s->power_detect);
1352 writel(0, ®s->int_enable);
1353 readl(®s->int_enable);
1354 dev->int_enable = 0;
1356 /* deassert reset, leave USB D+ at hi-Z (no pullup)
1357 * don't let INT_PWRDETECT sequence begin
1360 writel(PW_RESETB, ®s->power_detect);
1361 readl(®s->int_enable);
1364 static void ep0_start(struct goku_udc *dev)
1366 struct goku_udc_regs __iomem *regs = dev->regs;
1369 VDBG(dev, "%s\n", __FUNCTION__);
1373 //writel(MST_EOPB_ENA | MST_TIMEOUT_ENA, ®s->dma_master);
1375 /* hw handles set_address, set_feature, get_status; maybe more */
1376 writel( G_REQMODE_SET_INTF | G_REQMODE_GET_INTF
1377 | G_REQMODE_SET_CONF | G_REQMODE_GET_CONF
1378 | G_REQMODE_GET_DESC
1379 | G_REQMODE_CLEAR_FEAT
1382 for (i = 0; i < 4; i++)
1383 dev->ep[i].irqs = 0;
1385 /* can't modify descriptors after writing UsbReady */
1386 for (i = 0; i < DESC_LEN; i++)
1387 writel(0, ®s->descriptors[i]);
1388 writel(0, ®s->UsbReady);
1390 /* expect ep0 requests when the host drops reset */
1391 writel(PW_RESETB | PW_PULLUP, ®s->power_detect);
1392 dev->int_enable = INT_DEVWIDE | INT_EP0;
1393 writel(dev->int_enable, &dev->regs->int_enable);
1394 readl(®s->int_enable);
1395 dev->gadget.speed = USB_SPEED_FULL;
1396 dev->ep0state = EP0_IDLE;
1399 static void udc_enable(struct goku_udc *dev)
1401 /* start enumeration now, or after power detect irq */
1402 if (readl(&dev->regs->power_detect) & PW_DETECT)
1405 DBG(dev, "%s\n", __FUNCTION__);
1406 dev->int_enable = INT_PWRDETECT;
1407 writel(dev->int_enable, &dev->regs->int_enable);
1411 /*-------------------------------------------------------------------------*/
1413 /* keeping it simple:
1414 * - one bus driver, initted first;
1415 * - one function driver, initted second
1418 static struct goku_udc *the_controller;
1420 /* when a driver is successfully registered, it will receive
1421 * control requests including set_configuration(), which enables
1422 * non-control requests. then usb traffic follows until a
1423 * disconnect is reported. then a host may connect again, or
1424 * the driver might get unbound.
1426 int usb_gadget_register_driver(struct usb_gadget_driver *driver)
1428 struct goku_udc *dev = the_controller;
1432 || driver->speed != USB_SPEED_FULL
1434 || !driver->disconnect
1442 /* hook up the driver */
1443 driver->driver.bus = NULL;
1444 dev->driver = driver;
1445 dev->gadget.dev.driver = &driver->driver;
1446 retval = driver->bind(&dev->gadget);
1448 DBG(dev, "bind to driver %s --> error %d\n",
1449 driver->driver.name, retval);
1451 dev->gadget.dev.driver = NULL;
1455 /* then enable host detection and ep0; and we're ready
1456 * for set_configuration as well as eventual disconnect.
1460 DBG(dev, "registered gadget driver '%s'\n", driver->driver.name);
1463 EXPORT_SYMBOL(usb_gadget_register_driver);
1466 stop_activity(struct goku_udc *dev, struct usb_gadget_driver *driver)
1470 DBG (dev, "%s\n", __FUNCTION__);
1472 if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1475 /* disconnect gadget driver after quiesceing hw and the driver */
1477 for (i = 0; i < 4; i++)
1478 nuke(&dev->ep [i], -ESHUTDOWN);
1480 spin_unlock(&dev->lock);
1481 driver->disconnect(&dev->gadget);
1482 spin_lock(&dev->lock);
1489 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1491 struct goku_udc *dev = the_controller;
1492 unsigned long flags;
1496 if (!driver || driver != dev->driver || !driver->unbind)
1499 spin_lock_irqsave(&dev->lock, flags);
1501 stop_activity(dev, driver);
1502 spin_unlock_irqrestore(&dev->lock, flags);
1504 driver->unbind(&dev->gadget);
1506 DBG(dev, "unregistered driver '%s'\n", driver->driver.name);
1509 EXPORT_SYMBOL(usb_gadget_unregister_driver);
1512 /*-------------------------------------------------------------------------*/
1514 static void ep0_setup(struct goku_udc *dev)
1516 struct goku_udc_regs __iomem *regs = dev->regs;
1517 struct usb_ctrlrequest ctrl;
1520 /* read SETUP packet and enter DATA stage */
1521 ctrl.bRequestType = readl(®s->bRequestType);
1522 ctrl.bRequest = readl(®s->bRequest);
1523 ctrl.wValue = cpu_to_le16((readl(®s->wValueH) << 8)
1524 | readl(®s->wValueL));
1525 ctrl.wIndex = cpu_to_le16((readl(®s->wIndexH) << 8)
1526 | readl(®s->wIndexL));
1527 ctrl.wLength = cpu_to_le16((readl(®s->wLengthH) << 8)
1528 | readl(®s->wLengthL));
1529 writel(0, ®s->SetupRecv);
1531 nuke(&dev->ep[0], 0);
1532 dev->ep[0].stopped = 0;
1533 if (likely(ctrl.bRequestType & USB_DIR_IN)) {
1534 dev->ep[0].is_in = 1;
1535 dev->ep0state = EP0_IN;
1536 /* detect early status stages */
1537 writel(ICONTROL_STATUSNAK, &dev->regs->IntControl);
1539 dev->ep[0].is_in = 0;
1540 dev->ep0state = EP0_OUT;
1542 /* NOTE: CLEAR_FEATURE is done in software so that we can
1543 * synchronize transfer restarts after bulk IN stalls. data
1544 * won't even enter the fifo until the halt is cleared.
1546 switch (ctrl.bRequest) {
1547 case USB_REQ_CLEAR_FEATURE:
1548 switch (ctrl.bRequestType) {
1549 case USB_RECIP_ENDPOINT:
1550 tmp = le16_to_cpu(ctrl.wIndex) & 0x0f;
1551 /* active endpoint */
1552 if (tmp > 3 || (!dev->ep[tmp].desc && tmp != 0))
1554 if (ctrl.wIndex & __constant_cpu_to_le16(
1556 if (!dev->ep[tmp].is_in)
1559 if (dev->ep[tmp].is_in)
1562 if (ctrl.wValue != __constant_cpu_to_le16(
1566 goku_clear_halt(&dev->ep[tmp]);
1568 /* start ep0out status stage */
1569 writel(~(1<<0), ®s->EOP);
1570 dev->ep[0].stopped = 1;
1571 dev->ep0state = EP0_STATUS;
1573 case USB_RECIP_DEVICE:
1574 /* device remote wakeup: always clear */
1575 if (ctrl.wValue != __constant_cpu_to_le16(1))
1577 VDBG(dev, "clear dev remote wakeup\n");
1579 case USB_RECIP_INTERFACE:
1581 default: /* pass to gadget driver */
1591 VDBG(dev, "SETUP %02x.%02x v%04x i%04x l%04x\n",
1592 ctrl.bRequestType, ctrl.bRequest,
1593 le16_to_cpu(ctrl.wValue), le16_to_cpu(ctrl.wIndex),
1594 le16_to_cpu(ctrl.wLength));
1597 /* hw wants to know when we're configured (or not) */
1598 dev->req_config = (ctrl.bRequest == USB_REQ_SET_CONFIGURATION
1599 && ctrl.bRequestType == USB_RECIP_DEVICE);
1600 if (unlikely(dev->req_config))
1601 dev->configured = (ctrl.wValue != __constant_cpu_to_le16(0));
1603 /* delegate everything to the gadget driver.
1604 * it may respond after this irq handler returns.
1606 spin_unlock (&dev->lock);
1607 tmp = dev->driver->setup(&dev->gadget, &ctrl);
1608 spin_lock (&dev->lock);
1609 if (unlikely(tmp < 0)) {
1612 VDBG(dev, "req %02x.%02x protocol STALL; err %d\n",
1613 ctrl.bRequestType, ctrl.bRequest, tmp);
1615 command(regs, COMMAND_STALL, 0);
1616 dev->ep[0].stopped = 1;
1617 dev->ep0state = EP0_STALL;
1620 /* expect at least one data or status stage irq */
1623 #define ACK(irqbit) { \
1625 writel(~irqbit, ®s->int_status); \
1629 static irqreturn_t goku_irq(int irq, void *_dev)
1631 struct goku_udc *dev = _dev;
1632 struct goku_udc_regs __iomem *regs = dev->regs;
1634 u32 stat, handled = 0;
1635 unsigned i, rescans = 5;
1637 spin_lock(&dev->lock);
1640 stat = readl(®s->int_status) & dev->int_enable;
1645 /* device-wide irqs */
1646 if (unlikely(stat & INT_DEVWIDE)) {
1647 if (stat & INT_SYSERROR) {
1648 ERROR(dev, "system error\n");
1649 stop_activity(dev, dev->driver);
1652 // FIXME have a neater way to prevent re-enumeration
1656 if (stat & INT_PWRDETECT) {
1657 writel(~stat, ®s->int_status);
1658 if (readl(&dev->regs->power_detect) & PW_DETECT) {
1659 VDBG(dev, "connect\n");
1662 DBG(dev, "disconnect\n");
1663 if (dev->gadget.speed == USB_SPEED_FULL)
1664 stop_activity(dev, dev->driver);
1665 dev->ep0state = EP0_DISCONNECT;
1666 dev->int_enable = INT_DEVWIDE;
1667 writel(dev->int_enable, &dev->regs->int_enable);
1673 if (stat & INT_SUSPEND) {
1675 if (readl(®s->ep_status[0]) & EPxSTATUS_SUSPEND) {
1676 switch (dev->ep0state) {
1677 case EP0_DISCONNECT:
1683 DBG(dev, "USB suspend\n");
1684 dev->ep0state = EP0_SUSPEND;
1685 if (dev->gadget.speed != USB_SPEED_UNKNOWN
1687 && dev->driver->suspend) {
1688 spin_unlock(&dev->lock);
1689 dev->driver->suspend(&dev->gadget);
1690 spin_lock(&dev->lock);
1693 if (dev->ep0state != EP0_SUSPEND) {
1694 DBG(dev, "bogus USB resume %d\n",
1698 DBG(dev, "USB resume\n");
1699 dev->ep0state = EP0_IDLE;
1700 if (dev->gadget.speed != USB_SPEED_UNKNOWN
1702 && dev->driver->resume) {
1703 spin_unlock(&dev->lock);
1704 dev->driver->resume(&dev->gadget);
1705 spin_lock(&dev->lock);
1710 if (stat & INT_USBRESET) { /* hub reset done */
1712 INFO(dev, "USB reset done, gadget %s\n",
1713 dev->driver->driver.name);
1715 // and INT_ERR on some endpoint's crc/bitstuff/... problem
1718 /* progress ep0 setup, data, or status stages.
1719 * no transition {EP0_STATUS, EP0_STALL} --> EP0_IDLE; saves irqs
1721 if (stat & INT_SETUP) {
1726 if (stat & INT_STATUSNAK) {
1727 ACK(INT_STATUSNAK|INT_ENDPOINT0);
1728 if (dev->ep0state == EP0_IN) {
1732 writel(~(1<<0), ®s->EOP);
1733 dev->ep0state = EP0_STATUS;
1736 if (stat & INT_ENDPOINT0) {
1743 /* dma completion */
1744 if (stat & INT_MSTRDEND) { /* IN */
1746 ep = &dev->ep[UDC_MSTRD_ENDPOINT];
1748 dma_advance(dev, ep);
1750 if (stat & INT_MSTWREND) { /* OUT */
1752 ep = &dev->ep[UDC_MSTWR_ENDPOINT];
1754 dma_advance(dev, ep);
1756 if (stat & INT_MSTWRTMOUT) { /* OUT */
1757 ACK(INT_MSTWRTMOUT);
1758 ep = &dev->ep[UDC_MSTWR_ENDPOINT];
1760 ERROR(dev, "%s write timeout ?\n", ep->ep.name);
1761 // reset dma? then dma_advance()
1765 for (i = 1; i < 4; i++) {
1766 u32 tmp = INT_EPxDATASET(i);
1772 if (list_empty (&ep->queue))
1773 pio_irq_disable(dev, regs, i);
1783 (void)readl(®s->int_enable);
1784 spin_unlock(&dev->lock);
1786 DBG(dev, "unhandled irq status: %05x (%05x, %05x)\n", stat,
1787 readl(®s->int_status), dev->int_enable);
1788 return IRQ_RETVAL(handled);
1793 /*-------------------------------------------------------------------------*/
1795 static void gadget_release(struct device *_dev)
1797 struct goku_udc *dev = dev_get_drvdata(_dev);
1802 /* tear down the binding between this driver and the pci device */
1804 static void goku_remove(struct pci_dev *pdev)
1806 struct goku_udc *dev = pci_get_drvdata(pdev);
1808 DBG(dev, "%s\n", __FUNCTION__);
1810 BUG_ON(dev->driver);
1812 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1813 remove_proc_entry(proc_node_name, NULL);
1818 free_irq(pdev->irq, dev);
1821 if (dev->got_region)
1822 release_mem_region(pci_resource_start (pdev, 0),
1823 pci_resource_len (pdev, 0));
1825 pci_disable_device(pdev);
1826 device_unregister(&dev->gadget.dev);
1828 pci_set_drvdata(pdev, NULL);
1830 the_controller = NULL;
1832 INFO(dev, "unbind\n");
1835 /* wrap this driver around the specified pci device, but
1836 * don't respond over USB until a gadget driver binds to us.
1839 static int goku_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1841 struct goku_udc *dev = NULL;
1842 unsigned long resource, len;
1843 void __iomem *base = NULL;
1846 /* if you want to support more than one controller in a system,
1847 * usb_gadget_driver_{register,unregister}() must change.
1849 if (the_controller) {
1850 WARN(dev, "ignoring %s\n", pci_name(pdev));
1854 printk(KERN_ERR "Check PCI %s IRQ setup!\n", pci_name(pdev));
1859 /* alloc, and start init */
1860 dev = kmalloc (sizeof *dev, GFP_KERNEL);
1862 pr_debug("enomem %s\n", pci_name(pdev));
1867 memset(dev, 0, sizeof *dev);
1868 spin_lock_init(&dev->lock);
1870 dev->gadget.ops = &goku_ops;
1872 /* the "gadget" abstracts/virtualizes the controller */
1873 strcpy(dev->gadget.dev.bus_id, "gadget");
1874 dev->gadget.dev.parent = &pdev->dev;
1875 dev->gadget.dev.dma_mask = pdev->dev.dma_mask;
1876 dev->gadget.dev.release = gadget_release;
1877 dev->gadget.name = driver_name;
1879 /* now all the pci goodies ... */
1880 retval = pci_enable_device(pdev);
1882 DBG(dev, "can't enable, %d\n", retval);
1887 resource = pci_resource_start(pdev, 0);
1888 len = pci_resource_len(pdev, 0);
1889 if (!request_mem_region(resource, len, driver_name)) {
1890 DBG(dev, "controller already in use\n");
1894 dev->got_region = 1;
1896 base = ioremap_nocache(resource, len);
1898 DBG(dev, "can't map memory\n");
1902 dev->regs = (struct goku_udc_regs __iomem *) base;
1904 pci_set_drvdata(pdev, dev);
1905 INFO(dev, "%s\n", driver_desc);
1906 INFO(dev, "version: " DRIVER_VERSION " %s\n", dmastr());
1907 INFO(dev, "irq %d, pci mem %p\n", pdev->irq, base);
1909 /* init to known state, then setup irqs */
1912 if (request_irq(pdev->irq, goku_irq, IRQF_SHARED/*|IRQF_SAMPLE_RANDOM*/,
1913 driver_name, dev) != 0) {
1914 DBG(dev, "request interrupt %d failed\n", pdev->irq);
1920 pci_set_master(pdev);
1923 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1924 create_proc_read_entry(proc_node_name, 0, NULL, udc_proc_read, dev);
1928 the_controller = dev;
1929 device_register(&dev->gadget.dev);
1940 /*-------------------------------------------------------------------------*/
1942 static struct pci_device_id pci_ids [] = { {
1943 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
1945 .vendor = 0x102f, /* Toshiba */
1946 .device = 0x0107, /* this UDC */
1947 .subvendor = PCI_ANY_ID,
1948 .subdevice = PCI_ANY_ID,
1950 }, { /* end: all zeroes */ }
1952 MODULE_DEVICE_TABLE (pci, pci_ids);
1954 static struct pci_driver goku_pci_driver = {
1955 .name = (char *) driver_name,
1956 .id_table = pci_ids,
1958 .probe = goku_probe,
1959 .remove = goku_remove,
1961 /* FIXME add power management support */
1964 static int __init init (void)
1966 return pci_register_driver (&goku_pci_driver);
1970 static void __exit cleanup (void)
1972 pci_unregister_driver (&goku_pci_driver);
1974 module_exit (cleanup);