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/config.h>
28 #include <linux/kernel.h>
29 #include <linux/module.h>
30 #include <linux/pci.h>
31 #include <linux/delay.h>
32 #include <linux/ioport.h>
33 #include <linux/sched.h>
34 #include <linux/slab.h>
35 #include <linux/smp_lock.h>
36 #include <linux/errno.h>
37 #include <linux/init.h>
38 #include <linux/timer.h>
39 #include <linux/list.h>
40 #include <linux/interrupt.h>
41 #include <linux/proc_fs.h>
42 #include <linux/device.h>
43 #include <linux/usb_ch9.h>
44 #include <linux/usb_gadget.h>
46 #include <asm/byteorder.h>
49 #include <asm/system.h>
50 #include <asm/unaligned.h>
55 #define DRIVER_DESC "TC86C001 USB Device Controller"
56 #define DRIVER_VERSION "30-Oct 2003"
58 #define DMA_ADDR_INVALID (~(dma_addr_t)0)
60 static const char driver_name
[] = "goku_udc";
61 static const char driver_desc
[] = DRIVER_DESC
;
63 MODULE_AUTHOR("source@mvista.com");
64 MODULE_DESCRIPTION(DRIVER_DESC
);
65 MODULE_LICENSE("GPL");
69 * IN dma behaves ok under testing, though the IN-dma abort paths don't
70 * seem to behave quite as expected. Used by default.
72 * OUT dma documents design problems handling the common "short packet"
73 * transfer termination policy; it couldn't enabled by default, even
74 * if the OUT-dma abort problems had a resolution.
76 static unsigned use_dma
= 1;
79 //#include <linux/moduleparam.h>
80 /* "modprobe goku_udc use_dma=1" etc
82 * 1 to use IN dma only (normal operation)
83 * 2 to use IN and OUT dma
85 module_param(use_dma
, uint
, S_IRUGO
);
88 /*-------------------------------------------------------------------------*/
90 static void nuke(struct goku_ep
*, int status
);
93 command(struct goku_udc_regs __iomem
*regs
, int command
, unsigned epnum
)
95 writel(COMMAND_EP(epnum
) | command
, ®s
->Command
);
100 goku_ep_enable(struct usb_ep
*_ep
, const struct usb_endpoint_descriptor
*desc
)
102 struct goku_udc
*dev
;
108 ep
= container_of(_ep
, struct goku_ep
, ep
);
109 if (!_ep
|| !desc
|| ep
->desc
110 || desc
->bDescriptorType
!= USB_DT_ENDPOINT
)
113 if (ep
== &dev
->ep
[0])
115 if (!dev
->driver
|| dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)
117 if (ep
->num
!= (desc
->bEndpointAddress
& 0x0f))
120 switch (desc
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) {
121 case USB_ENDPOINT_XFER_BULK
:
122 case USB_ENDPOINT_XFER_INT
:
128 if ((readl(ep
->reg_status
) & EPxSTATUS_EP_MASK
)
129 != EPxSTATUS_EP_INVALID
)
132 /* enabling the no-toggle interrupt mode would need an api hook */
134 max
= le16_to_cpu(get_unaligned(&desc
->wMaxPacketSize
));
144 mode
|= 2 << 1; /* bulk, or intr-with-toggle */
146 /* ep1/ep2 dma direction is chosen early; it works in the other
147 * direction, with pio. be cautious with out-dma.
149 ep
->is_in
= (USB_DIR_IN
& desc
->bEndpointAddress
) != 0;
152 ep
->dma
= (use_dma
!= 0) && (ep
->num
== UDC_MSTRD_ENDPOINT
);
154 ep
->dma
= (use_dma
== 2) && (ep
->num
== UDC_MSTWR_ENDPOINT
);
156 DBG(dev
, "%s out-dma hides short packets\n",
160 spin_lock_irqsave(&ep
->dev
->lock
, flags
);
162 /* ep1 and ep2 can do double buffering and/or dma */
164 struct goku_udc_regs __iomem
*regs
= ep
->dev
->regs
;
167 /* double buffer except (for now) with pio in */
168 tmp
= ((ep
->dma
|| !ep
->is_in
)
169 ? 0x10 /* double buffered */
170 : 0x11 /* single buffer */
172 tmp
|= readl(®s
->EPxSingle
);
173 writel(tmp
, ®s
->EPxSingle
);
175 tmp
= (ep
->dma
? 0x10/*dma*/ : 0x11/*pio*/) << ep
->num
;
176 tmp
|= readl(®s
->EPxBCS
);
177 writel(tmp
, ®s
->EPxBCS
);
179 writel(mode
, ep
->reg_mode
);
180 command(ep
->dev
->regs
, COMMAND_RESET
, ep
->num
);
181 ep
->ep
.maxpacket
= max
;
184 spin_unlock_irqrestore(&ep
->dev
->lock
, flags
);
186 DBG(dev
, "enable %s %s %s maxpacket %u\n", ep
->ep
.name
,
187 ep
->is_in
? "IN" : "OUT",
188 ep
->dma
? "dma" : "pio",
194 static void ep_reset(struct goku_udc_regs __iomem
*regs
, struct goku_ep
*ep
)
196 struct goku_udc
*dev
= ep
->dev
;
199 command(regs
, COMMAND_INVALID
, ep
->num
);
201 if (ep
->num
== UDC_MSTWR_ENDPOINT
)
202 dev
->int_enable
&= ~(INT_MSTWREND
204 else if (ep
->num
== UDC_MSTRD_ENDPOINT
)
205 dev
->int_enable
&= ~INT_MSTRDEND
;
206 dev
->int_enable
&= ~INT_EPxDATASET (ep
->num
);
208 dev
->int_enable
&= ~INT_EP0
;
209 writel(dev
->int_enable
, ®s
->int_enable
);
210 readl(®s
->int_enable
);
212 struct goku_udc_regs __iomem
*r
= ep
->dev
->regs
;
215 tmp
= readl(&r
->EPxSingle
);
216 tmp
&= ~(0x11 << ep
->num
);
217 writel(tmp
, &r
->EPxSingle
);
219 tmp
= readl(&r
->EPxBCS
);
220 tmp
&= ~(0x11 << ep
->num
);
221 writel(tmp
, &r
->EPxBCS
);
223 /* reset dma in case we're still using it */
227 master
= readl(®s
->dma_master
) & MST_RW_BITS
;
228 if (ep
->num
== UDC_MSTWR_ENDPOINT
) {
229 master
&= ~MST_W_BITS
;
230 master
|= MST_WR_RESET
;
232 master
&= ~MST_R_BITS
;
233 master
|= MST_RD_RESET
;
235 writel(master
, ®s
->dma_master
);
239 ep
->ep
.maxpacket
= MAX_FIFO_SIZE
;
246 static int goku_ep_disable(struct usb_ep
*_ep
)
249 struct goku_udc
*dev
;
252 ep
= container_of(_ep
, struct goku_ep
, ep
);
253 if (!_ep
|| !ep
->desc
)
256 if (dev
->ep0state
== EP0_SUSPEND
)
259 VDBG(dev
, "disable %s\n", _ep
->name
);
261 spin_lock_irqsave(&dev
->lock
, flags
);
262 nuke(ep
, -ESHUTDOWN
);
263 ep_reset(dev
->regs
, ep
);
264 spin_unlock_irqrestore(&dev
->lock
, flags
);
269 /*-------------------------------------------------------------------------*/
271 static struct usb_request
*
272 goku_alloc_request(struct usb_ep
*_ep
, int gfp_flags
)
274 struct goku_request
*req
;
278 req
= kmalloc(sizeof *req
, gfp_flags
);
282 memset(req
, 0, sizeof *req
);
283 req
->req
.dma
= DMA_ADDR_INVALID
;
284 INIT_LIST_HEAD(&req
->queue
);
289 goku_free_request(struct usb_ep
*_ep
, struct usb_request
*_req
)
291 struct goku_request
*req
;
296 req
= container_of(_req
, struct goku_request
, req
);
297 WARN_ON(!list_empty(&req
->queue
));
301 /*-------------------------------------------------------------------------*/
305 /* many common platforms have dma-coherent caches, which means that it's
306 * safe to use kmalloc() memory for all i/o buffers without using any
307 * cache flushing calls. (unless you're trying to share cache lines
308 * between dma and non-dma activities, which is a slow idea in any case.)
310 * other platforms need more care, with 2.6 having a moderately general
311 * solution except for the common "buffer is smaller than a page" case.
313 #if defined(CONFIG_X86)
316 #elif defined(CONFIG_MIPS) && !defined(CONFIG_NONCOHERENT_IO)
319 #elif defined(CONFIG_PPC) && !defined(CONFIG_NOT_COHERENT_CACHE)
324 /* allocating buffers this way eliminates dma mapping overhead, which
325 * on some platforms will mean eliminating a per-io buffer copy. with
326 * some kinds of system caches, further tweaks may still be needed.
329 goku_alloc_buffer(struct usb_ep
*_ep
, unsigned bytes
,
330 dma_addr_t
*dma
, int gfp_flags
)
335 ep
= container_of(_ep
, struct goku_ep
, ep
);
338 *dma
= DMA_ADDR_INVALID
;
340 #if defined(USE_KMALLOC)
341 retval
= kmalloc(bytes
, gfp_flags
);
343 *dma
= virt_to_phys(retval
);
346 /* the main problem with this call is that it wastes memory
347 * on typical 1/N page allocations: it allocates 1-N pages.
349 #warning Using dma_alloc_coherent even with buffers smaller than a page.
350 retval
= dma_alloc_coherent(&ep
->dev
->pdev
->dev
,
351 bytes
, dma
, gfp_flags
);
353 retval
= kmalloc(bytes
, gfp_flags
);
359 goku_free_buffer(struct usb_ep
*_ep
, void *buf
, dma_addr_t dma
, unsigned bytes
)
361 /* free memory into the right allocator */
363 if (dma
!= DMA_ADDR_INVALID
) {
366 ep
= container_of(_ep
, struct goku_ep
, ep
);
369 dma_free_coherent(&ep
->dev
->pdev
->dev
, bytes
, buf
, dma
);
375 /*-------------------------------------------------------------------------*/
378 done(struct goku_ep
*ep
, struct goku_request
*req
, int status
)
380 struct goku_udc
*dev
;
381 unsigned stopped
= ep
->stopped
;
383 list_del_init(&req
->queue
);
385 if (likely(req
->req
.status
== -EINPROGRESS
))
386 req
->req
.status
= status
;
388 status
= req
->req
.status
;
392 pci_unmap_single(dev
->pdev
, req
->req
.dma
, req
->req
.length
,
393 ep
->is_in
? PCI_DMA_TODEVICE
: PCI_DMA_FROMDEVICE
);
394 req
->req
.dma
= DMA_ADDR_INVALID
;
399 if (status
&& status
!= -ESHUTDOWN
)
401 VDBG(dev
, "complete %s req %p stat %d len %u/%u\n",
402 ep
->ep
.name
, &req
->req
, status
,
403 req
->req
.actual
, req
->req
.length
);
405 /* don't modify queue heads during completion callback */
407 spin_unlock(&dev
->lock
);
408 req
->req
.complete(&ep
->ep
, &req
->req
);
409 spin_lock(&dev
->lock
);
410 ep
->stopped
= stopped
;
413 /*-------------------------------------------------------------------------*/
416 write_packet(u32 __iomem
*fifo
, u8
*buf
, struct goku_request
*req
, unsigned max
)
418 unsigned length
, count
;
420 length
= min(req
->req
.length
- req
->req
.actual
, max
);
421 req
->req
.actual
+= length
;
424 while (likely(count
--))
425 writel(*buf
++, fifo
);
429 // return: 0 = still running, 1 = completed, negative = errno
430 static int write_fifo(struct goku_ep
*ep
, struct goku_request
*req
)
432 struct goku_udc
*dev
= ep
->dev
;
438 tmp
= readl(&dev
->regs
->DataSet
);
439 buf
= req
->req
.buf
+ req
->req
.actual
;
443 if (unlikely(ep
->num
== 0 && dev
->ep0state
!= EP0_IN
))
446 /* NOTE: just single-buffered PIO-IN for now. */
447 if (unlikely((tmp
& DATASET_A(ep
->num
)) != 0))
450 /* clear our "packet available" irq */
452 writel(~INT_EPxDATASET(ep
->num
), &dev
->regs
->int_status
);
454 count
= write_packet(ep
->reg_fifo
, buf
, req
, ep
->ep
.maxpacket
);
456 /* last packet often short (sometimes a zlp, especially on ep0) */
457 if (unlikely(count
!= ep
->ep
.maxpacket
)) {
458 writel(~(1<<ep
->num
), &dev
->regs
->EOP
);
460 dev
->ep
[0].stopped
= 1;
461 dev
->ep0state
= EP0_STATUS
;
465 if (likely(req
->req
.length
!= req
->req
.actual
)
471 #if 0 /* printk seemed to trash is_last...*/
473 VDBG(dev
, "wrote %s %u bytes%s IN %u left %p\n",
474 ep
->ep
.name
, count
, is_last
? "/last" : "",
475 req
->req
.length
- req
->req
.actual
, req
);
478 /* requests complete when all IN data is in the FIFO,
479 * or sometimes later, if a zlp was needed.
489 static int read_fifo(struct goku_ep
*ep
, struct goku_request
*req
)
491 struct goku_udc_regs __iomem
*regs
;
494 unsigned bufferspace
, is_short
, dbuff
;
496 regs
= ep
->dev
->regs
;
498 buf
= req
->req
.buf
+ req
->req
.actual
;
501 if (unlikely(ep
->num
== 0 && ep
->dev
->ep0state
!= EP0_OUT
))
504 dbuff
= (ep
->num
== 1 || ep
->num
== 2);
506 /* ack dataset irq matching the status we'll handle */
508 writel(~INT_EPxDATASET(ep
->num
), ®s
->int_status
);
510 set
= readl(®s
->DataSet
) & DATASET_AB(ep
->num
);
511 size
= readl(®s
->EPxSizeLA
[ep
->num
]);
512 bufferspace
= req
->req
.length
- req
->req
.actual
;
514 /* usually do nothing without an OUT packet */
515 if (likely(ep
->num
!= 0 || bufferspace
!= 0)) {
516 if (unlikely(set
== 0))
518 /* use ep1/ep2 double-buffering for OUT */
519 if (!(size
& PACKET_ACTIVE
))
520 size
= readl(®s
->EPxSizeLB
[ep
->num
]);
521 if (!(size
& PACKET_ACTIVE
)) // "can't happen"
523 size
&= DATASIZE
; /* EPxSizeH == 0 */
525 /* ep0out no-out-data case for set_config, etc */
529 /* read all bytes from this packet */
530 req
->req
.actual
+= size
;
531 is_short
= (size
< ep
->ep
.maxpacket
);
533 VDBG(ep
->dev
, "read %s %u bytes%s OUT req %p %u/%u\n",
534 ep
->ep
.name
, size
, is_short
? "/S" : "",
535 req
, req
->req
.actual
, req
->req
.length
);
537 while (likely(size
-- != 0)) {
538 u8 byte
= (u8
) readl(ep
->reg_fifo
);
540 if (unlikely(bufferspace
== 0)) {
541 /* this happens when the driver's buffer
542 * is smaller than what the host sent.
543 * discard the extra data in this packet.
545 if (req
->req
.status
!= -EOVERFLOW
)
546 DBG(ep
->dev
, "%s overflow %u\n",
548 req
->req
.status
= -EOVERFLOW
;
556 if (unlikely(is_short
|| req
->req
.actual
== req
->req
.length
)) {
557 if (unlikely(ep
->num
== 0)) {
558 /* non-control endpoints now usable? */
559 if (ep
->dev
->req_config
)
560 writel(ep
->dev
->configured
561 ? USBSTATE_CONFIGURED
564 /* ep0out status stage */
565 writel(~(1<<0), ®s
->EOP
);
567 ep
->dev
->ep0state
= EP0_STATUS
;
571 /* empty the second buffer asap */
572 if (dbuff
&& !list_empty(&ep
->queue
)) {
573 req
= list_entry(ep
->queue
.next
,
574 struct goku_request
, queue
);
584 pio_irq_enable(struct goku_udc
*dev
,
585 struct goku_udc_regs __iomem
*regs
, int epnum
)
587 dev
->int_enable
|= INT_EPxDATASET (epnum
);
588 writel(dev
->int_enable
, ®s
->int_enable
);
589 /* write may still be posted */
593 pio_irq_disable(struct goku_udc
*dev
,
594 struct goku_udc_regs __iomem
*regs
, int epnum
)
596 dev
->int_enable
&= ~INT_EPxDATASET (epnum
);
597 writel(dev
->int_enable
, ®s
->int_enable
);
598 /* write may still be posted */
602 pio_advance(struct goku_ep
*ep
)
604 struct goku_request
*req
;
606 if (unlikely(list_empty (&ep
->queue
)))
608 req
= list_entry(ep
->queue
.next
, struct goku_request
, queue
);
609 (ep
->is_in
? write_fifo
: read_fifo
)(ep
, req
);
613 /*-------------------------------------------------------------------------*/
615 // return: 0 = q running, 1 = q stopped, negative = errno
616 static int start_dma(struct goku_ep
*ep
, struct goku_request
*req
)
618 struct goku_udc_regs __iomem
*regs
= ep
->dev
->regs
;
620 u32 start
= req
->req
.dma
;
621 u32 end
= start
+ req
->req
.length
- 1;
623 master
= readl(®s
->dma_master
) & MST_RW_BITS
;
625 /* re-init the bits affecting IN dma; careful with zlps */
626 if (likely(ep
->is_in
)) {
627 if (unlikely(master
& MST_RD_ENA
)) {
628 DBG (ep
->dev
, "start, IN active dma %03x!!\n",
632 writel(end
, ®s
->in_dma_end
);
633 writel(start
, ®s
->in_dma_start
);
635 master
&= ~MST_R_BITS
;
636 if (unlikely(req
->req
.length
== 0))
637 master
= MST_RD_ENA
| MST_RD_EOPB
;
638 else if ((req
->req
.length
% ep
->ep
.maxpacket
) != 0
640 master
= MST_RD_ENA
| MST_EOPB_ENA
;
642 master
= MST_RD_ENA
| MST_EOPB_DIS
;
644 ep
->dev
->int_enable
|= INT_MSTRDEND
;
646 /* Goku DMA-OUT merges short packets, which plays poorly with
647 * protocols where short packets mark the transfer boundaries.
648 * The chip supports a nonstandard policy with INT_MSTWRTMOUT,
649 * ending transfers after 3 SOFs; we don't turn it on.
652 if (unlikely(master
& MST_WR_ENA
)) {
653 DBG (ep
->dev
, "start, OUT active dma %03x!!\n",
657 writel(end
, ®s
->out_dma_end
);
658 writel(start
, ®s
->out_dma_start
);
660 master
&= ~MST_W_BITS
;
661 master
|= MST_WR_ENA
| MST_TIMEOUT_DIS
;
663 ep
->dev
->int_enable
|= INT_MSTWREND
|INT_MSTWRTMOUT
;
666 writel(master
, ®s
->dma_master
);
667 writel(ep
->dev
->int_enable
, ®s
->int_enable
);
671 static void dma_advance(struct goku_udc
*dev
, struct goku_ep
*ep
)
673 struct goku_request
*req
;
674 struct goku_udc_regs __iomem
*regs
= ep
->dev
->regs
;
677 master
= readl(®s
->dma_master
);
679 if (unlikely(list_empty(&ep
->queue
))) {
682 dev
->int_enable
&= ~INT_MSTRDEND
;
684 dev
->int_enable
&= ~(INT_MSTWREND
|INT_MSTWRTMOUT
);
685 writel(dev
->int_enable
, ®s
->int_enable
);
688 req
= list_entry(ep
->queue
.next
, struct goku_request
, queue
);
690 /* normal hw dma completion (not abort) */
691 if (likely(ep
->is_in
)) {
692 if (unlikely(master
& MST_RD_ENA
))
694 req
->req
.actual
= readl(®s
->in_dma_current
);
696 if (unlikely(master
& MST_WR_ENA
))
699 /* hardware merges short packets, and also hides packet
700 * overruns. a partial packet MAY be in the fifo here.
702 req
->req
.actual
= readl(®s
->out_dma_current
);
704 req
->req
.actual
-= req
->req
.dma
;
708 VDBG(dev
, "done %s %s dma, %u/%u bytes, req %p\n",
709 ep
->ep
.name
, ep
->is_in
? "IN" : "OUT",
710 req
->req
.actual
, req
->req
.length
, req
);
713 if (list_empty(&ep
->queue
))
715 req
= list_entry(ep
->queue
.next
, struct goku_request
, queue
);
716 (void) start_dma(ep
, req
);
719 static void abort_dma(struct goku_ep
*ep
, int status
)
721 struct goku_udc_regs __iomem
*regs
= ep
->dev
->regs
;
722 struct goku_request
*req
;
725 /* NAK future host requests, hoping the implicit delay lets the
726 * dma engine finish reading (or writing) its latest packet and
727 * empty the dma buffer (up to 16 bytes).
729 * This avoids needing to clean up a partial packet in the fifo;
730 * we can't do that for IN without side effects to HALT and TOGGLE.
732 command(regs
, COMMAND_FIFO_DISABLE
, ep
->num
);
733 req
= list_entry(ep
->queue
.next
, struct goku_request
, queue
);
734 master
= readl(®s
->dma_master
) & MST_RW_BITS
;
736 /* FIXME using these resets isn't usably documented. this may
737 * not work unless it's followed by disabling the endpoint.
739 * FIXME the OUT reset path doesn't even behave consistently.
742 if (unlikely((readl(®s
->dma_master
) & MST_RD_ENA
) == 0))
744 curr
= readl(®s
->in_dma_current
);
746 writel(curr
, ®s
->in_dma_end
);
747 writel(curr
, ®s
->in_dma_start
);
749 master
&= ~MST_R_BITS
;
750 master
|= MST_RD_RESET
;
751 writel(master
, ®s
->dma_master
);
753 if (readl(®s
->dma_master
) & MST_RD_ENA
)
754 DBG(ep
->dev
, "IN dma active after reset!\n");
757 if (unlikely((readl(®s
->dma_master
) & MST_WR_ENA
) == 0))
759 curr
= readl(®s
->out_dma_current
);
761 writel(curr
, ®s
->out_dma_end
);
762 writel(curr
, ®s
->out_dma_start
);
764 master
&= ~MST_W_BITS
;
765 master
|= MST_WR_RESET
;
766 writel(master
, ®s
->dma_master
);
768 if (readl(®s
->dma_master
) & MST_WR_ENA
)
769 DBG(ep
->dev
, "OUT dma active after reset!\n");
771 req
->req
.actual
= (curr
- req
->req
.dma
) + 1;
772 req
->req
.status
= status
;
774 VDBG(ep
->dev
, "%s %s %s %d/%d\n", __FUNCTION__
, ep
->ep
.name
,
775 ep
->is_in
? "IN" : "OUT",
776 req
->req
.actual
, req
->req
.length
);
778 command(regs
, COMMAND_FIFO_ENABLE
, ep
->num
);
783 /* dma already completed; no abort needed */
784 command(regs
, COMMAND_FIFO_ENABLE
, ep
->num
);
785 req
->req
.actual
= req
->req
.length
;
789 /*-------------------------------------------------------------------------*/
792 goku_queue(struct usb_ep
*_ep
, struct usb_request
*_req
, int gfp_flags
)
794 struct goku_request
*req
;
796 struct goku_udc
*dev
;
800 /* always require a cpu-view buffer so pio works */
801 req
= container_of(_req
, struct goku_request
, req
);
802 if (unlikely(!_req
|| !_req
->complete
803 || !_req
->buf
|| !list_empty(&req
->queue
)))
805 ep
= container_of(_ep
, struct goku_ep
, ep
);
806 if (unlikely(!_ep
|| (!ep
->desc
&& ep
->num
!= 0)))
809 if (unlikely(!dev
->driver
|| dev
->gadget
.speed
== USB_SPEED_UNKNOWN
))
812 /* can't touch registers when suspended */
813 if (dev
->ep0state
== EP0_SUSPEND
)
816 /* set up dma mapping in case the caller didn't */
817 if (ep
->dma
&& _req
->dma
== DMA_ADDR_INVALID
) {
818 _req
->dma
= pci_map_single(dev
->pdev
, _req
->buf
, _req
->length
,
819 ep
->is_in
? PCI_DMA_TODEVICE
: PCI_DMA_FROMDEVICE
);
824 VDBG(dev
, "%s queue req %p, len %u buf %p\n",
825 _ep
->name
, _req
, _req
->length
, _req
->buf
);
828 spin_lock_irqsave(&dev
->lock
, flags
);
830 _req
->status
= -EINPROGRESS
;
833 /* for ep0 IN without premature status, zlp is required and
834 * writing EOP starts the status stage (OUT).
836 if (unlikely(ep
->num
== 0 && ep
->is_in
))
839 /* kickstart this i/o queue? */
841 if (list_empty(&ep
->queue
) && likely(!ep
->stopped
)) {
842 /* dma: done after dma completion IRQ (or error)
843 * pio: done after last fifo operation
846 status
= start_dma(ep
, req
);
848 status
= (ep
->is_in
? write_fifo
: read_fifo
)(ep
, req
);
850 if (unlikely(status
!= 0)) {
856 } /* else pio or dma irq handler advances the queue. */
858 if (likely(req
!= 0))
859 list_add_tail(&req
->queue
, &ep
->queue
);
861 if (likely(!list_empty(&ep
->queue
))
862 && likely(ep
->num
!= 0)
864 && !(dev
->int_enable
& INT_EPxDATASET (ep
->num
)))
865 pio_irq_enable(dev
, dev
->regs
, ep
->num
);
867 spin_unlock_irqrestore(&dev
->lock
, flags
);
869 /* pci writes may still be posted */
873 /* dequeue ALL requests */
874 static void nuke(struct goku_ep
*ep
, int status
)
876 struct goku_request
*req
;
879 if (list_empty(&ep
->queue
))
882 abort_dma(ep
, status
);
883 while (!list_empty(&ep
->queue
)) {
884 req
= list_entry(ep
->queue
.next
, struct goku_request
, queue
);
885 done(ep
, req
, status
);
889 /* dequeue JUST ONE request */
890 static int goku_dequeue(struct usb_ep
*_ep
, struct usb_request
*_req
)
892 struct goku_request
*req
;
894 struct goku_udc
*dev
;
897 ep
= container_of(_ep
, struct goku_ep
, ep
);
898 if (!_ep
|| !_req
|| (!ep
->desc
&& ep
->num
!= 0))
904 /* we can't touch (dma) registers when suspended */
905 if (dev
->ep0state
== EP0_SUSPEND
)
908 VDBG(dev
, "%s %s %s %s %p\n", __FUNCTION__
, _ep
->name
,
909 ep
->is_in
? "IN" : "OUT",
910 ep
->dma
? "dma" : "pio",
913 spin_lock_irqsave(&dev
->lock
, flags
);
915 /* make sure it's actually queued on this endpoint */
916 list_for_each_entry (req
, &ep
->queue
, queue
) {
917 if (&req
->req
== _req
)
920 if (&req
->req
!= _req
) {
921 spin_unlock_irqrestore (&dev
->lock
, flags
);
925 if (ep
->dma
&& ep
->queue
.next
== &req
->queue
&& !ep
->stopped
) {
926 abort_dma(ep
, -ECONNRESET
);
927 done(ep
, req
, -ECONNRESET
);
928 dma_advance(dev
, ep
);
929 } else if (!list_empty(&req
->queue
))
930 done(ep
, req
, -ECONNRESET
);
933 spin_unlock_irqrestore(&dev
->lock
, flags
);
935 return req
? 0 : -EOPNOTSUPP
;
938 /*-------------------------------------------------------------------------*/
940 static void goku_clear_halt(struct goku_ep
*ep
)
942 // assert (ep->num !=0)
943 VDBG(ep
->dev
, "%s clear halt\n", ep
->ep
.name
);
944 command(ep
->dev
->regs
, COMMAND_SETDATA0
, ep
->num
);
945 command(ep
->dev
->regs
, COMMAND_STALL_CLEAR
, ep
->num
);
949 struct goku_request
*req
;
951 if (list_empty(&ep
->queue
))
953 req
= list_entry(ep
->queue
.next
, struct goku_request
,
955 (void) start_dma(ep
, req
);
961 static int goku_set_halt(struct usb_ep
*_ep
, int value
)
969 ep
= container_of (_ep
, struct goku_ep
, ep
);
973 ep
->dev
->ep0state
= EP0_STALL
;
974 ep
->dev
->ep
[0].stopped
= 1;
978 /* don't change EPxSTATUS_EP_INVALID to READY */
979 } else if (!ep
->desc
) {
980 DBG(ep
->dev
, "%s %s inactive?\n", __FUNCTION__
, ep
->ep
.name
);
984 spin_lock_irqsave(&ep
->dev
->lock
, flags
);
985 if (!list_empty(&ep
->queue
))
987 else if (ep
->is_in
&& value
988 /* data in (either) packet buffer? */
989 && (readl(&ep
->dev
->regs
->DataSet
)
990 & DATASET_AB(ep
->num
)))
996 VDBG(ep
->dev
, "%s set halt\n", ep
->ep
.name
);
997 command(ep
->dev
->regs
, COMMAND_STALL
, ep
->num
);
998 readl(ep
->reg_status
);
1000 spin_unlock_irqrestore(&ep
->dev
->lock
, flags
);
1004 static int goku_fifo_status(struct usb_ep
*_ep
)
1007 struct goku_udc_regs __iomem
*regs
;
1012 ep
= container_of(_ep
, struct goku_ep
, ep
);
1014 /* size is only reported sanely for OUT */
1018 /* ignores 16-byte dma buffer; SizeH == 0 */
1019 regs
= ep
->dev
->regs
;
1020 size
= readl(®s
->EPxSizeLA
[ep
->num
]) & DATASIZE
;
1021 size
+= readl(®s
->EPxSizeLB
[ep
->num
]) & DATASIZE
;
1022 VDBG(ep
->dev
, "%s %s %u\n", __FUNCTION__
, ep
->ep
.name
, size
);
1026 static void goku_fifo_flush(struct usb_ep
*_ep
)
1029 struct goku_udc_regs __iomem
*regs
;
1034 ep
= container_of(_ep
, struct goku_ep
, ep
);
1035 VDBG(ep
->dev
, "%s %s\n", __FUNCTION__
, ep
->ep
.name
);
1037 /* don't change EPxSTATUS_EP_INVALID to READY */
1038 if (!ep
->desc
&& ep
->num
!= 0) {
1039 DBG(ep
->dev
, "%s %s inactive?\n", __FUNCTION__
, ep
->ep
.name
);
1043 regs
= ep
->dev
->regs
;
1044 size
= readl(®s
->EPxSizeLA
[ep
->num
]);
1047 /* Non-desirable behavior: FIFO_CLEAR also clears the
1048 * endpoint halt feature. For OUT, we _could_ just read
1049 * the bytes out (PIO, if !ep->dma); for in, no choice.
1052 command(regs
, COMMAND_FIFO_CLEAR
, ep
->num
);
1055 static struct usb_ep_ops goku_ep_ops
= {
1056 .enable
= goku_ep_enable
,
1057 .disable
= goku_ep_disable
,
1059 .alloc_request
= goku_alloc_request
,
1060 .free_request
= goku_free_request
,
1062 .alloc_buffer
= goku_alloc_buffer
,
1063 .free_buffer
= goku_free_buffer
,
1065 .queue
= goku_queue
,
1066 .dequeue
= goku_dequeue
,
1068 .set_halt
= goku_set_halt
,
1069 .fifo_status
= goku_fifo_status
,
1070 .fifo_flush
= goku_fifo_flush
,
1073 /*-------------------------------------------------------------------------*/
1075 static int goku_get_frame(struct usb_gadget
*_gadget
)
1080 static const struct usb_gadget_ops goku_ops
= {
1081 .get_frame
= goku_get_frame
,
1086 /*-------------------------------------------------------------------------*/
1088 static inline char *dmastr(void)
1091 return "(dma disabled)";
1092 else if (use_dma
== 2)
1093 return "(dma IN and OUT)";
1098 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1100 static const char proc_node_name
[] = "driver/udc";
1102 #define FOURBITS "%s%s%s%s"
1103 #define EIGHTBITS FOURBITS FOURBITS
1106 dump_intmask(const char *label
, u32 mask
, char **next
, unsigned *size
)
1110 /* int_status is the same format ... */
1111 t
= scnprintf(*next
, *size
,
1112 "%s %05X =" FOURBITS EIGHTBITS EIGHTBITS
"\n",
1114 (mask
& INT_PWRDETECT
) ? " power" : "",
1115 (mask
& INT_SYSERROR
) ? " sys" : "",
1116 (mask
& INT_MSTRDEND
) ? " in-dma" : "",
1117 (mask
& INT_MSTWRTMOUT
) ? " wrtmo" : "",
1119 (mask
& INT_MSTWREND
) ? " out-dma" : "",
1120 (mask
& INT_MSTWRSET
) ? " wrset" : "",
1121 (mask
& INT_ERR
) ? " err" : "",
1122 (mask
& INT_SOF
) ? " sof" : "",
1124 (mask
& INT_EP3NAK
) ? " ep3nak" : "",
1125 (mask
& INT_EP2NAK
) ? " ep2nak" : "",
1126 (mask
& INT_EP1NAK
) ? " ep1nak" : "",
1127 (mask
& INT_EP3DATASET
) ? " ep3" : "",
1129 (mask
& INT_EP2DATASET
) ? " ep2" : "",
1130 (mask
& INT_EP1DATASET
) ? " ep1" : "",
1131 (mask
& INT_STATUSNAK
) ? " ep0snak" : "",
1132 (mask
& INT_STATUS
) ? " ep0status" : "",
1134 (mask
& INT_SETUP
) ? " setup" : "",
1135 (mask
& INT_ENDPOINT0
) ? " ep0" : "",
1136 (mask
& INT_USBRESET
) ? " reset" : "",
1137 (mask
& INT_SUSPEND
) ? " suspend" : "");
1144 udc_proc_read(char *buffer
, char **start
, off_t off
, int count
,
1145 int *eof
, void *_dev
)
1148 struct goku_udc
*dev
= _dev
;
1149 struct goku_udc_regs __iomem
*regs
= dev
->regs
;
1151 unsigned size
= count
;
1152 unsigned long flags
;
1153 int i
, t
, is_usb_connected
;
1159 local_irq_save(flags
);
1161 /* basic device status */
1162 tmp
= readl(®s
->power_detect
);
1163 is_usb_connected
= tmp
& PW_DETECT
;
1164 t
= scnprintf(next
, size
,
1166 "%s version: %s %s\n"
1167 "Gadget driver: %s\n"
1170 pci_name(dev
->pdev
), driver_desc
,
1171 driver_name
, DRIVER_VERSION
, dmastr(),
1172 dev
->driver
? dev
->driver
->driver
.name
: "(none)",
1174 ? ((tmp
& PW_PULLUP
) ? "full speed" : "powered")
1177 switch(dev
->ep0state
){
1178 case EP0_DISCONNECT
: tmp
= "ep0_disconnect"; break;
1179 case EP0_IDLE
: tmp
= "ep0_idle"; break;
1180 case EP0_IN
: tmp
= "ep0_in"; break;
1181 case EP0_OUT
: tmp
= "ep0_out"; break;
1182 case EP0_STATUS
: tmp
= "ep0_status"; break;
1183 case EP0_STALL
: tmp
= "ep0_stall"; break;
1184 case EP0_SUSPEND
: tmp
= "ep0_suspend"; break;
1185 default: tmp
= "ep0_?"; break;
1191 dump_intmask("int_status", readl(®s
->int_status
), &next
, &size
);
1192 dump_intmask("int_enable", readl(®s
->int_enable
), &next
, &size
);
1194 if (!is_usb_connected
|| !dev
->driver
|| (tmp
& PW_PULLUP
) == 0)
1197 /* registers for (active) device and ep0 */
1198 t
= scnprintf(next
, size
, "\nirqs %lu\ndataset %02x "
1199 "single.bcs %02x.%02x state %x addr %u\n",
1200 dev
->irqs
, readl(®s
->DataSet
),
1201 readl(®s
->EPxSingle
), readl(®s
->EPxBCS
),
1202 readl(®s
->UsbState
),
1203 readl(®s
->address
));
1207 tmp
= readl(®s
->dma_master
);
1208 t
= scnprintf(next
, size
,
1209 "dma %03X =" EIGHTBITS
"%s %s\n", tmp
,
1210 (tmp
& MST_EOPB_DIS
) ? " eopb-" : "",
1211 (tmp
& MST_EOPB_ENA
) ? " eopb+" : "",
1212 (tmp
& MST_TIMEOUT_DIS
) ? " tmo-" : "",
1213 (tmp
& MST_TIMEOUT_ENA
) ? " tmo+" : "",
1215 (tmp
& MST_RD_EOPB
) ? " eopb" : "",
1216 (tmp
& MST_RD_RESET
) ? " in_reset" : "",
1217 (tmp
& MST_WR_RESET
) ? " out_reset" : "",
1218 (tmp
& MST_RD_ENA
) ? " IN" : "",
1220 (tmp
& MST_WR_ENA
) ? " OUT" : "",
1221 (tmp
& MST_CONNECTION
)
1227 /* dump endpoint queues */
1228 for (i
= 0; i
< 4; i
++) {
1229 struct goku_ep
*ep
= &dev
->ep
[i
];
1230 struct goku_request
*req
;
1236 tmp
= readl(ep
->reg_status
);
1237 t
= scnprintf(next
, size
,
1238 "%s %s max %u %s, irqs %lu, "
1239 "status %02x (%s) " FOURBITS
"\n",
1241 ep
->is_in
? "in" : "out",
1243 ep
->dma
? "dma" : "pio",
1246 switch (tmp
& EPxSTATUS_EP_MASK
) {
1247 case EPxSTATUS_EP_READY
:
1249 case EPxSTATUS_EP_DATAIN
:
1250 s
= "packet"; break;
1251 case EPxSTATUS_EP_FULL
:
1253 case EPxSTATUS_EP_TX_ERR
: // host will retry
1254 s
= "tx_err"; break;
1255 case EPxSTATUS_EP_RX_ERR
:
1256 s
= "rx_err"; break;
1257 case EPxSTATUS_EP_BUSY
: /* ep0 only */
1259 case EPxSTATUS_EP_STALL
:
1261 case EPxSTATUS_EP_INVALID
: // these "can't happen"
1262 s
= "invalid"; break;
1266 (tmp
& EPxSTATUS_TOGGLE
) ? "data1" : "data0",
1267 (tmp
& EPxSTATUS_SUSPEND
) ? " suspend" : "",
1268 (tmp
& EPxSTATUS_FIFO_DISABLE
) ? " disable" : "",
1269 (tmp
& EPxSTATUS_STAGE_ERROR
) ? " ep0stat" : ""
1271 if (t
<= 0 || t
> size
)
1276 if (list_empty(&ep
->queue
)) {
1277 t
= scnprintf(next
, size
, "\t(nothing queued)\n");
1278 if (t
<= 0 || t
> size
)
1284 list_for_each_entry(req
, &ep
->queue
, queue
) {
1285 if (ep
->dma
&& req
->queue
.prev
== &ep
->queue
) {
1286 if (i
== UDC_MSTRD_ENDPOINT
)
1287 tmp
= readl(®s
->in_dma_current
);
1289 tmp
= readl(®s
->out_dma_current
);
1290 tmp
-= req
->req
.dma
;
1293 tmp
= req
->req
.actual
;
1295 t
= scnprintf(next
, size
,
1296 "\treq %p len %u/%u buf %p\n",
1297 &req
->req
, tmp
, req
->req
.length
,
1299 if (t
<= 0 || t
> size
)
1307 local_irq_restore(flags
);
1309 return count
- size
;
1312 #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
1314 /*-------------------------------------------------------------------------*/
1316 static void udc_reinit (struct goku_udc
*dev
)
1318 static char *names
[] = { "ep0", "ep1-bulk", "ep2-bulk", "ep3-bulk" };
1322 INIT_LIST_HEAD (&dev
->gadget
.ep_list
);
1323 dev
->gadget
.ep0
= &dev
->ep
[0].ep
;
1324 dev
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1325 dev
->ep0state
= EP0_DISCONNECT
;
1328 for (i
= 0; i
< 4; i
++) {
1329 struct goku_ep
*ep
= &dev
->ep
[i
];
1332 ep
->ep
.name
= names
[i
];
1333 ep
->reg_fifo
= &dev
->regs
->ep_fifo
[i
];
1334 ep
->reg_status
= &dev
->regs
->ep_status
[i
];
1335 ep
->reg_mode
= &dev
->regs
->ep_mode
[i
];
1337 ep
->ep
.ops
= &goku_ep_ops
;
1338 list_add_tail (&ep
->ep
.ep_list
, &dev
->gadget
.ep_list
);
1340 INIT_LIST_HEAD (&ep
->queue
);
1345 dev
->ep
[0].reg_mode
= NULL
;
1346 dev
->ep
[0].ep
.maxpacket
= MAX_EP0_SIZE
;
1347 list_del_init (&dev
->ep
[0].ep
.ep_list
);
1350 static void udc_reset(struct goku_udc
*dev
)
1352 struct goku_udc_regs __iomem
*regs
= dev
->regs
;
1354 writel(0, ®s
->power_detect
);
1355 writel(0, ®s
->int_enable
);
1356 readl(®s
->int_enable
);
1357 dev
->int_enable
= 0;
1359 /* deassert reset, leave USB D+ at hi-Z (no pullup)
1360 * don't let INT_PWRDETECT sequence begin
1363 writel(PW_RESETB
, ®s
->power_detect
);
1364 readl(®s
->int_enable
);
1367 static void ep0_start(struct goku_udc
*dev
)
1369 struct goku_udc_regs __iomem
*regs
= dev
->regs
;
1372 VDBG(dev
, "%s\n", __FUNCTION__
);
1376 //writel(MST_EOPB_ENA | MST_TIMEOUT_ENA, ®s->dma_master);
1378 /* hw handles set_address, set_feature, get_status; maybe more */
1379 writel( G_REQMODE_SET_INTF
| G_REQMODE_GET_INTF
1380 | G_REQMODE_SET_CONF
| G_REQMODE_GET_CONF
1381 | G_REQMODE_GET_DESC
1382 | G_REQMODE_CLEAR_FEAT
1385 for (i
= 0; i
< 4; i
++)
1386 dev
->ep
[i
].irqs
= 0;
1388 /* can't modify descriptors after writing UsbReady */
1389 for (i
= 0; i
< DESC_LEN
; i
++)
1390 writel(0, ®s
->descriptors
[i
]);
1391 writel(0, ®s
->UsbReady
);
1393 /* expect ep0 requests when the host drops reset */
1394 writel(PW_RESETB
| PW_PULLUP
, ®s
->power_detect
);
1395 dev
->int_enable
= INT_DEVWIDE
| INT_EP0
;
1396 writel(dev
->int_enable
, &dev
->regs
->int_enable
);
1397 readl(®s
->int_enable
);
1398 dev
->gadget
.speed
= USB_SPEED_FULL
;
1399 dev
->ep0state
= EP0_IDLE
;
1402 static void udc_enable(struct goku_udc
*dev
)
1404 /* start enumeration now, or after power detect irq */
1405 if (readl(&dev
->regs
->power_detect
) & PW_DETECT
)
1408 DBG(dev
, "%s\n", __FUNCTION__
);
1409 dev
->int_enable
= INT_PWRDETECT
;
1410 writel(dev
->int_enable
, &dev
->regs
->int_enable
);
1414 /*-------------------------------------------------------------------------*/
1416 /* keeping it simple:
1417 * - one bus driver, initted first;
1418 * - one function driver, initted second
1421 static struct goku_udc
*the_controller
;
1423 /* when a driver is successfully registered, it will receive
1424 * control requests including set_configuration(), which enables
1425 * non-control requests. then usb traffic follows until a
1426 * disconnect is reported. then a host may connect again, or
1427 * the driver might get unbound.
1429 int usb_gadget_register_driver(struct usb_gadget_driver
*driver
)
1431 struct goku_udc
*dev
= the_controller
;
1435 || driver
->speed
!= USB_SPEED_FULL
1438 || !driver
->disconnect
1446 /* hook up the driver */
1447 driver
->driver
.bus
= NULL
;
1448 dev
->driver
= driver
;
1449 dev
->gadget
.dev
.driver
= &driver
->driver
;
1450 retval
= driver
->bind(&dev
->gadget
);
1452 DBG(dev
, "bind to driver %s --> error %d\n",
1453 driver
->driver
.name
, retval
);
1455 dev
->gadget
.dev
.driver
= NULL
;
1459 /* then enable host detection and ep0; and we're ready
1460 * for set_configuration as well as eventual disconnect.
1464 DBG(dev
, "registered gadget driver '%s'\n", driver
->driver
.name
);
1467 EXPORT_SYMBOL(usb_gadget_register_driver
);
1470 stop_activity(struct goku_udc
*dev
, struct usb_gadget_driver
*driver
)
1474 DBG (dev
, "%s\n", __FUNCTION__
);
1476 if (dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)
1479 /* disconnect gadget driver after quiesceing hw and the driver */
1481 for (i
= 0; i
< 4; i
++)
1482 nuke(&dev
->ep
[i
], -ESHUTDOWN
);
1484 spin_unlock(&dev
->lock
);
1485 driver
->disconnect(&dev
->gadget
);
1486 spin_lock(&dev
->lock
);
1493 int usb_gadget_unregister_driver(struct usb_gadget_driver
*driver
)
1495 struct goku_udc
*dev
= the_controller
;
1496 unsigned long flags
;
1500 if (!driver
|| driver
!= dev
->driver
)
1503 spin_lock_irqsave(&dev
->lock
, flags
);
1505 stop_activity(dev
, driver
);
1506 spin_unlock_irqrestore(&dev
->lock
, flags
);
1508 driver
->unbind(&dev
->gadget
);
1510 DBG(dev
, "unregistered driver '%s'\n", driver
->driver
.name
);
1513 EXPORT_SYMBOL(usb_gadget_unregister_driver
);
1516 /*-------------------------------------------------------------------------*/
1518 static void ep0_setup(struct goku_udc
*dev
)
1520 struct goku_udc_regs __iomem
*regs
= dev
->regs
;
1521 struct usb_ctrlrequest ctrl
;
1524 /* read SETUP packet and enter DATA stage */
1525 ctrl
.bRequestType
= readl(®s
->bRequestType
);
1526 ctrl
.bRequest
= readl(®s
->bRequest
);
1527 ctrl
.wValue
= (readl(®s
->wValueH
) << 8) | readl(®s
->wValueL
);
1528 ctrl
.wIndex
= (readl(®s
->wIndexH
) << 8) | readl(®s
->wIndexL
);
1529 ctrl
.wLength
= (readl(®s
->wLengthH
) << 8) | 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
= ctrl
.wIndex
& 0x0f;
1552 /* active endpoint */
1553 if (tmp
> 3 || (!dev
->ep
[tmp
].desc
&& tmp
!= 0))
1555 if (ctrl
.wIndex
& USB_DIR_IN
) {
1556 if (!dev
->ep
[tmp
].is_in
)
1559 if (dev
->ep
[tmp
].is_in
)
1562 if (ctrl
.wValue
!= USB_ENDPOINT_HALT
)
1565 goku_clear_halt(&dev
->ep
[tmp
]);
1567 /* start ep0out status stage */
1568 writel(~(1<<0), ®s
->EOP
);
1569 dev
->ep
[0].stopped
= 1;
1570 dev
->ep0state
= EP0_STATUS
;
1572 case USB_RECIP_DEVICE
:
1573 /* device remote wakeup: always clear */
1574 if (ctrl
.wValue
!= 1)
1576 VDBG(dev
, "clear dev remote wakeup\n");
1578 case USB_RECIP_INTERFACE
:
1580 default: /* pass to gadget driver */
1590 VDBG(dev
, "SETUP %02x.%02x v%04x i%04x l%04x\n",
1591 ctrl
.bRequestType
, ctrl
.bRequest
,
1592 ctrl
.wValue
, ctrl
.wIndex
, ctrl
.wLength
);
1595 /* hw wants to know when we're configured (or not) */
1596 dev
->req_config
= (ctrl
.bRequest
== USB_REQ_SET_CONFIGURATION
1597 && ctrl
.bRequestType
== USB_RECIP_DEVICE
);
1598 if (unlikely(dev
->req_config
))
1599 dev
->configured
= (ctrl
.wValue
!= 0);
1601 /* delegate everything to the gadget driver.
1602 * it may respond after this irq handler returns.
1604 spin_unlock (&dev
->lock
);
1605 tmp
= dev
->driver
->setup(&dev
->gadget
, &ctrl
);
1606 spin_lock (&dev
->lock
);
1607 if (unlikely(tmp
< 0)) {
1610 VDBG(dev
, "req %02x.%02x protocol STALL; err %d\n",
1611 ctrl
.bRequestType
, ctrl
.bRequest
, tmp
);
1613 command(regs
, COMMAND_STALL
, 0);
1614 dev
->ep
[0].stopped
= 1;
1615 dev
->ep0state
= EP0_STALL
;
1618 /* expect at least one data or status stage irq */
1621 #define ACK(irqbit) { \
1623 writel(~irqbit, ®s->int_status); \
1627 static irqreturn_t
goku_irq(int irq
, void *_dev
, struct pt_regs
*r
)
1629 struct goku_udc
*dev
= _dev
;
1630 struct goku_udc_regs __iomem
*regs
= dev
->regs
;
1632 u32 stat
, handled
= 0;
1633 unsigned i
, rescans
= 5;
1635 spin_lock(&dev
->lock
);
1638 stat
= readl(®s
->int_status
) & dev
->int_enable
;
1643 /* device-wide irqs */
1644 if (unlikely(stat
& INT_DEVWIDE
)) {
1645 if (stat
& INT_SYSERROR
) {
1646 ERROR(dev
, "system error\n");
1647 stop_activity(dev
, dev
->driver
);
1650 // FIXME have a neater way to prevent re-enumeration
1654 if (stat
& INT_PWRDETECT
) {
1655 writel(~stat
, ®s
->int_status
);
1656 if (readl(&dev
->regs
->power_detect
) & PW_DETECT
) {
1657 VDBG(dev
, "connect\n");
1660 DBG(dev
, "disconnect\n");
1661 if (dev
->gadget
.speed
== USB_SPEED_FULL
)
1662 stop_activity(dev
, dev
->driver
);
1663 dev
->ep0state
= EP0_DISCONNECT
;
1664 dev
->int_enable
= INT_DEVWIDE
;
1665 writel(dev
->int_enable
, &dev
->regs
->int_enable
);
1671 if (stat
& INT_SUSPEND
) {
1673 if (readl(®s
->ep_status
[0]) & EPxSTATUS_SUSPEND
) {
1674 switch (dev
->ep0state
) {
1675 case EP0_DISCONNECT
:
1681 DBG(dev
, "USB suspend\n");
1682 dev
->ep0state
= EP0_SUSPEND
;
1683 if (dev
->gadget
.speed
!= USB_SPEED_UNKNOWN
1685 && dev
->driver
->suspend
) {
1686 spin_unlock(&dev
->lock
);
1687 dev
->driver
->suspend(&dev
->gadget
);
1688 spin_lock(&dev
->lock
);
1691 if (dev
->ep0state
!= EP0_SUSPEND
) {
1692 DBG(dev
, "bogus USB resume %d\n",
1696 DBG(dev
, "USB resume\n");
1697 dev
->ep0state
= EP0_IDLE
;
1698 if (dev
->gadget
.speed
!= USB_SPEED_UNKNOWN
1700 && dev
->driver
->resume
) {
1701 spin_unlock(&dev
->lock
);
1702 dev
->driver
->resume(&dev
->gadget
);
1703 spin_lock(&dev
->lock
);
1708 if (stat
& INT_USBRESET
) { /* hub reset done */
1710 INFO(dev
, "USB reset done, gadget %s\n",
1711 dev
->driver
->driver
.name
);
1713 // and INT_ERR on some endpoint's crc/bitstuff/... problem
1716 /* progress ep0 setup, data, or status stages.
1717 * no transition {EP0_STATUS, EP0_STALL} --> EP0_IDLE; saves irqs
1719 if (stat
& INT_SETUP
) {
1724 if (stat
& INT_STATUSNAK
) {
1725 ACK(INT_STATUSNAK
|INT_ENDPOINT0
);
1726 if (dev
->ep0state
== EP0_IN
) {
1730 writel(~(1<<0), ®s
->EOP
);
1731 dev
->ep0state
= EP0_STATUS
;
1734 if (stat
& INT_ENDPOINT0
) {
1741 /* dma completion */
1742 if (stat
& INT_MSTRDEND
) { /* IN */
1744 ep
= &dev
->ep
[UDC_MSTRD_ENDPOINT
];
1746 dma_advance(dev
, ep
);
1748 if (stat
& INT_MSTWREND
) { /* OUT */
1750 ep
= &dev
->ep
[UDC_MSTWR_ENDPOINT
];
1752 dma_advance(dev
, ep
);
1754 if (stat
& INT_MSTWRTMOUT
) { /* OUT */
1755 ACK(INT_MSTWRTMOUT
);
1756 ep
= &dev
->ep
[UDC_MSTWR_ENDPOINT
];
1758 ERROR(dev
, "%s write timeout ?\n", ep
->ep
.name
);
1759 // reset dma? then dma_advance()
1763 for (i
= 1; i
< 4; i
++) {
1764 u32 tmp
= INT_EPxDATASET(i
);
1770 if (list_empty (&ep
->queue
))
1771 pio_irq_disable(dev
, regs
, i
);
1781 (void)readl(®s
->int_enable
);
1782 spin_unlock(&dev
->lock
);
1784 DBG(dev
, "unhandled irq status: %05x (%05x, %05x)\n", stat
,
1785 readl(®s
->int_status
), dev
->int_enable
);
1786 return IRQ_RETVAL(handled
);
1791 /*-------------------------------------------------------------------------*/
1793 static void gadget_release(struct device
*_dev
)
1795 struct goku_udc
*dev
= dev_get_drvdata(_dev
);
1800 /* tear down the binding between this driver and the pci device */
1802 static void goku_remove(struct pci_dev
*pdev
)
1804 struct goku_udc
*dev
= pci_get_drvdata(pdev
);
1806 DBG(dev
, "%s\n", __FUNCTION__
);
1807 /* start with the driver above us */
1809 /* should have been done already by driver model core */
1810 WARN(dev
, "pci remove, driver '%s' is still registered\n",
1811 dev
->driver
->driver
.name
);
1812 usb_gadget_unregister_driver(dev
->driver
);
1815 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1816 remove_proc_entry(proc_node_name
, NULL
);
1821 free_irq(pdev
->irq
, dev
);
1824 if (dev
->got_region
)
1825 release_mem_region(pci_resource_start (pdev
, 0),
1826 pci_resource_len (pdev
, 0));
1828 pci_disable_device(pdev
);
1829 device_unregister(&dev
->gadget
.dev
);
1831 pci_set_drvdata(pdev
, NULL
);
1833 the_controller
= NULL
;
1835 INFO(dev
, "unbind\n");
1838 /* wrap this driver around the specified pci device, but
1839 * don't respond over USB until a gadget driver binds to us.
1842 static int goku_probe(struct pci_dev
*pdev
, const struct pci_device_id
*id
)
1844 struct goku_udc
*dev
= NULL
;
1845 unsigned long resource
, len
;
1846 void __iomem
*base
= NULL
;
1848 char buf
[8], *bufp
;
1850 /* if you want to support more than one controller in a system,
1851 * usb_gadget_driver_{register,unregister}() must change.
1853 if (the_controller
) {
1854 WARN(dev
, "ignoring %s\n", pci_name(pdev
));
1858 printk(KERN_ERR
"Check PCI %s IRQ setup!\n", pci_name(pdev
));
1863 /* alloc, and start init */
1864 dev
= kmalloc (sizeof *dev
, SLAB_KERNEL
);
1866 pr_debug("enomem %s\n", pci_name(pdev
));
1871 memset(dev
, 0, sizeof *dev
);
1872 spin_lock_init(&dev
->lock
);
1874 dev
->gadget
.ops
= &goku_ops
;
1876 /* the "gadget" abstracts/virtualizes the controller */
1877 strcpy(dev
->gadget
.dev
.bus_id
, "gadget");
1878 dev
->gadget
.dev
.parent
= &pdev
->dev
;
1879 dev
->gadget
.dev
.dma_mask
= pdev
->dev
.dma_mask
;
1880 dev
->gadget
.dev
.release
= gadget_release
;
1881 dev
->gadget
.name
= driver_name
;
1883 /* now all the pci goodies ... */
1884 retval
= pci_enable_device(pdev
);
1886 DBG(dev
, "can't enable, %d\n", retval
);
1891 resource
= pci_resource_start(pdev
, 0);
1892 len
= pci_resource_len(pdev
, 0);
1893 if (!request_mem_region(resource
, len
, driver_name
)) {
1894 DBG(dev
, "controller already in use\n");
1898 dev
->got_region
= 1;
1900 base
= ioremap_nocache(resource
, len
);
1902 DBG(dev
, "can't map memory\n");
1906 dev
->regs
= (struct goku_udc_regs __iomem
*) base
;
1908 pci_set_drvdata(pdev
, dev
);
1909 INFO(dev
, "%s\n", driver_desc
);
1910 INFO(dev
, "version: " DRIVER_VERSION
" %s\n", dmastr());
1912 scnprintf(buf
, sizeof buf
, "%d", pdev
->irq
);
1915 bufp
= __irq_itoa(pdev
->irq
);
1917 INFO(dev
, "irq %s, pci mem %p\n", bufp
, base
);
1919 /* init to known state, then setup irqs */
1922 if (request_irq(pdev
->irq
, goku_irq
, SA_SHIRQ
/*|SA_SAMPLE_RANDOM*/,
1923 driver_name
, dev
) != 0) {
1924 DBG(dev
, "request interrupt %s failed\n", bufp
);
1930 pci_set_master(pdev
);
1933 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1934 create_proc_read_entry(proc_node_name
, 0, NULL
, udc_proc_read
, dev
);
1938 the_controller
= dev
;
1939 device_register(&dev
->gadget
.dev
);
1950 /*-------------------------------------------------------------------------*/
1952 static struct pci_device_id pci_ids
[] = { {
1953 .class = ((PCI_CLASS_SERIAL_USB
<< 8) | 0xfe),
1955 .vendor
= 0x102f, /* Toshiba */
1956 .device
= 0x0107, /* this UDC */
1957 .subvendor
= PCI_ANY_ID
,
1958 .subdevice
= PCI_ANY_ID
,
1960 }, { /* end: all zeroes */ }
1962 MODULE_DEVICE_TABLE (pci
, pci_ids
);
1964 static struct pci_driver goku_pci_driver
= {
1965 .name
= (char *) driver_name
,
1966 .id_table
= pci_ids
,
1968 .probe
= goku_probe
,
1969 .remove
= goku_remove
,
1971 /* FIXME add power management support */
1974 static int __init
init (void)
1976 return pci_register_driver (&goku_pci_driver
);
1980 static void __exit
cleanup (void)
1982 pci_unregister_driver (&goku_pci_driver
);
1984 module_exit (cleanup
);