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 "lh7a40x_udc.h"
26 //#define DEBUG printk
27 //#define DEBUG_EP0 printk
28 //#define DEBUG_SETUP printk
31 # define DEBUG_EP0(fmt,args...)
34 # define DEBUG_SETUP(fmt,args...)
38 # define DEBUG(fmt,args...)
41 #define DRIVER_DESC "LH7A40x USB Device Controller"
42 #define DRIVER_VERSION __DATE__
44 #ifndef _BIT /* FIXME - what happended to _BIT in 2.6.7bk18? */
45 #define _BIT(x) (1<<(x))
48 struct lh7a40x_udc *the_controller;
50 static const char driver_name[] = "lh7a40x_udc";
51 static const char driver_desc[] = DRIVER_DESC;
52 static const char ep0name[] = "ep0-control";
59 static char *state_names[] = {
62 "DATA_STATE_NEED_ZLP",
63 "WAIT_FOR_OUT_STATUS",
71 static int lh7a40x_ep_enable(struct usb_ep *ep,
72 const struct usb_endpoint_descriptor *);
73 static int lh7a40x_ep_disable(struct usb_ep *ep);
74 static struct usb_request *lh7a40x_alloc_request(struct usb_ep *ep, gfp_t);
75 static void lh7a40x_free_request(struct usb_ep *ep, struct usb_request *);
76 static void *lh7a40x_alloc_buffer(struct usb_ep *ep, unsigned, dma_addr_t *,
78 static void lh7a40x_free_buffer(struct usb_ep *ep, void *, dma_addr_t,
80 static int lh7a40x_queue(struct usb_ep *ep, struct usb_request *, gfp_t);
81 static int lh7a40x_dequeue(struct usb_ep *ep, struct usb_request *);
82 static int lh7a40x_set_halt(struct usb_ep *ep, int);
83 static int lh7a40x_fifo_status(struct usb_ep *ep);
84 static int lh7a40x_fifo_status(struct usb_ep *ep);
85 static void lh7a40x_fifo_flush(struct usb_ep *ep);
86 static void lh7a40x_ep0_kick(struct lh7a40x_udc *dev, struct lh7a40x_ep *ep);
87 static void lh7a40x_handle_ep0(struct lh7a40x_udc *dev, u32 intr);
89 static void done(struct lh7a40x_ep *ep, struct lh7a40x_request *req,
91 static void pio_irq_enable(int bEndpointAddress);
92 static void pio_irq_disable(int bEndpointAddress);
93 static void stop_activity(struct lh7a40x_udc *dev,
94 struct usb_gadget_driver *driver);
95 static void flush(struct lh7a40x_ep *ep);
96 static void udc_enable(struct lh7a40x_udc *dev);
97 static void udc_set_address(struct lh7a40x_udc *dev, unsigned char address);
99 static struct usb_ep_ops lh7a40x_ep_ops = {
100 .enable = lh7a40x_ep_enable,
101 .disable = lh7a40x_ep_disable,
103 .alloc_request = lh7a40x_alloc_request,
104 .free_request = lh7a40x_free_request,
106 .alloc_buffer = lh7a40x_alloc_buffer,
107 .free_buffer = lh7a40x_free_buffer,
109 .queue = lh7a40x_queue,
110 .dequeue = lh7a40x_dequeue,
112 .set_halt = lh7a40x_set_halt,
113 .fifo_status = lh7a40x_fifo_status,
114 .fifo_flush = lh7a40x_fifo_flush,
119 static __inline__ int write_packet(struct lh7a40x_ep *ep,
120 struct lh7a40x_request *req, int max)
124 volatile u32 *fifo = (volatile u32 *)ep->fifo;
126 buf = req->req.buf + req->req.actual;
129 length = req->req.length - req->req.actual;
130 length = min(length, max);
131 req->req.actual += length;
133 DEBUG("Write %d (max %d), fifo %p\n", length, max, fifo);
143 static __inline__ void usb_set_index(u32 ep)
145 *(volatile u32 *)io_p2v(USB_INDEX) = ep;
148 static __inline__ u32 usb_read(u32 port)
150 return *(volatile u32 *)io_p2v(port);
153 static __inline__ void usb_write(u32 val, u32 port)
155 *(volatile u32 *)io_p2v(port) = val;
158 static __inline__ void usb_set(u32 val, u32 port)
160 volatile u32 *ioport = (volatile u32 *)io_p2v(port);
161 u32 after = (*ioport) | val;
165 static __inline__ void usb_clear(u32 val, u32 port)
167 volatile u32 *ioport = (volatile u32 *)io_p2v(port);
168 u32 after = (*ioport) & ~val;
172 /*-------------------------------------------------------------------------*/
174 #define GPIO_PORTC_DR (0x80000E08)
175 #define GPIO_PORTC_DDR (0x80000E18)
176 #define GPIO_PORTC_PDR (0x80000E70)
178 /* get port C pin data register */
179 #define get_portc_pdr(bit) ((usb_read(GPIO_PORTC_PDR) & _BIT(bit)) != 0)
180 /* get port C data direction register */
181 #define get_portc_ddr(bit) ((usb_read(GPIO_PORTC_DDR) & _BIT(bit)) != 0)
182 /* set port C data register */
183 #define set_portc_dr(bit, val) (val ? usb_set(_BIT(bit), GPIO_PORTC_DR) : usb_clear(_BIT(bit), GPIO_PORTC_DR))
184 /* set port C data direction register */
185 #define set_portc_ddr(bit, val) (val ? usb_set(_BIT(bit), GPIO_PORTC_DDR) : usb_clear(_BIT(bit), GPIO_PORTC_DDR))
189 * Port C bit 1 = USB Port 1 Power Enable
190 * Port C bit 2 = USB Port 1 Data Carrier Detect
192 #define is_usb_connected() get_portc_pdr(2)
194 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
196 static const char proc_node_name[] = "driver/udc";
199 udc_proc_read(char *page, char **start, off_t off, int count,
200 int *eof, void *_dev)
203 struct lh7a40x_udc *dev = _dev;
205 unsigned size = count;
212 local_irq_save(flags);
214 /* basic device status */
215 t = scnprintf(next, size,
218 "Gadget driver: %s\n"
220 driver_name, DRIVER_VERSION,
221 dev->driver ? dev->driver->driver.name : "(none)",
222 is_usb_connected()? "full speed" : "disconnected");
226 t = scnprintf(next, size,
228 " Port C bit 1: %d, dir %d\n"
229 " Port C bit 2: %d, dir %d\n\n",
230 get_portc_pdr(1), get_portc_ddr(1),
231 get_portc_pdr(2), get_portc_ddr(2)
236 t = scnprintf(next, size,
237 "DCP pullup: %d\n\n",
238 (usb_read(USB_PM) & PM_USB_DCP) != 0);
242 local_irq_restore(flags);
247 #define create_proc_files() create_proc_read_entry(proc_node_name, 0, NULL, udc_proc_read, dev)
248 #define remove_proc_files() remove_proc_entry(proc_node_name, NULL)
250 #else /* !CONFIG_USB_GADGET_DEBUG_FILES */
252 #define create_proc_files() do {} while (0)
253 #define remove_proc_files() do {} while (0)
255 #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
258 * udc_disable - disable USB device controller
260 static void udc_disable(struct lh7a40x_udc *dev)
262 DEBUG("%s, %p\n", __FUNCTION__, dev);
264 udc_set_address(dev, 0);
266 /* Disable interrupts */
267 usb_write(0, USB_IN_INT_EN);
268 usb_write(0, USB_OUT_INT_EN);
269 usb_write(0, USB_INT_EN);
271 /* Disable the USB */
272 usb_write(0, USB_PM);
274 #ifdef CONFIG_ARCH_LH7A404
275 /* Disable USB power */
279 /* if hardware supports it, disconnect from usb */
280 /* make_usb_disappear(); */
282 dev->ep0state = WAIT_FOR_SETUP;
283 dev->gadget.speed = USB_SPEED_UNKNOWN;
284 dev->usb_address = 0;
288 * udc_reinit - initialize software state
290 static void udc_reinit(struct lh7a40x_udc *dev)
294 DEBUG("%s, %p\n", __FUNCTION__, dev);
296 /* device/ep0 records init */
297 INIT_LIST_HEAD(&dev->gadget.ep_list);
298 INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
299 dev->ep0state = WAIT_FOR_SETUP;
301 /* basic endpoint records init */
302 for (i = 0; i < UDC_MAX_ENDPOINTS; i++) {
303 struct lh7a40x_ep *ep = &dev->ep[i];
306 list_add_tail(&ep->ep.ep_list, &dev->gadget.ep_list);
310 INIT_LIST_HEAD(&ep->queue);
314 /* the rest was statically initialized, and is read-only */
317 #define BYTES2MAXP(x) (x / 8)
318 #define MAXP2BYTES(x) (x * 8)
320 /* until it's enabled, this UDC should be completely invisible
323 static void udc_enable(struct lh7a40x_udc *dev)
327 DEBUG("%s, %p\n", __FUNCTION__, dev);
329 dev->gadget.speed = USB_SPEED_UNKNOWN;
331 #ifdef CONFIG_ARCH_LH7A404
332 /* Set Port C bit 1 & 2 as output */
336 /* Enable USB power */
341 * C.f Chapter 18.1.3.1 Initializing the USB
344 /* Disable the USB */
345 usb_clear(PM_USB_ENABLE, USB_PM);
347 /* Reset APB & I/O sides of the USB */
348 usb_set(USB_RESET_APB | USB_RESET_IO, USB_RESET);
350 usb_clear(USB_RESET_APB | USB_RESET_IO, USB_RESET);
352 /* Set MAXP values for each */
353 for (ep = 0; ep < UDC_MAX_ENDPOINTS; ep++) {
354 struct lh7a40x_ep *ep_reg = &dev->ep[ep];
359 switch (ep_reg->ep_type) {
362 usb_clear(USB_IN_CSR2_USB_DMA_EN | USB_IN_CSR2_AUTO_SET,
366 usb_write(BYTES2MAXP(ep_maxpacket(ep_reg)),
370 usb_clear(USB_OUT_CSR2_USB_DMA_EN |
371 USB_OUT_CSR2_AUTO_CLR, ep_reg->csr2);
372 usb_write(BYTES2MAXP(ep_maxpacket(ep_reg)),
377 /* Read & Write CSR1, just in case */
378 csr = usb_read(ep_reg->csr1);
379 usb_write(csr, ep_reg->csr1);
384 /* Disable interrupts */
385 usb_write(0, USB_IN_INT_EN);
386 usb_write(0, USB_OUT_INT_EN);
387 usb_write(0, USB_INT_EN);
389 /* Enable interrupts */
390 usb_set(USB_IN_INT_EP0, USB_IN_INT_EN);
391 usb_set(USB_INT_RESET_INT | USB_INT_RESUME_INT, USB_INT_EN);
392 /* Dont enable rest of the interrupts */
393 /* usb_set(USB_IN_INT_EP3 | USB_IN_INT_EP1 | USB_IN_INT_EP0, USB_IN_INT_EN);
394 usb_set(USB_OUT_INT_EP2, USB_OUT_INT_EN); */
397 usb_set(PM_ENABLE_SUSPEND, USB_PM);
400 usb_set(PM_USB_ENABLE, USB_PM);
402 #ifdef CONFIG_ARCH_LH7A404
403 /* NOTE: DOES NOT WORK! */
404 /* Let host detect UDC:
405 * Software must write a 0 to the PMR:DCP_CTRL bit to turn this
406 * transistor on and pull the USBDP pin HIGH.
408 /* usb_clear(PM_USB_DCP, USB_PM);
409 usb_set(PM_USB_DCP, USB_PM); */
414 Register entry point for the peripheral controller driver.
416 int usb_gadget_register_driver(struct usb_gadget_driver *driver)
418 struct lh7a40x_udc *dev = the_controller;
421 DEBUG("%s: %s\n", __FUNCTION__, driver->driver.name);
424 || driver->speed != USB_SPEED_FULL
426 || !driver->unbind || !driver->disconnect || !driver->setup)
433 /* first hook up the driver ... */
434 dev->driver = driver;
435 dev->gadget.dev.driver = &driver->driver;
437 device_add(&dev->gadget.dev);
438 retval = driver->bind(&dev->gadget);
440 printk("%s: bind to driver %s --> error %d\n", dev->gadget.name,
441 driver->driver.name, retval);
442 device_del(&dev->gadget.dev);
445 dev->gadget.dev.driver = 0;
449 /* ... then enable host detection and ep0; and we're ready
450 * for set_configuration as well as eventual disconnect.
451 * NOTE: this shouldn't power up until later.
453 printk("%s: registered gadget driver '%s'\n", dev->gadget.name,
454 driver->driver.name);
461 EXPORT_SYMBOL(usb_gadget_register_driver);
464 Unregister entry point for the peripheral controller driver.
466 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
468 struct lh7a40x_udc *dev = the_controller;
473 if (!driver || driver != dev->driver)
476 spin_lock_irqsave(&dev->lock, flags);
478 stop_activity(dev, driver);
479 spin_unlock_irqrestore(&dev->lock, flags);
481 driver->unbind(&dev->gadget);
482 device_del(&dev->gadget.dev);
486 DEBUG("unregistered gadget driver '%s'\n", driver->driver.name);
490 EXPORT_SYMBOL(usb_gadget_unregister_driver);
492 /*-------------------------------------------------------------------------*/
494 /** Write request to FIFO (max write == maxp size)
495 * Return: 0 = still running, 1 = completed, negative = errno
496 * NOTE: INDEX register must be set for EP
498 static int write_fifo(struct lh7a40x_ep *ep, struct lh7a40x_request *req)
503 max = le16_to_cpu(ep->desc->wMaxPacketSize);
505 csr = usb_read(ep->csr1);
506 DEBUG("CSR: %x %d\n", csr, csr & USB_IN_CSR1_FIFO_NOT_EMPTY);
508 if (!(csr & USB_IN_CSR1_FIFO_NOT_EMPTY)) {
510 int is_last, is_short;
512 count = write_packet(ep, req, max);
513 usb_set(USB_IN_CSR1_IN_PKT_RDY, ep->csr1);
515 /* last packet is usually short (or a zlp) */
516 if (unlikely(count != max))
517 is_last = is_short = 1;
519 if (likely(req->req.length != req->req.actual)
524 /* interrupt/iso maxpacket may not fill the fifo */
525 is_short = unlikely(max < ep_maxpacket(ep));
528 DEBUG("%s: wrote %s %d bytes%s%s %d left %p\n", __FUNCTION__,
530 is_last ? "/L" : "", is_short ? "/S" : "",
531 req->req.length - req->req.actual, req);
533 /* requests complete when all IN data is in the FIFO */
536 if (list_empty(&ep->queue)) {
537 pio_irq_disable(ep_index(ep));
542 DEBUG("Hmm.. %d ep FIFO is not empty!\n", ep_index(ep));
548 /** Read to request from FIFO (max read == bytes in fifo)
549 * Return: 0 = still running, 1 = completed, negative = errno
550 * NOTE: INDEX register must be set for EP
552 static int read_fifo(struct lh7a40x_ep *ep, struct lh7a40x_request *req)
556 unsigned bufferspace, count, is_short;
557 volatile u32 *fifo = (volatile u32 *)ep->fifo;
559 /* make sure there's a packet in the FIFO. */
560 csr = usb_read(ep->csr1);
561 if (!(csr & USB_OUT_CSR1_OUT_PKT_RDY)) {
562 DEBUG("%s: Packet NOT ready!\n", __FUNCTION__);
566 buf = req->req.buf + req->req.actual;
568 bufferspace = req->req.length - req->req.actual;
570 /* read all bytes from this packet */
571 count = usb_read(USB_OUT_FIFO_WC1);
572 req->req.actual += min(count, bufferspace);
574 is_short = (count < ep->ep.maxpacket);
575 DEBUG("read %s %02x, %d bytes%s req %p %d/%d\n",
576 ep->ep.name, csr, count,
577 is_short ? "/S" : "", req, req->req.actual, req->req.length);
579 while (likely(count-- != 0)) {
580 u8 byte = (u8) (*fifo & 0xff);
582 if (unlikely(bufferspace == 0)) {
583 /* this happens when the driver's buffer
584 * is smaller than what the host sent.
585 * discard the extra data.
587 if (req->req.status != -EOVERFLOW)
588 printk("%s overflow %d\n", ep->ep.name, count);
589 req->req.status = -EOVERFLOW;
596 usb_clear(USB_OUT_CSR1_OUT_PKT_RDY, ep->csr1);
599 if (is_short || req->req.actual == req->req.length) {
601 usb_set(USB_OUT_CSR1_FIFO_FLUSH, ep->csr1);
603 if (list_empty(&ep->queue))
604 pio_irq_disable(ep_index(ep));
608 /* finished that packet. the next one may be waiting... */
613 * done - retire a request; caller blocked irqs
614 * INDEX register is preserved to keep same
616 static void done(struct lh7a40x_ep *ep, struct lh7a40x_request *req, int status)
618 unsigned int stopped = ep->stopped;
621 DEBUG("%s, %p\n", __FUNCTION__, ep);
622 list_del_init(&req->queue);
624 if (likely(req->req.status == -EINPROGRESS))
625 req->req.status = status;
627 status = req->req.status;
629 if (status && status != -ESHUTDOWN)
630 DEBUG("complete %s req %p stat %d len %u/%u\n",
631 ep->ep.name, &req->req, status,
632 req->req.actual, req->req.length);
634 /* don't modify queue heads during completion callback */
636 /* Read current index (completion may modify it) */
637 index = usb_read(USB_INDEX);
639 spin_unlock(&ep->dev->lock);
640 req->req.complete(&ep->ep, &req->req);
641 spin_lock(&ep->dev->lock);
644 usb_set_index(index);
645 ep->stopped = stopped;
648 /** Enable EP interrupt */
649 static void pio_irq_enable(int ep)
651 DEBUG("%s: %d\n", __FUNCTION__, ep);
655 usb_set(USB_IN_INT_EP1, USB_IN_INT_EN);
658 usb_set(USB_OUT_INT_EP2, USB_OUT_INT_EN);
661 usb_set(USB_IN_INT_EP3, USB_IN_INT_EN);
664 DEBUG("Unknown endpoint: %d\n", ep);
669 /** Disable EP interrupt */
670 static void pio_irq_disable(int ep)
672 DEBUG("%s: %d\n", __FUNCTION__, ep);
676 usb_clear(USB_IN_INT_EP1, USB_IN_INT_EN);
679 usb_clear(USB_OUT_INT_EP2, USB_OUT_INT_EN);
682 usb_clear(USB_IN_INT_EP3, USB_IN_INT_EN);
685 DEBUG("Unknown endpoint: %d\n", ep);
691 * nuke - dequeue ALL requests
693 void nuke(struct lh7a40x_ep *ep, int status)
695 struct lh7a40x_request *req;
697 DEBUG("%s, %p\n", __FUNCTION__, ep);
702 /* called with irqs blocked */
703 while (!list_empty(&ep->queue)) {
704 req = list_entry(ep->queue.next, struct lh7a40x_request, queue);
705 done(ep, req, status);
708 /* Disable IRQ if EP is enabled (has descriptor) */
710 pio_irq_disable(ep_index(ep));
714 void nuke_all(struct lh7a40x_udc *dev)
717 for(n=0; n<UDC_MAX_ENDPOINTS; n++) {
718 struct lh7a40x_ep *ep = &dev->ep[n];
725 static void flush_all(struct lh7a40x_udc *dev)
728 for (n = 0; n < UDC_MAX_ENDPOINTS; n++)
730 struct lh7a40x_ep *ep = &dev->ep[n];
737 * NOTE: INDEX register must be set before this call
739 static void flush(struct lh7a40x_ep *ep)
741 DEBUG("%s, %p\n", __FUNCTION__, ep);
743 switch (ep->ep_type) {
745 /* check, by implication c.f. 15.1.2.11 */
750 /* if(csr & USB_IN_CSR1_IN_PKT_RDY) */
751 usb_set(USB_IN_CSR1_FIFO_FLUSH, ep->csr1);
755 /* if(csr & USB_OUT_CSR1_OUT_PKT_RDY) */
756 usb_set(USB_OUT_CSR1_FIFO_FLUSH, ep->csr1);
762 * lh7a40x_in_epn - handle IN interrupt
764 static void lh7a40x_in_epn(struct lh7a40x_udc *dev, u32 ep_idx, u32 intr)
767 struct lh7a40x_ep *ep = &dev->ep[ep_idx];
768 struct lh7a40x_request *req;
770 usb_set_index(ep_idx);
772 csr = usb_read(ep->csr1);
773 DEBUG("%s: %d, csr %x\n", __FUNCTION__, ep_idx, csr);
775 if (csr & USB_IN_CSR1_SENT_STALL) {
776 DEBUG("USB_IN_CSR1_SENT_STALL\n");
777 usb_set(USB_IN_CSR1_SENT_STALL /*|USB_IN_CSR1_SEND_STALL */ ,
783 DEBUG("%s: NO EP DESC\n", __FUNCTION__);
787 if (list_empty(&ep->queue))
790 req = list_entry(ep->queue.next, struct lh7a40x_request, queue);
792 DEBUG("req: %p\n", req);
800 /* ********************************************************************************************* */
804 static void lh7a40x_out_epn(struct lh7a40x_udc *dev, u32 ep_idx, u32 intr)
806 struct lh7a40x_ep *ep = &dev->ep[ep_idx];
807 struct lh7a40x_request *req;
809 DEBUG("%s: %d\n", __FUNCTION__, ep_idx);
811 usb_set_index(ep_idx);
815 csr = usb_read(ep->csr1);
819 csr1)) & (USB_OUT_CSR1_OUT_PKT_RDY |
820 USB_OUT_CSR1_SENT_STALL)) {
821 DEBUG("%s: %x\n", __FUNCTION__, csr);
823 if (csr & USB_OUT_CSR1_SENT_STALL) {
824 DEBUG("%s: stall sent, flush fifo\n",
826 /* usb_set(USB_OUT_CSR1_FIFO_FLUSH, ep->csr1); */
828 } else if (csr & USB_OUT_CSR1_OUT_PKT_RDY) {
829 if (list_empty(&ep->queue))
833 list_entry(ep->queue.next,
834 struct lh7a40x_request,
838 printk("%s: NULL REQ %d\n",
839 __FUNCTION__, ep_idx);
850 /* Throw packet away.. */
851 printk("%s: No descriptor?!?\n", __FUNCTION__);
856 static void stop_activity(struct lh7a40x_udc *dev,
857 struct usb_gadget_driver *driver)
861 /* don't disconnect drivers more than once */
862 if (dev->gadget.speed == USB_SPEED_UNKNOWN)
864 dev->gadget.speed = USB_SPEED_UNKNOWN;
866 /* prevent new request submissions, kill any outstanding requests */
867 for (i = 0; i < UDC_MAX_ENDPOINTS; i++) {
868 struct lh7a40x_ep *ep = &dev->ep[i];
872 nuke(ep, -ESHUTDOWN);
875 /* report disconnect; the driver is already quiesced */
877 spin_unlock(&dev->lock);
878 driver->disconnect(&dev->gadget);
879 spin_lock(&dev->lock);
882 /* re-init driver-visible data structures */
886 /** Handle USB RESET interrupt
888 static void lh7a40x_reset_intr(struct lh7a40x_udc *dev)
890 #if 0 /* def CONFIG_ARCH_LH7A404 */
891 /* Does not work always... */
893 DEBUG("%s: %d\n", __FUNCTION__, dev->usb_address);
895 if (!dev->usb_address) {
896 /*usb_set(USB_RESET_IO, USB_RESET);
898 usb_clear(USB_RESET_IO, USB_RESET); */
901 /* Put the USB controller into reset. */
902 usb_set(USB_RESET_IO, USB_RESET);
904 /* Set Device ID to 0 */
905 udc_set_address(dev, 0);
907 /* Let PLL2 settle down */
910 /* Release the USB controller from reset */
911 usb_clear(USB_RESET_IO, USB_RESET);
917 dev->gadget.speed = USB_SPEED_FULL;
921 * lh7a40x usb client interrupt handler.
923 static irqreturn_t lh7a40x_udc_irq(int irq, void *_dev, struct pt_regs *r)
925 struct lh7a40x_udc *dev = _dev;
929 spin_lock(&dev->lock);
932 u32 intr_in = usb_read(USB_IN_INT);
933 u32 intr_out = usb_read(USB_OUT_INT);
934 u32 intr_int = usb_read(USB_INT);
936 /* Test also against enable bits.. (lh7a40x errata).. Sigh.. */
937 u32 in_en = usb_read(USB_IN_INT_EN);
938 u32 out_en = usb_read(USB_OUT_INT_EN);
940 if (!intr_out && !intr_in && !intr_int)
943 DEBUG("%s (on state %s)\n", __FUNCTION__,
944 state_names[dev->ep0state]);
945 DEBUG("intr_out = %x\n", intr_out);
946 DEBUG("intr_in = %x\n", intr_in);
947 DEBUG("intr_int = %x\n", intr_int);
950 usb_write(intr_in, USB_IN_INT);
952 if ((intr_in & USB_IN_INT_EP1)
953 && (in_en & USB_IN_INT_EP1)) {
954 DEBUG("USB_IN_INT_EP1\n");
955 lh7a40x_in_epn(dev, 1, intr_in);
957 if ((intr_in & USB_IN_INT_EP3)
958 && (in_en & USB_IN_INT_EP3)) {
959 DEBUG("USB_IN_INT_EP3\n");
960 lh7a40x_in_epn(dev, 3, intr_in);
962 if (intr_in & USB_IN_INT_EP0) {
963 DEBUG("USB_IN_INT_EP0 (control)\n");
964 lh7a40x_handle_ep0(dev, intr_in);
969 usb_write(intr_out, USB_OUT_INT);
971 if ((intr_out & USB_OUT_INT_EP2)
972 && (out_en & USB_OUT_INT_EP2)) {
973 DEBUG("USB_OUT_INT_EP2\n");
974 lh7a40x_out_epn(dev, 2, intr_out);
979 usb_write(intr_int, USB_INT);
981 if (intr_int & USB_INT_RESET_INT) {
982 lh7a40x_reset_intr(dev);
985 if (intr_int & USB_INT_RESUME_INT) {
986 DEBUG("USB resume\n");
988 if (dev->gadget.speed != USB_SPEED_UNKNOWN
990 && dev->driver->resume
991 && is_usb_connected()) {
992 dev->driver->resume(&dev->gadget);
996 if (intr_int & USB_INT_SUSPEND_INT) {
997 DEBUG("USB suspend%s\n",
998 is_usb_connected()? "" : "+disconnect");
999 if (!is_usb_connected()) {
1000 stop_activity(dev, dev->driver);
1001 } else if (dev->gadget.speed !=
1002 USB_SPEED_UNKNOWN && dev->driver
1003 && dev->driver->suspend) {
1004 dev->driver->suspend(&dev->gadget);
1011 spin_unlock(&dev->lock);
1016 static int lh7a40x_ep_enable(struct usb_ep *_ep,
1017 const struct usb_endpoint_descriptor *desc)
1019 struct lh7a40x_ep *ep;
1020 struct lh7a40x_udc *dev;
1021 unsigned long flags;
1023 DEBUG("%s, %p\n", __FUNCTION__, _ep);
1025 ep = container_of(_ep, struct lh7a40x_ep, ep);
1026 if (!_ep || !desc || ep->desc || _ep->name == ep0name
1027 || desc->bDescriptorType != USB_DT_ENDPOINT
1028 || ep->bEndpointAddress != desc->bEndpointAddress
1029 || ep_maxpacket(ep) < le16_to_cpu(desc->wMaxPacketSize)) {
1030 DEBUG("%s, bad ep or descriptor\n", __FUNCTION__);
1034 /* xfer types must match, except that interrupt ~= bulk */
1035 if (ep->bmAttributes != desc->bmAttributes
1036 && ep->bmAttributes != USB_ENDPOINT_XFER_BULK
1037 && desc->bmAttributes != USB_ENDPOINT_XFER_INT) {
1038 DEBUG("%s, %s type mismatch\n", __FUNCTION__, _ep->name);
1042 /* hardware _could_ do smaller, but driver doesn't */
1043 if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK
1044 && le16_to_cpu(desc->wMaxPacketSize) != ep_maxpacket(ep))
1045 || !desc->wMaxPacketSize) {
1046 DEBUG("%s, bad %s maxpacket\n", __FUNCTION__, _ep->name);
1051 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
1052 DEBUG("%s, bogus device state\n", __FUNCTION__);
1056 spin_lock_irqsave(&ep->dev->lock, flags);
1061 ep->ep.maxpacket = le16_to_cpu(desc->wMaxPacketSize);
1063 /* Reset halt state (does flush) */
1064 lh7a40x_set_halt(_ep, 0);
1066 spin_unlock_irqrestore(&ep->dev->lock, flags);
1068 DEBUG("%s: enabled %s\n", __FUNCTION__, _ep->name);
1073 * NOTE: Sets INDEX register
1075 static int lh7a40x_ep_disable(struct usb_ep *_ep)
1077 struct lh7a40x_ep *ep;
1078 unsigned long flags;
1080 DEBUG("%s, %p\n", __FUNCTION__, _ep);
1082 ep = container_of(_ep, struct lh7a40x_ep, ep);
1083 if (!_ep || !ep->desc) {
1084 DEBUG("%s, %s not enabled\n", __FUNCTION__,
1085 _ep ? ep->ep.name : NULL);
1089 spin_lock_irqsave(&ep->dev->lock, flags);
1091 usb_set_index(ep_index(ep));
1093 /* Nuke all pending requests (does flush) */
1094 nuke(ep, -ESHUTDOWN);
1096 /* Disable ep IRQ */
1097 pio_irq_disable(ep_index(ep));
1102 spin_unlock_irqrestore(&ep->dev->lock, flags);
1104 DEBUG("%s: disabled %s\n", __FUNCTION__, _ep->name);
1108 static struct usb_request *lh7a40x_alloc_request(struct usb_ep *ep,
1111 struct lh7a40x_request *req;
1113 DEBUG("%s, %p\n", __FUNCTION__, ep);
1115 req = kmalloc(sizeof *req, gfp_flags);
1119 memset(req, 0, sizeof *req);
1120 INIT_LIST_HEAD(&req->queue);
1125 static void lh7a40x_free_request(struct usb_ep *ep, struct usb_request *_req)
1127 struct lh7a40x_request *req;
1129 DEBUG("%s, %p\n", __FUNCTION__, ep);
1131 req = container_of(_req, struct lh7a40x_request, req);
1132 WARN_ON(!list_empty(&req->queue));
1136 static void *lh7a40x_alloc_buffer(struct usb_ep *ep, unsigned bytes,
1137 dma_addr_t * dma, gfp_t gfp_flags)
1141 DEBUG("%s (%p, %d, %d)\n", __FUNCTION__, ep, bytes, gfp_flags);
1143 retval = kmalloc(bytes, gfp_flags & ~(__GFP_DMA | __GFP_HIGHMEM));
1145 *dma = virt_to_bus(retval);
1149 static void lh7a40x_free_buffer(struct usb_ep *ep, void *buf, dma_addr_t dma,
1152 DEBUG("%s, %p\n", __FUNCTION__, ep);
1156 /** Queue one request
1157 * Kickstart transfer if needed
1158 * NOTE: Sets INDEX register
1160 static int lh7a40x_queue(struct usb_ep *_ep, struct usb_request *_req,
1163 struct lh7a40x_request *req;
1164 struct lh7a40x_ep *ep;
1165 struct lh7a40x_udc *dev;
1166 unsigned long flags;
1168 DEBUG("\n\n\n%s, %p\n", __FUNCTION__, _ep);
1170 req = container_of(_req, struct lh7a40x_request, req);
1172 (!_req || !_req->complete || !_req->buf
1173 || !list_empty(&req->queue))) {
1174 DEBUG("%s, bad params\n", __FUNCTION__);
1178 ep = container_of(_ep, struct lh7a40x_ep, ep);
1179 if (unlikely(!_ep || (!ep->desc && ep->ep.name != ep0name))) {
1180 DEBUG("%s, bad ep\n", __FUNCTION__);
1185 if (unlikely(!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)) {
1186 DEBUG("%s, bogus device state %p\n", __FUNCTION__, dev->driver);
1190 DEBUG("%s queue req %p, len %d buf %p\n", _ep->name, _req, _req->length,
1193 spin_lock_irqsave(&dev->lock, flags);
1195 _req->status = -EINPROGRESS;
1198 /* kickstart this i/o queue? */
1199 DEBUG("Add to %d Q %d %d\n", ep_index(ep), list_empty(&ep->queue),
1201 if (list_empty(&ep->queue) && likely(!ep->stopped)) {
1204 if (unlikely(ep_index(ep) == 0)) {
1206 list_add_tail(&req->queue, &ep->queue);
1207 lh7a40x_ep0_kick(dev, ep);
1209 } else if (ep_is_in(ep)) {
1211 usb_set_index(ep_index(ep));
1212 csr = usb_read(ep->csr1);
1213 pio_irq_enable(ep_index(ep));
1214 if ((csr & USB_IN_CSR1_FIFO_NOT_EMPTY) == 0) {
1215 if (write_fifo(ep, req) == 1)
1220 usb_set_index(ep_index(ep));
1221 csr = usb_read(ep->csr1);
1222 pio_irq_enable(ep_index(ep));
1223 if (!(csr & USB_OUT_CSR1_FIFO_FULL)) {
1224 if (read_fifo(ep, req) == 1)
1230 /* pio or dma irq handler advances the queue. */
1231 if (likely(req != 0))
1232 list_add_tail(&req->queue, &ep->queue);
1234 spin_unlock_irqrestore(&dev->lock, flags);
1239 /* dequeue JUST ONE request */
1240 static int lh7a40x_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1242 struct lh7a40x_ep *ep;
1243 struct lh7a40x_request *req;
1244 unsigned long flags;
1246 DEBUG("%s, %p\n", __FUNCTION__, _ep);
1248 ep = container_of(_ep, struct lh7a40x_ep, ep);
1249 if (!_ep || ep->ep.name == ep0name)
1252 spin_lock_irqsave(&ep->dev->lock, flags);
1254 /* make sure it's actually queued on this endpoint */
1255 list_for_each_entry(req, &ep->queue, queue) {
1256 if (&req->req == _req)
1259 if (&req->req != _req) {
1260 spin_unlock_irqrestore(&ep->dev->lock, flags);
1264 done(ep, req, -ECONNRESET);
1266 spin_unlock_irqrestore(&ep->dev->lock, flags);
1270 /** Halt specific EP
1271 * Return 0 if success
1272 * NOTE: Sets INDEX register to EP !
1274 static int lh7a40x_set_halt(struct usb_ep *_ep, int value)
1276 struct lh7a40x_ep *ep;
1277 unsigned long flags;
1279 ep = container_of(_ep, struct lh7a40x_ep, ep);
1280 if (unlikely(!_ep || (!ep->desc && ep->ep.name != ep0name))) {
1281 DEBUG("%s, bad ep\n", __FUNCTION__);
1285 usb_set_index(ep_index(ep));
1287 DEBUG("%s, ep %d, val %d\n", __FUNCTION__, ep_index(ep), value);
1289 spin_lock_irqsave(&ep->dev->lock, flags);
1291 if (ep_index(ep) == 0) {
1293 usb_set(EP0_SEND_STALL, ep->csr1);
1294 } else if (ep_is_in(ep)) {
1295 u32 csr = usb_read(ep->csr1);
1296 if (value && ((csr & USB_IN_CSR1_FIFO_NOT_EMPTY)
1297 || !list_empty(&ep->queue))) {
1299 * Attempts to halt IN endpoints will fail (returning -EAGAIN)
1300 * if any transfer requests are still queued, or if the controller
1301 * FIFO still holds bytes that the host hasn
\92t collected.
1303 spin_unlock_irqrestore(&ep->dev->lock, flags);
1305 ("Attempt to halt IN endpoint failed (returning -EAGAIN) %d %d\n",
1306 (csr & USB_IN_CSR1_FIFO_NOT_EMPTY),
1307 !list_empty(&ep->queue));
1312 usb_set(USB_IN_CSR1_SEND_STALL, ep->csr1);
1314 usb_clear(USB_IN_CSR1_SEND_STALL, ep->csr1);
1315 usb_set(USB_IN_CSR1_CLR_DATA_TOGGLE, ep->csr1);
1322 usb_set(USB_OUT_CSR1_SEND_STALL, ep->csr1);
1324 usb_clear(USB_OUT_CSR1_SEND_STALL, ep->csr1);
1325 usb_set(USB_OUT_CSR1_CLR_DATA_REG, ep->csr1);
1335 spin_unlock_irqrestore(&ep->dev->lock, flags);
1337 DEBUG("%s %s halted\n", _ep->name, value == 0 ? "NOT" : "IS");
1342 /** Return bytes in EP FIFO
1343 * NOTE: Sets INDEX register to EP
1345 static int lh7a40x_fifo_status(struct usb_ep *_ep)
1349 struct lh7a40x_ep *ep;
1351 ep = container_of(_ep, struct lh7a40x_ep, ep);
1353 DEBUG("%s, bad ep\n", __FUNCTION__);
1357 DEBUG("%s, %d\n", __FUNCTION__, ep_index(ep));
1359 /* LPD can't report unclaimed bytes from IN fifos */
1363 usb_set_index(ep_index(ep));
1365 csr = usb_read(ep->csr1);
1366 if (ep->dev->gadget.speed != USB_SPEED_UNKNOWN ||
1367 csr & USB_OUT_CSR1_OUT_PKT_RDY) {
1368 count = usb_read(USB_OUT_FIFO_WC1);
1375 * NOTE: Sets INDEX register to EP
1377 static void lh7a40x_fifo_flush(struct usb_ep *_ep)
1379 struct lh7a40x_ep *ep;
1381 ep = container_of(_ep, struct lh7a40x_ep, ep);
1382 if (unlikely(!_ep || (!ep->desc && ep->ep.name != ep0name))) {
1383 DEBUG("%s, bad ep\n", __FUNCTION__);
1387 usb_set_index(ep_index(ep));
1391 /****************************************************************/
1392 /* End Point 0 related functions */
1393 /****************************************************************/
1395 /* return: 0 = still running, 1 = completed, negative = errno */
1396 static int write_fifo_ep0(struct lh7a40x_ep *ep, struct lh7a40x_request *req)
1402 max = ep_maxpacket(ep);
1404 DEBUG_EP0("%s\n", __FUNCTION__);
1406 count = write_packet(ep, req, max);
1408 /* last packet is usually short (or a zlp) */
1409 if (unlikely(count != max))
1412 if (likely(req->req.length != req->req.actual) || req->req.zero)
1418 DEBUG_EP0("%s: wrote %s %d bytes%s %d left %p\n", __FUNCTION__,
1420 is_last ? "/L" : "", req->req.length - req->req.actual, req);
1422 /* requests complete when all IN data is in the FIFO */
1431 static __inline__ int lh7a40x_fifo_read(struct lh7a40x_ep *ep,
1432 unsigned char *cp, int max)
1435 int count = usb_read(USB_OUT_FIFO_WC1);
1436 volatile u32 *fifo = (volatile u32 *)ep->fifo;
1442 *cp++ = *fifo & 0xFF;
1446 static __inline__ void lh7a40x_fifo_write(struct lh7a40x_ep *ep,
1447 unsigned char *cp, int count)
1449 volatile u32 *fifo = (volatile u32 *)ep->fifo;
1450 DEBUG_EP0("fifo_write: %d %d\n", ep_index(ep), count);
1455 static int read_fifo_ep0(struct lh7a40x_ep *ep, struct lh7a40x_request *req)
1459 unsigned bufferspace, count, is_short;
1460 volatile u32 *fifo = (volatile u32 *)ep->fifo;
1462 DEBUG_EP0("%s\n", __FUNCTION__);
1464 csr = usb_read(USB_EP0_CSR);
1465 if (!(csr & USB_OUT_CSR1_OUT_PKT_RDY))
1468 buf = req->req.buf + req->req.actual;
1470 bufferspace = req->req.length - req->req.actual;
1472 /* read all bytes from this packet */
1473 if (likely(csr & EP0_OUT_PKT_RDY)) {
1474 count = usb_read(USB_OUT_FIFO_WC1);
1475 req->req.actual += min(count, bufferspace);
1479 is_short = (count < ep->ep.maxpacket);
1480 DEBUG_EP0("read %s %02x, %d bytes%s req %p %d/%d\n",
1481 ep->ep.name, csr, count,
1482 is_short ? "/S" : "", req, req->req.actual, req->req.length);
1484 while (likely(count-- != 0)) {
1485 u8 byte = (u8) (*fifo & 0xff);
1487 if (unlikely(bufferspace == 0)) {
1488 /* this happens when the driver's buffer
1489 * is smaller than what the host sent.
1490 * discard the extra data.
1492 if (req->req.status != -EOVERFLOW)
1493 DEBUG_EP0("%s overflow %d\n", ep->ep.name,
1495 req->req.status = -EOVERFLOW;
1503 if (is_short || req->req.actual == req->req.length) {
1508 /* finished that packet. the next one may be waiting... */
1513 * udc_set_address - set the USB address for this device
1516 * Called from control endpoint function after it decodes a set address setup packet.
1518 static void udc_set_address(struct lh7a40x_udc *dev, unsigned char address)
1520 DEBUG_EP0("%s: %d\n", __FUNCTION__, address);
1521 /* c.f. 15.1.2.2 Table 15-4 address will be used after DATA_END is set */
1522 dev->usb_address = address;
1523 usb_set((address & USB_FA_FUNCTION_ADDR), USB_FA);
1524 usb_set(USB_FA_ADDR_UPDATE | (address & USB_FA_FUNCTION_ADDR), USB_FA);
1525 /* usb_read(USB_FA); */
1529 * DATA_STATE_RECV (OUT_PKT_RDY)
1531 * set EP0_CLR_OUT | EP0_DATA_END | EP0_SEND_STALL bits
1533 * set EP0_CLR_OUT bit
1534 if last set EP0_DATA_END bit
1536 static void lh7a40x_ep0_out(struct lh7a40x_udc *dev, u32 csr)
1538 struct lh7a40x_request *req;
1539 struct lh7a40x_ep *ep = &dev->ep[0];
1542 DEBUG_EP0("%s: %x\n", __FUNCTION__, csr);
1544 if (list_empty(&ep->queue))
1547 req = list_entry(ep->queue.next, struct lh7a40x_request, queue);
1551 if (req->req.length == 0) {
1552 DEBUG_EP0("ZERO LENGTH OUT!\n");
1553 usb_set((EP0_CLR_OUT | EP0_DATA_END), USB_EP0_CSR);
1554 dev->ep0state = WAIT_FOR_SETUP;
1557 ret = read_fifo_ep0(ep, req);
1560 DEBUG_EP0("%s: finished, waiting for status\n",
1563 usb_set((EP0_CLR_OUT | EP0_DATA_END), USB_EP0_CSR);
1564 dev->ep0state = WAIT_FOR_SETUP;
1566 /* Not done yet.. */
1567 DEBUG_EP0("%s: not finished\n", __FUNCTION__);
1568 usb_set(EP0_CLR_OUT, USB_EP0_CSR);
1571 DEBUG_EP0("NO REQ??!\n");
1578 static int lh7a40x_ep0_in(struct lh7a40x_udc *dev, u32 csr)
1580 struct lh7a40x_request *req;
1581 struct lh7a40x_ep *ep = &dev->ep[0];
1582 int ret, need_zlp = 0;
1584 DEBUG_EP0("%s: %x\n", __FUNCTION__, csr);
1586 if (list_empty(&ep->queue))
1589 req = list_entry(ep->queue.next, struct lh7a40x_request, queue);
1592 DEBUG_EP0("%s: NULL REQ\n", __FUNCTION__);
1596 if (req->req.length == 0) {
1598 usb_set((EP0_IN_PKT_RDY | EP0_DATA_END), USB_EP0_CSR);
1599 dev->ep0state = WAIT_FOR_SETUP;
1603 if (req->req.length - req->req.actual == EP0_PACKETSIZE) {
1604 /* Next write will end with the packet size, */
1605 /* so we need Zero-length-packet */
1609 ret = write_fifo_ep0(ep, req);
1611 if (ret == 1 && !need_zlp) {
1613 DEBUG_EP0("%s: finished, waiting for status\n", __FUNCTION__);
1615 usb_set((EP0_IN_PKT_RDY | EP0_DATA_END), USB_EP0_CSR);
1616 dev->ep0state = WAIT_FOR_SETUP;
1618 DEBUG_EP0("%s: not finished\n", __FUNCTION__);
1619 usb_set(EP0_IN_PKT_RDY, USB_EP0_CSR);
1623 DEBUG_EP0("%s: Need ZLP!\n", __FUNCTION__);
1624 usb_set(EP0_IN_PKT_RDY, USB_EP0_CSR);
1625 dev->ep0state = DATA_STATE_NEED_ZLP;
1631 static int lh7a40x_handle_get_status(struct lh7a40x_udc *dev,
1632 struct usb_ctrlrequest *ctrl)
1634 struct lh7a40x_ep *ep0 = &dev->ep[0];
1635 struct lh7a40x_ep *qep;
1636 int reqtype = (ctrl->bRequestType & USB_RECIP_MASK);
1639 if (reqtype == USB_RECIP_INTERFACE) {
1640 /* This is not supported.
1641 * And according to the USB spec, this one does nothing..
1644 DEBUG_SETUP("GET_STATUS: USB_RECIP_INTERFACE\n");
1645 } else if (reqtype == USB_RECIP_DEVICE) {
1646 DEBUG_SETUP("GET_STATUS: USB_RECIP_DEVICE\n");
1647 val |= (1 << 0); /* Self powered */
1648 /*val |= (1<<1); *//* Remote wakeup */
1649 } else if (reqtype == USB_RECIP_ENDPOINT) {
1650 int ep_num = (ctrl->wIndex & ~USB_DIR_IN);
1653 ("GET_STATUS: USB_RECIP_ENDPOINT (%d), ctrl->wLength = %d\n",
1654 ep_num, ctrl->wLength);
1656 if (ctrl->wLength > 2 || ep_num > 3)
1659 qep = &dev->ep[ep_num];
1660 if (ep_is_in(qep) != ((ctrl->wIndex & USB_DIR_IN) ? 1 : 0)
1661 && ep_index(qep) != 0) {
1665 usb_set_index(ep_index(qep));
1667 /* Return status on next IN token */
1668 switch (qep->ep_type) {
1671 (usb_read(qep->csr1) & EP0_SEND_STALL) ==
1677 (usb_read(qep->csr1) & USB_IN_CSR1_SEND_STALL) ==
1678 USB_IN_CSR1_SEND_STALL;
1682 (usb_read(qep->csr1) & USB_OUT_CSR1_SEND_STALL) ==
1683 USB_OUT_CSR1_SEND_STALL;
1687 /* Back to EP0 index */
1690 DEBUG_SETUP("GET_STATUS, ep: %d (%x), val = %d\n", ep_num,
1693 DEBUG_SETUP("Unknown REQ TYPE: %d\n", reqtype);
1697 /* Clear "out packet ready" */
1698 usb_set((EP0_CLR_OUT), USB_EP0_CSR);
1699 /* Put status to FIFO */
1700 lh7a40x_fifo_write(ep0, (u8 *) & val, sizeof(val));
1701 /* Issue "In packet ready" */
1702 usb_set((EP0_IN_PKT_RDY | EP0_DATA_END), USB_EP0_CSR);
1708 * WAIT_FOR_SETUP (OUT_PKT_RDY)
1709 * - read data packet from EP0 FIFO
1712 * set EP0_CLR_OUT | EP0_DATA_END | EP0_SEND_STALL bits
1714 * set EP0_CLR_OUT | EP0_DATA_END bits
1716 static void lh7a40x_ep0_setup(struct lh7a40x_udc *dev, u32 csr)
1718 struct lh7a40x_ep *ep = &dev->ep[0];
1719 struct usb_ctrlrequest ctrl;
1720 int i, bytes, is_in;
1722 DEBUG_SETUP("%s: %x\n", __FUNCTION__, csr);
1724 /* Nuke all previous transfers */
1727 /* read control req from fifo (8 bytes) */
1728 bytes = lh7a40x_fifo_read(ep, (unsigned char *)&ctrl, 8);
1730 DEBUG_SETUP("Read CTRL REQ %d bytes\n", bytes);
1731 DEBUG_SETUP("CTRL.bRequestType = %d (is_in %d)\n", ctrl.bRequestType,
1732 ctrl.bRequestType == USB_DIR_IN);
1733 DEBUG_SETUP("CTRL.bRequest = %d\n", ctrl.bRequest);
1734 DEBUG_SETUP("CTRL.wLength = %d\n", ctrl.wLength);
1735 DEBUG_SETUP("CTRL.wValue = %d (%d)\n", ctrl.wValue, ctrl.wValue >> 8);
1736 DEBUG_SETUP("CTRL.wIndex = %d\n", ctrl.wIndex);
1738 /* Set direction of EP0 */
1739 if (likely(ctrl.bRequestType & USB_DIR_IN)) {
1740 ep->bEndpointAddress |= USB_DIR_IN;
1743 ep->bEndpointAddress &= ~USB_DIR_IN;
1747 dev->req_pending = 1;
1749 /* Handle some SETUP packets ourselves */
1750 switch (ctrl.bRequest) {
1751 case USB_REQ_SET_ADDRESS:
1752 if (ctrl.bRequestType != (USB_TYPE_STANDARD | USB_RECIP_DEVICE))
1755 DEBUG_SETUP("USB_REQ_SET_ADDRESS (%d)\n", ctrl.wValue);
1756 udc_set_address(dev, ctrl.wValue);
1757 usb_set((EP0_CLR_OUT | EP0_DATA_END), USB_EP0_CSR);
1760 case USB_REQ_GET_STATUS:{
1761 if (lh7a40x_handle_get_status(dev, &ctrl) == 0)
1764 case USB_REQ_CLEAR_FEATURE:
1765 case USB_REQ_SET_FEATURE:
1766 if (ctrl.bRequestType == USB_RECIP_ENDPOINT) {
1767 struct lh7a40x_ep *qep;
1768 int ep_num = (ctrl.wIndex & 0x0f);
1770 /* Support only HALT feature */
1771 if (ctrl.wValue != 0 || ctrl.wLength != 0
1772 || ep_num > 3 || ep_num < 1)
1775 qep = &dev->ep[ep_num];
1776 if (ctrl.bRequest == USB_REQ_SET_FEATURE) {
1777 DEBUG_SETUP("SET_FEATURE (%d)\n",
1779 lh7a40x_set_halt(&qep->ep, 1);
1781 DEBUG_SETUP("CLR_FEATURE (%d)\n",
1783 lh7a40x_set_halt(&qep->ep, 0);
1787 /* Reply with a ZLP on next IN token */
1788 usb_set((EP0_CLR_OUT | EP0_DATA_END),
1799 if (likely(dev->driver)) {
1800 /* device-2-host (IN) or no data setup command, process immediately */
1801 spin_unlock(&dev->lock);
1802 i = dev->driver->setup(&dev->gadget, &ctrl);
1803 spin_lock(&dev->lock);
1806 /* setup processing failed, force stall */
1808 (" --> ERROR: gadget setup FAILED (stalling), setup returned %d\n",
1811 usb_set((EP0_CLR_OUT | EP0_DATA_END | EP0_SEND_STALL),
1814 /* ep->stopped = 1; */
1815 dev->ep0state = WAIT_FOR_SETUP;
1821 * DATA_STATE_NEED_ZLP
1823 static void lh7a40x_ep0_in_zlp(struct lh7a40x_udc *dev, u32 csr)
1825 DEBUG_EP0("%s: %x\n", __FUNCTION__, csr);
1827 /* c.f. Table 15-14 */
1828 usb_set((EP0_IN_PKT_RDY | EP0_DATA_END), USB_EP0_CSR);
1829 dev->ep0state = WAIT_FOR_SETUP;
1833 * handle ep0 interrupt
1835 static void lh7a40x_handle_ep0(struct lh7a40x_udc *dev, u32 intr)
1837 struct lh7a40x_ep *ep = &dev->ep[0];
1842 csr = usb_read(USB_EP0_CSR);
1844 DEBUG_EP0("%s: csr = %x\n", __FUNCTION__, csr);
1847 * For overview of what we should be doing see c.f. Chapter 18.1.2.4
1848 * We will follow that outline here modified by our own global state
1849 * indication which provides hints as to what we think should be
1854 * if SENT_STALL is set
1855 * - clear the SENT_STALL bit
1857 if (csr & EP0_SENT_STALL) {
1858 DEBUG_EP0("%s: EP0_SENT_STALL is set: %x\n", __FUNCTION__, csr);
1859 usb_clear((EP0_SENT_STALL | EP0_SEND_STALL), USB_EP0_CSR);
1860 nuke(ep, -ECONNABORTED);
1861 dev->ep0state = WAIT_FOR_SETUP;
1866 * if a transfer is in progress && IN_PKT_RDY and OUT_PKT_RDY are clear
1869 * - set IN_PKT_RDY | DATA_END
1873 if (!(csr & (EP0_IN_PKT_RDY | EP0_OUT_PKT_RDY))) {
1874 DEBUG_EP0("%s: IN_PKT_RDY and OUT_PKT_RDY are clear\n",
1877 switch (dev->ep0state) {
1878 case DATA_STATE_XMIT:
1879 DEBUG_EP0("continue with DATA_STATE_XMIT\n");
1880 lh7a40x_ep0_in(dev, csr);
1882 case DATA_STATE_NEED_ZLP:
1883 DEBUG_EP0("continue with DATA_STATE_NEED_ZLP\n");
1884 lh7a40x_ep0_in_zlp(dev, csr);
1888 DEBUG_EP0("Odd state!! state = %s\n",
1889 state_names[dev->ep0state]);
1890 dev->ep0state = WAIT_FOR_SETUP;
1892 /* usb_set(EP0_SEND_STALL, ep->csr1); */
1898 * if SETUP_END is set
1899 * - abort the last transfer
1900 * - set SERVICED_SETUP_END_BIT
1902 if (csr & EP0_SETUP_END) {
1903 DEBUG_EP0("%s: EP0_SETUP_END is set: %x\n", __FUNCTION__, csr);
1905 usb_set(EP0_CLR_SETUP_END, USB_EP0_CSR);
1908 dev->ep0state = WAIT_FOR_SETUP;
1912 * if EP0_OUT_PKT_RDY is set
1913 * - read data packet from EP0 FIFO
1916 * set SERVICED_OUT_PKT_RDY | DATA_END bits | SEND_STALL
1918 * set SERVICED_OUT_PKT_RDY | DATA_END bits
1920 if (csr & EP0_OUT_PKT_RDY) {
1922 DEBUG_EP0("%s: EP0_OUT_PKT_RDY is set: %x\n", __FUNCTION__,
1925 switch (dev->ep0state) {
1926 case WAIT_FOR_SETUP:
1927 DEBUG_EP0("WAIT_FOR_SETUP\n");
1928 lh7a40x_ep0_setup(dev, csr);
1931 case DATA_STATE_RECV:
1932 DEBUG_EP0("DATA_STATE_RECV\n");
1933 lh7a40x_ep0_out(dev, csr);
1938 DEBUG_EP0("strange state!! 2. send stall? state = %d\n",
1945 static void lh7a40x_ep0_kick(struct lh7a40x_udc *dev, struct lh7a40x_ep *ep)
1950 csr = usb_read(USB_EP0_CSR);
1952 DEBUG_EP0("%s: %x\n", __FUNCTION__, csr);
1954 /* Clear "out packet ready" */
1955 usb_set(EP0_CLR_OUT, USB_EP0_CSR);
1958 dev->ep0state = DATA_STATE_XMIT;
1959 lh7a40x_ep0_in(dev, csr);
1961 dev->ep0state = DATA_STATE_RECV;
1962 lh7a40x_ep0_out(dev, csr);
1966 /* ---------------------------------------------------------------------------
1967 * device-scoped parts of the api to the usb controller hardware
1968 * ---------------------------------------------------------------------------
1971 static int lh7a40x_udc_get_frame(struct usb_gadget *_gadget)
1973 u32 frame1 = usb_read(USB_FRM_NUM1); /* Least significant 8 bits */
1974 u32 frame2 = usb_read(USB_FRM_NUM2); /* Most significant 3 bits */
1975 DEBUG("%s, %p\n", __FUNCTION__, _gadget);
1976 return ((frame2 & 0x07) << 8) | (frame1 & 0xff);
1979 static int lh7a40x_udc_wakeup(struct usb_gadget *_gadget)
1981 /* host may not have enabled remote wakeup */
1982 /*if ((UDCCS0 & UDCCS0_DRWF) == 0)
1983 return -EHOSTUNREACH;
1984 udc_set_mask_UDCCR(UDCCR_RSM); */
1988 static const struct usb_gadget_ops lh7a40x_udc_ops = {
1989 .get_frame = lh7a40x_udc_get_frame,
1990 .wakeup = lh7a40x_udc_wakeup,
1991 /* current versions must always be self-powered */
1994 static void nop_release(struct device *dev)
1996 DEBUG("%s %s\n", __FUNCTION__, dev->bus_id);
1999 static struct lh7a40x_udc memory = {
2003 .ops = &lh7a40x_udc_ops,
2004 .ep0 = &memory.ep[0].ep,
2005 .name = driver_name,
2008 .release = nop_release,
2012 /* control endpoint */
2016 .ops = &lh7a40x_ep_ops,
2017 .maxpacket = EP0_PACKETSIZE,
2021 .bEndpointAddress = 0,
2024 .ep_type = ep_control,
2025 .fifo = io_p2v(USB_EP0_FIFO),
2026 .csr1 = USB_EP0_CSR,
2027 .csr2 = USB_EP0_CSR,
2030 /* first group of endpoints */
2033 .name = "ep1in-bulk",
2034 .ops = &lh7a40x_ep_ops,
2039 .bEndpointAddress = USB_DIR_IN | 1,
2040 .bmAttributes = USB_ENDPOINT_XFER_BULK,
2042 .ep_type = ep_bulk_in,
2043 .fifo = io_p2v(USB_EP1_FIFO),
2044 .csr1 = USB_IN_CSR1,
2045 .csr2 = USB_IN_CSR2,
2050 .name = "ep2out-bulk",
2051 .ops = &lh7a40x_ep_ops,
2056 .bEndpointAddress = 2,
2057 .bmAttributes = USB_ENDPOINT_XFER_BULK,
2059 .ep_type = ep_bulk_out,
2060 .fifo = io_p2v(USB_EP2_FIFO),
2061 .csr1 = USB_OUT_CSR1,
2062 .csr2 = USB_OUT_CSR2,
2067 .name = "ep3in-int",
2068 .ops = &lh7a40x_ep_ops,
2073 .bEndpointAddress = USB_DIR_IN | 3,
2074 .bmAttributes = USB_ENDPOINT_XFER_INT,
2076 .ep_type = ep_interrupt,
2077 .fifo = io_p2v(USB_EP3_FIFO),
2078 .csr1 = USB_IN_CSR1,
2079 .csr2 = USB_IN_CSR2,
2084 * probe - binds to the platform device
2086 static int lh7a40x_udc_probe(struct device *_dev)
2088 struct lh7a40x_udc *dev = &memory;
2091 DEBUG("%s: %p\n", __FUNCTION__, _dev);
2093 spin_lock_init(&dev->lock);
2096 device_initialize(&dev->gadget.dev);
2097 dev->gadget.dev.parent = _dev;
2099 the_controller = dev;
2100 dev_set_drvdata(_dev, dev);
2105 /* irq setup after old hardware state is cleaned up */
2107 request_irq(IRQ_USBINTR, lh7a40x_udc_irq, SA_INTERRUPT, driver_name,
2110 DEBUG(KERN_ERR "%s: can't get irq %i, err %d\n", driver_name,
2111 IRQ_USBINTR, retval);
2115 create_proc_files();
2120 static int lh7a40x_udc_remove(struct device *_dev)
2122 struct lh7a40x_udc *dev = _dev->driver_data;
2124 DEBUG("%s: %p\n", __FUNCTION__, dev);
2127 remove_proc_files();
2128 usb_gadget_unregister_driver(dev->driver);
2130 free_irq(IRQ_USBINTR, dev);
2132 dev_set_drvdata(_dev, 0);
2139 /*-------------------------------------------------------------------------*/
2141 static struct device_driver udc_driver = {
2142 .name = (char *)driver_name,
2143 .owner = THIS_MODULE,
2144 .bus = &platform_bus_type,
2145 .probe = lh7a40x_udc_probe,
2146 .remove = lh7a40x_udc_remove
2147 /* FIXME power management support */
2148 /* .suspend = ... disable UDC */
2149 /* .resume = ... re-enable UDC */
2152 static int __init udc_init(void)
2154 DEBUG("%s: %s version %s\n", __FUNCTION__, driver_name, DRIVER_VERSION);
2155 return driver_register(&udc_driver);
2158 static void __exit udc_exit(void)
2160 driver_unregister(&udc_driver);
2163 module_init(udc_init);
2164 module_exit(udc_exit);
2166 MODULE_DESCRIPTION(DRIVER_DESC);
2167 MODULE_AUTHOR("Mikko Lahteenmaki, Bo Henriksen");
2168 MODULE_LICENSE("GPL");