[XFRM]: skb_cow_data() does not set proper owner for new skbs.
[linux-2.6/verdex.git] / drivers / usb / gadget / net2280.c
blobe5457f2026ccd3891c0129a08bc9fe2445d88aea
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, int 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_NONCOHERENT_IO)
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 int 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, int 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 u32 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 | 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, 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, 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 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, 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);
1783 * net2280_set_fifo_mode - change allocation of fifo buffers
1784 * @gadget: access to the net2280 device that will be updated
1785 * @mode: 0 for default, four 1kB buffers (ep-a through ep-d);
1786 * 1 for two 2kB buffers (ep-a and ep-b only);
1787 * 2 for one 2kB buffer (ep-a) and two 1kB ones (ep-b, ep-c).
1789 * returns zero on success, else negative errno. when this succeeds,
1790 * the contents of gadget->ep_list may have changed.
1792 * you may only call this function when endpoints a-d are all disabled.
1793 * use it whenever extra hardware buffering can help performance, such
1794 * as before enabling "high bandwidth" interrupt endpoints that use
1795 * maxpacket bigger than 512 (when double buffering would otherwise
1796 * be unavailable).
1798 int net2280_set_fifo_mode (struct usb_gadget *gadget, int mode)
1800 int i;
1801 struct net2280 *dev;
1802 int status = 0;
1803 unsigned long flags;
1805 if (!gadget)
1806 return -ENODEV;
1807 dev = container_of (gadget, struct net2280, gadget);
1809 spin_lock_irqsave (&dev->lock, flags);
1811 for (i = 1; i <= 4; i++)
1812 if (dev->ep [i].desc) {
1813 status = -EINVAL;
1814 break;
1816 if (mode < 0 || mode > 2)
1817 status = -EINVAL;
1818 if (status == 0)
1819 set_fifo_mode (dev, mode);
1820 spin_unlock_irqrestore (&dev->lock, flags);
1822 if (status == 0) {
1823 if (mode == 1)
1824 DEBUG (dev, "fifo: ep-a 2K, ep-b 2K\n");
1825 else if (mode == 2)
1826 DEBUG (dev, "fifo: ep-a 2K, ep-b 1K, ep-c 1K\n");
1827 /* else all are 1K */
1829 return status;
1831 EXPORT_SYMBOL (net2280_set_fifo_mode);
1833 /*-------------------------------------------------------------------------*/
1835 /* keeping it simple:
1836 * - one bus driver, initted first;
1837 * - one function driver, initted second
1839 * most of the work to support multiple net2280 controllers would
1840 * be to associate this gadget driver (yes?) with all of them, or
1841 * perhaps to bind specific drivers to specific devices.
1844 static struct net2280 *the_controller;
1846 static void usb_reset (struct net2280 *dev)
1848 u32 tmp;
1850 dev->gadget.speed = USB_SPEED_UNKNOWN;
1851 (void) readl (&dev->usb->usbctl);
1853 net2280_led_init (dev);
1855 /* disable automatic responses, and irqs */
1856 writel (0, &dev->usb->stdrsp);
1857 writel (0, &dev->regs->pciirqenb0);
1858 writel (0, &dev->regs->pciirqenb1);
1860 /* clear old dma and irq state */
1861 for (tmp = 0; tmp < 4; tmp++) {
1862 struct net2280_ep *ep = &dev->ep [tmp + 1];
1864 if (ep->dma)
1865 abort_dma (ep);
1867 writel (~0, &dev->regs->irqstat0),
1868 writel (~(1 << SUSPEND_REQUEST_INTERRUPT), &dev->regs->irqstat1),
1870 /* reset, and enable pci */
1871 tmp = readl (&dev->regs->devinit)
1872 | (1 << PCI_ENABLE)
1873 | (1 << FIFO_SOFT_RESET)
1874 | (1 << USB_SOFT_RESET)
1875 | (1 << M8051_RESET);
1876 writel (tmp, &dev->regs->devinit);
1878 /* standard fifo and endpoint allocations */
1879 set_fifo_mode (dev, (fifo_mode <= 2) ? fifo_mode : 0);
1882 static void usb_reinit (struct net2280 *dev)
1884 u32 tmp;
1885 int init_dma;
1887 /* use_dma changes are ignored till next device re-init */
1888 init_dma = use_dma;
1890 /* basic endpoint init */
1891 for (tmp = 0; tmp < 7; tmp++) {
1892 struct net2280_ep *ep = &dev->ep [tmp];
1894 ep->ep.name = ep_name [tmp];
1895 ep->dev = dev;
1896 ep->num = tmp;
1898 if (tmp > 0 && tmp <= 4) {
1899 ep->fifo_size = 1024;
1900 if (init_dma)
1901 ep->dma = &dev->dma [tmp - 1];
1902 } else
1903 ep->fifo_size = 64;
1904 ep->regs = &dev->epregs [tmp];
1905 ep_reset (dev->regs, ep);
1907 dev->ep [0].ep.maxpacket = 64;
1908 dev->ep [5].ep.maxpacket = 64;
1909 dev->ep [6].ep.maxpacket = 64;
1911 dev->gadget.ep0 = &dev->ep [0].ep;
1912 dev->ep [0].stopped = 0;
1913 INIT_LIST_HEAD (&dev->gadget.ep0->ep_list);
1915 /* we want to prevent lowlevel/insecure access from the USB host,
1916 * but erratum 0119 means this enable bit is ignored
1918 for (tmp = 0; tmp < 5; tmp++)
1919 writel (EP_DONTUSE, &dev->dep [tmp].dep_cfg);
1922 static void ep0_start (struct net2280 *dev)
1924 writel ( (1 << CLEAR_EP_HIDE_STATUS_PHASE)
1925 | (1 << CLEAR_NAK_OUT_PACKETS)
1926 | (1 << CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE)
1927 , &dev->epregs [0].ep_rsp);
1930 * hardware optionally handles a bunch of standard requests
1931 * that the API hides from drivers anyway. have it do so.
1932 * endpoint status/features are handled in software, to
1933 * help pass tests for some dubious behavior.
1935 writel ( (1 << SET_TEST_MODE)
1936 | (1 << SET_ADDRESS)
1937 | (1 << DEVICE_SET_CLEAR_DEVICE_REMOTE_WAKEUP)
1938 | (1 << GET_DEVICE_STATUS)
1939 | (1 << GET_INTERFACE_STATUS)
1940 , &dev->usb->stdrsp);
1941 writel ( (1 << USB_ROOT_PORT_WAKEUP_ENABLE)
1942 | (1 << SELF_POWERED_USB_DEVICE)
1943 | (1 << REMOTE_WAKEUP_SUPPORT)
1944 | (dev->softconnect << USB_DETECT_ENABLE)
1945 | (1 << SELF_POWERED_STATUS)
1946 , &dev->usb->usbctl);
1948 /* enable irqs so we can see ep0 and general operation */
1949 writel ( (1 << SETUP_PACKET_INTERRUPT_ENABLE)
1950 | (1 << ENDPOINT_0_INTERRUPT_ENABLE)
1951 , &dev->regs->pciirqenb0);
1952 writel ( (1 << PCI_INTERRUPT_ENABLE)
1953 | (1 << PCI_MASTER_ABORT_RECEIVED_INTERRUPT_ENABLE)
1954 | (1 << PCI_TARGET_ABORT_RECEIVED_INTERRUPT_ENABLE)
1955 | (1 << PCI_RETRY_ABORT_INTERRUPT_ENABLE)
1956 | (1 << VBUS_INTERRUPT_ENABLE)
1957 | (1 << ROOT_PORT_RESET_INTERRUPT_ENABLE)
1958 | (1 << SUSPEND_REQUEST_CHANGE_INTERRUPT_ENABLE)
1959 , &dev->regs->pciirqenb1);
1961 /* don't leave any writes posted */
1962 (void) readl (&dev->usb->usbctl);
1965 /* when a driver is successfully registered, it will receive
1966 * control requests including set_configuration(), which enables
1967 * non-control requests. then usb traffic follows until a
1968 * disconnect is reported. then a host may connect again, or
1969 * the driver might get unbound.
1971 int usb_gadget_register_driver (struct usb_gadget_driver *driver)
1973 struct net2280 *dev = the_controller;
1974 int retval;
1975 unsigned i;
1977 /* insist on high speed support from the driver, since
1978 * (dev->usb->xcvrdiag & FORCE_FULL_SPEED_MODE)
1979 * "must not be used in normal operation"
1981 if (!driver
1982 || driver->speed != USB_SPEED_HIGH
1983 || !driver->bind
1984 || !driver->unbind
1985 || !driver->setup)
1986 return -EINVAL;
1987 if (!dev)
1988 return -ENODEV;
1989 if (dev->driver)
1990 return -EBUSY;
1992 for (i = 0; i < 7; i++)
1993 dev->ep [i].irqs = 0;
1995 /* hook up the driver ... */
1996 dev->softconnect = 1;
1997 driver->driver.bus = NULL;
1998 dev->driver = driver;
1999 dev->gadget.dev.driver = &driver->driver;
2000 retval = driver->bind (&dev->gadget);
2001 if (retval) {
2002 DEBUG (dev, "bind to driver %s --> %d\n",
2003 driver->driver.name, retval);
2004 dev->driver = NULL;
2005 dev->gadget.dev.driver = NULL;
2006 return retval;
2009 device_create_file (&dev->pdev->dev, &dev_attr_function);
2010 device_create_file (&dev->pdev->dev, &dev_attr_queues);
2012 /* ... then enable host detection and ep0; and we're ready
2013 * for set_configuration as well as eventual disconnect.
2015 net2280_led_active (dev, 1);
2016 ep0_start (dev);
2018 DEBUG (dev, "%s ready, usbctl %08x stdrsp %08x\n",
2019 driver->driver.name,
2020 readl (&dev->usb->usbctl),
2021 readl (&dev->usb->stdrsp));
2023 /* pci writes may still be posted */
2024 return 0;
2026 EXPORT_SYMBOL (usb_gadget_register_driver);
2028 static void
2029 stop_activity (struct net2280 *dev, struct usb_gadget_driver *driver)
2031 int i;
2033 /* don't disconnect if it's not connected */
2034 if (dev->gadget.speed == USB_SPEED_UNKNOWN)
2035 driver = NULL;
2037 /* stop hardware; prevent new request submissions;
2038 * and kill any outstanding requests.
2040 usb_reset (dev);
2041 for (i = 0; i < 7; i++)
2042 nuke (&dev->ep [i]);
2044 /* report disconnect; the driver is already quiesced */
2045 if (driver) {
2046 spin_unlock (&dev->lock);
2047 driver->disconnect (&dev->gadget);
2048 spin_lock (&dev->lock);
2051 usb_reinit (dev);
2054 int usb_gadget_unregister_driver (struct usb_gadget_driver *driver)
2056 struct net2280 *dev = the_controller;
2057 unsigned long flags;
2059 if (!dev)
2060 return -ENODEV;
2061 if (!driver || driver != dev->driver)
2062 return -EINVAL;
2064 spin_lock_irqsave (&dev->lock, flags);
2065 stop_activity (dev, driver);
2066 spin_unlock_irqrestore (&dev->lock, flags);
2068 net2280_pullup (&dev->gadget, 0);
2070 driver->unbind (&dev->gadget);
2071 dev->gadget.dev.driver = NULL;
2072 dev->driver = NULL;
2074 net2280_led_active (dev, 0);
2075 device_remove_file (&dev->pdev->dev, &dev_attr_function);
2076 device_remove_file (&dev->pdev->dev, &dev_attr_queues);
2078 DEBUG (dev, "unregistered driver '%s'\n", driver->driver.name);
2079 return 0;
2081 EXPORT_SYMBOL (usb_gadget_unregister_driver);
2084 /*-------------------------------------------------------------------------*/
2086 /* handle ep0, ep-e, ep-f with 64 byte packets: packet per irq.
2087 * also works for dma-capable endpoints, in pio mode or just
2088 * to manually advance the queue after short OUT transfers.
2090 static void handle_ep_small (struct net2280_ep *ep)
2092 struct net2280_request *req;
2093 u32 t;
2094 /* 0 error, 1 mid-data, 2 done */
2095 int mode = 1;
2097 if (!list_empty (&ep->queue))
2098 req = list_entry (ep->queue.next,
2099 struct net2280_request, queue);
2100 else
2101 req = NULL;
2103 /* ack all, and handle what we care about */
2104 t = readl (&ep->regs->ep_stat);
2105 ep->irqs++;
2106 #if 0
2107 VDEBUG (ep->dev, "%s ack ep_stat %08x, req %p\n",
2108 ep->ep.name, t, req ? &req->req : 0);
2109 #endif
2110 writel (t & ~(1 << NAK_OUT_PACKETS), &ep->regs->ep_stat);
2112 /* for ep0, monitor token irqs to catch data stage length errors
2113 * and to synchronize on status.
2115 * also, to defer reporting of protocol stalls ... here's where
2116 * data or status first appears, handling stalls here should never
2117 * cause trouble on the host side..
2119 * control requests could be slightly faster without token synch for
2120 * status, but status can jam up that way.
2122 if (unlikely (ep->num == 0)) {
2123 if (ep->is_in) {
2124 /* status; stop NAKing */
2125 if (t & (1 << DATA_OUT_PING_TOKEN_INTERRUPT)) {
2126 if (ep->dev->protocol_stall) {
2127 ep->stopped = 1;
2128 set_halt (ep);
2130 if (!req)
2131 allow_status (ep);
2132 mode = 2;
2133 /* reply to extra IN data tokens with a zlp */
2134 } else if (t & (1 << DATA_IN_TOKEN_INTERRUPT)) {
2135 if (ep->dev->protocol_stall) {
2136 ep->stopped = 1;
2137 set_halt (ep);
2138 mode = 2;
2139 } else if (!req && ep->stopped)
2140 write_fifo (ep, NULL);
2142 } else {
2143 /* status; stop NAKing */
2144 if (t & (1 << DATA_IN_TOKEN_INTERRUPT)) {
2145 if (ep->dev->protocol_stall) {
2146 ep->stopped = 1;
2147 set_halt (ep);
2149 mode = 2;
2150 /* an extra OUT token is an error */
2151 } else if (((t & (1 << DATA_OUT_PING_TOKEN_INTERRUPT))
2152 && req
2153 && req->req.actual == req->req.length)
2154 || !req) {
2155 ep->dev->protocol_stall = 1;
2156 set_halt (ep);
2157 ep->stopped = 1;
2158 if (req)
2159 done (ep, req, -EOVERFLOW);
2160 req = NULL;
2165 if (unlikely (!req))
2166 return;
2168 /* manual DMA queue advance after short OUT */
2169 if (likely (ep->dma != 0)) {
2170 if (t & (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)) {
2171 u32 count;
2172 int stopped = ep->stopped;
2174 /* TRANSFERRED works around OUT_DONE erratum 0112.
2175 * we expect (N <= maxpacket) bytes; host wrote M.
2176 * iff (M < N) we won't ever see a DMA interrupt.
2178 ep->stopped = 1;
2179 for (count = 0; ; t = readl (&ep->regs->ep_stat)) {
2181 /* any preceding dma transfers must finish.
2182 * dma handles (M >= N), may empty the queue
2184 scan_dma_completions (ep);
2185 if (unlikely (list_empty (&ep->queue)
2186 || ep->out_overflow)) {
2187 req = NULL;
2188 break;
2190 req = list_entry (ep->queue.next,
2191 struct net2280_request, queue);
2193 /* here either (M < N), a "real" short rx;
2194 * or (M == N) and the queue didn't empty
2196 if (likely (t & (1 << FIFO_EMPTY))) {
2197 count = readl (&ep->dma->dmacount);
2198 count &= DMA_BYTE_COUNT_MASK;
2199 if (readl (&ep->dma->dmadesc)
2200 != req->td_dma)
2201 req = NULL;
2202 break;
2204 udelay(1);
2207 /* stop DMA, leave ep NAKing */
2208 writel ((1 << DMA_ABORT), &ep->dma->dmastat);
2209 spin_stop_dma (ep->dma);
2211 if (likely (req)) {
2212 req->td->dmacount = 0;
2213 t = readl (&ep->regs->ep_avail);
2214 dma_done (ep, req, count, t);
2217 /* also flush to prevent erratum 0106 trouble */
2218 if (unlikely (ep->out_overflow
2219 || (ep->dev->chiprev == 0x0100
2220 && ep->dev->gadget.speed
2221 == USB_SPEED_FULL))) {
2222 out_flush (ep);
2223 ep->out_overflow = 0;
2226 /* (re)start dma if needed, stop NAKing */
2227 ep->stopped = stopped;
2228 if (!list_empty (&ep->queue))
2229 restart_dma (ep);
2230 } else
2231 DEBUG (ep->dev, "%s dma ep_stat %08x ??\n",
2232 ep->ep.name, t);
2233 return;
2235 /* data packet(s) received (in the fifo, OUT) */
2236 } else if (t & (1 << DATA_PACKET_RECEIVED_INTERRUPT)) {
2237 if (read_fifo (ep, req) && ep->num != 0)
2238 mode = 2;
2240 /* data packet(s) transmitted (IN) */
2241 } else if (t & (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)) {
2242 unsigned len;
2244 len = req->req.length - req->req.actual;
2245 if (len > ep->ep.maxpacket)
2246 len = ep->ep.maxpacket;
2247 req->req.actual += len;
2249 /* if we wrote it all, we're usually done */
2250 if (req->req.actual == req->req.length) {
2251 if (ep->num == 0) {
2252 /* wait for control status */
2253 if (mode != 2)
2254 req = NULL;
2255 } else if (!req->req.zero || len != ep->ep.maxpacket)
2256 mode = 2;
2259 /* there was nothing to do ... */
2260 } else if (mode == 1)
2261 return;
2263 /* done */
2264 if (mode == 2) {
2265 /* stream endpoints often resubmit/unlink in completion */
2266 done (ep, req, 0);
2268 /* maybe advance queue to next request */
2269 if (ep->num == 0) {
2270 /* NOTE: net2280 could let gadget driver start the
2271 * status stage later. since not all controllers let
2272 * them control that, the api doesn't (yet) allow it.
2274 if (!ep->stopped)
2275 allow_status (ep);
2276 req = NULL;
2277 } else {
2278 if (!list_empty (&ep->queue) && !ep->stopped)
2279 req = list_entry (ep->queue.next,
2280 struct net2280_request, queue);
2281 else
2282 req = NULL;
2283 if (req && !ep->is_in)
2284 stop_out_naking (ep);
2288 /* is there a buffer for the next packet?
2289 * for best streaming performance, make sure there is one.
2291 if (req && !ep->stopped) {
2293 /* load IN fifo with next packet (may be zlp) */
2294 if (t & (1 << DATA_PACKET_TRANSMITTED_INTERRUPT))
2295 write_fifo (ep, &req->req);
2299 static struct net2280_ep *
2300 get_ep_by_addr (struct net2280 *dev, u16 wIndex)
2302 struct net2280_ep *ep;
2304 if ((wIndex & USB_ENDPOINT_NUMBER_MASK) == 0)
2305 return &dev->ep [0];
2306 list_for_each_entry (ep, &dev->gadget.ep_list, ep.ep_list) {
2307 u8 bEndpointAddress;
2309 if (!ep->desc)
2310 continue;
2311 bEndpointAddress = ep->desc->bEndpointAddress;
2312 if ((wIndex ^ bEndpointAddress) & USB_DIR_IN)
2313 continue;
2314 if ((wIndex & 0x0f) == (bEndpointAddress & 0x0f))
2315 return ep;
2317 return NULL;
2320 static void handle_stat0_irqs (struct net2280 *dev, u32 stat)
2322 struct net2280_ep *ep;
2323 u32 num, scratch;
2325 /* most of these don't need individual acks */
2326 stat &= ~(1 << INTA_ASSERTED);
2327 if (!stat)
2328 return;
2329 // DEBUG (dev, "irqstat0 %04x\n", stat);
2331 /* starting a control request? */
2332 if (unlikely (stat & (1 << SETUP_PACKET_INTERRUPT))) {
2333 union {
2334 u32 raw [2];
2335 struct usb_ctrlrequest r;
2336 } u;
2337 int tmp = 0;
2338 struct net2280_request *req;
2340 if (dev->gadget.speed == USB_SPEED_UNKNOWN) {
2341 if (readl (&dev->usb->usbstat) & (1 << HIGH_SPEED))
2342 dev->gadget.speed = USB_SPEED_HIGH;
2343 else
2344 dev->gadget.speed = USB_SPEED_FULL;
2345 net2280_led_speed (dev, dev->gadget.speed);
2346 DEBUG (dev, "%s speed\n",
2347 (dev->gadget.speed == USB_SPEED_HIGH)
2348 ? "high" : "full");
2351 ep = &dev->ep [0];
2352 ep->irqs++;
2354 /* make sure any leftover request state is cleared */
2355 stat &= ~(1 << ENDPOINT_0_INTERRUPT);
2356 while (!list_empty (&ep->queue)) {
2357 req = list_entry (ep->queue.next,
2358 struct net2280_request, queue);
2359 done (ep, req, (req->req.actual == req->req.length)
2360 ? 0 : -EPROTO);
2362 ep->stopped = 0;
2363 dev->protocol_stall = 0;
2364 writel ( (1 << TIMEOUT)
2365 | (1 << USB_STALL_SENT)
2366 | (1 << USB_IN_NAK_SENT)
2367 | (1 << USB_IN_ACK_RCVD)
2368 | (1 << USB_OUT_PING_NAK_SENT)
2369 | (1 << USB_OUT_ACK_SENT)
2370 | (1 << FIFO_OVERFLOW)
2371 | (1 << FIFO_UNDERFLOW)
2372 | (1 << SHORT_PACKET_OUT_DONE_INTERRUPT)
2373 | (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)
2374 | (1 << DATA_PACKET_RECEIVED_INTERRUPT)
2375 | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
2376 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
2377 | (1 << DATA_IN_TOKEN_INTERRUPT)
2378 , &ep->regs->ep_stat);
2379 u.raw [0] = readl (&dev->usb->setup0123);
2380 u.raw [1] = readl (&dev->usb->setup4567);
2382 cpu_to_le32s (&u.raw [0]);
2383 cpu_to_le32s (&u.raw [1]);
2385 le16_to_cpus (&u.r.wValue);
2386 le16_to_cpus (&u.r.wIndex);
2387 le16_to_cpus (&u.r.wLength);
2389 /* ack the irq */
2390 writel (1 << SETUP_PACKET_INTERRUPT, &dev->regs->irqstat0);
2391 stat ^= (1 << SETUP_PACKET_INTERRUPT);
2393 /* watch control traffic at the token level, and force
2394 * synchronization before letting the status stage happen.
2395 * FIXME ignore tokens we'll NAK, until driver responds.
2396 * that'll mean a lot less irqs for some drivers.
2398 ep->is_in = (u.r.bRequestType & USB_DIR_IN) != 0;
2399 if (ep->is_in) {
2400 scratch = (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
2401 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
2402 | (1 << DATA_IN_TOKEN_INTERRUPT);
2403 stop_out_naking (ep);
2404 } else
2405 scratch = (1 << DATA_PACKET_RECEIVED_INTERRUPT)
2406 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
2407 | (1 << DATA_IN_TOKEN_INTERRUPT);
2408 writel (scratch, &dev->epregs [0].ep_irqenb);
2410 /* we made the hardware handle most lowlevel requests;
2411 * everything else goes uplevel to the gadget code.
2413 switch (u.r.bRequest) {
2414 case USB_REQ_GET_STATUS: {
2415 struct net2280_ep *e;
2416 u16 status;
2418 /* hw handles device and interface status */
2419 if (u.r.bRequestType != (USB_DIR_IN|USB_RECIP_ENDPOINT))
2420 goto delegate;
2421 if ((e = get_ep_by_addr (dev, u.r.wIndex)) == 0
2422 || u.r.wLength > 2)
2423 goto do_stall;
2425 if (readl (&e->regs->ep_rsp)
2426 & (1 << SET_ENDPOINT_HALT))
2427 status = __constant_cpu_to_le16 (1);
2428 else
2429 status = __constant_cpu_to_le16 (0);
2431 /* don't bother with a request object! */
2432 writel (0, &dev->epregs [0].ep_irqenb);
2433 set_fifo_bytecount (ep, u.r.wLength);
2434 writel (status, &dev->epregs [0].ep_data);
2435 allow_status (ep);
2436 VDEBUG (dev, "%s stat %02x\n", ep->ep.name, status);
2437 goto next_endpoints;
2439 break;
2440 case USB_REQ_CLEAR_FEATURE: {
2441 struct net2280_ep *e;
2443 /* hw handles device features */
2444 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
2445 goto delegate;
2446 if (u.r.wValue != USB_ENDPOINT_HALT
2447 || u.r.wLength != 0)
2448 goto do_stall;
2449 if ((e = get_ep_by_addr (dev, u.r.wIndex)) == 0)
2450 goto do_stall;
2451 clear_halt (e);
2452 allow_status (ep);
2453 VDEBUG (dev, "%s clear halt\n", ep->ep.name);
2454 goto next_endpoints;
2456 break;
2457 case USB_REQ_SET_FEATURE: {
2458 struct net2280_ep *e;
2460 /* hw handles device features */
2461 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
2462 goto delegate;
2463 if (u.r.wValue != USB_ENDPOINT_HALT
2464 || u.r.wLength != 0)
2465 goto do_stall;
2466 if ((e = get_ep_by_addr (dev, u.r.wIndex)) == 0)
2467 goto do_stall;
2468 set_halt (e);
2469 allow_status (ep);
2470 VDEBUG (dev, "%s set halt\n", ep->ep.name);
2471 goto next_endpoints;
2473 break;
2474 default:
2475 delegate:
2476 VDEBUG (dev, "setup %02x.%02x v%04x i%04x "
2477 "ep_cfg %08x\n",
2478 u.r.bRequestType, u.r.bRequest,
2479 u.r.wValue, u.r.wIndex,
2480 readl (&ep->regs->ep_cfg));
2481 spin_unlock (&dev->lock);
2482 tmp = dev->driver->setup (&dev->gadget, &u.r);
2483 spin_lock (&dev->lock);
2486 /* stall ep0 on error */
2487 if (tmp < 0) {
2488 do_stall:
2489 VDEBUG (dev, "req %02x.%02x protocol STALL; stat %d\n",
2490 u.r.bRequestType, u.r.bRequest, tmp);
2491 dev->protocol_stall = 1;
2494 /* some in/out token irq should follow; maybe stall then.
2495 * driver must queue a request (even zlp) or halt ep0
2496 * before the host times out.
2500 next_endpoints:
2501 /* endpoint data irq ? */
2502 scratch = stat & 0x7f;
2503 stat &= ~0x7f;
2504 for (num = 0; scratch; num++) {
2505 u32 t;
2507 /* do this endpoint's FIFO and queue need tending? */
2508 t = 1 << num;
2509 if ((scratch & t) == 0)
2510 continue;
2511 scratch ^= t;
2513 ep = &dev->ep [num];
2514 handle_ep_small (ep);
2517 if (stat)
2518 DEBUG (dev, "unhandled irqstat0 %08x\n", stat);
2521 #define DMA_INTERRUPTS ( \
2522 (1 << DMA_D_INTERRUPT) \
2523 | (1 << DMA_C_INTERRUPT) \
2524 | (1 << DMA_B_INTERRUPT) \
2525 | (1 << DMA_A_INTERRUPT))
2526 #define PCI_ERROR_INTERRUPTS ( \
2527 (1 << PCI_MASTER_ABORT_RECEIVED_INTERRUPT) \
2528 | (1 << PCI_TARGET_ABORT_RECEIVED_INTERRUPT) \
2529 | (1 << PCI_RETRY_ABORT_INTERRUPT))
2531 static void handle_stat1_irqs (struct net2280 *dev, u32 stat)
2533 struct net2280_ep *ep;
2534 u32 tmp, num, mask, scratch;
2536 /* after disconnect there's nothing else to do! */
2537 tmp = (1 << VBUS_INTERRUPT) | (1 << ROOT_PORT_RESET_INTERRUPT);
2538 mask = (1 << HIGH_SPEED) | (1 << FULL_SPEED);
2540 /* VBUS disconnect is indicated by VBUS_PIN and VBUS_INTERRUPT set.
2541 * Root Port Reset is indicated by ROOT_PORT_RESET_INTERRRUPT set and
2542 * both HIGH_SPEED and FULL_SPEED clear (as ROOT_PORT_RESET_INTERRUPT
2543 * only indicates a change in the reset state).
2545 if (stat & tmp) {
2546 writel (tmp, &dev->regs->irqstat1);
2547 if ((((stat & (1 << ROOT_PORT_RESET_INTERRUPT)) &&
2548 ((readl (&dev->usb->usbstat) & mask) == 0))
2549 || ((readl (&dev->usb->usbctl) & (1 << VBUS_PIN)) == 0)
2550 ) && ( dev->gadget.speed != USB_SPEED_UNKNOWN)) {
2551 DEBUG (dev, "disconnect %s\n",
2552 dev->driver->driver.name);
2553 stop_activity (dev, dev->driver);
2554 ep0_start (dev);
2555 return;
2557 stat &= ~tmp;
2559 /* vBUS can bounce ... one of many reasons to ignore the
2560 * notion of hotplug events on bus connect/disconnect!
2562 if (!stat)
2563 return;
2566 /* NOTE: chip stays in PCI D0 state for now, but it could
2567 * enter D1 to save more power
2569 tmp = (1 << SUSPEND_REQUEST_CHANGE_INTERRUPT);
2570 if (stat & tmp) {
2571 writel (tmp, &dev->regs->irqstat1);
2572 if (stat & (1 << SUSPEND_REQUEST_INTERRUPT)) {
2573 if (dev->driver->suspend)
2574 dev->driver->suspend (&dev->gadget);
2575 if (!enable_suspend)
2576 stat &= ~(1 << SUSPEND_REQUEST_INTERRUPT);
2577 } else {
2578 if (dev->driver->resume)
2579 dev->driver->resume (&dev->gadget);
2580 /* at high speed, note erratum 0133 */
2582 stat &= ~tmp;
2585 /* clear any other status/irqs */
2586 if (stat)
2587 writel (stat, &dev->regs->irqstat1);
2589 /* some status we can just ignore */
2590 stat &= ~((1 << CONTROL_STATUS_INTERRUPT)
2591 | (1 << SUSPEND_REQUEST_INTERRUPT)
2592 | (1 << RESUME_INTERRUPT)
2593 | (1 << SOF_INTERRUPT));
2594 if (!stat)
2595 return;
2596 // DEBUG (dev, "irqstat1 %08x\n", stat);
2598 /* DMA status, for ep-{a,b,c,d} */
2599 scratch = stat & DMA_INTERRUPTS;
2600 stat &= ~DMA_INTERRUPTS;
2601 scratch >>= 9;
2602 for (num = 0; scratch; num++) {
2603 struct net2280_dma_regs __iomem *dma;
2605 tmp = 1 << num;
2606 if ((tmp & scratch) == 0)
2607 continue;
2608 scratch ^= tmp;
2610 ep = &dev->ep [num + 1];
2611 dma = ep->dma;
2613 if (!dma)
2614 continue;
2616 /* clear ep's dma status */
2617 tmp = readl (&dma->dmastat);
2618 writel (tmp, &dma->dmastat);
2620 /* chaining should stop on abort, short OUT from fifo,
2621 * or (stat0 codepath) short OUT transfer.
2623 if (!use_dma_chaining) {
2624 if ((tmp & (1 << DMA_TRANSACTION_DONE_INTERRUPT))
2625 == 0) {
2626 DEBUG (ep->dev, "%s no xact done? %08x\n",
2627 ep->ep.name, tmp);
2628 continue;
2630 stop_dma (ep->dma);
2633 /* OUT transfers terminate when the data from the
2634 * host is in our memory. Process whatever's done.
2635 * On this path, we know transfer's last packet wasn't
2636 * less than req->length. NAK_OUT_PACKETS may be set,
2637 * or the FIFO may already be holding new packets.
2639 * IN transfers can linger in the FIFO for a very
2640 * long time ... we ignore that for now, accounting
2641 * precisely (like PIO does) needs per-packet irqs
2643 scan_dma_completions (ep);
2645 /* disable dma on inactive queues; else maybe restart */
2646 if (list_empty (&ep->queue)) {
2647 if (use_dma_chaining)
2648 stop_dma (ep->dma);
2649 } else {
2650 tmp = readl (&dma->dmactl);
2651 if (!use_dma_chaining
2652 || (tmp & (1 << DMA_ENABLE)) == 0)
2653 restart_dma (ep);
2654 else if (ep->is_in && use_dma_chaining) {
2655 struct net2280_request *req;
2656 u32 dmacount;
2658 /* the descriptor at the head of the chain
2659 * may still have VALID_BIT clear; that's
2660 * used to trigger changing DMA_FIFO_VALIDATE
2661 * (affects automagic zlp writes).
2663 req = list_entry (ep->queue.next,
2664 struct net2280_request, queue);
2665 dmacount = req->td->dmacount;
2666 dmacount &= __constant_cpu_to_le32 (
2667 (1 << VALID_BIT)
2668 | DMA_BYTE_COUNT_MASK);
2669 if (dmacount && (dmacount & valid_bit) == 0)
2670 restart_dma (ep);
2673 ep->irqs++;
2676 /* NOTE: there are other PCI errors we might usefully notice.
2677 * if they appear very often, here's where to try recovering.
2679 if (stat & PCI_ERROR_INTERRUPTS) {
2680 ERROR (dev, "pci dma error; stat %08x\n", stat);
2681 stat &= ~PCI_ERROR_INTERRUPTS;
2682 /* these are fatal errors, but "maybe" they won't
2683 * happen again ...
2685 stop_activity (dev, dev->driver);
2686 ep0_start (dev);
2687 stat = 0;
2690 if (stat)
2691 DEBUG (dev, "unhandled irqstat1 %08x\n", stat);
2694 static irqreturn_t net2280_irq (int irq, void *_dev, struct pt_regs * r)
2696 struct net2280 *dev = _dev;
2698 spin_lock (&dev->lock);
2700 /* handle disconnect, dma, and more */
2701 handle_stat1_irqs (dev, readl (&dev->regs->irqstat1));
2703 /* control requests and PIO */
2704 handle_stat0_irqs (dev, readl (&dev->regs->irqstat0));
2706 spin_unlock (&dev->lock);
2708 return IRQ_HANDLED;
2711 /*-------------------------------------------------------------------------*/
2713 static void gadget_release (struct device *_dev)
2715 struct net2280 *dev = dev_get_drvdata (_dev);
2717 kfree (dev);
2720 /* tear down the binding between this driver and the pci device */
2722 static void net2280_remove (struct pci_dev *pdev)
2724 struct net2280 *dev = pci_get_drvdata (pdev);
2726 /* start with the driver above us */
2727 if (dev->driver) {
2728 /* should have been done already by driver model core */
2729 WARN (dev, "pci remove, driver '%s' is still registered\n",
2730 dev->driver->driver.name);
2731 usb_gadget_unregister_driver (dev->driver);
2734 /* then clean up the resources we allocated during probe() */
2735 net2280_led_shutdown (dev);
2736 if (dev->requests) {
2737 int i;
2738 for (i = 1; i < 5; i++) {
2739 if (!dev->ep [i].dummy)
2740 continue;
2741 pci_pool_free (dev->requests, dev->ep [i].dummy,
2742 dev->ep [i].td_dma);
2744 pci_pool_destroy (dev->requests);
2746 if (dev->got_irq)
2747 free_irq (pdev->irq, dev);
2748 if (dev->regs)
2749 iounmap (dev->regs);
2750 if (dev->region)
2751 release_mem_region (pci_resource_start (pdev, 0),
2752 pci_resource_len (pdev, 0));
2753 if (dev->enabled)
2754 pci_disable_device (pdev);
2755 device_unregister (&dev->gadget.dev);
2756 device_remove_file (&pdev->dev, &dev_attr_registers);
2757 pci_set_drvdata (pdev, NULL);
2759 INFO (dev, "unbind\n");
2761 the_controller = NULL;
2764 /* wrap this driver around the specified device, but
2765 * don't respond over USB until a gadget driver binds to us.
2768 static int net2280_probe (struct pci_dev *pdev, const struct pci_device_id *id)
2770 struct net2280 *dev;
2771 unsigned long resource, len;
2772 void __iomem *base = NULL;
2773 int retval, i;
2774 char buf [8], *bufp;
2776 /* if you want to support more than one controller in a system,
2777 * usb_gadget_driver_{register,unregister}() must change.
2779 if (the_controller) {
2780 dev_warn (&pdev->dev, "ignoring\n");
2781 return -EBUSY;
2784 /* alloc, and start init */
2785 dev = kmalloc (sizeof *dev, SLAB_KERNEL);
2786 if (dev == NULL){
2787 retval = -ENOMEM;
2788 goto done;
2791 memset (dev, 0, sizeof *dev);
2792 spin_lock_init (&dev->lock);
2793 dev->pdev = pdev;
2794 dev->gadget.ops = &net2280_ops;
2795 dev->gadget.is_dualspeed = 1;
2797 /* the "gadget" abstracts/virtualizes the controller */
2798 strcpy (dev->gadget.dev.bus_id, "gadget");
2799 dev->gadget.dev.parent = &pdev->dev;
2800 dev->gadget.dev.dma_mask = pdev->dev.dma_mask;
2801 dev->gadget.dev.release = gadget_release;
2802 dev->gadget.name = driver_name;
2804 /* now all the pci goodies ... */
2805 if (pci_enable_device (pdev) < 0) {
2806 retval = -ENODEV;
2807 goto done;
2809 dev->enabled = 1;
2811 /* BAR 0 holds all the registers
2812 * BAR 1 is 8051 memory; unused here (note erratum 0103)
2813 * BAR 2 is fifo memory; unused here
2815 resource = pci_resource_start (pdev, 0);
2816 len = pci_resource_len (pdev, 0);
2817 if (!request_mem_region (resource, len, driver_name)) {
2818 DEBUG (dev, "controller already in use\n");
2819 retval = -EBUSY;
2820 goto done;
2822 dev->region = 1;
2824 base = ioremap_nocache (resource, len);
2825 if (base == NULL) {
2826 DEBUG (dev, "can't map memory\n");
2827 retval = -EFAULT;
2828 goto done;
2830 dev->regs = (struct net2280_regs __iomem *) base;
2831 dev->usb = (struct net2280_usb_regs __iomem *) (base + 0x0080);
2832 dev->pci = (struct net2280_pci_regs __iomem *) (base + 0x0100);
2833 dev->dma = (struct net2280_dma_regs __iomem *) (base + 0x0180);
2834 dev->dep = (struct net2280_dep_regs __iomem *) (base + 0x0200);
2835 dev->epregs = (struct net2280_ep_regs __iomem *) (base + 0x0300);
2837 /* put into initial config, link up all endpoints */
2838 writel (0, &dev->usb->usbctl);
2839 usb_reset (dev);
2840 usb_reinit (dev);
2842 /* irq setup after old hardware is cleaned up */
2843 if (!pdev->irq) {
2844 ERROR (dev, "No IRQ. Check PCI setup!\n");
2845 retval = -ENODEV;
2846 goto done;
2848 #ifndef __sparc__
2849 scnprintf (buf, sizeof buf, "%d", pdev->irq);
2850 bufp = buf;
2851 #else
2852 bufp = __irq_itoa(pdev->irq);
2853 #endif
2854 if (request_irq (pdev->irq, net2280_irq, SA_SHIRQ, driver_name, dev)
2855 != 0) {
2856 ERROR (dev, "request interrupt %s failed\n", bufp);
2857 retval = -EBUSY;
2858 goto done;
2860 dev->got_irq = 1;
2862 /* DMA setup */
2863 /* NOTE: we know only the 32 LSBs of dma addresses may be nonzero */
2864 dev->requests = pci_pool_create ("requests", pdev,
2865 sizeof (struct net2280_dma),
2866 0 /* no alignment requirements */,
2867 0 /* or page-crossing issues */);
2868 if (!dev->requests) {
2869 DEBUG (dev, "can't get request pool\n");
2870 retval = -ENOMEM;
2871 goto done;
2873 for (i = 1; i < 5; i++) {
2874 struct net2280_dma *td;
2876 td = pci_pool_alloc (dev->requests, GFP_KERNEL,
2877 &dev->ep [i].td_dma);
2878 if (!td) {
2879 DEBUG (dev, "can't get dummy %d\n", i);
2880 retval = -ENOMEM;
2881 goto done;
2883 td->dmacount = 0; /* not VALID */
2884 td->dmaaddr = __constant_cpu_to_le32 (DMA_ADDR_INVALID);
2885 td->dmadesc = td->dmaaddr;
2886 dev->ep [i].dummy = td;
2889 /* enable lower-overhead pci memory bursts during DMA */
2890 writel ( (1 << DMA_MEMORY_WRITE_AND_INVALIDATE_ENABLE)
2891 // 256 write retries may not be enough...
2892 // | (1 << PCI_RETRY_ABORT_ENABLE)
2893 | (1 << DMA_READ_MULTIPLE_ENABLE)
2894 | (1 << DMA_READ_LINE_ENABLE)
2895 , &dev->pci->pcimstctl);
2896 /* erratum 0115 shouldn't appear: Linux inits PCI_LATENCY_TIMER */
2897 pci_set_master (pdev);
2898 pci_set_mwi (pdev);
2900 /* ... also flushes any posted pci writes */
2901 dev->chiprev = get_idx_reg (dev->regs, REG_CHIPREV) & 0xffff;
2903 /* done */
2904 pci_set_drvdata (pdev, dev);
2905 INFO (dev, "%s\n", driver_desc);
2906 INFO (dev, "irq %s, pci mem %p, chip rev %04x\n",
2907 bufp, base, dev->chiprev);
2908 INFO (dev, "version: " DRIVER_VERSION "; dma %s\n",
2909 use_dma
2910 ? (use_dma_chaining ? "chaining" : "enabled")
2911 : "disabled");
2912 the_controller = dev;
2914 device_register (&dev->gadget.dev);
2915 device_create_file (&pdev->dev, &dev_attr_registers);
2917 return 0;
2919 done:
2920 if (dev)
2921 net2280_remove (pdev);
2922 return retval;
2926 /*-------------------------------------------------------------------------*/
2928 static struct pci_device_id pci_ids [] = { {
2929 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
2930 .class_mask = ~0,
2931 .vendor = 0x17cc,
2932 .device = 0x2280,
2933 .subvendor = PCI_ANY_ID,
2934 .subdevice = PCI_ANY_ID,
2936 }, { /* end: all zeroes */ }
2938 MODULE_DEVICE_TABLE (pci, pci_ids);
2940 /* pci driver glue; this is a "new style" PCI driver module */
2941 static struct pci_driver net2280_pci_driver = {
2942 .name = (char *) driver_name,
2943 .id_table = pci_ids,
2945 .probe = net2280_probe,
2946 .remove = net2280_remove,
2948 /* FIXME add power management support */
2951 MODULE_DESCRIPTION (DRIVER_DESC);
2952 MODULE_AUTHOR ("David Brownell");
2953 MODULE_LICENSE ("GPL");
2955 static int __init init (void)
2957 if (!use_dma)
2958 use_dma_chaining = 0;
2959 return pci_register_driver (&net2280_pci_driver);
2961 module_init (init);
2963 static void __exit cleanup (void)
2965 pci_unregister_driver (&net2280_pci_driver);
2967 module_exit (cleanup);