blob: 57a547049df57a828f838892a003bf36577ceb4c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070023// #define VERBOSE /* extra debug messages (success too) */
24// #define USB_TRACE /* packet-level success messages */
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/kernel.h>
27#include <linux/module.h>
28#include <linux/pci.h>
29#include <linux/delay.h>
30#include <linux/ioport.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/errno.h>
33#include <linux/init.h>
34#include <linux/timer.h>
35#include <linux/list.h>
36#include <linux/interrupt.h>
37#include <linux/proc_fs.h>
David Howells96c7a222013-04-10 11:51:21 +010038#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/device.h>
David Brownell5f848132006-12-16 15:34:53 -080040#include <linux/usb/ch9.h>
David Brownell9454a572007-10-04 18:05:17 -070041#include <linux/usb/gadget.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -070042#include <linux/prefetch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44#include <asm/byteorder.h>
45#include <asm/io.h>
46#include <asm/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <asm/unaligned.h>
48
49
50#include "goku_udc.h"
51
52#define DRIVER_DESC "TC86C001 USB Device Controller"
53#define DRIVER_VERSION "30-Oct 2003"
54
55#define DMA_ADDR_INVALID (~(dma_addr_t)0)
56
57static const char driver_name [] = "goku_udc";
58static const char driver_desc [] = DRIVER_DESC;
59
60MODULE_AUTHOR("source@mvista.com");
61MODULE_DESCRIPTION(DRIVER_DESC);
62MODULE_LICENSE("GPL");
63
64
65/*
66 * IN dma behaves ok under testing, though the IN-dma abort paths don't
67 * seem to behave quite as expected. Used by default.
68 *
69 * OUT dma documents design problems handling the common "short packet"
David Brownell988199f2005-05-07 13:05:52 -070070 * transfer termination policy; it couldn't be enabled by default, even
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 * if the OUT-dma abort problems had a resolution.
72 */
73static unsigned use_dma = 1;
74
75#if 0
76//#include <linux/moduleparam.h>
77/* "modprobe goku_udc use_dma=1" etc
78 * 0 to disable dma
79 * 1 to use IN dma only (normal operation)
80 * 2 to use IN and OUT dma
81 */
82module_param(use_dma, uint, S_IRUGO);
83#endif
84
85/*-------------------------------------------------------------------------*/
86
87static void nuke(struct goku_ep *, int status);
88
89static inline void
90command(struct goku_udc_regs __iomem *regs, int command, unsigned epnum)
91{
92 writel(COMMAND_EP(epnum) | command, &regs->Command);
93 udelay(300);
94}
95
96static int
97goku_ep_enable(struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
98{
99 struct goku_udc *dev;
100 struct goku_ep *ep;
101 u32 mode;
102 u16 max;
103 unsigned long flags;
104
105 ep = container_of(_ep, struct goku_ep, ep);
Ido Shayevitzaedaa442012-06-04 13:35:22 +0300106 if (!_ep || !desc
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 || desc->bDescriptorType != USB_DT_ENDPOINT)
108 return -EINVAL;
109 dev = ep->dev;
110 if (ep == &dev->ep[0])
111 return -EINVAL;
112 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
113 return -ESHUTDOWN;
Matthias Kaehlcke9ab15652009-04-15 22:28:21 +0200114 if (ep->num != usb_endpoint_num(desc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 return -EINVAL;
116
Matthias Kaehlcke9ab15652009-04-15 22:28:21 +0200117 switch (usb_endpoint_type(desc)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 case USB_ENDPOINT_XFER_BULK:
119 case USB_ENDPOINT_XFER_INT:
120 break;
121 default:
122 return -EINVAL;
123 }
124
125 if ((readl(ep->reg_status) & EPxSTATUS_EP_MASK)
126 != EPxSTATUS_EP_INVALID)
127 return -EBUSY;
128
129 /* enabling the no-toggle interrupt mode would need an api hook */
130 mode = 0;
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700131 max = get_unaligned_le16(&desc->wMaxPacketSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 switch (max) {
133 case 64: mode++;
134 case 32: mode++;
135 case 16: mode++;
136 case 8: mode <<= 3;
137 break;
138 default:
139 return -EINVAL;
140 }
141 mode |= 2 << 1; /* bulk, or intr-with-toggle */
142
143 /* ep1/ep2 dma direction is chosen early; it works in the other
144 * direction, with pio. be cautious with out-dma.
145 */
Matthias Kaehlcke9ab15652009-04-15 22:28:21 +0200146 ep->is_in = usb_endpoint_dir_in(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 if (ep->is_in) {
148 mode |= 1;
149 ep->dma = (use_dma != 0) && (ep->num == UDC_MSTRD_ENDPOINT);
150 } else {
151 ep->dma = (use_dma == 2) && (ep->num == UDC_MSTWR_ENDPOINT);
152 if (ep->dma)
153 DBG(dev, "%s out-dma hides short packets\n",
154 ep->ep.name);
155 }
156
157 spin_lock_irqsave(&ep->dev->lock, flags);
158
159 /* ep1 and ep2 can do double buffering and/or dma */
160 if (ep->num < 3) {
161 struct goku_udc_regs __iomem *regs = ep->dev->regs;
162 u32 tmp;
163
164 /* double buffer except (for now) with pio in */
165 tmp = ((ep->dma || !ep->is_in)
166 ? 0x10 /* double buffered */
167 : 0x11 /* single buffer */
168 ) << ep->num;
169 tmp |= readl(&regs->EPxSingle);
170 writel(tmp, &regs->EPxSingle);
171
172 tmp = (ep->dma ? 0x10/*dma*/ : 0x11/*pio*/) << ep->num;
173 tmp |= readl(&regs->EPxBCS);
174 writel(tmp, &regs->EPxBCS);
175 }
176 writel(mode, ep->reg_mode);
177 command(ep->dev->regs, COMMAND_RESET, ep->num);
178 ep->ep.maxpacket = max;
179 ep->stopped = 0;
Ido Shayevitzdb79d982012-03-12 20:25:31 +0200180 ep->ep.desc = desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 spin_unlock_irqrestore(&ep->dev->lock, flags);
182
183 DBG(dev, "enable %s %s %s maxpacket %u\n", ep->ep.name,
184 ep->is_in ? "IN" : "OUT",
185 ep->dma ? "dma" : "pio",
186 max);
187
188 return 0;
189}
190
191static void ep_reset(struct goku_udc_regs __iomem *regs, struct goku_ep *ep)
192{
193 struct goku_udc *dev = ep->dev;
194
195 if (regs) {
196 command(regs, COMMAND_INVALID, ep->num);
197 if (ep->num) {
198 if (ep->num == UDC_MSTWR_ENDPOINT)
199 dev->int_enable &= ~(INT_MSTWREND
200 |INT_MSTWRTMOUT);
201 else if (ep->num == UDC_MSTRD_ENDPOINT)
202 dev->int_enable &= ~INT_MSTRDEND;
203 dev->int_enable &= ~INT_EPxDATASET (ep->num);
204 } else
205 dev->int_enable &= ~INT_EP0;
206 writel(dev->int_enable, &regs->int_enable);
207 readl(&regs->int_enable);
208 if (ep->num < 3) {
209 struct goku_udc_regs __iomem *r = ep->dev->regs;
210 u32 tmp;
211
212 tmp = readl(&r->EPxSingle);
213 tmp &= ~(0x11 << ep->num);
214 writel(tmp, &r->EPxSingle);
215
216 tmp = readl(&r->EPxBCS);
217 tmp &= ~(0x11 << ep->num);
218 writel(tmp, &r->EPxBCS);
219 }
220 /* reset dma in case we're still using it */
221 if (ep->dma) {
222 u32 master;
223
224 master = readl(&regs->dma_master) & MST_RW_BITS;
225 if (ep->num == UDC_MSTWR_ENDPOINT) {
226 master &= ~MST_W_BITS;
227 master |= MST_WR_RESET;
228 } else {
229 master &= ~MST_R_BITS;
230 master |= MST_RD_RESET;
231 }
232 writel(master, &regs->dma_master);
233 }
234 }
235
236 ep->ep.maxpacket = MAX_FIFO_SIZE;
Ido Shayevitzf9c56cd2012-02-08 13:56:48 +0200237 ep->ep.desc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 ep->stopped = 1;
239 ep->irqs = 0;
240 ep->dma = 0;
241}
242
243static int goku_ep_disable(struct usb_ep *_ep)
244{
245 struct goku_ep *ep;
246 struct goku_udc *dev;
247 unsigned long flags;
248
249 ep = container_of(_ep, struct goku_ep, ep);
Ido Shayevitzdb79d982012-03-12 20:25:31 +0200250 if (!_ep || !ep->ep.desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 return -ENODEV;
252 dev = ep->dev;
253 if (dev->ep0state == EP0_SUSPEND)
254 return -EBUSY;
255
256 VDBG(dev, "disable %s\n", _ep->name);
257
258 spin_lock_irqsave(&dev->lock, flags);
259 nuke(ep, -ESHUTDOWN);
260 ep_reset(dev->regs, ep);
261 spin_unlock_irqrestore(&dev->lock, flags);
262
263 return 0;
264}
265
266/*-------------------------------------------------------------------------*/
267
268static struct usb_request *
Al Viro55016f12005-10-21 03:21:58 -0400269goku_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
271 struct goku_request *req;
272
273 if (!_ep)
274 return NULL;
Eric Sesterhenn7039f422006-02-27 13:34:10 -0800275 req = kzalloc(sizeof *req, gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 if (!req)
277 return NULL;
278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 req->req.dma = DMA_ADDR_INVALID;
280 INIT_LIST_HEAD(&req->queue);
281 return &req->req;
282}
283
284static void
285goku_free_request(struct usb_ep *_ep, struct usb_request *_req)
286{
287 struct goku_request *req;
288
289 if (!_ep || !_req)
290 return;
291
292 req = container_of(_req, struct goku_request, req);
293 WARN_ON(!list_empty(&req->queue));
294 kfree(req);
295}
296
297/*-------------------------------------------------------------------------*/
298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299static void
300done(struct goku_ep *ep, struct goku_request *req, int status)
301{
302 struct goku_udc *dev;
303 unsigned stopped = ep->stopped;
304
305 list_del_init(&req->queue);
306
307 if (likely(req->req.status == -EINPROGRESS))
308 req->req.status = status;
309 else
310 status = req->req.status;
311
312 dev = ep->dev;
Felipe Balbi64400932011-12-19 12:11:44 +0200313
314 if (ep->dma)
315 usb_gadget_unmap_request(&dev->gadget, &req->req, ep->is_in);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317#ifndef USB_TRACE
318 if (status && status != -ESHUTDOWN)
319#endif
320 VDBG(dev, "complete %s req %p stat %d len %u/%u\n",
321 ep->ep.name, &req->req, status,
322 req->req.actual, req->req.length);
323
324 /* don't modify queue heads during completion callback */
325 ep->stopped = 1;
326 spin_unlock(&dev->lock);
327 req->req.complete(&ep->ep, &req->req);
328 spin_lock(&dev->lock);
329 ep->stopped = stopped;
330}
331
332/*-------------------------------------------------------------------------*/
333
334static inline int
335write_packet(u32 __iomem *fifo, u8 *buf, struct goku_request *req, unsigned max)
336{
337 unsigned length, count;
338
339 length = min(req->req.length - req->req.actual, max);
340 req->req.actual += length;
341
342 count = length;
343 while (likely(count--))
344 writel(*buf++, fifo);
345 return length;
346}
347
348// return: 0 = still running, 1 = completed, negative = errno
349static int write_fifo(struct goku_ep *ep, struct goku_request *req)
350{
351 struct goku_udc *dev = ep->dev;
352 u32 tmp;
353 u8 *buf;
354 unsigned count;
355 int is_last;
356
357 tmp = readl(&dev->regs->DataSet);
358 buf = req->req.buf + req->req.actual;
359 prefetch(buf);
360
361 dev = ep->dev;
362 if (unlikely(ep->num == 0 && dev->ep0state != EP0_IN))
363 return -EL2HLT;
364
365 /* NOTE: just single-buffered PIO-IN for now. */
366 if (unlikely((tmp & DATASET_A(ep->num)) != 0))
367 return 0;
368
369 /* clear our "packet available" irq */
370 if (ep->num != 0)
371 writel(~INT_EPxDATASET(ep->num), &dev->regs->int_status);
372
373 count = write_packet(ep->reg_fifo, buf, req, ep->ep.maxpacket);
374
375 /* last packet often short (sometimes a zlp, especially on ep0) */
376 if (unlikely(count != ep->ep.maxpacket)) {
377 writel(~(1<<ep->num), &dev->regs->EOP);
378 if (ep->num == 0) {
379 dev->ep[0].stopped = 1;
380 dev->ep0state = EP0_STATUS;
381 }
382 is_last = 1;
383 } else {
384 if (likely(req->req.length != req->req.actual)
385 || req->req.zero)
386 is_last = 0;
387 else
388 is_last = 1;
389 }
390#if 0 /* printk seemed to trash is_last...*/
391//#ifdef USB_TRACE
392 VDBG(dev, "wrote %s %u bytes%s IN %u left %p\n",
393 ep->ep.name, count, is_last ? "/last" : "",
394 req->req.length - req->req.actual, req);
395#endif
396
397 /* requests complete when all IN data is in the FIFO,
398 * or sometimes later, if a zlp was needed.
399 */
400 if (is_last) {
401 done(ep, req, 0);
402 return 1;
403 }
404
405 return 0;
406}
407
408static int read_fifo(struct goku_ep *ep, struct goku_request *req)
409{
410 struct goku_udc_regs __iomem *regs;
411 u32 size, set;
412 u8 *buf;
413 unsigned bufferspace, is_short, dbuff;
414
415 regs = ep->dev->regs;
416top:
417 buf = req->req.buf + req->req.actual;
418 prefetchw(buf);
419
420 if (unlikely(ep->num == 0 && ep->dev->ep0state != EP0_OUT))
421 return -EL2HLT;
422
423 dbuff = (ep->num == 1 || ep->num == 2);
424 do {
425 /* ack dataset irq matching the status we'll handle */
426 if (ep->num != 0)
427 writel(~INT_EPxDATASET(ep->num), &regs->int_status);
428
429 set = readl(&regs->DataSet) & DATASET_AB(ep->num);
430 size = readl(&regs->EPxSizeLA[ep->num]);
431 bufferspace = req->req.length - req->req.actual;
432
433 /* usually do nothing without an OUT packet */
434 if (likely(ep->num != 0 || bufferspace != 0)) {
435 if (unlikely(set == 0))
436 break;
437 /* use ep1/ep2 double-buffering for OUT */
438 if (!(size & PACKET_ACTIVE))
439 size = readl(&regs->EPxSizeLB[ep->num]);
David Brownell2d70c992007-07-01 17:55:50 -0700440 if (!(size & PACKET_ACTIVE)) /* "can't happen" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 break;
442 size &= DATASIZE; /* EPxSizeH == 0 */
443
444 /* ep0out no-out-data case for set_config, etc */
445 } else
446 size = 0;
447
448 /* read all bytes from this packet */
449 req->req.actual += size;
450 is_short = (size < ep->ep.maxpacket);
451#ifdef USB_TRACE
452 VDBG(ep->dev, "read %s %u bytes%s OUT req %p %u/%u\n",
453 ep->ep.name, size, is_short ? "/S" : "",
454 req, req->req.actual, req->req.length);
455#endif
456 while (likely(size-- != 0)) {
457 u8 byte = (u8) readl(ep->reg_fifo);
458
459 if (unlikely(bufferspace == 0)) {
460 /* this happens when the driver's buffer
461 * is smaller than what the host sent.
462 * discard the extra data in this packet.
463 */
464 if (req->req.status != -EOVERFLOW)
465 DBG(ep->dev, "%s overflow %u\n",
466 ep->ep.name, size);
467 req->req.status = -EOVERFLOW;
468 } else {
469 *buf++ = byte;
470 bufferspace--;
471 }
472 }
473
474 /* completion */
475 if (unlikely(is_short || req->req.actual == req->req.length)) {
476 if (unlikely(ep->num == 0)) {
477 /* non-control endpoints now usable? */
478 if (ep->dev->req_config)
479 writel(ep->dev->configured
480 ? USBSTATE_CONFIGURED
481 : 0,
482 &regs->UsbState);
483 /* ep0out status stage */
484 writel(~(1<<0), &regs->EOP);
485 ep->stopped = 1;
486 ep->dev->ep0state = EP0_STATUS;
487 }
488 done(ep, req, 0);
489
490 /* empty the second buffer asap */
491 if (dbuff && !list_empty(&ep->queue)) {
492 req = list_entry(ep->queue.next,
493 struct goku_request, queue);
494 goto top;
495 }
496 return 1;
497 }
498 } while (dbuff);
499 return 0;
500}
501
502static inline void
503pio_irq_enable(struct goku_udc *dev,
504 struct goku_udc_regs __iomem *regs, int epnum)
505{
506 dev->int_enable |= INT_EPxDATASET (epnum);
507 writel(dev->int_enable, &regs->int_enable);
508 /* write may still be posted */
509}
510
511static inline void
512pio_irq_disable(struct goku_udc *dev,
513 struct goku_udc_regs __iomem *regs, int epnum)
514{
515 dev->int_enable &= ~INT_EPxDATASET (epnum);
516 writel(dev->int_enable, &regs->int_enable);
517 /* write may still be posted */
518}
519
520static inline void
521pio_advance(struct goku_ep *ep)
522{
523 struct goku_request *req;
524
525 if (unlikely(list_empty (&ep->queue)))
526 return;
527 req = list_entry(ep->queue.next, struct goku_request, queue);
528 (ep->is_in ? write_fifo : read_fifo)(ep, req);
529}
530
531
532/*-------------------------------------------------------------------------*/
533
534// return: 0 = q running, 1 = q stopped, negative = errno
535static int start_dma(struct goku_ep *ep, struct goku_request *req)
536{
537 struct goku_udc_regs __iomem *regs = ep->dev->regs;
538 u32 master;
539 u32 start = req->req.dma;
540 u32 end = start + req->req.length - 1;
541
542 master = readl(&regs->dma_master) & MST_RW_BITS;
543
544 /* re-init the bits affecting IN dma; careful with zlps */
545 if (likely(ep->is_in)) {
546 if (unlikely(master & MST_RD_ENA)) {
547 DBG (ep->dev, "start, IN active dma %03x!!\n",
548 master);
549// return -EL2HLT;
550 }
551 writel(end, &regs->in_dma_end);
552 writel(start, &regs->in_dma_start);
553
554 master &= ~MST_R_BITS;
555 if (unlikely(req->req.length == 0))
556 master = MST_RD_ENA | MST_RD_EOPB;
557 else if ((req->req.length % ep->ep.maxpacket) != 0
558 || req->req.zero)
559 master = MST_RD_ENA | MST_EOPB_ENA;
560 else
561 master = MST_RD_ENA | MST_EOPB_DIS;
562
563 ep->dev->int_enable |= INT_MSTRDEND;
564
565 /* Goku DMA-OUT merges short packets, which plays poorly with
566 * protocols where short packets mark the transfer boundaries.
567 * The chip supports a nonstandard policy with INT_MSTWRTMOUT,
568 * ending transfers after 3 SOFs; we don't turn it on.
569 */
570 } else {
571 if (unlikely(master & MST_WR_ENA)) {
572 DBG (ep->dev, "start, OUT active dma %03x!!\n",
573 master);
574// return -EL2HLT;
575 }
576 writel(end, &regs->out_dma_end);
577 writel(start, &regs->out_dma_start);
578
579 master &= ~MST_W_BITS;
580 master |= MST_WR_ENA | MST_TIMEOUT_DIS;
581
582 ep->dev->int_enable |= INT_MSTWREND|INT_MSTWRTMOUT;
583 }
584
585 writel(master, &regs->dma_master);
586 writel(ep->dev->int_enable, &regs->int_enable);
587 return 0;
588}
589
590static void dma_advance(struct goku_udc *dev, struct goku_ep *ep)
591{
592 struct goku_request *req;
593 struct goku_udc_regs __iomem *regs = ep->dev->regs;
594 u32 master;
595
596 master = readl(&regs->dma_master);
597
598 if (unlikely(list_empty(&ep->queue))) {
599stop:
600 if (ep->is_in)
601 dev->int_enable &= ~INT_MSTRDEND;
602 else
603 dev->int_enable &= ~(INT_MSTWREND|INT_MSTWRTMOUT);
604 writel(dev->int_enable, &regs->int_enable);
605 return;
606 }
607 req = list_entry(ep->queue.next, struct goku_request, queue);
608
609 /* normal hw dma completion (not abort) */
610 if (likely(ep->is_in)) {
611 if (unlikely(master & MST_RD_ENA))
612 return;
613 req->req.actual = readl(&regs->in_dma_current);
614 } else {
615 if (unlikely(master & MST_WR_ENA))
616 return;
617
618 /* hardware merges short packets, and also hides packet
619 * overruns. a partial packet MAY be in the fifo here.
620 */
621 req->req.actual = readl(&regs->out_dma_current);
622 }
623 req->req.actual -= req->req.dma;
624 req->req.actual++;
625
626#ifdef USB_TRACE
627 VDBG(dev, "done %s %s dma, %u/%u bytes, req %p\n",
628 ep->ep.name, ep->is_in ? "IN" : "OUT",
629 req->req.actual, req->req.length, req);
630#endif
631 done(ep, req, 0);
632 if (list_empty(&ep->queue))
633 goto stop;
634 req = list_entry(ep->queue.next, struct goku_request, queue);
635 (void) start_dma(ep, req);
636}
637
638static void abort_dma(struct goku_ep *ep, int status)
639{
640 struct goku_udc_regs __iomem *regs = ep->dev->regs;
641 struct goku_request *req;
642 u32 curr, master;
643
644 /* NAK future host requests, hoping the implicit delay lets the
645 * dma engine finish reading (or writing) its latest packet and
646 * empty the dma buffer (up to 16 bytes).
647 *
648 * This avoids needing to clean up a partial packet in the fifo;
649 * we can't do that for IN without side effects to HALT and TOGGLE.
650 */
651 command(regs, COMMAND_FIFO_DISABLE, ep->num);
652 req = list_entry(ep->queue.next, struct goku_request, queue);
653 master = readl(&regs->dma_master) & MST_RW_BITS;
654
655 /* FIXME using these resets isn't usably documented. this may
656 * not work unless it's followed by disabling the endpoint.
657 *
658 * FIXME the OUT reset path doesn't even behave consistently.
659 */
660 if (ep->is_in) {
661 if (unlikely((readl(&regs->dma_master) & MST_RD_ENA) == 0))
662 goto finished;
663 curr = readl(&regs->in_dma_current);
664
665 writel(curr, &regs->in_dma_end);
666 writel(curr, &regs->in_dma_start);
667
668 master &= ~MST_R_BITS;
669 master |= MST_RD_RESET;
670 writel(master, &regs->dma_master);
671
672 if (readl(&regs->dma_master) & MST_RD_ENA)
673 DBG(ep->dev, "IN dma active after reset!\n");
674
675 } else {
676 if (unlikely((readl(&regs->dma_master) & MST_WR_ENA) == 0))
677 goto finished;
678 curr = readl(&regs->out_dma_current);
679
680 writel(curr, &regs->out_dma_end);
681 writel(curr, &regs->out_dma_start);
682
683 master &= ~MST_W_BITS;
684 master |= MST_WR_RESET;
685 writel(master, &regs->dma_master);
686
687 if (readl(&regs->dma_master) & MST_WR_ENA)
688 DBG(ep->dev, "OUT dma active after reset!\n");
689 }
690 req->req.actual = (curr - req->req.dma) + 1;
691 req->req.status = status;
692
Harvey Harrison441b62c2008-03-03 16:08:34 -0800693 VDBG(ep->dev, "%s %s %s %d/%d\n", __func__, ep->ep.name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 ep->is_in ? "IN" : "OUT",
695 req->req.actual, req->req.length);
696
697 command(regs, COMMAND_FIFO_ENABLE, ep->num);
698
699 return;
700
701finished:
702 /* dma already completed; no abort needed */
703 command(regs, COMMAND_FIFO_ENABLE, ep->num);
704 req->req.actual = req->req.length;
705 req->req.status = 0;
706}
707
708/*-------------------------------------------------------------------------*/
709
710static int
Al Viro55016f12005-10-21 03:21:58 -0400711goku_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
713 struct goku_request *req;
714 struct goku_ep *ep;
715 struct goku_udc *dev;
716 unsigned long flags;
717 int status;
718
719 /* always require a cpu-view buffer so pio works */
720 req = container_of(_req, struct goku_request, req);
721 if (unlikely(!_req || !_req->complete
722 || !_req->buf || !list_empty(&req->queue)))
723 return -EINVAL;
724 ep = container_of(_ep, struct goku_ep, ep);
Ido Shayevitzdb79d982012-03-12 20:25:31 +0200725 if (unlikely(!_ep || (!ep->ep.desc && ep->num != 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 return -EINVAL;
727 dev = ep->dev;
728 if (unlikely(!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN))
729 return -ESHUTDOWN;
730
731 /* can't touch registers when suspended */
732 if (dev->ep0state == EP0_SUSPEND)
733 return -EBUSY;
734
735 /* set up dma mapping in case the caller didn't */
Felipe Balbi64400932011-12-19 12:11:44 +0200736 if (ep->dma) {
737 status = usb_gadget_map_request(&dev->gadget, &req->req,
738 ep->is_in);
739 if (status)
740 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 }
742
743#ifdef USB_TRACE
744 VDBG(dev, "%s queue req %p, len %u buf %p\n",
745 _ep->name, _req, _req->length, _req->buf);
746#endif
747
748 spin_lock_irqsave(&dev->lock, flags);
749
750 _req->status = -EINPROGRESS;
751 _req->actual = 0;
752
753 /* for ep0 IN without premature status, zlp is required and
754 * writing EOP starts the status stage (OUT).
755 */
756 if (unlikely(ep->num == 0 && ep->is_in))
757 _req->zero = 1;
758
759 /* kickstart this i/o queue? */
760 status = 0;
761 if (list_empty(&ep->queue) && likely(!ep->stopped)) {
762 /* dma: done after dma completion IRQ (or error)
763 * pio: done after last fifo operation
764 */
765 if (ep->dma)
766 status = start_dma(ep, req);
767 else
768 status = (ep->is_in ? write_fifo : read_fifo)(ep, req);
769
770 if (unlikely(status != 0)) {
771 if (status > 0)
772 status = 0;
773 req = NULL;
774 }
775
776 } /* else pio or dma irq handler advances the queue. */
777
778 if (likely(req != 0))
779 list_add_tail(&req->queue, &ep->queue);
780
781 if (likely(!list_empty(&ep->queue))
782 && likely(ep->num != 0)
783 && !ep->dma
784 && !(dev->int_enable & INT_EPxDATASET (ep->num)))
785 pio_irq_enable(dev, dev->regs, ep->num);
786
787 spin_unlock_irqrestore(&dev->lock, flags);
788
789 /* pci writes may still be posted */
790 return status;
791}
792
793/* dequeue ALL requests */
794static void nuke(struct goku_ep *ep, int status)
795{
796 struct goku_request *req;
797
798 ep->stopped = 1;
799 if (list_empty(&ep->queue))
800 return;
801 if (ep->dma)
802 abort_dma(ep, status);
803 while (!list_empty(&ep->queue)) {
804 req = list_entry(ep->queue.next, struct goku_request, queue);
805 done(ep, req, status);
806 }
807}
808
809/* dequeue JUST ONE request */
810static int goku_dequeue(struct usb_ep *_ep, struct usb_request *_req)
811{
812 struct goku_request *req;
813 struct goku_ep *ep;
814 struct goku_udc *dev;
815 unsigned long flags;
816
817 ep = container_of(_ep, struct goku_ep, ep);
Ido Shayevitzdb79d982012-03-12 20:25:31 +0200818 if (!_ep || !_req || (!ep->ep.desc && ep->num != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 return -EINVAL;
820 dev = ep->dev;
821 if (!dev->driver)
822 return -ESHUTDOWN;
823
824 /* we can't touch (dma) registers when suspended */
825 if (dev->ep0state == EP0_SUSPEND)
826 return -EBUSY;
827
Harvey Harrison441b62c2008-03-03 16:08:34 -0800828 VDBG(dev, "%s %s %s %s %p\n", __func__, _ep->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 ep->is_in ? "IN" : "OUT",
830 ep->dma ? "dma" : "pio",
831 _req);
832
833 spin_lock_irqsave(&dev->lock, flags);
834
835 /* make sure it's actually queued on this endpoint */
836 list_for_each_entry (req, &ep->queue, queue) {
837 if (&req->req == _req)
838 break;
839 }
840 if (&req->req != _req) {
841 spin_unlock_irqrestore (&dev->lock, flags);
842 return -EINVAL;
843 }
844
845 if (ep->dma && ep->queue.next == &req->queue && !ep->stopped) {
846 abort_dma(ep, -ECONNRESET);
847 done(ep, req, -ECONNRESET);
848 dma_advance(dev, ep);
849 } else if (!list_empty(&req->queue))
850 done(ep, req, -ECONNRESET);
851 else
852 req = NULL;
853 spin_unlock_irqrestore(&dev->lock, flags);
854
855 return req ? 0 : -EOPNOTSUPP;
856}
857
858/*-------------------------------------------------------------------------*/
859
860static void goku_clear_halt(struct goku_ep *ep)
861{
862 // assert (ep->num !=0)
863 VDBG(ep->dev, "%s clear halt\n", ep->ep.name);
864 command(ep->dev->regs, COMMAND_SETDATA0, ep->num);
865 command(ep->dev->regs, COMMAND_STALL_CLEAR, ep->num);
866 if (ep->stopped) {
867 ep->stopped = 0;
868 if (ep->dma) {
869 struct goku_request *req;
870
871 if (list_empty(&ep->queue))
872 return;
873 req = list_entry(ep->queue.next, struct goku_request,
874 queue);
875 (void) start_dma(ep, req);
876 } else
877 pio_advance(ep);
878 }
879}
880
881static int goku_set_halt(struct usb_ep *_ep, int value)
882{
883 struct goku_ep *ep;
884 unsigned long flags;
885 int retval = 0;
886
887 if (!_ep)
888 return -ENODEV;
889 ep = container_of (_ep, struct goku_ep, ep);
890
891 if (ep->num == 0) {
892 if (value) {
893 ep->dev->ep0state = EP0_STALL;
894 ep->dev->ep[0].stopped = 1;
895 } else
896 return -EINVAL;
897
898 /* don't change EPxSTATUS_EP_INVALID to READY */
Ido Shayevitzdb79d982012-03-12 20:25:31 +0200899 } else if (!ep->ep.desc) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800900 DBG(ep->dev, "%s %s inactive?\n", __func__, ep->ep.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 return -EINVAL;
902 }
903
904 spin_lock_irqsave(&ep->dev->lock, flags);
905 if (!list_empty(&ep->queue))
906 retval = -EAGAIN;
907 else if (ep->is_in && value
908 /* data in (either) packet buffer? */
909 && (readl(&ep->dev->regs->DataSet)
910 & DATASET_AB(ep->num)))
911 retval = -EAGAIN;
912 else if (!value)
913 goku_clear_halt(ep);
914 else {
915 ep->stopped = 1;
916 VDBG(ep->dev, "%s set halt\n", ep->ep.name);
917 command(ep->dev->regs, COMMAND_STALL, ep->num);
918 readl(ep->reg_status);
919 }
920 spin_unlock_irqrestore(&ep->dev->lock, flags);
921 return retval;
922}
923
924static int goku_fifo_status(struct usb_ep *_ep)
925{
926 struct goku_ep *ep;
927 struct goku_udc_regs __iomem *regs;
928 u32 size;
929
930 if (!_ep)
931 return -ENODEV;
932 ep = container_of(_ep, struct goku_ep, ep);
933
934 /* size is only reported sanely for OUT */
935 if (ep->is_in)
936 return -EOPNOTSUPP;
937
938 /* ignores 16-byte dma buffer; SizeH == 0 */
939 regs = ep->dev->regs;
940 size = readl(&regs->EPxSizeLA[ep->num]) & DATASIZE;
941 size += readl(&regs->EPxSizeLB[ep->num]) & DATASIZE;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800942 VDBG(ep->dev, "%s %s %u\n", __func__, ep->ep.name, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 return size;
944}
945
946static void goku_fifo_flush(struct usb_ep *_ep)
947{
948 struct goku_ep *ep;
949 struct goku_udc_regs __iomem *regs;
950 u32 size;
951
952 if (!_ep)
953 return;
954 ep = container_of(_ep, struct goku_ep, ep);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800955 VDBG(ep->dev, "%s %s\n", __func__, ep->ep.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
957 /* don't change EPxSTATUS_EP_INVALID to READY */
Ido Shayevitzdb79d982012-03-12 20:25:31 +0200958 if (!ep->ep.desc && ep->num != 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800959 DBG(ep->dev, "%s %s inactive?\n", __func__, ep->ep.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 return;
961 }
962
963 regs = ep->dev->regs;
964 size = readl(&regs->EPxSizeLA[ep->num]);
965 size &= DATASIZE;
966
967 /* Non-desirable behavior: FIFO_CLEAR also clears the
968 * endpoint halt feature. For OUT, we _could_ just read
969 * the bytes out (PIO, if !ep->dma); for in, no choice.
970 */
971 if (size)
972 command(regs, COMMAND_FIFO_CLEAR, ep->num);
973}
974
975static struct usb_ep_ops goku_ep_ops = {
976 .enable = goku_ep_enable,
977 .disable = goku_ep_disable,
978
979 .alloc_request = goku_alloc_request,
980 .free_request = goku_free_request,
981
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 .queue = goku_queue,
983 .dequeue = goku_dequeue,
984
985 .set_halt = goku_set_halt,
986 .fifo_status = goku_fifo_status,
987 .fifo_flush = goku_fifo_flush,
988};
989
990/*-------------------------------------------------------------------------*/
991
992static int goku_get_frame(struct usb_gadget *_gadget)
993{
994 return -EOPNOTSUPP;
995}
996
Felipe Balbi950b3c12013-01-24 10:40:21 +0200997static int goku_udc_start(struct usb_gadget *g,
998 struct usb_gadget_driver *driver);
999static int goku_udc_stop(struct usb_gadget *g,
1000 struct usb_gadget_driver *driver);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001001
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002static const struct usb_gadget_ops goku_ops = {
1003 .get_frame = goku_get_frame,
Felipe Balbi950b3c12013-01-24 10:40:21 +02001004 .udc_start = goku_udc_start,
1005 .udc_stop = goku_udc_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 // no remote wakeup
1007 // not selfpowered
1008};
1009
1010/*-------------------------------------------------------------------------*/
1011
David Howells96c7a222013-04-10 11:51:21 +01001012static inline const char *dmastr(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013{
1014 if (use_dma == 0)
1015 return "(dma disabled)";
1016 else if (use_dma == 2)
1017 return "(dma IN and OUT)";
1018 else
1019 return "(dma IN)";
1020}
1021
1022#ifdef CONFIG_USB_GADGET_DEBUG_FILES
1023
1024static const char proc_node_name [] = "driver/udc";
1025
1026#define FOURBITS "%s%s%s%s"
1027#define EIGHTBITS FOURBITS FOURBITS
1028
David Howells96c7a222013-04-10 11:51:21 +01001029static void dump_intmask(struct seq_file *m, const char *label, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 /* int_status is the same format ... */
David Howells96c7a222013-04-10 11:51:21 +01001032 seq_printf(m,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 "%s %05X =" FOURBITS EIGHTBITS EIGHTBITS "\n",
1034 label, mask,
1035 (mask & INT_PWRDETECT) ? " power" : "",
1036 (mask & INT_SYSERROR) ? " sys" : "",
1037 (mask & INT_MSTRDEND) ? " in-dma" : "",
1038 (mask & INT_MSTWRTMOUT) ? " wrtmo" : "",
1039
1040 (mask & INT_MSTWREND) ? " out-dma" : "",
1041 (mask & INT_MSTWRSET) ? " wrset" : "",
1042 (mask & INT_ERR) ? " err" : "",
1043 (mask & INT_SOF) ? " sof" : "",
1044
1045 (mask & INT_EP3NAK) ? " ep3nak" : "",
1046 (mask & INT_EP2NAK) ? " ep2nak" : "",
1047 (mask & INT_EP1NAK) ? " ep1nak" : "",
1048 (mask & INT_EP3DATASET) ? " ep3" : "",
1049
1050 (mask & INT_EP2DATASET) ? " ep2" : "",
1051 (mask & INT_EP1DATASET) ? " ep1" : "",
1052 (mask & INT_STATUSNAK) ? " ep0snak" : "",
1053 (mask & INT_STATUS) ? " ep0status" : "",
1054
1055 (mask & INT_SETUP) ? " setup" : "",
1056 (mask & INT_ENDPOINT0) ? " ep0" : "",
1057 (mask & INT_USBRESET) ? " reset" : "",
1058 (mask & INT_SUSPEND) ? " suspend" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059}
1060
1061
David Howells96c7a222013-04-10 11:51:21 +01001062static int udc_proc_read(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063{
David Howells96c7a222013-04-10 11:51:21 +01001064 struct goku_udc *dev = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 struct goku_udc_regs __iomem *regs = dev->regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 unsigned long flags;
David Howells96c7a222013-04-10 11:51:21 +01001067 int i, is_usb_connected;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 u32 tmp;
1069
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 local_irq_save(flags);
1071
1072 /* basic device status */
1073 tmp = readl(&regs->power_detect);
1074 is_usb_connected = tmp & PW_DETECT;
David Howells96c7a222013-04-10 11:51:21 +01001075 seq_printf(m,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 "%s - %s\n"
1077 "%s version: %s %s\n"
1078 "Gadget driver: %s\n"
1079 "Host %s, %s\n"
1080 "\n",
1081 pci_name(dev->pdev), driver_desc,
1082 driver_name, DRIVER_VERSION, dmastr(),
1083 dev->driver ? dev->driver->driver.name : "(none)",
1084 is_usb_connected
1085 ? ((tmp & PW_PULLUP) ? "full speed" : "powered")
1086 : "disconnected",
David Howells96c7a222013-04-10 11:51:21 +01001087 ({const char *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 switch(dev->ep0state){
David Brownell2d70c992007-07-01 17:55:50 -07001089 case EP0_DISCONNECT: state = "ep0_disconnect"; break;
1090 case EP0_IDLE: state = "ep0_idle"; break;
1091 case EP0_IN: state = "ep0_in"; break;
1092 case EP0_OUT: state = "ep0_out"; break;
1093 case EP0_STATUS: state = "ep0_status"; break;
1094 case EP0_STALL: state = "ep0_stall"; break;
1095 case EP0_SUSPEND: state = "ep0_suspend"; break;
1096 default: state = "ep0_?"; break;
1097 } state; })
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
David Howells96c7a222013-04-10 11:51:21 +01001100 dump_intmask(m, "int_status", readl(&regs->int_status));
1101 dump_intmask(m, "int_enable", readl(&regs->int_enable));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
1103 if (!is_usb_connected || !dev->driver || (tmp & PW_PULLUP) == 0)
1104 goto done;
1105
1106 /* registers for (active) device and ep0 */
David Howells96c7a222013-04-10 11:51:21 +01001107 if (seq_printf(m, "\nirqs %lu\ndataset %02x "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 "single.bcs %02x.%02x state %x addr %u\n",
1109 dev->irqs, readl(&regs->DataSet),
1110 readl(&regs->EPxSingle), readl(&regs->EPxBCS),
1111 readl(&regs->UsbState),
David Howells96c7a222013-04-10 11:51:21 +01001112 readl(&regs->address)) < 0)
1113 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
1115 tmp = readl(&regs->dma_master);
David Howells96c7a222013-04-10 11:51:21 +01001116 if (seq_printf(m,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 "dma %03X =" EIGHTBITS "%s %s\n", tmp,
1118 (tmp & MST_EOPB_DIS) ? " eopb-" : "",
1119 (tmp & MST_EOPB_ENA) ? " eopb+" : "",
1120 (tmp & MST_TIMEOUT_DIS) ? " tmo-" : "",
1121 (tmp & MST_TIMEOUT_ENA) ? " tmo+" : "",
1122
1123 (tmp & MST_RD_EOPB) ? " eopb" : "",
1124 (tmp & MST_RD_RESET) ? " in_reset" : "",
1125 (tmp & MST_WR_RESET) ? " out_reset" : "",
1126 (tmp & MST_RD_ENA) ? " IN" : "",
1127
1128 (tmp & MST_WR_ENA) ? " OUT" : "",
1129 (tmp & MST_CONNECTION)
1130 ? "ep1in/ep2out"
David Howells96c7a222013-04-10 11:51:21 +01001131 : "ep1out/ep2in") < 0)
1132 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
1134 /* dump endpoint queues */
1135 for (i = 0; i < 4; i++) {
1136 struct goku_ep *ep = &dev->ep [i];
1137 struct goku_request *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Ido Shayevitzdb79d982012-03-12 20:25:31 +02001139 if (i && !ep->ep.desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 continue;
1141
1142 tmp = readl(ep->reg_status);
David Howells96c7a222013-04-10 11:51:21 +01001143 if (seq_printf(m,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 "%s %s max %u %s, irqs %lu, "
1145 "status %02x (%s) " FOURBITS "\n",
1146 ep->ep.name,
1147 ep->is_in ? "in" : "out",
1148 ep->ep.maxpacket,
1149 ep->dma ? "dma" : "pio",
1150 ep->irqs,
1151 tmp, ({ char *s;
1152 switch (tmp & EPxSTATUS_EP_MASK) {
1153 case EPxSTATUS_EP_READY:
1154 s = "ready"; break;
1155 case EPxSTATUS_EP_DATAIN:
1156 s = "packet"; break;
1157 case EPxSTATUS_EP_FULL:
1158 s = "full"; break;
1159 case EPxSTATUS_EP_TX_ERR: // host will retry
1160 s = "tx_err"; break;
1161 case EPxSTATUS_EP_RX_ERR:
1162 s = "rx_err"; break;
1163 case EPxSTATUS_EP_BUSY: /* ep0 only */
1164 s = "busy"; break;
1165 case EPxSTATUS_EP_STALL:
1166 s = "stall"; break;
1167 case EPxSTATUS_EP_INVALID: // these "can't happen"
1168 s = "invalid"; break;
1169 default:
1170 s = "?"; break;
1171 }; s; }),
1172 (tmp & EPxSTATUS_TOGGLE) ? "data1" : "data0",
1173 (tmp & EPxSTATUS_SUSPEND) ? " suspend" : "",
1174 (tmp & EPxSTATUS_FIFO_DISABLE) ? " disable" : "",
1175 (tmp & EPxSTATUS_STAGE_ERROR) ? " ep0stat" : ""
David Howells96c7a222013-04-10 11:51:21 +01001176 ) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179 if (list_empty(&ep->queue)) {
David Howells96c7a222013-04-10 11:51:21 +01001180 if (seq_puts(m, "\t(nothing queued)\n") < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 continue;
1183 }
1184 list_for_each_entry(req, &ep->queue, queue) {
1185 if (ep->dma && req->queue.prev == &ep->queue) {
1186 if (i == UDC_MSTRD_ENDPOINT)
1187 tmp = readl(&regs->in_dma_current);
1188 else
1189 tmp = readl(&regs->out_dma_current);
1190 tmp -= req->req.dma;
1191 tmp++;
1192 } else
1193 tmp = req->req.actual;
1194
David Howells96c7a222013-04-10 11:51:21 +01001195 if (seq_printf(m,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 "\treq %p len %u/%u buf %p\n",
1197 &req->req, tmp, req->req.length,
David Howells96c7a222013-04-10 11:51:21 +01001198 req->req.buf) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 }
1201 }
1202
1203done:
1204 local_irq_restore(flags);
David Howells96c7a222013-04-10 11:51:21 +01001205 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206}
1207
David Howells96c7a222013-04-10 11:51:21 +01001208/*
1209 * seq_file wrappers for procfile show routines.
1210 */
1211static int udc_proc_open(struct inode *inode, struct file *file)
1212{
1213 return single_open(file, udc_proc_read, PDE_DATA(file_inode(file)));
1214}
1215
1216static const struct file_operations udc_proc_fops = {
1217 .open = udc_proc_open,
1218 .read = seq_read,
1219 .llseek = seq_lseek,
1220 .release = seq_release,
1221};
1222
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223#endif /* CONFIG_USB_GADGET_DEBUG_FILES */
1224
1225/*-------------------------------------------------------------------------*/
1226
1227static void udc_reinit (struct goku_udc *dev)
1228{
1229 static char *names [] = { "ep0", "ep1-bulk", "ep2-bulk", "ep3-bulk" };
David Brownell2d70c992007-07-01 17:55:50 -07001230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 unsigned i;
1232
1233 INIT_LIST_HEAD (&dev->gadget.ep_list);
1234 dev->gadget.ep0 = &dev->ep [0].ep;
1235 dev->gadget.speed = USB_SPEED_UNKNOWN;
1236 dev->ep0state = EP0_DISCONNECT;
1237 dev->irqs = 0;
1238
1239 for (i = 0; i < 4; i++) {
1240 struct goku_ep *ep = &dev->ep[i];
1241
1242 ep->num = i;
1243 ep->ep.name = names[i];
1244 ep->reg_fifo = &dev->regs->ep_fifo [i];
1245 ep->reg_status = &dev->regs->ep_status [i];
1246 ep->reg_mode = &dev->regs->ep_mode[i];
1247
1248 ep->ep.ops = &goku_ep_ops;
1249 list_add_tail (&ep->ep.ep_list, &dev->gadget.ep_list);
1250 ep->dev = dev;
1251 INIT_LIST_HEAD (&ep->queue);
1252
1253 ep_reset(NULL, ep);
1254 }
1255
1256 dev->ep[0].reg_mode = NULL;
1257 dev->ep[0].ep.maxpacket = MAX_EP0_SIZE;
1258 list_del_init (&dev->ep[0].ep.ep_list);
1259}
1260
1261static void udc_reset(struct goku_udc *dev)
1262{
1263 struct goku_udc_regs __iomem *regs = dev->regs;
1264
1265 writel(0, &regs->power_detect);
1266 writel(0, &regs->int_enable);
1267 readl(&regs->int_enable);
1268 dev->int_enable = 0;
1269
1270 /* deassert reset, leave USB D+ at hi-Z (no pullup)
1271 * don't let INT_PWRDETECT sequence begin
1272 */
1273 udelay(250);
1274 writel(PW_RESETB, &regs->power_detect);
1275 readl(&regs->int_enable);
1276}
1277
1278static void ep0_start(struct goku_udc *dev)
1279{
1280 struct goku_udc_regs __iomem *regs = dev->regs;
1281 unsigned i;
1282
Harvey Harrison441b62c2008-03-03 16:08:34 -08001283 VDBG(dev, "%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
1285 udc_reset(dev);
1286 udc_reinit (dev);
1287 //writel(MST_EOPB_ENA | MST_TIMEOUT_ENA, &regs->dma_master);
1288
1289 /* hw handles set_address, set_feature, get_status; maybe more */
1290 writel( G_REQMODE_SET_INTF | G_REQMODE_GET_INTF
1291 | G_REQMODE_SET_CONF | G_REQMODE_GET_CONF
1292 | G_REQMODE_GET_DESC
1293 | G_REQMODE_CLEAR_FEAT
1294 , &regs->reqmode);
1295
1296 for (i = 0; i < 4; i++)
1297 dev->ep[i].irqs = 0;
1298
1299 /* can't modify descriptors after writing UsbReady */
1300 for (i = 0; i < DESC_LEN; i++)
1301 writel(0, &regs->descriptors[i]);
1302 writel(0, &regs->UsbReady);
1303
1304 /* expect ep0 requests when the host drops reset */
1305 writel(PW_RESETB | PW_PULLUP, &regs->power_detect);
1306 dev->int_enable = INT_DEVWIDE | INT_EP0;
1307 writel(dev->int_enable, &dev->regs->int_enable);
1308 readl(&regs->int_enable);
1309 dev->gadget.speed = USB_SPEED_FULL;
1310 dev->ep0state = EP0_IDLE;
1311}
1312
1313static void udc_enable(struct goku_udc *dev)
1314{
1315 /* start enumeration now, or after power detect irq */
1316 if (readl(&dev->regs->power_detect) & PW_DETECT)
1317 ep0_start(dev);
1318 else {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001319 DBG(dev, "%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 dev->int_enable = INT_PWRDETECT;
1321 writel(dev->int_enable, &dev->regs->int_enable);
1322 }
1323}
1324
1325/*-------------------------------------------------------------------------*/
1326
1327/* keeping it simple:
1328 * - one bus driver, initted first;
1329 * - one function driver, initted second
1330 */
1331
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332/* when a driver is successfully registered, it will receive
1333 * control requests including set_configuration(), which enables
1334 * non-control requests. then usb traffic follows until a
1335 * disconnect is reported. then a host may connect again, or
1336 * the driver might get unbound.
1337 */
Felipe Balbi950b3c12013-01-24 10:40:21 +02001338static int goku_udc_start(struct usb_gadget *g,
1339 struct usb_gadget_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340{
Felipe Balbi950b3c12013-01-24 10:40:21 +02001341 struct goku_udc *dev = to_goku_udc(g);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
1343 /* hook up the driver */
1344 driver->driver.bus = NULL;
1345 dev->driver = driver;
1346 dev->gadget.dev.driver = &driver->driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
Felipe Balbi950b3c12013-01-24 10:40:21 +02001348 /*
1349 * then enable host detection and ep0; and we're ready
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 * for set_configuration as well as eventual disconnect.
1351 */
1352 udc_enable(dev);
1353
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 return 0;
1355}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
1357static void
1358stop_activity(struct goku_udc *dev, struct usb_gadget_driver *driver)
1359{
1360 unsigned i;
1361
Harvey Harrison441b62c2008-03-03 16:08:34 -08001362 DBG (dev, "%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
1364 if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1365 driver = NULL;
1366
1367 /* disconnect gadget driver after quiesceing hw and the driver */
1368 udc_reset (dev);
1369 for (i = 0; i < 4; i++)
1370 nuke(&dev->ep [i], -ESHUTDOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
1372 if (dev->driver)
1373 udc_enable(dev);
1374}
1375
Felipe Balbi950b3c12013-01-24 10:40:21 +02001376static int goku_udc_stop(struct usb_gadget *g,
1377 struct usb_gadget_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378{
Felipe Balbi950b3c12013-01-24 10:40:21 +02001379 struct goku_udc *dev = to_goku_udc(g);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 unsigned long flags;
1381
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 spin_lock_irqsave(&dev->lock, flags);
1383 dev->driver = NULL;
1384 stop_activity(dev, driver);
1385 spin_unlock_irqrestore(&dev->lock, flags);
Patrik Sevalliuseb0be472007-11-20 09:32:00 -08001386 dev->gadget.dev.driver = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 return 0;
1389}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
1391/*-------------------------------------------------------------------------*/
1392
1393static void ep0_setup(struct goku_udc *dev)
1394{
1395 struct goku_udc_regs __iomem *regs = dev->regs;
1396 struct usb_ctrlrequest ctrl;
1397 int tmp;
1398
1399 /* read SETUP packet and enter DATA stage */
1400 ctrl.bRequestType = readl(&regs->bRequestType);
1401 ctrl.bRequest = readl(&regs->bRequest);
David Brownell988199f2005-05-07 13:05:52 -07001402 ctrl.wValue = cpu_to_le16((readl(&regs->wValueH) << 8)
1403 | readl(&regs->wValueL));
1404 ctrl.wIndex = cpu_to_le16((readl(&regs->wIndexH) << 8)
1405 | readl(&regs->wIndexL));
1406 ctrl.wLength = cpu_to_le16((readl(&regs->wLengthH) << 8)
1407 | readl(&regs->wLengthL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 writel(0, &regs->SetupRecv);
1409
1410 nuke(&dev->ep[0], 0);
1411 dev->ep[0].stopped = 0;
1412 if (likely(ctrl.bRequestType & USB_DIR_IN)) {
1413 dev->ep[0].is_in = 1;
1414 dev->ep0state = EP0_IN;
1415 /* detect early status stages */
1416 writel(ICONTROL_STATUSNAK, &dev->regs->IntControl);
1417 } else {
1418 dev->ep[0].is_in = 0;
1419 dev->ep0state = EP0_OUT;
1420
1421 /* NOTE: CLEAR_FEATURE is done in software so that we can
1422 * synchronize transfer restarts after bulk IN stalls. data
1423 * won't even enter the fifo until the halt is cleared.
1424 */
1425 switch (ctrl.bRequest) {
1426 case USB_REQ_CLEAR_FEATURE:
1427 switch (ctrl.bRequestType) {
1428 case USB_RECIP_ENDPOINT:
David Brownell988199f2005-05-07 13:05:52 -07001429 tmp = le16_to_cpu(ctrl.wIndex) & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 /* active endpoint */
Ido Shayevitzdb79d982012-03-12 20:25:31 +02001431 if (tmp > 3 ||
1432 (!dev->ep[tmp].ep.desc && tmp != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 goto stall;
Harvey Harrison551509d2009-02-11 14:11:36 -08001434 if (ctrl.wIndex & cpu_to_le16(
David Brownell988199f2005-05-07 13:05:52 -07001435 USB_DIR_IN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 if (!dev->ep[tmp].is_in)
1437 goto stall;
1438 } else {
1439 if (dev->ep[tmp].is_in)
1440 goto stall;
1441 }
Harvey Harrison551509d2009-02-11 14:11:36 -08001442 if (ctrl.wValue != cpu_to_le16(
David Brownell988199f2005-05-07 13:05:52 -07001443 USB_ENDPOINT_HALT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 goto stall;
1445 if (tmp)
1446 goku_clear_halt(&dev->ep[tmp]);
1447succeed:
1448 /* start ep0out status stage */
1449 writel(~(1<<0), &regs->EOP);
1450 dev->ep[0].stopped = 1;
1451 dev->ep0state = EP0_STATUS;
1452 return;
1453 case USB_RECIP_DEVICE:
1454 /* device remote wakeup: always clear */
Harvey Harrison551509d2009-02-11 14:11:36 -08001455 if (ctrl.wValue != cpu_to_le16(1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 goto stall;
1457 VDBG(dev, "clear dev remote wakeup\n");
1458 goto succeed;
1459 case USB_RECIP_INTERFACE:
1460 goto stall;
1461 default: /* pass to gadget driver */
1462 break;
1463 }
1464 break;
1465 default:
1466 break;
1467 }
1468 }
1469
1470#ifdef USB_TRACE
1471 VDBG(dev, "SETUP %02x.%02x v%04x i%04x l%04x\n",
1472 ctrl.bRequestType, ctrl.bRequest,
David Brownell988199f2005-05-07 13:05:52 -07001473 le16_to_cpu(ctrl.wValue), le16_to_cpu(ctrl.wIndex),
1474 le16_to_cpu(ctrl.wLength));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475#endif
1476
1477 /* hw wants to know when we're configured (or not) */
1478 dev->req_config = (ctrl.bRequest == USB_REQ_SET_CONFIGURATION
1479 && ctrl.bRequestType == USB_RECIP_DEVICE);
1480 if (unlikely(dev->req_config))
Harvey Harrison551509d2009-02-11 14:11:36 -08001481 dev->configured = (ctrl.wValue != cpu_to_le16(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
1483 /* delegate everything to the gadget driver.
1484 * it may respond after this irq handler returns.
1485 */
1486 spin_unlock (&dev->lock);
1487 tmp = dev->driver->setup(&dev->gadget, &ctrl);
1488 spin_lock (&dev->lock);
1489 if (unlikely(tmp < 0)) {
1490stall:
1491#ifdef USB_TRACE
1492 VDBG(dev, "req %02x.%02x protocol STALL; err %d\n",
1493 ctrl.bRequestType, ctrl.bRequest, tmp);
1494#endif
1495 command(regs, COMMAND_STALL, 0);
1496 dev->ep[0].stopped = 1;
1497 dev->ep0state = EP0_STALL;
1498 }
1499
1500 /* expect at least one data or status stage irq */
1501}
1502
1503#define ACK(irqbit) { \
1504 stat &= ~irqbit; \
1505 writel(~irqbit, &regs->int_status); \
1506 handled = 1; \
1507 }
1508
David Howells7d12e782006-10-05 14:55:46 +01001509static irqreturn_t goku_irq(int irq, void *_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510{
1511 struct goku_udc *dev = _dev;
1512 struct goku_udc_regs __iomem *regs = dev->regs;
1513 struct goku_ep *ep;
1514 u32 stat, handled = 0;
1515 unsigned i, rescans = 5;
1516
1517 spin_lock(&dev->lock);
1518
1519rescan:
1520 stat = readl(&regs->int_status) & dev->int_enable;
1521 if (!stat)
1522 goto done;
1523 dev->irqs++;
1524
1525 /* device-wide irqs */
1526 if (unlikely(stat & INT_DEVWIDE)) {
1527 if (stat & INT_SYSERROR) {
1528 ERROR(dev, "system error\n");
1529 stop_activity(dev, dev->driver);
1530 stat = 0;
1531 handled = 1;
1532 // FIXME have a neater way to prevent re-enumeration
1533 dev->driver = NULL;
1534 goto done;
1535 }
1536 if (stat & INT_PWRDETECT) {
1537 writel(~stat, &regs->int_status);
1538 if (readl(&dev->regs->power_detect) & PW_DETECT) {
1539 VDBG(dev, "connect\n");
1540 ep0_start(dev);
1541 } else {
1542 DBG(dev, "disconnect\n");
1543 if (dev->gadget.speed == USB_SPEED_FULL)
1544 stop_activity(dev, dev->driver);
1545 dev->ep0state = EP0_DISCONNECT;
1546 dev->int_enable = INT_DEVWIDE;
1547 writel(dev->int_enable, &dev->regs->int_enable);
1548 }
1549 stat = 0;
1550 handled = 1;
1551 goto done;
1552 }
1553 if (stat & INT_SUSPEND) {
1554 ACK(INT_SUSPEND);
1555 if (readl(&regs->ep_status[0]) & EPxSTATUS_SUSPEND) {
1556 switch (dev->ep0state) {
1557 case EP0_DISCONNECT:
1558 case EP0_SUSPEND:
1559 goto pm_next;
1560 default:
1561 break;
1562 }
1563 DBG(dev, "USB suspend\n");
1564 dev->ep0state = EP0_SUSPEND;
1565 if (dev->gadget.speed != USB_SPEED_UNKNOWN
1566 && dev->driver
1567 && dev->driver->suspend) {
1568 spin_unlock(&dev->lock);
1569 dev->driver->suspend(&dev->gadget);
1570 spin_lock(&dev->lock);
1571 }
1572 } else {
1573 if (dev->ep0state != EP0_SUSPEND) {
1574 DBG(dev, "bogus USB resume %d\n",
1575 dev->ep0state);
1576 goto pm_next;
1577 }
1578 DBG(dev, "USB resume\n");
1579 dev->ep0state = EP0_IDLE;
1580 if (dev->gadget.speed != USB_SPEED_UNKNOWN
1581 && dev->driver
1582 && dev->driver->resume) {
1583 spin_unlock(&dev->lock);
1584 dev->driver->resume(&dev->gadget);
1585 spin_lock(&dev->lock);
1586 }
1587 }
1588 }
1589pm_next:
1590 if (stat & INT_USBRESET) { /* hub reset done */
1591 ACK(INT_USBRESET);
1592 INFO(dev, "USB reset done, gadget %s\n",
1593 dev->driver->driver.name);
1594 }
1595 // and INT_ERR on some endpoint's crc/bitstuff/... problem
1596 }
1597
1598 /* progress ep0 setup, data, or status stages.
1599 * no transition {EP0_STATUS, EP0_STALL} --> EP0_IDLE; saves irqs
1600 */
1601 if (stat & INT_SETUP) {
1602 ACK(INT_SETUP);
1603 dev->ep[0].irqs++;
1604 ep0_setup(dev);
1605 }
1606 if (stat & INT_STATUSNAK) {
1607 ACK(INT_STATUSNAK|INT_ENDPOINT0);
1608 if (dev->ep0state == EP0_IN) {
1609 ep = &dev->ep[0];
1610 ep->irqs++;
1611 nuke(ep, 0);
1612 writel(~(1<<0), &regs->EOP);
1613 dev->ep0state = EP0_STATUS;
1614 }
1615 }
1616 if (stat & INT_ENDPOINT0) {
1617 ACK(INT_ENDPOINT0);
1618 ep = &dev->ep[0];
1619 ep->irqs++;
1620 pio_advance(ep);
1621 }
1622
1623 /* dma completion */
1624 if (stat & INT_MSTRDEND) { /* IN */
1625 ACK(INT_MSTRDEND);
1626 ep = &dev->ep[UDC_MSTRD_ENDPOINT];
1627 ep->irqs++;
1628 dma_advance(dev, ep);
1629 }
1630 if (stat & INT_MSTWREND) { /* OUT */
1631 ACK(INT_MSTWREND);
1632 ep = &dev->ep[UDC_MSTWR_ENDPOINT];
1633 ep->irqs++;
1634 dma_advance(dev, ep);
1635 }
1636 if (stat & INT_MSTWRTMOUT) { /* OUT */
1637 ACK(INT_MSTWRTMOUT);
1638 ep = &dev->ep[UDC_MSTWR_ENDPOINT];
1639 ep->irqs++;
1640 ERROR(dev, "%s write timeout ?\n", ep->ep.name);
1641 // reset dma? then dma_advance()
1642 }
1643
1644 /* pio */
1645 for (i = 1; i < 4; i++) {
1646 u32 tmp = INT_EPxDATASET(i);
1647
1648 if (!(stat & tmp))
1649 continue;
1650 ep = &dev->ep[i];
1651 pio_advance(ep);
1652 if (list_empty (&ep->queue))
1653 pio_irq_disable(dev, regs, i);
1654 stat &= ~tmp;
1655 handled = 1;
1656 ep->irqs++;
1657 }
1658
1659 if (rescans--)
1660 goto rescan;
1661
1662done:
1663 (void)readl(&regs->int_enable);
1664 spin_unlock(&dev->lock);
1665 if (stat)
1666 DBG(dev, "unhandled irq status: %05x (%05x, %05x)\n", stat,
1667 readl(&regs->int_status), dev->int_enable);
1668 return IRQ_RETVAL(handled);
1669}
1670
1671#undef ACK
1672
1673/*-------------------------------------------------------------------------*/
1674
1675static void gadget_release(struct device *_dev)
1676{
1677 struct goku_udc *dev = dev_get_drvdata(_dev);
1678
1679 kfree(dev);
1680}
1681
1682/* tear down the binding between this driver and the pci device */
1683
1684static void goku_remove(struct pci_dev *pdev)
1685{
1686 struct goku_udc *dev = pci_get_drvdata(pdev);
1687
Harvey Harrison441b62c2008-03-03 16:08:34 -08001688 DBG(dev, "%s\n", __func__);
David Brownell6bea4762006-12-05 03:15:33 -08001689
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001690 usb_del_gadget_udc(&dev->gadget);
1691
David Brownell6bea4762006-12-05 03:15:33 -08001692 BUG_ON(dev->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
1694#ifdef CONFIG_USB_GADGET_DEBUG_FILES
1695 remove_proc_entry(proc_node_name, NULL);
1696#endif
1697 if (dev->regs)
1698 udc_reset(dev);
1699 if (dev->got_irq)
1700 free_irq(pdev->irq, dev);
1701 if (dev->regs)
1702 iounmap(dev->regs);
1703 if (dev->got_region)
1704 release_mem_region(pci_resource_start (pdev, 0),
1705 pci_resource_len (pdev, 0));
1706 if (dev->enabled)
1707 pci_disable_device(pdev);
Dan Carpenter319feaa2010-10-05 18:55:34 +02001708 if (dev->registered)
1709 device_unregister(&dev->gadget.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710
1711 pci_set_drvdata(pdev, NULL);
1712 dev->regs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
1714 INFO(dev, "unbind\n");
1715}
1716
1717/* wrap this driver around the specified pci device, but
1718 * don't respond over USB until a gadget driver binds to us.
1719 */
1720
1721static int goku_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1722{
1723 struct goku_udc *dev = NULL;
1724 unsigned long resource, len;
1725 void __iomem *base = NULL;
1726 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 if (!pdev->irq) {
1729 printk(KERN_ERR "Check PCI %s IRQ setup!\n", pci_name(pdev));
1730 retval = -ENODEV;
Dan Carpenter319feaa2010-10-05 18:55:34 +02001731 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 }
1733
1734 /* alloc, and start init */
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07001735 dev = kzalloc (sizeof *dev, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 if (dev == NULL){
1737 pr_debug("enomem %s\n", pci_name(pdev));
1738 retval = -ENOMEM;
Dan Carpenter319feaa2010-10-05 18:55:34 +02001739 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 }
1741
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 spin_lock_init(&dev->lock);
1743 dev->pdev = pdev;
1744 dev->gadget.ops = &goku_ops;
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01001745 dev->gadget.max_speed = USB_SPEED_FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
1747 /* the "gadget" abstracts/virtualizes the controller */
Kay Sievers0031a062008-05-02 06:02:41 +02001748 dev_set_name(&dev->gadget.dev, "gadget");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 dev->gadget.dev.parent = &pdev->dev;
1750 dev->gadget.dev.dma_mask = pdev->dev.dma_mask;
1751 dev->gadget.dev.release = gadget_release;
1752 dev->gadget.name = driver_name;
1753
1754 /* now all the pci goodies ... */
1755 retval = pci_enable_device(pdev);
1756 if (retval < 0) {
1757 DBG(dev, "can't enable, %d\n", retval);
Dan Carpenter319feaa2010-10-05 18:55:34 +02001758 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 }
1760 dev->enabled = 1;
1761
1762 resource = pci_resource_start(pdev, 0);
1763 len = pci_resource_len(pdev, 0);
1764 if (!request_mem_region(resource, len, driver_name)) {
1765 DBG(dev, "controller already in use\n");
1766 retval = -EBUSY;
Dan Carpenter319feaa2010-10-05 18:55:34 +02001767 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 }
1769 dev->got_region = 1;
1770
1771 base = ioremap_nocache(resource, len);
1772 if (base == NULL) {
1773 DBG(dev, "can't map memory\n");
1774 retval = -EFAULT;
Dan Carpenter319feaa2010-10-05 18:55:34 +02001775 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 }
1777 dev->regs = (struct goku_udc_regs __iomem *) base;
1778
1779 pci_set_drvdata(pdev, dev);
1780 INFO(dev, "%s\n", driver_desc);
1781 INFO(dev, "version: " DRIVER_VERSION " %s\n", dmastr());
David S. Millerc6387a42006-06-20 01:21:29 -07001782 INFO(dev, "irq %d, pci mem %p\n", pdev->irq, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
1784 /* init to known state, then setup irqs */
1785 udc_reset(dev);
1786 udc_reinit (dev);
Theodore Ts'o11bc2682012-07-17 13:53:36 -04001787 if (request_irq(pdev->irq, goku_irq, IRQF_SHARED,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 driver_name, dev) != 0) {
David S. Millerc6387a42006-06-20 01:21:29 -07001789 DBG(dev, "request interrupt %d failed\n", pdev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 retval = -EBUSY;
Dan Carpenter319feaa2010-10-05 18:55:34 +02001791 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 }
1793 dev->got_irq = 1;
1794 if (use_dma)
1795 pci_set_master(pdev);
1796
1797
1798#ifdef CONFIG_USB_GADGET_DEBUG_FILES
David Howells96c7a222013-04-10 11:51:21 +01001799 proc_create_data(proc_node_name, 0, NULL, &udc_proc_fops, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800#endif
1801
David Brownell2d70c992007-07-01 17:55:50 -07001802 retval = device_register(&dev->gadget.dev);
Dan Carpenter319feaa2010-10-05 18:55:34 +02001803 if (retval) {
1804 put_device(&dev->gadget.dev);
1805 goto err;
1806 }
1807 dev->registered = 1;
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001808 retval = usb_add_gadget_udc(&pdev->dev, &dev->gadget);
1809 if (retval)
1810 goto err;
1811
Dan Carpenter319feaa2010-10-05 18:55:34 +02001812 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
Dan Carpenter319feaa2010-10-05 18:55:34 +02001814err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 if (dev)
1816 goku_remove (pdev);
1817 return retval;
1818}
1819
1820
1821/*-------------------------------------------------------------------------*/
1822
Németh Márton1ba25572010-01-10 15:35:23 +01001823static const struct pci_device_id pci_ids[] = { {
David Brownell2d70c992007-07-01 17:55:50 -07001824 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
1825 .class_mask = ~0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 .vendor = 0x102f, /* Toshiba */
1827 .device = 0x0107, /* this UDC */
1828 .subvendor = PCI_ANY_ID,
1829 .subdevice = PCI_ANY_ID,
1830
1831}, { /* end: all zeroes */ }
1832};
1833MODULE_DEVICE_TABLE (pci, pci_ids);
1834
1835static struct pci_driver goku_pci_driver = {
1836 .name = (char *) driver_name,
1837 .id_table = pci_ids,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
1839 .probe = goku_probe,
1840 .remove = goku_remove,
1841
1842 /* FIXME add power management support */
1843};
1844
Axel Lin3cdb7722012-04-04 22:14:58 +08001845module_pci_driver(goku_pci_driver);