[PATCH] irq-flags: usb: Use the new IRQF_ constants
[linux-2.6.22.y-op.git] / drivers / usb / gadget / net2280.c
blob09243239d948cf4c034515c89838b41b769b7817
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 * Modified Seth Levy 2005 PLX Technology, Inc. to provide compatibility with 2282 chip
31 * This program is free software; you can redistribute it and/or modify
32 * it under the terms of the GNU General Public License as published by
33 * the Free Software Foundation; either version 2 of the License, or
34 * (at your option) any later version.
36 * This program is distributed in the hope that it will be useful,
37 * but WITHOUT ANY WARRANTY; without even the implied warranty of
38 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
39 * GNU General Public License for more details.
41 * You should have received a copy of the GNU General Public License
42 * along with this program; if not, write to the Free Software
43 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
46 #undef DEBUG /* messages on error and most fault paths */
47 #undef VERBOSE /* extra debug messages (success too) */
49 #include <linux/module.h>
50 #include <linux/pci.h>
51 #include <linux/dma-mapping.h>
52 #include <linux/kernel.h>
53 #include <linux/delay.h>
54 #include <linux/ioport.h>
55 #include <linux/sched.h>
56 #include <linux/slab.h>
57 #include <linux/smp_lock.h>
58 #include <linux/errno.h>
59 #include <linux/init.h>
60 #include <linux/timer.h>
61 #include <linux/list.h>
62 #include <linux/interrupt.h>
63 #include <linux/moduleparam.h>
64 #include <linux/device.h>
65 #include <linux/usb_ch9.h>
66 #include <linux/usb_gadget.h>
68 #include <asm/byteorder.h>
69 #include <asm/io.h>
70 #include <asm/irq.h>
71 #include <asm/system.h>
72 #include <asm/unaligned.h>
75 #define DRIVER_DESC "PLX NET228x USB Peripheral Controller"
76 #define DRIVER_VERSION "2005 Sept 27"
78 #define DMA_ADDR_INVALID (~(dma_addr_t)0)
79 #define EP_DONTUSE 13 /* nonzero */
81 #define USE_RDK_LEDS /* GPIO pins control three LEDs */
84 static const char driver_name [] = "net2280";
85 static const char driver_desc [] = DRIVER_DESC;
87 static const char ep0name [] = "ep0";
88 static const char *ep_name [] = {
89 ep0name,
90 "ep-a", "ep-b", "ep-c", "ep-d",
91 "ep-e", "ep-f",
94 /* use_dma -- general goodness, fewer interrupts, less cpu load (vs PIO)
95 * use_dma_chaining -- dma descriptor queueing gives even more irq reduction
97 * The net2280 DMA engines are not tightly integrated with their FIFOs;
98 * not all cases are (yet) handled well in this driver or the silicon.
99 * Some gadget drivers work better with the dma support here than others.
100 * These two parameters let you use PIO or more aggressive DMA.
102 static int use_dma = 1;
103 static int use_dma_chaining = 0;
105 /* "modprobe net2280 use_dma=n" etc */
106 module_param (use_dma, bool, S_IRUGO);
107 module_param (use_dma_chaining, bool, S_IRUGO);
110 /* mode 0 == ep-{a,b,c,d} 1K fifo each
111 * mode 1 == ep-{a,b} 2K fifo each, ep-{c,d} unavailable
112 * mode 2 == ep-a 2K fifo, ep-{b,c} 1K each, ep-d unavailable
114 static ushort fifo_mode = 0;
116 /* "modprobe net2280 fifo_mode=1" etc */
117 module_param (fifo_mode, ushort, 0644);
119 /* enable_suspend -- When enabled, the driver will respond to
120 * USB suspend requests by powering down the NET2280. Otherwise,
121 * USB suspend requests will be ignored. This is acceptible for
122 * self-powered devices
124 static int enable_suspend = 0;
126 /* "modprobe net2280 enable_suspend=1" etc */
127 module_param (enable_suspend, bool, S_IRUGO);
130 #define DIR_STRING(bAddress) (((bAddress) & USB_DIR_IN) ? "in" : "out")
132 #if defined(CONFIG_USB_GADGET_DEBUG_FILES) || defined (DEBUG)
133 static char *type_string (u8 bmAttributes)
135 switch ((bmAttributes) & USB_ENDPOINT_XFERTYPE_MASK) {
136 case USB_ENDPOINT_XFER_BULK: return "bulk";
137 case USB_ENDPOINT_XFER_ISOC: return "iso";
138 case USB_ENDPOINT_XFER_INT: return "intr";
140 return "control";
142 #endif
144 #include "net2280.h"
146 #define valid_bit __constant_cpu_to_le32 (1 << VALID_BIT)
147 #define dma_done_ie __constant_cpu_to_le32 (1 << DMA_DONE_INTERRUPT_ENABLE)
149 /*-------------------------------------------------------------------------*/
151 static int
152 net2280_enable (struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
154 struct net2280 *dev;
155 struct net2280_ep *ep;
156 u32 max, tmp;
157 unsigned long flags;
159 ep = container_of (_ep, struct net2280_ep, ep);
160 if (!_ep || !desc || ep->desc || _ep->name == ep0name
161 || desc->bDescriptorType != USB_DT_ENDPOINT)
162 return -EINVAL;
163 dev = ep->dev;
164 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
165 return -ESHUTDOWN;
167 /* erratum 0119 workaround ties up an endpoint number */
168 if ((desc->bEndpointAddress & 0x0f) == EP_DONTUSE)
169 return -EDOM;
171 /* sanity check ep-e/ep-f since their fifos are small */
172 max = le16_to_cpu (desc->wMaxPacketSize) & 0x1fff;
173 if (ep->num > 4 && max > 64)
174 return -ERANGE;
176 spin_lock_irqsave (&dev->lock, flags);
177 _ep->maxpacket = max & 0x7ff;
178 ep->desc = desc;
180 /* ep_reset() has already been called */
181 ep->stopped = 0;
182 ep->out_overflow = 0;
184 /* set speed-dependent max packet; may kick in high bandwidth */
185 set_idx_reg (dev->regs, REG_EP_MAXPKT (dev, ep->num), max);
187 /* FIFO lines can't go to different packets. PIO is ok, so
188 * use it instead of troublesome (non-bulk) multi-packet DMA.
190 if (ep->dma && (max % 4) != 0 && use_dma_chaining) {
191 DEBUG (ep->dev, "%s, no dma for maxpacket %d\n",
192 ep->ep.name, ep->ep.maxpacket);
193 ep->dma = NULL;
196 /* set type, direction, address; reset fifo counters */
197 writel ((1 << FIFO_FLUSH), &ep->regs->ep_stat);
198 tmp = (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK);
199 if (tmp == USB_ENDPOINT_XFER_INT) {
200 /* erratum 0105 workaround prevents hs NYET */
201 if (dev->chiprev == 0100
202 && dev->gadget.speed == USB_SPEED_HIGH
203 && !(desc->bEndpointAddress & USB_DIR_IN))
204 writel ((1 << CLEAR_NAK_OUT_PACKETS_MODE),
205 &ep->regs->ep_rsp);
206 } else if (tmp == USB_ENDPOINT_XFER_BULK) {
207 /* catch some particularly blatant driver bugs */
208 if ((dev->gadget.speed == USB_SPEED_HIGH
209 && max != 512)
210 || (dev->gadget.speed == USB_SPEED_FULL
211 && max > 64)) {
212 spin_unlock_irqrestore (&dev->lock, flags);
213 return -ERANGE;
216 ep->is_iso = (tmp == USB_ENDPOINT_XFER_ISOC) ? 1 : 0;
217 tmp <<= ENDPOINT_TYPE;
218 tmp |= desc->bEndpointAddress;
219 tmp |= (4 << ENDPOINT_BYTE_COUNT); /* default full fifo lines */
220 tmp |= 1 << ENDPOINT_ENABLE;
221 wmb ();
223 /* for OUT transfers, block the rx fifo until a read is posted */
224 ep->is_in = (tmp & USB_DIR_IN) != 0;
225 if (!ep->is_in)
226 writel ((1 << SET_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
227 else if (dev->pdev->device != 0x2280) {
228 /* Added for 2282, Don't use nak packets on an in endpoint, this was ignored on 2280 */
229 writel ((1 << CLEAR_NAK_OUT_PACKETS)
230 | (1 << CLEAR_NAK_OUT_PACKETS_MODE), &ep->regs->ep_rsp);
233 writel (tmp, &ep->regs->ep_cfg);
235 /* enable irqs */
236 if (!ep->dma) { /* pio, per-packet */
237 tmp = (1 << ep->num) | readl (&dev->regs->pciirqenb0);
238 writel (tmp, &dev->regs->pciirqenb0);
240 tmp = (1 << DATA_PACKET_RECEIVED_INTERRUPT_ENABLE)
241 | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT_ENABLE);
242 if (dev->pdev->device == 0x2280)
243 tmp |= readl (&ep->regs->ep_irqenb);
244 writel (tmp, &ep->regs->ep_irqenb);
245 } else { /* dma, per-request */
246 tmp = (1 << (8 + ep->num)); /* completion */
247 tmp |= readl (&dev->regs->pciirqenb1);
248 writel (tmp, &dev->regs->pciirqenb1);
250 /* for short OUT transfers, dma completions can't
251 * advance the queue; do it pio-style, by hand.
252 * NOTE erratum 0112 workaround #2
254 if ((desc->bEndpointAddress & USB_DIR_IN) == 0) {
255 tmp = (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT_ENABLE);
256 writel (tmp, &ep->regs->ep_irqenb);
258 tmp = (1 << ep->num) | readl (&dev->regs->pciirqenb0);
259 writel (tmp, &dev->regs->pciirqenb0);
263 tmp = desc->bEndpointAddress;
264 DEBUG (dev, "enabled %s (ep%d%s-%s) %s max %04x\n",
265 _ep->name, tmp & 0x0f, DIR_STRING (tmp),
266 type_string (desc->bmAttributes),
267 ep->dma ? "dma" : "pio", max);
269 /* pci writes may still be posted */
270 spin_unlock_irqrestore (&dev->lock, flags);
271 return 0;
274 static int handshake (u32 __iomem *ptr, u32 mask, u32 done, int usec)
276 u32 result;
278 do {
279 result = readl (ptr);
280 if (result == ~(u32)0) /* "device unplugged" */
281 return -ENODEV;
282 result &= mask;
283 if (result == done)
284 return 0;
285 udelay (1);
286 usec--;
287 } while (usec > 0);
288 return -ETIMEDOUT;
291 static struct usb_ep_ops net2280_ep_ops;
293 static void ep_reset (struct net2280_regs __iomem *regs, struct net2280_ep *ep)
295 u32 tmp;
297 ep->desc = NULL;
298 INIT_LIST_HEAD (&ep->queue);
300 ep->ep.maxpacket = ~0;
301 ep->ep.ops = &net2280_ep_ops;
303 /* disable the dma, irqs, endpoint... */
304 if (ep->dma) {
305 writel (0, &ep->dma->dmactl);
306 writel ( (1 << DMA_SCATTER_GATHER_DONE_INTERRUPT)
307 | (1 << DMA_TRANSACTION_DONE_INTERRUPT)
308 | (1 << DMA_ABORT)
309 , &ep->dma->dmastat);
311 tmp = readl (&regs->pciirqenb0);
312 tmp &= ~(1 << ep->num);
313 writel (tmp, &regs->pciirqenb0);
314 } else {
315 tmp = readl (&regs->pciirqenb1);
316 tmp &= ~(1 << (8 + ep->num)); /* completion */
317 writel (tmp, &regs->pciirqenb1);
319 writel (0, &ep->regs->ep_irqenb);
321 /* init to our chosen defaults, notably so that we NAK OUT
322 * packets until the driver queues a read (+note erratum 0112)
324 if (!ep->is_in || ep->dev->pdev->device == 0x2280) {
325 tmp = (1 << SET_NAK_OUT_PACKETS_MODE)
326 | (1 << SET_NAK_OUT_PACKETS)
327 | (1 << CLEAR_EP_HIDE_STATUS_PHASE)
328 | (1 << CLEAR_INTERRUPT_MODE);
329 } else {
330 /* added for 2282 */
331 tmp = (1 << CLEAR_NAK_OUT_PACKETS_MODE)
332 | (1 << CLEAR_NAK_OUT_PACKETS)
333 | (1 << CLEAR_EP_HIDE_STATUS_PHASE)
334 | (1 << CLEAR_INTERRUPT_MODE);
337 if (ep->num != 0) {
338 tmp |= (1 << CLEAR_ENDPOINT_TOGGLE)
339 | (1 << CLEAR_ENDPOINT_HALT);
341 writel (tmp, &ep->regs->ep_rsp);
343 /* scrub most status bits, and flush any fifo state */
344 if (ep->dev->pdev->device == 0x2280)
345 tmp = (1 << FIFO_OVERFLOW)
346 | (1 << FIFO_UNDERFLOW);
347 else
348 tmp = 0;
350 writel (tmp | (1 << TIMEOUT)
351 | (1 << USB_STALL_SENT)
352 | (1 << USB_IN_NAK_SENT)
353 | (1 << USB_IN_ACK_RCVD)
354 | (1 << USB_OUT_PING_NAK_SENT)
355 | (1 << USB_OUT_ACK_SENT)
356 | (1 << FIFO_FLUSH)
357 | (1 << SHORT_PACKET_OUT_DONE_INTERRUPT)
358 | (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)
359 | (1 << DATA_PACKET_RECEIVED_INTERRUPT)
360 | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
361 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
362 | (1 << DATA_IN_TOKEN_INTERRUPT)
363 , &ep->regs->ep_stat);
365 /* fifo size is handled separately */
368 static void nuke (struct net2280_ep *);
370 static int net2280_disable (struct usb_ep *_ep)
372 struct net2280_ep *ep;
373 unsigned long flags;
375 ep = container_of (_ep, struct net2280_ep, ep);
376 if (!_ep || !ep->desc || _ep->name == ep0name)
377 return -EINVAL;
379 spin_lock_irqsave (&ep->dev->lock, flags);
380 nuke (ep);
381 ep_reset (ep->dev->regs, ep);
383 VDEBUG (ep->dev, "disabled %s %s\n",
384 ep->dma ? "dma" : "pio", _ep->name);
386 /* synch memory views with the device */
387 (void) readl (&ep->regs->ep_cfg);
389 if (use_dma && !ep->dma && ep->num >= 1 && ep->num <= 4)
390 ep->dma = &ep->dev->dma [ep->num - 1];
392 spin_unlock_irqrestore (&ep->dev->lock, flags);
393 return 0;
396 /*-------------------------------------------------------------------------*/
398 static struct usb_request *
399 net2280_alloc_request (struct usb_ep *_ep, gfp_t gfp_flags)
401 struct net2280_ep *ep;
402 struct net2280_request *req;
404 if (!_ep)
405 return NULL;
406 ep = container_of (_ep, struct net2280_ep, ep);
408 req = kzalloc(sizeof(*req), gfp_flags);
409 if (!req)
410 return NULL;
412 req->req.dma = DMA_ADDR_INVALID;
413 INIT_LIST_HEAD (&req->queue);
415 /* this dma descriptor may be swapped with the previous dummy */
416 if (ep->dma) {
417 struct net2280_dma *td;
419 td = pci_pool_alloc (ep->dev->requests, gfp_flags,
420 &req->td_dma);
421 if (!td) {
422 kfree (req);
423 return NULL;
425 td->dmacount = 0; /* not VALID */
426 td->dmaaddr = __constant_cpu_to_le32 (DMA_ADDR_INVALID);
427 td->dmadesc = td->dmaaddr;
428 req->td = td;
430 return &req->req;
433 static void
434 net2280_free_request (struct usb_ep *_ep, struct usb_request *_req)
436 struct net2280_ep *ep;
437 struct net2280_request *req;
439 ep = container_of (_ep, struct net2280_ep, ep);
440 if (!_ep || !_req)
441 return;
443 req = container_of (_req, struct net2280_request, req);
444 WARN_ON (!list_empty (&req->queue));
445 if (req->td)
446 pci_pool_free (ep->dev->requests, req->td, req->td_dma);
447 kfree (req);
450 /*-------------------------------------------------------------------------*/
452 #undef USE_KMALLOC
454 /* many common platforms have dma-coherent caches, which means that it's
455 * safe to use kmalloc() memory for all i/o buffers without using any
456 * cache flushing calls. (unless you're trying to share cache lines
457 * between dma and non-dma activities, which is a slow idea in any case.)
459 * other platforms need more care, with 2.5 having a moderately general
460 * solution (which falls down for allocations smaller than one page)
461 * that improves significantly on the 2.4 PCI allocators by removing
462 * the restriction that memory never be freed in_interrupt().
464 #if defined(CONFIG_X86)
465 #define USE_KMALLOC
467 #elif defined(CONFIG_PPC) && !defined(CONFIG_NOT_COHERENT_CACHE)
468 #define USE_KMALLOC
470 #elif defined(CONFIG_MIPS) && !defined(CONFIG_DMA_NONCOHERENT)
471 #define USE_KMALLOC
473 /* FIXME there are other cases, including an x86-64 one ... */
474 #endif
476 /* allocating buffers this way eliminates dma mapping overhead, which
477 * on some platforms will mean eliminating a per-io buffer copy. with
478 * some kinds of system caches, further tweaks may still be needed.
480 static void *
481 net2280_alloc_buffer (
482 struct usb_ep *_ep,
483 unsigned bytes,
484 dma_addr_t *dma,
485 gfp_t gfp_flags
488 void *retval;
489 struct net2280_ep *ep;
491 ep = container_of (_ep, struct net2280_ep, ep);
492 if (!_ep)
493 return NULL;
494 *dma = DMA_ADDR_INVALID;
496 #if defined(USE_KMALLOC)
497 retval = kmalloc(bytes, gfp_flags);
498 if (retval)
499 *dma = virt_to_phys(retval);
500 #else
501 if (ep->dma) {
502 /* the main problem with this call is that it wastes memory
503 * on typical 1/N page allocations: it allocates 1-N pages.
505 #warning Using dma_alloc_coherent even with buffers smaller than a page.
506 retval = dma_alloc_coherent(&ep->dev->pdev->dev,
507 bytes, dma, gfp_flags);
508 } else
509 retval = kmalloc(bytes, gfp_flags);
510 #endif
511 return retval;
514 static void
515 net2280_free_buffer (
516 struct usb_ep *_ep,
517 void *buf,
518 dma_addr_t dma,
519 unsigned bytes
521 /* free memory into the right allocator */
522 #ifndef USE_KMALLOC
523 if (dma != DMA_ADDR_INVALID) {
524 struct net2280_ep *ep;
526 ep = container_of(_ep, struct net2280_ep, ep);
527 if (!_ep)
528 return;
529 dma_free_coherent(&ep->dev->pdev->dev, bytes, buf, dma);
530 } else
531 #endif
532 kfree (buf);
535 /*-------------------------------------------------------------------------*/
537 /* load a packet into the fifo we use for usb IN transfers.
538 * works for all endpoints.
540 * NOTE: pio with ep-a..ep-d could stuff multiple packets into the fifo
541 * at a time, but this code is simpler because it knows it only writes
542 * one packet. ep-a..ep-d should use dma instead.
544 static void
545 write_fifo (struct net2280_ep *ep, struct usb_request *req)
547 struct net2280_ep_regs __iomem *regs = ep->regs;
548 u8 *buf;
549 u32 tmp;
550 unsigned count, total;
552 /* INVARIANT: fifo is currently empty. (testable) */
554 if (req) {
555 buf = req->buf + req->actual;
556 prefetch (buf);
557 total = req->length - req->actual;
558 } else {
559 total = 0;
560 buf = NULL;
563 /* write just one packet at a time */
564 count = ep->ep.maxpacket;
565 if (count > total) /* min() cannot be used on a bitfield */
566 count = total;
568 VDEBUG (ep->dev, "write %s fifo (IN) %d bytes%s req %p\n",
569 ep->ep.name, count,
570 (count != ep->ep.maxpacket) ? " (short)" : "",
571 req);
572 while (count >= 4) {
573 /* NOTE be careful if you try to align these. fifo lines
574 * should normally be full (4 bytes) and successive partial
575 * lines are ok only in certain cases.
577 tmp = get_unaligned ((u32 *)buf);
578 cpu_to_le32s (&tmp);
579 writel (tmp, &regs->ep_data);
580 buf += 4;
581 count -= 4;
584 /* last fifo entry is "short" unless we wrote a full packet.
585 * also explicitly validate last word in (periodic) transfers
586 * when maxpacket is not a multiple of 4 bytes.
588 if (count || total < ep->ep.maxpacket) {
589 tmp = count ? get_unaligned ((u32 *)buf) : count;
590 cpu_to_le32s (&tmp);
591 set_fifo_bytecount (ep, count & 0x03);
592 writel (tmp, &regs->ep_data);
595 /* pci writes may still be posted */
598 /* work around erratum 0106: PCI and USB race over the OUT fifo.
599 * caller guarantees chiprev 0100, out endpoint is NAKing, and
600 * there's no real data in the fifo.
602 * NOTE: also used in cases where that erratum doesn't apply:
603 * where the host wrote "too much" data to us.
605 static void out_flush (struct net2280_ep *ep)
607 u32 __iomem *statp;
608 u32 tmp;
610 ASSERT_OUT_NAKING (ep);
612 statp = &ep->regs->ep_stat;
613 writel ( (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
614 | (1 << DATA_PACKET_RECEIVED_INTERRUPT)
615 , statp);
616 writel ((1 << FIFO_FLUSH), statp);
617 mb ();
618 tmp = readl (statp);
619 if (tmp & (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
620 /* high speed did bulk NYET; fifo isn't filling */
621 && ep->dev->gadget.speed == USB_SPEED_FULL) {
622 unsigned usec;
624 usec = 50; /* 64 byte bulk/interrupt */
625 handshake (statp, (1 << USB_OUT_PING_NAK_SENT),
626 (1 << USB_OUT_PING_NAK_SENT), usec);
627 /* NAK done; now CLEAR_NAK_OUT_PACKETS is safe */
631 /* unload packet(s) from the fifo we use for usb OUT transfers.
632 * returns true iff the request completed, because of short packet
633 * or the request buffer having filled with full packets.
635 * for ep-a..ep-d this will read multiple packets out when they
636 * have been accepted.
638 static int
639 read_fifo (struct net2280_ep *ep, struct net2280_request *req)
641 struct net2280_ep_regs __iomem *regs = ep->regs;
642 u8 *buf = req->req.buf + req->req.actual;
643 unsigned count, tmp, is_short;
644 unsigned cleanup = 0, prevent = 0;
646 /* erratum 0106 ... packets coming in during fifo reads might
647 * be incompletely rejected. not all cases have workarounds.
649 if (ep->dev->chiprev == 0x0100
650 && ep->dev->gadget.speed == USB_SPEED_FULL) {
651 udelay (1);
652 tmp = readl (&ep->regs->ep_stat);
653 if ((tmp & (1 << NAK_OUT_PACKETS)))
654 cleanup = 1;
655 else if ((tmp & (1 << FIFO_FULL))) {
656 start_out_naking (ep);
657 prevent = 1;
659 /* else: hope we don't see the problem */
662 /* never overflow the rx buffer. the fifo reads packets until
663 * it sees a short one; we might not be ready for them all.
665 prefetchw (buf);
666 count = readl (&regs->ep_avail);
667 if (unlikely (count == 0)) {
668 udelay (1);
669 tmp = readl (&ep->regs->ep_stat);
670 count = readl (&regs->ep_avail);
671 /* handled that data already? */
672 if (count == 0 && (tmp & (1 << NAK_OUT_PACKETS)) == 0)
673 return 0;
676 tmp = req->req.length - req->req.actual;
677 if (count > tmp) {
678 /* as with DMA, data overflow gets flushed */
679 if ((tmp % ep->ep.maxpacket) != 0) {
680 ERROR (ep->dev,
681 "%s out fifo %d bytes, expected %d\n",
682 ep->ep.name, count, tmp);
683 req->req.status = -EOVERFLOW;
684 cleanup = 1;
685 /* NAK_OUT_PACKETS will be set, so flushing is safe;
686 * the next read will start with the next packet
688 } /* else it's a ZLP, no worries */
689 count = tmp;
691 req->req.actual += count;
693 is_short = (count == 0) || ((count % ep->ep.maxpacket) != 0);
695 VDEBUG (ep->dev, "read %s fifo (OUT) %d bytes%s%s%s req %p %d/%d\n",
696 ep->ep.name, count, is_short ? " (short)" : "",
697 cleanup ? " flush" : "", prevent ? " nak" : "",
698 req, req->req.actual, req->req.length);
700 while (count >= 4) {
701 tmp = readl (&regs->ep_data);
702 cpu_to_le32s (&tmp);
703 put_unaligned (tmp, (u32 *)buf);
704 buf += 4;
705 count -= 4;
707 if (count) {
708 tmp = readl (&regs->ep_data);
709 /* LE conversion is implicit here: */
710 do {
711 *buf++ = (u8) tmp;
712 tmp >>= 8;
713 } while (--count);
715 if (cleanup)
716 out_flush (ep);
717 if (prevent) {
718 writel ((1 << CLEAR_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
719 (void) readl (&ep->regs->ep_rsp);
722 return is_short || ((req->req.actual == req->req.length)
723 && !req->req.zero);
726 /* fill out dma descriptor to match a given request */
727 static void
728 fill_dma_desc (struct net2280_ep *ep, struct net2280_request *req, int valid)
730 struct net2280_dma *td = req->td;
731 u32 dmacount = req->req.length;
733 /* don't let DMA continue after a short OUT packet,
734 * so overruns can't affect the next transfer.
735 * in case of overruns on max-size packets, we can't
736 * stop the fifo from filling but we can flush it.
738 if (ep->is_in)
739 dmacount |= (1 << DMA_DIRECTION);
740 if ((!ep->is_in && (dmacount % ep->ep.maxpacket) != 0) || ep->dev->pdev->device != 0x2280)
741 dmacount |= (1 << END_OF_CHAIN);
743 req->valid = valid;
744 if (valid)
745 dmacount |= (1 << VALID_BIT);
746 if (likely(!req->req.no_interrupt || !use_dma_chaining))
747 dmacount |= (1 << DMA_DONE_INTERRUPT_ENABLE);
749 /* td->dmadesc = previously set by caller */
750 td->dmaaddr = cpu_to_le32 (req->req.dma);
752 /* 2280 may be polling VALID_BIT through ep->dma->dmadesc */
753 wmb ();
754 td->dmacount = cpu_to_le32p (&dmacount);
757 static const u32 dmactl_default =
758 (1 << DMA_SCATTER_GATHER_DONE_INTERRUPT)
759 | (1 << DMA_CLEAR_COUNT_ENABLE)
760 /* erratum 0116 workaround part 1 (use POLLING) */
761 | (POLL_100_USEC << DESCRIPTOR_POLLING_RATE)
762 | (1 << DMA_VALID_BIT_POLLING_ENABLE)
763 | (1 << DMA_VALID_BIT_ENABLE)
764 | (1 << DMA_SCATTER_GATHER_ENABLE)
765 /* erratum 0116 workaround part 2 (no AUTOSTART) */
766 | (1 << DMA_ENABLE);
768 static inline void spin_stop_dma (struct net2280_dma_regs __iomem *dma)
770 handshake (&dma->dmactl, (1 << DMA_ENABLE), 0, 50);
773 static inline void stop_dma (struct net2280_dma_regs __iomem *dma)
775 writel (readl (&dma->dmactl) & ~(1 << DMA_ENABLE), &dma->dmactl);
776 spin_stop_dma (dma);
779 static void start_queue (struct net2280_ep *ep, u32 dmactl, u32 td_dma)
781 struct net2280_dma_regs __iomem *dma = ep->dma;
782 unsigned int tmp = (1 << VALID_BIT) | (ep->is_in << DMA_DIRECTION);
784 if (ep->dev->pdev->device != 0x2280)
785 tmp |= (1 << END_OF_CHAIN);
787 writel (tmp, &dma->dmacount);
788 writel (readl (&dma->dmastat), &dma->dmastat);
790 writel (td_dma, &dma->dmadesc);
791 writel (dmactl, &dma->dmactl);
793 /* erratum 0116 workaround part 3: pci arbiter away from net2280 */
794 (void) readl (&ep->dev->pci->pcimstctl);
796 writel ((1 << DMA_START), &dma->dmastat);
798 if (!ep->is_in)
799 stop_out_naking (ep);
802 static void start_dma (struct net2280_ep *ep, struct net2280_request *req)
804 u32 tmp;
805 struct net2280_dma_regs __iomem *dma = ep->dma;
807 /* FIXME can't use DMA for ZLPs */
809 /* on this path we "know" there's no dma active (yet) */
810 WARN_ON (readl (&dma->dmactl) & (1 << DMA_ENABLE));
811 writel (0, &ep->dma->dmactl);
813 /* previous OUT packet might have been short */
814 if (!ep->is_in && ((tmp = readl (&ep->regs->ep_stat))
815 & (1 << NAK_OUT_PACKETS)) != 0) {
816 writel ((1 << SHORT_PACKET_TRANSFERRED_INTERRUPT),
817 &ep->regs->ep_stat);
819 tmp = readl (&ep->regs->ep_avail);
820 if (tmp) {
821 writel (readl (&dma->dmastat), &dma->dmastat);
823 /* transfer all/some fifo data */
824 writel (req->req.dma, &dma->dmaaddr);
825 tmp = min (tmp, req->req.length);
827 /* dma irq, faking scatterlist status */
828 req->td->dmacount = cpu_to_le32 (req->req.length - tmp);
829 writel ((1 << DMA_DONE_INTERRUPT_ENABLE)
830 | tmp, &dma->dmacount);
831 req->td->dmadesc = 0;
832 req->valid = 1;
834 writel ((1 << DMA_ENABLE), &dma->dmactl);
835 writel ((1 << DMA_START), &dma->dmastat);
836 return;
840 tmp = dmactl_default;
842 /* force packet boundaries between dma requests, but prevent the
843 * controller from automagically writing a last "short" packet
844 * (zero length) unless the driver explicitly said to do that.
846 if (ep->is_in) {
847 if (likely ((req->req.length % ep->ep.maxpacket) != 0
848 || req->req.zero)) {
849 tmp |= (1 << DMA_FIFO_VALIDATE);
850 ep->in_fifo_validate = 1;
851 } else
852 ep->in_fifo_validate = 0;
855 /* init req->td, pointing to the current dummy */
856 req->td->dmadesc = cpu_to_le32 (ep->td_dma);
857 fill_dma_desc (ep, req, 1);
859 if (!use_dma_chaining)
860 req->td->dmacount |= __constant_cpu_to_le32 (1 << END_OF_CHAIN);
862 start_queue (ep, tmp, req->td_dma);
865 static inline void
866 queue_dma (struct net2280_ep *ep, struct net2280_request *req, int valid)
868 struct net2280_dma *end;
869 dma_addr_t tmp;
871 /* swap new dummy for old, link; fill and maybe activate */
872 end = ep->dummy;
873 ep->dummy = req->td;
874 req->td = end;
876 tmp = ep->td_dma;
877 ep->td_dma = req->td_dma;
878 req->td_dma = tmp;
880 end->dmadesc = cpu_to_le32 (ep->td_dma);
882 fill_dma_desc (ep, req, valid);
885 static void
886 done (struct net2280_ep *ep, struct net2280_request *req, int status)
888 struct net2280 *dev;
889 unsigned stopped = ep->stopped;
891 list_del_init (&req->queue);
893 if (req->req.status == -EINPROGRESS)
894 req->req.status = status;
895 else
896 status = req->req.status;
898 dev = ep->dev;
899 if (req->mapped) {
900 pci_unmap_single (dev->pdev, req->req.dma, req->req.length,
901 ep->is_in ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
902 req->req.dma = DMA_ADDR_INVALID;
903 req->mapped = 0;
906 if (status && status != -ESHUTDOWN)
907 VDEBUG (dev, "complete %s req %p stat %d len %u/%u\n",
908 ep->ep.name, &req->req, status,
909 req->req.actual, req->req.length);
911 /* don't modify queue heads during completion callback */
912 ep->stopped = 1;
913 spin_unlock (&dev->lock);
914 req->req.complete (&ep->ep, &req->req);
915 spin_lock (&dev->lock);
916 ep->stopped = stopped;
919 /*-------------------------------------------------------------------------*/
921 static int
922 net2280_queue (struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
924 struct net2280_request *req;
925 struct net2280_ep *ep;
926 struct net2280 *dev;
927 unsigned long flags;
929 /* we always require a cpu-view buffer, so that we can
930 * always use pio (as fallback or whatever).
932 req = container_of (_req, struct net2280_request, req);
933 if (!_req || !_req->complete || !_req->buf
934 || !list_empty (&req->queue))
935 return -EINVAL;
936 if (_req->length > (~0 & DMA_BYTE_COUNT_MASK))
937 return -EDOM;
938 ep = container_of (_ep, struct net2280_ep, ep);
939 if (!_ep || (!ep->desc && ep->num != 0))
940 return -EINVAL;
941 dev = ep->dev;
942 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
943 return -ESHUTDOWN;
945 /* FIXME implement PIO fallback for ZLPs with DMA */
946 if (ep->dma && _req->length == 0)
947 return -EOPNOTSUPP;
949 /* set up dma mapping in case the caller didn't */
950 if (ep->dma && _req->dma == DMA_ADDR_INVALID) {
951 _req->dma = pci_map_single (dev->pdev, _req->buf, _req->length,
952 ep->is_in ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
953 req->mapped = 1;
956 #if 0
957 VDEBUG (dev, "%s queue req %p, len %d buf %p\n",
958 _ep->name, _req, _req->length, _req->buf);
959 #endif
961 spin_lock_irqsave (&dev->lock, flags);
963 _req->status = -EINPROGRESS;
964 _req->actual = 0;
966 /* kickstart this i/o queue? */
967 if (list_empty (&ep->queue) && !ep->stopped) {
968 /* use DMA if the endpoint supports it, else pio */
969 if (ep->dma)
970 start_dma (ep, req);
971 else {
972 /* maybe there's no control data, just status ack */
973 if (ep->num == 0 && _req->length == 0) {
974 allow_status (ep);
975 done (ep, req, 0);
976 VDEBUG (dev, "%s status ack\n", ep->ep.name);
977 goto done;
980 /* PIO ... stuff the fifo, or unblock it. */
981 if (ep->is_in)
982 write_fifo (ep, _req);
983 else if (list_empty (&ep->queue)) {
984 u32 s;
986 /* OUT FIFO might have packet(s) buffered */
987 s = readl (&ep->regs->ep_stat);
988 if ((s & (1 << FIFO_EMPTY)) == 0) {
989 /* note: _req->short_not_ok is
990 * ignored here since PIO _always_
991 * stops queue advance here, and
992 * _req->status doesn't change for
993 * short reads (only _req->actual)
995 if (read_fifo (ep, req)) {
996 done (ep, req, 0);
997 if (ep->num == 0)
998 allow_status (ep);
999 /* don't queue it */
1000 req = NULL;
1001 } else
1002 s = readl (&ep->regs->ep_stat);
1005 /* don't NAK, let the fifo fill */
1006 if (req && (s & (1 << NAK_OUT_PACKETS)))
1007 writel ((1 << CLEAR_NAK_OUT_PACKETS),
1008 &ep->regs->ep_rsp);
1012 } else if (ep->dma) {
1013 int valid = 1;
1015 if (ep->is_in) {
1016 int expect;
1018 /* preventing magic zlps is per-engine state, not
1019 * per-transfer; irq logic must recover hiccups.
1021 expect = likely (req->req.zero
1022 || (req->req.length % ep->ep.maxpacket) != 0);
1023 if (expect != ep->in_fifo_validate)
1024 valid = 0;
1026 queue_dma (ep, req, valid);
1028 } /* else the irq handler advances the queue. */
1030 if (req)
1031 list_add_tail (&req->queue, &ep->queue);
1032 done:
1033 spin_unlock_irqrestore (&dev->lock, flags);
1035 /* pci writes may still be posted */
1036 return 0;
1039 static inline void
1040 dma_done (
1041 struct net2280_ep *ep,
1042 struct net2280_request *req,
1043 u32 dmacount,
1044 int status
1047 req->req.actual = req->req.length - (DMA_BYTE_COUNT_MASK & dmacount);
1048 done (ep, req, status);
1051 static void restart_dma (struct net2280_ep *ep);
1053 static void scan_dma_completions (struct net2280_ep *ep)
1055 /* only look at descriptors that were "naturally" retired,
1056 * so fifo and list head state won't matter
1058 while (!list_empty (&ep->queue)) {
1059 struct net2280_request *req;
1060 u32 tmp;
1062 req = list_entry (ep->queue.next,
1063 struct net2280_request, queue);
1064 if (!req->valid)
1065 break;
1066 rmb ();
1067 tmp = le32_to_cpup (&req->td->dmacount);
1068 if ((tmp & (1 << VALID_BIT)) != 0)
1069 break;
1071 /* SHORT_PACKET_TRANSFERRED_INTERRUPT handles "usb-short"
1072 * cases where DMA must be aborted; this code handles
1073 * all non-abort DMA completions.
1075 if (unlikely (req->td->dmadesc == 0)) {
1076 /* paranoia */
1077 tmp = readl (&ep->dma->dmacount);
1078 if (tmp & DMA_BYTE_COUNT_MASK)
1079 break;
1080 /* single transfer mode */
1081 dma_done (ep, req, tmp, 0);
1082 break;
1083 } else if (!ep->is_in
1084 && (req->req.length % ep->ep.maxpacket) != 0) {
1085 tmp = readl (&ep->regs->ep_stat);
1087 /* AVOID TROUBLE HERE by not issuing short reads from
1088 * your gadget driver. That helps avoids errata 0121,
1089 * 0122, and 0124; not all cases trigger the warning.
1091 if ((tmp & (1 << NAK_OUT_PACKETS)) == 0) {
1092 WARN (ep->dev, "%s lost packet sync!\n",
1093 ep->ep.name);
1094 req->req.status = -EOVERFLOW;
1095 } else if ((tmp = readl (&ep->regs->ep_avail)) != 0) {
1096 /* fifo gets flushed later */
1097 ep->out_overflow = 1;
1098 DEBUG (ep->dev, "%s dma, discard %d len %d\n",
1099 ep->ep.name, tmp,
1100 req->req.length);
1101 req->req.status = -EOVERFLOW;
1104 dma_done (ep, req, tmp, 0);
1108 static void restart_dma (struct net2280_ep *ep)
1110 struct net2280_request *req;
1111 u32 dmactl = dmactl_default;
1113 if (ep->stopped)
1114 return;
1115 req = list_entry (ep->queue.next, struct net2280_request, queue);
1117 if (!use_dma_chaining) {
1118 start_dma (ep, req);
1119 return;
1122 /* the 2280 will be processing the queue unless queue hiccups after
1123 * the previous transfer:
1124 * IN: wanted automagic zlp, head doesn't (or vice versa)
1125 * DMA_FIFO_VALIDATE doesn't init from dma descriptors.
1126 * OUT: was "usb-short", we must restart.
1128 if (ep->is_in && !req->valid) {
1129 struct net2280_request *entry, *prev = NULL;
1130 int reqmode, done = 0;
1132 DEBUG (ep->dev, "%s dma hiccup td %p\n", ep->ep.name, req->td);
1133 ep->in_fifo_validate = likely (req->req.zero
1134 || (req->req.length % ep->ep.maxpacket) != 0);
1135 if (ep->in_fifo_validate)
1136 dmactl |= (1 << DMA_FIFO_VALIDATE);
1137 list_for_each_entry (entry, &ep->queue, queue) {
1138 __le32 dmacount;
1140 if (entry == req)
1141 continue;
1142 dmacount = entry->td->dmacount;
1143 if (!done) {
1144 reqmode = likely (entry->req.zero
1145 || (entry->req.length
1146 % ep->ep.maxpacket) != 0);
1147 if (reqmode == ep->in_fifo_validate) {
1148 entry->valid = 1;
1149 dmacount |= valid_bit;
1150 entry->td->dmacount = dmacount;
1151 prev = entry;
1152 continue;
1153 } else {
1154 /* force a hiccup */
1155 prev->td->dmacount |= dma_done_ie;
1156 done = 1;
1160 /* walk the rest of the queue so unlinks behave */
1161 entry->valid = 0;
1162 dmacount &= ~valid_bit;
1163 entry->td->dmacount = dmacount;
1164 prev = entry;
1168 writel (0, &ep->dma->dmactl);
1169 start_queue (ep, dmactl, req->td_dma);
1172 static void abort_dma (struct net2280_ep *ep)
1174 /* abort the current transfer */
1175 if (likely (!list_empty (&ep->queue))) {
1176 /* FIXME work around errata 0121, 0122, 0124 */
1177 writel ((1 << DMA_ABORT), &ep->dma->dmastat);
1178 spin_stop_dma (ep->dma);
1179 } else
1180 stop_dma (ep->dma);
1181 scan_dma_completions (ep);
1184 /* dequeue ALL requests */
1185 static void nuke (struct net2280_ep *ep)
1187 struct net2280_request *req;
1189 /* called with spinlock held */
1190 ep->stopped = 1;
1191 if (ep->dma)
1192 abort_dma (ep);
1193 while (!list_empty (&ep->queue)) {
1194 req = list_entry (ep->queue.next,
1195 struct net2280_request,
1196 queue);
1197 done (ep, req, -ESHUTDOWN);
1201 /* dequeue JUST ONE request */
1202 static int net2280_dequeue (struct usb_ep *_ep, struct usb_request *_req)
1204 struct net2280_ep *ep;
1205 struct net2280_request *req;
1206 unsigned long flags;
1207 u32 dmactl;
1208 int stopped;
1210 ep = container_of (_ep, struct net2280_ep, ep);
1211 if (!_ep || (!ep->desc && ep->num != 0) || !_req)
1212 return -EINVAL;
1214 spin_lock_irqsave (&ep->dev->lock, flags);
1215 stopped = ep->stopped;
1217 /* quiesce dma while we patch the queue */
1218 dmactl = 0;
1219 ep->stopped = 1;
1220 if (ep->dma) {
1221 dmactl = readl (&ep->dma->dmactl);
1222 /* WARNING erratum 0127 may kick in ... */
1223 stop_dma (ep->dma);
1224 scan_dma_completions (ep);
1227 /* make sure it's still queued on this endpoint */
1228 list_for_each_entry (req, &ep->queue, queue) {
1229 if (&req->req == _req)
1230 break;
1232 if (&req->req != _req) {
1233 spin_unlock_irqrestore (&ep->dev->lock, flags);
1234 return -EINVAL;
1237 /* queue head may be partially complete. */
1238 if (ep->queue.next == &req->queue) {
1239 if (ep->dma) {
1240 DEBUG (ep->dev, "unlink (%s) dma\n", _ep->name);
1241 _req->status = -ECONNRESET;
1242 abort_dma (ep);
1243 if (likely (ep->queue.next == &req->queue)) {
1244 // NOTE: misreports single-transfer mode
1245 req->td->dmacount = 0; /* invalidate */
1246 dma_done (ep, req,
1247 readl (&ep->dma->dmacount),
1248 -ECONNRESET);
1250 } else {
1251 DEBUG (ep->dev, "unlink (%s) pio\n", _ep->name);
1252 done (ep, req, -ECONNRESET);
1254 req = NULL;
1256 /* patch up hardware chaining data */
1257 } else if (ep->dma && use_dma_chaining) {
1258 if (req->queue.prev == ep->queue.next) {
1259 writel (le32_to_cpu (req->td->dmadesc),
1260 &ep->dma->dmadesc);
1261 if (req->td->dmacount & dma_done_ie)
1262 writel (readl (&ep->dma->dmacount)
1263 | le32_to_cpu(dma_done_ie),
1264 &ep->dma->dmacount);
1265 } else {
1266 struct net2280_request *prev;
1268 prev = list_entry (req->queue.prev,
1269 struct net2280_request, queue);
1270 prev->td->dmadesc = req->td->dmadesc;
1271 if (req->td->dmacount & dma_done_ie)
1272 prev->td->dmacount |= dma_done_ie;
1276 if (req)
1277 done (ep, req, -ECONNRESET);
1278 ep->stopped = stopped;
1280 if (ep->dma) {
1281 /* turn off dma on inactive queues */
1282 if (list_empty (&ep->queue))
1283 stop_dma (ep->dma);
1284 else if (!ep->stopped) {
1285 /* resume current request, or start new one */
1286 if (req)
1287 writel (dmactl, &ep->dma->dmactl);
1288 else
1289 start_dma (ep, list_entry (ep->queue.next,
1290 struct net2280_request, queue));
1294 spin_unlock_irqrestore (&ep->dev->lock, flags);
1295 return 0;
1298 /*-------------------------------------------------------------------------*/
1300 static int net2280_fifo_status (struct usb_ep *_ep);
1302 static int
1303 net2280_set_halt (struct usb_ep *_ep, int value)
1305 struct net2280_ep *ep;
1306 unsigned long flags;
1307 int retval = 0;
1309 ep = container_of (_ep, struct net2280_ep, ep);
1310 if (!_ep || (!ep->desc && ep->num != 0))
1311 return -EINVAL;
1312 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1313 return -ESHUTDOWN;
1314 if (ep->desc /* not ep0 */ && (ep->desc->bmAttributes & 0x03)
1315 == USB_ENDPOINT_XFER_ISOC)
1316 return -EINVAL;
1318 spin_lock_irqsave (&ep->dev->lock, flags);
1319 if (!list_empty (&ep->queue))
1320 retval = -EAGAIN;
1321 else if (ep->is_in && value && net2280_fifo_status (_ep) != 0)
1322 retval = -EAGAIN;
1323 else {
1324 VDEBUG (ep->dev, "%s %s halt\n", _ep->name,
1325 value ? "set" : "clear");
1326 /* set/clear, then synch memory views with the device */
1327 if (value) {
1328 if (ep->num == 0)
1329 ep->dev->protocol_stall = 1;
1330 else
1331 set_halt (ep);
1332 } else
1333 clear_halt (ep);
1334 (void) readl (&ep->regs->ep_rsp);
1336 spin_unlock_irqrestore (&ep->dev->lock, flags);
1338 return retval;
1341 static int
1342 net2280_fifo_status (struct usb_ep *_ep)
1344 struct net2280_ep *ep;
1345 u32 avail;
1347 ep = container_of (_ep, struct net2280_ep, ep);
1348 if (!_ep || (!ep->desc && ep->num != 0))
1349 return -ENODEV;
1350 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1351 return -ESHUTDOWN;
1353 avail = readl (&ep->regs->ep_avail) & ((1 << 12) - 1);
1354 if (avail > ep->fifo_size)
1355 return -EOVERFLOW;
1356 if (ep->is_in)
1357 avail = ep->fifo_size - avail;
1358 return avail;
1361 static void
1362 net2280_fifo_flush (struct usb_ep *_ep)
1364 struct net2280_ep *ep;
1366 ep = container_of (_ep, struct net2280_ep, ep);
1367 if (!_ep || (!ep->desc && ep->num != 0))
1368 return;
1369 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1370 return;
1372 writel ((1 << FIFO_FLUSH), &ep->regs->ep_stat);
1373 (void) readl (&ep->regs->ep_rsp);
1376 static struct usb_ep_ops net2280_ep_ops = {
1377 .enable = net2280_enable,
1378 .disable = net2280_disable,
1380 .alloc_request = net2280_alloc_request,
1381 .free_request = net2280_free_request,
1383 .alloc_buffer = net2280_alloc_buffer,
1384 .free_buffer = net2280_free_buffer,
1386 .queue = net2280_queue,
1387 .dequeue = net2280_dequeue,
1389 .set_halt = net2280_set_halt,
1390 .fifo_status = net2280_fifo_status,
1391 .fifo_flush = net2280_fifo_flush,
1394 /*-------------------------------------------------------------------------*/
1396 static int net2280_get_frame (struct usb_gadget *_gadget)
1398 struct net2280 *dev;
1399 unsigned long flags;
1400 u16 retval;
1402 if (!_gadget)
1403 return -ENODEV;
1404 dev = container_of (_gadget, struct net2280, gadget);
1405 spin_lock_irqsave (&dev->lock, flags);
1406 retval = get_idx_reg (dev->regs, REG_FRAME) & 0x03ff;
1407 spin_unlock_irqrestore (&dev->lock, flags);
1408 return retval;
1411 static int net2280_wakeup (struct usb_gadget *_gadget)
1413 struct net2280 *dev;
1414 u32 tmp;
1415 unsigned long flags;
1417 if (!_gadget)
1418 return 0;
1419 dev = container_of (_gadget, struct net2280, gadget);
1421 spin_lock_irqsave (&dev->lock, flags);
1422 tmp = readl (&dev->usb->usbctl);
1423 if (tmp & (1 << DEVICE_REMOTE_WAKEUP_ENABLE))
1424 writel (1 << GENERATE_RESUME, &dev->usb->usbstat);
1425 spin_unlock_irqrestore (&dev->lock, flags);
1427 /* pci writes may still be posted */
1428 return 0;
1431 static int net2280_set_selfpowered (struct usb_gadget *_gadget, int value)
1433 struct net2280 *dev;
1434 u32 tmp;
1435 unsigned long flags;
1437 if (!_gadget)
1438 return 0;
1439 dev = container_of (_gadget, struct net2280, gadget);
1441 spin_lock_irqsave (&dev->lock, flags);
1442 tmp = readl (&dev->usb->usbctl);
1443 if (value)
1444 tmp |= (1 << SELF_POWERED_STATUS);
1445 else
1446 tmp &= ~(1 << SELF_POWERED_STATUS);
1447 writel (tmp, &dev->usb->usbctl);
1448 spin_unlock_irqrestore (&dev->lock, flags);
1450 return 0;
1453 static int net2280_pullup(struct usb_gadget *_gadget, int is_on)
1455 struct net2280 *dev;
1456 u32 tmp;
1457 unsigned long flags;
1459 if (!_gadget)
1460 return -ENODEV;
1461 dev = container_of (_gadget, struct net2280, gadget);
1463 spin_lock_irqsave (&dev->lock, flags);
1464 tmp = readl (&dev->usb->usbctl);
1465 dev->softconnect = (is_on != 0);
1466 if (is_on)
1467 tmp |= (1 << USB_DETECT_ENABLE);
1468 else
1469 tmp &= ~(1 << USB_DETECT_ENABLE);
1470 writel (tmp, &dev->usb->usbctl);
1471 spin_unlock_irqrestore (&dev->lock, flags);
1473 return 0;
1476 static const struct usb_gadget_ops net2280_ops = {
1477 .get_frame = net2280_get_frame,
1478 .wakeup = net2280_wakeup,
1479 .set_selfpowered = net2280_set_selfpowered,
1480 .pullup = net2280_pullup,
1483 /*-------------------------------------------------------------------------*/
1485 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1487 /* FIXME move these into procfs, and use seq_file.
1488 * Sysfs _still_ doesn't behave for arbitrarily sized files,
1489 * and also doesn't help products using this with 2.4 kernels.
1492 /* "function" sysfs attribute */
1493 static ssize_t
1494 show_function (struct device *_dev, struct device_attribute *attr, char *buf)
1496 struct net2280 *dev = dev_get_drvdata (_dev);
1498 if (!dev->driver
1499 || !dev->driver->function
1500 || strlen (dev->driver->function) > PAGE_SIZE)
1501 return 0;
1502 return scnprintf (buf, PAGE_SIZE, "%s\n", dev->driver->function);
1504 static DEVICE_ATTR (function, S_IRUGO, show_function, NULL);
1506 static ssize_t
1507 show_registers (struct device *_dev, struct device_attribute *attr, char *buf)
1509 struct net2280 *dev;
1510 char *next;
1511 unsigned size, t;
1512 unsigned long flags;
1513 int i;
1514 u32 t1, t2;
1515 const char *s;
1517 dev = dev_get_drvdata (_dev);
1518 next = buf;
1519 size = PAGE_SIZE;
1520 spin_lock_irqsave (&dev->lock, flags);
1522 if (dev->driver)
1523 s = dev->driver->driver.name;
1524 else
1525 s = "(none)";
1527 /* Main Control Registers */
1528 t = scnprintf (next, size, "%s version " DRIVER_VERSION
1529 ", chiprev %04x, dma %s\n\n"
1530 "devinit %03x fifoctl %08x gadget '%s'\n"
1531 "pci irqenb0 %02x irqenb1 %08x "
1532 "irqstat0 %04x irqstat1 %08x\n",
1533 driver_name, dev->chiprev,
1534 use_dma
1535 ? (use_dma_chaining ? "chaining" : "enabled")
1536 : "disabled",
1537 readl (&dev->regs->devinit),
1538 readl (&dev->regs->fifoctl),
1540 readl (&dev->regs->pciirqenb0),
1541 readl (&dev->regs->pciirqenb1),
1542 readl (&dev->regs->irqstat0),
1543 readl (&dev->regs->irqstat1));
1544 size -= t;
1545 next += t;
1547 /* USB Control Registers */
1548 t1 = readl (&dev->usb->usbctl);
1549 t2 = readl (&dev->usb->usbstat);
1550 if (t1 & (1 << VBUS_PIN)) {
1551 if (t2 & (1 << HIGH_SPEED))
1552 s = "high speed";
1553 else if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1554 s = "powered";
1555 else
1556 s = "full speed";
1557 /* full speed bit (6) not working?? */
1558 } else
1559 s = "not attached";
1560 t = scnprintf (next, size,
1561 "stdrsp %08x usbctl %08x usbstat %08x "
1562 "addr 0x%02x (%s)\n",
1563 readl (&dev->usb->stdrsp), t1, t2,
1564 readl (&dev->usb->ouraddr), s);
1565 size -= t;
1566 next += t;
1568 /* PCI Master Control Registers */
1570 /* DMA Control Registers */
1572 /* Configurable EP Control Registers */
1573 for (i = 0; i < 7; i++) {
1574 struct net2280_ep *ep;
1576 ep = &dev->ep [i];
1577 if (i && !ep->desc)
1578 continue;
1580 t1 = readl (&ep->regs->ep_cfg);
1581 t2 = readl (&ep->regs->ep_rsp) & 0xff;
1582 t = scnprintf (next, size,
1583 "\n%s\tcfg %05x rsp (%02x) %s%s%s%s%s%s%s%s"
1584 "irqenb %02x\n",
1585 ep->ep.name, t1, t2,
1586 (t2 & (1 << CLEAR_NAK_OUT_PACKETS))
1587 ? "NAK " : "",
1588 (t2 & (1 << CLEAR_EP_HIDE_STATUS_PHASE))
1589 ? "hide " : "",
1590 (t2 & (1 << CLEAR_EP_FORCE_CRC_ERROR))
1591 ? "CRC " : "",
1592 (t2 & (1 << CLEAR_INTERRUPT_MODE))
1593 ? "interrupt " : "",
1594 (t2 & (1<<CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE))
1595 ? "status " : "",
1596 (t2 & (1 << CLEAR_NAK_OUT_PACKETS_MODE))
1597 ? "NAKmode " : "",
1598 (t2 & (1 << CLEAR_ENDPOINT_TOGGLE))
1599 ? "DATA1 " : "DATA0 ",
1600 (t2 & (1 << CLEAR_ENDPOINT_HALT))
1601 ? "HALT " : "",
1602 readl (&ep->regs->ep_irqenb));
1603 size -= t;
1604 next += t;
1606 t = scnprintf (next, size,
1607 "\tstat %08x avail %04x "
1608 "(ep%d%s-%s)%s\n",
1609 readl (&ep->regs->ep_stat),
1610 readl (&ep->regs->ep_avail),
1611 t1 & 0x0f, DIR_STRING (t1),
1612 type_string (t1 >> 8),
1613 ep->stopped ? "*" : "");
1614 size -= t;
1615 next += t;
1617 if (!ep->dma)
1618 continue;
1620 t = scnprintf (next, size,
1621 " dma\tctl %08x stat %08x count %08x\n"
1622 "\taddr %08x desc %08x\n",
1623 readl (&ep->dma->dmactl),
1624 readl (&ep->dma->dmastat),
1625 readl (&ep->dma->dmacount),
1626 readl (&ep->dma->dmaaddr),
1627 readl (&ep->dma->dmadesc));
1628 size -= t;
1629 next += t;
1633 /* Indexed Registers */
1634 // none yet
1636 /* Statistics */
1637 t = scnprintf (next, size, "\nirqs: ");
1638 size -= t;
1639 next += t;
1640 for (i = 0; i < 7; i++) {
1641 struct net2280_ep *ep;
1643 ep = &dev->ep [i];
1644 if (i && !ep->irqs)
1645 continue;
1646 t = scnprintf (next, size, " %s/%lu", ep->ep.name, ep->irqs);
1647 size -= t;
1648 next += t;
1651 t = scnprintf (next, size, "\n");
1652 size -= t;
1653 next += t;
1655 spin_unlock_irqrestore (&dev->lock, flags);
1657 return PAGE_SIZE - size;
1659 static DEVICE_ATTR (registers, S_IRUGO, show_registers, NULL);
1661 static ssize_t
1662 show_queues (struct device *_dev, struct device_attribute *attr, char *buf)
1664 struct net2280 *dev;
1665 char *next;
1666 unsigned size;
1667 unsigned long flags;
1668 int i;
1670 dev = dev_get_drvdata (_dev);
1671 next = buf;
1672 size = PAGE_SIZE;
1673 spin_lock_irqsave (&dev->lock, flags);
1675 for (i = 0; i < 7; i++) {
1676 struct net2280_ep *ep = &dev->ep [i];
1677 struct net2280_request *req;
1678 int t;
1680 if (i != 0) {
1681 const struct usb_endpoint_descriptor *d;
1683 d = ep->desc;
1684 if (!d)
1685 continue;
1686 t = d->bEndpointAddress;
1687 t = scnprintf (next, size,
1688 "\n%s (ep%d%s-%s) max %04x %s fifo %d\n",
1689 ep->ep.name, t & USB_ENDPOINT_NUMBER_MASK,
1690 (t & USB_DIR_IN) ? "in" : "out",
1691 ({ char *val;
1692 switch (d->bmAttributes & 0x03) {
1693 case USB_ENDPOINT_XFER_BULK:
1694 val = "bulk"; break;
1695 case USB_ENDPOINT_XFER_INT:
1696 val = "intr"; break;
1697 default:
1698 val = "iso"; break;
1699 }; val; }),
1700 le16_to_cpu (d->wMaxPacketSize) & 0x1fff,
1701 ep->dma ? "dma" : "pio", ep->fifo_size
1703 } else /* ep0 should only have one transfer queued */
1704 t = scnprintf (next, size, "ep0 max 64 pio %s\n",
1705 ep->is_in ? "in" : "out");
1706 if (t <= 0 || t > size)
1707 goto done;
1708 size -= t;
1709 next += t;
1711 if (list_empty (&ep->queue)) {
1712 t = scnprintf (next, size, "\t(nothing queued)\n");
1713 if (t <= 0 || t > size)
1714 goto done;
1715 size -= t;
1716 next += t;
1717 continue;
1719 list_for_each_entry (req, &ep->queue, queue) {
1720 if (ep->dma && req->td_dma == readl (&ep->dma->dmadesc))
1721 t = scnprintf (next, size,
1722 "\treq %p len %d/%d "
1723 "buf %p (dmacount %08x)\n",
1724 &req->req, req->req.actual,
1725 req->req.length, req->req.buf,
1726 readl (&ep->dma->dmacount));
1727 else
1728 t = scnprintf (next, size,
1729 "\treq %p len %d/%d buf %p\n",
1730 &req->req, req->req.actual,
1731 req->req.length, req->req.buf);
1732 if (t <= 0 || t > size)
1733 goto done;
1734 size -= t;
1735 next += t;
1737 if (ep->dma) {
1738 struct net2280_dma *td;
1740 td = req->td;
1741 t = scnprintf (next, size, "\t td %08x "
1742 " count %08x buf %08x desc %08x\n",
1743 (u32) req->td_dma,
1744 le32_to_cpu (td->dmacount),
1745 le32_to_cpu (td->dmaaddr),
1746 le32_to_cpu (td->dmadesc));
1747 if (t <= 0 || t > size)
1748 goto done;
1749 size -= t;
1750 next += t;
1755 done:
1756 spin_unlock_irqrestore (&dev->lock, flags);
1757 return PAGE_SIZE - size;
1759 static DEVICE_ATTR (queues, S_IRUGO, show_queues, NULL);
1762 #else
1764 #define device_create_file(a,b) do {} while (0)
1765 #define device_remove_file device_create_file
1767 #endif
1769 /*-------------------------------------------------------------------------*/
1771 /* another driver-specific mode might be a request type doing dma
1772 * to/from another device fifo instead of to/from memory.
1775 static void set_fifo_mode (struct net2280 *dev, int mode)
1777 /* keeping high bits preserves BAR2 */
1778 writel ((0xffff << PCI_BASE2_RANGE) | mode, &dev->regs->fifoctl);
1780 /* always ep-{a,b,e,f} ... maybe not ep-c or ep-d */
1781 INIT_LIST_HEAD (&dev->gadget.ep_list);
1782 list_add_tail (&dev->ep [1].ep.ep_list, &dev->gadget.ep_list);
1783 list_add_tail (&dev->ep [2].ep.ep_list, &dev->gadget.ep_list);
1784 switch (mode) {
1785 case 0:
1786 list_add_tail (&dev->ep [3].ep.ep_list, &dev->gadget.ep_list);
1787 list_add_tail (&dev->ep [4].ep.ep_list, &dev->gadget.ep_list);
1788 dev->ep [1].fifo_size = dev->ep [2].fifo_size = 1024;
1789 break;
1790 case 1:
1791 dev->ep [1].fifo_size = dev->ep [2].fifo_size = 2048;
1792 break;
1793 case 2:
1794 list_add_tail (&dev->ep [3].ep.ep_list, &dev->gadget.ep_list);
1795 dev->ep [1].fifo_size = 2048;
1796 dev->ep [2].fifo_size = 1024;
1797 break;
1799 /* fifo sizes for ep0, ep-c, ep-d, ep-e, and ep-f never change */
1800 list_add_tail (&dev->ep [5].ep.ep_list, &dev->gadget.ep_list);
1801 list_add_tail (&dev->ep [6].ep.ep_list, &dev->gadget.ep_list);
1804 /* just declare this in any driver that really need it */
1805 extern int net2280_set_fifo_mode (struct usb_gadget *gadget, int mode);
1808 * net2280_set_fifo_mode - change allocation of fifo buffers
1809 * @gadget: access to the net2280 device that will be updated
1810 * @mode: 0 for default, four 1kB buffers (ep-a through ep-d);
1811 * 1 for two 2kB buffers (ep-a and ep-b only);
1812 * 2 for one 2kB buffer (ep-a) and two 1kB ones (ep-b, ep-c).
1814 * returns zero on success, else negative errno. when this succeeds,
1815 * the contents of gadget->ep_list may have changed.
1817 * you may only call this function when endpoints a-d are all disabled.
1818 * use it whenever extra hardware buffering can help performance, such
1819 * as before enabling "high bandwidth" interrupt endpoints that use
1820 * maxpacket bigger than 512 (when double buffering would otherwise
1821 * be unavailable).
1823 int net2280_set_fifo_mode (struct usb_gadget *gadget, int mode)
1825 int i;
1826 struct net2280 *dev;
1827 int status = 0;
1828 unsigned long flags;
1830 if (!gadget)
1831 return -ENODEV;
1832 dev = container_of (gadget, struct net2280, gadget);
1834 spin_lock_irqsave (&dev->lock, flags);
1836 for (i = 1; i <= 4; i++)
1837 if (dev->ep [i].desc) {
1838 status = -EINVAL;
1839 break;
1841 if (mode < 0 || mode > 2)
1842 status = -EINVAL;
1843 if (status == 0)
1844 set_fifo_mode (dev, mode);
1845 spin_unlock_irqrestore (&dev->lock, flags);
1847 if (status == 0) {
1848 if (mode == 1)
1849 DEBUG (dev, "fifo: ep-a 2K, ep-b 2K\n");
1850 else if (mode == 2)
1851 DEBUG (dev, "fifo: ep-a 2K, ep-b 1K, ep-c 1K\n");
1852 /* else all are 1K */
1854 return status;
1856 EXPORT_SYMBOL (net2280_set_fifo_mode);
1858 /*-------------------------------------------------------------------------*/
1860 /* keeping it simple:
1861 * - one bus driver, initted first;
1862 * - one function driver, initted second
1864 * most of the work to support multiple net2280 controllers would
1865 * be to associate this gadget driver (yes?) with all of them, or
1866 * perhaps to bind specific drivers to specific devices.
1869 static struct net2280 *the_controller;
1871 static void usb_reset (struct net2280 *dev)
1873 u32 tmp;
1875 dev->gadget.speed = USB_SPEED_UNKNOWN;
1876 (void) readl (&dev->usb->usbctl);
1878 net2280_led_init (dev);
1880 /* disable automatic responses, and irqs */
1881 writel (0, &dev->usb->stdrsp);
1882 writel (0, &dev->regs->pciirqenb0);
1883 writel (0, &dev->regs->pciirqenb1);
1885 /* clear old dma and irq state */
1886 for (tmp = 0; tmp < 4; tmp++) {
1887 struct net2280_ep *ep = &dev->ep [tmp + 1];
1889 if (ep->dma)
1890 abort_dma (ep);
1892 writel (~0, &dev->regs->irqstat0),
1893 writel (~(1 << SUSPEND_REQUEST_INTERRUPT), &dev->regs->irqstat1),
1895 /* reset, and enable pci */
1896 tmp = readl (&dev->regs->devinit)
1897 | (1 << PCI_ENABLE)
1898 | (1 << FIFO_SOFT_RESET)
1899 | (1 << USB_SOFT_RESET)
1900 | (1 << M8051_RESET);
1901 writel (tmp, &dev->regs->devinit);
1903 /* standard fifo and endpoint allocations */
1904 set_fifo_mode (dev, (fifo_mode <= 2) ? fifo_mode : 0);
1907 static void usb_reinit (struct net2280 *dev)
1909 u32 tmp;
1910 int init_dma;
1912 /* use_dma changes are ignored till next device re-init */
1913 init_dma = use_dma;
1915 /* basic endpoint init */
1916 for (tmp = 0; tmp < 7; tmp++) {
1917 struct net2280_ep *ep = &dev->ep [tmp];
1919 ep->ep.name = ep_name [tmp];
1920 ep->dev = dev;
1921 ep->num = tmp;
1923 if (tmp > 0 && tmp <= 4) {
1924 ep->fifo_size = 1024;
1925 if (init_dma)
1926 ep->dma = &dev->dma [tmp - 1];
1927 } else
1928 ep->fifo_size = 64;
1929 ep->regs = &dev->epregs [tmp];
1930 ep_reset (dev->regs, ep);
1932 dev->ep [0].ep.maxpacket = 64;
1933 dev->ep [5].ep.maxpacket = 64;
1934 dev->ep [6].ep.maxpacket = 64;
1936 dev->gadget.ep0 = &dev->ep [0].ep;
1937 dev->ep [0].stopped = 0;
1938 INIT_LIST_HEAD (&dev->gadget.ep0->ep_list);
1940 /* we want to prevent lowlevel/insecure access from the USB host,
1941 * but erratum 0119 means this enable bit is ignored
1943 for (tmp = 0; tmp < 5; tmp++)
1944 writel (EP_DONTUSE, &dev->dep [tmp].dep_cfg);
1947 static void ep0_start (struct net2280 *dev)
1949 writel ( (1 << CLEAR_EP_HIDE_STATUS_PHASE)
1950 | (1 << CLEAR_NAK_OUT_PACKETS)
1951 | (1 << CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE)
1952 , &dev->epregs [0].ep_rsp);
1955 * hardware optionally handles a bunch of standard requests
1956 * that the API hides from drivers anyway. have it do so.
1957 * endpoint status/features are handled in software, to
1958 * help pass tests for some dubious behavior.
1960 writel ( (1 << SET_TEST_MODE)
1961 | (1 << SET_ADDRESS)
1962 | (1 << DEVICE_SET_CLEAR_DEVICE_REMOTE_WAKEUP)
1963 | (1 << GET_DEVICE_STATUS)
1964 | (1 << GET_INTERFACE_STATUS)
1965 , &dev->usb->stdrsp);
1966 writel ( (1 << USB_ROOT_PORT_WAKEUP_ENABLE)
1967 | (1 << SELF_POWERED_USB_DEVICE)
1968 | (1 << REMOTE_WAKEUP_SUPPORT)
1969 | (dev->softconnect << USB_DETECT_ENABLE)
1970 | (1 << SELF_POWERED_STATUS)
1971 , &dev->usb->usbctl);
1973 /* enable irqs so we can see ep0 and general operation */
1974 writel ( (1 << SETUP_PACKET_INTERRUPT_ENABLE)
1975 | (1 << ENDPOINT_0_INTERRUPT_ENABLE)
1976 , &dev->regs->pciirqenb0);
1977 writel ( (1 << PCI_INTERRUPT_ENABLE)
1978 | (1 << PCI_MASTER_ABORT_RECEIVED_INTERRUPT_ENABLE)
1979 | (1 << PCI_TARGET_ABORT_RECEIVED_INTERRUPT_ENABLE)
1980 | (1 << PCI_RETRY_ABORT_INTERRUPT_ENABLE)
1981 | (1 << VBUS_INTERRUPT_ENABLE)
1982 | (1 << ROOT_PORT_RESET_INTERRUPT_ENABLE)
1983 | (1 << SUSPEND_REQUEST_CHANGE_INTERRUPT_ENABLE)
1984 , &dev->regs->pciirqenb1);
1986 /* don't leave any writes posted */
1987 (void) readl (&dev->usb->usbctl);
1990 /* when a driver is successfully registered, it will receive
1991 * control requests including set_configuration(), which enables
1992 * non-control requests. then usb traffic follows until a
1993 * disconnect is reported. then a host may connect again, or
1994 * the driver might get unbound.
1996 int usb_gadget_register_driver (struct usb_gadget_driver *driver)
1998 struct net2280 *dev = the_controller;
1999 int retval;
2000 unsigned i;
2002 /* insist on high speed support from the driver, since
2003 * (dev->usb->xcvrdiag & FORCE_FULL_SPEED_MODE)
2004 * "must not be used in normal operation"
2006 if (!driver
2007 || driver->speed != USB_SPEED_HIGH
2008 || !driver->bind
2009 || !driver->unbind
2010 || !driver->setup)
2011 return -EINVAL;
2012 if (!dev)
2013 return -ENODEV;
2014 if (dev->driver)
2015 return -EBUSY;
2017 for (i = 0; i < 7; i++)
2018 dev->ep [i].irqs = 0;
2020 /* hook up the driver ... */
2021 dev->softconnect = 1;
2022 driver->driver.bus = NULL;
2023 dev->driver = driver;
2024 dev->gadget.dev.driver = &driver->driver;
2025 retval = driver->bind (&dev->gadget);
2026 if (retval) {
2027 DEBUG (dev, "bind to driver %s --> %d\n",
2028 driver->driver.name, retval);
2029 dev->driver = NULL;
2030 dev->gadget.dev.driver = NULL;
2031 return retval;
2034 device_create_file (&dev->pdev->dev, &dev_attr_function);
2035 device_create_file (&dev->pdev->dev, &dev_attr_queues);
2037 /* ... then enable host detection and ep0; and we're ready
2038 * for set_configuration as well as eventual disconnect.
2040 net2280_led_active (dev, 1);
2041 ep0_start (dev);
2043 DEBUG (dev, "%s ready, usbctl %08x stdrsp %08x\n",
2044 driver->driver.name,
2045 readl (&dev->usb->usbctl),
2046 readl (&dev->usb->stdrsp));
2048 /* pci writes may still be posted */
2049 return 0;
2051 EXPORT_SYMBOL (usb_gadget_register_driver);
2053 static void
2054 stop_activity (struct net2280 *dev, struct usb_gadget_driver *driver)
2056 int i;
2058 /* don't disconnect if it's not connected */
2059 if (dev->gadget.speed == USB_SPEED_UNKNOWN)
2060 driver = NULL;
2062 /* stop hardware; prevent new request submissions;
2063 * and kill any outstanding requests.
2065 usb_reset (dev);
2066 for (i = 0; i < 7; i++)
2067 nuke (&dev->ep [i]);
2069 /* report disconnect; the driver is already quiesced */
2070 if (driver) {
2071 spin_unlock (&dev->lock);
2072 driver->disconnect (&dev->gadget);
2073 spin_lock (&dev->lock);
2076 usb_reinit (dev);
2079 int usb_gadget_unregister_driver (struct usb_gadget_driver *driver)
2081 struct net2280 *dev = the_controller;
2082 unsigned long flags;
2084 if (!dev)
2085 return -ENODEV;
2086 if (!driver || driver != dev->driver)
2087 return -EINVAL;
2089 spin_lock_irqsave (&dev->lock, flags);
2090 stop_activity (dev, driver);
2091 spin_unlock_irqrestore (&dev->lock, flags);
2093 net2280_pullup (&dev->gadget, 0);
2095 driver->unbind (&dev->gadget);
2096 dev->gadget.dev.driver = NULL;
2097 dev->driver = NULL;
2099 net2280_led_active (dev, 0);
2100 device_remove_file (&dev->pdev->dev, &dev_attr_function);
2101 device_remove_file (&dev->pdev->dev, &dev_attr_queues);
2103 DEBUG (dev, "unregistered driver '%s'\n", driver->driver.name);
2104 return 0;
2106 EXPORT_SYMBOL (usb_gadget_unregister_driver);
2109 /*-------------------------------------------------------------------------*/
2111 /* handle ep0, ep-e, ep-f with 64 byte packets: packet per irq.
2112 * also works for dma-capable endpoints, in pio mode or just
2113 * to manually advance the queue after short OUT transfers.
2115 static void handle_ep_small (struct net2280_ep *ep)
2117 struct net2280_request *req;
2118 u32 t;
2119 /* 0 error, 1 mid-data, 2 done */
2120 int mode = 1;
2122 if (!list_empty (&ep->queue))
2123 req = list_entry (ep->queue.next,
2124 struct net2280_request, queue);
2125 else
2126 req = NULL;
2128 /* ack all, and handle what we care about */
2129 t = readl (&ep->regs->ep_stat);
2130 ep->irqs++;
2131 #if 0
2132 VDEBUG (ep->dev, "%s ack ep_stat %08x, req %p\n",
2133 ep->ep.name, t, req ? &req->req : 0);
2134 #endif
2135 if (!ep->is_in || ep->dev->pdev->device == 0x2280)
2136 writel (t & ~(1 << NAK_OUT_PACKETS), &ep->regs->ep_stat);
2137 else
2138 /* Added for 2282 */
2139 writel (t, &ep->regs->ep_stat);
2141 /* for ep0, monitor token irqs to catch data stage length errors
2142 * and to synchronize on status.
2144 * also, to defer reporting of protocol stalls ... here's where
2145 * data or status first appears, handling stalls here should never
2146 * cause trouble on the host side..
2148 * control requests could be slightly faster without token synch for
2149 * status, but status can jam up that way.
2151 if (unlikely (ep->num == 0)) {
2152 if (ep->is_in) {
2153 /* status; stop NAKing */
2154 if (t & (1 << DATA_OUT_PING_TOKEN_INTERRUPT)) {
2155 if (ep->dev->protocol_stall) {
2156 ep->stopped = 1;
2157 set_halt (ep);
2159 if (!req)
2160 allow_status (ep);
2161 mode = 2;
2162 /* reply to extra IN data tokens with a zlp */
2163 } else if (t & (1 << DATA_IN_TOKEN_INTERRUPT)) {
2164 if (ep->dev->protocol_stall) {
2165 ep->stopped = 1;
2166 set_halt (ep);
2167 mode = 2;
2168 } else if (!req && !ep->stopped)
2169 write_fifo (ep, NULL);
2171 } else {
2172 /* status; stop NAKing */
2173 if (t & (1 << DATA_IN_TOKEN_INTERRUPT)) {
2174 if (ep->dev->protocol_stall) {
2175 ep->stopped = 1;
2176 set_halt (ep);
2178 mode = 2;
2179 /* an extra OUT token is an error */
2180 } else if (((t & (1 << DATA_OUT_PING_TOKEN_INTERRUPT))
2181 && req
2182 && req->req.actual == req->req.length)
2183 || !req) {
2184 ep->dev->protocol_stall = 1;
2185 set_halt (ep);
2186 ep->stopped = 1;
2187 if (req)
2188 done (ep, req, -EOVERFLOW);
2189 req = NULL;
2194 if (unlikely (!req))
2195 return;
2197 /* manual DMA queue advance after short OUT */
2198 if (likely (ep->dma != 0)) {
2199 if (t & (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)) {
2200 u32 count;
2201 int stopped = ep->stopped;
2203 /* TRANSFERRED works around OUT_DONE erratum 0112.
2204 * we expect (N <= maxpacket) bytes; host wrote M.
2205 * iff (M < N) we won't ever see a DMA interrupt.
2207 ep->stopped = 1;
2208 for (count = 0; ; t = readl (&ep->regs->ep_stat)) {
2210 /* any preceding dma transfers must finish.
2211 * dma handles (M >= N), may empty the queue
2213 scan_dma_completions (ep);
2214 if (unlikely (list_empty (&ep->queue)
2215 || ep->out_overflow)) {
2216 req = NULL;
2217 break;
2219 req = list_entry (ep->queue.next,
2220 struct net2280_request, queue);
2222 /* here either (M < N), a "real" short rx;
2223 * or (M == N) and the queue didn't empty
2225 if (likely (t & (1 << FIFO_EMPTY))) {
2226 count = readl (&ep->dma->dmacount);
2227 count &= DMA_BYTE_COUNT_MASK;
2228 if (readl (&ep->dma->dmadesc)
2229 != req->td_dma)
2230 req = NULL;
2231 break;
2233 udelay(1);
2236 /* stop DMA, leave ep NAKing */
2237 writel ((1 << DMA_ABORT), &ep->dma->dmastat);
2238 spin_stop_dma (ep->dma);
2240 if (likely (req)) {
2241 req->td->dmacount = 0;
2242 t = readl (&ep->regs->ep_avail);
2243 dma_done (ep, req, count,
2244 (ep->out_overflow || t) ? -EOVERFLOW : 0);
2247 /* also flush to prevent erratum 0106 trouble */
2248 if (unlikely (ep->out_overflow
2249 || (ep->dev->chiprev == 0x0100
2250 && ep->dev->gadget.speed
2251 == USB_SPEED_FULL))) {
2252 out_flush (ep);
2253 ep->out_overflow = 0;
2256 /* (re)start dma if needed, stop NAKing */
2257 ep->stopped = stopped;
2258 if (!list_empty (&ep->queue))
2259 restart_dma (ep);
2260 } else
2261 DEBUG (ep->dev, "%s dma ep_stat %08x ??\n",
2262 ep->ep.name, t);
2263 return;
2265 /* data packet(s) received (in the fifo, OUT) */
2266 } else if (t & (1 << DATA_PACKET_RECEIVED_INTERRUPT)) {
2267 if (read_fifo (ep, req) && ep->num != 0)
2268 mode = 2;
2270 /* data packet(s) transmitted (IN) */
2271 } else if (t & (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)) {
2272 unsigned len;
2274 len = req->req.length - req->req.actual;
2275 if (len > ep->ep.maxpacket)
2276 len = ep->ep.maxpacket;
2277 req->req.actual += len;
2279 /* if we wrote it all, we're usually done */
2280 if (req->req.actual == req->req.length) {
2281 if (ep->num == 0) {
2282 /* send zlps until the status stage */
2283 } else if (!req->req.zero || len != ep->ep.maxpacket)
2284 mode = 2;
2287 /* there was nothing to do ... */
2288 } else if (mode == 1)
2289 return;
2291 /* done */
2292 if (mode == 2) {
2293 /* stream endpoints often resubmit/unlink in completion */
2294 done (ep, req, 0);
2296 /* maybe advance queue to next request */
2297 if (ep->num == 0) {
2298 /* NOTE: net2280 could let gadget driver start the
2299 * status stage later. since not all controllers let
2300 * them control that, the api doesn't (yet) allow it.
2302 if (!ep->stopped)
2303 allow_status (ep);
2304 req = NULL;
2305 } else {
2306 if (!list_empty (&ep->queue) && !ep->stopped)
2307 req = list_entry (ep->queue.next,
2308 struct net2280_request, queue);
2309 else
2310 req = NULL;
2311 if (req && !ep->is_in)
2312 stop_out_naking (ep);
2316 /* is there a buffer for the next packet?
2317 * for best streaming performance, make sure there is one.
2319 if (req && !ep->stopped) {
2321 /* load IN fifo with next packet (may be zlp) */
2322 if (t & (1 << DATA_PACKET_TRANSMITTED_INTERRUPT))
2323 write_fifo (ep, &req->req);
2327 static struct net2280_ep *
2328 get_ep_by_addr (struct net2280 *dev, u16 wIndex)
2330 struct net2280_ep *ep;
2332 if ((wIndex & USB_ENDPOINT_NUMBER_MASK) == 0)
2333 return &dev->ep [0];
2334 list_for_each_entry (ep, &dev->gadget.ep_list, ep.ep_list) {
2335 u8 bEndpointAddress;
2337 if (!ep->desc)
2338 continue;
2339 bEndpointAddress = ep->desc->bEndpointAddress;
2340 if ((wIndex ^ bEndpointAddress) & USB_DIR_IN)
2341 continue;
2342 if ((wIndex & 0x0f) == (bEndpointAddress & 0x0f))
2343 return ep;
2345 return NULL;
2348 static void handle_stat0_irqs (struct net2280 *dev, u32 stat)
2350 struct net2280_ep *ep;
2351 u32 num, scratch;
2353 /* most of these don't need individual acks */
2354 stat &= ~(1 << INTA_ASSERTED);
2355 if (!stat)
2356 return;
2357 // DEBUG (dev, "irqstat0 %04x\n", stat);
2359 /* starting a control request? */
2360 if (unlikely (stat & (1 << SETUP_PACKET_INTERRUPT))) {
2361 union {
2362 u32 raw [2];
2363 struct usb_ctrlrequest r;
2364 } u;
2365 int tmp;
2366 struct net2280_request *req;
2368 if (dev->gadget.speed == USB_SPEED_UNKNOWN) {
2369 if (readl (&dev->usb->usbstat) & (1 << HIGH_SPEED))
2370 dev->gadget.speed = USB_SPEED_HIGH;
2371 else
2372 dev->gadget.speed = USB_SPEED_FULL;
2373 net2280_led_speed (dev, dev->gadget.speed);
2374 DEBUG (dev, "%s speed\n",
2375 (dev->gadget.speed == USB_SPEED_HIGH)
2376 ? "high" : "full");
2379 ep = &dev->ep [0];
2380 ep->irqs++;
2382 /* make sure any leftover request state is cleared */
2383 stat &= ~(1 << ENDPOINT_0_INTERRUPT);
2384 while (!list_empty (&ep->queue)) {
2385 req = list_entry (ep->queue.next,
2386 struct net2280_request, queue);
2387 done (ep, req, (req->req.actual == req->req.length)
2388 ? 0 : -EPROTO);
2390 ep->stopped = 0;
2391 dev->protocol_stall = 0;
2393 if (ep->dev->pdev->device == 0x2280)
2394 tmp = (1 << FIFO_OVERFLOW)
2395 | (1 << FIFO_UNDERFLOW);
2396 else
2397 tmp = 0;
2399 writel (tmp | (1 << TIMEOUT)
2400 | (1 << USB_STALL_SENT)
2401 | (1 << USB_IN_NAK_SENT)
2402 | (1 << USB_IN_ACK_RCVD)
2403 | (1 << USB_OUT_PING_NAK_SENT)
2404 | (1 << USB_OUT_ACK_SENT)
2405 | (1 << SHORT_PACKET_OUT_DONE_INTERRUPT)
2406 | (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)
2407 | (1 << DATA_PACKET_RECEIVED_INTERRUPT)
2408 | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
2409 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
2410 | (1 << DATA_IN_TOKEN_INTERRUPT)
2411 , &ep->regs->ep_stat);
2412 u.raw [0] = readl (&dev->usb->setup0123);
2413 u.raw [1] = readl (&dev->usb->setup4567);
2415 cpu_to_le32s (&u.raw [0]);
2416 cpu_to_le32s (&u.raw [1]);
2418 tmp = 0;
2420 #define w_value le16_to_cpup (&u.r.wValue)
2421 #define w_index le16_to_cpup (&u.r.wIndex)
2422 #define w_length le16_to_cpup (&u.r.wLength)
2424 /* ack the irq */
2425 writel (1 << SETUP_PACKET_INTERRUPT, &dev->regs->irqstat0);
2426 stat ^= (1 << SETUP_PACKET_INTERRUPT);
2428 /* watch control traffic at the token level, and force
2429 * synchronization before letting the status stage happen.
2430 * FIXME ignore tokens we'll NAK, until driver responds.
2431 * that'll mean a lot less irqs for some drivers.
2433 ep->is_in = (u.r.bRequestType & USB_DIR_IN) != 0;
2434 if (ep->is_in) {
2435 scratch = (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
2436 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
2437 | (1 << DATA_IN_TOKEN_INTERRUPT);
2438 stop_out_naking (ep);
2439 } else
2440 scratch = (1 << DATA_PACKET_RECEIVED_INTERRUPT)
2441 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
2442 | (1 << DATA_IN_TOKEN_INTERRUPT);
2443 writel (scratch, &dev->epregs [0].ep_irqenb);
2445 /* we made the hardware handle most lowlevel requests;
2446 * everything else goes uplevel to the gadget code.
2448 switch (u.r.bRequest) {
2449 case USB_REQ_GET_STATUS: {
2450 struct net2280_ep *e;
2451 __le32 status;
2453 /* hw handles device and interface status */
2454 if (u.r.bRequestType != (USB_DIR_IN|USB_RECIP_ENDPOINT))
2455 goto delegate;
2456 if ((e = get_ep_by_addr (dev, w_index)) == 0
2457 || w_length > 2)
2458 goto do_stall;
2460 if (readl (&e->regs->ep_rsp)
2461 & (1 << SET_ENDPOINT_HALT))
2462 status = __constant_cpu_to_le32 (1);
2463 else
2464 status = __constant_cpu_to_le32 (0);
2466 /* don't bother with a request object! */
2467 writel (0, &dev->epregs [0].ep_irqenb);
2468 set_fifo_bytecount (ep, w_length);
2469 writel ((__force u32)status, &dev->epregs [0].ep_data);
2470 allow_status (ep);
2471 VDEBUG (dev, "%s stat %02x\n", ep->ep.name, status);
2472 goto next_endpoints;
2474 break;
2475 case USB_REQ_CLEAR_FEATURE: {
2476 struct net2280_ep *e;
2478 /* hw handles device features */
2479 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
2480 goto delegate;
2481 if (w_value != USB_ENDPOINT_HALT
2482 || w_length != 0)
2483 goto do_stall;
2484 if ((e = get_ep_by_addr (dev, w_index)) == 0)
2485 goto do_stall;
2486 clear_halt (e);
2487 allow_status (ep);
2488 VDEBUG (dev, "%s clear halt\n", ep->ep.name);
2489 goto next_endpoints;
2491 break;
2492 case USB_REQ_SET_FEATURE: {
2493 struct net2280_ep *e;
2495 /* hw handles device features */
2496 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
2497 goto delegate;
2498 if (w_value != USB_ENDPOINT_HALT
2499 || w_length != 0)
2500 goto do_stall;
2501 if ((e = get_ep_by_addr (dev, w_index)) == 0)
2502 goto do_stall;
2503 set_halt (e);
2504 allow_status (ep);
2505 VDEBUG (dev, "%s set halt\n", ep->ep.name);
2506 goto next_endpoints;
2508 break;
2509 default:
2510 delegate:
2511 VDEBUG (dev, "setup %02x.%02x v%04x i%04x l%04x"
2512 "ep_cfg %08x\n",
2513 u.r.bRequestType, u.r.bRequest,
2514 w_value, w_index, w_length,
2515 readl (&ep->regs->ep_cfg));
2516 spin_unlock (&dev->lock);
2517 tmp = dev->driver->setup (&dev->gadget, &u.r);
2518 spin_lock (&dev->lock);
2521 /* stall ep0 on error */
2522 if (tmp < 0) {
2523 do_stall:
2524 VDEBUG (dev, "req %02x.%02x protocol STALL; stat %d\n",
2525 u.r.bRequestType, u.r.bRequest, tmp);
2526 dev->protocol_stall = 1;
2529 /* some in/out token irq should follow; maybe stall then.
2530 * driver must queue a request (even zlp) or halt ep0
2531 * before the host times out.
2535 #undef w_value
2536 #undef w_index
2537 #undef w_length
2539 next_endpoints:
2540 /* endpoint data irq ? */
2541 scratch = stat & 0x7f;
2542 stat &= ~0x7f;
2543 for (num = 0; scratch; num++) {
2544 u32 t;
2546 /* do this endpoint's FIFO and queue need tending? */
2547 t = 1 << num;
2548 if ((scratch & t) == 0)
2549 continue;
2550 scratch ^= t;
2552 ep = &dev->ep [num];
2553 handle_ep_small (ep);
2556 if (stat)
2557 DEBUG (dev, "unhandled irqstat0 %08x\n", stat);
2560 #define DMA_INTERRUPTS ( \
2561 (1 << DMA_D_INTERRUPT) \
2562 | (1 << DMA_C_INTERRUPT) \
2563 | (1 << DMA_B_INTERRUPT) \
2564 | (1 << DMA_A_INTERRUPT))
2565 #define PCI_ERROR_INTERRUPTS ( \
2566 (1 << PCI_MASTER_ABORT_RECEIVED_INTERRUPT) \
2567 | (1 << PCI_TARGET_ABORT_RECEIVED_INTERRUPT) \
2568 | (1 << PCI_RETRY_ABORT_INTERRUPT))
2570 static void handle_stat1_irqs (struct net2280 *dev, u32 stat)
2572 struct net2280_ep *ep;
2573 u32 tmp, num, mask, scratch;
2575 /* after disconnect there's nothing else to do! */
2576 tmp = (1 << VBUS_INTERRUPT) | (1 << ROOT_PORT_RESET_INTERRUPT);
2577 mask = (1 << HIGH_SPEED) | (1 << FULL_SPEED);
2579 /* VBUS disconnect is indicated by VBUS_PIN and VBUS_INTERRUPT set.
2580 * Root Port Reset is indicated by ROOT_PORT_RESET_INTERRRUPT set and
2581 * both HIGH_SPEED and FULL_SPEED clear (as ROOT_PORT_RESET_INTERRUPT
2582 * only indicates a change in the reset state).
2584 if (stat & tmp) {
2585 writel (tmp, &dev->regs->irqstat1);
2586 if ((((stat & (1 << ROOT_PORT_RESET_INTERRUPT)) &&
2587 ((readl (&dev->usb->usbstat) & mask) == 0))
2588 || ((readl (&dev->usb->usbctl) & (1 << VBUS_PIN)) == 0)
2589 ) && ( dev->gadget.speed != USB_SPEED_UNKNOWN)) {
2590 DEBUG (dev, "disconnect %s\n",
2591 dev->driver->driver.name);
2592 stop_activity (dev, dev->driver);
2593 ep0_start (dev);
2594 return;
2596 stat &= ~tmp;
2598 /* vBUS can bounce ... one of many reasons to ignore the
2599 * notion of hotplug events on bus connect/disconnect!
2601 if (!stat)
2602 return;
2605 /* NOTE: chip stays in PCI D0 state for now, but it could
2606 * enter D1 to save more power
2608 tmp = (1 << SUSPEND_REQUEST_CHANGE_INTERRUPT);
2609 if (stat & tmp) {
2610 writel (tmp, &dev->regs->irqstat1);
2611 if (stat & (1 << SUSPEND_REQUEST_INTERRUPT)) {
2612 if (dev->driver->suspend)
2613 dev->driver->suspend (&dev->gadget);
2614 if (!enable_suspend)
2615 stat &= ~(1 << SUSPEND_REQUEST_INTERRUPT);
2616 } else {
2617 if (dev->driver->resume)
2618 dev->driver->resume (&dev->gadget);
2619 /* at high speed, note erratum 0133 */
2621 stat &= ~tmp;
2624 /* clear any other status/irqs */
2625 if (stat)
2626 writel (stat, &dev->regs->irqstat1);
2628 /* some status we can just ignore */
2629 if (dev->pdev->device == 0x2280)
2630 stat &= ~((1 << CONTROL_STATUS_INTERRUPT)
2631 | (1 << SUSPEND_REQUEST_INTERRUPT)
2632 | (1 << RESUME_INTERRUPT)
2633 | (1 << SOF_INTERRUPT));
2634 else
2635 stat &= ~((1 << CONTROL_STATUS_INTERRUPT)
2636 | (1 << RESUME_INTERRUPT)
2637 | (1 << SOF_DOWN_INTERRUPT)
2638 | (1 << SOF_INTERRUPT));
2640 if (!stat)
2641 return;
2642 // DEBUG (dev, "irqstat1 %08x\n", stat);
2644 /* DMA status, for ep-{a,b,c,d} */
2645 scratch = stat & DMA_INTERRUPTS;
2646 stat &= ~DMA_INTERRUPTS;
2647 scratch >>= 9;
2648 for (num = 0; scratch; num++) {
2649 struct net2280_dma_regs __iomem *dma;
2651 tmp = 1 << num;
2652 if ((tmp & scratch) == 0)
2653 continue;
2654 scratch ^= tmp;
2656 ep = &dev->ep [num + 1];
2657 dma = ep->dma;
2659 if (!dma)
2660 continue;
2662 /* clear ep's dma status */
2663 tmp = readl (&dma->dmastat);
2664 writel (tmp, &dma->dmastat);
2666 /* chaining should stop on abort, short OUT from fifo,
2667 * or (stat0 codepath) short OUT transfer.
2669 if (!use_dma_chaining) {
2670 if ((tmp & (1 << DMA_TRANSACTION_DONE_INTERRUPT))
2671 == 0) {
2672 DEBUG (ep->dev, "%s no xact done? %08x\n",
2673 ep->ep.name, tmp);
2674 continue;
2676 stop_dma (ep->dma);
2679 /* OUT transfers terminate when the data from the
2680 * host is in our memory. Process whatever's done.
2681 * On this path, we know transfer's last packet wasn't
2682 * less than req->length. NAK_OUT_PACKETS may be set,
2683 * or the FIFO may already be holding new packets.
2685 * IN transfers can linger in the FIFO for a very
2686 * long time ... we ignore that for now, accounting
2687 * precisely (like PIO does) needs per-packet irqs
2689 scan_dma_completions (ep);
2691 /* disable dma on inactive queues; else maybe restart */
2692 if (list_empty (&ep->queue)) {
2693 if (use_dma_chaining)
2694 stop_dma (ep->dma);
2695 } else {
2696 tmp = readl (&dma->dmactl);
2697 if (!use_dma_chaining
2698 || (tmp & (1 << DMA_ENABLE)) == 0)
2699 restart_dma (ep);
2700 else if (ep->is_in && use_dma_chaining) {
2701 struct net2280_request *req;
2702 __le32 dmacount;
2704 /* the descriptor at the head of the chain
2705 * may still have VALID_BIT clear; that's
2706 * used to trigger changing DMA_FIFO_VALIDATE
2707 * (affects automagic zlp writes).
2709 req = list_entry (ep->queue.next,
2710 struct net2280_request, queue);
2711 dmacount = req->td->dmacount;
2712 dmacount &= __constant_cpu_to_le32 (
2713 (1 << VALID_BIT)
2714 | DMA_BYTE_COUNT_MASK);
2715 if (dmacount && (dmacount & valid_bit) == 0)
2716 restart_dma (ep);
2719 ep->irqs++;
2722 /* NOTE: there are other PCI errors we might usefully notice.
2723 * if they appear very often, here's where to try recovering.
2725 if (stat & PCI_ERROR_INTERRUPTS) {
2726 ERROR (dev, "pci dma error; stat %08x\n", stat);
2727 stat &= ~PCI_ERROR_INTERRUPTS;
2728 /* these are fatal errors, but "maybe" they won't
2729 * happen again ...
2731 stop_activity (dev, dev->driver);
2732 ep0_start (dev);
2733 stat = 0;
2736 if (stat)
2737 DEBUG (dev, "unhandled irqstat1 %08x\n", stat);
2740 static irqreturn_t net2280_irq (int irq, void *_dev, struct pt_regs * r)
2742 struct net2280 *dev = _dev;
2744 /* shared interrupt, not ours */
2745 if (!(readl(&dev->regs->irqstat0) & (1 << INTA_ASSERTED)))
2746 return IRQ_NONE;
2748 spin_lock (&dev->lock);
2750 /* handle disconnect, dma, and more */
2751 handle_stat1_irqs (dev, readl (&dev->regs->irqstat1));
2753 /* control requests and PIO */
2754 handle_stat0_irqs (dev, readl (&dev->regs->irqstat0));
2756 spin_unlock (&dev->lock);
2758 return IRQ_HANDLED;
2761 /*-------------------------------------------------------------------------*/
2763 static void gadget_release (struct device *_dev)
2765 struct net2280 *dev = dev_get_drvdata (_dev);
2767 kfree (dev);
2770 /* tear down the binding between this driver and the pci device */
2772 static void net2280_remove (struct pci_dev *pdev)
2774 struct net2280 *dev = pci_get_drvdata (pdev);
2776 /* start with the driver above us */
2777 if (dev->driver) {
2778 /* should have been done already by driver model core */
2779 WARN (dev, "pci remove, driver '%s' is still registered\n",
2780 dev->driver->driver.name);
2781 usb_gadget_unregister_driver (dev->driver);
2784 /* then clean up the resources we allocated during probe() */
2785 net2280_led_shutdown (dev);
2786 if (dev->requests) {
2787 int i;
2788 for (i = 1; i < 5; i++) {
2789 if (!dev->ep [i].dummy)
2790 continue;
2791 pci_pool_free (dev->requests, dev->ep [i].dummy,
2792 dev->ep [i].td_dma);
2794 pci_pool_destroy (dev->requests);
2796 if (dev->got_irq)
2797 free_irq (pdev->irq, dev);
2798 if (dev->regs)
2799 iounmap (dev->regs);
2800 if (dev->region)
2801 release_mem_region (pci_resource_start (pdev, 0),
2802 pci_resource_len (pdev, 0));
2803 if (dev->enabled)
2804 pci_disable_device (pdev);
2805 device_unregister (&dev->gadget.dev);
2806 device_remove_file (&pdev->dev, &dev_attr_registers);
2807 pci_set_drvdata (pdev, NULL);
2809 INFO (dev, "unbind\n");
2811 the_controller = NULL;
2814 /* wrap this driver around the specified device, but
2815 * don't respond over USB until a gadget driver binds to us.
2818 static int net2280_probe (struct pci_dev *pdev, const struct pci_device_id *id)
2820 struct net2280 *dev;
2821 unsigned long resource, len;
2822 void __iomem *base = NULL;
2823 int retval, i;
2825 /* if you want to support more than one controller in a system,
2826 * usb_gadget_driver_{register,unregister}() must change.
2828 if (the_controller) {
2829 dev_warn (&pdev->dev, "ignoring\n");
2830 return -EBUSY;
2833 /* alloc, and start init */
2834 dev = kzalloc (sizeof *dev, SLAB_KERNEL);
2835 if (dev == NULL){
2836 retval = -ENOMEM;
2837 goto done;
2840 pci_set_drvdata (pdev, dev);
2841 spin_lock_init (&dev->lock);
2842 dev->pdev = pdev;
2843 dev->gadget.ops = &net2280_ops;
2844 dev->gadget.is_dualspeed = 1;
2846 /* the "gadget" abstracts/virtualizes the controller */
2847 strcpy (dev->gadget.dev.bus_id, "gadget");
2848 dev->gadget.dev.parent = &pdev->dev;
2849 dev->gadget.dev.dma_mask = pdev->dev.dma_mask;
2850 dev->gadget.dev.release = gadget_release;
2851 dev->gadget.name = driver_name;
2853 /* now all the pci goodies ... */
2854 if (pci_enable_device (pdev) < 0) {
2855 retval = -ENODEV;
2856 goto done;
2858 dev->enabled = 1;
2860 /* BAR 0 holds all the registers
2861 * BAR 1 is 8051 memory; unused here (note erratum 0103)
2862 * BAR 2 is fifo memory; unused here
2864 resource = pci_resource_start (pdev, 0);
2865 len = pci_resource_len (pdev, 0);
2866 if (!request_mem_region (resource, len, driver_name)) {
2867 DEBUG (dev, "controller already in use\n");
2868 retval = -EBUSY;
2869 goto done;
2871 dev->region = 1;
2873 base = ioremap_nocache (resource, len);
2874 if (base == NULL) {
2875 DEBUG (dev, "can't map memory\n");
2876 retval = -EFAULT;
2877 goto done;
2879 dev->regs = (struct net2280_regs __iomem *) base;
2880 dev->usb = (struct net2280_usb_regs __iomem *) (base + 0x0080);
2881 dev->pci = (struct net2280_pci_regs __iomem *) (base + 0x0100);
2882 dev->dma = (struct net2280_dma_regs __iomem *) (base + 0x0180);
2883 dev->dep = (struct net2280_dep_regs __iomem *) (base + 0x0200);
2884 dev->epregs = (struct net2280_ep_regs __iomem *) (base + 0x0300);
2886 /* put into initial config, link up all endpoints */
2887 writel (0, &dev->usb->usbctl);
2888 usb_reset (dev);
2889 usb_reinit (dev);
2891 /* irq setup after old hardware is cleaned up */
2892 if (!pdev->irq) {
2893 ERROR (dev, "No IRQ. Check PCI setup!\n");
2894 retval = -ENODEV;
2895 goto done;
2898 if (request_irq (pdev->irq, net2280_irq, IRQF_SHARED, driver_name, dev)
2899 != 0) {
2900 ERROR (dev, "request interrupt %d failed\n", pdev->irq);
2901 retval = -EBUSY;
2902 goto done;
2904 dev->got_irq = 1;
2906 /* DMA setup */
2907 /* NOTE: we know only the 32 LSBs of dma addresses may be nonzero */
2908 dev->requests = pci_pool_create ("requests", pdev,
2909 sizeof (struct net2280_dma),
2910 0 /* no alignment requirements */,
2911 0 /* or page-crossing issues */);
2912 if (!dev->requests) {
2913 DEBUG (dev, "can't get request pool\n");
2914 retval = -ENOMEM;
2915 goto done;
2917 for (i = 1; i < 5; i++) {
2918 struct net2280_dma *td;
2920 td = pci_pool_alloc (dev->requests, GFP_KERNEL,
2921 &dev->ep [i].td_dma);
2922 if (!td) {
2923 DEBUG (dev, "can't get dummy %d\n", i);
2924 retval = -ENOMEM;
2925 goto done;
2927 td->dmacount = 0; /* not VALID */
2928 td->dmaaddr = __constant_cpu_to_le32 (DMA_ADDR_INVALID);
2929 td->dmadesc = td->dmaaddr;
2930 dev->ep [i].dummy = td;
2933 /* enable lower-overhead pci memory bursts during DMA */
2934 writel ( (1 << DMA_MEMORY_WRITE_AND_INVALIDATE_ENABLE)
2935 // 256 write retries may not be enough...
2936 // | (1 << PCI_RETRY_ABORT_ENABLE)
2937 | (1 << DMA_READ_MULTIPLE_ENABLE)
2938 | (1 << DMA_READ_LINE_ENABLE)
2939 , &dev->pci->pcimstctl);
2940 /* erratum 0115 shouldn't appear: Linux inits PCI_LATENCY_TIMER */
2941 pci_set_master (pdev);
2942 pci_set_mwi (pdev);
2944 /* ... also flushes any posted pci writes */
2945 dev->chiprev = get_idx_reg (dev->regs, REG_CHIPREV) & 0xffff;
2947 /* done */
2948 INFO (dev, "%s\n", driver_desc);
2949 INFO (dev, "irq %d, pci mem %p, chip rev %04x\n",
2950 pdev->irq, base, dev->chiprev);
2951 INFO (dev, "version: " DRIVER_VERSION "; dma %s\n",
2952 use_dma
2953 ? (use_dma_chaining ? "chaining" : "enabled")
2954 : "disabled");
2955 the_controller = dev;
2957 device_register (&dev->gadget.dev);
2958 device_create_file (&pdev->dev, &dev_attr_registers);
2960 return 0;
2962 done:
2963 if (dev)
2964 net2280_remove (pdev);
2965 return retval;
2968 /* make sure the board is quiescent; otherwise it will continue
2969 * generating IRQs across the upcoming reboot.
2972 static void net2280_shutdown (struct pci_dev *pdev)
2974 struct net2280 *dev = pci_get_drvdata (pdev);
2976 /* disable IRQs */
2977 writel (0, &dev->regs->pciirqenb0);
2978 writel (0, &dev->regs->pciirqenb1);
2980 /* disable the pullup so the host will think we're gone */
2981 writel (0, &dev->usb->usbctl);
2985 /*-------------------------------------------------------------------------*/
2987 static struct pci_device_id pci_ids [] = { {
2988 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
2989 .class_mask = ~0,
2990 .vendor = 0x17cc,
2991 .device = 0x2280,
2992 .subvendor = PCI_ANY_ID,
2993 .subdevice = PCI_ANY_ID,
2994 }, {
2995 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
2996 .class_mask = ~0,
2997 .vendor = 0x17cc,
2998 .device = 0x2282,
2999 .subvendor = PCI_ANY_ID,
3000 .subdevice = PCI_ANY_ID,
3002 }, { /* end: all zeroes */ }
3004 MODULE_DEVICE_TABLE (pci, pci_ids);
3006 /* pci driver glue; this is a "new style" PCI driver module */
3007 static struct pci_driver net2280_pci_driver = {
3008 .name = (char *) driver_name,
3009 .id_table = pci_ids,
3011 .probe = net2280_probe,
3012 .remove = net2280_remove,
3013 .shutdown = net2280_shutdown,
3015 /* FIXME add power management support */
3018 MODULE_DESCRIPTION (DRIVER_DESC);
3019 MODULE_AUTHOR ("David Brownell");
3020 MODULE_LICENSE ("GPL");
3022 static int __init init (void)
3024 if (!use_dma)
3025 use_dma_chaining = 0;
3026 return pci_register_driver (&net2280_pci_driver);
3028 module_init (init);
3030 static void __exit cleanup (void)
3032 pci_unregister_driver (&net2280_pci_driver);
3034 module_exit (cleanup);