usb: gadget: langwell: convert to new style
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / usb / gadget / net2280.c
blobdb508d0bb3af82d1acb31f6c4f174d7795e4118f
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
30 * with 2282 chip
32 * This program is free software; you can redistribute it and/or modify
33 * it under the terms of the GNU General Public License as published by
34 * the Free Software Foundation; either version 2 of the License, or
35 * (at your option) any later version.
38 #undef DEBUG /* messages on error and most fault paths */
39 #undef VERBOSE /* extra debug messages (success too) */
41 #include <linux/module.h>
42 #include <linux/pci.h>
43 #include <linux/dma-mapping.h>
44 #include <linux/kernel.h>
45 #include <linux/delay.h>
46 #include <linux/ioport.h>
47 #include <linux/slab.h>
48 #include <linux/errno.h>
49 #include <linux/init.h>
50 #include <linux/timer.h>
51 #include <linux/list.h>
52 #include <linux/interrupt.h>
53 #include <linux/moduleparam.h>
54 #include <linux/device.h>
55 #include <linux/usb/ch9.h>
56 #include <linux/usb/gadget.h>
57 #include <linux/prefetch.h>
59 #include <asm/byteorder.h>
60 #include <asm/io.h>
61 #include <asm/irq.h>
62 #include <asm/system.h>
63 #include <asm/unaligned.h>
66 #define DRIVER_DESC "PLX NET228x USB Peripheral Controller"
67 #define DRIVER_VERSION "2005 Sept 27"
69 #define DMA_ADDR_INVALID (~(dma_addr_t)0)
70 #define EP_DONTUSE 13 /* nonzero */
72 #define USE_RDK_LEDS /* GPIO pins control three LEDs */
75 static const char driver_name [] = "net2280";
76 static const char driver_desc [] = DRIVER_DESC;
78 static const char ep0name [] = "ep0";
79 static const char *const ep_name [] = {
80 ep0name,
81 "ep-a", "ep-b", "ep-c", "ep-d",
82 "ep-e", "ep-f",
85 /* use_dma -- general goodness, fewer interrupts, less cpu load (vs PIO)
86 * use_dma_chaining -- dma descriptor queueing gives even more irq reduction
88 * The net2280 DMA engines are not tightly integrated with their FIFOs;
89 * not all cases are (yet) handled well in this driver or the silicon.
90 * Some gadget drivers work better with the dma support here than others.
91 * These two parameters let you use PIO or more aggressive DMA.
93 static int use_dma = 1;
94 static int use_dma_chaining = 0;
96 /* "modprobe net2280 use_dma=n" etc */
97 module_param (use_dma, bool, S_IRUGO);
98 module_param (use_dma_chaining, bool, S_IRUGO);
101 /* mode 0 == ep-{a,b,c,d} 1K fifo each
102 * mode 1 == ep-{a,b} 2K fifo each, ep-{c,d} unavailable
103 * mode 2 == ep-a 2K fifo, ep-{b,c} 1K each, ep-d unavailable
105 static ushort fifo_mode = 0;
107 /* "modprobe net2280 fifo_mode=1" etc */
108 module_param (fifo_mode, ushort, 0644);
110 /* enable_suspend -- When enabled, the driver will respond to
111 * USB suspend requests by powering down the NET2280. Otherwise,
112 * USB suspend requests will be ignored. This is acceptable for
113 * self-powered devices
115 static int enable_suspend = 0;
117 /* "modprobe net2280 enable_suspend=1" etc */
118 module_param (enable_suspend, bool, S_IRUGO);
121 #define DIR_STRING(bAddress) (((bAddress) & USB_DIR_IN) ? "in" : "out")
123 #if defined(CONFIG_USB_GADGET_DEBUG_FILES) || defined (DEBUG)
124 static char *type_string (u8 bmAttributes)
126 switch ((bmAttributes) & USB_ENDPOINT_XFERTYPE_MASK) {
127 case USB_ENDPOINT_XFER_BULK: return "bulk";
128 case USB_ENDPOINT_XFER_ISOC: return "iso";
129 case USB_ENDPOINT_XFER_INT: return "intr";
131 return "control";
133 #endif
135 #include "net2280.h"
137 #define valid_bit cpu_to_le32 (1 << VALID_BIT)
138 #define dma_done_ie cpu_to_le32 (1 << DMA_DONE_INTERRUPT_ENABLE)
140 /*-------------------------------------------------------------------------*/
142 static int
143 net2280_enable (struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
145 struct net2280 *dev;
146 struct net2280_ep *ep;
147 u32 max, tmp;
148 unsigned long flags;
150 ep = container_of (_ep, struct net2280_ep, ep);
151 if (!_ep || !desc || ep->desc || _ep->name == ep0name
152 || desc->bDescriptorType != USB_DT_ENDPOINT)
153 return -EINVAL;
154 dev = ep->dev;
155 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
156 return -ESHUTDOWN;
158 /* erratum 0119 workaround ties up an endpoint number */
159 if ((desc->bEndpointAddress & 0x0f) == EP_DONTUSE)
160 return -EDOM;
162 /* sanity check ep-e/ep-f since their fifos are small */
163 max = usb_endpoint_maxp (desc) & 0x1fff;
164 if (ep->num > 4 && max > 64)
165 return -ERANGE;
167 spin_lock_irqsave (&dev->lock, flags);
168 _ep->maxpacket = max & 0x7ff;
169 ep->desc = desc;
171 /* ep_reset() has already been called */
172 ep->stopped = 0;
173 ep->wedged = 0;
174 ep->out_overflow = 0;
176 /* set speed-dependent max packet; may kick in high bandwidth */
177 set_idx_reg (dev->regs, REG_EP_MAXPKT (dev, ep->num), max);
179 /* FIFO lines can't go to different packets. PIO is ok, so
180 * use it instead of troublesome (non-bulk) multi-packet DMA.
182 if (ep->dma && (max % 4) != 0 && use_dma_chaining) {
183 DEBUG (ep->dev, "%s, no dma for maxpacket %d\n",
184 ep->ep.name, ep->ep.maxpacket);
185 ep->dma = NULL;
188 /* set type, direction, address; reset fifo counters */
189 writel ((1 << FIFO_FLUSH), &ep->regs->ep_stat);
190 tmp = (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK);
191 if (tmp == USB_ENDPOINT_XFER_INT) {
192 /* erratum 0105 workaround prevents hs NYET */
193 if (dev->chiprev == 0100
194 && dev->gadget.speed == USB_SPEED_HIGH
195 && !(desc->bEndpointAddress & USB_DIR_IN))
196 writel ((1 << CLEAR_NAK_OUT_PACKETS_MODE),
197 &ep->regs->ep_rsp);
198 } else if (tmp == USB_ENDPOINT_XFER_BULK) {
199 /* catch some particularly blatant driver bugs */
200 if ((dev->gadget.speed == USB_SPEED_HIGH
201 && max != 512)
202 || (dev->gadget.speed == USB_SPEED_FULL
203 && max > 64)) {
204 spin_unlock_irqrestore (&dev->lock, flags);
205 return -ERANGE;
208 ep->is_iso = (tmp == USB_ENDPOINT_XFER_ISOC) ? 1 : 0;
209 tmp <<= ENDPOINT_TYPE;
210 tmp |= desc->bEndpointAddress;
211 tmp |= (4 << ENDPOINT_BYTE_COUNT); /* default full fifo lines */
212 tmp |= 1 << ENDPOINT_ENABLE;
213 wmb ();
215 /* for OUT transfers, block the rx fifo until a read is posted */
216 ep->is_in = (tmp & USB_DIR_IN) != 0;
217 if (!ep->is_in)
218 writel ((1 << SET_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
219 else if (dev->pdev->device != 0x2280) {
220 /* Added for 2282, Don't use nak packets on an in endpoint,
221 * this was ignored on 2280
223 writel ((1 << CLEAR_NAK_OUT_PACKETS)
224 | (1 << CLEAR_NAK_OUT_PACKETS_MODE), &ep->regs->ep_rsp);
227 writel (tmp, &ep->regs->ep_cfg);
229 /* enable irqs */
230 if (!ep->dma) { /* pio, per-packet */
231 tmp = (1 << ep->num) | readl (&dev->regs->pciirqenb0);
232 writel (tmp, &dev->regs->pciirqenb0);
234 tmp = (1 << DATA_PACKET_RECEIVED_INTERRUPT_ENABLE)
235 | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT_ENABLE);
236 if (dev->pdev->device == 0x2280)
237 tmp |= readl (&ep->regs->ep_irqenb);
238 writel (tmp, &ep->regs->ep_irqenb);
239 } else { /* dma, per-request */
240 tmp = (1 << (8 + ep->num)); /* completion */
241 tmp |= readl (&dev->regs->pciirqenb1);
242 writel (tmp, &dev->regs->pciirqenb1);
244 /* for short OUT transfers, dma completions can't
245 * advance the queue; do it pio-style, by hand.
246 * NOTE erratum 0112 workaround #2
248 if ((desc->bEndpointAddress & USB_DIR_IN) == 0) {
249 tmp = (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT_ENABLE);
250 writel (tmp, &ep->regs->ep_irqenb);
252 tmp = (1 << ep->num) | readl (&dev->regs->pciirqenb0);
253 writel (tmp, &dev->regs->pciirqenb0);
257 tmp = desc->bEndpointAddress;
258 DEBUG (dev, "enabled %s (ep%d%s-%s) %s max %04x\n",
259 _ep->name, tmp & 0x0f, DIR_STRING (tmp),
260 type_string (desc->bmAttributes),
261 ep->dma ? "dma" : "pio", max);
263 /* pci writes may still be posted */
264 spin_unlock_irqrestore (&dev->lock, flags);
265 return 0;
268 static int handshake (u32 __iomem *ptr, u32 mask, u32 done, int usec)
270 u32 result;
272 do {
273 result = readl (ptr);
274 if (result == ~(u32)0) /* "device unplugged" */
275 return -ENODEV;
276 result &= mask;
277 if (result == done)
278 return 0;
279 udelay (1);
280 usec--;
281 } while (usec > 0);
282 return -ETIMEDOUT;
285 static const struct usb_ep_ops net2280_ep_ops;
287 static void ep_reset (struct net2280_regs __iomem *regs, struct net2280_ep *ep)
289 u32 tmp;
291 ep->desc = NULL;
292 INIT_LIST_HEAD (&ep->queue);
294 ep->ep.maxpacket = ~0;
295 ep->ep.ops = &net2280_ep_ops;
297 /* disable the dma, irqs, endpoint... */
298 if (ep->dma) {
299 writel (0, &ep->dma->dmactl);
300 writel ( (1 << DMA_SCATTER_GATHER_DONE_INTERRUPT)
301 | (1 << DMA_TRANSACTION_DONE_INTERRUPT)
302 | (1 << DMA_ABORT)
303 , &ep->dma->dmastat);
305 tmp = readl (&regs->pciirqenb0);
306 tmp &= ~(1 << ep->num);
307 writel (tmp, &regs->pciirqenb0);
308 } else {
309 tmp = readl (&regs->pciirqenb1);
310 tmp &= ~(1 << (8 + ep->num)); /* completion */
311 writel (tmp, &regs->pciirqenb1);
313 writel (0, &ep->regs->ep_irqenb);
315 /* init to our chosen defaults, notably so that we NAK OUT
316 * packets until the driver queues a read (+note erratum 0112)
318 if (!ep->is_in || ep->dev->pdev->device == 0x2280) {
319 tmp = (1 << SET_NAK_OUT_PACKETS_MODE)
320 | (1 << SET_NAK_OUT_PACKETS)
321 | (1 << CLEAR_EP_HIDE_STATUS_PHASE)
322 | (1 << CLEAR_INTERRUPT_MODE);
323 } else {
324 /* added for 2282 */
325 tmp = (1 << CLEAR_NAK_OUT_PACKETS_MODE)
326 | (1 << CLEAR_NAK_OUT_PACKETS)
327 | (1 << CLEAR_EP_HIDE_STATUS_PHASE)
328 | (1 << CLEAR_INTERRUPT_MODE);
331 if (ep->num != 0) {
332 tmp |= (1 << CLEAR_ENDPOINT_TOGGLE)
333 | (1 << CLEAR_ENDPOINT_HALT);
335 writel (tmp, &ep->regs->ep_rsp);
337 /* scrub most status bits, and flush any fifo state */
338 if (ep->dev->pdev->device == 0x2280)
339 tmp = (1 << FIFO_OVERFLOW)
340 | (1 << FIFO_UNDERFLOW);
341 else
342 tmp = 0;
344 writel (tmp | (1 << TIMEOUT)
345 | (1 << USB_STALL_SENT)
346 | (1 << USB_IN_NAK_SENT)
347 | (1 << USB_IN_ACK_RCVD)
348 | (1 << USB_OUT_PING_NAK_SENT)
349 | (1 << USB_OUT_ACK_SENT)
350 | (1 << FIFO_FLUSH)
351 | (1 << SHORT_PACKET_OUT_DONE_INTERRUPT)
352 | (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)
353 | (1 << DATA_PACKET_RECEIVED_INTERRUPT)
354 | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
355 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
356 | (1 << DATA_IN_TOKEN_INTERRUPT)
357 , &ep->regs->ep_stat);
359 /* fifo size is handled separately */
362 static void nuke (struct net2280_ep *);
364 static int net2280_disable (struct usb_ep *_ep)
366 struct net2280_ep *ep;
367 unsigned long flags;
369 ep = container_of (_ep, struct net2280_ep, ep);
370 if (!_ep || !ep->desc || _ep->name == ep0name)
371 return -EINVAL;
373 spin_lock_irqsave (&ep->dev->lock, flags);
374 nuke (ep);
375 ep_reset (ep->dev->regs, ep);
377 VDEBUG (ep->dev, "disabled %s %s\n",
378 ep->dma ? "dma" : "pio", _ep->name);
380 /* synch memory views with the device */
381 (void) readl (&ep->regs->ep_cfg);
383 if (use_dma && !ep->dma && ep->num >= 1 && ep->num <= 4)
384 ep->dma = &ep->dev->dma [ep->num - 1];
386 spin_unlock_irqrestore (&ep->dev->lock, flags);
387 return 0;
390 /*-------------------------------------------------------------------------*/
392 static struct usb_request *
393 net2280_alloc_request (struct usb_ep *_ep, gfp_t gfp_flags)
395 struct net2280_ep *ep;
396 struct net2280_request *req;
398 if (!_ep)
399 return NULL;
400 ep = container_of (_ep, struct net2280_ep, ep);
402 req = kzalloc(sizeof(*req), gfp_flags);
403 if (!req)
404 return NULL;
406 req->req.dma = DMA_ADDR_INVALID;
407 INIT_LIST_HEAD (&req->queue);
409 /* this dma descriptor may be swapped with the previous dummy */
410 if (ep->dma) {
411 struct net2280_dma *td;
413 td = pci_pool_alloc (ep->dev->requests, gfp_flags,
414 &req->td_dma);
415 if (!td) {
416 kfree (req);
417 return NULL;
419 td->dmacount = 0; /* not VALID */
420 td->dmaaddr = cpu_to_le32 (DMA_ADDR_INVALID);
421 td->dmadesc = td->dmaaddr;
422 req->td = td;
424 return &req->req;
427 static void
428 net2280_free_request (struct usb_ep *_ep, struct usb_request *_req)
430 struct net2280_ep *ep;
431 struct net2280_request *req;
433 ep = container_of (_ep, struct net2280_ep, ep);
434 if (!_ep || !_req)
435 return;
437 req = container_of (_req, struct net2280_request, req);
438 WARN_ON (!list_empty (&req->queue));
439 if (req->td)
440 pci_pool_free (ep->dev->requests, req->td, req->td_dma);
441 kfree (req);
444 /*-------------------------------------------------------------------------*/
446 /* load a packet into the fifo we use for usb IN transfers.
447 * works for all endpoints.
449 * NOTE: pio with ep-a..ep-d could stuff multiple packets into the fifo
450 * at a time, but this code is simpler because it knows it only writes
451 * one packet. ep-a..ep-d should use dma instead.
453 static void
454 write_fifo (struct net2280_ep *ep, struct usb_request *req)
456 struct net2280_ep_regs __iomem *regs = ep->regs;
457 u8 *buf;
458 u32 tmp;
459 unsigned count, total;
461 /* INVARIANT: fifo is currently empty. (testable) */
463 if (req) {
464 buf = req->buf + req->actual;
465 prefetch (buf);
466 total = req->length - req->actual;
467 } else {
468 total = 0;
469 buf = NULL;
472 /* write just one packet at a time */
473 count = ep->ep.maxpacket;
474 if (count > total) /* min() cannot be used on a bitfield */
475 count = total;
477 VDEBUG (ep->dev, "write %s fifo (IN) %d bytes%s req %p\n",
478 ep->ep.name, count,
479 (count != ep->ep.maxpacket) ? " (short)" : "",
480 req);
481 while (count >= 4) {
482 /* NOTE be careful if you try to align these. fifo lines
483 * should normally be full (4 bytes) and successive partial
484 * lines are ok only in certain cases.
486 tmp = get_unaligned ((u32 *)buf);
487 cpu_to_le32s (&tmp);
488 writel (tmp, &regs->ep_data);
489 buf += 4;
490 count -= 4;
493 /* last fifo entry is "short" unless we wrote a full packet.
494 * also explicitly validate last word in (periodic) transfers
495 * when maxpacket is not a multiple of 4 bytes.
497 if (count || total < ep->ep.maxpacket) {
498 tmp = count ? get_unaligned ((u32 *)buf) : count;
499 cpu_to_le32s (&tmp);
500 set_fifo_bytecount (ep, count & 0x03);
501 writel (tmp, &regs->ep_data);
504 /* pci writes may still be posted */
507 /* work around erratum 0106: PCI and USB race over the OUT fifo.
508 * caller guarantees chiprev 0100, out endpoint is NAKing, and
509 * there's no real data in the fifo.
511 * NOTE: also used in cases where that erratum doesn't apply:
512 * where the host wrote "too much" data to us.
514 static void out_flush (struct net2280_ep *ep)
516 u32 __iomem *statp;
517 u32 tmp;
519 ASSERT_OUT_NAKING (ep);
521 statp = &ep->regs->ep_stat;
522 writel ( (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
523 | (1 << DATA_PACKET_RECEIVED_INTERRUPT)
524 , statp);
525 writel ((1 << FIFO_FLUSH), statp);
526 mb ();
527 tmp = readl (statp);
528 if (tmp & (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
529 /* high speed did bulk NYET; fifo isn't filling */
530 && ep->dev->gadget.speed == USB_SPEED_FULL) {
531 unsigned usec;
533 usec = 50; /* 64 byte bulk/interrupt */
534 handshake (statp, (1 << USB_OUT_PING_NAK_SENT),
535 (1 << USB_OUT_PING_NAK_SENT), usec);
536 /* NAK done; now CLEAR_NAK_OUT_PACKETS is safe */
540 /* unload packet(s) from the fifo we use for usb OUT transfers.
541 * returns true iff the request completed, because of short packet
542 * or the request buffer having filled with full packets.
544 * for ep-a..ep-d this will read multiple packets out when they
545 * have been accepted.
547 static int
548 read_fifo (struct net2280_ep *ep, struct net2280_request *req)
550 struct net2280_ep_regs __iomem *regs = ep->regs;
551 u8 *buf = req->req.buf + req->req.actual;
552 unsigned count, tmp, is_short;
553 unsigned cleanup = 0, prevent = 0;
555 /* erratum 0106 ... packets coming in during fifo reads might
556 * be incompletely rejected. not all cases have workarounds.
558 if (ep->dev->chiprev == 0x0100
559 && ep->dev->gadget.speed == USB_SPEED_FULL) {
560 udelay (1);
561 tmp = readl (&ep->regs->ep_stat);
562 if ((tmp & (1 << NAK_OUT_PACKETS)))
563 cleanup = 1;
564 else if ((tmp & (1 << FIFO_FULL))) {
565 start_out_naking (ep);
566 prevent = 1;
568 /* else: hope we don't see the problem */
571 /* never overflow the rx buffer. the fifo reads packets until
572 * it sees a short one; we might not be ready for them all.
574 prefetchw (buf);
575 count = readl (&regs->ep_avail);
576 if (unlikely (count == 0)) {
577 udelay (1);
578 tmp = readl (&ep->regs->ep_stat);
579 count = readl (&regs->ep_avail);
580 /* handled that data already? */
581 if (count == 0 && (tmp & (1 << NAK_OUT_PACKETS)) == 0)
582 return 0;
585 tmp = req->req.length - req->req.actual;
586 if (count > tmp) {
587 /* as with DMA, data overflow gets flushed */
588 if ((tmp % ep->ep.maxpacket) != 0) {
589 ERROR (ep->dev,
590 "%s out fifo %d bytes, expected %d\n",
591 ep->ep.name, count, tmp);
592 req->req.status = -EOVERFLOW;
593 cleanup = 1;
594 /* NAK_OUT_PACKETS will be set, so flushing is safe;
595 * the next read will start with the next packet
597 } /* else it's a ZLP, no worries */
598 count = tmp;
600 req->req.actual += count;
602 is_short = (count == 0) || ((count % ep->ep.maxpacket) != 0);
604 VDEBUG (ep->dev, "read %s fifo (OUT) %d bytes%s%s%s req %p %d/%d\n",
605 ep->ep.name, count, is_short ? " (short)" : "",
606 cleanup ? " flush" : "", prevent ? " nak" : "",
607 req, req->req.actual, req->req.length);
609 while (count >= 4) {
610 tmp = readl (&regs->ep_data);
611 cpu_to_le32s (&tmp);
612 put_unaligned (tmp, (u32 *)buf);
613 buf += 4;
614 count -= 4;
616 if (count) {
617 tmp = readl (&regs->ep_data);
618 /* LE conversion is implicit here: */
619 do {
620 *buf++ = (u8) tmp;
621 tmp >>= 8;
622 } while (--count);
624 if (cleanup)
625 out_flush (ep);
626 if (prevent) {
627 writel ((1 << CLEAR_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
628 (void) readl (&ep->regs->ep_rsp);
631 return is_short || ((req->req.actual == req->req.length)
632 && !req->req.zero);
635 /* fill out dma descriptor to match a given request */
636 static void
637 fill_dma_desc (struct net2280_ep *ep, struct net2280_request *req, int valid)
639 struct net2280_dma *td = req->td;
640 u32 dmacount = req->req.length;
642 /* don't let DMA continue after a short OUT packet,
643 * so overruns can't affect the next transfer.
644 * in case of overruns on max-size packets, we can't
645 * stop the fifo from filling but we can flush it.
647 if (ep->is_in)
648 dmacount |= (1 << DMA_DIRECTION);
649 if ((!ep->is_in && (dmacount % ep->ep.maxpacket) != 0)
650 || ep->dev->pdev->device != 0x2280)
651 dmacount |= (1 << END_OF_CHAIN);
653 req->valid = valid;
654 if (valid)
655 dmacount |= (1 << VALID_BIT);
656 if (likely(!req->req.no_interrupt || !use_dma_chaining))
657 dmacount |= (1 << DMA_DONE_INTERRUPT_ENABLE);
659 /* td->dmadesc = previously set by caller */
660 td->dmaaddr = cpu_to_le32 (req->req.dma);
662 /* 2280 may be polling VALID_BIT through ep->dma->dmadesc */
663 wmb ();
664 td->dmacount = cpu_to_le32(dmacount);
667 static const u32 dmactl_default =
668 (1 << DMA_SCATTER_GATHER_DONE_INTERRUPT)
669 | (1 << DMA_CLEAR_COUNT_ENABLE)
670 /* erratum 0116 workaround part 1 (use POLLING) */
671 | (POLL_100_USEC << DESCRIPTOR_POLLING_RATE)
672 | (1 << DMA_VALID_BIT_POLLING_ENABLE)
673 | (1 << DMA_VALID_BIT_ENABLE)
674 | (1 << DMA_SCATTER_GATHER_ENABLE)
675 /* erratum 0116 workaround part 2 (no AUTOSTART) */
676 | (1 << DMA_ENABLE);
678 static inline void spin_stop_dma (struct net2280_dma_regs __iomem *dma)
680 handshake (&dma->dmactl, (1 << DMA_ENABLE), 0, 50);
683 static inline void stop_dma (struct net2280_dma_regs __iomem *dma)
685 writel (readl (&dma->dmactl) & ~(1 << DMA_ENABLE), &dma->dmactl);
686 spin_stop_dma (dma);
689 static void start_queue (struct net2280_ep *ep, u32 dmactl, u32 td_dma)
691 struct net2280_dma_regs __iomem *dma = ep->dma;
692 unsigned int tmp = (1 << VALID_BIT) | (ep->is_in << DMA_DIRECTION);
694 if (ep->dev->pdev->device != 0x2280)
695 tmp |= (1 << END_OF_CHAIN);
697 writel (tmp, &dma->dmacount);
698 writel (readl (&dma->dmastat), &dma->dmastat);
700 writel (td_dma, &dma->dmadesc);
701 writel (dmactl, &dma->dmactl);
703 /* erratum 0116 workaround part 3: pci arbiter away from net2280 */
704 (void) readl (&ep->dev->pci->pcimstctl);
706 writel ((1 << DMA_START), &dma->dmastat);
708 if (!ep->is_in)
709 stop_out_naking (ep);
712 static void start_dma (struct net2280_ep *ep, struct net2280_request *req)
714 u32 tmp;
715 struct net2280_dma_regs __iomem *dma = ep->dma;
717 /* FIXME can't use DMA for ZLPs */
719 /* on this path we "know" there's no dma active (yet) */
720 WARN_ON (readl (&dma->dmactl) & (1 << DMA_ENABLE));
721 writel (0, &ep->dma->dmactl);
723 /* previous OUT packet might have been short */
724 if (!ep->is_in && ((tmp = readl (&ep->regs->ep_stat))
725 & (1 << NAK_OUT_PACKETS)) != 0) {
726 writel ((1 << SHORT_PACKET_TRANSFERRED_INTERRUPT),
727 &ep->regs->ep_stat);
729 tmp = readl (&ep->regs->ep_avail);
730 if (tmp) {
731 writel (readl (&dma->dmastat), &dma->dmastat);
733 /* transfer all/some fifo data */
734 writel (req->req.dma, &dma->dmaaddr);
735 tmp = min (tmp, req->req.length);
737 /* dma irq, faking scatterlist status */
738 req->td->dmacount = cpu_to_le32 (req->req.length - tmp);
739 writel ((1 << DMA_DONE_INTERRUPT_ENABLE)
740 | tmp, &dma->dmacount);
741 req->td->dmadesc = 0;
742 req->valid = 1;
744 writel ((1 << DMA_ENABLE), &dma->dmactl);
745 writel ((1 << DMA_START), &dma->dmastat);
746 return;
750 tmp = dmactl_default;
752 /* force packet boundaries between dma requests, but prevent the
753 * controller from automagically writing a last "short" packet
754 * (zero length) unless the driver explicitly said to do that.
756 if (ep->is_in) {
757 if (likely ((req->req.length % ep->ep.maxpacket) != 0
758 || req->req.zero)) {
759 tmp |= (1 << DMA_FIFO_VALIDATE);
760 ep->in_fifo_validate = 1;
761 } else
762 ep->in_fifo_validate = 0;
765 /* init req->td, pointing to the current dummy */
766 req->td->dmadesc = cpu_to_le32 (ep->td_dma);
767 fill_dma_desc (ep, req, 1);
769 if (!use_dma_chaining)
770 req->td->dmacount |= cpu_to_le32 (1 << END_OF_CHAIN);
772 start_queue (ep, tmp, req->td_dma);
775 static inline void
776 queue_dma (struct net2280_ep *ep, struct net2280_request *req, int valid)
778 struct net2280_dma *end;
779 dma_addr_t tmp;
781 /* swap new dummy for old, link; fill and maybe activate */
782 end = ep->dummy;
783 ep->dummy = req->td;
784 req->td = end;
786 tmp = ep->td_dma;
787 ep->td_dma = req->td_dma;
788 req->td_dma = tmp;
790 end->dmadesc = cpu_to_le32 (ep->td_dma);
792 fill_dma_desc (ep, req, valid);
795 static void
796 done (struct net2280_ep *ep, struct net2280_request *req, int status)
798 struct net2280 *dev;
799 unsigned stopped = ep->stopped;
801 list_del_init (&req->queue);
803 if (req->req.status == -EINPROGRESS)
804 req->req.status = status;
805 else
806 status = req->req.status;
808 dev = ep->dev;
809 if (req->mapped) {
810 pci_unmap_single (dev->pdev, req->req.dma, req->req.length,
811 ep->is_in ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
812 req->req.dma = DMA_ADDR_INVALID;
813 req->mapped = 0;
816 if (status && status != -ESHUTDOWN)
817 VDEBUG (dev, "complete %s req %p stat %d len %u/%u\n",
818 ep->ep.name, &req->req, status,
819 req->req.actual, req->req.length);
821 /* don't modify queue heads during completion callback */
822 ep->stopped = 1;
823 spin_unlock (&dev->lock);
824 req->req.complete (&ep->ep, &req->req);
825 spin_lock (&dev->lock);
826 ep->stopped = stopped;
829 /*-------------------------------------------------------------------------*/
831 static int
832 net2280_queue (struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
834 struct net2280_request *req;
835 struct net2280_ep *ep;
836 struct net2280 *dev;
837 unsigned long flags;
839 /* we always require a cpu-view buffer, so that we can
840 * always use pio (as fallback or whatever).
842 req = container_of (_req, struct net2280_request, req);
843 if (!_req || !_req->complete || !_req->buf
844 || !list_empty (&req->queue))
845 return -EINVAL;
846 if (_req->length > (~0 & DMA_BYTE_COUNT_MASK))
847 return -EDOM;
848 ep = container_of (_ep, struct net2280_ep, ep);
849 if (!_ep || (!ep->desc && ep->num != 0))
850 return -EINVAL;
851 dev = ep->dev;
852 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
853 return -ESHUTDOWN;
855 /* FIXME implement PIO fallback for ZLPs with DMA */
856 if (ep->dma && _req->length == 0)
857 return -EOPNOTSUPP;
859 /* set up dma mapping in case the caller didn't */
860 if (ep->dma && _req->dma == DMA_ADDR_INVALID) {
861 _req->dma = pci_map_single (dev->pdev, _req->buf, _req->length,
862 ep->is_in ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
863 req->mapped = 1;
866 #if 0
867 VDEBUG (dev, "%s queue req %p, len %d buf %p\n",
868 _ep->name, _req, _req->length, _req->buf);
869 #endif
871 spin_lock_irqsave (&dev->lock, flags);
873 _req->status = -EINPROGRESS;
874 _req->actual = 0;
876 /* kickstart this i/o queue? */
877 if (list_empty (&ep->queue) && !ep->stopped) {
878 /* use DMA if the endpoint supports it, else pio */
879 if (ep->dma)
880 start_dma (ep, req);
881 else {
882 /* maybe there's no control data, just status ack */
883 if (ep->num == 0 && _req->length == 0) {
884 allow_status (ep);
885 done (ep, req, 0);
886 VDEBUG (dev, "%s status ack\n", ep->ep.name);
887 goto done;
890 /* PIO ... stuff the fifo, or unblock it. */
891 if (ep->is_in)
892 write_fifo (ep, _req);
893 else if (list_empty (&ep->queue)) {
894 u32 s;
896 /* OUT FIFO might have packet(s) buffered */
897 s = readl (&ep->regs->ep_stat);
898 if ((s & (1 << FIFO_EMPTY)) == 0) {
899 /* note: _req->short_not_ok is
900 * ignored here since PIO _always_
901 * stops queue advance here, and
902 * _req->status doesn't change for
903 * short reads (only _req->actual)
905 if (read_fifo (ep, req)) {
906 done (ep, req, 0);
907 if (ep->num == 0)
908 allow_status (ep);
909 /* don't queue it */
910 req = NULL;
911 } else
912 s = readl (&ep->regs->ep_stat);
915 /* don't NAK, let the fifo fill */
916 if (req && (s & (1 << NAK_OUT_PACKETS)))
917 writel ((1 << CLEAR_NAK_OUT_PACKETS),
918 &ep->regs->ep_rsp);
922 } else if (ep->dma) {
923 int valid = 1;
925 if (ep->is_in) {
926 int expect;
928 /* preventing magic zlps is per-engine state, not
929 * per-transfer; irq logic must recover hiccups.
931 expect = likely (req->req.zero
932 || (req->req.length % ep->ep.maxpacket) != 0);
933 if (expect != ep->in_fifo_validate)
934 valid = 0;
936 queue_dma (ep, req, valid);
938 } /* else the irq handler advances the queue. */
940 ep->responded = 1;
941 if (req)
942 list_add_tail (&req->queue, &ep->queue);
943 done:
944 spin_unlock_irqrestore (&dev->lock, flags);
946 /* pci writes may still be posted */
947 return 0;
950 static inline void
951 dma_done (
952 struct net2280_ep *ep,
953 struct net2280_request *req,
954 u32 dmacount,
955 int status
958 req->req.actual = req->req.length - (DMA_BYTE_COUNT_MASK & dmacount);
959 done (ep, req, status);
962 static void restart_dma (struct net2280_ep *ep);
964 static void scan_dma_completions (struct net2280_ep *ep)
966 /* only look at descriptors that were "naturally" retired,
967 * so fifo and list head state won't matter
969 while (!list_empty (&ep->queue)) {
970 struct net2280_request *req;
971 u32 tmp;
973 req = list_entry (ep->queue.next,
974 struct net2280_request, queue);
975 if (!req->valid)
976 break;
977 rmb ();
978 tmp = le32_to_cpup (&req->td->dmacount);
979 if ((tmp & (1 << VALID_BIT)) != 0)
980 break;
982 /* SHORT_PACKET_TRANSFERRED_INTERRUPT handles "usb-short"
983 * cases where DMA must be aborted; this code handles
984 * all non-abort DMA completions.
986 if (unlikely (req->td->dmadesc == 0)) {
987 /* paranoia */
988 tmp = readl (&ep->dma->dmacount);
989 if (tmp & DMA_BYTE_COUNT_MASK)
990 break;
991 /* single transfer mode */
992 dma_done (ep, req, tmp, 0);
993 break;
994 } else if (!ep->is_in
995 && (req->req.length % ep->ep.maxpacket) != 0) {
996 tmp = readl (&ep->regs->ep_stat);
998 /* AVOID TROUBLE HERE by not issuing short reads from
999 * your gadget driver. That helps avoids errata 0121,
1000 * 0122, and 0124; not all cases trigger the warning.
1002 if ((tmp & (1 << NAK_OUT_PACKETS)) == 0) {
1003 WARNING (ep->dev, "%s lost packet sync!\n",
1004 ep->ep.name);
1005 req->req.status = -EOVERFLOW;
1006 } else if ((tmp = readl (&ep->regs->ep_avail)) != 0) {
1007 /* fifo gets flushed later */
1008 ep->out_overflow = 1;
1009 DEBUG (ep->dev, "%s dma, discard %d len %d\n",
1010 ep->ep.name, tmp,
1011 req->req.length);
1012 req->req.status = -EOVERFLOW;
1015 dma_done (ep, req, tmp, 0);
1019 static void restart_dma (struct net2280_ep *ep)
1021 struct net2280_request *req;
1022 u32 dmactl = dmactl_default;
1024 if (ep->stopped)
1025 return;
1026 req = list_entry (ep->queue.next, struct net2280_request, queue);
1028 if (!use_dma_chaining) {
1029 start_dma (ep, req);
1030 return;
1033 /* the 2280 will be processing the queue unless queue hiccups after
1034 * the previous transfer:
1035 * IN: wanted automagic zlp, head doesn't (or vice versa)
1036 * DMA_FIFO_VALIDATE doesn't init from dma descriptors.
1037 * OUT: was "usb-short", we must restart.
1039 if (ep->is_in && !req->valid) {
1040 struct net2280_request *entry, *prev = NULL;
1041 int reqmode, done = 0;
1043 DEBUG (ep->dev, "%s dma hiccup td %p\n", ep->ep.name, req->td);
1044 ep->in_fifo_validate = likely (req->req.zero
1045 || (req->req.length % ep->ep.maxpacket) != 0);
1046 if (ep->in_fifo_validate)
1047 dmactl |= (1 << DMA_FIFO_VALIDATE);
1048 list_for_each_entry (entry, &ep->queue, queue) {
1049 __le32 dmacount;
1051 if (entry == req)
1052 continue;
1053 dmacount = entry->td->dmacount;
1054 if (!done) {
1055 reqmode = likely (entry->req.zero
1056 || (entry->req.length
1057 % ep->ep.maxpacket) != 0);
1058 if (reqmode == ep->in_fifo_validate) {
1059 entry->valid = 1;
1060 dmacount |= valid_bit;
1061 entry->td->dmacount = dmacount;
1062 prev = entry;
1063 continue;
1064 } else {
1065 /* force a hiccup */
1066 prev->td->dmacount |= dma_done_ie;
1067 done = 1;
1071 /* walk the rest of the queue so unlinks behave */
1072 entry->valid = 0;
1073 dmacount &= ~valid_bit;
1074 entry->td->dmacount = dmacount;
1075 prev = entry;
1079 writel (0, &ep->dma->dmactl);
1080 start_queue (ep, dmactl, req->td_dma);
1083 static void abort_dma (struct net2280_ep *ep)
1085 /* abort the current transfer */
1086 if (likely (!list_empty (&ep->queue))) {
1087 /* FIXME work around errata 0121, 0122, 0124 */
1088 writel ((1 << DMA_ABORT), &ep->dma->dmastat);
1089 spin_stop_dma (ep->dma);
1090 } else
1091 stop_dma (ep->dma);
1092 scan_dma_completions (ep);
1095 /* dequeue ALL requests */
1096 static void nuke (struct net2280_ep *ep)
1098 struct net2280_request *req;
1100 /* called with spinlock held */
1101 ep->stopped = 1;
1102 if (ep->dma)
1103 abort_dma (ep);
1104 while (!list_empty (&ep->queue)) {
1105 req = list_entry (ep->queue.next,
1106 struct net2280_request,
1107 queue);
1108 done (ep, req, -ESHUTDOWN);
1112 /* dequeue JUST ONE request */
1113 static int net2280_dequeue (struct usb_ep *_ep, struct usb_request *_req)
1115 struct net2280_ep *ep;
1116 struct net2280_request *req;
1117 unsigned long flags;
1118 u32 dmactl;
1119 int stopped;
1121 ep = container_of (_ep, struct net2280_ep, ep);
1122 if (!_ep || (!ep->desc && ep->num != 0) || !_req)
1123 return -EINVAL;
1125 spin_lock_irqsave (&ep->dev->lock, flags);
1126 stopped = ep->stopped;
1128 /* quiesce dma while we patch the queue */
1129 dmactl = 0;
1130 ep->stopped = 1;
1131 if (ep->dma) {
1132 dmactl = readl (&ep->dma->dmactl);
1133 /* WARNING erratum 0127 may kick in ... */
1134 stop_dma (ep->dma);
1135 scan_dma_completions (ep);
1138 /* make sure it's still queued on this endpoint */
1139 list_for_each_entry (req, &ep->queue, queue) {
1140 if (&req->req == _req)
1141 break;
1143 if (&req->req != _req) {
1144 spin_unlock_irqrestore (&ep->dev->lock, flags);
1145 return -EINVAL;
1148 /* queue head may be partially complete. */
1149 if (ep->queue.next == &req->queue) {
1150 if (ep->dma) {
1151 DEBUG (ep->dev, "unlink (%s) dma\n", _ep->name);
1152 _req->status = -ECONNRESET;
1153 abort_dma (ep);
1154 if (likely (ep->queue.next == &req->queue)) {
1155 // NOTE: misreports single-transfer mode
1156 req->td->dmacount = 0; /* invalidate */
1157 dma_done (ep, req,
1158 readl (&ep->dma->dmacount),
1159 -ECONNRESET);
1161 } else {
1162 DEBUG (ep->dev, "unlink (%s) pio\n", _ep->name);
1163 done (ep, req, -ECONNRESET);
1165 req = NULL;
1167 /* patch up hardware chaining data */
1168 } else if (ep->dma && use_dma_chaining) {
1169 if (req->queue.prev == ep->queue.next) {
1170 writel (le32_to_cpu (req->td->dmadesc),
1171 &ep->dma->dmadesc);
1172 if (req->td->dmacount & dma_done_ie)
1173 writel (readl (&ep->dma->dmacount)
1174 | le32_to_cpu(dma_done_ie),
1175 &ep->dma->dmacount);
1176 } else {
1177 struct net2280_request *prev;
1179 prev = list_entry (req->queue.prev,
1180 struct net2280_request, queue);
1181 prev->td->dmadesc = req->td->dmadesc;
1182 if (req->td->dmacount & dma_done_ie)
1183 prev->td->dmacount |= dma_done_ie;
1187 if (req)
1188 done (ep, req, -ECONNRESET);
1189 ep->stopped = stopped;
1191 if (ep->dma) {
1192 /* turn off dma on inactive queues */
1193 if (list_empty (&ep->queue))
1194 stop_dma (ep->dma);
1195 else if (!ep->stopped) {
1196 /* resume current request, or start new one */
1197 if (req)
1198 writel (dmactl, &ep->dma->dmactl);
1199 else
1200 start_dma (ep, list_entry (ep->queue.next,
1201 struct net2280_request, queue));
1205 spin_unlock_irqrestore (&ep->dev->lock, flags);
1206 return 0;
1209 /*-------------------------------------------------------------------------*/
1211 static int net2280_fifo_status (struct usb_ep *_ep);
1213 static int
1214 net2280_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedged)
1216 struct net2280_ep *ep;
1217 unsigned long flags;
1218 int retval = 0;
1220 ep = container_of (_ep, struct net2280_ep, ep);
1221 if (!_ep || (!ep->desc && ep->num != 0))
1222 return -EINVAL;
1223 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1224 return -ESHUTDOWN;
1225 if (ep->desc /* not ep0 */ && (ep->desc->bmAttributes & 0x03)
1226 == USB_ENDPOINT_XFER_ISOC)
1227 return -EINVAL;
1229 spin_lock_irqsave (&ep->dev->lock, flags);
1230 if (!list_empty (&ep->queue))
1231 retval = -EAGAIN;
1232 else if (ep->is_in && value && net2280_fifo_status (_ep) != 0)
1233 retval = -EAGAIN;
1234 else {
1235 VDEBUG (ep->dev, "%s %s %s\n", _ep->name,
1236 value ? "set" : "clear",
1237 wedged ? "wedge" : "halt");
1238 /* set/clear, then synch memory views with the device */
1239 if (value) {
1240 if (ep->num == 0)
1241 ep->dev->protocol_stall = 1;
1242 else
1243 set_halt (ep);
1244 if (wedged)
1245 ep->wedged = 1;
1246 } else {
1247 clear_halt (ep);
1248 ep->wedged = 0;
1250 (void) readl (&ep->regs->ep_rsp);
1252 spin_unlock_irqrestore (&ep->dev->lock, flags);
1254 return retval;
1257 static int
1258 net2280_set_halt(struct usb_ep *_ep, int value)
1260 return net2280_set_halt_and_wedge(_ep, value, 0);
1263 static int
1264 net2280_set_wedge(struct usb_ep *_ep)
1266 if (!_ep || _ep->name == ep0name)
1267 return -EINVAL;
1268 return net2280_set_halt_and_wedge(_ep, 1, 1);
1271 static int
1272 net2280_fifo_status (struct usb_ep *_ep)
1274 struct net2280_ep *ep;
1275 u32 avail;
1277 ep = container_of (_ep, struct net2280_ep, ep);
1278 if (!_ep || (!ep->desc && ep->num != 0))
1279 return -ENODEV;
1280 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1281 return -ESHUTDOWN;
1283 avail = readl (&ep->regs->ep_avail) & ((1 << 12) - 1);
1284 if (avail > ep->fifo_size)
1285 return -EOVERFLOW;
1286 if (ep->is_in)
1287 avail = ep->fifo_size - avail;
1288 return avail;
1291 static void
1292 net2280_fifo_flush (struct usb_ep *_ep)
1294 struct net2280_ep *ep;
1296 ep = container_of (_ep, struct net2280_ep, ep);
1297 if (!_ep || (!ep->desc && ep->num != 0))
1298 return;
1299 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1300 return;
1302 writel ((1 << FIFO_FLUSH), &ep->regs->ep_stat);
1303 (void) readl (&ep->regs->ep_rsp);
1306 static const struct usb_ep_ops net2280_ep_ops = {
1307 .enable = net2280_enable,
1308 .disable = net2280_disable,
1310 .alloc_request = net2280_alloc_request,
1311 .free_request = net2280_free_request,
1313 .queue = net2280_queue,
1314 .dequeue = net2280_dequeue,
1316 .set_halt = net2280_set_halt,
1317 .set_wedge = net2280_set_wedge,
1318 .fifo_status = net2280_fifo_status,
1319 .fifo_flush = net2280_fifo_flush,
1322 /*-------------------------------------------------------------------------*/
1324 static int net2280_get_frame (struct usb_gadget *_gadget)
1326 struct net2280 *dev;
1327 unsigned long flags;
1328 u16 retval;
1330 if (!_gadget)
1331 return -ENODEV;
1332 dev = container_of (_gadget, struct net2280, gadget);
1333 spin_lock_irqsave (&dev->lock, flags);
1334 retval = get_idx_reg (dev->regs, REG_FRAME) & 0x03ff;
1335 spin_unlock_irqrestore (&dev->lock, flags);
1336 return retval;
1339 static int net2280_wakeup (struct usb_gadget *_gadget)
1341 struct net2280 *dev;
1342 u32 tmp;
1343 unsigned long flags;
1345 if (!_gadget)
1346 return 0;
1347 dev = container_of (_gadget, struct net2280, gadget);
1349 spin_lock_irqsave (&dev->lock, flags);
1350 tmp = readl (&dev->usb->usbctl);
1351 if (tmp & (1 << DEVICE_REMOTE_WAKEUP_ENABLE))
1352 writel (1 << GENERATE_RESUME, &dev->usb->usbstat);
1353 spin_unlock_irqrestore (&dev->lock, flags);
1355 /* pci writes may still be posted */
1356 return 0;
1359 static int net2280_set_selfpowered (struct usb_gadget *_gadget, int value)
1361 struct net2280 *dev;
1362 u32 tmp;
1363 unsigned long flags;
1365 if (!_gadget)
1366 return 0;
1367 dev = container_of (_gadget, struct net2280, gadget);
1369 spin_lock_irqsave (&dev->lock, flags);
1370 tmp = readl (&dev->usb->usbctl);
1371 if (value)
1372 tmp |= (1 << SELF_POWERED_STATUS);
1373 else
1374 tmp &= ~(1 << SELF_POWERED_STATUS);
1375 writel (tmp, &dev->usb->usbctl);
1376 spin_unlock_irqrestore (&dev->lock, flags);
1378 return 0;
1381 static int net2280_pullup(struct usb_gadget *_gadget, int is_on)
1383 struct net2280 *dev;
1384 u32 tmp;
1385 unsigned long flags;
1387 if (!_gadget)
1388 return -ENODEV;
1389 dev = container_of (_gadget, struct net2280, gadget);
1391 spin_lock_irqsave (&dev->lock, flags);
1392 tmp = readl (&dev->usb->usbctl);
1393 dev->softconnect = (is_on != 0);
1394 if (is_on)
1395 tmp |= (1 << USB_DETECT_ENABLE);
1396 else
1397 tmp &= ~(1 << USB_DETECT_ENABLE);
1398 writel (tmp, &dev->usb->usbctl);
1399 spin_unlock_irqrestore (&dev->lock, flags);
1401 return 0;
1404 static int net2280_start(struct usb_gadget_driver *driver,
1405 int (*bind)(struct usb_gadget *));
1406 static int net2280_stop(struct usb_gadget_driver *driver);
1408 static const struct usb_gadget_ops net2280_ops = {
1409 .get_frame = net2280_get_frame,
1410 .wakeup = net2280_wakeup,
1411 .set_selfpowered = net2280_set_selfpowered,
1412 .pullup = net2280_pullup,
1413 .start = net2280_start,
1414 .stop = net2280_stop,
1417 /*-------------------------------------------------------------------------*/
1419 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1421 /* FIXME move these into procfs, and use seq_file.
1422 * Sysfs _still_ doesn't behave for arbitrarily sized files,
1423 * and also doesn't help products using this with 2.4 kernels.
1426 /* "function" sysfs attribute */
1427 static ssize_t
1428 show_function (struct device *_dev, struct device_attribute *attr, char *buf)
1430 struct net2280 *dev = dev_get_drvdata (_dev);
1432 if (!dev->driver
1433 || !dev->driver->function
1434 || strlen (dev->driver->function) > PAGE_SIZE)
1435 return 0;
1436 return scnprintf (buf, PAGE_SIZE, "%s\n", dev->driver->function);
1438 static DEVICE_ATTR (function, S_IRUGO, show_function, NULL);
1440 static ssize_t net2280_show_registers(struct device *_dev,
1441 struct device_attribute *attr, char *buf)
1443 struct net2280 *dev;
1444 char *next;
1445 unsigned size, t;
1446 unsigned long flags;
1447 int i;
1448 u32 t1, t2;
1449 const char *s;
1451 dev = dev_get_drvdata (_dev);
1452 next = buf;
1453 size = PAGE_SIZE;
1454 spin_lock_irqsave (&dev->lock, flags);
1456 if (dev->driver)
1457 s = dev->driver->driver.name;
1458 else
1459 s = "(none)";
1461 /* Main Control Registers */
1462 t = scnprintf (next, size, "%s version " DRIVER_VERSION
1463 ", chiprev %04x, dma %s\n\n"
1464 "devinit %03x fifoctl %08x gadget '%s'\n"
1465 "pci irqenb0 %02x irqenb1 %08x "
1466 "irqstat0 %04x irqstat1 %08x\n",
1467 driver_name, dev->chiprev,
1468 use_dma
1469 ? (use_dma_chaining ? "chaining" : "enabled")
1470 : "disabled",
1471 readl (&dev->regs->devinit),
1472 readl (&dev->regs->fifoctl),
1474 readl (&dev->regs->pciirqenb0),
1475 readl (&dev->regs->pciirqenb1),
1476 readl (&dev->regs->irqstat0),
1477 readl (&dev->regs->irqstat1));
1478 size -= t;
1479 next += t;
1481 /* USB Control Registers */
1482 t1 = readl (&dev->usb->usbctl);
1483 t2 = readl (&dev->usb->usbstat);
1484 if (t1 & (1 << VBUS_PIN)) {
1485 if (t2 & (1 << HIGH_SPEED))
1486 s = "high speed";
1487 else if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1488 s = "powered";
1489 else
1490 s = "full speed";
1491 /* full speed bit (6) not working?? */
1492 } else
1493 s = "not attached";
1494 t = scnprintf (next, size,
1495 "stdrsp %08x usbctl %08x usbstat %08x "
1496 "addr 0x%02x (%s)\n",
1497 readl (&dev->usb->stdrsp), t1, t2,
1498 readl (&dev->usb->ouraddr), s);
1499 size -= t;
1500 next += t;
1502 /* PCI Master Control Registers */
1504 /* DMA Control Registers */
1506 /* Configurable EP Control Registers */
1507 for (i = 0; i < 7; i++) {
1508 struct net2280_ep *ep;
1510 ep = &dev->ep [i];
1511 if (i && !ep->desc)
1512 continue;
1514 t1 = readl (&ep->regs->ep_cfg);
1515 t2 = readl (&ep->regs->ep_rsp) & 0xff;
1516 t = scnprintf (next, size,
1517 "\n%s\tcfg %05x rsp (%02x) %s%s%s%s%s%s%s%s"
1518 "irqenb %02x\n",
1519 ep->ep.name, t1, t2,
1520 (t2 & (1 << CLEAR_NAK_OUT_PACKETS))
1521 ? "NAK " : "",
1522 (t2 & (1 << CLEAR_EP_HIDE_STATUS_PHASE))
1523 ? "hide " : "",
1524 (t2 & (1 << CLEAR_EP_FORCE_CRC_ERROR))
1525 ? "CRC " : "",
1526 (t2 & (1 << CLEAR_INTERRUPT_MODE))
1527 ? "interrupt " : "",
1528 (t2 & (1<<CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE))
1529 ? "status " : "",
1530 (t2 & (1 << CLEAR_NAK_OUT_PACKETS_MODE))
1531 ? "NAKmode " : "",
1532 (t2 & (1 << CLEAR_ENDPOINT_TOGGLE))
1533 ? "DATA1 " : "DATA0 ",
1534 (t2 & (1 << CLEAR_ENDPOINT_HALT))
1535 ? "HALT " : "",
1536 readl (&ep->regs->ep_irqenb));
1537 size -= t;
1538 next += t;
1540 t = scnprintf (next, size,
1541 "\tstat %08x avail %04x "
1542 "(ep%d%s-%s)%s\n",
1543 readl (&ep->regs->ep_stat),
1544 readl (&ep->regs->ep_avail),
1545 t1 & 0x0f, DIR_STRING (t1),
1546 type_string (t1 >> 8),
1547 ep->stopped ? "*" : "");
1548 size -= t;
1549 next += t;
1551 if (!ep->dma)
1552 continue;
1554 t = scnprintf (next, size,
1555 " dma\tctl %08x stat %08x count %08x\n"
1556 "\taddr %08x desc %08x\n",
1557 readl (&ep->dma->dmactl),
1558 readl (&ep->dma->dmastat),
1559 readl (&ep->dma->dmacount),
1560 readl (&ep->dma->dmaaddr),
1561 readl (&ep->dma->dmadesc));
1562 size -= t;
1563 next += t;
1567 /* Indexed Registers */
1568 // none yet
1570 /* Statistics */
1571 t = scnprintf (next, size, "\nirqs: ");
1572 size -= t;
1573 next += t;
1574 for (i = 0; i < 7; i++) {
1575 struct net2280_ep *ep;
1577 ep = &dev->ep [i];
1578 if (i && !ep->irqs)
1579 continue;
1580 t = scnprintf (next, size, " %s/%lu", ep->ep.name, ep->irqs);
1581 size -= t;
1582 next += t;
1585 t = scnprintf (next, size, "\n");
1586 size -= t;
1587 next += t;
1589 spin_unlock_irqrestore (&dev->lock, flags);
1591 return PAGE_SIZE - size;
1593 static DEVICE_ATTR(registers, S_IRUGO, net2280_show_registers, NULL);
1595 static ssize_t
1596 show_queues (struct device *_dev, struct device_attribute *attr, char *buf)
1598 struct net2280 *dev;
1599 char *next;
1600 unsigned size;
1601 unsigned long flags;
1602 int i;
1604 dev = dev_get_drvdata (_dev);
1605 next = buf;
1606 size = PAGE_SIZE;
1607 spin_lock_irqsave (&dev->lock, flags);
1609 for (i = 0; i < 7; i++) {
1610 struct net2280_ep *ep = &dev->ep [i];
1611 struct net2280_request *req;
1612 int t;
1614 if (i != 0) {
1615 const struct usb_endpoint_descriptor *d;
1617 d = ep->desc;
1618 if (!d)
1619 continue;
1620 t = d->bEndpointAddress;
1621 t = scnprintf (next, size,
1622 "\n%s (ep%d%s-%s) max %04x %s fifo %d\n",
1623 ep->ep.name, t & USB_ENDPOINT_NUMBER_MASK,
1624 (t & USB_DIR_IN) ? "in" : "out",
1625 ({ char *val;
1626 switch (d->bmAttributes & 0x03) {
1627 case USB_ENDPOINT_XFER_BULK:
1628 val = "bulk"; break;
1629 case USB_ENDPOINT_XFER_INT:
1630 val = "intr"; break;
1631 default:
1632 val = "iso"; break;
1633 }; val; }),
1634 usb_endpoint_maxp (d) & 0x1fff,
1635 ep->dma ? "dma" : "pio", ep->fifo_size
1637 } else /* ep0 should only have one transfer queued */
1638 t = scnprintf (next, size, "ep0 max 64 pio %s\n",
1639 ep->is_in ? "in" : "out");
1640 if (t <= 0 || t > size)
1641 goto done;
1642 size -= t;
1643 next += t;
1645 if (list_empty (&ep->queue)) {
1646 t = scnprintf (next, size, "\t(nothing queued)\n");
1647 if (t <= 0 || t > size)
1648 goto done;
1649 size -= t;
1650 next += t;
1651 continue;
1653 list_for_each_entry (req, &ep->queue, queue) {
1654 if (ep->dma && req->td_dma == readl (&ep->dma->dmadesc))
1655 t = scnprintf (next, size,
1656 "\treq %p len %d/%d "
1657 "buf %p (dmacount %08x)\n",
1658 &req->req, req->req.actual,
1659 req->req.length, req->req.buf,
1660 readl (&ep->dma->dmacount));
1661 else
1662 t = scnprintf (next, size,
1663 "\treq %p len %d/%d buf %p\n",
1664 &req->req, req->req.actual,
1665 req->req.length, req->req.buf);
1666 if (t <= 0 || t > size)
1667 goto done;
1668 size -= t;
1669 next += t;
1671 if (ep->dma) {
1672 struct net2280_dma *td;
1674 td = req->td;
1675 t = scnprintf (next, size, "\t td %08x "
1676 " count %08x buf %08x desc %08x\n",
1677 (u32) req->td_dma,
1678 le32_to_cpu (td->dmacount),
1679 le32_to_cpu (td->dmaaddr),
1680 le32_to_cpu (td->dmadesc));
1681 if (t <= 0 || t > size)
1682 goto done;
1683 size -= t;
1684 next += t;
1689 done:
1690 spin_unlock_irqrestore (&dev->lock, flags);
1691 return PAGE_SIZE - size;
1693 static DEVICE_ATTR (queues, S_IRUGO, show_queues, NULL);
1696 #else
1698 #define device_create_file(a,b) (0)
1699 #define device_remove_file(a,b) do { } while (0)
1701 #endif
1703 /*-------------------------------------------------------------------------*/
1705 /* another driver-specific mode might be a request type doing dma
1706 * to/from another device fifo instead of to/from memory.
1709 static void set_fifo_mode (struct net2280 *dev, int mode)
1711 /* keeping high bits preserves BAR2 */
1712 writel ((0xffff << PCI_BASE2_RANGE) | mode, &dev->regs->fifoctl);
1714 /* always ep-{a,b,e,f} ... maybe not ep-c or ep-d */
1715 INIT_LIST_HEAD (&dev->gadget.ep_list);
1716 list_add_tail (&dev->ep [1].ep.ep_list, &dev->gadget.ep_list);
1717 list_add_tail (&dev->ep [2].ep.ep_list, &dev->gadget.ep_list);
1718 switch (mode) {
1719 case 0:
1720 list_add_tail (&dev->ep [3].ep.ep_list, &dev->gadget.ep_list);
1721 list_add_tail (&dev->ep [4].ep.ep_list, &dev->gadget.ep_list);
1722 dev->ep [1].fifo_size = dev->ep [2].fifo_size = 1024;
1723 break;
1724 case 1:
1725 dev->ep [1].fifo_size = dev->ep [2].fifo_size = 2048;
1726 break;
1727 case 2:
1728 list_add_tail (&dev->ep [3].ep.ep_list, &dev->gadget.ep_list);
1729 dev->ep [1].fifo_size = 2048;
1730 dev->ep [2].fifo_size = 1024;
1731 break;
1733 /* fifo sizes for ep0, ep-c, ep-d, ep-e, and ep-f never change */
1734 list_add_tail (&dev->ep [5].ep.ep_list, &dev->gadget.ep_list);
1735 list_add_tail (&dev->ep [6].ep.ep_list, &dev->gadget.ep_list);
1738 /* keeping it simple:
1739 * - one bus driver, initted first;
1740 * - one function driver, initted second
1742 * most of the work to support multiple net2280 controllers would
1743 * be to associate this gadget driver (yes?) with all of them, or
1744 * perhaps to bind specific drivers to specific devices.
1747 static struct net2280 *the_controller;
1749 static void usb_reset (struct net2280 *dev)
1751 u32 tmp;
1753 dev->gadget.speed = USB_SPEED_UNKNOWN;
1754 (void) readl (&dev->usb->usbctl);
1756 net2280_led_init (dev);
1758 /* disable automatic responses, and irqs */
1759 writel (0, &dev->usb->stdrsp);
1760 writel (0, &dev->regs->pciirqenb0);
1761 writel (0, &dev->regs->pciirqenb1);
1763 /* clear old dma and irq state */
1764 for (tmp = 0; tmp < 4; tmp++) {
1765 struct net2280_ep *ep = &dev->ep [tmp + 1];
1767 if (ep->dma)
1768 abort_dma (ep);
1770 writel (~0, &dev->regs->irqstat0),
1771 writel (~(1 << SUSPEND_REQUEST_INTERRUPT), &dev->regs->irqstat1),
1773 /* reset, and enable pci */
1774 tmp = readl (&dev->regs->devinit)
1775 | (1 << PCI_ENABLE)
1776 | (1 << FIFO_SOFT_RESET)
1777 | (1 << USB_SOFT_RESET)
1778 | (1 << M8051_RESET);
1779 writel (tmp, &dev->regs->devinit);
1781 /* standard fifo and endpoint allocations */
1782 set_fifo_mode (dev, (fifo_mode <= 2) ? fifo_mode : 0);
1785 static void usb_reinit (struct net2280 *dev)
1787 u32 tmp;
1788 int init_dma;
1790 /* use_dma changes are ignored till next device re-init */
1791 init_dma = use_dma;
1793 /* basic endpoint init */
1794 for (tmp = 0; tmp < 7; tmp++) {
1795 struct net2280_ep *ep = &dev->ep [tmp];
1797 ep->ep.name = ep_name [tmp];
1798 ep->dev = dev;
1799 ep->num = tmp;
1801 if (tmp > 0 && tmp <= 4) {
1802 ep->fifo_size = 1024;
1803 if (init_dma)
1804 ep->dma = &dev->dma [tmp - 1];
1805 } else
1806 ep->fifo_size = 64;
1807 ep->regs = &dev->epregs [tmp];
1808 ep_reset (dev->regs, ep);
1810 dev->ep [0].ep.maxpacket = 64;
1811 dev->ep [5].ep.maxpacket = 64;
1812 dev->ep [6].ep.maxpacket = 64;
1814 dev->gadget.ep0 = &dev->ep [0].ep;
1815 dev->ep [0].stopped = 0;
1816 INIT_LIST_HEAD (&dev->gadget.ep0->ep_list);
1818 /* we want to prevent lowlevel/insecure access from the USB host,
1819 * but erratum 0119 means this enable bit is ignored
1821 for (tmp = 0; tmp < 5; tmp++)
1822 writel (EP_DONTUSE, &dev->dep [tmp].dep_cfg);
1825 static void ep0_start (struct net2280 *dev)
1827 writel ( (1 << CLEAR_EP_HIDE_STATUS_PHASE)
1828 | (1 << CLEAR_NAK_OUT_PACKETS)
1829 | (1 << CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE)
1830 , &dev->epregs [0].ep_rsp);
1833 * hardware optionally handles a bunch of standard requests
1834 * that the API hides from drivers anyway. have it do so.
1835 * endpoint status/features are handled in software, to
1836 * help pass tests for some dubious behavior.
1838 writel ( (1 << SET_TEST_MODE)
1839 | (1 << SET_ADDRESS)
1840 | (1 << DEVICE_SET_CLEAR_DEVICE_REMOTE_WAKEUP)
1841 | (1 << GET_DEVICE_STATUS)
1842 | (1 << GET_INTERFACE_STATUS)
1843 , &dev->usb->stdrsp);
1844 writel ( (1 << USB_ROOT_PORT_WAKEUP_ENABLE)
1845 | (1 << SELF_POWERED_USB_DEVICE)
1846 | (1 << REMOTE_WAKEUP_SUPPORT)
1847 | (dev->softconnect << USB_DETECT_ENABLE)
1848 | (1 << SELF_POWERED_STATUS)
1849 , &dev->usb->usbctl);
1851 /* enable irqs so we can see ep0 and general operation */
1852 writel ( (1 << SETUP_PACKET_INTERRUPT_ENABLE)
1853 | (1 << ENDPOINT_0_INTERRUPT_ENABLE)
1854 , &dev->regs->pciirqenb0);
1855 writel ( (1 << PCI_INTERRUPT_ENABLE)
1856 | (1 << PCI_MASTER_ABORT_RECEIVED_INTERRUPT_ENABLE)
1857 | (1 << PCI_TARGET_ABORT_RECEIVED_INTERRUPT_ENABLE)
1858 | (1 << PCI_RETRY_ABORT_INTERRUPT_ENABLE)
1859 | (1 << VBUS_INTERRUPT_ENABLE)
1860 | (1 << ROOT_PORT_RESET_INTERRUPT_ENABLE)
1861 | (1 << SUSPEND_REQUEST_CHANGE_INTERRUPT_ENABLE)
1862 , &dev->regs->pciirqenb1);
1864 /* don't leave any writes posted */
1865 (void) readl (&dev->usb->usbctl);
1868 /* when a driver is successfully registered, it will receive
1869 * control requests including set_configuration(), which enables
1870 * non-control requests. then usb traffic follows until a
1871 * disconnect is reported. then a host may connect again, or
1872 * the driver might get unbound.
1874 static int net2280_start(struct usb_gadget_driver *driver,
1875 int (*bind)(struct usb_gadget *))
1877 struct net2280 *dev = the_controller;
1878 int retval;
1879 unsigned i;
1881 /* insist on high speed support from the driver, since
1882 * (dev->usb->xcvrdiag & FORCE_FULL_SPEED_MODE)
1883 * "must not be used in normal operation"
1885 if (!driver
1886 || driver->speed != USB_SPEED_HIGH
1887 || !bind || !driver->setup)
1888 return -EINVAL;
1889 if (!dev)
1890 return -ENODEV;
1891 if (dev->driver)
1892 return -EBUSY;
1894 for (i = 0; i < 7; i++)
1895 dev->ep [i].irqs = 0;
1897 /* hook up the driver ... */
1898 dev->softconnect = 1;
1899 driver->driver.bus = NULL;
1900 dev->driver = driver;
1901 dev->gadget.dev.driver = &driver->driver;
1902 retval = bind(&dev->gadget);
1903 if (retval) {
1904 DEBUG (dev, "bind to driver %s --> %d\n",
1905 driver->driver.name, retval);
1906 dev->driver = NULL;
1907 dev->gadget.dev.driver = NULL;
1908 return retval;
1911 retval = device_create_file (&dev->pdev->dev, &dev_attr_function);
1912 if (retval) goto err_unbind;
1913 retval = device_create_file (&dev->pdev->dev, &dev_attr_queues);
1914 if (retval) goto err_func;
1916 /* ... then enable host detection and ep0; and we're ready
1917 * for set_configuration as well as eventual disconnect.
1919 net2280_led_active (dev, 1);
1920 ep0_start (dev);
1922 DEBUG (dev, "%s ready, usbctl %08x stdrsp %08x\n",
1923 driver->driver.name,
1924 readl (&dev->usb->usbctl),
1925 readl (&dev->usb->stdrsp));
1927 /* pci writes may still be posted */
1928 return 0;
1930 err_func:
1931 device_remove_file (&dev->pdev->dev, &dev_attr_function);
1932 err_unbind:
1933 driver->unbind (&dev->gadget);
1934 dev->gadget.dev.driver = NULL;
1935 dev->driver = NULL;
1936 return retval;
1939 static void
1940 stop_activity (struct net2280 *dev, struct usb_gadget_driver *driver)
1942 int i;
1944 /* don't disconnect if it's not connected */
1945 if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1946 driver = NULL;
1948 /* stop hardware; prevent new request submissions;
1949 * and kill any outstanding requests.
1951 usb_reset (dev);
1952 for (i = 0; i < 7; i++)
1953 nuke (&dev->ep [i]);
1955 /* report disconnect; the driver is already quiesced */
1956 if (driver) {
1957 spin_unlock (&dev->lock);
1958 driver->disconnect (&dev->gadget);
1959 spin_lock (&dev->lock);
1962 usb_reinit (dev);
1965 static int net2280_stop(struct usb_gadget_driver *driver)
1967 struct net2280 *dev = the_controller;
1968 unsigned long flags;
1970 if (!dev)
1971 return -ENODEV;
1972 if (!driver || driver != dev->driver || !driver->unbind)
1973 return -EINVAL;
1975 spin_lock_irqsave (&dev->lock, flags);
1976 stop_activity (dev, driver);
1977 spin_unlock_irqrestore (&dev->lock, flags);
1979 net2280_pullup (&dev->gadget, 0);
1981 driver->unbind (&dev->gadget);
1982 dev->gadget.dev.driver = NULL;
1983 dev->driver = NULL;
1985 net2280_led_active (dev, 0);
1986 device_remove_file (&dev->pdev->dev, &dev_attr_function);
1987 device_remove_file (&dev->pdev->dev, &dev_attr_queues);
1989 DEBUG (dev, "unregistered driver '%s'\n", driver->driver.name);
1990 return 0;
1993 /*-------------------------------------------------------------------------*/
1995 /* handle ep0, ep-e, ep-f with 64 byte packets: packet per irq.
1996 * also works for dma-capable endpoints, in pio mode or just
1997 * to manually advance the queue after short OUT transfers.
1999 static void handle_ep_small (struct net2280_ep *ep)
2001 struct net2280_request *req;
2002 u32 t;
2003 /* 0 error, 1 mid-data, 2 done */
2004 int mode = 1;
2006 if (!list_empty (&ep->queue))
2007 req = list_entry (ep->queue.next,
2008 struct net2280_request, queue);
2009 else
2010 req = NULL;
2012 /* ack all, and handle what we care about */
2013 t = readl (&ep->regs->ep_stat);
2014 ep->irqs++;
2015 #if 0
2016 VDEBUG (ep->dev, "%s ack ep_stat %08x, req %p\n",
2017 ep->ep.name, t, req ? &req->req : 0);
2018 #endif
2019 if (!ep->is_in || ep->dev->pdev->device == 0x2280)
2020 writel (t & ~(1 << NAK_OUT_PACKETS), &ep->regs->ep_stat);
2021 else
2022 /* Added for 2282 */
2023 writel (t, &ep->regs->ep_stat);
2025 /* for ep0, monitor token irqs to catch data stage length errors
2026 * and to synchronize on status.
2028 * also, to defer reporting of protocol stalls ... here's where
2029 * data or status first appears, handling stalls here should never
2030 * cause trouble on the host side..
2032 * control requests could be slightly faster without token synch for
2033 * status, but status can jam up that way.
2035 if (unlikely (ep->num == 0)) {
2036 if (ep->is_in) {
2037 /* status; stop NAKing */
2038 if (t & (1 << DATA_OUT_PING_TOKEN_INTERRUPT)) {
2039 if (ep->dev->protocol_stall) {
2040 ep->stopped = 1;
2041 set_halt (ep);
2043 if (!req)
2044 allow_status (ep);
2045 mode = 2;
2046 /* reply to extra IN data tokens with a zlp */
2047 } else if (t & (1 << DATA_IN_TOKEN_INTERRUPT)) {
2048 if (ep->dev->protocol_stall) {
2049 ep->stopped = 1;
2050 set_halt (ep);
2051 mode = 2;
2052 } else if (ep->responded &&
2053 !req && !ep->stopped)
2054 write_fifo (ep, NULL);
2056 } else {
2057 /* status; stop NAKing */
2058 if (t & (1 << DATA_IN_TOKEN_INTERRUPT)) {
2059 if (ep->dev->protocol_stall) {
2060 ep->stopped = 1;
2061 set_halt (ep);
2063 mode = 2;
2064 /* an extra OUT token is an error */
2065 } else if (((t & (1 << DATA_OUT_PING_TOKEN_INTERRUPT))
2066 && req
2067 && req->req.actual == req->req.length)
2068 || (ep->responded && !req)) {
2069 ep->dev->protocol_stall = 1;
2070 set_halt (ep);
2071 ep->stopped = 1;
2072 if (req)
2073 done (ep, req, -EOVERFLOW);
2074 req = NULL;
2079 if (unlikely (!req))
2080 return;
2082 /* manual DMA queue advance after short OUT */
2083 if (likely (ep->dma != 0)) {
2084 if (t & (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)) {
2085 u32 count;
2086 int stopped = ep->stopped;
2088 /* TRANSFERRED works around OUT_DONE erratum 0112.
2089 * we expect (N <= maxpacket) bytes; host wrote M.
2090 * iff (M < N) we won't ever see a DMA interrupt.
2092 ep->stopped = 1;
2093 for (count = 0; ; t = readl (&ep->regs->ep_stat)) {
2095 /* any preceding dma transfers must finish.
2096 * dma handles (M >= N), may empty the queue
2098 scan_dma_completions (ep);
2099 if (unlikely (list_empty (&ep->queue)
2100 || ep->out_overflow)) {
2101 req = NULL;
2102 break;
2104 req = list_entry (ep->queue.next,
2105 struct net2280_request, queue);
2107 /* here either (M < N), a "real" short rx;
2108 * or (M == N) and the queue didn't empty
2110 if (likely (t & (1 << FIFO_EMPTY))) {
2111 count = readl (&ep->dma->dmacount);
2112 count &= DMA_BYTE_COUNT_MASK;
2113 if (readl (&ep->dma->dmadesc)
2114 != req->td_dma)
2115 req = NULL;
2116 break;
2118 udelay(1);
2121 /* stop DMA, leave ep NAKing */
2122 writel ((1 << DMA_ABORT), &ep->dma->dmastat);
2123 spin_stop_dma (ep->dma);
2125 if (likely (req)) {
2126 req->td->dmacount = 0;
2127 t = readl (&ep->regs->ep_avail);
2128 dma_done (ep, req, count,
2129 (ep->out_overflow || t)
2130 ? -EOVERFLOW : 0);
2133 /* also flush to prevent erratum 0106 trouble */
2134 if (unlikely (ep->out_overflow
2135 || (ep->dev->chiprev == 0x0100
2136 && ep->dev->gadget.speed
2137 == USB_SPEED_FULL))) {
2138 out_flush (ep);
2139 ep->out_overflow = 0;
2142 /* (re)start dma if needed, stop NAKing */
2143 ep->stopped = stopped;
2144 if (!list_empty (&ep->queue))
2145 restart_dma (ep);
2146 } else
2147 DEBUG (ep->dev, "%s dma ep_stat %08x ??\n",
2148 ep->ep.name, t);
2149 return;
2151 /* data packet(s) received (in the fifo, OUT) */
2152 } else if (t & (1 << DATA_PACKET_RECEIVED_INTERRUPT)) {
2153 if (read_fifo (ep, req) && ep->num != 0)
2154 mode = 2;
2156 /* data packet(s) transmitted (IN) */
2157 } else if (t & (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)) {
2158 unsigned len;
2160 len = req->req.length - req->req.actual;
2161 if (len > ep->ep.maxpacket)
2162 len = ep->ep.maxpacket;
2163 req->req.actual += len;
2165 /* if we wrote it all, we're usually done */
2166 if (req->req.actual == req->req.length) {
2167 if (ep->num == 0) {
2168 /* send zlps until the status stage */
2169 } else if (!req->req.zero || len != ep->ep.maxpacket)
2170 mode = 2;
2173 /* there was nothing to do ... */
2174 } else if (mode == 1)
2175 return;
2177 /* done */
2178 if (mode == 2) {
2179 /* stream endpoints often resubmit/unlink in completion */
2180 done (ep, req, 0);
2182 /* maybe advance queue to next request */
2183 if (ep->num == 0) {
2184 /* NOTE: net2280 could let gadget driver start the
2185 * status stage later. since not all controllers let
2186 * them control that, the api doesn't (yet) allow it.
2188 if (!ep->stopped)
2189 allow_status (ep);
2190 req = NULL;
2191 } else {
2192 if (!list_empty (&ep->queue) && !ep->stopped)
2193 req = list_entry (ep->queue.next,
2194 struct net2280_request, queue);
2195 else
2196 req = NULL;
2197 if (req && !ep->is_in)
2198 stop_out_naking (ep);
2202 /* is there a buffer for the next packet?
2203 * for best streaming performance, make sure there is one.
2205 if (req && !ep->stopped) {
2207 /* load IN fifo with next packet (may be zlp) */
2208 if (t & (1 << DATA_PACKET_TRANSMITTED_INTERRUPT))
2209 write_fifo (ep, &req->req);
2213 static struct net2280_ep *
2214 get_ep_by_addr (struct net2280 *dev, u16 wIndex)
2216 struct net2280_ep *ep;
2218 if ((wIndex & USB_ENDPOINT_NUMBER_MASK) == 0)
2219 return &dev->ep [0];
2220 list_for_each_entry (ep, &dev->gadget.ep_list, ep.ep_list) {
2221 u8 bEndpointAddress;
2223 if (!ep->desc)
2224 continue;
2225 bEndpointAddress = ep->desc->bEndpointAddress;
2226 if ((wIndex ^ bEndpointAddress) & USB_DIR_IN)
2227 continue;
2228 if ((wIndex & 0x0f) == (bEndpointAddress & 0x0f))
2229 return ep;
2231 return NULL;
2234 static void handle_stat0_irqs (struct net2280 *dev, u32 stat)
2236 struct net2280_ep *ep;
2237 u32 num, scratch;
2239 /* most of these don't need individual acks */
2240 stat &= ~(1 << INTA_ASSERTED);
2241 if (!stat)
2242 return;
2243 // DEBUG (dev, "irqstat0 %04x\n", stat);
2245 /* starting a control request? */
2246 if (unlikely (stat & (1 << SETUP_PACKET_INTERRUPT))) {
2247 union {
2248 u32 raw [2];
2249 struct usb_ctrlrequest r;
2250 } u;
2251 int tmp;
2252 struct net2280_request *req;
2254 if (dev->gadget.speed == USB_SPEED_UNKNOWN) {
2255 if (readl (&dev->usb->usbstat) & (1 << HIGH_SPEED))
2256 dev->gadget.speed = USB_SPEED_HIGH;
2257 else
2258 dev->gadget.speed = USB_SPEED_FULL;
2259 net2280_led_speed (dev, dev->gadget.speed);
2260 DEBUG(dev, "%s\n", usb_speed_string(dev->gadget.speed));
2263 ep = &dev->ep [0];
2264 ep->irqs++;
2266 /* make sure any leftover request state is cleared */
2267 stat &= ~(1 << ENDPOINT_0_INTERRUPT);
2268 while (!list_empty (&ep->queue)) {
2269 req = list_entry (ep->queue.next,
2270 struct net2280_request, queue);
2271 done (ep, req, (req->req.actual == req->req.length)
2272 ? 0 : -EPROTO);
2274 ep->stopped = 0;
2275 dev->protocol_stall = 0;
2277 if (ep->dev->pdev->device == 0x2280)
2278 tmp = (1 << FIFO_OVERFLOW)
2279 | (1 << FIFO_UNDERFLOW);
2280 else
2281 tmp = 0;
2283 writel (tmp | (1 << TIMEOUT)
2284 | (1 << USB_STALL_SENT)
2285 | (1 << USB_IN_NAK_SENT)
2286 | (1 << USB_IN_ACK_RCVD)
2287 | (1 << USB_OUT_PING_NAK_SENT)
2288 | (1 << USB_OUT_ACK_SENT)
2289 | (1 << SHORT_PACKET_OUT_DONE_INTERRUPT)
2290 | (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)
2291 | (1 << DATA_PACKET_RECEIVED_INTERRUPT)
2292 | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
2293 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
2294 | (1 << DATA_IN_TOKEN_INTERRUPT)
2295 , &ep->regs->ep_stat);
2296 u.raw [0] = readl (&dev->usb->setup0123);
2297 u.raw [1] = readl (&dev->usb->setup4567);
2299 cpu_to_le32s (&u.raw [0]);
2300 cpu_to_le32s (&u.raw [1]);
2302 tmp = 0;
2304 #define w_value le16_to_cpu(u.r.wValue)
2305 #define w_index le16_to_cpu(u.r.wIndex)
2306 #define w_length le16_to_cpu(u.r.wLength)
2308 /* ack the irq */
2309 writel (1 << SETUP_PACKET_INTERRUPT, &dev->regs->irqstat0);
2310 stat ^= (1 << SETUP_PACKET_INTERRUPT);
2312 /* watch control traffic at the token level, and force
2313 * synchronization before letting the status stage happen.
2314 * FIXME ignore tokens we'll NAK, until driver responds.
2315 * that'll mean a lot less irqs for some drivers.
2317 ep->is_in = (u.r.bRequestType & USB_DIR_IN) != 0;
2318 if (ep->is_in) {
2319 scratch = (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
2320 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
2321 | (1 << DATA_IN_TOKEN_INTERRUPT);
2322 stop_out_naking (ep);
2323 } else
2324 scratch = (1 << DATA_PACKET_RECEIVED_INTERRUPT)
2325 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
2326 | (1 << DATA_IN_TOKEN_INTERRUPT);
2327 writel (scratch, &dev->epregs [0].ep_irqenb);
2329 /* we made the hardware handle most lowlevel requests;
2330 * everything else goes uplevel to the gadget code.
2332 ep->responded = 1;
2333 switch (u.r.bRequest) {
2334 case USB_REQ_GET_STATUS: {
2335 struct net2280_ep *e;
2336 __le32 status;
2338 /* hw handles device and interface status */
2339 if (u.r.bRequestType != (USB_DIR_IN|USB_RECIP_ENDPOINT))
2340 goto delegate;
2341 if ((e = get_ep_by_addr (dev, w_index)) == 0
2342 || w_length > 2)
2343 goto do_stall;
2345 if (readl (&e->regs->ep_rsp)
2346 & (1 << SET_ENDPOINT_HALT))
2347 status = cpu_to_le32 (1);
2348 else
2349 status = cpu_to_le32 (0);
2351 /* don't bother with a request object! */
2352 writel (0, &dev->epregs [0].ep_irqenb);
2353 set_fifo_bytecount (ep, w_length);
2354 writel ((__force u32)status, &dev->epregs [0].ep_data);
2355 allow_status (ep);
2356 VDEBUG (dev, "%s stat %02x\n", ep->ep.name, status);
2357 goto next_endpoints;
2359 break;
2360 case USB_REQ_CLEAR_FEATURE: {
2361 struct net2280_ep *e;
2363 /* hw handles device features */
2364 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
2365 goto delegate;
2366 if (w_value != USB_ENDPOINT_HALT
2367 || w_length != 0)
2368 goto do_stall;
2369 if ((e = get_ep_by_addr (dev, w_index)) == 0)
2370 goto do_stall;
2371 if (e->wedged) {
2372 VDEBUG(dev, "%s wedged, halt not cleared\n",
2373 ep->ep.name);
2374 } else {
2375 VDEBUG(dev, "%s clear halt\n", ep->ep.name);
2376 clear_halt(e);
2378 allow_status (ep);
2379 goto next_endpoints;
2381 break;
2382 case USB_REQ_SET_FEATURE: {
2383 struct net2280_ep *e;
2385 /* hw handles device features */
2386 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
2387 goto delegate;
2388 if (w_value != USB_ENDPOINT_HALT
2389 || w_length != 0)
2390 goto do_stall;
2391 if ((e = get_ep_by_addr (dev, w_index)) == 0)
2392 goto do_stall;
2393 if (e->ep.name == ep0name)
2394 goto do_stall;
2395 set_halt (e);
2396 allow_status (ep);
2397 VDEBUG (dev, "%s set halt\n", ep->ep.name);
2398 goto next_endpoints;
2400 break;
2401 default:
2402 delegate:
2403 VDEBUG (dev, "setup %02x.%02x v%04x i%04x l%04x "
2404 "ep_cfg %08x\n",
2405 u.r.bRequestType, u.r.bRequest,
2406 w_value, w_index, w_length,
2407 readl (&ep->regs->ep_cfg));
2408 ep->responded = 0;
2409 spin_unlock (&dev->lock);
2410 tmp = dev->driver->setup (&dev->gadget, &u.r);
2411 spin_lock (&dev->lock);
2414 /* stall ep0 on error */
2415 if (tmp < 0) {
2416 do_stall:
2417 VDEBUG (dev, "req %02x.%02x protocol STALL; stat %d\n",
2418 u.r.bRequestType, u.r.bRequest, tmp);
2419 dev->protocol_stall = 1;
2422 /* some in/out token irq should follow; maybe stall then.
2423 * driver must queue a request (even zlp) or halt ep0
2424 * before the host times out.
2428 #undef w_value
2429 #undef w_index
2430 #undef w_length
2432 next_endpoints:
2433 /* endpoint data irq ? */
2434 scratch = stat & 0x7f;
2435 stat &= ~0x7f;
2436 for (num = 0; scratch; num++) {
2437 u32 t;
2439 /* do this endpoint's FIFO and queue need tending? */
2440 t = 1 << num;
2441 if ((scratch & t) == 0)
2442 continue;
2443 scratch ^= t;
2445 ep = &dev->ep [num];
2446 handle_ep_small (ep);
2449 if (stat)
2450 DEBUG (dev, "unhandled irqstat0 %08x\n", stat);
2453 #define DMA_INTERRUPTS ( \
2454 (1 << DMA_D_INTERRUPT) \
2455 | (1 << DMA_C_INTERRUPT) \
2456 | (1 << DMA_B_INTERRUPT) \
2457 | (1 << DMA_A_INTERRUPT))
2458 #define PCI_ERROR_INTERRUPTS ( \
2459 (1 << PCI_MASTER_ABORT_RECEIVED_INTERRUPT) \
2460 | (1 << PCI_TARGET_ABORT_RECEIVED_INTERRUPT) \
2461 | (1 << PCI_RETRY_ABORT_INTERRUPT))
2463 static void handle_stat1_irqs (struct net2280 *dev, u32 stat)
2465 struct net2280_ep *ep;
2466 u32 tmp, num, mask, scratch;
2468 /* after disconnect there's nothing else to do! */
2469 tmp = (1 << VBUS_INTERRUPT) | (1 << ROOT_PORT_RESET_INTERRUPT);
2470 mask = (1 << HIGH_SPEED) | (1 << FULL_SPEED);
2472 /* VBUS disconnect is indicated by VBUS_PIN and VBUS_INTERRUPT set.
2473 * Root Port Reset is indicated by ROOT_PORT_RESET_INTERRRUPT set and
2474 * both HIGH_SPEED and FULL_SPEED clear (as ROOT_PORT_RESET_INTERRUPT
2475 * only indicates a change in the reset state).
2477 if (stat & tmp) {
2478 writel (tmp, &dev->regs->irqstat1);
2479 if ((((stat & (1 << ROOT_PORT_RESET_INTERRUPT))
2480 && ((readl (&dev->usb->usbstat) & mask)
2481 == 0))
2482 || ((readl (&dev->usb->usbctl)
2483 & (1 << VBUS_PIN)) == 0)
2484 ) && ( dev->gadget.speed != USB_SPEED_UNKNOWN)) {
2485 DEBUG (dev, "disconnect %s\n",
2486 dev->driver->driver.name);
2487 stop_activity (dev, dev->driver);
2488 ep0_start (dev);
2489 return;
2491 stat &= ~tmp;
2493 /* vBUS can bounce ... one of many reasons to ignore the
2494 * notion of hotplug events on bus connect/disconnect!
2496 if (!stat)
2497 return;
2500 /* NOTE: chip stays in PCI D0 state for now, but it could
2501 * enter D1 to save more power
2503 tmp = (1 << SUSPEND_REQUEST_CHANGE_INTERRUPT);
2504 if (stat & tmp) {
2505 writel (tmp, &dev->regs->irqstat1);
2506 if (stat & (1 << SUSPEND_REQUEST_INTERRUPT)) {
2507 if (dev->driver->suspend)
2508 dev->driver->suspend (&dev->gadget);
2509 if (!enable_suspend)
2510 stat &= ~(1 << SUSPEND_REQUEST_INTERRUPT);
2511 } else {
2512 if (dev->driver->resume)
2513 dev->driver->resume (&dev->gadget);
2514 /* at high speed, note erratum 0133 */
2516 stat &= ~tmp;
2519 /* clear any other status/irqs */
2520 if (stat)
2521 writel (stat, &dev->regs->irqstat1);
2523 /* some status we can just ignore */
2524 if (dev->pdev->device == 0x2280)
2525 stat &= ~((1 << CONTROL_STATUS_INTERRUPT)
2526 | (1 << SUSPEND_REQUEST_INTERRUPT)
2527 | (1 << RESUME_INTERRUPT)
2528 | (1 << SOF_INTERRUPT));
2529 else
2530 stat &= ~((1 << CONTROL_STATUS_INTERRUPT)
2531 | (1 << RESUME_INTERRUPT)
2532 | (1 << SOF_DOWN_INTERRUPT)
2533 | (1 << SOF_INTERRUPT));
2535 if (!stat)
2536 return;
2537 // DEBUG (dev, "irqstat1 %08x\n", stat);
2539 /* DMA status, for ep-{a,b,c,d} */
2540 scratch = stat & DMA_INTERRUPTS;
2541 stat &= ~DMA_INTERRUPTS;
2542 scratch >>= 9;
2543 for (num = 0; scratch; num++) {
2544 struct net2280_dma_regs __iomem *dma;
2546 tmp = 1 << num;
2547 if ((tmp & scratch) == 0)
2548 continue;
2549 scratch ^= tmp;
2551 ep = &dev->ep [num + 1];
2552 dma = ep->dma;
2554 if (!dma)
2555 continue;
2557 /* clear ep's dma status */
2558 tmp = readl (&dma->dmastat);
2559 writel (tmp, &dma->dmastat);
2561 /* chaining should stop on abort, short OUT from fifo,
2562 * or (stat0 codepath) short OUT transfer.
2564 if (!use_dma_chaining) {
2565 if ((tmp & (1 << DMA_TRANSACTION_DONE_INTERRUPT))
2566 == 0) {
2567 DEBUG (ep->dev, "%s no xact done? %08x\n",
2568 ep->ep.name, tmp);
2569 continue;
2571 stop_dma (ep->dma);
2574 /* OUT transfers terminate when the data from the
2575 * host is in our memory. Process whatever's done.
2576 * On this path, we know transfer's last packet wasn't
2577 * less than req->length. NAK_OUT_PACKETS may be set,
2578 * or the FIFO may already be holding new packets.
2580 * IN transfers can linger in the FIFO for a very
2581 * long time ... we ignore that for now, accounting
2582 * precisely (like PIO does) needs per-packet irqs
2584 scan_dma_completions (ep);
2586 /* disable dma on inactive queues; else maybe restart */
2587 if (list_empty (&ep->queue)) {
2588 if (use_dma_chaining)
2589 stop_dma (ep->dma);
2590 } else {
2591 tmp = readl (&dma->dmactl);
2592 if (!use_dma_chaining
2593 || (tmp & (1 << DMA_ENABLE)) == 0)
2594 restart_dma (ep);
2595 else if (ep->is_in && use_dma_chaining) {
2596 struct net2280_request *req;
2597 __le32 dmacount;
2599 /* the descriptor at the head of the chain
2600 * may still have VALID_BIT clear; that's
2601 * used to trigger changing DMA_FIFO_VALIDATE
2602 * (affects automagic zlp writes).
2604 req = list_entry (ep->queue.next,
2605 struct net2280_request, queue);
2606 dmacount = req->td->dmacount;
2607 dmacount &= cpu_to_le32 (
2608 (1 << VALID_BIT)
2609 | DMA_BYTE_COUNT_MASK);
2610 if (dmacount && (dmacount & valid_bit) == 0)
2611 restart_dma (ep);
2614 ep->irqs++;
2617 /* NOTE: there are other PCI errors we might usefully notice.
2618 * if they appear very often, here's where to try recovering.
2620 if (stat & PCI_ERROR_INTERRUPTS) {
2621 ERROR (dev, "pci dma error; stat %08x\n", stat);
2622 stat &= ~PCI_ERROR_INTERRUPTS;
2623 /* these are fatal errors, but "maybe" they won't
2624 * happen again ...
2626 stop_activity (dev, dev->driver);
2627 ep0_start (dev);
2628 stat = 0;
2631 if (stat)
2632 DEBUG (dev, "unhandled irqstat1 %08x\n", stat);
2635 static irqreturn_t net2280_irq (int irq, void *_dev)
2637 struct net2280 *dev = _dev;
2639 /* shared interrupt, not ours */
2640 if (!(readl(&dev->regs->irqstat0) & (1 << INTA_ASSERTED)))
2641 return IRQ_NONE;
2643 spin_lock (&dev->lock);
2645 /* handle disconnect, dma, and more */
2646 handle_stat1_irqs (dev, readl (&dev->regs->irqstat1));
2648 /* control requests and PIO */
2649 handle_stat0_irqs (dev, readl (&dev->regs->irqstat0));
2651 spin_unlock (&dev->lock);
2653 return IRQ_HANDLED;
2656 /*-------------------------------------------------------------------------*/
2658 static void gadget_release (struct device *_dev)
2660 struct net2280 *dev = dev_get_drvdata (_dev);
2662 kfree (dev);
2665 /* tear down the binding between this driver and the pci device */
2667 static void net2280_remove (struct pci_dev *pdev)
2669 struct net2280 *dev = pci_get_drvdata (pdev);
2671 usb_del_gadget_udc(&dev->gadget);
2673 BUG_ON(dev->driver);
2675 /* then clean up the resources we allocated during probe() */
2676 net2280_led_shutdown (dev);
2677 if (dev->requests) {
2678 int i;
2679 for (i = 1; i < 5; i++) {
2680 if (!dev->ep [i].dummy)
2681 continue;
2682 pci_pool_free (dev->requests, dev->ep [i].dummy,
2683 dev->ep [i].td_dma);
2685 pci_pool_destroy (dev->requests);
2687 if (dev->got_irq)
2688 free_irq (pdev->irq, dev);
2689 if (dev->regs)
2690 iounmap (dev->regs);
2691 if (dev->region)
2692 release_mem_region (pci_resource_start (pdev, 0),
2693 pci_resource_len (pdev, 0));
2694 if (dev->enabled)
2695 pci_disable_device (pdev);
2696 device_unregister (&dev->gadget.dev);
2697 device_remove_file (&pdev->dev, &dev_attr_registers);
2698 pci_set_drvdata (pdev, NULL);
2700 INFO (dev, "unbind\n");
2702 the_controller = NULL;
2705 /* wrap this driver around the specified device, but
2706 * don't respond over USB until a gadget driver binds to us.
2709 static int net2280_probe (struct pci_dev *pdev, const struct pci_device_id *id)
2711 struct net2280 *dev;
2712 unsigned long resource, len;
2713 void __iomem *base = NULL;
2714 int retval, i;
2716 /* if you want to support more than one controller in a system,
2717 * usb_gadget_driver_{register,unregister}() must change.
2719 if (the_controller) {
2720 dev_warn (&pdev->dev, "ignoring\n");
2721 return -EBUSY;
2724 /* alloc, and start init */
2725 dev = kzalloc (sizeof *dev, GFP_KERNEL);
2726 if (dev == NULL){
2727 retval = -ENOMEM;
2728 goto done;
2731 pci_set_drvdata (pdev, dev);
2732 spin_lock_init (&dev->lock);
2733 dev->pdev = pdev;
2734 dev->gadget.ops = &net2280_ops;
2735 dev->gadget.is_dualspeed = 1;
2737 /* the "gadget" abstracts/virtualizes the controller */
2738 dev_set_name(&dev->gadget.dev, "gadget");
2739 dev->gadget.dev.parent = &pdev->dev;
2740 dev->gadget.dev.dma_mask = pdev->dev.dma_mask;
2741 dev->gadget.dev.release = gadget_release;
2742 dev->gadget.name = driver_name;
2744 /* now all the pci goodies ... */
2745 if (pci_enable_device (pdev) < 0) {
2746 retval = -ENODEV;
2747 goto done;
2749 dev->enabled = 1;
2751 /* BAR 0 holds all the registers
2752 * BAR 1 is 8051 memory; unused here (note erratum 0103)
2753 * BAR 2 is fifo memory; unused here
2755 resource = pci_resource_start (pdev, 0);
2756 len = pci_resource_len (pdev, 0);
2757 if (!request_mem_region (resource, len, driver_name)) {
2758 DEBUG (dev, "controller already in use\n");
2759 retval = -EBUSY;
2760 goto done;
2762 dev->region = 1;
2764 /* FIXME provide firmware download interface to put
2765 * 8051 code into the chip, e.g. to turn on PCI PM.
2768 base = ioremap_nocache (resource, len);
2769 if (base == NULL) {
2770 DEBUG (dev, "can't map memory\n");
2771 retval = -EFAULT;
2772 goto done;
2774 dev->regs = (struct net2280_regs __iomem *) base;
2775 dev->usb = (struct net2280_usb_regs __iomem *) (base + 0x0080);
2776 dev->pci = (struct net2280_pci_regs __iomem *) (base + 0x0100);
2777 dev->dma = (struct net2280_dma_regs __iomem *) (base + 0x0180);
2778 dev->dep = (struct net2280_dep_regs __iomem *) (base + 0x0200);
2779 dev->epregs = (struct net2280_ep_regs __iomem *) (base + 0x0300);
2781 /* put into initial config, link up all endpoints */
2782 writel (0, &dev->usb->usbctl);
2783 usb_reset (dev);
2784 usb_reinit (dev);
2786 /* irq setup after old hardware is cleaned up */
2787 if (!pdev->irq) {
2788 ERROR (dev, "No IRQ. Check PCI setup!\n");
2789 retval = -ENODEV;
2790 goto done;
2793 if (request_irq (pdev->irq, net2280_irq, IRQF_SHARED, driver_name, dev)
2794 != 0) {
2795 ERROR (dev, "request interrupt %d failed\n", pdev->irq);
2796 retval = -EBUSY;
2797 goto done;
2799 dev->got_irq = 1;
2801 /* DMA setup */
2802 /* NOTE: we know only the 32 LSBs of dma addresses may be nonzero */
2803 dev->requests = pci_pool_create ("requests", pdev,
2804 sizeof (struct net2280_dma),
2805 0 /* no alignment requirements */,
2806 0 /* or page-crossing issues */);
2807 if (!dev->requests) {
2808 DEBUG (dev, "can't get request pool\n");
2809 retval = -ENOMEM;
2810 goto done;
2812 for (i = 1; i < 5; i++) {
2813 struct net2280_dma *td;
2815 td = pci_pool_alloc (dev->requests, GFP_KERNEL,
2816 &dev->ep [i].td_dma);
2817 if (!td) {
2818 DEBUG (dev, "can't get dummy %d\n", i);
2819 retval = -ENOMEM;
2820 goto done;
2822 td->dmacount = 0; /* not VALID */
2823 td->dmaaddr = cpu_to_le32 (DMA_ADDR_INVALID);
2824 td->dmadesc = td->dmaaddr;
2825 dev->ep [i].dummy = td;
2828 /* enable lower-overhead pci memory bursts during DMA */
2829 writel ( (1 << DMA_MEMORY_WRITE_AND_INVALIDATE_ENABLE)
2830 // 256 write retries may not be enough...
2831 // | (1 << PCI_RETRY_ABORT_ENABLE)
2832 | (1 << DMA_READ_MULTIPLE_ENABLE)
2833 | (1 << DMA_READ_LINE_ENABLE)
2834 , &dev->pci->pcimstctl);
2835 /* erratum 0115 shouldn't appear: Linux inits PCI_LATENCY_TIMER */
2836 pci_set_master (pdev);
2837 pci_try_set_mwi (pdev);
2839 /* ... also flushes any posted pci writes */
2840 dev->chiprev = get_idx_reg (dev->regs, REG_CHIPREV) & 0xffff;
2842 /* done */
2843 INFO (dev, "%s\n", driver_desc);
2844 INFO (dev, "irq %d, pci mem %p, chip rev %04x\n",
2845 pdev->irq, base, dev->chiprev);
2846 INFO (dev, "version: " DRIVER_VERSION "; dma %s\n",
2847 use_dma
2848 ? (use_dma_chaining ? "chaining" : "enabled")
2849 : "disabled");
2850 the_controller = dev;
2852 retval = device_register (&dev->gadget.dev);
2853 if (retval) goto done;
2854 retval = device_create_file (&pdev->dev, &dev_attr_registers);
2855 if (retval) goto done;
2857 retval = usb_add_gadget_udc(&pdev->dev, &dev->gadget);
2858 if (retval)
2859 goto done;
2860 return 0;
2862 done:
2863 if (dev)
2864 net2280_remove (pdev);
2865 return retval;
2868 /* make sure the board is quiescent; otherwise it will continue
2869 * generating IRQs across the upcoming reboot.
2872 static void net2280_shutdown (struct pci_dev *pdev)
2874 struct net2280 *dev = pci_get_drvdata (pdev);
2876 /* disable IRQs */
2877 writel (0, &dev->regs->pciirqenb0);
2878 writel (0, &dev->regs->pciirqenb1);
2880 /* disable the pullup so the host will think we're gone */
2881 writel (0, &dev->usb->usbctl);
2885 /*-------------------------------------------------------------------------*/
2887 static const struct pci_device_id pci_ids [] = { {
2888 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
2889 .class_mask = ~0,
2890 .vendor = 0x17cc,
2891 .device = 0x2280,
2892 .subvendor = PCI_ANY_ID,
2893 .subdevice = PCI_ANY_ID,
2894 }, {
2895 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
2896 .class_mask = ~0,
2897 .vendor = 0x17cc,
2898 .device = 0x2282,
2899 .subvendor = PCI_ANY_ID,
2900 .subdevice = PCI_ANY_ID,
2902 }, { /* end: all zeroes */ }
2904 MODULE_DEVICE_TABLE (pci, pci_ids);
2906 /* pci driver glue; this is a "new style" PCI driver module */
2907 static struct pci_driver net2280_pci_driver = {
2908 .name = (char *) driver_name,
2909 .id_table = pci_ids,
2911 .probe = net2280_probe,
2912 .remove = net2280_remove,
2913 .shutdown = net2280_shutdown,
2915 /* FIXME add power management support */
2918 MODULE_DESCRIPTION (DRIVER_DESC);
2919 MODULE_AUTHOR ("David Brownell");
2920 MODULE_LICENSE ("GPL");
2922 static int __init init (void)
2924 if (!use_dma)
2925 use_dma_chaining = 0;
2926 return pci_register_driver (&net2280_pci_driver);
2928 module_init (init);
2930 static void __exit cleanup (void)
2932 pci_unregister_driver (&net2280_pci_driver);
2934 module_exit (cleanup);