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/sched.h>
33 #include <linux/slab.h>
34 #include <linux/smp_lock.h>
35 #include <linux/errno.h>
36 #include <linux/init.h>
37 #include <linux/timer.h>
38 #include <linux/list.h>
39 #include <linux/interrupt.h>
40 #include <linux/proc_fs.h>
41 #include <linux/device.h>
42 #include <linux/usb/ch9.h>
43 #include <linux/usb_gadget.h>
45 #include <asm/byteorder.h>
48 #include <asm/system.h>
49 #include <asm/unaligned.h>
54 #define DRIVER_DESC "TC86C001 USB Device Controller"
55 #define DRIVER_VERSION "30-Oct 2003"
57 #define DMA_ADDR_INVALID (~(dma_addr_t)0)
59 static const char driver_name [] = "goku_udc";
60 static const char driver_desc [] = DRIVER_DESC;
62 MODULE_AUTHOR("source@mvista.com");
63 MODULE_DESCRIPTION(DRIVER_DESC);
64 MODULE_LICENSE("GPL");
68 * IN dma behaves ok under testing, though the IN-dma abort paths don't
69 * seem to behave quite as expected. Used by default.
71 * OUT dma documents design problems handling the common "short packet"
72 * transfer termination policy; it couldn't be enabled by default, even
73 * if the OUT-dma abort problems had a resolution.
75 static unsigned use_dma = 1;
78 //#include <linux/moduleparam.h>
79 /* "modprobe goku_udc use_dma=1" etc
81 * 1 to use IN dma only (normal operation)
82 * 2 to use IN and OUT dma
84 module_param(use_dma, uint, S_IRUGO);
87 /*-------------------------------------------------------------------------*/
89 static void nuke(struct goku_ep *, int status);
92 command(struct goku_udc_regs __iomem *regs, int command, unsigned epnum)
94 writel(COMMAND_EP(epnum) | command, ®s->Command);
99 goku_ep_enable(struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
101 struct goku_udc *dev;
107 ep = container_of(_ep, struct goku_ep, ep);
108 if (!_ep || !desc || ep->desc
109 || desc->bDescriptorType != USB_DT_ENDPOINT)
112 if (ep == &dev->ep[0])
114 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
116 if (ep->num != (desc->bEndpointAddress & 0x0f))
119 switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
120 case USB_ENDPOINT_XFER_BULK:
121 case USB_ENDPOINT_XFER_INT:
127 if ((readl(ep->reg_status) & EPxSTATUS_EP_MASK)
128 != EPxSTATUS_EP_INVALID)
131 /* enabling the no-toggle interrupt mode would need an api hook */
133 max = le16_to_cpu(get_unaligned(&desc->wMaxPacketSize));
143 mode |= 2 << 1; /* bulk, or intr-with-toggle */
145 /* ep1/ep2 dma direction is chosen early; it works in the other
146 * direction, with pio. be cautious with out-dma.
148 ep->is_in = (USB_DIR_IN & desc->bEndpointAddress) != 0;
151 ep->dma = (use_dma != 0) && (ep->num == UDC_MSTRD_ENDPOINT);
153 ep->dma = (use_dma == 2) && (ep->num == UDC_MSTWR_ENDPOINT);
155 DBG(dev, "%s out-dma hides short packets\n",
159 spin_lock_irqsave(&ep->dev->lock, flags);
161 /* ep1 and ep2 can do double buffering and/or dma */
163 struct goku_udc_regs __iomem *regs = ep->dev->regs;
166 /* double buffer except (for now) with pio in */
167 tmp = ((ep->dma || !ep->is_in)
168 ? 0x10 /* double buffered */
169 : 0x11 /* single buffer */
171 tmp |= readl(®s->EPxSingle);
172 writel(tmp, ®s->EPxSingle);
174 tmp = (ep->dma ? 0x10/*dma*/ : 0x11/*pio*/) << ep->num;
175 tmp |= readl(®s->EPxBCS);
176 writel(tmp, ®s->EPxBCS);
178 writel(mode, ep->reg_mode);
179 command(ep->dev->regs, COMMAND_RESET, ep->num);
180 ep->ep.maxpacket = max;
183 spin_unlock_irqrestore(&ep->dev->lock, flags);
185 DBG(dev, "enable %s %s %s maxpacket %u\n", ep->ep.name,
186 ep->is_in ? "IN" : "OUT",
187 ep->dma ? "dma" : "pio",
193 static void ep_reset(struct goku_udc_regs __iomem *regs, struct goku_ep *ep)
195 struct goku_udc *dev = ep->dev;
198 command(regs, COMMAND_INVALID, ep->num);
200 if (ep->num == UDC_MSTWR_ENDPOINT)
201 dev->int_enable &= ~(INT_MSTWREND
203 else if (ep->num == UDC_MSTRD_ENDPOINT)
204 dev->int_enable &= ~INT_MSTRDEND;
205 dev->int_enable &= ~INT_EPxDATASET (ep->num);
207 dev->int_enable &= ~INT_EP0;
208 writel(dev->int_enable, ®s->int_enable);
209 readl(®s->int_enable);
211 struct goku_udc_regs __iomem *r = ep->dev->regs;
214 tmp = readl(&r->EPxSingle);
215 tmp &= ~(0x11 << ep->num);
216 writel(tmp, &r->EPxSingle);
218 tmp = readl(&r->EPxBCS);
219 tmp &= ~(0x11 << ep->num);
220 writel(tmp, &r->EPxBCS);
222 /* reset dma in case we're still using it */
226 master = readl(®s->dma_master) & MST_RW_BITS;
227 if (ep->num == UDC_MSTWR_ENDPOINT) {
228 master &= ~MST_W_BITS;
229 master |= MST_WR_RESET;
231 master &= ~MST_R_BITS;
232 master |= MST_RD_RESET;
234 writel(master, ®s->dma_master);
238 ep->ep.maxpacket = MAX_FIFO_SIZE;
245 static int goku_ep_disable(struct usb_ep *_ep)
248 struct goku_udc *dev;
251 ep = container_of(_ep, struct goku_ep, ep);
252 if (!_ep || !ep->desc)
255 if (dev->ep0state == EP0_SUSPEND)
258 VDBG(dev, "disable %s\n", _ep->name);
260 spin_lock_irqsave(&dev->lock, flags);
261 nuke(ep, -ESHUTDOWN);
262 ep_reset(dev->regs, ep);
263 spin_unlock_irqrestore(&dev->lock, flags);
268 /*-------------------------------------------------------------------------*/
270 static struct usb_request *
271 goku_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
273 struct goku_request *req;
277 req = kzalloc(sizeof *req, gfp_flags);
281 req->req.dma = DMA_ADDR_INVALID;
282 INIT_LIST_HEAD(&req->queue);
287 goku_free_request(struct usb_ep *_ep, struct usb_request *_req)
289 struct goku_request *req;
294 req = container_of(_req, struct goku_request, req);
295 WARN_ON(!list_empty(&req->queue));
299 /*-------------------------------------------------------------------------*/
303 /* many common platforms have dma-coherent caches, which means that it's
304 * safe to use kmalloc() memory for all i/o buffers without using any
305 * cache flushing calls. (unless you're trying to share cache lines
306 * between dma and non-dma activities, which is a slow idea in any case.)
308 * other platforms need more care, with 2.6 having a moderately general
309 * solution except for the common "buffer is smaller than a page" case.
311 #if defined(CONFIG_X86)
314 #elif defined(CONFIG_MIPS) && !defined(CONFIG_DMA_NONCOHERENT)
317 #elif defined(CONFIG_PPC) && !defined(CONFIG_NOT_COHERENT_CACHE)
322 /* allocating buffers this way eliminates dma mapping overhead, which
323 * on some platforms will mean eliminating a per-io buffer copy. with
324 * some kinds of system caches, further tweaks may still be needed.
327 goku_alloc_buffer(struct usb_ep *_ep, unsigned bytes,
328 dma_addr_t *dma, gfp_t gfp_flags)
333 ep = container_of(_ep, struct goku_ep, ep);
336 *dma = DMA_ADDR_INVALID;
338 #if defined(USE_KMALLOC)
339 retval = kmalloc(bytes, gfp_flags);
341 *dma = virt_to_phys(retval);
344 /* the main problem with this call is that it wastes memory
345 * on typical 1/N page allocations: it allocates 1-N pages.
347 #warning Using dma_alloc_coherent even with buffers smaller than a page.
348 retval = dma_alloc_coherent(&ep->dev->pdev->dev,
349 bytes, dma, gfp_flags);
351 retval = kmalloc(bytes, gfp_flags);
357 goku_free_buffer(struct usb_ep *_ep, void *buf, dma_addr_t dma, unsigned bytes)
359 /* free memory into the right allocator */
361 if (dma != DMA_ADDR_INVALID) {
364 ep = container_of(_ep, struct goku_ep, ep);
367 dma_free_coherent(&ep->dev->pdev->dev, bytes, buf, dma);
373 /*-------------------------------------------------------------------------*/
376 done(struct goku_ep *ep, struct goku_request *req, int status)
378 struct goku_udc *dev;
379 unsigned stopped = ep->stopped;
381 list_del_init(&req->queue);
383 if (likely(req->req.status == -EINPROGRESS))
384 req->req.status = status;
386 status = req->req.status;
390 pci_unmap_single(dev->pdev, req->req.dma, req->req.length,
391 ep->is_in ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
392 req->req.dma = DMA_ADDR_INVALID;
397 if (status && status != -ESHUTDOWN)
399 VDBG(dev, "complete %s req %p stat %d len %u/%u\n",
400 ep->ep.name, &req->req, status,
401 req->req.actual, req->req.length);
403 /* don't modify queue heads during completion callback */
405 spin_unlock(&dev->lock);
406 req->req.complete(&ep->ep, &req->req);
407 spin_lock(&dev->lock);
408 ep->stopped = stopped;
411 /*-------------------------------------------------------------------------*/
414 write_packet(u32 __iomem *fifo, u8 *buf, struct goku_request *req, unsigned max)
416 unsigned length, count;
418 length = min(req->req.length - req->req.actual, max);
419 req->req.actual += length;
422 while (likely(count--))
423 writel(*buf++, fifo);
427 // return: 0 = still running, 1 = completed, negative = errno
428 static int write_fifo(struct goku_ep *ep, struct goku_request *req)
430 struct goku_udc *dev = ep->dev;
436 tmp = readl(&dev->regs->DataSet);
437 buf = req->req.buf + req->req.actual;
441 if (unlikely(ep->num == 0 && dev->ep0state != EP0_IN))
444 /* NOTE: just single-buffered PIO-IN for now. */
445 if (unlikely((tmp & DATASET_A(ep->num)) != 0))
448 /* clear our "packet available" irq */
450 writel(~INT_EPxDATASET(ep->num), &dev->regs->int_status);
452 count = write_packet(ep->reg_fifo, buf, req, ep->ep.maxpacket);
454 /* last packet often short (sometimes a zlp, especially on ep0) */
455 if (unlikely(count != ep->ep.maxpacket)) {
456 writel(~(1<<ep->num), &dev->regs->EOP);
458 dev->ep[0].stopped = 1;
459 dev->ep0state = EP0_STATUS;
463 if (likely(req->req.length != req->req.actual)
469 #if 0 /* printk seemed to trash is_last...*/
471 VDBG(dev, "wrote %s %u bytes%s IN %u left %p\n",
472 ep->ep.name, count, is_last ? "/last" : "",
473 req->req.length - req->req.actual, req);
476 /* requests complete when all IN data is in the FIFO,
477 * or sometimes later, if a zlp was needed.
487 static int read_fifo(struct goku_ep *ep, struct goku_request *req)
489 struct goku_udc_regs __iomem *regs;
492 unsigned bufferspace, is_short, dbuff;
494 regs = ep->dev->regs;
496 buf = req->req.buf + req->req.actual;
499 if (unlikely(ep->num == 0 && ep->dev->ep0state != EP0_OUT))
502 dbuff = (ep->num == 1 || ep->num == 2);
504 /* ack dataset irq matching the status we'll handle */
506 writel(~INT_EPxDATASET(ep->num), ®s->int_status);
508 set = readl(®s->DataSet) & DATASET_AB(ep->num);
509 size = readl(®s->EPxSizeLA[ep->num]);
510 bufferspace = req->req.length - req->req.actual;
512 /* usually do nothing without an OUT packet */
513 if (likely(ep->num != 0 || bufferspace != 0)) {
514 if (unlikely(set == 0))
516 /* use ep1/ep2 double-buffering for OUT */
517 if (!(size & PACKET_ACTIVE))
518 size = readl(®s->EPxSizeLB[ep->num]);
519 if (!(size & PACKET_ACTIVE)) // "can't happen"
521 size &= DATASIZE; /* EPxSizeH == 0 */
523 /* ep0out no-out-data case for set_config, etc */
527 /* read all bytes from this packet */
528 req->req.actual += size;
529 is_short = (size < ep->ep.maxpacket);
531 VDBG(ep->dev, "read %s %u bytes%s OUT req %p %u/%u\n",
532 ep->ep.name, size, is_short ? "/S" : "",
533 req, req->req.actual, req->req.length);
535 while (likely(size-- != 0)) {
536 u8 byte = (u8) readl(ep->reg_fifo);
538 if (unlikely(bufferspace == 0)) {
539 /* this happens when the driver's buffer
540 * is smaller than what the host sent.
541 * discard the extra data in this packet.
543 if (req->req.status != -EOVERFLOW)
544 DBG(ep->dev, "%s overflow %u\n",
546 req->req.status = -EOVERFLOW;
554 if (unlikely(is_short || req->req.actual == req->req.length)) {
555 if (unlikely(ep->num == 0)) {
556 /* non-control endpoints now usable? */
557 if (ep->dev->req_config)
558 writel(ep->dev->configured
559 ? USBSTATE_CONFIGURED
562 /* ep0out status stage */
563 writel(~(1<<0), ®s->EOP);
565 ep->dev->ep0state = EP0_STATUS;
569 /* empty the second buffer asap */
570 if (dbuff && !list_empty(&ep->queue)) {
571 req = list_entry(ep->queue.next,
572 struct goku_request, queue);
582 pio_irq_enable(struct goku_udc *dev,
583 struct goku_udc_regs __iomem *regs, int epnum)
585 dev->int_enable |= INT_EPxDATASET (epnum);
586 writel(dev->int_enable, ®s->int_enable);
587 /* write may still be posted */
591 pio_irq_disable(struct goku_udc *dev,
592 struct goku_udc_regs __iomem *regs, int epnum)
594 dev->int_enable &= ~INT_EPxDATASET (epnum);
595 writel(dev->int_enable, ®s->int_enable);
596 /* write may still be posted */
600 pio_advance(struct goku_ep *ep)
602 struct goku_request *req;
604 if (unlikely(list_empty (&ep->queue)))
606 req = list_entry(ep->queue.next, struct goku_request, queue);
607 (ep->is_in ? write_fifo : read_fifo)(ep, req);
611 /*-------------------------------------------------------------------------*/
613 // return: 0 = q running, 1 = q stopped, negative = errno
614 static int start_dma(struct goku_ep *ep, struct goku_request *req)
616 struct goku_udc_regs __iomem *regs = ep->dev->regs;
618 u32 start = req->req.dma;
619 u32 end = start + req->req.length - 1;
621 master = readl(®s->dma_master) & MST_RW_BITS;
623 /* re-init the bits affecting IN dma; careful with zlps */
624 if (likely(ep->is_in)) {
625 if (unlikely(master & MST_RD_ENA)) {
626 DBG (ep->dev, "start, IN active dma %03x!!\n",
630 writel(end, ®s->in_dma_end);
631 writel(start, ®s->in_dma_start);
633 master &= ~MST_R_BITS;
634 if (unlikely(req->req.length == 0))
635 master = MST_RD_ENA | MST_RD_EOPB;
636 else if ((req->req.length % ep->ep.maxpacket) != 0
638 master = MST_RD_ENA | MST_EOPB_ENA;
640 master = MST_RD_ENA | MST_EOPB_DIS;
642 ep->dev->int_enable |= INT_MSTRDEND;
644 /* Goku DMA-OUT merges short packets, which plays poorly with
645 * protocols where short packets mark the transfer boundaries.
646 * The chip supports a nonstandard policy with INT_MSTWRTMOUT,
647 * ending transfers after 3 SOFs; we don't turn it on.
650 if (unlikely(master & MST_WR_ENA)) {
651 DBG (ep->dev, "start, OUT active dma %03x!!\n",
655 writel(end, ®s->out_dma_end);
656 writel(start, ®s->out_dma_start);
658 master &= ~MST_W_BITS;
659 master |= MST_WR_ENA | MST_TIMEOUT_DIS;
661 ep->dev->int_enable |= INT_MSTWREND|INT_MSTWRTMOUT;
664 writel(master, ®s->dma_master);
665 writel(ep->dev->int_enable, ®s->int_enable);
669 static void dma_advance(struct goku_udc *dev, struct goku_ep *ep)
671 struct goku_request *req;
672 struct goku_udc_regs __iomem *regs = ep->dev->regs;
675 master = readl(®s->dma_master);
677 if (unlikely(list_empty(&ep->queue))) {
680 dev->int_enable &= ~INT_MSTRDEND;
682 dev->int_enable &= ~(INT_MSTWREND|INT_MSTWRTMOUT);
683 writel(dev->int_enable, ®s->int_enable);
686 req = list_entry(ep->queue.next, struct goku_request, queue);
688 /* normal hw dma completion (not abort) */
689 if (likely(ep->is_in)) {
690 if (unlikely(master & MST_RD_ENA))
692 req->req.actual = readl(®s->in_dma_current);
694 if (unlikely(master & MST_WR_ENA))
697 /* hardware merges short packets, and also hides packet
698 * overruns. a partial packet MAY be in the fifo here.
700 req->req.actual = readl(®s->out_dma_current);
702 req->req.actual -= req->req.dma;
706 VDBG(dev, "done %s %s dma, %u/%u bytes, req %p\n",
707 ep->ep.name, ep->is_in ? "IN" : "OUT",
708 req->req.actual, req->req.length, req);
711 if (list_empty(&ep->queue))
713 req = list_entry(ep->queue.next, struct goku_request, queue);
714 (void) start_dma(ep, req);
717 static void abort_dma(struct goku_ep *ep, int status)
719 struct goku_udc_regs __iomem *regs = ep->dev->regs;
720 struct goku_request *req;
723 /* NAK future host requests, hoping the implicit delay lets the
724 * dma engine finish reading (or writing) its latest packet and
725 * empty the dma buffer (up to 16 bytes).
727 * This avoids needing to clean up a partial packet in the fifo;
728 * we can't do that for IN without side effects to HALT and TOGGLE.
730 command(regs, COMMAND_FIFO_DISABLE, ep->num);
731 req = list_entry(ep->queue.next, struct goku_request, queue);
732 master = readl(®s->dma_master) & MST_RW_BITS;
734 /* FIXME using these resets isn't usably documented. this may
735 * not work unless it's followed by disabling the endpoint.
737 * FIXME the OUT reset path doesn't even behave consistently.
740 if (unlikely((readl(®s->dma_master) & MST_RD_ENA) == 0))
742 curr = readl(®s->in_dma_current);
744 writel(curr, ®s->in_dma_end);
745 writel(curr, ®s->in_dma_start);
747 master &= ~MST_R_BITS;
748 master |= MST_RD_RESET;
749 writel(master, ®s->dma_master);
751 if (readl(®s->dma_master) & MST_RD_ENA)
752 DBG(ep->dev, "IN dma active after reset!\n");
755 if (unlikely((readl(®s->dma_master) & MST_WR_ENA) == 0))
757 curr = readl(®s->out_dma_current);
759 writel(curr, ®s->out_dma_end);
760 writel(curr, ®s->out_dma_start);
762 master &= ~MST_W_BITS;
763 master |= MST_WR_RESET;
764 writel(master, ®s->dma_master);
766 if (readl(®s->dma_master) & MST_WR_ENA)
767 DBG(ep->dev, "OUT dma active after reset!\n");
769 req->req.actual = (curr - req->req.dma) + 1;
770 req->req.status = status;
772 VDBG(ep->dev, "%s %s %s %d/%d\n", __FUNCTION__, ep->ep.name,
773 ep->is_in ? "IN" : "OUT",
774 req->req.actual, req->req.length);
776 command(regs, COMMAND_FIFO_ENABLE, ep->num);
781 /* dma already completed; no abort needed */
782 command(regs, COMMAND_FIFO_ENABLE, ep->num);
783 req->req.actual = req->req.length;
787 /*-------------------------------------------------------------------------*/
790 goku_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
792 struct goku_request *req;
794 struct goku_udc *dev;
798 /* always require a cpu-view buffer so pio works */
799 req = container_of(_req, struct goku_request, req);
800 if (unlikely(!_req || !_req->complete
801 || !_req->buf || !list_empty(&req->queue)))
803 ep = container_of(_ep, struct goku_ep, ep);
804 if (unlikely(!_ep || (!ep->desc && ep->num != 0)))
807 if (unlikely(!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN))
810 /* can't touch registers when suspended */
811 if (dev->ep0state == EP0_SUSPEND)
814 /* set up dma mapping in case the caller didn't */
815 if (ep->dma && _req->dma == DMA_ADDR_INVALID) {
816 _req->dma = pci_map_single(dev->pdev, _req->buf, _req->length,
817 ep->is_in ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
822 VDBG(dev, "%s queue req %p, len %u buf %p\n",
823 _ep->name, _req, _req->length, _req->buf);
826 spin_lock_irqsave(&dev->lock, flags);
828 _req->status = -EINPROGRESS;
831 /* for ep0 IN without premature status, zlp is required and
832 * writing EOP starts the status stage (OUT).
834 if (unlikely(ep->num == 0 && ep->is_in))
837 /* kickstart this i/o queue? */
839 if (list_empty(&ep->queue) && likely(!ep->stopped)) {
840 /* dma: done after dma completion IRQ (or error)
841 * pio: done after last fifo operation
844 status = start_dma(ep, req);
846 status = (ep->is_in ? write_fifo : read_fifo)(ep, req);
848 if (unlikely(status != 0)) {
854 } /* else pio or dma irq handler advances the queue. */
856 if (likely(req != 0))
857 list_add_tail(&req->queue, &ep->queue);
859 if (likely(!list_empty(&ep->queue))
860 && likely(ep->num != 0)
862 && !(dev->int_enable & INT_EPxDATASET (ep->num)))
863 pio_irq_enable(dev, dev->regs, ep->num);
865 spin_unlock_irqrestore(&dev->lock, flags);
867 /* pci writes may still be posted */
871 /* dequeue ALL requests */
872 static void nuke(struct goku_ep *ep, int status)
874 struct goku_request *req;
877 if (list_empty(&ep->queue))
880 abort_dma(ep, status);
881 while (!list_empty(&ep->queue)) {
882 req = list_entry(ep->queue.next, struct goku_request, queue);
883 done(ep, req, status);
887 /* dequeue JUST ONE request */
888 static int goku_dequeue(struct usb_ep *_ep, struct usb_request *_req)
890 struct goku_request *req;
892 struct goku_udc *dev;
895 ep = container_of(_ep, struct goku_ep, ep);
896 if (!_ep || !_req || (!ep->desc && ep->num != 0))
902 /* we can't touch (dma) registers when suspended */
903 if (dev->ep0state == EP0_SUSPEND)
906 VDBG(dev, "%s %s %s %s %p\n", __FUNCTION__, _ep->name,
907 ep->is_in ? "IN" : "OUT",
908 ep->dma ? "dma" : "pio",
911 spin_lock_irqsave(&dev->lock, flags);
913 /* make sure it's actually queued on this endpoint */
914 list_for_each_entry (req, &ep->queue, queue) {
915 if (&req->req == _req)
918 if (&req->req != _req) {
919 spin_unlock_irqrestore (&dev->lock, flags);
923 if (ep->dma && ep->queue.next == &req->queue && !ep->stopped) {
924 abort_dma(ep, -ECONNRESET);
925 done(ep, req, -ECONNRESET);
926 dma_advance(dev, ep);
927 } else if (!list_empty(&req->queue))
928 done(ep, req, -ECONNRESET);
931 spin_unlock_irqrestore(&dev->lock, flags);
933 return req ? 0 : -EOPNOTSUPP;
936 /*-------------------------------------------------------------------------*/
938 static void goku_clear_halt(struct goku_ep *ep)
940 // assert (ep->num !=0)
941 VDBG(ep->dev, "%s clear halt\n", ep->ep.name);
942 command(ep->dev->regs, COMMAND_SETDATA0, ep->num);
943 command(ep->dev->regs, COMMAND_STALL_CLEAR, ep->num);
947 struct goku_request *req;
949 if (list_empty(&ep->queue))
951 req = list_entry(ep->queue.next, struct goku_request,
953 (void) start_dma(ep, req);
959 static int goku_set_halt(struct usb_ep *_ep, int value)
967 ep = container_of (_ep, struct goku_ep, ep);
971 ep->dev->ep0state = EP0_STALL;
972 ep->dev->ep[0].stopped = 1;
976 /* don't change EPxSTATUS_EP_INVALID to READY */
977 } else if (!ep->desc) {
978 DBG(ep->dev, "%s %s inactive?\n", __FUNCTION__, ep->ep.name);
982 spin_lock_irqsave(&ep->dev->lock, flags);
983 if (!list_empty(&ep->queue))
985 else if (ep->is_in && value
986 /* data in (either) packet buffer? */
987 && (readl(&ep->dev->regs->DataSet)
988 & DATASET_AB(ep->num)))
994 VDBG(ep->dev, "%s set halt\n", ep->ep.name);
995 command(ep->dev->regs, COMMAND_STALL, ep->num);
996 readl(ep->reg_status);
998 spin_unlock_irqrestore(&ep->dev->lock, flags);
1002 static int goku_fifo_status(struct usb_ep *_ep)
1005 struct goku_udc_regs __iomem *regs;
1010 ep = container_of(_ep, struct goku_ep, ep);
1012 /* size is only reported sanely for OUT */
1016 /* ignores 16-byte dma buffer; SizeH == 0 */
1017 regs = ep->dev->regs;
1018 size = readl(®s->EPxSizeLA[ep->num]) & DATASIZE;
1019 size += readl(®s->EPxSizeLB[ep->num]) & DATASIZE;
1020 VDBG(ep->dev, "%s %s %u\n", __FUNCTION__, ep->ep.name, size);
1024 static void goku_fifo_flush(struct usb_ep *_ep)
1027 struct goku_udc_regs __iomem *regs;
1032 ep = container_of(_ep, struct goku_ep, ep);
1033 VDBG(ep->dev, "%s %s\n", __FUNCTION__, ep->ep.name);
1035 /* don't change EPxSTATUS_EP_INVALID to READY */
1036 if (!ep->desc && ep->num != 0) {
1037 DBG(ep->dev, "%s %s inactive?\n", __FUNCTION__, ep->ep.name);
1041 regs = ep->dev->regs;
1042 size = readl(®s->EPxSizeLA[ep->num]);
1045 /* Non-desirable behavior: FIFO_CLEAR also clears the
1046 * endpoint halt feature. For OUT, we _could_ just read
1047 * the bytes out (PIO, if !ep->dma); for in, no choice.
1050 command(regs, COMMAND_FIFO_CLEAR, ep->num);
1053 static struct usb_ep_ops goku_ep_ops = {
1054 .enable = goku_ep_enable,
1055 .disable = goku_ep_disable,
1057 .alloc_request = goku_alloc_request,
1058 .free_request = goku_free_request,
1060 .alloc_buffer = goku_alloc_buffer,
1061 .free_buffer = goku_free_buffer,
1063 .queue = goku_queue,
1064 .dequeue = goku_dequeue,
1066 .set_halt = goku_set_halt,
1067 .fifo_status = goku_fifo_status,
1068 .fifo_flush = goku_fifo_flush,
1071 /*-------------------------------------------------------------------------*/
1073 static int goku_get_frame(struct usb_gadget *_gadget)
1078 static const struct usb_gadget_ops goku_ops = {
1079 .get_frame = goku_get_frame,
1084 /*-------------------------------------------------------------------------*/
1086 static inline char *dmastr(void)
1089 return "(dma disabled)";
1090 else if (use_dma == 2)
1091 return "(dma IN and OUT)";
1096 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1098 static const char proc_node_name [] = "driver/udc";
1100 #define FOURBITS "%s%s%s%s"
1101 #define EIGHTBITS FOURBITS FOURBITS
1104 dump_intmask(const char *label, u32 mask, char **next, unsigned *size)
1108 /* int_status is the same format ... */
1109 t = scnprintf(*next, *size,
1110 "%s %05X =" FOURBITS EIGHTBITS EIGHTBITS "\n",
1112 (mask & INT_PWRDETECT) ? " power" : "",
1113 (mask & INT_SYSERROR) ? " sys" : "",
1114 (mask & INT_MSTRDEND) ? " in-dma" : "",
1115 (mask & INT_MSTWRTMOUT) ? " wrtmo" : "",
1117 (mask & INT_MSTWREND) ? " out-dma" : "",
1118 (mask & INT_MSTWRSET) ? " wrset" : "",
1119 (mask & INT_ERR) ? " err" : "",
1120 (mask & INT_SOF) ? " sof" : "",
1122 (mask & INT_EP3NAK) ? " ep3nak" : "",
1123 (mask & INT_EP2NAK) ? " ep2nak" : "",
1124 (mask & INT_EP1NAK) ? " ep1nak" : "",
1125 (mask & INT_EP3DATASET) ? " ep3" : "",
1127 (mask & INT_EP2DATASET) ? " ep2" : "",
1128 (mask & INT_EP1DATASET) ? " ep1" : "",
1129 (mask & INT_STATUSNAK) ? " ep0snak" : "",
1130 (mask & INT_STATUS) ? " ep0status" : "",
1132 (mask & INT_SETUP) ? " setup" : "",
1133 (mask & INT_ENDPOINT0) ? " ep0" : "",
1134 (mask & INT_USBRESET) ? " reset" : "",
1135 (mask & INT_SUSPEND) ? " suspend" : "");
1142 udc_proc_read(char *buffer, char **start, off_t off, int count,
1143 int *eof, void *_dev)
1146 struct goku_udc *dev = _dev;
1147 struct goku_udc_regs __iomem *regs = dev->regs;
1149 unsigned size = count;
1150 unsigned long flags;
1151 int i, t, is_usb_connected;
1157 local_irq_save(flags);
1159 /* basic device status */
1160 tmp = readl(®s->power_detect);
1161 is_usb_connected = tmp & PW_DETECT;
1162 t = scnprintf(next, size,
1164 "%s version: %s %s\n"
1165 "Gadget driver: %s\n"
1168 pci_name(dev->pdev), driver_desc,
1169 driver_name, DRIVER_VERSION, dmastr(),
1170 dev->driver ? dev->driver->driver.name : "(none)",
1172 ? ((tmp & PW_PULLUP) ? "full speed" : "powered")
1175 switch(dev->ep0state){
1176 case EP0_DISCONNECT: tmp = "ep0_disconnect"; break;
1177 case EP0_IDLE: tmp = "ep0_idle"; break;
1178 case EP0_IN: tmp = "ep0_in"; break;
1179 case EP0_OUT: tmp = "ep0_out"; break;
1180 case EP0_STATUS: tmp = "ep0_status"; break;
1181 case EP0_STALL: tmp = "ep0_stall"; break;
1182 case EP0_SUSPEND: tmp = "ep0_suspend"; break;
1183 default: tmp = "ep0_?"; break;
1189 dump_intmask("int_status", readl(®s->int_status), &next, &size);
1190 dump_intmask("int_enable", readl(®s->int_enable), &next, &size);
1192 if (!is_usb_connected || !dev->driver || (tmp & PW_PULLUP) == 0)
1195 /* registers for (active) device and ep0 */
1196 t = scnprintf(next, size, "\nirqs %lu\ndataset %02x "
1197 "single.bcs %02x.%02x state %x addr %u\n",
1198 dev->irqs, readl(®s->DataSet),
1199 readl(®s->EPxSingle), readl(®s->EPxBCS),
1200 readl(®s->UsbState),
1201 readl(®s->address));
1205 tmp = readl(®s->dma_master);
1206 t = scnprintf(next, size,
1207 "dma %03X =" EIGHTBITS "%s %s\n", tmp,
1208 (tmp & MST_EOPB_DIS) ? " eopb-" : "",
1209 (tmp & MST_EOPB_ENA) ? " eopb+" : "",
1210 (tmp & MST_TIMEOUT_DIS) ? " tmo-" : "",
1211 (tmp & MST_TIMEOUT_ENA) ? " tmo+" : "",
1213 (tmp & MST_RD_EOPB) ? " eopb" : "",
1214 (tmp & MST_RD_RESET) ? " in_reset" : "",
1215 (tmp & MST_WR_RESET) ? " out_reset" : "",
1216 (tmp & MST_RD_ENA) ? " IN" : "",
1218 (tmp & MST_WR_ENA) ? " OUT" : "",
1219 (tmp & MST_CONNECTION)
1225 /* dump endpoint queues */
1226 for (i = 0; i < 4; i++) {
1227 struct goku_ep *ep = &dev->ep [i];
1228 struct goku_request *req;
1234 tmp = readl(ep->reg_status);
1235 t = scnprintf(next, size,
1236 "%s %s max %u %s, irqs %lu, "
1237 "status %02x (%s) " FOURBITS "\n",
1239 ep->is_in ? "in" : "out",
1241 ep->dma ? "dma" : "pio",
1244 switch (tmp & EPxSTATUS_EP_MASK) {
1245 case EPxSTATUS_EP_READY:
1247 case EPxSTATUS_EP_DATAIN:
1248 s = "packet"; break;
1249 case EPxSTATUS_EP_FULL:
1251 case EPxSTATUS_EP_TX_ERR: // host will retry
1252 s = "tx_err"; break;
1253 case EPxSTATUS_EP_RX_ERR:
1254 s = "rx_err"; break;
1255 case EPxSTATUS_EP_BUSY: /* ep0 only */
1257 case EPxSTATUS_EP_STALL:
1259 case EPxSTATUS_EP_INVALID: // these "can't happen"
1260 s = "invalid"; break;
1264 (tmp & EPxSTATUS_TOGGLE) ? "data1" : "data0",
1265 (tmp & EPxSTATUS_SUSPEND) ? " suspend" : "",
1266 (tmp & EPxSTATUS_FIFO_DISABLE) ? " disable" : "",
1267 (tmp & EPxSTATUS_STAGE_ERROR) ? " ep0stat" : ""
1269 if (t <= 0 || t > size)
1274 if (list_empty(&ep->queue)) {
1275 t = scnprintf(next, size, "\t(nothing queued)\n");
1276 if (t <= 0 || t > size)
1282 list_for_each_entry(req, &ep->queue, queue) {
1283 if (ep->dma && req->queue.prev == &ep->queue) {
1284 if (i == UDC_MSTRD_ENDPOINT)
1285 tmp = readl(®s->in_dma_current);
1287 tmp = readl(®s->out_dma_current);
1288 tmp -= req->req.dma;
1291 tmp = req->req.actual;
1293 t = scnprintf(next, size,
1294 "\treq %p len %u/%u buf %p\n",
1295 &req->req, tmp, req->req.length,
1297 if (t <= 0 || t > size)
1305 local_irq_restore(flags);
1307 return count - size;
1310 #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
1312 /*-------------------------------------------------------------------------*/
1314 static void udc_reinit (struct goku_udc *dev)
1316 static char *names [] = { "ep0", "ep1-bulk", "ep2-bulk", "ep3-bulk" };
1320 INIT_LIST_HEAD (&dev->gadget.ep_list);
1321 dev->gadget.ep0 = &dev->ep [0].ep;
1322 dev->gadget.speed = USB_SPEED_UNKNOWN;
1323 dev->ep0state = EP0_DISCONNECT;
1326 for (i = 0; i < 4; i++) {
1327 struct goku_ep *ep = &dev->ep[i];
1330 ep->ep.name = names[i];
1331 ep->reg_fifo = &dev->regs->ep_fifo [i];
1332 ep->reg_status = &dev->regs->ep_status [i];
1333 ep->reg_mode = &dev->regs->ep_mode[i];
1335 ep->ep.ops = &goku_ep_ops;
1336 list_add_tail (&ep->ep.ep_list, &dev->gadget.ep_list);
1338 INIT_LIST_HEAD (&ep->queue);
1343 dev->ep[0].reg_mode = NULL;
1344 dev->ep[0].ep.maxpacket = MAX_EP0_SIZE;
1345 list_del_init (&dev->ep[0].ep.ep_list);
1348 static void udc_reset(struct goku_udc *dev)
1350 struct goku_udc_regs __iomem *regs = dev->regs;
1352 writel(0, ®s->power_detect);
1353 writel(0, ®s->int_enable);
1354 readl(®s->int_enable);
1355 dev->int_enable = 0;
1357 /* deassert reset, leave USB D+ at hi-Z (no pullup)
1358 * don't let INT_PWRDETECT sequence begin
1361 writel(PW_RESETB, ®s->power_detect);
1362 readl(®s->int_enable);
1365 static void ep0_start(struct goku_udc *dev)
1367 struct goku_udc_regs __iomem *regs = dev->regs;
1370 VDBG(dev, "%s\n", __FUNCTION__);
1374 //writel(MST_EOPB_ENA | MST_TIMEOUT_ENA, ®s->dma_master);
1376 /* hw handles set_address, set_feature, get_status; maybe more */
1377 writel( G_REQMODE_SET_INTF | G_REQMODE_GET_INTF
1378 | G_REQMODE_SET_CONF | G_REQMODE_GET_CONF
1379 | G_REQMODE_GET_DESC
1380 | G_REQMODE_CLEAR_FEAT
1383 for (i = 0; i < 4; i++)
1384 dev->ep[i].irqs = 0;
1386 /* can't modify descriptors after writing UsbReady */
1387 for (i = 0; i < DESC_LEN; i++)
1388 writel(0, ®s->descriptors[i]);
1389 writel(0, ®s->UsbReady);
1391 /* expect ep0 requests when the host drops reset */
1392 writel(PW_RESETB | PW_PULLUP, ®s->power_detect);
1393 dev->int_enable = INT_DEVWIDE | INT_EP0;
1394 writel(dev->int_enable, &dev->regs->int_enable);
1395 readl(®s->int_enable);
1396 dev->gadget.speed = USB_SPEED_FULL;
1397 dev->ep0state = EP0_IDLE;
1400 static void udc_enable(struct goku_udc *dev)
1402 /* start enumeration now, or after power detect irq */
1403 if (readl(&dev->regs->power_detect) & PW_DETECT)
1406 DBG(dev, "%s\n", __FUNCTION__);
1407 dev->int_enable = INT_PWRDETECT;
1408 writel(dev->int_enable, &dev->regs->int_enable);
1412 /*-------------------------------------------------------------------------*/
1414 /* keeping it simple:
1415 * - one bus driver, initted first;
1416 * - one function driver, initted second
1419 static struct goku_udc *the_controller;
1421 /* when a driver is successfully registered, it will receive
1422 * control requests including set_configuration(), which enables
1423 * non-control requests. then usb traffic follows until a
1424 * disconnect is reported. then a host may connect again, or
1425 * the driver might get unbound.
1427 int usb_gadget_register_driver(struct usb_gadget_driver *driver)
1429 struct goku_udc *dev = the_controller;
1433 || driver->speed != USB_SPEED_FULL
1435 || !driver->disconnect
1443 /* hook up the driver */
1444 driver->driver.bus = NULL;
1445 dev->driver = driver;
1446 dev->gadget.dev.driver = &driver->driver;
1447 retval = driver->bind(&dev->gadget);
1449 DBG(dev, "bind to driver %s --> error %d\n",
1450 driver->driver.name, retval);
1452 dev->gadget.dev.driver = NULL;
1456 /* then enable host detection and ep0; and we're ready
1457 * for set_configuration as well as eventual disconnect.
1461 DBG(dev, "registered gadget driver '%s'\n", driver->driver.name);
1464 EXPORT_SYMBOL(usb_gadget_register_driver);
1467 stop_activity(struct goku_udc *dev, struct usb_gadget_driver *driver)
1471 DBG (dev, "%s\n", __FUNCTION__);
1473 if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1476 /* disconnect gadget driver after quiesceing hw and the driver */
1478 for (i = 0; i < 4; i++)
1479 nuke(&dev->ep [i], -ESHUTDOWN);
1481 spin_unlock(&dev->lock);
1482 driver->disconnect(&dev->gadget);
1483 spin_lock(&dev->lock);
1490 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1492 struct goku_udc *dev = the_controller;
1493 unsigned long flags;
1497 if (!driver || driver != dev->driver || !driver->unbind)
1500 spin_lock_irqsave(&dev->lock, flags);
1502 stop_activity(dev, driver);
1503 spin_unlock_irqrestore(&dev->lock, flags);
1505 driver->unbind(&dev->gadget);
1507 DBG(dev, "unregistered driver '%s'\n", driver->driver.name);
1510 EXPORT_SYMBOL(usb_gadget_unregister_driver);
1513 /*-------------------------------------------------------------------------*/
1515 static void ep0_setup(struct goku_udc *dev)
1517 struct goku_udc_regs __iomem *regs = dev->regs;
1518 struct usb_ctrlrequest ctrl;
1521 /* read SETUP packet and enter DATA stage */
1522 ctrl.bRequestType = readl(®s->bRequestType);
1523 ctrl.bRequest = readl(®s->bRequest);
1524 ctrl.wValue = cpu_to_le16((readl(®s->wValueH) << 8)
1525 | readl(®s->wValueL));
1526 ctrl.wIndex = cpu_to_le16((readl(®s->wIndexH) << 8)
1527 | readl(®s->wIndexL));
1528 ctrl.wLength = cpu_to_le16((readl(®s->wLengthH) << 8)
1529 | readl(®s->wLengthL));
1530 writel(0, ®s->SetupRecv);
1532 nuke(&dev->ep[0], 0);
1533 dev->ep[0].stopped = 0;
1534 if (likely(ctrl.bRequestType & USB_DIR_IN)) {
1535 dev->ep[0].is_in = 1;
1536 dev->ep0state = EP0_IN;
1537 /* detect early status stages */
1538 writel(ICONTROL_STATUSNAK, &dev->regs->IntControl);
1540 dev->ep[0].is_in = 0;
1541 dev->ep0state = EP0_OUT;
1543 /* NOTE: CLEAR_FEATURE is done in software so that we can
1544 * synchronize transfer restarts after bulk IN stalls. data
1545 * won't even enter the fifo until the halt is cleared.
1547 switch (ctrl.bRequest) {
1548 case USB_REQ_CLEAR_FEATURE:
1549 switch (ctrl.bRequestType) {
1550 case USB_RECIP_ENDPOINT:
1551 tmp = le16_to_cpu(ctrl.wIndex) & 0x0f;
1552 /* active endpoint */
1553 if (tmp > 3 || (!dev->ep[tmp].desc && tmp != 0))
1555 if (ctrl.wIndex & __constant_cpu_to_le16(
1557 if (!dev->ep[tmp].is_in)
1560 if (dev->ep[tmp].is_in)
1563 if (ctrl.wValue != __constant_cpu_to_le16(
1567 goku_clear_halt(&dev->ep[tmp]);
1569 /* start ep0out status stage */
1570 writel(~(1<<0), ®s->EOP);
1571 dev->ep[0].stopped = 1;
1572 dev->ep0state = EP0_STATUS;
1574 case USB_RECIP_DEVICE:
1575 /* device remote wakeup: always clear */
1576 if (ctrl.wValue != __constant_cpu_to_le16(1))
1578 VDBG(dev, "clear dev remote wakeup\n");
1580 case USB_RECIP_INTERFACE:
1582 default: /* pass to gadget driver */
1592 VDBG(dev, "SETUP %02x.%02x v%04x i%04x l%04x\n",
1593 ctrl.bRequestType, ctrl.bRequest,
1594 le16_to_cpu(ctrl.wValue), le16_to_cpu(ctrl.wIndex),
1595 le16_to_cpu(ctrl.wLength));
1598 /* hw wants to know when we're configured (or not) */
1599 dev->req_config = (ctrl.bRequest == USB_REQ_SET_CONFIGURATION
1600 && ctrl.bRequestType == USB_RECIP_DEVICE);
1601 if (unlikely(dev->req_config))
1602 dev->configured = (ctrl.wValue != __constant_cpu_to_le16(0));
1604 /* delegate everything to the gadget driver.
1605 * it may respond after this irq handler returns.
1607 spin_unlock (&dev->lock);
1608 tmp = dev->driver->setup(&dev->gadget, &ctrl);
1609 spin_lock (&dev->lock);
1610 if (unlikely(tmp < 0)) {
1613 VDBG(dev, "req %02x.%02x protocol STALL; err %d\n",
1614 ctrl.bRequestType, ctrl.bRequest, tmp);
1616 command(regs, COMMAND_STALL, 0);
1617 dev->ep[0].stopped = 1;
1618 dev->ep0state = EP0_STALL;
1621 /* expect at least one data or status stage irq */
1624 #define ACK(irqbit) { \
1626 writel(~irqbit, ®s->int_status); \
1630 static irqreturn_t goku_irq(int irq, void *_dev)
1632 struct goku_udc *dev = _dev;
1633 struct goku_udc_regs __iomem *regs = dev->regs;
1635 u32 stat, handled = 0;
1636 unsigned i, rescans = 5;
1638 spin_lock(&dev->lock);
1641 stat = readl(®s->int_status) & dev->int_enable;
1646 /* device-wide irqs */
1647 if (unlikely(stat & INT_DEVWIDE)) {
1648 if (stat & INT_SYSERROR) {
1649 ERROR(dev, "system error\n");
1650 stop_activity(dev, dev->driver);
1653 // FIXME have a neater way to prevent re-enumeration
1657 if (stat & INT_PWRDETECT) {
1658 writel(~stat, ®s->int_status);
1659 if (readl(&dev->regs->power_detect) & PW_DETECT) {
1660 VDBG(dev, "connect\n");
1663 DBG(dev, "disconnect\n");
1664 if (dev->gadget.speed == USB_SPEED_FULL)
1665 stop_activity(dev, dev->driver);
1666 dev->ep0state = EP0_DISCONNECT;
1667 dev->int_enable = INT_DEVWIDE;
1668 writel(dev->int_enable, &dev->regs->int_enable);
1674 if (stat & INT_SUSPEND) {
1676 if (readl(®s->ep_status[0]) & EPxSTATUS_SUSPEND) {
1677 switch (dev->ep0state) {
1678 case EP0_DISCONNECT:
1684 DBG(dev, "USB suspend\n");
1685 dev->ep0state = EP0_SUSPEND;
1686 if (dev->gadget.speed != USB_SPEED_UNKNOWN
1688 && dev->driver->suspend) {
1689 spin_unlock(&dev->lock);
1690 dev->driver->suspend(&dev->gadget);
1691 spin_lock(&dev->lock);
1694 if (dev->ep0state != EP0_SUSPEND) {
1695 DBG(dev, "bogus USB resume %d\n",
1699 DBG(dev, "USB resume\n");
1700 dev->ep0state = EP0_IDLE;
1701 if (dev->gadget.speed != USB_SPEED_UNKNOWN
1703 && dev->driver->resume) {
1704 spin_unlock(&dev->lock);
1705 dev->driver->resume(&dev->gadget);
1706 spin_lock(&dev->lock);
1711 if (stat & INT_USBRESET) { /* hub reset done */
1713 INFO(dev, "USB reset done, gadget %s\n",
1714 dev->driver->driver.name);
1716 // and INT_ERR on some endpoint's crc/bitstuff/... problem
1719 /* progress ep0 setup, data, or status stages.
1720 * no transition {EP0_STATUS, EP0_STALL} --> EP0_IDLE; saves irqs
1722 if (stat & INT_SETUP) {
1727 if (stat & INT_STATUSNAK) {
1728 ACK(INT_STATUSNAK|INT_ENDPOINT0);
1729 if (dev->ep0state == EP0_IN) {
1733 writel(~(1<<0), ®s->EOP);
1734 dev->ep0state = EP0_STATUS;
1737 if (stat & INT_ENDPOINT0) {
1744 /* dma completion */
1745 if (stat & INT_MSTRDEND) { /* IN */
1747 ep = &dev->ep[UDC_MSTRD_ENDPOINT];
1749 dma_advance(dev, ep);
1751 if (stat & INT_MSTWREND) { /* OUT */
1753 ep = &dev->ep[UDC_MSTWR_ENDPOINT];
1755 dma_advance(dev, ep);
1757 if (stat & INT_MSTWRTMOUT) { /* OUT */
1758 ACK(INT_MSTWRTMOUT);
1759 ep = &dev->ep[UDC_MSTWR_ENDPOINT];
1761 ERROR(dev, "%s write timeout ?\n", ep->ep.name);
1762 // reset dma? then dma_advance()
1766 for (i = 1; i < 4; i++) {
1767 u32 tmp = INT_EPxDATASET(i);
1773 if (list_empty (&ep->queue))
1774 pio_irq_disable(dev, regs, i);
1784 (void)readl(®s->int_enable);
1785 spin_unlock(&dev->lock);
1787 DBG(dev, "unhandled irq status: %05x (%05x, %05x)\n", stat,
1788 readl(®s->int_status), dev->int_enable);
1789 return IRQ_RETVAL(handled);
1794 /*-------------------------------------------------------------------------*/
1796 static void gadget_release(struct device *_dev)
1798 struct goku_udc *dev = dev_get_drvdata(_dev);
1803 /* tear down the binding between this driver and the pci device */
1805 static void goku_remove(struct pci_dev *pdev)
1807 struct goku_udc *dev = pci_get_drvdata(pdev);
1809 DBG(dev, "%s\n", __FUNCTION__);
1811 BUG_ON(dev->driver);
1813 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1814 remove_proc_entry(proc_node_name, NULL);
1819 free_irq(pdev->irq, dev);
1822 if (dev->got_region)
1823 release_mem_region(pci_resource_start (pdev, 0),
1824 pci_resource_len (pdev, 0));
1826 pci_disable_device(pdev);
1827 device_unregister(&dev->gadget.dev);
1829 pci_set_drvdata(pdev, NULL);
1831 the_controller = NULL;
1833 INFO(dev, "unbind\n");
1836 /* wrap this driver around the specified pci device, but
1837 * don't respond over USB until a gadget driver binds to us.
1840 static int goku_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1842 struct goku_udc *dev = NULL;
1843 unsigned long resource, len;
1844 void __iomem *base = NULL;
1847 /* if you want to support more than one controller in a system,
1848 * usb_gadget_driver_{register,unregister}() must change.
1850 if (the_controller) {
1851 WARN(dev, "ignoring %s\n", pci_name(pdev));
1855 printk(KERN_ERR "Check PCI %s IRQ setup!\n", pci_name(pdev));
1860 /* alloc, and start init */
1861 dev = kmalloc (sizeof *dev, GFP_KERNEL);
1863 pr_debug("enomem %s\n", pci_name(pdev));
1868 memset(dev, 0, sizeof *dev);
1869 spin_lock_init(&dev->lock);
1871 dev->gadget.ops = &goku_ops;
1873 /* the "gadget" abstracts/virtualizes the controller */
1874 strcpy(dev->gadget.dev.bus_id, "gadget");
1875 dev->gadget.dev.parent = &pdev->dev;
1876 dev->gadget.dev.dma_mask = pdev->dev.dma_mask;
1877 dev->gadget.dev.release = gadget_release;
1878 dev->gadget.name = driver_name;
1880 /* now all the pci goodies ... */
1881 retval = pci_enable_device(pdev);
1883 DBG(dev, "can't enable, %d\n", retval);
1888 resource = pci_resource_start(pdev, 0);
1889 len = pci_resource_len(pdev, 0);
1890 if (!request_mem_region(resource, len, driver_name)) {
1891 DBG(dev, "controller already in use\n");
1895 dev->got_region = 1;
1897 base = ioremap_nocache(resource, len);
1899 DBG(dev, "can't map memory\n");
1903 dev->regs = (struct goku_udc_regs __iomem *) base;
1905 pci_set_drvdata(pdev, dev);
1906 INFO(dev, "%s\n", driver_desc);
1907 INFO(dev, "version: " DRIVER_VERSION " %s\n", dmastr());
1908 INFO(dev, "irq %d, pci mem %p\n", pdev->irq, base);
1910 /* init to known state, then setup irqs */
1913 if (request_irq(pdev->irq, goku_irq, IRQF_SHARED/*|IRQF_SAMPLE_RANDOM*/,
1914 driver_name, dev) != 0) {
1915 DBG(dev, "request interrupt %d failed\n", pdev->irq);
1921 pci_set_master(pdev);
1924 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1925 create_proc_read_entry(proc_node_name, 0, NULL, udc_proc_read, dev);
1929 the_controller = dev;
1930 device_register(&dev->gadget.dev);
1941 /*-------------------------------------------------------------------------*/
1943 static struct pci_device_id pci_ids [] = { {
1944 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
1946 .vendor = 0x102f, /* Toshiba */
1947 .device = 0x0107, /* this UDC */
1948 .subvendor = PCI_ANY_ID,
1949 .subdevice = PCI_ANY_ID,
1951 }, { /* end: all zeroes */ }
1953 MODULE_DEVICE_TABLE (pci, pci_ids);
1955 static struct pci_driver goku_pci_driver = {
1956 .name = (char *) driver_name,
1957 .id_table = pci_ids,
1959 .probe = goku_probe,
1960 .remove = goku_remove,
1962 /* FIXME add power management support */
1965 static int __init init (void)
1967 return pci_register_driver (&goku_pci_driver);
1971 static void __exit cleanup (void)
1973 pci_unregister_driver (&goku_pci_driver);
1975 module_exit (cleanup);