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