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/kernel.h>
28 #include <linux/module.h>
29 #include <linux/pci.h>
30 #include <linux/delay.h>
31 #include <linux/ioport.h>
32 #include <linux/slab.h>
33 #include <linux/smp_lock.h>
34 #include <linux/errno.h>
35 #include <linux/init.h>
36 #include <linux/timer.h>
37 #include <linux/list.h>
38 #include <linux/interrupt.h>
39 #include <linux/proc_fs.h>
40 #include <linux/device.h>
41 #include <linux/usb/ch9.h>
42 #include <linux/usb_gadget.h>
44 #include <asm/byteorder.h>
47 #include <asm/system.h>
48 #include <asm/unaligned.h>
53 #define DRIVER_DESC "TC86C001 USB Device Controller"
54 #define DRIVER_VERSION "30-Oct 2003"
56 #define DMA_ADDR_INVALID (~(dma_addr_t)0)
58 static const char driver_name
[] = "goku_udc";
59 static const char driver_desc
[] = DRIVER_DESC
;
61 MODULE_AUTHOR("source@mvista.com");
62 MODULE_DESCRIPTION(DRIVER_DESC
);
63 MODULE_LICENSE("GPL");
67 * IN dma behaves ok under testing, though the IN-dma abort paths don't
68 * seem to behave quite as expected. Used by default.
70 * OUT dma documents design problems handling the common "short packet"
71 * transfer termination policy; it couldn't be enabled by default, even
72 * if the OUT-dma abort problems had a resolution.
74 static unsigned use_dma
= 1;
77 //#include <linux/moduleparam.h>
78 /* "modprobe goku_udc use_dma=1" etc
80 * 1 to use IN dma only (normal operation)
81 * 2 to use IN and OUT dma
83 module_param(use_dma
, uint
, S_IRUGO
);
86 /*-------------------------------------------------------------------------*/
88 static void nuke(struct goku_ep
*, int status
);
91 command(struct goku_udc_regs __iomem
*regs
, int command
, unsigned epnum
)
93 writel(COMMAND_EP(epnum
) | command
, ®s
->Command
);
98 goku_ep_enable(struct usb_ep
*_ep
, const struct usb_endpoint_descriptor
*desc
)
100 struct goku_udc
*dev
;
106 ep
= container_of(_ep
, struct goku_ep
, ep
);
107 if (!_ep
|| !desc
|| ep
->desc
108 || desc
->bDescriptorType
!= USB_DT_ENDPOINT
)
111 if (ep
== &dev
->ep
[0])
113 if (!dev
->driver
|| dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)
115 if (ep
->num
!= (desc
->bEndpointAddress
& 0x0f))
118 switch (desc
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) {
119 case USB_ENDPOINT_XFER_BULK
:
120 case USB_ENDPOINT_XFER_INT
:
126 if ((readl(ep
->reg_status
) & EPxSTATUS_EP_MASK
)
127 != EPxSTATUS_EP_INVALID
)
130 /* enabling the no-toggle interrupt mode would need an api hook */
132 max
= le16_to_cpu(get_unaligned(&desc
->wMaxPacketSize
));
142 mode
|= 2 << 1; /* bulk, or intr-with-toggle */
144 /* ep1/ep2 dma direction is chosen early; it works in the other
145 * direction, with pio. be cautious with out-dma.
147 ep
->is_in
= (USB_DIR_IN
& desc
->bEndpointAddress
) != 0;
150 ep
->dma
= (use_dma
!= 0) && (ep
->num
== UDC_MSTRD_ENDPOINT
);
152 ep
->dma
= (use_dma
== 2) && (ep
->num
== UDC_MSTWR_ENDPOINT
);
154 DBG(dev
, "%s out-dma hides short packets\n",
158 spin_lock_irqsave(&ep
->dev
->lock
, flags
);
160 /* ep1 and ep2 can do double buffering and/or dma */
162 struct goku_udc_regs __iomem
*regs
= ep
->dev
->regs
;
165 /* double buffer except (for now) with pio in */
166 tmp
= ((ep
->dma
|| !ep
->is_in
)
167 ? 0x10 /* double buffered */
168 : 0x11 /* single buffer */
170 tmp
|= readl(®s
->EPxSingle
);
171 writel(tmp
, ®s
->EPxSingle
);
173 tmp
= (ep
->dma
? 0x10/*dma*/ : 0x11/*pio*/) << ep
->num
;
174 tmp
|= readl(®s
->EPxBCS
);
175 writel(tmp
, ®s
->EPxBCS
);
177 writel(mode
, ep
->reg_mode
);
178 command(ep
->dev
->regs
, COMMAND_RESET
, ep
->num
);
179 ep
->ep
.maxpacket
= max
;
182 spin_unlock_irqrestore(&ep
->dev
->lock
, flags
);
184 DBG(dev
, "enable %s %s %s maxpacket %u\n", ep
->ep
.name
,
185 ep
->is_in
? "IN" : "OUT",
186 ep
->dma
? "dma" : "pio",
192 static void ep_reset(struct goku_udc_regs __iomem
*regs
, struct goku_ep
*ep
)
194 struct goku_udc
*dev
= ep
->dev
;
197 command(regs
, COMMAND_INVALID
, ep
->num
);
199 if (ep
->num
== UDC_MSTWR_ENDPOINT
)
200 dev
->int_enable
&= ~(INT_MSTWREND
202 else if (ep
->num
== UDC_MSTRD_ENDPOINT
)
203 dev
->int_enable
&= ~INT_MSTRDEND
;
204 dev
->int_enable
&= ~INT_EPxDATASET (ep
->num
);
206 dev
->int_enable
&= ~INT_EP0
;
207 writel(dev
->int_enable
, ®s
->int_enable
);
208 readl(®s
->int_enable
);
210 struct goku_udc_regs __iomem
*r
= ep
->dev
->regs
;
213 tmp
= readl(&r
->EPxSingle
);
214 tmp
&= ~(0x11 << ep
->num
);
215 writel(tmp
, &r
->EPxSingle
);
217 tmp
= readl(&r
->EPxBCS
);
218 tmp
&= ~(0x11 << ep
->num
);
219 writel(tmp
, &r
->EPxBCS
);
221 /* reset dma in case we're still using it */
225 master
= readl(®s
->dma_master
) & MST_RW_BITS
;
226 if (ep
->num
== UDC_MSTWR_ENDPOINT
) {
227 master
&= ~MST_W_BITS
;
228 master
|= MST_WR_RESET
;
230 master
&= ~MST_R_BITS
;
231 master
|= MST_RD_RESET
;
233 writel(master
, ®s
->dma_master
);
237 ep
->ep
.maxpacket
= MAX_FIFO_SIZE
;
244 static int goku_ep_disable(struct usb_ep
*_ep
)
247 struct goku_udc
*dev
;
250 ep
= container_of(_ep
, struct goku_ep
, ep
);
251 if (!_ep
|| !ep
->desc
)
254 if (dev
->ep0state
== EP0_SUSPEND
)
257 VDBG(dev
, "disable %s\n", _ep
->name
);
259 spin_lock_irqsave(&dev
->lock
, flags
);
260 nuke(ep
, -ESHUTDOWN
);
261 ep_reset(dev
->regs
, ep
);
262 spin_unlock_irqrestore(&dev
->lock
, flags
);
267 /*-------------------------------------------------------------------------*/
269 static struct usb_request
*
270 goku_alloc_request(struct usb_ep
*_ep
, gfp_t gfp_flags
)
272 struct goku_request
*req
;
276 req
= kzalloc(sizeof *req
, gfp_flags
);
280 req
->req
.dma
= DMA_ADDR_INVALID
;
281 INIT_LIST_HEAD(&req
->queue
);
286 goku_free_request(struct usb_ep
*_ep
, struct usb_request
*_req
)
288 struct goku_request
*req
;
293 req
= container_of(_req
, struct goku_request
, req
);
294 WARN_ON(!list_empty(&req
->queue
));
298 /*-------------------------------------------------------------------------*/
300 /* allocating buffers this way eliminates dma mapping overhead, which
301 * on some platforms will mean eliminating a per-io buffer copy. with
302 * some kinds of system caches, further tweaks may still be needed.
305 goku_alloc_buffer(struct usb_ep
*_ep
, unsigned bytes
,
306 dma_addr_t
*dma
, gfp_t gfp_flags
)
311 ep
= container_of(_ep
, struct goku_ep
, ep
);
314 *dma
= DMA_ADDR_INVALID
;
317 /* the main problem with this call is that it wastes memory
318 * on typical 1/N page allocations: it allocates 1-N pages.
320 #warning Using dma_alloc_coherent even with buffers smaller than a page.
321 retval
= dma_alloc_coherent(&ep
->dev
->pdev
->dev
,
322 bytes
, dma
, gfp_flags
);
324 retval
= kmalloc(bytes
, gfp_flags
);
329 goku_free_buffer(struct usb_ep
*_ep
, void *buf
, dma_addr_t dma
, unsigned bytes
)
331 /* free memory into the right allocator */
332 if (dma
!= DMA_ADDR_INVALID
) {
335 ep
= container_of(_ep
, struct goku_ep
, ep
);
338 dma_free_coherent(&ep
->dev
->pdev
->dev
, bytes
, buf
, dma
);
343 /*-------------------------------------------------------------------------*/
346 done(struct goku_ep
*ep
, struct goku_request
*req
, int status
)
348 struct goku_udc
*dev
;
349 unsigned stopped
= ep
->stopped
;
351 list_del_init(&req
->queue
);
353 if (likely(req
->req
.status
== -EINPROGRESS
))
354 req
->req
.status
= status
;
356 status
= req
->req
.status
;
360 pci_unmap_single(dev
->pdev
, req
->req
.dma
, req
->req
.length
,
361 ep
->is_in
? PCI_DMA_TODEVICE
: PCI_DMA_FROMDEVICE
);
362 req
->req
.dma
= DMA_ADDR_INVALID
;
367 if (status
&& status
!= -ESHUTDOWN
)
369 VDBG(dev
, "complete %s req %p stat %d len %u/%u\n",
370 ep
->ep
.name
, &req
->req
, status
,
371 req
->req
.actual
, req
->req
.length
);
373 /* don't modify queue heads during completion callback */
375 spin_unlock(&dev
->lock
);
376 req
->req
.complete(&ep
->ep
, &req
->req
);
377 spin_lock(&dev
->lock
);
378 ep
->stopped
= stopped
;
381 /*-------------------------------------------------------------------------*/
384 write_packet(u32 __iomem
*fifo
, u8
*buf
, struct goku_request
*req
, unsigned max
)
386 unsigned length
, count
;
388 length
= min(req
->req
.length
- req
->req
.actual
, max
);
389 req
->req
.actual
+= length
;
392 while (likely(count
--))
393 writel(*buf
++, fifo
);
397 // return: 0 = still running, 1 = completed, negative = errno
398 static int write_fifo(struct goku_ep
*ep
, struct goku_request
*req
)
400 struct goku_udc
*dev
= ep
->dev
;
406 tmp
= readl(&dev
->regs
->DataSet
);
407 buf
= req
->req
.buf
+ req
->req
.actual
;
411 if (unlikely(ep
->num
== 0 && dev
->ep0state
!= EP0_IN
))
414 /* NOTE: just single-buffered PIO-IN for now. */
415 if (unlikely((tmp
& DATASET_A(ep
->num
)) != 0))
418 /* clear our "packet available" irq */
420 writel(~INT_EPxDATASET(ep
->num
), &dev
->regs
->int_status
);
422 count
= write_packet(ep
->reg_fifo
, buf
, req
, ep
->ep
.maxpacket
);
424 /* last packet often short (sometimes a zlp, especially on ep0) */
425 if (unlikely(count
!= ep
->ep
.maxpacket
)) {
426 writel(~(1<<ep
->num
), &dev
->regs
->EOP
);
428 dev
->ep
[0].stopped
= 1;
429 dev
->ep0state
= EP0_STATUS
;
433 if (likely(req
->req
.length
!= req
->req
.actual
)
439 #if 0 /* printk seemed to trash is_last...*/
441 VDBG(dev
, "wrote %s %u bytes%s IN %u left %p\n",
442 ep
->ep
.name
, count
, is_last
? "/last" : "",
443 req
->req
.length
- req
->req
.actual
, req
);
446 /* requests complete when all IN data is in the FIFO,
447 * or sometimes later, if a zlp was needed.
457 static int read_fifo(struct goku_ep
*ep
, struct goku_request
*req
)
459 struct goku_udc_regs __iomem
*regs
;
462 unsigned bufferspace
, is_short
, dbuff
;
464 regs
= ep
->dev
->regs
;
466 buf
= req
->req
.buf
+ req
->req
.actual
;
469 if (unlikely(ep
->num
== 0 && ep
->dev
->ep0state
!= EP0_OUT
))
472 dbuff
= (ep
->num
== 1 || ep
->num
== 2);
474 /* ack dataset irq matching the status we'll handle */
476 writel(~INT_EPxDATASET(ep
->num
), ®s
->int_status
);
478 set
= readl(®s
->DataSet
) & DATASET_AB(ep
->num
);
479 size
= readl(®s
->EPxSizeLA
[ep
->num
]);
480 bufferspace
= req
->req
.length
- req
->req
.actual
;
482 /* usually do nothing without an OUT packet */
483 if (likely(ep
->num
!= 0 || bufferspace
!= 0)) {
484 if (unlikely(set
== 0))
486 /* use ep1/ep2 double-buffering for OUT */
487 if (!(size
& PACKET_ACTIVE
))
488 size
= readl(®s
->EPxSizeLB
[ep
->num
]);
489 if (!(size
& PACKET_ACTIVE
)) // "can't happen"
491 size
&= DATASIZE
; /* EPxSizeH == 0 */
493 /* ep0out no-out-data case for set_config, etc */
497 /* read all bytes from this packet */
498 req
->req
.actual
+= size
;
499 is_short
= (size
< ep
->ep
.maxpacket
);
501 VDBG(ep
->dev
, "read %s %u bytes%s OUT req %p %u/%u\n",
502 ep
->ep
.name
, size
, is_short
? "/S" : "",
503 req
, req
->req
.actual
, req
->req
.length
);
505 while (likely(size
-- != 0)) {
506 u8 byte
= (u8
) readl(ep
->reg_fifo
);
508 if (unlikely(bufferspace
== 0)) {
509 /* this happens when the driver's buffer
510 * is smaller than what the host sent.
511 * discard the extra data in this packet.
513 if (req
->req
.status
!= -EOVERFLOW
)
514 DBG(ep
->dev
, "%s overflow %u\n",
516 req
->req
.status
= -EOVERFLOW
;
524 if (unlikely(is_short
|| req
->req
.actual
== req
->req
.length
)) {
525 if (unlikely(ep
->num
== 0)) {
526 /* non-control endpoints now usable? */
527 if (ep
->dev
->req_config
)
528 writel(ep
->dev
->configured
529 ? USBSTATE_CONFIGURED
532 /* ep0out status stage */
533 writel(~(1<<0), ®s
->EOP
);
535 ep
->dev
->ep0state
= EP0_STATUS
;
539 /* empty the second buffer asap */
540 if (dbuff
&& !list_empty(&ep
->queue
)) {
541 req
= list_entry(ep
->queue
.next
,
542 struct goku_request
, queue
);
552 pio_irq_enable(struct goku_udc
*dev
,
553 struct goku_udc_regs __iomem
*regs
, int epnum
)
555 dev
->int_enable
|= INT_EPxDATASET (epnum
);
556 writel(dev
->int_enable
, ®s
->int_enable
);
557 /* write may still be posted */
561 pio_irq_disable(struct goku_udc
*dev
,
562 struct goku_udc_regs __iomem
*regs
, int epnum
)
564 dev
->int_enable
&= ~INT_EPxDATASET (epnum
);
565 writel(dev
->int_enable
, ®s
->int_enable
);
566 /* write may still be posted */
570 pio_advance(struct goku_ep
*ep
)
572 struct goku_request
*req
;
574 if (unlikely(list_empty (&ep
->queue
)))
576 req
= list_entry(ep
->queue
.next
, struct goku_request
, queue
);
577 (ep
->is_in
? write_fifo
: read_fifo
)(ep
, req
);
581 /*-------------------------------------------------------------------------*/
583 // return: 0 = q running, 1 = q stopped, negative = errno
584 static int start_dma(struct goku_ep
*ep
, struct goku_request
*req
)
586 struct goku_udc_regs __iomem
*regs
= ep
->dev
->regs
;
588 u32 start
= req
->req
.dma
;
589 u32 end
= start
+ req
->req
.length
- 1;
591 master
= readl(®s
->dma_master
) & MST_RW_BITS
;
593 /* re-init the bits affecting IN dma; careful with zlps */
594 if (likely(ep
->is_in
)) {
595 if (unlikely(master
& MST_RD_ENA
)) {
596 DBG (ep
->dev
, "start, IN active dma %03x!!\n",
600 writel(end
, ®s
->in_dma_end
);
601 writel(start
, ®s
->in_dma_start
);
603 master
&= ~MST_R_BITS
;
604 if (unlikely(req
->req
.length
== 0))
605 master
= MST_RD_ENA
| MST_RD_EOPB
;
606 else if ((req
->req
.length
% ep
->ep
.maxpacket
) != 0
608 master
= MST_RD_ENA
| MST_EOPB_ENA
;
610 master
= MST_RD_ENA
| MST_EOPB_DIS
;
612 ep
->dev
->int_enable
|= INT_MSTRDEND
;
614 /* Goku DMA-OUT merges short packets, which plays poorly with
615 * protocols where short packets mark the transfer boundaries.
616 * The chip supports a nonstandard policy with INT_MSTWRTMOUT,
617 * ending transfers after 3 SOFs; we don't turn it on.
620 if (unlikely(master
& MST_WR_ENA
)) {
621 DBG (ep
->dev
, "start, OUT active dma %03x!!\n",
625 writel(end
, ®s
->out_dma_end
);
626 writel(start
, ®s
->out_dma_start
);
628 master
&= ~MST_W_BITS
;
629 master
|= MST_WR_ENA
| MST_TIMEOUT_DIS
;
631 ep
->dev
->int_enable
|= INT_MSTWREND
|INT_MSTWRTMOUT
;
634 writel(master
, ®s
->dma_master
);
635 writel(ep
->dev
->int_enable
, ®s
->int_enable
);
639 static void dma_advance(struct goku_udc
*dev
, struct goku_ep
*ep
)
641 struct goku_request
*req
;
642 struct goku_udc_regs __iomem
*regs
= ep
->dev
->regs
;
645 master
= readl(®s
->dma_master
);
647 if (unlikely(list_empty(&ep
->queue
))) {
650 dev
->int_enable
&= ~INT_MSTRDEND
;
652 dev
->int_enable
&= ~(INT_MSTWREND
|INT_MSTWRTMOUT
);
653 writel(dev
->int_enable
, ®s
->int_enable
);
656 req
= list_entry(ep
->queue
.next
, struct goku_request
, queue
);
658 /* normal hw dma completion (not abort) */
659 if (likely(ep
->is_in
)) {
660 if (unlikely(master
& MST_RD_ENA
))
662 req
->req
.actual
= readl(®s
->in_dma_current
);
664 if (unlikely(master
& MST_WR_ENA
))
667 /* hardware merges short packets, and also hides packet
668 * overruns. a partial packet MAY be in the fifo here.
670 req
->req
.actual
= readl(®s
->out_dma_current
);
672 req
->req
.actual
-= req
->req
.dma
;
676 VDBG(dev
, "done %s %s dma, %u/%u bytes, req %p\n",
677 ep
->ep
.name
, ep
->is_in
? "IN" : "OUT",
678 req
->req
.actual
, req
->req
.length
, req
);
681 if (list_empty(&ep
->queue
))
683 req
= list_entry(ep
->queue
.next
, struct goku_request
, queue
);
684 (void) start_dma(ep
, req
);
687 static void abort_dma(struct goku_ep
*ep
, int status
)
689 struct goku_udc_regs __iomem
*regs
= ep
->dev
->regs
;
690 struct goku_request
*req
;
693 /* NAK future host requests, hoping the implicit delay lets the
694 * dma engine finish reading (or writing) its latest packet and
695 * empty the dma buffer (up to 16 bytes).
697 * This avoids needing to clean up a partial packet in the fifo;
698 * we can't do that for IN without side effects to HALT and TOGGLE.
700 command(regs
, COMMAND_FIFO_DISABLE
, ep
->num
);
701 req
= list_entry(ep
->queue
.next
, struct goku_request
, queue
);
702 master
= readl(®s
->dma_master
) & MST_RW_BITS
;
704 /* FIXME using these resets isn't usably documented. this may
705 * not work unless it's followed by disabling the endpoint.
707 * FIXME the OUT reset path doesn't even behave consistently.
710 if (unlikely((readl(®s
->dma_master
) & MST_RD_ENA
) == 0))
712 curr
= readl(®s
->in_dma_current
);
714 writel(curr
, ®s
->in_dma_end
);
715 writel(curr
, ®s
->in_dma_start
);
717 master
&= ~MST_R_BITS
;
718 master
|= MST_RD_RESET
;
719 writel(master
, ®s
->dma_master
);
721 if (readl(®s
->dma_master
) & MST_RD_ENA
)
722 DBG(ep
->dev
, "IN dma active after reset!\n");
725 if (unlikely((readl(®s
->dma_master
) & MST_WR_ENA
) == 0))
727 curr
= readl(®s
->out_dma_current
);
729 writel(curr
, ®s
->out_dma_end
);
730 writel(curr
, ®s
->out_dma_start
);
732 master
&= ~MST_W_BITS
;
733 master
|= MST_WR_RESET
;
734 writel(master
, ®s
->dma_master
);
736 if (readl(®s
->dma_master
) & MST_WR_ENA
)
737 DBG(ep
->dev
, "OUT dma active after reset!\n");
739 req
->req
.actual
= (curr
- req
->req
.dma
) + 1;
740 req
->req
.status
= status
;
742 VDBG(ep
->dev
, "%s %s %s %d/%d\n", __FUNCTION__
, ep
->ep
.name
,
743 ep
->is_in
? "IN" : "OUT",
744 req
->req
.actual
, req
->req
.length
);
746 command(regs
, COMMAND_FIFO_ENABLE
, ep
->num
);
751 /* dma already completed; no abort needed */
752 command(regs
, COMMAND_FIFO_ENABLE
, ep
->num
);
753 req
->req
.actual
= req
->req
.length
;
757 /*-------------------------------------------------------------------------*/
760 goku_queue(struct usb_ep
*_ep
, struct usb_request
*_req
, gfp_t gfp_flags
)
762 struct goku_request
*req
;
764 struct goku_udc
*dev
;
768 /* always require a cpu-view buffer so pio works */
769 req
= container_of(_req
, struct goku_request
, req
);
770 if (unlikely(!_req
|| !_req
->complete
771 || !_req
->buf
|| !list_empty(&req
->queue
)))
773 ep
= container_of(_ep
, struct goku_ep
, ep
);
774 if (unlikely(!_ep
|| (!ep
->desc
&& ep
->num
!= 0)))
777 if (unlikely(!dev
->driver
|| dev
->gadget
.speed
== USB_SPEED_UNKNOWN
))
780 /* can't touch registers when suspended */
781 if (dev
->ep0state
== EP0_SUSPEND
)
784 /* set up dma mapping in case the caller didn't */
785 if (ep
->dma
&& _req
->dma
== DMA_ADDR_INVALID
) {
786 _req
->dma
= pci_map_single(dev
->pdev
, _req
->buf
, _req
->length
,
787 ep
->is_in
? PCI_DMA_TODEVICE
: PCI_DMA_FROMDEVICE
);
792 VDBG(dev
, "%s queue req %p, len %u buf %p\n",
793 _ep
->name
, _req
, _req
->length
, _req
->buf
);
796 spin_lock_irqsave(&dev
->lock
, flags
);
798 _req
->status
= -EINPROGRESS
;
801 /* for ep0 IN without premature status, zlp is required and
802 * writing EOP starts the status stage (OUT).
804 if (unlikely(ep
->num
== 0 && ep
->is_in
))
807 /* kickstart this i/o queue? */
809 if (list_empty(&ep
->queue
) && likely(!ep
->stopped
)) {
810 /* dma: done after dma completion IRQ (or error)
811 * pio: done after last fifo operation
814 status
= start_dma(ep
, req
);
816 status
= (ep
->is_in
? write_fifo
: read_fifo
)(ep
, req
);
818 if (unlikely(status
!= 0)) {
824 } /* else pio or dma irq handler advances the queue. */
826 if (likely(req
!= 0))
827 list_add_tail(&req
->queue
, &ep
->queue
);
829 if (likely(!list_empty(&ep
->queue
))
830 && likely(ep
->num
!= 0)
832 && !(dev
->int_enable
& INT_EPxDATASET (ep
->num
)))
833 pio_irq_enable(dev
, dev
->regs
, ep
->num
);
835 spin_unlock_irqrestore(&dev
->lock
, flags
);
837 /* pci writes may still be posted */
841 /* dequeue ALL requests */
842 static void nuke(struct goku_ep
*ep
, int status
)
844 struct goku_request
*req
;
847 if (list_empty(&ep
->queue
))
850 abort_dma(ep
, status
);
851 while (!list_empty(&ep
->queue
)) {
852 req
= list_entry(ep
->queue
.next
, struct goku_request
, queue
);
853 done(ep
, req
, status
);
857 /* dequeue JUST ONE request */
858 static int goku_dequeue(struct usb_ep
*_ep
, struct usb_request
*_req
)
860 struct goku_request
*req
;
862 struct goku_udc
*dev
;
865 ep
= container_of(_ep
, struct goku_ep
, ep
);
866 if (!_ep
|| !_req
|| (!ep
->desc
&& ep
->num
!= 0))
872 /* we can't touch (dma) registers when suspended */
873 if (dev
->ep0state
== EP0_SUSPEND
)
876 VDBG(dev
, "%s %s %s %s %p\n", __FUNCTION__
, _ep
->name
,
877 ep
->is_in
? "IN" : "OUT",
878 ep
->dma
? "dma" : "pio",
881 spin_lock_irqsave(&dev
->lock
, flags
);
883 /* make sure it's actually queued on this endpoint */
884 list_for_each_entry (req
, &ep
->queue
, queue
) {
885 if (&req
->req
== _req
)
888 if (&req
->req
!= _req
) {
889 spin_unlock_irqrestore (&dev
->lock
, flags
);
893 if (ep
->dma
&& ep
->queue
.next
== &req
->queue
&& !ep
->stopped
) {
894 abort_dma(ep
, -ECONNRESET
);
895 done(ep
, req
, -ECONNRESET
);
896 dma_advance(dev
, ep
);
897 } else if (!list_empty(&req
->queue
))
898 done(ep
, req
, -ECONNRESET
);
901 spin_unlock_irqrestore(&dev
->lock
, flags
);
903 return req
? 0 : -EOPNOTSUPP
;
906 /*-------------------------------------------------------------------------*/
908 static void goku_clear_halt(struct goku_ep
*ep
)
910 // assert (ep->num !=0)
911 VDBG(ep
->dev
, "%s clear halt\n", ep
->ep
.name
);
912 command(ep
->dev
->regs
, COMMAND_SETDATA0
, ep
->num
);
913 command(ep
->dev
->regs
, COMMAND_STALL_CLEAR
, ep
->num
);
917 struct goku_request
*req
;
919 if (list_empty(&ep
->queue
))
921 req
= list_entry(ep
->queue
.next
, struct goku_request
,
923 (void) start_dma(ep
, req
);
929 static int goku_set_halt(struct usb_ep
*_ep
, int value
)
937 ep
= container_of (_ep
, struct goku_ep
, ep
);
941 ep
->dev
->ep0state
= EP0_STALL
;
942 ep
->dev
->ep
[0].stopped
= 1;
946 /* don't change EPxSTATUS_EP_INVALID to READY */
947 } else if (!ep
->desc
) {
948 DBG(ep
->dev
, "%s %s inactive?\n", __FUNCTION__
, ep
->ep
.name
);
952 spin_lock_irqsave(&ep
->dev
->lock
, flags
);
953 if (!list_empty(&ep
->queue
))
955 else if (ep
->is_in
&& value
956 /* data in (either) packet buffer? */
957 && (readl(&ep
->dev
->regs
->DataSet
)
958 & DATASET_AB(ep
->num
)))
964 VDBG(ep
->dev
, "%s set halt\n", ep
->ep
.name
);
965 command(ep
->dev
->regs
, COMMAND_STALL
, ep
->num
);
966 readl(ep
->reg_status
);
968 spin_unlock_irqrestore(&ep
->dev
->lock
, flags
);
972 static int goku_fifo_status(struct usb_ep
*_ep
)
975 struct goku_udc_regs __iomem
*regs
;
980 ep
= container_of(_ep
, struct goku_ep
, ep
);
982 /* size is only reported sanely for OUT */
986 /* ignores 16-byte dma buffer; SizeH == 0 */
987 regs
= ep
->dev
->regs
;
988 size
= readl(®s
->EPxSizeLA
[ep
->num
]) & DATASIZE
;
989 size
+= readl(®s
->EPxSizeLB
[ep
->num
]) & DATASIZE
;
990 VDBG(ep
->dev
, "%s %s %u\n", __FUNCTION__
, ep
->ep
.name
, size
);
994 static void goku_fifo_flush(struct usb_ep
*_ep
)
997 struct goku_udc_regs __iomem
*regs
;
1002 ep
= container_of(_ep
, struct goku_ep
, ep
);
1003 VDBG(ep
->dev
, "%s %s\n", __FUNCTION__
, ep
->ep
.name
);
1005 /* don't change EPxSTATUS_EP_INVALID to READY */
1006 if (!ep
->desc
&& ep
->num
!= 0) {
1007 DBG(ep
->dev
, "%s %s inactive?\n", __FUNCTION__
, ep
->ep
.name
);
1011 regs
= ep
->dev
->regs
;
1012 size
= readl(®s
->EPxSizeLA
[ep
->num
]);
1015 /* Non-desirable behavior: FIFO_CLEAR also clears the
1016 * endpoint halt feature. For OUT, we _could_ just read
1017 * the bytes out (PIO, if !ep->dma); for in, no choice.
1020 command(regs
, COMMAND_FIFO_CLEAR
, ep
->num
);
1023 static struct usb_ep_ops goku_ep_ops
= {
1024 .enable
= goku_ep_enable
,
1025 .disable
= goku_ep_disable
,
1027 .alloc_request
= goku_alloc_request
,
1028 .free_request
= goku_free_request
,
1030 .alloc_buffer
= goku_alloc_buffer
,
1031 .free_buffer
= goku_free_buffer
,
1033 .queue
= goku_queue
,
1034 .dequeue
= goku_dequeue
,
1036 .set_halt
= goku_set_halt
,
1037 .fifo_status
= goku_fifo_status
,
1038 .fifo_flush
= goku_fifo_flush
,
1041 /*-------------------------------------------------------------------------*/
1043 static int goku_get_frame(struct usb_gadget
*_gadget
)
1048 static const struct usb_gadget_ops goku_ops
= {
1049 .get_frame
= goku_get_frame
,
1054 /*-------------------------------------------------------------------------*/
1056 static inline char *dmastr(void)
1059 return "(dma disabled)";
1060 else if (use_dma
== 2)
1061 return "(dma IN and OUT)";
1066 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1068 static const char proc_node_name
[] = "driver/udc";
1070 #define FOURBITS "%s%s%s%s"
1071 #define EIGHTBITS FOURBITS FOURBITS
1074 dump_intmask(const char *label
, u32 mask
, char **next
, unsigned *size
)
1078 /* int_status is the same format ... */
1079 t
= scnprintf(*next
, *size
,
1080 "%s %05X =" FOURBITS EIGHTBITS EIGHTBITS
"\n",
1082 (mask
& INT_PWRDETECT
) ? " power" : "",
1083 (mask
& INT_SYSERROR
) ? " sys" : "",
1084 (mask
& INT_MSTRDEND
) ? " in-dma" : "",
1085 (mask
& INT_MSTWRTMOUT
) ? " wrtmo" : "",
1087 (mask
& INT_MSTWREND
) ? " out-dma" : "",
1088 (mask
& INT_MSTWRSET
) ? " wrset" : "",
1089 (mask
& INT_ERR
) ? " err" : "",
1090 (mask
& INT_SOF
) ? " sof" : "",
1092 (mask
& INT_EP3NAK
) ? " ep3nak" : "",
1093 (mask
& INT_EP2NAK
) ? " ep2nak" : "",
1094 (mask
& INT_EP1NAK
) ? " ep1nak" : "",
1095 (mask
& INT_EP3DATASET
) ? " ep3" : "",
1097 (mask
& INT_EP2DATASET
) ? " ep2" : "",
1098 (mask
& INT_EP1DATASET
) ? " ep1" : "",
1099 (mask
& INT_STATUSNAK
) ? " ep0snak" : "",
1100 (mask
& INT_STATUS
) ? " ep0status" : "",
1102 (mask
& INT_SETUP
) ? " setup" : "",
1103 (mask
& INT_ENDPOINT0
) ? " ep0" : "",
1104 (mask
& INT_USBRESET
) ? " reset" : "",
1105 (mask
& INT_SUSPEND
) ? " suspend" : "");
1112 udc_proc_read(char *buffer
, char **start
, off_t off
, int count
,
1113 int *eof
, void *_dev
)
1116 struct goku_udc
*dev
= _dev
;
1117 struct goku_udc_regs __iomem
*regs
= dev
->regs
;
1119 unsigned size
= count
;
1120 unsigned long flags
;
1121 int i
, t
, is_usb_connected
;
1127 local_irq_save(flags
);
1129 /* basic device status */
1130 tmp
= readl(®s
->power_detect
);
1131 is_usb_connected
= tmp
& PW_DETECT
;
1132 t
= scnprintf(next
, size
,
1134 "%s version: %s %s\n"
1135 "Gadget driver: %s\n"
1138 pci_name(dev
->pdev
), driver_desc
,
1139 driver_name
, DRIVER_VERSION
, dmastr(),
1140 dev
->driver
? dev
->driver
->driver
.name
: "(none)",
1142 ? ((tmp
& PW_PULLUP
) ? "full speed" : "powered")
1145 switch(dev
->ep0state
){
1146 case EP0_DISCONNECT
: tmp
= "ep0_disconnect"; break;
1147 case EP0_IDLE
: tmp
= "ep0_idle"; break;
1148 case EP0_IN
: tmp
= "ep0_in"; break;
1149 case EP0_OUT
: tmp
= "ep0_out"; break;
1150 case EP0_STATUS
: tmp
= "ep0_status"; break;
1151 case EP0_STALL
: tmp
= "ep0_stall"; break;
1152 case EP0_SUSPEND
: tmp
= "ep0_suspend"; break;
1153 default: tmp
= "ep0_?"; break;
1159 dump_intmask("int_status", readl(®s
->int_status
), &next
, &size
);
1160 dump_intmask("int_enable", readl(®s
->int_enable
), &next
, &size
);
1162 if (!is_usb_connected
|| !dev
->driver
|| (tmp
& PW_PULLUP
) == 0)
1165 /* registers for (active) device and ep0 */
1166 t
= scnprintf(next
, size
, "\nirqs %lu\ndataset %02x "
1167 "single.bcs %02x.%02x state %x addr %u\n",
1168 dev
->irqs
, readl(®s
->DataSet
),
1169 readl(®s
->EPxSingle
), readl(®s
->EPxBCS
),
1170 readl(®s
->UsbState
),
1171 readl(®s
->address
));
1175 tmp
= readl(®s
->dma_master
);
1176 t
= scnprintf(next
, size
,
1177 "dma %03X =" EIGHTBITS
"%s %s\n", tmp
,
1178 (tmp
& MST_EOPB_DIS
) ? " eopb-" : "",
1179 (tmp
& MST_EOPB_ENA
) ? " eopb+" : "",
1180 (tmp
& MST_TIMEOUT_DIS
) ? " tmo-" : "",
1181 (tmp
& MST_TIMEOUT_ENA
) ? " tmo+" : "",
1183 (tmp
& MST_RD_EOPB
) ? " eopb" : "",
1184 (tmp
& MST_RD_RESET
) ? " in_reset" : "",
1185 (tmp
& MST_WR_RESET
) ? " out_reset" : "",
1186 (tmp
& MST_RD_ENA
) ? " IN" : "",
1188 (tmp
& MST_WR_ENA
) ? " OUT" : "",
1189 (tmp
& MST_CONNECTION
)
1195 /* dump endpoint queues */
1196 for (i
= 0; i
< 4; i
++) {
1197 struct goku_ep
*ep
= &dev
->ep
[i
];
1198 struct goku_request
*req
;
1204 tmp
= readl(ep
->reg_status
);
1205 t
= scnprintf(next
, size
,
1206 "%s %s max %u %s, irqs %lu, "
1207 "status %02x (%s) " FOURBITS
"\n",
1209 ep
->is_in
? "in" : "out",
1211 ep
->dma
? "dma" : "pio",
1214 switch (tmp
& EPxSTATUS_EP_MASK
) {
1215 case EPxSTATUS_EP_READY
:
1217 case EPxSTATUS_EP_DATAIN
:
1218 s
= "packet"; break;
1219 case EPxSTATUS_EP_FULL
:
1221 case EPxSTATUS_EP_TX_ERR
: // host will retry
1222 s
= "tx_err"; break;
1223 case EPxSTATUS_EP_RX_ERR
:
1224 s
= "rx_err"; break;
1225 case EPxSTATUS_EP_BUSY
: /* ep0 only */
1227 case EPxSTATUS_EP_STALL
:
1229 case EPxSTATUS_EP_INVALID
: // these "can't happen"
1230 s
= "invalid"; break;
1234 (tmp
& EPxSTATUS_TOGGLE
) ? "data1" : "data0",
1235 (tmp
& EPxSTATUS_SUSPEND
) ? " suspend" : "",
1236 (tmp
& EPxSTATUS_FIFO_DISABLE
) ? " disable" : "",
1237 (tmp
& EPxSTATUS_STAGE_ERROR
) ? " ep0stat" : ""
1239 if (t
<= 0 || t
> size
)
1244 if (list_empty(&ep
->queue
)) {
1245 t
= scnprintf(next
, size
, "\t(nothing queued)\n");
1246 if (t
<= 0 || t
> size
)
1252 list_for_each_entry(req
, &ep
->queue
, queue
) {
1253 if (ep
->dma
&& req
->queue
.prev
== &ep
->queue
) {
1254 if (i
== UDC_MSTRD_ENDPOINT
)
1255 tmp
= readl(®s
->in_dma_current
);
1257 tmp
= readl(®s
->out_dma_current
);
1258 tmp
-= req
->req
.dma
;
1261 tmp
= req
->req
.actual
;
1263 t
= scnprintf(next
, size
,
1264 "\treq %p len %u/%u buf %p\n",
1265 &req
->req
, tmp
, req
->req
.length
,
1267 if (t
<= 0 || t
> size
)
1275 local_irq_restore(flags
);
1277 return count
- size
;
1280 #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
1282 /*-------------------------------------------------------------------------*/
1284 static void udc_reinit (struct goku_udc
*dev
)
1286 static char *names
[] = { "ep0", "ep1-bulk", "ep2-bulk", "ep3-bulk" };
1290 INIT_LIST_HEAD (&dev
->gadget
.ep_list
);
1291 dev
->gadget
.ep0
= &dev
->ep
[0].ep
;
1292 dev
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1293 dev
->ep0state
= EP0_DISCONNECT
;
1296 for (i
= 0; i
< 4; i
++) {
1297 struct goku_ep
*ep
= &dev
->ep
[i
];
1300 ep
->ep
.name
= names
[i
];
1301 ep
->reg_fifo
= &dev
->regs
->ep_fifo
[i
];
1302 ep
->reg_status
= &dev
->regs
->ep_status
[i
];
1303 ep
->reg_mode
= &dev
->regs
->ep_mode
[i
];
1305 ep
->ep
.ops
= &goku_ep_ops
;
1306 list_add_tail (&ep
->ep
.ep_list
, &dev
->gadget
.ep_list
);
1308 INIT_LIST_HEAD (&ep
->queue
);
1313 dev
->ep
[0].reg_mode
= NULL
;
1314 dev
->ep
[0].ep
.maxpacket
= MAX_EP0_SIZE
;
1315 list_del_init (&dev
->ep
[0].ep
.ep_list
);
1318 static void udc_reset(struct goku_udc
*dev
)
1320 struct goku_udc_regs __iomem
*regs
= dev
->regs
;
1322 writel(0, ®s
->power_detect
);
1323 writel(0, ®s
->int_enable
);
1324 readl(®s
->int_enable
);
1325 dev
->int_enable
= 0;
1327 /* deassert reset, leave USB D+ at hi-Z (no pullup)
1328 * don't let INT_PWRDETECT sequence begin
1331 writel(PW_RESETB
, ®s
->power_detect
);
1332 readl(®s
->int_enable
);
1335 static void ep0_start(struct goku_udc
*dev
)
1337 struct goku_udc_regs __iomem
*regs
= dev
->regs
;
1340 VDBG(dev
, "%s\n", __FUNCTION__
);
1344 //writel(MST_EOPB_ENA | MST_TIMEOUT_ENA, ®s->dma_master);
1346 /* hw handles set_address, set_feature, get_status; maybe more */
1347 writel( G_REQMODE_SET_INTF
| G_REQMODE_GET_INTF
1348 | G_REQMODE_SET_CONF
| G_REQMODE_GET_CONF
1349 | G_REQMODE_GET_DESC
1350 | G_REQMODE_CLEAR_FEAT
1353 for (i
= 0; i
< 4; i
++)
1354 dev
->ep
[i
].irqs
= 0;
1356 /* can't modify descriptors after writing UsbReady */
1357 for (i
= 0; i
< DESC_LEN
; i
++)
1358 writel(0, ®s
->descriptors
[i
]);
1359 writel(0, ®s
->UsbReady
);
1361 /* expect ep0 requests when the host drops reset */
1362 writel(PW_RESETB
| PW_PULLUP
, ®s
->power_detect
);
1363 dev
->int_enable
= INT_DEVWIDE
| INT_EP0
;
1364 writel(dev
->int_enable
, &dev
->regs
->int_enable
);
1365 readl(®s
->int_enable
);
1366 dev
->gadget
.speed
= USB_SPEED_FULL
;
1367 dev
->ep0state
= EP0_IDLE
;
1370 static void udc_enable(struct goku_udc
*dev
)
1372 /* start enumeration now, or after power detect irq */
1373 if (readl(&dev
->regs
->power_detect
) & PW_DETECT
)
1376 DBG(dev
, "%s\n", __FUNCTION__
);
1377 dev
->int_enable
= INT_PWRDETECT
;
1378 writel(dev
->int_enable
, &dev
->regs
->int_enable
);
1382 /*-------------------------------------------------------------------------*/
1384 /* keeping it simple:
1385 * - one bus driver, initted first;
1386 * - one function driver, initted second
1389 static struct goku_udc
*the_controller
;
1391 /* when a driver is successfully registered, it will receive
1392 * control requests including set_configuration(), which enables
1393 * non-control requests. then usb traffic follows until a
1394 * disconnect is reported. then a host may connect again, or
1395 * the driver might get unbound.
1397 int usb_gadget_register_driver(struct usb_gadget_driver
*driver
)
1399 struct goku_udc
*dev
= the_controller
;
1403 || driver
->speed
!= USB_SPEED_FULL
1405 || !driver
->disconnect
1413 /* hook up the driver */
1414 driver
->driver
.bus
= NULL
;
1415 dev
->driver
= driver
;
1416 dev
->gadget
.dev
.driver
= &driver
->driver
;
1417 retval
= driver
->bind(&dev
->gadget
);
1419 DBG(dev
, "bind to driver %s --> error %d\n",
1420 driver
->driver
.name
, retval
);
1422 dev
->gadget
.dev
.driver
= NULL
;
1426 /* then enable host detection and ep0; and we're ready
1427 * for set_configuration as well as eventual disconnect.
1431 DBG(dev
, "registered gadget driver '%s'\n", driver
->driver
.name
);
1434 EXPORT_SYMBOL(usb_gadget_register_driver
);
1437 stop_activity(struct goku_udc
*dev
, struct usb_gadget_driver
*driver
)
1441 DBG (dev
, "%s\n", __FUNCTION__
);
1443 if (dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)
1446 /* disconnect gadget driver after quiesceing hw and the driver */
1448 for (i
= 0; i
< 4; i
++)
1449 nuke(&dev
->ep
[i
], -ESHUTDOWN
);
1451 spin_unlock(&dev
->lock
);
1452 driver
->disconnect(&dev
->gadget
);
1453 spin_lock(&dev
->lock
);
1460 int usb_gadget_unregister_driver(struct usb_gadget_driver
*driver
)
1462 struct goku_udc
*dev
= the_controller
;
1463 unsigned long flags
;
1467 if (!driver
|| driver
!= dev
->driver
|| !driver
->unbind
)
1470 spin_lock_irqsave(&dev
->lock
, flags
);
1472 stop_activity(dev
, driver
);
1473 spin_unlock_irqrestore(&dev
->lock
, flags
);
1475 driver
->unbind(&dev
->gadget
);
1477 DBG(dev
, "unregistered driver '%s'\n", driver
->driver
.name
);
1480 EXPORT_SYMBOL(usb_gadget_unregister_driver
);
1483 /*-------------------------------------------------------------------------*/
1485 static void ep0_setup(struct goku_udc
*dev
)
1487 struct goku_udc_regs __iomem
*regs
= dev
->regs
;
1488 struct usb_ctrlrequest ctrl
;
1491 /* read SETUP packet and enter DATA stage */
1492 ctrl
.bRequestType
= readl(®s
->bRequestType
);
1493 ctrl
.bRequest
= readl(®s
->bRequest
);
1494 ctrl
.wValue
= cpu_to_le16((readl(®s
->wValueH
) << 8)
1495 | readl(®s
->wValueL
));
1496 ctrl
.wIndex
= cpu_to_le16((readl(®s
->wIndexH
) << 8)
1497 | readl(®s
->wIndexL
));
1498 ctrl
.wLength
= cpu_to_le16((readl(®s
->wLengthH
) << 8)
1499 | readl(®s
->wLengthL
));
1500 writel(0, ®s
->SetupRecv
);
1502 nuke(&dev
->ep
[0], 0);
1503 dev
->ep
[0].stopped
= 0;
1504 if (likely(ctrl
.bRequestType
& USB_DIR_IN
)) {
1505 dev
->ep
[0].is_in
= 1;
1506 dev
->ep0state
= EP0_IN
;
1507 /* detect early status stages */
1508 writel(ICONTROL_STATUSNAK
, &dev
->regs
->IntControl
);
1510 dev
->ep
[0].is_in
= 0;
1511 dev
->ep0state
= EP0_OUT
;
1513 /* NOTE: CLEAR_FEATURE is done in software so that we can
1514 * synchronize transfer restarts after bulk IN stalls. data
1515 * won't even enter the fifo until the halt is cleared.
1517 switch (ctrl
.bRequest
) {
1518 case USB_REQ_CLEAR_FEATURE
:
1519 switch (ctrl
.bRequestType
) {
1520 case USB_RECIP_ENDPOINT
:
1521 tmp
= le16_to_cpu(ctrl
.wIndex
) & 0x0f;
1522 /* active endpoint */
1523 if (tmp
> 3 || (!dev
->ep
[tmp
].desc
&& tmp
!= 0))
1525 if (ctrl
.wIndex
& __constant_cpu_to_le16(
1527 if (!dev
->ep
[tmp
].is_in
)
1530 if (dev
->ep
[tmp
].is_in
)
1533 if (ctrl
.wValue
!= __constant_cpu_to_le16(
1537 goku_clear_halt(&dev
->ep
[tmp
]);
1539 /* start ep0out status stage */
1540 writel(~(1<<0), ®s
->EOP
);
1541 dev
->ep
[0].stopped
= 1;
1542 dev
->ep0state
= EP0_STATUS
;
1544 case USB_RECIP_DEVICE
:
1545 /* device remote wakeup: always clear */
1546 if (ctrl
.wValue
!= __constant_cpu_to_le16(1))
1548 VDBG(dev
, "clear dev remote wakeup\n");
1550 case USB_RECIP_INTERFACE
:
1552 default: /* pass to gadget driver */
1562 VDBG(dev
, "SETUP %02x.%02x v%04x i%04x l%04x\n",
1563 ctrl
.bRequestType
, ctrl
.bRequest
,
1564 le16_to_cpu(ctrl
.wValue
), le16_to_cpu(ctrl
.wIndex
),
1565 le16_to_cpu(ctrl
.wLength
));
1568 /* hw wants to know when we're configured (or not) */
1569 dev
->req_config
= (ctrl
.bRequest
== USB_REQ_SET_CONFIGURATION
1570 && ctrl
.bRequestType
== USB_RECIP_DEVICE
);
1571 if (unlikely(dev
->req_config
))
1572 dev
->configured
= (ctrl
.wValue
!= __constant_cpu_to_le16(0));
1574 /* delegate everything to the gadget driver.
1575 * it may respond after this irq handler returns.
1577 spin_unlock (&dev
->lock
);
1578 tmp
= dev
->driver
->setup(&dev
->gadget
, &ctrl
);
1579 spin_lock (&dev
->lock
);
1580 if (unlikely(tmp
< 0)) {
1583 VDBG(dev
, "req %02x.%02x protocol STALL; err %d\n",
1584 ctrl
.bRequestType
, ctrl
.bRequest
, tmp
);
1586 command(regs
, COMMAND_STALL
, 0);
1587 dev
->ep
[0].stopped
= 1;
1588 dev
->ep0state
= EP0_STALL
;
1591 /* expect at least one data or status stage irq */
1594 #define ACK(irqbit) { \
1596 writel(~irqbit, ®s->int_status); \
1600 static irqreturn_t
goku_irq(int irq
, void *_dev
)
1602 struct goku_udc
*dev
= _dev
;
1603 struct goku_udc_regs __iomem
*regs
= dev
->regs
;
1605 u32 stat
, handled
= 0;
1606 unsigned i
, rescans
= 5;
1608 spin_lock(&dev
->lock
);
1611 stat
= readl(®s
->int_status
) & dev
->int_enable
;
1616 /* device-wide irqs */
1617 if (unlikely(stat
& INT_DEVWIDE
)) {
1618 if (stat
& INT_SYSERROR
) {
1619 ERROR(dev
, "system error\n");
1620 stop_activity(dev
, dev
->driver
);
1623 // FIXME have a neater way to prevent re-enumeration
1627 if (stat
& INT_PWRDETECT
) {
1628 writel(~stat
, ®s
->int_status
);
1629 if (readl(&dev
->regs
->power_detect
) & PW_DETECT
) {
1630 VDBG(dev
, "connect\n");
1633 DBG(dev
, "disconnect\n");
1634 if (dev
->gadget
.speed
== USB_SPEED_FULL
)
1635 stop_activity(dev
, dev
->driver
);
1636 dev
->ep0state
= EP0_DISCONNECT
;
1637 dev
->int_enable
= INT_DEVWIDE
;
1638 writel(dev
->int_enable
, &dev
->regs
->int_enable
);
1644 if (stat
& INT_SUSPEND
) {
1646 if (readl(®s
->ep_status
[0]) & EPxSTATUS_SUSPEND
) {
1647 switch (dev
->ep0state
) {
1648 case EP0_DISCONNECT
:
1654 DBG(dev
, "USB suspend\n");
1655 dev
->ep0state
= EP0_SUSPEND
;
1656 if (dev
->gadget
.speed
!= USB_SPEED_UNKNOWN
1658 && dev
->driver
->suspend
) {
1659 spin_unlock(&dev
->lock
);
1660 dev
->driver
->suspend(&dev
->gadget
);
1661 spin_lock(&dev
->lock
);
1664 if (dev
->ep0state
!= EP0_SUSPEND
) {
1665 DBG(dev
, "bogus USB resume %d\n",
1669 DBG(dev
, "USB resume\n");
1670 dev
->ep0state
= EP0_IDLE
;
1671 if (dev
->gadget
.speed
!= USB_SPEED_UNKNOWN
1673 && dev
->driver
->resume
) {
1674 spin_unlock(&dev
->lock
);
1675 dev
->driver
->resume(&dev
->gadget
);
1676 spin_lock(&dev
->lock
);
1681 if (stat
& INT_USBRESET
) { /* hub reset done */
1683 INFO(dev
, "USB reset done, gadget %s\n",
1684 dev
->driver
->driver
.name
);
1686 // and INT_ERR on some endpoint's crc/bitstuff/... problem
1689 /* progress ep0 setup, data, or status stages.
1690 * no transition {EP0_STATUS, EP0_STALL} --> EP0_IDLE; saves irqs
1692 if (stat
& INT_SETUP
) {
1697 if (stat
& INT_STATUSNAK
) {
1698 ACK(INT_STATUSNAK
|INT_ENDPOINT0
);
1699 if (dev
->ep0state
== EP0_IN
) {
1703 writel(~(1<<0), ®s
->EOP
);
1704 dev
->ep0state
= EP0_STATUS
;
1707 if (stat
& INT_ENDPOINT0
) {
1714 /* dma completion */
1715 if (stat
& INT_MSTRDEND
) { /* IN */
1717 ep
= &dev
->ep
[UDC_MSTRD_ENDPOINT
];
1719 dma_advance(dev
, ep
);
1721 if (stat
& INT_MSTWREND
) { /* OUT */
1723 ep
= &dev
->ep
[UDC_MSTWR_ENDPOINT
];
1725 dma_advance(dev
, ep
);
1727 if (stat
& INT_MSTWRTMOUT
) { /* OUT */
1728 ACK(INT_MSTWRTMOUT
);
1729 ep
= &dev
->ep
[UDC_MSTWR_ENDPOINT
];
1731 ERROR(dev
, "%s write timeout ?\n", ep
->ep
.name
);
1732 // reset dma? then dma_advance()
1736 for (i
= 1; i
< 4; i
++) {
1737 u32 tmp
= INT_EPxDATASET(i
);
1743 if (list_empty (&ep
->queue
))
1744 pio_irq_disable(dev
, regs
, i
);
1754 (void)readl(®s
->int_enable
);
1755 spin_unlock(&dev
->lock
);
1757 DBG(dev
, "unhandled irq status: %05x (%05x, %05x)\n", stat
,
1758 readl(®s
->int_status
), dev
->int_enable
);
1759 return IRQ_RETVAL(handled
);
1764 /*-------------------------------------------------------------------------*/
1766 static void gadget_release(struct device
*_dev
)
1768 struct goku_udc
*dev
= dev_get_drvdata(_dev
);
1773 /* tear down the binding between this driver and the pci device */
1775 static void goku_remove(struct pci_dev
*pdev
)
1777 struct goku_udc
*dev
= pci_get_drvdata(pdev
);
1779 DBG(dev
, "%s\n", __FUNCTION__
);
1781 BUG_ON(dev
->driver
);
1783 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1784 remove_proc_entry(proc_node_name
, NULL
);
1789 free_irq(pdev
->irq
, dev
);
1792 if (dev
->got_region
)
1793 release_mem_region(pci_resource_start (pdev
, 0),
1794 pci_resource_len (pdev
, 0));
1796 pci_disable_device(pdev
);
1797 device_unregister(&dev
->gadget
.dev
);
1799 pci_set_drvdata(pdev
, NULL
);
1801 the_controller
= NULL
;
1803 INFO(dev
, "unbind\n");
1806 /* wrap this driver around the specified pci device, but
1807 * don't respond over USB until a gadget driver binds to us.
1810 static int goku_probe(struct pci_dev
*pdev
, const struct pci_device_id
*id
)
1812 struct goku_udc
*dev
= NULL
;
1813 unsigned long resource
, len
;
1814 void __iomem
*base
= NULL
;
1817 /* if you want to support more than one controller in a system,
1818 * usb_gadget_driver_{register,unregister}() must change.
1820 if (the_controller
) {
1821 WARN(dev
, "ignoring %s\n", pci_name(pdev
));
1825 printk(KERN_ERR
"Check PCI %s IRQ setup!\n", pci_name(pdev
));
1830 /* alloc, and start init */
1831 dev
= kmalloc (sizeof *dev
, GFP_KERNEL
);
1833 pr_debug("enomem %s\n", pci_name(pdev
));
1838 memset(dev
, 0, sizeof *dev
);
1839 spin_lock_init(&dev
->lock
);
1841 dev
->gadget
.ops
= &goku_ops
;
1843 /* the "gadget" abstracts/virtualizes the controller */
1844 strcpy(dev
->gadget
.dev
.bus_id
, "gadget");
1845 dev
->gadget
.dev
.parent
= &pdev
->dev
;
1846 dev
->gadget
.dev
.dma_mask
= pdev
->dev
.dma_mask
;
1847 dev
->gadget
.dev
.release
= gadget_release
;
1848 dev
->gadget
.name
= driver_name
;
1850 /* now all the pci goodies ... */
1851 retval
= pci_enable_device(pdev
);
1853 DBG(dev
, "can't enable, %d\n", retval
);
1858 resource
= pci_resource_start(pdev
, 0);
1859 len
= pci_resource_len(pdev
, 0);
1860 if (!request_mem_region(resource
, len
, driver_name
)) {
1861 DBG(dev
, "controller already in use\n");
1865 dev
->got_region
= 1;
1867 base
= ioremap_nocache(resource
, len
);
1869 DBG(dev
, "can't map memory\n");
1873 dev
->regs
= (struct goku_udc_regs __iomem
*) base
;
1875 pci_set_drvdata(pdev
, dev
);
1876 INFO(dev
, "%s\n", driver_desc
);
1877 INFO(dev
, "version: " DRIVER_VERSION
" %s\n", dmastr());
1878 INFO(dev
, "irq %d, pci mem %p\n", pdev
->irq
, base
);
1880 /* init to known state, then setup irqs */
1883 if (request_irq(pdev
->irq
, goku_irq
, IRQF_SHARED
/*|IRQF_SAMPLE_RANDOM*/,
1884 driver_name
, dev
) != 0) {
1885 DBG(dev
, "request interrupt %d failed\n", pdev
->irq
);
1891 pci_set_master(pdev
);
1894 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1895 create_proc_read_entry(proc_node_name
, 0, NULL
, udc_proc_read
, dev
);
1899 the_controller
= dev
;
1900 device_register(&dev
->gadget
.dev
);
1911 /*-------------------------------------------------------------------------*/
1913 static struct pci_device_id pci_ids
[] = { {
1914 .class = ((PCI_CLASS_SERIAL_USB
<< 8) | 0xfe),
1916 .vendor
= 0x102f, /* Toshiba */
1917 .device
= 0x0107, /* this UDC */
1918 .subvendor
= PCI_ANY_ID
,
1919 .subdevice
= PCI_ANY_ID
,
1921 }, { /* end: all zeroes */ }
1923 MODULE_DEVICE_TABLE (pci
, pci_ids
);
1925 static struct pci_driver goku_pci_driver
= {
1926 .name
= (char *) driver_name
,
1927 .id_table
= pci_ids
,
1929 .probe
= goku_probe
,
1930 .remove
= goku_remove
,
1932 /* FIXME add power management support */
1935 static int __init
init (void)
1937 return pci_register_driver (&goku_pci_driver
);
1941 static void __exit
cleanup (void)
1943 pci_unregister_driver (&goku_pci_driver
);
1945 module_exit (cleanup
);