1 From 0e3b1bffd1974e6852912865a7cea481617b1c39 Mon Sep 17 00:00:00 2001
2 From: John Crispin <blogic@openwrt.org>
3 Date: Thu, 30 May 2013 16:06:35 +0200
4 Subject: [PATCH 162/164] USB: MIPS: ralink: add rt5350/mt7620 UDC
6 Signed-off-by: John Crispin <blogic@openwrt.org>
8 drivers/usb/gadget/Kconfig | 8 +
9 drivers/usb/gadget/Makefile | 1 +
10 drivers/usb/gadget/rt_udc.h | 417 +++++++
11 drivers/usb/gadget/rt_udc_pdma.c | 2547 ++++++++++++++++++++++++++++++++++++++
12 4 files changed, 2973 insertions(+)
13 create mode 100644 drivers/usb/gadget/rt_udc.h
14 create mode 100644 drivers/usb/gadget/rt_udc_pdma.c
16 --- a/drivers/usb/gadget/Kconfig
17 +++ b/drivers/usb/gadget/Kconfig
18 @@ -336,6 +336,14 @@ config USB_MV_U3D
19 MARVELL PXA2128 Processor series include a super speed USB3.0 device
20 controller, which support super speed USB peripheral.
23 + boolean "Ralink USB Device Port"
24 + depends on SOC_MT7620
26 + Say "y" to link the driver statically, or "m" to build a
27 + dynamically linked module called "rt_udc" and force all
28 + gadget drivers to also be dynamically linked.
31 # Controllers available in both integrated and discrete versions
33 --- a/drivers/usb/gadget/Makefile
34 +++ b/drivers/usb/gadget/Makefile
35 @@ -34,6 +34,7 @@ obj-$(CONFIG_USB_MV_UDC) += mv_udc.o
36 mv_udc-y := mv_udc_core.o
37 obj-$(CONFIG_USB_FUSB300) += fusb300_udc.o
38 obj-$(CONFIG_USB_MV_U3D) += mv_u3d_core.o
39 +obj-$(CONFIG_USB_RT_UDC) += rt_udc_pdma.o
42 obj-$(CONFIG_USB_F_ACM) += f_acm.o
44 +++ b/drivers/usb/gadget/rt_udc.h
47 + * Copyright (C) 2009 Y.Y. Huang, Ralink Tech.(yy_huang@ralinktech.com)
49 + * This udc driver is now under testing and code is based on pxa2xx_udc.h
50 + * Please use it with your own risk!
52 + * This program is free software; you can redistribute it and/or modify
53 + * it under the terms of the GNU General Public License as published by
54 + * the Free Software Foundation; either version 2 of the License, or
55 + * (at your option) any later version.
57 + * This program is distributed in the hope that it will be useful,
58 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
59 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
60 + * GNU General Public License for more details.
63 +#ifndef __LINUX_USB_GADGET_RT_UDC_H
64 +#define __LINUX_USB_GADGET_RT_UDC_H
66 +#define CONFIG_RALINK_MT7620
68 +#include <linux/types.h>
70 +//#include "../host/ralink_usb.h" /* for port sharing setting and power saving purpose */
72 +#if defined (CONFIG_RALINK_RT3883) || defined (CONFIG_RALINK_RT3352) || defined (CONFIG_RALINK_MT7620)
75 +#elif defined (CONFIG_RALINK_RT5350)
79 +#error "Please define a platform."
83 +#define EP_IDX(ep) ((ep->bEndpointAddress & ~USB_DIR_IN)+(EP_DIR(ep)? 0:IN_EP_NUM)) /* IN:1, OUT:0 */
84 +#define EP_NO(ep) ((ep->bEndpointAddress & ~USB_DIR_IN)) /* IN:1, OUT:0 */
85 +#define EP_DIR(ep) ((ep->bEndpointAddress) & USB_DIR_IN ? 1 : 0)
88 +#define RT_USB_NB_EP (IN_EP_NUM + OUT_EP_NUM + 1)
90 +/* Driver structures */
92 + struct usb_request req;
93 + struct list_head queue;
94 + unsigned int in_use;
95 + struct rt_ep_struct *rt_ep; // test for rx tasklet
96 + int zlp_dma_done; // used for DMA ZLP packet.
103 + EP0_OUT_DATA_PHASE,
108 +struct rt_ep_struct {
110 + struct rt_udc_struct *rt_usb;
111 + struct list_head queue;
112 + unsigned char stopped;
113 + unsigned char bEndpointAddress;
114 + unsigned char bmAttributes;
116 + unsigned char pending;
117 + unsigned int rx_done_count; /* used by OUT EP only */
118 + unsigned int tx_done_count; /* used by OUT EP only */
121 +struct rt_udc_struct {
122 + struct usb_gadget gadget;
123 + struct usb_gadget_driver *driver;
124 + struct device *dev;
125 + struct rt_ep_struct rt_ep[RT_USB_NB_EP];
126 + /* struct clk *clk; */
127 + struct timer_list timer;
128 + enum ep0_state ep0state;
129 + struct resource *res;
130 + void __iomem *base;
131 + unsigned char set_config;
138 +#define USB_BASE (0xB0120000)
140 +#define OUT0BC (0x000)
141 +#define IN0BC (0x001)
142 +#define EP0CS (0x002)
144 +#define OUT1CON (0x00A)
145 +#define IN1CON (0x00E)
146 +#define OUT2CON (0x012)
147 +#define IN2CON (0x016)
148 +#define OUT3CON (0x01A)
149 +#define IN3CON (0x01E)
150 +#define OUT4CON (0x022)
151 +#define IN4CON (0x026)
154 +#define EP0INDAT (0x100)
155 +#define EP0OUTDAT (0x140)
156 +#define SETUPDATA (0x180)
158 +#define IN07IRQ (0x188)
159 +#define IN815IRQ (0x189)
160 +#define OUT07IRQ (0x18A)
161 +#define OUT815IRQ (0x18B)
162 +#define USBIRQ (0x18C)
163 +#define OUT07PNGIRQ (0x18E)
164 +#define OUT815PNGIRQ (0x18F)
166 +#define IN07IEN (0x194)
167 +#define OUT07IEN (0x196)
168 +#define USBIEN (0x198)
170 +#define OUT07PNGIEN (0x19A)
171 +#define OUT815PNGIEN (0x19B)
173 +#define ENDPRST (0x1A2)
174 +#define ENDPRST_IO (0x1 << 4)
175 +#define ENDPRST_TOGRST (0x1 << 5)
176 +#define ENDPRST_FIFORST (0x1 << 6)
178 +#define FIFOCTRL (0x1A8)
180 +#define EP_CS_EP0_STALL (0x1 << 0)
181 +#define EP_CS_EP0_HSNAK (0x1 << 1)
182 +#define EP_CS_EP0_INBSY (0x1 << 2)
183 +#define EP_CS_EP0_OUTBSY (0x1 << 3)
184 +#define EP_CS_AUTO (0x1 << 4)
185 +#define EP_CS_NPAK1 (0x1 << 3)
186 +#define EP_CS_NPAK0 (0x1 << 2)
187 +#define EP_CS_BSY (0x1 << 1)
188 +#define EP_CS_ERR (0x1 << 0)
190 +#define EP0_OUT_BSY (0x1 << 3)
191 +#define EP0_IN_BSY (0x1 << 2)
193 +#define USB_INTR_HSPEED (0x20)
194 +#define USB_INTR_RESET (0x10)
195 +#define USB_INTR_SUSPEND (0x08)
196 +#define USB_INTR_SETUP_TOKEN (0x04)
197 +#define USB_INTR_SOF (0x02)
198 +#define USB_INTR_SETUP_TOKEN_VALID (0x01)
201 +#define RTUSB_UDMA_CTRL (USB_BASE + 0x800)
202 +#define RTUSB_UDMA_WRR (USB_BASE + 0x804)
205 +#define RTUSB_TX_BASE_PTR0 (USB_BASE + 0x1000)
206 +#define RTUSB_TX_MAX_CNT0 (USB_BASE + 0x1004)
207 +#define RTUSB_TX_CTX_IDX0 (USB_BASE + 0x1008)
208 +#define RTUSB_TX_DTX_IDX0 (USB_BASE + 0x100C)
209 +#define RTUSB_TX_BASE_PTR1 (USB_BASE + 0x1010)
210 +#define RTUSB_TX_MAX_CNT1 (USB_BASE + 0x1014)
211 +#define RTUSB_TX_CTX_IDX1 (USB_BASE + 0x1018)
212 +#define RTUSB_TX_DTX_IDX1 (USB_BASE + 0x101C)
213 +#define RTUSB_RX_BASE_PTR0 (USB_BASE + 0x1100)
214 +#define RTUSB_RX_MAX_CNT0 (USB_BASE + 0x1104)
215 +#define RTUSB_RX_CALC_IDX0 (USB_BASE + 0x1108)
216 +#define RTUSB_RX_DRX_IDX0 (USB_BASE + 0x110C)
217 +#define RTUSB_PDMA_GLO_CFG (USB_BASE + 0x1204)
219 +#define RTUSB_TX_WB_DDONE (0x1 << 6)
220 +#define RTUSB_RX_DMA_BUSY (0x1 << 3)
221 +#define RTUSB_RX_DMA_EN (0x1 << 2)
222 +#define RTUSB_TX_DMA_BUSY (0x1 << 1)
223 +#define RTUSB_TX_DMA_EN (0x1 << 0)
225 +#define RTUSB_PDMA_RST_IDX (USB_BASE + 0x1208)
227 +#define RTUSB_RST_DRX_IDX1 (0x1 << 17)
228 +#define RTUSB_RST_DRX_IDX0 (0x1 << 16)
229 +#define RTUSB_RST_DTX_IDX3 (0x1 << 3)
230 +#define RTUSB_RST_DTX_IDX2 (0x1 << 2)
231 +#define RTUSB_RST_DTX_IDX1 (0x1 << 1)
232 +#define RTUSB_RST_DTX_IDX0 (0x1 << 0)
234 +#define RTUSB_DELAY_INT_CFG (USB_BASE + 0x120C)
235 +#define RTUSB_INT_STATUS (USB_BASE + 0x1220)
236 +#define RTUSB_RX_DONE_INT1 (0x1 << 17)
237 +#define RTUSB_RX_DONE_INT0 (0x1 << 16)
238 +#define RTUSB_TX_DONE_INT3 (0x1 << 3)
239 +#define RTUSB_TX_DONE_INT2 (0x1 << 2)
240 +#define RTUSB_TX_DONE_INT1 (0x1 << 1)
241 +#define RTUSB_TX_DONE_INT0 (0x1 << 0)
243 +#define RTUSB_INT_MASK (USB_BASE + 0x1228)
244 +#define RTUSB_RX_DONE_INT_MSK1 (0x1 << 17)
245 +#define RTUSB_RX_DONE_INT_MSK0 (0x1 << 16)
246 +#define RTUSB_TX_DONE_INT_MSK3 (0x1 << 3)
247 +#define RTUSB_TX_DONE_INT_MSK2 (0x1 << 2)
248 +#define RTUSB_TX_DONE_INT_MSK1 (0x1 << 1)
249 +#define RTUSB_TX_DONE_INT_MSK0 (0x1 << 0)
252 +/*=========================================
253 + PDMA RX Descriptor Format define
254 +=========================================*/
255 +//-------------------------------------------------
256 +typedef struct _PDMA_RXD_INFO1_ PDMA_RXD_INFO1_T;
258 +struct _PDMA_RXD_INFO1_ {
261 +//-------------------------------------------------
262 +typedef struct _PDMA_RXD_INFO2_ PDMA_RXD_INFO2_T;
264 +struct _PDMA_RXD_INFO2_ {
265 + unsigned int PLEN1 : 14;
266 + unsigned int LS1 : 1;
267 + unsigned int UN_USED : 1;
268 + unsigned int PLEN0 : 14;
269 + unsigned int LS0 : 1;
270 + unsigned int DDONE_bit : 1;
272 +//-------------------------------------------------
273 +typedef struct _PDMA_RXD_INFO3_ PDMA_RXD_INFO3_T;
275 +struct _PDMA_RXD_INFO3_ {
278 +//-------------------------------------------------
279 +typedef struct _PDMA_RXD_INFO4_ PDMA_RXD_INFO4_T;
281 +struct _PDMA_RXD_INFO4_ {
282 + unsigned int Rx_bcnt:16;
283 + unsigned int Reserved1:8;
284 + unsigned int Out_ep_addr:4;
285 + unsigned int Reserved0:4;
287 +struct PDMA_rxdesc {
288 + PDMA_RXD_INFO1_T rxd_info1;
289 + PDMA_RXD_INFO2_T rxd_info2;
290 + PDMA_RXD_INFO3_T rxd_info3;
291 + PDMA_RXD_INFO4_T rxd_info4;
293 +/*=========================================
294 + PDMA TX Descriptor Format define
295 +=========================================*/
296 +//-------------------------------------------------
297 +typedef struct _PDMA_TXD_INFO1_ PDMA_TXD_INFO1_T;
299 +struct _PDMA_TXD_INFO1_ {
302 +//-------------------------------------------------
303 +typedef struct _PDMA_TXD_INFO2_ PDMA_TXD_INFO2_T;
305 +struct _PDMA_TXD_INFO2_ {
306 + unsigned int SDL1 : 14;
307 + unsigned int LS1_bit : 1;
308 + unsigned int BURST_bit : 1;
309 + unsigned int SDL0 : 14;
310 + unsigned int LS0_bit : 1;
311 + unsigned int DDONE_bit : 1;
313 +//-------------------------------------------------
314 +typedef struct _PDMA_TXD_INFO3_ PDMA_TXD_INFO3_T;
316 +struct _PDMA_TXD_INFO3_ {
319 +//-------------------------------------------------
320 +typedef struct _PDMA_TXD_INFO4_ PDMA_TXD_INFO4_T;
321 +struct _PDMA_TXD_INFO4_ {
322 + unsigned int reserved2:17;
323 + unsigned int zlp_flag:1;
324 + unsigned int reserved1:6;
325 + unsigned int In_ep_addr:4;
326 + unsigned int rsv:4;
329 +struct PDMA_txdesc {
330 + PDMA_TXD_INFO1_T txd_info1;
331 + PDMA_TXD_INFO2_T txd_info2;
332 + PDMA_TXD_INFO3_T txd_info3;
333 + PDMA_TXD_INFO4_T txd_info4;
338 +#define DBG do{ if(debuglevel) printk("%s()\n", __FUNCTION__); }while(0);
339 +#define DD do{ printk("%s: %s %d\n", driver_name, __FUNCTION__, __LINE__); } while(0);
340 +#define xprintk(fmt, args...) do{ if(debuglevel) printk(fmt, ## args); } while(0);
344 +#define xprintk(fmt, args...)
347 +#define FATAL_ERROR(fmt, args...) do{ printk(fmt, ## args); printk("\n############### ERROR #####################\n %s %d\n############### ERROR #####################\n", __FUNCTION__, __LINE__); BUG(); } while(0)
349 +static void inline dump_usbirq(u32 irqreg)
352 + xprintk("U%s%s%s%s%s%s\n",
353 + (irqreg & USB_INTR_SOF) ? "sof" : "",
354 + (irqreg & USB_INTR_RESET) ? " rst" : "",
355 + (irqreg & USB_INTR_SUSPEND) ? " sus" : "",
356 + (irqreg & USB_INTR_SETUP_TOKEN) ? "st" : "",
357 + (irqreg & USB_INTR_SETUP_TOKEN_VALID) ? "sv" : "",
358 + (irqreg & USB_INTR_HSPEED) ? " HS" : "");
360 +// if(irqreg & USB_INTR_SETUP_TOKEN)
362 +// if(irqreg & USB_INTR_SETUP_TOKEN_VALID)
367 +static void inline dump_epirq(u32 irqreg, u32 ienreg, int dir)
370 + xprintk("%s%x\n", dir? "I" : "O", irqreg);
373 +static __inline__ u32 usb_read(u32 addr)
375 + return ioread32( (void __iomem *)(USB_BASE + (addr << 2)) );
378 +static __inline__ void usb_write(u32 addr, u32 value)
380 + iowrite32(value, (void __iomem *)(USB_BASE + (addr << 2)) );
383 +static __inline__ void reg_write(u32 addr, u32 value)
385 + iowrite32(value, (void __iomem *)0x0 + addr);
388 +static __inline__ u32 reg_read(u32 addr)
390 + return ioread32( (void __iomem *)0x0 + addr);
394 +static void handle_pending_epoutirq(struct rt_udc_struct *rt_usb, struct rt_ep_struct *rt_ep, struct rt_request *req);
406 + #define D_REQ(dev, args...) printk(args)
408 + #define D_REQ(dev, args...) do {} while (0)
409 +#endif /* DEBUG_REQ */
412 + #define D_TRX(dev, args...) printk(args)
414 + #define D_TRX(dev, args...) do {} while (0)
415 +#endif /* DEBUG_TRX */
418 + #define D_INI(dev, args...) printk(args)
420 + #define D_INI(dev, args...) do {} while (0)
421 +#endif /* DEBUG_INIT */
424 + static const char *state_name[] = {
431 + #define D_EP0(dev, args...) printk(args)
433 + #define D_EP0(dev, args...) do {} while (0)
434 +#endif /* DEBUG_EP0 */
437 + #define D_EPX(dev, args...) printk(args)
439 + #define D_EPX(dev, args...) do {} while (0)
440 +#endif /* DEBUG_EP0 */
443 + #define D_ERR(dev, args...) printk(args)
445 + #define D_ERR(dev, args...) do {} while (0)
449 + #define D_REQ(dev, args...) do {} while (0)
450 + #define D_TRX(dev, args...) do {} while (0)
451 + #define D_INI(dev, args...) do {} while (0)
452 + #define D_EP0(dev, args...) do {} while (0)
453 + #define D_EPX(dev, args...) do {} while (0)
454 + #define dump_ep_intr(x, y, z, i) do {} while (0)
455 + #define dump_intr(x, y, z) do {} while (0)
456 + #define dump_ep_stat(x, y) do {} while (0)
457 + #define dump_usb_stat(x, y) do {} while (0)
458 + #define dump_req(x, y, z) do {} while (0)
459 + #define D_ERR(dev, args...) do {} while (0)
462 +#endif /* __LINUX_USB_GADGET_RT_UDC_H */
464 +++ b/drivers/usb/gadget/rt_udc_pdma.c
467 + * driver/usb/gadget/rt_udc.c
469 + * Copyright (C) 2009 Ying Yuan Huang, Ralink Tech. <yyhuang@ralink_tech.com>
471 + * This program is free software; you can redistribute it and/or modify
472 + * it under the terms of the GNU General Public License as published by
473 + * the Free Software Foundation; either version 2 of the License, or
474 + * (at your option) any later version.
476 + * This program is distributed in the hope that it will be useful,
477 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
478 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
479 + * GNU General Public License for more details.
483 + * 1) [ USB composite device ]. The USB PDMA architecture is not suitable for USB composite
484 + * device support. A passive gadget driver(device) may slow down or block other gadget
485 + * (device) because they are in the same ring.
487 +#include <linux/init.h>
488 +#include <linux/kernel.h>
489 +#include <linux/platform_device.h>
490 +#include <linux/module.h>
491 +#include <linux/errno.h>
492 +#include <linux/list.h>
493 +#include <linux/interrupt.h>
494 +#include <linux/io.h>
495 +#include <linux/irq.h>
496 +#include <linux/device.h>
497 +#include <linux/dma-mapping.h>
498 +#include <linux/clk.h>
499 +#include <linux/delay.h>
500 +#include <linux/timer.h>
501 +#include <linux/proc_fs.h>
502 +#include <linux/usb/ch9.h>
503 +#include <linux/version.h>
504 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
505 +#include <linux/usb_gadget.h>
507 +#include <linux/usb/gadget.h>
510 +static const char driver_name[] = "rt_udc";
511 +static const char ep0name[] = "ep0";
512 +static unsigned debuglevel = 0;
513 +module_param (debuglevel, uint, S_IRUGO);
518 +#define PROC_DIR driver_name
519 +#define DEBUGLEVEL_PROCFILE "debuglevel"
520 +static struct proc_dir_entry *pProcDir = NULL;
521 +static struct proc_dir_entry *pProcDebugLevel = NULL;
526 +#define NUM_RX_DESC 256
527 +#define NUM_TX_DESC 256
528 +#define RX_BUFF_SZ 1600 /* 1536 */
529 +#define RING_RESET_TIMEOUT 3000 /* 3 secs */
530 +#define RX_RESCHEDULE 64
531 +#define TX_RESCHEDULE 4
532 +static unsigned dma = 0;
533 +module_param (dma, uint, S_IRUGO);
534 +static unsigned sm = 0;
535 +module_param (sm, uint, S_IRUGO);
536 +static unsigned int TXMAXCAP = 512;
537 +module_param (TXMAXCAP, uint, S_IRUGO);
539 +static struct PDMA_txdesc *tx_ring0_cache = NULL;
540 +static struct PDMA_rxdesc *rx_ring0_cache = NULL;
541 +static volatile struct PDMA_rxdesc *rx_ring0_noncache = NULL;
542 +static volatile struct PDMA_txdesc *tx_ring0_noncache = NULL;
543 +static dma_addr_t tx_ring_bus_addr;
544 +static dma_addr_t rx_ring_bus_addr;
546 +static int rx_dma_owner_idx0; /* Point to the next RXD DMA wants to use in RXD Ring#0. */
547 +static int tx_cpu_owner_idx0;
548 +static int tx_need_free_idx0;
550 +static volatile unsigned char *USBRxPackets[NUM_RX_DESC]; /* Receive packets */
551 +static unsigned char tx_zlp_dummy_buf[8];
552 +struct tasklet_struct rx_dma_tasklet;
553 +struct tasklet_struct tx_dma_tasklet;
555 +static struct rt_udc_struct controller;
556 +static struct rt_request *handle_outep(struct rt_ep_struct *rt_ep);
558 +static int debuglevel_read(char *page, char **start, off_t off,int count, int *eof, void *data)
561 + sprintf(page, "%d\n", debuglevel);
562 + len = strlen(page) + 1;
567 +static int debuglevel_write(struct file *file, const char *buffer, unsigned long count, void *data)
570 + count = (count > 32) ? 32 : count;
571 + memset(tmp, 0, 32);
572 + if (copy_from_user(tmp, buffer, count))
574 + debuglevel = simple_strtol(tmp, 0, 10);
578 +static void ep0_chg_stat(const char *label, struct rt_udc_struct *rt_usb, enum ep0_state stat)
580 + xprintk("<0st>%s->%s\n", state_name[rt_usb->ep0state], state_name[stat]);
582 + if (rt_usb->ep0state == stat)
584 + rt_usb->ep0state = stat;
587 +static u8 read_epcs(struct rt_ep_struct *rt_ep)
589 + int idx = EP_NO(rt_ep);
590 + int dir = EP_DIR(rt_ep);
593 + return usb_read(EP0CS);
595 + return (dir == EP_IN ? usb_read(0x7 + idx*8) : usb_read(0x3 + idx*8) );
598 +static void write_epcs(struct rt_ep_struct *rt_ep, u8 val)
600 + int idx = EP_NO(rt_ep);
601 + int dir = EP_DIR(rt_ep);
604 + usb_write(EP0CS, val);
606 + (dir == EP_IN ? /*IN */ usb_write(0x7 + idx*8, val) : usb_write(0x3 + idx*8, val) );
609 +static u16 read_inbc(int epnum)
612 + if(epnum == 0){ /* EP0 */
613 + low = usb_read(IN0BC);
615 + low = usb_read(epnum * 8 + 4);
616 + high = usb_read((epnum * 8 + 4)+1);
618 + return (low | (high << 8));
621 +static u16 read_outbc(int epnum)
624 + if(epnum == 0){ /* EP0 */
625 + low = usb_read(OUT0BC);
627 + low = usb_read(epnum * 8);
628 + high = usb_read((epnum * 8)+1);
630 + return (low | (high << 8));
634 +static void rt_all_eps_reset(void)
636 + // reset(toggle & fifo) all 16 IN & 16 OUT endpoints
637 + usb_write(ENDPRST, 0x10);
638 + usb_write(ENDPRST, 0x70);
639 + usb_write(ENDPRST, 0x00);
640 + usb_write(ENDPRST, 0x60);
643 +static void rt_ep_rst(struct rt_ep_struct *rt_ep)
646 + u8 idx = EP_NO(rt_ep);
647 + u8 dir = EP_DIR(rt_ep);
649 + reg |= ENDPRST_IO | idx;
650 + usb_write(ENDPRST, reg);
652 + reg |= ENDPRST_TOGRST | ENDPRST_FIFORST;
653 + usb_write(ENDPRST, reg);
656 +static void rt_ep_irq_enable(struct rt_ep_struct *rt_ep)
659 + u8 idx = EP_NO(rt_ep);
660 + u8 dir = EP_DIR(rt_ep);
662 + if(idx == 0 /* ep0 */){
663 + usb_write(IN07IEN, (usb_read(IN07IEN) | 0x1) );
664 + usb_write(OUT07IEN, (usb_read(OUT07IEN) | 0x1) );
666 + reg = usb_read(dir ? IN07IEN : OUT07IEN);
667 + reg = reg | (0x1 << idx);
668 + usb_write(dir == EP_IN ? IN07IEN : OUT07IEN, reg);
669 + reg = usb_read(dir ? IN07IEN : OUT07IEN);
673 +static void rt_udc_init_ep(struct rt_udc_struct *rt_usb)
677 +#if defined (CONFIG_RALINK_RT3883) || defined (CONFIG_RALINK_RT3352) || defined (CONFIG_RALINK_MT7620)
678 + usb_write(IN1CON, 0x8D); // InEP1 : Int, 2 subfifos
679 + usb_write(IN2CON, 0x89); // InEP2 : Bulk, 2 subfifos
680 + usb_write(OUT1CON, 0x8D); // OutEP1 : Int, 2 subfifos
681 + usb_write(OUT2CON, 0x89); // OutEP2 : Bulk, 2 subfifos
682 + //usb_write(OUT3CON, 0x89); // OutEP3 : Bulk, 2 subfifos
683 + //usb_write(OUT4CON, 0x89); // OutEP4 : Bulk, 2 subfifos
684 +#elif defined (CONFIG_RALINK_RT5350)
685 + usb_write(IN1CON, 0x89); // InEP1 : BULK, 2 subfifos
686 + usb_write(OUT1CON, 0x89); // OutEP1 : BULK, 2 subfifos
688 +#error "define a platform"
691 +#if defined (CONFIG_RALINK_RT3883) || defined (CONFIG_RALINK_RT3352) || defined (CONFIG_RALINK_MT7620)
692 + usb_write(IN1CON, 0x8C); // InEP1 : Int , 1 subfifos
693 + usb_write(IN2CON, 0x88); // InEP2 : Bulk, 1 subfifo
694 + usb_write(OUT1CON, 0x8C); // OutEP1 : Int, 1 subfifos
695 + usb_write(OUT2CON, 0x88); // OutEP2 : Bulk, 1 subfifos
696 + //usb_write(OUT3CON, 0x88); // OutEP3 : Bulk, 1 subfifo
697 + //usb_write(OUT4CON, 0x88); // OutEP4 : Bulk. 1 subfifo
699 +#elif defined (CONFIG_RALINK_RT5350)
700 + usb_write(IN1CON, 0x88); // InEP1 : BULK , 1 subfifos
701 + usb_write(OUT1CON, 0x88); // OutEP1 : BULK, 1 subfifos
703 +#error "define a platform"
706 + // clear all pending HW interrupts
707 + usb_write(IN07IRQ, 0xFF);
708 + usb_write(OUT07IRQ, 0xFF);
709 + rt_all_eps_reset();
710 + rt_ep_irq_enable(&rt_usb->rt_ep[0]);
713 +static void rt_udc_init_fifo(struct rt_udc_struct *rt_usb)
717 + usb_write(FIFOCTRL, 0x31); // INEP1, Autoin = 1
718 + usb_write(FIFOCTRL, 0x32); // INEP2, Autoin = 1
719 + usb_write(FIFOCTRL, 0x21); // OUTEP1, Autoin = 1
720 + usb_write(FIFOCTRL, 0x22); // OUTEP2, Autoin = 1
721 + //usb_write(FIFOCTRL, 0x23);// OUTEP3, Autoin = 1
722 + //usb_write(FIFOCTRL, 0x24);// OUTEP4, Autoin = 1
724 + usb_write(FIFOCTRL, 0x00); // Access by DMA
726 + usb_write(FIFOCTRL, 0x11); // INEP1, Autoin = 0
727 + usb_write(FIFOCTRL, 0x12); // INEP2, Autoin = 0
728 + usb_write(FIFOCTRL, 0x01); // OUTEP1, Autoin = 0
729 + usb_write(FIFOCTRL, 0x02); // OUTEP2, Autoin = 0
730 + //usb_write(FIFOCTRL, 0x03);// OUTEP3, Autoin = 0
731 + //usb_write(FIFOCTRL, 0x04);// OUTEP4, Autoin = 0
733 + usb_write(FIFOCTRL, 0x80); // Access by CPU
737 +static void rt_udc_init(struct rt_udc_struct *rt_usb)
739 + /* Setup & Init endpoints */
740 + rt_udc_init_ep(rt_usb);
742 + // Enable HS, reset, suspend, SETUP valid data interrupt
743 + usb_write(USBIRQ, 0xff); // clear first
744 + usb_write(USBIEN, 0x21);
746 + /* Setup ep fifos */
747 + rt_udc_init_fifo(rt_usb);
750 +static void rt_ep_irq_disable(struct rt_ep_struct *rt_ep)
753 + u8 idx = EP_NO(rt_ep);
754 + u8 dir = EP_DIR(rt_ep);
756 + if(idx == 0 /* ep0 */){
757 + usb_write(IN07IEN, (usb_read(IN07IEN) & ~(0x1)) );
758 + usb_write(OUT07IEN, (usb_read(OUT07IEN) & ~(0x1)) );
760 + reg = usb_read(dir ? IN07IEN : OUT07IEN);
761 + reg = reg & ~(0x1 << idx);
762 + usb_write(dir == EP_IN ? IN07IEN : OUT07IEN, reg);
763 + reg = usb_read(dir ? IN07IEN : OUT07IEN);
767 +static u32 rt_fifo_bcount(struct rt_ep_struct *rt_ep)
772 + int idx = EP_NO(rt_ep);
773 + int dir = EP_DIR(rt_ep);
779 + low = usb_read(0x004 + idx*8);
780 + high = usb_read( (0x004 + idx*8)+1 );
782 + low = usb_read(0x000 + idx*8);
783 + high = usb_read( (0x000 + idx*8)+1 );
789 +void rt_flush(struct rt_ep_struct *rt_ep)
794 +void rt_ep_stall(struct rt_ep_struct *rt_ep, int value)
798 + int idx = EP_NO(rt_ep);
799 + int dir = EP_DIR(rt_ep);
802 + tmp = usb_read(EP0CS);
807 + usb_write(EP0CS, tmp);
809 + addr = (dir == EP_IN ? 0x006 : 0x002) + idx * 8;
810 + tmp = usb_read(addr);
815 + usb_write(addr, tmp);
821 +static int rt_udc_get_frame(struct usb_gadget *_gadget)
826 +static int rt_udc_wakeup(struct usb_gadget *_gadget)
833 +/*******************************************************************************
834 + * USB request control functions
835 + *******************************************************************************
837 +static inline void ep_add_request(struct rt_ep_struct *rt_ep, struct rt_request *req)
839 + if (unlikely(!req))
842 + req->zlp_dma_done = 0;
843 + req->rt_ep = rt_ep;
844 + list_add_tail(&req->queue, &rt_ep->queue);
847 +static inline void ep_del_request(struct rt_ep_struct *rt_ep, struct rt_request *req)
849 + if (unlikely(!req))
851 + list_del_init(&req->queue);
852 + req->zlp_dma_done = 0;
856 +static void done(struct rt_ep_struct *rt_ep, struct rt_request *req, int status)
858 + ep_del_request(rt_ep, req);
860 + if (likely(req->req.status == -EINPROGRESS))
861 + req->req.status = status;
863 + status = req->req.status;
865 + if (status && status != -ESHUTDOWN)
866 + D_ERR(rt_ep->rt_usb->dev, "<%s> complete %s req %p stat %d len %u/%u\n", __func__, rt_ep->ep.name, &req->req, status,req->req.actual, req->req.length);
868 + req->req.complete(&rt_ep->ep, &req->req);
873 +struct tasklet_struct rx_tasklet_tmp;
874 +static void rx_done_do_tasklet(unsigned long arg)
876 + struct rt_ep_struct *rt_ep;
877 + struct rt_request *rt_req;
878 + struct usb_request *usb_req;
880 + int i, rx_count, status = 0;
881 + struct rt_udc_struct *rt_usb = &controller;
883 + for (i = (IN_EP_NUM+1); i < RT_USB_NB_EP; i++) {
884 + rt_ep = &rt_usb->rt_ep[i];
887 + // shared by irq handler, protect it
888 + spin_lock_irqsave(&rx_done_lock, rx_done_lock_flags);
889 + rx_count = rt_ep->rx_done_count;
891 + //spin_unlock_irqrestore(&rx_done_lock, rx_done_lock_flags);
893 + for (;rx_count > 0; rx_count--) {
894 + if(unlikely(list_empty(&rt_ep->queue)))
895 + FATAL_ERROR("empty queue");
897 + rt_req = list_entry(rt_ep->queue.next, struct rt_request, queue);
898 + usb_req = &rt_req->req;
900 + ep_del_request(rt_ep, rt_req);
901 + rt_ep->rx_done_count--;
903 + spin_unlock_irqrestore(&rx_done_lock, rx_done_lock_flags);
905 + if (unlikely(rt_req->req.status == -EINPROGRESS))
906 + rt_req->req.status = status;
908 + status = rt_req->req.status;
910 + if (unlikely(status && status != -ESHUTDOWN))
911 + D_ERR(rt_ep->rt_usb->dev, "<%s> complete %s req %p stat %d len %u/%u\n", __func__, rt_ep->ep.name, &rt_req->req, status,rt_req->req.actual, rt_req->req.length);
913 + // indicate gadget driver.
914 + usb_req->complete(ep, usb_req);
916 + spin_lock_irqsave(&rx_done_lock, rx_done_lock_flags);
918 + spin_unlock_irqrestore(&rx_done_lock, rx_done_lock_flags);
923 +struct tasklet_struct tx_tasklet;
924 +static void tx_do_tasklet(unsigned long arg)
929 +struct tasklet_struct rx_tasklet;
930 +static void rx_do_tasklet(unsigned long arg)
932 + struct rt_ep_struct *rt_ep;
933 + struct rt_request *req;
936 + struct rt_udc_struct *rt_usb = &controller;
938 + for (i = (IN_EP_NUM+1/* EP0 */); i < RT_USB_NB_EP; i++){
940 + rt_ep = &rt_usb->rt_ep[i];
943 + epcs = read_epcs(rt_ep);
944 + while(!(epcs & EP_CS_BSY)){
945 + req = handle_outep(rt_ep);
947 + // No usb request found.
948 + // Just set up the flag (pending) and clear int.
949 + rt_ep->pending = 1;
952 + if(req && ( (req->req.actual % rt_ep->ep.maxpacket) || (req->req.actual >= req->req.length))){
953 + xprintk("q.l=%d,q.a=%d\n", req->req.length, req->req.actual);
954 + done(rt_ep, req, 0);
958 + epcs = read_epcs(rt_ep);
959 + write_epcs(rt_ep, 0x0);
960 + epcs = read_epcs(rt_ep);
965 +static void nuke(struct rt_ep_struct *rt_ep, int status)
967 + struct rt_request *req;
970 + while (!list_empty(&rt_ep->queue)) {
971 + req = list_entry(rt_ep->queue.next, struct rt_request, queue);
972 + done(rt_ep, req, status);
977 + *******************************************************************************
978 + * Data tansfer over USB functions
979 + *******************************************************************************
981 +static int read_ep0_fifo(struct rt_ep_struct *rt_ep, struct rt_request *req)
984 + int byte_count, req_bufferspace, count, i;
988 + FATAL_ERROR("not irq context.");
990 + byte_count = read_outbc(EP_NO(rt_ep));
991 + req_bufferspace = req->req.length - req->req.actual;
993 + buf = req->req.buf + req->req.actual;
995 + if(!req_bufferspace)
996 + FATAL_ERROR("zlp");
998 + if(byte_count > req_bufferspace)
999 + FATAL_ERROR("buffer overflow, byte_count=%d, req->req.length=%d, req->req.actual=%d\n", byte_count, req->req.length ,req->req.actual);
1001 + count = min(byte_count, req_bufferspace);
1003 + //test, Access by CPU
1005 + usb_write(FIFOCTRL, 0x80);
1007 + for (i = 0; i < count; i++){
1008 + *buf = usb_read(EP0OUTDAT+i);
1011 + req->req.actual += count;
1013 + //test, Access by DMA
1015 + usb_write(FIFOCTRL, 0x00);
1021 +static int read_ep_fifo(struct rt_ep_struct *rt_ep, struct rt_request *req)
1023 + u8 *buf, ep_no, ep_no_shift;
1024 + int byte_count, req_bufferspace, count, i;
1027 + ep_no = EP_NO(rt_ep);
1029 + byte_count = read_outbc(ep_no);
1030 + if(unlikely(!byte_count))
1031 + FATAL_ERROR("ep_no:%d bc = 0", ep_no);
1033 + req_bufferspace = req->req.length - req->req.actual;
1035 + buf = req->req.buf + req->req.actual;
1037 + if(unlikely(!req_bufferspace))
1038 + FATAL_ERROR("zlp");
1040 + xprintk("bc=%d,r.l=%d,r.a=%d\n", byte_count, req->req.length ,req->req.actual);
1041 + if(unlikely(byte_count > req_bufferspace))
1042 + FATAL_ERROR("buffer overflow, byte_count=%d, req->req.length=%d, req->req.actual=%d\n", byte_count, req->req.length ,req->req.actual);
1044 + count = min(byte_count, req_bufferspace);
1046 + ep_no_shift = 0x80+ep_no * 4;
1047 + for (i = 0; i < count; i++){
1048 + *buf = usb_read(ep_no_shift);
1052 + req->req.actual += count;
1054 + // EP Out irq handler would arm another transaction.
1058 +static int write_ep_fifo_zlp(struct rt_ep_struct *rt_ep)
1061 + int ep_no = EP_NO(rt_ep);
1064 + xprintk("w%d ZLP\n", EP_NO(rt_ep));
1065 + epcs = read_epcs(rt_ep);
1066 + if(epcs & EP_CS_BSY)
1067 + FATAL_ERROR("EP%d busy. cs=%x\n", ep_no, epcs);
1069 + /* check INEP byte count is zero? */
1070 + if(read_inbc(ep_no))
1071 + FATAL_ERROR("EP%d bc zero. bc=%d\n", ep_no, read_inbc(ep_no));
1073 + epcs = read_epcs(rt_ep);
1074 + write_epcs(rt_ep, epcs);
1080 + * handle_epinirq()
1082 +static int write_ep_fifo(struct rt_ep_struct *rt_ep, struct rt_request *req)
1085 + int length, i, ep_no = EP_NO(rt_ep);
1088 + xprintk("w ep%d req=%p,r.l=%d,r.a=%d\n",EP_NO(rt_ep),&req->req,req->req.length,req->req.actual);
1089 + epcs = read_epcs(rt_ep);
1090 + if(epcs & EP_CS_BSY)
1091 + FATAL_ERROR("EP%d busy. epcs=%x\n", ep_no, epcs);
1093 + /* check INEP byte count is zero? */
1094 + if(read_inbc(ep_no))
1095 + FATAL_ERROR("EP%d bc=%d\n", ep_no, read_inbc(ep_no));
1097 + buf = req->req.buf + req->req.actual;
1098 + length = (req->req.length - req->req.actual) < rt_ep->ep.maxpacket ? (req->req.length - req->req.actual) : rt_ep->ep.maxpacket;
1099 + req->req.actual += length;
1100 + if (!length) { /* zlp */
1102 + xprintk("<%s> zero packet\n", __func__);
1103 + write_ep_fifo_zlp(rt_ep);
1107 + // write to ep in fifo
1108 + for (i=0; i< length; i++)
1109 + usb_write(0x80+ep_no*4, *buf++);
1111 + epcs = read_epcs(rt_ep);
1112 + write_epcs(rt_ep, epcs);
1120 +static int write_ep0_fifo(struct rt_ep_struct *rt_ep, struct rt_request *req)
1127 + xprintk("q.l=%d, q.a=%d, maxp=%d\n", req->req.length, req->req.actual, rt_ep->ep.maxpacket);
1129 + buf = req->req.buf + req->req.actual;
1130 + maxpacket = (u32)(rt_ep->ep.maxpacket);
1131 + length = min(req->req.length - req->req.actual, maxpacket);
1133 + req->req.actual += length;
1135 + if (!length && req->req.zero)
1136 + FATAL_ERROR("zlp");
1139 + FATAL_ERROR("Not in irq context");
1141 + //test, Access by CPU
1143 + usb_write(FIFOCTRL, 0x80);
1145 + //write to ep0in fifo
1146 + for (i=0; i< length; i++)
1147 + usb_write(EP0INDAT+i, *buf++);
1150 + usb_write(IN0BC, length);
1151 + if(length != rt_ep->ep.maxpacket)
1152 + usb_write(EP0CS, 0x2); // clear NAK bit to ACK host.
1154 + //test, Access by CPU
1156 + usb_write(FIFOCTRL, 0x00);
1161 +static struct rt_request *get_unhandled_req(struct rt_ep_struct *rt_ep)
1163 + struct list_head *p;
1164 + struct rt_request *req = NULL;
1166 + if(EP_DIR(rt_ep) == EP_OUT)
1167 + FATAL_ERROR("Out EP");
1170 + list_for_each(p, &rt_ep->queue){
1171 + req = list_entry(p, struct rt_request, queue);
1172 + if(req->req.length > req->req.actual )
1174 + else if(unlikely(req->req.length == 0 && req->zlp_dma_done == 0))
1181 + if (!list_empty(&rt_ep->queue)){
1182 + req = list_entry(rt_ep->queue.next, struct rt_request, queue);
1184 + FATAL_ERROR("%s No request", rt_ep->ep.name);
1190 +#define PADDING_LENGTH 64
1191 +static int write_dma_txring(struct rt_ep_struct *rt_ep,struct rt_request *req)
1195 + int retry_times = 0;
1196 + u32 hw_current_idx;
1198 + xprintk("w%dr=%p,r.l=%d,r.a=%d\n", EP_NO(rt_ep), &req->req,req->req.length,req->req.actual);
1200 + length = req->req.length;
1202 + while(length > 0 || (req->req.length == 0 && req->zlp_dma_done == 0)){
1204 + /* wait for a free TXD */
1205 + hw_current_idx = reg_read(RTUSB_TX_DTX_IDX0);
1206 + if ( tx_ring0_cache[tx_cpu_owner_idx0].txd_info2.DDONE_bit == 0 ||
1207 + ((tx_cpu_owner_idx0+1) % NUM_TX_DESC == hw_current_idx) ) {
1208 + if(retry_times > 1000)
1215 + if(length > TXMAXCAP)
1216 + length = TXMAXCAP;
1218 + buf = req->req.buf + req->req.actual;
1219 + req->req.actual += length;
1221 + /* deal with ZLP.*/
1222 + if(req->req.length == 0 && req->zlp_dma_done == 0)
1223 + req->zlp_dma_done = 1;
1227 +#define phys_to_bus(a) ((u32)a & 0x1FFFFFFF)
1229 + tx_ring0_cache[tx_cpu_owner_idx0].txd_info1.SDP0 = cpu_to_le32(phys_to_bus(buf));
1230 + tx_ring0_cache[tx_cpu_owner_idx0].txd_info2.SDL0 = cpu_to_le32(length);
1231 + tx_ring0_cache[tx_cpu_owner_idx0].txd_info4.zlp_flag = 0;
1232 + dma_cache_sync(NULL, (void *)buf, length, DMA_TO_DEVICE);
1234 + tx_ring0_cache[tx_cpu_owner_idx0].txd_info1.SDP0 = cpu_to_le32(phys_to_bus(tx_zlp_dummy_buf));
1235 + tx_ring0_cache[tx_cpu_owner_idx0].txd_info2.SDL0 = cpu_to_le32(sizeof(tx_zlp_dummy_buf));
1236 + tx_ring0_cache[tx_cpu_owner_idx0].txd_info4.zlp_flag = 1;
1239 + tx_ring0_cache[tx_cpu_owner_idx0].txd_info4.In_ep_addr = cpu_to_le32(EP_NO(rt_ep));
1240 + tx_ring0_cache[tx_cpu_owner_idx0].txd_info2.DDONE_bit = 0;
1241 + tx_cpu_owner_idx0 = (tx_cpu_owner_idx0 + 1) % NUM_TX_DESC;
1243 + length = req->req.length - req->req.actual;
1246 + reg_write(RTUSB_TX_CTX_IDX0, tx_cpu_owner_idx0);
1252 +/*******************************************************************************
1253 + * Endpoint handlers
1254 + *******************************************************************************
1258 + * Handle In Endpoint
1260 + * Enqueue -> Write fifo -> TX_DONE -> Write fifo -> TX_DONE -> ..
1263 + * Enqueue -> Kick off TxD. Enqueue -> Kick off TxD. Enqueue -> Kick off TxD.
1265 +static int handle_inep(struct rt_ep_struct *rt_ep)
1267 + struct rt_request *req;
1270 + if(!(req = get_unhandled_req(rt_ep)))
1274 + write_dma_txring(rt_ep, req);
1276 + write_ep_fifo(rt_ep, req);
1277 + rt_ep->tx_done_count = 1;
1285 +static struct rt_request *handle_outep(struct rt_ep_struct *rt_ep)
1287 + struct rt_request *req;
1288 + struct list_head *p;
1292 + if (list_empty(&rt_ep->queue)){
1296 + list_for_each(p, &rt_ep->queue){
1297 + if(count != rt_ep->rx_done_count){
1301 + req = list_entry(p, struct rt_request, queue);
1302 + read_ep_fifo(rt_ep, req);
1309 +static struct rt_request *handle_inep0(struct rt_ep_struct *rt_ep)
1311 + struct rt_request *req = NULL;
1314 + if (list_empty(&rt_ep->queue)) {
1315 + D_ERR(rt_ep->rt_usb->dev, "<%s> no request on %s\n", __func__, rt_ep->ep.name);
1319 + req = list_entry(rt_ep->queue.next, struct rt_request, queue);
1320 + switch (rt_ep->rt_usb->ep0state) {
1321 + case EP0_IN_DATA_PHASE: /* GET_DESCRIPTOR */
1322 + write_ep0_fifo(rt_ep, req);
1326 + //case EP0_OUT_DATA_PHASE: /* SET_DESCRIPTOR */
1327 + //case EP0_NO_DATA_PHASE: /* for no data stage control transfer */
1330 + D_ERR(rt_ep->rt_usb->dev, "<%s> ep0 i/o, odd state %d\n", __func__, rt_ep->rt_usb->ep0state);
1331 + ep_del_request(rt_ep, req);
1339 +static struct rt_request *handle_outep0(struct rt_ep_struct *rt_ep)
1341 + struct rt_request *req = NULL;
1344 + if (list_empty(&rt_ep->queue)) {
1345 + D_ERR(rt_ep->rt_usb->dev, "<%s> no request on %s\n", __func__, rt_ep->ep.name);
1349 + if(rt_ep->rt_usb->ep0state != EP0_OUT_DATA_PHASE){
1350 + D_EP0(rt_ep->rt_usb->dev, "<%s> ep0 i/o, odd state %d\n", __func__, rt_ep->rt_usb->ep0state);
1351 + ep_del_request(rt_ep, req);
1355 + req = list_entry(rt_ep->queue.next, struct rt_request, queue);
1357 + read_ep0_fifo(rt_ep, req);
1362 +/*******************************************************************************
1363 + * USB gadget callback functions
1364 + *******************************************************************************
1366 +static void handle_dma_rxdone(struct rt_udc_struct *rt_usb)
1369 + tasklet_schedule(&rx_dma_tasklet);
1372 +static void handle_dma_txdone(struct rt_udc_struct *rt_usb)
1375 + tasklet_schedule(&tx_dma_tasklet);
1378 +static void handle_dmairq(struct rt_udc_struct *rt_usb, u32 irq)
1380 + if(irq & RTUSB_RX_DONE_INT0){
1381 + handle_dma_rxdone(rt_usb);
1384 + if(irq & RTUSB_TX_DONE_INT0){
1385 + handle_dma_txdone(rt_usb);
1388 + reg_write(RTUSB_INT_STATUS, irq);
1391 +static inline int udc_dma_reset_txring(void)
1396 + while(count++< RING_RESET_TIMEOUT){
1397 + reg = reg_read(RTUSB_PDMA_GLO_CFG);
1398 + if(reg & RTUSB_TX_DMA_BUSY){
1404 + if(count== RING_RESET_TIMEOUT)
1407 + reg = reg_read(RTUSB_PDMA_RST_IDX);
1409 + reg |= (RTUSB_RST_DTX_IDX1 | RTUSB_RST_DTX_IDX0);
1410 + reg_write(RTUSB_PDMA_RST_IDX, reg);
1415 +static inline int udc_dma_reset_rxring(void)
1420 + while(count++< RING_RESET_TIMEOUT){
1421 + reg = reg_read(RTUSB_PDMA_GLO_CFG);
1422 + if(reg & RTUSB_RX_DMA_BUSY){
1427 + if(count== RING_RESET_TIMEOUT)
1430 + reg = reg_read(RTUSB_PDMA_RST_IDX);
1432 + reg |= (RTUSB_RST_DRX_IDX1 | RTUSB_RST_DRX_IDX0);
1433 + reg_write(RTUSB_PDMA_RST_IDX, reg);
1438 +static int udc_dma_hw_reset(void)
1440 + if(udc_dma_reset_rxring() == -1)
1442 + if(udc_dma_reset_txring() == -1)
1447 +static void udc_dma_enable(int enable)
1450 + reg = reg_read(RTUSB_PDMA_GLO_CFG);
1453 + reg |= RTUSB_TX_WB_DDONE | RTUSB_RX_DMA_EN | RTUSB_TX_DMA_EN ;
1455 + reg &= ~(RTUSB_TX_WB_DDONE | RTUSB_RX_DMA_EN | RTUSB_TX_DMA_EN) ;
1456 + reg_write(RTUSB_PDMA_GLO_CFG, reg);
1460 +static void udc_dma_int_enable(int enable)
1463 + reg = reg_read(RTUSB_INT_MASK);
1466 + reg |= RTUSB_RX_DONE_INT_MSK0 | RTUSB_TX_DONE_INT_MSK0 ;
1468 + reg &= ~(RTUSB_RX_DONE_INT_MSK0 | RTUSB_TX_DONE_INT_MSK0) ;
1469 + reg_write(RTUSB_INT_MASK, reg);
1473 +static inline void udc_dma_tx_int_clear(void)
1475 + reg_write(RTUSB_INT_STATUS, 0x0000000F);
1478 +static inline void udc_dma_rx_int_clear(void)
1480 + reg_write(RTUSB_INT_STATUS, 0x00030000);
1483 +static inline void udc_dma_all_int_clear(void)
1485 + reg_write(RTUSB_INT_STATUS, 0xFFFFFFFF);
1488 +static int copy_data_to_ep(void *src, int length, int ep_num)
1490 + struct rt_ep_struct *rt_ep;
1491 + struct rt_udc_struct *rt_usb = &controller;
1492 + struct rt_request *req;
1493 + int req_bufferspace, count;
1497 + rt_ep = &rt_usb->rt_ep[ep_num+IN_EP_NUM];
1499 + if (list_empty(&rt_ep->queue)){
1500 + /* It is safe to return 0 if no req queued. */
1504 + req = list_entry(rt_ep->queue.next, struct rt_request, queue);
1505 + req_bufferspace = req->req.length - req->req.actual;
1507 + if(unlikely(!req_bufferspace)){
1509 + FATAL_ERROR("zlp");
1513 + if(length > req_bufferspace){
1514 + FATAL_ERROR("buffer overflow");
1518 + // sync with cache.
1519 + if(likely(length))
1520 + dma_cache_sync(NULL, src, length, DMA_FROM_DEVICE);
1522 + buf = req->req.buf + req->req.actual;
1523 + count = min(length, req_bufferspace);
1524 + memcpy(buf, src, count);
1526 + req->req.actual += count;
1528 + if((req->req.actual % rt_ep->ep.maxpacket) || (req->req.actual >= req->req.length)){
1529 + done(rt_ep, req, 0); // short packet indicates transaction is done.
1534 +static void rx_dma_done_do_tasklet(unsigned long arg)
1539 + int processed_count=0;
1543 + if (rx_ring0_cache[rx_dma_owner_idx0].rxd_info2.DDONE_bit == 0)
1546 + if(processed_count++ > RX_RESCHEDULE){
1547 + tasklet_schedule(&rx_dma_tasklet);
1551 + length = rx_ring0_cache[rx_dma_owner_idx0].rxd_info4.Rx_bcnt;
1552 + ep = rx_ring0_cache[rx_dma_owner_idx0].rxd_info4.Out_ep_addr;
1554 + // copy data from RXD->buffer to ep queue.
1555 + rc = copy_data_to_ep((void *)USBRxPackets[rx_dma_owner_idx0], length, ep);
1559 + rxd_info = (u32 *)&rx_ring0_cache[rx_dma_owner_idx0].rxd_info4;
1562 + /* clear DDONE bit*/
1563 + rxd_info = (u32 *)&rx_ring0_cache[rx_dma_owner_idx0].rxd_info2;
1565 + //rx_ring0_cache[rx_dma_owner_idx0].rxd_info2.DDONE_bit = 0;
1566 + //rx_ring0_cache[i].rxd_info2.LS0= 0;
1567 + rx_ring0_cache[rx_dma_owner_idx0].rxd_info2.PLEN0= sizeof(u8) * RX_BUFF_SZ;
1569 + /* Move point to next RXD which wants to alloc */
1570 + //OUTL(cpu_to_le32((u32) rx_dma_owner_idx0), RTUSB_RX_CALC_IDX0);
1571 + reg_write(RTUSB_RX_CALC_IDX0, rx_dma_owner_idx0);
1573 + /* Update to Next packet point that was received.
1575 + rx_dma_owner_idx0 = (rx_dma_owner_idx0 + 1) % NUM_RX_DESC;
1580 + * Recycle reqs and call gadget complete callback function.
1582 +static void tx_dma_done_do_tasklet(unsigned long arg)
1586 + struct rt_ep_struct *rt_ep;
1587 + struct rt_request *rt_req;
1588 + struct rt_udc_struct *rt_usb = &controller;
1591 + while(tx_need_free_idx0 != (hw_current = reg_read(RTUSB_TX_DTX_IDX0))){
1593 + while(tx_ring0_cache[tx_need_free_idx0].txd_info2.DDONE_bit != 1){
1597 + FATAL_ERROR("tx timeout");
1600 + // rt_ep = tx_ring0_req_mapping[tx_need_free_idx0];
1601 + ep_num = tx_ring0_cache[tx_need_free_idx0].txd_info4.In_ep_addr;
1602 + if(!ep_num || ep_num > IN_EP_NUM)
1603 + FATAL_ERROR("Out of range");
1605 + rt_ep = &rt_usb->rt_ep[ep_num];
1606 + if(list_empty(&rt_ep->queue))
1607 + FATAL_ERROR("ep[%d] No request", ep_num);
1609 + rt_req = list_entry(rt_ep->queue.next, struct rt_request, queue);
1610 + rt_req->txd_count--;
1613 + if(rt_req->txd_count == 0)
1614 + done(rt_ep, rt_req, 0);
1616 + tx_need_free_idx0 = (tx_need_free_idx0 + 1) % NUM_TX_DESC;
1621 +static int udc_dma_rst(void)
1623 + if( udc_dma_reset_txring() == -1)
1625 + if( udc_dma_reset_rxring() == -1)
1628 + tx_cpu_owner_idx0 = 0;
1629 + tx_need_free_idx0 = 0;
1630 + rx_dma_owner_idx0 = 0;
1631 + reg_write(RTUSB_RX_CALC_IDX0, cpu_to_le32(NUM_RX_DESC - 1));
1635 +static int rt_udc_dma_init(void)
1639 + if( udc_dma_hw_reset() == -1)
1642 + for(i=0; i<NUM_RX_DESC; i++){
1643 + USBRxPackets[i] = kmalloc(sizeof(u8) * RX_BUFF_SZ, GFP_ATOMIC | GFP_DMA); // todo: use GFP_KERNEL instead.
1644 + if(!USBRxPackets[i]){
1645 + for(i=i-1; i>=0; i--)
1646 + kfree((void *)USBRxPackets[i]);
1647 + printk("No mem.");
1652 + tx_ring0_cache = dma_alloc_coherent(NULL, sizeof(struct PDMA_txdesc) * NUM_TX_DESC, &tx_ring_bus_addr, GFP_KERNEL);
1653 + rx_ring0_cache = dma_alloc_coherent(NULL, sizeof(struct PDMA_rxdesc) * NUM_RX_DESC, &rx_ring_bus_addr, GFP_KERNEL);
1655 + printk("USB PDMA mode enabled.\n");
1656 + printk("tx_ring=%p\n", tx_ring0_cache);
1657 + printk("rx_ring=%p\n", rx_ring0_cache);
1659 + tx_ring0_noncache = tx_ring0_cache;
1660 + rx_ring0_noncache = rx_ring0_cache;
1662 + for(i=0; i < NUM_RX_DESC; i++){
1663 + memset((void *)&rx_ring0_noncache[i], 0, 16 /* sizeof()*/);
1664 + rx_ring0_noncache[i].rxd_info2.DDONE_bit = 0;
1665 + rx_ring0_noncache[i].rxd_info2.LS0= 0;
1666 + rx_ring0_noncache[i].rxd_info2.PLEN0= sizeof(u8) * RX_BUFF_SZ;
1667 + rx_ring0_noncache[i].rxd_info1.PDP0 = dma_map_single(NULL, (void *)USBRxPackets[i], sizeof(u8) * RX_BUFF_SZ, DMA_FROM_DEVICE);
1670 + for (i=0; i < NUM_TX_DESC; i++) {
1671 + memset((void *)&tx_ring0_noncache[i],0, 16 /* sizeof()*/);
1672 + tx_ring0_noncache[i].txd_info2.LS0_bit = 1;
1673 + tx_ring0_noncache[i].txd_info2.DDONE_bit = 1;
1674 + // we would map dma buffer dynamically in IRQ handler & ep_queue();
1677 + rx_dma_owner_idx0 = 0;
1678 + tx_cpu_owner_idx0 = 0;
1679 + tx_need_free_idx0 = 0;
1681 + /* initial UDMA register */
1682 + //OUTL(cpu_to_le32((u32) UDMA_Init_Setting), RTUSB_UDMA_CTRL);
1685 + printk("Storage mode enabled.\n");
1686 + reg_write(RTUSB_UDMA_CTRL, 0x3F000063); /* enable storage mode */
1688 + reg_write(RTUSB_UDMA_CTRL, 0x3F000003);
1691 + /* Tell the adapter where the TX/RX rings are located. */
1692 + //OUTL(phys_to_bus((u32) &rx_ring[0]), RTUSB_RX_BASE_PTR0);
1693 + reg_write(RTUSB_RX_BASE_PTR0, rx_ring_bus_addr);
1695 + //OUTL(phys_to_bus((u32) &tx_ring0[0]), RTUSB_TX_BASE_PTR0);
1696 + reg_write(RTUSB_TX_BASE_PTR0, tx_ring_bus_addr);
1698 + //OUTL(cpu_to_le32((u32) NUM_RX_DESC), RTUSB_RX_MAX_CNT0);
1699 + //OUTL(cpu_to_le32((u32) NUM_TX_DESC), RTUSB_TX_MAX_CNT0);
1700 + reg_write(RTUSB_RX_MAX_CNT0, cpu_to_le32(NUM_RX_DESC));
1701 + reg_write(RTUSB_TX_MAX_CNT0, cpu_to_le32(NUM_TX_DESC));
1703 + //OUTL(cpu_to_le32((u32) tx_cpu_owner_idx0), RTUSB_TX_CTX_IDX0);
1704 + //OUTL(cpu_to_le32((u32) (NUM_RX_DESC - 1)), RTUSB_RX_CALC_IDX0);
1705 + reg_write(RTUSB_TX_CTX_IDX0, cpu_to_le32(tx_cpu_owner_idx0));
1706 + reg_write(RTUSB_RX_CALC_IDX0, cpu_to_le32(NUM_RX_DESC - 1));
1712 +static int udc_dma_fini(void)
1718 + udc_dma_enable(false);
1719 + udc_dma_int_enable(false);
1721 + /* restore UDMA register */
1722 + reg_write(RTUSB_UDMA_CTRL, 0x0);
1724 + // unmap & free RX buffer
1725 + for(i=0; i<NUM_RX_DESC; i++){
1726 + addr = rx_ring0_noncache[i].rxd_info1.PDP0;
1728 + dma_unmap_single(NULL, addr, sizeof(u8) * RX_BUFF_SZ, DMA_FROM_DEVICE);
1729 + kfree((void *)USBRxPackets[i]);
1732 + // unmap Tx buffer only(but not free it)
1733 + for(i=0; i<NUM_TX_DESC; i++){
1734 + addr = tx_ring0_noncache[i].txd_info1.SDP0;
1736 + len = tx_ring0_noncache[i].txd_info2.SDL0;
1737 + dma_unmap_single(NULL, addr, sizeof(u8) * len, DMA_TO_DEVICE);
1741 + dma_free_coherent(NULL, sizeof(struct PDMA_txdesc) * NUM_TX_DESC, tx_ring0_cache, tx_ring_bus_addr);
1742 + dma_free_coherent(NULL, sizeof(struct PDMA_rxdesc) * NUM_RX_DESC, rx_ring0_cache, rx_ring_bus_addr);
1747 +static int rt_ep_enable(struct usb_ep *usb_ep, const struct usb_endpoint_descriptor *desc)
1749 + struct rt_ep_struct *rt_ep = container_of(usb_ep, struct rt_ep_struct, ep);
1750 + struct rt_udc_struct *rt_usb = rt_ep->rt_usb;
1751 + unsigned long flags;
1755 + if (!usb_ep || !desc || !EP_NO(rt_ep) || desc->bDescriptorType != USB_DT_ENDPOINT || rt_ep->bEndpointAddress != desc->bEndpointAddress) {
1756 + D_ERR(rt_usb->dev, "<%s> bad ep or descriptor\n", __func__);
1759 + if (rt_ep->bmAttributes != desc->bmAttributes) {
1760 + D_ERR(rt_usb->dev, "<%s> %s type mismatch, 0x%x, 0x%x\n", __func__, usb_ep->name, rt_ep->bmAttributes, desc->bmAttributes);
1763 + if (!rt_usb->driver || rt_usb->gadget.speed == USB_SPEED_UNKNOWN) {
1764 + D_ERR(rt_usb->dev, "<%s> bogus device state\n", __func__);
1765 + return -ESHUTDOWN;
1767 + local_irq_save(flags);
1768 + rt_ep->stopped = 0;
1770 + //rt_ep_irq_enable(rt_ep);
1772 + rt_ep_irq_enable(rt_ep);
1773 + local_irq_restore(flags);
1775 + xprintk("<%s> ENABLED %s\n", __func__, usb_ep->name);
1779 +static int rt_ep_disable(struct usb_ep *usb_ep)
1781 + struct rt_ep_struct *rt_ep = container_of(usb_ep, struct rt_ep_struct, ep);
1782 + unsigned long flags;
1785 + if (!usb_ep || !EP_NO(rt_ep) /* || !list_empty(&rt_ep->queue) */) {
1786 + D_ERR(rt_ep->rt_usb->dev, "<%s> %s can not be disabled\n", __func__, usb_ep ? rt_ep->ep.name : NULL);
1790 + local_irq_save(flags);
1791 + rt_ep->stopped = 1;
1792 + nuke(rt_ep, -ESHUTDOWN);
1794 + rt_ep_irq_disable(rt_ep);
1796 + local_irq_restore(flags);
1798 + xprintk("<%s> DISABLED %s\n", __func__, usb_ep->name);
1802 +static struct usb_request *rt_ep_alloc_request (struct usb_ep *usb_ep, gfp_t gfp_flags)
1804 + struct rt_request *req;
1807 + req = kzalloc(sizeof *req, gfp_flags);
1808 + if (!req || !usb_ep)
1811 + INIT_LIST_HEAD(&req->queue);
1816 +static void rt_ep_free_request(struct usb_ep *usb_ep, struct usb_request *usb_req)
1818 + struct rt_request *req;
1821 + req = container_of(usb_req, struct rt_request, req);
1822 + WARN_ON(!list_empty(&req->queue));
1828 + * 1) UDC TX (IN EPs)
1829 + * enqueue req -> handle_ep() -> write fifo -> TX_DONE -> handle_ep() -> write next fifo -> TX_DONE...
1831 + * 2) UDC RX (OUT EPs)
1832 + * enqueue req -> RX_DONE -> handle_ep() -> read_fifo -> RX_DONE -> handle_ep() -> read fifo...
1834 +static int rt_ep_queue(struct usb_ep *usb_ep, struct usb_request *req, gfp_t gfp_flags)
1836 + struct rt_ep_struct *rt_ep;
1837 + struct rt_udc_struct *rt_usb;
1838 + struct rt_request *rt_req;
1839 + unsigned long flags;
1841 + int handle_right_now = 0;
1843 + rt_ep = container_of(usb_ep, struct rt_ep_struct, ep);
1844 + rt_usb = rt_ep->rt_usb;
1845 + rt_req = container_of(req, struct rt_request, req);
1846 + rt_req->rt_ep = rt_ep;
1848 + if (rt_usb->set_config && !EP_NO(rt_ep)) {
1849 + rt_usb->set_config = 0;
1850 + D_ERR(rt_usb->dev, "<%s> gadget reply set config\n", __func__);
1854 + if (unlikely(!req || !rt_req || !req->complete || !req->buf)) {
1855 + D_ERR(rt_usb->dev, "<%s> bad params\n", __func__);
1859 + if (unlikely(!usb_ep || !rt_ep)) {
1860 + D_ERR(rt_usb->dev, "<%s> bad ep\n", __func__);
1864 + if (!rt_usb->driver || rt_usb->gadget.speed == USB_SPEED_UNKNOWN) {
1865 + D_ERR(rt_usb->dev, "<%s> bogus device state\n", __func__);
1866 + return -ESHUTDOWN;
1870 + xprintk("<eq> ep%d%s %p %dB\n", EP_NO(rt_ep), ((!EP_NO(rt_ep) && rt_ep->rt_usb->ep0state == EP0_IN_DATA_PHASE) || (EP_NO(rt_ep) && EP_DIR(rt_ep) == EP_IN )) ? "IN" : "OUT", &rt_req->req, req->length);
1872 + if (rt_ep->stopped) {
1873 + printk("EP%d -> stopped.\n", EP_NO(rt_ep));
1874 + req->status = -ESHUTDOWN;
1875 + return -ESHUTDOWN;
1878 + if (rt_req->in_use) {
1879 + D_ERR(rt_usb->dev, "<%s> refusing to queue req %p (already queued)\n", __func__, req);
1883 + local_irq_save(flags);
1885 + * handle No-data Ctrl transfer.
1887 + if(!EP_NO(rt_ep)/* EP0 */ && EP_DIR(rt_ep) == EP_OUT && !req->length){
1888 + done(rt_ep, rt_req, 0);
1889 + local_irq_restore(flags);
1893 + req->status = -EINPROGRESS;
1896 + if(dma || list_empty(&rt_ep->queue))
1897 + handle_right_now = 1;
1899 + ep_add_request(rt_ep, rt_req);
1901 + if(handle_right_now){
1902 + if(!EP_NO(rt_ep) && rt_ep->rt_usb->ep0state != EP0_OUT_DATA_PHASE){ /* ep0 && TX*/
1903 + handle_inep0(rt_ep);
1904 + }else if( EP_DIR(rt_ep) == EP_IN){ /* epin[1-x] */
1905 + handle_inep(rt_ep);
1907 + // other reqs are waiting for TX_DONE int.
1912 + if(EP_NO(rt_ep) && (EP_DIR(rt_ep) == EP_OUT))
1913 + tasklet_schedule(&rx_dma_tasklet);
1915 + if( (EP_DIR(rt_ep) == EP_OUT)/* OUT EP */ && rt_ep->pending){
1916 + rt_ep->pending = 0;
1917 + handle_pending_epoutirq(rt_usb, rt_ep, rt_req);
1921 + local_irq_restore(flags);
1925 +static int rt_ep_dequeue(struct usb_ep *usb_ep, struct usb_request *usb_req)
1927 + struct rt_ep_struct *rt_ep = container_of(usb_ep, struct rt_ep_struct, ep);
1928 + struct rt_request *req;
1929 + unsigned long flags;
1932 + if (unlikely(!usb_ep || !EP_NO(rt_ep))) {
1933 + D_ERR(rt_ep->rt_usb->dev, "<%s> bad ep\n", __func__);
1937 + local_irq_save(flags);
1939 + /* make sure it's actually queued on this endpoint */
1940 + list_for_each_entry(req, &rt_ep->queue, queue) {
1941 + if (&req->req == usb_req)
1944 + if (&req->req != usb_req) {
1945 + local_irq_restore(flags);
1949 + done(rt_ep, req, -ECONNRESET);
1951 + local_irq_restore(flags);
1955 +static int rt_ep_set_halt(struct usb_ep *usb_ep, int value)
1957 + struct rt_ep_struct *rt_ep = container_of(usb_ep, struct rt_ep_struct, ep);
1958 + unsigned long flags;
1961 + if (unlikely(!usb_ep || !EP_NO(rt_ep))) {
1962 + D_ERR(rt_ep->rt_usb->dev, "<%s> bad ep\n", __func__);
1966 + local_irq_save(flags);
1968 + if ((rt_ep->bEndpointAddress & USB_DIR_IN) && !list_empty(&rt_ep->queue)) {
1969 + local_irq_restore(flags);
1973 + rt_ep_stall(rt_ep, 1);
1975 + local_irq_restore(flags);
1977 + D_EPX(rt_ep->rt_usb->dev, "<%s> %s halt\n", __func__, usb_ep->name);
1981 +static int rt_ep_fifo_status(struct usb_ep *usb_ep)
1983 + struct rt_ep_struct *rt_ep = container_of(usb_ep, struct rt_ep_struct, ep);
1987 + D_ERR(rt_ep->rt_usb->dev, "<%s> bad ep\n", __func__);
1991 + if (rt_ep->rt_usb->gadget.speed == USB_SPEED_UNKNOWN)
1994 + return rt_fifo_bcount(rt_ep);
1997 +static void rt_ep_fifo_flush(struct usb_ep *usb_ep)
1999 + struct rt_ep_struct *rt_ep = container_of(usb_ep, struct rt_ep_struct, ep);
2000 + unsigned long flags;
2003 + local_irq_save(flags);
2005 + if (!usb_ep || !EP_NO(rt_ep) || !list_empty(&rt_ep->queue)) {
2006 + D_ERR(rt_ep->rt_usb->dev, "<%s> bad ep\n", __func__);
2007 + local_irq_restore(flags);
2011 + /* toggle and halt bits stay unchanged */
2014 + local_irq_restore(flags);
2017 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
2018 +static void *rt_ep_alloc_buffer(struct usb_ep *_ep, unsigned bytes, dma_addr_t *dma, gfp_t gfp_flags)
2022 + retval = kmalloc (bytes, gfp_flags & ~(__GFP_DMA|__GFP_HIGHMEM));
2024 +// *dma = virt_to_bus (retval);
2025 + *dma = (dma_addr_t)~0;
2029 +static void rt_ep_free_buffer(struct usb_ep *_ep, void *buf, dma_addr_t dma, unsigned bytes)
2035 +static struct usb_ep_ops rt_ep_ops = {
2036 + .enable = rt_ep_enable,
2037 + .disable = rt_ep_disable,
2039 + .alloc_request = rt_ep_alloc_request,
2040 + .free_request = rt_ep_free_request,
2041 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
2042 + .alloc_buffer = rt_ep_alloc_buffer,
2043 + .free_buffer = rt_ep_free_buffer,
2045 + .queue = rt_ep_queue,
2046 + .dequeue = rt_ep_dequeue,
2048 + .set_halt = rt_ep_set_halt,
2049 + .fifo_status= rt_ep_fifo_status,
2050 + .fifo_flush = rt_ep_fifo_flush,
2053 +/*******************************************************************************
2054 + * USB endpoint control functions
2055 + *******************************************************************************
2057 +static void usb_init_data(struct rt_udc_struct *rt_usb)
2059 + struct rt_ep_struct *rt_ep;
2063 + /* device/ep0 records init */
2064 + INIT_LIST_HEAD(&rt_usb->gadget.ep_list);
2065 + INIT_LIST_HEAD(&rt_usb->gadget.ep0->ep_list);
2066 + ep0_chg_stat(__func__, rt_usb, EP0_IDLE);
2068 + /* basic endpoint records init */
2069 + for (i = 0; i < RT_USB_NB_EP; i++) {
2070 + rt_ep = &rt_usb->rt_ep[i];
2073 + list_add_tail(&rt_ep->ep.ep_list, &rt_usb->gadget.ep_list);
2074 + rt_ep->stopped = 1;
2076 + rt_ep->stopped = 0;
2078 + INIT_LIST_HEAD(&rt_ep->queue);
2082 +static void udc_stop_activity(struct rt_udc_struct *rt_usb, struct usb_gadget_driver *driver)
2084 + struct rt_ep_struct *rt_ep;
2087 + if (rt_usb->gadget.speed == USB_SPEED_UNKNOWN)
2090 + /* prevent new request submissions, kill any outstanding requests */
2091 + for (i = 0; i < RT_USB_NB_EP; i++) {
2092 + rt_ep = &rt_usb->rt_ep[i];
2093 + if(i != 0){ /* don't have to flush EP[0]. */
2095 + rt_ep->stopped = 1;
2096 + rt_ep_irq_disable(rt_ep);
2098 + nuke(rt_ep, -ESHUTDOWN);
2106 + driver->disconnect(&rt_usb->gadget);
2110 + * keep for reference.
2112 +static void handle_config(unsigned long data)
2116 + struct imx_udc_struct *imx_usb = (void *)data;
2117 + struct usb_ctrlrequest u;
2118 + int temp, cfg, intf, alt;
2120 + local_irq_disable();
2122 + temp = __raw_readl(imx_usb->base + USB_STAT);
2123 + cfg = (temp & STAT_CFG) >> 5;
2124 + intf = (temp & STAT_INTF) >> 3;
2125 + alt = temp & STAT_ALTSET;
2127 + xprintk("<%s> orig config C=%d, I=%d, A=%d / req config C=%d, I=%d, A=%d\n", __func__, imx_usb->cfg, imx_usb->intf, imx_usb->alt, cfg, intf, alt);
2129 + if (cfg == 1 || cfg == 2) {
2131 + if (imx_usb->cfg != cfg) {
2132 + u.bRequest = USB_REQ_SET_CONFIGURATION;
2133 + u.bRequestType = USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
2137 + imx_usb->cfg = cfg;
2138 + imx_usb->driver->setup(&imx_usb->gadget, &u);
2141 + if (imx_usb->intf != intf || imx_usb->alt != alt) {
2142 + u.bRequest = USB_REQ_SET_INTERFACE;
2143 + u.bRequestType = USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_INTERFACE;
2147 + imx_usb->intf = intf;
2148 + imx_usb->alt = alt;
2149 + imx_usb->driver->setup(&imx_usb->gadget, &u);
2153 + imx_usb->set_config = 0;
2155 + local_irq_enable();
2159 +static void handle_setup(struct rt_udc_struct *rt_usb)
2164 + struct usb_ctrlrequest r;
2168 + struct rt_ep_struct *rt_ep = &rt_usb->rt_ep[0];
2170 + nuke(rt_ep, -EPROTO);
2172 + // read setup packet
2173 + for (i = 0; i < 8; i++)
2174 + u.raw[i] = usb_read(SETUPDATA + i);
2176 + le16_to_cpus(&u.r.wValue);
2177 + le16_to_cpus(&u.r.wIndex);
2178 + le16_to_cpus(&u.r.wLength);
2180 + xprintk("<SETUP> %02x.%02x v%04x\n", u.r.bRequestType, u.r.bRequest, u.r.wValue);
2182 + switch(u.r.bRequest){
2183 + /* HW(CUSB2) has handled it. */
2184 + case USB_REQ_SET_ADDRESS:
2185 + if (u.r.bRequestType != (USB_TYPE_STANDARD | USB_RECIP_DEVICE))
2191 + ep0_chg_stat(__func__, rt_usb, EP0_NO_DATA_PHASE);
2192 + }else if (u.r.bRequestType & USB_DIR_IN){
2193 + ep0_chg_stat(__func__, rt_usb, EP0_IN_DATA_PHASE);
2195 + // reload and clear out0bc
2196 + usb_write(OUT0BC, 0);
2197 + ep0_chg_stat(__func__, rt_usb, EP0_OUT_DATA_PHASE);
2200 + if(!rt_usb->driver){
2201 + printk("<%s> please insert gadget driver/module.\n", __func__);
2205 + if(!rt_usb->driver->setup)
2208 + i = rt_usb->driver->setup(&rt_usb->gadget, &u.r); // gadget would queue more usb req here
2211 + printk("<%s> device setup error %d\n", __func__, i);
2215 + if(rt_usb->ep0state == EP0_NO_DATA_PHASE){
2216 + epcs = read_epcs(rt_ep);
2217 + epcs |= EP_CS_EP0_HSNAK; // clear hsnak to let HW ack the status stage.
2218 + write_epcs(rt_ep, epcs);
2223 + printk("<%s> protocol STALL\n", __func__);
2224 + rt_ep_stall(rt_ep, 1);
2225 + ep0_chg_stat(__func__, rt_usb, EP0_STALL);
2230 + * handle TX done interrupt
2232 +static void handle_epinirq(struct rt_udc_struct *rt_usb, u8 epinirq)
2235 + struct rt_request *req;
2236 + struct rt_ep_struct *rt_ep;
2238 + rt_ep = &rt_usb->rt_ep[epinirq];
2240 + if (list_empty(&rt_ep->queue))
2241 + FATAL_ERROR("empty queue");
2243 + // clear ep interrupt
2245 + irq |= 1 << epinirq;
2246 + usb_write(IN07IRQ, irq);
2248 + irq |= 1 << (epinirq-8);
2249 + usb_write(IN815IRQ, irq);
2252 + req = list_entry(rt_ep->queue.next, struct rt_request, queue);
2253 + xprintk("r.l=%d, r.a=%d\n", req->req.length, req->req.actual);
2254 + if(req->req.actual >= req->req.length ){
2255 + if( req->req.actual && (!(req->req.actual % rt_ep->ep.maxpacket)) && req->req.zero){
2256 + // deal with one more "zlp"
2257 + req->req.zero = 0;
2258 + write_ep_fifo_zlp(rt_ep);
2262 + // the first tx req in ep->queue is done.
2263 + rt_ep->tx_done_count = 0;
2264 + if(!epinirq /* EP0 */)
2265 + ep0_chg_stat(__func__, rt_usb, EP0_IDLE);
2266 + done(rt_ep, req, 0);
2268 + // more reqs there.
2269 + if (!list_empty(&rt_ep->queue) && !rt_ep->tx_done_count){
2270 + if(!epinirq /* EP0 */){
2271 + handle_inep0(rt_ep);
2273 + handle_inep(rt_ep);
2278 + if(!epinirq /* EP0 */){
2279 + handle_inep0(rt_ep);
2281 + handle_inep(rt_ep);
2285 +static void handle_ep0outirq(struct rt_udc_struct *rt_usb, u8 epoutirq)
2287 + u8 epcs, irq = 0x0;
2288 + struct rt_request *req = NULL;
2289 + struct rt_ep_struct *rt_ep = NULL;
2292 + rt_ep = &rt_usb->rt_ep[0];
2294 + if(rt_usb->ep0state == EP0_STALL){
2295 + printk("<%s> protocol STALL\n", __func__);
2296 + rt_ep_stall(rt_ep, 1);
2300 + if(rt_usb->ep0state != EP0_OUT_DATA_PHASE)
2301 + FATAL_ERROR("Odd stage");
2304 + if(unlikely(!read_outbc(0x0)))
2305 + FATAL_ERROR("EP0 BC");
2307 + if (unlikely(list_empty(&rt_ep->queue)))
2308 + FATAL_ERROR("EP0 no req");
2310 + req = handle_outep0(rt_ep);
2312 + //req = list_entry(rt_ep->queue.next, struct rt_request, queue);
2313 + xprintk("q.l=%d,q.a=%d\n", req->req.length, req->req.actual);
2315 + // clear ep interrupt
2317 + usb_write(OUT07IRQ, irq);
2319 + if(req && ((req->req.actual % rt_ep->ep.maxpacket) || (req->req.actual >= req->req.length))){
2320 + ep0_chg_stat(__func__, rt_usb, EP0_IDLE);
2321 + done(rt_ep, req, 0); // short packet indicates transaction is done.
2323 + epcs = read_epcs(rt_ep);
2324 + epcs |= EP_CS_EP0_HSNAK; // clear hsnak bit to let HW ack the status stage.
2325 + write_epcs(rt_ep, epcs);
2330 + usb_write(OUT0BC /*out0bc*/, 0x0);
2331 + epcs = read_epcs(rt_ep);
2332 + }while(!(epcs & EP0_OUT_BSY));
2335 +static void handle_pending_epoutirq(struct rt_udc_struct *rt_usb, struct rt_ep_struct *rt_ep, struct rt_request *req)
2341 + if(unlikely(!read_outbc(EP_NO(rt_ep))))
2342 + FATAL_ERROR("No BC");
2344 + handle_outep(rt_ep);
2345 + if(req && ( (req->req.actual % rt_ep->ep.maxpacket) || (req->req.actual >= req->req.length))){
2346 + xprintk("q.l=%d,q.a=%d\n", req->req.length, req->req.actual);
2348 + //rx_done(rt_ep, req, 0);
2349 + done(rt_ep, req, 0);
2352 + epcs = read_epcs(rt_ep);
2353 + write_epcs(rt_ep, 0x0);
2354 + epcs = read_epcs(rt_ep);
2356 + }while(!(epcs & EP_CS_BSY));
2359 +static void handle_epoutirq(struct rt_udc_struct *rt_usb, u8 epoutirq)
2364 + if(unlikely(epoutirq == 0x0)){
2365 + handle_ep0outirq(rt_usb, 0x0);
2369 + tasklet_schedule(&rx_tasklet);
2371 + // clear ep interrupt
2372 + irq |= 1 << epoutirq;
2373 + usb_write(OUT07IRQ, irq);
2377 +static void eps_change_to_hs(struct rt_udc_struct *rt_usb)
2380 + struct rt_ep_struct *rt_ep;
2381 + for(i = 0; i < RT_USB_NB_EP; i++){
2382 + rt_ep = &rt_usb->rt_ep[i];
2383 + if(rt_ep->bmAttributes == USB_ENDPOINT_XFER_BULK){
2384 + rt_ep->ep.maxpacket = 512;
2389 +static void eps_change_to_fs(struct rt_udc_struct *rt_usb)
2392 + struct rt_ep_struct *rt_ep;
2393 + for(i = 0; i < RT_USB_NB_EP; i++){
2394 + rt_ep = &rt_usb->rt_ep[i];
2395 + if(rt_ep->bmAttributes == USB_ENDPOINT_XFER_BULK){
2396 + rt_ep->ep.maxpacket = 64;
2401 +void handle_highspeed(struct rt_udc_struct *rt_usb)
2405 + eps_change_to_hs(rt_usb);
2408 +#if defined (CONFIG_RALINK_RT3883) || defined (CONFIG_RALINK_RT3352) || defined (CONFIG_RALINK_MT7620)
2409 + usb_write(IN1CON, 0x8D); // InEP1 : Int, 2 subfifos
2410 + usb_write(IN2CON, 0x89); // InEP2 : Bulk, 2 subfifo
2411 + usb_write(OUT1CON, 0x8D); // OutEP1 : Int, 2 subfifos
2412 + usb_write(OUT2CON, 0x89); // OutEP2 : Bulk, 2 subfifos
2413 + //usb_write(OUT3CON, 0x89); // OutEP3 : Bulk, 2 subfifo
2414 + //usb_write(OUT4CON, 0x89); // OutEP4 : Bulk. 2 subfifo
2415 +#elif defined (CONFIG_RALINK_RT5350)
2417 + usb_write(IN1CON, 0x89); // InEP1 : Bulk, 2 subfifos
2418 + usb_write(OUT1CON, 0x89); // OutEP1 : Bulk, 2 subfifos
2420 +#error "define a platform"
2424 +#if defined (CONFIG_RALINK_RT3883) || defined (CONFIG_RALINK_RT3352) || defined (CONFIG_RALINK_MT7620)
2425 + usb_write(IN1CON, 0x8C); // InEP1 : Int , 1 subfifos
2426 + usb_write(IN2CON, 0x88); // InEP2 : Bulk, 1 subfifo
2428 + usb_write(OUT1CON, 0x8C); // OutEP1 : Int, 1 subfifos
2429 + usb_write(OUT2CON, 0x88); // OutEP2 : Bulk, 1 subfifos
2430 + //usb_write(OUT3CON, 0x88); // OutEP3 : Bulk, 1 subfifo
2431 + //usb_write(OUT4CON, 0x88); // OutEP4 : Bulk. 1 subfifo
2432 +#elif defined (CONFIG_RALINK_RT5350)
2434 + usb_write(IN1CON, 0x88); // InEP1 : Bulk , 1 subfifos
2435 + usb_write(OUT1CON, 0x88); // OutEP1 : Bulk, 1 subfifos
2437 +#error "define a platform"
2441 + // clear all pending interrupts
2442 + usb_write(IN07IRQ, 0xFF);
2443 + usb_write(OUT07IRQ, 0xFF);
2445 + rt_usb->gadget.speed = USB_SPEED_HIGH;
2447 + // reset ALL endpoints
2448 + rt_all_eps_reset();
2450 + // Enable ep0 interrupt.
2451 + // (EPx interrupt is enabled in EPx_enable(). )
2452 + rt_ep_irq_enable(&rt_usb->rt_ep[0]);
2455 +static void handle_reset(struct rt_udc_struct *rt_usb)
2457 + struct rt_ep_struct *rt_ep;
2460 + eps_change_to_fs(rt_usb);
2462 + // remove all EPs' usb request
2463 + for (i = 0; i < RT_USB_NB_EP; i++) {
2464 + rt_ep = &rt_usb->rt_ep[i];
2465 + if(i != 0){ /* don't have to flush EP[0]. */
2467 + rt_ep->stopped = 1;
2468 + rt_ep_irq_disable(rt_ep);
2470 + nuke(rt_ep, -ESHUTDOWN);
2478 + // clear all PDMA interrupts
2479 + udc_dma_all_int_clear();
2484 + // clear all pending interrupts
2485 + usb_write(IN07IRQ, 0xFF);
2486 + usb_write(OUT07IRQ, 0xFF);
2488 + // flush all EP's fifo
2489 + rt_all_eps_reset();
2492 +static void handle_usbirq(struct rt_udc_struct *rt_usb, u8 usbirq)
2494 + if(usbirq & USB_INTR_SETUP_TOKEN_VALID){
2495 + // Setup token is arrival.
2496 + // get setup data and pass it to gadget driver.
2497 + handle_setup(rt_usb);
2500 + if(usbirq & USB_INTR_RESET)
2501 + handle_reset(rt_usb);
2503 + if(usbirq & USB_INTR_HSPEED)
2504 + handle_highspeed(rt_usb);
2507 + * DO NOT try to clear SoF and token Interrupt!
2509 + if( (usbirq & USB_INTR_SETUP_TOKEN_VALID) ||
2510 + (usbirq & USB_INTR_HSPEED) ||
2511 + (usbirq & USB_INTR_RESET))
2512 + usb_write(USBIRQ, usbirq);
2515 +static int irq_count = 100; /* for debug */
2517 + * Interrupt handler
2519 +irqreturn_t rt_irq_handler(int irq, void *_dev)
2521 + u32 usbirq,epin07irq,epin07ien,epout07irq,epout07ien;
2522 + struct rt_udc_struct *rt_usb = _dev;
2524 + u32 count_tmp = irq_count;
2531 + usbirq = usb_read(USBIRQ);
2532 + epin07irq = usb_read(IN07IRQ);
2533 + epin07ien = usb_read(IN07IEN);
2534 + epout07irq = usb_read(OUT07IRQ);
2535 + epout07ien = usb_read(OUT07IEN);
2537 + //epin07irq = epin07irq & epin07ien;
2538 + //epout07irq = epout07irq & epout07ien;
2540 + xprintk(">%x\n", count_tmp);
2541 + dump_usbirq(usbirq);
2542 + dump_epirq(epin07irq, epin07ien, 1);
2543 + dump_epirq(epout07irq, epout07ien, 0);
2546 + u32 dma_irq = reg_read(RTUSB_INT_STATUS);
2547 + if(epin07irq & 0x1) // INEP0
2548 + handle_epinirq(rt_usb, 0);
2550 + if(usbirq) // HS, Reset, SetupValid
2551 + handle_usbirq(rt_usb, usbirq);
2553 + if(epout07irq & 0x1) // OUTEP0
2554 + handle_epoutirq(rt_usb, 0);
2557 + handle_dmairq(rt_usb, dma_irq);
2560 + if(epin07irq & 0x1) // INEP0
2561 + handle_epinirq(rt_usb, 0);
2563 + if(usbirq) // HS, Reset, SetupValid
2564 + handle_usbirq(rt_usb, usbirq);
2566 + if(epout07irq & 0x1) // OUTEP0
2567 + handle_epoutirq(rt_usb, 0);
2569 + if(epout07irq & 0x2) // OUTEP1
2570 + handle_epoutirq(rt_usb, 1);
2572 + if(epin07irq & 0x2) // INEP1
2573 + handle_epinirq(rt_usb, 1);
2575 + if(epout07irq & 0x4) // OUTEP2
2576 + handle_epoutirq(rt_usb, 2);
2578 + if(epin07irq & 0x4) // INEP2
2579 + handle_epinirq(rt_usb, 2);
2581 + //if(epout07irq & 0x8) // OUTEP3
2582 + // handle_epoutirq(rt_usb, 3);
2584 + //if(epout07irq & 0x10) // OUTEP4
2585 + // handle_epoutirq(rt_usb, 4);
2587 + xprintk("<%x\n", count_tmp);
2588 + return IRQ_HANDLED;
2592 + ******************************************************************************
2593 + * Static defined Ralink UDC structure
2594 + *******************************************************************************
2596 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36)
2597 +static void nop_release(struct device *dev)
2603 +static const struct usb_gadget_ops rt_udc_ops = {
2604 + .get_frame = rt_udc_get_frame,
2605 + .wakeup = rt_udc_wakeup,
2608 +static struct rt_udc_struct controller = {
2610 + .ops = &rt_udc_ops,
2611 + .ep0 = &controller.rt_ep[0].ep,
2612 + .name = driver_name,
2614 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36)
2615 + .init_name = "gadget",
2616 + .release = nop_release,
2618 + .bus_id = "gadget",
2625 + .ops = &rt_ep_ops,
2628 + .rt_usb = &controller,
2629 + .bEndpointAddress = 0,
2630 + .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
2633 +#if defined (CONFIG_RALINK_RT3883) || defined (CONFIG_RALINK_RT3352) || defined (CONFIG_RALINK_MT7620)
2636 + .name = "ep1in-int",
2637 + .ops = &rt_ep_ops,
2640 + .rt_usb = &controller,
2641 + .bEndpointAddress = USB_DIR_IN | 1,
2642 + .bmAttributes = USB_ENDPOINT_XFER_INT,
2647 + .name = "ep2in-bulk",
2648 + .ops = &rt_ep_ops,
2651 + .rt_usb = &controller,
2652 + .bEndpointAddress = USB_DIR_IN | 2,
2653 + .bmAttributes = USB_ENDPOINT_XFER_BULK,
2658 + .name = "ep1out-int",
2659 + .ops = &rt_ep_ops,
2662 + .rt_usb = &controller,
2663 + .bEndpointAddress = USB_DIR_OUT | 1,
2664 + .bmAttributes = USB_ENDPOINT_XFER_INT,
2669 + .name = "ep2out-bulk",
2670 + .ops = &rt_ep_ops,
2673 + .rt_usb = &controller,
2674 + .bEndpointAddress = USB_DIR_OUT | 2,
2675 + .bmAttributes = USB_ENDPOINT_XFER_BULK,
2681 + .name = "ep3out-bulk",
2682 + .ops = &rt_ep_ops,
2685 + .rt_usb = &controller,
2686 + .bEndpointAddress = USB_DIR_OUT | 3,
2687 + .bmAttributes = USB_ENDPOINT_XFER_BULK,
2692 + .name = "ep4out-bulk",
2693 + .ops = &rt_ep_ops,
2696 + .rt_usb = &controller,
2697 + .bEndpointAddress = USB_DIR_OUT | 4,
2698 + .bmAttributes = USB_ENDPOINT_XFER_BULK,
2703 +#elif defined (CONFIG_RALINK_RT5350)
2706 + .name = "ep1in-bulk",
2707 + .ops = &rt_ep_ops,
2710 + .rt_usb = &controller,
2711 + .bEndpointAddress = USB_DIR_IN | 1,
2712 + .bmAttributes = USB_ENDPOINT_XFER_BULK,
2717 + .name = "ep1out-bulk",
2718 + .ops = &rt_ep_ops,
2721 + .rt_usb = &controller,
2722 + .bEndpointAddress = USB_DIR_OUT | 1,
2723 + .bmAttributes = USB_ENDPOINT_XFER_BULK,
2727 +#error "define a platform"
2732 + *******************************************************************************
2733 + * USB gadged driver functions
2734 + *******************************************************************************
2737 +static void rt_udc_enable(struct rt_udc_struct *rt_usb)
2740 + rt_usb->gadget.speed = USB_SPEED_FULL;
2742 + // enable dma interrupts
2743 + udc_dma_all_int_clear();
2744 + udc_dma_int_enable(true);
2749 + udc_dma_enable(true);
2753 +static void rt_udc_disable(struct rt_udc_struct *rt_usb)
2756 + ep0_chg_stat(__func__, rt_usb, EP0_IDLE);
2757 + rt_usb->gadget.speed = USB_SPEED_UNKNOWN;
2759 + // disable dma interrupts
2760 + udc_dma_all_int_clear();
2761 + udc_dma_int_enable(false);
2766 + udc_dma_enable(false);
2770 +int usb_gadget_register_driver(struct usb_gadget_driver *driver)
2772 + struct rt_udc_struct *rt_usb = &controller;
2776 + if (!driver || driver->speed < USB_SPEED_FULL || !driver->bind || !driver->disconnect || !driver->setup)
2780 + if (rt_usb->driver)
2783 + /* first hook up the driver ... */
2784 + rt_usb->driver = driver;
2785 + rt_usb->gadget.dev.driver = &driver->driver;
2786 + retval = device_add(&rt_usb->gadget.dev);
2790 + retval = driver->bind(&rt_usb->gadget);
2792 + D_ERR(rt_usb->dev, "<%s> bind to driver --> error %d\n", __func__, retval);
2793 + device_del(&rt_usb->gadget.dev);
2797 + D_INI(rt_usb->dev, "<%s> registered gadget driver '%s'\n", __func__, driver->driver.name);
2798 + rt_udc_enable(rt_usb);
2802 + rt_usb->driver = NULL;
2803 + rt_usb->gadget.dev.driver = NULL;
2806 +EXPORT_SYMBOL(usb_gadget_register_driver);
2808 +int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
2810 + struct rt_udc_struct *rt_usb = &controller;
2815 + if (!driver || driver != rt_usb->driver || !driver->unbind)
2818 + udc_stop_activity(rt_usb, driver);
2819 + rt_udc_disable(rt_usb);
2820 + del_timer(&rt_usb->timer);
2822 + driver->unbind(&rt_usb->gadget);
2823 + rt_usb->gadget.dev.driver = NULL;
2824 + rt_usb->driver = NULL;
2826 + device_del(&rt_usb->gadget.dev);
2828 + D_INI(rt_usb->dev, "<%s> unregistered gadget driver '%s'\n", __func__, driver->driver.name);
2832 +EXPORT_SYMBOL(usb_gadget_unregister_driver);
2834 +/*******************************************************************************
2835 + * Module functions
2836 + *******************************************************************************
2838 +static int __init rt_udc_probe(struct platform_device *pdev)
2840 + struct rt_udc_struct *rt_usb = &controller;
2841 + struct resource *res_mem, *res_irq;
2842 + void __iomem *base;
2843 + int ret = 0, res_mem_size;
2846 + res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2848 + dev_err(&pdev->dev, "can't get device resources\n");
2852 + res_mem_size = res_mem->end - res_mem->start + 1;
2853 + if (!request_mem_region(res_mem->start, res_mem_size, res_mem->name)) {
2854 + dev_err(&pdev->dev, "can't allocate %d bytes at %d address\n", res_mem_size, res_mem->start);
2858 + base = ioremap(res_mem->start, res_mem_size);
2860 + dev_err(&pdev->dev, "ioremap failed\n");
2865 + res_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2867 + dev_err(&pdev->dev, "can't get irq number\n");
2871 + rt_usb->interrupt = res_irq->start;
2873 + ret = request_irq(rt_usb->interrupt, rt_irq_handler, IRQF_DISABLED, driver_name, rt_usb);
2875 + dev_err(&pdev->dev, "can't get irq %i, err %d\n", rt_usb->interrupt, ret);
2879 + rt_usb->res = res_mem;
2880 + rt_usb->base = base;
2881 + rt_usb->dev = &pdev->dev;
2883 + device_initialize(&rt_usb->gadget.dev);
2885 + rt_usb->gadget.dev.parent = &pdev->dev;
2886 + rt_usb->gadget.dev.dma_mask = pdev->dev.dma_mask;
2888 + platform_set_drvdata(pdev, rt_usb);
2890 + usb_init_data(rt_usb);
2893 + if(rt_udc_dma_init())
2897 + rt_udc_init(rt_usb);
2899 + init_timer(&rt_usb->timer);
2900 + rt_usb->timer.function = handle_config;
2901 + rt_usb->timer.data = (unsigned long)rt_usb;
2905 + free_irq(rt_usb->interrupt, rt_usb);
2909 + release_mem_region(res_mem->start, res_mem_size);
2913 +static int __exit rt_udc_remove(struct platform_device *pdev)
2915 + struct rt_udc_struct *rt_usb = platform_get_drvdata(pdev);
2918 + rt_udc_disable(rt_usb);
2919 + del_timer(&rt_usb->timer);
2921 + free_irq(rt_usb->interrupt, rt_usb);
2923 + iounmap(rt_usb->base);
2924 + release_mem_region(rt_usb->res->start, rt_usb->res->end - rt_usb->res->start + 1);
2926 + //if (pdata->exit)
2927 + // pdata->exit(&pdev->dev);
2928 + platform_set_drvdata(pdev, NULL);
2933 +static void set_device_mode(void)
2936 + val = le32_to_cpu(*(volatile u_long *)(SYSCFG1));
2937 + val = val & ~(USB0_HOST_MODE);
2938 + *(volatile u_long *)(SYSCFG1) = cpu_to_le32(val);
2942 +/*----------------------------------------------------------------------------*/
2943 +static struct platform_driver udc_driver = {
2946 + .name = driver_name,
2947 + .owner = THIS_MODULE,
2949 + .probe = rt_udc_probe,
2950 + .remove = __exit_p(rt_udc_remove),
2955 +static int udc_create_proc(void)
2957 + pProcDir = proc_mkdir(PROC_DIR, NULL);
2958 + if ((pProcDebugLevel = create_proc_entry(DEBUGLEVEL_PROCFILE, 0, pProcDir))){
2959 + pProcDebugLevel->read_proc = (read_proc_t*)&debuglevel_read;
2960 + pProcDebugLevel->write_proc = (write_proc_t*)&debuglevel_write;
2965 +static int udc_remove_proc(void)
2967 + if (pProcDebugLevel)
2968 + remove_proc_entry(DEBUGLEVEL_PROCFILE, pProcDir);
2970 + remove_proc_entry(PROC_DIR, 0);
2975 +static int __init udc_init(void)
2978 + udc_create_proc();
2981 + set_device_mode();
2983 + ret = platform_driver_register(&udc_driver);
2985 + tasklet_init(&rx_tasklet, rx_do_tasklet, 0);
2986 + tasklet_init(&tx_tasklet, tx_do_tasklet, 0);
2989 + printk("DMA TXMAXCAP=%d\n", TXMAXCAP);
2990 + tasklet_init(&rx_dma_tasklet, rx_dma_done_do_tasklet, 0);
2991 + tasklet_init(&tx_dma_tasklet, tx_dma_done_do_tasklet, 0);
2994 + return ret; //platform_driver_probe(&udc_driver, rt_udc_probe);
2996 +module_init(udc_init);
2998 +static void __exit udc_exit(void)
3001 + udc_remove_proc();
3004 + platform_driver_unregister(&udc_driver);
3006 +module_exit(udc_exit);
3008 +MODULE_DESCRIPTION("Ralink USB Device Controller driver");
3009 +MODULE_AUTHOR("Ying Yuan Huang <yy_huang@ralinktech.com>");
3010 +MODULE_LICENSE("GPL");
3011 +MODULE_ALIAS("platform:rt_udc");