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).
23 // #define VERBOSE /* extra debug messages (success too) */
24 // #define USB_TRACE /* packet-level success messages */
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/pci.h>
29 #include <linux/delay.h>
30 #include <linux/ioport.h>
31 #include <linux/slab.h>
32 #include <linux/errno.h>
33 #include <linux/init.h>
34 #include <linux/timer.h>
35 #include <linux/list.h>
36 #include <linux/interrupt.h>
37 #include <linux/proc_fs.h>
38 #include <linux/device.h>
39 #include <linux/usb/ch9.h>
40 #include <linux/usb/gadget.h>
41 #include <linux/prefetch.h>
43 #include <asm/byteorder.h>
46 #include <asm/system.h>
47 #include <asm/unaligned.h>
52 #define DRIVER_DESC "TC86C001 USB Device Controller"
53 #define DRIVER_VERSION "30-Oct 2003"
55 #define DMA_ADDR_INVALID (~(dma_addr_t)0)
57 static const char driver_name
[] = "goku_udc";
58 static const char driver_desc
[] = DRIVER_DESC
;
60 MODULE_AUTHOR("source@mvista.com");
61 MODULE_DESCRIPTION(DRIVER_DESC
);
62 MODULE_LICENSE("GPL");
66 * IN dma behaves ok under testing, though the IN-dma abort paths don't
67 * seem to behave quite as expected. Used by default.
69 * OUT dma documents design problems handling the common "short packet"
70 * transfer termination policy; it couldn't be enabled by default, even
71 * if the OUT-dma abort problems had a resolution.
73 static unsigned use_dma
= 1;
76 //#include <linux/moduleparam.h>
77 /* "modprobe goku_udc use_dma=1" etc
79 * 1 to use IN dma only (normal operation)
80 * 2 to use IN and OUT dma
82 module_param(use_dma
, uint
, S_IRUGO
);
85 /*-------------------------------------------------------------------------*/
87 static void nuke(struct goku_ep
*, int status
);
90 command(struct goku_udc_regs __iomem
*regs
, int command
, unsigned epnum
)
92 writel(COMMAND_EP(epnum
) | command
, ®s
->Command
);
97 goku_ep_enable(struct usb_ep
*_ep
, const struct usb_endpoint_descriptor
*desc
)
105 ep
= container_of(_ep
, struct goku_ep
, ep
);
106 if (!_ep
|| !desc
|| ep
->desc
107 || desc
->bDescriptorType
!= USB_DT_ENDPOINT
)
110 if (ep
== &dev
->ep
[0])
112 if (!dev
->driver
|| dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)
114 if (ep
->num
!= usb_endpoint_num(desc
))
117 switch (usb_endpoint_type(desc
)) {
118 case USB_ENDPOINT_XFER_BULK
:
119 case USB_ENDPOINT_XFER_INT
:
125 if ((readl(ep
->reg_status
) & EPxSTATUS_EP_MASK
)
126 != EPxSTATUS_EP_INVALID
)
129 /* enabling the no-toggle interrupt mode would need an api hook */
131 max
= get_unaligned_le16(&desc
->wMaxPacketSize
);
141 mode
|= 2 << 1; /* bulk, or intr-with-toggle */
143 /* ep1/ep2 dma direction is chosen early; it works in the other
144 * direction, with pio. be cautious with out-dma.
146 ep
->is_in
= usb_endpoint_dir_in(desc
);
149 ep
->dma
= (use_dma
!= 0) && (ep
->num
== UDC_MSTRD_ENDPOINT
);
151 ep
->dma
= (use_dma
== 2) && (ep
->num
== UDC_MSTWR_ENDPOINT
);
153 DBG(dev
, "%s out-dma hides short packets\n",
157 spin_lock_irqsave(&ep
->dev
->lock
, flags
);
159 /* ep1 and ep2 can do double buffering and/or dma */
161 struct goku_udc_regs __iomem
*regs
= ep
->dev
->regs
;
164 /* double buffer except (for now) with pio in */
165 tmp
= ((ep
->dma
|| !ep
->is_in
)
166 ? 0x10 /* double buffered */
167 : 0x11 /* single buffer */
169 tmp
|= readl(®s
->EPxSingle
);
170 writel(tmp
, ®s
->EPxSingle
);
172 tmp
= (ep
->dma
? 0x10/*dma*/ : 0x11/*pio*/) << ep
->num
;
173 tmp
|= readl(®s
->EPxBCS
);
174 writel(tmp
, ®s
->EPxBCS
);
176 writel(mode
, ep
->reg_mode
);
177 command(ep
->dev
->regs
, COMMAND_RESET
, ep
->num
);
178 ep
->ep
.maxpacket
= max
;
181 spin_unlock_irqrestore(&ep
->dev
->lock
, flags
);
183 DBG(dev
, "enable %s %s %s maxpacket %u\n", ep
->ep
.name
,
184 ep
->is_in
? "IN" : "OUT",
185 ep
->dma
? "dma" : "pio",
191 static void ep_reset(struct goku_udc_regs __iomem
*regs
, struct goku_ep
*ep
)
193 struct goku_udc
*dev
= ep
->dev
;
196 command(regs
, COMMAND_INVALID
, ep
->num
);
198 if (ep
->num
== UDC_MSTWR_ENDPOINT
)
199 dev
->int_enable
&= ~(INT_MSTWREND
201 else if (ep
->num
== UDC_MSTRD_ENDPOINT
)
202 dev
->int_enable
&= ~INT_MSTRDEND
;
203 dev
->int_enable
&= ~INT_EPxDATASET (ep
->num
);
205 dev
->int_enable
&= ~INT_EP0
;
206 writel(dev
->int_enable
, ®s
->int_enable
);
207 readl(®s
->int_enable
);
209 struct goku_udc_regs __iomem
*r
= ep
->dev
->regs
;
212 tmp
= readl(&r
->EPxSingle
);
213 tmp
&= ~(0x11 << ep
->num
);
214 writel(tmp
, &r
->EPxSingle
);
216 tmp
= readl(&r
->EPxBCS
);
217 tmp
&= ~(0x11 << ep
->num
);
218 writel(tmp
, &r
->EPxBCS
);
220 /* reset dma in case we're still using it */
224 master
= readl(®s
->dma_master
) & MST_RW_BITS
;
225 if (ep
->num
== UDC_MSTWR_ENDPOINT
) {
226 master
&= ~MST_W_BITS
;
227 master
|= MST_WR_RESET
;
229 master
&= ~MST_R_BITS
;
230 master
|= MST_RD_RESET
;
232 writel(master
, ®s
->dma_master
);
236 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 /*-------------------------------------------------------------------------*/
301 done(struct goku_ep
*ep
, struct goku_request
*req
, int status
)
303 struct goku_udc
*dev
;
304 unsigned stopped
= ep
->stopped
;
306 list_del_init(&req
->queue
);
308 if (likely(req
->req
.status
== -EINPROGRESS
))
309 req
->req
.status
= status
;
311 status
= req
->req
.status
;
315 pci_unmap_single(dev
->pdev
, req
->req
.dma
, req
->req
.length
,
316 ep
->is_in
? PCI_DMA_TODEVICE
: PCI_DMA_FROMDEVICE
);
317 req
->req
.dma
= DMA_ADDR_INVALID
;
322 if (status
&& status
!= -ESHUTDOWN
)
324 VDBG(dev
, "complete %s req %p stat %d len %u/%u\n",
325 ep
->ep
.name
, &req
->req
, status
,
326 req
->req
.actual
, req
->req
.length
);
328 /* don't modify queue heads during completion callback */
330 spin_unlock(&dev
->lock
);
331 req
->req
.complete(&ep
->ep
, &req
->req
);
332 spin_lock(&dev
->lock
);
333 ep
->stopped
= stopped
;
336 /*-------------------------------------------------------------------------*/
339 write_packet(u32 __iomem
*fifo
, u8
*buf
, struct goku_request
*req
, unsigned max
)
341 unsigned length
, count
;
343 length
= min(req
->req
.length
- req
->req
.actual
, max
);
344 req
->req
.actual
+= length
;
347 while (likely(count
--))
348 writel(*buf
++, fifo
);
352 // return: 0 = still running, 1 = completed, negative = errno
353 static int write_fifo(struct goku_ep
*ep
, struct goku_request
*req
)
355 struct goku_udc
*dev
= ep
->dev
;
361 tmp
= readl(&dev
->regs
->DataSet
);
362 buf
= req
->req
.buf
+ req
->req
.actual
;
366 if (unlikely(ep
->num
== 0 && dev
->ep0state
!= EP0_IN
))
369 /* NOTE: just single-buffered PIO-IN for now. */
370 if (unlikely((tmp
& DATASET_A(ep
->num
)) != 0))
373 /* clear our "packet available" irq */
375 writel(~INT_EPxDATASET(ep
->num
), &dev
->regs
->int_status
);
377 count
= write_packet(ep
->reg_fifo
, buf
, req
, ep
->ep
.maxpacket
);
379 /* last packet often short (sometimes a zlp, especially on ep0) */
380 if (unlikely(count
!= ep
->ep
.maxpacket
)) {
381 writel(~(1<<ep
->num
), &dev
->regs
->EOP
);
383 dev
->ep
[0].stopped
= 1;
384 dev
->ep0state
= EP0_STATUS
;
388 if (likely(req
->req
.length
!= req
->req
.actual
)
394 #if 0 /* printk seemed to trash is_last...*/
396 VDBG(dev
, "wrote %s %u bytes%s IN %u left %p\n",
397 ep
->ep
.name
, count
, is_last
? "/last" : "",
398 req
->req
.length
- req
->req
.actual
, req
);
401 /* requests complete when all IN data is in the FIFO,
402 * or sometimes later, if a zlp was needed.
412 static int read_fifo(struct goku_ep
*ep
, struct goku_request
*req
)
414 struct goku_udc_regs __iomem
*regs
;
417 unsigned bufferspace
, is_short
, dbuff
;
419 regs
= ep
->dev
->regs
;
421 buf
= req
->req
.buf
+ req
->req
.actual
;
424 if (unlikely(ep
->num
== 0 && ep
->dev
->ep0state
!= EP0_OUT
))
427 dbuff
= (ep
->num
== 1 || ep
->num
== 2);
429 /* ack dataset irq matching the status we'll handle */
431 writel(~INT_EPxDATASET(ep
->num
), ®s
->int_status
);
433 set
= readl(®s
->DataSet
) & DATASET_AB(ep
->num
);
434 size
= readl(®s
->EPxSizeLA
[ep
->num
]);
435 bufferspace
= req
->req
.length
- req
->req
.actual
;
437 /* usually do nothing without an OUT packet */
438 if (likely(ep
->num
!= 0 || bufferspace
!= 0)) {
439 if (unlikely(set
== 0))
441 /* use ep1/ep2 double-buffering for OUT */
442 if (!(size
& PACKET_ACTIVE
))
443 size
= readl(®s
->EPxSizeLB
[ep
->num
]);
444 if (!(size
& PACKET_ACTIVE
)) /* "can't happen" */
446 size
&= DATASIZE
; /* EPxSizeH == 0 */
448 /* ep0out no-out-data case for set_config, etc */
452 /* read all bytes from this packet */
453 req
->req
.actual
+= size
;
454 is_short
= (size
< ep
->ep
.maxpacket
);
456 VDBG(ep
->dev
, "read %s %u bytes%s OUT req %p %u/%u\n",
457 ep
->ep
.name
, size
, is_short
? "/S" : "",
458 req
, req
->req
.actual
, req
->req
.length
);
460 while (likely(size
-- != 0)) {
461 u8 byte
= (u8
) readl(ep
->reg_fifo
);
463 if (unlikely(bufferspace
== 0)) {
464 /* this happens when the driver's buffer
465 * is smaller than what the host sent.
466 * discard the extra data in this packet.
468 if (req
->req
.status
!= -EOVERFLOW
)
469 DBG(ep
->dev
, "%s overflow %u\n",
471 req
->req
.status
= -EOVERFLOW
;
479 if (unlikely(is_short
|| req
->req
.actual
== req
->req
.length
)) {
480 if (unlikely(ep
->num
== 0)) {
481 /* non-control endpoints now usable? */
482 if (ep
->dev
->req_config
)
483 writel(ep
->dev
->configured
484 ? USBSTATE_CONFIGURED
487 /* ep0out status stage */
488 writel(~(1<<0), ®s
->EOP
);
490 ep
->dev
->ep0state
= EP0_STATUS
;
494 /* empty the second buffer asap */
495 if (dbuff
&& !list_empty(&ep
->queue
)) {
496 req
= list_entry(ep
->queue
.next
,
497 struct goku_request
, queue
);
507 pio_irq_enable(struct goku_udc
*dev
,
508 struct goku_udc_regs __iomem
*regs
, int epnum
)
510 dev
->int_enable
|= INT_EPxDATASET (epnum
);
511 writel(dev
->int_enable
, ®s
->int_enable
);
512 /* write may still be posted */
516 pio_irq_disable(struct goku_udc
*dev
,
517 struct goku_udc_regs __iomem
*regs
, int epnum
)
519 dev
->int_enable
&= ~INT_EPxDATASET (epnum
);
520 writel(dev
->int_enable
, ®s
->int_enable
);
521 /* write may still be posted */
525 pio_advance(struct goku_ep
*ep
)
527 struct goku_request
*req
;
529 if (unlikely(list_empty (&ep
->queue
)))
531 req
= list_entry(ep
->queue
.next
, struct goku_request
, queue
);
532 (ep
->is_in
? write_fifo
: read_fifo
)(ep
, req
);
536 /*-------------------------------------------------------------------------*/
538 // return: 0 = q running, 1 = q stopped, negative = errno
539 static int start_dma(struct goku_ep
*ep
, struct goku_request
*req
)
541 struct goku_udc_regs __iomem
*regs
= ep
->dev
->regs
;
543 u32 start
= req
->req
.dma
;
544 u32 end
= start
+ req
->req
.length
- 1;
546 master
= readl(®s
->dma_master
) & MST_RW_BITS
;
548 /* re-init the bits affecting IN dma; careful with zlps */
549 if (likely(ep
->is_in
)) {
550 if (unlikely(master
& MST_RD_ENA
)) {
551 DBG (ep
->dev
, "start, IN active dma %03x!!\n",
555 writel(end
, ®s
->in_dma_end
);
556 writel(start
, ®s
->in_dma_start
);
558 master
&= ~MST_R_BITS
;
559 if (unlikely(req
->req
.length
== 0))
560 master
= MST_RD_ENA
| MST_RD_EOPB
;
561 else if ((req
->req
.length
% ep
->ep
.maxpacket
) != 0
563 master
= MST_RD_ENA
| MST_EOPB_ENA
;
565 master
= MST_RD_ENA
| MST_EOPB_DIS
;
567 ep
->dev
->int_enable
|= INT_MSTRDEND
;
569 /* Goku DMA-OUT merges short packets, which plays poorly with
570 * protocols where short packets mark the transfer boundaries.
571 * The chip supports a nonstandard policy with INT_MSTWRTMOUT,
572 * ending transfers after 3 SOFs; we don't turn it on.
575 if (unlikely(master
& MST_WR_ENA
)) {
576 DBG (ep
->dev
, "start, OUT active dma %03x!!\n",
580 writel(end
, ®s
->out_dma_end
);
581 writel(start
, ®s
->out_dma_start
);
583 master
&= ~MST_W_BITS
;
584 master
|= MST_WR_ENA
| MST_TIMEOUT_DIS
;
586 ep
->dev
->int_enable
|= INT_MSTWREND
|INT_MSTWRTMOUT
;
589 writel(master
, ®s
->dma_master
);
590 writel(ep
->dev
->int_enable
, ®s
->int_enable
);
594 static void dma_advance(struct goku_udc
*dev
, struct goku_ep
*ep
)
596 struct goku_request
*req
;
597 struct goku_udc_regs __iomem
*regs
= ep
->dev
->regs
;
600 master
= readl(®s
->dma_master
);
602 if (unlikely(list_empty(&ep
->queue
))) {
605 dev
->int_enable
&= ~INT_MSTRDEND
;
607 dev
->int_enable
&= ~(INT_MSTWREND
|INT_MSTWRTMOUT
);
608 writel(dev
->int_enable
, ®s
->int_enable
);
611 req
= list_entry(ep
->queue
.next
, struct goku_request
, queue
);
613 /* normal hw dma completion (not abort) */
614 if (likely(ep
->is_in
)) {
615 if (unlikely(master
& MST_RD_ENA
))
617 req
->req
.actual
= readl(®s
->in_dma_current
);
619 if (unlikely(master
& MST_WR_ENA
))
622 /* hardware merges short packets, and also hides packet
623 * overruns. a partial packet MAY be in the fifo here.
625 req
->req
.actual
= readl(®s
->out_dma_current
);
627 req
->req
.actual
-= req
->req
.dma
;
631 VDBG(dev
, "done %s %s dma, %u/%u bytes, req %p\n",
632 ep
->ep
.name
, ep
->is_in
? "IN" : "OUT",
633 req
->req
.actual
, req
->req
.length
, req
);
636 if (list_empty(&ep
->queue
))
638 req
= list_entry(ep
->queue
.next
, struct goku_request
, queue
);
639 (void) start_dma(ep
, req
);
642 static void abort_dma(struct goku_ep
*ep
, int status
)
644 struct goku_udc_regs __iomem
*regs
= ep
->dev
->regs
;
645 struct goku_request
*req
;
648 /* NAK future host requests, hoping the implicit delay lets the
649 * dma engine finish reading (or writing) its latest packet and
650 * empty the dma buffer (up to 16 bytes).
652 * This avoids needing to clean up a partial packet in the fifo;
653 * we can't do that for IN without side effects to HALT and TOGGLE.
655 command(regs
, COMMAND_FIFO_DISABLE
, ep
->num
);
656 req
= list_entry(ep
->queue
.next
, struct goku_request
, queue
);
657 master
= readl(®s
->dma_master
) & MST_RW_BITS
;
659 /* FIXME using these resets isn't usably documented. this may
660 * not work unless it's followed by disabling the endpoint.
662 * FIXME the OUT reset path doesn't even behave consistently.
665 if (unlikely((readl(®s
->dma_master
) & MST_RD_ENA
) == 0))
667 curr
= readl(®s
->in_dma_current
);
669 writel(curr
, ®s
->in_dma_end
);
670 writel(curr
, ®s
->in_dma_start
);
672 master
&= ~MST_R_BITS
;
673 master
|= MST_RD_RESET
;
674 writel(master
, ®s
->dma_master
);
676 if (readl(®s
->dma_master
) & MST_RD_ENA
)
677 DBG(ep
->dev
, "IN dma active after reset!\n");
680 if (unlikely((readl(®s
->dma_master
) & MST_WR_ENA
) == 0))
682 curr
= readl(®s
->out_dma_current
);
684 writel(curr
, ®s
->out_dma_end
);
685 writel(curr
, ®s
->out_dma_start
);
687 master
&= ~MST_W_BITS
;
688 master
|= MST_WR_RESET
;
689 writel(master
, ®s
->dma_master
);
691 if (readl(®s
->dma_master
) & MST_WR_ENA
)
692 DBG(ep
->dev
, "OUT dma active after reset!\n");
694 req
->req
.actual
= (curr
- req
->req
.dma
) + 1;
695 req
->req
.status
= status
;
697 VDBG(ep
->dev
, "%s %s %s %d/%d\n", __func__
, ep
->ep
.name
,
698 ep
->is_in
? "IN" : "OUT",
699 req
->req
.actual
, req
->req
.length
);
701 command(regs
, COMMAND_FIFO_ENABLE
, ep
->num
);
706 /* dma already completed; no abort needed */
707 command(regs
, COMMAND_FIFO_ENABLE
, ep
->num
);
708 req
->req
.actual
= req
->req
.length
;
712 /*-------------------------------------------------------------------------*/
715 goku_queue(struct usb_ep
*_ep
, struct usb_request
*_req
, gfp_t gfp_flags
)
717 struct goku_request
*req
;
719 struct goku_udc
*dev
;
723 /* always require a cpu-view buffer so pio works */
724 req
= container_of(_req
, struct goku_request
, req
);
725 if (unlikely(!_req
|| !_req
->complete
726 || !_req
->buf
|| !list_empty(&req
->queue
)))
728 ep
= container_of(_ep
, struct goku_ep
, ep
);
729 if (unlikely(!_ep
|| (!ep
->desc
&& ep
->num
!= 0)))
732 if (unlikely(!dev
->driver
|| dev
->gadget
.speed
== USB_SPEED_UNKNOWN
))
735 /* can't touch registers when suspended */
736 if (dev
->ep0state
== EP0_SUSPEND
)
739 /* set up dma mapping in case the caller didn't */
740 if (ep
->dma
&& _req
->dma
== DMA_ADDR_INVALID
) {
741 _req
->dma
= pci_map_single(dev
->pdev
, _req
->buf
, _req
->length
,
742 ep
->is_in
? PCI_DMA_TODEVICE
: PCI_DMA_FROMDEVICE
);
747 VDBG(dev
, "%s queue req %p, len %u buf %p\n",
748 _ep
->name
, _req
, _req
->length
, _req
->buf
);
751 spin_lock_irqsave(&dev
->lock
, flags
);
753 _req
->status
= -EINPROGRESS
;
756 /* for ep0 IN without premature status, zlp is required and
757 * writing EOP starts the status stage (OUT).
759 if (unlikely(ep
->num
== 0 && ep
->is_in
))
762 /* kickstart this i/o queue? */
764 if (list_empty(&ep
->queue
) && likely(!ep
->stopped
)) {
765 /* dma: done after dma completion IRQ (or error)
766 * pio: done after last fifo operation
769 status
= start_dma(ep
, req
);
771 status
= (ep
->is_in
? write_fifo
: read_fifo
)(ep
, req
);
773 if (unlikely(status
!= 0)) {
779 } /* else pio or dma irq handler advances the queue. */
781 if (likely(req
!= 0))
782 list_add_tail(&req
->queue
, &ep
->queue
);
784 if (likely(!list_empty(&ep
->queue
))
785 && likely(ep
->num
!= 0)
787 && !(dev
->int_enable
& INT_EPxDATASET (ep
->num
)))
788 pio_irq_enable(dev
, dev
->regs
, ep
->num
);
790 spin_unlock_irqrestore(&dev
->lock
, flags
);
792 /* pci writes may still be posted */
796 /* dequeue ALL requests */
797 static void nuke(struct goku_ep
*ep
, int status
)
799 struct goku_request
*req
;
802 if (list_empty(&ep
->queue
))
805 abort_dma(ep
, status
);
806 while (!list_empty(&ep
->queue
)) {
807 req
= list_entry(ep
->queue
.next
, struct goku_request
, queue
);
808 done(ep
, req
, status
);
812 /* dequeue JUST ONE request */
813 static int goku_dequeue(struct usb_ep
*_ep
, struct usb_request
*_req
)
815 struct goku_request
*req
;
817 struct goku_udc
*dev
;
820 ep
= container_of(_ep
, struct goku_ep
, ep
);
821 if (!_ep
|| !_req
|| (!ep
->desc
&& ep
->num
!= 0))
827 /* we can't touch (dma) registers when suspended */
828 if (dev
->ep0state
== EP0_SUSPEND
)
831 VDBG(dev
, "%s %s %s %s %p\n", __func__
, _ep
->name
,
832 ep
->is_in
? "IN" : "OUT",
833 ep
->dma
? "dma" : "pio",
836 spin_lock_irqsave(&dev
->lock
, flags
);
838 /* make sure it's actually queued on this endpoint */
839 list_for_each_entry (req
, &ep
->queue
, queue
) {
840 if (&req
->req
== _req
)
843 if (&req
->req
!= _req
) {
844 spin_unlock_irqrestore (&dev
->lock
, flags
);
848 if (ep
->dma
&& ep
->queue
.next
== &req
->queue
&& !ep
->stopped
) {
849 abort_dma(ep
, -ECONNRESET
);
850 done(ep
, req
, -ECONNRESET
);
851 dma_advance(dev
, ep
);
852 } else if (!list_empty(&req
->queue
))
853 done(ep
, req
, -ECONNRESET
);
856 spin_unlock_irqrestore(&dev
->lock
, flags
);
858 return req
? 0 : -EOPNOTSUPP
;
861 /*-------------------------------------------------------------------------*/
863 static void goku_clear_halt(struct goku_ep
*ep
)
865 // assert (ep->num !=0)
866 VDBG(ep
->dev
, "%s clear halt\n", ep
->ep
.name
);
867 command(ep
->dev
->regs
, COMMAND_SETDATA0
, ep
->num
);
868 command(ep
->dev
->regs
, COMMAND_STALL_CLEAR
, ep
->num
);
872 struct goku_request
*req
;
874 if (list_empty(&ep
->queue
))
876 req
= list_entry(ep
->queue
.next
, struct goku_request
,
878 (void) start_dma(ep
, req
);
884 static int goku_set_halt(struct usb_ep
*_ep
, int value
)
892 ep
= container_of (_ep
, struct goku_ep
, ep
);
896 ep
->dev
->ep0state
= EP0_STALL
;
897 ep
->dev
->ep
[0].stopped
= 1;
901 /* don't change EPxSTATUS_EP_INVALID to READY */
902 } else if (!ep
->desc
) {
903 DBG(ep
->dev
, "%s %s inactive?\n", __func__
, ep
->ep
.name
);
907 spin_lock_irqsave(&ep
->dev
->lock
, flags
);
908 if (!list_empty(&ep
->queue
))
910 else if (ep
->is_in
&& value
911 /* data in (either) packet buffer? */
912 && (readl(&ep
->dev
->regs
->DataSet
)
913 & DATASET_AB(ep
->num
)))
919 VDBG(ep
->dev
, "%s set halt\n", ep
->ep
.name
);
920 command(ep
->dev
->regs
, COMMAND_STALL
, ep
->num
);
921 readl(ep
->reg_status
);
923 spin_unlock_irqrestore(&ep
->dev
->lock
, flags
);
927 static int goku_fifo_status(struct usb_ep
*_ep
)
930 struct goku_udc_regs __iomem
*regs
;
935 ep
= container_of(_ep
, struct goku_ep
, ep
);
937 /* size is only reported sanely for OUT */
941 /* ignores 16-byte dma buffer; SizeH == 0 */
942 regs
= ep
->dev
->regs
;
943 size
= readl(®s
->EPxSizeLA
[ep
->num
]) & DATASIZE
;
944 size
+= readl(®s
->EPxSizeLB
[ep
->num
]) & DATASIZE
;
945 VDBG(ep
->dev
, "%s %s %u\n", __func__
, ep
->ep
.name
, size
);
949 static void goku_fifo_flush(struct usb_ep
*_ep
)
952 struct goku_udc_regs __iomem
*regs
;
957 ep
= container_of(_ep
, struct goku_ep
, ep
);
958 VDBG(ep
->dev
, "%s %s\n", __func__
, ep
->ep
.name
);
960 /* don't change EPxSTATUS_EP_INVALID to READY */
961 if (!ep
->desc
&& ep
->num
!= 0) {
962 DBG(ep
->dev
, "%s %s inactive?\n", __func__
, ep
->ep
.name
);
966 regs
= ep
->dev
->regs
;
967 size
= readl(®s
->EPxSizeLA
[ep
->num
]);
970 /* Non-desirable behavior: FIFO_CLEAR also clears the
971 * endpoint halt feature. For OUT, we _could_ just read
972 * the bytes out (PIO, if !ep->dma); for in, no choice.
975 command(regs
, COMMAND_FIFO_CLEAR
, ep
->num
);
978 static struct usb_ep_ops goku_ep_ops
= {
979 .enable
= goku_ep_enable
,
980 .disable
= goku_ep_disable
,
982 .alloc_request
= goku_alloc_request
,
983 .free_request
= goku_free_request
,
986 .dequeue
= goku_dequeue
,
988 .set_halt
= goku_set_halt
,
989 .fifo_status
= goku_fifo_status
,
990 .fifo_flush
= goku_fifo_flush
,
993 /*-------------------------------------------------------------------------*/
995 static int goku_get_frame(struct usb_gadget
*_gadget
)
1000 static int goku_start(struct usb_gadget_driver
*driver
,
1001 int (*bind
)(struct usb_gadget
*));
1002 static int goku_stop(struct usb_gadget_driver
*driver
);
1004 static const struct usb_gadget_ops goku_ops
= {
1005 .get_frame
= goku_get_frame
,
1006 .start
= goku_start
,
1012 /*-------------------------------------------------------------------------*/
1014 static inline char *dmastr(void)
1017 return "(dma disabled)";
1018 else if (use_dma
== 2)
1019 return "(dma IN and OUT)";
1024 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1026 static const char proc_node_name
[] = "driver/udc";
1028 #define FOURBITS "%s%s%s%s"
1029 #define EIGHTBITS FOURBITS FOURBITS
1032 dump_intmask(const char *label
, u32 mask
, char **next
, unsigned *size
)
1036 /* int_status is the same format ... */
1037 t
= scnprintf(*next
, *size
,
1038 "%s %05X =" FOURBITS EIGHTBITS EIGHTBITS
"\n",
1040 (mask
& INT_PWRDETECT
) ? " power" : "",
1041 (mask
& INT_SYSERROR
) ? " sys" : "",
1042 (mask
& INT_MSTRDEND
) ? " in-dma" : "",
1043 (mask
& INT_MSTWRTMOUT
) ? " wrtmo" : "",
1045 (mask
& INT_MSTWREND
) ? " out-dma" : "",
1046 (mask
& INT_MSTWRSET
) ? " wrset" : "",
1047 (mask
& INT_ERR
) ? " err" : "",
1048 (mask
& INT_SOF
) ? " sof" : "",
1050 (mask
& INT_EP3NAK
) ? " ep3nak" : "",
1051 (mask
& INT_EP2NAK
) ? " ep2nak" : "",
1052 (mask
& INT_EP1NAK
) ? " ep1nak" : "",
1053 (mask
& INT_EP3DATASET
) ? " ep3" : "",
1055 (mask
& INT_EP2DATASET
) ? " ep2" : "",
1056 (mask
& INT_EP1DATASET
) ? " ep1" : "",
1057 (mask
& INT_STATUSNAK
) ? " ep0snak" : "",
1058 (mask
& INT_STATUS
) ? " ep0status" : "",
1060 (mask
& INT_SETUP
) ? " setup" : "",
1061 (mask
& INT_ENDPOINT0
) ? " ep0" : "",
1062 (mask
& INT_USBRESET
) ? " reset" : "",
1063 (mask
& INT_SUSPEND
) ? " suspend" : "");
1070 udc_proc_read(char *buffer
, char **start
, off_t off
, int count
,
1071 int *eof
, void *_dev
)
1074 struct goku_udc
*dev
= _dev
;
1075 struct goku_udc_regs __iomem
*regs
= dev
->regs
;
1077 unsigned size
= count
;
1078 unsigned long flags
;
1079 int i
, t
, is_usb_connected
;
1085 local_irq_save(flags
);
1087 /* basic device status */
1088 tmp
= readl(®s
->power_detect
);
1089 is_usb_connected
= tmp
& PW_DETECT
;
1090 t
= scnprintf(next
, size
,
1092 "%s version: %s %s\n"
1093 "Gadget driver: %s\n"
1096 pci_name(dev
->pdev
), driver_desc
,
1097 driver_name
, DRIVER_VERSION
, dmastr(),
1098 dev
->driver
? dev
->driver
->driver
.name
: "(none)",
1100 ? ((tmp
& PW_PULLUP
) ? "full speed" : "powered")
1103 switch(dev
->ep0state
){
1104 case EP0_DISCONNECT
: state
= "ep0_disconnect"; break;
1105 case EP0_IDLE
: state
= "ep0_idle"; break;
1106 case EP0_IN
: state
= "ep0_in"; break;
1107 case EP0_OUT
: state
= "ep0_out"; break;
1108 case EP0_STATUS
: state
= "ep0_status"; break;
1109 case EP0_STALL
: state
= "ep0_stall"; break;
1110 case EP0_SUSPEND
: state
= "ep0_suspend"; break;
1111 default: state
= "ep0_?"; break;
1117 dump_intmask("int_status", readl(®s
->int_status
), &next
, &size
);
1118 dump_intmask("int_enable", readl(®s
->int_enable
), &next
, &size
);
1120 if (!is_usb_connected
|| !dev
->driver
|| (tmp
& PW_PULLUP
) == 0)
1123 /* registers for (active) device and ep0 */
1124 t
= scnprintf(next
, size
, "\nirqs %lu\ndataset %02x "
1125 "single.bcs %02x.%02x state %x addr %u\n",
1126 dev
->irqs
, readl(®s
->DataSet
),
1127 readl(®s
->EPxSingle
), readl(®s
->EPxBCS
),
1128 readl(®s
->UsbState
),
1129 readl(®s
->address
));
1133 tmp
= readl(®s
->dma_master
);
1134 t
= scnprintf(next
, size
,
1135 "dma %03X =" EIGHTBITS
"%s %s\n", tmp
,
1136 (tmp
& MST_EOPB_DIS
) ? " eopb-" : "",
1137 (tmp
& MST_EOPB_ENA
) ? " eopb+" : "",
1138 (tmp
& MST_TIMEOUT_DIS
) ? " tmo-" : "",
1139 (tmp
& MST_TIMEOUT_ENA
) ? " tmo+" : "",
1141 (tmp
& MST_RD_EOPB
) ? " eopb" : "",
1142 (tmp
& MST_RD_RESET
) ? " in_reset" : "",
1143 (tmp
& MST_WR_RESET
) ? " out_reset" : "",
1144 (tmp
& MST_RD_ENA
) ? " IN" : "",
1146 (tmp
& MST_WR_ENA
) ? " OUT" : "",
1147 (tmp
& MST_CONNECTION
)
1153 /* dump endpoint queues */
1154 for (i
= 0; i
< 4; i
++) {
1155 struct goku_ep
*ep
= &dev
->ep
[i
];
1156 struct goku_request
*req
;
1161 tmp
= readl(ep
->reg_status
);
1162 t
= scnprintf(next
, size
,
1163 "%s %s max %u %s, irqs %lu, "
1164 "status %02x (%s) " FOURBITS
"\n",
1166 ep
->is_in
? "in" : "out",
1168 ep
->dma
? "dma" : "pio",
1171 switch (tmp
& EPxSTATUS_EP_MASK
) {
1172 case EPxSTATUS_EP_READY
:
1174 case EPxSTATUS_EP_DATAIN
:
1175 s
= "packet"; break;
1176 case EPxSTATUS_EP_FULL
:
1178 case EPxSTATUS_EP_TX_ERR
: // host will retry
1179 s
= "tx_err"; break;
1180 case EPxSTATUS_EP_RX_ERR
:
1181 s
= "rx_err"; break;
1182 case EPxSTATUS_EP_BUSY
: /* ep0 only */
1184 case EPxSTATUS_EP_STALL
:
1186 case EPxSTATUS_EP_INVALID
: // these "can't happen"
1187 s
= "invalid"; break;
1191 (tmp
& EPxSTATUS_TOGGLE
) ? "data1" : "data0",
1192 (tmp
& EPxSTATUS_SUSPEND
) ? " suspend" : "",
1193 (tmp
& EPxSTATUS_FIFO_DISABLE
) ? " disable" : "",
1194 (tmp
& EPxSTATUS_STAGE_ERROR
) ? " ep0stat" : ""
1196 if (t
<= 0 || t
> size
)
1201 if (list_empty(&ep
->queue
)) {
1202 t
= scnprintf(next
, size
, "\t(nothing queued)\n");
1203 if (t
<= 0 || t
> size
)
1209 list_for_each_entry(req
, &ep
->queue
, queue
) {
1210 if (ep
->dma
&& req
->queue
.prev
== &ep
->queue
) {
1211 if (i
== UDC_MSTRD_ENDPOINT
)
1212 tmp
= readl(®s
->in_dma_current
);
1214 tmp
= readl(®s
->out_dma_current
);
1215 tmp
-= req
->req
.dma
;
1218 tmp
= req
->req
.actual
;
1220 t
= scnprintf(next
, size
,
1221 "\treq %p len %u/%u buf %p\n",
1222 &req
->req
, tmp
, req
->req
.length
,
1224 if (t
<= 0 || t
> size
)
1232 local_irq_restore(flags
);
1234 return count
- size
;
1237 #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
1239 /*-------------------------------------------------------------------------*/
1241 static void udc_reinit (struct goku_udc
*dev
)
1243 static char *names
[] = { "ep0", "ep1-bulk", "ep2-bulk", "ep3-bulk" };
1247 INIT_LIST_HEAD (&dev
->gadget
.ep_list
);
1248 dev
->gadget
.ep0
= &dev
->ep
[0].ep
;
1249 dev
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1250 dev
->ep0state
= EP0_DISCONNECT
;
1253 for (i
= 0; i
< 4; i
++) {
1254 struct goku_ep
*ep
= &dev
->ep
[i
];
1257 ep
->ep
.name
= names
[i
];
1258 ep
->reg_fifo
= &dev
->regs
->ep_fifo
[i
];
1259 ep
->reg_status
= &dev
->regs
->ep_status
[i
];
1260 ep
->reg_mode
= &dev
->regs
->ep_mode
[i
];
1262 ep
->ep
.ops
= &goku_ep_ops
;
1263 list_add_tail (&ep
->ep
.ep_list
, &dev
->gadget
.ep_list
);
1265 INIT_LIST_HEAD (&ep
->queue
);
1270 dev
->ep
[0].reg_mode
= NULL
;
1271 dev
->ep
[0].ep
.maxpacket
= MAX_EP0_SIZE
;
1272 list_del_init (&dev
->ep
[0].ep
.ep_list
);
1275 static void udc_reset(struct goku_udc
*dev
)
1277 struct goku_udc_regs __iomem
*regs
= dev
->regs
;
1279 writel(0, ®s
->power_detect
);
1280 writel(0, ®s
->int_enable
);
1281 readl(®s
->int_enable
);
1282 dev
->int_enable
= 0;
1284 /* deassert reset, leave USB D+ at hi-Z (no pullup)
1285 * don't let INT_PWRDETECT sequence begin
1288 writel(PW_RESETB
, ®s
->power_detect
);
1289 readl(®s
->int_enable
);
1292 static void ep0_start(struct goku_udc
*dev
)
1294 struct goku_udc_regs __iomem
*regs
= dev
->regs
;
1297 VDBG(dev
, "%s\n", __func__
);
1301 //writel(MST_EOPB_ENA | MST_TIMEOUT_ENA, ®s->dma_master);
1303 /* hw handles set_address, set_feature, get_status; maybe more */
1304 writel( G_REQMODE_SET_INTF
| G_REQMODE_GET_INTF
1305 | G_REQMODE_SET_CONF
| G_REQMODE_GET_CONF
1306 | G_REQMODE_GET_DESC
1307 | G_REQMODE_CLEAR_FEAT
1310 for (i
= 0; i
< 4; i
++)
1311 dev
->ep
[i
].irqs
= 0;
1313 /* can't modify descriptors after writing UsbReady */
1314 for (i
= 0; i
< DESC_LEN
; i
++)
1315 writel(0, ®s
->descriptors
[i
]);
1316 writel(0, ®s
->UsbReady
);
1318 /* expect ep0 requests when the host drops reset */
1319 writel(PW_RESETB
| PW_PULLUP
, ®s
->power_detect
);
1320 dev
->int_enable
= INT_DEVWIDE
| INT_EP0
;
1321 writel(dev
->int_enable
, &dev
->regs
->int_enable
);
1322 readl(®s
->int_enable
);
1323 dev
->gadget
.speed
= USB_SPEED_FULL
;
1324 dev
->ep0state
= EP0_IDLE
;
1327 static void udc_enable(struct goku_udc
*dev
)
1329 /* start enumeration now, or after power detect irq */
1330 if (readl(&dev
->regs
->power_detect
) & PW_DETECT
)
1333 DBG(dev
, "%s\n", __func__
);
1334 dev
->int_enable
= INT_PWRDETECT
;
1335 writel(dev
->int_enable
, &dev
->regs
->int_enable
);
1339 /*-------------------------------------------------------------------------*/
1341 /* keeping it simple:
1342 * - one bus driver, initted first;
1343 * - one function driver, initted second
1346 static struct goku_udc
*the_controller
;
1348 /* when a driver is successfully registered, it will receive
1349 * control requests including set_configuration(), which enables
1350 * non-control requests. then usb traffic follows until a
1351 * disconnect is reported. then a host may connect again, or
1352 * the driver might get unbound.
1354 static int goku_start(struct usb_gadget_driver
*driver
,
1355 int (*bind
)(struct usb_gadget
*))
1357 struct goku_udc
*dev
= the_controller
;
1361 || driver
->max_speed
< USB_SPEED_FULL
1363 || !driver
->disconnect
1371 /* hook up the driver */
1372 driver
->driver
.bus
= NULL
;
1373 dev
->driver
= driver
;
1374 dev
->gadget
.dev
.driver
= &driver
->driver
;
1375 retval
= bind(&dev
->gadget
);
1377 DBG(dev
, "bind to driver %s --> error %d\n",
1378 driver
->driver
.name
, retval
);
1380 dev
->gadget
.dev
.driver
= NULL
;
1384 /* then enable host detection and ep0; and we're ready
1385 * for set_configuration as well as eventual disconnect.
1389 DBG(dev
, "registered gadget driver '%s'\n", driver
->driver
.name
);
1394 stop_activity(struct goku_udc
*dev
, struct usb_gadget_driver
*driver
)
1398 DBG (dev
, "%s\n", __func__
);
1400 if (dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)
1403 /* disconnect gadget driver after quiesceing hw and the driver */
1405 for (i
= 0; i
< 4; i
++)
1406 nuke(&dev
->ep
[i
], -ESHUTDOWN
);
1408 spin_unlock(&dev
->lock
);
1409 driver
->disconnect(&dev
->gadget
);
1410 spin_lock(&dev
->lock
);
1417 static int goku_stop(struct usb_gadget_driver
*driver
)
1419 struct goku_udc
*dev
= the_controller
;
1420 unsigned long flags
;
1424 if (!driver
|| driver
!= dev
->driver
|| !driver
->unbind
)
1427 spin_lock_irqsave(&dev
->lock
, flags
);
1429 stop_activity(dev
, driver
);
1430 spin_unlock_irqrestore(&dev
->lock
, flags
);
1432 driver
->unbind(&dev
->gadget
);
1433 dev
->gadget
.dev
.driver
= NULL
;
1435 DBG(dev
, "unregistered driver '%s'\n", driver
->driver
.name
);
1439 /*-------------------------------------------------------------------------*/
1441 static void ep0_setup(struct goku_udc
*dev
)
1443 struct goku_udc_regs __iomem
*regs
= dev
->regs
;
1444 struct usb_ctrlrequest ctrl
;
1447 /* read SETUP packet and enter DATA stage */
1448 ctrl
.bRequestType
= readl(®s
->bRequestType
);
1449 ctrl
.bRequest
= readl(®s
->bRequest
);
1450 ctrl
.wValue
= cpu_to_le16((readl(®s
->wValueH
) << 8)
1451 | readl(®s
->wValueL
));
1452 ctrl
.wIndex
= cpu_to_le16((readl(®s
->wIndexH
) << 8)
1453 | readl(®s
->wIndexL
));
1454 ctrl
.wLength
= cpu_to_le16((readl(®s
->wLengthH
) << 8)
1455 | readl(®s
->wLengthL
));
1456 writel(0, ®s
->SetupRecv
);
1458 nuke(&dev
->ep
[0], 0);
1459 dev
->ep
[0].stopped
= 0;
1460 if (likely(ctrl
.bRequestType
& USB_DIR_IN
)) {
1461 dev
->ep
[0].is_in
= 1;
1462 dev
->ep0state
= EP0_IN
;
1463 /* detect early status stages */
1464 writel(ICONTROL_STATUSNAK
, &dev
->regs
->IntControl
);
1466 dev
->ep
[0].is_in
= 0;
1467 dev
->ep0state
= EP0_OUT
;
1469 /* NOTE: CLEAR_FEATURE is done in software so that we can
1470 * synchronize transfer restarts after bulk IN stalls. data
1471 * won't even enter the fifo until the halt is cleared.
1473 switch (ctrl
.bRequest
) {
1474 case USB_REQ_CLEAR_FEATURE
:
1475 switch (ctrl
.bRequestType
) {
1476 case USB_RECIP_ENDPOINT
:
1477 tmp
= le16_to_cpu(ctrl
.wIndex
) & 0x0f;
1478 /* active endpoint */
1479 if (tmp
> 3 || (!dev
->ep
[tmp
].desc
&& tmp
!= 0))
1481 if (ctrl
.wIndex
& cpu_to_le16(
1483 if (!dev
->ep
[tmp
].is_in
)
1486 if (dev
->ep
[tmp
].is_in
)
1489 if (ctrl
.wValue
!= cpu_to_le16(
1493 goku_clear_halt(&dev
->ep
[tmp
]);
1495 /* start ep0out status stage */
1496 writel(~(1<<0), ®s
->EOP
);
1497 dev
->ep
[0].stopped
= 1;
1498 dev
->ep0state
= EP0_STATUS
;
1500 case USB_RECIP_DEVICE
:
1501 /* device remote wakeup: always clear */
1502 if (ctrl
.wValue
!= cpu_to_le16(1))
1504 VDBG(dev
, "clear dev remote wakeup\n");
1506 case USB_RECIP_INTERFACE
:
1508 default: /* pass to gadget driver */
1518 VDBG(dev
, "SETUP %02x.%02x v%04x i%04x l%04x\n",
1519 ctrl
.bRequestType
, ctrl
.bRequest
,
1520 le16_to_cpu(ctrl
.wValue
), le16_to_cpu(ctrl
.wIndex
),
1521 le16_to_cpu(ctrl
.wLength
));
1524 /* hw wants to know when we're configured (or not) */
1525 dev
->req_config
= (ctrl
.bRequest
== USB_REQ_SET_CONFIGURATION
1526 && ctrl
.bRequestType
== USB_RECIP_DEVICE
);
1527 if (unlikely(dev
->req_config
))
1528 dev
->configured
= (ctrl
.wValue
!= cpu_to_le16(0));
1530 /* delegate everything to the gadget driver.
1531 * it may respond after this irq handler returns.
1533 spin_unlock (&dev
->lock
);
1534 tmp
= dev
->driver
->setup(&dev
->gadget
, &ctrl
);
1535 spin_lock (&dev
->lock
);
1536 if (unlikely(tmp
< 0)) {
1539 VDBG(dev
, "req %02x.%02x protocol STALL; err %d\n",
1540 ctrl
.bRequestType
, ctrl
.bRequest
, tmp
);
1542 command(regs
, COMMAND_STALL
, 0);
1543 dev
->ep
[0].stopped
= 1;
1544 dev
->ep0state
= EP0_STALL
;
1547 /* expect at least one data or status stage irq */
1550 #define ACK(irqbit) { \
1552 writel(~irqbit, ®s->int_status); \
1556 static irqreturn_t
goku_irq(int irq
, void *_dev
)
1558 struct goku_udc
*dev
= _dev
;
1559 struct goku_udc_regs __iomem
*regs
= dev
->regs
;
1561 u32 stat
, handled
= 0;
1562 unsigned i
, rescans
= 5;
1564 spin_lock(&dev
->lock
);
1567 stat
= readl(®s
->int_status
) & dev
->int_enable
;
1572 /* device-wide irqs */
1573 if (unlikely(stat
& INT_DEVWIDE
)) {
1574 if (stat
& INT_SYSERROR
) {
1575 ERROR(dev
, "system error\n");
1576 stop_activity(dev
, dev
->driver
);
1579 // FIXME have a neater way to prevent re-enumeration
1583 if (stat
& INT_PWRDETECT
) {
1584 writel(~stat
, ®s
->int_status
);
1585 if (readl(&dev
->regs
->power_detect
) & PW_DETECT
) {
1586 VDBG(dev
, "connect\n");
1589 DBG(dev
, "disconnect\n");
1590 if (dev
->gadget
.speed
== USB_SPEED_FULL
)
1591 stop_activity(dev
, dev
->driver
);
1592 dev
->ep0state
= EP0_DISCONNECT
;
1593 dev
->int_enable
= INT_DEVWIDE
;
1594 writel(dev
->int_enable
, &dev
->regs
->int_enable
);
1600 if (stat
& INT_SUSPEND
) {
1602 if (readl(®s
->ep_status
[0]) & EPxSTATUS_SUSPEND
) {
1603 switch (dev
->ep0state
) {
1604 case EP0_DISCONNECT
:
1610 DBG(dev
, "USB suspend\n");
1611 dev
->ep0state
= EP0_SUSPEND
;
1612 if (dev
->gadget
.speed
!= USB_SPEED_UNKNOWN
1614 && dev
->driver
->suspend
) {
1615 spin_unlock(&dev
->lock
);
1616 dev
->driver
->suspend(&dev
->gadget
);
1617 spin_lock(&dev
->lock
);
1620 if (dev
->ep0state
!= EP0_SUSPEND
) {
1621 DBG(dev
, "bogus USB resume %d\n",
1625 DBG(dev
, "USB resume\n");
1626 dev
->ep0state
= EP0_IDLE
;
1627 if (dev
->gadget
.speed
!= USB_SPEED_UNKNOWN
1629 && dev
->driver
->resume
) {
1630 spin_unlock(&dev
->lock
);
1631 dev
->driver
->resume(&dev
->gadget
);
1632 spin_lock(&dev
->lock
);
1637 if (stat
& INT_USBRESET
) { /* hub reset done */
1639 INFO(dev
, "USB reset done, gadget %s\n",
1640 dev
->driver
->driver
.name
);
1642 // and INT_ERR on some endpoint's crc/bitstuff/... problem
1645 /* progress ep0 setup, data, or status stages.
1646 * no transition {EP0_STATUS, EP0_STALL} --> EP0_IDLE; saves irqs
1648 if (stat
& INT_SETUP
) {
1653 if (stat
& INT_STATUSNAK
) {
1654 ACK(INT_STATUSNAK
|INT_ENDPOINT0
);
1655 if (dev
->ep0state
== EP0_IN
) {
1659 writel(~(1<<0), ®s
->EOP
);
1660 dev
->ep0state
= EP0_STATUS
;
1663 if (stat
& INT_ENDPOINT0
) {
1670 /* dma completion */
1671 if (stat
& INT_MSTRDEND
) { /* IN */
1673 ep
= &dev
->ep
[UDC_MSTRD_ENDPOINT
];
1675 dma_advance(dev
, ep
);
1677 if (stat
& INT_MSTWREND
) { /* OUT */
1679 ep
= &dev
->ep
[UDC_MSTWR_ENDPOINT
];
1681 dma_advance(dev
, ep
);
1683 if (stat
& INT_MSTWRTMOUT
) { /* OUT */
1684 ACK(INT_MSTWRTMOUT
);
1685 ep
= &dev
->ep
[UDC_MSTWR_ENDPOINT
];
1687 ERROR(dev
, "%s write timeout ?\n", ep
->ep
.name
);
1688 // reset dma? then dma_advance()
1692 for (i
= 1; i
< 4; i
++) {
1693 u32 tmp
= INT_EPxDATASET(i
);
1699 if (list_empty (&ep
->queue
))
1700 pio_irq_disable(dev
, regs
, i
);
1710 (void)readl(®s
->int_enable
);
1711 spin_unlock(&dev
->lock
);
1713 DBG(dev
, "unhandled irq status: %05x (%05x, %05x)\n", stat
,
1714 readl(®s
->int_status
), dev
->int_enable
);
1715 return IRQ_RETVAL(handled
);
1720 /*-------------------------------------------------------------------------*/
1722 static void gadget_release(struct device
*_dev
)
1724 struct goku_udc
*dev
= dev_get_drvdata(_dev
);
1729 /* tear down the binding between this driver and the pci device */
1731 static void goku_remove(struct pci_dev
*pdev
)
1733 struct goku_udc
*dev
= pci_get_drvdata(pdev
);
1735 DBG(dev
, "%s\n", __func__
);
1737 usb_del_gadget_udc(&dev
->gadget
);
1739 BUG_ON(dev
->driver
);
1741 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1742 remove_proc_entry(proc_node_name
, NULL
);
1747 free_irq(pdev
->irq
, dev
);
1750 if (dev
->got_region
)
1751 release_mem_region(pci_resource_start (pdev
, 0),
1752 pci_resource_len (pdev
, 0));
1754 pci_disable_device(pdev
);
1755 if (dev
->registered
)
1756 device_unregister(&dev
->gadget
.dev
);
1758 pci_set_drvdata(pdev
, NULL
);
1760 the_controller
= NULL
;
1762 INFO(dev
, "unbind\n");
1765 /* wrap this driver around the specified pci device, but
1766 * don't respond over USB until a gadget driver binds to us.
1769 static int goku_probe(struct pci_dev
*pdev
, const struct pci_device_id
*id
)
1771 struct goku_udc
*dev
= NULL
;
1772 unsigned long resource
, len
;
1773 void __iomem
*base
= NULL
;
1776 /* if you want to support more than one controller in a system,
1777 * usb_gadget_driver_{register,unregister}() must change.
1779 if (the_controller
) {
1780 pr_warning("ignoring %s\n", pci_name(pdev
));
1784 printk(KERN_ERR
"Check PCI %s IRQ setup!\n", pci_name(pdev
));
1789 /* alloc, and start init */
1790 dev
= kzalloc (sizeof *dev
, GFP_KERNEL
);
1792 pr_debug("enomem %s\n", pci_name(pdev
));
1797 spin_lock_init(&dev
->lock
);
1799 dev
->gadget
.ops
= &goku_ops
;
1800 dev
->gadget
.max_speed
= USB_SPEED_FULL
;
1802 /* the "gadget" abstracts/virtualizes the controller */
1803 dev_set_name(&dev
->gadget
.dev
, "gadget");
1804 dev
->gadget
.dev
.parent
= &pdev
->dev
;
1805 dev
->gadget
.dev
.dma_mask
= pdev
->dev
.dma_mask
;
1806 dev
->gadget
.dev
.release
= gadget_release
;
1807 dev
->gadget
.name
= driver_name
;
1809 /* now all the pci goodies ... */
1810 retval
= pci_enable_device(pdev
);
1812 DBG(dev
, "can't enable, %d\n", retval
);
1817 resource
= pci_resource_start(pdev
, 0);
1818 len
= pci_resource_len(pdev
, 0);
1819 if (!request_mem_region(resource
, len
, driver_name
)) {
1820 DBG(dev
, "controller already in use\n");
1824 dev
->got_region
= 1;
1826 base
= ioremap_nocache(resource
, len
);
1828 DBG(dev
, "can't map memory\n");
1832 dev
->regs
= (struct goku_udc_regs __iomem
*) base
;
1834 pci_set_drvdata(pdev
, dev
);
1835 INFO(dev
, "%s\n", driver_desc
);
1836 INFO(dev
, "version: " DRIVER_VERSION
" %s\n", dmastr());
1837 INFO(dev
, "irq %d, pci mem %p\n", pdev
->irq
, base
);
1839 /* init to known state, then setup irqs */
1842 if (request_irq(pdev
->irq
, goku_irq
, IRQF_SHARED
/*|IRQF_SAMPLE_RANDOM*/,
1843 driver_name
, dev
) != 0) {
1844 DBG(dev
, "request interrupt %d failed\n", pdev
->irq
);
1850 pci_set_master(pdev
);
1853 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1854 create_proc_read_entry(proc_node_name
, 0, NULL
, udc_proc_read
, dev
);
1857 the_controller
= dev
;
1858 retval
= device_register(&dev
->gadget
.dev
);
1860 put_device(&dev
->gadget
.dev
);
1863 dev
->registered
= 1;
1864 retval
= usb_add_gadget_udc(&pdev
->dev
, &dev
->gadget
);
1877 /*-------------------------------------------------------------------------*/
1879 static const struct pci_device_id pci_ids
[] = { {
1880 .class = ((PCI_CLASS_SERIAL_USB
<< 8) | 0xfe),
1882 .vendor
= 0x102f, /* Toshiba */
1883 .device
= 0x0107, /* this UDC */
1884 .subvendor
= PCI_ANY_ID
,
1885 .subdevice
= PCI_ANY_ID
,
1887 }, { /* end: all zeroes */ }
1889 MODULE_DEVICE_TABLE (pci
, pci_ids
);
1891 static struct pci_driver goku_pci_driver
= {
1892 .name
= (char *) driver_name
,
1893 .id_table
= pci_ids
,
1895 .probe
= goku_probe
,
1896 .remove
= goku_remove
,
1898 /* FIXME add power management support */
1901 static int __init
init (void)
1903 return pci_register_driver (&goku_pci_driver
);
1907 static void __exit
cleanup (void)
1909 pci_unregister_driver (&goku_pci_driver
);
1911 module_exit (cleanup
);