diff options
author | Linus Torvalds <torvalds@ppc970.osdl.org> | 2005-04-16 18:20:36 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@ppc970.osdl.org> | 2005-04-16 18:20:36 -0400 |
commit | 1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch) | |
tree | 0bba044c4ce775e45a88a51686b5d9f90697ea9d /drivers/usb/gadget/goku_udc.c |
Linux-2.6.12-rc2v2.6.12-rc2
Initial git repository build. I'm not bothering with the full history,
even though we have it. We can create a separate "historical" git
archive of that later if we want to, and in the meantime it's about
3.2GB when imported into git - space that would just make the early
git days unnecessarily complicated, when we don't have a lot of good
infrastructure for it.
Let it rip!
Diffstat (limited to 'drivers/usb/gadget/goku_udc.c')
-rw-r--r-- | drivers/usb/gadget/goku_udc.c | 1984 |
1 files changed, 1984 insertions, 0 deletions
diff --git a/drivers/usb/gadget/goku_udc.c b/drivers/usb/gadget/goku_udc.c new file mode 100644 index 000000000000..005db7cca292 --- /dev/null +++ b/drivers/usb/gadget/goku_udc.c | |||
@@ -0,0 +1,1984 @@ | |||
1 | /* | ||
2 | * Toshiba TC86C001 ("Goku-S") USB Device Controller driver | ||
3 | * | ||
4 | * Copyright (C) 2000-2002 Lineo | ||
5 | * by Stuart Lynne, Tom Rushworth, and Bruce Balden | ||
6 | * Copyright (C) 2002 Toshiba Corporation | ||
7 | * Copyright (C) 2003 MontaVista Software (source@mvista.com) | ||
8 | * | ||
9 | * This file is licensed under the terms of the GNU General Public | ||
10 | * License version 2. This program is licensed "as is" without any | ||
11 | * warranty of any kind, whether express or implied. | ||
12 | */ | ||
13 | |||
14 | /* | ||
15 | * This device has ep0 and three semi-configurable bulk/interrupt endpoints. | ||
16 | * | ||
17 | * - Endpoint numbering is fixed: ep{1,2,3}-bulk | ||
18 | * - Gadget drivers can choose ep maxpacket (8/16/32/64) | ||
19 | * - Gadget drivers can choose direction (IN, OUT) | ||
20 | * - DMA works with ep1 (OUT transfers) and ep2 (IN transfers). | ||
21 | */ | ||
22 | |||
23 | #undef DEBUG | ||
24 | // #define VERBOSE /* extra debug messages (success too) */ | ||
25 | // #define USB_TRACE /* packet-level success messages */ | ||
26 | |||
27 | #include <linux/config.h> | ||
28 | #include <linux/kernel.h> | ||
29 | #include <linux/module.h> | ||
30 | #include <linux/pci.h> | ||
31 | #include <linux/delay.h> | ||
32 | #include <linux/ioport.h> | ||
33 | #include <linux/sched.h> | ||
34 | #include <linux/slab.h> | ||
35 | #include <linux/smp_lock.h> | ||
36 | #include <linux/errno.h> | ||
37 | #include <linux/init.h> | ||
38 | #include <linux/timer.h> | ||
39 | #include <linux/list.h> | ||
40 | #include <linux/interrupt.h> | ||
41 | #include <linux/proc_fs.h> | ||
42 | #include <linux/device.h> | ||
43 | #include <linux/usb_ch9.h> | ||
44 | #include <linux/usb_gadget.h> | ||
45 | |||
46 | #include <asm/byteorder.h> | ||
47 | #include <asm/io.h> | ||
48 | #include <asm/irq.h> | ||
49 | #include <asm/system.h> | ||
50 | #include <asm/unaligned.h> | ||
51 | |||
52 | |||
53 | #include "goku_udc.h" | ||
54 | |||
55 | #define DRIVER_DESC "TC86C001 USB Device Controller" | ||
56 | #define DRIVER_VERSION "30-Oct 2003" | ||
57 | |||
58 | #define DMA_ADDR_INVALID (~(dma_addr_t)0) | ||
59 | |||
60 | static const char driver_name [] = "goku_udc"; | ||
61 | static const char driver_desc [] = DRIVER_DESC; | ||
62 | |||
63 | MODULE_AUTHOR("source@mvista.com"); | ||
64 | MODULE_DESCRIPTION(DRIVER_DESC); | ||
65 | MODULE_LICENSE("GPL"); | ||
66 | |||
67 | |||
68 | /* | ||
69 | * IN dma behaves ok under testing, though the IN-dma abort paths don't | ||
70 | * seem to behave quite as expected. Used by default. | ||
71 | * | ||
72 | * OUT dma documents design problems handling the common "short packet" | ||
73 | * transfer termination policy; it couldn't enabled by default, even | ||
74 | * if the OUT-dma abort problems had a resolution. | ||
75 | */ | ||
76 | static unsigned use_dma = 1; | ||
77 | |||
78 | #if 0 | ||
79 | //#include <linux/moduleparam.h> | ||
80 | /* "modprobe goku_udc use_dma=1" etc | ||
81 | * 0 to disable dma | ||
82 | * 1 to use IN dma only (normal operation) | ||
83 | * 2 to use IN and OUT dma | ||
84 | */ | ||
85 | module_param(use_dma, uint, S_IRUGO); | ||
86 | #endif | ||
87 | |||
88 | /*-------------------------------------------------------------------------*/ | ||
89 | |||
90 | static void nuke(struct goku_ep *, int status); | ||
91 | |||
92 | static inline void | ||
93 | command(struct goku_udc_regs __iomem *regs, int command, unsigned epnum) | ||
94 | { | ||
95 | writel(COMMAND_EP(epnum) | command, ®s->Command); | ||
96 | udelay(300); | ||
97 | } | ||
98 | |||
99 | static int | ||
100 | goku_ep_enable(struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc) | ||
101 | { | ||
102 | struct goku_udc *dev; | ||
103 | struct goku_ep *ep; | ||
104 | u32 mode; | ||
105 | u16 max; | ||
106 | unsigned long flags; | ||
107 | |||
108 | ep = container_of(_ep, struct goku_ep, ep); | ||
109 | if (!_ep || !desc || ep->desc | ||
110 | || desc->bDescriptorType != USB_DT_ENDPOINT) | ||
111 | return -EINVAL; | ||
112 | dev = ep->dev; | ||
113 | if (ep == &dev->ep[0]) | ||
114 | return -EINVAL; | ||
115 | if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) | ||
116 | return -ESHUTDOWN; | ||
117 | if (ep->num != (desc->bEndpointAddress & 0x0f)) | ||
118 | return -EINVAL; | ||
119 | |||
120 | switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) { | ||
121 | case USB_ENDPOINT_XFER_BULK: | ||
122 | case USB_ENDPOINT_XFER_INT: | ||
123 | break; | ||
124 | default: | ||
125 | return -EINVAL; | ||
126 | } | ||
127 | |||
128 | if ((readl(ep->reg_status) & EPxSTATUS_EP_MASK) | ||
129 | != EPxSTATUS_EP_INVALID) | ||
130 | return -EBUSY; | ||
131 | |||
132 | /* enabling the no-toggle interrupt mode would need an api hook */ | ||
133 | mode = 0; | ||
134 | max = le16_to_cpu(get_unaligned(&desc->wMaxPacketSize)); | ||
135 | switch (max) { | ||
136 | case 64: mode++; | ||
137 | case 32: mode++; | ||
138 | case 16: mode++; | ||
139 | case 8: mode <<= 3; | ||
140 | break; | ||
141 | default: | ||
142 | return -EINVAL; | ||
143 | } | ||
144 | mode |= 2 << 1; /* bulk, or intr-with-toggle */ | ||
145 | |||
146 | /* ep1/ep2 dma direction is chosen early; it works in the other | ||
147 | * direction, with pio. be cautious with out-dma. | ||
148 | */ | ||
149 | ep->is_in = (USB_DIR_IN & desc->bEndpointAddress) != 0; | ||
150 | if (ep->is_in) { | ||
151 | mode |= 1; | ||
152 | ep->dma = (use_dma != 0) && (ep->num == UDC_MSTRD_ENDPOINT); | ||
153 | } else { | ||
154 | ep->dma = (use_dma == 2) && (ep->num == UDC_MSTWR_ENDPOINT); | ||
155 | if (ep->dma) | ||
156 | DBG(dev, "%s out-dma hides short packets\n", | ||
157 | ep->ep.name); | ||
158 | } | ||
159 | |||
160 | spin_lock_irqsave(&ep->dev->lock, flags); | ||
161 | |||
162 | /* ep1 and ep2 can do double buffering and/or dma */ | ||
163 | if (ep->num < 3) { | ||
164 | struct goku_udc_regs __iomem *regs = ep->dev->regs; | ||
165 | u32 tmp; | ||
166 | |||
167 | /* double buffer except (for now) with pio in */ | ||
168 | tmp = ((ep->dma || !ep->is_in) | ||
169 | ? 0x10 /* double buffered */ | ||
170 | : 0x11 /* single buffer */ | ||
171 | ) << ep->num; | ||
172 | tmp |= readl(®s->EPxSingle); | ||
173 | writel(tmp, ®s->EPxSingle); | ||
174 | |||
175 | tmp = (ep->dma ? 0x10/*dma*/ : 0x11/*pio*/) << ep->num; | ||
176 | tmp |= readl(®s->EPxBCS); | ||
177 | writel(tmp, ®s->EPxBCS); | ||
178 | } | ||
179 | writel(mode, ep->reg_mode); | ||
180 | command(ep->dev->regs, COMMAND_RESET, ep->num); | ||
181 | ep->ep.maxpacket = max; | ||
182 | ep->stopped = 0; | ||
183 | ep->desc = desc; | ||
184 | spin_unlock_irqrestore(&ep->dev->lock, flags); | ||
185 | |||
186 | DBG(dev, "enable %s %s %s maxpacket %u\n", ep->ep.name, | ||
187 | ep->is_in ? "IN" : "OUT", | ||
188 | ep->dma ? "dma" : "pio", | ||
189 | max); | ||
190 | |||
191 | return 0; | ||
192 | } | ||
193 | |||
194 | static void ep_reset(struct goku_udc_regs __iomem *regs, struct goku_ep *ep) | ||
195 | { | ||
196 | struct goku_udc *dev = ep->dev; | ||
197 | |||
198 | if (regs) { | ||
199 | command(regs, COMMAND_INVALID, ep->num); | ||
200 | if (ep->num) { | ||
201 | if (ep->num == UDC_MSTWR_ENDPOINT) | ||
202 | dev->int_enable &= ~(INT_MSTWREND | ||
203 | |INT_MSTWRTMOUT); | ||
204 | else if (ep->num == UDC_MSTRD_ENDPOINT) | ||
205 | dev->int_enable &= ~INT_MSTRDEND; | ||
206 | dev->int_enable &= ~INT_EPxDATASET (ep->num); | ||
207 | } else | ||
208 | dev->int_enable &= ~INT_EP0; | ||
209 | writel(dev->int_enable, ®s->int_enable); | ||
210 | readl(®s->int_enable); | ||
211 | if (ep->num < 3) { | ||
212 | struct goku_udc_regs __iomem *r = ep->dev->regs; | ||
213 | u32 tmp; | ||
214 | |||
215 | tmp = readl(&r->EPxSingle); | ||
216 | tmp &= ~(0x11 << ep->num); | ||
217 | writel(tmp, &r->EPxSingle); | ||
218 | |||
219 | tmp = readl(&r->EPxBCS); | ||
220 | tmp &= ~(0x11 << ep->num); | ||
221 | writel(tmp, &r->EPxBCS); | ||
222 | } | ||
223 | /* reset dma in case we're still using it */ | ||
224 | if (ep->dma) { | ||
225 | u32 master; | ||
226 | |||
227 | master = readl(®s->dma_master) & MST_RW_BITS; | ||
228 | if (ep->num == UDC_MSTWR_ENDPOINT) { | ||
229 | master &= ~MST_W_BITS; | ||
230 | master |= MST_WR_RESET; | ||
231 | } else { | ||
232 | master &= ~MST_R_BITS; | ||
233 | master |= MST_RD_RESET; | ||
234 | } | ||
235 | writel(master, ®s->dma_master); | ||
236 | } | ||
237 | } | ||
238 | |||
239 | ep->ep.maxpacket = MAX_FIFO_SIZE; | ||
240 | ep->desc = NULL; | ||
241 | ep->stopped = 1; | ||
242 | ep->irqs = 0; | ||
243 | ep->dma = 0; | ||
244 | } | ||
245 | |||
246 | static int goku_ep_disable(struct usb_ep *_ep) | ||
247 | { | ||
248 | struct goku_ep *ep; | ||
249 | struct goku_udc *dev; | ||
250 | unsigned long flags; | ||
251 | |||
252 | ep = container_of(_ep, struct goku_ep, ep); | ||
253 | if (!_ep || !ep->desc) | ||
254 | return -ENODEV; | ||
255 | dev = ep->dev; | ||
256 | if (dev->ep0state == EP0_SUSPEND) | ||
257 | return -EBUSY; | ||
258 | |||
259 | VDBG(dev, "disable %s\n", _ep->name); | ||
260 | |||
261 | spin_lock_irqsave(&dev->lock, flags); | ||
262 | nuke(ep, -ESHUTDOWN); | ||
263 | ep_reset(dev->regs, ep); | ||
264 | spin_unlock_irqrestore(&dev->lock, flags); | ||
265 | |||
266 | return 0; | ||
267 | } | ||
268 | |||
269 | /*-------------------------------------------------------------------------*/ | ||
270 | |||
271 | static struct usb_request * | ||
272 | goku_alloc_request(struct usb_ep *_ep, int gfp_flags) | ||
273 | { | ||
274 | struct goku_request *req; | ||
275 | |||
276 | if (!_ep) | ||
277 | return NULL; | ||
278 | req = kmalloc(sizeof *req, gfp_flags); | ||
279 | if (!req) | ||
280 | return NULL; | ||
281 | |||
282 | memset(req, 0, sizeof *req); | ||
283 | req->req.dma = DMA_ADDR_INVALID; | ||
284 | INIT_LIST_HEAD(&req->queue); | ||
285 | return &req->req; | ||
286 | } | ||
287 | |||
288 | static void | ||
289 | goku_free_request(struct usb_ep *_ep, struct usb_request *_req) | ||
290 | { | ||
291 | struct goku_request *req; | ||
292 | |||
293 | if (!_ep || !_req) | ||
294 | return; | ||
295 | |||
296 | req = container_of(_req, struct goku_request, req); | ||
297 | WARN_ON(!list_empty(&req->queue)); | ||
298 | kfree(req); | ||
299 | } | ||
300 | |||
301 | /*-------------------------------------------------------------------------*/ | ||
302 | |||
303 | #undef USE_KMALLOC | ||
304 | |||
305 | /* many common platforms have dma-coherent caches, which means that it's | ||
306 | * safe to use kmalloc() memory for all i/o buffers without using any | ||
307 | * cache flushing calls. (unless you're trying to share cache lines | ||
308 | * between dma and non-dma activities, which is a slow idea in any case.) | ||
309 | * | ||
310 | * other platforms need more care, with 2.6 having a moderately general | ||
311 | * solution except for the common "buffer is smaller than a page" case. | ||
312 | */ | ||
313 | #if defined(CONFIG_X86) | ||
314 | #define USE_KMALLOC | ||
315 | |||
316 | #elif defined(CONFIG_MIPS) && !defined(CONFIG_NONCOHERENT_IO) | ||
317 | #define USE_KMALLOC | ||
318 | |||
319 | #elif defined(CONFIG_PPC) && !defined(CONFIG_NOT_COHERENT_CACHE) | ||
320 | #define USE_KMALLOC | ||
321 | |||
322 | #endif | ||
323 | |||
324 | /* allocating buffers this way eliminates dma mapping overhead, which | ||
325 | * on some platforms will mean eliminating a per-io buffer copy. with | ||
326 | * some kinds of system caches, further tweaks may still be needed. | ||
327 | */ | ||
328 | static void * | ||
329 | goku_alloc_buffer(struct usb_ep *_ep, unsigned bytes, | ||
330 | dma_addr_t *dma, int gfp_flags) | ||
331 | { | ||
332 | void *retval; | ||
333 | struct goku_ep *ep; | ||
334 | |||
335 | ep = container_of(_ep, struct goku_ep, ep); | ||
336 | if (!_ep) | ||
337 | return NULL; | ||
338 | *dma = DMA_ADDR_INVALID; | ||
339 | |||
340 | #if defined(USE_KMALLOC) | ||
341 | retval = kmalloc(bytes, gfp_flags); | ||
342 | if (retval) | ||
343 | *dma = virt_to_phys(retval); | ||
344 | #else | ||
345 | if (ep->dma) { | ||
346 | /* the main problem with this call is that it wastes memory | ||
347 | * on typical 1/N page allocations: it allocates 1-N pages. | ||
348 | */ | ||
349 | #warning Using dma_alloc_coherent even with buffers smaller than a page. | ||
350 | retval = dma_alloc_coherent(&ep->dev->pdev->dev, | ||
351 | bytes, dma, gfp_flags); | ||
352 | } else | ||
353 | retval = kmalloc(bytes, gfp_flags); | ||
354 | #endif | ||
355 | return retval; | ||
356 | } | ||
357 | |||
358 | static void | ||
359 | goku_free_buffer(struct usb_ep *_ep, void *buf, dma_addr_t dma, unsigned bytes) | ||
360 | { | ||
361 | /* free memory into the right allocator */ | ||
362 | #ifndef USE_KMALLOC | ||
363 | if (dma != DMA_ADDR_INVALID) { | ||
364 | struct goku_ep *ep; | ||
365 | |||
366 | ep = container_of(_ep, struct goku_ep, ep); | ||
367 | if (!_ep) | ||
368 | return; | ||
369 | dma_free_coherent(&ep->dev->pdev->dev, bytes, buf, dma); | ||
370 | } else | ||
371 | #endif | ||
372 | kfree (buf); | ||
373 | } | ||
374 | |||
375 | /*-------------------------------------------------------------------------*/ | ||
376 | |||
377 | static void | ||
378 | done(struct goku_ep *ep, struct goku_request *req, int status) | ||
379 | { | ||
380 | struct goku_udc *dev; | ||
381 | unsigned stopped = ep->stopped; | ||
382 | |||
383 | list_del_init(&req->queue); | ||
384 | |||
385 | if (likely(req->req.status == -EINPROGRESS)) | ||
386 | req->req.status = status; | ||
387 | else | ||
388 | status = req->req.status; | ||
389 | |||
390 | dev = ep->dev; | ||
391 | if (req->mapped) { | ||
392 | pci_unmap_single(dev->pdev, req->req.dma, req->req.length, | ||
393 | ep->is_in ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE); | ||
394 | req->req.dma = DMA_ADDR_INVALID; | ||
395 | req->mapped = 0; | ||
396 | } | ||
397 | |||
398 | #ifndef USB_TRACE | ||
399 | if (status && status != -ESHUTDOWN) | ||
400 | #endif | ||
401 | VDBG(dev, "complete %s req %p stat %d len %u/%u\n", | ||
402 | ep->ep.name, &req->req, status, | ||
403 | req->req.actual, req->req.length); | ||
404 | |||
405 | /* don't modify queue heads during completion callback */ | ||
406 | ep->stopped = 1; | ||
407 | spin_unlock(&dev->lock); | ||
408 | req->req.complete(&ep->ep, &req->req); | ||
409 | spin_lock(&dev->lock); | ||
410 | ep->stopped = stopped; | ||
411 | } | ||
412 | |||
413 | /*-------------------------------------------------------------------------*/ | ||
414 | |||
415 | static inline int | ||
416 | write_packet(u32 __iomem *fifo, u8 *buf, struct goku_request *req, unsigned max) | ||
417 | { | ||
418 | unsigned length, count; | ||
419 | |||
420 | length = min(req->req.length - req->req.actual, max); | ||
421 | req->req.actual += length; | ||
422 | |||
423 | count = length; | ||
424 | while (likely(count--)) | ||
425 | writel(*buf++, fifo); | ||
426 | return length; | ||
427 | } | ||
428 | |||
429 | // return: 0 = still running, 1 = completed, negative = errno | ||
430 | static int write_fifo(struct goku_ep *ep, struct goku_request *req) | ||
431 | { | ||
432 | struct goku_udc *dev = ep->dev; | ||
433 | u32 tmp; | ||
434 | u8 *buf; | ||
435 | unsigned count; | ||
436 | int is_last; | ||
437 | |||
438 | tmp = readl(&dev->regs->DataSet); | ||
439 | buf = req->req.buf + req->req.actual; | ||
440 | prefetch(buf); | ||
441 | |||
442 | dev = ep->dev; | ||
443 | if (unlikely(ep->num == 0 && dev->ep0state != EP0_IN)) | ||
444 | return -EL2HLT; | ||
445 | |||
446 | /* NOTE: just single-buffered PIO-IN for now. */ | ||
447 | if (unlikely((tmp & DATASET_A(ep->num)) != 0)) | ||
448 | return 0; | ||
449 | |||
450 | /* clear our "packet available" irq */ | ||
451 | if (ep->num != 0) | ||
452 | writel(~INT_EPxDATASET(ep->num), &dev->regs->int_status); | ||
453 | |||
454 | count = write_packet(ep->reg_fifo, buf, req, ep->ep.maxpacket); | ||
455 | |||
456 | /* last packet often short (sometimes a zlp, especially on ep0) */ | ||
457 | if (unlikely(count != ep->ep.maxpacket)) { | ||
458 | writel(~(1<<ep->num), &dev->regs->EOP); | ||
459 | if (ep->num == 0) { | ||
460 | dev->ep[0].stopped = 1; | ||
461 | dev->ep0state = EP0_STATUS; | ||
462 | } | ||
463 | is_last = 1; | ||
464 | } else { | ||
465 | if (likely(req->req.length != req->req.actual) | ||
466 | || req->req.zero) | ||
467 | is_last = 0; | ||
468 | else | ||
469 | is_last = 1; | ||
470 | } | ||
471 | #if 0 /* printk seemed to trash is_last...*/ | ||
472 | //#ifdef USB_TRACE | ||
473 | VDBG(dev, "wrote %s %u bytes%s IN %u left %p\n", | ||
474 | ep->ep.name, count, is_last ? "/last" : "", | ||
475 | req->req.length - req->req.actual, req); | ||
476 | #endif | ||
477 | |||
478 | /* requests complete when all IN data is in the FIFO, | ||
479 | * or sometimes later, if a zlp was needed. | ||
480 | */ | ||
481 | if (is_last) { | ||
482 | done(ep, req, 0); | ||
483 | return 1; | ||
484 | } | ||
485 | |||
486 | return 0; | ||
487 | } | ||
488 | |||
489 | static int read_fifo(struct goku_ep *ep, struct goku_request *req) | ||
490 | { | ||
491 | struct goku_udc_regs __iomem *regs; | ||
492 | u32 size, set; | ||
493 | u8 *buf; | ||
494 | unsigned bufferspace, is_short, dbuff; | ||
495 | |||
496 | regs = ep->dev->regs; | ||
497 | top: | ||
498 | buf = req->req.buf + req->req.actual; | ||
499 | prefetchw(buf); | ||
500 | |||
501 | if (unlikely(ep->num == 0 && ep->dev->ep0state != EP0_OUT)) | ||
502 | return -EL2HLT; | ||
503 | |||
504 | dbuff = (ep->num == 1 || ep->num == 2); | ||
505 | do { | ||
506 | /* ack dataset irq matching the status we'll handle */ | ||
507 | if (ep->num != 0) | ||
508 | writel(~INT_EPxDATASET(ep->num), ®s->int_status); | ||
509 | |||
510 | set = readl(®s->DataSet) & DATASET_AB(ep->num); | ||
511 | size = readl(®s->EPxSizeLA[ep->num]); | ||
512 | bufferspace = req->req.length - req->req.actual; | ||
513 | |||
514 | /* usually do nothing without an OUT packet */ | ||
515 | if (likely(ep->num != 0 || bufferspace != 0)) { | ||
516 | if (unlikely(set == 0)) | ||
517 | break; | ||
518 | /* use ep1/ep2 double-buffering for OUT */ | ||
519 | if (!(size & PACKET_ACTIVE)) | ||
520 | size = readl(®s->EPxSizeLB[ep->num]); | ||
521 | if (!(size & PACKET_ACTIVE)) // "can't happen" | ||
522 | break; | ||
523 | size &= DATASIZE; /* EPxSizeH == 0 */ | ||
524 | |||
525 | /* ep0out no-out-data case for set_config, etc */ | ||
526 | } else | ||
527 | size = 0; | ||
528 | |||
529 | /* read all bytes from this packet */ | ||
530 | req->req.actual += size; | ||
531 | is_short = (size < ep->ep.maxpacket); | ||
532 | #ifdef USB_TRACE | ||
533 | VDBG(ep->dev, "read %s %u bytes%s OUT req %p %u/%u\n", | ||
534 | ep->ep.name, size, is_short ? "/S" : "", | ||
535 | req, req->req.actual, req->req.length); | ||
536 | #endif | ||
537 | while (likely(size-- != 0)) { | ||
538 | u8 byte = (u8) readl(ep->reg_fifo); | ||
539 | |||
540 | if (unlikely(bufferspace == 0)) { | ||
541 | /* this happens when the driver's buffer | ||
542 | * is smaller than what the host sent. | ||
543 | * discard the extra data in this packet. | ||
544 | */ | ||
545 | if (req->req.status != -EOVERFLOW) | ||
546 | DBG(ep->dev, "%s overflow %u\n", | ||
547 | ep->ep.name, size); | ||
548 | req->req.status = -EOVERFLOW; | ||
549 | } else { | ||
550 | *buf++ = byte; | ||
551 | bufferspace--; | ||
552 | } | ||
553 | } | ||
554 | |||
555 | /* completion */ | ||
556 | if (unlikely(is_short || req->req.actual == req->req.length)) { | ||
557 | if (unlikely(ep->num == 0)) { | ||
558 | /* non-control endpoints now usable? */ | ||
559 | if (ep->dev->req_config) | ||
560 | writel(ep->dev->configured | ||
561 | ? USBSTATE_CONFIGURED | ||
562 | : 0, | ||
563 | ®s->UsbState); | ||
564 | /* ep0out status stage */ | ||
565 | writel(~(1<<0), ®s->EOP); | ||
566 | ep->stopped = 1; | ||
567 | ep->dev->ep0state = EP0_STATUS; | ||
568 | } | ||
569 | done(ep, req, 0); | ||
570 | |||
571 | /* empty the second buffer asap */ | ||
572 | if (dbuff && !list_empty(&ep->queue)) { | ||
573 | req = list_entry(ep->queue.next, | ||
574 | struct goku_request, queue); | ||
575 | goto top; | ||
576 | } | ||
577 | return 1; | ||
578 | } | ||
579 | } while (dbuff); | ||
580 | return 0; | ||
581 | } | ||
582 | |||
583 | static inline void | ||
584 | pio_irq_enable(struct goku_udc *dev, | ||
585 | struct goku_udc_regs __iomem *regs, int epnum) | ||
586 | { | ||
587 | dev->int_enable |= INT_EPxDATASET (epnum); | ||
588 | writel(dev->int_enable, ®s->int_enable); | ||
589 | /* write may still be posted */ | ||
590 | } | ||
591 | |||
592 | static inline void | ||
593 | pio_irq_disable(struct goku_udc *dev, | ||
594 | struct goku_udc_regs __iomem *regs, int epnum) | ||
595 | { | ||
596 | dev->int_enable &= ~INT_EPxDATASET (epnum); | ||
597 | writel(dev->int_enable, ®s->int_enable); | ||
598 | /* write may still be posted */ | ||
599 | } | ||
600 | |||
601 | static inline void | ||
602 | pio_advance(struct goku_ep *ep) | ||
603 | { | ||
604 | struct goku_request *req; | ||
605 | |||
606 | if (unlikely(list_empty (&ep->queue))) | ||
607 | return; | ||
608 | req = list_entry(ep->queue.next, struct goku_request, queue); | ||
609 | (ep->is_in ? write_fifo : read_fifo)(ep, req); | ||
610 | } | ||
611 | |||
612 | |||
613 | /*-------------------------------------------------------------------------*/ | ||
614 | |||
615 | // return: 0 = q running, 1 = q stopped, negative = errno | ||
616 | static int start_dma(struct goku_ep *ep, struct goku_request *req) | ||
617 | { | ||
618 | struct goku_udc_regs __iomem *regs = ep->dev->regs; | ||
619 | u32 master; | ||
620 | u32 start = req->req.dma; | ||
621 | u32 end = start + req->req.length - 1; | ||
622 | |||
623 | master = readl(®s->dma_master) & MST_RW_BITS; | ||
624 | |||
625 | /* re-init the bits affecting IN dma; careful with zlps */ | ||
626 | if (likely(ep->is_in)) { | ||
627 | if (unlikely(master & MST_RD_ENA)) { | ||
628 | DBG (ep->dev, "start, IN active dma %03x!!\n", | ||
629 | master); | ||
630 | // return -EL2HLT; | ||
631 | } | ||
632 | writel(end, ®s->in_dma_end); | ||
633 | writel(start, ®s->in_dma_start); | ||
634 | |||
635 | master &= ~MST_R_BITS; | ||
636 | if (unlikely(req->req.length == 0)) | ||
637 | master = MST_RD_ENA | MST_RD_EOPB; | ||
638 | else if ((req->req.length % ep->ep.maxpacket) != 0 | ||
639 | || req->req.zero) | ||
640 | master = MST_RD_ENA | MST_EOPB_ENA; | ||
641 | else | ||
642 | master = MST_RD_ENA | MST_EOPB_DIS; | ||
643 | |||
644 | ep->dev->int_enable |= INT_MSTRDEND; | ||
645 | |||
646 | /* Goku DMA-OUT merges short packets, which plays poorly with | ||
647 | * protocols where short packets mark the transfer boundaries. | ||
648 | * The chip supports a nonstandard policy with INT_MSTWRTMOUT, | ||
649 | * ending transfers after 3 SOFs; we don't turn it on. | ||
650 | */ | ||
651 | } else { | ||
652 | if (unlikely(master & MST_WR_ENA)) { | ||
653 | DBG (ep->dev, "start, OUT active dma %03x!!\n", | ||
654 | master); | ||
655 | // return -EL2HLT; | ||
656 | } | ||
657 | writel(end, ®s->out_dma_end); | ||
658 | writel(start, ®s->out_dma_start); | ||
659 | |||
660 | master &= ~MST_W_BITS; | ||
661 | master |= MST_WR_ENA | MST_TIMEOUT_DIS; | ||
662 | |||
663 | ep->dev->int_enable |= INT_MSTWREND|INT_MSTWRTMOUT; | ||
664 | } | ||
665 | |||
666 | writel(master, ®s->dma_master); | ||
667 | writel(ep->dev->int_enable, ®s->int_enable); | ||
668 | return 0; | ||
669 | } | ||
670 | |||
671 | static void dma_advance(struct goku_udc *dev, struct goku_ep *ep) | ||
672 | { | ||
673 | struct goku_request *req; | ||
674 | struct goku_udc_regs __iomem *regs = ep->dev->regs; | ||
675 | u32 master; | ||
676 | |||
677 | master = readl(®s->dma_master); | ||
678 | |||
679 | if (unlikely(list_empty(&ep->queue))) { | ||
680 | stop: | ||
681 | if (ep->is_in) | ||
682 | dev->int_enable &= ~INT_MSTRDEND; | ||
683 | else | ||
684 | dev->int_enable &= ~(INT_MSTWREND|INT_MSTWRTMOUT); | ||
685 | writel(dev->int_enable, ®s->int_enable); | ||
686 | return; | ||
687 | } | ||
688 | req = list_entry(ep->queue.next, struct goku_request, queue); | ||
689 | |||
690 | /* normal hw dma completion (not abort) */ | ||
691 | if (likely(ep->is_in)) { | ||
692 | if (unlikely(master & MST_RD_ENA)) | ||
693 | return; | ||
694 | req->req.actual = readl(®s->in_dma_current); | ||
695 | } else { | ||
696 | if (unlikely(master & MST_WR_ENA)) | ||
697 | return; | ||
698 | |||
699 | /* hardware merges short packets, and also hides packet | ||
700 | * overruns. a partial packet MAY be in the fifo here. | ||
701 | */ | ||
702 | req->req.actual = readl(®s->out_dma_current); | ||
703 | } | ||
704 | req->req.actual -= req->req.dma; | ||
705 | req->req.actual++; | ||
706 | |||
707 | #ifdef USB_TRACE | ||
708 | VDBG(dev, "done %s %s dma, %u/%u bytes, req %p\n", | ||
709 | ep->ep.name, ep->is_in ? "IN" : "OUT", | ||
710 | req->req.actual, req->req.length, req); | ||
711 | #endif | ||
712 | done(ep, req, 0); | ||
713 | if (list_empty(&ep->queue)) | ||
714 | goto stop; | ||
715 | req = list_entry(ep->queue.next, struct goku_request, queue); | ||
716 | (void) start_dma(ep, req); | ||
717 | } | ||
718 | |||
719 | static void abort_dma(struct goku_ep *ep, int status) | ||
720 | { | ||
721 | struct goku_udc_regs __iomem *regs = ep->dev->regs; | ||
722 | struct goku_request *req; | ||
723 | u32 curr, master; | ||
724 | |||
725 | /* NAK future host requests, hoping the implicit delay lets the | ||
726 | * dma engine finish reading (or writing) its latest packet and | ||
727 | * empty the dma buffer (up to 16 bytes). | ||
728 | * | ||
729 | * This avoids needing to clean up a partial packet in the fifo; | ||
730 | * we can't do that for IN without side effects to HALT and TOGGLE. | ||
731 | */ | ||
732 | command(regs, COMMAND_FIFO_DISABLE, ep->num); | ||
733 | req = list_entry(ep->queue.next, struct goku_request, queue); | ||
734 | master = readl(®s->dma_master) & MST_RW_BITS; | ||
735 | |||
736 | /* FIXME using these resets isn't usably documented. this may | ||
737 | * not work unless it's followed by disabling the endpoint. | ||
738 | * | ||
739 | * FIXME the OUT reset path doesn't even behave consistently. | ||
740 | */ | ||
741 | if (ep->is_in) { | ||
742 | if (unlikely((readl(®s->dma_master) & MST_RD_ENA) == 0)) | ||
743 | goto finished; | ||
744 | curr = readl(®s->in_dma_current); | ||
745 | |||
746 | writel(curr, ®s->in_dma_end); | ||
747 | writel(curr, ®s->in_dma_start); | ||
748 | |||
749 | master &= ~MST_R_BITS; | ||
750 | master |= MST_RD_RESET; | ||
751 | writel(master, ®s->dma_master); | ||
752 | |||
753 | if (readl(®s->dma_master) & MST_RD_ENA) | ||
754 | DBG(ep->dev, "IN dma active after reset!\n"); | ||
755 | |||
756 | } else { | ||
757 | if (unlikely((readl(®s->dma_master) & MST_WR_ENA) == 0)) | ||
758 | goto finished; | ||
759 | curr = readl(®s->out_dma_current); | ||
760 | |||
761 | writel(curr, ®s->out_dma_end); | ||
762 | writel(curr, ®s->out_dma_start); | ||
763 | |||
764 | master &= ~MST_W_BITS; | ||
765 | master |= MST_WR_RESET; | ||
766 | writel(master, ®s->dma_master); | ||
767 | |||
768 | if (readl(®s->dma_master) & MST_WR_ENA) | ||
769 | DBG(ep->dev, "OUT dma active after reset!\n"); | ||
770 | } | ||
771 | req->req.actual = (curr - req->req.dma) + 1; | ||
772 | req->req.status = status; | ||
773 | |||
774 | VDBG(ep->dev, "%s %s %s %d/%d\n", __FUNCTION__, ep->ep.name, | ||
775 | ep->is_in ? "IN" : "OUT", | ||
776 | req->req.actual, req->req.length); | ||
777 | |||
778 | command(regs, COMMAND_FIFO_ENABLE, ep->num); | ||
779 | |||
780 | return; | ||
781 | |||
782 | finished: | ||
783 | /* dma already completed; no abort needed */ | ||
784 | command(regs, COMMAND_FIFO_ENABLE, ep->num); | ||
785 | req->req.actual = req->req.length; | ||
786 | req->req.status = 0; | ||
787 | } | ||
788 | |||
789 | /*-------------------------------------------------------------------------*/ | ||
790 | |||
791 | static int | ||
792 | goku_queue(struct usb_ep *_ep, struct usb_request *_req, int gfp_flags) | ||
793 | { | ||
794 | struct goku_request *req; | ||
795 | struct goku_ep *ep; | ||
796 | struct goku_udc *dev; | ||
797 | unsigned long flags; | ||
798 | int status; | ||
799 | |||
800 | /* always require a cpu-view buffer so pio works */ | ||
801 | req = container_of(_req, struct goku_request, req); | ||
802 | if (unlikely(!_req || !_req->complete | ||
803 | || !_req->buf || !list_empty(&req->queue))) | ||
804 | return -EINVAL; | ||
805 | ep = container_of(_ep, struct goku_ep, ep); | ||
806 | if (unlikely(!_ep || (!ep->desc && ep->num != 0))) | ||
807 | return -EINVAL; | ||
808 | dev = ep->dev; | ||
809 | if (unlikely(!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)) | ||
810 | return -ESHUTDOWN; | ||
811 | |||
812 | /* can't touch registers when suspended */ | ||
813 | if (dev->ep0state == EP0_SUSPEND) | ||
814 | return -EBUSY; | ||
815 | |||
816 | /* set up dma mapping in case the caller didn't */ | ||
817 | if (ep->dma && _req->dma == DMA_ADDR_INVALID) { | ||
818 | _req->dma = pci_map_single(dev->pdev, _req->buf, _req->length, | ||
819 | ep->is_in ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE); | ||
820 | req->mapped = 1; | ||
821 | } | ||
822 | |||
823 | #ifdef USB_TRACE | ||
824 | VDBG(dev, "%s queue req %p, len %u buf %p\n", | ||
825 | _ep->name, _req, _req->length, _req->buf); | ||
826 | #endif | ||
827 | |||
828 | spin_lock_irqsave(&dev->lock, flags); | ||
829 | |||
830 | _req->status = -EINPROGRESS; | ||
831 | _req->actual = 0; | ||
832 | |||
833 | /* for ep0 IN without premature status, zlp is required and | ||
834 | * writing EOP starts the status stage (OUT). | ||
835 | */ | ||
836 | if (unlikely(ep->num == 0 && ep->is_in)) | ||
837 | _req->zero = 1; | ||
838 | |||
839 | /* kickstart this i/o queue? */ | ||
840 | status = 0; | ||
841 | if (list_empty(&ep->queue) && likely(!ep->stopped)) { | ||
842 | /* dma: done after dma completion IRQ (or error) | ||
843 | * pio: done after last fifo operation | ||
844 | */ | ||
845 | if (ep->dma) | ||
846 | status = start_dma(ep, req); | ||
847 | else | ||
848 | status = (ep->is_in ? write_fifo : read_fifo)(ep, req); | ||
849 | |||
850 | if (unlikely(status != 0)) { | ||
851 | if (status > 0) | ||
852 | status = 0; | ||
853 | req = NULL; | ||
854 | } | ||
855 | |||
856 | } /* else pio or dma irq handler advances the queue. */ | ||
857 | |||
858 | if (likely(req != 0)) | ||
859 | list_add_tail(&req->queue, &ep->queue); | ||
860 | |||
861 | if (likely(!list_empty(&ep->queue)) | ||
862 | && likely(ep->num != 0) | ||
863 | && !ep->dma | ||
864 | && !(dev->int_enable & INT_EPxDATASET (ep->num))) | ||
865 | pio_irq_enable(dev, dev->regs, ep->num); | ||
866 | |||
867 | spin_unlock_irqrestore(&dev->lock, flags); | ||
868 | |||
869 | /* pci writes may still be posted */ | ||
870 | return status; | ||
871 | } | ||
872 | |||
873 | /* dequeue ALL requests */ | ||
874 | static void nuke(struct goku_ep *ep, int status) | ||
875 | { | ||
876 | struct goku_request *req; | ||
877 | |||
878 | ep->stopped = 1; | ||
879 | if (list_empty(&ep->queue)) | ||
880 | return; | ||
881 | if (ep->dma) | ||
882 | abort_dma(ep, status); | ||
883 | while (!list_empty(&ep->queue)) { | ||
884 | req = list_entry(ep->queue.next, struct goku_request, queue); | ||
885 | done(ep, req, status); | ||
886 | } | ||
887 | } | ||
888 | |||
889 | /* dequeue JUST ONE request */ | ||
890 | static int goku_dequeue(struct usb_ep *_ep, struct usb_request *_req) | ||
891 | { | ||
892 | struct goku_request *req; | ||
893 | struct goku_ep *ep; | ||
894 | struct goku_udc *dev; | ||
895 | unsigned long flags; | ||
896 | |||
897 | ep = container_of(_ep, struct goku_ep, ep); | ||
898 | if (!_ep || !_req || (!ep->desc && ep->num != 0)) | ||
899 | return -EINVAL; | ||
900 | dev = ep->dev; | ||
901 | if (!dev->driver) | ||
902 | return -ESHUTDOWN; | ||
903 | |||
904 | /* we can't touch (dma) registers when suspended */ | ||
905 | if (dev->ep0state == EP0_SUSPEND) | ||
906 | return -EBUSY; | ||
907 | |||
908 | VDBG(dev, "%s %s %s %s %p\n", __FUNCTION__, _ep->name, | ||
909 | ep->is_in ? "IN" : "OUT", | ||
910 | ep->dma ? "dma" : "pio", | ||
911 | _req); | ||
912 | |||
913 | spin_lock_irqsave(&dev->lock, flags); | ||
914 | |||
915 | /* make sure it's actually queued on this endpoint */ | ||
916 | list_for_each_entry (req, &ep->queue, queue) { | ||
917 | if (&req->req == _req) | ||
918 | break; | ||
919 | } | ||
920 | if (&req->req != _req) { | ||
921 | spin_unlock_irqrestore (&dev->lock, flags); | ||
922 | return -EINVAL; | ||
923 | } | ||
924 | |||
925 | if (ep->dma && ep->queue.next == &req->queue && !ep->stopped) { | ||
926 | abort_dma(ep, -ECONNRESET); | ||
927 | done(ep, req, -ECONNRESET); | ||
928 | dma_advance(dev, ep); | ||
929 | } else if (!list_empty(&req->queue)) | ||
930 | done(ep, req, -ECONNRESET); | ||
931 | else | ||
932 | req = NULL; | ||
933 | spin_unlock_irqrestore(&dev->lock, flags); | ||
934 | |||
935 | return req ? 0 : -EOPNOTSUPP; | ||
936 | } | ||
937 | |||
938 | /*-------------------------------------------------------------------------*/ | ||
939 | |||
940 | static void goku_clear_halt(struct goku_ep *ep) | ||
941 | { | ||
942 | // assert (ep->num !=0) | ||
943 | VDBG(ep->dev, "%s clear halt\n", ep->ep.name); | ||
944 | command(ep->dev->regs, COMMAND_SETDATA0, ep->num); | ||
945 | command(ep->dev->regs, COMMAND_STALL_CLEAR, ep->num); | ||
946 | if (ep->stopped) { | ||
947 | ep->stopped = 0; | ||
948 | if (ep->dma) { | ||
949 | struct goku_request *req; | ||
950 | |||
951 | if (list_empty(&ep->queue)) | ||
952 | return; | ||
953 | req = list_entry(ep->queue.next, struct goku_request, | ||
954 | queue); | ||
955 | (void) start_dma(ep, req); | ||
956 | } else | ||
957 | pio_advance(ep); | ||
958 | } | ||
959 | } | ||
960 | |||
961 | static int goku_set_halt(struct usb_ep *_ep, int value) | ||
962 | { | ||
963 | struct goku_ep *ep; | ||
964 | unsigned long flags; | ||
965 | int retval = 0; | ||
966 | |||
967 | if (!_ep) | ||
968 | return -ENODEV; | ||
969 | ep = container_of (_ep, struct goku_ep, ep); | ||
970 | |||
971 | if (ep->num == 0) { | ||
972 | if (value) { | ||
973 | ep->dev->ep0state = EP0_STALL; | ||
974 | ep->dev->ep[0].stopped = 1; | ||
975 | } else | ||
976 | return -EINVAL; | ||
977 | |||
978 | /* don't change EPxSTATUS_EP_INVALID to READY */ | ||
979 | } else if (!ep->desc) { | ||
980 | DBG(ep->dev, "%s %s inactive?\n", __FUNCTION__, ep->ep.name); | ||
981 | return -EINVAL; | ||
982 | } | ||
983 | |||
984 | spin_lock_irqsave(&ep->dev->lock, flags); | ||
985 | if (!list_empty(&ep->queue)) | ||
986 | retval = -EAGAIN; | ||
987 | else if (ep->is_in && value | ||
988 | /* data in (either) packet buffer? */ | ||
989 | && (readl(&ep->dev->regs->DataSet) | ||
990 | & DATASET_AB(ep->num))) | ||
991 | retval = -EAGAIN; | ||
992 | else if (!value) | ||
993 | goku_clear_halt(ep); | ||
994 | else { | ||
995 | ep->stopped = 1; | ||
996 | VDBG(ep->dev, "%s set halt\n", ep->ep.name); | ||
997 | command(ep->dev->regs, COMMAND_STALL, ep->num); | ||
998 | readl(ep->reg_status); | ||
999 | } | ||
1000 | spin_unlock_irqrestore(&ep->dev->lock, flags); | ||
1001 | return retval; | ||
1002 | } | ||
1003 | |||
1004 | static int goku_fifo_status(struct usb_ep *_ep) | ||
1005 | { | ||
1006 | struct goku_ep *ep; | ||
1007 | struct goku_udc_regs __iomem *regs; | ||
1008 | u32 size; | ||
1009 | |||
1010 | if (!_ep) | ||
1011 | return -ENODEV; | ||
1012 | ep = container_of(_ep, struct goku_ep, ep); | ||
1013 | |||
1014 | /* size is only reported sanely for OUT */ | ||
1015 | if (ep->is_in) | ||
1016 | return -EOPNOTSUPP; | ||
1017 | |||
1018 | /* ignores 16-byte dma buffer; SizeH == 0 */ | ||
1019 | regs = ep->dev->regs; | ||
1020 | size = readl(®s->EPxSizeLA[ep->num]) & DATASIZE; | ||
1021 | size += readl(®s->EPxSizeLB[ep->num]) & DATASIZE; | ||
1022 | VDBG(ep->dev, "%s %s %u\n", __FUNCTION__, ep->ep.name, size); | ||
1023 | return size; | ||
1024 | } | ||
1025 | |||
1026 | static void goku_fifo_flush(struct usb_ep *_ep) | ||
1027 | { | ||
1028 | struct goku_ep *ep; | ||
1029 | struct goku_udc_regs __iomem *regs; | ||
1030 | u32 size; | ||
1031 | |||
1032 | if (!_ep) | ||
1033 | return; | ||
1034 | ep = container_of(_ep, struct goku_ep, ep); | ||
1035 | VDBG(ep->dev, "%s %s\n", __FUNCTION__, ep->ep.name); | ||
1036 | |||
1037 | /* don't change EPxSTATUS_EP_INVALID to READY */ | ||
1038 | if (!ep->desc && ep->num != 0) { | ||
1039 | DBG(ep->dev, "%s %s inactive?\n", __FUNCTION__, ep->ep.name); | ||
1040 | return; | ||
1041 | } | ||
1042 | |||
1043 | regs = ep->dev->regs; | ||
1044 | size = readl(®s->EPxSizeLA[ep->num]); | ||
1045 | size &= DATASIZE; | ||
1046 | |||
1047 | /* Non-desirable behavior: FIFO_CLEAR also clears the | ||
1048 | * endpoint halt feature. For OUT, we _could_ just read | ||
1049 | * the bytes out (PIO, if !ep->dma); for in, no choice. | ||
1050 | */ | ||
1051 | if (size) | ||
1052 | command(regs, COMMAND_FIFO_CLEAR, ep->num); | ||
1053 | } | ||
1054 | |||
1055 | static struct usb_ep_ops goku_ep_ops = { | ||
1056 | .enable = goku_ep_enable, | ||
1057 | .disable = goku_ep_disable, | ||
1058 | |||
1059 | .alloc_request = goku_alloc_request, | ||
1060 | .free_request = goku_free_request, | ||
1061 | |||
1062 | .alloc_buffer = goku_alloc_buffer, | ||
1063 | .free_buffer = goku_free_buffer, | ||
1064 | |||
1065 | .queue = goku_queue, | ||
1066 | .dequeue = goku_dequeue, | ||
1067 | |||
1068 | .set_halt = goku_set_halt, | ||
1069 | .fifo_status = goku_fifo_status, | ||
1070 | .fifo_flush = goku_fifo_flush, | ||
1071 | }; | ||
1072 | |||
1073 | /*-------------------------------------------------------------------------*/ | ||
1074 | |||
1075 | static int goku_get_frame(struct usb_gadget *_gadget) | ||
1076 | { | ||
1077 | return -EOPNOTSUPP; | ||
1078 | } | ||
1079 | |||
1080 | static const struct usb_gadget_ops goku_ops = { | ||
1081 | .get_frame = goku_get_frame, | ||
1082 | // no remote wakeup | ||
1083 | // not selfpowered | ||
1084 | }; | ||
1085 | |||
1086 | /*-------------------------------------------------------------------------*/ | ||
1087 | |||
1088 | static inline char *dmastr(void) | ||
1089 | { | ||
1090 | if (use_dma == 0) | ||
1091 | return "(dma disabled)"; | ||
1092 | else if (use_dma == 2) | ||
1093 | return "(dma IN and OUT)"; | ||
1094 | else | ||
1095 | return "(dma IN)"; | ||
1096 | } | ||
1097 | |||
1098 | #ifdef CONFIG_USB_GADGET_DEBUG_FILES | ||
1099 | |||
1100 | static const char proc_node_name [] = "driver/udc"; | ||
1101 | |||
1102 | #define FOURBITS "%s%s%s%s" | ||
1103 | #define EIGHTBITS FOURBITS FOURBITS | ||
1104 | |||
1105 | static void | ||
1106 | dump_intmask(const char *label, u32 mask, char **next, unsigned *size) | ||
1107 | { | ||
1108 | int t; | ||
1109 | |||
1110 | /* int_status is the same format ... */ | ||
1111 | t = scnprintf(*next, *size, | ||
1112 | "%s %05X =" FOURBITS EIGHTBITS EIGHTBITS "\n", | ||
1113 | label, mask, | ||
1114 | (mask & INT_PWRDETECT) ? " power" : "", | ||
1115 | (mask & INT_SYSERROR) ? " sys" : "", | ||
1116 | (mask & INT_MSTRDEND) ? " in-dma" : "", | ||
1117 | (mask & INT_MSTWRTMOUT) ? " wrtmo" : "", | ||
1118 | |||
1119 | (mask & INT_MSTWREND) ? " out-dma" : "", | ||
1120 | (mask & INT_MSTWRSET) ? " wrset" : "", | ||
1121 | (mask & INT_ERR) ? " err" : "", | ||
1122 | (mask & INT_SOF) ? " sof" : "", | ||
1123 | |||
1124 | (mask & INT_EP3NAK) ? " ep3nak" : "", | ||
1125 | (mask & INT_EP2NAK) ? " ep2nak" : "", | ||
1126 | (mask & INT_EP1NAK) ? " ep1nak" : "", | ||
1127 | (mask & INT_EP3DATASET) ? " ep3" : "", | ||
1128 | |||
1129 | (mask & INT_EP2DATASET) ? " ep2" : "", | ||
1130 | (mask & INT_EP1DATASET) ? " ep1" : "", | ||
1131 | (mask & INT_STATUSNAK) ? " ep0snak" : "", | ||
1132 | (mask & INT_STATUS) ? " ep0status" : "", | ||
1133 | |||
1134 | (mask & INT_SETUP) ? " setup" : "", | ||
1135 | (mask & INT_ENDPOINT0) ? " ep0" : "", | ||
1136 | (mask & INT_USBRESET) ? " reset" : "", | ||
1137 | (mask & INT_SUSPEND) ? " suspend" : ""); | ||
1138 | *size -= t; | ||
1139 | *next += t; | ||
1140 | } | ||
1141 | |||
1142 | |||
1143 | static int | ||
1144 | udc_proc_read(char *buffer, char **start, off_t off, int count, | ||
1145 | int *eof, void *_dev) | ||
1146 | { | ||
1147 | char *buf = buffer; | ||
1148 | struct goku_udc *dev = _dev; | ||
1149 | struct goku_udc_regs __iomem *regs = dev->regs; | ||
1150 | char *next = buf; | ||
1151 | unsigned size = count; | ||
1152 | unsigned long flags; | ||
1153 | int i, t, is_usb_connected; | ||
1154 | u32 tmp; | ||
1155 | |||
1156 | if (off != 0) | ||
1157 | return 0; | ||
1158 | |||
1159 | local_irq_save(flags); | ||
1160 | |||
1161 | /* basic device status */ | ||
1162 | tmp = readl(®s->power_detect); | ||
1163 | is_usb_connected = tmp & PW_DETECT; | ||
1164 | t = scnprintf(next, size, | ||
1165 | "%s - %s\n" | ||
1166 | "%s version: %s %s\n" | ||
1167 | "Gadget driver: %s\n" | ||
1168 | "Host %s, %s\n" | ||
1169 | "\n", | ||
1170 | pci_name(dev->pdev), driver_desc, | ||
1171 | driver_name, DRIVER_VERSION, dmastr(), | ||
1172 | dev->driver ? dev->driver->driver.name : "(none)", | ||
1173 | is_usb_connected | ||
1174 | ? ((tmp & PW_PULLUP) ? "full speed" : "powered") | ||
1175 | : "disconnected", | ||
1176 | ({char *tmp; | ||
1177 | switch(dev->ep0state){ | ||
1178 | case EP0_DISCONNECT: tmp = "ep0_disconnect"; break; | ||
1179 | case EP0_IDLE: tmp = "ep0_idle"; break; | ||
1180 | case EP0_IN: tmp = "ep0_in"; break; | ||
1181 | case EP0_OUT: tmp = "ep0_out"; break; | ||
1182 | case EP0_STATUS: tmp = "ep0_status"; break; | ||
1183 | case EP0_STALL: tmp = "ep0_stall"; break; | ||
1184 | case EP0_SUSPEND: tmp = "ep0_suspend"; break; | ||
1185 | default: tmp = "ep0_?"; break; | ||
1186 | } tmp; }) | ||
1187 | ); | ||
1188 | size -= t; | ||
1189 | next += t; | ||
1190 | |||
1191 | dump_intmask("int_status", readl(®s->int_status), &next, &size); | ||
1192 | dump_intmask("int_enable", readl(®s->int_enable), &next, &size); | ||
1193 | |||
1194 | if (!is_usb_connected || !dev->driver || (tmp & PW_PULLUP) == 0) | ||
1195 | goto done; | ||
1196 | |||
1197 | /* registers for (active) device and ep0 */ | ||
1198 | t = scnprintf(next, size, "\nirqs %lu\ndataset %02x " | ||
1199 | "single.bcs %02x.%02x state %x addr %u\n", | ||
1200 | dev->irqs, readl(®s->DataSet), | ||
1201 | readl(®s->EPxSingle), readl(®s->EPxBCS), | ||
1202 | readl(®s->UsbState), | ||
1203 | readl(®s->address)); | ||
1204 | size -= t; | ||
1205 | next += t; | ||
1206 | |||
1207 | tmp = readl(®s->dma_master); | ||
1208 | t = scnprintf(next, size, | ||
1209 | "dma %03X =" EIGHTBITS "%s %s\n", tmp, | ||
1210 | (tmp & MST_EOPB_DIS) ? " eopb-" : "", | ||
1211 | (tmp & MST_EOPB_ENA) ? " eopb+" : "", | ||
1212 | (tmp & MST_TIMEOUT_DIS) ? " tmo-" : "", | ||
1213 | (tmp & MST_TIMEOUT_ENA) ? " tmo+" : "", | ||
1214 | |||
1215 | (tmp & MST_RD_EOPB) ? " eopb" : "", | ||
1216 | (tmp & MST_RD_RESET) ? " in_reset" : "", | ||
1217 | (tmp & MST_WR_RESET) ? " out_reset" : "", | ||
1218 | (tmp & MST_RD_ENA) ? " IN" : "", | ||
1219 | |||
1220 | (tmp & MST_WR_ENA) ? " OUT" : "", | ||
1221 | (tmp & MST_CONNECTION) | ||
1222 | ? "ep1in/ep2out" | ||
1223 | : "ep1out/ep2in"); | ||
1224 | size -= t; | ||
1225 | next += t; | ||
1226 | |||
1227 | /* dump endpoint queues */ | ||
1228 | for (i = 0; i < 4; i++) { | ||
1229 | struct goku_ep *ep = &dev->ep [i]; | ||
1230 | struct goku_request *req; | ||
1231 | int t; | ||
1232 | |||
1233 | if (i && !ep->desc) | ||
1234 | continue; | ||
1235 | |||
1236 | tmp = readl(ep->reg_status); | ||
1237 | t = scnprintf(next, size, | ||
1238 | "%s %s max %u %s, irqs %lu, " | ||
1239 | "status %02x (%s) " FOURBITS "\n", | ||
1240 | ep->ep.name, | ||
1241 | ep->is_in ? "in" : "out", | ||
1242 | ep->ep.maxpacket, | ||
1243 | ep->dma ? "dma" : "pio", | ||
1244 | ep->irqs, | ||
1245 | tmp, ({ char *s; | ||
1246 | switch (tmp & EPxSTATUS_EP_MASK) { | ||
1247 | case EPxSTATUS_EP_READY: | ||
1248 | s = "ready"; break; | ||
1249 | case EPxSTATUS_EP_DATAIN: | ||
1250 | s = "packet"; break; | ||
1251 | case EPxSTATUS_EP_FULL: | ||
1252 | s = "full"; break; | ||
1253 | case EPxSTATUS_EP_TX_ERR: // host will retry | ||
1254 | s = "tx_err"; break; | ||
1255 | case EPxSTATUS_EP_RX_ERR: | ||
1256 | s = "rx_err"; break; | ||
1257 | case EPxSTATUS_EP_BUSY: /* ep0 only */ | ||
1258 | s = "busy"; break; | ||
1259 | case EPxSTATUS_EP_STALL: | ||
1260 | s = "stall"; break; | ||
1261 | case EPxSTATUS_EP_INVALID: // these "can't happen" | ||
1262 | s = "invalid"; break; | ||
1263 | default: | ||
1264 | s = "?"; break; | ||
1265 | }; s; }), | ||
1266 | (tmp & EPxSTATUS_TOGGLE) ? "data1" : "data0", | ||
1267 | (tmp & EPxSTATUS_SUSPEND) ? " suspend" : "", | ||
1268 | (tmp & EPxSTATUS_FIFO_DISABLE) ? " disable" : "", | ||
1269 | (tmp & EPxSTATUS_STAGE_ERROR) ? " ep0stat" : "" | ||
1270 | ); | ||
1271 | if (t <= 0 || t > size) | ||
1272 | goto done; | ||
1273 | size -= t; | ||
1274 | next += t; | ||
1275 | |||
1276 | if (list_empty(&ep->queue)) { | ||
1277 | t = scnprintf(next, size, "\t(nothing queued)\n"); | ||
1278 | if (t <= 0 || t > size) | ||
1279 | goto done; | ||
1280 | size -= t; | ||
1281 | next += t; | ||
1282 | continue; | ||
1283 | } | ||
1284 | list_for_each_entry(req, &ep->queue, queue) { | ||
1285 | if (ep->dma && req->queue.prev == &ep->queue) { | ||
1286 | if (i == UDC_MSTRD_ENDPOINT) | ||
1287 | tmp = readl(®s->in_dma_current); | ||
1288 | else | ||
1289 | tmp = readl(®s->out_dma_current); | ||
1290 | tmp -= req->req.dma; | ||
1291 | tmp++; | ||
1292 | } else | ||
1293 | tmp = req->req.actual; | ||
1294 | |||
1295 | t = scnprintf(next, size, | ||
1296 | "\treq %p len %u/%u buf %p\n", | ||
1297 | &req->req, tmp, req->req.length, | ||
1298 | req->req.buf); | ||
1299 | if (t <= 0 || t > size) | ||
1300 | goto done; | ||
1301 | size -= t; | ||
1302 | next += t; | ||
1303 | } | ||
1304 | } | ||
1305 | |||
1306 | done: | ||
1307 | local_irq_restore(flags); | ||
1308 | *eof = 1; | ||
1309 | return count - size; | ||
1310 | } | ||
1311 | |||
1312 | #endif /* CONFIG_USB_GADGET_DEBUG_FILES */ | ||
1313 | |||
1314 | /*-------------------------------------------------------------------------*/ | ||
1315 | |||
1316 | static void udc_reinit (struct goku_udc *dev) | ||
1317 | { | ||
1318 | static char *names [] = { "ep0", "ep1-bulk", "ep2-bulk", "ep3-bulk" }; | ||
1319 | |||
1320 | unsigned i; | ||
1321 | |||
1322 | INIT_LIST_HEAD (&dev->gadget.ep_list); | ||
1323 | dev->gadget.ep0 = &dev->ep [0].ep; | ||
1324 | dev->gadget.speed = USB_SPEED_UNKNOWN; | ||
1325 | dev->ep0state = EP0_DISCONNECT; | ||
1326 | dev->irqs = 0; | ||
1327 | |||
1328 | for (i = 0; i < 4; i++) { | ||
1329 | struct goku_ep *ep = &dev->ep[i]; | ||
1330 | |||
1331 | ep->num = i; | ||
1332 | ep->ep.name = names[i]; | ||
1333 | ep->reg_fifo = &dev->regs->ep_fifo [i]; | ||
1334 | ep->reg_status = &dev->regs->ep_status [i]; | ||
1335 | ep->reg_mode = &dev->regs->ep_mode[i]; | ||
1336 | |||
1337 | ep->ep.ops = &goku_ep_ops; | ||
1338 | list_add_tail (&ep->ep.ep_list, &dev->gadget.ep_list); | ||
1339 | ep->dev = dev; | ||
1340 | INIT_LIST_HEAD (&ep->queue); | ||
1341 | |||
1342 | ep_reset(NULL, ep); | ||
1343 | } | ||
1344 | |||
1345 | dev->ep[0].reg_mode = NULL; | ||
1346 | dev->ep[0].ep.maxpacket = MAX_EP0_SIZE; | ||
1347 | list_del_init (&dev->ep[0].ep.ep_list); | ||
1348 | } | ||
1349 | |||
1350 | static void udc_reset(struct goku_udc *dev) | ||
1351 | { | ||
1352 | struct goku_udc_regs __iomem *regs = dev->regs; | ||
1353 | |||
1354 | writel(0, ®s->power_detect); | ||
1355 | writel(0, ®s->int_enable); | ||
1356 | readl(®s->int_enable); | ||
1357 | dev->int_enable = 0; | ||
1358 | |||
1359 | /* deassert reset, leave USB D+ at hi-Z (no pullup) | ||
1360 | * don't let INT_PWRDETECT sequence begin | ||
1361 | */ | ||
1362 | udelay(250); | ||
1363 | writel(PW_RESETB, ®s->power_detect); | ||
1364 | readl(®s->int_enable); | ||
1365 | } | ||
1366 | |||
1367 | static void ep0_start(struct goku_udc *dev) | ||
1368 | { | ||
1369 | struct goku_udc_regs __iomem *regs = dev->regs; | ||
1370 | unsigned i; | ||
1371 | |||
1372 | VDBG(dev, "%s\n", __FUNCTION__); | ||
1373 | |||
1374 | udc_reset(dev); | ||
1375 | udc_reinit (dev); | ||
1376 | //writel(MST_EOPB_ENA | MST_TIMEOUT_ENA, ®s->dma_master); | ||
1377 | |||
1378 | /* hw handles set_address, set_feature, get_status; maybe more */ | ||
1379 | writel( G_REQMODE_SET_INTF | G_REQMODE_GET_INTF | ||
1380 | | G_REQMODE_SET_CONF | G_REQMODE_GET_CONF | ||
1381 | | G_REQMODE_GET_DESC | ||
1382 | | G_REQMODE_CLEAR_FEAT | ||
1383 | , ®s->reqmode); | ||
1384 | |||
1385 | for (i = 0; i < 4; i++) | ||
1386 | dev->ep[i].irqs = 0; | ||
1387 | |||
1388 | /* can't modify descriptors after writing UsbReady */ | ||
1389 | for (i = 0; i < DESC_LEN; i++) | ||
1390 | writel(0, ®s->descriptors[i]); | ||
1391 | writel(0, ®s->UsbReady); | ||
1392 | |||
1393 | /* expect ep0 requests when the host drops reset */ | ||
1394 | writel(PW_RESETB | PW_PULLUP, ®s->power_detect); | ||
1395 | dev->int_enable = INT_DEVWIDE | INT_EP0; | ||
1396 | writel(dev->int_enable, &dev->regs->int_enable); | ||
1397 | readl(®s->int_enable); | ||
1398 | dev->gadget.speed = USB_SPEED_FULL; | ||
1399 | dev->ep0state = EP0_IDLE; | ||
1400 | } | ||
1401 | |||
1402 | static void udc_enable(struct goku_udc *dev) | ||
1403 | { | ||
1404 | /* start enumeration now, or after power detect irq */ | ||
1405 | if (readl(&dev->regs->power_detect) & PW_DETECT) | ||
1406 | ep0_start(dev); | ||
1407 | else { | ||
1408 | DBG(dev, "%s\n", __FUNCTION__); | ||
1409 | dev->int_enable = INT_PWRDETECT; | ||
1410 | writel(dev->int_enable, &dev->regs->int_enable); | ||
1411 | } | ||
1412 | } | ||
1413 | |||
1414 | /*-------------------------------------------------------------------------*/ | ||
1415 | |||
1416 | /* keeping it simple: | ||
1417 | * - one bus driver, initted first; | ||
1418 | * - one function driver, initted second | ||
1419 | */ | ||
1420 | |||
1421 | static struct goku_udc *the_controller; | ||
1422 | |||
1423 | /* when a driver is successfully registered, it will receive | ||
1424 | * control requests including set_configuration(), which enables | ||
1425 | * non-control requests. then usb traffic follows until a | ||
1426 | * disconnect is reported. then a host may connect again, or | ||
1427 | * the driver might get unbound. | ||
1428 | */ | ||
1429 | int usb_gadget_register_driver(struct usb_gadget_driver *driver) | ||
1430 | { | ||
1431 | struct goku_udc *dev = the_controller; | ||
1432 | int retval; | ||
1433 | |||
1434 | if (!driver | ||
1435 | || driver->speed != USB_SPEED_FULL | ||
1436 | || !driver->bind | ||
1437 | || !driver->unbind | ||
1438 | || !driver->disconnect | ||
1439 | || !driver->setup) | ||
1440 | return -EINVAL; | ||
1441 | if (!dev) | ||
1442 | return -ENODEV; | ||
1443 | if (dev->driver) | ||
1444 | return -EBUSY; | ||
1445 | |||
1446 | /* hook up the driver */ | ||
1447 | driver->driver.bus = NULL; | ||
1448 | dev->driver = driver; | ||
1449 | dev->gadget.dev.driver = &driver->driver; | ||
1450 | retval = driver->bind(&dev->gadget); | ||
1451 | if (retval) { | ||
1452 | DBG(dev, "bind to driver %s --> error %d\n", | ||
1453 | driver->driver.name, retval); | ||
1454 | dev->driver = NULL; | ||
1455 | dev->gadget.dev.driver = NULL; | ||
1456 | return retval; | ||
1457 | } | ||
1458 | |||
1459 | /* then enable host detection and ep0; and we're ready | ||
1460 | * for set_configuration as well as eventual disconnect. | ||
1461 | */ | ||
1462 | udc_enable(dev); | ||
1463 | |||
1464 | DBG(dev, "registered gadget driver '%s'\n", driver->driver.name); | ||
1465 | return 0; | ||
1466 | } | ||
1467 | EXPORT_SYMBOL(usb_gadget_register_driver); | ||
1468 | |||
1469 | static void | ||
1470 | stop_activity(struct goku_udc *dev, struct usb_gadget_driver *driver) | ||
1471 | { | ||
1472 | unsigned i; | ||
1473 | |||
1474 | DBG (dev, "%s\n", __FUNCTION__); | ||
1475 | |||
1476 | if (dev->gadget.speed == USB_SPEED_UNKNOWN) | ||
1477 | driver = NULL; | ||
1478 | |||
1479 | /* disconnect gadget driver after quiesceing hw and the driver */ | ||
1480 | udc_reset (dev); | ||
1481 | for (i = 0; i < 4; i++) | ||
1482 | nuke(&dev->ep [i], -ESHUTDOWN); | ||
1483 | if (driver) { | ||
1484 | spin_unlock(&dev->lock); | ||
1485 | driver->disconnect(&dev->gadget); | ||
1486 | spin_lock(&dev->lock); | ||
1487 | } | ||
1488 | |||
1489 | if (dev->driver) | ||
1490 | udc_enable(dev); | ||
1491 | } | ||
1492 | |||
1493 | int usb_gadget_unregister_driver(struct usb_gadget_driver *driver) | ||
1494 | { | ||
1495 | struct goku_udc *dev = the_controller; | ||
1496 | unsigned long flags; | ||
1497 | |||
1498 | if (!dev) | ||
1499 | return -ENODEV; | ||
1500 | if (!driver || driver != dev->driver) | ||
1501 | return -EINVAL; | ||
1502 | |||
1503 | spin_lock_irqsave(&dev->lock, flags); | ||
1504 | dev->driver = NULL; | ||
1505 | stop_activity(dev, driver); | ||
1506 | spin_unlock_irqrestore(&dev->lock, flags); | ||
1507 | |||
1508 | driver->unbind(&dev->gadget); | ||
1509 | |||
1510 | DBG(dev, "unregistered driver '%s'\n", driver->driver.name); | ||
1511 | return 0; | ||
1512 | } | ||
1513 | EXPORT_SYMBOL(usb_gadget_unregister_driver); | ||
1514 | |||
1515 | |||
1516 | /*-------------------------------------------------------------------------*/ | ||
1517 | |||
1518 | static void ep0_setup(struct goku_udc *dev) | ||
1519 | { | ||
1520 | struct goku_udc_regs __iomem *regs = dev->regs; | ||
1521 | struct usb_ctrlrequest ctrl; | ||
1522 | int tmp; | ||
1523 | |||
1524 | /* read SETUP packet and enter DATA stage */ | ||
1525 | ctrl.bRequestType = readl(®s->bRequestType); | ||
1526 | ctrl.bRequest = readl(®s->bRequest); | ||
1527 | ctrl.wValue = (readl(®s->wValueH) << 8) | readl(®s->wValueL); | ||
1528 | ctrl.wIndex = (readl(®s->wIndexH) << 8) | readl(®s->wIndexL); | ||
1529 | ctrl.wLength = (readl(®s->wLengthH) << 8) | readl(®s->wLengthL); | ||
1530 | writel(0, ®s->SetupRecv); | ||
1531 | |||
1532 | nuke(&dev->ep[0], 0); | ||
1533 | dev->ep[0].stopped = 0; | ||
1534 | if (likely(ctrl.bRequestType & USB_DIR_IN)) { | ||
1535 | dev->ep[0].is_in = 1; | ||
1536 | dev->ep0state = EP0_IN; | ||
1537 | /* detect early status stages */ | ||
1538 | writel(ICONTROL_STATUSNAK, &dev->regs->IntControl); | ||
1539 | } else { | ||
1540 | dev->ep[0].is_in = 0; | ||
1541 | dev->ep0state = EP0_OUT; | ||
1542 | |||
1543 | /* NOTE: CLEAR_FEATURE is done in software so that we can | ||
1544 | * synchronize transfer restarts after bulk IN stalls. data | ||
1545 | * won't even enter the fifo until the halt is cleared. | ||
1546 | */ | ||
1547 | switch (ctrl.bRequest) { | ||
1548 | case USB_REQ_CLEAR_FEATURE: | ||
1549 | switch (ctrl.bRequestType) { | ||
1550 | case USB_RECIP_ENDPOINT: | ||
1551 | tmp = ctrl.wIndex & 0x0f; | ||
1552 | /* active endpoint */ | ||
1553 | if (tmp > 3 || (!dev->ep[tmp].desc && tmp != 0)) | ||
1554 | goto stall; | ||
1555 | if (ctrl.wIndex & USB_DIR_IN) { | ||
1556 | if (!dev->ep[tmp].is_in) | ||
1557 | goto stall; | ||
1558 | } else { | ||
1559 | if (dev->ep[tmp].is_in) | ||
1560 | goto stall; | ||
1561 | } | ||
1562 | if (ctrl.wValue != USB_ENDPOINT_HALT) | ||
1563 | goto stall; | ||
1564 | if (tmp) | ||
1565 | goku_clear_halt(&dev->ep[tmp]); | ||
1566 | succeed: | ||
1567 | /* start ep0out status stage */ | ||
1568 | writel(~(1<<0), ®s->EOP); | ||
1569 | dev->ep[0].stopped = 1; | ||
1570 | dev->ep0state = EP0_STATUS; | ||
1571 | return; | ||
1572 | case USB_RECIP_DEVICE: | ||
1573 | /* device remote wakeup: always clear */ | ||
1574 | if (ctrl.wValue != 1) | ||
1575 | goto stall; | ||
1576 | VDBG(dev, "clear dev remote wakeup\n"); | ||
1577 | goto succeed; | ||
1578 | case USB_RECIP_INTERFACE: | ||
1579 | goto stall; | ||
1580 | default: /* pass to gadget driver */ | ||
1581 | break; | ||
1582 | } | ||
1583 | break; | ||
1584 | default: | ||
1585 | break; | ||
1586 | } | ||
1587 | } | ||
1588 | |||
1589 | #ifdef USB_TRACE | ||
1590 | VDBG(dev, "SETUP %02x.%02x v%04x i%04x l%04x\n", | ||
1591 | ctrl.bRequestType, ctrl.bRequest, | ||
1592 | ctrl.wValue, ctrl.wIndex, ctrl.wLength); | ||
1593 | #endif | ||
1594 | |||
1595 | /* hw wants to know when we're configured (or not) */ | ||
1596 | dev->req_config = (ctrl.bRequest == USB_REQ_SET_CONFIGURATION | ||
1597 | && ctrl.bRequestType == USB_RECIP_DEVICE); | ||
1598 | if (unlikely(dev->req_config)) | ||
1599 | dev->configured = (ctrl.wValue != 0); | ||
1600 | |||
1601 | /* delegate everything to the gadget driver. | ||
1602 | * it may respond after this irq handler returns. | ||
1603 | */ | ||
1604 | spin_unlock (&dev->lock); | ||
1605 | tmp = dev->driver->setup(&dev->gadget, &ctrl); | ||
1606 | spin_lock (&dev->lock); | ||
1607 | if (unlikely(tmp < 0)) { | ||
1608 | stall: | ||
1609 | #ifdef USB_TRACE | ||
1610 | VDBG(dev, "req %02x.%02x protocol STALL; err %d\n", | ||
1611 | ctrl.bRequestType, ctrl.bRequest, tmp); | ||
1612 | #endif | ||
1613 | command(regs, COMMAND_STALL, 0); | ||
1614 | dev->ep[0].stopped = 1; | ||
1615 | dev->ep0state = EP0_STALL; | ||
1616 | } | ||
1617 | |||
1618 | /* expect at least one data or status stage irq */ | ||
1619 | } | ||
1620 | |||
1621 | #define ACK(irqbit) { \ | ||
1622 | stat &= ~irqbit; \ | ||
1623 | writel(~irqbit, ®s->int_status); \ | ||
1624 | handled = 1; \ | ||
1625 | } | ||
1626 | |||
1627 | static irqreturn_t goku_irq(int irq, void *_dev, struct pt_regs *r) | ||
1628 | { | ||
1629 | struct goku_udc *dev = _dev; | ||
1630 | struct goku_udc_regs __iomem *regs = dev->regs; | ||
1631 | struct goku_ep *ep; | ||
1632 | u32 stat, handled = 0; | ||
1633 | unsigned i, rescans = 5; | ||
1634 | |||
1635 | spin_lock(&dev->lock); | ||
1636 | |||
1637 | rescan: | ||
1638 | stat = readl(®s->int_status) & dev->int_enable; | ||
1639 | if (!stat) | ||
1640 | goto done; | ||
1641 | dev->irqs++; | ||
1642 | |||
1643 | /* device-wide irqs */ | ||
1644 | if (unlikely(stat & INT_DEVWIDE)) { | ||
1645 | if (stat & INT_SYSERROR) { | ||
1646 | ERROR(dev, "system error\n"); | ||
1647 | stop_activity(dev, dev->driver); | ||
1648 | stat = 0; | ||
1649 | handled = 1; | ||
1650 | // FIXME have a neater way to prevent re-enumeration | ||
1651 | dev->driver = NULL; | ||
1652 | goto done; | ||
1653 | } | ||
1654 | if (stat & INT_PWRDETECT) { | ||
1655 | writel(~stat, ®s->int_status); | ||
1656 | if (readl(&dev->regs->power_detect) & PW_DETECT) { | ||
1657 | VDBG(dev, "connect\n"); | ||
1658 | ep0_start(dev); | ||
1659 | } else { | ||
1660 | DBG(dev, "disconnect\n"); | ||
1661 | if (dev->gadget.speed == USB_SPEED_FULL) | ||
1662 | stop_activity(dev, dev->driver); | ||
1663 | dev->ep0state = EP0_DISCONNECT; | ||
1664 | dev->int_enable = INT_DEVWIDE; | ||
1665 | writel(dev->int_enable, &dev->regs->int_enable); | ||
1666 | } | ||
1667 | stat = 0; | ||
1668 | handled = 1; | ||
1669 | goto done; | ||
1670 | } | ||
1671 | if (stat & INT_SUSPEND) { | ||
1672 | ACK(INT_SUSPEND); | ||
1673 | if (readl(®s->ep_status[0]) & EPxSTATUS_SUSPEND) { | ||
1674 | switch (dev->ep0state) { | ||
1675 | case EP0_DISCONNECT: | ||
1676 | case EP0_SUSPEND: | ||
1677 | goto pm_next; | ||
1678 | default: | ||
1679 | break; | ||
1680 | } | ||
1681 | DBG(dev, "USB suspend\n"); | ||
1682 | dev->ep0state = EP0_SUSPEND; | ||
1683 | if (dev->gadget.speed != USB_SPEED_UNKNOWN | ||
1684 | && dev->driver | ||
1685 | && dev->driver->suspend) { | ||
1686 | spin_unlock(&dev->lock); | ||
1687 | dev->driver->suspend(&dev->gadget); | ||
1688 | spin_lock(&dev->lock); | ||
1689 | } | ||
1690 | } else { | ||
1691 | if (dev->ep0state != EP0_SUSPEND) { | ||
1692 | DBG(dev, "bogus USB resume %d\n", | ||
1693 | dev->ep0state); | ||
1694 | goto pm_next; | ||
1695 | } | ||
1696 | DBG(dev, "USB resume\n"); | ||
1697 | dev->ep0state = EP0_IDLE; | ||
1698 | if (dev->gadget.speed != USB_SPEED_UNKNOWN | ||
1699 | && dev->driver | ||
1700 | && dev->driver->resume) { | ||
1701 | spin_unlock(&dev->lock); | ||
1702 | dev->driver->resume(&dev->gadget); | ||
1703 | spin_lock(&dev->lock); | ||
1704 | } | ||
1705 | } | ||
1706 | } | ||
1707 | pm_next: | ||
1708 | if (stat & INT_USBRESET) { /* hub reset done */ | ||
1709 | ACK(INT_USBRESET); | ||
1710 | INFO(dev, "USB reset done, gadget %s\n", | ||
1711 | dev->driver->driver.name); | ||
1712 | } | ||
1713 | // and INT_ERR on some endpoint's crc/bitstuff/... problem | ||
1714 | } | ||
1715 | |||
1716 | /* progress ep0 setup, data, or status stages. | ||
1717 | * no transition {EP0_STATUS, EP0_STALL} --> EP0_IDLE; saves irqs | ||
1718 | */ | ||
1719 | if (stat & INT_SETUP) { | ||
1720 | ACK(INT_SETUP); | ||
1721 | dev->ep[0].irqs++; | ||
1722 | ep0_setup(dev); | ||
1723 | } | ||
1724 | if (stat & INT_STATUSNAK) { | ||
1725 | ACK(INT_STATUSNAK|INT_ENDPOINT0); | ||
1726 | if (dev->ep0state == EP0_IN) { | ||
1727 | ep = &dev->ep[0]; | ||
1728 | ep->irqs++; | ||
1729 | nuke(ep, 0); | ||
1730 | writel(~(1<<0), ®s->EOP); | ||
1731 | dev->ep0state = EP0_STATUS; | ||
1732 | } | ||
1733 | } | ||
1734 | if (stat & INT_ENDPOINT0) { | ||
1735 | ACK(INT_ENDPOINT0); | ||
1736 | ep = &dev->ep[0]; | ||
1737 | ep->irqs++; | ||
1738 | pio_advance(ep); | ||
1739 | } | ||
1740 | |||
1741 | /* dma completion */ | ||
1742 | if (stat & INT_MSTRDEND) { /* IN */ | ||
1743 | ACK(INT_MSTRDEND); | ||
1744 | ep = &dev->ep[UDC_MSTRD_ENDPOINT]; | ||
1745 | ep->irqs++; | ||
1746 | dma_advance(dev, ep); | ||
1747 | } | ||
1748 | if (stat & INT_MSTWREND) { /* OUT */ | ||
1749 | ACK(INT_MSTWREND); | ||
1750 | ep = &dev->ep[UDC_MSTWR_ENDPOINT]; | ||
1751 | ep->irqs++; | ||
1752 | dma_advance(dev, ep); | ||
1753 | } | ||
1754 | if (stat & INT_MSTWRTMOUT) { /* OUT */ | ||
1755 | ACK(INT_MSTWRTMOUT); | ||
1756 | ep = &dev->ep[UDC_MSTWR_ENDPOINT]; | ||
1757 | ep->irqs++; | ||
1758 | ERROR(dev, "%s write timeout ?\n", ep->ep.name); | ||
1759 | // reset dma? then dma_advance() | ||
1760 | } | ||
1761 | |||
1762 | /* pio */ | ||
1763 | for (i = 1; i < 4; i++) { | ||
1764 | u32 tmp = INT_EPxDATASET(i); | ||
1765 | |||
1766 | if (!(stat & tmp)) | ||
1767 | continue; | ||
1768 | ep = &dev->ep[i]; | ||
1769 | pio_advance(ep); | ||
1770 | if (list_empty (&ep->queue)) | ||
1771 | pio_irq_disable(dev, regs, i); | ||
1772 | stat &= ~tmp; | ||
1773 | handled = 1; | ||
1774 | ep->irqs++; | ||
1775 | } | ||
1776 | |||
1777 | if (rescans--) | ||
1778 | goto rescan; | ||
1779 | |||
1780 | done: | ||
1781 | (void)readl(®s->int_enable); | ||
1782 | spin_unlock(&dev->lock); | ||
1783 | if (stat) | ||
1784 | DBG(dev, "unhandled irq status: %05x (%05x, %05x)\n", stat, | ||
1785 | readl(®s->int_status), dev->int_enable); | ||
1786 | return IRQ_RETVAL(handled); | ||
1787 | } | ||
1788 | |||
1789 | #undef ACK | ||
1790 | |||
1791 | /*-------------------------------------------------------------------------*/ | ||
1792 | |||
1793 | static void gadget_release(struct device *_dev) | ||
1794 | { | ||
1795 | struct goku_udc *dev = dev_get_drvdata(_dev); | ||
1796 | |||
1797 | kfree(dev); | ||
1798 | } | ||
1799 | |||
1800 | /* tear down the binding between this driver and the pci device */ | ||
1801 | |||
1802 | static void goku_remove(struct pci_dev *pdev) | ||
1803 | { | ||
1804 | struct goku_udc *dev = pci_get_drvdata(pdev); | ||
1805 | |||
1806 | DBG(dev, "%s\n", __FUNCTION__); | ||
1807 | /* start with the driver above us */ | ||
1808 | if (dev->driver) { | ||
1809 | /* should have been done already by driver model core */ | ||
1810 | WARN(dev, "pci remove, driver '%s' is still registered\n", | ||
1811 | dev->driver->driver.name); | ||
1812 | usb_gadget_unregister_driver(dev->driver); | ||
1813 | } | ||
1814 | |||
1815 | #ifdef CONFIG_USB_GADGET_DEBUG_FILES | ||
1816 | remove_proc_entry(proc_node_name, NULL); | ||
1817 | #endif | ||
1818 | if (dev->regs) | ||
1819 | udc_reset(dev); | ||
1820 | if (dev->got_irq) | ||
1821 | free_irq(pdev->irq, dev); | ||
1822 | if (dev->regs) | ||
1823 | iounmap(dev->regs); | ||
1824 | if (dev->got_region) | ||
1825 | release_mem_region(pci_resource_start (pdev, 0), | ||
1826 | pci_resource_len (pdev, 0)); | ||
1827 | if (dev->enabled) | ||
1828 | pci_disable_device(pdev); | ||
1829 | device_unregister(&dev->gadget.dev); | ||
1830 | |||
1831 | pci_set_drvdata(pdev, NULL); | ||
1832 | dev->regs = NULL; | ||
1833 | the_controller = NULL; | ||
1834 | |||
1835 | INFO(dev, "unbind\n"); | ||
1836 | } | ||
1837 | |||
1838 | /* wrap this driver around the specified pci device, but | ||
1839 | * don't respond over USB until a gadget driver binds to us. | ||
1840 | */ | ||
1841 | |||
1842 | static int goku_probe(struct pci_dev *pdev, const struct pci_device_id *id) | ||
1843 | { | ||
1844 | struct goku_udc *dev = NULL; | ||
1845 | unsigned long resource, len; | ||
1846 | void __iomem *base = NULL; | ||
1847 | int retval; | ||
1848 | char buf [8], *bufp; | ||
1849 | |||
1850 | /* if you want to support more than one controller in a system, | ||
1851 | * usb_gadget_driver_{register,unregister}() must change. | ||
1852 | */ | ||
1853 | if (the_controller) { | ||
1854 | WARN(dev, "ignoring %s\n", pci_name(pdev)); | ||
1855 | return -EBUSY; | ||
1856 | } | ||
1857 | if (!pdev->irq) { | ||
1858 | printk(KERN_ERR "Check PCI %s IRQ setup!\n", pci_name(pdev)); | ||
1859 | retval = -ENODEV; | ||
1860 | goto done; | ||
1861 | } | ||
1862 | |||
1863 | /* alloc, and start init */ | ||
1864 | dev = kmalloc (sizeof *dev, SLAB_KERNEL); | ||
1865 | if (dev == NULL){ | ||
1866 | pr_debug("enomem %s\n", pci_name(pdev)); | ||
1867 | retval = -ENOMEM; | ||
1868 | goto done; | ||
1869 | } | ||
1870 | |||
1871 | memset(dev, 0, sizeof *dev); | ||
1872 | spin_lock_init(&dev->lock); | ||
1873 | dev->pdev = pdev; | ||
1874 | dev->gadget.ops = &goku_ops; | ||
1875 | |||
1876 | /* the "gadget" abstracts/virtualizes the controller */ | ||
1877 | strcpy(dev->gadget.dev.bus_id, "gadget"); | ||
1878 | dev->gadget.dev.parent = &pdev->dev; | ||
1879 | dev->gadget.dev.dma_mask = pdev->dev.dma_mask; | ||
1880 | dev->gadget.dev.release = gadget_release; | ||
1881 | dev->gadget.name = driver_name; | ||
1882 | |||
1883 | /* now all the pci goodies ... */ | ||
1884 | retval = pci_enable_device(pdev); | ||
1885 | if (retval < 0) { | ||
1886 | DBG(dev, "can't enable, %d\n", retval); | ||
1887 | goto done; | ||
1888 | } | ||
1889 | dev->enabled = 1; | ||
1890 | |||
1891 | resource = pci_resource_start(pdev, 0); | ||
1892 | len = pci_resource_len(pdev, 0); | ||
1893 | if (!request_mem_region(resource, len, driver_name)) { | ||
1894 | DBG(dev, "controller already in use\n"); | ||
1895 | retval = -EBUSY; | ||
1896 | goto done; | ||
1897 | } | ||
1898 | dev->got_region = 1; | ||
1899 | |||
1900 | base = ioremap_nocache(resource, len); | ||
1901 | if (base == NULL) { | ||
1902 | DBG(dev, "can't map memory\n"); | ||
1903 | retval = -EFAULT; | ||
1904 | goto done; | ||
1905 | } | ||
1906 | dev->regs = (struct goku_udc_regs __iomem *) base; | ||
1907 | |||
1908 | pci_set_drvdata(pdev, dev); | ||
1909 | INFO(dev, "%s\n", driver_desc); | ||
1910 | INFO(dev, "version: " DRIVER_VERSION " %s\n", dmastr()); | ||
1911 | #ifndef __sparc__ | ||
1912 | scnprintf(buf, sizeof buf, "%d", pdev->irq); | ||
1913 | bufp = buf; | ||
1914 | #else | ||
1915 | bufp = __irq_itoa(pdev->irq); | ||
1916 | #endif | ||
1917 | INFO(dev, "irq %s, pci mem %p\n", bufp, base); | ||
1918 | |||
1919 | /* init to known state, then setup irqs */ | ||
1920 | udc_reset(dev); | ||
1921 | udc_reinit (dev); | ||
1922 | if (request_irq(pdev->irq, goku_irq, SA_SHIRQ/*|SA_SAMPLE_RANDOM*/, | ||
1923 | driver_name, dev) != 0) { | ||
1924 | DBG(dev, "request interrupt %s failed\n", bufp); | ||
1925 | retval = -EBUSY; | ||
1926 | goto done; | ||
1927 | } | ||
1928 | dev->got_irq = 1; | ||
1929 | if (use_dma) | ||
1930 | pci_set_master(pdev); | ||
1931 | |||
1932 | |||
1933 | #ifdef CONFIG_USB_GADGET_DEBUG_FILES | ||
1934 | create_proc_read_entry(proc_node_name, 0, NULL, udc_proc_read, dev); | ||
1935 | #endif | ||
1936 | |||
1937 | /* done */ | ||
1938 | the_controller = dev; | ||
1939 | device_register(&dev->gadget.dev); | ||
1940 | |||
1941 | return 0; | ||
1942 | |||
1943 | done: | ||
1944 | if (dev) | ||
1945 | goku_remove (pdev); | ||
1946 | return retval; | ||
1947 | } | ||
1948 | |||
1949 | |||
1950 | /*-------------------------------------------------------------------------*/ | ||
1951 | |||
1952 | static struct pci_device_id pci_ids [] = { { | ||
1953 | .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe), | ||
1954 | .class_mask = ~0, | ||
1955 | .vendor = 0x102f, /* Toshiba */ | ||
1956 | .device = 0x0107, /* this UDC */ | ||
1957 | .subvendor = PCI_ANY_ID, | ||
1958 | .subdevice = PCI_ANY_ID, | ||
1959 | |||
1960 | }, { /* end: all zeroes */ } | ||
1961 | }; | ||
1962 | MODULE_DEVICE_TABLE (pci, pci_ids); | ||
1963 | |||
1964 | static struct pci_driver goku_pci_driver = { | ||
1965 | .name = (char *) driver_name, | ||
1966 | .id_table = pci_ids, | ||
1967 | |||
1968 | .probe = goku_probe, | ||
1969 | .remove = goku_remove, | ||
1970 | |||
1971 | /* FIXME add power management support */ | ||
1972 | }; | ||
1973 | |||
1974 | static int __init init (void) | ||
1975 | { | ||
1976 | return pci_register_driver (&goku_pci_driver); | ||
1977 | } | ||
1978 | module_init (init); | ||
1979 | |||
1980 | static void __exit cleanup (void) | ||
1981 | { | ||
1982 | pci_unregister_driver (&goku_pci_driver); | ||
1983 | } | ||
1984 | module_exit (cleanup); | ||