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 be 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
, gfp_t gfp_flags
)
274 struct goku_request
*req
;
278 req
= kzalloc(sizeof *req
, gfp_flags
);
282 req
->req
.dma
= DMA_ADDR_INVALID
;
283 INIT_LIST_HEAD(&req
->queue
);
288 goku_free_request(struct usb_ep
*_ep
, struct usb_request
*_req
)
290 struct goku_request
*req
;
295 req
= container_of(_req
, struct goku_request
, req
);
296 WARN_ON(!list_empty(&req
->queue
));
300 /*-------------------------------------------------------------------------*/
304 /* many common platforms have dma-coherent caches, which means that it's
305 * safe to use kmalloc() memory for all i/o buffers without using any
306 * cache flushing calls. (unless you're trying to share cache lines
307 * between dma and non-dma activities, which is a slow idea in any case.)
309 * other platforms need more care, with 2.6 having a moderately general
310 * solution except for the common "buffer is smaller than a page" case.
312 #if defined(CONFIG_X86)
315 #elif defined(CONFIG_MIPS) && !defined(CONFIG_DMA_NONCOHERENT)
318 #elif defined(CONFIG_PPC) && !defined(CONFIG_NOT_COHERENT_CACHE)
323 /* allocating buffers this way eliminates dma mapping overhead, which
324 * on some platforms will mean eliminating a per-io buffer copy. with
325 * some kinds of system caches, further tweaks may still be needed.
328 goku_alloc_buffer(struct usb_ep
*_ep
, unsigned bytes
,
329 dma_addr_t
*dma
, gfp_t gfp_flags
)
334 ep
= container_of(_ep
, struct goku_ep
, ep
);
337 *dma
= DMA_ADDR_INVALID
;
339 #if defined(USE_KMALLOC)
340 retval
= kmalloc(bytes
, gfp_flags
);
342 *dma
= virt_to_phys(retval
);
345 /* the main problem with this call is that it wastes memory
346 * on typical 1/N page allocations: it allocates 1-N pages.
348 #warning Using dma_alloc_coherent even with buffers smaller than a page.
349 retval
= dma_alloc_coherent(&ep
->dev
->pdev
->dev
,
350 bytes
, dma
, gfp_flags
);
352 retval
= kmalloc(bytes
, gfp_flags
);
358 goku_free_buffer(struct usb_ep
*_ep
, void *buf
, dma_addr_t dma
, unsigned bytes
)
360 /* free memory into the right allocator */
362 if (dma
!= DMA_ADDR_INVALID
) {
365 ep
= container_of(_ep
, struct goku_ep
, ep
);
368 dma_free_coherent(&ep
->dev
->pdev
->dev
, bytes
, buf
, dma
);
374 /*-------------------------------------------------------------------------*/
377 done(struct goku_ep
*ep
, struct goku_request
*req
, int status
)
379 struct goku_udc
*dev
;
380 unsigned stopped
= ep
->stopped
;
382 list_del_init(&req
->queue
);
384 if (likely(req
->req
.status
== -EINPROGRESS
))
385 req
->req
.status
= status
;
387 status
= req
->req
.status
;
391 pci_unmap_single(dev
->pdev
, req
->req
.dma
, req
->req
.length
,
392 ep
->is_in
? PCI_DMA_TODEVICE
: PCI_DMA_FROMDEVICE
);
393 req
->req
.dma
= DMA_ADDR_INVALID
;
398 if (status
&& status
!= -ESHUTDOWN
)
400 VDBG(dev
, "complete %s req %p stat %d len %u/%u\n",
401 ep
->ep
.name
, &req
->req
, status
,
402 req
->req
.actual
, req
->req
.length
);
404 /* don't modify queue heads during completion callback */
406 spin_unlock(&dev
->lock
);
407 req
->req
.complete(&ep
->ep
, &req
->req
);
408 spin_lock(&dev
->lock
);
409 ep
->stopped
= stopped
;
412 /*-------------------------------------------------------------------------*/
415 write_packet(u32 __iomem
*fifo
, u8
*buf
, struct goku_request
*req
, unsigned max
)
417 unsigned length
, count
;
419 length
= min(req
->req
.length
- req
->req
.actual
, max
);
420 req
->req
.actual
+= length
;
423 while (likely(count
--))
424 writel(*buf
++, fifo
);
428 // return: 0 = still running, 1 = completed, negative = errno
429 static int write_fifo(struct goku_ep
*ep
, struct goku_request
*req
)
431 struct goku_udc
*dev
= ep
->dev
;
437 tmp
= readl(&dev
->regs
->DataSet
);
438 buf
= req
->req
.buf
+ req
->req
.actual
;
442 if (unlikely(ep
->num
== 0 && dev
->ep0state
!= EP0_IN
))
445 /* NOTE: just single-buffered PIO-IN for now. */
446 if (unlikely((tmp
& DATASET_A(ep
->num
)) != 0))
449 /* clear our "packet available" irq */
451 writel(~INT_EPxDATASET(ep
->num
), &dev
->regs
->int_status
);
453 count
= write_packet(ep
->reg_fifo
, buf
, req
, ep
->ep
.maxpacket
);
455 /* last packet often short (sometimes a zlp, especially on ep0) */
456 if (unlikely(count
!= ep
->ep
.maxpacket
)) {
457 writel(~(1<<ep
->num
), &dev
->regs
->EOP
);
459 dev
->ep
[0].stopped
= 1;
460 dev
->ep0state
= EP0_STATUS
;
464 if (likely(req
->req
.length
!= req
->req
.actual
)
470 #if 0 /* printk seemed to trash is_last...*/
472 VDBG(dev
, "wrote %s %u bytes%s IN %u left %p\n",
473 ep
->ep
.name
, count
, is_last
? "/last" : "",
474 req
->req
.length
- req
->req
.actual
, req
);
477 /* requests complete when all IN data is in the FIFO,
478 * or sometimes later, if a zlp was needed.
488 static int read_fifo(struct goku_ep
*ep
, struct goku_request
*req
)
490 struct goku_udc_regs __iomem
*regs
;
493 unsigned bufferspace
, is_short
, dbuff
;
495 regs
= ep
->dev
->regs
;
497 buf
= req
->req
.buf
+ req
->req
.actual
;
500 if (unlikely(ep
->num
== 0 && ep
->dev
->ep0state
!= EP0_OUT
))
503 dbuff
= (ep
->num
== 1 || ep
->num
== 2);
505 /* ack dataset irq matching the status we'll handle */
507 writel(~INT_EPxDATASET(ep
->num
), ®s
->int_status
);
509 set
= readl(®s
->DataSet
) & DATASET_AB(ep
->num
);
510 size
= readl(®s
->EPxSizeLA
[ep
->num
]);
511 bufferspace
= req
->req
.length
- req
->req
.actual
;
513 /* usually do nothing without an OUT packet */
514 if (likely(ep
->num
!= 0 || bufferspace
!= 0)) {
515 if (unlikely(set
== 0))
517 /* use ep1/ep2 double-buffering for OUT */
518 if (!(size
& PACKET_ACTIVE
))
519 size
= readl(®s
->EPxSizeLB
[ep
->num
]);
520 if (!(size
& PACKET_ACTIVE
)) // "can't happen"
522 size
&= DATASIZE
; /* EPxSizeH == 0 */
524 /* ep0out no-out-data case for set_config, etc */
528 /* read all bytes from this packet */
529 req
->req
.actual
+= size
;
530 is_short
= (size
< ep
->ep
.maxpacket
);
532 VDBG(ep
->dev
, "read %s %u bytes%s OUT req %p %u/%u\n",
533 ep
->ep
.name
, size
, is_short
? "/S" : "",
534 req
, req
->req
.actual
, req
->req
.length
);
536 while (likely(size
-- != 0)) {
537 u8 byte
= (u8
) readl(ep
->reg_fifo
);
539 if (unlikely(bufferspace
== 0)) {
540 /* this happens when the driver's buffer
541 * is smaller than what the host sent.
542 * discard the extra data in this packet.
544 if (req
->req
.status
!= -EOVERFLOW
)
545 DBG(ep
->dev
, "%s overflow %u\n",
547 req
->req
.status
= -EOVERFLOW
;
555 if (unlikely(is_short
|| req
->req
.actual
== req
->req
.length
)) {
556 if (unlikely(ep
->num
== 0)) {
557 /* non-control endpoints now usable? */
558 if (ep
->dev
->req_config
)
559 writel(ep
->dev
->configured
560 ? USBSTATE_CONFIGURED
563 /* ep0out status stage */
564 writel(~(1<<0), ®s
->EOP
);
566 ep
->dev
->ep0state
= EP0_STATUS
;
570 /* empty the second buffer asap */
571 if (dbuff
&& !list_empty(&ep
->queue
)) {
572 req
= list_entry(ep
->queue
.next
,
573 struct goku_request
, queue
);
583 pio_irq_enable(struct goku_udc
*dev
,
584 struct goku_udc_regs __iomem
*regs
, int epnum
)
586 dev
->int_enable
|= INT_EPxDATASET (epnum
);
587 writel(dev
->int_enable
, ®s
->int_enable
);
588 /* write may still be posted */
592 pio_irq_disable(struct goku_udc
*dev
,
593 struct goku_udc_regs __iomem
*regs
, int epnum
)
595 dev
->int_enable
&= ~INT_EPxDATASET (epnum
);
596 writel(dev
->int_enable
, ®s
->int_enable
);
597 /* write may still be posted */
601 pio_advance(struct goku_ep
*ep
)
603 struct goku_request
*req
;
605 if (unlikely(list_empty (&ep
->queue
)))
607 req
= list_entry(ep
->queue
.next
, struct goku_request
, queue
);
608 (ep
->is_in
? write_fifo
: read_fifo
)(ep
, req
);
612 /*-------------------------------------------------------------------------*/
614 // return: 0 = q running, 1 = q stopped, negative = errno
615 static int start_dma(struct goku_ep
*ep
, struct goku_request
*req
)
617 struct goku_udc_regs __iomem
*regs
= ep
->dev
->regs
;
619 u32 start
= req
->req
.dma
;
620 u32 end
= start
+ req
->req
.length
- 1;
622 master
= readl(®s
->dma_master
) & MST_RW_BITS
;
624 /* re-init the bits affecting IN dma; careful with zlps */
625 if (likely(ep
->is_in
)) {
626 if (unlikely(master
& MST_RD_ENA
)) {
627 DBG (ep
->dev
, "start, IN active dma %03x!!\n",
631 writel(end
, ®s
->in_dma_end
);
632 writel(start
, ®s
->in_dma_start
);
634 master
&= ~MST_R_BITS
;
635 if (unlikely(req
->req
.length
== 0))
636 master
= MST_RD_ENA
| MST_RD_EOPB
;
637 else if ((req
->req
.length
% ep
->ep
.maxpacket
) != 0
639 master
= MST_RD_ENA
| MST_EOPB_ENA
;
641 master
= MST_RD_ENA
| MST_EOPB_DIS
;
643 ep
->dev
->int_enable
|= INT_MSTRDEND
;
645 /* Goku DMA-OUT merges short packets, which plays poorly with
646 * protocols where short packets mark the transfer boundaries.
647 * The chip supports a nonstandard policy with INT_MSTWRTMOUT,
648 * ending transfers after 3 SOFs; we don't turn it on.
651 if (unlikely(master
& MST_WR_ENA
)) {
652 DBG (ep
->dev
, "start, OUT active dma %03x!!\n",
656 writel(end
, ®s
->out_dma_end
);
657 writel(start
, ®s
->out_dma_start
);
659 master
&= ~MST_W_BITS
;
660 master
|= MST_WR_ENA
| MST_TIMEOUT_DIS
;
662 ep
->dev
->int_enable
|= INT_MSTWREND
|INT_MSTWRTMOUT
;
665 writel(master
, ®s
->dma_master
);
666 writel(ep
->dev
->int_enable
, ®s
->int_enable
);
670 static void dma_advance(struct goku_udc
*dev
, struct goku_ep
*ep
)
672 struct goku_request
*req
;
673 struct goku_udc_regs __iomem
*regs
= ep
->dev
->regs
;
676 master
= readl(®s
->dma_master
);
678 if (unlikely(list_empty(&ep
->queue
))) {
681 dev
->int_enable
&= ~INT_MSTRDEND
;
683 dev
->int_enable
&= ~(INT_MSTWREND
|INT_MSTWRTMOUT
);
684 writel(dev
->int_enable
, ®s
->int_enable
);
687 req
= list_entry(ep
->queue
.next
, struct goku_request
, queue
);
689 /* normal hw dma completion (not abort) */
690 if (likely(ep
->is_in
)) {
691 if (unlikely(master
& MST_RD_ENA
))
693 req
->req
.actual
= readl(®s
->in_dma_current
);
695 if (unlikely(master
& MST_WR_ENA
))
698 /* hardware merges short packets, and also hides packet
699 * overruns. a partial packet MAY be in the fifo here.
701 req
->req
.actual
= readl(®s
->out_dma_current
);
703 req
->req
.actual
-= req
->req
.dma
;
707 VDBG(dev
, "done %s %s dma, %u/%u bytes, req %p\n",
708 ep
->ep
.name
, ep
->is_in
? "IN" : "OUT",
709 req
->req
.actual
, req
->req
.length
, req
);
712 if (list_empty(&ep
->queue
))
714 req
= list_entry(ep
->queue
.next
, struct goku_request
, queue
);
715 (void) start_dma(ep
, req
);
718 static void abort_dma(struct goku_ep
*ep
, int status
)
720 struct goku_udc_regs __iomem
*regs
= ep
->dev
->regs
;
721 struct goku_request
*req
;
724 /* NAK future host requests, hoping the implicit delay lets the
725 * dma engine finish reading (or writing) its latest packet and
726 * empty the dma buffer (up to 16 bytes).
728 * This avoids needing to clean up a partial packet in the fifo;
729 * we can't do that for IN without side effects to HALT and TOGGLE.
731 command(regs
, COMMAND_FIFO_DISABLE
, ep
->num
);
732 req
= list_entry(ep
->queue
.next
, struct goku_request
, queue
);
733 master
= readl(®s
->dma_master
) & MST_RW_BITS
;
735 /* FIXME using these resets isn't usably documented. this may
736 * not work unless it's followed by disabling the endpoint.
738 * FIXME the OUT reset path doesn't even behave consistently.
741 if (unlikely((readl(®s
->dma_master
) & MST_RD_ENA
) == 0))
743 curr
= readl(®s
->in_dma_current
);
745 writel(curr
, ®s
->in_dma_end
);
746 writel(curr
, ®s
->in_dma_start
);
748 master
&= ~MST_R_BITS
;
749 master
|= MST_RD_RESET
;
750 writel(master
, ®s
->dma_master
);
752 if (readl(®s
->dma_master
) & MST_RD_ENA
)
753 DBG(ep
->dev
, "IN dma active after reset!\n");
756 if (unlikely((readl(®s
->dma_master
) & MST_WR_ENA
) == 0))
758 curr
= readl(®s
->out_dma_current
);
760 writel(curr
, ®s
->out_dma_end
);
761 writel(curr
, ®s
->out_dma_start
);
763 master
&= ~MST_W_BITS
;
764 master
|= MST_WR_RESET
;
765 writel(master
, ®s
->dma_master
);
767 if (readl(®s
->dma_master
) & MST_WR_ENA
)
768 DBG(ep
->dev
, "OUT dma active after reset!\n");
770 req
->req
.actual
= (curr
- req
->req
.dma
) + 1;
771 req
->req
.status
= status
;
773 VDBG(ep
->dev
, "%s %s %s %d/%d\n", __FUNCTION__
, ep
->ep
.name
,
774 ep
->is_in
? "IN" : "OUT",
775 req
->req
.actual
, req
->req
.length
);
777 command(regs
, COMMAND_FIFO_ENABLE
, ep
->num
);
782 /* dma already completed; no abort needed */
783 command(regs
, COMMAND_FIFO_ENABLE
, ep
->num
);
784 req
->req
.actual
= req
->req
.length
;
788 /*-------------------------------------------------------------------------*/
791 goku_queue(struct usb_ep
*_ep
, struct usb_request
*_req
, gfp_t gfp_flags
)
793 struct goku_request
*req
;
795 struct goku_udc
*dev
;
799 /* always require a cpu-view buffer so pio works */
800 req
= container_of(_req
, struct goku_request
, req
);
801 if (unlikely(!_req
|| !_req
->complete
802 || !_req
->buf
|| !list_empty(&req
->queue
)))
804 ep
= container_of(_ep
, struct goku_ep
, ep
);
805 if (unlikely(!_ep
|| (!ep
->desc
&& ep
->num
!= 0)))
808 if (unlikely(!dev
->driver
|| dev
->gadget
.speed
== USB_SPEED_UNKNOWN
))
811 /* can't touch registers when suspended */
812 if (dev
->ep0state
== EP0_SUSPEND
)
815 /* set up dma mapping in case the caller didn't */
816 if (ep
->dma
&& _req
->dma
== DMA_ADDR_INVALID
) {
817 _req
->dma
= pci_map_single(dev
->pdev
, _req
->buf
, _req
->length
,
818 ep
->is_in
? PCI_DMA_TODEVICE
: PCI_DMA_FROMDEVICE
);
823 VDBG(dev
, "%s queue req %p, len %u buf %p\n",
824 _ep
->name
, _req
, _req
->length
, _req
->buf
);
827 spin_lock_irqsave(&dev
->lock
, flags
);
829 _req
->status
= -EINPROGRESS
;
832 /* for ep0 IN without premature status, zlp is required and
833 * writing EOP starts the status stage (OUT).
835 if (unlikely(ep
->num
== 0 && ep
->is_in
))
838 /* kickstart this i/o queue? */
840 if (list_empty(&ep
->queue
) && likely(!ep
->stopped
)) {
841 /* dma: done after dma completion IRQ (or error)
842 * pio: done after last fifo operation
845 status
= start_dma(ep
, req
);
847 status
= (ep
->is_in
? write_fifo
: read_fifo
)(ep
, req
);
849 if (unlikely(status
!= 0)) {
855 } /* else pio or dma irq handler advances the queue. */
857 if (likely(req
!= 0))
858 list_add_tail(&req
->queue
, &ep
->queue
);
860 if (likely(!list_empty(&ep
->queue
))
861 && likely(ep
->num
!= 0)
863 && !(dev
->int_enable
& INT_EPxDATASET (ep
->num
)))
864 pio_irq_enable(dev
, dev
->regs
, ep
->num
);
866 spin_unlock_irqrestore(&dev
->lock
, flags
);
868 /* pci writes may still be posted */
872 /* dequeue ALL requests */
873 static void nuke(struct goku_ep
*ep
, int status
)
875 struct goku_request
*req
;
878 if (list_empty(&ep
->queue
))
881 abort_dma(ep
, status
);
882 while (!list_empty(&ep
->queue
)) {
883 req
= list_entry(ep
->queue
.next
, struct goku_request
, queue
);
884 done(ep
, req
, status
);
888 /* dequeue JUST ONE request */
889 static int goku_dequeue(struct usb_ep
*_ep
, struct usb_request
*_req
)
891 struct goku_request
*req
;
893 struct goku_udc
*dev
;
896 ep
= container_of(_ep
, struct goku_ep
, ep
);
897 if (!_ep
|| !_req
|| (!ep
->desc
&& ep
->num
!= 0))
903 /* we can't touch (dma) registers when suspended */
904 if (dev
->ep0state
== EP0_SUSPEND
)
907 VDBG(dev
, "%s %s %s %s %p\n", __FUNCTION__
, _ep
->name
,
908 ep
->is_in
? "IN" : "OUT",
909 ep
->dma
? "dma" : "pio",
912 spin_lock_irqsave(&dev
->lock
, flags
);
914 /* make sure it's actually queued on this endpoint */
915 list_for_each_entry (req
, &ep
->queue
, queue
) {
916 if (&req
->req
== _req
)
919 if (&req
->req
!= _req
) {
920 spin_unlock_irqrestore (&dev
->lock
, flags
);
924 if (ep
->dma
&& ep
->queue
.next
== &req
->queue
&& !ep
->stopped
) {
925 abort_dma(ep
, -ECONNRESET
);
926 done(ep
, req
, -ECONNRESET
);
927 dma_advance(dev
, ep
);
928 } else if (!list_empty(&req
->queue
))
929 done(ep
, req
, -ECONNRESET
);
932 spin_unlock_irqrestore(&dev
->lock
, flags
);
934 return req
? 0 : -EOPNOTSUPP
;
937 /*-------------------------------------------------------------------------*/
939 static void goku_clear_halt(struct goku_ep
*ep
)
941 // assert (ep->num !=0)
942 VDBG(ep
->dev
, "%s clear halt\n", ep
->ep
.name
);
943 command(ep
->dev
->regs
, COMMAND_SETDATA0
, ep
->num
);
944 command(ep
->dev
->regs
, COMMAND_STALL_CLEAR
, ep
->num
);
948 struct goku_request
*req
;
950 if (list_empty(&ep
->queue
))
952 req
= list_entry(ep
->queue
.next
, struct goku_request
,
954 (void) start_dma(ep
, req
);
960 static int goku_set_halt(struct usb_ep
*_ep
, int value
)
968 ep
= container_of (_ep
, struct goku_ep
, ep
);
972 ep
->dev
->ep0state
= EP0_STALL
;
973 ep
->dev
->ep
[0].stopped
= 1;
977 /* don't change EPxSTATUS_EP_INVALID to READY */
978 } else if (!ep
->desc
) {
979 DBG(ep
->dev
, "%s %s inactive?\n", __FUNCTION__
, ep
->ep
.name
);
983 spin_lock_irqsave(&ep
->dev
->lock
, flags
);
984 if (!list_empty(&ep
->queue
))
986 else if (ep
->is_in
&& value
987 /* data in (either) packet buffer? */
988 && (readl(&ep
->dev
->regs
->DataSet
)
989 & DATASET_AB(ep
->num
)))
995 VDBG(ep
->dev
, "%s set halt\n", ep
->ep
.name
);
996 command(ep
->dev
->regs
, COMMAND_STALL
, ep
->num
);
997 readl(ep
->reg_status
);
999 spin_unlock_irqrestore(&ep
->dev
->lock
, flags
);
1003 static int goku_fifo_status(struct usb_ep
*_ep
)
1006 struct goku_udc_regs __iomem
*regs
;
1011 ep
= container_of(_ep
, struct goku_ep
, ep
);
1013 /* size is only reported sanely for OUT */
1017 /* ignores 16-byte dma buffer; SizeH == 0 */
1018 regs
= ep
->dev
->regs
;
1019 size
= readl(®s
->EPxSizeLA
[ep
->num
]) & DATASIZE
;
1020 size
+= readl(®s
->EPxSizeLB
[ep
->num
]) & DATASIZE
;
1021 VDBG(ep
->dev
, "%s %s %u\n", __FUNCTION__
, ep
->ep
.name
, size
);
1025 static void goku_fifo_flush(struct usb_ep
*_ep
)
1028 struct goku_udc_regs __iomem
*regs
;
1033 ep
= container_of(_ep
, struct goku_ep
, ep
);
1034 VDBG(ep
->dev
, "%s %s\n", __FUNCTION__
, ep
->ep
.name
);
1036 /* don't change EPxSTATUS_EP_INVALID to READY */
1037 if (!ep
->desc
&& ep
->num
!= 0) {
1038 DBG(ep
->dev
, "%s %s inactive?\n", __FUNCTION__
, ep
->ep
.name
);
1042 regs
= ep
->dev
->regs
;
1043 size
= readl(®s
->EPxSizeLA
[ep
->num
]);
1046 /* Non-desirable behavior: FIFO_CLEAR also clears the
1047 * endpoint halt feature. For OUT, we _could_ just read
1048 * the bytes out (PIO, if !ep->dma); for in, no choice.
1051 command(regs
, COMMAND_FIFO_CLEAR
, ep
->num
);
1054 static struct usb_ep_ops goku_ep_ops
= {
1055 .enable
= goku_ep_enable
,
1056 .disable
= goku_ep_disable
,
1058 .alloc_request
= goku_alloc_request
,
1059 .free_request
= goku_free_request
,
1061 .alloc_buffer
= goku_alloc_buffer
,
1062 .free_buffer
= goku_free_buffer
,
1064 .queue
= goku_queue
,
1065 .dequeue
= goku_dequeue
,
1067 .set_halt
= goku_set_halt
,
1068 .fifo_status
= goku_fifo_status
,
1069 .fifo_flush
= goku_fifo_flush
,
1072 /*-------------------------------------------------------------------------*/
1074 static int goku_get_frame(struct usb_gadget
*_gadget
)
1079 static const struct usb_gadget_ops goku_ops
= {
1080 .get_frame
= goku_get_frame
,
1085 /*-------------------------------------------------------------------------*/
1087 static inline char *dmastr(void)
1090 return "(dma disabled)";
1091 else if (use_dma
== 2)
1092 return "(dma IN and OUT)";
1097 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1099 static const char proc_node_name
[] = "driver/udc";
1101 #define FOURBITS "%s%s%s%s"
1102 #define EIGHTBITS FOURBITS FOURBITS
1105 dump_intmask(const char *label
, u32 mask
, char **next
, unsigned *size
)
1109 /* int_status is the same format ... */
1110 t
= scnprintf(*next
, *size
,
1111 "%s %05X =" FOURBITS EIGHTBITS EIGHTBITS
"\n",
1113 (mask
& INT_PWRDETECT
) ? " power" : "",
1114 (mask
& INT_SYSERROR
) ? " sys" : "",
1115 (mask
& INT_MSTRDEND
) ? " in-dma" : "",
1116 (mask
& INT_MSTWRTMOUT
) ? " wrtmo" : "",
1118 (mask
& INT_MSTWREND
) ? " out-dma" : "",
1119 (mask
& INT_MSTWRSET
) ? " wrset" : "",
1120 (mask
& INT_ERR
) ? " err" : "",
1121 (mask
& INT_SOF
) ? " sof" : "",
1123 (mask
& INT_EP3NAK
) ? " ep3nak" : "",
1124 (mask
& INT_EP2NAK
) ? " ep2nak" : "",
1125 (mask
& INT_EP1NAK
) ? " ep1nak" : "",
1126 (mask
& INT_EP3DATASET
) ? " ep3" : "",
1128 (mask
& INT_EP2DATASET
) ? " ep2" : "",
1129 (mask
& INT_EP1DATASET
) ? " ep1" : "",
1130 (mask
& INT_STATUSNAK
) ? " ep0snak" : "",
1131 (mask
& INT_STATUS
) ? " ep0status" : "",
1133 (mask
& INT_SETUP
) ? " setup" : "",
1134 (mask
& INT_ENDPOINT0
) ? " ep0" : "",
1135 (mask
& INT_USBRESET
) ? " reset" : "",
1136 (mask
& INT_SUSPEND
) ? " suspend" : "");
1143 udc_proc_read(char *buffer
, char **start
, off_t off
, int count
,
1144 int *eof
, void *_dev
)
1147 struct goku_udc
*dev
= _dev
;
1148 struct goku_udc_regs __iomem
*regs
= dev
->regs
;
1150 unsigned size
= count
;
1151 unsigned long flags
;
1152 int i
, t
, is_usb_connected
;
1158 local_irq_save(flags
);
1160 /* basic device status */
1161 tmp
= readl(®s
->power_detect
);
1162 is_usb_connected
= tmp
& PW_DETECT
;
1163 t
= scnprintf(next
, size
,
1165 "%s version: %s %s\n"
1166 "Gadget driver: %s\n"
1169 pci_name(dev
->pdev
), driver_desc
,
1170 driver_name
, DRIVER_VERSION
, dmastr(),
1171 dev
->driver
? dev
->driver
->driver
.name
: "(none)",
1173 ? ((tmp
& PW_PULLUP
) ? "full speed" : "powered")
1176 switch(dev
->ep0state
){
1177 case EP0_DISCONNECT
: tmp
= "ep0_disconnect"; break;
1178 case EP0_IDLE
: tmp
= "ep0_idle"; break;
1179 case EP0_IN
: tmp
= "ep0_in"; break;
1180 case EP0_OUT
: tmp
= "ep0_out"; break;
1181 case EP0_STATUS
: tmp
= "ep0_status"; break;
1182 case EP0_STALL
: tmp
= "ep0_stall"; break;
1183 case EP0_SUSPEND
: tmp
= "ep0_suspend"; break;
1184 default: tmp
= "ep0_?"; break;
1190 dump_intmask("int_status", readl(®s
->int_status
), &next
, &size
);
1191 dump_intmask("int_enable", readl(®s
->int_enable
), &next
, &size
);
1193 if (!is_usb_connected
|| !dev
->driver
|| (tmp
& PW_PULLUP
) == 0)
1196 /* registers for (active) device and ep0 */
1197 t
= scnprintf(next
, size
, "\nirqs %lu\ndataset %02x "
1198 "single.bcs %02x.%02x state %x addr %u\n",
1199 dev
->irqs
, readl(®s
->DataSet
),
1200 readl(®s
->EPxSingle
), readl(®s
->EPxBCS
),
1201 readl(®s
->UsbState
),
1202 readl(®s
->address
));
1206 tmp
= readl(®s
->dma_master
);
1207 t
= scnprintf(next
, size
,
1208 "dma %03X =" EIGHTBITS
"%s %s\n", tmp
,
1209 (tmp
& MST_EOPB_DIS
) ? " eopb-" : "",
1210 (tmp
& MST_EOPB_ENA
) ? " eopb+" : "",
1211 (tmp
& MST_TIMEOUT_DIS
) ? " tmo-" : "",
1212 (tmp
& MST_TIMEOUT_ENA
) ? " tmo+" : "",
1214 (tmp
& MST_RD_EOPB
) ? " eopb" : "",
1215 (tmp
& MST_RD_RESET
) ? " in_reset" : "",
1216 (tmp
& MST_WR_RESET
) ? " out_reset" : "",
1217 (tmp
& MST_RD_ENA
) ? " IN" : "",
1219 (tmp
& MST_WR_ENA
) ? " OUT" : "",
1220 (tmp
& MST_CONNECTION
)
1226 /* dump endpoint queues */
1227 for (i
= 0; i
< 4; i
++) {
1228 struct goku_ep
*ep
= &dev
->ep
[i
];
1229 struct goku_request
*req
;
1235 tmp
= readl(ep
->reg_status
);
1236 t
= scnprintf(next
, size
,
1237 "%s %s max %u %s, irqs %lu, "
1238 "status %02x (%s) " FOURBITS
"\n",
1240 ep
->is_in
? "in" : "out",
1242 ep
->dma
? "dma" : "pio",
1245 switch (tmp
& EPxSTATUS_EP_MASK
) {
1246 case EPxSTATUS_EP_READY
:
1248 case EPxSTATUS_EP_DATAIN
:
1249 s
= "packet"; break;
1250 case EPxSTATUS_EP_FULL
:
1252 case EPxSTATUS_EP_TX_ERR
: // host will retry
1253 s
= "tx_err"; break;
1254 case EPxSTATUS_EP_RX_ERR
:
1255 s
= "rx_err"; break;
1256 case EPxSTATUS_EP_BUSY
: /* ep0 only */
1258 case EPxSTATUS_EP_STALL
:
1260 case EPxSTATUS_EP_INVALID
: // these "can't happen"
1261 s
= "invalid"; break;
1265 (tmp
& EPxSTATUS_TOGGLE
) ? "data1" : "data0",
1266 (tmp
& EPxSTATUS_SUSPEND
) ? " suspend" : "",
1267 (tmp
& EPxSTATUS_FIFO_DISABLE
) ? " disable" : "",
1268 (tmp
& EPxSTATUS_STAGE_ERROR
) ? " ep0stat" : ""
1270 if (t
<= 0 || t
> size
)
1275 if (list_empty(&ep
->queue
)) {
1276 t
= scnprintf(next
, size
, "\t(nothing queued)\n");
1277 if (t
<= 0 || t
> size
)
1283 list_for_each_entry(req
, &ep
->queue
, queue
) {
1284 if (ep
->dma
&& req
->queue
.prev
== &ep
->queue
) {
1285 if (i
== UDC_MSTRD_ENDPOINT
)
1286 tmp
= readl(®s
->in_dma_current
);
1288 tmp
= readl(®s
->out_dma_current
);
1289 tmp
-= req
->req
.dma
;
1292 tmp
= req
->req
.actual
;
1294 t
= scnprintf(next
, size
,
1295 "\treq %p len %u/%u buf %p\n",
1296 &req
->req
, tmp
, req
->req
.length
,
1298 if (t
<= 0 || t
> size
)
1306 local_irq_restore(flags
);
1308 return count
- size
;
1311 #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
1313 /*-------------------------------------------------------------------------*/
1315 static void udc_reinit (struct goku_udc
*dev
)
1317 static char *names
[] = { "ep0", "ep1-bulk", "ep2-bulk", "ep3-bulk" };
1321 INIT_LIST_HEAD (&dev
->gadget
.ep_list
);
1322 dev
->gadget
.ep0
= &dev
->ep
[0].ep
;
1323 dev
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1324 dev
->ep0state
= EP0_DISCONNECT
;
1327 for (i
= 0; i
< 4; i
++) {
1328 struct goku_ep
*ep
= &dev
->ep
[i
];
1331 ep
->ep
.name
= names
[i
];
1332 ep
->reg_fifo
= &dev
->regs
->ep_fifo
[i
];
1333 ep
->reg_status
= &dev
->regs
->ep_status
[i
];
1334 ep
->reg_mode
= &dev
->regs
->ep_mode
[i
];
1336 ep
->ep
.ops
= &goku_ep_ops
;
1337 list_add_tail (&ep
->ep
.ep_list
, &dev
->gadget
.ep_list
);
1339 INIT_LIST_HEAD (&ep
->queue
);
1344 dev
->ep
[0].reg_mode
= NULL
;
1345 dev
->ep
[0].ep
.maxpacket
= MAX_EP0_SIZE
;
1346 list_del_init (&dev
->ep
[0].ep
.ep_list
);
1349 static void udc_reset(struct goku_udc
*dev
)
1351 struct goku_udc_regs __iomem
*regs
= dev
->regs
;
1353 writel(0, ®s
->power_detect
);
1354 writel(0, ®s
->int_enable
);
1355 readl(®s
->int_enable
);
1356 dev
->int_enable
= 0;
1358 /* deassert reset, leave USB D+ at hi-Z (no pullup)
1359 * don't let INT_PWRDETECT sequence begin
1362 writel(PW_RESETB
, ®s
->power_detect
);
1363 readl(®s
->int_enable
);
1366 static void ep0_start(struct goku_udc
*dev
)
1368 struct goku_udc_regs __iomem
*regs
= dev
->regs
;
1371 VDBG(dev
, "%s\n", __FUNCTION__
);
1375 //writel(MST_EOPB_ENA | MST_TIMEOUT_ENA, ®s->dma_master);
1377 /* hw handles set_address, set_feature, get_status; maybe more */
1378 writel( G_REQMODE_SET_INTF
| G_REQMODE_GET_INTF
1379 | G_REQMODE_SET_CONF
| G_REQMODE_GET_CONF
1380 | G_REQMODE_GET_DESC
1381 | G_REQMODE_CLEAR_FEAT
1384 for (i
= 0; i
< 4; i
++)
1385 dev
->ep
[i
].irqs
= 0;
1387 /* can't modify descriptors after writing UsbReady */
1388 for (i
= 0; i
< DESC_LEN
; i
++)
1389 writel(0, ®s
->descriptors
[i
]);
1390 writel(0, ®s
->UsbReady
);
1392 /* expect ep0 requests when the host drops reset */
1393 writel(PW_RESETB
| PW_PULLUP
, ®s
->power_detect
);
1394 dev
->int_enable
= INT_DEVWIDE
| INT_EP0
;
1395 writel(dev
->int_enable
, &dev
->regs
->int_enable
);
1396 readl(®s
->int_enable
);
1397 dev
->gadget
.speed
= USB_SPEED_FULL
;
1398 dev
->ep0state
= EP0_IDLE
;
1401 static void udc_enable(struct goku_udc
*dev
)
1403 /* start enumeration now, or after power detect irq */
1404 if (readl(&dev
->regs
->power_detect
) & PW_DETECT
)
1407 DBG(dev
, "%s\n", __FUNCTION__
);
1408 dev
->int_enable
= INT_PWRDETECT
;
1409 writel(dev
->int_enable
, &dev
->regs
->int_enable
);
1413 /*-------------------------------------------------------------------------*/
1415 /* keeping it simple:
1416 * - one bus driver, initted first;
1417 * - one function driver, initted second
1420 static struct goku_udc
*the_controller
;
1422 /* when a driver is successfully registered, it will receive
1423 * control requests including set_configuration(), which enables
1424 * non-control requests. then usb traffic follows until a
1425 * disconnect is reported. then a host may connect again, or
1426 * the driver might get unbound.
1428 int usb_gadget_register_driver(struct usb_gadget_driver
*driver
)
1430 struct goku_udc
*dev
= the_controller
;
1434 || driver
->speed
!= USB_SPEED_FULL
1437 || !driver
->disconnect
1445 /* hook up the driver */
1446 driver
->driver
.bus
= NULL
;
1447 dev
->driver
= driver
;
1448 dev
->gadget
.dev
.driver
= &driver
->driver
;
1449 retval
= driver
->bind(&dev
->gadget
);
1451 DBG(dev
, "bind to driver %s --> error %d\n",
1452 driver
->driver
.name
, retval
);
1454 dev
->gadget
.dev
.driver
= NULL
;
1458 /* then enable host detection and ep0; and we're ready
1459 * for set_configuration as well as eventual disconnect.
1463 DBG(dev
, "registered gadget driver '%s'\n", driver
->driver
.name
);
1466 EXPORT_SYMBOL(usb_gadget_register_driver
);
1469 stop_activity(struct goku_udc
*dev
, struct usb_gadget_driver
*driver
)
1473 DBG (dev
, "%s\n", __FUNCTION__
);
1475 if (dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)
1478 /* disconnect gadget driver after quiesceing hw and the driver */
1480 for (i
= 0; i
< 4; i
++)
1481 nuke(&dev
->ep
[i
], -ESHUTDOWN
);
1483 spin_unlock(&dev
->lock
);
1484 driver
->disconnect(&dev
->gadget
);
1485 spin_lock(&dev
->lock
);
1492 int usb_gadget_unregister_driver(struct usb_gadget_driver
*driver
)
1494 struct goku_udc
*dev
= the_controller
;
1495 unsigned long flags
;
1499 if (!driver
|| driver
!= dev
->driver
)
1502 spin_lock_irqsave(&dev
->lock
, flags
);
1504 stop_activity(dev
, driver
);
1505 spin_unlock_irqrestore(&dev
->lock
, flags
);
1507 driver
->unbind(&dev
->gadget
);
1509 DBG(dev
, "unregistered driver '%s'\n", driver
->driver
.name
);
1512 EXPORT_SYMBOL(usb_gadget_unregister_driver
);
1515 /*-------------------------------------------------------------------------*/
1517 static void ep0_setup(struct goku_udc
*dev
)
1519 struct goku_udc_regs __iomem
*regs
= dev
->regs
;
1520 struct usb_ctrlrequest ctrl
;
1523 /* read SETUP packet and enter DATA stage */
1524 ctrl
.bRequestType
= readl(®s
->bRequestType
);
1525 ctrl
.bRequest
= readl(®s
->bRequest
);
1526 ctrl
.wValue
= cpu_to_le16((readl(®s
->wValueH
) << 8)
1527 | readl(®s
->wValueL
));
1528 ctrl
.wIndex
= cpu_to_le16((readl(®s
->wIndexH
) << 8)
1529 | readl(®s
->wIndexL
));
1530 ctrl
.wLength
= cpu_to_le16((readl(®s
->wLengthH
) << 8)
1531 | readl(®s
->wLengthL
));
1532 writel(0, ®s
->SetupRecv
);
1534 nuke(&dev
->ep
[0], 0);
1535 dev
->ep
[0].stopped
= 0;
1536 if (likely(ctrl
.bRequestType
& USB_DIR_IN
)) {
1537 dev
->ep
[0].is_in
= 1;
1538 dev
->ep0state
= EP0_IN
;
1539 /* detect early status stages */
1540 writel(ICONTROL_STATUSNAK
, &dev
->regs
->IntControl
);
1542 dev
->ep
[0].is_in
= 0;
1543 dev
->ep0state
= EP0_OUT
;
1545 /* NOTE: CLEAR_FEATURE is done in software so that we can
1546 * synchronize transfer restarts after bulk IN stalls. data
1547 * won't even enter the fifo until the halt is cleared.
1549 switch (ctrl
.bRequest
) {
1550 case USB_REQ_CLEAR_FEATURE
:
1551 switch (ctrl
.bRequestType
) {
1552 case USB_RECIP_ENDPOINT
:
1553 tmp
= le16_to_cpu(ctrl
.wIndex
) & 0x0f;
1554 /* active endpoint */
1555 if (tmp
> 3 || (!dev
->ep
[tmp
].desc
&& tmp
!= 0))
1557 if (ctrl
.wIndex
& __constant_cpu_to_le16(
1559 if (!dev
->ep
[tmp
].is_in
)
1562 if (dev
->ep
[tmp
].is_in
)
1565 if (ctrl
.wValue
!= __constant_cpu_to_le16(
1569 goku_clear_halt(&dev
->ep
[tmp
]);
1571 /* start ep0out status stage */
1572 writel(~(1<<0), ®s
->EOP
);
1573 dev
->ep
[0].stopped
= 1;
1574 dev
->ep0state
= EP0_STATUS
;
1576 case USB_RECIP_DEVICE
:
1577 /* device remote wakeup: always clear */
1578 if (ctrl
.wValue
!= __constant_cpu_to_le16(1))
1580 VDBG(dev
, "clear dev remote wakeup\n");
1582 case USB_RECIP_INTERFACE
:
1584 default: /* pass to gadget driver */
1594 VDBG(dev
, "SETUP %02x.%02x v%04x i%04x l%04x\n",
1595 ctrl
.bRequestType
, ctrl
.bRequest
,
1596 le16_to_cpu(ctrl
.wValue
), le16_to_cpu(ctrl
.wIndex
),
1597 le16_to_cpu(ctrl
.wLength
));
1600 /* hw wants to know when we're configured (or not) */
1601 dev
->req_config
= (ctrl
.bRequest
== USB_REQ_SET_CONFIGURATION
1602 && ctrl
.bRequestType
== USB_RECIP_DEVICE
);
1603 if (unlikely(dev
->req_config
))
1604 dev
->configured
= (ctrl
.wValue
!= __constant_cpu_to_le16(0));
1606 /* delegate everything to the gadget driver.
1607 * it may respond after this irq handler returns.
1609 spin_unlock (&dev
->lock
);
1610 tmp
= dev
->driver
->setup(&dev
->gadget
, &ctrl
);
1611 spin_lock (&dev
->lock
);
1612 if (unlikely(tmp
< 0)) {
1615 VDBG(dev
, "req %02x.%02x protocol STALL; err %d\n",
1616 ctrl
.bRequestType
, ctrl
.bRequest
, tmp
);
1618 command(regs
, COMMAND_STALL
, 0);
1619 dev
->ep
[0].stopped
= 1;
1620 dev
->ep0state
= EP0_STALL
;
1623 /* expect at least one data or status stage irq */
1626 #define ACK(irqbit) { \
1628 writel(~irqbit, ®s->int_status); \
1632 static irqreturn_t
goku_irq(int irq
, void *_dev
, struct pt_regs
*r
)
1634 struct goku_udc
*dev
= _dev
;
1635 struct goku_udc_regs __iomem
*regs
= dev
->regs
;
1637 u32 stat
, handled
= 0;
1638 unsigned i
, rescans
= 5;
1640 spin_lock(&dev
->lock
);
1643 stat
= readl(®s
->int_status
) & dev
->int_enable
;
1648 /* device-wide irqs */
1649 if (unlikely(stat
& INT_DEVWIDE
)) {
1650 if (stat
& INT_SYSERROR
) {
1651 ERROR(dev
, "system error\n");
1652 stop_activity(dev
, dev
->driver
);
1655 // FIXME have a neater way to prevent re-enumeration
1659 if (stat
& INT_PWRDETECT
) {
1660 writel(~stat
, ®s
->int_status
);
1661 if (readl(&dev
->regs
->power_detect
) & PW_DETECT
) {
1662 VDBG(dev
, "connect\n");
1665 DBG(dev
, "disconnect\n");
1666 if (dev
->gadget
.speed
== USB_SPEED_FULL
)
1667 stop_activity(dev
, dev
->driver
);
1668 dev
->ep0state
= EP0_DISCONNECT
;
1669 dev
->int_enable
= INT_DEVWIDE
;
1670 writel(dev
->int_enable
, &dev
->regs
->int_enable
);
1676 if (stat
& INT_SUSPEND
) {
1678 if (readl(®s
->ep_status
[0]) & EPxSTATUS_SUSPEND
) {
1679 switch (dev
->ep0state
) {
1680 case EP0_DISCONNECT
:
1686 DBG(dev
, "USB suspend\n");
1687 dev
->ep0state
= EP0_SUSPEND
;
1688 if (dev
->gadget
.speed
!= USB_SPEED_UNKNOWN
1690 && dev
->driver
->suspend
) {
1691 spin_unlock(&dev
->lock
);
1692 dev
->driver
->suspend(&dev
->gadget
);
1693 spin_lock(&dev
->lock
);
1696 if (dev
->ep0state
!= EP0_SUSPEND
) {
1697 DBG(dev
, "bogus USB resume %d\n",
1701 DBG(dev
, "USB resume\n");
1702 dev
->ep0state
= EP0_IDLE
;
1703 if (dev
->gadget
.speed
!= USB_SPEED_UNKNOWN
1705 && dev
->driver
->resume
) {
1706 spin_unlock(&dev
->lock
);
1707 dev
->driver
->resume(&dev
->gadget
);
1708 spin_lock(&dev
->lock
);
1713 if (stat
& INT_USBRESET
) { /* hub reset done */
1715 INFO(dev
, "USB reset done, gadget %s\n",
1716 dev
->driver
->driver
.name
);
1718 // and INT_ERR on some endpoint's crc/bitstuff/... problem
1721 /* progress ep0 setup, data, or status stages.
1722 * no transition {EP0_STATUS, EP0_STALL} --> EP0_IDLE; saves irqs
1724 if (stat
& INT_SETUP
) {
1729 if (stat
& INT_STATUSNAK
) {
1730 ACK(INT_STATUSNAK
|INT_ENDPOINT0
);
1731 if (dev
->ep0state
== EP0_IN
) {
1735 writel(~(1<<0), ®s
->EOP
);
1736 dev
->ep0state
= EP0_STATUS
;
1739 if (stat
& INT_ENDPOINT0
) {
1746 /* dma completion */
1747 if (stat
& INT_MSTRDEND
) { /* IN */
1749 ep
= &dev
->ep
[UDC_MSTRD_ENDPOINT
];
1751 dma_advance(dev
, ep
);
1753 if (stat
& INT_MSTWREND
) { /* OUT */
1755 ep
= &dev
->ep
[UDC_MSTWR_ENDPOINT
];
1757 dma_advance(dev
, ep
);
1759 if (stat
& INT_MSTWRTMOUT
) { /* OUT */
1760 ACK(INT_MSTWRTMOUT
);
1761 ep
= &dev
->ep
[UDC_MSTWR_ENDPOINT
];
1763 ERROR(dev
, "%s write timeout ?\n", ep
->ep
.name
);
1764 // reset dma? then dma_advance()
1768 for (i
= 1; i
< 4; i
++) {
1769 u32 tmp
= INT_EPxDATASET(i
);
1775 if (list_empty (&ep
->queue
))
1776 pio_irq_disable(dev
, regs
, i
);
1786 (void)readl(®s
->int_enable
);
1787 spin_unlock(&dev
->lock
);
1789 DBG(dev
, "unhandled irq status: %05x (%05x, %05x)\n", stat
,
1790 readl(®s
->int_status
), dev
->int_enable
);
1791 return IRQ_RETVAL(handled
);
1796 /*-------------------------------------------------------------------------*/
1798 static void gadget_release(struct device
*_dev
)
1800 struct goku_udc
*dev
= dev_get_drvdata(_dev
);
1805 /* tear down the binding between this driver and the pci device */
1807 static void goku_remove(struct pci_dev
*pdev
)
1809 struct goku_udc
*dev
= pci_get_drvdata(pdev
);
1811 DBG(dev
, "%s\n", __FUNCTION__
);
1812 /* start with the driver above us */
1814 /* should have been done already by driver model core */
1815 WARN(dev
, "pci remove, driver '%s' is still registered\n",
1816 dev
->driver
->driver
.name
);
1817 usb_gadget_unregister_driver(dev
->driver
);
1820 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1821 remove_proc_entry(proc_node_name
, NULL
);
1826 free_irq(pdev
->irq
, dev
);
1829 if (dev
->got_region
)
1830 release_mem_region(pci_resource_start (pdev
, 0),
1831 pci_resource_len (pdev
, 0));
1833 pci_disable_device(pdev
);
1834 device_unregister(&dev
->gadget
.dev
);
1836 pci_set_drvdata(pdev
, NULL
);
1838 the_controller
= NULL
;
1840 INFO(dev
, "unbind\n");
1843 /* wrap this driver around the specified pci device, but
1844 * don't respond over USB until a gadget driver binds to us.
1847 static int goku_probe(struct pci_dev
*pdev
, const struct pci_device_id
*id
)
1849 struct goku_udc
*dev
= NULL
;
1850 unsigned long resource
, len
;
1851 void __iomem
*base
= NULL
;
1853 char buf
[8], *bufp
;
1855 /* if you want to support more than one controller in a system,
1856 * usb_gadget_driver_{register,unregister}() must change.
1858 if (the_controller
) {
1859 WARN(dev
, "ignoring %s\n", pci_name(pdev
));
1863 printk(KERN_ERR
"Check PCI %s IRQ setup!\n", pci_name(pdev
));
1868 /* alloc, and start init */
1869 dev
= kmalloc (sizeof *dev
, SLAB_KERNEL
);
1871 pr_debug("enomem %s\n", pci_name(pdev
));
1876 memset(dev
, 0, sizeof *dev
);
1877 spin_lock_init(&dev
->lock
);
1879 dev
->gadget
.ops
= &goku_ops
;
1881 /* the "gadget" abstracts/virtualizes the controller */
1882 strcpy(dev
->gadget
.dev
.bus_id
, "gadget");
1883 dev
->gadget
.dev
.parent
= &pdev
->dev
;
1884 dev
->gadget
.dev
.dma_mask
= pdev
->dev
.dma_mask
;
1885 dev
->gadget
.dev
.release
= gadget_release
;
1886 dev
->gadget
.name
= driver_name
;
1888 /* now all the pci goodies ... */
1889 retval
= pci_enable_device(pdev
);
1891 DBG(dev
, "can't enable, %d\n", retval
);
1896 resource
= pci_resource_start(pdev
, 0);
1897 len
= pci_resource_len(pdev
, 0);
1898 if (!request_mem_region(resource
, len
, driver_name
)) {
1899 DBG(dev
, "controller already in use\n");
1903 dev
->got_region
= 1;
1905 base
= ioremap_nocache(resource
, len
);
1907 DBG(dev
, "can't map memory\n");
1911 dev
->regs
= (struct goku_udc_regs __iomem
*) base
;
1913 pci_set_drvdata(pdev
, dev
);
1914 INFO(dev
, "%s\n", driver_desc
);
1915 INFO(dev
, "version: " DRIVER_VERSION
" %s\n", dmastr());
1917 scnprintf(buf
, sizeof buf
, "%d", pdev
->irq
);
1920 bufp
= __irq_itoa(pdev
->irq
);
1922 INFO(dev
, "irq %s, pci mem %p\n", bufp
, base
);
1924 /* init to known state, then setup irqs */
1927 if (request_irq(pdev
->irq
, goku_irq
, SA_SHIRQ
/*|SA_SAMPLE_RANDOM*/,
1928 driver_name
, dev
) != 0) {
1929 DBG(dev
, "request interrupt %s failed\n", bufp
);
1935 pci_set_master(pdev
);
1938 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1939 create_proc_read_entry(proc_node_name
, 0, NULL
, udc_proc_read
, dev
);
1943 the_controller
= dev
;
1944 device_register(&dev
->gadget
.dev
);
1955 /*-------------------------------------------------------------------------*/
1957 static struct pci_device_id pci_ids
[] = { {
1958 .class = ((PCI_CLASS_SERIAL_USB
<< 8) | 0xfe),
1960 .vendor
= 0x102f, /* Toshiba */
1961 .device
= 0x0107, /* this UDC */
1962 .subvendor
= PCI_ANY_ID
,
1963 .subdevice
= PCI_ANY_ID
,
1965 }, { /* end: all zeroes */ }
1967 MODULE_DEVICE_TABLE (pci
, pci_ids
);
1969 static struct pci_driver goku_pci_driver
= {
1970 .name
= (char *) driver_name
,
1971 .id_table
= pci_ids
,
1973 .probe
= goku_probe
,
1974 .remove
= goku_remove
,
1976 /* FIXME add power management support */
1979 static int __init
init (void)
1981 return pci_register_driver (&goku_pci_driver
);
1985 static void __exit
cleanup (void)
1987 pci_unregister_driver (&goku_pci_driver
);
1989 module_exit (cleanup
);