2 * linux/drivers/usb/gadget/lh7a40x_udc.c
3 * Sharp LH7A40x on-chip full speed USB device controllers
5 * Copyright (C) 2004 Mikko Lahteenmaki, Nordic ID
6 * Copyright (C) 2004 Bo Henriksen, Nordic ID
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.
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.
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
24 #include <linux/platform_device.h>
26 #include "lh7a40x_udc.h"
28 //#define DEBUG printk
29 //#define DEBUG_EP0 printk
30 //#define DEBUG_SETUP printk
33 # define DEBUG_EP0(fmt,args...)
36 # define DEBUG_SETUP(fmt,args...)
40 # define DEBUG(fmt,args...)
43 #define DRIVER_DESC "LH7A40x USB Device Controller"
44 #define DRIVER_VERSION __DATE__
46 #ifndef _BIT /* FIXME - what happended to _BIT in 2.6.7bk18? */
47 #define _BIT(x) (1<<(x))
50 struct lh7a40x_udc *the_controller;
52 static const char driver_name[] = "lh7a40x_udc";
53 static const char driver_desc[] = DRIVER_DESC;
54 static const char ep0name[] = "ep0-control";
61 static char *state_names[] = {
64 "DATA_STATE_NEED_ZLP",
65 "WAIT_FOR_OUT_STATUS",
73 static int lh7a40x_ep_enable(struct usb_ep *ep,
74 const struct usb_endpoint_descriptor *);
75 static int lh7a40x_ep_disable(struct usb_ep *ep);
76 static struct usb_request *lh7a40x_alloc_request(struct usb_ep *ep, gfp_t);
77 static void lh7a40x_free_request(struct usb_ep *ep, struct usb_request *);
78 static void *lh7a40x_alloc_buffer(struct usb_ep *ep, unsigned, dma_addr_t *,
80 static void lh7a40x_free_buffer(struct usb_ep *ep, void *, dma_addr_t,
82 static int lh7a40x_queue(struct usb_ep *ep, struct usb_request *, gfp_t);
83 static int lh7a40x_dequeue(struct usb_ep *ep, struct usb_request *);
84 static int lh7a40x_set_halt(struct usb_ep *ep, int);
85 static int lh7a40x_fifo_status(struct usb_ep *ep);
86 static int lh7a40x_fifo_status(struct usb_ep *ep);
87 static void lh7a40x_fifo_flush(struct usb_ep *ep);
88 static void lh7a40x_ep0_kick(struct lh7a40x_udc *dev, struct lh7a40x_ep *ep);
89 static void lh7a40x_handle_ep0(struct lh7a40x_udc *dev, u32 intr);
91 static void done(struct lh7a40x_ep *ep, struct lh7a40x_request *req,
93 static void pio_irq_enable(int bEndpointAddress);
94 static void pio_irq_disable(int bEndpointAddress);
95 static void stop_activity(struct lh7a40x_udc *dev,
96 struct usb_gadget_driver *driver);
97 static void flush(struct lh7a40x_ep *ep);
98 static void udc_enable(struct lh7a40x_udc *dev);
99 static void udc_set_address(struct lh7a40x_udc *dev, unsigned char address);
101 static struct usb_ep_ops lh7a40x_ep_ops = {
102 .enable = lh7a40x_ep_enable,
103 .disable = lh7a40x_ep_disable,
105 .alloc_request = lh7a40x_alloc_request,
106 .free_request = lh7a40x_free_request,
108 .alloc_buffer = lh7a40x_alloc_buffer,
109 .free_buffer = lh7a40x_free_buffer,
111 .queue = lh7a40x_queue,
112 .dequeue = lh7a40x_dequeue,
114 .set_halt = lh7a40x_set_halt,
115 .fifo_status = lh7a40x_fifo_status,
116 .fifo_flush = lh7a40x_fifo_flush,
121 static __inline__ int write_packet(struct lh7a40x_ep *ep,
122 struct lh7a40x_request *req, int max)
126 volatile u32 *fifo = (volatile u32 *)ep->fifo;
128 buf = req->req.buf + req->req.actual;
131 length = req->req.length - req->req.actual;
132 length = min(length, max);
133 req->req.actual += length;
135 DEBUG("Write %d (max %d), fifo %p\n", length, max, fifo);
145 static __inline__ void usb_set_index(u32 ep)
147 *(volatile u32 *)io_p2v(USB_INDEX) = ep;
150 static __inline__ u32 usb_read(u32 port)
152 return *(volatile u32 *)io_p2v(port);
155 static __inline__ void usb_write(u32 val, u32 port)
157 *(volatile u32 *)io_p2v(port) = val;
160 static __inline__ void usb_set(u32 val, u32 port)
162 volatile u32 *ioport = (volatile u32 *)io_p2v(port);
163 u32 after = (*ioport) | val;
167 static __inline__ void usb_clear(u32 val, u32 port)
169 volatile u32 *ioport = (volatile u32 *)io_p2v(port);
170 u32 after = (*ioport) & ~val;
174 /*-------------------------------------------------------------------------*/
176 #define GPIO_PORTC_DR (0x80000E08)
177 #define GPIO_PORTC_DDR (0x80000E18)
178 #define GPIO_PORTC_PDR (0x80000E70)
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))
191 * Port C bit 1 = USB Port 1 Power Enable
192 * Port C bit 2 = USB Port 1 Data Carrier Detect
194 #define is_usb_connected() get_portc_pdr(2)
196 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
198 static const char proc_node_name[] = "driver/udc";
201 udc_proc_read(char *page, char **start, off_t off, int count,
202 int *eof, void *_dev)
205 struct lh7a40x_udc *dev = _dev;
207 unsigned size = count;
214 local_irq_save(flags);
216 /* basic device status */
217 t = scnprintf(next, size,
220 "Gadget driver: %s\n"
222 driver_name, DRIVER_VERSION,
223 dev->driver ? dev->driver->driver.name : "(none)",
224 is_usb_connected()? "full speed" : "disconnected");
228 t = scnprintf(next, size,
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)
238 t = scnprintf(next, size,
239 "DCP pullup: %d\n\n",
240 (usb_read(USB_PM) & PM_USB_DCP) != 0);
244 local_irq_restore(flags);
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)
252 #else /* !CONFIG_USB_GADGET_DEBUG_FILES */
254 #define create_proc_files() do {} while (0)
255 #define remove_proc_files() do {} while (0)
257 #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
260 * udc_disable - disable USB device controller
262 static void udc_disable(struct lh7a40x_udc *dev)
264 DEBUG("%s, %p\n", __FUNCTION__, dev);
266 udc_set_address(dev, 0);
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);
273 /* Disable the USB */
274 usb_write(0, USB_PM);
276 #ifdef CONFIG_ARCH_LH7A404
277 /* Disable USB power */
281 /* if hardware supports it, disconnect from usb */
282 /* make_usb_disappear(); */
284 dev->ep0state = WAIT_FOR_SETUP;
285 dev->gadget.speed = USB_SPEED_UNKNOWN;
286 dev->usb_address = 0;
290 * udc_reinit - initialize software state
292 static void udc_reinit(struct lh7a40x_udc *dev)
296 DEBUG("%s, %p\n", __FUNCTION__, dev);
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;
303 /* basic endpoint records init */
304 for (i = 0; i < UDC_MAX_ENDPOINTS; i++) {
305 struct lh7a40x_ep *ep = &dev->ep[i];
308 list_add_tail(&ep->ep.ep_list, &dev->gadget.ep_list);
312 INIT_LIST_HEAD(&ep->queue);
316 /* the rest was statically initialized, and is read-only */
319 #define BYTES2MAXP(x) (x / 8)
320 #define MAXP2BYTES(x) (x * 8)
322 /* until it's enabled, this UDC should be completely invisible
325 static void udc_enable(struct lh7a40x_udc *dev)
329 DEBUG("%s, %p\n", __FUNCTION__, dev);
331 dev->gadget.speed = USB_SPEED_UNKNOWN;
333 #ifdef CONFIG_ARCH_LH7A404
334 /* Set Port C bit 1 & 2 as output */
338 /* Enable USB power */
343 * C.f Chapter 18.1.3.1 Initializing the USB
346 /* Disable the USB */
347 usb_clear(PM_USB_ENABLE, USB_PM);
349 /* Reset APB & I/O sides of the USB */
350 usb_set(USB_RESET_APB | USB_RESET_IO, USB_RESET);
352 usb_clear(USB_RESET_APB | USB_RESET_IO, USB_RESET);
354 /* Set MAXP values for each */
355 for (ep = 0; ep < UDC_MAX_ENDPOINTS; ep++) {
356 struct lh7a40x_ep *ep_reg = &dev->ep[ep];
361 switch (ep_reg->ep_type) {
364 usb_clear(USB_IN_CSR2_USB_DMA_EN | USB_IN_CSR2_AUTO_SET,
368 usb_write(BYTES2MAXP(ep_maxpacket(ep_reg)),
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)),
379 /* Read & Write CSR1, just in case */
380 csr = usb_read(ep_reg->csr1);
381 usb_write(csr, ep_reg->csr1);
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);
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); */
399 usb_set(PM_ENABLE_SUSPEND, USB_PM);
402 usb_set(PM_USB_ENABLE, USB_PM);
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.
410 /* usb_clear(PM_USB_DCP, USB_PM);
411 usb_set(PM_USB_DCP, USB_PM); */
416 Register entry point for the peripheral controller driver.
418 int usb_gadget_register_driver(struct usb_gadget_driver *driver)
420 struct lh7a40x_udc *dev = the_controller;
423 DEBUG("%s: %s\n", __FUNCTION__, driver->driver.name);
426 || driver->speed != USB_SPEED_FULL
428 || !driver->unbind || !driver->disconnect || !driver->setup)
435 /* first hook up the driver ... */
436 dev->driver = driver;
437 dev->gadget.dev.driver = &driver->driver;
439 device_add(&dev->gadget.dev);
440 retval = driver->bind(&dev->gadget);
442 printk("%s: bind to driver %s --> error %d\n", dev->gadget.name,
443 driver->driver.name, retval);
444 device_del(&dev->gadget.dev);
447 dev->gadget.dev.driver = 0;
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.
455 printk("%s: registered gadget driver '%s'\n", dev->gadget.name,
456 driver->driver.name);
463 EXPORT_SYMBOL(usb_gadget_register_driver);
466 Unregister entry point for the peripheral controller driver.
468 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
470 struct lh7a40x_udc *dev = the_controller;
475 if (!driver || driver != dev->driver)
478 spin_lock_irqsave(&dev->lock, flags);
480 stop_activity(dev, driver);
481 spin_unlock_irqrestore(&dev->lock, flags);
483 driver->unbind(&dev->gadget);
484 device_del(&dev->gadget.dev);
488 DEBUG("unregistered gadget driver '%s'\n", driver->driver.name);
492 EXPORT_SYMBOL(usb_gadget_unregister_driver);
494 /*-------------------------------------------------------------------------*/
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
500 static int write_fifo(struct lh7a40x_ep *ep, struct lh7a40x_request *req)
505 max = le16_to_cpu(ep->desc->wMaxPacketSize);
507 csr = usb_read(ep->csr1);
508 DEBUG("CSR: %x %d\n", csr, csr & USB_IN_CSR1_FIFO_NOT_EMPTY);
510 if (!(csr & USB_IN_CSR1_FIFO_NOT_EMPTY)) {
512 int is_last, is_short;
514 count = write_packet(ep, req, max);
515 usb_set(USB_IN_CSR1_IN_PKT_RDY, ep->csr1);
517 /* last packet is usually short (or a zlp) */
518 if (unlikely(count != max))
519 is_last = is_short = 1;
521 if (likely(req->req.length != req->req.actual)
526 /* interrupt/iso maxpacket may not fill the fifo */
527 is_short = unlikely(max < ep_maxpacket(ep));
530 DEBUG("%s: wrote %s %d bytes%s%s %d left %p\n", __FUNCTION__,
532 is_last ? "/L" : "", is_short ? "/S" : "",
533 req->req.length - req->req.actual, req);
535 /* requests complete when all IN data is in the FIFO */
538 if (list_empty(&ep->queue)) {
539 pio_irq_disable(ep_index(ep));
544 DEBUG("Hmm.. %d ep FIFO is not empty!\n", ep_index(ep));
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
554 static int read_fifo(struct lh7a40x_ep *ep, struct lh7a40x_request *req)
558 unsigned bufferspace, count, is_short;
559 volatile u32 *fifo = (volatile u32 *)ep->fifo;
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__);
568 buf = req->req.buf + req->req.actual;
570 bufferspace = req->req.length - req->req.actual;
572 /* read all bytes from this packet */
573 count = usb_read(USB_OUT_FIFO_WC1);
574 req->req.actual += min(count, bufferspace);
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);
581 while (likely(count-- != 0)) {
582 u8 byte = (u8) (*fifo & 0xff);
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.
589 if (req->req.status != -EOVERFLOW)
590 printk("%s overflow %d\n", ep->ep.name, count);
591 req->req.status = -EOVERFLOW;
598 usb_clear(USB_OUT_CSR1_OUT_PKT_RDY, ep->csr1);
601 if (is_short || req->req.actual == req->req.length) {
603 usb_set(USB_OUT_CSR1_FIFO_FLUSH, ep->csr1);
605 if (list_empty(&ep->queue))
606 pio_irq_disable(ep_index(ep));
610 /* finished that packet. the next one may be waiting... */
615 * done - retire a request; caller blocked irqs
616 * INDEX register is preserved to keep same
618 static void done(struct lh7a40x_ep *ep, struct lh7a40x_request *req, int status)
620 unsigned int stopped = ep->stopped;
623 DEBUG("%s, %p\n", __FUNCTION__, ep);
624 list_del_init(&req->queue);
626 if (likely(req->req.status == -EINPROGRESS))
627 req->req.status = status;
629 status = req->req.status;
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);
636 /* don't modify queue heads during completion callback */
638 /* Read current index (completion may modify it) */
639 index = usb_read(USB_INDEX);
641 spin_unlock(&ep->dev->lock);
642 req->req.complete(&ep->ep, &req->req);
643 spin_lock(&ep->dev->lock);
646 usb_set_index(index);
647 ep->stopped = stopped;
650 /** Enable EP interrupt */
651 static void pio_irq_enable(int ep)
653 DEBUG("%s: %d\n", __FUNCTION__, ep);
657 usb_set(USB_IN_INT_EP1, USB_IN_INT_EN);
660 usb_set(USB_OUT_INT_EP2, USB_OUT_INT_EN);
663 usb_set(USB_IN_INT_EP3, USB_IN_INT_EN);
666 DEBUG("Unknown endpoint: %d\n", ep);
671 /** Disable EP interrupt */
672 static void pio_irq_disable(int ep)
674 DEBUG("%s: %d\n", __FUNCTION__, ep);
678 usb_clear(USB_IN_INT_EP1, USB_IN_INT_EN);
681 usb_clear(USB_OUT_INT_EP2, USB_OUT_INT_EN);
684 usb_clear(USB_IN_INT_EP3, USB_IN_INT_EN);
687 DEBUG("Unknown endpoint: %d\n", ep);
693 * nuke - dequeue ALL requests
695 void nuke(struct lh7a40x_ep *ep, int status)
697 struct lh7a40x_request *req;
699 DEBUG("%s, %p\n", __FUNCTION__, ep);
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);
710 /* Disable IRQ if EP is enabled (has descriptor) */
712 pio_irq_disable(ep_index(ep));
716 void nuke_all(struct lh7a40x_udc *dev)
719 for(n=0; n<UDC_MAX_ENDPOINTS; n++) {
720 struct lh7a40x_ep *ep = &dev->ep[n];
727 static void flush_all(struct lh7a40x_udc *dev)
730 for (n = 0; n < UDC_MAX_ENDPOINTS; n++)
732 struct lh7a40x_ep *ep = &dev->ep[n];
739 * NOTE: INDEX register must be set before this call
741 static void flush(struct lh7a40x_ep *ep)
743 DEBUG("%s, %p\n", __FUNCTION__, ep);
745 switch (ep->ep_type) {
747 /* check, by implication c.f. 15.1.2.11 */
752 /* if(csr & USB_IN_CSR1_IN_PKT_RDY) */
753 usb_set(USB_IN_CSR1_FIFO_FLUSH, ep->csr1);
757 /* if(csr & USB_OUT_CSR1_OUT_PKT_RDY) */
758 usb_set(USB_OUT_CSR1_FIFO_FLUSH, ep->csr1);
764 * lh7a40x_in_epn - handle IN interrupt
766 static void lh7a40x_in_epn(struct lh7a40x_udc *dev, u32 ep_idx, u32 intr)
769 struct lh7a40x_ep *ep = &dev->ep[ep_idx];
770 struct lh7a40x_request *req;
772 usb_set_index(ep_idx);
774 csr = usb_read(ep->csr1);
775 DEBUG("%s: %d, csr %x\n", __FUNCTION__, ep_idx, csr);
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 */ ,
785 DEBUG("%s: NO EP DESC\n", __FUNCTION__);
789 if (list_empty(&ep->queue))
792 req = list_entry(ep->queue.next, struct lh7a40x_request, queue);
794 DEBUG("req: %p\n", req);
802 /* ********************************************************************************************* */
806 static void lh7a40x_out_epn(struct lh7a40x_udc *dev, u32 ep_idx, u32 intr)
808 struct lh7a40x_ep *ep = &dev->ep[ep_idx];
809 struct lh7a40x_request *req;
811 DEBUG("%s: %d\n", __FUNCTION__, ep_idx);
813 usb_set_index(ep_idx);
817 csr = usb_read(ep->csr1);
821 csr1)) & (USB_OUT_CSR1_OUT_PKT_RDY |
822 USB_OUT_CSR1_SENT_STALL)) {
823 DEBUG("%s: %x\n", __FUNCTION__, csr);
825 if (csr & USB_OUT_CSR1_SENT_STALL) {
826 DEBUG("%s: stall sent, flush fifo\n",
828 /* usb_set(USB_OUT_CSR1_FIFO_FLUSH, ep->csr1); */
830 } else if (csr & USB_OUT_CSR1_OUT_PKT_RDY) {
831 if (list_empty(&ep->queue))
835 list_entry(ep->queue.next,
836 struct lh7a40x_request,
840 printk("%s: NULL REQ %d\n",
841 __FUNCTION__, ep_idx);
852 /* Throw packet away.. */
853 printk("%s: No descriptor?!?\n", __FUNCTION__);
858 static void stop_activity(struct lh7a40x_udc *dev,
859 struct usb_gadget_driver *driver)
863 /* don't disconnect drivers more than once */
864 if (dev->gadget.speed == USB_SPEED_UNKNOWN)
866 dev->gadget.speed = USB_SPEED_UNKNOWN;
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];
874 nuke(ep, -ESHUTDOWN);
877 /* report disconnect; the driver is already quiesced */
879 spin_unlock(&dev->lock);
880 driver->disconnect(&dev->gadget);
881 spin_lock(&dev->lock);
884 /* re-init driver-visible data structures */
888 /** Handle USB RESET interrupt
890 static void lh7a40x_reset_intr(struct lh7a40x_udc *dev)
892 #if 0 /* def CONFIG_ARCH_LH7A404 */
893 /* Does not work always... */
895 DEBUG("%s: %d\n", __FUNCTION__, dev->usb_address);
897 if (!dev->usb_address) {
898 /*usb_set(USB_RESET_IO, USB_RESET);
900 usb_clear(USB_RESET_IO, USB_RESET); */
903 /* Put the USB controller into reset. */
904 usb_set(USB_RESET_IO, USB_RESET);
906 /* Set Device ID to 0 */
907 udc_set_address(dev, 0);
909 /* Let PLL2 settle down */
912 /* Release the USB controller from reset */
913 usb_clear(USB_RESET_IO, USB_RESET);
919 dev->gadget.speed = USB_SPEED_FULL;
923 * lh7a40x usb client interrupt handler.
925 static irqreturn_t lh7a40x_udc_irq(int irq, void *_dev, struct pt_regs *r)
927 struct lh7a40x_udc *dev = _dev;
931 spin_lock(&dev->lock);
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);
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);
942 if (!intr_out && !intr_in && !intr_int)
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);
952 usb_write(intr_in, USB_IN_INT);
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);
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);
964 if (intr_in & USB_IN_INT_EP0) {
965 DEBUG("USB_IN_INT_EP0 (control)\n");
966 lh7a40x_handle_ep0(dev, intr_in);
971 usb_write(intr_out, USB_OUT_INT);
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);
981 usb_write(intr_int, USB_INT);
983 if (intr_int & USB_INT_RESET_INT) {
984 lh7a40x_reset_intr(dev);
987 if (intr_int & USB_INT_RESUME_INT) {
988 DEBUG("USB resume\n");
990 if (dev->gadget.speed != USB_SPEED_UNKNOWN
992 && dev->driver->resume
993 && is_usb_connected()) {
994 dev->driver->resume(&dev->gadget);
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);
1013 spin_unlock(&dev->lock);
1018 static int lh7a40x_ep_enable(struct usb_ep *_ep,
1019 const struct usb_endpoint_descriptor *desc)
1021 struct lh7a40x_ep *ep;
1022 struct lh7a40x_udc *dev;
1023 unsigned long flags;
1025 DEBUG("%s, %p\n", __FUNCTION__, _ep);
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__);
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);
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);
1053 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
1054 DEBUG("%s, bogus device state\n", __FUNCTION__);
1058 spin_lock_irqsave(&ep->dev->lock, flags);
1063 ep->ep.maxpacket = le16_to_cpu(desc->wMaxPacketSize);
1065 /* Reset halt state (does flush) */
1066 lh7a40x_set_halt(_ep, 0);
1068 spin_unlock_irqrestore(&ep->dev->lock, flags);
1070 DEBUG("%s: enabled %s\n", __FUNCTION__, _ep->name);
1075 * NOTE: Sets INDEX register
1077 static int lh7a40x_ep_disable(struct usb_ep *_ep)
1079 struct lh7a40x_ep *ep;
1080 unsigned long flags;
1082 DEBUG("%s, %p\n", __FUNCTION__, _ep);
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);
1091 spin_lock_irqsave(&ep->dev->lock, flags);
1093 usb_set_index(ep_index(ep));
1095 /* Nuke all pending requests (does flush) */
1096 nuke(ep, -ESHUTDOWN);
1098 /* Disable ep IRQ */
1099 pio_irq_disable(ep_index(ep));
1104 spin_unlock_irqrestore(&ep->dev->lock, flags);
1106 DEBUG("%s: disabled %s\n", __FUNCTION__, _ep->name);
1110 static struct usb_request *lh7a40x_alloc_request(struct usb_ep *ep,
1113 struct lh7a40x_request *req;
1115 DEBUG("%s, %p\n", __FUNCTION__, ep);
1117 req = kmalloc(sizeof *req, gfp_flags);
1121 memset(req, 0, sizeof *req);
1122 INIT_LIST_HEAD(&req->queue);
1127 static void lh7a40x_free_request(struct usb_ep *ep, struct usb_request *_req)
1129 struct lh7a40x_request *req;
1131 DEBUG("%s, %p\n", __FUNCTION__, ep);
1133 req = container_of(_req, struct lh7a40x_request, req);
1134 WARN_ON(!list_empty(&req->queue));
1138 static void *lh7a40x_alloc_buffer(struct usb_ep *ep, unsigned bytes,
1139 dma_addr_t * dma, gfp_t gfp_flags)
1143 DEBUG("%s (%p, %d, %d)\n", __FUNCTION__, ep, bytes, gfp_flags);
1145 retval = kmalloc(bytes, gfp_flags & ~(__GFP_DMA | __GFP_HIGHMEM));
1147 *dma = virt_to_bus(retval);
1151 static void lh7a40x_free_buffer(struct usb_ep *ep, void *buf, dma_addr_t dma,
1154 DEBUG("%s, %p\n", __FUNCTION__, ep);
1158 /** Queue one request
1159 * Kickstart transfer if needed
1160 * NOTE: Sets INDEX register
1162 static int lh7a40x_queue(struct usb_ep *_ep, struct usb_request *_req,
1165 struct lh7a40x_request *req;
1166 struct lh7a40x_ep *ep;
1167 struct lh7a40x_udc *dev;
1168 unsigned long flags;
1170 DEBUG("\n\n\n%s, %p\n", __FUNCTION__, _ep);
1172 req = container_of(_req, struct lh7a40x_request, req);
1174 (!_req || !_req->complete || !_req->buf
1175 || !list_empty(&req->queue))) {
1176 DEBUG("%s, bad params\n", __FUNCTION__);
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__);
1187 if (unlikely(!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)) {
1188 DEBUG("%s, bogus device state %p\n", __FUNCTION__, dev->driver);
1192 DEBUG("%s queue req %p, len %d buf %p\n", _ep->name, _req, _req->length,
1195 spin_lock_irqsave(&dev->lock, flags);
1197 _req->status = -EINPROGRESS;
1200 /* kickstart this i/o queue? */
1201 DEBUG("Add to %d Q %d %d\n", ep_index(ep), list_empty(&ep->queue),
1203 if (list_empty(&ep->queue) && likely(!ep->stopped)) {
1206 if (unlikely(ep_index(ep) == 0)) {
1208 list_add_tail(&req->queue, &ep->queue);
1209 lh7a40x_ep0_kick(dev, ep);
1211 } else if (ep_is_in(ep)) {
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)
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)
1232 /* pio or dma irq handler advances the queue. */
1233 if (likely(req != 0))
1234 list_add_tail(&req->queue, &ep->queue);
1236 spin_unlock_irqrestore(&dev->lock, flags);
1241 /* dequeue JUST ONE request */
1242 static int lh7a40x_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1244 struct lh7a40x_ep *ep;
1245 struct lh7a40x_request *req;
1246 unsigned long flags;
1248 DEBUG("%s, %p\n", __FUNCTION__, _ep);
1250 ep = container_of(_ep, struct lh7a40x_ep, ep);
1251 if (!_ep || ep->ep.name == ep0name)
1254 spin_lock_irqsave(&ep->dev->lock, flags);
1256 /* make sure it's actually queued on this endpoint */
1257 list_for_each_entry(req, &ep->queue, queue) {
1258 if (&req->req == _req)
1261 if (&req->req != _req) {
1262 spin_unlock_irqrestore(&ep->dev->lock, flags);
1266 done(ep, req, -ECONNRESET);
1268 spin_unlock_irqrestore(&ep->dev->lock, flags);
1272 /** Halt specific EP
1273 * Return 0 if success
1274 * NOTE: Sets INDEX register to EP !
1276 static int lh7a40x_set_halt(struct usb_ep *_ep, int value)
1278 struct lh7a40x_ep *ep;
1279 unsigned long flags;
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__);
1287 usb_set_index(ep_index(ep));
1289 DEBUG("%s, ep %d, val %d\n", __FUNCTION__, ep_index(ep), value);
1291 spin_lock_irqsave(&ep->dev->lock, flags);
1293 if (ep_index(ep) == 0) {
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))) {
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
\92t collected.
1305 spin_unlock_irqrestore(&ep->dev->lock, flags);
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));
1314 usb_set(USB_IN_CSR1_SEND_STALL, ep->csr1);
1316 usb_clear(USB_IN_CSR1_SEND_STALL, ep->csr1);
1317 usb_set(USB_IN_CSR1_CLR_DATA_TOGGLE, ep->csr1);
1324 usb_set(USB_OUT_CSR1_SEND_STALL, ep->csr1);
1326 usb_clear(USB_OUT_CSR1_SEND_STALL, ep->csr1);
1327 usb_set(USB_OUT_CSR1_CLR_DATA_REG, ep->csr1);
1337 spin_unlock_irqrestore(&ep->dev->lock, flags);
1339 DEBUG("%s %s halted\n", _ep->name, value == 0 ? "NOT" : "IS");
1344 /** Return bytes in EP FIFO
1345 * NOTE: Sets INDEX register to EP
1347 static int lh7a40x_fifo_status(struct usb_ep *_ep)
1351 struct lh7a40x_ep *ep;
1353 ep = container_of(_ep, struct lh7a40x_ep, ep);
1355 DEBUG("%s, bad ep\n", __FUNCTION__);
1359 DEBUG("%s, %d\n", __FUNCTION__, ep_index(ep));
1361 /* LPD can't report unclaimed bytes from IN fifos */
1365 usb_set_index(ep_index(ep));
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);
1377 * NOTE: Sets INDEX register to EP
1379 static void lh7a40x_fifo_flush(struct usb_ep *_ep)
1381 struct lh7a40x_ep *ep;
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__);
1389 usb_set_index(ep_index(ep));
1393 /****************************************************************/
1394 /* End Point 0 related functions */
1395 /****************************************************************/
1397 /* return: 0 = still running, 1 = completed, negative = errno */
1398 static int write_fifo_ep0(struct lh7a40x_ep *ep, struct lh7a40x_request *req)
1404 max = ep_maxpacket(ep);
1406 DEBUG_EP0("%s\n", __FUNCTION__);
1408 count = write_packet(ep, req, max);
1410 /* last packet is usually short (or a zlp) */
1411 if (unlikely(count != max))
1414 if (likely(req->req.length != req->req.actual) || req->req.zero)
1420 DEBUG_EP0("%s: wrote %s %d bytes%s %d left %p\n", __FUNCTION__,
1422 is_last ? "/L" : "", req->req.length - req->req.actual, req);
1424 /* requests complete when all IN data is in the FIFO */
1433 static __inline__ int lh7a40x_fifo_read(struct lh7a40x_ep *ep,
1434 unsigned char *cp, int max)
1437 int count = usb_read(USB_OUT_FIFO_WC1);
1438 volatile u32 *fifo = (volatile u32 *)ep->fifo;
1444 *cp++ = *fifo & 0xFF;
1448 static __inline__ void lh7a40x_fifo_write(struct lh7a40x_ep *ep,
1449 unsigned char *cp, int count)
1451 volatile u32 *fifo = (volatile u32 *)ep->fifo;
1452 DEBUG_EP0("fifo_write: %d %d\n", ep_index(ep), count);
1457 static int read_fifo_ep0(struct lh7a40x_ep *ep, struct lh7a40x_request *req)
1461 unsigned bufferspace, count, is_short;
1462 volatile u32 *fifo = (volatile u32 *)ep->fifo;
1464 DEBUG_EP0("%s\n", __FUNCTION__);
1466 csr = usb_read(USB_EP0_CSR);
1467 if (!(csr & USB_OUT_CSR1_OUT_PKT_RDY))
1470 buf = req->req.buf + req->req.actual;
1472 bufferspace = req->req.length - req->req.actual;
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);
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);
1486 while (likely(count-- != 0)) {
1487 u8 byte = (u8) (*fifo & 0xff);
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.
1494 if (req->req.status != -EOVERFLOW)
1495 DEBUG_EP0("%s overflow %d\n", ep->ep.name,
1497 req->req.status = -EOVERFLOW;
1505 if (is_short || req->req.actual == req->req.length) {
1510 /* finished that packet. the next one may be waiting... */
1515 * udc_set_address - set the USB address for this device
1518 * Called from control endpoint function after it decodes a set address setup packet.
1520 static void udc_set_address(struct lh7a40x_udc *dev, unsigned char address)
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); */
1531 * DATA_STATE_RECV (OUT_PKT_RDY)
1533 * set EP0_CLR_OUT | EP0_DATA_END | EP0_SEND_STALL bits
1535 * set EP0_CLR_OUT bit
1536 if last set EP0_DATA_END bit
1538 static void lh7a40x_ep0_out(struct lh7a40x_udc *dev, u32 csr)
1540 struct lh7a40x_request *req;
1541 struct lh7a40x_ep *ep = &dev->ep[0];
1544 DEBUG_EP0("%s: %x\n", __FUNCTION__, csr);
1546 if (list_empty(&ep->queue))
1549 req = list_entry(ep->queue.next, struct lh7a40x_request, queue);
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;
1559 ret = read_fifo_ep0(ep, req);
1562 DEBUG_EP0("%s: finished, waiting for status\n",
1565 usb_set((EP0_CLR_OUT | EP0_DATA_END), USB_EP0_CSR);
1566 dev->ep0state = WAIT_FOR_SETUP;
1568 /* Not done yet.. */
1569 DEBUG_EP0("%s: not finished\n", __FUNCTION__);
1570 usb_set(EP0_CLR_OUT, USB_EP0_CSR);
1573 DEBUG_EP0("NO REQ??!\n");
1580 static int lh7a40x_ep0_in(struct lh7a40x_udc *dev, u32 csr)
1582 struct lh7a40x_request *req;
1583 struct lh7a40x_ep *ep = &dev->ep[0];
1584 int ret, need_zlp = 0;
1586 DEBUG_EP0("%s: %x\n", __FUNCTION__, csr);
1588 if (list_empty(&ep->queue))
1591 req = list_entry(ep->queue.next, struct lh7a40x_request, queue);
1594 DEBUG_EP0("%s: NULL REQ\n", __FUNCTION__);
1598 if (req->req.length == 0) {
1600 usb_set((EP0_IN_PKT_RDY | EP0_DATA_END), USB_EP0_CSR);
1601 dev->ep0state = WAIT_FOR_SETUP;
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 */
1611 ret = write_fifo_ep0(ep, req);
1613 if (ret == 1 && !need_zlp) {
1615 DEBUG_EP0("%s: finished, waiting for status\n", __FUNCTION__);
1617 usb_set((EP0_IN_PKT_RDY | EP0_DATA_END), USB_EP0_CSR);
1618 dev->ep0state = WAIT_FOR_SETUP;
1620 DEBUG_EP0("%s: not finished\n", __FUNCTION__);
1621 usb_set(EP0_IN_PKT_RDY, USB_EP0_CSR);
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;
1633 static int lh7a40x_handle_get_status(struct lh7a40x_udc *dev,
1634 struct usb_ctrlrequest *ctrl)
1636 struct lh7a40x_ep *ep0 = &dev->ep[0];
1637 struct lh7a40x_ep *qep;
1638 int reqtype = (ctrl->bRequestType & USB_RECIP_MASK);
1641 if (reqtype == USB_RECIP_INTERFACE) {
1642 /* This is not supported.
1643 * And according to the USB spec, this one does nothing..
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);
1655 ("GET_STATUS: USB_RECIP_ENDPOINT (%d), ctrl->wLength = %d\n",
1656 ep_num, ctrl->wLength);
1658 if (ctrl->wLength > 2 || ep_num > 3)
1661 qep = &dev->ep[ep_num];
1662 if (ep_is_in(qep) != ((ctrl->wIndex & USB_DIR_IN) ? 1 : 0)
1663 && ep_index(qep) != 0) {
1667 usb_set_index(ep_index(qep));
1669 /* Return status on next IN token */
1670 switch (qep->ep_type) {
1673 (usb_read(qep->csr1) & EP0_SEND_STALL) ==
1679 (usb_read(qep->csr1) & USB_IN_CSR1_SEND_STALL) ==
1680 USB_IN_CSR1_SEND_STALL;
1684 (usb_read(qep->csr1) & USB_OUT_CSR1_SEND_STALL) ==
1685 USB_OUT_CSR1_SEND_STALL;
1689 /* Back to EP0 index */
1692 DEBUG_SETUP("GET_STATUS, ep: %d (%x), val = %d\n", ep_num,
1695 DEBUG_SETUP("Unknown REQ TYPE: %d\n", reqtype);
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);
1710 * WAIT_FOR_SETUP (OUT_PKT_RDY)
1711 * - read data packet from EP0 FIFO
1714 * set EP0_CLR_OUT | EP0_DATA_END | EP0_SEND_STALL bits
1716 * set EP0_CLR_OUT | EP0_DATA_END bits
1718 static void lh7a40x_ep0_setup(struct lh7a40x_udc *dev, u32 csr)
1720 struct lh7a40x_ep *ep = &dev->ep[0];
1721 struct usb_ctrlrequest ctrl;
1722 int i, bytes, is_in;
1724 DEBUG_SETUP("%s: %x\n", __FUNCTION__, csr);
1726 /* Nuke all previous transfers */
1729 /* read control req from fifo (8 bytes) */
1730 bytes = lh7a40x_fifo_read(ep, (unsigned char *)&ctrl, 8);
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);
1740 /* Set direction of EP0 */
1741 if (likely(ctrl.bRequestType & USB_DIR_IN)) {
1742 ep->bEndpointAddress |= USB_DIR_IN;
1745 ep->bEndpointAddress &= ~USB_DIR_IN;
1749 dev->req_pending = 1;
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))
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);
1762 case USB_REQ_GET_STATUS:{
1763 if (lh7a40x_handle_get_status(dev, &ctrl) == 0)
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);
1772 /* Support only HALT feature */
1773 if (ctrl.wValue != 0 || ctrl.wLength != 0
1774 || ep_num > 3 || ep_num < 1)
1777 qep = &dev->ep[ep_num];
1778 if (ctrl.bRequest == USB_REQ_SET_FEATURE) {
1779 DEBUG_SETUP("SET_FEATURE (%d)\n",
1781 lh7a40x_set_halt(&qep->ep, 1);
1783 DEBUG_SETUP("CLR_FEATURE (%d)\n",
1785 lh7a40x_set_halt(&qep->ep, 0);
1789 /* Reply with a ZLP on next IN token */
1790 usb_set((EP0_CLR_OUT | EP0_DATA_END),
1801 if (likely(dev->driver)) {
1802 /* device-2-host (IN) or no data setup command, process immediately */
1803 spin_unlock(&dev->lock);
1804 i = dev->driver->setup(&dev->gadget, &ctrl);
1805 spin_lock(&dev->lock);
1808 /* setup processing failed, force stall */
1810 (" --> ERROR: gadget setup FAILED (stalling), setup returned %d\n",
1813 usb_set((EP0_CLR_OUT | EP0_DATA_END | EP0_SEND_STALL),
1816 /* ep->stopped = 1; */
1817 dev->ep0state = WAIT_FOR_SETUP;
1823 * DATA_STATE_NEED_ZLP
1825 static void lh7a40x_ep0_in_zlp(struct lh7a40x_udc *dev, u32 csr)
1827 DEBUG_EP0("%s: %x\n", __FUNCTION__, csr);
1829 /* c.f. Table 15-14 */
1830 usb_set((EP0_IN_PKT_RDY | EP0_DATA_END), USB_EP0_CSR);
1831 dev->ep0state = WAIT_FOR_SETUP;
1835 * handle ep0 interrupt
1837 static void lh7a40x_handle_ep0(struct lh7a40x_udc *dev, u32 intr)
1839 struct lh7a40x_ep *ep = &dev->ep[0];
1844 csr = usb_read(USB_EP0_CSR);
1846 DEBUG_EP0("%s: csr = %x\n", __FUNCTION__, csr);
1849 * For overview of what we should be doing see c.f. Chapter 18.1.2.4
1850 * We will follow that outline here modified by our own global state
1851 * indication which provides hints as to what we think should be
1856 * if SENT_STALL is set
1857 * - clear the SENT_STALL bit
1859 if (csr & EP0_SENT_STALL) {
1860 DEBUG_EP0("%s: EP0_SENT_STALL is set: %x\n", __FUNCTION__, csr);
1861 usb_clear((EP0_SENT_STALL | EP0_SEND_STALL), USB_EP0_CSR);
1862 nuke(ep, -ECONNABORTED);
1863 dev->ep0state = WAIT_FOR_SETUP;
1868 * if a transfer is in progress && IN_PKT_RDY and OUT_PKT_RDY are clear
1871 * - set IN_PKT_RDY | DATA_END
1875 if (!(csr & (EP0_IN_PKT_RDY | EP0_OUT_PKT_RDY))) {
1876 DEBUG_EP0("%s: IN_PKT_RDY and OUT_PKT_RDY are clear\n",
1879 switch (dev->ep0state) {
1880 case DATA_STATE_XMIT:
1881 DEBUG_EP0("continue with DATA_STATE_XMIT\n");
1882 lh7a40x_ep0_in(dev, csr);
1884 case DATA_STATE_NEED_ZLP:
1885 DEBUG_EP0("continue with DATA_STATE_NEED_ZLP\n");
1886 lh7a40x_ep0_in_zlp(dev, csr);
1890 DEBUG_EP0("Odd state!! state = %s\n",
1891 state_names[dev->ep0state]);
1892 dev->ep0state = WAIT_FOR_SETUP;
1894 /* usb_set(EP0_SEND_STALL, ep->csr1); */
1900 * if SETUP_END is set
1901 * - abort the last transfer
1902 * - set SERVICED_SETUP_END_BIT
1904 if (csr & EP0_SETUP_END) {
1905 DEBUG_EP0("%s: EP0_SETUP_END is set: %x\n", __FUNCTION__, csr);
1907 usb_set(EP0_CLR_SETUP_END, USB_EP0_CSR);
1910 dev->ep0state = WAIT_FOR_SETUP;
1914 * if EP0_OUT_PKT_RDY is set
1915 * - read data packet from EP0 FIFO
1918 * set SERVICED_OUT_PKT_RDY | DATA_END bits | SEND_STALL
1920 * set SERVICED_OUT_PKT_RDY | DATA_END bits
1922 if (csr & EP0_OUT_PKT_RDY) {
1924 DEBUG_EP0("%s: EP0_OUT_PKT_RDY is set: %x\n", __FUNCTION__,
1927 switch (dev->ep0state) {
1928 case WAIT_FOR_SETUP:
1929 DEBUG_EP0("WAIT_FOR_SETUP\n");
1930 lh7a40x_ep0_setup(dev, csr);
1933 case DATA_STATE_RECV:
1934 DEBUG_EP0("DATA_STATE_RECV\n");
1935 lh7a40x_ep0_out(dev, csr);
1940 DEBUG_EP0("strange state!! 2. send stall? state = %d\n",
1947 static void lh7a40x_ep0_kick(struct lh7a40x_udc *dev, struct lh7a40x_ep *ep)
1952 csr = usb_read(USB_EP0_CSR);
1954 DEBUG_EP0("%s: %x\n", __FUNCTION__, csr);
1956 /* Clear "out packet ready" */
1957 usb_set(EP0_CLR_OUT, USB_EP0_CSR);
1960 dev->ep0state = DATA_STATE_XMIT;
1961 lh7a40x_ep0_in(dev, csr);
1963 dev->ep0state = DATA_STATE_RECV;
1964 lh7a40x_ep0_out(dev, csr);
1968 /* ---------------------------------------------------------------------------
1969 * device-scoped parts of the api to the usb controller hardware
1970 * ---------------------------------------------------------------------------
1973 static int lh7a40x_udc_get_frame(struct usb_gadget *_gadget)
1975 u32 frame1 = usb_read(USB_FRM_NUM1); /* Least significant 8 bits */
1976 u32 frame2 = usb_read(USB_FRM_NUM2); /* Most significant 3 bits */
1977 DEBUG("%s, %p\n", __FUNCTION__, _gadget);
1978 return ((frame2 & 0x07) << 8) | (frame1 & 0xff);
1981 static int lh7a40x_udc_wakeup(struct usb_gadget *_gadget)
1983 /* host may not have enabled remote wakeup */
1984 /*if ((UDCCS0 & UDCCS0_DRWF) == 0)
1985 return -EHOSTUNREACH;
1986 udc_set_mask_UDCCR(UDCCR_RSM); */
1990 static const struct usb_gadget_ops lh7a40x_udc_ops = {
1991 .get_frame = lh7a40x_udc_get_frame,
1992 .wakeup = lh7a40x_udc_wakeup,
1993 /* current versions must always be self-powered */
1996 static void nop_release(struct device *dev)
1998 DEBUG("%s %s\n", __FUNCTION__, dev->bus_id);
2001 static struct lh7a40x_udc memory = {
2005 .ops = &lh7a40x_udc_ops,
2006 .ep0 = &memory.ep[0].ep,
2007 .name = driver_name,
2010 .release = nop_release,
2014 /* control endpoint */
2018 .ops = &lh7a40x_ep_ops,
2019 .maxpacket = EP0_PACKETSIZE,
2023 .bEndpointAddress = 0,
2026 .ep_type = ep_control,
2027 .fifo = io_p2v(USB_EP0_FIFO),
2028 .csr1 = USB_EP0_CSR,
2029 .csr2 = USB_EP0_CSR,
2032 /* first group of endpoints */
2035 .name = "ep1in-bulk",
2036 .ops = &lh7a40x_ep_ops,
2041 .bEndpointAddress = USB_DIR_IN | 1,
2042 .bmAttributes = USB_ENDPOINT_XFER_BULK,
2044 .ep_type = ep_bulk_in,
2045 .fifo = io_p2v(USB_EP1_FIFO),
2046 .csr1 = USB_IN_CSR1,
2047 .csr2 = USB_IN_CSR2,
2052 .name = "ep2out-bulk",
2053 .ops = &lh7a40x_ep_ops,
2058 .bEndpointAddress = 2,
2059 .bmAttributes = USB_ENDPOINT_XFER_BULK,
2061 .ep_type = ep_bulk_out,
2062 .fifo = io_p2v(USB_EP2_FIFO),
2063 .csr1 = USB_OUT_CSR1,
2064 .csr2 = USB_OUT_CSR2,
2069 .name = "ep3in-int",
2070 .ops = &lh7a40x_ep_ops,
2075 .bEndpointAddress = USB_DIR_IN | 3,
2076 .bmAttributes = USB_ENDPOINT_XFER_INT,
2078 .ep_type = ep_interrupt,
2079 .fifo = io_p2v(USB_EP3_FIFO),
2080 .csr1 = USB_IN_CSR1,
2081 .csr2 = USB_IN_CSR2,
2086 * probe - binds to the platform device
2088 static int lh7a40x_udc_probe(struct platform_device *pdev)
2090 struct lh7a40x_udc *dev = &memory;
2093 DEBUG("%s: %p\n", __FUNCTION__, pdev);
2095 spin_lock_init(&dev->lock);
2096 dev->dev = &pdev->dev;
2098 device_initialize(&dev->gadget.dev);
2099 dev->gadget.dev.parent = &pdev->dev;
2101 the_controller = dev;
2102 platform_set_drvdata(pdev, dev);
2107 /* irq setup after old hardware state is cleaned up */
2109 request_irq(IRQ_USBINTR, lh7a40x_udc_irq, SA_INTERRUPT, driver_name,
2112 DEBUG(KERN_ERR "%s: can't get irq %i, err %d\n", driver_name,
2113 IRQ_USBINTR, retval);
2117 create_proc_files();
2122 static int lh7a40x_udc_remove(struct platform_device *pdev)
2124 struct lh7a40x_udc *dev = platform_get_drvdata(pdev);
2126 DEBUG("%s: %p\n", __FUNCTION__, pdev);
2129 remove_proc_files();
2130 usb_gadget_unregister_driver(dev->driver);
2132 free_irq(IRQ_USBINTR, dev);
2134 platform_set_drvdata(pdev, 0);
2141 /*-------------------------------------------------------------------------*/
2143 static struct platform_driver udc_driver = {
2144 .probe = lh7a40x_udc_probe,
2145 .remove = lh7a40x_udc_remove
2146 /* FIXME power management support */
2147 /* .suspend = ... disable UDC */
2148 /* .resume = ... re-enable UDC */
2150 .name = (char *)driver_name,
2151 .owner = THIS_MODULE,
2155 static int __init udc_init(void)
2157 DEBUG("%s: %s version %s\n", __FUNCTION__, driver_name, DRIVER_VERSION);
2158 return platform_driver_register(&udc_driver);
2161 static void __exit udc_exit(void)
2163 platform_driver_unregister(&udc_driver);
2166 module_init(udc_init);
2167 module_exit(udc_exit);
2169 MODULE_DESCRIPTION(DRIVER_DESC);
2170 MODULE_AUTHOR("Mikko Lahteenmaki, Bo Henriksen");
2171 MODULE_LICENSE("GPL");