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