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/timer.h>
34 #include <linux/list.h>
35 #include <linux/interrupt.h>
36 #include <linux/proc_fs.h>
37 #include <linux/seq_file.h>
38 #include <linux/device.h>
39 #include <linux/usb/ch9.h>
40 #include <linux/usb/gadget.h>
41 #include <linux/prefetch.h>
43 #include <asm/byteorder.h>
46 #include <asm/unaligned.h>
51 #define DRIVER_DESC "TC86C001 USB Device Controller"
52 #define DRIVER_VERSION "30-Oct 2003"
54 static const char driver_name
[] = "goku_udc";
55 static const char driver_desc
[] = DRIVER_DESC
;
57 MODULE_AUTHOR("source@mvista.com");
58 MODULE_DESCRIPTION(DRIVER_DESC
);
59 MODULE_LICENSE("GPL");
63 * IN dma behaves ok under testing, though the IN-dma abort paths don't
64 * seem to behave quite as expected. Used by default.
66 * OUT dma documents design problems handling the common "short packet"
67 * transfer termination policy; it couldn't be enabled by default, even
68 * if the OUT-dma abort problems had a resolution.
70 static unsigned use_dma
= 1;
73 //#include <linux/moduleparam.h>
74 /* "modprobe goku_udc use_dma=1" etc
76 * 1 to use IN dma only (normal operation)
77 * 2 to use IN and OUT dma
79 module_param(use_dma
, uint
, S_IRUGO
);
82 /*-------------------------------------------------------------------------*/
84 static void nuke(struct goku_ep
*, int status
);
87 command(struct goku_udc_regs __iomem
*regs
, int command
, unsigned epnum
)
89 writel(COMMAND_EP(epnum
) | command
, ®s
->Command
);
94 goku_ep_enable(struct usb_ep
*_ep
, const struct usb_endpoint_descriptor
*desc
)
102 ep
= container_of(_ep
, struct goku_ep
, ep
);
104 || desc
->bDescriptorType
!= USB_DT_ENDPOINT
)
107 if (ep
== &dev
->ep
[0])
109 if (!dev
->driver
|| dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)
111 if (ep
->num
!= usb_endpoint_num(desc
))
114 switch (usb_endpoint_type(desc
)) {
115 case USB_ENDPOINT_XFER_BULK
:
116 case USB_ENDPOINT_XFER_INT
:
122 if ((readl(ep
->reg_status
) & EPxSTATUS_EP_MASK
)
123 != EPxSTATUS_EP_INVALID
)
126 /* enabling the no-toggle interrupt mode would need an api hook */
128 max
= get_unaligned_le16(&desc
->wMaxPacketSize
);
138 mode
|= 2 << 1; /* bulk, or intr-with-toggle */
140 /* ep1/ep2 dma direction is chosen early; it works in the other
141 * direction, with pio. be cautious with out-dma.
143 ep
->is_in
= usb_endpoint_dir_in(desc
);
146 ep
->dma
= (use_dma
!= 0) && (ep
->num
== UDC_MSTRD_ENDPOINT
);
148 ep
->dma
= (use_dma
== 2) && (ep
->num
== UDC_MSTWR_ENDPOINT
);
150 DBG(dev
, "%s out-dma hides short packets\n",
154 spin_lock_irqsave(&ep
->dev
->lock
, flags
);
156 /* ep1 and ep2 can do double buffering and/or dma */
158 struct goku_udc_regs __iomem
*regs
= ep
->dev
->regs
;
161 /* double buffer except (for now) with pio in */
162 tmp
= ((ep
->dma
|| !ep
->is_in
)
163 ? 0x10 /* double buffered */
164 : 0x11 /* single buffer */
166 tmp
|= readl(®s
->EPxSingle
);
167 writel(tmp
, ®s
->EPxSingle
);
169 tmp
= (ep
->dma
? 0x10/*dma*/ : 0x11/*pio*/) << ep
->num
;
170 tmp
|= readl(®s
->EPxBCS
);
171 writel(tmp
, ®s
->EPxBCS
);
173 writel(mode
, ep
->reg_mode
);
174 command(ep
->dev
->regs
, COMMAND_RESET
, ep
->num
);
175 ep
->ep
.maxpacket
= max
;
178 spin_unlock_irqrestore(&ep
->dev
->lock
, flags
);
180 DBG(dev
, "enable %s %s %s maxpacket %u\n", ep
->ep
.name
,
181 ep
->is_in
? "IN" : "OUT",
182 ep
->dma
? "dma" : "pio",
188 static void ep_reset(struct goku_udc_regs __iomem
*regs
, struct goku_ep
*ep
)
190 struct goku_udc
*dev
= ep
->dev
;
193 command(regs
, COMMAND_INVALID
, ep
->num
);
195 if (ep
->num
== UDC_MSTWR_ENDPOINT
)
196 dev
->int_enable
&= ~(INT_MSTWREND
198 else if (ep
->num
== UDC_MSTRD_ENDPOINT
)
199 dev
->int_enable
&= ~INT_MSTRDEND
;
200 dev
->int_enable
&= ~INT_EPxDATASET (ep
->num
);
202 dev
->int_enable
&= ~INT_EP0
;
203 writel(dev
->int_enable
, ®s
->int_enable
);
204 readl(®s
->int_enable
);
206 struct goku_udc_regs __iomem
*r
= ep
->dev
->regs
;
209 tmp
= readl(&r
->EPxSingle
);
210 tmp
&= ~(0x11 << ep
->num
);
211 writel(tmp
, &r
->EPxSingle
);
213 tmp
= readl(&r
->EPxBCS
);
214 tmp
&= ~(0x11 << ep
->num
);
215 writel(tmp
, &r
->EPxBCS
);
217 /* reset dma in case we're still using it */
221 master
= readl(®s
->dma_master
) & MST_RW_BITS
;
222 if (ep
->num
== UDC_MSTWR_ENDPOINT
) {
223 master
&= ~MST_W_BITS
;
224 master
|= MST_WR_RESET
;
226 master
&= ~MST_R_BITS
;
227 master
|= MST_RD_RESET
;
229 writel(master
, ®s
->dma_master
);
233 usb_ep_set_maxpacket_limit(&ep
->ep
, MAX_FIFO_SIZE
);
240 static int goku_ep_disable(struct usb_ep
*_ep
)
243 struct goku_udc
*dev
;
246 ep
= container_of(_ep
, struct goku_ep
, ep
);
247 if (!_ep
|| !ep
->ep
.desc
)
250 if (dev
->ep0state
== EP0_SUSPEND
)
253 VDBG(dev
, "disable %s\n", _ep
->name
);
255 spin_lock_irqsave(&dev
->lock
, flags
);
256 nuke(ep
, -ESHUTDOWN
);
257 ep_reset(dev
->regs
, ep
);
258 spin_unlock_irqrestore(&dev
->lock
, flags
);
263 /*-------------------------------------------------------------------------*/
265 static struct usb_request
*
266 goku_alloc_request(struct usb_ep
*_ep
, gfp_t gfp_flags
)
268 struct goku_request
*req
;
272 req
= kzalloc(sizeof *req
, gfp_flags
);
276 INIT_LIST_HEAD(&req
->queue
);
281 goku_free_request(struct usb_ep
*_ep
, struct usb_request
*_req
)
283 struct goku_request
*req
;
288 req
= container_of(_req
, struct goku_request
, req
);
289 WARN_ON(!list_empty(&req
->queue
));
293 /*-------------------------------------------------------------------------*/
296 done(struct goku_ep
*ep
, struct goku_request
*req
, int status
)
298 struct goku_udc
*dev
;
299 unsigned stopped
= ep
->stopped
;
301 list_del_init(&req
->queue
);
303 if (likely(req
->req
.status
== -EINPROGRESS
))
304 req
->req
.status
= status
;
306 status
= req
->req
.status
;
311 usb_gadget_unmap_request(&dev
->gadget
, &req
->req
, ep
->is_in
);
314 if (status
&& status
!= -ESHUTDOWN
)
316 VDBG(dev
, "complete %s req %p stat %d len %u/%u\n",
317 ep
->ep
.name
, &req
->req
, status
,
318 req
->req
.actual
, req
->req
.length
);
320 /* don't modify queue heads during completion callback */
322 spin_unlock(&dev
->lock
);
323 req
->req
.complete(&ep
->ep
, &req
->req
);
324 spin_lock(&dev
->lock
);
325 ep
->stopped
= stopped
;
328 /*-------------------------------------------------------------------------*/
331 write_packet(u32 __iomem
*fifo
, u8
*buf
, struct goku_request
*req
, unsigned max
)
333 unsigned length
, count
;
335 length
= min(req
->req
.length
- req
->req
.actual
, max
);
336 req
->req
.actual
+= length
;
339 while (likely(count
--))
340 writel(*buf
++, fifo
);
344 // return: 0 = still running, 1 = completed, negative = errno
345 static int write_fifo(struct goku_ep
*ep
, struct goku_request
*req
)
347 struct goku_udc
*dev
= ep
->dev
;
353 tmp
= readl(&dev
->regs
->DataSet
);
354 buf
= req
->req
.buf
+ req
->req
.actual
;
358 if (unlikely(ep
->num
== 0 && dev
->ep0state
!= EP0_IN
))
361 /* NOTE: just single-buffered PIO-IN for now. */
362 if (unlikely((tmp
& DATASET_A(ep
->num
)) != 0))
365 /* clear our "packet available" irq */
367 writel(~INT_EPxDATASET(ep
->num
), &dev
->regs
->int_status
);
369 count
= write_packet(ep
->reg_fifo
, buf
, req
, ep
->ep
.maxpacket
);
371 /* last packet often short (sometimes a zlp, especially on ep0) */
372 if (unlikely(count
!= ep
->ep
.maxpacket
)) {
373 writel(~(1<<ep
->num
), &dev
->regs
->EOP
);
375 dev
->ep
[0].stopped
= 1;
376 dev
->ep0state
= EP0_STATUS
;
380 if (likely(req
->req
.length
!= req
->req
.actual
)
386 #if 0 /* printk seemed to trash is_last...*/
388 VDBG(dev
, "wrote %s %u bytes%s IN %u left %p\n",
389 ep
->ep
.name
, count
, is_last
? "/last" : "",
390 req
->req
.length
- req
->req
.actual
, req
);
393 /* requests complete when all IN data is in the FIFO,
394 * or sometimes later, if a zlp was needed.
404 static int read_fifo(struct goku_ep
*ep
, struct goku_request
*req
)
406 struct goku_udc_regs __iomem
*regs
;
409 unsigned bufferspace
, is_short
, dbuff
;
411 regs
= ep
->dev
->regs
;
413 buf
= req
->req
.buf
+ req
->req
.actual
;
416 if (unlikely(ep
->num
== 0 && ep
->dev
->ep0state
!= EP0_OUT
))
419 dbuff
= (ep
->num
== 1 || ep
->num
== 2);
421 /* ack dataset irq matching the status we'll handle */
423 writel(~INT_EPxDATASET(ep
->num
), ®s
->int_status
);
425 set
= readl(®s
->DataSet
) & DATASET_AB(ep
->num
);
426 size
= readl(®s
->EPxSizeLA
[ep
->num
]);
427 bufferspace
= req
->req
.length
- req
->req
.actual
;
429 /* usually do nothing without an OUT packet */
430 if (likely(ep
->num
!= 0 || bufferspace
!= 0)) {
431 if (unlikely(set
== 0))
433 /* use ep1/ep2 double-buffering for OUT */
434 if (!(size
& PACKET_ACTIVE
))
435 size
= readl(®s
->EPxSizeLB
[ep
->num
]);
436 if (!(size
& PACKET_ACTIVE
)) /* "can't happen" */
438 size
&= DATASIZE
; /* EPxSizeH == 0 */
440 /* ep0out no-out-data case for set_config, etc */
444 /* read all bytes from this packet */
445 req
->req
.actual
+= size
;
446 is_short
= (size
< ep
->ep
.maxpacket
);
448 VDBG(ep
->dev
, "read %s %u bytes%s OUT req %p %u/%u\n",
449 ep
->ep
.name
, size
, is_short
? "/S" : "",
450 req
, req
->req
.actual
, req
->req
.length
);
452 while (likely(size
-- != 0)) {
453 u8 byte
= (u8
) readl(ep
->reg_fifo
);
455 if (unlikely(bufferspace
== 0)) {
456 /* this happens when the driver's buffer
457 * is smaller than what the host sent.
458 * discard the extra data in this packet.
460 if (req
->req
.status
!= -EOVERFLOW
)
461 DBG(ep
->dev
, "%s overflow %u\n",
463 req
->req
.status
= -EOVERFLOW
;
471 if (unlikely(is_short
|| req
->req
.actual
== req
->req
.length
)) {
472 if (unlikely(ep
->num
== 0)) {
473 /* non-control endpoints now usable? */
474 if (ep
->dev
->req_config
)
475 writel(ep
->dev
->configured
476 ? USBSTATE_CONFIGURED
479 /* ep0out status stage */
480 writel(~(1<<0), ®s
->EOP
);
482 ep
->dev
->ep0state
= EP0_STATUS
;
486 /* empty the second buffer asap */
487 if (dbuff
&& !list_empty(&ep
->queue
)) {
488 req
= list_entry(ep
->queue
.next
,
489 struct goku_request
, queue
);
499 pio_irq_enable(struct goku_udc
*dev
,
500 struct goku_udc_regs __iomem
*regs
, int epnum
)
502 dev
->int_enable
|= INT_EPxDATASET (epnum
);
503 writel(dev
->int_enable
, ®s
->int_enable
);
504 /* write may still be posted */
508 pio_irq_disable(struct goku_udc
*dev
,
509 struct goku_udc_regs __iomem
*regs
, int epnum
)
511 dev
->int_enable
&= ~INT_EPxDATASET (epnum
);
512 writel(dev
->int_enable
, ®s
->int_enable
);
513 /* write may still be posted */
517 pio_advance(struct goku_ep
*ep
)
519 struct goku_request
*req
;
521 if (unlikely(list_empty (&ep
->queue
)))
523 req
= list_entry(ep
->queue
.next
, struct goku_request
, queue
);
524 (ep
->is_in
? write_fifo
: read_fifo
)(ep
, req
);
528 /*-------------------------------------------------------------------------*/
530 // return: 0 = q running, 1 = q stopped, negative = errno
531 static int start_dma(struct goku_ep
*ep
, struct goku_request
*req
)
533 struct goku_udc_regs __iomem
*regs
= ep
->dev
->regs
;
535 u32 start
= req
->req
.dma
;
536 u32 end
= start
+ req
->req
.length
- 1;
538 master
= readl(®s
->dma_master
) & MST_RW_BITS
;
540 /* re-init the bits affecting IN dma; careful with zlps */
541 if (likely(ep
->is_in
)) {
542 if (unlikely(master
& MST_RD_ENA
)) {
543 DBG (ep
->dev
, "start, IN active dma %03x!!\n",
547 writel(end
, ®s
->in_dma_end
);
548 writel(start
, ®s
->in_dma_start
);
550 master
&= ~MST_R_BITS
;
551 if (unlikely(req
->req
.length
== 0))
552 master
= MST_RD_ENA
| MST_RD_EOPB
;
553 else if ((req
->req
.length
% ep
->ep
.maxpacket
) != 0
555 master
= MST_RD_ENA
| MST_EOPB_ENA
;
557 master
= MST_RD_ENA
| MST_EOPB_DIS
;
559 ep
->dev
->int_enable
|= INT_MSTRDEND
;
561 /* Goku DMA-OUT merges short packets, which plays poorly with
562 * protocols where short packets mark the transfer boundaries.
563 * The chip supports a nonstandard policy with INT_MSTWRTMOUT,
564 * ending transfers after 3 SOFs; we don't turn it on.
567 if (unlikely(master
& MST_WR_ENA
)) {
568 DBG (ep
->dev
, "start, OUT active dma %03x!!\n",
572 writel(end
, ®s
->out_dma_end
);
573 writel(start
, ®s
->out_dma_start
);
575 master
&= ~MST_W_BITS
;
576 master
|= MST_WR_ENA
| MST_TIMEOUT_DIS
;
578 ep
->dev
->int_enable
|= INT_MSTWREND
|INT_MSTWRTMOUT
;
581 writel(master
, ®s
->dma_master
);
582 writel(ep
->dev
->int_enable
, ®s
->int_enable
);
586 static void dma_advance(struct goku_udc
*dev
, struct goku_ep
*ep
)
588 struct goku_request
*req
;
589 struct goku_udc_regs __iomem
*regs
= ep
->dev
->regs
;
592 master
= readl(®s
->dma_master
);
594 if (unlikely(list_empty(&ep
->queue
))) {
597 dev
->int_enable
&= ~INT_MSTRDEND
;
599 dev
->int_enable
&= ~(INT_MSTWREND
|INT_MSTWRTMOUT
);
600 writel(dev
->int_enable
, ®s
->int_enable
);
603 req
= list_entry(ep
->queue
.next
, struct goku_request
, queue
);
605 /* normal hw dma completion (not abort) */
606 if (likely(ep
->is_in
)) {
607 if (unlikely(master
& MST_RD_ENA
))
609 req
->req
.actual
= readl(®s
->in_dma_current
);
611 if (unlikely(master
& MST_WR_ENA
))
614 /* hardware merges short packets, and also hides packet
615 * overruns. a partial packet MAY be in the fifo here.
617 req
->req
.actual
= readl(®s
->out_dma_current
);
619 req
->req
.actual
-= req
->req
.dma
;
623 VDBG(dev
, "done %s %s dma, %u/%u bytes, req %p\n",
624 ep
->ep
.name
, ep
->is_in
? "IN" : "OUT",
625 req
->req
.actual
, req
->req
.length
, req
);
628 if (list_empty(&ep
->queue
))
630 req
= list_entry(ep
->queue
.next
, struct goku_request
, queue
);
631 (void) start_dma(ep
, req
);
634 static void abort_dma(struct goku_ep
*ep
, int status
)
636 struct goku_udc_regs __iomem
*regs
= ep
->dev
->regs
;
637 struct goku_request
*req
;
640 /* NAK future host requests, hoping the implicit delay lets the
641 * dma engine finish reading (or writing) its latest packet and
642 * empty the dma buffer (up to 16 bytes).
644 * This avoids needing to clean up a partial packet in the fifo;
645 * we can't do that for IN without side effects to HALT and TOGGLE.
647 command(regs
, COMMAND_FIFO_DISABLE
, ep
->num
);
648 req
= list_entry(ep
->queue
.next
, struct goku_request
, queue
);
649 master
= readl(®s
->dma_master
) & MST_RW_BITS
;
651 /* FIXME using these resets isn't usably documented. this may
652 * not work unless it's followed by disabling the endpoint.
654 * FIXME the OUT reset path doesn't even behave consistently.
657 if (unlikely((readl(®s
->dma_master
) & MST_RD_ENA
) == 0))
659 curr
= readl(®s
->in_dma_current
);
661 writel(curr
, ®s
->in_dma_end
);
662 writel(curr
, ®s
->in_dma_start
);
664 master
&= ~MST_R_BITS
;
665 master
|= MST_RD_RESET
;
666 writel(master
, ®s
->dma_master
);
668 if (readl(®s
->dma_master
) & MST_RD_ENA
)
669 DBG(ep
->dev
, "IN dma active after reset!\n");
672 if (unlikely((readl(®s
->dma_master
) & MST_WR_ENA
) == 0))
674 curr
= readl(®s
->out_dma_current
);
676 writel(curr
, ®s
->out_dma_end
);
677 writel(curr
, ®s
->out_dma_start
);
679 master
&= ~MST_W_BITS
;
680 master
|= MST_WR_RESET
;
681 writel(master
, ®s
->dma_master
);
683 if (readl(®s
->dma_master
) & MST_WR_ENA
)
684 DBG(ep
->dev
, "OUT dma active after reset!\n");
686 req
->req
.actual
= (curr
- req
->req
.dma
) + 1;
687 req
->req
.status
= status
;
689 VDBG(ep
->dev
, "%s %s %s %d/%d\n", __func__
, ep
->ep
.name
,
690 ep
->is_in
? "IN" : "OUT",
691 req
->req
.actual
, req
->req
.length
);
693 command(regs
, COMMAND_FIFO_ENABLE
, ep
->num
);
698 /* dma already completed; no abort needed */
699 command(regs
, COMMAND_FIFO_ENABLE
, ep
->num
);
700 req
->req
.actual
= req
->req
.length
;
704 /*-------------------------------------------------------------------------*/
707 goku_queue(struct usb_ep
*_ep
, struct usb_request
*_req
, gfp_t gfp_flags
)
709 struct goku_request
*req
;
711 struct goku_udc
*dev
;
715 /* always require a cpu-view buffer so pio works */
716 req
= container_of(_req
, struct goku_request
, req
);
717 if (unlikely(!_req
|| !_req
->complete
718 || !_req
->buf
|| !list_empty(&req
->queue
)))
720 ep
= container_of(_ep
, struct goku_ep
, ep
);
721 if (unlikely(!_ep
|| (!ep
->ep
.desc
&& ep
->num
!= 0)))
724 if (unlikely(!dev
->driver
|| dev
->gadget
.speed
== USB_SPEED_UNKNOWN
))
727 /* can't touch registers when suspended */
728 if (dev
->ep0state
== EP0_SUSPEND
)
731 /* set up dma mapping in case the caller didn't */
733 status
= usb_gadget_map_request(&dev
->gadget
, &req
->req
,
740 VDBG(dev
, "%s queue req %p, len %u buf %p\n",
741 _ep
->name
, _req
, _req
->length
, _req
->buf
);
744 spin_lock_irqsave(&dev
->lock
, flags
);
746 _req
->status
= -EINPROGRESS
;
749 /* for ep0 IN without premature status, zlp is required and
750 * writing EOP starts the status stage (OUT).
752 if (unlikely(ep
->num
== 0 && ep
->is_in
))
755 /* kickstart this i/o queue? */
757 if (list_empty(&ep
->queue
) && likely(!ep
->stopped
)) {
758 /* dma: done after dma completion IRQ (or error)
759 * pio: done after last fifo operation
762 status
= start_dma(ep
, req
);
764 status
= (ep
->is_in
? write_fifo
: read_fifo
)(ep
, req
);
766 if (unlikely(status
!= 0)) {
772 } /* else pio or dma irq handler advances the queue. */
774 if (likely(req
!= NULL
))
775 list_add_tail(&req
->queue
, &ep
->queue
);
777 if (likely(!list_empty(&ep
->queue
))
778 && likely(ep
->num
!= 0)
780 && !(dev
->int_enable
& INT_EPxDATASET (ep
->num
)))
781 pio_irq_enable(dev
, dev
->regs
, ep
->num
);
783 spin_unlock_irqrestore(&dev
->lock
, flags
);
785 /* pci writes may still be posted */
789 /* dequeue ALL requests */
790 static void nuke(struct goku_ep
*ep
, int status
)
792 struct goku_request
*req
;
795 if (list_empty(&ep
->queue
))
798 abort_dma(ep
, status
);
799 while (!list_empty(&ep
->queue
)) {
800 req
= list_entry(ep
->queue
.next
, struct goku_request
, queue
);
801 done(ep
, req
, status
);
805 /* dequeue JUST ONE request */
806 static int goku_dequeue(struct usb_ep
*_ep
, struct usb_request
*_req
)
808 struct goku_request
*req
;
810 struct goku_udc
*dev
;
813 ep
= container_of(_ep
, struct goku_ep
, ep
);
814 if (!_ep
|| !_req
|| (!ep
->ep
.desc
&& ep
->num
!= 0))
820 /* we can't touch (dma) registers when suspended */
821 if (dev
->ep0state
== EP0_SUSPEND
)
824 VDBG(dev
, "%s %s %s %s %p\n", __func__
, _ep
->name
,
825 ep
->is_in
? "IN" : "OUT",
826 ep
->dma
? "dma" : "pio",
829 spin_lock_irqsave(&dev
->lock
, flags
);
831 /* make sure it's actually queued on this endpoint */
832 list_for_each_entry (req
, &ep
->queue
, queue
) {
833 if (&req
->req
== _req
)
836 if (&req
->req
!= _req
) {
837 spin_unlock_irqrestore (&dev
->lock
, flags
);
841 if (ep
->dma
&& ep
->queue
.next
== &req
->queue
&& !ep
->stopped
) {
842 abort_dma(ep
, -ECONNRESET
);
843 done(ep
, req
, -ECONNRESET
);
844 dma_advance(dev
, ep
);
845 } else if (!list_empty(&req
->queue
))
846 done(ep
, req
, -ECONNRESET
);
849 spin_unlock_irqrestore(&dev
->lock
, flags
);
851 return req
? 0 : -EOPNOTSUPP
;
854 /*-------------------------------------------------------------------------*/
856 static void goku_clear_halt(struct goku_ep
*ep
)
858 // assert (ep->num !=0)
859 VDBG(ep
->dev
, "%s clear halt\n", ep
->ep
.name
);
860 command(ep
->dev
->regs
, COMMAND_SETDATA0
, ep
->num
);
861 command(ep
->dev
->regs
, COMMAND_STALL_CLEAR
, ep
->num
);
865 struct goku_request
*req
;
867 if (list_empty(&ep
->queue
))
869 req
= list_entry(ep
->queue
.next
, struct goku_request
,
871 (void) start_dma(ep
, req
);
877 static int goku_set_halt(struct usb_ep
*_ep
, int value
)
885 ep
= container_of (_ep
, struct goku_ep
, ep
);
889 ep
->dev
->ep0state
= EP0_STALL
;
890 ep
->dev
->ep
[0].stopped
= 1;
894 /* don't change EPxSTATUS_EP_INVALID to READY */
895 } else if (!ep
->ep
.desc
) {
896 DBG(ep
->dev
, "%s %s inactive?\n", __func__
, ep
->ep
.name
);
900 spin_lock_irqsave(&ep
->dev
->lock
, flags
);
901 if (!list_empty(&ep
->queue
))
903 else if (ep
->is_in
&& value
904 /* data in (either) packet buffer? */
905 && (readl(&ep
->dev
->regs
->DataSet
)
906 & DATASET_AB(ep
->num
)))
912 VDBG(ep
->dev
, "%s set halt\n", ep
->ep
.name
);
913 command(ep
->dev
->regs
, COMMAND_STALL
, ep
->num
);
914 readl(ep
->reg_status
);
916 spin_unlock_irqrestore(&ep
->dev
->lock
, flags
);
920 static int goku_fifo_status(struct usb_ep
*_ep
)
923 struct goku_udc_regs __iomem
*regs
;
928 ep
= container_of(_ep
, struct goku_ep
, ep
);
930 /* size is only reported sanely for OUT */
934 /* ignores 16-byte dma buffer; SizeH == 0 */
935 regs
= ep
->dev
->regs
;
936 size
= readl(®s
->EPxSizeLA
[ep
->num
]) & DATASIZE
;
937 size
+= readl(®s
->EPxSizeLB
[ep
->num
]) & DATASIZE
;
938 VDBG(ep
->dev
, "%s %s %u\n", __func__
, ep
->ep
.name
, size
);
942 static void goku_fifo_flush(struct usb_ep
*_ep
)
945 struct goku_udc_regs __iomem
*regs
;
950 ep
= container_of(_ep
, struct goku_ep
, ep
);
951 VDBG(ep
->dev
, "%s %s\n", __func__
, ep
->ep
.name
);
953 /* don't change EPxSTATUS_EP_INVALID to READY */
954 if (!ep
->ep
.desc
&& ep
->num
!= 0) {
955 DBG(ep
->dev
, "%s %s inactive?\n", __func__
, ep
->ep
.name
);
959 regs
= ep
->dev
->regs
;
960 size
= readl(®s
->EPxSizeLA
[ep
->num
]);
963 /* Non-desirable behavior: FIFO_CLEAR also clears the
964 * endpoint halt feature. For OUT, we _could_ just read
965 * the bytes out (PIO, if !ep->dma); for in, no choice.
968 command(regs
, COMMAND_FIFO_CLEAR
, ep
->num
);
971 static struct usb_ep_ops goku_ep_ops
= {
972 .enable
= goku_ep_enable
,
973 .disable
= goku_ep_disable
,
975 .alloc_request
= goku_alloc_request
,
976 .free_request
= goku_free_request
,
979 .dequeue
= goku_dequeue
,
981 .set_halt
= goku_set_halt
,
982 .fifo_status
= goku_fifo_status
,
983 .fifo_flush
= goku_fifo_flush
,
986 /*-------------------------------------------------------------------------*/
988 static int goku_get_frame(struct usb_gadget
*_gadget
)
993 static int goku_udc_start(struct usb_gadget
*g
,
994 struct usb_gadget_driver
*driver
);
995 static int goku_udc_stop(struct usb_gadget
*g
,
996 struct usb_gadget_driver
*driver
);
998 static const struct usb_gadget_ops goku_ops
= {
999 .get_frame
= goku_get_frame
,
1000 .udc_start
= goku_udc_start
,
1001 .udc_stop
= goku_udc_stop
,
1006 /*-------------------------------------------------------------------------*/
1008 static inline const char *dmastr(void)
1011 return "(dma disabled)";
1012 else if (use_dma
== 2)
1013 return "(dma IN and OUT)";
1018 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1020 static const char proc_node_name
[] = "driver/udc";
1022 #define FOURBITS "%s%s%s%s"
1023 #define EIGHTBITS FOURBITS FOURBITS
1025 static void dump_intmask(struct seq_file
*m
, const char *label
, u32 mask
)
1027 /* int_status is the same format ... */
1029 "%s %05X =" FOURBITS EIGHTBITS EIGHTBITS
"\n",
1031 (mask
& INT_PWRDETECT
) ? " power" : "",
1032 (mask
& INT_SYSERROR
) ? " sys" : "",
1033 (mask
& INT_MSTRDEND
) ? " in-dma" : "",
1034 (mask
& INT_MSTWRTMOUT
) ? " wrtmo" : "",
1036 (mask
& INT_MSTWREND
) ? " out-dma" : "",
1037 (mask
& INT_MSTWRSET
) ? " wrset" : "",
1038 (mask
& INT_ERR
) ? " err" : "",
1039 (mask
& INT_SOF
) ? " sof" : "",
1041 (mask
& INT_EP3NAK
) ? " ep3nak" : "",
1042 (mask
& INT_EP2NAK
) ? " ep2nak" : "",
1043 (mask
& INT_EP1NAK
) ? " ep1nak" : "",
1044 (mask
& INT_EP3DATASET
) ? " ep3" : "",
1046 (mask
& INT_EP2DATASET
) ? " ep2" : "",
1047 (mask
& INT_EP1DATASET
) ? " ep1" : "",
1048 (mask
& INT_STATUSNAK
) ? " ep0snak" : "",
1049 (mask
& INT_STATUS
) ? " ep0status" : "",
1051 (mask
& INT_SETUP
) ? " setup" : "",
1052 (mask
& INT_ENDPOINT0
) ? " ep0" : "",
1053 (mask
& INT_USBRESET
) ? " reset" : "",
1054 (mask
& INT_SUSPEND
) ? " suspend" : "");
1058 static int udc_proc_read(struct seq_file
*m
, void *v
)
1060 struct goku_udc
*dev
= m
->private;
1061 struct goku_udc_regs __iomem
*regs
= dev
->regs
;
1062 unsigned long flags
;
1063 int i
, is_usb_connected
;
1066 local_irq_save(flags
);
1068 /* basic device status */
1069 tmp
= readl(®s
->power_detect
);
1070 is_usb_connected
= tmp
& PW_DETECT
;
1073 "%s version: %s %s\n"
1074 "Gadget driver: %s\n"
1077 pci_name(dev
->pdev
), driver_desc
,
1078 driver_name
, DRIVER_VERSION
, dmastr(),
1079 dev
->driver
? dev
->driver
->driver
.name
: "(none)",
1081 ? ((tmp
& PW_PULLUP
) ? "full speed" : "powered")
1083 ({const char *state
;
1084 switch(dev
->ep0state
){
1085 case EP0_DISCONNECT
: state
= "ep0_disconnect"; break;
1086 case EP0_IDLE
: state
= "ep0_idle"; break;
1087 case EP0_IN
: state
= "ep0_in"; break;
1088 case EP0_OUT
: state
= "ep0_out"; break;
1089 case EP0_STATUS
: state
= "ep0_status"; break;
1090 case EP0_STALL
: state
= "ep0_stall"; break;
1091 case EP0_SUSPEND
: state
= "ep0_suspend"; break;
1092 default: state
= "ep0_?"; break;
1096 dump_intmask(m
, "int_status", readl(®s
->int_status
));
1097 dump_intmask(m
, "int_enable", readl(®s
->int_enable
));
1099 if (!is_usb_connected
|| !dev
->driver
|| (tmp
& PW_PULLUP
) == 0)
1102 /* registers for (active) device and ep0 */
1103 if (seq_printf(m
, "\nirqs %lu\ndataset %02x "
1104 "single.bcs %02x.%02x state %x addr %u\n",
1105 dev
->irqs
, readl(®s
->DataSet
),
1106 readl(®s
->EPxSingle
), readl(®s
->EPxBCS
),
1107 readl(®s
->UsbState
),
1108 readl(®s
->address
)) < 0)
1111 tmp
= readl(®s
->dma_master
);
1113 "dma %03X =" EIGHTBITS
"%s %s\n", tmp
,
1114 (tmp
& MST_EOPB_DIS
) ? " eopb-" : "",
1115 (tmp
& MST_EOPB_ENA
) ? " eopb+" : "",
1116 (tmp
& MST_TIMEOUT_DIS
) ? " tmo-" : "",
1117 (tmp
& MST_TIMEOUT_ENA
) ? " tmo+" : "",
1119 (tmp
& MST_RD_EOPB
) ? " eopb" : "",
1120 (tmp
& MST_RD_RESET
) ? " in_reset" : "",
1121 (tmp
& MST_WR_RESET
) ? " out_reset" : "",
1122 (tmp
& MST_RD_ENA
) ? " IN" : "",
1124 (tmp
& MST_WR_ENA
) ? " OUT" : "",
1125 (tmp
& MST_CONNECTION
)
1127 : "ep1out/ep2in") < 0)
1130 /* dump endpoint queues */
1131 for (i
= 0; i
< 4; i
++) {
1132 struct goku_ep
*ep
= &dev
->ep
[i
];
1133 struct goku_request
*req
;
1135 if (i
&& !ep
->ep
.desc
)
1138 tmp
= readl(ep
->reg_status
);
1140 "%s %s max %u %s, irqs %lu, "
1141 "status %02x (%s) " FOURBITS
"\n",
1143 ep
->is_in
? "in" : "out",
1145 ep
->dma
? "dma" : "pio",
1148 switch (tmp
& EPxSTATUS_EP_MASK
) {
1149 case EPxSTATUS_EP_READY
:
1151 case EPxSTATUS_EP_DATAIN
:
1152 s
= "packet"; break;
1153 case EPxSTATUS_EP_FULL
:
1155 case EPxSTATUS_EP_TX_ERR
: // host will retry
1156 s
= "tx_err"; break;
1157 case EPxSTATUS_EP_RX_ERR
:
1158 s
= "rx_err"; break;
1159 case EPxSTATUS_EP_BUSY
: /* ep0 only */
1161 case EPxSTATUS_EP_STALL
:
1163 case EPxSTATUS_EP_INVALID
: // these "can't happen"
1164 s
= "invalid"; break;
1168 (tmp
& EPxSTATUS_TOGGLE
) ? "data1" : "data0",
1169 (tmp
& EPxSTATUS_SUSPEND
) ? " suspend" : "",
1170 (tmp
& EPxSTATUS_FIFO_DISABLE
) ? " disable" : "",
1171 (tmp
& EPxSTATUS_STAGE_ERROR
) ? " ep0stat" : ""
1175 if (list_empty(&ep
->queue
)) {
1176 if (seq_puts(m
, "\t(nothing queued)\n") < 0)
1180 list_for_each_entry(req
, &ep
->queue
, queue
) {
1181 if (ep
->dma
&& req
->queue
.prev
== &ep
->queue
) {
1182 if (i
== UDC_MSTRD_ENDPOINT
)
1183 tmp
= readl(®s
->in_dma_current
);
1185 tmp
= readl(®s
->out_dma_current
);
1186 tmp
-= req
->req
.dma
;
1189 tmp
= req
->req
.actual
;
1192 "\treq %p len %u/%u buf %p\n",
1193 &req
->req
, tmp
, req
->req
.length
,
1200 local_irq_restore(flags
);
1205 * seq_file wrappers for procfile show routines.
1207 static int udc_proc_open(struct inode
*inode
, struct file
*file
)
1209 return single_open(file
, udc_proc_read
, PDE_DATA(file_inode(file
)));
1212 static const struct file_operations udc_proc_fops
= {
1213 .open
= udc_proc_open
,
1215 .llseek
= seq_lseek
,
1216 .release
= single_release
,
1219 #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
1221 /*-------------------------------------------------------------------------*/
1223 static void udc_reinit (struct goku_udc
*dev
)
1225 static char *names
[] = { "ep0", "ep1-bulk", "ep2-bulk", "ep3-bulk" };
1229 INIT_LIST_HEAD (&dev
->gadget
.ep_list
);
1230 dev
->gadget
.ep0
= &dev
->ep
[0].ep
;
1231 dev
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1232 dev
->ep0state
= EP0_DISCONNECT
;
1235 for (i
= 0; i
< 4; i
++) {
1236 struct goku_ep
*ep
= &dev
->ep
[i
];
1239 ep
->ep
.name
= names
[i
];
1240 ep
->reg_fifo
= &dev
->regs
->ep_fifo
[i
];
1241 ep
->reg_status
= &dev
->regs
->ep_status
[i
];
1242 ep
->reg_mode
= &dev
->regs
->ep_mode
[i
];
1244 ep
->ep
.ops
= &goku_ep_ops
;
1245 list_add_tail (&ep
->ep
.ep_list
, &dev
->gadget
.ep_list
);
1247 INIT_LIST_HEAD (&ep
->queue
);
1252 dev
->ep
[0].reg_mode
= NULL
;
1253 usb_ep_set_maxpacket_limit(&dev
->ep
[0].ep
, MAX_EP0_SIZE
);
1254 list_del_init (&dev
->ep
[0].ep
.ep_list
);
1257 static void udc_reset(struct goku_udc
*dev
)
1259 struct goku_udc_regs __iomem
*regs
= dev
->regs
;
1261 writel(0, ®s
->power_detect
);
1262 writel(0, ®s
->int_enable
);
1263 readl(®s
->int_enable
);
1264 dev
->int_enable
= 0;
1266 /* deassert reset, leave USB D+ at hi-Z (no pullup)
1267 * don't let INT_PWRDETECT sequence begin
1270 writel(PW_RESETB
, ®s
->power_detect
);
1271 readl(®s
->int_enable
);
1274 static void ep0_start(struct goku_udc
*dev
)
1276 struct goku_udc_regs __iomem
*regs
= dev
->regs
;
1279 VDBG(dev
, "%s\n", __func__
);
1283 //writel(MST_EOPB_ENA | MST_TIMEOUT_ENA, ®s->dma_master);
1285 /* hw handles set_address, set_feature, get_status; maybe more */
1286 writel( G_REQMODE_SET_INTF
| G_REQMODE_GET_INTF
1287 | G_REQMODE_SET_CONF
| G_REQMODE_GET_CONF
1288 | G_REQMODE_GET_DESC
1289 | G_REQMODE_CLEAR_FEAT
1292 for (i
= 0; i
< 4; i
++)
1293 dev
->ep
[i
].irqs
= 0;
1295 /* can't modify descriptors after writing UsbReady */
1296 for (i
= 0; i
< DESC_LEN
; i
++)
1297 writel(0, ®s
->descriptors
[i
]);
1298 writel(0, ®s
->UsbReady
);
1300 /* expect ep0 requests when the host drops reset */
1301 writel(PW_RESETB
| PW_PULLUP
, ®s
->power_detect
);
1302 dev
->int_enable
= INT_DEVWIDE
| INT_EP0
;
1303 writel(dev
->int_enable
, &dev
->regs
->int_enable
);
1304 readl(®s
->int_enable
);
1305 dev
->gadget
.speed
= USB_SPEED_FULL
;
1306 dev
->ep0state
= EP0_IDLE
;
1309 static void udc_enable(struct goku_udc
*dev
)
1311 /* start enumeration now, or after power detect irq */
1312 if (readl(&dev
->regs
->power_detect
) & PW_DETECT
)
1315 DBG(dev
, "%s\n", __func__
);
1316 dev
->int_enable
= INT_PWRDETECT
;
1317 writel(dev
->int_enable
, &dev
->regs
->int_enable
);
1321 /*-------------------------------------------------------------------------*/
1323 /* keeping it simple:
1324 * - one bus driver, initted first;
1325 * - one function driver, initted second
1328 /* when a driver is successfully registered, it will receive
1329 * control requests including set_configuration(), which enables
1330 * non-control requests. then usb traffic follows until a
1331 * disconnect is reported. then a host may connect again, or
1332 * the driver might get unbound.
1334 static int goku_udc_start(struct usb_gadget
*g
,
1335 struct usb_gadget_driver
*driver
)
1337 struct goku_udc
*dev
= to_goku_udc(g
);
1339 /* hook up the driver */
1340 driver
->driver
.bus
= NULL
;
1341 dev
->driver
= driver
;
1344 * then enable host detection and ep0; and we're ready
1345 * for set_configuration as well as eventual disconnect.
1352 static void stop_activity(struct goku_udc
*dev
)
1356 DBG (dev
, "%s\n", __func__
);
1358 /* disconnect gadget driver after quiesceing hw and the driver */
1360 for (i
= 0; i
< 4; i
++)
1361 nuke(&dev
->ep
[i
], -ESHUTDOWN
);
1367 static int goku_udc_stop(struct usb_gadget
*g
,
1368 struct usb_gadget_driver
*driver
)
1370 struct goku_udc
*dev
= to_goku_udc(g
);
1371 unsigned long flags
;
1373 spin_lock_irqsave(&dev
->lock
, flags
);
1376 spin_unlock_irqrestore(&dev
->lock
, flags
);
1381 /*-------------------------------------------------------------------------*/
1383 static void ep0_setup(struct goku_udc
*dev
)
1385 struct goku_udc_regs __iomem
*regs
= dev
->regs
;
1386 struct usb_ctrlrequest ctrl
;
1389 /* read SETUP packet and enter DATA stage */
1390 ctrl
.bRequestType
= readl(®s
->bRequestType
);
1391 ctrl
.bRequest
= readl(®s
->bRequest
);
1392 ctrl
.wValue
= cpu_to_le16((readl(®s
->wValueH
) << 8)
1393 | readl(®s
->wValueL
));
1394 ctrl
.wIndex
= cpu_to_le16((readl(®s
->wIndexH
) << 8)
1395 | readl(®s
->wIndexL
));
1396 ctrl
.wLength
= cpu_to_le16((readl(®s
->wLengthH
) << 8)
1397 | readl(®s
->wLengthL
));
1398 writel(0, ®s
->SetupRecv
);
1400 nuke(&dev
->ep
[0], 0);
1401 dev
->ep
[0].stopped
= 0;
1402 if (likely(ctrl
.bRequestType
& USB_DIR_IN
)) {
1403 dev
->ep
[0].is_in
= 1;
1404 dev
->ep0state
= EP0_IN
;
1405 /* detect early status stages */
1406 writel(ICONTROL_STATUSNAK
, &dev
->regs
->IntControl
);
1408 dev
->ep
[0].is_in
= 0;
1409 dev
->ep0state
= EP0_OUT
;
1411 /* NOTE: CLEAR_FEATURE is done in software so that we can
1412 * synchronize transfer restarts after bulk IN stalls. data
1413 * won't even enter the fifo until the halt is cleared.
1415 switch (ctrl
.bRequest
) {
1416 case USB_REQ_CLEAR_FEATURE
:
1417 switch (ctrl
.bRequestType
) {
1418 case USB_RECIP_ENDPOINT
:
1419 tmp
= le16_to_cpu(ctrl
.wIndex
) & 0x0f;
1420 /* active endpoint */
1422 (!dev
->ep
[tmp
].ep
.desc
&& tmp
!= 0))
1424 if (ctrl
.wIndex
& cpu_to_le16(
1426 if (!dev
->ep
[tmp
].is_in
)
1429 if (dev
->ep
[tmp
].is_in
)
1432 if (ctrl
.wValue
!= cpu_to_le16(
1436 goku_clear_halt(&dev
->ep
[tmp
]);
1438 /* start ep0out status stage */
1439 writel(~(1<<0), ®s
->EOP
);
1440 dev
->ep
[0].stopped
= 1;
1441 dev
->ep0state
= EP0_STATUS
;
1443 case USB_RECIP_DEVICE
:
1444 /* device remote wakeup: always clear */
1445 if (ctrl
.wValue
!= cpu_to_le16(1))
1447 VDBG(dev
, "clear dev remote wakeup\n");
1449 case USB_RECIP_INTERFACE
:
1451 default: /* pass to gadget driver */
1461 VDBG(dev
, "SETUP %02x.%02x v%04x i%04x l%04x\n",
1462 ctrl
.bRequestType
, ctrl
.bRequest
,
1463 le16_to_cpu(ctrl
.wValue
), le16_to_cpu(ctrl
.wIndex
),
1464 le16_to_cpu(ctrl
.wLength
));
1467 /* hw wants to know when we're configured (or not) */
1468 dev
->req_config
= (ctrl
.bRequest
== USB_REQ_SET_CONFIGURATION
1469 && ctrl
.bRequestType
== USB_RECIP_DEVICE
);
1470 if (unlikely(dev
->req_config
))
1471 dev
->configured
= (ctrl
.wValue
!= cpu_to_le16(0));
1473 /* delegate everything to the gadget driver.
1474 * it may respond after this irq handler returns.
1476 spin_unlock (&dev
->lock
);
1477 tmp
= dev
->driver
->setup(&dev
->gadget
, &ctrl
);
1478 spin_lock (&dev
->lock
);
1479 if (unlikely(tmp
< 0)) {
1482 VDBG(dev
, "req %02x.%02x protocol STALL; err %d\n",
1483 ctrl
.bRequestType
, ctrl
.bRequest
, tmp
);
1485 command(regs
, COMMAND_STALL
, 0);
1486 dev
->ep
[0].stopped
= 1;
1487 dev
->ep0state
= EP0_STALL
;
1490 /* expect at least one data or status stage irq */
1493 #define ACK(irqbit) { \
1495 writel(~irqbit, ®s->int_status); \
1499 static irqreturn_t
goku_irq(int irq
, void *_dev
)
1501 struct goku_udc
*dev
= _dev
;
1502 struct goku_udc_regs __iomem
*regs
= dev
->regs
;
1504 u32 stat
, handled
= 0;
1505 unsigned i
, rescans
= 5;
1507 spin_lock(&dev
->lock
);
1510 stat
= readl(®s
->int_status
) & dev
->int_enable
;
1515 /* device-wide irqs */
1516 if (unlikely(stat
& INT_DEVWIDE
)) {
1517 if (stat
& INT_SYSERROR
) {
1518 ERROR(dev
, "system error\n");
1522 // FIXME have a neater way to prevent re-enumeration
1526 if (stat
& INT_PWRDETECT
) {
1527 writel(~stat
, ®s
->int_status
);
1528 if (readl(&dev
->regs
->power_detect
) & PW_DETECT
) {
1529 VDBG(dev
, "connect\n");
1532 DBG(dev
, "disconnect\n");
1533 if (dev
->gadget
.speed
== USB_SPEED_FULL
)
1535 dev
->ep0state
= EP0_DISCONNECT
;
1536 dev
->int_enable
= INT_DEVWIDE
;
1537 writel(dev
->int_enable
, &dev
->regs
->int_enable
);
1543 if (stat
& INT_SUSPEND
) {
1545 if (readl(®s
->ep_status
[0]) & EPxSTATUS_SUSPEND
) {
1546 switch (dev
->ep0state
) {
1547 case EP0_DISCONNECT
:
1553 DBG(dev
, "USB suspend\n");
1554 dev
->ep0state
= EP0_SUSPEND
;
1555 if (dev
->gadget
.speed
!= USB_SPEED_UNKNOWN
1557 && dev
->driver
->suspend
) {
1558 spin_unlock(&dev
->lock
);
1559 dev
->driver
->suspend(&dev
->gadget
);
1560 spin_lock(&dev
->lock
);
1563 if (dev
->ep0state
!= EP0_SUSPEND
) {
1564 DBG(dev
, "bogus USB resume %d\n",
1568 DBG(dev
, "USB resume\n");
1569 dev
->ep0state
= EP0_IDLE
;
1570 if (dev
->gadget
.speed
!= USB_SPEED_UNKNOWN
1572 && dev
->driver
->resume
) {
1573 spin_unlock(&dev
->lock
);
1574 dev
->driver
->resume(&dev
->gadget
);
1575 spin_lock(&dev
->lock
);
1580 if (stat
& INT_USBRESET
) { /* hub reset done */
1582 INFO(dev
, "USB reset done, gadget %s\n",
1583 dev
->driver
->driver
.name
);
1585 // and INT_ERR on some endpoint's crc/bitstuff/... problem
1588 /* progress ep0 setup, data, or status stages.
1589 * no transition {EP0_STATUS, EP0_STALL} --> EP0_IDLE; saves irqs
1591 if (stat
& INT_SETUP
) {
1596 if (stat
& INT_STATUSNAK
) {
1597 ACK(INT_STATUSNAK
|INT_ENDPOINT0
);
1598 if (dev
->ep0state
== EP0_IN
) {
1602 writel(~(1<<0), ®s
->EOP
);
1603 dev
->ep0state
= EP0_STATUS
;
1606 if (stat
& INT_ENDPOINT0
) {
1613 /* dma completion */
1614 if (stat
& INT_MSTRDEND
) { /* IN */
1616 ep
= &dev
->ep
[UDC_MSTRD_ENDPOINT
];
1618 dma_advance(dev
, ep
);
1620 if (stat
& INT_MSTWREND
) { /* OUT */
1622 ep
= &dev
->ep
[UDC_MSTWR_ENDPOINT
];
1624 dma_advance(dev
, ep
);
1626 if (stat
& INT_MSTWRTMOUT
) { /* OUT */
1627 ACK(INT_MSTWRTMOUT
);
1628 ep
= &dev
->ep
[UDC_MSTWR_ENDPOINT
];
1630 ERROR(dev
, "%s write timeout ?\n", ep
->ep
.name
);
1631 // reset dma? then dma_advance()
1635 for (i
= 1; i
< 4; i
++) {
1636 u32 tmp
= INT_EPxDATASET(i
);
1642 if (list_empty (&ep
->queue
))
1643 pio_irq_disable(dev
, regs
, i
);
1653 (void)readl(®s
->int_enable
);
1654 spin_unlock(&dev
->lock
);
1656 DBG(dev
, "unhandled irq status: %05x (%05x, %05x)\n", stat
,
1657 readl(®s
->int_status
), dev
->int_enable
);
1658 return IRQ_RETVAL(handled
);
1663 /*-------------------------------------------------------------------------*/
1665 static void gadget_release(struct device
*_dev
)
1667 struct goku_udc
*dev
= dev_get_drvdata(_dev
);
1672 /* tear down the binding between this driver and the pci device */
1674 static void goku_remove(struct pci_dev
*pdev
)
1676 struct goku_udc
*dev
= pci_get_drvdata(pdev
);
1678 DBG(dev
, "%s\n", __func__
);
1680 usb_del_gadget_udc(&dev
->gadget
);
1682 BUG_ON(dev
->driver
);
1684 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1685 remove_proc_entry(proc_node_name
, NULL
);
1690 free_irq(pdev
->irq
, dev
);
1693 if (dev
->got_region
)
1694 release_mem_region(pci_resource_start (pdev
, 0),
1695 pci_resource_len (pdev
, 0));
1697 pci_disable_device(pdev
);
1701 INFO(dev
, "unbind\n");
1704 /* wrap this driver around the specified pci device, but
1705 * don't respond over USB until a gadget driver binds to us.
1708 static int goku_probe(struct pci_dev
*pdev
, const struct pci_device_id
*id
)
1710 struct goku_udc
*dev
= NULL
;
1711 unsigned long resource
, len
;
1712 void __iomem
*base
= NULL
;
1716 printk(KERN_ERR
"Check PCI %s IRQ setup!\n", pci_name(pdev
));
1721 /* alloc, and start init */
1722 dev
= kzalloc (sizeof *dev
, GFP_KERNEL
);
1724 pr_debug("enomem %s\n", pci_name(pdev
));
1729 spin_lock_init(&dev
->lock
);
1731 dev
->gadget
.ops
= &goku_ops
;
1732 dev
->gadget
.max_speed
= USB_SPEED_FULL
;
1734 /* the "gadget" abstracts/virtualizes the controller */
1735 dev
->gadget
.name
= driver_name
;
1737 /* now all the pci goodies ... */
1738 retval
= pci_enable_device(pdev
);
1740 DBG(dev
, "can't enable, %d\n", retval
);
1745 resource
= pci_resource_start(pdev
, 0);
1746 len
= pci_resource_len(pdev
, 0);
1747 if (!request_mem_region(resource
, len
, driver_name
)) {
1748 DBG(dev
, "controller already in use\n");
1752 dev
->got_region
= 1;
1754 base
= ioremap_nocache(resource
, len
);
1756 DBG(dev
, "can't map memory\n");
1760 dev
->regs
= (struct goku_udc_regs __iomem
*) base
;
1762 pci_set_drvdata(pdev
, dev
);
1763 INFO(dev
, "%s\n", driver_desc
);
1764 INFO(dev
, "version: " DRIVER_VERSION
" %s\n", dmastr());
1765 INFO(dev
, "irq %d, pci mem %p\n", pdev
->irq
, base
);
1767 /* init to known state, then setup irqs */
1770 if (request_irq(pdev
->irq
, goku_irq
, IRQF_SHARED
,
1771 driver_name
, dev
) != 0) {
1772 DBG(dev
, "request interrupt %d failed\n", pdev
->irq
);
1778 pci_set_master(pdev
);
1781 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1782 proc_create_data(proc_node_name
, 0, NULL
, &udc_proc_fops
, dev
);
1785 retval
= usb_add_gadget_udc_release(&pdev
->dev
, &dev
->gadget
,
1799 /*-------------------------------------------------------------------------*/
1801 static const struct pci_device_id pci_ids
[] = { {
1802 .class = ((PCI_CLASS_SERIAL_USB
<< 8) | 0xfe),
1804 .vendor
= 0x102f, /* Toshiba */
1805 .device
= 0x0107, /* this UDC */
1806 .subvendor
= PCI_ANY_ID
,
1807 .subdevice
= PCI_ANY_ID
,
1809 }, { /* end: all zeroes */ }
1811 MODULE_DEVICE_TABLE (pci
, pci_ids
);
1813 static struct pci_driver goku_pci_driver
= {
1814 .name
= (char *) driver_name
,
1815 .id_table
= pci_ids
,
1817 .probe
= goku_probe
,
1818 .remove
= goku_remove
,
1820 /* FIXME add power management support */
1823 module_pci_driver(goku_pci_driver
);