2 * Toshiba TC86C001 ("Goku-S") USB Device Controller driver
4 * Copyright (C) 2000-2002 Lineo
5 * by Stuart Lynne, Tom Rushworth, and Bruce Balden
6 * Copyright (C) 2002 Toshiba Corporation
7 * Copyright (C) 2003 MontaVista Software (source@mvista.com)
9 * This file is licensed under the terms of the GNU General Public
10 * License version 2. This program is licensed "as is" without any
11 * warranty of any kind, whether express or implied.
15 * This device has ep0 and three semi-configurable bulk/interrupt endpoints.
17 * - Endpoint numbering is fixed: ep{1,2,3}-bulk
18 * - Gadget drivers can choose ep maxpacket (8/16/32/64)
19 * - Gadget drivers can choose direction (IN, OUT)
20 * - DMA works with ep1 (OUT transfers) and ep2 (IN transfers).
24 // #define VERBOSE /* extra debug messages (success too) */
25 // #define USB_TRACE /* packet-level success messages */
27 #include <linux/config.h>
28 #include <linux/kernel.h>
29 #include <linux/module.h>
30 #include <linux/pci.h>
31 #include <linux/delay.h>
32 #include <linux/ioport.h>
33 #include <linux/sched.h>
34 #include <linux/slab.h>
35 #include <linux/smp_lock.h>
36 #include <linux/errno.h>
37 #include <linux/init.h>
38 #include <linux/timer.h>
39 #include <linux/list.h>
40 #include <linux/interrupt.h>
41 #include <linux/proc_fs.h>
42 #include <linux/device.h>
43 #include <linux/usb_ch9.h>
44 #include <linux/usb_gadget.h>
46 #include <asm/byteorder.h>
49 #include <asm/system.h>
50 #include <asm/unaligned.h>
55 #define DRIVER_DESC "TC86C001 USB Device Controller"
56 #define DRIVER_VERSION "30-Oct 2003"
58 #define DMA_ADDR_INVALID (~(dma_addr_t)0)
60 static const char driver_name
[] = "goku_udc";
61 static const char driver_desc
[] = DRIVER_DESC
;
63 MODULE_AUTHOR("source@mvista.com");
64 MODULE_DESCRIPTION(DRIVER_DESC
);
65 MODULE_LICENSE("GPL");
69 * IN dma behaves ok under testing, though the IN-dma abort paths don't
70 * seem to behave quite as expected. Used by default.
72 * OUT dma documents design problems handling the common "short packet"
73 * transfer termination policy; it couldn't enabled by default, even
74 * if the OUT-dma abort problems had a resolution.
76 static unsigned use_dma
= 1;
79 //#include <linux/moduleparam.h>
80 /* "modprobe goku_udc use_dma=1" etc
82 * 1 to use IN dma only (normal operation)
83 * 2 to use IN and OUT dma
85 module_param(use_dma
, uint
, S_IRUGO
);
88 /*-------------------------------------------------------------------------*/
90 static void nuke(struct goku_ep
*, int status
);
93 command(struct goku_udc_regs
*regs
, int command
, unsigned epnum
)
95 writel(COMMAND_EP(epnum
) | command
, ®s
->Command
);
100 goku_ep_enable(struct usb_ep
*_ep
, const struct usb_endpoint_descriptor
*desc
)
102 struct goku_udc
*dev
;
108 ep
= container_of(_ep
, struct goku_ep
, ep
);
109 if (!_ep
|| !desc
|| ep
->desc
110 || desc
->bDescriptorType
!= USB_DT_ENDPOINT
)
113 if (ep
== &dev
->ep
[0])
115 if (!dev
->driver
|| dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)
117 if (ep
->num
!= (desc
->bEndpointAddress
& 0x0f))
120 switch (desc
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) {
121 case USB_ENDPOINT_XFER_BULK
:
122 case USB_ENDPOINT_XFER_INT
:
128 if ((readl(ep
->reg_status
) & EPxSTATUS_EP_MASK
)
129 != EPxSTATUS_EP_INVALID
)
132 /* enabling the no-toggle interrupt mode would need an api hook */
134 max
= le16_to_cpu(get_unaligned(&desc
->wMaxPacketSize
));
144 mode
|= 2 << 1; /* bulk, or intr-with-toggle */
146 /* ep1/ep2 dma direction is chosen early; it works in the other
147 * direction, with pio. be cautious with out-dma.
149 ep
->is_in
= (USB_DIR_IN
& desc
->bEndpointAddress
) != 0;
152 ep
->dma
= (use_dma
!= 0) && (ep
->num
== UDC_MSTRD_ENDPOINT
);
154 ep
->dma
= (use_dma
== 2) && (ep
->num
== UDC_MSTWR_ENDPOINT
);
156 DBG(dev
, "%s out-dma hides short packets\n",
160 spin_lock_irqsave(&ep
->dev
->lock
, flags
);
162 /* ep1 and ep2 can do double buffering and/or dma */
164 struct goku_udc_regs
*regs
= ep
->dev
->regs
;
167 /* double buffer except (for now) with pio in */
168 tmp
= ((ep
->dma
|| !ep
->is_in
)
169 ? 0x10 /* double buffered */
170 : 0x11 /* single buffer */
172 tmp
|= readl(®s
->EPxSingle
);
173 writel(tmp
, ®s
->EPxSingle
);
175 tmp
= (ep
->dma
? 0x10/*dma*/ : 0x11/*pio*/) << ep
->num
;
176 tmp
|= readl(®s
->EPxBCS
);
177 writel(tmp
, ®s
->EPxBCS
);
179 writel(mode
, ep
->reg_mode
);
180 command(ep
->dev
->regs
, COMMAND_RESET
, ep
->num
);
181 ep
->ep
.maxpacket
= max
;
184 spin_unlock_irqrestore(&ep
->dev
->lock
, flags
);
186 DBG(dev
, "enable %s %s %s maxpacket %u\n", ep
->ep
.name
,
187 ep
->is_in
? "IN" : "OUT",
188 ep
->dma
? "dma" : "pio",
194 static void ep_reset(struct goku_udc_regs
*regs
, struct goku_ep
*ep
)
196 struct goku_udc
*dev
= ep
->dev
;
199 command(regs
, COMMAND_INVALID
, ep
->num
);
201 if (ep
->num
== UDC_MSTWR_ENDPOINT
)
202 dev
->int_enable
&= ~(INT_MSTWREND
204 else if (ep
->num
== UDC_MSTRD_ENDPOINT
)
205 dev
->int_enable
&= ~INT_MSTRDEND
;
206 dev
->int_enable
&= ~INT_EPxDATASET (ep
->num
);
208 dev
->int_enable
&= ~INT_EP0
;
209 writel(dev
->int_enable
, ®s
->int_enable
);
210 readl(®s
->int_enable
);
212 struct goku_udc_regs
*regs
= ep
->dev
->regs
;
215 tmp
= readl(®s
->EPxSingle
);
216 tmp
&= ~(0x11 << ep
->num
);
217 writel(tmp
, ®s
->EPxSingle
);
219 tmp
= readl(®s
->EPxBCS
);
220 tmp
&= ~(0x11 << ep
->num
);
221 writel(tmp
, ®s
->EPxBCS
);
223 /* reset dma in case we're still using it */
227 master
= readl(®s
->dma_master
) & MST_RW_BITS
;
228 if (ep
->num
== UDC_MSTWR_ENDPOINT
) {
229 master
&= ~MST_W_BITS
;
230 master
|= MST_WR_RESET
;
232 master
&= ~MST_R_BITS
;
233 master
|= MST_RD_RESET
;
235 writel(master
, ®s
->dma_master
);
239 ep
->ep
.maxpacket
= MAX_FIFO_SIZE
;
246 static int goku_ep_disable(struct usb_ep
*_ep
)
249 struct goku_udc
*dev
;
252 ep
= container_of(_ep
, struct goku_ep
, ep
);
253 if (!_ep
|| !ep
->desc
)
256 if (dev
->ep0state
== EP0_SUSPEND
)
259 VDBG(dev
, "disable %s\n", _ep
->name
);
261 spin_lock_irqsave(&dev
->lock
, flags
);
262 nuke(ep
, -ESHUTDOWN
);
263 ep_reset(dev
->regs
, ep
);
264 spin_unlock_irqrestore(&dev
->lock
, flags
);
269 /*-------------------------------------------------------------------------*/
271 static struct usb_request
*
272 goku_alloc_request(struct usb_ep
*_ep
, int gfp_flags
)
274 struct goku_request
*req
;
278 req
= kmalloc(sizeof *req
, gfp_flags
);
282 memset(req
, 0, sizeof *req
);
283 req
->req
.dma
= DMA_ADDR_INVALID
;
284 INIT_LIST_HEAD(&req
->queue
);
289 goku_free_request(struct usb_ep
*_ep
, struct usb_request
*_req
)
291 struct goku_request
*req
;
296 req
= container_of(_req
, struct goku_request
, req
);
297 WARN_ON(!list_empty(&req
->queue
));
301 /*-------------------------------------------------------------------------*/
305 /* many common platforms have dma-coherent caches, which means that it's
306 * safe to use kmalloc() memory for all i/o buffers without using any
307 * cache flushing calls. (unless you're trying to share cache lines
308 * between dma and non-dma activities, which is a slow idea in any case.)
310 * other platforms need more care, with 2.6 having a moderately general
311 * solution except for the common "buffer is smaller than a page" case.
313 #if defined(CONFIG_X86)
316 #elif defined(CONFIG_MIPS) && !defined(CONFIG_NONCOHERENT_IO)
319 #elif defined(CONFIG_PPC) && !defined(CONFIG_NOT_COHERENT_CACHE)
324 /* allocating buffers this way eliminates dma mapping overhead, which
325 * on some platforms will mean eliminating a per-io buffer copy. with
326 * some kinds of system caches, further tweaks may still be needed.
329 goku_alloc_buffer(struct usb_ep
*_ep
, unsigned bytes
,
330 dma_addr_t
*dma
, int gfp_flags
)
335 ep
= container_of(_ep
, struct goku_ep
, ep
);
338 *dma
= DMA_ADDR_INVALID
;
340 #if defined(USE_KMALLOC)
341 retval
= kmalloc(bytes
, gfp_flags
);
343 *dma
= virt_to_phys(retval
);
346 /* the main problem with this call is that it wastes memory
347 * on typical 1/N page allocations: it allocates 1-N pages.
349 #warning Using dma_alloc_coherent even with buffers smaller than a page.
350 retval
= dma_alloc_coherent(&ep
->dev
->pdev
->dev
,
351 bytes
, dma
, gfp_flags
);
353 retval
= kmalloc(bytes
, gfp_flags
);
359 goku_free_buffer(struct usb_ep
*_ep
, void *buf
, dma_addr_t dma
, unsigned bytes
)
361 /* free memory into the right allocator */
363 if (dma
!= DMA_ADDR_INVALID
) {
366 ep
= container_of(_ep
, struct goku_ep
, ep
);
369 dma_free_coherent(&ep
->dev
->pdev
->dev
, bytes
, buf
, dma
);
375 /*-------------------------------------------------------------------------*/
378 done(struct goku_ep
*ep
, struct goku_request
*req
, int status
)
380 struct goku_udc
*dev
;
381 unsigned stopped
= ep
->stopped
;
383 list_del_init(&req
->queue
);
385 if (likely(req
->req
.status
== -EINPROGRESS
))
386 req
->req
.status
= status
;
388 status
= req
->req
.status
;
392 pci_unmap_single(dev
->pdev
, req
->req
.dma
, req
->req
.length
,
393 ep
->is_in
? PCI_DMA_TODEVICE
: PCI_DMA_FROMDEVICE
);
394 req
->req
.dma
= DMA_ADDR_INVALID
;
399 if (status
&& status
!= -ESHUTDOWN
)
401 VDBG(dev
, "complete %s req %p stat %d len %u/%u\n",
402 ep
->ep
.name
, &req
->req
, status
,
403 req
->req
.actual
, req
->req
.length
);
405 /* don't modify queue heads during completion callback */
407 spin_unlock(&dev
->lock
);
408 req
->req
.complete(&ep
->ep
, &req
->req
);
409 spin_lock(&dev
->lock
);
410 ep
->stopped
= stopped
;
413 /*-------------------------------------------------------------------------*/
416 write_packet(u32
*fifo
, u8
*buf
, struct goku_request
*req
, unsigned max
)
418 unsigned length
, count
;
420 length
= min(req
->req
.length
- req
->req
.actual
, max
);
421 req
->req
.actual
+= length
;
424 while (likely(count
--))
425 writel(*buf
++, fifo
);
429 // return: 0 = still running, 1 = completed, negative = errno
430 static int write_fifo(struct goku_ep
*ep
, struct goku_request
*req
)
432 struct goku_udc
*dev
= ep
->dev
;
438 tmp
= readl(&dev
->regs
->DataSet
);
439 buf
= req
->req
.buf
+ req
->req
.actual
;
443 if (unlikely(ep
->num
== 0 && dev
->ep0state
!= EP0_IN
))
446 /* NOTE: just single-buffered PIO-IN for now. */
447 if (unlikely((tmp
& DATASET_A(ep
->num
)) != 0))
450 /* clear our "packet available" irq */
452 writel(~INT_EPxDATASET(ep
->num
), &dev
->regs
->int_status
);
454 count
= write_packet(ep
->reg_fifo
, buf
, req
, ep
->ep
.maxpacket
);
456 /* last packet often short (sometimes a zlp, especially on ep0) */
457 if (unlikely(count
!= ep
->ep
.maxpacket
)) {
458 writel(~(1<<ep
->num
), &dev
->regs
->EOP
);
460 dev
->ep
[0].stopped
= 1;
461 dev
->ep0state
= EP0_STATUS
;
465 if (likely(req
->req
.length
!= req
->req
.actual
)
471 #if 0 /* printk seemed to trash is_last...*/
473 VDBG(dev
, "wrote %s %u bytes%s IN %u left %p\n",
474 ep
->ep
.name
, count
, is_last
? "/last" : "",
475 req
->req
.length
- req
->req
.actual
, req
);
478 /* requests complete when all IN data is in the FIFO,
479 * or sometimes later, if a zlp was needed.
489 static int read_fifo(struct goku_ep
*ep
, struct goku_request
*req
)
491 struct goku_udc_regs
*regs
;
494 unsigned bufferspace
, is_short
, dbuff
;
496 regs
= ep
->dev
->regs
;
498 buf
= req
->req
.buf
+ req
->req
.actual
;
501 if (unlikely(ep
->num
== 0 && ep
->dev
->ep0state
!= EP0_OUT
))
504 dbuff
= (ep
->num
== 1 || ep
->num
== 2);
506 /* ack dataset irq matching the status we'll handle */
508 writel(~INT_EPxDATASET(ep
->num
), ®s
->int_status
);
510 set
= readl(®s
->DataSet
) & DATASET_AB(ep
->num
);
511 size
= readl(®s
->EPxSizeLA
[ep
->num
]);
512 bufferspace
= req
->req
.length
- req
->req
.actual
;
514 /* usually do nothing without an OUT packet */
515 if (likely(ep
->num
!= 0 || bufferspace
!= 0)) {
516 if (unlikely(set
== 0))
518 /* use ep1/ep2 double-buffering for OUT */
519 if (!(size
& PACKET_ACTIVE
))
520 size
= readl(®s
->EPxSizeLB
[ep
->num
]);
521 if (!(size
& PACKET_ACTIVE
)) // "can't happen"
523 size
&= DATASIZE
; /* EPxSizeH == 0 */
525 /* ep0out no-out-data case for set_config, etc */
529 /* read all bytes from this packet */
530 req
->req
.actual
+= size
;
531 is_short
= (size
< ep
->ep
.maxpacket
);
533 VDBG(ep
->dev
, "read %s %u bytes%s OUT req %p %u/%u\n",
534 ep
->ep
.name
, size
, is_short
? "/S" : "",
535 req
, req
->req
.actual
, req
->req
.length
);
537 while (likely(size
-- != 0)) {
538 u8 byte
= (u8
) readl(ep
->reg_fifo
);
540 if (unlikely(bufferspace
== 0)) {
541 /* this happens when the driver's buffer
542 * is smaller than what the host sent.
543 * discard the extra data in this packet.
545 if (req
->req
.status
!= -EOVERFLOW
)
546 DBG(ep
->dev
, "%s overflow %u\n",
548 req
->req
.status
= -EOVERFLOW
;
556 if (unlikely(is_short
|| req
->req
.actual
== req
->req
.length
)) {
557 if (unlikely(ep
->num
== 0)) {
558 /* non-control endpoints now usable? */
559 if (ep
->dev
->req_config
)
560 writel(ep
->dev
->configured
561 ? USBSTATE_CONFIGURED
564 /* ep0out status stage */
565 writel(~(1<<0), ®s
->EOP
);
567 ep
->dev
->ep0state
= EP0_STATUS
;
571 /* empty the second buffer asap */
572 if (dbuff
&& !list_empty(&ep
->queue
)) {
573 req
= list_entry(ep
->queue
.next
,
574 struct goku_request
, queue
);
584 pio_irq_enable(struct goku_udc
*dev
, struct goku_udc_regs
*regs
, int epnum
)
586 dev
->int_enable
|= INT_EPxDATASET (epnum
);
587 writel(dev
->int_enable
, ®s
->int_enable
);
588 /* write may still be posted */
592 pio_irq_disable(struct goku_udc
*dev
, struct goku_udc_regs
*regs
, int epnum
)
594 dev
->int_enable
&= ~INT_EPxDATASET (epnum
);
595 writel(dev
->int_enable
, ®s
->int_enable
);
596 /* write may still be posted */
600 pio_advance(struct goku_ep
*ep
)
602 struct goku_request
*req
;
604 if (unlikely(list_empty (&ep
->queue
)))
606 req
= list_entry(ep
->queue
.next
, struct goku_request
, queue
);
607 (ep
->is_in
? write_fifo
: read_fifo
)(ep
, req
);
611 /*-------------------------------------------------------------------------*/
613 // return: 0 = q running, 1 = q stopped, negative = errno
614 static int start_dma(struct goku_ep
*ep
, struct goku_request
*req
)
616 struct goku_udc_regs
*regs
= ep
->dev
->regs
;
618 u32 start
= req
->req
.dma
;
619 u32 end
= start
+ req
->req
.length
- 1;
621 master
= readl(®s
->dma_master
) & MST_RW_BITS
;
623 /* re-init the bits affecting IN dma; careful with zlps */
624 if (likely(ep
->is_in
)) {
625 if (unlikely(master
& MST_RD_ENA
)) {
626 DBG (ep
->dev
, "start, IN active dma %03x!!\n",
630 writel(end
, ®s
->in_dma_end
);
631 writel(start
, ®s
->in_dma_start
);
633 master
&= ~MST_R_BITS
;
634 if (unlikely(req
->req
.length
== 0))
635 master
= MST_RD_ENA
| MST_RD_EOPB
;
636 else if ((req
->req
.length
% ep
->ep
.maxpacket
) != 0
638 master
= MST_RD_ENA
| MST_EOPB_ENA
;
640 master
= MST_RD_ENA
| MST_EOPB_DIS
;
642 ep
->dev
->int_enable
|= INT_MSTRDEND
;
644 /* Goku DMA-OUT merges short packets, which plays poorly with
645 * protocols where short packets mark the transfer boundaries.
646 * The chip supports a nonstandard policy with INT_MSTWRTMOUT,
647 * ending transfers after 3 SOFs; we don't turn it on.
650 if (unlikely(master
& MST_WR_ENA
)) {
651 DBG (ep
->dev
, "start, OUT active dma %03x!!\n",
655 writel(end
, ®s
->out_dma_end
);
656 writel(start
, ®s
->out_dma_start
);
658 master
&= ~MST_W_BITS
;
659 master
|= MST_WR_ENA
| MST_TIMEOUT_DIS
;
661 ep
->dev
->int_enable
|= INT_MSTWREND
|INT_MSTWRTMOUT
;
664 writel(master
, ®s
->dma_master
);
665 writel(ep
->dev
->int_enable
, ®s
->int_enable
);
669 static void dma_advance(struct goku_udc
*dev
, struct goku_ep
*ep
)
671 struct goku_request
*req
;
672 struct goku_udc_regs
*regs
= ep
->dev
->regs
;
675 master
= readl(®s
->dma_master
);
677 if (unlikely(list_empty(&ep
->queue
))) {
680 dev
->int_enable
&= ~INT_MSTRDEND
;
682 dev
->int_enable
&= ~(INT_MSTWREND
|INT_MSTWRTMOUT
);
683 writel(dev
->int_enable
, ®s
->int_enable
);
686 req
= list_entry(ep
->queue
.next
, struct goku_request
, queue
);
688 /* normal hw dma completion (not abort) */
689 if (likely(ep
->is_in
)) {
690 if (unlikely(master
& MST_RD_ENA
))
692 req
->req
.actual
= readl(®s
->in_dma_current
);
694 if (unlikely(master
& MST_WR_ENA
))
697 /* hardware merges short packets, and also hides packet
698 * overruns. a partial packet MAY be in the fifo here.
700 req
->req
.actual
= readl(®s
->out_dma_current
);
702 req
->req
.actual
-= req
->req
.dma
;
706 VDBG(dev
, "done %s %s dma, %u/%u bytes, req %p\n",
707 ep
->ep
.name
, ep
->is_in
? "IN" : "OUT",
708 req
->req
.actual
, req
->req
.length
, req
);
711 if (list_empty(&ep
->queue
))
713 req
= list_entry(ep
->queue
.next
, struct goku_request
, queue
);
714 (void) start_dma(ep
, req
);
717 static void abort_dma(struct goku_ep
*ep
, int status
)
719 struct goku_udc_regs
*regs
= ep
->dev
->regs
;
720 struct goku_request
*req
;
723 /* NAK future host requests, hoping the implicit delay lets the
724 * dma engine finish reading (or writing) its latest packet and
725 * empty the dma buffer (up to 16 bytes).
727 * This avoids needing to clean up a partial packet in the fifo;
728 * we can't do that for IN without side effects to HALT and TOGGLE.
730 command(regs
, COMMAND_FIFO_DISABLE
, ep
->num
);
731 req
= list_entry(ep
->queue
.next
, struct goku_request
, queue
);
732 master
= readl(®s
->dma_master
) & MST_RW_BITS
;
734 /* FIXME using these resets isn't usably documented. this may
735 * not work unless it's followed by disabling the endpoint.
737 * FIXME the OUT reset path doesn't even behave consistently.
740 if (unlikely((readl(®s
->dma_master
) & MST_RD_ENA
) == 0))
742 curr
= readl(®s
->in_dma_current
);
744 writel(curr
, ®s
->in_dma_end
);
745 writel(curr
, ®s
->in_dma_start
);
747 master
&= ~MST_R_BITS
;
748 master
|= MST_RD_RESET
;
749 writel(master
, ®s
->dma_master
);
751 if (readl(®s
->dma_master
) & MST_RD_ENA
)
752 DBG(ep
->dev
, "IN dma active after reset!\n");
755 if (unlikely((readl(®s
->dma_master
) & MST_WR_ENA
) == 0))
757 curr
= readl(®s
->out_dma_current
);
759 writel(curr
, ®s
->out_dma_end
);
760 writel(curr
, ®s
->out_dma_start
);
762 master
&= ~MST_W_BITS
;
763 master
|= MST_WR_RESET
;
764 writel(master
, ®s
->dma_master
);
766 if (readl(®s
->dma_master
) & MST_WR_ENA
)
767 DBG(ep
->dev
, "OUT dma active after reset!\n");
769 req
->req
.actual
= (curr
- req
->req
.dma
) + 1;
770 req
->req
.status
= status
;
772 VDBG(ep
->dev
, "%s %s %s %d/%d\n", __FUNCTION__
, ep
->ep
.name
,
773 ep
->is_in
? "IN" : "OUT",
774 req
->req
.actual
, req
->req
.length
);
776 command(regs
, COMMAND_FIFO_ENABLE
, ep
->num
);
781 /* dma already completed; no abort needed */
782 command(regs
, COMMAND_FIFO_ENABLE
, ep
->num
);
783 req
->req
.actual
= req
->req
.length
;
787 /*-------------------------------------------------------------------------*/
790 goku_queue(struct usb_ep
*_ep
, struct usb_request
*_req
, int gfp_flags
)
792 struct goku_request
*req
;
794 struct goku_udc
*dev
;
798 /* always require a cpu-view buffer so pio works */
799 req
= container_of(_req
, struct goku_request
, req
);
800 if (unlikely(!_req
|| !_req
->complete
801 || !_req
->buf
|| !list_empty(&req
->queue
)))
803 ep
= container_of(_ep
, struct goku_ep
, ep
);
804 if (unlikely(!_ep
|| (!ep
->desc
&& ep
->num
!= 0)))
807 if (unlikely(!dev
->driver
|| dev
->gadget
.speed
== USB_SPEED_UNKNOWN
))
810 /* can't touch registers when suspended */
811 if (dev
->ep0state
== EP0_SUSPEND
)
814 /* set up dma mapping in case the caller didn't */
815 if (ep
->dma
&& _req
->dma
== DMA_ADDR_INVALID
) {
816 _req
->dma
= pci_map_single(dev
->pdev
, _req
->buf
, _req
->length
,
817 ep
->is_in
? PCI_DMA_TODEVICE
: PCI_DMA_FROMDEVICE
);
822 VDBG(dev
, "%s queue req %p, len %u buf %p\n",
823 _ep
->name
, _req
, _req
->length
, _req
->buf
);
826 spin_lock_irqsave(&dev
->lock
, flags
);
828 _req
->status
= -EINPROGRESS
;
831 /* for ep0 IN without premature status, zlp is required and
832 * writing EOP starts the status stage (OUT).
834 if (unlikely(ep
->num
== 0 && ep
->is_in
))
837 /* kickstart this i/o queue? */
839 if (list_empty(&ep
->queue
) && likely(!ep
->stopped
)) {
840 /* dma: done after dma completion IRQ (or error)
841 * pio: done after last fifo operation
844 status
= start_dma(ep
, req
);
846 status
= (ep
->is_in
? write_fifo
: read_fifo
)(ep
, req
);
848 if (unlikely(status
!= 0)) {
854 } /* else pio or dma irq handler advances the queue. */
856 if (likely(req
!= 0))
857 list_add_tail(&req
->queue
, &ep
->queue
);
859 if (likely(!list_empty(&ep
->queue
))
860 && likely(ep
->num
!= 0)
862 && !(dev
->int_enable
& INT_EPxDATASET (ep
->num
)))
863 pio_irq_enable(dev
, dev
->regs
, ep
->num
);
865 spin_unlock_irqrestore(&dev
->lock
, flags
);
867 /* pci writes may still be posted */
871 /* dequeue ALL requests */
872 static void nuke(struct goku_ep
*ep
, int status
)
874 struct goku_request
*req
;
877 if (list_empty(&ep
->queue
))
880 abort_dma(ep
, status
);
881 while (!list_empty(&ep
->queue
)) {
882 req
= list_entry(ep
->queue
.next
, struct goku_request
, queue
);
883 done(ep
, req
, status
);
887 /* dequeue JUST ONE request */
888 static int goku_dequeue(struct usb_ep
*_ep
, struct usb_request
*_req
)
890 struct goku_request
*req
;
892 struct goku_udc
*dev
;
895 ep
= container_of(_ep
, struct goku_ep
, ep
);
896 if (!_ep
|| !_req
|| (!ep
->desc
&& ep
->num
!= 0))
902 /* we can't touch (dma) registers when suspended */
903 if (dev
->ep0state
== EP0_SUSPEND
)
906 VDBG(dev
, "%s %s %s %s %p\n", __FUNCTION__
, _ep
->name
,
907 ep
->is_in
? "IN" : "OUT",
908 ep
->dma
? "dma" : "pio",
911 spin_lock_irqsave(&dev
->lock
, flags
);
913 /* make sure it's actually queued on this endpoint */
914 list_for_each_entry (req
, &ep
->queue
, queue
) {
915 if (&req
->req
== _req
)
918 if (&req
->req
!= _req
) {
919 spin_unlock_irqrestore (&dev
->lock
, flags
);
923 if (ep
->dma
&& ep
->queue
.next
== &req
->queue
&& !ep
->stopped
) {
924 abort_dma(ep
, -ECONNRESET
);
925 done(ep
, req
, -ECONNRESET
);
926 dma_advance(dev
, ep
);
927 } else if (!list_empty(&req
->queue
))
928 done(ep
, req
, -ECONNRESET
);
931 spin_unlock_irqrestore(&dev
->lock
, flags
);
933 return req
? 0 : -EOPNOTSUPP
;
936 /*-------------------------------------------------------------------------*/
938 static void goku_clear_halt(struct goku_ep
*ep
)
940 // assert (ep->num !=0)
941 VDBG(ep
->dev
, "%s clear halt\n", ep
->ep
.name
);
942 command(ep
->dev
->regs
, COMMAND_SETDATA0
, ep
->num
);
943 command(ep
->dev
->regs
, COMMAND_STALL_CLEAR
, ep
->num
);
947 struct goku_request
*req
;
949 if (list_empty(&ep
->queue
))
951 req
= list_entry(ep
->queue
.next
, struct goku_request
,
953 (void) start_dma(ep
, req
);
959 static int goku_set_halt(struct usb_ep
*_ep
, int value
)
967 ep
= container_of (_ep
, struct goku_ep
, ep
);
971 ep
->dev
->ep0state
= EP0_STALL
;
972 ep
->dev
->ep
[0].stopped
= 1;
976 /* don't change EPxSTATUS_EP_INVALID to READY */
977 } else if (!ep
->desc
) {
978 DBG(ep
->dev
, "%s %s inactive?\n", __FUNCTION__
, ep
->ep
.name
);
982 spin_lock_irqsave(&ep
->dev
->lock
, flags
);
983 if (!list_empty(&ep
->queue
))
985 else if (ep
->is_in
&& value
986 /* data in (either) packet buffer? */
987 && (ep
->dev
->regs
->DataSet
& DATASET_AB(ep
->num
)))
993 VDBG(ep
->dev
, "%s set halt\n", ep
->ep
.name
);
994 command(ep
->dev
->regs
, COMMAND_STALL
, ep
->num
);
995 readl(ep
->reg_status
);
997 spin_unlock_irqrestore(&ep
->dev
->lock
, flags
);
1001 static int goku_fifo_status(struct usb_ep
*_ep
)
1004 struct goku_udc_regs
*regs
;
1009 ep
= container_of(_ep
, struct goku_ep
, ep
);
1011 /* size is only reported sanely for OUT */
1015 /* ignores 16-byte dma buffer; SizeH == 0 */
1016 regs
= ep
->dev
->regs
;
1017 size
= readl(®s
->EPxSizeLA
[ep
->num
]) & DATASIZE
;
1018 size
+= readl(®s
->EPxSizeLB
[ep
->num
]) & DATASIZE
;
1019 VDBG(ep
->dev
, "%s %s %u\n", __FUNCTION__
, ep
->ep
.name
, size
);
1023 static void goku_fifo_flush(struct usb_ep
*_ep
)
1026 struct goku_udc_regs
*regs
;
1031 ep
= container_of(_ep
, struct goku_ep
, ep
);
1032 VDBG(ep
->dev
, "%s %s\n", __FUNCTION__
, ep
->ep
.name
);
1034 /* don't change EPxSTATUS_EP_INVALID to READY */
1035 if (!ep
->desc
&& ep
->num
!= 0) {
1036 DBG(ep
->dev
, "%s %s inactive?\n", __FUNCTION__
, ep
->ep
.name
);
1040 regs
= ep
->dev
->regs
;
1041 size
= readl(®s
->EPxSizeLA
[ep
->num
]);
1044 /* Non-desirable behavior: FIFO_CLEAR also clears the
1045 * endpoint halt feature. For OUT, we _could_ just read
1046 * the bytes out (PIO, if !ep->dma); for in, no choice.
1049 command(regs
, COMMAND_FIFO_CLEAR
, ep
->num
);
1052 static struct usb_ep_ops goku_ep_ops
= {
1053 .enable
= goku_ep_enable
,
1054 .disable
= goku_ep_disable
,
1056 .alloc_request
= goku_alloc_request
,
1057 .free_request
= goku_free_request
,
1059 .alloc_buffer
= goku_alloc_buffer
,
1060 .free_buffer
= goku_free_buffer
,
1062 .queue
= goku_queue
,
1063 .dequeue
= goku_dequeue
,
1065 .set_halt
= goku_set_halt
,
1066 .fifo_status
= goku_fifo_status
,
1067 .fifo_flush
= goku_fifo_flush
,
1070 /*-------------------------------------------------------------------------*/
1072 static int goku_get_frame(struct usb_gadget
*_gadget
)
1077 static const struct usb_gadget_ops goku_ops
= {
1078 .get_frame
= goku_get_frame
,
1083 /*-------------------------------------------------------------------------*/
1085 static inline char *dmastr(void)
1088 return "(dma disabled)";
1089 else if (use_dma
== 2)
1090 return "(dma IN and OUT)";
1095 /* if we're trying to save space, don't bother with this proc file */
1097 #if defined(CONFIG_PROC_FS) && !defined(CONFIG_EMBEDDED)
1098 # define UDC_PROC_FILE
1101 #ifdef UDC_PROC_FILE
1103 static const char proc_node_name
[] = "driver/udc";
1105 #define FOURBITS "%s%s%s%s"
1106 #define EIGHTBITS FOURBITS FOURBITS
1109 dump_intmask(const char *label
, u32 mask
, char **next
, unsigned *size
)
1113 /* int_status is the same format ... */
1114 t
= scnprintf(*next
, *size
,
1115 "%s %05X =" FOURBITS EIGHTBITS EIGHTBITS
"\n",
1117 (mask
& INT_PWRDETECT
) ? " power" : "",
1118 (mask
& INT_SYSERROR
) ? " sys" : "",
1119 (mask
& INT_MSTRDEND
) ? " in-dma" : "",
1120 (mask
& INT_MSTWRTMOUT
) ? " wrtmo" : "",
1122 (mask
& INT_MSTWREND
) ? " out-dma" : "",
1123 (mask
& INT_MSTWRSET
) ? " wrset" : "",
1124 (mask
& INT_ERR
) ? " err" : "",
1125 (mask
& INT_SOF
) ? " sof" : "",
1127 (mask
& INT_EP3NAK
) ? " ep3nak" : "",
1128 (mask
& INT_EP2NAK
) ? " ep2nak" : "",
1129 (mask
& INT_EP1NAK
) ? " ep1nak" : "",
1130 (mask
& INT_EP3DATASET
) ? " ep3" : "",
1132 (mask
& INT_EP2DATASET
) ? " ep2" : "",
1133 (mask
& INT_EP1DATASET
) ? " ep1" : "",
1134 (mask
& INT_STATUSNAK
) ? " ep0snak" : "",
1135 (mask
& INT_STATUS
) ? " ep0status" : "",
1137 (mask
& INT_SETUP
) ? " setup" : "",
1138 (mask
& INT_ENDPOINT0
) ? " ep0" : "",
1139 (mask
& INT_USBRESET
) ? " reset" : "",
1140 (mask
& INT_SUSPEND
) ? " suspend" : "");
1147 udc_proc_read(char *buffer
, char **start
, off_t off
, int count
,
1148 int *eof
, void *_dev
)
1151 struct goku_udc
*dev
= _dev
;
1152 struct goku_udc_regs
*regs
= dev
->regs
;
1154 unsigned size
= count
;
1155 unsigned long flags
;
1156 int i
, t
, is_usb_connected
;
1162 local_irq_save(flags
);
1164 /* basic device status */
1165 tmp
= readl(®s
->power_detect
);
1166 is_usb_connected
= tmp
& PW_DETECT
;
1167 t
= scnprintf(next
, size
,
1169 "%s version: %s %s\n"
1170 "Gadget driver: %s\n"
1173 pci_name(dev
->pdev
), driver_desc
,
1174 driver_name
, DRIVER_VERSION
, dmastr(),
1175 dev
->driver
? dev
->driver
->driver
.name
: "(none)",
1177 ? ((tmp
& PW_PULLUP
) ? "full speed" : "powered")
1180 switch(dev
->ep0state
){
1181 case EP0_DISCONNECT
: tmp
= "ep0_disconnect"; break;
1182 case EP0_IDLE
: tmp
= "ep0_idle"; break;
1183 case EP0_IN
: tmp
= "ep0_in"; break;
1184 case EP0_OUT
: tmp
= "ep0_out"; break;
1185 case EP0_STATUS
: tmp
= "ep0_status"; break;
1186 case EP0_STALL
: tmp
= "ep0_stall"; break;
1187 case EP0_SUSPEND
: tmp
= "ep0_suspend"; break;
1188 default: tmp
= "ep0_?"; break;
1194 dump_intmask("int_status", readl(®s
->int_status
), &next
, &size
);
1195 dump_intmask("int_enable", readl(®s
->int_enable
), &next
, &size
);
1197 if (!is_usb_connected
|| !dev
->driver
|| (tmp
& PW_PULLUP
) == 0)
1200 /* registers for (active) device and ep0 */
1201 t
= scnprintf(next
, size
, "\nirqs %lu\ndataset %02x "
1202 "single.bcs %02x.%02x state %x addr %u\n",
1203 dev
->irqs
, readl(®s
->DataSet
),
1204 readl(®s
->EPxSingle
), readl(®s
->EPxBCS
),
1205 readl(®s
->UsbState
),
1206 readl(®s
->address
));
1210 tmp
= readl(®s
->dma_master
);
1211 t
= scnprintf(next
, size
,
1212 "dma %03X =" EIGHTBITS
"%s %s\n", tmp
,
1213 (tmp
& MST_EOPB_DIS
) ? " eopb-" : "",
1214 (tmp
& MST_EOPB_ENA
) ? " eopb+" : "",
1215 (tmp
& MST_TIMEOUT_DIS
) ? " tmo-" : "",
1216 (tmp
& MST_TIMEOUT_ENA
) ? " tmo+" : "",
1218 (tmp
& MST_RD_EOPB
) ? " eopb" : "",
1219 (tmp
& MST_RD_RESET
) ? " in_reset" : "",
1220 (tmp
& MST_WR_RESET
) ? " out_reset" : "",
1221 (tmp
& MST_RD_ENA
) ? " IN" : "",
1223 (tmp
& MST_WR_ENA
) ? " OUT" : "",
1224 (tmp
& MST_CONNECTION
)
1230 /* dump endpoint queues */
1231 for (i
= 0; i
< 4; i
++) {
1232 struct goku_ep
*ep
= &dev
->ep
[i
];
1233 struct goku_request
*req
;
1239 tmp
= readl(ep
->reg_status
);
1240 t
= scnprintf(next
, size
,
1241 "%s %s max %u %s, irqs %lu, "
1242 "status %02x (%s) " FOURBITS
"\n",
1244 ep
->is_in
? "in" : "out",
1246 ep
->dma
? "dma" : "pio",
1249 switch (tmp
& EPxSTATUS_EP_MASK
) {
1250 case EPxSTATUS_EP_READY
:
1252 case EPxSTATUS_EP_DATAIN
:
1253 s
= "packet"; break;
1254 case EPxSTATUS_EP_FULL
:
1256 case EPxSTATUS_EP_TX_ERR
: // host will retry
1257 s
= "tx_err"; break;
1258 case EPxSTATUS_EP_RX_ERR
:
1259 s
= "rx_err"; break;
1260 case EPxSTATUS_EP_BUSY
: /* ep0 only */
1262 case EPxSTATUS_EP_STALL
:
1264 case EPxSTATUS_EP_INVALID
: // these "can't happen"
1265 s
= "invalid"; break;
1269 (tmp
& EPxSTATUS_TOGGLE
) ? "data1" : "data0",
1270 (tmp
& EPxSTATUS_SUSPEND
) ? " suspend" : "",
1271 (tmp
& EPxSTATUS_FIFO_DISABLE
) ? " disable" : "",
1272 (tmp
& EPxSTATUS_STAGE_ERROR
) ? " ep0stat" : ""
1274 if (t
<= 0 || t
> size
)
1279 if (list_empty(&ep
->queue
)) {
1280 t
= scnprintf(next
, size
, "\t(nothing queued)\n");
1281 if (t
<= 0 || t
> size
)
1287 list_for_each_entry(req
, &ep
->queue
, queue
) {
1288 if (ep
->dma
&& req
->queue
.prev
== &ep
->queue
) {
1289 if (i
== UDC_MSTRD_ENDPOINT
)
1290 tmp
= readl(®s
->in_dma_current
);
1292 tmp
= readl(®s
->out_dma_current
);
1293 tmp
-= req
->req
.dma
;
1296 tmp
= req
->req
.actual
;
1298 t
= scnprintf(next
, size
,
1299 "\treq %p len %u/%u buf %p\n",
1300 &req
->req
, tmp
, req
->req
.length
,
1302 if (t
<= 0 || t
> size
)
1310 local_irq_restore(flags
);
1312 return count
- size
;
1315 #endif /* UDC_PROC_FILE */
1317 /*-------------------------------------------------------------------------*/
1319 static void udc_reinit (struct goku_udc
*dev
)
1321 static char *names
[] = { "ep0", "ep1-bulk", "ep2-bulk", "ep3-bulk" };
1325 INIT_LIST_HEAD (&dev
->gadget
.ep_list
);
1326 dev
->gadget
.ep0
= &dev
->ep
[0].ep
;
1327 dev
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1328 dev
->ep0state
= EP0_DISCONNECT
;
1331 for (i
= 0; i
< 4; i
++) {
1332 struct goku_ep
*ep
= &dev
->ep
[i
];
1335 ep
->ep
.name
= names
[i
];
1336 ep
->reg_fifo
= &dev
->regs
->ep_fifo
[i
];
1337 ep
->reg_status
= &dev
->regs
->ep_status
[i
];
1338 ep
->reg_mode
= &dev
->regs
->ep_mode
[i
];
1340 ep
->ep
.ops
= &goku_ep_ops
;
1341 list_add_tail (&ep
->ep
.ep_list
, &dev
->gadget
.ep_list
);
1343 INIT_LIST_HEAD (&ep
->queue
);
1348 dev
->ep
[0].reg_mode
= 0;
1349 dev
->ep
[0].ep
.maxpacket
= MAX_EP0_SIZE
;
1350 list_del_init (&dev
->ep
[0].ep
.ep_list
);
1353 static void udc_reset(struct goku_udc
*dev
)
1355 struct goku_udc_regs
*regs
= dev
->regs
;
1357 writel(0, ®s
->power_detect
);
1358 writel(0, ®s
->int_enable
);
1359 readl(®s
->int_enable
);
1360 dev
->int_enable
= 0;
1362 /* deassert reset, leave USB D+ at hi-Z (no pullup)
1363 * don't let INT_PWRDETECT sequence begin
1366 writel(PW_RESETB
, ®s
->power_detect
);
1367 readl(®s
->int_enable
);
1370 static void ep0_start(struct goku_udc
*dev
)
1372 struct goku_udc_regs
*regs
= dev
->regs
;
1375 VDBG(dev
, "%s\n", __FUNCTION__
);
1379 //writel(MST_EOPB_ENA | MST_TIMEOUT_ENA, ®s->dma_master);
1381 /* hw handles set_address, set_feature, get_status; maybe more */
1382 writel( G_REQMODE_SET_INTF
| G_REQMODE_GET_INTF
1383 | G_REQMODE_SET_CONF
| G_REQMODE_GET_CONF
1384 | G_REQMODE_GET_DESC
1385 | G_REQMODE_CLEAR_FEAT
1388 for (i
= 0; i
< 4; i
++)
1389 dev
->ep
[i
].irqs
= 0;
1391 /* can't modify descriptors after writing UsbReady */
1392 for (i
= 0; i
< DESC_LEN
; i
++)
1393 writel(0, ®s
->descriptors
[i
]);
1394 writel(0, ®s
->UsbReady
);
1396 /* expect ep0 requests when the host drops reset */
1397 writel(PW_RESETB
| PW_PULLUP
, ®s
->power_detect
);
1398 dev
->int_enable
= INT_DEVWIDE
| INT_EP0
;
1399 writel(dev
->int_enable
, &dev
->regs
->int_enable
);
1400 readl(®s
->int_enable
);
1401 dev
->gadget
.speed
= USB_SPEED_FULL
;
1402 dev
->ep0state
= EP0_IDLE
;
1405 static void udc_enable(struct goku_udc
*dev
)
1407 /* start enumeration now, or after power detect irq */
1408 if (readl(&dev
->regs
->power_detect
) & PW_DETECT
)
1411 DBG(dev
, "%s\n", __FUNCTION__
);
1412 dev
->int_enable
= INT_PWRDETECT
;
1413 writel(dev
->int_enable
, &dev
->regs
->int_enable
);
1417 /*-------------------------------------------------------------------------*/
1419 /* keeping it simple:
1420 * - one bus driver, initted first;
1421 * - one function driver, initted second
1424 static struct goku_udc
*the_controller
;
1426 /* when a driver is successfully registered, it will receive
1427 * control requests including set_configuration(), which enables
1428 * non-control requests. then usb traffic follows until a
1429 * disconnect is reported. then a host may connect again, or
1430 * the driver might get unbound.
1432 int usb_gadget_register_driver(struct usb_gadget_driver
*driver
)
1434 struct goku_udc
*dev
= the_controller
;
1438 || driver
->speed
!= USB_SPEED_FULL
1441 || !driver
->disconnect
1449 /* hook up the driver */
1450 driver
->driver
.bus
= 0;
1451 dev
->driver
= driver
;
1452 dev
->gadget
.dev
.driver
= &driver
->driver
;
1453 retval
= driver
->bind(&dev
->gadget
);
1455 DBG(dev
, "bind to driver %s --> error %d\n",
1456 driver
->driver
.name
, retval
);
1458 dev
->gadget
.dev
.driver
= 0;
1462 /* then enable host detection and ep0; and we're ready
1463 * for set_configuration as well as eventual disconnect.
1467 DBG(dev
, "registered gadget driver '%s'\n", driver
->driver
.name
);
1470 EXPORT_SYMBOL(usb_gadget_register_driver
);
1473 stop_activity(struct goku_udc
*dev
, struct usb_gadget_driver
*driver
)
1477 DBG (dev
, "%s\n", __FUNCTION__
);
1479 if (dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)
1482 /* disconnect gadget driver after quiesceing hw and the driver */
1484 for (i
= 0; i
< 4; i
++)
1485 nuke(&dev
->ep
[i
], -ESHUTDOWN
);
1487 spin_unlock(&dev
->lock
);
1488 driver
->disconnect(&dev
->gadget
);
1489 spin_lock(&dev
->lock
);
1496 int usb_gadget_unregister_driver(struct usb_gadget_driver
*driver
)
1498 struct goku_udc
*dev
= the_controller
;
1499 unsigned long flags
;
1503 if (!driver
|| driver
!= dev
->driver
)
1506 spin_lock_irqsave(&dev
->lock
, flags
);
1508 stop_activity(dev
, driver
);
1509 spin_unlock_irqrestore(&dev
->lock
, flags
);
1511 driver
->unbind(&dev
->gadget
);
1513 DBG(dev
, "unregistered driver '%s'\n", driver
->driver
.name
);
1516 EXPORT_SYMBOL(usb_gadget_unregister_driver
);
1519 /*-------------------------------------------------------------------------*/
1521 static void ep0_setup(struct goku_udc
*dev
)
1523 struct goku_udc_regs
*regs
= dev
->regs
;
1524 struct usb_ctrlrequest ctrl
;
1527 /* read SETUP packet and enter DATA stage */
1528 ctrl
.bRequestType
= readl(®s
->bRequestType
);
1529 ctrl
.bRequest
= readl(®s
->bRequest
);
1530 ctrl
.wValue
= (readl(®s
->wValueH
) << 8) | readl(®s
->wValueL
);
1531 ctrl
.wIndex
= (readl(®s
->wIndexH
) << 8) | readl(®s
->wIndexL
);
1532 ctrl
.wLength
= (readl(®s
->wLengthH
) << 8) | readl(®s
->wLengthL
);
1533 writel(0, ®s
->SetupRecv
);
1535 nuke(&dev
->ep
[0], 0);
1536 dev
->ep
[0].stopped
= 0;
1537 if (likely(ctrl
.bRequestType
& USB_DIR_IN
)) {
1538 dev
->ep
[0].is_in
= 1;
1539 dev
->ep0state
= EP0_IN
;
1540 /* detect early status stages */
1541 writel(ICONTROL_STATUSNAK
, &dev
->regs
->IntControl
);
1543 dev
->ep
[0].is_in
= 0;
1544 dev
->ep0state
= EP0_OUT
;
1546 /* NOTE: CLEAR_FEATURE is done in software so that we can
1547 * synchronize transfer restarts after bulk IN stalls. data
1548 * won't even enter the fifo until the halt is cleared.
1550 switch (ctrl
.bRequest
) {
1551 case USB_REQ_CLEAR_FEATURE
:
1552 switch (ctrl
.bRequestType
) {
1553 case USB_RECIP_ENDPOINT
:
1554 tmp
= ctrl
.wIndex
& 0x0f;
1555 /* active endpoint */
1556 if (tmp
> 3 || (!dev
->ep
[tmp
].desc
&& tmp
!= 0))
1558 if (ctrl
.wIndex
& USB_DIR_IN
) {
1559 if (!dev
->ep
[tmp
].is_in
)
1562 if (dev
->ep
[tmp
].is_in
)
1565 if (ctrl
.wValue
!= USB_ENDPOINT_HALT
)
1568 goku_clear_halt(&dev
->ep
[tmp
]);
1570 /* start ep0out status stage */
1571 writel(~(1<<0), ®s
->EOP
);
1572 dev
->ep
[0].stopped
= 1;
1573 dev
->ep0state
= EP0_STATUS
;
1575 case USB_RECIP_DEVICE
:
1576 /* device remote wakeup: always clear */
1577 if (ctrl
.wValue
!= 1)
1579 VDBG(dev
, "clear dev remote wakeup\n");
1581 case USB_RECIP_INTERFACE
:
1583 default: /* pass to gadget driver */
1593 VDBG(dev
, "SETUP %02x.%02x v%04x i%04x l%04x\n",
1594 ctrl
.bRequestType
, ctrl
.bRequest
,
1595 ctrl
.wValue
, ctrl
.wIndex
, ctrl
.wLength
);
1598 /* hw wants to know when we're configured (or not) */
1599 dev
->req_config
= (ctrl
.bRequest
== USB_REQ_SET_CONFIGURATION
1600 && ctrl
.bRequestType
== USB_RECIP_DEVICE
);
1601 if (unlikely(dev
->req_config
))
1602 dev
->configured
= (ctrl
.wValue
!= 0);
1604 /* delegate everything to the gadget driver.
1605 * it may respond after this irq handler returns.
1607 spin_unlock (&dev
->lock
);
1608 tmp
= dev
->driver
->setup(&dev
->gadget
, &ctrl
);
1609 spin_lock (&dev
->lock
);
1610 if (unlikely(tmp
< 0)) {
1613 VDBG(dev
, "req %02x.%02x protocol STALL; err %d\n",
1614 ctrl
.bRequestType
, ctrl
.bRequest
, tmp
);
1616 command(regs
, COMMAND_STALL
, 0);
1617 dev
->ep
[0].stopped
= 1;
1618 dev
->ep0state
= EP0_STALL
;
1621 /* expect at least one data or status stage irq */
1624 #define ACK(irqbit) { \
1626 writel(~irqbit, ®s->int_status); \
1630 static irqreturn_t
goku_irq(int irq
, void *_dev
, struct pt_regs
*r
)
1632 struct goku_udc
*dev
= _dev
;
1633 struct goku_udc_regs
*regs
= dev
->regs
;
1635 u32 stat
, handled
= 0;
1636 unsigned i
, rescans
= 5;
1638 spin_lock(&dev
->lock
);
1641 stat
= readl(®s
->int_status
) & dev
->int_enable
;
1646 /* device-wide irqs */
1647 if (unlikely(stat
& INT_DEVWIDE
)) {
1648 if (stat
& INT_SYSERROR
) {
1649 ERROR(dev
, "system error\n");
1650 stop_activity(dev
, dev
->driver
);
1653 // FIXME have a neater way to prevent re-enumeration
1657 if (stat
& INT_PWRDETECT
) {
1658 writel(~stat
, ®s
->int_status
);
1659 if (readl(&dev
->regs
->power_detect
) & PW_DETECT
) {
1660 VDBG(dev
, "connect\n");
1663 DBG(dev
, "disconnect\n");
1664 if (dev
->gadget
.speed
== USB_SPEED_FULL
)
1665 stop_activity(dev
, dev
->driver
);
1666 dev
->ep0state
= EP0_DISCONNECT
;
1667 dev
->int_enable
= INT_DEVWIDE
;
1668 writel(dev
->int_enable
, &dev
->regs
->int_enable
);
1674 if (stat
& INT_SUSPEND
) {
1676 if (readl(®s
->ep_status
[0]) & EPxSTATUS_SUSPEND
) {
1677 switch (dev
->ep0state
) {
1678 case EP0_DISCONNECT
:
1684 DBG(dev
, "USB suspend\n");
1685 dev
->ep0state
= EP0_SUSPEND
;
1686 if (dev
->gadget
.speed
!= USB_SPEED_UNKNOWN
1688 && dev
->driver
->suspend
) {
1689 spin_unlock(&dev
->lock
);
1690 dev
->driver
->suspend(&dev
->gadget
);
1691 spin_lock(&dev
->lock
);
1694 if (dev
->ep0state
!= EP0_SUSPEND
) {
1695 DBG(dev
, "bogus USB resume %d\n",
1699 DBG(dev
, "USB resume\n");
1700 dev
->ep0state
= EP0_IDLE
;
1701 if (dev
->gadget
.speed
!= USB_SPEED_UNKNOWN
1703 && dev
->driver
->resume
) {
1704 spin_unlock(&dev
->lock
);
1705 dev
->driver
->resume(&dev
->gadget
);
1706 spin_lock(&dev
->lock
);
1711 if (stat
& INT_USBRESET
) { /* hub reset done */
1713 INFO(dev
, "USB reset done, gadget %s\n",
1714 dev
->driver
->driver
.name
);
1716 // and INT_ERR on some endpoint's crc/bitstuff/... problem
1719 /* progress ep0 setup, data, or status stages.
1720 * no transition {EP0_STATUS, EP0_STALL} --> EP0_IDLE; saves irqs
1722 if (stat
& INT_SETUP
) {
1727 if (stat
& INT_STATUSNAK
) {
1728 ACK(INT_STATUSNAK
|INT_ENDPOINT0
);
1729 if (dev
->ep0state
== EP0_IN
) {
1733 writel(~(1<<0), ®s
->EOP
);
1734 dev
->ep0state
= EP0_STATUS
;
1737 if (stat
& INT_ENDPOINT0
) {
1744 /* dma completion */
1745 if (stat
& INT_MSTRDEND
) { /* IN */
1747 ep
= &dev
->ep
[UDC_MSTRD_ENDPOINT
];
1749 dma_advance(dev
, ep
);
1751 if (stat
& INT_MSTWREND
) { /* OUT */
1753 ep
= &dev
->ep
[UDC_MSTWR_ENDPOINT
];
1755 dma_advance(dev
, ep
);
1757 if (stat
& INT_MSTWRTMOUT
) { /* OUT */
1758 ACK(INT_MSTWRTMOUT
);
1759 ep
= &dev
->ep
[UDC_MSTWR_ENDPOINT
];
1761 ERROR(dev
, "%s write timeout ?\n", ep
->ep
.name
);
1762 // reset dma? then dma_advance()
1766 for (i
= 1; i
< 4; i
++) {
1767 u32 tmp
= INT_EPxDATASET(i
);
1773 if (list_empty (&ep
->queue
))
1774 pio_irq_disable(dev
, regs
, i
);
1784 (void)readl(®s
->int_enable
);
1785 spin_unlock(&dev
->lock
);
1787 DBG(dev
, "unhandled irq status: %05x (%05x, %05x)\n", stat
,
1788 readl(®s
->int_status
), dev
->int_enable
);
1789 return IRQ_RETVAL(handled
);
1794 /*-------------------------------------------------------------------------*/
1796 static void gadget_release(struct device
*_dev
)
1798 struct goku_udc
*dev
= dev_get_drvdata(_dev
);
1803 /* tear down the binding between this driver and the pci device */
1805 static void goku_remove(struct pci_dev
*pdev
)
1807 struct goku_udc
*dev
= pci_get_drvdata(pdev
);
1809 DBG(dev
, "%s\n", __FUNCTION__
);
1810 /* start with the driver above us */
1812 /* should have been done already by driver model core */
1813 WARN(dev
, "pci remove, driver '%s' is still registered\n",
1814 dev
->driver
->driver
.name
);
1815 usb_gadget_unregister_driver(dev
->driver
);
1818 #ifdef UDC_PROC_FILE
1819 remove_proc_entry(proc_node_name
, NULL
);
1824 free_irq(pdev
->irq
, dev
);
1827 if (dev
->got_region
)
1828 release_mem_region(pci_resource_start (pdev
, 0),
1829 pci_resource_len (pdev
, 0));
1831 pci_disable_device(pdev
);
1832 device_unregister(&dev
->gadget
.dev
);
1834 pci_set_drvdata(pdev
, 0);
1838 INFO(dev
, "unbind\n");
1841 /* wrap this driver around the specified pci device, but
1842 * don't respond over USB until a gadget driver binds to us.
1845 static int goku_probe(struct pci_dev
*pdev
, const struct pci_device_id
*id
)
1847 struct goku_udc
*dev
= 0;
1848 unsigned long resource
, len
;
1851 char buf
[8], *bufp
;
1853 /* if you want to support more than one controller in a system,
1854 * usb_gadget_driver_{register,unregister}() must change.
1856 if (the_controller
) {
1857 WARN(dev
, "ignoring %s\n", pci_name(pdev
));
1861 printk(KERN_ERR
"Check PCI %s IRQ setup!\n", pci_name(pdev
));
1866 /* alloc, and start init */
1867 dev
= kmalloc (sizeof *dev
, SLAB_KERNEL
);
1869 pr_debug("enomem %s\n", pci_name(pdev
));
1874 memset(dev
, 0, sizeof *dev
);
1875 spin_lock_init(&dev
->lock
);
1877 dev
->gadget
.ops
= &goku_ops
;
1879 /* the "gadget" abstracts/virtualizes the controller */
1880 strcpy(dev
->gadget
.dev
.bus_id
, "gadget");
1881 dev
->gadget
.dev
.parent
= &pdev
->dev
;
1882 dev
->gadget
.dev
.dma_mask
= pdev
->dev
.dma_mask
;
1883 dev
->gadget
.dev
.release
= gadget_release
;
1884 dev
->gadget
.name
= driver_name
;
1886 /* now all the pci goodies ... */
1887 retval
= pci_enable_device(pdev
);
1889 DBG(dev
, "can't enable, %d\n", retval
);
1894 resource
= pci_resource_start(pdev
, 0);
1895 len
= pci_resource_len(pdev
, 0);
1896 if (!request_mem_region(resource
, len
, driver_name
)) {
1897 DBG(dev
, "controller already in use\n");
1901 dev
->got_region
= 1;
1903 base
= ioremap_nocache(resource
, len
);
1905 DBG(dev
, "can't map memory\n");
1909 dev
->regs
= (struct goku_udc_regs
*) base
;
1911 pci_set_drvdata(pdev
, dev
);
1912 INFO(dev
, "%s\n", driver_desc
);
1913 INFO(dev
, "version: " DRIVER_VERSION
" %s\n", dmastr());
1915 scnprintf(buf
, sizeof buf
, "%d", pdev
->irq
);
1918 bufp
= __irq_itoa(pdev
->irq
);
1920 INFO(dev
, "irq %s, pci mem %p\n", bufp
, base
);
1922 /* init to known state, then setup irqs */
1925 if (request_irq(pdev
->irq
, goku_irq
, SA_SHIRQ
/*|SA_SAMPLE_RANDOM*/,
1926 driver_name
, dev
) != 0) {
1927 DBG(dev
, "request interrupt %s failed\n", bufp
);
1933 pci_set_master(pdev
);
1936 #ifdef UDC_PROC_FILE
1937 create_proc_read_entry(proc_node_name
, 0, NULL
, udc_proc_read
, dev
);
1941 the_controller
= dev
;
1942 device_register(&dev
->gadget
.dev
);
1953 /*-------------------------------------------------------------------------*/
1955 static struct pci_device_id pci_ids
[] = { {
1956 .class = ((PCI_CLASS_SERIAL_USB
<< 8) | 0xfe),
1958 .vendor
= 0x102f, /* Toshiba */
1959 .device
= 0x0107, /* this UDC */
1960 .subvendor
= PCI_ANY_ID
,
1961 .subdevice
= PCI_ANY_ID
,
1963 }, { /* end: all zeroes */ }
1965 MODULE_DEVICE_TABLE (pci
, pci_ids
);
1967 static struct pci_driver goku_pci_driver
= {
1968 .name
= (char *) driver_name
,
1969 .id_table
= pci_ids
,
1971 .probe
= goku_probe
,
1972 .remove
= goku_remove
,
1974 /* FIXME add power management support */
1977 static int __init
init (void)
1979 return pci_module_init (&goku_pci_driver
);
1983 static void __exit
cleanup (void)
1985 pci_unregister_driver (&goku_pci_driver
);
1987 module_exit (cleanup
);