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>
42 #include <asm/byteorder.h>
45 #include <asm/system.h>
46 #include <asm/unaligned.h>
51 #define DRIVER_DESC "TC86C001 USB Device Controller"
52 #define DRIVER_VERSION "30-Oct 2003"
54 #define DMA_ADDR_INVALID (~(dma_addr_t)0)
56 static const char driver_name
[] = "goku_udc";
57 static const char driver_desc
[] = DRIVER_DESC
;
59 MODULE_AUTHOR("source@mvista.com");
60 MODULE_DESCRIPTION(DRIVER_DESC
);
61 MODULE_LICENSE("GPL");
65 * IN dma behaves ok under testing, though the IN-dma abort paths don't
66 * seem to behave quite as expected. Used by default.
68 * OUT dma documents design problems handling the common "short packet"
69 * transfer termination policy; it couldn't be enabled by default, even
70 * if the OUT-dma abort problems had a resolution.
72 static unsigned use_dma
= 1;
75 //#include <linux/moduleparam.h>
76 /* "modprobe goku_udc use_dma=1" etc
78 * 1 to use IN dma only (normal operation)
79 * 2 to use IN and OUT dma
81 module_param(use_dma
, uint
, S_IRUGO
);
84 /*-------------------------------------------------------------------------*/
86 static void nuke(struct goku_ep
*, int status
);
89 command(struct goku_udc_regs __iomem
*regs
, int command
, unsigned epnum
)
91 writel(COMMAND_EP(epnum
) | command
, ®s
->Command
);
96 goku_ep_enable(struct usb_ep
*_ep
, const struct usb_endpoint_descriptor
*desc
)
104 ep
= container_of(_ep
, struct goku_ep
, ep
);
105 if (!_ep
|| !desc
|| ep
->desc
106 || desc
->bDescriptorType
!= USB_DT_ENDPOINT
)
109 if (ep
== &dev
->ep
[0])
111 if (!dev
->driver
|| dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)
113 if (ep
->num
!= (desc
->bEndpointAddress
& 0x0f))
116 switch (desc
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) {
117 case USB_ENDPOINT_XFER_BULK
:
118 case USB_ENDPOINT_XFER_INT
:
124 if ((readl(ep
->reg_status
) & EPxSTATUS_EP_MASK
)
125 != EPxSTATUS_EP_INVALID
)
128 /* enabling the no-toggle interrupt mode would need an api hook */
130 max
= le16_to_cpu(get_unaligned(&desc
->wMaxPacketSize
));
140 mode
|= 2 << 1; /* bulk, or intr-with-toggle */
142 /* ep1/ep2 dma direction is chosen early; it works in the other
143 * direction, with pio. be cautious with out-dma.
145 ep
->is_in
= (USB_DIR_IN
& desc
->bEndpointAddress
) != 0;
148 ep
->dma
= (use_dma
!= 0) && (ep
->num
== UDC_MSTRD_ENDPOINT
);
150 ep
->dma
= (use_dma
== 2) && (ep
->num
== UDC_MSTWR_ENDPOINT
);
152 DBG(dev
, "%s out-dma hides short packets\n",
156 spin_lock_irqsave(&ep
->dev
->lock
, flags
);
158 /* ep1 and ep2 can do double buffering and/or dma */
160 struct goku_udc_regs __iomem
*regs
= ep
->dev
->regs
;
163 /* double buffer except (for now) with pio in */
164 tmp
= ((ep
->dma
|| !ep
->is_in
)
165 ? 0x10 /* double buffered */
166 : 0x11 /* single buffer */
168 tmp
|= readl(®s
->EPxSingle
);
169 writel(tmp
, ®s
->EPxSingle
);
171 tmp
= (ep
->dma
? 0x10/*dma*/ : 0x11/*pio*/) << ep
->num
;
172 tmp
|= readl(®s
->EPxBCS
);
173 writel(tmp
, ®s
->EPxBCS
);
175 writel(mode
, ep
->reg_mode
);
176 command(ep
->dev
->regs
, COMMAND_RESET
, ep
->num
);
177 ep
->ep
.maxpacket
= max
;
180 spin_unlock_irqrestore(&ep
->dev
->lock
, flags
);
182 DBG(dev
, "enable %s %s %s maxpacket %u\n", ep
->ep
.name
,
183 ep
->is_in
? "IN" : "OUT",
184 ep
->dma
? "dma" : "pio",
190 static void ep_reset(struct goku_udc_regs __iomem
*regs
, struct goku_ep
*ep
)
192 struct goku_udc
*dev
= ep
->dev
;
195 command(regs
, COMMAND_INVALID
, ep
->num
);
197 if (ep
->num
== UDC_MSTWR_ENDPOINT
)
198 dev
->int_enable
&= ~(INT_MSTWREND
200 else if (ep
->num
== UDC_MSTRD_ENDPOINT
)
201 dev
->int_enable
&= ~INT_MSTRDEND
;
202 dev
->int_enable
&= ~INT_EPxDATASET (ep
->num
);
204 dev
->int_enable
&= ~INT_EP0
;
205 writel(dev
->int_enable
, ®s
->int_enable
);
206 readl(®s
->int_enable
);
208 struct goku_udc_regs __iomem
*r
= ep
->dev
->regs
;
211 tmp
= readl(&r
->EPxSingle
);
212 tmp
&= ~(0x11 << ep
->num
);
213 writel(tmp
, &r
->EPxSingle
);
215 tmp
= readl(&r
->EPxBCS
);
216 tmp
&= ~(0x11 << ep
->num
);
217 writel(tmp
, &r
->EPxBCS
);
219 /* reset dma in case we're still using it */
223 master
= readl(®s
->dma_master
) & MST_RW_BITS
;
224 if (ep
->num
== UDC_MSTWR_ENDPOINT
) {
225 master
&= ~MST_W_BITS
;
226 master
|= MST_WR_RESET
;
228 master
&= ~MST_R_BITS
;
229 master
|= MST_RD_RESET
;
231 writel(master
, ®s
->dma_master
);
235 ep
->ep
.maxpacket
= MAX_FIFO_SIZE
;
242 static int goku_ep_disable(struct usb_ep
*_ep
)
245 struct goku_udc
*dev
;
248 ep
= container_of(_ep
, struct goku_ep
, ep
);
249 if (!_ep
|| !ep
->desc
)
252 if (dev
->ep0state
== EP0_SUSPEND
)
255 VDBG(dev
, "disable %s\n", _ep
->name
);
257 spin_lock_irqsave(&dev
->lock
, flags
);
258 nuke(ep
, -ESHUTDOWN
);
259 ep_reset(dev
->regs
, ep
);
260 spin_unlock_irqrestore(&dev
->lock
, flags
);
265 /*-------------------------------------------------------------------------*/
267 static struct usb_request
*
268 goku_alloc_request(struct usb_ep
*_ep
, gfp_t gfp_flags
)
270 struct goku_request
*req
;
274 req
= kzalloc(sizeof *req
, gfp_flags
);
278 req
->req
.dma
= DMA_ADDR_INVALID
;
279 INIT_LIST_HEAD(&req
->queue
);
284 goku_free_request(struct usb_ep
*_ep
, struct usb_request
*_req
)
286 struct goku_request
*req
;
291 req
= container_of(_req
, struct goku_request
, req
);
292 WARN_ON(!list_empty(&req
->queue
));
296 /*-------------------------------------------------------------------------*/
299 done(struct goku_ep
*ep
, struct goku_request
*req
, int status
)
301 struct goku_udc
*dev
;
302 unsigned stopped
= ep
->stopped
;
304 list_del_init(&req
->queue
);
306 if (likely(req
->req
.status
== -EINPROGRESS
))
307 req
->req
.status
= status
;
309 status
= req
->req
.status
;
313 pci_unmap_single(dev
->pdev
, req
->req
.dma
, req
->req
.length
,
314 ep
->is_in
? PCI_DMA_TODEVICE
: PCI_DMA_FROMDEVICE
);
315 req
->req
.dma
= DMA_ADDR_INVALID
;
320 if (status
&& status
!= -ESHUTDOWN
)
322 VDBG(dev
, "complete %s req %p stat %d len %u/%u\n",
323 ep
->ep
.name
, &req
->req
, status
,
324 req
->req
.actual
, req
->req
.length
);
326 /* don't modify queue heads during completion callback */
328 spin_unlock(&dev
->lock
);
329 req
->req
.complete(&ep
->ep
, &req
->req
);
330 spin_lock(&dev
->lock
);
331 ep
->stopped
= stopped
;
334 /*-------------------------------------------------------------------------*/
337 write_packet(u32 __iomem
*fifo
, u8
*buf
, struct goku_request
*req
, unsigned max
)
339 unsigned length
, count
;
341 length
= min(req
->req
.length
- req
->req
.actual
, max
);
342 req
->req
.actual
+= length
;
345 while (likely(count
--))
346 writel(*buf
++, fifo
);
350 // return: 0 = still running, 1 = completed, negative = errno
351 static int write_fifo(struct goku_ep
*ep
, struct goku_request
*req
)
353 struct goku_udc
*dev
= ep
->dev
;
359 tmp
= readl(&dev
->regs
->DataSet
);
360 buf
= req
->req
.buf
+ req
->req
.actual
;
364 if (unlikely(ep
->num
== 0 && dev
->ep0state
!= EP0_IN
))
367 /* NOTE: just single-buffered PIO-IN for now. */
368 if (unlikely((tmp
& DATASET_A(ep
->num
)) != 0))
371 /* clear our "packet available" irq */
373 writel(~INT_EPxDATASET(ep
->num
), &dev
->regs
->int_status
);
375 count
= write_packet(ep
->reg_fifo
, buf
, req
, ep
->ep
.maxpacket
);
377 /* last packet often short (sometimes a zlp, especially on ep0) */
378 if (unlikely(count
!= ep
->ep
.maxpacket
)) {
379 writel(~(1<<ep
->num
), &dev
->regs
->EOP
);
381 dev
->ep
[0].stopped
= 1;
382 dev
->ep0state
= EP0_STATUS
;
386 if (likely(req
->req
.length
!= req
->req
.actual
)
392 #if 0 /* printk seemed to trash is_last...*/
394 VDBG(dev
, "wrote %s %u bytes%s IN %u left %p\n",
395 ep
->ep
.name
, count
, is_last
? "/last" : "",
396 req
->req
.length
- req
->req
.actual
, req
);
399 /* requests complete when all IN data is in the FIFO,
400 * or sometimes later, if a zlp was needed.
410 static int read_fifo(struct goku_ep
*ep
, struct goku_request
*req
)
412 struct goku_udc_regs __iomem
*regs
;
415 unsigned bufferspace
, is_short
, dbuff
;
417 regs
= ep
->dev
->regs
;
419 buf
= req
->req
.buf
+ req
->req
.actual
;
422 if (unlikely(ep
->num
== 0 && ep
->dev
->ep0state
!= EP0_OUT
))
425 dbuff
= (ep
->num
== 1 || ep
->num
== 2);
427 /* ack dataset irq matching the status we'll handle */
429 writel(~INT_EPxDATASET(ep
->num
), ®s
->int_status
);
431 set
= readl(®s
->DataSet
) & DATASET_AB(ep
->num
);
432 size
= readl(®s
->EPxSizeLA
[ep
->num
]);
433 bufferspace
= req
->req
.length
- req
->req
.actual
;
435 /* usually do nothing without an OUT packet */
436 if (likely(ep
->num
!= 0 || bufferspace
!= 0)) {
437 if (unlikely(set
== 0))
439 /* use ep1/ep2 double-buffering for OUT */
440 if (!(size
& PACKET_ACTIVE
))
441 size
= readl(®s
->EPxSizeLB
[ep
->num
]);
442 if (!(size
& PACKET_ACTIVE
)) /* "can't happen" */
444 size
&= DATASIZE
; /* EPxSizeH == 0 */
446 /* ep0out no-out-data case for set_config, etc */
450 /* read all bytes from this packet */
451 req
->req
.actual
+= size
;
452 is_short
= (size
< ep
->ep
.maxpacket
);
454 VDBG(ep
->dev
, "read %s %u bytes%s OUT req %p %u/%u\n",
455 ep
->ep
.name
, size
, is_short
? "/S" : "",
456 req
, req
->req
.actual
, req
->req
.length
);
458 while (likely(size
-- != 0)) {
459 u8 byte
= (u8
) readl(ep
->reg_fifo
);
461 if (unlikely(bufferspace
== 0)) {
462 /* this happens when the driver's buffer
463 * is smaller than what the host sent.
464 * discard the extra data in this packet.
466 if (req
->req
.status
!= -EOVERFLOW
)
467 DBG(ep
->dev
, "%s overflow %u\n",
469 req
->req
.status
= -EOVERFLOW
;
477 if (unlikely(is_short
|| req
->req
.actual
== req
->req
.length
)) {
478 if (unlikely(ep
->num
== 0)) {
479 /* non-control endpoints now usable? */
480 if (ep
->dev
->req_config
)
481 writel(ep
->dev
->configured
482 ? USBSTATE_CONFIGURED
485 /* ep0out status stage */
486 writel(~(1<<0), ®s
->EOP
);
488 ep
->dev
->ep0state
= EP0_STATUS
;
492 /* empty the second buffer asap */
493 if (dbuff
&& !list_empty(&ep
->queue
)) {
494 req
= list_entry(ep
->queue
.next
,
495 struct goku_request
, queue
);
505 pio_irq_enable(struct goku_udc
*dev
,
506 struct goku_udc_regs __iomem
*regs
, int epnum
)
508 dev
->int_enable
|= INT_EPxDATASET (epnum
);
509 writel(dev
->int_enable
, ®s
->int_enable
);
510 /* write may still be posted */
514 pio_irq_disable(struct goku_udc
*dev
,
515 struct goku_udc_regs __iomem
*regs
, int epnum
)
517 dev
->int_enable
&= ~INT_EPxDATASET (epnum
);
518 writel(dev
->int_enable
, ®s
->int_enable
);
519 /* write may still be posted */
523 pio_advance(struct goku_ep
*ep
)
525 struct goku_request
*req
;
527 if (unlikely(list_empty (&ep
->queue
)))
529 req
= list_entry(ep
->queue
.next
, struct goku_request
, queue
);
530 (ep
->is_in
? write_fifo
: read_fifo
)(ep
, req
);
534 /*-------------------------------------------------------------------------*/
536 // return: 0 = q running, 1 = q stopped, negative = errno
537 static int start_dma(struct goku_ep
*ep
, struct goku_request
*req
)
539 struct goku_udc_regs __iomem
*regs
= ep
->dev
->regs
;
541 u32 start
= req
->req
.dma
;
542 u32 end
= start
+ req
->req
.length
- 1;
544 master
= readl(®s
->dma_master
) & MST_RW_BITS
;
546 /* re-init the bits affecting IN dma; careful with zlps */
547 if (likely(ep
->is_in
)) {
548 if (unlikely(master
& MST_RD_ENA
)) {
549 DBG (ep
->dev
, "start, IN active dma %03x!!\n",
553 writel(end
, ®s
->in_dma_end
);
554 writel(start
, ®s
->in_dma_start
);
556 master
&= ~MST_R_BITS
;
557 if (unlikely(req
->req
.length
== 0))
558 master
= MST_RD_ENA
| MST_RD_EOPB
;
559 else if ((req
->req
.length
% ep
->ep
.maxpacket
) != 0
561 master
= MST_RD_ENA
| MST_EOPB_ENA
;
563 master
= MST_RD_ENA
| MST_EOPB_DIS
;
565 ep
->dev
->int_enable
|= INT_MSTRDEND
;
567 /* Goku DMA-OUT merges short packets, which plays poorly with
568 * protocols where short packets mark the transfer boundaries.
569 * The chip supports a nonstandard policy with INT_MSTWRTMOUT,
570 * ending transfers after 3 SOFs; we don't turn it on.
573 if (unlikely(master
& MST_WR_ENA
)) {
574 DBG (ep
->dev
, "start, OUT active dma %03x!!\n",
578 writel(end
, ®s
->out_dma_end
);
579 writel(start
, ®s
->out_dma_start
);
581 master
&= ~MST_W_BITS
;
582 master
|= MST_WR_ENA
| MST_TIMEOUT_DIS
;
584 ep
->dev
->int_enable
|= INT_MSTWREND
|INT_MSTWRTMOUT
;
587 writel(master
, ®s
->dma_master
);
588 writel(ep
->dev
->int_enable
, ®s
->int_enable
);
592 static void dma_advance(struct goku_udc
*dev
, struct goku_ep
*ep
)
594 struct goku_request
*req
;
595 struct goku_udc_regs __iomem
*regs
= ep
->dev
->regs
;
598 master
= readl(®s
->dma_master
);
600 if (unlikely(list_empty(&ep
->queue
))) {
603 dev
->int_enable
&= ~INT_MSTRDEND
;
605 dev
->int_enable
&= ~(INT_MSTWREND
|INT_MSTWRTMOUT
);
606 writel(dev
->int_enable
, ®s
->int_enable
);
609 req
= list_entry(ep
->queue
.next
, struct goku_request
, queue
);
611 /* normal hw dma completion (not abort) */
612 if (likely(ep
->is_in
)) {
613 if (unlikely(master
& MST_RD_ENA
))
615 req
->req
.actual
= readl(®s
->in_dma_current
);
617 if (unlikely(master
& MST_WR_ENA
))
620 /* hardware merges short packets, and also hides packet
621 * overruns. a partial packet MAY be in the fifo here.
623 req
->req
.actual
= readl(®s
->out_dma_current
);
625 req
->req
.actual
-= req
->req
.dma
;
629 VDBG(dev
, "done %s %s dma, %u/%u bytes, req %p\n",
630 ep
->ep
.name
, ep
->is_in
? "IN" : "OUT",
631 req
->req
.actual
, req
->req
.length
, req
);
634 if (list_empty(&ep
->queue
))
636 req
= list_entry(ep
->queue
.next
, struct goku_request
, queue
);
637 (void) start_dma(ep
, req
);
640 static void abort_dma(struct goku_ep
*ep
, int status
)
642 struct goku_udc_regs __iomem
*regs
= ep
->dev
->regs
;
643 struct goku_request
*req
;
646 /* NAK future host requests, hoping the implicit delay lets the
647 * dma engine finish reading (or writing) its latest packet and
648 * empty the dma buffer (up to 16 bytes).
650 * This avoids needing to clean up a partial packet in the fifo;
651 * we can't do that for IN without side effects to HALT and TOGGLE.
653 command(regs
, COMMAND_FIFO_DISABLE
, ep
->num
);
654 req
= list_entry(ep
->queue
.next
, struct goku_request
, queue
);
655 master
= readl(®s
->dma_master
) & MST_RW_BITS
;
657 /* FIXME using these resets isn't usably documented. this may
658 * not work unless it's followed by disabling the endpoint.
660 * FIXME the OUT reset path doesn't even behave consistently.
663 if (unlikely((readl(®s
->dma_master
) & MST_RD_ENA
) == 0))
665 curr
= readl(®s
->in_dma_current
);
667 writel(curr
, ®s
->in_dma_end
);
668 writel(curr
, ®s
->in_dma_start
);
670 master
&= ~MST_R_BITS
;
671 master
|= MST_RD_RESET
;
672 writel(master
, ®s
->dma_master
);
674 if (readl(®s
->dma_master
) & MST_RD_ENA
)
675 DBG(ep
->dev
, "IN dma active after reset!\n");
678 if (unlikely((readl(®s
->dma_master
) & MST_WR_ENA
) == 0))
680 curr
= readl(®s
->out_dma_current
);
682 writel(curr
, ®s
->out_dma_end
);
683 writel(curr
, ®s
->out_dma_start
);
685 master
&= ~MST_W_BITS
;
686 master
|= MST_WR_RESET
;
687 writel(master
, ®s
->dma_master
);
689 if (readl(®s
->dma_master
) & MST_WR_ENA
)
690 DBG(ep
->dev
, "OUT dma active after reset!\n");
692 req
->req
.actual
= (curr
- req
->req
.dma
) + 1;
693 req
->req
.status
= status
;
695 VDBG(ep
->dev
, "%s %s %s %d/%d\n", __FUNCTION__
, ep
->ep
.name
,
696 ep
->is_in
? "IN" : "OUT",
697 req
->req
.actual
, req
->req
.length
);
699 command(regs
, COMMAND_FIFO_ENABLE
, ep
->num
);
704 /* dma already completed; no abort needed */
705 command(regs
, COMMAND_FIFO_ENABLE
, ep
->num
);
706 req
->req
.actual
= req
->req
.length
;
710 /*-------------------------------------------------------------------------*/
713 goku_queue(struct usb_ep
*_ep
, struct usb_request
*_req
, gfp_t gfp_flags
)
715 struct goku_request
*req
;
717 struct goku_udc
*dev
;
721 /* always require a cpu-view buffer so pio works */
722 req
= container_of(_req
, struct goku_request
, req
);
723 if (unlikely(!_req
|| !_req
->complete
724 || !_req
->buf
|| !list_empty(&req
->queue
)))
726 ep
= container_of(_ep
, struct goku_ep
, ep
);
727 if (unlikely(!_ep
|| (!ep
->desc
&& ep
->num
!= 0)))
730 if (unlikely(!dev
->driver
|| dev
->gadget
.speed
== USB_SPEED_UNKNOWN
))
733 /* can't touch registers when suspended */
734 if (dev
->ep0state
== EP0_SUSPEND
)
737 /* set up dma mapping in case the caller didn't */
738 if (ep
->dma
&& _req
->dma
== DMA_ADDR_INVALID
) {
739 _req
->dma
= pci_map_single(dev
->pdev
, _req
->buf
, _req
->length
,
740 ep
->is_in
? PCI_DMA_TODEVICE
: PCI_DMA_FROMDEVICE
);
745 VDBG(dev
, "%s queue req %p, len %u buf %p\n",
746 _ep
->name
, _req
, _req
->length
, _req
->buf
);
749 spin_lock_irqsave(&dev
->lock
, flags
);
751 _req
->status
= -EINPROGRESS
;
754 /* for ep0 IN without premature status, zlp is required and
755 * writing EOP starts the status stage (OUT).
757 if (unlikely(ep
->num
== 0 && ep
->is_in
))
760 /* kickstart this i/o queue? */
762 if (list_empty(&ep
->queue
) && likely(!ep
->stopped
)) {
763 /* dma: done after dma completion IRQ (or error)
764 * pio: done after last fifo operation
767 status
= start_dma(ep
, req
);
769 status
= (ep
->is_in
? write_fifo
: read_fifo
)(ep
, req
);
771 if (unlikely(status
!= 0)) {
777 } /* else pio or dma irq handler advances the queue. */
779 if (likely(req
!= 0))
780 list_add_tail(&req
->queue
, &ep
->queue
);
782 if (likely(!list_empty(&ep
->queue
))
783 && likely(ep
->num
!= 0)
785 && !(dev
->int_enable
& INT_EPxDATASET (ep
->num
)))
786 pio_irq_enable(dev
, dev
->regs
, ep
->num
);
788 spin_unlock_irqrestore(&dev
->lock
, flags
);
790 /* pci writes may still be posted */
794 /* dequeue ALL requests */
795 static void nuke(struct goku_ep
*ep
, int status
)
797 struct goku_request
*req
;
800 if (list_empty(&ep
->queue
))
803 abort_dma(ep
, status
);
804 while (!list_empty(&ep
->queue
)) {
805 req
= list_entry(ep
->queue
.next
, struct goku_request
, queue
);
806 done(ep
, req
, status
);
810 /* dequeue JUST ONE request */
811 static int goku_dequeue(struct usb_ep
*_ep
, struct usb_request
*_req
)
813 struct goku_request
*req
;
815 struct goku_udc
*dev
;
818 ep
= container_of(_ep
, struct goku_ep
, ep
);
819 if (!_ep
|| !_req
|| (!ep
->desc
&& ep
->num
!= 0))
825 /* we can't touch (dma) registers when suspended */
826 if (dev
->ep0state
== EP0_SUSPEND
)
829 VDBG(dev
, "%s %s %s %s %p\n", __FUNCTION__
, _ep
->name
,
830 ep
->is_in
? "IN" : "OUT",
831 ep
->dma
? "dma" : "pio",
834 spin_lock_irqsave(&dev
->lock
, flags
);
836 /* make sure it's actually queued on this endpoint */
837 list_for_each_entry (req
, &ep
->queue
, queue
) {
838 if (&req
->req
== _req
)
841 if (&req
->req
!= _req
) {
842 spin_unlock_irqrestore (&dev
->lock
, flags
);
846 if (ep
->dma
&& ep
->queue
.next
== &req
->queue
&& !ep
->stopped
) {
847 abort_dma(ep
, -ECONNRESET
);
848 done(ep
, req
, -ECONNRESET
);
849 dma_advance(dev
, ep
);
850 } else if (!list_empty(&req
->queue
))
851 done(ep
, req
, -ECONNRESET
);
854 spin_unlock_irqrestore(&dev
->lock
, flags
);
856 return req
? 0 : -EOPNOTSUPP
;
859 /*-------------------------------------------------------------------------*/
861 static void goku_clear_halt(struct goku_ep
*ep
)
863 // assert (ep->num !=0)
864 VDBG(ep
->dev
, "%s clear halt\n", ep
->ep
.name
);
865 command(ep
->dev
->regs
, COMMAND_SETDATA0
, ep
->num
);
866 command(ep
->dev
->regs
, COMMAND_STALL_CLEAR
, ep
->num
);
870 struct goku_request
*req
;
872 if (list_empty(&ep
->queue
))
874 req
= list_entry(ep
->queue
.next
, struct goku_request
,
876 (void) start_dma(ep
, req
);
882 static int goku_set_halt(struct usb_ep
*_ep
, int value
)
890 ep
= container_of (_ep
, struct goku_ep
, ep
);
894 ep
->dev
->ep0state
= EP0_STALL
;
895 ep
->dev
->ep
[0].stopped
= 1;
899 /* don't change EPxSTATUS_EP_INVALID to READY */
900 } else if (!ep
->desc
) {
901 DBG(ep
->dev
, "%s %s inactive?\n", __FUNCTION__
, ep
->ep
.name
);
905 spin_lock_irqsave(&ep
->dev
->lock
, flags
);
906 if (!list_empty(&ep
->queue
))
908 else if (ep
->is_in
&& value
909 /* data in (either) packet buffer? */
910 && (readl(&ep
->dev
->regs
->DataSet
)
911 & DATASET_AB(ep
->num
)))
917 VDBG(ep
->dev
, "%s set halt\n", ep
->ep
.name
);
918 command(ep
->dev
->regs
, COMMAND_STALL
, ep
->num
);
919 readl(ep
->reg_status
);
921 spin_unlock_irqrestore(&ep
->dev
->lock
, flags
);
925 static int goku_fifo_status(struct usb_ep
*_ep
)
928 struct goku_udc_regs __iomem
*regs
;
933 ep
= container_of(_ep
, struct goku_ep
, ep
);
935 /* size is only reported sanely for OUT */
939 /* ignores 16-byte dma buffer; SizeH == 0 */
940 regs
= ep
->dev
->regs
;
941 size
= readl(®s
->EPxSizeLA
[ep
->num
]) & DATASIZE
;
942 size
+= readl(®s
->EPxSizeLB
[ep
->num
]) & DATASIZE
;
943 VDBG(ep
->dev
, "%s %s %u\n", __FUNCTION__
, ep
->ep
.name
, size
);
947 static void goku_fifo_flush(struct usb_ep
*_ep
)
950 struct goku_udc_regs __iomem
*regs
;
955 ep
= container_of(_ep
, struct goku_ep
, ep
);
956 VDBG(ep
->dev
, "%s %s\n", __FUNCTION__
, ep
->ep
.name
);
958 /* don't change EPxSTATUS_EP_INVALID to READY */
959 if (!ep
->desc
&& ep
->num
!= 0) {
960 DBG(ep
->dev
, "%s %s inactive?\n", __FUNCTION__
, ep
->ep
.name
);
964 regs
= ep
->dev
->regs
;
965 size
= readl(®s
->EPxSizeLA
[ep
->num
]);
968 /* Non-desirable behavior: FIFO_CLEAR also clears the
969 * endpoint halt feature. For OUT, we _could_ just read
970 * the bytes out (PIO, if !ep->dma); for in, no choice.
973 command(regs
, COMMAND_FIFO_CLEAR
, ep
->num
);
976 static struct usb_ep_ops goku_ep_ops
= {
977 .enable
= goku_ep_enable
,
978 .disable
= goku_ep_disable
,
980 .alloc_request
= goku_alloc_request
,
981 .free_request
= goku_free_request
,
984 .dequeue
= goku_dequeue
,
986 .set_halt
= goku_set_halt
,
987 .fifo_status
= goku_fifo_status
,
988 .fifo_flush
= goku_fifo_flush
,
991 /*-------------------------------------------------------------------------*/
993 static int goku_get_frame(struct usb_gadget
*_gadget
)
998 static const struct usb_gadget_ops goku_ops
= {
999 .get_frame
= goku_get_frame
,
1004 /*-------------------------------------------------------------------------*/
1006 static inline char *dmastr(void)
1009 return "(dma disabled)";
1010 else if (use_dma
== 2)
1011 return "(dma IN and OUT)";
1016 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1018 static const char proc_node_name
[] = "driver/udc";
1020 #define FOURBITS "%s%s%s%s"
1021 #define EIGHTBITS FOURBITS FOURBITS
1024 dump_intmask(const char *label
, u32 mask
, char **next
, unsigned *size
)
1028 /* int_status is the same format ... */
1029 t
= scnprintf(*next
, *size
,
1030 "%s %05X =" FOURBITS EIGHTBITS EIGHTBITS
"\n",
1032 (mask
& INT_PWRDETECT
) ? " power" : "",
1033 (mask
& INT_SYSERROR
) ? " sys" : "",
1034 (mask
& INT_MSTRDEND
) ? " in-dma" : "",
1035 (mask
& INT_MSTWRTMOUT
) ? " wrtmo" : "",
1037 (mask
& INT_MSTWREND
) ? " out-dma" : "",
1038 (mask
& INT_MSTWRSET
) ? " wrset" : "",
1039 (mask
& INT_ERR
) ? " err" : "",
1040 (mask
& INT_SOF
) ? " sof" : "",
1042 (mask
& INT_EP3NAK
) ? " ep3nak" : "",
1043 (mask
& INT_EP2NAK
) ? " ep2nak" : "",
1044 (mask
& INT_EP1NAK
) ? " ep1nak" : "",
1045 (mask
& INT_EP3DATASET
) ? " ep3" : "",
1047 (mask
& INT_EP2DATASET
) ? " ep2" : "",
1048 (mask
& INT_EP1DATASET
) ? " ep1" : "",
1049 (mask
& INT_STATUSNAK
) ? " ep0snak" : "",
1050 (mask
& INT_STATUS
) ? " ep0status" : "",
1052 (mask
& INT_SETUP
) ? " setup" : "",
1053 (mask
& INT_ENDPOINT0
) ? " ep0" : "",
1054 (mask
& INT_USBRESET
) ? " reset" : "",
1055 (mask
& INT_SUSPEND
) ? " suspend" : "");
1062 udc_proc_read(char *buffer
, char **start
, off_t off
, int count
,
1063 int *eof
, void *_dev
)
1066 struct goku_udc
*dev
= _dev
;
1067 struct goku_udc_regs __iomem
*regs
= dev
->regs
;
1069 unsigned size
= count
;
1070 unsigned long flags
;
1071 int i
, t
, is_usb_connected
;
1077 local_irq_save(flags
);
1079 /* basic device status */
1080 tmp
= readl(®s
->power_detect
);
1081 is_usb_connected
= tmp
& PW_DETECT
;
1082 t
= scnprintf(next
, size
,
1084 "%s version: %s %s\n"
1085 "Gadget driver: %s\n"
1088 pci_name(dev
->pdev
), driver_desc
,
1089 driver_name
, DRIVER_VERSION
, dmastr(),
1090 dev
->driver
? dev
->driver
->driver
.name
: "(none)",
1092 ? ((tmp
& PW_PULLUP
) ? "full speed" : "powered")
1095 switch(dev
->ep0state
){
1096 case EP0_DISCONNECT
: state
= "ep0_disconnect"; break;
1097 case EP0_IDLE
: state
= "ep0_idle"; break;
1098 case EP0_IN
: state
= "ep0_in"; break;
1099 case EP0_OUT
: state
= "ep0_out"; break;
1100 case EP0_STATUS
: state
= "ep0_status"; break;
1101 case EP0_STALL
: state
= "ep0_stall"; break;
1102 case EP0_SUSPEND
: state
= "ep0_suspend"; break;
1103 default: state
= "ep0_?"; break;
1109 dump_intmask("int_status", readl(®s
->int_status
), &next
, &size
);
1110 dump_intmask("int_enable", readl(®s
->int_enable
), &next
, &size
);
1112 if (!is_usb_connected
|| !dev
->driver
|| (tmp
& PW_PULLUP
) == 0)
1115 /* registers for (active) device and ep0 */
1116 t
= scnprintf(next
, size
, "\nirqs %lu\ndataset %02x "
1117 "single.bcs %02x.%02x state %x addr %u\n",
1118 dev
->irqs
, readl(®s
->DataSet
),
1119 readl(®s
->EPxSingle
), readl(®s
->EPxBCS
),
1120 readl(®s
->UsbState
),
1121 readl(®s
->address
));
1125 tmp
= readl(®s
->dma_master
);
1126 t
= scnprintf(next
, size
,
1127 "dma %03X =" EIGHTBITS
"%s %s\n", tmp
,
1128 (tmp
& MST_EOPB_DIS
) ? " eopb-" : "",
1129 (tmp
& MST_EOPB_ENA
) ? " eopb+" : "",
1130 (tmp
& MST_TIMEOUT_DIS
) ? " tmo-" : "",
1131 (tmp
& MST_TIMEOUT_ENA
) ? " tmo+" : "",
1133 (tmp
& MST_RD_EOPB
) ? " eopb" : "",
1134 (tmp
& MST_RD_RESET
) ? " in_reset" : "",
1135 (tmp
& MST_WR_RESET
) ? " out_reset" : "",
1136 (tmp
& MST_RD_ENA
) ? " IN" : "",
1138 (tmp
& MST_WR_ENA
) ? " OUT" : "",
1139 (tmp
& MST_CONNECTION
)
1145 /* dump endpoint queues */
1146 for (i
= 0; i
< 4; i
++) {
1147 struct goku_ep
*ep
= &dev
->ep
[i
];
1148 struct goku_request
*req
;
1153 tmp
= readl(ep
->reg_status
);
1154 t
= scnprintf(next
, size
,
1155 "%s %s max %u %s, irqs %lu, "
1156 "status %02x (%s) " FOURBITS
"\n",
1158 ep
->is_in
? "in" : "out",
1160 ep
->dma
? "dma" : "pio",
1163 switch (tmp
& EPxSTATUS_EP_MASK
) {
1164 case EPxSTATUS_EP_READY
:
1166 case EPxSTATUS_EP_DATAIN
:
1167 s
= "packet"; break;
1168 case EPxSTATUS_EP_FULL
:
1170 case EPxSTATUS_EP_TX_ERR
: // host will retry
1171 s
= "tx_err"; break;
1172 case EPxSTATUS_EP_RX_ERR
:
1173 s
= "rx_err"; break;
1174 case EPxSTATUS_EP_BUSY
: /* ep0 only */
1176 case EPxSTATUS_EP_STALL
:
1178 case EPxSTATUS_EP_INVALID
: // these "can't happen"
1179 s
= "invalid"; break;
1183 (tmp
& EPxSTATUS_TOGGLE
) ? "data1" : "data0",
1184 (tmp
& EPxSTATUS_SUSPEND
) ? " suspend" : "",
1185 (tmp
& EPxSTATUS_FIFO_DISABLE
) ? " disable" : "",
1186 (tmp
& EPxSTATUS_STAGE_ERROR
) ? " ep0stat" : ""
1188 if (t
<= 0 || t
> size
)
1193 if (list_empty(&ep
->queue
)) {
1194 t
= scnprintf(next
, size
, "\t(nothing queued)\n");
1195 if (t
<= 0 || t
> size
)
1201 list_for_each_entry(req
, &ep
->queue
, queue
) {
1202 if (ep
->dma
&& req
->queue
.prev
== &ep
->queue
) {
1203 if (i
== UDC_MSTRD_ENDPOINT
)
1204 tmp
= readl(®s
->in_dma_current
);
1206 tmp
= readl(®s
->out_dma_current
);
1207 tmp
-= req
->req
.dma
;
1210 tmp
= req
->req
.actual
;
1212 t
= scnprintf(next
, size
,
1213 "\treq %p len %u/%u buf %p\n",
1214 &req
->req
, tmp
, req
->req
.length
,
1216 if (t
<= 0 || t
> size
)
1224 local_irq_restore(flags
);
1226 return count
- size
;
1229 #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
1231 /*-------------------------------------------------------------------------*/
1233 static void udc_reinit (struct goku_udc
*dev
)
1235 static char *names
[] = { "ep0", "ep1-bulk", "ep2-bulk", "ep3-bulk" };
1239 INIT_LIST_HEAD (&dev
->gadget
.ep_list
);
1240 dev
->gadget
.ep0
= &dev
->ep
[0].ep
;
1241 dev
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1242 dev
->ep0state
= EP0_DISCONNECT
;
1245 for (i
= 0; i
< 4; i
++) {
1246 struct goku_ep
*ep
= &dev
->ep
[i
];
1249 ep
->ep
.name
= names
[i
];
1250 ep
->reg_fifo
= &dev
->regs
->ep_fifo
[i
];
1251 ep
->reg_status
= &dev
->regs
->ep_status
[i
];
1252 ep
->reg_mode
= &dev
->regs
->ep_mode
[i
];
1254 ep
->ep
.ops
= &goku_ep_ops
;
1255 list_add_tail (&ep
->ep
.ep_list
, &dev
->gadget
.ep_list
);
1257 INIT_LIST_HEAD (&ep
->queue
);
1262 dev
->ep
[0].reg_mode
= NULL
;
1263 dev
->ep
[0].ep
.maxpacket
= MAX_EP0_SIZE
;
1264 list_del_init (&dev
->ep
[0].ep
.ep_list
);
1267 static void udc_reset(struct goku_udc
*dev
)
1269 struct goku_udc_regs __iomem
*regs
= dev
->regs
;
1271 writel(0, ®s
->power_detect
);
1272 writel(0, ®s
->int_enable
);
1273 readl(®s
->int_enable
);
1274 dev
->int_enable
= 0;
1276 /* deassert reset, leave USB D+ at hi-Z (no pullup)
1277 * don't let INT_PWRDETECT sequence begin
1280 writel(PW_RESETB
, ®s
->power_detect
);
1281 readl(®s
->int_enable
);
1284 static void ep0_start(struct goku_udc
*dev
)
1286 struct goku_udc_regs __iomem
*regs
= dev
->regs
;
1289 VDBG(dev
, "%s\n", __FUNCTION__
);
1293 //writel(MST_EOPB_ENA | MST_TIMEOUT_ENA, ®s->dma_master);
1295 /* hw handles set_address, set_feature, get_status; maybe more */
1296 writel( G_REQMODE_SET_INTF
| G_REQMODE_GET_INTF
1297 | G_REQMODE_SET_CONF
| G_REQMODE_GET_CONF
1298 | G_REQMODE_GET_DESC
1299 | G_REQMODE_CLEAR_FEAT
1302 for (i
= 0; i
< 4; i
++)
1303 dev
->ep
[i
].irqs
= 0;
1305 /* can't modify descriptors after writing UsbReady */
1306 for (i
= 0; i
< DESC_LEN
; i
++)
1307 writel(0, ®s
->descriptors
[i
]);
1308 writel(0, ®s
->UsbReady
);
1310 /* expect ep0 requests when the host drops reset */
1311 writel(PW_RESETB
| PW_PULLUP
, ®s
->power_detect
);
1312 dev
->int_enable
= INT_DEVWIDE
| INT_EP0
;
1313 writel(dev
->int_enable
, &dev
->regs
->int_enable
);
1314 readl(®s
->int_enable
);
1315 dev
->gadget
.speed
= USB_SPEED_FULL
;
1316 dev
->ep0state
= EP0_IDLE
;
1319 static void udc_enable(struct goku_udc
*dev
)
1321 /* start enumeration now, or after power detect irq */
1322 if (readl(&dev
->regs
->power_detect
) & PW_DETECT
)
1325 DBG(dev
, "%s\n", __FUNCTION__
);
1326 dev
->int_enable
= INT_PWRDETECT
;
1327 writel(dev
->int_enable
, &dev
->regs
->int_enable
);
1331 /*-------------------------------------------------------------------------*/
1333 /* keeping it simple:
1334 * - one bus driver, initted first;
1335 * - one function driver, initted second
1338 static struct goku_udc
*the_controller
;
1340 /* when a driver is successfully registered, it will receive
1341 * control requests including set_configuration(), which enables
1342 * non-control requests. then usb traffic follows until a
1343 * disconnect is reported. then a host may connect again, or
1344 * the driver might get unbound.
1346 int usb_gadget_register_driver(struct usb_gadget_driver
*driver
)
1348 struct goku_udc
*dev
= the_controller
;
1352 || driver
->speed
!= USB_SPEED_FULL
1354 || !driver
->disconnect
1362 /* hook up the driver */
1363 driver
->driver
.bus
= NULL
;
1364 dev
->driver
= driver
;
1365 dev
->gadget
.dev
.driver
= &driver
->driver
;
1366 retval
= driver
->bind(&dev
->gadget
);
1368 DBG(dev
, "bind to driver %s --> error %d\n",
1369 driver
->driver
.name
, retval
);
1371 dev
->gadget
.dev
.driver
= NULL
;
1375 /* then enable host detection and ep0; and we're ready
1376 * for set_configuration as well as eventual disconnect.
1380 DBG(dev
, "registered gadget driver '%s'\n", driver
->driver
.name
);
1383 EXPORT_SYMBOL(usb_gadget_register_driver
);
1386 stop_activity(struct goku_udc
*dev
, struct usb_gadget_driver
*driver
)
1390 DBG (dev
, "%s\n", __FUNCTION__
);
1392 if (dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)
1395 /* disconnect gadget driver after quiesceing hw and the driver */
1397 for (i
= 0; i
< 4; i
++)
1398 nuke(&dev
->ep
[i
], -ESHUTDOWN
);
1400 spin_unlock(&dev
->lock
);
1401 driver
->disconnect(&dev
->gadget
);
1402 spin_lock(&dev
->lock
);
1409 int usb_gadget_unregister_driver(struct usb_gadget_driver
*driver
)
1411 struct goku_udc
*dev
= the_controller
;
1412 unsigned long flags
;
1416 if (!driver
|| driver
!= dev
->driver
|| !driver
->unbind
)
1419 spin_lock_irqsave(&dev
->lock
, flags
);
1421 stop_activity(dev
, driver
);
1422 spin_unlock_irqrestore(&dev
->lock
, flags
);
1424 driver
->unbind(&dev
->gadget
);
1426 DBG(dev
, "unregistered driver '%s'\n", driver
->driver
.name
);
1429 EXPORT_SYMBOL(usb_gadget_unregister_driver
);
1432 /*-------------------------------------------------------------------------*/
1434 static void ep0_setup(struct goku_udc
*dev
)
1436 struct goku_udc_regs __iomem
*regs
= dev
->regs
;
1437 struct usb_ctrlrequest ctrl
;
1440 /* read SETUP packet and enter DATA stage */
1441 ctrl
.bRequestType
= readl(®s
->bRequestType
);
1442 ctrl
.bRequest
= readl(®s
->bRequest
);
1443 ctrl
.wValue
= cpu_to_le16((readl(®s
->wValueH
) << 8)
1444 | readl(®s
->wValueL
));
1445 ctrl
.wIndex
= cpu_to_le16((readl(®s
->wIndexH
) << 8)
1446 | readl(®s
->wIndexL
));
1447 ctrl
.wLength
= cpu_to_le16((readl(®s
->wLengthH
) << 8)
1448 | readl(®s
->wLengthL
));
1449 writel(0, ®s
->SetupRecv
);
1451 nuke(&dev
->ep
[0], 0);
1452 dev
->ep
[0].stopped
= 0;
1453 if (likely(ctrl
.bRequestType
& USB_DIR_IN
)) {
1454 dev
->ep
[0].is_in
= 1;
1455 dev
->ep0state
= EP0_IN
;
1456 /* detect early status stages */
1457 writel(ICONTROL_STATUSNAK
, &dev
->regs
->IntControl
);
1459 dev
->ep
[0].is_in
= 0;
1460 dev
->ep0state
= EP0_OUT
;
1462 /* NOTE: CLEAR_FEATURE is done in software so that we can
1463 * synchronize transfer restarts after bulk IN stalls. data
1464 * won't even enter the fifo until the halt is cleared.
1466 switch (ctrl
.bRequest
) {
1467 case USB_REQ_CLEAR_FEATURE
:
1468 switch (ctrl
.bRequestType
) {
1469 case USB_RECIP_ENDPOINT
:
1470 tmp
= le16_to_cpu(ctrl
.wIndex
) & 0x0f;
1471 /* active endpoint */
1472 if (tmp
> 3 || (!dev
->ep
[tmp
].desc
&& tmp
!= 0))
1474 if (ctrl
.wIndex
& __constant_cpu_to_le16(
1476 if (!dev
->ep
[tmp
].is_in
)
1479 if (dev
->ep
[tmp
].is_in
)
1482 if (ctrl
.wValue
!= __constant_cpu_to_le16(
1486 goku_clear_halt(&dev
->ep
[tmp
]);
1488 /* start ep0out status stage */
1489 writel(~(1<<0), ®s
->EOP
);
1490 dev
->ep
[0].stopped
= 1;
1491 dev
->ep0state
= EP0_STATUS
;
1493 case USB_RECIP_DEVICE
:
1494 /* device remote wakeup: always clear */
1495 if (ctrl
.wValue
!= __constant_cpu_to_le16(1))
1497 VDBG(dev
, "clear dev remote wakeup\n");
1499 case USB_RECIP_INTERFACE
:
1501 default: /* pass to gadget driver */
1511 VDBG(dev
, "SETUP %02x.%02x v%04x i%04x l%04x\n",
1512 ctrl
.bRequestType
, ctrl
.bRequest
,
1513 le16_to_cpu(ctrl
.wValue
), le16_to_cpu(ctrl
.wIndex
),
1514 le16_to_cpu(ctrl
.wLength
));
1517 /* hw wants to know when we're configured (or not) */
1518 dev
->req_config
= (ctrl
.bRequest
== USB_REQ_SET_CONFIGURATION
1519 && ctrl
.bRequestType
== USB_RECIP_DEVICE
);
1520 if (unlikely(dev
->req_config
))
1521 dev
->configured
= (ctrl
.wValue
!= __constant_cpu_to_le16(0));
1523 /* delegate everything to the gadget driver.
1524 * it may respond after this irq handler returns.
1526 spin_unlock (&dev
->lock
);
1527 tmp
= dev
->driver
->setup(&dev
->gadget
, &ctrl
);
1528 spin_lock (&dev
->lock
);
1529 if (unlikely(tmp
< 0)) {
1532 VDBG(dev
, "req %02x.%02x protocol STALL; err %d\n",
1533 ctrl
.bRequestType
, ctrl
.bRequest
, tmp
);
1535 command(regs
, COMMAND_STALL
, 0);
1536 dev
->ep
[0].stopped
= 1;
1537 dev
->ep0state
= EP0_STALL
;
1540 /* expect at least one data or status stage irq */
1543 #define ACK(irqbit) { \
1545 writel(~irqbit, ®s->int_status); \
1549 static irqreturn_t
goku_irq(int irq
, void *_dev
)
1551 struct goku_udc
*dev
= _dev
;
1552 struct goku_udc_regs __iomem
*regs
= dev
->regs
;
1554 u32 stat
, handled
= 0;
1555 unsigned i
, rescans
= 5;
1557 spin_lock(&dev
->lock
);
1560 stat
= readl(®s
->int_status
) & dev
->int_enable
;
1565 /* device-wide irqs */
1566 if (unlikely(stat
& INT_DEVWIDE
)) {
1567 if (stat
& INT_SYSERROR
) {
1568 ERROR(dev
, "system error\n");
1569 stop_activity(dev
, dev
->driver
);
1572 // FIXME have a neater way to prevent re-enumeration
1576 if (stat
& INT_PWRDETECT
) {
1577 writel(~stat
, ®s
->int_status
);
1578 if (readl(&dev
->regs
->power_detect
) & PW_DETECT
) {
1579 VDBG(dev
, "connect\n");
1582 DBG(dev
, "disconnect\n");
1583 if (dev
->gadget
.speed
== USB_SPEED_FULL
)
1584 stop_activity(dev
, dev
->driver
);
1585 dev
->ep0state
= EP0_DISCONNECT
;
1586 dev
->int_enable
= INT_DEVWIDE
;
1587 writel(dev
->int_enable
, &dev
->regs
->int_enable
);
1593 if (stat
& INT_SUSPEND
) {
1595 if (readl(®s
->ep_status
[0]) & EPxSTATUS_SUSPEND
) {
1596 switch (dev
->ep0state
) {
1597 case EP0_DISCONNECT
:
1603 DBG(dev
, "USB suspend\n");
1604 dev
->ep0state
= EP0_SUSPEND
;
1605 if (dev
->gadget
.speed
!= USB_SPEED_UNKNOWN
1607 && dev
->driver
->suspend
) {
1608 spin_unlock(&dev
->lock
);
1609 dev
->driver
->suspend(&dev
->gadget
);
1610 spin_lock(&dev
->lock
);
1613 if (dev
->ep0state
!= EP0_SUSPEND
) {
1614 DBG(dev
, "bogus USB resume %d\n",
1618 DBG(dev
, "USB resume\n");
1619 dev
->ep0state
= EP0_IDLE
;
1620 if (dev
->gadget
.speed
!= USB_SPEED_UNKNOWN
1622 && dev
->driver
->resume
) {
1623 spin_unlock(&dev
->lock
);
1624 dev
->driver
->resume(&dev
->gadget
);
1625 spin_lock(&dev
->lock
);
1630 if (stat
& INT_USBRESET
) { /* hub reset done */
1632 INFO(dev
, "USB reset done, gadget %s\n",
1633 dev
->driver
->driver
.name
);
1635 // and INT_ERR on some endpoint's crc/bitstuff/... problem
1638 /* progress ep0 setup, data, or status stages.
1639 * no transition {EP0_STATUS, EP0_STALL} --> EP0_IDLE; saves irqs
1641 if (stat
& INT_SETUP
) {
1646 if (stat
& INT_STATUSNAK
) {
1647 ACK(INT_STATUSNAK
|INT_ENDPOINT0
);
1648 if (dev
->ep0state
== EP0_IN
) {
1652 writel(~(1<<0), ®s
->EOP
);
1653 dev
->ep0state
= EP0_STATUS
;
1656 if (stat
& INT_ENDPOINT0
) {
1663 /* dma completion */
1664 if (stat
& INT_MSTRDEND
) { /* IN */
1666 ep
= &dev
->ep
[UDC_MSTRD_ENDPOINT
];
1668 dma_advance(dev
, ep
);
1670 if (stat
& INT_MSTWREND
) { /* OUT */
1672 ep
= &dev
->ep
[UDC_MSTWR_ENDPOINT
];
1674 dma_advance(dev
, ep
);
1676 if (stat
& INT_MSTWRTMOUT
) { /* OUT */
1677 ACK(INT_MSTWRTMOUT
);
1678 ep
= &dev
->ep
[UDC_MSTWR_ENDPOINT
];
1680 ERROR(dev
, "%s write timeout ?\n", ep
->ep
.name
);
1681 // reset dma? then dma_advance()
1685 for (i
= 1; i
< 4; i
++) {
1686 u32 tmp
= INT_EPxDATASET(i
);
1692 if (list_empty (&ep
->queue
))
1693 pio_irq_disable(dev
, regs
, i
);
1703 (void)readl(®s
->int_enable
);
1704 spin_unlock(&dev
->lock
);
1706 DBG(dev
, "unhandled irq status: %05x (%05x, %05x)\n", stat
,
1707 readl(®s
->int_status
), dev
->int_enable
);
1708 return IRQ_RETVAL(handled
);
1713 /*-------------------------------------------------------------------------*/
1715 static void gadget_release(struct device
*_dev
)
1717 struct goku_udc
*dev
= dev_get_drvdata(_dev
);
1722 /* tear down the binding between this driver and the pci device */
1724 static void goku_remove(struct pci_dev
*pdev
)
1726 struct goku_udc
*dev
= pci_get_drvdata(pdev
);
1728 DBG(dev
, "%s\n", __FUNCTION__
);
1730 BUG_ON(dev
->driver
);
1732 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1733 remove_proc_entry(proc_node_name
, NULL
);
1738 free_irq(pdev
->irq
, dev
);
1741 if (dev
->got_region
)
1742 release_mem_region(pci_resource_start (pdev
, 0),
1743 pci_resource_len (pdev
, 0));
1745 pci_disable_device(pdev
);
1746 device_unregister(&dev
->gadget
.dev
);
1748 pci_set_drvdata(pdev
, NULL
);
1750 the_controller
= NULL
;
1752 INFO(dev
, "unbind\n");
1755 /* wrap this driver around the specified pci device, but
1756 * don't respond over USB until a gadget driver binds to us.
1759 static int goku_probe(struct pci_dev
*pdev
, const struct pci_device_id
*id
)
1761 struct goku_udc
*dev
= NULL
;
1762 unsigned long resource
, len
;
1763 void __iomem
*base
= NULL
;
1766 /* if you want to support more than one controller in a system,
1767 * usb_gadget_driver_{register,unregister}() must change.
1769 if (the_controller
) {
1770 WARN(dev
, "ignoring %s\n", pci_name(pdev
));
1774 printk(KERN_ERR
"Check PCI %s IRQ setup!\n", pci_name(pdev
));
1779 /* alloc, and start init */
1780 dev
= kzalloc (sizeof *dev
, GFP_KERNEL
);
1782 pr_debug("enomem %s\n", pci_name(pdev
));
1787 spin_lock_init(&dev
->lock
);
1789 dev
->gadget
.ops
= &goku_ops
;
1791 /* the "gadget" abstracts/virtualizes the controller */
1792 strcpy(dev
->gadget
.dev
.bus_id
, "gadget");
1793 dev
->gadget
.dev
.parent
= &pdev
->dev
;
1794 dev
->gadget
.dev
.dma_mask
= pdev
->dev
.dma_mask
;
1795 dev
->gadget
.dev
.release
= gadget_release
;
1796 dev
->gadget
.name
= driver_name
;
1798 /* now all the pci goodies ... */
1799 retval
= pci_enable_device(pdev
);
1801 DBG(dev
, "can't enable, %d\n", retval
);
1806 resource
= pci_resource_start(pdev
, 0);
1807 len
= pci_resource_len(pdev
, 0);
1808 if (!request_mem_region(resource
, len
, driver_name
)) {
1809 DBG(dev
, "controller already in use\n");
1813 dev
->got_region
= 1;
1815 base
= ioremap_nocache(resource
, len
);
1817 DBG(dev
, "can't map memory\n");
1821 dev
->regs
= (struct goku_udc_regs __iomem
*) base
;
1823 pci_set_drvdata(pdev
, dev
);
1824 INFO(dev
, "%s\n", driver_desc
);
1825 INFO(dev
, "version: " DRIVER_VERSION
" %s\n", dmastr());
1826 INFO(dev
, "irq %d, pci mem %p\n", pdev
->irq
, base
);
1828 /* init to known state, then setup irqs */
1831 if (request_irq(pdev
->irq
, goku_irq
, IRQF_SHARED
/*|IRQF_SAMPLE_RANDOM*/,
1832 driver_name
, dev
) != 0) {
1833 DBG(dev
, "request interrupt %d failed\n", pdev
->irq
);
1839 pci_set_master(pdev
);
1842 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1843 create_proc_read_entry(proc_node_name
, 0, NULL
, udc_proc_read
, dev
);
1847 the_controller
= dev
;
1848 retval
= device_register(&dev
->gadget
.dev
);
1859 /*-------------------------------------------------------------------------*/
1861 static struct pci_device_id pci_ids
[] = { {
1862 .class = ((PCI_CLASS_SERIAL_USB
<< 8) | 0xfe),
1864 .vendor
= 0x102f, /* Toshiba */
1865 .device
= 0x0107, /* this UDC */
1866 .subvendor
= PCI_ANY_ID
,
1867 .subdevice
= PCI_ANY_ID
,
1869 }, { /* end: all zeroes */ }
1871 MODULE_DEVICE_TABLE (pci
, pci_ids
);
1873 static struct pci_driver goku_pci_driver
= {
1874 .name
= (char *) driver_name
,
1875 .id_table
= pci_ids
,
1877 .probe
= goku_probe
,
1878 .remove
= goku_remove
,
1880 /* FIXME add power management support */
1883 static int __init
init (void)
1885 return pci_register_driver (&goku_pci_driver
);
1889 static void __exit
cleanup (void)
1891 pci_unregister_driver (&goku_pci_driver
);
1893 module_exit (cleanup
);