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