[IA64-SGI] move xpc.h to include/asm-ia64/sn (cleanup)
[linux-2.6/libata-dev.git] / drivers / usb / gadget / net2280.c
blobc32e1f7476da46d67b4e2f8336ab8cd5929b1b75
1 /*
2 * Driver for the PLX NET2280 USB device controller.
3 * Specs and errata are available from <http://www.plxtech.com>.
5 * PLX Technology Inc. (formerly NetChip Technology) supported the
6 * development of this driver.
9 * CODE STATUS HIGHLIGHTS
11 * This driver should work well with most "gadget" drivers, including
12 * the File Storage, Serial, and Ethernet/RNDIS gadget drivers
13 * as well as Gadget Zero and Gadgetfs.
15 * DMA is enabled by default. Drivers using transfer queues might use
16 * DMA chaining to remove IRQ latencies between transfers. (Except when
17 * short OUT transfers happen.) Drivers can use the req->no_interrupt
18 * hint to completely eliminate some IRQs, if a later IRQ is guaranteed
19 * and DMA chaining is enabled.
21 * Note that almost all the errata workarounds here are only needed for
22 * rev1 chips. Rev1a silicon (0110) fixes almost all of them.
26 * Copyright (C) 2003 David Brownell
27 * Copyright (C) 2003-2005 PLX Technology, Inc.
29 * This program is free software; you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License as published by
31 * the Free Software Foundation; either version 2 of the License, or
32 * (at your option) any later version.
34 * This program is distributed in the hope that it will be useful,
35 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 * GNU General Public License for more details.
39 * You should have received a copy of the GNU General Public License
40 * along with this program; if not, write to the Free Software
41 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
44 #undef DEBUG /* messages on error and most fault paths */
45 #undef VERBOSE /* extra debug messages (success too) */
47 #include <linux/config.h>
48 #include <linux/module.h>
49 #include <linux/pci.h>
50 #include <linux/kernel.h>
51 #include <linux/delay.h>
52 #include <linux/ioport.h>
53 #include <linux/sched.h>
54 #include <linux/slab.h>
55 #include <linux/smp_lock.h>
56 #include <linux/errno.h>
57 #include <linux/init.h>
58 #include <linux/timer.h>
59 #include <linux/list.h>
60 #include <linux/interrupt.h>
61 #include <linux/moduleparam.h>
62 #include <linux/device.h>
63 #include <linux/usb_ch9.h>
64 #include <linux/usb_gadget.h>
66 #include <asm/byteorder.h>
67 #include <asm/io.h>
68 #include <asm/irq.h>
69 #include <asm/system.h>
70 #include <asm/unaligned.h>
73 #define DRIVER_DESC "PLX NET2280 USB Peripheral Controller"
74 #define DRIVER_VERSION "2005 Feb 03"
76 #define DMA_ADDR_INVALID (~(dma_addr_t)0)
77 #define EP_DONTUSE 13 /* nonzero */
79 #define USE_RDK_LEDS /* GPIO pins control three LEDs */
82 static const char driver_name [] = "net2280";
83 static const char driver_desc [] = DRIVER_DESC;
85 static const char ep0name [] = "ep0";
86 static const char *ep_name [] = {
87 ep0name,
88 "ep-a", "ep-b", "ep-c", "ep-d",
89 "ep-e", "ep-f",
92 /* use_dma -- general goodness, fewer interrupts, less cpu load (vs PIO)
93 * use_dma_chaining -- dma descriptor queueing gives even more irq reduction
95 * The net2280 DMA engines are not tightly integrated with their FIFOs;
96 * not all cases are (yet) handled well in this driver or the silicon.
97 * Some gadget drivers work better with the dma support here than others.
98 * These two parameters let you use PIO or more aggressive DMA.
100 static int use_dma = 1;
101 static int use_dma_chaining = 0;
103 /* "modprobe net2280 use_dma=n" etc */
104 module_param (use_dma, bool, S_IRUGO);
105 module_param (use_dma_chaining, bool, S_IRUGO);
108 /* mode 0 == ep-{a,b,c,d} 1K fifo each
109 * mode 1 == ep-{a,b} 2K fifo each, ep-{c,d} unavailable
110 * mode 2 == ep-a 2K fifo, ep-{b,c} 1K each, ep-d unavailable
112 static ushort fifo_mode = 0;
114 /* "modprobe net2280 fifo_mode=1" etc */
115 module_param (fifo_mode, ushort, 0644);
117 /* enable_suspend -- When enabled, the driver will respond to
118 * USB suspend requests by powering down the NET2280. Otherwise,
119 * USB suspend requests will be ignored. This is acceptible for
120 * self-powered devices, and helps avoid some quirks.
122 static int enable_suspend = 0;
124 /* "modprobe net2280 enable_suspend=1" etc */
125 module_param (enable_suspend, bool, S_IRUGO);
128 #define DIR_STRING(bAddress) (((bAddress) & USB_DIR_IN) ? "in" : "out")
130 #if defined(CONFIG_USB_GADGET_DEBUG_FILES) || defined (DEBUG)
131 static char *type_string (u8 bmAttributes)
133 switch ((bmAttributes) & USB_ENDPOINT_XFERTYPE_MASK) {
134 case USB_ENDPOINT_XFER_BULK: return "bulk";
135 case USB_ENDPOINT_XFER_ISOC: return "iso";
136 case USB_ENDPOINT_XFER_INT: return "intr";
138 return "control";
140 #endif
142 #include "net2280.h"
144 #define valid_bit __constant_cpu_to_le32 (1 << VALID_BIT)
145 #define dma_done_ie __constant_cpu_to_le32 (1 << DMA_DONE_INTERRUPT_ENABLE)
147 /*-------------------------------------------------------------------------*/
149 static int
150 net2280_enable (struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
152 struct net2280 *dev;
153 struct net2280_ep *ep;
154 u32 max, tmp;
155 unsigned long flags;
157 ep = container_of (_ep, struct net2280_ep, ep);
158 if (!_ep || !desc || ep->desc || _ep->name == ep0name
159 || desc->bDescriptorType != USB_DT_ENDPOINT)
160 return -EINVAL;
161 dev = ep->dev;
162 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
163 return -ESHUTDOWN;
165 /* erratum 0119 workaround ties up an endpoint number */
166 if ((desc->bEndpointAddress & 0x0f) == EP_DONTUSE)
167 return -EDOM;
169 /* sanity check ep-e/ep-f since their fifos are small */
170 max = le16_to_cpu (desc->wMaxPacketSize) & 0x1fff;
171 if (ep->num > 4 && max > 64)
172 return -ERANGE;
174 spin_lock_irqsave (&dev->lock, flags);
175 _ep->maxpacket = max & 0x7ff;
176 ep->desc = desc;
178 /* ep_reset() has already been called */
179 ep->stopped = 0;
180 ep->out_overflow = 0;
182 /* set speed-dependent max packet; may kick in high bandwidth */
183 set_idx_reg (dev->regs, REG_EP_MAXPKT (dev, ep->num), max);
185 /* FIFO lines can't go to different packets. PIO is ok, so
186 * use it instead of troublesome (non-bulk) multi-packet DMA.
188 if (ep->dma && (max % 4) != 0 && use_dma_chaining) {
189 DEBUG (ep->dev, "%s, no dma for maxpacket %d\n",
190 ep->ep.name, ep->ep.maxpacket);
191 ep->dma = NULL;
194 /* set type, direction, address; reset fifo counters */
195 writel ((1 << FIFO_FLUSH), &ep->regs->ep_stat);
196 tmp = (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK);
197 if (tmp == USB_ENDPOINT_XFER_INT) {
198 /* erratum 0105 workaround prevents hs NYET */
199 if (dev->chiprev == 0100
200 && dev->gadget.speed == USB_SPEED_HIGH
201 && !(desc->bEndpointAddress & USB_DIR_IN))
202 writel ((1 << CLEAR_NAK_OUT_PACKETS_MODE),
203 &ep->regs->ep_rsp);
204 } else if (tmp == USB_ENDPOINT_XFER_BULK) {
205 /* catch some particularly blatant driver bugs */
206 if ((dev->gadget.speed == USB_SPEED_HIGH
207 && max != 512)
208 || (dev->gadget.speed == USB_SPEED_FULL
209 && max > 64)) {
210 spin_unlock_irqrestore (&dev->lock, flags);
211 return -ERANGE;
214 ep->is_iso = (tmp == USB_ENDPOINT_XFER_ISOC) ? 1 : 0;
215 tmp <<= ENDPOINT_TYPE;
216 tmp |= desc->bEndpointAddress;
217 tmp |= (4 << ENDPOINT_BYTE_COUNT); /* default full fifo lines */
218 tmp |= 1 << ENDPOINT_ENABLE;
219 wmb ();
221 /* for OUT transfers, block the rx fifo until a read is posted */
222 ep->is_in = (tmp & USB_DIR_IN) != 0;
223 if (!ep->is_in)
224 writel ((1 << SET_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
226 writel (tmp, &ep->regs->ep_cfg);
228 /* enable irqs */
229 if (!ep->dma) { /* pio, per-packet */
230 tmp = (1 << ep->num) | readl (&dev->regs->pciirqenb0);
231 writel (tmp, &dev->regs->pciirqenb0);
233 tmp = (1 << DATA_PACKET_RECEIVED_INTERRUPT_ENABLE)
234 | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT_ENABLE)
235 | readl (&ep->regs->ep_irqenb);
236 writel (tmp, &ep->regs->ep_irqenb);
237 } else { /* dma, per-request */
238 tmp = (1 << (8 + ep->num)); /* completion */
239 tmp |= readl (&dev->regs->pciirqenb1);
240 writel (tmp, &dev->regs->pciirqenb1);
242 /* for short OUT transfers, dma completions can't
243 * advance the queue; do it pio-style, by hand.
244 * NOTE erratum 0112 workaround #2
246 if ((desc->bEndpointAddress & USB_DIR_IN) == 0) {
247 tmp = (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT_ENABLE);
248 writel (tmp, &ep->regs->ep_irqenb);
250 tmp = (1 << ep->num) | readl (&dev->regs->pciirqenb0);
251 writel (tmp, &dev->regs->pciirqenb0);
255 tmp = desc->bEndpointAddress;
256 DEBUG (dev, "enabled %s (ep%d%s-%s) %s max %04x\n",
257 _ep->name, tmp & 0x0f, DIR_STRING (tmp),
258 type_string (desc->bmAttributes),
259 ep->dma ? "dma" : "pio", max);
261 /* pci writes may still be posted */
262 spin_unlock_irqrestore (&dev->lock, flags);
263 return 0;
266 static int handshake (u32 __iomem *ptr, u32 mask, u32 done, int usec)
268 u32 result;
270 do {
271 result = readl (ptr);
272 if (result == ~(u32)0) /* "device unplugged" */
273 return -ENODEV;
274 result &= mask;
275 if (result == done)
276 return 0;
277 udelay (1);
278 usec--;
279 } while (usec > 0);
280 return -ETIMEDOUT;
283 static struct usb_ep_ops net2280_ep_ops;
285 static void ep_reset (struct net2280_regs __iomem *regs, struct net2280_ep *ep)
287 u32 tmp;
289 ep->desc = NULL;
290 INIT_LIST_HEAD (&ep->queue);
292 ep->ep.maxpacket = ~0;
293 ep->ep.ops = &net2280_ep_ops;
295 /* disable the dma, irqs, endpoint... */
296 if (ep->dma) {
297 writel (0, &ep->dma->dmactl);
298 writel ( (1 << DMA_SCATTER_GATHER_DONE_INTERRUPT)
299 | (1 << DMA_TRANSACTION_DONE_INTERRUPT)
300 | (1 << DMA_ABORT)
301 , &ep->dma->dmastat);
303 tmp = readl (&regs->pciirqenb0);
304 tmp &= ~(1 << ep->num);
305 writel (tmp, &regs->pciirqenb0);
306 } else {
307 tmp = readl (&regs->pciirqenb1);
308 tmp &= ~(1 << (8 + ep->num)); /* completion */
309 writel (tmp, &regs->pciirqenb1);
311 writel (0, &ep->regs->ep_irqenb);
313 /* init to our chosen defaults, notably so that we NAK OUT
314 * packets until the driver queues a read (+note erratum 0112)
316 tmp = (1 << SET_NAK_OUT_PACKETS_MODE)
317 | (1 << SET_NAK_OUT_PACKETS)
318 | (1 << CLEAR_EP_HIDE_STATUS_PHASE)
319 | (1 << CLEAR_INTERRUPT_MODE);
321 if (ep->num != 0) {
322 tmp |= (1 << CLEAR_ENDPOINT_TOGGLE)
323 | (1 << CLEAR_ENDPOINT_HALT);
325 writel (tmp, &ep->regs->ep_rsp);
327 /* scrub most status bits, and flush any fifo state */
328 writel ( (1 << TIMEOUT)
329 | (1 << USB_STALL_SENT)
330 | (1 << USB_IN_NAK_SENT)
331 | (1 << USB_IN_ACK_RCVD)
332 | (1 << USB_OUT_PING_NAK_SENT)
333 | (1 << USB_OUT_ACK_SENT)
334 | (1 << FIFO_OVERFLOW)
335 | (1 << FIFO_UNDERFLOW)
336 | (1 << FIFO_FLUSH)
337 | (1 << SHORT_PACKET_OUT_DONE_INTERRUPT)
338 | (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)
339 | (1 << DATA_PACKET_RECEIVED_INTERRUPT)
340 | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
341 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
342 | (1 << DATA_IN_TOKEN_INTERRUPT)
343 , &ep->regs->ep_stat);
345 /* fifo size is handled separately */
348 static void nuke (struct net2280_ep *);
350 static int net2280_disable (struct usb_ep *_ep)
352 struct net2280_ep *ep;
353 unsigned long flags;
355 ep = container_of (_ep, struct net2280_ep, ep);
356 if (!_ep || !ep->desc || _ep->name == ep0name)
357 return -EINVAL;
359 spin_lock_irqsave (&ep->dev->lock, flags);
360 nuke (ep);
361 ep_reset (ep->dev->regs, ep);
363 VDEBUG (ep->dev, "disabled %s %s\n",
364 ep->dma ? "dma" : "pio", _ep->name);
366 /* synch memory views with the device */
367 (void) readl (&ep->regs->ep_cfg);
369 if (use_dma && !ep->dma && ep->num >= 1 && ep->num <= 4)
370 ep->dma = &ep->dev->dma [ep->num - 1];
372 spin_unlock_irqrestore (&ep->dev->lock, flags);
373 return 0;
376 /*-------------------------------------------------------------------------*/
378 static struct usb_request *
379 net2280_alloc_request (struct usb_ep *_ep, gfp_t gfp_flags)
381 struct net2280_ep *ep;
382 struct net2280_request *req;
384 if (!_ep)
385 return NULL;
386 ep = container_of (_ep, struct net2280_ep, ep);
388 req = kmalloc (sizeof *req, gfp_flags);
389 if (!req)
390 return NULL;
392 memset (req, 0, sizeof *req);
393 req->req.dma = DMA_ADDR_INVALID;
394 INIT_LIST_HEAD (&req->queue);
396 /* this dma descriptor may be swapped with the previous dummy */
397 if (ep->dma) {
398 struct net2280_dma *td;
400 td = pci_pool_alloc (ep->dev->requests, gfp_flags,
401 &req->td_dma);
402 if (!td) {
403 kfree (req);
404 return NULL;
406 td->dmacount = 0; /* not VALID */
407 td->dmaaddr = __constant_cpu_to_le32 (DMA_ADDR_INVALID);
408 td->dmadesc = td->dmaaddr;
409 req->td = td;
411 return &req->req;
414 static void
415 net2280_free_request (struct usb_ep *_ep, struct usb_request *_req)
417 struct net2280_ep *ep;
418 struct net2280_request *req;
420 ep = container_of (_ep, struct net2280_ep, ep);
421 if (!_ep || !_req)
422 return;
424 req = container_of (_req, struct net2280_request, req);
425 WARN_ON (!list_empty (&req->queue));
426 if (req->td)
427 pci_pool_free (ep->dev->requests, req->td, req->td_dma);
428 kfree (req);
431 /*-------------------------------------------------------------------------*/
433 #undef USE_KMALLOC
435 /* many common platforms have dma-coherent caches, which means that it's
436 * safe to use kmalloc() memory for all i/o buffers without using any
437 * cache flushing calls. (unless you're trying to share cache lines
438 * between dma and non-dma activities, which is a slow idea in any case.)
440 * other platforms need more care, with 2.5 having a moderately general
441 * solution (which falls down for allocations smaller than one page)
442 * that improves significantly on the 2.4 PCI allocators by removing
443 * the restriction that memory never be freed in_interrupt().
445 #if defined(CONFIG_X86)
446 #define USE_KMALLOC
448 #elif defined(CONFIG_PPC) && !defined(CONFIG_NOT_COHERENT_CACHE)
449 #define USE_KMALLOC
451 #elif defined(CONFIG_MIPS) && !defined(CONFIG_DMA_NONCOHERENT)
452 #define USE_KMALLOC
454 /* FIXME there are other cases, including an x86-64 one ... */
455 #endif
457 /* allocating buffers this way eliminates dma mapping overhead, which
458 * on some platforms will mean eliminating a per-io buffer copy. with
459 * some kinds of system caches, further tweaks may still be needed.
461 static void *
462 net2280_alloc_buffer (
463 struct usb_ep *_ep,
464 unsigned bytes,
465 dma_addr_t *dma,
466 gfp_t gfp_flags
469 void *retval;
470 struct net2280_ep *ep;
472 ep = container_of (_ep, struct net2280_ep, ep);
473 if (!_ep)
474 return NULL;
475 *dma = DMA_ADDR_INVALID;
477 #if defined(USE_KMALLOC)
478 retval = kmalloc(bytes, gfp_flags);
479 if (retval)
480 *dma = virt_to_phys(retval);
481 #else
482 if (ep->dma) {
483 /* the main problem with this call is that it wastes memory
484 * on typical 1/N page allocations: it allocates 1-N pages.
486 #warning Using dma_alloc_coherent even with buffers smaller than a page.
487 retval = dma_alloc_coherent(&ep->dev->pdev->dev,
488 bytes, dma, gfp_flags);
489 } else
490 retval = kmalloc(bytes, gfp_flags);
491 #endif
492 return retval;
495 static void
496 net2280_free_buffer (
497 struct usb_ep *_ep,
498 void *buf,
499 dma_addr_t dma,
500 unsigned bytes
502 /* free memory into the right allocator */
503 #ifndef USE_KMALLOC
504 if (dma != DMA_ADDR_INVALID) {
505 struct net2280_ep *ep;
507 ep = container_of(_ep, struct net2280_ep, ep);
508 if (!_ep)
509 return;
510 dma_free_coherent(&ep->dev->pdev->dev, bytes, buf, dma);
511 } else
512 #endif
513 kfree (buf);
516 /*-------------------------------------------------------------------------*/
518 /* load a packet into the fifo we use for usb IN transfers.
519 * works for all endpoints.
521 * NOTE: pio with ep-a..ep-d could stuff multiple packets into the fifo
522 * at a time, but this code is simpler because it knows it only writes
523 * one packet. ep-a..ep-d should use dma instead.
525 static void
526 write_fifo (struct net2280_ep *ep, struct usb_request *req)
528 struct net2280_ep_regs __iomem *regs = ep->regs;
529 u8 *buf;
530 u32 tmp;
531 unsigned count, total;
533 /* INVARIANT: fifo is currently empty. (testable) */
535 if (req) {
536 buf = req->buf + req->actual;
537 prefetch (buf);
538 total = req->length - req->actual;
539 } else {
540 total = 0;
541 buf = NULL;
544 /* write just one packet at a time */
545 count = ep->ep.maxpacket;
546 if (count > total) /* min() cannot be used on a bitfield */
547 count = total;
549 VDEBUG (ep->dev, "write %s fifo (IN) %d bytes%s req %p\n",
550 ep->ep.name, count,
551 (count != ep->ep.maxpacket) ? " (short)" : "",
552 req);
553 while (count >= 4) {
554 /* NOTE be careful if you try to align these. fifo lines
555 * should normally be full (4 bytes) and successive partial
556 * lines are ok only in certain cases.
558 tmp = get_unaligned ((u32 *)buf);
559 cpu_to_le32s (&tmp);
560 writel (tmp, &regs->ep_data);
561 buf += 4;
562 count -= 4;
565 /* last fifo entry is "short" unless we wrote a full packet.
566 * also explicitly validate last word in (periodic) transfers
567 * when maxpacket is not a multiple of 4 bytes.
569 if (count || total < ep->ep.maxpacket) {
570 tmp = count ? get_unaligned ((u32 *)buf) : count;
571 cpu_to_le32s (&tmp);
572 set_fifo_bytecount (ep, count & 0x03);
573 writel (tmp, &regs->ep_data);
576 /* pci writes may still be posted */
579 /* work around erratum 0106: PCI and USB race over the OUT fifo.
580 * caller guarantees chiprev 0100, out endpoint is NAKing, and
581 * there's no real data in the fifo.
583 * NOTE: also used in cases where that erratum doesn't apply:
584 * where the host wrote "too much" data to us.
586 static void out_flush (struct net2280_ep *ep)
588 u32 __iomem *statp;
589 u32 tmp;
591 ASSERT_OUT_NAKING (ep);
593 statp = &ep->regs->ep_stat;
594 writel ( (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
595 | (1 << DATA_PACKET_RECEIVED_INTERRUPT)
596 , statp);
597 writel ((1 << FIFO_FLUSH), statp);
598 mb ();
599 tmp = readl (statp);
600 if (tmp & (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
601 /* high speed did bulk NYET; fifo isn't filling */
602 && ep->dev->gadget.speed == USB_SPEED_FULL) {
603 unsigned usec;
605 usec = 50; /* 64 byte bulk/interrupt */
606 handshake (statp, (1 << USB_OUT_PING_NAK_SENT),
607 (1 << USB_OUT_PING_NAK_SENT), usec);
608 /* NAK done; now CLEAR_NAK_OUT_PACKETS is safe */
612 /* unload packet(s) from the fifo we use for usb OUT transfers.
613 * returns true iff the request completed, because of short packet
614 * or the request buffer having filled with full packets.
616 * for ep-a..ep-d this will read multiple packets out when they
617 * have been accepted.
619 static int
620 read_fifo (struct net2280_ep *ep, struct net2280_request *req)
622 struct net2280_ep_regs __iomem *regs = ep->regs;
623 u8 *buf = req->req.buf + req->req.actual;
624 unsigned count, tmp, is_short;
625 unsigned cleanup = 0, prevent = 0;
627 /* erratum 0106 ... packets coming in during fifo reads might
628 * be incompletely rejected. not all cases have workarounds.
630 if (ep->dev->chiprev == 0x0100
631 && ep->dev->gadget.speed == USB_SPEED_FULL) {
632 udelay (1);
633 tmp = readl (&ep->regs->ep_stat);
634 if ((tmp & (1 << NAK_OUT_PACKETS)))
635 cleanup = 1;
636 else if ((tmp & (1 << FIFO_FULL))) {
637 start_out_naking (ep);
638 prevent = 1;
640 /* else: hope we don't see the problem */
643 /* never overflow the rx buffer. the fifo reads packets until
644 * it sees a short one; we might not be ready for them all.
646 prefetchw (buf);
647 count = readl (&regs->ep_avail);
648 if (unlikely (count == 0)) {
649 udelay (1);
650 tmp = readl (&ep->regs->ep_stat);
651 count = readl (&regs->ep_avail);
652 /* handled that data already? */
653 if (count == 0 && (tmp & (1 << NAK_OUT_PACKETS)) == 0)
654 return 0;
657 tmp = req->req.length - req->req.actual;
658 if (count > tmp) {
659 /* as with DMA, data overflow gets flushed */
660 if ((tmp % ep->ep.maxpacket) != 0) {
661 ERROR (ep->dev,
662 "%s out fifo %d bytes, expected %d\n",
663 ep->ep.name, count, tmp);
664 req->req.status = -EOVERFLOW;
665 cleanup = 1;
666 /* NAK_OUT_PACKETS will be set, so flushing is safe;
667 * the next read will start with the next packet
669 } /* else it's a ZLP, no worries */
670 count = tmp;
672 req->req.actual += count;
674 is_short = (count == 0) || ((count % ep->ep.maxpacket) != 0);
676 VDEBUG (ep->dev, "read %s fifo (OUT) %d bytes%s%s%s req %p %d/%d\n",
677 ep->ep.name, count, is_short ? " (short)" : "",
678 cleanup ? " flush" : "", prevent ? " nak" : "",
679 req, req->req.actual, req->req.length);
681 while (count >= 4) {
682 tmp = readl (&regs->ep_data);
683 cpu_to_le32s (&tmp);
684 put_unaligned (tmp, (u32 *)buf);
685 buf += 4;
686 count -= 4;
688 if (count) {
689 tmp = readl (&regs->ep_data);
690 /* LE conversion is implicit here: */
691 do {
692 *buf++ = (u8) tmp;
693 tmp >>= 8;
694 } while (--count);
696 if (cleanup)
697 out_flush (ep);
698 if (prevent) {
699 writel ((1 << CLEAR_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
700 (void) readl (&ep->regs->ep_rsp);
703 return is_short || ((req->req.actual == req->req.length)
704 && !req->req.zero);
707 /* fill out dma descriptor to match a given request */
708 static void
709 fill_dma_desc (struct net2280_ep *ep, struct net2280_request *req, int valid)
711 struct net2280_dma *td = req->td;
712 u32 dmacount = req->req.length;
714 /* don't let DMA continue after a short OUT packet,
715 * so overruns can't affect the next transfer.
716 * in case of overruns on max-size packets, we can't
717 * stop the fifo from filling but we can flush it.
719 if (ep->is_in)
720 dmacount |= (1 << DMA_DIRECTION);
721 else if ((dmacount % ep->ep.maxpacket) != 0)
722 dmacount |= (1 << END_OF_CHAIN);
724 req->valid = valid;
725 if (valid)
726 dmacount |= (1 << VALID_BIT);
727 if (likely(!req->req.no_interrupt || !use_dma_chaining))
728 dmacount |= (1 << DMA_DONE_INTERRUPT_ENABLE);
730 /* td->dmadesc = previously set by caller */
731 td->dmaaddr = cpu_to_le32 (req->req.dma);
733 /* 2280 may be polling VALID_BIT through ep->dma->dmadesc */
734 wmb ();
735 td->dmacount = cpu_to_le32p (&dmacount);
738 static const u32 dmactl_default =
739 (1 << DMA_SCATTER_GATHER_DONE_INTERRUPT)
740 | (1 << DMA_CLEAR_COUNT_ENABLE)
741 /* erratum 0116 workaround part 1 (use POLLING) */
742 | (POLL_100_USEC << DESCRIPTOR_POLLING_RATE)
743 | (1 << DMA_VALID_BIT_POLLING_ENABLE)
744 | (1 << DMA_VALID_BIT_ENABLE)
745 | (1 << DMA_SCATTER_GATHER_ENABLE)
746 /* erratum 0116 workaround part 2 (no AUTOSTART) */
747 | (1 << DMA_ENABLE);
749 static inline void spin_stop_dma (struct net2280_dma_regs __iomem *dma)
751 handshake (&dma->dmactl, (1 << DMA_ENABLE), 0, 50);
754 static inline void stop_dma (struct net2280_dma_regs __iomem *dma)
756 writel (readl (&dma->dmactl) & ~(1 << DMA_ENABLE), &dma->dmactl);
757 spin_stop_dma (dma);
760 static void start_queue (struct net2280_ep *ep, u32 dmactl, u32 td_dma)
762 struct net2280_dma_regs __iomem *dma = ep->dma;
764 writel ((1 << VALID_BIT) | (ep->is_in << DMA_DIRECTION),
765 &dma->dmacount);
766 writel (readl (&dma->dmastat), &dma->dmastat);
768 writel (td_dma, &dma->dmadesc);
769 writel (dmactl, &dma->dmactl);
771 /* erratum 0116 workaround part 3: pci arbiter away from net2280 */
772 (void) readl (&ep->dev->pci->pcimstctl);
774 writel ((1 << DMA_START), &dma->dmastat);
776 if (!ep->is_in)
777 stop_out_naking (ep);
780 static void start_dma (struct net2280_ep *ep, struct net2280_request *req)
782 u32 tmp;
783 struct net2280_dma_regs __iomem *dma = ep->dma;
785 /* FIXME can't use DMA for ZLPs */
787 /* on this path we "know" there's no dma active (yet) */
788 WARN_ON (readl (&dma->dmactl) & (1 << DMA_ENABLE));
789 writel (0, &ep->dma->dmactl);
791 /* previous OUT packet might have been short */
792 if (!ep->is_in && ((tmp = readl (&ep->regs->ep_stat))
793 & (1 << NAK_OUT_PACKETS)) != 0) {
794 writel ((1 << SHORT_PACKET_TRANSFERRED_INTERRUPT),
795 &ep->regs->ep_stat);
797 tmp = readl (&ep->regs->ep_avail);
798 if (tmp) {
799 writel (readl (&dma->dmastat), &dma->dmastat);
801 /* transfer all/some fifo data */
802 writel (req->req.dma, &dma->dmaaddr);
803 tmp = min (tmp, req->req.length);
805 /* dma irq, faking scatterlist status */
806 req->td->dmacount = cpu_to_le32 (req->req.length - tmp);
807 writel ((1 << DMA_DONE_INTERRUPT_ENABLE)
808 | tmp, &dma->dmacount);
809 req->td->dmadesc = 0;
810 req->valid = 1;
812 writel ((1 << DMA_ENABLE), &dma->dmactl);
813 writel ((1 << DMA_START), &dma->dmastat);
814 return;
818 tmp = dmactl_default;
820 /* force packet boundaries between dma requests, but prevent the
821 * controller from automagically writing a last "short" packet
822 * (zero length) unless the driver explicitly said to do that.
824 if (ep->is_in) {
825 if (likely ((req->req.length % ep->ep.maxpacket) != 0
826 || req->req.zero)) {
827 tmp |= (1 << DMA_FIFO_VALIDATE);
828 ep->in_fifo_validate = 1;
829 } else
830 ep->in_fifo_validate = 0;
833 /* init req->td, pointing to the current dummy */
834 req->td->dmadesc = cpu_to_le32 (ep->td_dma);
835 fill_dma_desc (ep, req, 1);
837 if (!use_dma_chaining)
838 req->td->dmacount |= __constant_cpu_to_le32 (1 << END_OF_CHAIN);
840 start_queue (ep, tmp, req->td_dma);
843 static inline void
844 queue_dma (struct net2280_ep *ep, struct net2280_request *req, int valid)
846 struct net2280_dma *end;
847 dma_addr_t tmp;
849 /* swap new dummy for old, link; fill and maybe activate */
850 end = ep->dummy;
851 ep->dummy = req->td;
852 req->td = end;
854 tmp = ep->td_dma;
855 ep->td_dma = req->td_dma;
856 req->td_dma = tmp;
858 end->dmadesc = cpu_to_le32 (ep->td_dma);
860 fill_dma_desc (ep, req, valid);
863 static void
864 done (struct net2280_ep *ep, struct net2280_request *req, int status)
866 struct net2280 *dev;
867 unsigned stopped = ep->stopped;
869 list_del_init (&req->queue);
871 if (req->req.status == -EINPROGRESS)
872 req->req.status = status;
873 else
874 status = req->req.status;
876 dev = ep->dev;
877 if (req->mapped) {
878 pci_unmap_single (dev->pdev, req->req.dma, req->req.length,
879 ep->is_in ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
880 req->req.dma = DMA_ADDR_INVALID;
881 req->mapped = 0;
884 if (status && status != -ESHUTDOWN)
885 VDEBUG (dev, "complete %s req %p stat %d len %u/%u\n",
886 ep->ep.name, &req->req, status,
887 req->req.actual, req->req.length);
889 /* don't modify queue heads during completion callback */
890 ep->stopped = 1;
891 spin_unlock (&dev->lock);
892 req->req.complete (&ep->ep, &req->req);
893 spin_lock (&dev->lock);
894 ep->stopped = stopped;
897 /*-------------------------------------------------------------------------*/
899 static int
900 net2280_queue (struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
902 struct net2280_request *req;
903 struct net2280_ep *ep;
904 struct net2280 *dev;
905 unsigned long flags;
907 /* we always require a cpu-view buffer, so that we can
908 * always use pio (as fallback or whatever).
910 req = container_of (_req, struct net2280_request, req);
911 if (!_req || !_req->complete || !_req->buf
912 || !list_empty (&req->queue))
913 return -EINVAL;
914 if (_req->length > (~0 & DMA_BYTE_COUNT_MASK))
915 return -EDOM;
916 ep = container_of (_ep, struct net2280_ep, ep);
917 if (!_ep || (!ep->desc && ep->num != 0))
918 return -EINVAL;
919 dev = ep->dev;
920 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
921 return -ESHUTDOWN;
923 /* FIXME implement PIO fallback for ZLPs with DMA */
924 if (ep->dma && _req->length == 0)
925 return -EOPNOTSUPP;
927 /* set up dma mapping in case the caller didn't */
928 if (ep->dma && _req->dma == DMA_ADDR_INVALID) {
929 _req->dma = pci_map_single (dev->pdev, _req->buf, _req->length,
930 ep->is_in ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
931 req->mapped = 1;
934 #if 0
935 VDEBUG (dev, "%s queue req %p, len %d buf %p\n",
936 _ep->name, _req, _req->length, _req->buf);
937 #endif
939 spin_lock_irqsave (&dev->lock, flags);
941 _req->status = -EINPROGRESS;
942 _req->actual = 0;
944 /* kickstart this i/o queue? */
945 if (list_empty (&ep->queue) && !ep->stopped) {
946 /* use DMA if the endpoint supports it, else pio */
947 if (ep->dma)
948 start_dma (ep, req);
949 else {
950 /* maybe there's no control data, just status ack */
951 if (ep->num == 0 && _req->length == 0) {
952 allow_status (ep);
953 done (ep, req, 0);
954 VDEBUG (dev, "%s status ack\n", ep->ep.name);
955 goto done;
958 /* PIO ... stuff the fifo, or unblock it. */
959 if (ep->is_in)
960 write_fifo (ep, _req);
961 else if (list_empty (&ep->queue)) {
962 u32 s;
964 /* OUT FIFO might have packet(s) buffered */
965 s = readl (&ep->regs->ep_stat);
966 if ((s & (1 << FIFO_EMPTY)) == 0) {
967 /* note: _req->short_not_ok is
968 * ignored here since PIO _always_
969 * stops queue advance here, and
970 * _req->status doesn't change for
971 * short reads (only _req->actual)
973 if (read_fifo (ep, req)) {
974 done (ep, req, 0);
975 if (ep->num == 0)
976 allow_status (ep);
977 /* don't queue it */
978 req = NULL;
979 } else
980 s = readl (&ep->regs->ep_stat);
983 /* don't NAK, let the fifo fill */
984 if (req && (s & (1 << NAK_OUT_PACKETS)))
985 writel ((1 << CLEAR_NAK_OUT_PACKETS),
986 &ep->regs->ep_rsp);
990 } else if (ep->dma) {
991 int valid = 1;
993 if (ep->is_in) {
994 int expect;
996 /* preventing magic zlps is per-engine state, not
997 * per-transfer; irq logic must recover hiccups.
999 expect = likely (req->req.zero
1000 || (req->req.length % ep->ep.maxpacket) != 0);
1001 if (expect != ep->in_fifo_validate)
1002 valid = 0;
1004 queue_dma (ep, req, valid);
1006 } /* else the irq handler advances the queue. */
1008 if (req)
1009 list_add_tail (&req->queue, &ep->queue);
1010 done:
1011 spin_unlock_irqrestore (&dev->lock, flags);
1013 /* pci writes may still be posted */
1014 return 0;
1017 static inline void
1018 dma_done (
1019 struct net2280_ep *ep,
1020 struct net2280_request *req,
1021 u32 dmacount,
1022 int status
1025 req->req.actual = req->req.length - (DMA_BYTE_COUNT_MASK & dmacount);
1026 done (ep, req, status);
1029 static void restart_dma (struct net2280_ep *ep);
1031 static void scan_dma_completions (struct net2280_ep *ep)
1033 /* only look at descriptors that were "naturally" retired,
1034 * so fifo and list head state won't matter
1036 while (!list_empty (&ep->queue)) {
1037 struct net2280_request *req;
1038 u32 tmp;
1040 req = list_entry (ep->queue.next,
1041 struct net2280_request, queue);
1042 if (!req->valid)
1043 break;
1044 rmb ();
1045 tmp = le32_to_cpup (&req->td->dmacount);
1046 if ((tmp & (1 << VALID_BIT)) != 0)
1047 break;
1049 /* SHORT_PACKET_TRANSFERRED_INTERRUPT handles "usb-short"
1050 * cases where DMA must be aborted; this code handles
1051 * all non-abort DMA completions.
1053 if (unlikely (req->td->dmadesc == 0)) {
1054 /* paranoia */
1055 tmp = readl (&ep->dma->dmacount);
1056 if (tmp & DMA_BYTE_COUNT_MASK)
1057 break;
1058 /* single transfer mode */
1059 dma_done (ep, req, tmp, 0);
1060 break;
1061 } else if (!ep->is_in
1062 && (req->req.length % ep->ep.maxpacket) != 0) {
1063 tmp = readl (&ep->regs->ep_stat);
1065 /* AVOID TROUBLE HERE by not issuing short reads from
1066 * your gadget driver. That helps avoids errata 0121,
1067 * 0122, and 0124; not all cases trigger the warning.
1069 if ((tmp & (1 << NAK_OUT_PACKETS)) == 0) {
1070 WARN (ep->dev, "%s lost packet sync!\n",
1071 ep->ep.name);
1072 req->req.status = -EOVERFLOW;
1073 } else if ((tmp = readl (&ep->regs->ep_avail)) != 0) {
1074 /* fifo gets flushed later */
1075 ep->out_overflow = 1;
1076 DEBUG (ep->dev, "%s dma, discard %d len %d\n",
1077 ep->ep.name, tmp,
1078 req->req.length);
1079 req->req.status = -EOVERFLOW;
1082 dma_done (ep, req, tmp, 0);
1086 static void restart_dma (struct net2280_ep *ep)
1088 struct net2280_request *req;
1089 u32 dmactl = dmactl_default;
1091 if (ep->stopped)
1092 return;
1093 req = list_entry (ep->queue.next, struct net2280_request, queue);
1095 if (!use_dma_chaining) {
1096 start_dma (ep, req);
1097 return;
1100 /* the 2280 will be processing the queue unless queue hiccups after
1101 * the previous transfer:
1102 * IN: wanted automagic zlp, head doesn't (or vice versa)
1103 * DMA_FIFO_VALIDATE doesn't init from dma descriptors.
1104 * OUT: was "usb-short", we must restart.
1106 if (ep->is_in && !req->valid) {
1107 struct net2280_request *entry, *prev = NULL;
1108 int reqmode, done = 0;
1110 DEBUG (ep->dev, "%s dma hiccup td %p\n", ep->ep.name, req->td);
1111 ep->in_fifo_validate = likely (req->req.zero
1112 || (req->req.length % ep->ep.maxpacket) != 0);
1113 if (ep->in_fifo_validate)
1114 dmactl |= (1 << DMA_FIFO_VALIDATE);
1115 list_for_each_entry (entry, &ep->queue, queue) {
1116 __le32 dmacount;
1118 if (entry == req)
1119 continue;
1120 dmacount = entry->td->dmacount;
1121 if (!done) {
1122 reqmode = likely (entry->req.zero
1123 || (entry->req.length
1124 % ep->ep.maxpacket) != 0);
1125 if (reqmode == ep->in_fifo_validate) {
1126 entry->valid = 1;
1127 dmacount |= valid_bit;
1128 entry->td->dmacount = dmacount;
1129 prev = entry;
1130 continue;
1131 } else {
1132 /* force a hiccup */
1133 prev->td->dmacount |= dma_done_ie;
1134 done = 1;
1138 /* walk the rest of the queue so unlinks behave */
1139 entry->valid = 0;
1140 dmacount &= ~valid_bit;
1141 entry->td->dmacount = dmacount;
1142 prev = entry;
1146 writel (0, &ep->dma->dmactl);
1147 start_queue (ep, dmactl, req->td_dma);
1150 static void abort_dma (struct net2280_ep *ep)
1152 /* abort the current transfer */
1153 if (likely (!list_empty (&ep->queue))) {
1154 /* FIXME work around errata 0121, 0122, 0124 */
1155 writel ((1 << DMA_ABORT), &ep->dma->dmastat);
1156 spin_stop_dma (ep->dma);
1157 } else
1158 stop_dma (ep->dma);
1159 scan_dma_completions (ep);
1162 /* dequeue ALL requests */
1163 static void nuke (struct net2280_ep *ep)
1165 struct net2280_request *req;
1167 /* called with spinlock held */
1168 ep->stopped = 1;
1169 if (ep->dma)
1170 abort_dma (ep);
1171 while (!list_empty (&ep->queue)) {
1172 req = list_entry (ep->queue.next,
1173 struct net2280_request,
1174 queue);
1175 done (ep, req, -ESHUTDOWN);
1179 /* dequeue JUST ONE request */
1180 static int net2280_dequeue (struct usb_ep *_ep, struct usb_request *_req)
1182 struct net2280_ep *ep;
1183 struct net2280_request *req;
1184 unsigned long flags;
1185 u32 dmactl;
1186 int stopped;
1188 ep = container_of (_ep, struct net2280_ep, ep);
1189 if (!_ep || (!ep->desc && ep->num != 0) || !_req)
1190 return -EINVAL;
1192 spin_lock_irqsave (&ep->dev->lock, flags);
1193 stopped = ep->stopped;
1195 /* quiesce dma while we patch the queue */
1196 dmactl = 0;
1197 ep->stopped = 1;
1198 if (ep->dma) {
1199 dmactl = readl (&ep->dma->dmactl);
1200 /* WARNING erratum 0127 may kick in ... */
1201 stop_dma (ep->dma);
1202 scan_dma_completions (ep);
1205 /* make sure it's still queued on this endpoint */
1206 list_for_each_entry (req, &ep->queue, queue) {
1207 if (&req->req == _req)
1208 break;
1210 if (&req->req != _req) {
1211 spin_unlock_irqrestore (&ep->dev->lock, flags);
1212 return -EINVAL;
1215 /* queue head may be partially complete. */
1216 if (ep->queue.next == &req->queue) {
1217 if (ep->dma) {
1218 DEBUG (ep->dev, "unlink (%s) dma\n", _ep->name);
1219 _req->status = -ECONNRESET;
1220 abort_dma (ep);
1221 if (likely (ep->queue.next == &req->queue)) {
1222 // NOTE: misreports single-transfer mode
1223 req->td->dmacount = 0; /* invalidate */
1224 dma_done (ep, req,
1225 readl (&ep->dma->dmacount),
1226 -ECONNRESET);
1228 } else {
1229 DEBUG (ep->dev, "unlink (%s) pio\n", _ep->name);
1230 done (ep, req, -ECONNRESET);
1232 req = NULL;
1234 /* patch up hardware chaining data */
1235 } else if (ep->dma && use_dma_chaining) {
1236 if (req->queue.prev == ep->queue.next) {
1237 writel (le32_to_cpu (req->td->dmadesc),
1238 &ep->dma->dmadesc);
1239 if (req->td->dmacount & dma_done_ie)
1240 writel (readl (&ep->dma->dmacount)
1241 | le32_to_cpu(dma_done_ie),
1242 &ep->dma->dmacount);
1243 } else {
1244 struct net2280_request *prev;
1246 prev = list_entry (req->queue.prev,
1247 struct net2280_request, queue);
1248 prev->td->dmadesc = req->td->dmadesc;
1249 if (req->td->dmacount & dma_done_ie)
1250 prev->td->dmacount |= dma_done_ie;
1254 if (req)
1255 done (ep, req, -ECONNRESET);
1256 ep->stopped = stopped;
1258 if (ep->dma) {
1259 /* turn off dma on inactive queues */
1260 if (list_empty (&ep->queue))
1261 stop_dma (ep->dma);
1262 else if (!ep->stopped) {
1263 /* resume current request, or start new one */
1264 if (req)
1265 writel (dmactl, &ep->dma->dmactl);
1266 else
1267 start_dma (ep, list_entry (ep->queue.next,
1268 struct net2280_request, queue));
1272 spin_unlock_irqrestore (&ep->dev->lock, flags);
1273 return 0;
1276 /*-------------------------------------------------------------------------*/
1278 static int net2280_fifo_status (struct usb_ep *_ep);
1280 static int
1281 net2280_set_halt (struct usb_ep *_ep, int value)
1283 struct net2280_ep *ep;
1284 unsigned long flags;
1285 int retval = 0;
1287 ep = container_of (_ep, struct net2280_ep, ep);
1288 if (!_ep || (!ep->desc && ep->num != 0))
1289 return -EINVAL;
1290 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1291 return -ESHUTDOWN;
1292 if (ep->desc /* not ep0 */ && (ep->desc->bmAttributes & 0x03)
1293 == USB_ENDPOINT_XFER_ISOC)
1294 return -EINVAL;
1296 spin_lock_irqsave (&ep->dev->lock, flags);
1297 if (!list_empty (&ep->queue))
1298 retval = -EAGAIN;
1299 else if (ep->is_in && value && net2280_fifo_status (_ep) != 0)
1300 retval = -EAGAIN;
1301 else {
1302 VDEBUG (ep->dev, "%s %s halt\n", _ep->name,
1303 value ? "set" : "clear");
1304 /* set/clear, then synch memory views with the device */
1305 if (value) {
1306 if (ep->num == 0)
1307 ep->dev->protocol_stall = 1;
1308 else
1309 set_halt (ep);
1310 } else
1311 clear_halt (ep);
1312 (void) readl (&ep->regs->ep_rsp);
1314 spin_unlock_irqrestore (&ep->dev->lock, flags);
1316 return retval;
1319 static int
1320 net2280_fifo_status (struct usb_ep *_ep)
1322 struct net2280_ep *ep;
1323 u32 avail;
1325 ep = container_of (_ep, struct net2280_ep, ep);
1326 if (!_ep || (!ep->desc && ep->num != 0))
1327 return -ENODEV;
1328 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1329 return -ESHUTDOWN;
1331 avail = readl (&ep->regs->ep_avail) & ((1 << 12) - 1);
1332 if (avail > ep->fifo_size)
1333 return -EOVERFLOW;
1334 if (ep->is_in)
1335 avail = ep->fifo_size - avail;
1336 return avail;
1339 static void
1340 net2280_fifo_flush (struct usb_ep *_ep)
1342 struct net2280_ep *ep;
1344 ep = container_of (_ep, struct net2280_ep, ep);
1345 if (!_ep || (!ep->desc && ep->num != 0))
1346 return;
1347 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1348 return;
1350 writel ((1 << FIFO_FLUSH), &ep->regs->ep_stat);
1351 (void) readl (&ep->regs->ep_rsp);
1354 static struct usb_ep_ops net2280_ep_ops = {
1355 .enable = net2280_enable,
1356 .disable = net2280_disable,
1358 .alloc_request = net2280_alloc_request,
1359 .free_request = net2280_free_request,
1361 .alloc_buffer = net2280_alloc_buffer,
1362 .free_buffer = net2280_free_buffer,
1364 .queue = net2280_queue,
1365 .dequeue = net2280_dequeue,
1367 .set_halt = net2280_set_halt,
1368 .fifo_status = net2280_fifo_status,
1369 .fifo_flush = net2280_fifo_flush,
1372 /*-------------------------------------------------------------------------*/
1374 static int net2280_get_frame (struct usb_gadget *_gadget)
1376 struct net2280 *dev;
1377 unsigned long flags;
1378 u16 retval;
1380 if (!_gadget)
1381 return -ENODEV;
1382 dev = container_of (_gadget, struct net2280, gadget);
1383 spin_lock_irqsave (&dev->lock, flags);
1384 retval = get_idx_reg (dev->regs, REG_FRAME) & 0x03ff;
1385 spin_unlock_irqrestore (&dev->lock, flags);
1386 return retval;
1389 static int net2280_wakeup (struct usb_gadget *_gadget)
1391 struct net2280 *dev;
1392 u32 tmp;
1393 unsigned long flags;
1395 if (!_gadget)
1396 return 0;
1397 dev = container_of (_gadget, struct net2280, gadget);
1399 spin_lock_irqsave (&dev->lock, flags);
1400 tmp = readl (&dev->usb->usbctl);
1401 if (tmp & (1 << DEVICE_REMOTE_WAKEUP_ENABLE))
1402 writel (1 << GENERATE_RESUME, &dev->usb->usbstat);
1403 spin_unlock_irqrestore (&dev->lock, flags);
1405 /* pci writes may still be posted */
1406 return 0;
1409 static int net2280_set_selfpowered (struct usb_gadget *_gadget, int value)
1411 struct net2280 *dev;
1412 u32 tmp;
1413 unsigned long flags;
1415 if (!_gadget)
1416 return 0;
1417 dev = container_of (_gadget, struct net2280, gadget);
1419 spin_lock_irqsave (&dev->lock, flags);
1420 tmp = readl (&dev->usb->usbctl);
1421 if (value)
1422 tmp |= (1 << SELF_POWERED_STATUS);
1423 else
1424 tmp &= ~(1 << SELF_POWERED_STATUS);
1425 writel (tmp, &dev->usb->usbctl);
1426 spin_unlock_irqrestore (&dev->lock, flags);
1428 return 0;
1431 static int net2280_pullup(struct usb_gadget *_gadget, int is_on)
1433 struct net2280 *dev;
1434 u32 tmp;
1435 unsigned long flags;
1437 if (!_gadget)
1438 return -ENODEV;
1439 dev = container_of (_gadget, struct net2280, gadget);
1441 spin_lock_irqsave (&dev->lock, flags);
1442 tmp = readl (&dev->usb->usbctl);
1443 dev->softconnect = (is_on != 0);
1444 if (is_on)
1445 tmp |= (1 << USB_DETECT_ENABLE);
1446 else
1447 tmp &= ~(1 << USB_DETECT_ENABLE);
1448 writel (tmp, &dev->usb->usbctl);
1449 spin_unlock_irqrestore (&dev->lock, flags);
1451 return 0;
1454 static const struct usb_gadget_ops net2280_ops = {
1455 .get_frame = net2280_get_frame,
1456 .wakeup = net2280_wakeup,
1457 .set_selfpowered = net2280_set_selfpowered,
1458 .pullup = net2280_pullup,
1461 /*-------------------------------------------------------------------------*/
1463 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1465 /* FIXME move these into procfs, and use seq_file.
1466 * Sysfs _still_ doesn't behave for arbitrarily sized files,
1467 * and also doesn't help products using this with 2.4 kernels.
1470 /* "function" sysfs attribute */
1471 static ssize_t
1472 show_function (struct device *_dev, struct device_attribute *attr, char *buf)
1474 struct net2280 *dev = dev_get_drvdata (_dev);
1476 if (!dev->driver
1477 || !dev->driver->function
1478 || strlen (dev->driver->function) > PAGE_SIZE)
1479 return 0;
1480 return scnprintf (buf, PAGE_SIZE, "%s\n", dev->driver->function);
1482 static DEVICE_ATTR (function, S_IRUGO, show_function, NULL);
1484 static ssize_t
1485 show_registers (struct device *_dev, struct device_attribute *attr, char *buf)
1487 struct net2280 *dev;
1488 char *next;
1489 unsigned size, t;
1490 unsigned long flags;
1491 int i;
1492 u32 t1, t2;
1493 const char *s;
1495 dev = dev_get_drvdata (_dev);
1496 next = buf;
1497 size = PAGE_SIZE;
1498 spin_lock_irqsave (&dev->lock, flags);
1500 if (dev->driver)
1501 s = dev->driver->driver.name;
1502 else
1503 s = "(none)";
1505 /* Main Control Registers */
1506 t = scnprintf (next, size, "%s version " DRIVER_VERSION
1507 ", chiprev %04x, dma %s\n\n"
1508 "devinit %03x fifoctl %08x gadget '%s'\n"
1509 "pci irqenb0 %02x irqenb1 %08x "
1510 "irqstat0 %04x irqstat1 %08x\n",
1511 driver_name, dev->chiprev,
1512 use_dma
1513 ? (use_dma_chaining ? "chaining" : "enabled")
1514 : "disabled",
1515 readl (&dev->regs->devinit),
1516 readl (&dev->regs->fifoctl),
1518 readl (&dev->regs->pciirqenb0),
1519 readl (&dev->regs->pciirqenb1),
1520 readl (&dev->regs->irqstat0),
1521 readl (&dev->regs->irqstat1));
1522 size -= t;
1523 next += t;
1525 /* USB Control Registers */
1526 t1 = readl (&dev->usb->usbctl);
1527 t2 = readl (&dev->usb->usbstat);
1528 if (t1 & (1 << VBUS_PIN)) {
1529 if (t2 & (1 << HIGH_SPEED))
1530 s = "high speed";
1531 else if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1532 s = "powered";
1533 else
1534 s = "full speed";
1535 /* full speed bit (6) not working?? */
1536 } else
1537 s = "not attached";
1538 t = scnprintf (next, size,
1539 "stdrsp %08x usbctl %08x usbstat %08x "
1540 "addr 0x%02x (%s)\n",
1541 readl (&dev->usb->stdrsp), t1, t2,
1542 readl (&dev->usb->ouraddr), s);
1543 size -= t;
1544 next += t;
1546 /* PCI Master Control Registers */
1548 /* DMA Control Registers */
1550 /* Configurable EP Control Registers */
1551 for (i = 0; i < 7; i++) {
1552 struct net2280_ep *ep;
1554 ep = &dev->ep [i];
1555 if (i && !ep->desc)
1556 continue;
1558 t1 = readl (&ep->regs->ep_cfg);
1559 t2 = readl (&ep->regs->ep_rsp) & 0xff;
1560 t = scnprintf (next, size,
1561 "\n%s\tcfg %05x rsp (%02x) %s%s%s%s%s%s%s%s"
1562 "irqenb %02x\n",
1563 ep->ep.name, t1, t2,
1564 (t2 & (1 << CLEAR_NAK_OUT_PACKETS))
1565 ? "NAK " : "",
1566 (t2 & (1 << CLEAR_EP_HIDE_STATUS_PHASE))
1567 ? "hide " : "",
1568 (t2 & (1 << CLEAR_EP_FORCE_CRC_ERROR))
1569 ? "CRC " : "",
1570 (t2 & (1 << CLEAR_INTERRUPT_MODE))
1571 ? "interrupt " : "",
1572 (t2 & (1<<CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE))
1573 ? "status " : "",
1574 (t2 & (1 << CLEAR_NAK_OUT_PACKETS_MODE))
1575 ? "NAKmode " : "",
1576 (t2 & (1 << CLEAR_ENDPOINT_TOGGLE))
1577 ? "DATA1 " : "DATA0 ",
1578 (t2 & (1 << CLEAR_ENDPOINT_HALT))
1579 ? "HALT " : "",
1580 readl (&ep->regs->ep_irqenb));
1581 size -= t;
1582 next += t;
1584 t = scnprintf (next, size,
1585 "\tstat %08x avail %04x "
1586 "(ep%d%s-%s)%s\n",
1587 readl (&ep->regs->ep_stat),
1588 readl (&ep->regs->ep_avail),
1589 t1 & 0x0f, DIR_STRING (t1),
1590 type_string (t1 >> 8),
1591 ep->stopped ? "*" : "");
1592 size -= t;
1593 next += t;
1595 if (!ep->dma)
1596 continue;
1598 t = scnprintf (next, size,
1599 " dma\tctl %08x stat %08x count %08x\n"
1600 "\taddr %08x desc %08x\n",
1601 readl (&ep->dma->dmactl),
1602 readl (&ep->dma->dmastat),
1603 readl (&ep->dma->dmacount),
1604 readl (&ep->dma->dmaaddr),
1605 readl (&ep->dma->dmadesc));
1606 size -= t;
1607 next += t;
1611 /* Indexed Registers */
1612 // none yet
1614 /* Statistics */
1615 t = scnprintf (next, size, "\nirqs: ");
1616 size -= t;
1617 next += t;
1618 for (i = 0; i < 7; i++) {
1619 struct net2280_ep *ep;
1621 ep = &dev->ep [i];
1622 if (i && !ep->irqs)
1623 continue;
1624 t = scnprintf (next, size, " %s/%lu", ep->ep.name, ep->irqs);
1625 size -= t;
1626 next += t;
1629 t = scnprintf (next, size, "\n");
1630 size -= t;
1631 next += t;
1633 spin_unlock_irqrestore (&dev->lock, flags);
1635 return PAGE_SIZE - size;
1637 static DEVICE_ATTR (registers, S_IRUGO, show_registers, NULL);
1639 static ssize_t
1640 show_queues (struct device *_dev, struct device_attribute *attr, char *buf)
1642 struct net2280 *dev;
1643 char *next;
1644 unsigned size;
1645 unsigned long flags;
1646 int i;
1648 dev = dev_get_drvdata (_dev);
1649 next = buf;
1650 size = PAGE_SIZE;
1651 spin_lock_irqsave (&dev->lock, flags);
1653 for (i = 0; i < 7; i++) {
1654 struct net2280_ep *ep = &dev->ep [i];
1655 struct net2280_request *req;
1656 int t;
1658 if (i != 0) {
1659 const struct usb_endpoint_descriptor *d;
1661 d = ep->desc;
1662 if (!d)
1663 continue;
1664 t = d->bEndpointAddress;
1665 t = scnprintf (next, size,
1666 "\n%s (ep%d%s-%s) max %04x %s fifo %d\n",
1667 ep->ep.name, t & USB_ENDPOINT_NUMBER_MASK,
1668 (t & USB_DIR_IN) ? "in" : "out",
1669 ({ char *val;
1670 switch (d->bmAttributes & 0x03) {
1671 case USB_ENDPOINT_XFER_BULK:
1672 val = "bulk"; break;
1673 case USB_ENDPOINT_XFER_INT:
1674 val = "intr"; break;
1675 default:
1676 val = "iso"; break;
1677 }; val; }),
1678 le16_to_cpu (d->wMaxPacketSize) & 0x1fff,
1679 ep->dma ? "dma" : "pio", ep->fifo_size
1681 } else /* ep0 should only have one transfer queued */
1682 t = scnprintf (next, size, "ep0 max 64 pio %s\n",
1683 ep->is_in ? "in" : "out");
1684 if (t <= 0 || t > size)
1685 goto done;
1686 size -= t;
1687 next += t;
1689 if (list_empty (&ep->queue)) {
1690 t = scnprintf (next, size, "\t(nothing queued)\n");
1691 if (t <= 0 || t > size)
1692 goto done;
1693 size -= t;
1694 next += t;
1695 continue;
1697 list_for_each_entry (req, &ep->queue, queue) {
1698 if (ep->dma && req->td_dma == readl (&ep->dma->dmadesc))
1699 t = scnprintf (next, size,
1700 "\treq %p len %d/%d "
1701 "buf %p (dmacount %08x)\n",
1702 &req->req, req->req.actual,
1703 req->req.length, req->req.buf,
1704 readl (&ep->dma->dmacount));
1705 else
1706 t = scnprintf (next, size,
1707 "\treq %p len %d/%d buf %p\n",
1708 &req->req, req->req.actual,
1709 req->req.length, req->req.buf);
1710 if (t <= 0 || t > size)
1711 goto done;
1712 size -= t;
1713 next += t;
1715 if (ep->dma) {
1716 struct net2280_dma *td;
1718 td = req->td;
1719 t = scnprintf (next, size, "\t td %08x "
1720 " count %08x buf %08x desc %08x\n",
1721 (u32) req->td_dma,
1722 le32_to_cpu (td->dmacount),
1723 le32_to_cpu (td->dmaaddr),
1724 le32_to_cpu (td->dmadesc));
1725 if (t <= 0 || t > size)
1726 goto done;
1727 size -= t;
1728 next += t;
1733 done:
1734 spin_unlock_irqrestore (&dev->lock, flags);
1735 return PAGE_SIZE - size;
1737 static DEVICE_ATTR (queues, S_IRUGO, show_queues, NULL);
1740 #else
1742 #define device_create_file(a,b) do {} while (0)
1743 #define device_remove_file device_create_file
1745 #endif
1747 /*-------------------------------------------------------------------------*/
1749 /* another driver-specific mode might be a request type doing dma
1750 * to/from another device fifo instead of to/from memory.
1753 static void set_fifo_mode (struct net2280 *dev, int mode)
1755 /* keeping high bits preserves BAR2 */
1756 writel ((0xffff << PCI_BASE2_RANGE) | mode, &dev->regs->fifoctl);
1758 /* always ep-{a,b,e,f} ... maybe not ep-c or ep-d */
1759 INIT_LIST_HEAD (&dev->gadget.ep_list);
1760 list_add_tail (&dev->ep [1].ep.ep_list, &dev->gadget.ep_list);
1761 list_add_tail (&dev->ep [2].ep.ep_list, &dev->gadget.ep_list);
1762 switch (mode) {
1763 case 0:
1764 list_add_tail (&dev->ep [3].ep.ep_list, &dev->gadget.ep_list);
1765 list_add_tail (&dev->ep [4].ep.ep_list, &dev->gadget.ep_list);
1766 dev->ep [1].fifo_size = dev->ep [2].fifo_size = 1024;
1767 break;
1768 case 1:
1769 dev->ep [1].fifo_size = dev->ep [2].fifo_size = 2048;
1770 break;
1771 case 2:
1772 list_add_tail (&dev->ep [3].ep.ep_list, &dev->gadget.ep_list);
1773 dev->ep [1].fifo_size = 2048;
1774 dev->ep [2].fifo_size = 1024;
1775 break;
1777 /* fifo sizes for ep0, ep-c, ep-d, ep-e, and ep-f never change */
1778 list_add_tail (&dev->ep [5].ep.ep_list, &dev->gadget.ep_list);
1779 list_add_tail (&dev->ep [6].ep.ep_list, &dev->gadget.ep_list);
1782 /* just declare this in any driver that really need it */
1783 extern int net2280_set_fifo_mode (struct usb_gadget *gadget, int mode);
1786 * net2280_set_fifo_mode - change allocation of fifo buffers
1787 * @gadget: access to the net2280 device that will be updated
1788 * @mode: 0 for default, four 1kB buffers (ep-a through ep-d);
1789 * 1 for two 2kB buffers (ep-a and ep-b only);
1790 * 2 for one 2kB buffer (ep-a) and two 1kB ones (ep-b, ep-c).
1792 * returns zero on success, else negative errno. when this succeeds,
1793 * the contents of gadget->ep_list may have changed.
1795 * you may only call this function when endpoints a-d are all disabled.
1796 * use it whenever extra hardware buffering can help performance, such
1797 * as before enabling "high bandwidth" interrupt endpoints that use
1798 * maxpacket bigger than 512 (when double buffering would otherwise
1799 * be unavailable).
1801 int net2280_set_fifo_mode (struct usb_gadget *gadget, int mode)
1803 int i;
1804 struct net2280 *dev;
1805 int status = 0;
1806 unsigned long flags;
1808 if (!gadget)
1809 return -ENODEV;
1810 dev = container_of (gadget, struct net2280, gadget);
1812 spin_lock_irqsave (&dev->lock, flags);
1814 for (i = 1; i <= 4; i++)
1815 if (dev->ep [i].desc) {
1816 status = -EINVAL;
1817 break;
1819 if (mode < 0 || mode > 2)
1820 status = -EINVAL;
1821 if (status == 0)
1822 set_fifo_mode (dev, mode);
1823 spin_unlock_irqrestore (&dev->lock, flags);
1825 if (status == 0) {
1826 if (mode == 1)
1827 DEBUG (dev, "fifo: ep-a 2K, ep-b 2K\n");
1828 else if (mode == 2)
1829 DEBUG (dev, "fifo: ep-a 2K, ep-b 1K, ep-c 1K\n");
1830 /* else all are 1K */
1832 return status;
1834 EXPORT_SYMBOL (net2280_set_fifo_mode);
1836 /*-------------------------------------------------------------------------*/
1838 /* keeping it simple:
1839 * - one bus driver, initted first;
1840 * - one function driver, initted second
1842 * most of the work to support multiple net2280 controllers would
1843 * be to associate this gadget driver (yes?) with all of them, or
1844 * perhaps to bind specific drivers to specific devices.
1847 static struct net2280 *the_controller;
1849 static void usb_reset (struct net2280 *dev)
1851 u32 tmp;
1853 dev->gadget.speed = USB_SPEED_UNKNOWN;
1854 (void) readl (&dev->usb->usbctl);
1856 net2280_led_init (dev);
1858 /* disable automatic responses, and irqs */
1859 writel (0, &dev->usb->stdrsp);
1860 writel (0, &dev->regs->pciirqenb0);
1861 writel (0, &dev->regs->pciirqenb1);
1863 /* clear old dma and irq state */
1864 for (tmp = 0; tmp < 4; tmp++) {
1865 struct net2280_ep *ep = &dev->ep [tmp + 1];
1867 if (ep->dma)
1868 abort_dma (ep);
1870 writel (~0, &dev->regs->irqstat0),
1871 writel (~(1 << SUSPEND_REQUEST_INTERRUPT), &dev->regs->irqstat1),
1873 /* reset, and enable pci */
1874 tmp = readl (&dev->regs->devinit)
1875 | (1 << PCI_ENABLE)
1876 | (1 << FIFO_SOFT_RESET)
1877 | (1 << USB_SOFT_RESET)
1878 | (1 << M8051_RESET);
1879 writel (tmp, &dev->regs->devinit);
1881 /* standard fifo and endpoint allocations */
1882 set_fifo_mode (dev, (fifo_mode <= 2) ? fifo_mode : 0);
1885 static void usb_reinit (struct net2280 *dev)
1887 u32 tmp;
1888 int init_dma;
1890 /* use_dma changes are ignored till next device re-init */
1891 init_dma = use_dma;
1893 /* basic endpoint init */
1894 for (tmp = 0; tmp < 7; tmp++) {
1895 struct net2280_ep *ep = &dev->ep [tmp];
1897 ep->ep.name = ep_name [tmp];
1898 ep->dev = dev;
1899 ep->num = tmp;
1901 if (tmp > 0 && tmp <= 4) {
1902 ep->fifo_size = 1024;
1903 if (init_dma)
1904 ep->dma = &dev->dma [tmp - 1];
1905 } else
1906 ep->fifo_size = 64;
1907 ep->regs = &dev->epregs [tmp];
1908 ep_reset (dev->regs, ep);
1910 dev->ep [0].ep.maxpacket = 64;
1911 dev->ep [5].ep.maxpacket = 64;
1912 dev->ep [6].ep.maxpacket = 64;
1914 dev->gadget.ep0 = &dev->ep [0].ep;
1915 dev->ep [0].stopped = 0;
1916 INIT_LIST_HEAD (&dev->gadget.ep0->ep_list);
1918 /* we want to prevent lowlevel/insecure access from the USB host,
1919 * but erratum 0119 means this enable bit is ignored
1921 for (tmp = 0; tmp < 5; tmp++)
1922 writel (EP_DONTUSE, &dev->dep [tmp].dep_cfg);
1925 static void ep0_start (struct net2280 *dev)
1927 writel ( (1 << CLEAR_EP_HIDE_STATUS_PHASE)
1928 | (1 << CLEAR_NAK_OUT_PACKETS)
1929 | (1 << CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE)
1930 , &dev->epregs [0].ep_rsp);
1933 * hardware optionally handles a bunch of standard requests
1934 * that the API hides from drivers anyway. have it do so.
1935 * endpoint status/features are handled in software, to
1936 * help pass tests for some dubious behavior.
1938 writel ( (1 << SET_TEST_MODE)
1939 | (1 << SET_ADDRESS)
1940 | (1 << DEVICE_SET_CLEAR_DEVICE_REMOTE_WAKEUP)
1941 | (1 << GET_DEVICE_STATUS)
1942 | (1 << GET_INTERFACE_STATUS)
1943 , &dev->usb->stdrsp);
1944 writel ( (1 << USB_ROOT_PORT_WAKEUP_ENABLE)
1945 | (1 << SELF_POWERED_USB_DEVICE)
1946 | (1 << REMOTE_WAKEUP_SUPPORT)
1947 | (dev->softconnect << USB_DETECT_ENABLE)
1948 | (1 << SELF_POWERED_STATUS)
1949 , &dev->usb->usbctl);
1951 /* enable irqs so we can see ep0 and general operation */
1952 writel ( (1 << SETUP_PACKET_INTERRUPT_ENABLE)
1953 | (1 << ENDPOINT_0_INTERRUPT_ENABLE)
1954 , &dev->regs->pciirqenb0);
1955 writel ( (1 << PCI_INTERRUPT_ENABLE)
1956 | (1 << PCI_MASTER_ABORT_RECEIVED_INTERRUPT_ENABLE)
1957 | (1 << PCI_TARGET_ABORT_RECEIVED_INTERRUPT_ENABLE)
1958 | (1 << PCI_RETRY_ABORT_INTERRUPT_ENABLE)
1959 | (1 << VBUS_INTERRUPT_ENABLE)
1960 | (1 << ROOT_PORT_RESET_INTERRUPT_ENABLE)
1961 | (1 << SUSPEND_REQUEST_CHANGE_INTERRUPT_ENABLE)
1962 , &dev->regs->pciirqenb1);
1964 /* don't leave any writes posted */
1965 (void) readl (&dev->usb->usbctl);
1968 /* when a driver is successfully registered, it will receive
1969 * control requests including set_configuration(), which enables
1970 * non-control requests. then usb traffic follows until a
1971 * disconnect is reported. then a host may connect again, or
1972 * the driver might get unbound.
1974 int usb_gadget_register_driver (struct usb_gadget_driver *driver)
1976 struct net2280 *dev = the_controller;
1977 int retval;
1978 unsigned i;
1980 /* insist on high speed support from the driver, since
1981 * (dev->usb->xcvrdiag & FORCE_FULL_SPEED_MODE)
1982 * "must not be used in normal operation"
1984 if (!driver
1985 || driver->speed != USB_SPEED_HIGH
1986 || !driver->bind
1987 || !driver->unbind
1988 || !driver->setup)
1989 return -EINVAL;
1990 if (!dev)
1991 return -ENODEV;
1992 if (dev->driver)
1993 return -EBUSY;
1995 for (i = 0; i < 7; i++)
1996 dev->ep [i].irqs = 0;
1998 /* hook up the driver ... */
1999 dev->softconnect = 1;
2000 driver->driver.bus = NULL;
2001 dev->driver = driver;
2002 dev->gadget.dev.driver = &driver->driver;
2003 retval = driver->bind (&dev->gadget);
2004 if (retval) {
2005 DEBUG (dev, "bind to driver %s --> %d\n",
2006 driver->driver.name, retval);
2007 dev->driver = NULL;
2008 dev->gadget.dev.driver = NULL;
2009 return retval;
2012 device_create_file (&dev->pdev->dev, &dev_attr_function);
2013 device_create_file (&dev->pdev->dev, &dev_attr_queues);
2015 /* ... then enable host detection and ep0; and we're ready
2016 * for set_configuration as well as eventual disconnect.
2018 net2280_led_active (dev, 1);
2019 ep0_start (dev);
2021 DEBUG (dev, "%s ready, usbctl %08x stdrsp %08x\n",
2022 driver->driver.name,
2023 readl (&dev->usb->usbctl),
2024 readl (&dev->usb->stdrsp));
2026 /* pci writes may still be posted */
2027 return 0;
2029 EXPORT_SYMBOL (usb_gadget_register_driver);
2031 static void
2032 stop_activity (struct net2280 *dev, struct usb_gadget_driver *driver)
2034 int i;
2036 /* don't disconnect if it's not connected */
2037 if (dev->gadget.speed == USB_SPEED_UNKNOWN)
2038 driver = NULL;
2040 /* stop hardware; prevent new request submissions;
2041 * and kill any outstanding requests.
2043 usb_reset (dev);
2044 for (i = 0; i < 7; i++)
2045 nuke (&dev->ep [i]);
2047 /* report disconnect; the driver is already quiesced */
2048 if (driver) {
2049 spin_unlock (&dev->lock);
2050 driver->disconnect (&dev->gadget);
2051 spin_lock (&dev->lock);
2054 usb_reinit (dev);
2057 int usb_gadget_unregister_driver (struct usb_gadget_driver *driver)
2059 struct net2280 *dev = the_controller;
2060 unsigned long flags;
2062 if (!dev)
2063 return -ENODEV;
2064 if (!driver || driver != dev->driver)
2065 return -EINVAL;
2067 spin_lock_irqsave (&dev->lock, flags);
2068 stop_activity (dev, driver);
2069 spin_unlock_irqrestore (&dev->lock, flags);
2071 net2280_pullup (&dev->gadget, 0);
2073 driver->unbind (&dev->gadget);
2074 dev->gadget.dev.driver = NULL;
2075 dev->driver = NULL;
2077 net2280_led_active (dev, 0);
2078 device_remove_file (&dev->pdev->dev, &dev_attr_function);
2079 device_remove_file (&dev->pdev->dev, &dev_attr_queues);
2081 DEBUG (dev, "unregistered driver '%s'\n", driver->driver.name);
2082 return 0;
2084 EXPORT_SYMBOL (usb_gadget_unregister_driver);
2087 /*-------------------------------------------------------------------------*/
2089 /* handle ep0, ep-e, ep-f with 64 byte packets: packet per irq.
2090 * also works for dma-capable endpoints, in pio mode or just
2091 * to manually advance the queue after short OUT transfers.
2093 static void handle_ep_small (struct net2280_ep *ep)
2095 struct net2280_request *req;
2096 u32 t;
2097 /* 0 error, 1 mid-data, 2 done */
2098 int mode = 1;
2100 if (!list_empty (&ep->queue))
2101 req = list_entry (ep->queue.next,
2102 struct net2280_request, queue);
2103 else
2104 req = NULL;
2106 /* ack all, and handle what we care about */
2107 t = readl (&ep->regs->ep_stat);
2108 ep->irqs++;
2109 #if 0
2110 VDEBUG (ep->dev, "%s ack ep_stat %08x, req %p\n",
2111 ep->ep.name, t, req ? &req->req : 0);
2112 #endif
2113 writel (t & ~(1 << NAK_OUT_PACKETS), &ep->regs->ep_stat);
2115 /* for ep0, monitor token irqs to catch data stage length errors
2116 * and to synchronize on status.
2118 * also, to defer reporting of protocol stalls ... here's where
2119 * data or status first appears, handling stalls here should never
2120 * cause trouble on the host side..
2122 * control requests could be slightly faster without token synch for
2123 * status, but status can jam up that way.
2125 if (unlikely (ep->num == 0)) {
2126 if (ep->is_in) {
2127 /* status; stop NAKing */
2128 if (t & (1 << DATA_OUT_PING_TOKEN_INTERRUPT)) {
2129 if (ep->dev->protocol_stall) {
2130 ep->stopped = 1;
2131 set_halt (ep);
2133 if (!req)
2134 allow_status (ep);
2135 mode = 2;
2136 /* reply to extra IN data tokens with a zlp */
2137 } else if (t & (1 << DATA_IN_TOKEN_INTERRUPT)) {
2138 if (ep->dev->protocol_stall) {
2139 ep->stopped = 1;
2140 set_halt (ep);
2141 mode = 2;
2142 } else if (!req && ep->stopped)
2143 write_fifo (ep, NULL);
2145 } else {
2146 /* status; stop NAKing */
2147 if (t & (1 << DATA_IN_TOKEN_INTERRUPT)) {
2148 if (ep->dev->protocol_stall) {
2149 ep->stopped = 1;
2150 set_halt (ep);
2152 mode = 2;
2153 /* an extra OUT token is an error */
2154 } else if (((t & (1 << DATA_OUT_PING_TOKEN_INTERRUPT))
2155 && req
2156 && req->req.actual == req->req.length)
2157 || !req) {
2158 ep->dev->protocol_stall = 1;
2159 set_halt (ep);
2160 ep->stopped = 1;
2161 if (req)
2162 done (ep, req, -EOVERFLOW);
2163 req = NULL;
2168 if (unlikely (!req))
2169 return;
2171 /* manual DMA queue advance after short OUT */
2172 if (likely (ep->dma != 0)) {
2173 if (t & (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)) {
2174 u32 count;
2175 int stopped = ep->stopped;
2177 /* TRANSFERRED works around OUT_DONE erratum 0112.
2178 * we expect (N <= maxpacket) bytes; host wrote M.
2179 * iff (M < N) we won't ever see a DMA interrupt.
2181 ep->stopped = 1;
2182 for (count = 0; ; t = readl (&ep->regs->ep_stat)) {
2184 /* any preceding dma transfers must finish.
2185 * dma handles (M >= N), may empty the queue
2187 scan_dma_completions (ep);
2188 if (unlikely (list_empty (&ep->queue)
2189 || ep->out_overflow)) {
2190 req = NULL;
2191 break;
2193 req = list_entry (ep->queue.next,
2194 struct net2280_request, queue);
2196 /* here either (M < N), a "real" short rx;
2197 * or (M == N) and the queue didn't empty
2199 if (likely (t & (1 << FIFO_EMPTY))) {
2200 count = readl (&ep->dma->dmacount);
2201 count &= DMA_BYTE_COUNT_MASK;
2202 if (readl (&ep->dma->dmadesc)
2203 != req->td_dma)
2204 req = NULL;
2205 break;
2207 udelay(1);
2210 /* stop DMA, leave ep NAKing */
2211 writel ((1 << DMA_ABORT), &ep->dma->dmastat);
2212 spin_stop_dma (ep->dma);
2214 if (likely (req)) {
2215 req->td->dmacount = 0;
2216 t = readl (&ep->regs->ep_avail);
2217 dma_done (ep, req, count, t);
2220 /* also flush to prevent erratum 0106 trouble */
2221 if (unlikely (ep->out_overflow
2222 || (ep->dev->chiprev == 0x0100
2223 && ep->dev->gadget.speed
2224 == USB_SPEED_FULL))) {
2225 out_flush (ep);
2226 ep->out_overflow = 0;
2229 /* (re)start dma if needed, stop NAKing */
2230 ep->stopped = stopped;
2231 if (!list_empty (&ep->queue))
2232 restart_dma (ep);
2233 } else
2234 DEBUG (ep->dev, "%s dma ep_stat %08x ??\n",
2235 ep->ep.name, t);
2236 return;
2238 /* data packet(s) received (in the fifo, OUT) */
2239 } else if (t & (1 << DATA_PACKET_RECEIVED_INTERRUPT)) {
2240 if (read_fifo (ep, req) && ep->num != 0)
2241 mode = 2;
2243 /* data packet(s) transmitted (IN) */
2244 } else if (t & (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)) {
2245 unsigned len;
2247 len = req->req.length - req->req.actual;
2248 if (len > ep->ep.maxpacket)
2249 len = ep->ep.maxpacket;
2250 req->req.actual += len;
2252 /* if we wrote it all, we're usually done */
2253 if (req->req.actual == req->req.length) {
2254 if (ep->num == 0) {
2255 /* wait for control status */
2256 if (mode != 2)
2257 req = NULL;
2258 } else if (!req->req.zero || len != ep->ep.maxpacket)
2259 mode = 2;
2262 /* there was nothing to do ... */
2263 } else if (mode == 1)
2264 return;
2266 /* done */
2267 if (mode == 2) {
2268 /* stream endpoints often resubmit/unlink in completion */
2269 done (ep, req, 0);
2271 /* maybe advance queue to next request */
2272 if (ep->num == 0) {
2273 /* NOTE: net2280 could let gadget driver start the
2274 * status stage later. since not all controllers let
2275 * them control that, the api doesn't (yet) allow it.
2277 if (!ep->stopped)
2278 allow_status (ep);
2279 req = NULL;
2280 } else {
2281 if (!list_empty (&ep->queue) && !ep->stopped)
2282 req = list_entry (ep->queue.next,
2283 struct net2280_request, queue);
2284 else
2285 req = NULL;
2286 if (req && !ep->is_in)
2287 stop_out_naking (ep);
2291 /* is there a buffer for the next packet?
2292 * for best streaming performance, make sure there is one.
2294 if (req && !ep->stopped) {
2296 /* load IN fifo with next packet (may be zlp) */
2297 if (t & (1 << DATA_PACKET_TRANSMITTED_INTERRUPT))
2298 write_fifo (ep, &req->req);
2302 static struct net2280_ep *
2303 get_ep_by_addr (struct net2280 *dev, u16 wIndex)
2305 struct net2280_ep *ep;
2307 if ((wIndex & USB_ENDPOINT_NUMBER_MASK) == 0)
2308 return &dev->ep [0];
2309 list_for_each_entry (ep, &dev->gadget.ep_list, ep.ep_list) {
2310 u8 bEndpointAddress;
2312 if (!ep->desc)
2313 continue;
2314 bEndpointAddress = ep->desc->bEndpointAddress;
2315 if ((wIndex ^ bEndpointAddress) & USB_DIR_IN)
2316 continue;
2317 if ((wIndex & 0x0f) == (bEndpointAddress & 0x0f))
2318 return ep;
2320 return NULL;
2323 static void handle_stat0_irqs (struct net2280 *dev, u32 stat)
2325 struct net2280_ep *ep;
2326 u32 num, scratch;
2328 /* most of these don't need individual acks */
2329 stat &= ~(1 << INTA_ASSERTED);
2330 if (!stat)
2331 return;
2332 // DEBUG (dev, "irqstat0 %04x\n", stat);
2334 /* starting a control request? */
2335 if (unlikely (stat & (1 << SETUP_PACKET_INTERRUPT))) {
2336 union {
2337 u32 raw [2];
2338 struct usb_ctrlrequest r;
2339 } u;
2340 int tmp = 0;
2341 struct net2280_request *req;
2343 if (dev->gadget.speed == USB_SPEED_UNKNOWN) {
2344 if (readl (&dev->usb->usbstat) & (1 << HIGH_SPEED))
2345 dev->gadget.speed = USB_SPEED_HIGH;
2346 else
2347 dev->gadget.speed = USB_SPEED_FULL;
2348 net2280_led_speed (dev, dev->gadget.speed);
2349 DEBUG (dev, "%s speed\n",
2350 (dev->gadget.speed == USB_SPEED_HIGH)
2351 ? "high" : "full");
2354 ep = &dev->ep [0];
2355 ep->irqs++;
2357 /* make sure any leftover request state is cleared */
2358 stat &= ~(1 << ENDPOINT_0_INTERRUPT);
2359 while (!list_empty (&ep->queue)) {
2360 req = list_entry (ep->queue.next,
2361 struct net2280_request, queue);
2362 done (ep, req, (req->req.actual == req->req.length)
2363 ? 0 : -EPROTO);
2365 ep->stopped = 0;
2366 dev->protocol_stall = 0;
2367 writel ( (1 << TIMEOUT)
2368 | (1 << USB_STALL_SENT)
2369 | (1 << USB_IN_NAK_SENT)
2370 | (1 << USB_IN_ACK_RCVD)
2371 | (1 << USB_OUT_PING_NAK_SENT)
2372 | (1 << USB_OUT_ACK_SENT)
2373 | (1 << FIFO_OVERFLOW)
2374 | (1 << FIFO_UNDERFLOW)
2375 | (1 << SHORT_PACKET_OUT_DONE_INTERRUPT)
2376 | (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)
2377 | (1 << DATA_PACKET_RECEIVED_INTERRUPT)
2378 | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
2379 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
2380 | (1 << DATA_IN_TOKEN_INTERRUPT)
2381 , &ep->regs->ep_stat);
2382 u.raw [0] = readl (&dev->usb->setup0123);
2383 u.raw [1] = readl (&dev->usb->setup4567);
2385 cpu_to_le32s (&u.raw [0]);
2386 cpu_to_le32s (&u.raw [1]);
2388 #define w_value le16_to_cpup (&u.r.wValue)
2389 #define w_index le16_to_cpup (&u.r.wIndex)
2390 #define w_length le16_to_cpup (&u.r.wLength)
2392 /* ack the irq */
2393 writel (1 << SETUP_PACKET_INTERRUPT, &dev->regs->irqstat0);
2394 stat ^= (1 << SETUP_PACKET_INTERRUPT);
2396 /* watch control traffic at the token level, and force
2397 * synchronization before letting the status stage happen.
2398 * FIXME ignore tokens we'll NAK, until driver responds.
2399 * that'll mean a lot less irqs for some drivers.
2401 ep->is_in = (u.r.bRequestType & USB_DIR_IN) != 0;
2402 if (ep->is_in) {
2403 scratch = (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
2404 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
2405 | (1 << DATA_IN_TOKEN_INTERRUPT);
2406 stop_out_naking (ep);
2407 } else
2408 scratch = (1 << DATA_PACKET_RECEIVED_INTERRUPT)
2409 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
2410 | (1 << DATA_IN_TOKEN_INTERRUPT);
2411 writel (scratch, &dev->epregs [0].ep_irqenb);
2413 /* we made the hardware handle most lowlevel requests;
2414 * everything else goes uplevel to the gadget code.
2416 switch (u.r.bRequest) {
2417 case USB_REQ_GET_STATUS: {
2418 struct net2280_ep *e;
2419 __le32 status;
2421 /* hw handles device and interface status */
2422 if (u.r.bRequestType != (USB_DIR_IN|USB_RECIP_ENDPOINT))
2423 goto delegate;
2424 if ((e = get_ep_by_addr (dev, w_index)) == 0
2425 || w_length > 2)
2426 goto do_stall;
2428 if (readl (&e->regs->ep_rsp)
2429 & (1 << SET_ENDPOINT_HALT))
2430 status = __constant_cpu_to_le32 (1);
2431 else
2432 status = __constant_cpu_to_le32 (0);
2434 /* don't bother with a request object! */
2435 writel (0, &dev->epregs [0].ep_irqenb);
2436 set_fifo_bytecount (ep, w_length);
2437 writel ((__force u32)status, &dev->epregs [0].ep_data);
2438 allow_status (ep);
2439 VDEBUG (dev, "%s stat %02x\n", ep->ep.name, status);
2440 goto next_endpoints;
2442 break;
2443 case USB_REQ_CLEAR_FEATURE: {
2444 struct net2280_ep *e;
2446 /* hw handles device features */
2447 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
2448 goto delegate;
2449 if (w_value != USB_ENDPOINT_HALT
2450 || w_length != 0)
2451 goto do_stall;
2452 if ((e = get_ep_by_addr (dev, w_index)) == 0)
2453 goto do_stall;
2454 clear_halt (e);
2455 allow_status (ep);
2456 VDEBUG (dev, "%s clear halt\n", ep->ep.name);
2457 goto next_endpoints;
2459 break;
2460 case USB_REQ_SET_FEATURE: {
2461 struct net2280_ep *e;
2463 /* hw handles device features */
2464 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
2465 goto delegate;
2466 if (w_value != USB_ENDPOINT_HALT
2467 || w_length != 0)
2468 goto do_stall;
2469 if ((e = get_ep_by_addr (dev, w_index)) == 0)
2470 goto do_stall;
2471 set_halt (e);
2472 allow_status (ep);
2473 VDEBUG (dev, "%s set halt\n", ep->ep.name);
2474 goto next_endpoints;
2476 break;
2477 default:
2478 delegate:
2479 VDEBUG (dev, "setup %02x.%02x v%04x i%04x l%04x"
2480 "ep_cfg %08x\n",
2481 u.r.bRequestType, u.r.bRequest,
2482 w_value, w_index, w_length,
2483 readl (&ep->regs->ep_cfg));
2484 spin_unlock (&dev->lock);
2485 tmp = dev->driver->setup (&dev->gadget, &u.r);
2486 spin_lock (&dev->lock);
2489 /* stall ep0 on error */
2490 if (tmp < 0) {
2491 do_stall:
2492 VDEBUG (dev, "req %02x.%02x protocol STALL; stat %d\n",
2493 u.r.bRequestType, u.r.bRequest, tmp);
2494 dev->protocol_stall = 1;
2497 /* some in/out token irq should follow; maybe stall then.
2498 * driver must queue a request (even zlp) or halt ep0
2499 * before the host times out.
2503 #undef w_value
2504 #undef w_index
2505 #undef w_length
2507 next_endpoints:
2508 /* endpoint data irq ? */
2509 scratch = stat & 0x7f;
2510 stat &= ~0x7f;
2511 for (num = 0; scratch; num++) {
2512 u32 t;
2514 /* do this endpoint's FIFO and queue need tending? */
2515 t = 1 << num;
2516 if ((scratch & t) == 0)
2517 continue;
2518 scratch ^= t;
2520 ep = &dev->ep [num];
2521 handle_ep_small (ep);
2524 if (stat)
2525 DEBUG (dev, "unhandled irqstat0 %08x\n", stat);
2528 #define DMA_INTERRUPTS ( \
2529 (1 << DMA_D_INTERRUPT) \
2530 | (1 << DMA_C_INTERRUPT) \
2531 | (1 << DMA_B_INTERRUPT) \
2532 | (1 << DMA_A_INTERRUPT))
2533 #define PCI_ERROR_INTERRUPTS ( \
2534 (1 << PCI_MASTER_ABORT_RECEIVED_INTERRUPT) \
2535 | (1 << PCI_TARGET_ABORT_RECEIVED_INTERRUPT) \
2536 | (1 << PCI_RETRY_ABORT_INTERRUPT))
2538 static void handle_stat1_irqs (struct net2280 *dev, u32 stat)
2540 struct net2280_ep *ep;
2541 u32 tmp, num, mask, scratch;
2543 /* after disconnect there's nothing else to do! */
2544 tmp = (1 << VBUS_INTERRUPT) | (1 << ROOT_PORT_RESET_INTERRUPT);
2545 mask = (1 << HIGH_SPEED) | (1 << FULL_SPEED);
2547 /* VBUS disconnect is indicated by VBUS_PIN and VBUS_INTERRUPT set.
2548 * Root Port Reset is indicated by ROOT_PORT_RESET_INTERRRUPT set and
2549 * both HIGH_SPEED and FULL_SPEED clear (as ROOT_PORT_RESET_INTERRUPT
2550 * only indicates a change in the reset state).
2552 if (stat & tmp) {
2553 writel (tmp, &dev->regs->irqstat1);
2554 if ((((stat & (1 << ROOT_PORT_RESET_INTERRUPT)) &&
2555 ((readl (&dev->usb->usbstat) & mask) == 0))
2556 || ((readl (&dev->usb->usbctl) & (1 << VBUS_PIN)) == 0)
2557 ) && ( dev->gadget.speed != USB_SPEED_UNKNOWN)) {
2558 DEBUG (dev, "disconnect %s\n",
2559 dev->driver->driver.name);
2560 stop_activity (dev, dev->driver);
2561 ep0_start (dev);
2562 return;
2564 stat &= ~tmp;
2566 /* vBUS can bounce ... one of many reasons to ignore the
2567 * notion of hotplug events on bus connect/disconnect!
2569 if (!stat)
2570 return;
2573 /* NOTE: chip stays in PCI D0 state for now, but it could
2574 * enter D1 to save more power
2576 tmp = (1 << SUSPEND_REQUEST_CHANGE_INTERRUPT);
2577 if (stat & tmp) {
2578 writel (tmp, &dev->regs->irqstat1);
2579 if (stat & (1 << SUSPEND_REQUEST_INTERRUPT)) {
2580 if (dev->driver->suspend)
2581 dev->driver->suspend (&dev->gadget);
2582 if (!enable_suspend)
2583 stat &= ~(1 << SUSPEND_REQUEST_INTERRUPT);
2584 } else {
2585 if (dev->driver->resume)
2586 dev->driver->resume (&dev->gadget);
2587 /* at high speed, note erratum 0133 */
2589 stat &= ~tmp;
2592 /* clear any other status/irqs */
2593 if (stat)
2594 writel (stat, &dev->regs->irqstat1);
2596 /* some status we can just ignore */
2597 stat &= ~((1 << CONTROL_STATUS_INTERRUPT)
2598 | (1 << SUSPEND_REQUEST_INTERRUPT)
2599 | (1 << RESUME_INTERRUPT)
2600 | (1 << SOF_INTERRUPT));
2601 if (!stat)
2602 return;
2603 // DEBUG (dev, "irqstat1 %08x\n", stat);
2605 /* DMA status, for ep-{a,b,c,d} */
2606 scratch = stat & DMA_INTERRUPTS;
2607 stat &= ~DMA_INTERRUPTS;
2608 scratch >>= 9;
2609 for (num = 0; scratch; num++) {
2610 struct net2280_dma_regs __iomem *dma;
2612 tmp = 1 << num;
2613 if ((tmp & scratch) == 0)
2614 continue;
2615 scratch ^= tmp;
2617 ep = &dev->ep [num + 1];
2618 dma = ep->dma;
2620 if (!dma)
2621 continue;
2623 /* clear ep's dma status */
2624 tmp = readl (&dma->dmastat);
2625 writel (tmp, &dma->dmastat);
2627 /* chaining should stop on abort, short OUT from fifo,
2628 * or (stat0 codepath) short OUT transfer.
2630 if (!use_dma_chaining) {
2631 if ((tmp & (1 << DMA_TRANSACTION_DONE_INTERRUPT))
2632 == 0) {
2633 DEBUG (ep->dev, "%s no xact done? %08x\n",
2634 ep->ep.name, tmp);
2635 continue;
2637 stop_dma (ep->dma);
2640 /* OUT transfers terminate when the data from the
2641 * host is in our memory. Process whatever's done.
2642 * On this path, we know transfer's last packet wasn't
2643 * less than req->length. NAK_OUT_PACKETS may be set,
2644 * or the FIFO may already be holding new packets.
2646 * IN transfers can linger in the FIFO for a very
2647 * long time ... we ignore that for now, accounting
2648 * precisely (like PIO does) needs per-packet irqs
2650 scan_dma_completions (ep);
2652 /* disable dma on inactive queues; else maybe restart */
2653 if (list_empty (&ep->queue)) {
2654 if (use_dma_chaining)
2655 stop_dma (ep->dma);
2656 } else {
2657 tmp = readl (&dma->dmactl);
2658 if (!use_dma_chaining
2659 || (tmp & (1 << DMA_ENABLE)) == 0)
2660 restart_dma (ep);
2661 else if (ep->is_in && use_dma_chaining) {
2662 struct net2280_request *req;
2663 __le32 dmacount;
2665 /* the descriptor at the head of the chain
2666 * may still have VALID_BIT clear; that's
2667 * used to trigger changing DMA_FIFO_VALIDATE
2668 * (affects automagic zlp writes).
2670 req = list_entry (ep->queue.next,
2671 struct net2280_request, queue);
2672 dmacount = req->td->dmacount;
2673 dmacount &= __constant_cpu_to_le32 (
2674 (1 << VALID_BIT)
2675 | DMA_BYTE_COUNT_MASK);
2676 if (dmacount && (dmacount & valid_bit) == 0)
2677 restart_dma (ep);
2680 ep->irqs++;
2683 /* NOTE: there are other PCI errors we might usefully notice.
2684 * if they appear very often, here's where to try recovering.
2686 if (stat & PCI_ERROR_INTERRUPTS) {
2687 ERROR (dev, "pci dma error; stat %08x\n", stat);
2688 stat &= ~PCI_ERROR_INTERRUPTS;
2689 /* these are fatal errors, but "maybe" they won't
2690 * happen again ...
2692 stop_activity (dev, dev->driver);
2693 ep0_start (dev);
2694 stat = 0;
2697 if (stat)
2698 DEBUG (dev, "unhandled irqstat1 %08x\n", stat);
2701 static irqreturn_t net2280_irq (int irq, void *_dev, struct pt_regs * r)
2703 struct net2280 *dev = _dev;
2705 spin_lock (&dev->lock);
2707 /* handle disconnect, dma, and more */
2708 handle_stat1_irqs (dev, readl (&dev->regs->irqstat1));
2710 /* control requests and PIO */
2711 handle_stat0_irqs (dev, readl (&dev->regs->irqstat0));
2713 spin_unlock (&dev->lock);
2715 return IRQ_HANDLED;
2718 /*-------------------------------------------------------------------------*/
2720 static void gadget_release (struct device *_dev)
2722 struct net2280 *dev = dev_get_drvdata (_dev);
2724 kfree (dev);
2727 /* tear down the binding between this driver and the pci device */
2729 static void net2280_remove (struct pci_dev *pdev)
2731 struct net2280 *dev = pci_get_drvdata (pdev);
2733 /* start with the driver above us */
2734 if (dev->driver) {
2735 /* should have been done already by driver model core */
2736 WARN (dev, "pci remove, driver '%s' is still registered\n",
2737 dev->driver->driver.name);
2738 usb_gadget_unregister_driver (dev->driver);
2741 /* then clean up the resources we allocated during probe() */
2742 net2280_led_shutdown (dev);
2743 if (dev->requests) {
2744 int i;
2745 for (i = 1; i < 5; i++) {
2746 if (!dev->ep [i].dummy)
2747 continue;
2748 pci_pool_free (dev->requests, dev->ep [i].dummy,
2749 dev->ep [i].td_dma);
2751 pci_pool_destroy (dev->requests);
2753 if (dev->got_irq)
2754 free_irq (pdev->irq, dev);
2755 if (dev->regs)
2756 iounmap (dev->regs);
2757 if (dev->region)
2758 release_mem_region (pci_resource_start (pdev, 0),
2759 pci_resource_len (pdev, 0));
2760 if (dev->enabled)
2761 pci_disable_device (pdev);
2762 device_unregister (&dev->gadget.dev);
2763 device_remove_file (&pdev->dev, &dev_attr_registers);
2764 pci_set_drvdata (pdev, NULL);
2766 INFO (dev, "unbind\n");
2768 the_controller = NULL;
2771 /* wrap this driver around the specified device, but
2772 * don't respond over USB until a gadget driver binds to us.
2775 static int net2280_probe (struct pci_dev *pdev, const struct pci_device_id *id)
2777 struct net2280 *dev;
2778 unsigned long resource, len;
2779 void __iomem *base = NULL;
2780 int retval, i;
2781 char buf [8], *bufp;
2783 /* if you want to support more than one controller in a system,
2784 * usb_gadget_driver_{register,unregister}() must change.
2786 if (the_controller) {
2787 dev_warn (&pdev->dev, "ignoring\n");
2788 return -EBUSY;
2791 /* alloc, and start init */
2792 dev = kmalloc (sizeof *dev, SLAB_KERNEL);
2793 if (dev == NULL){
2794 retval = -ENOMEM;
2795 goto done;
2798 memset (dev, 0, sizeof *dev);
2799 spin_lock_init (&dev->lock);
2800 dev->pdev = pdev;
2801 dev->gadget.ops = &net2280_ops;
2802 dev->gadget.is_dualspeed = 1;
2804 /* the "gadget" abstracts/virtualizes the controller */
2805 strcpy (dev->gadget.dev.bus_id, "gadget");
2806 dev->gadget.dev.parent = &pdev->dev;
2807 dev->gadget.dev.dma_mask = pdev->dev.dma_mask;
2808 dev->gadget.dev.release = gadget_release;
2809 dev->gadget.name = driver_name;
2811 /* now all the pci goodies ... */
2812 if (pci_enable_device (pdev) < 0) {
2813 retval = -ENODEV;
2814 goto done;
2816 dev->enabled = 1;
2818 /* BAR 0 holds all the registers
2819 * BAR 1 is 8051 memory; unused here (note erratum 0103)
2820 * BAR 2 is fifo memory; unused here
2822 resource = pci_resource_start (pdev, 0);
2823 len = pci_resource_len (pdev, 0);
2824 if (!request_mem_region (resource, len, driver_name)) {
2825 DEBUG (dev, "controller already in use\n");
2826 retval = -EBUSY;
2827 goto done;
2829 dev->region = 1;
2831 base = ioremap_nocache (resource, len);
2832 if (base == NULL) {
2833 DEBUG (dev, "can't map memory\n");
2834 retval = -EFAULT;
2835 goto done;
2837 dev->regs = (struct net2280_regs __iomem *) base;
2838 dev->usb = (struct net2280_usb_regs __iomem *) (base + 0x0080);
2839 dev->pci = (struct net2280_pci_regs __iomem *) (base + 0x0100);
2840 dev->dma = (struct net2280_dma_regs __iomem *) (base + 0x0180);
2841 dev->dep = (struct net2280_dep_regs __iomem *) (base + 0x0200);
2842 dev->epregs = (struct net2280_ep_regs __iomem *) (base + 0x0300);
2844 /* put into initial config, link up all endpoints */
2845 writel (0, &dev->usb->usbctl);
2846 usb_reset (dev);
2847 usb_reinit (dev);
2849 /* irq setup after old hardware is cleaned up */
2850 if (!pdev->irq) {
2851 ERROR (dev, "No IRQ. Check PCI setup!\n");
2852 retval = -ENODEV;
2853 goto done;
2855 #ifndef __sparc__
2856 scnprintf (buf, sizeof buf, "%d", pdev->irq);
2857 bufp = buf;
2858 #else
2859 bufp = __irq_itoa(pdev->irq);
2860 #endif
2861 if (request_irq (pdev->irq, net2280_irq, SA_SHIRQ, driver_name, dev)
2862 != 0) {
2863 ERROR (dev, "request interrupt %s failed\n", bufp);
2864 retval = -EBUSY;
2865 goto done;
2867 dev->got_irq = 1;
2869 /* DMA setup */
2870 /* NOTE: we know only the 32 LSBs of dma addresses may be nonzero */
2871 dev->requests = pci_pool_create ("requests", pdev,
2872 sizeof (struct net2280_dma),
2873 0 /* no alignment requirements */,
2874 0 /* or page-crossing issues */);
2875 if (!dev->requests) {
2876 DEBUG (dev, "can't get request pool\n");
2877 retval = -ENOMEM;
2878 goto done;
2880 for (i = 1; i < 5; i++) {
2881 struct net2280_dma *td;
2883 td = pci_pool_alloc (dev->requests, GFP_KERNEL,
2884 &dev->ep [i].td_dma);
2885 if (!td) {
2886 DEBUG (dev, "can't get dummy %d\n", i);
2887 retval = -ENOMEM;
2888 goto done;
2890 td->dmacount = 0; /* not VALID */
2891 td->dmaaddr = __constant_cpu_to_le32 (DMA_ADDR_INVALID);
2892 td->dmadesc = td->dmaaddr;
2893 dev->ep [i].dummy = td;
2896 /* enable lower-overhead pci memory bursts during DMA */
2897 writel ( (1 << DMA_MEMORY_WRITE_AND_INVALIDATE_ENABLE)
2898 // 256 write retries may not be enough...
2899 // | (1 << PCI_RETRY_ABORT_ENABLE)
2900 | (1 << DMA_READ_MULTIPLE_ENABLE)
2901 | (1 << DMA_READ_LINE_ENABLE)
2902 , &dev->pci->pcimstctl);
2903 /* erratum 0115 shouldn't appear: Linux inits PCI_LATENCY_TIMER */
2904 pci_set_master (pdev);
2905 pci_set_mwi (pdev);
2907 /* ... also flushes any posted pci writes */
2908 dev->chiprev = get_idx_reg (dev->regs, REG_CHIPREV) & 0xffff;
2910 /* done */
2911 pci_set_drvdata (pdev, dev);
2912 INFO (dev, "%s\n", driver_desc);
2913 INFO (dev, "irq %s, pci mem %p, chip rev %04x\n",
2914 bufp, base, dev->chiprev);
2915 INFO (dev, "version: " DRIVER_VERSION "; dma %s\n",
2916 use_dma
2917 ? (use_dma_chaining ? "chaining" : "enabled")
2918 : "disabled");
2919 the_controller = dev;
2921 device_register (&dev->gadget.dev);
2922 device_create_file (&pdev->dev, &dev_attr_registers);
2924 return 0;
2926 done:
2927 if (dev)
2928 net2280_remove (pdev);
2929 return retval;
2933 /*-------------------------------------------------------------------------*/
2935 static struct pci_device_id pci_ids [] = { {
2936 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
2937 .class_mask = ~0,
2938 .vendor = 0x17cc,
2939 .device = 0x2280,
2940 .subvendor = PCI_ANY_ID,
2941 .subdevice = PCI_ANY_ID,
2943 }, { /* end: all zeroes */ }
2945 MODULE_DEVICE_TABLE (pci, pci_ids);
2947 /* pci driver glue; this is a "new style" PCI driver module */
2948 static struct pci_driver net2280_pci_driver = {
2949 .name = (char *) driver_name,
2950 .id_table = pci_ids,
2952 .probe = net2280_probe,
2953 .remove = net2280_remove,
2955 /* FIXME add power management support */
2958 MODULE_DESCRIPTION (DRIVER_DESC);
2959 MODULE_AUTHOR ("David Brownell");
2960 MODULE_LICENSE ("GPL");
2962 static int __init init (void)
2964 if (!use_dma)
2965 use_dma_chaining = 0;
2966 return pci_register_driver (&net2280_pci_driver);
2968 module_init (init);
2970 static void __exit cleanup (void)
2972 pci_unregister_driver (&net2280_pci_driver);
2974 module_exit (cleanup);