blob: 1a362c5e7f3d05c88fdfc66af743c30dfc067a91 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/usb/gadget/lh7a40x_udc.c
3 * Sharp LH7A40x on-chip full speed USB device controllers
4 *
5 * Copyright (C) 2004 Mikko Lahteenmaki, Nordic ID
6 * Copyright (C) 2004 Bo Henriksen, Nordic ID
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 */
23
Russell Kingd052d1b2005-10-29 19:07:23 +010024#include <linux/platform_device.h>
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "lh7a40x_udc.h"
27
28//#define DEBUG printk
29//#define DEBUG_EP0 printk
30//#define DEBUG_SETUP printk
31
32#ifndef DEBUG_EP0
33# define DEBUG_EP0(fmt,args...)
34#endif
35#ifndef DEBUG_SETUP
36# define DEBUG_SETUP(fmt,args...)
37#endif
38#ifndef DEBUG
39# define NO_STATES
40# define DEBUG(fmt,args...)
41#endif
42
43#define DRIVER_DESC "LH7A40x USB Device Controller"
44#define DRIVER_VERSION __DATE__
45
46#ifndef _BIT /* FIXME - what happended to _BIT in 2.6.7bk18? */
47#define _BIT(x) (1<<(x))
48#endif
49
50struct lh7a40x_udc *the_controller;
51
52static const char driver_name[] = "lh7a40x_udc";
53static const char driver_desc[] = DRIVER_DESC;
54static const char ep0name[] = "ep0-control";
55
56/*
57 Local definintions.
58*/
59
60#ifndef NO_STATES
61static char *state_names[] = {
62 "WAIT_FOR_SETUP",
63 "DATA_STATE_XMIT",
64 "DATA_STATE_NEED_ZLP",
65 "WAIT_FOR_OUT_STATUS",
66 "DATA_STATE_RECV"
67};
68#endif
69
70/*
71 Local declarations.
72*/
73static int lh7a40x_ep_enable(struct usb_ep *ep,
74 const struct usb_endpoint_descriptor *);
75static int lh7a40x_ep_disable(struct usb_ep *ep);
Al Viro55016f12005-10-21 03:21:58 -040076static struct usb_request *lh7a40x_alloc_request(struct usb_ep *ep, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077static void lh7a40x_free_request(struct usb_ep *ep, struct usb_request *);
78static void *lh7a40x_alloc_buffer(struct usb_ep *ep, unsigned, dma_addr_t *,
Al Viro55016f12005-10-21 03:21:58 -040079 gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080static void lh7a40x_free_buffer(struct usb_ep *ep, void *, dma_addr_t,
81 unsigned);
Al Viro55016f12005-10-21 03:21:58 -040082static int lh7a40x_queue(struct usb_ep *ep, struct usb_request *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083static int lh7a40x_dequeue(struct usb_ep *ep, struct usb_request *);
84static int lh7a40x_set_halt(struct usb_ep *ep, int);
85static int lh7a40x_fifo_status(struct usb_ep *ep);
86static int lh7a40x_fifo_status(struct usb_ep *ep);
87static void lh7a40x_fifo_flush(struct usb_ep *ep);
88static void lh7a40x_ep0_kick(struct lh7a40x_udc *dev, struct lh7a40x_ep *ep);
89static void lh7a40x_handle_ep0(struct lh7a40x_udc *dev, u32 intr);
90
91static void done(struct lh7a40x_ep *ep, struct lh7a40x_request *req,
92 int status);
93static void pio_irq_enable(int bEndpointAddress);
94static void pio_irq_disable(int bEndpointAddress);
95static void stop_activity(struct lh7a40x_udc *dev,
96 struct usb_gadget_driver *driver);
97static void flush(struct lh7a40x_ep *ep);
98static void udc_enable(struct lh7a40x_udc *dev);
99static void udc_set_address(struct lh7a40x_udc *dev, unsigned char address);
100
101static struct usb_ep_ops lh7a40x_ep_ops = {
102 .enable = lh7a40x_ep_enable,
103 .disable = lh7a40x_ep_disable,
104
105 .alloc_request = lh7a40x_alloc_request,
106 .free_request = lh7a40x_free_request,
107
108 .alloc_buffer = lh7a40x_alloc_buffer,
109 .free_buffer = lh7a40x_free_buffer,
110
111 .queue = lh7a40x_queue,
112 .dequeue = lh7a40x_dequeue,
113
114 .set_halt = lh7a40x_set_halt,
115 .fifo_status = lh7a40x_fifo_status,
116 .fifo_flush = lh7a40x_fifo_flush,
117};
118
119/* Inline code */
120
121static __inline__ int write_packet(struct lh7a40x_ep *ep,
122 struct lh7a40x_request *req, int max)
123{
124 u8 *buf;
125 int length, count;
126 volatile u32 *fifo = (volatile u32 *)ep->fifo;
127
128 buf = req->req.buf + req->req.actual;
129 prefetch(buf);
130
131 length = req->req.length - req->req.actual;
132 length = min(length, max);
133 req->req.actual += length;
134
135 DEBUG("Write %d (max %d), fifo %p\n", length, max, fifo);
136
137 count = length;
138 while (count--) {
139 *fifo = *buf++;
140 }
141
142 return length;
143}
144
145static __inline__ void usb_set_index(u32 ep)
146{
147 *(volatile u32 *)io_p2v(USB_INDEX) = ep;
148}
149
150static __inline__ u32 usb_read(u32 port)
151{
152 return *(volatile u32 *)io_p2v(port);
153}
154
155static __inline__ void usb_write(u32 val, u32 port)
156{
157 *(volatile u32 *)io_p2v(port) = val;
158}
159
160static __inline__ void usb_set(u32 val, u32 port)
161{
162 volatile u32 *ioport = (volatile u32 *)io_p2v(port);
163 u32 after = (*ioport) | val;
164 *ioport = after;
165}
166
167static __inline__ void usb_clear(u32 val, u32 port)
168{
169 volatile u32 *ioport = (volatile u32 *)io_p2v(port);
170 u32 after = (*ioport) & ~val;
171 *ioport = after;
172}
173
174/*-------------------------------------------------------------------------*/
175
176#define GPIO_PORTC_DR (0x80000E08)
177#define GPIO_PORTC_DDR (0x80000E18)
178#define GPIO_PORTC_PDR (0x80000E70)
179
180/* get port C pin data register */
181#define get_portc_pdr(bit) ((usb_read(GPIO_PORTC_PDR) & _BIT(bit)) != 0)
182/* get port C data direction register */
183#define get_portc_ddr(bit) ((usb_read(GPIO_PORTC_DDR) & _BIT(bit)) != 0)
184/* set port C data register */
185#define set_portc_dr(bit, val) (val ? usb_set(_BIT(bit), GPIO_PORTC_DR) : usb_clear(_BIT(bit), GPIO_PORTC_DR))
186/* set port C data direction register */
187#define set_portc_ddr(bit, val) (val ? usb_set(_BIT(bit), GPIO_PORTC_DDR) : usb_clear(_BIT(bit), GPIO_PORTC_DDR))
188
189/*
190 * LPD7A404 GPIO's:
191 * Port C bit 1 = USB Port 1 Power Enable
192 * Port C bit 2 = USB Port 1 Data Carrier Detect
193 */
194#define is_usb_connected() get_portc_pdr(2)
195
196#ifdef CONFIG_USB_GADGET_DEBUG_FILES
197
198static const char proc_node_name[] = "driver/udc";
199
200static int
201udc_proc_read(char *page, char **start, off_t off, int count,
202 int *eof, void *_dev)
203{
204 char *buf = page;
205 struct lh7a40x_udc *dev = _dev;
206 char *next = buf;
207 unsigned size = count;
208 unsigned long flags;
209 int t;
210
211 if (off != 0)
212 return 0;
213
214 local_irq_save(flags);
215
216 /* basic device status */
217 t = scnprintf(next, size,
218 DRIVER_DESC "\n"
219 "%s version: %s\n"
220 "Gadget driver: %s\n"
221 "Host: %s\n\n",
222 driver_name, DRIVER_VERSION,
223 dev->driver ? dev->driver->driver.name : "(none)",
224 is_usb_connected()? "full speed" : "disconnected");
225 size -= t;
226 next += t;
227
228 t = scnprintf(next, size,
229 "GPIO:\n"
230 " Port C bit 1: %d, dir %d\n"
231 " Port C bit 2: %d, dir %d\n\n",
232 get_portc_pdr(1), get_portc_ddr(1),
233 get_portc_pdr(2), get_portc_ddr(2)
234 );
235 size -= t;
236 next += t;
237
238 t = scnprintf(next, size,
239 "DCP pullup: %d\n\n",
240 (usb_read(USB_PM) & PM_USB_DCP) != 0);
241 size -= t;
242 next += t;
243
244 local_irq_restore(flags);
245 *eof = 1;
246 return count - size;
247}
248
249#define create_proc_files() create_proc_read_entry(proc_node_name, 0, NULL, udc_proc_read, dev)
250#define remove_proc_files() remove_proc_entry(proc_node_name, NULL)
251
252#else /* !CONFIG_USB_GADGET_DEBUG_FILES */
253
254#define create_proc_files() do {} while (0)
255#define remove_proc_files() do {} while (0)
256
257#endif /* CONFIG_USB_GADGET_DEBUG_FILES */
258
259/*
260 * udc_disable - disable USB device controller
261 */
262static void udc_disable(struct lh7a40x_udc *dev)
263{
264 DEBUG("%s, %p\n", __FUNCTION__, dev);
265
266 udc_set_address(dev, 0);
267
268 /* Disable interrupts */
269 usb_write(0, USB_IN_INT_EN);
270 usb_write(0, USB_OUT_INT_EN);
271 usb_write(0, USB_INT_EN);
272
273 /* Disable the USB */
274 usb_write(0, USB_PM);
275
276#ifdef CONFIG_ARCH_LH7A404
277 /* Disable USB power */
278 set_portc_dr(1, 0);
279#endif
280
281 /* if hardware supports it, disconnect from usb */
282 /* make_usb_disappear(); */
283
284 dev->ep0state = WAIT_FOR_SETUP;
285 dev->gadget.speed = USB_SPEED_UNKNOWN;
286 dev->usb_address = 0;
287}
288
289/*
290 * udc_reinit - initialize software state
291 */
292static void udc_reinit(struct lh7a40x_udc *dev)
293{
294 u32 i;
295
296 DEBUG("%s, %p\n", __FUNCTION__, dev);
297
298 /* device/ep0 records init */
299 INIT_LIST_HEAD(&dev->gadget.ep_list);
300 INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
301 dev->ep0state = WAIT_FOR_SETUP;
302
303 /* basic endpoint records init */
304 for (i = 0; i < UDC_MAX_ENDPOINTS; i++) {
305 struct lh7a40x_ep *ep = &dev->ep[i];
306
307 if (i != 0)
308 list_add_tail(&ep->ep.ep_list, &dev->gadget.ep_list);
309
310 ep->desc = 0;
311 ep->stopped = 0;
312 INIT_LIST_HEAD(&ep->queue);
313 ep->pio_irqs = 0;
314 }
315
316 /* the rest was statically initialized, and is read-only */
317}
318
319#define BYTES2MAXP(x) (x / 8)
320#define MAXP2BYTES(x) (x * 8)
321
322/* until it's enabled, this UDC should be completely invisible
323 * to any USB host.
324 */
325static void udc_enable(struct lh7a40x_udc *dev)
326{
327 int ep;
328
329 DEBUG("%s, %p\n", __FUNCTION__, dev);
330
331 dev->gadget.speed = USB_SPEED_UNKNOWN;
332
333#ifdef CONFIG_ARCH_LH7A404
334 /* Set Port C bit 1 & 2 as output */
335 set_portc_ddr(1, 1);
336 set_portc_ddr(2, 1);
337
338 /* Enable USB power */
339 set_portc_dr(1, 0);
340#endif
341
342 /*
343 * C.f Chapter 18.1.3.1 Initializing the USB
344 */
345
346 /* Disable the USB */
347 usb_clear(PM_USB_ENABLE, USB_PM);
348
349 /* Reset APB & I/O sides of the USB */
350 usb_set(USB_RESET_APB | USB_RESET_IO, USB_RESET);
351 mdelay(5);
352 usb_clear(USB_RESET_APB | USB_RESET_IO, USB_RESET);
353
354 /* Set MAXP values for each */
355 for (ep = 0; ep < UDC_MAX_ENDPOINTS; ep++) {
356 struct lh7a40x_ep *ep_reg = &dev->ep[ep];
357 u32 csr;
358
359 usb_set_index(ep);
360
361 switch (ep_reg->ep_type) {
362 case ep_bulk_in:
363 case ep_interrupt:
364 usb_clear(USB_IN_CSR2_USB_DMA_EN | USB_IN_CSR2_AUTO_SET,
365 ep_reg->csr2);
366 /* Fall through */
367 case ep_control:
368 usb_write(BYTES2MAXP(ep_maxpacket(ep_reg)),
369 USB_IN_MAXP);
370 break;
371 case ep_bulk_out:
372 usb_clear(USB_OUT_CSR2_USB_DMA_EN |
373 USB_OUT_CSR2_AUTO_CLR, ep_reg->csr2);
374 usb_write(BYTES2MAXP(ep_maxpacket(ep_reg)),
375 USB_OUT_MAXP);
376 break;
377 }
378
379 /* Read & Write CSR1, just in case */
380 csr = usb_read(ep_reg->csr1);
381 usb_write(csr, ep_reg->csr1);
382
383 flush(ep_reg);
384 }
385
386 /* Disable interrupts */
387 usb_write(0, USB_IN_INT_EN);
388 usb_write(0, USB_OUT_INT_EN);
389 usb_write(0, USB_INT_EN);
390
391 /* Enable interrupts */
392 usb_set(USB_IN_INT_EP0, USB_IN_INT_EN);
393 usb_set(USB_INT_RESET_INT | USB_INT_RESUME_INT, USB_INT_EN);
394 /* Dont enable rest of the interrupts */
395 /* usb_set(USB_IN_INT_EP3 | USB_IN_INT_EP1 | USB_IN_INT_EP0, USB_IN_INT_EN);
396 usb_set(USB_OUT_INT_EP2, USB_OUT_INT_EN); */
397
398 /* Enable SUSPEND */
399 usb_set(PM_ENABLE_SUSPEND, USB_PM);
400
401 /* Enable the USB */
402 usb_set(PM_USB_ENABLE, USB_PM);
403
404#ifdef CONFIG_ARCH_LH7A404
405 /* NOTE: DOES NOT WORK! */
406 /* Let host detect UDC:
407 * Software must write a 0 to the PMR:DCP_CTRL bit to turn this
408 * transistor on and pull the USBDP pin HIGH.
409 */
410 /* usb_clear(PM_USB_DCP, USB_PM);
411 usb_set(PM_USB_DCP, USB_PM); */
412#endif
413}
414
415/*
416 Register entry point for the peripheral controller driver.
417*/
418int usb_gadget_register_driver(struct usb_gadget_driver *driver)
419{
420 struct lh7a40x_udc *dev = the_controller;
421 int retval;
422
423 DEBUG("%s: %s\n", __FUNCTION__, driver->driver.name);
424
425 if (!driver
426 || driver->speed != USB_SPEED_FULL
427 || !driver->bind
428 || !driver->unbind || !driver->disconnect || !driver->setup)
429 return -EINVAL;
430 if (!dev)
431 return -ENODEV;
432 if (dev->driver)
433 return -EBUSY;
434
435 /* first hook up the driver ... */
436 dev->driver = driver;
437 dev->gadget.dev.driver = &driver->driver;
438
439 device_add(&dev->gadget.dev);
440 retval = driver->bind(&dev->gadget);
441 if (retval) {
442 printk("%s: bind to driver %s --> error %d\n", dev->gadget.name,
443 driver->driver.name, retval);
444 device_del(&dev->gadget.dev);
445
446 dev->driver = 0;
447 dev->gadget.dev.driver = 0;
448 return retval;
449 }
450
451 /* ... then enable host detection and ep0; and we're ready
452 * for set_configuration as well as eventual disconnect.
453 * NOTE: this shouldn't power up until later.
454 */
455 printk("%s: registered gadget driver '%s'\n", dev->gadget.name,
456 driver->driver.name);
457
458 udc_enable(dev);
459
460 return 0;
461}
462
463EXPORT_SYMBOL(usb_gadget_register_driver);
464
465/*
466 Unregister entry point for the peripheral controller driver.
467*/
468int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
469{
470 struct lh7a40x_udc *dev = the_controller;
471 unsigned long flags;
472
473 if (!dev)
474 return -ENODEV;
475 if (!driver || driver != dev->driver)
476 return -EINVAL;
477
478 spin_lock_irqsave(&dev->lock, flags);
479 dev->driver = 0;
480 stop_activity(dev, driver);
481 spin_unlock_irqrestore(&dev->lock, flags);
482
483 driver->unbind(&dev->gadget);
484 device_del(&dev->gadget.dev);
485
486 udc_disable(dev);
487
488 DEBUG("unregistered gadget driver '%s'\n", driver->driver.name);
489 return 0;
490}
491
492EXPORT_SYMBOL(usb_gadget_unregister_driver);
493
494/*-------------------------------------------------------------------------*/
495
496/** Write request to FIFO (max write == maxp size)
497 * Return: 0 = still running, 1 = completed, negative = errno
498 * NOTE: INDEX register must be set for EP
499 */
500static int write_fifo(struct lh7a40x_ep *ep, struct lh7a40x_request *req)
501{
502 u32 max;
503 u32 csr;
504
505 max = le16_to_cpu(ep->desc->wMaxPacketSize);
506
507 csr = usb_read(ep->csr1);
508 DEBUG("CSR: %x %d\n", csr, csr & USB_IN_CSR1_FIFO_NOT_EMPTY);
509
510 if (!(csr & USB_IN_CSR1_FIFO_NOT_EMPTY)) {
511 unsigned count;
512 int is_last, is_short;
513
514 count = write_packet(ep, req, max);
515 usb_set(USB_IN_CSR1_IN_PKT_RDY, ep->csr1);
516
517 /* last packet is usually short (or a zlp) */
518 if (unlikely(count != max))
519 is_last = is_short = 1;
520 else {
521 if (likely(req->req.length != req->req.actual)
522 || req->req.zero)
523 is_last = 0;
524 else
525 is_last = 1;
526 /* interrupt/iso maxpacket may not fill the fifo */
527 is_short = unlikely(max < ep_maxpacket(ep));
528 }
529
530 DEBUG("%s: wrote %s %d bytes%s%s %d left %p\n", __FUNCTION__,
531 ep->ep.name, count,
532 is_last ? "/L" : "", is_short ? "/S" : "",
533 req->req.length - req->req.actual, req);
534
535 /* requests complete when all IN data is in the FIFO */
536 if (is_last) {
537 done(ep, req, 0);
538 if (list_empty(&ep->queue)) {
539 pio_irq_disable(ep_index(ep));
540 }
541 return 1;
542 }
543 } else {
544 DEBUG("Hmm.. %d ep FIFO is not empty!\n", ep_index(ep));
545 }
546
547 return 0;
548}
549
550/** Read to request from FIFO (max read == bytes in fifo)
551 * Return: 0 = still running, 1 = completed, negative = errno
552 * NOTE: INDEX register must be set for EP
553 */
554static int read_fifo(struct lh7a40x_ep *ep, struct lh7a40x_request *req)
555{
556 u32 csr;
557 u8 *buf;
558 unsigned bufferspace, count, is_short;
559 volatile u32 *fifo = (volatile u32 *)ep->fifo;
560
561 /* make sure there's a packet in the FIFO. */
562 csr = usb_read(ep->csr1);
563 if (!(csr & USB_OUT_CSR1_OUT_PKT_RDY)) {
564 DEBUG("%s: Packet NOT ready!\n", __FUNCTION__);
565 return -EINVAL;
566 }
567
568 buf = req->req.buf + req->req.actual;
569 prefetchw(buf);
570 bufferspace = req->req.length - req->req.actual;
571
572 /* read all bytes from this packet */
573 count = usb_read(USB_OUT_FIFO_WC1);
574 req->req.actual += min(count, bufferspace);
575
576 is_short = (count < ep->ep.maxpacket);
577 DEBUG("read %s %02x, %d bytes%s req %p %d/%d\n",
578 ep->ep.name, csr, count,
579 is_short ? "/S" : "", req, req->req.actual, req->req.length);
580
581 while (likely(count-- != 0)) {
582 u8 byte = (u8) (*fifo & 0xff);
583
584 if (unlikely(bufferspace == 0)) {
585 /* this happens when the driver's buffer
586 * is smaller than what the host sent.
587 * discard the extra data.
588 */
589 if (req->req.status != -EOVERFLOW)
590 printk("%s overflow %d\n", ep->ep.name, count);
591 req->req.status = -EOVERFLOW;
592 } else {
593 *buf++ = byte;
594 bufferspace--;
595 }
596 }
597
598 usb_clear(USB_OUT_CSR1_OUT_PKT_RDY, ep->csr1);
599
600 /* completion */
601 if (is_short || req->req.actual == req->req.length) {
602 done(ep, req, 0);
603 usb_set(USB_OUT_CSR1_FIFO_FLUSH, ep->csr1);
604
605 if (list_empty(&ep->queue))
606 pio_irq_disable(ep_index(ep));
607 return 1;
608 }
609
610 /* finished that packet. the next one may be waiting... */
611 return 0;
612}
613
614/*
615 * done - retire a request; caller blocked irqs
616 * INDEX register is preserved to keep same
617 */
618static void done(struct lh7a40x_ep *ep, struct lh7a40x_request *req, int status)
619{
620 unsigned int stopped = ep->stopped;
621 u32 index;
622
623 DEBUG("%s, %p\n", __FUNCTION__, ep);
624 list_del_init(&req->queue);
625
626 if (likely(req->req.status == -EINPROGRESS))
627 req->req.status = status;
628 else
629 status = req->req.status;
630
631 if (status && status != -ESHUTDOWN)
632 DEBUG("complete %s req %p stat %d len %u/%u\n",
633 ep->ep.name, &req->req, status,
634 req->req.actual, req->req.length);
635
636 /* don't modify queue heads during completion callback */
637 ep->stopped = 1;
638 /* Read current index (completion may modify it) */
639 index = usb_read(USB_INDEX);
640
641 spin_unlock(&ep->dev->lock);
642 req->req.complete(&ep->ep, &req->req);
643 spin_lock(&ep->dev->lock);
644
645 /* Restore index */
646 usb_set_index(index);
647 ep->stopped = stopped;
648}
649
650/** Enable EP interrupt */
651static void pio_irq_enable(int ep)
652{
653 DEBUG("%s: %d\n", __FUNCTION__, ep);
654
655 switch (ep) {
656 case 1:
657 usb_set(USB_IN_INT_EP1, USB_IN_INT_EN);
658 break;
659 case 2:
660 usb_set(USB_OUT_INT_EP2, USB_OUT_INT_EN);
661 break;
662 case 3:
663 usb_set(USB_IN_INT_EP3, USB_IN_INT_EN);
664 break;
665 default:
666 DEBUG("Unknown endpoint: %d\n", ep);
667 break;
668 }
669}
670
671/** Disable EP interrupt */
672static void pio_irq_disable(int ep)
673{
674 DEBUG("%s: %d\n", __FUNCTION__, ep);
675
676 switch (ep) {
677 case 1:
678 usb_clear(USB_IN_INT_EP1, USB_IN_INT_EN);
679 break;
680 case 2:
681 usb_clear(USB_OUT_INT_EP2, USB_OUT_INT_EN);
682 break;
683 case 3:
684 usb_clear(USB_IN_INT_EP3, USB_IN_INT_EN);
685 break;
686 default:
687 DEBUG("Unknown endpoint: %d\n", ep);
688 break;
689 }
690}
691
692/*
693 * nuke - dequeue ALL requests
694 */
695void nuke(struct lh7a40x_ep *ep, int status)
696{
697 struct lh7a40x_request *req;
698
699 DEBUG("%s, %p\n", __FUNCTION__, ep);
700
701 /* Flush FIFO */
702 flush(ep);
703
704 /* called with irqs blocked */
705 while (!list_empty(&ep->queue)) {
706 req = list_entry(ep->queue.next, struct lh7a40x_request, queue);
707 done(ep, req, status);
708 }
709
Steven Cole093cf722005-05-03 19:07:24 -0600710 /* Disable IRQ if EP is enabled (has descriptor) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 if (ep->desc)
712 pio_irq_disable(ep_index(ep));
713}
714
715/*
716void nuke_all(struct lh7a40x_udc *dev)
717{
718 int n;
719 for(n=0; n<UDC_MAX_ENDPOINTS; n++) {
720 struct lh7a40x_ep *ep = &dev->ep[n];
721 usb_set_index(n);
722 nuke(ep, 0);
723 }
724}*/
725
726/*
727static void flush_all(struct lh7a40x_udc *dev)
728{
729 int n;
730 for (n = 0; n < UDC_MAX_ENDPOINTS; n++)
731 {
732 struct lh7a40x_ep *ep = &dev->ep[n];
733 flush(ep);
734 }
735}
736*/
737
738/** Flush EP
739 * NOTE: INDEX register must be set before this call
740 */
741static void flush(struct lh7a40x_ep *ep)
742{
743 DEBUG("%s, %p\n", __FUNCTION__, ep);
744
745 switch (ep->ep_type) {
746 case ep_control:
747 /* check, by implication c.f. 15.1.2.11 */
748 break;
749
750 case ep_bulk_in:
751 case ep_interrupt:
752 /* if(csr & USB_IN_CSR1_IN_PKT_RDY) */
753 usb_set(USB_IN_CSR1_FIFO_FLUSH, ep->csr1);
754 break;
755
756 case ep_bulk_out:
757 /* if(csr & USB_OUT_CSR1_OUT_PKT_RDY) */
758 usb_set(USB_OUT_CSR1_FIFO_FLUSH, ep->csr1);
759 break;
760 }
761}
762
763/**
764 * lh7a40x_in_epn - handle IN interrupt
765 */
766static void lh7a40x_in_epn(struct lh7a40x_udc *dev, u32 ep_idx, u32 intr)
767{
768 u32 csr;
769 struct lh7a40x_ep *ep = &dev->ep[ep_idx];
770 struct lh7a40x_request *req;
771
772 usb_set_index(ep_idx);
773
774 csr = usb_read(ep->csr1);
775 DEBUG("%s: %d, csr %x\n", __FUNCTION__, ep_idx, csr);
776
777 if (csr & USB_IN_CSR1_SENT_STALL) {
778 DEBUG("USB_IN_CSR1_SENT_STALL\n");
779 usb_set(USB_IN_CSR1_SENT_STALL /*|USB_IN_CSR1_SEND_STALL */ ,
780 ep->csr1);
781 return;
782 }
783
784 if (!ep->desc) {
785 DEBUG("%s: NO EP DESC\n", __FUNCTION__);
786 return;
787 }
788
789 if (list_empty(&ep->queue))
790 req = 0;
791 else
792 req = list_entry(ep->queue.next, struct lh7a40x_request, queue);
793
794 DEBUG("req: %p\n", req);
795
796 if (!req)
797 return;
798
799 write_fifo(ep, req);
800}
801
802/* ********************************************************************************************* */
803/* Bulk OUT (recv)
804 */
805
806static void lh7a40x_out_epn(struct lh7a40x_udc *dev, u32 ep_idx, u32 intr)
807{
808 struct lh7a40x_ep *ep = &dev->ep[ep_idx];
809 struct lh7a40x_request *req;
810
811 DEBUG("%s: %d\n", __FUNCTION__, ep_idx);
812
813 usb_set_index(ep_idx);
814
815 if (ep->desc) {
816 u32 csr;
817 csr = usb_read(ep->csr1);
818
819 while ((csr =
820 usb_read(ep->
821 csr1)) & (USB_OUT_CSR1_OUT_PKT_RDY |
822 USB_OUT_CSR1_SENT_STALL)) {
823 DEBUG("%s: %x\n", __FUNCTION__, csr);
824
825 if (csr & USB_OUT_CSR1_SENT_STALL) {
826 DEBUG("%s: stall sent, flush fifo\n",
827 __FUNCTION__);
828 /* usb_set(USB_OUT_CSR1_FIFO_FLUSH, ep->csr1); */
829 flush(ep);
830 } else if (csr & USB_OUT_CSR1_OUT_PKT_RDY) {
831 if (list_empty(&ep->queue))
832 req = 0;
833 else
834 req =
835 list_entry(ep->queue.next,
836 struct lh7a40x_request,
837 queue);
838
839 if (!req) {
840 printk("%s: NULL REQ %d\n",
841 __FUNCTION__, ep_idx);
842 flush(ep);
843 break;
844 } else {
845 read_fifo(ep, req);
846 }
847 }
848
849 }
850
851 } else {
852 /* Throw packet away.. */
853 printk("%s: No descriptor?!?\n", __FUNCTION__);
854 flush(ep);
855 }
856}
857
858static void stop_activity(struct lh7a40x_udc *dev,
859 struct usb_gadget_driver *driver)
860{
861 int i;
862
863 /* don't disconnect drivers more than once */
864 if (dev->gadget.speed == USB_SPEED_UNKNOWN)
865 driver = 0;
866 dev->gadget.speed = USB_SPEED_UNKNOWN;
867
868 /* prevent new request submissions, kill any outstanding requests */
869 for (i = 0; i < UDC_MAX_ENDPOINTS; i++) {
870 struct lh7a40x_ep *ep = &dev->ep[i];
871 ep->stopped = 1;
872
873 usb_set_index(i);
874 nuke(ep, -ESHUTDOWN);
875 }
876
877 /* report disconnect; the driver is already quiesced */
878 if (driver) {
879 spin_unlock(&dev->lock);
880 driver->disconnect(&dev->gadget);
881 spin_lock(&dev->lock);
882 }
883
884 /* re-init driver-visible data structures */
885 udc_reinit(dev);
886}
887
888/** Handle USB RESET interrupt
889 */
890static void lh7a40x_reset_intr(struct lh7a40x_udc *dev)
891{
892#if 0 /* def CONFIG_ARCH_LH7A404 */
893 /* Does not work always... */
894
895 DEBUG("%s: %d\n", __FUNCTION__, dev->usb_address);
896
897 if (!dev->usb_address) {
898 /*usb_set(USB_RESET_IO, USB_RESET);
899 mdelay(5);
900 usb_clear(USB_RESET_IO, USB_RESET); */
901 return;
902 }
903 /* Put the USB controller into reset. */
904 usb_set(USB_RESET_IO, USB_RESET);
905
906 /* Set Device ID to 0 */
907 udc_set_address(dev, 0);
908
909 /* Let PLL2 settle down */
910 mdelay(5);
911
912 /* Release the USB controller from reset */
913 usb_clear(USB_RESET_IO, USB_RESET);
914
915 /* Re-enable UDC */
916 udc_enable(dev);
917
918#endif
919 dev->gadget.speed = USB_SPEED_FULL;
920}
921
922/*
923 * lh7a40x usb client interrupt handler.
924 */
925static irqreturn_t lh7a40x_udc_irq(int irq, void *_dev, struct pt_regs *r)
926{
927 struct lh7a40x_udc *dev = _dev;
928
929 DEBUG("\n\n");
930
931 spin_lock(&dev->lock);
932
933 for (;;) {
934 u32 intr_in = usb_read(USB_IN_INT);
935 u32 intr_out = usb_read(USB_OUT_INT);
936 u32 intr_int = usb_read(USB_INT);
937
938 /* Test also against enable bits.. (lh7a40x errata).. Sigh.. */
939 u32 in_en = usb_read(USB_IN_INT_EN);
940 u32 out_en = usb_read(USB_OUT_INT_EN);
941
942 if (!intr_out && !intr_in && !intr_int)
943 break;
944
945 DEBUG("%s (on state %s)\n", __FUNCTION__,
946 state_names[dev->ep0state]);
947 DEBUG("intr_out = %x\n", intr_out);
948 DEBUG("intr_in = %x\n", intr_in);
949 DEBUG("intr_int = %x\n", intr_int);
950
951 if (intr_in) {
952 usb_write(intr_in, USB_IN_INT);
953
954 if ((intr_in & USB_IN_INT_EP1)
955 && (in_en & USB_IN_INT_EP1)) {
956 DEBUG("USB_IN_INT_EP1\n");
957 lh7a40x_in_epn(dev, 1, intr_in);
958 }
959 if ((intr_in & USB_IN_INT_EP3)
960 && (in_en & USB_IN_INT_EP3)) {
961 DEBUG("USB_IN_INT_EP3\n");
962 lh7a40x_in_epn(dev, 3, intr_in);
963 }
964 if (intr_in & USB_IN_INT_EP0) {
965 DEBUG("USB_IN_INT_EP0 (control)\n");
966 lh7a40x_handle_ep0(dev, intr_in);
967 }
968 }
969
970 if (intr_out) {
971 usb_write(intr_out, USB_OUT_INT);
972
973 if ((intr_out & USB_OUT_INT_EP2)
974 && (out_en & USB_OUT_INT_EP2)) {
975 DEBUG("USB_OUT_INT_EP2\n");
976 lh7a40x_out_epn(dev, 2, intr_out);
977 }
978 }
979
980 if (intr_int) {
981 usb_write(intr_int, USB_INT);
982
983 if (intr_int & USB_INT_RESET_INT) {
984 lh7a40x_reset_intr(dev);
985 }
986
987 if (intr_int & USB_INT_RESUME_INT) {
988 DEBUG("USB resume\n");
989
990 if (dev->gadget.speed != USB_SPEED_UNKNOWN
991 && dev->driver
992 && dev->driver->resume
993 && is_usb_connected()) {
994 dev->driver->resume(&dev->gadget);
995 }
996 }
997
998 if (intr_int & USB_INT_SUSPEND_INT) {
999 DEBUG("USB suspend%s\n",
1000 is_usb_connected()? "" : "+disconnect");
1001 if (!is_usb_connected()) {
1002 stop_activity(dev, dev->driver);
1003 } else if (dev->gadget.speed !=
1004 USB_SPEED_UNKNOWN && dev->driver
1005 && dev->driver->suspend) {
1006 dev->driver->suspend(&dev->gadget);
1007 }
1008 }
1009
1010 }
1011 }
1012
1013 spin_unlock(&dev->lock);
1014
1015 return IRQ_HANDLED;
1016}
1017
1018static int lh7a40x_ep_enable(struct usb_ep *_ep,
1019 const struct usb_endpoint_descriptor *desc)
1020{
1021 struct lh7a40x_ep *ep;
1022 struct lh7a40x_udc *dev;
1023 unsigned long flags;
1024
1025 DEBUG("%s, %p\n", __FUNCTION__, _ep);
1026
1027 ep = container_of(_ep, struct lh7a40x_ep, ep);
1028 if (!_ep || !desc || ep->desc || _ep->name == ep0name
1029 || desc->bDescriptorType != USB_DT_ENDPOINT
1030 || ep->bEndpointAddress != desc->bEndpointAddress
1031 || ep_maxpacket(ep) < le16_to_cpu(desc->wMaxPacketSize)) {
1032 DEBUG("%s, bad ep or descriptor\n", __FUNCTION__);
1033 return -EINVAL;
1034 }
1035
1036 /* xfer types must match, except that interrupt ~= bulk */
1037 if (ep->bmAttributes != desc->bmAttributes
1038 && ep->bmAttributes != USB_ENDPOINT_XFER_BULK
1039 && desc->bmAttributes != USB_ENDPOINT_XFER_INT) {
1040 DEBUG("%s, %s type mismatch\n", __FUNCTION__, _ep->name);
1041 return -EINVAL;
1042 }
1043
1044 /* hardware _could_ do smaller, but driver doesn't */
1045 if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK
1046 && le16_to_cpu(desc->wMaxPacketSize) != ep_maxpacket(ep))
1047 || !desc->wMaxPacketSize) {
1048 DEBUG("%s, bad %s maxpacket\n", __FUNCTION__, _ep->name);
1049 return -ERANGE;
1050 }
1051
1052 dev = ep->dev;
1053 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
1054 DEBUG("%s, bogus device state\n", __FUNCTION__);
1055 return -ESHUTDOWN;
1056 }
1057
1058 spin_lock_irqsave(&ep->dev->lock, flags);
1059
1060 ep->stopped = 0;
1061 ep->desc = desc;
1062 ep->pio_irqs = 0;
1063 ep->ep.maxpacket = le16_to_cpu(desc->wMaxPacketSize);
1064
Franck Bui-Huud5ec3342006-02-23 09:35:06 +01001065 spin_unlock_irqrestore(&ep->dev->lock, flags);
1066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 /* Reset halt state (does flush) */
1068 lh7a40x_set_halt(_ep, 0);
1069
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 DEBUG("%s: enabled %s\n", __FUNCTION__, _ep->name);
1071 return 0;
1072}
1073
1074/** Disable EP
1075 * NOTE: Sets INDEX register
1076 */
1077static int lh7a40x_ep_disable(struct usb_ep *_ep)
1078{
1079 struct lh7a40x_ep *ep;
1080 unsigned long flags;
1081
1082 DEBUG("%s, %p\n", __FUNCTION__, _ep);
1083
1084 ep = container_of(_ep, struct lh7a40x_ep, ep);
1085 if (!_ep || !ep->desc) {
1086 DEBUG("%s, %s not enabled\n", __FUNCTION__,
1087 _ep ? ep->ep.name : NULL);
1088 return -EINVAL;
1089 }
1090
1091 spin_lock_irqsave(&ep->dev->lock, flags);
1092
1093 usb_set_index(ep_index(ep));
1094
1095 /* Nuke all pending requests (does flush) */
1096 nuke(ep, -ESHUTDOWN);
1097
1098 /* Disable ep IRQ */
1099 pio_irq_disable(ep_index(ep));
1100
1101 ep->desc = 0;
1102 ep->stopped = 1;
1103
1104 spin_unlock_irqrestore(&ep->dev->lock, flags);
1105
1106 DEBUG("%s: disabled %s\n", __FUNCTION__, _ep->name);
1107 return 0;
1108}
1109
1110static struct usb_request *lh7a40x_alloc_request(struct usb_ep *ep,
Al Viro55016f12005-10-21 03:21:58 -04001111 gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112{
1113 struct lh7a40x_request *req;
1114
1115 DEBUG("%s, %p\n", __FUNCTION__, ep);
1116
1117 req = kmalloc(sizeof *req, gfp_flags);
1118 if (!req)
1119 return 0;
1120
1121 memset(req, 0, sizeof *req);
1122 INIT_LIST_HEAD(&req->queue);
1123
1124 return &req->req;
1125}
1126
1127static void lh7a40x_free_request(struct usb_ep *ep, struct usb_request *_req)
1128{
1129 struct lh7a40x_request *req;
1130
1131 DEBUG("%s, %p\n", __FUNCTION__, ep);
1132
1133 req = container_of(_req, struct lh7a40x_request, req);
1134 WARN_ON(!list_empty(&req->queue));
1135 kfree(req);
1136}
1137
1138static void *lh7a40x_alloc_buffer(struct usb_ep *ep, unsigned bytes,
Al Viro55016f12005-10-21 03:21:58 -04001139 dma_addr_t * dma, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140{
1141 char *retval;
1142
1143 DEBUG("%s (%p, %d, %d)\n", __FUNCTION__, ep, bytes, gfp_flags);
1144
1145 retval = kmalloc(bytes, gfp_flags & ~(__GFP_DMA | __GFP_HIGHMEM));
1146 if (retval)
1147 *dma = virt_to_bus(retval);
1148 return retval;
1149}
1150
1151static void lh7a40x_free_buffer(struct usb_ep *ep, void *buf, dma_addr_t dma,
1152 unsigned bytes)
1153{
1154 DEBUG("%s, %p\n", __FUNCTION__, ep);
1155 kfree(buf);
1156}
1157
1158/** Queue one request
1159 * Kickstart transfer if needed
1160 * NOTE: Sets INDEX register
1161 */
1162static int lh7a40x_queue(struct usb_ep *_ep, struct usb_request *_req,
Al Viro55016f12005-10-21 03:21:58 -04001163 gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164{
1165 struct lh7a40x_request *req;
1166 struct lh7a40x_ep *ep;
1167 struct lh7a40x_udc *dev;
1168 unsigned long flags;
1169
1170 DEBUG("\n\n\n%s, %p\n", __FUNCTION__, _ep);
1171
1172 req = container_of(_req, struct lh7a40x_request, req);
1173 if (unlikely
1174 (!_req || !_req->complete || !_req->buf
1175 || !list_empty(&req->queue))) {
1176 DEBUG("%s, bad params\n", __FUNCTION__);
1177 return -EINVAL;
1178 }
1179
1180 ep = container_of(_ep, struct lh7a40x_ep, ep);
1181 if (unlikely(!_ep || (!ep->desc && ep->ep.name != ep0name))) {
1182 DEBUG("%s, bad ep\n", __FUNCTION__);
1183 return -EINVAL;
1184 }
1185
1186 dev = ep->dev;
1187 if (unlikely(!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)) {
1188 DEBUG("%s, bogus device state %p\n", __FUNCTION__, dev->driver);
1189 return -ESHUTDOWN;
1190 }
1191
1192 DEBUG("%s queue req %p, len %d buf %p\n", _ep->name, _req, _req->length,
1193 _req->buf);
1194
1195 spin_lock_irqsave(&dev->lock, flags);
1196
1197 _req->status = -EINPROGRESS;
1198 _req->actual = 0;
1199
1200 /* kickstart this i/o queue? */
1201 DEBUG("Add to %d Q %d %d\n", ep_index(ep), list_empty(&ep->queue),
1202 ep->stopped);
1203 if (list_empty(&ep->queue) && likely(!ep->stopped)) {
1204 u32 csr;
1205
1206 if (unlikely(ep_index(ep) == 0)) {
1207 /* EP0 */
1208 list_add_tail(&req->queue, &ep->queue);
1209 lh7a40x_ep0_kick(dev, ep);
1210 req = 0;
1211 } else if (ep_is_in(ep)) {
1212 /* EP1 & EP3 */
1213 usb_set_index(ep_index(ep));
1214 csr = usb_read(ep->csr1);
1215 pio_irq_enable(ep_index(ep));
1216 if ((csr & USB_IN_CSR1_FIFO_NOT_EMPTY) == 0) {
1217 if (write_fifo(ep, req) == 1)
1218 req = 0;
1219 }
1220 } else {
1221 /* EP2 */
1222 usb_set_index(ep_index(ep));
1223 csr = usb_read(ep->csr1);
1224 pio_irq_enable(ep_index(ep));
1225 if (!(csr & USB_OUT_CSR1_FIFO_FULL)) {
1226 if (read_fifo(ep, req) == 1)
1227 req = 0;
1228 }
1229 }
1230 }
1231
1232 /* pio or dma irq handler advances the queue. */
1233 if (likely(req != 0))
1234 list_add_tail(&req->queue, &ep->queue);
1235
1236 spin_unlock_irqrestore(&dev->lock, flags);
1237
1238 return 0;
1239}
1240
1241/* dequeue JUST ONE request */
1242static int lh7a40x_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1243{
1244 struct lh7a40x_ep *ep;
1245 struct lh7a40x_request *req;
1246 unsigned long flags;
1247
1248 DEBUG("%s, %p\n", __FUNCTION__, _ep);
1249
1250 ep = container_of(_ep, struct lh7a40x_ep, ep);
1251 if (!_ep || ep->ep.name == ep0name)
1252 return -EINVAL;
1253
1254 spin_lock_irqsave(&ep->dev->lock, flags);
1255
1256 /* make sure it's actually queued on this endpoint */
1257 list_for_each_entry(req, &ep->queue, queue) {
1258 if (&req->req == _req)
1259 break;
1260 }
1261 if (&req->req != _req) {
1262 spin_unlock_irqrestore(&ep->dev->lock, flags);
1263 return -EINVAL;
1264 }
1265
1266 done(ep, req, -ECONNRESET);
1267
1268 spin_unlock_irqrestore(&ep->dev->lock, flags);
1269 return 0;
1270}
1271
1272/** Halt specific EP
1273 * Return 0 if success
1274 * NOTE: Sets INDEX register to EP !
1275 */
1276static int lh7a40x_set_halt(struct usb_ep *_ep, int value)
1277{
1278 struct lh7a40x_ep *ep;
1279 unsigned long flags;
1280
1281 ep = container_of(_ep, struct lh7a40x_ep, ep);
1282 if (unlikely(!_ep || (!ep->desc && ep->ep.name != ep0name))) {
1283 DEBUG("%s, bad ep\n", __FUNCTION__);
1284 return -EINVAL;
1285 }
1286
1287 usb_set_index(ep_index(ep));
1288
1289 DEBUG("%s, ep %d, val %d\n", __FUNCTION__, ep_index(ep), value);
1290
1291 spin_lock_irqsave(&ep->dev->lock, flags);
1292
1293 if (ep_index(ep) == 0) {
1294 /* EP0 */
1295 usb_set(EP0_SEND_STALL, ep->csr1);
1296 } else if (ep_is_in(ep)) {
1297 u32 csr = usb_read(ep->csr1);
1298 if (value && ((csr & USB_IN_CSR1_FIFO_NOT_EMPTY)
1299 || !list_empty(&ep->queue))) {
1300 /*
1301 * Attempts to halt IN endpoints will fail (returning -EAGAIN)
1302 * if any transfer requests are still queued, or if the controller
1303 * FIFO still holds bytes that the host hasnÂ’t collected.
1304 */
1305 spin_unlock_irqrestore(&ep->dev->lock, flags);
1306 DEBUG
1307 ("Attempt to halt IN endpoint failed (returning -EAGAIN) %d %d\n",
1308 (csr & USB_IN_CSR1_FIFO_NOT_EMPTY),
1309 !list_empty(&ep->queue));
1310 return -EAGAIN;
1311 }
1312 flush(ep);
1313 if (value)
1314 usb_set(USB_IN_CSR1_SEND_STALL, ep->csr1);
1315 else {
1316 usb_clear(USB_IN_CSR1_SEND_STALL, ep->csr1);
1317 usb_set(USB_IN_CSR1_CLR_DATA_TOGGLE, ep->csr1);
1318 }
1319
1320 } else {
1321
1322 flush(ep);
1323 if (value)
1324 usb_set(USB_OUT_CSR1_SEND_STALL, ep->csr1);
1325 else {
1326 usb_clear(USB_OUT_CSR1_SEND_STALL, ep->csr1);
1327 usb_set(USB_OUT_CSR1_CLR_DATA_REG, ep->csr1);
1328 }
1329 }
1330
1331 if (value) {
1332 ep->stopped = 1;
1333 } else {
1334 ep->stopped = 0;
1335 }
1336
1337 spin_unlock_irqrestore(&ep->dev->lock, flags);
1338
1339 DEBUG("%s %s halted\n", _ep->name, value == 0 ? "NOT" : "IS");
1340
1341 return 0;
1342}
1343
1344/** Return bytes in EP FIFO
1345 * NOTE: Sets INDEX register to EP
1346 */
1347static int lh7a40x_fifo_status(struct usb_ep *_ep)
1348{
1349 u32 csr;
1350 int count = 0;
1351 struct lh7a40x_ep *ep;
1352
1353 ep = container_of(_ep, struct lh7a40x_ep, ep);
1354 if (!_ep) {
1355 DEBUG("%s, bad ep\n", __FUNCTION__);
1356 return -ENODEV;
1357 }
1358
1359 DEBUG("%s, %d\n", __FUNCTION__, ep_index(ep));
1360
1361 /* LPD can't report unclaimed bytes from IN fifos */
1362 if (ep_is_in(ep))
1363 return -EOPNOTSUPP;
1364
1365 usb_set_index(ep_index(ep));
1366
1367 csr = usb_read(ep->csr1);
1368 if (ep->dev->gadget.speed != USB_SPEED_UNKNOWN ||
1369 csr & USB_OUT_CSR1_OUT_PKT_RDY) {
1370 count = usb_read(USB_OUT_FIFO_WC1);
1371 }
1372
1373 return count;
1374}
1375
1376/** Flush EP FIFO
1377 * NOTE: Sets INDEX register to EP
1378 */
1379static void lh7a40x_fifo_flush(struct usb_ep *_ep)
1380{
1381 struct lh7a40x_ep *ep;
1382
1383 ep = container_of(_ep, struct lh7a40x_ep, ep);
1384 if (unlikely(!_ep || (!ep->desc && ep->ep.name != ep0name))) {
1385 DEBUG("%s, bad ep\n", __FUNCTION__);
1386 return;
1387 }
1388
1389 usb_set_index(ep_index(ep));
1390 flush(ep);
1391}
1392
1393/****************************************************************/
1394/* End Point 0 related functions */
1395/****************************************************************/
1396
1397/* return: 0 = still running, 1 = completed, negative = errno */
1398static int write_fifo_ep0(struct lh7a40x_ep *ep, struct lh7a40x_request *req)
1399{
1400 u32 max;
1401 unsigned count;
1402 int is_last;
1403
1404 max = ep_maxpacket(ep);
1405
1406 DEBUG_EP0("%s\n", __FUNCTION__);
1407
1408 count = write_packet(ep, req, max);
1409
1410 /* last packet is usually short (or a zlp) */
1411 if (unlikely(count != max))
1412 is_last = 1;
1413 else {
1414 if (likely(req->req.length != req->req.actual) || req->req.zero)
1415 is_last = 0;
1416 else
1417 is_last = 1;
1418 }
1419
1420 DEBUG_EP0("%s: wrote %s %d bytes%s %d left %p\n", __FUNCTION__,
1421 ep->ep.name, count,
1422 is_last ? "/L" : "", req->req.length - req->req.actual, req);
1423
1424 /* requests complete when all IN data is in the FIFO */
1425 if (is_last) {
1426 done(ep, req, 0);
1427 return 1;
1428 }
1429
1430 return 0;
1431}
1432
1433static __inline__ int lh7a40x_fifo_read(struct lh7a40x_ep *ep,
1434 unsigned char *cp, int max)
1435{
1436 int bytes;
1437 int count = usb_read(USB_OUT_FIFO_WC1);
1438 volatile u32 *fifo = (volatile u32 *)ep->fifo;
1439
1440 if (count > max)
1441 count = max;
1442 bytes = count;
1443 while (count--)
1444 *cp++ = *fifo & 0xFF;
1445 return bytes;
1446}
1447
1448static __inline__ void lh7a40x_fifo_write(struct lh7a40x_ep *ep,
1449 unsigned char *cp, int count)
1450{
1451 volatile u32 *fifo = (volatile u32 *)ep->fifo;
1452 DEBUG_EP0("fifo_write: %d %d\n", ep_index(ep), count);
1453 while (count--)
1454 *fifo = *cp++;
1455}
1456
1457static int read_fifo_ep0(struct lh7a40x_ep *ep, struct lh7a40x_request *req)
1458{
1459 u32 csr;
1460 u8 *buf;
1461 unsigned bufferspace, count, is_short;
1462 volatile u32 *fifo = (volatile u32 *)ep->fifo;
1463
1464 DEBUG_EP0("%s\n", __FUNCTION__);
1465
1466 csr = usb_read(USB_EP0_CSR);
1467 if (!(csr & USB_OUT_CSR1_OUT_PKT_RDY))
1468 return 0;
1469
1470 buf = req->req.buf + req->req.actual;
1471 prefetchw(buf);
1472 bufferspace = req->req.length - req->req.actual;
1473
1474 /* read all bytes from this packet */
1475 if (likely(csr & EP0_OUT_PKT_RDY)) {
1476 count = usb_read(USB_OUT_FIFO_WC1);
1477 req->req.actual += min(count, bufferspace);
1478 } else /* zlp */
1479 count = 0;
1480
1481 is_short = (count < ep->ep.maxpacket);
1482 DEBUG_EP0("read %s %02x, %d bytes%s req %p %d/%d\n",
1483 ep->ep.name, csr, count,
1484 is_short ? "/S" : "", req, req->req.actual, req->req.length);
1485
1486 while (likely(count-- != 0)) {
1487 u8 byte = (u8) (*fifo & 0xff);
1488
1489 if (unlikely(bufferspace == 0)) {
1490 /* this happens when the driver's buffer
1491 * is smaller than what the host sent.
1492 * discard the extra data.
1493 */
1494 if (req->req.status != -EOVERFLOW)
1495 DEBUG_EP0("%s overflow %d\n", ep->ep.name,
1496 count);
1497 req->req.status = -EOVERFLOW;
1498 } else {
1499 *buf++ = byte;
1500 bufferspace--;
1501 }
1502 }
1503
1504 /* completion */
1505 if (is_short || req->req.actual == req->req.length) {
1506 done(ep, req, 0);
1507 return 1;
1508 }
1509
1510 /* finished that packet. the next one may be waiting... */
1511 return 0;
1512}
1513
1514/**
1515 * udc_set_address - set the USB address for this device
1516 * @address:
1517 *
1518 * Called from control endpoint function after it decodes a set address setup packet.
1519 */
1520static void udc_set_address(struct lh7a40x_udc *dev, unsigned char address)
1521{
1522 DEBUG_EP0("%s: %d\n", __FUNCTION__, address);
1523 /* c.f. 15.1.2.2 Table 15-4 address will be used after DATA_END is set */
1524 dev->usb_address = address;
1525 usb_set((address & USB_FA_FUNCTION_ADDR), USB_FA);
1526 usb_set(USB_FA_ADDR_UPDATE | (address & USB_FA_FUNCTION_ADDR), USB_FA);
1527 /* usb_read(USB_FA); */
1528}
1529
1530/*
1531 * DATA_STATE_RECV (OUT_PKT_RDY)
1532 * - if error
1533 * set EP0_CLR_OUT | EP0_DATA_END | EP0_SEND_STALL bits
1534 * - else
1535 * set EP0_CLR_OUT bit
1536 if last set EP0_DATA_END bit
1537 */
1538static void lh7a40x_ep0_out(struct lh7a40x_udc *dev, u32 csr)
1539{
1540 struct lh7a40x_request *req;
1541 struct lh7a40x_ep *ep = &dev->ep[0];
1542 int ret;
1543
1544 DEBUG_EP0("%s: %x\n", __FUNCTION__, csr);
1545
1546 if (list_empty(&ep->queue))
1547 req = 0;
1548 else
1549 req = list_entry(ep->queue.next, struct lh7a40x_request, queue);
1550
1551 if (req) {
1552
1553 if (req->req.length == 0) {
1554 DEBUG_EP0("ZERO LENGTH OUT!\n");
1555 usb_set((EP0_CLR_OUT | EP0_DATA_END), USB_EP0_CSR);
1556 dev->ep0state = WAIT_FOR_SETUP;
1557 return;
1558 }
1559 ret = read_fifo_ep0(ep, req);
1560 if (ret) {
1561 /* Done! */
1562 DEBUG_EP0("%s: finished, waiting for status\n",
1563 __FUNCTION__);
1564
1565 usb_set((EP0_CLR_OUT | EP0_DATA_END), USB_EP0_CSR);
1566 dev->ep0state = WAIT_FOR_SETUP;
1567 } else {
1568 /* Not done yet.. */
1569 DEBUG_EP0("%s: not finished\n", __FUNCTION__);
1570 usb_set(EP0_CLR_OUT, USB_EP0_CSR);
1571 }
1572 } else {
1573 DEBUG_EP0("NO REQ??!\n");
1574 }
1575}
1576
1577/*
1578 * DATA_STATE_XMIT
1579 */
1580static int lh7a40x_ep0_in(struct lh7a40x_udc *dev, u32 csr)
1581{
1582 struct lh7a40x_request *req;
1583 struct lh7a40x_ep *ep = &dev->ep[0];
1584 int ret, need_zlp = 0;
1585
1586 DEBUG_EP0("%s: %x\n", __FUNCTION__, csr);
1587
1588 if (list_empty(&ep->queue))
1589 req = 0;
1590 else
1591 req = list_entry(ep->queue.next, struct lh7a40x_request, queue);
1592
1593 if (!req) {
1594 DEBUG_EP0("%s: NULL REQ\n", __FUNCTION__);
1595 return 0;
1596 }
1597
1598 if (req->req.length == 0) {
1599
1600 usb_set((EP0_IN_PKT_RDY | EP0_DATA_END), USB_EP0_CSR);
1601 dev->ep0state = WAIT_FOR_SETUP;
1602 return 1;
1603 }
1604
1605 if (req->req.length - req->req.actual == EP0_PACKETSIZE) {
1606 /* Next write will end with the packet size, */
1607 /* so we need Zero-length-packet */
1608 need_zlp = 1;
1609 }
1610
1611 ret = write_fifo_ep0(ep, req);
1612
1613 if (ret == 1 && !need_zlp) {
1614 /* Last packet */
1615 DEBUG_EP0("%s: finished, waiting for status\n", __FUNCTION__);
1616
1617 usb_set((EP0_IN_PKT_RDY | EP0_DATA_END), USB_EP0_CSR);
1618 dev->ep0state = WAIT_FOR_SETUP;
1619 } else {
1620 DEBUG_EP0("%s: not finished\n", __FUNCTION__);
1621 usb_set(EP0_IN_PKT_RDY, USB_EP0_CSR);
1622 }
1623
1624 if (need_zlp) {
1625 DEBUG_EP0("%s: Need ZLP!\n", __FUNCTION__);
1626 usb_set(EP0_IN_PKT_RDY, USB_EP0_CSR);
1627 dev->ep0state = DATA_STATE_NEED_ZLP;
1628 }
1629
1630 return 1;
1631}
1632
1633static int lh7a40x_handle_get_status(struct lh7a40x_udc *dev,
1634 struct usb_ctrlrequest *ctrl)
1635{
1636 struct lh7a40x_ep *ep0 = &dev->ep[0];
1637 struct lh7a40x_ep *qep;
1638 int reqtype = (ctrl->bRequestType & USB_RECIP_MASK);
1639 u16 val = 0;
1640
1641 if (reqtype == USB_RECIP_INTERFACE) {
1642 /* This is not supported.
1643 * And according to the USB spec, this one does nothing..
1644 * Just return 0
1645 */
1646 DEBUG_SETUP("GET_STATUS: USB_RECIP_INTERFACE\n");
1647 } else if (reqtype == USB_RECIP_DEVICE) {
1648 DEBUG_SETUP("GET_STATUS: USB_RECIP_DEVICE\n");
1649 val |= (1 << 0); /* Self powered */
1650 /*val |= (1<<1); *//* Remote wakeup */
1651 } else if (reqtype == USB_RECIP_ENDPOINT) {
1652 int ep_num = (ctrl->wIndex & ~USB_DIR_IN);
1653
1654 DEBUG_SETUP
1655 ("GET_STATUS: USB_RECIP_ENDPOINT (%d), ctrl->wLength = %d\n",
1656 ep_num, ctrl->wLength);
1657
1658 if (ctrl->wLength > 2 || ep_num > 3)
1659 return -EOPNOTSUPP;
1660
1661 qep = &dev->ep[ep_num];
1662 if (ep_is_in(qep) != ((ctrl->wIndex & USB_DIR_IN) ? 1 : 0)
1663 && ep_index(qep) != 0) {
1664 return -EOPNOTSUPP;
1665 }
1666
1667 usb_set_index(ep_index(qep));
1668
1669 /* Return status on next IN token */
1670 switch (qep->ep_type) {
1671 case ep_control:
1672 val =
1673 (usb_read(qep->csr1) & EP0_SEND_STALL) ==
1674 EP0_SEND_STALL;
1675 break;
1676 case ep_bulk_in:
1677 case ep_interrupt:
1678 val =
1679 (usb_read(qep->csr1) & USB_IN_CSR1_SEND_STALL) ==
1680 USB_IN_CSR1_SEND_STALL;
1681 break;
1682 case ep_bulk_out:
1683 val =
1684 (usb_read(qep->csr1) & USB_OUT_CSR1_SEND_STALL) ==
1685 USB_OUT_CSR1_SEND_STALL;
1686 break;
1687 }
1688
1689 /* Back to EP0 index */
1690 usb_set_index(0);
1691
1692 DEBUG_SETUP("GET_STATUS, ep: %d (%x), val = %d\n", ep_num,
1693 ctrl->wIndex, val);
1694 } else {
1695 DEBUG_SETUP("Unknown REQ TYPE: %d\n", reqtype);
1696 return -EOPNOTSUPP;
1697 }
1698
1699 /* Clear "out packet ready" */
1700 usb_set((EP0_CLR_OUT), USB_EP0_CSR);
1701 /* Put status to FIFO */
1702 lh7a40x_fifo_write(ep0, (u8 *) & val, sizeof(val));
1703 /* Issue "In packet ready" */
1704 usb_set((EP0_IN_PKT_RDY | EP0_DATA_END), USB_EP0_CSR);
1705
1706 return 0;
1707}
1708
1709/*
1710 * WAIT_FOR_SETUP (OUT_PKT_RDY)
1711 * - read data packet from EP0 FIFO
1712 * - decode command
1713 * - if error
1714 * set EP0_CLR_OUT | EP0_DATA_END | EP0_SEND_STALL bits
1715 * - else
1716 * set EP0_CLR_OUT | EP0_DATA_END bits
1717 */
1718static void lh7a40x_ep0_setup(struct lh7a40x_udc *dev, u32 csr)
1719{
1720 struct lh7a40x_ep *ep = &dev->ep[0];
1721 struct usb_ctrlrequest ctrl;
1722 int i, bytes, is_in;
1723
1724 DEBUG_SETUP("%s: %x\n", __FUNCTION__, csr);
1725
1726 /* Nuke all previous transfers */
1727 nuke(ep, -EPROTO);
1728
1729 /* read control req from fifo (8 bytes) */
1730 bytes = lh7a40x_fifo_read(ep, (unsigned char *)&ctrl, 8);
1731
1732 DEBUG_SETUP("Read CTRL REQ %d bytes\n", bytes);
1733 DEBUG_SETUP("CTRL.bRequestType = %d (is_in %d)\n", ctrl.bRequestType,
1734 ctrl.bRequestType == USB_DIR_IN);
1735 DEBUG_SETUP("CTRL.bRequest = %d\n", ctrl.bRequest);
1736 DEBUG_SETUP("CTRL.wLength = %d\n", ctrl.wLength);
1737 DEBUG_SETUP("CTRL.wValue = %d (%d)\n", ctrl.wValue, ctrl.wValue >> 8);
1738 DEBUG_SETUP("CTRL.wIndex = %d\n", ctrl.wIndex);
1739
1740 /* Set direction of EP0 */
1741 if (likely(ctrl.bRequestType & USB_DIR_IN)) {
1742 ep->bEndpointAddress |= USB_DIR_IN;
1743 is_in = 1;
1744 } else {
1745 ep->bEndpointAddress &= ~USB_DIR_IN;
1746 is_in = 0;
1747 }
1748
1749 dev->req_pending = 1;
1750
1751 /* Handle some SETUP packets ourselves */
1752 switch (ctrl.bRequest) {
1753 case USB_REQ_SET_ADDRESS:
1754 if (ctrl.bRequestType != (USB_TYPE_STANDARD | USB_RECIP_DEVICE))
1755 break;
1756
1757 DEBUG_SETUP("USB_REQ_SET_ADDRESS (%d)\n", ctrl.wValue);
1758 udc_set_address(dev, ctrl.wValue);
1759 usb_set((EP0_CLR_OUT | EP0_DATA_END), USB_EP0_CSR);
1760 return;
1761
1762 case USB_REQ_GET_STATUS:{
1763 if (lh7a40x_handle_get_status(dev, &ctrl) == 0)
1764 return;
1765
1766 case USB_REQ_CLEAR_FEATURE:
1767 case USB_REQ_SET_FEATURE:
1768 if (ctrl.bRequestType == USB_RECIP_ENDPOINT) {
1769 struct lh7a40x_ep *qep;
1770 int ep_num = (ctrl.wIndex & 0x0f);
1771
1772 /* Support only HALT feature */
1773 if (ctrl.wValue != 0 || ctrl.wLength != 0
1774 || ep_num > 3 || ep_num < 1)
1775 break;
1776
1777 qep = &dev->ep[ep_num];
Franck Bui-Huud5ec3342006-02-23 09:35:06 +01001778 spin_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 if (ctrl.bRequest == USB_REQ_SET_FEATURE) {
1780 DEBUG_SETUP("SET_FEATURE (%d)\n",
1781 ep_num);
1782 lh7a40x_set_halt(&qep->ep, 1);
1783 } else {
1784 DEBUG_SETUP("CLR_FEATURE (%d)\n",
1785 ep_num);
1786 lh7a40x_set_halt(&qep->ep, 0);
1787 }
Franck Bui-Huud5ec3342006-02-23 09:35:06 +01001788 spin_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 usb_set_index(0);
1790
1791 /* Reply with a ZLP on next IN token */
1792 usb_set((EP0_CLR_OUT | EP0_DATA_END),
1793 USB_EP0_CSR);
1794 return;
1795 }
1796 break;
1797 }
1798
1799 default:
1800 break;
1801 }
1802
1803 if (likely(dev->driver)) {
1804 /* device-2-host (IN) or no data setup command, process immediately */
1805 spin_unlock(&dev->lock);
1806 i = dev->driver->setup(&dev->gadget, &ctrl);
1807 spin_lock(&dev->lock);
1808
1809 if (i < 0) {
1810 /* setup processing failed, force stall */
1811 DEBUG_SETUP
1812 (" --> ERROR: gadget setup FAILED (stalling), setup returned %d\n",
1813 i);
1814 usb_set_index(0);
1815 usb_set((EP0_CLR_OUT | EP0_DATA_END | EP0_SEND_STALL),
1816 USB_EP0_CSR);
1817
1818 /* ep->stopped = 1; */
1819 dev->ep0state = WAIT_FOR_SETUP;
1820 }
1821 }
1822}
1823
1824/*
1825 * DATA_STATE_NEED_ZLP
1826 */
1827static void lh7a40x_ep0_in_zlp(struct lh7a40x_udc *dev, u32 csr)
1828{
1829 DEBUG_EP0("%s: %x\n", __FUNCTION__, csr);
1830
1831 /* c.f. Table 15-14 */
1832 usb_set((EP0_IN_PKT_RDY | EP0_DATA_END), USB_EP0_CSR);
1833 dev->ep0state = WAIT_FOR_SETUP;
1834}
1835
1836/*
1837 * handle ep0 interrupt
1838 */
1839static void lh7a40x_handle_ep0(struct lh7a40x_udc *dev, u32 intr)
1840{
1841 struct lh7a40x_ep *ep = &dev->ep[0];
1842 u32 csr;
1843
1844 /* Set index 0 */
1845 usb_set_index(0);
1846 csr = usb_read(USB_EP0_CSR);
1847
1848 DEBUG_EP0("%s: csr = %x\n", __FUNCTION__, csr);
1849
1850 /*
1851 * For overview of what we should be doing see c.f. Chapter 18.1.2.4
1852 * We will follow that outline here modified by our own global state
1853 * indication which provides hints as to what we think should be
1854 * happening..
1855 */
1856
1857 /*
1858 * if SENT_STALL is set
1859 * - clear the SENT_STALL bit
1860 */
1861 if (csr & EP0_SENT_STALL) {
1862 DEBUG_EP0("%s: EP0_SENT_STALL is set: %x\n", __FUNCTION__, csr);
1863 usb_clear((EP0_SENT_STALL | EP0_SEND_STALL), USB_EP0_CSR);
1864 nuke(ep, -ECONNABORTED);
1865 dev->ep0state = WAIT_FOR_SETUP;
1866 return;
1867 }
1868
1869 /*
1870 * if a transfer is in progress && IN_PKT_RDY and OUT_PKT_RDY are clear
1871 * - fill EP0 FIFO
1872 * - if last packet
1873 * - set IN_PKT_RDY | DATA_END
1874 * - else
1875 * set IN_PKT_RDY
1876 */
1877 if (!(csr & (EP0_IN_PKT_RDY | EP0_OUT_PKT_RDY))) {
1878 DEBUG_EP0("%s: IN_PKT_RDY and OUT_PKT_RDY are clear\n",
1879 __FUNCTION__);
1880
1881 switch (dev->ep0state) {
1882 case DATA_STATE_XMIT:
1883 DEBUG_EP0("continue with DATA_STATE_XMIT\n");
1884 lh7a40x_ep0_in(dev, csr);
1885 return;
1886 case DATA_STATE_NEED_ZLP:
1887 DEBUG_EP0("continue with DATA_STATE_NEED_ZLP\n");
1888 lh7a40x_ep0_in_zlp(dev, csr);
1889 return;
1890 default:
1891 /* Stall? */
1892 DEBUG_EP0("Odd state!! state = %s\n",
1893 state_names[dev->ep0state]);
1894 dev->ep0state = WAIT_FOR_SETUP;
1895 /* nuke(ep, 0); */
1896 /* usb_set(EP0_SEND_STALL, ep->csr1); */
1897 break;
1898 }
1899 }
1900
1901 /*
1902 * if SETUP_END is set
1903 * - abort the last transfer
1904 * - set SERVICED_SETUP_END_BIT
1905 */
1906 if (csr & EP0_SETUP_END) {
1907 DEBUG_EP0("%s: EP0_SETUP_END is set: %x\n", __FUNCTION__, csr);
1908
1909 usb_set(EP0_CLR_SETUP_END, USB_EP0_CSR);
1910
1911 nuke(ep, 0);
1912 dev->ep0state = WAIT_FOR_SETUP;
1913 }
1914
1915 /*
1916 * if EP0_OUT_PKT_RDY is set
1917 * - read data packet from EP0 FIFO
1918 * - decode command
1919 * - if error
1920 * set SERVICED_OUT_PKT_RDY | DATA_END bits | SEND_STALL
1921 * - else
1922 * set SERVICED_OUT_PKT_RDY | DATA_END bits
1923 */
1924 if (csr & EP0_OUT_PKT_RDY) {
1925
1926 DEBUG_EP0("%s: EP0_OUT_PKT_RDY is set: %x\n", __FUNCTION__,
1927 csr);
1928
1929 switch (dev->ep0state) {
1930 case WAIT_FOR_SETUP:
1931 DEBUG_EP0("WAIT_FOR_SETUP\n");
1932 lh7a40x_ep0_setup(dev, csr);
1933 break;
1934
1935 case DATA_STATE_RECV:
1936 DEBUG_EP0("DATA_STATE_RECV\n");
1937 lh7a40x_ep0_out(dev, csr);
1938 break;
1939
1940 default:
1941 /* send stall? */
1942 DEBUG_EP0("strange state!! 2. send stall? state = %d\n",
1943 dev->ep0state);
1944 break;
1945 }
1946 }
1947}
1948
1949static void lh7a40x_ep0_kick(struct lh7a40x_udc *dev, struct lh7a40x_ep *ep)
1950{
1951 u32 csr;
1952
1953 usb_set_index(0);
1954 csr = usb_read(USB_EP0_CSR);
1955
1956 DEBUG_EP0("%s: %x\n", __FUNCTION__, csr);
1957
1958 /* Clear "out packet ready" */
1959 usb_set(EP0_CLR_OUT, USB_EP0_CSR);
1960
1961 if (ep_is_in(ep)) {
1962 dev->ep0state = DATA_STATE_XMIT;
1963 lh7a40x_ep0_in(dev, csr);
1964 } else {
1965 dev->ep0state = DATA_STATE_RECV;
1966 lh7a40x_ep0_out(dev, csr);
1967 }
1968}
1969
1970/* ---------------------------------------------------------------------------
1971 * device-scoped parts of the api to the usb controller hardware
1972 * ---------------------------------------------------------------------------
1973 */
1974
1975static int lh7a40x_udc_get_frame(struct usb_gadget *_gadget)
1976{
1977 u32 frame1 = usb_read(USB_FRM_NUM1); /* Least significant 8 bits */
1978 u32 frame2 = usb_read(USB_FRM_NUM2); /* Most significant 3 bits */
1979 DEBUG("%s, %p\n", __FUNCTION__, _gadget);
1980 return ((frame2 & 0x07) << 8) | (frame1 & 0xff);
1981}
1982
1983static int lh7a40x_udc_wakeup(struct usb_gadget *_gadget)
1984{
1985 /* host may not have enabled remote wakeup */
1986 /*if ((UDCCS0 & UDCCS0_DRWF) == 0)
1987 return -EHOSTUNREACH;
1988 udc_set_mask_UDCCR(UDCCR_RSM); */
1989 return -ENOTSUPP;
1990}
1991
1992static const struct usb_gadget_ops lh7a40x_udc_ops = {
1993 .get_frame = lh7a40x_udc_get_frame,
1994 .wakeup = lh7a40x_udc_wakeup,
1995 /* current versions must always be self-powered */
1996};
1997
1998static void nop_release(struct device *dev)
1999{
2000 DEBUG("%s %s\n", __FUNCTION__, dev->bus_id);
2001}
2002
2003static struct lh7a40x_udc memory = {
2004 .usb_address = 0,
2005
2006 .gadget = {
2007 .ops = &lh7a40x_udc_ops,
2008 .ep0 = &memory.ep[0].ep,
2009 .name = driver_name,
2010 .dev = {
2011 .bus_id = "gadget",
2012 .release = nop_release,
2013 },
2014 },
2015
2016 /* control endpoint */
2017 .ep[0] = {
2018 .ep = {
2019 .name = ep0name,
2020 .ops = &lh7a40x_ep_ops,
2021 .maxpacket = EP0_PACKETSIZE,
2022 },
2023 .dev = &memory,
2024
2025 .bEndpointAddress = 0,
2026 .bmAttributes = 0,
2027
2028 .ep_type = ep_control,
2029 .fifo = io_p2v(USB_EP0_FIFO),
2030 .csr1 = USB_EP0_CSR,
2031 .csr2 = USB_EP0_CSR,
2032 },
2033
2034 /* first group of endpoints */
2035 .ep[1] = {
2036 .ep = {
2037 .name = "ep1in-bulk",
2038 .ops = &lh7a40x_ep_ops,
2039 .maxpacket = 64,
2040 },
2041 .dev = &memory,
2042
2043 .bEndpointAddress = USB_DIR_IN | 1,
2044 .bmAttributes = USB_ENDPOINT_XFER_BULK,
2045
2046 .ep_type = ep_bulk_in,
2047 .fifo = io_p2v(USB_EP1_FIFO),
2048 .csr1 = USB_IN_CSR1,
2049 .csr2 = USB_IN_CSR2,
2050 },
2051
2052 .ep[2] = {
2053 .ep = {
2054 .name = "ep2out-bulk",
2055 .ops = &lh7a40x_ep_ops,
2056 .maxpacket = 64,
2057 },
2058 .dev = &memory,
2059
2060 .bEndpointAddress = 2,
2061 .bmAttributes = USB_ENDPOINT_XFER_BULK,
2062
2063 .ep_type = ep_bulk_out,
2064 .fifo = io_p2v(USB_EP2_FIFO),
2065 .csr1 = USB_OUT_CSR1,
2066 .csr2 = USB_OUT_CSR2,
2067 },
2068
2069 .ep[3] = {
2070 .ep = {
2071 .name = "ep3in-int",
2072 .ops = &lh7a40x_ep_ops,
2073 .maxpacket = 64,
2074 },
2075 .dev = &memory,
2076
2077 .bEndpointAddress = USB_DIR_IN | 3,
2078 .bmAttributes = USB_ENDPOINT_XFER_INT,
2079
2080 .ep_type = ep_interrupt,
2081 .fifo = io_p2v(USB_EP3_FIFO),
2082 .csr1 = USB_IN_CSR1,
2083 .csr2 = USB_IN_CSR2,
2084 },
2085};
2086
2087/*
2088 * probe - binds to the platform device
2089 */
Russell King3ae5eae2005-11-09 22:32:44 +00002090static int lh7a40x_udc_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091{
2092 struct lh7a40x_udc *dev = &memory;
2093 int retval;
2094
Russell King3ae5eae2005-11-09 22:32:44 +00002095 DEBUG("%s: %p\n", __FUNCTION__, pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
2097 spin_lock_init(&dev->lock);
Russell King3ae5eae2005-11-09 22:32:44 +00002098 dev->dev = &pdev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099
2100 device_initialize(&dev->gadget.dev);
Russell King3ae5eae2005-11-09 22:32:44 +00002101 dev->gadget.dev.parent = &pdev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
2103 the_controller = dev;
Russell King3ae5eae2005-11-09 22:32:44 +00002104 platform_set_drvdata(pdev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105
2106 udc_disable(dev);
2107 udc_reinit(dev);
2108
2109 /* irq setup after old hardware state is cleaned up */
2110 retval =
2111 request_irq(IRQ_USBINTR, lh7a40x_udc_irq, SA_INTERRUPT, driver_name,
2112 dev);
2113 if (retval != 0) {
2114 DEBUG(KERN_ERR "%s: can't get irq %i, err %d\n", driver_name,
2115 IRQ_USBINTR, retval);
2116 return -EBUSY;
2117 }
2118
2119 create_proc_files();
2120
2121 return retval;
2122}
2123
Russell King3ae5eae2005-11-09 22:32:44 +00002124static int lh7a40x_udc_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125{
Russell King3ae5eae2005-11-09 22:32:44 +00002126 struct lh7a40x_udc *dev = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127
Russell King3ae5eae2005-11-09 22:32:44 +00002128 DEBUG("%s: %p\n", __FUNCTION__, pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
2130 udc_disable(dev);
2131 remove_proc_files();
2132 usb_gadget_unregister_driver(dev->driver);
2133
2134 free_irq(IRQ_USBINTR, dev);
2135
Russell King3ae5eae2005-11-09 22:32:44 +00002136 platform_set_drvdata(pdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137
2138 the_controller = 0;
2139
2140 return 0;
2141}
2142
2143/*-------------------------------------------------------------------------*/
2144
Russell King3ae5eae2005-11-09 22:32:44 +00002145static struct platform_driver udc_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 .probe = lh7a40x_udc_probe,
2147 .remove = lh7a40x_udc_remove
2148 /* FIXME power management support */
2149 /* .suspend = ... disable UDC */
2150 /* .resume = ... re-enable UDC */
Russell King3ae5eae2005-11-09 22:32:44 +00002151 .driver = {
2152 .name = (char *)driver_name,
2153 .owner = THIS_MODULE,
2154 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155};
2156
2157static int __init udc_init(void)
2158{
2159 DEBUG("%s: %s version %s\n", __FUNCTION__, driver_name, DRIVER_VERSION);
Russell King3ae5eae2005-11-09 22:32:44 +00002160 return platform_driver_register(&udc_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161}
2162
2163static void __exit udc_exit(void)
2164{
Russell King3ae5eae2005-11-09 22:32:44 +00002165 platform_driver_unregister(&udc_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166}
2167
2168module_init(udc_init);
2169module_exit(udc_exit);
2170
2171MODULE_DESCRIPTION(DRIVER_DESC);
2172MODULE_AUTHOR("Mikko Lahteenmaki, Bo Henriksen");
2173MODULE_LICENSE("GPL");