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