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