Merge remote-tracking branch 'stable/linux-3.0.y' into develop-3.0
[firefly-linux-kernel-4.4.55.git] / drivers / usb / dwc_otg / dwc_otg_pcd.c
1  /* ==========================================================================
2  * $File: //dwh/usb_iip/dev/software/otg_ipmate/linux/drivers/dwc_otg_pcd.c $
3  * $Revision: #18 $
4  * $Date: 2007/02/07 $
5  * $Change: 791271 $
6  *
7  * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
8  * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
9  * otherwise expressly agreed to in writing between Synopsys and you.
10  * 
11  * The Software IS NOT an item of Licensed Software or Licensed Product under
12  * any End User Software License Agreement or Agreement for Licensed Product
13  * with Synopsys or any supplement thereto. You are permitted to use and
14  * redistribute this Software in source and binary forms, with or without
15  * modification, provided that redistributions of source code must retain this
16  * notice. You may not view, use, disclose, copy or distribute this file or
17  * any information contained herein except pursuant to this license grant from
18  * Synopsys. If you do not agree with this notice, including the disclaimer
19  * below, then you are not authorized to use the Software.
20  * 
21  * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
25  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
31  * DAMAGE.
32  * ========================================================================== */
33 #ifndef DWC_HOST_ONLY
34
35 /** @file 
36  * This file implements the Peripheral Controller Driver.
37  *
38  * The Peripheral Controller Driver (PCD) is responsible for
39  * translating requests from the Function Driver into the appropriate
40  * actions on the DWC_otg controller. It isolates the Function Driver
41  * from the specifics of the controller by providing an API to the
42  * Function Driver. 
43  *
44  * The Peripheral Controller Driver for Linux will implement the
45  * Gadget API, so that the existing Gadget drivers can be used.
46  * (Gadget Driver is the Linux terminology for a Function Driver.)
47  * 
48  * The Linux Gadget API is defined in the header file
49  * <code><linux/usb_gadget.h></code>.  The USB EP operations API is
50  * defined in the structure <code>usb_ep_ops</code> and the USB
51  * Controller API is defined in the structure
52  * <code>usb_gadget_ops</code>.
53  *
54  * An important function of the PCD is managing interrupts generated
55  * by the DWC_otg controller. The implementation of the DWC_otg device
56  * mode interrupt service routines is in dwc_otg_pcd_intr.c.
57  *
58  * @todo Add Device Mode test modes (Test J mode, Test K mode, etc).
59  * @todo Does it work when the request size is greater than DEPTSIZ
60  * transfer size
61  *
62  */
63
64 #include <linux/kernel.h>
65 #include <linux/module.h>
66 #include <linux/moduleparam.h>
67 #include <linux/init.h>
68 #include <linux/device.h>
69 #include <linux/errno.h>
70 #include <linux/list.h>
71 #include <linux/interrupt.h>
72 #include <linux/string.h>
73 #include <linux/dma-mapping.h>
74 #include <linux/irq.h>
75 #include <linux/kallsyms.h>
76 #include <linux/device.h>
77
78 #include <linux/usb/ch9.h>
79 #include <linux/usb/gadget.h>
80 #include <linux/platform_device.h>
81
82 #include <linux/usb/composite.h>
83
84 #include "dwc_otg_driver.h"
85 #include "dwc_otg_pcd.h"
86 #include "dwc_otg_regs.h"
87
88 #include "usbdev_rk.h"
89 /**
90  * Static PCD pointer for use in usb_gadget_register_driver and
91  * usb_gadget_unregister_driver.  Initialized in dwc_otg_pcd_init.
92  */
93  static 
94  dwc_otg_pcd_t *s_pcd = 0;
95
96
97 /* Display the contents of the buffer */
98 extern void dump_msg(const u8 *buf, unsigned int length);
99
100
101 /**
102  * This function completes a request.  It call's the request call back.
103  */
104 void request_done(dwc_otg_pcd_ep_t *_ep, dwc_otg_pcd_request_t *_req, 
105                                   int _status)
106 {
107         unsigned stopped = _ep->stopped;
108     
109         DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, _ep);
110         list_del_init(&_req->queue);
111
112         if (_req->req.status == -EINPROGRESS) 
113         {
114                 _req->req.status = _status;
115         } 
116         else 
117         {       
118                 _status = _req->req.status;
119         }
120 #if 1
121     if (_req->req.dma != DMA_ADDR_INVALID){
122         if (_req->mapped) {
123                 dma_unmap_single(_ep->pcd->gadget.dev.parent,
124                         _req->req.dma, _req->req.length,
125                         _ep->dwc_ep.is_in
126                                 ? DMA_TO_DEVICE
127                                 : DMA_FROM_DEVICE);
128                 _req->req.dma = DMA_ADDR_INVALID;
129                 _req->mapped = 0;
130         } else
131                 dma_sync_single_for_cpu(_ep->pcd->gadget.dev.parent,
132                         _req->req.dma, _req->req.length,
133                         _ep->dwc_ep.is_in
134                                 ? DMA_TO_DEVICE
135                                 : DMA_FROM_DEVICE);
136         }
137 #endif
138         /* don't modify queue heads during completion callback */
139         _ep->stopped = 1;
140         SPIN_UNLOCK(&_ep->pcd->lock);
141         _req->req.complete(&_ep->ep, &_req->req);
142         SPIN_LOCK(&_ep->pcd->lock);
143
144         if (_ep->pcd->request_pending > 0)
145         {
146                 --_ep->pcd->request_pending;
147         }
148                 
149         _ep->stopped = stopped;
150 }
151
152 /**
153  * This function terminates all the requsts in the EP request queue.
154  */
155 void request_nuke( dwc_otg_pcd_ep_t *_ep )
156 {
157         dwc_otg_pcd_request_t *req;
158
159         _ep->stopped = 1;
160
161         /* called with irqs blocked?? */
162         while (!list_empty(&_ep->queue)) 
163         {
164                 req = list_entry(_ep->queue.next, dwc_otg_pcd_request_t,
165                                  queue);
166                 request_done(_ep, req, -ESHUTDOWN );
167         }
168 }
169
170 /* USB Endpoint Operations */
171 /* 
172  * The following sections briefly describe the behavior of the Gadget
173  * API endpoint operations implemented in the DWC_otg driver
174  * software. Detailed descriptions of the generic behavior of each of
175  * these functions can be found in the Linux header file
176  * include/linux/usb_gadget.h.
177  *
178  * The Gadget API provides wrapper functions for each of the function
179  * pointers defined in usb_ep_ops. The Gadget Driver calls the wrapper
180  * function, which then calls the underlying PCD function. The
181  * following sections are named according to the wrapper
182  * functions. Within each section, the corresponding DWC_otg PCD
183  * function name is specified.
184  *
185  */
186
187 /**
188  * This function assigns periodic Tx FIFO to an periodic EP
189  * in shared Tx FIFO mode
190  */
191 #ifndef CONFIG_ARCH_RK29
192 static uint32_t assign_perio_tx_fifo(dwc_otg_core_if_t  *core_if)
193 {
194         uint32_t PerTxMsk = 1;
195         int i;
196         for(i = 0; i < core_if->hwcfg4.b.num_dev_perio_in_ep; ++i)
197         {
198                 if((PerTxMsk & core_if->p_tx_msk) == 0)
199                 {
200                         core_if->p_tx_msk |= PerTxMsk;
201                         return i + 1;
202                 }
203                 PerTxMsk <<= 1;
204         }
205         return 0;
206 }
207 #endif
208 /**
209  * This function releases periodic Tx FIFO 
210  * in shared Tx FIFO mode
211  */
212 static void release_perio_tx_fifo(dwc_otg_core_if_t *core_if, uint32_t fifo_num)
213 {
214         core_if->p_tx_msk = (core_if->p_tx_msk & (1 << (fifo_num - 1))) ^ core_if->p_tx_msk;
215 }
216 /**
217  * This function assigns periodic Tx FIFO to an periodic EP
218  * in Dedicated FIFOs mode
219  */
220 #ifndef CONFIG_ARCH_RK29
221 static uint32_t assign_tx_fifo(dwc_otg_core_if_t *core_if)
222 {
223         uint32_t TxMsk = 1;
224         int i;
225         
226         for(i = 0; i < core_if->hwcfg4.b.num_in_eps; ++i)
227         {
228                 if((TxMsk & core_if->tx_msk) == 0)
229                 {
230                         core_if->tx_msk |= TxMsk;
231                         return i + 1;
232                 }
233                 TxMsk <<= 1;
234         }
235         return 0;
236 }
237 #endif
238 /**
239  * This function releases periodic Tx FIFO 
240  * in Dedicated FIFOs mode
241  */
242 static void release_tx_fifo(dwc_otg_core_if_t   *core_if, uint32_t fifo_num)
243 {
244         core_if->tx_msk = (core_if->tx_msk & (1 << (fifo_num - 1))) ^ core_if->tx_msk;
245 }
246 /**
247  * This function is called by the Gadget Driver for each EP to be
248  * configured for the current configuration (SET_CONFIGURATION).  
249  * 
250  * This function initializes the dwc_otg_ep_t data structure, and then
251  * calls dwc_otg_ep_activate.
252  */
253 static int dwc_otg_pcd_ep_enable(struct usb_ep *_ep, 
254                                                                  const struct usb_endpoint_descriptor *_desc)
255 {
256         dwc_otg_pcd_ep_t *ep = 0;
257         dwc_otg_pcd_t *pcd = 0;
258         unsigned long flags;
259         
260         DWC_DEBUGPL(DBG_PCDV,"%s(%p,%p)\n", __func__, _ep, _desc );
261              
262         ep = container_of(_ep, dwc_otg_pcd_ep_t, ep);
263         if (!_ep || !_desc || ep->desc || 
264                         _desc->bDescriptorType != USB_DT_ENDPOINT) 
265         {
266                 DWC_WARN( "%s, bad ep or descriptor\n", __func__);
267                 return -EINVAL;
268         }
269         if (ep == &ep->pcd->ep0)
270         {
271                 DWC_WARN("%s, bad ep(0)\n", __func__);
272                 return -EINVAL;
273         }
274                 
275         /* Check FIFO size? */
276         if (!_desc->wMaxPacketSize) 
277         {
278                 DWC_WARN("%s, bad %s maxpacket\n", __func__, _ep->name);
279                 return -ERANGE;
280         }
281
282         pcd = ep->pcd;
283         if (!pcd->driver || pcd->gadget.speed == USB_SPEED_UNKNOWN) 
284         {
285                 DWC_WARN("%s, bogus device state\n", __func__);
286                 return -ESHUTDOWN;
287         }
288
289         SPIN_LOCK_IRQSAVE(&pcd->lock, flags);
290                 
291         ep->desc = _desc;
292         ep->ep.maxpacket = le16_to_cpu (_desc->wMaxPacketSize);
293                 
294         /*
295          * Activate the EP
296          */
297         ep->stopped = 0;
298                 
299         ep->dwc_ep.is_in = (USB_DIR_IN & _desc->bEndpointAddress) != 0;
300         ep->dwc_ep.maxpacket = ep->ep.maxpacket;
301         
302         ep->dwc_ep.type = _desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
303
304         if(ep->dwc_ep.is_in)
305         {
306 #ifndef CONFIG_ARCH_RK29
307                 if(!pcd->otg_dev->core_if->en_multiple_tx_fifo)
308                 {
309                         ep->dwc_ep.tx_fifo_num = 0;
310                 
311                         if ((_desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == 
312                                 USB_ENDPOINT_XFER_ISOC ) 
313                         {
314                                 /* 
315                                  * if ISOC EP then assign a Periodic Tx FIFO.
316                                  */
317                                 ep->dwc_ep.tx_fifo_num = assign_perio_tx_fifo(pcd->otg_dev->core_if);
318                          }
319                 }
320                 else
321                 {
322                         /* 
323                          * if Dedicated FIFOs mode is on then assign a Tx FIFO.
324                          */
325                         ep->dwc_ep.tx_fifo_num = assign_tx_fifo(pcd->otg_dev->core_if);
326                 }
327 #else
328         /* yk@rk
329          * ep0 -- tx fifo 0
330          * ep1 -- tx fifo 1
331          * ep3 -- tx fifo 3
332          * ep5 -- tx fifo 2
333          */
334         if(ep->dwc_ep.num == 0)
335                 ep->dwc_ep.tx_fifo_num = 0;
336         else if(ep->dwc_ep.num == 1)
337                 ep->dwc_ep.tx_fifo_num = 1;
338         else if(ep->dwc_ep.num == 3)
339                 ep->dwc_ep.tx_fifo_num = 3;
340         else if(ep->dwc_ep.num == 5)
341                 ep->dwc_ep.tx_fifo_num = 2;
342         else
343             ep->dwc_ep.tx_fifo_num = (ep->dwc_ep.num>>1)+1 ; /* 1,3,5 */
344 #endif
345         }                
346         /* Set initial data PID. */
347         if ((_desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == 
348                         USB_ENDPOINT_XFER_BULK ) 
349         {
350                 ep->dwc_ep.data_pid_start = 0;  
351         }
352                 
353         DWC_DEBUGPL(DBG_PCD, "Activate %s-%s: type=%d, mps=%d desc=%p\n", 
354                                         ep->ep.name, (ep->dwc_ep.is_in ?"IN":"OUT"),
355                                         ep->dwc_ep.type, ep->dwc_ep.maxpacket, ep->desc );
356                 
357         dwc_otg_ep_activate( GET_CORE_IF(pcd), &ep->dwc_ep );
358         SPIN_UNLOCK_IRQRESTORE(&pcd->lock, flags);
359         return 0;
360 }
361
362 /** 
363  * This function is called when an EP is disabled due to disconnect or
364  * change in configuration. Any pending requests will terminate with a
365  * status of -ESHUTDOWN.
366  *
367  * This function modifies the dwc_otg_ep_t data structure for this EP,
368  * and then calls dwc_otg_ep_deactivate.
369  */
370 static int dwc_otg_pcd_ep_disable(struct usb_ep *_ep)
371 {
372         dwc_otg_pcd_ep_t *ep;
373         unsigned long flags;
374
375              
376         DWC_DEBUGPL(DBG_PCDV,"%s(%p)\n", __func__, _ep);
377         ep = container_of(_ep, dwc_otg_pcd_ep_t, ep);
378         if (!_ep || !ep->desc) 
379         {
380                 DWC_DEBUGPL(DBG_PCD, "%s, %s not enabled\n", __func__,
381                         _ep ? ep->ep.name : NULL);
382                 return -EINVAL;
383         }
384                 
385         SPIN_LOCK_IRQSAVE(&ep->pcd->lock, flags);
386         request_nuke( ep );                
387
388         dwc_otg_ep_deactivate( GET_CORE_IF(ep->pcd), &ep->dwc_ep );
389         ep->desc = 0;
390         ep->stopped = 1;
391         
392         if(ep->dwc_ep.is_in)
393         {
394                 release_perio_tx_fifo(GET_CORE_IF(ep->pcd), ep->dwc_ep.tx_fifo_num);
395                 release_tx_fifo(GET_CORE_IF(ep->pcd), ep->dwc_ep.tx_fifo_num);
396         }       
397         
398         SPIN_UNLOCK_IRQRESTORE(&ep->pcd->lock, flags);
399
400         DWC_DEBUGPL(DBG_PCD, "%s disabled\n", _ep->name);
401         return 0;
402 }
403
404 /**
405  * This function allocates a request object to use with the specified
406  * endpoint.
407  *
408  * @param _ep The endpoint to be used with with the request
409  * @param _gfp_flags the GFP_* flags to use.
410  */
411 static struct usb_request *dwc_otg_pcd_alloc_request(struct usb_ep *_ep,
412                                                                                                          gfp_t _gfp_flags)
413 {
414         dwc_otg_pcd_request_t *req;
415         DWC_DEBUGPL(DBG_PCDV,"%s(%p,%d)\n", __func__, _ep, _gfp_flags);
416         if (0 == _ep ) 
417         {
418                 DWC_WARN("%s() %s\n", __func__, "Invalid EP!\n");
419                 return 0;
420         }
421         req = kmalloc( sizeof(dwc_otg_pcd_request_t), _gfp_flags);
422         if (0 == req)
423         {
424                 DWC_WARN("%s() %s\n", __func__, 
425                                  "request allocation failed!\n");
426                 return 0;
427         }
428         memset(req, 0, sizeof(dwc_otg_pcd_request_t));
429         req->req.dma = DMA_ADDR_INVALID;
430         INIT_LIST_HEAD(&req->queue);
431         return &req->req;
432 }
433
434 /**
435  * This function frees a request object.
436  *
437  * @param _ep The endpoint associated with the request
438  * @param _req The request being freed
439  */
440 static void dwc_otg_pcd_free_request(struct usb_ep *_ep,
441                                                                          struct usb_request *_req)
442 {
443         dwc_otg_pcd_request_t *req;
444         DWC_DEBUGPL(DBG_PCDV,"%s(%p,%p)\n", __func__, _ep, _req);
445
446         if (0 == _ep || 0 == _req) 
447         {
448                 DWC_WARN("%s() %s\n", __func__, 
449                                  "Invalid ep or req argument!\n");
450                 return;
451         }
452                 
453         req = container_of(_req, dwc_otg_pcd_request_t, req);
454         kfree(req);
455 }
456 #if 0
457 /**
458  * This function allocates an I/O buffer to be used for a transfer
459  * to/from the specified endpoint.
460  * 
461  * @param _ep The endpoint to be used with with the request
462  * @param _bytes The desired number of bytes for the buffer
463  * @param _dma Pointer to the buffer's DMA address; must be valid
464  * @param _gfp_flags the GFP_* flags to use.
465  * @return address of a new buffer or null is buffer could not be allocated.
466  */
467 static void *dwc_otg_pcd_alloc_buffer(struct usb_ep *_ep, unsigned _bytes,
468                                                                           dma_addr_t *_dma, int _gfp_flags)
469 {
470         void *buf;
471         dwc_otg_pcd_ep_t *ep;
472         dwc_otg_pcd_t *pcd = 0;
473
474         ep = container_of(_ep, dwc_otg_pcd_ep_t, ep);
475         pcd = ep->pcd;
476
477         DWC_DEBUGPL(DBG_PCDV,"%s(%p,%d,%p,%0x)\n", __func__, _ep, _bytes, 
478                                 _dma, _gfp_flags);
479
480         /* Check dword alignment */
481         if ((_bytes & 0x3UL) != 0) 
482         {
483                 DWC_WARN("%s() Buffer size is not a multiple of" 
484                                  "DWORD size (%d)",__func__, _bytes);
485         }
486
487         if (GET_CORE_IF(pcd)->dma_enable) 
488         {
489                 buf = dma_alloc_coherent (NULL, _bytes, _dma, _gfp_flags);
490         }
491         else 
492         {
493                 buf = kmalloc( _bytes, _gfp_flags);
494         }
495
496         /* Check dword alignment */
497         if (((int)buf & 0x3UL) != 0) 
498         {
499                 DWC_WARN("%s() Buffer is not DWORD aligned (%p)",
500                                         __func__, buf);
501         }
502                 
503         return buf;
504 }
505
506 /**
507  * This function frees an I/O buffer that was allocated by alloc_buffer.
508  *
509  * @param _ep the endpoint associated with the buffer
510  * @param _buf address of the buffer
511  * @param _dma The buffer's DMA address
512  * @param _bytes The number of bytes of the buffer
513  */
514 static void dwc_otg_pcd_free_buffer(struct usb_ep *_ep, void *_buf,
515                                                                         dma_addr_t _dma, unsigned _bytes)
516 {
517         dwc_otg_pcd_ep_t *ep;
518         dwc_otg_pcd_t *pcd = 0;
519
520         ep = container_of(_ep, dwc_otg_pcd_ep_t, ep);
521         pcd = ep->pcd;
522
523         DWC_DEBUGPL(DBG_PCDV,"%s(%p,%p,%0x,%d)\n", __func__, _ep, _buf, _dma, _bytes);
524         
525         if (GET_CORE_IF(pcd)->dma_enable) 
526         {
527                 dma_free_coherent (NULL, _bytes, _buf, _dma);
528         }
529         else 
530         {
531                 kfree( _buf );
532         }
533 }
534 #endif
535 /**
536  * This function is used to submit an I/O Request to an EP.
537  *
538  *      - When the request completes the request's completion callback
539  *        is called to return the request to the driver.
540  *      - An EP, except control EPs, may have multiple requests
541  *        pending.
542  *      - Once submitted the request cannot be examined or modified.
543  *      - Each request is turned into one or more packets.
544  *      - A BULK EP can queue any amount of data; the transfer is
545  *        packetized.
546  *      - Zero length Packets are specified with the request 'zero'
547  *        flag.
548  */
549 static int dwc_otg_pcd_ep_queue(struct usb_ep *_ep, 
550                                                                 struct usb_request *_req, gfp_t _gfp_flags)
551 {
552         int prevented = 0;
553         dwc_otg_pcd_request_t *req;
554         dwc_otg_pcd_ep_t *ep;
555         dwc_otg_pcd_t   *pcd;
556         unsigned long flags = 0;
557
558         DWC_DEBUGPL(DBG_PCDV,"%s(%p,%p,%d)\n", 
559                                 __func__, _ep, _req, _gfp_flags);
560         
561         req = container_of(_req, dwc_otg_pcd_request_t, req);
562         if (!_req || !_req->complete || !_req->buf ) 
563         {
564                 DWC_WARN("%s, bad params\n", __func__);
565                 return -EINVAL;
566         }
567
568         /* 20091226,HSL@RK */
569         if ( !list_empty(&req->queue) ) 
570         {
571         while(!list_empty(&req->queue) ) {
572                 ep = container_of(_ep, dwc_otg_pcd_ep_t, ep);
573                 request_done(ep, req, -ECONNABORTED);
574         DWC_PRINT("%s::ep %s req not empty,done it error!\n" , __func__, _ep->name);
575         }
576                 return -EINVAL;
577         }
578         
579         ep = container_of(_ep, dwc_otg_pcd_ep_t, ep);
580         if (!_ep || (!ep->desc && ep->dwc_ep.num != 0)) 
581         {
582                 DWC_WARN("%s, bad ep\n", __func__);
583                 return -EINVAL;
584         }
585         pcd = ep->pcd;
586         if (!pcd->driver || pcd->gadget.speed == USB_SPEED_UNKNOWN) 
587         {
588                 DWC_DEBUGPL(DBG_PCDV, "gadget.speed=%d\n", pcd->gadget.speed);
589                 DWC_WARN("%s, bogus device state\n", __func__);
590                 return -ESHUTDOWN;
591         }
592
593
594         DWC_DEBUGPL(DBG_PCD, "%s queue req %p, len %d buf %p\n",
595                                    _ep->name, _req, _req->length, _req->buf);
596
597         if (!GET_CORE_IF(pcd)->core_params->opt) 
598         {
599                 if (ep->dwc_ep.num != 0) 
600                 {
601                         DWC_ERROR("%s queue req %p, len %d buf %p\n",
602                                           _ep->name, _req, _req->length, _req->buf);
603                 }
604         }
605
606
607 #if defined(DEBUG) & defined(VERBOSE)
608         dump_msg(_req->buf, _req->length);
609 #endif  
610         /* map virtual address to hardware */
611         if (req->req.dma == DMA_ADDR_INVALID) {
612                 req->req.dma = dma_map_single(ep->pcd->gadget.dev.parent,
613                                         req->req.buf,
614                                         req->req.length, ep->dwc_ep.is_in
615                                                 ? DMA_TO_DEVICE
616                                                 : DMA_FROM_DEVICE);
617                 req->mapped = 1;
618         } else {
619                 dma_sync_single_for_device(ep->pcd->gadget.dev.parent,
620                                         req->req.dma, req->req.length,
621                                         ep->dwc_ep.is_in
622                                                 ? DMA_TO_DEVICE
623                                                 : DMA_FROM_DEVICE);
624                 req->mapped = 0;
625         }
626         SPIN_LOCK_IRQSAVE(&pcd->lock, flags);
627         
628         _req->status = -EINPROGRESS;
629         _req->actual = 0;
630
631         /* 
632          * For EP0 IN without premature status, zlp is required?
633          */
634         if (ep->dwc_ep.num == 0 && ep->dwc_ep.is_in) 
635         {
636                 DWC_DEBUGPL(DBG_PCDV, "%s-OUT ZLP\n", _ep->name);
637 //              _req->zero = 1;
638         }
639
640         /* Start the transfer */
641         if (list_empty(&ep->queue) && !ep->stopped) 
642         {
643                 /* EP0 Transfer? */
644                 if (ep->dwc_ep.num == 0) 
645                 {
646                         switch (pcd->ep0state) 
647                         {
648                         case EP0_IN_DATA_PHASE:
649                                 DWC_DEBUGPL(DBG_PCD, 
650                                                                 "%s ep0: EP0_IN_DATA_PHASE\n", 
651                                                                 __func__);
652                                 break;
653
654                         case EP0_OUT_DATA_PHASE:
655                                 DWC_DEBUGPL(DBG_PCD, 
656                                                                 "%s ep0: EP0_OUT_DATA_PHASE\n", 
657                                                                 __func__);
658                                 if (pcd->request_config) 
659                                 { 
660                                         /* Complete STATUS PHASE */
661                                         ep->dwc_ep.is_in = 1;
662                                         pcd->ep0state = EP0_STATUS;
663                                 }
664                                 break;
665                                                 
666                         default:
667                                 DWC_DEBUGPL(DBG_ANY, "ep0: odd state %d\n", 
668                                                                                         pcd->ep0state);
669                                 SPIN_UNLOCK_IRQRESTORE(&pcd->lock, flags);
670                                 return -EL2HLT;
671                         }
672
673                         ep->dwc_ep.dma_addr = _req->dma;
674                         ep->dwc_ep.start_xfer_buff = _req->buf;
675                         ep->dwc_ep.xfer_buff = _req->buf;
676                         ep->dwc_ep.xfer_len = _req->length;
677                         ep->dwc_ep.xfer_count = 0;
678                         ep->dwc_ep.sent_zlp = 0;
679                         ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
680                         dwc_otg_ep0_start_transfer( GET_CORE_IF(pcd), 
681                                                                                 &ep->dwc_ep );
682                 } 
683                 else 
684                 {
685                         /* Setup and start the Transfer */
686                         ep->dwc_ep.dma_addr = _req->dma;
687                         ep->dwc_ep.start_xfer_buff = _req->buf;
688                         ep->dwc_ep.xfer_buff = _req->buf;
689                         ep->dwc_ep.xfer_len = _req->length;
690                         ep->dwc_ep.xfer_count = 0;
691                         ep->dwc_ep.sent_zlp = 0;
692                         ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
693                         dwc_otg_ep_start_transfer( GET_CORE_IF(pcd), 
694                                                                            &ep->dwc_ep );
695                 }
696         }
697
698         if ((req != 0) || prevented) 
699         {
700                 ++pcd->request_pending;
701                 list_add_tail(&req->queue, &ep->queue);
702                 if (ep->dwc_ep.is_in && ep->stopped && !(GET_CORE_IF(pcd)->dma_enable)) 
703                 {
704                         /** @todo NGS Create a function for this. */
705                         diepmsk_data_t diepmsk = { .d32 = 0};
706                         diepmsk.b.intktxfemp = 1;
707                         dwc_modify_reg32( &GET_CORE_IF(pcd)->dev_if->dev_global_regs->diepmsk, 0, diepmsk.d32 );
708                 }
709         }
710                 
711         SPIN_UNLOCK_IRQRESTORE(&pcd->lock, flags);
712         return 0;
713 }
714
715 /**
716  * This function cancels an I/O request from an EP.
717  */
718 static int dwc_otg_pcd_ep_dequeue(struct usb_ep *_ep,
719                                                                   struct usb_request *_req)
720 {
721         dwc_otg_pcd_request_t *req;
722         dwc_otg_pcd_ep_t *ep;
723         dwc_otg_pcd_t   *pcd;
724         unsigned long flags;
725     volatile depctl_data_t depctl = {.d32 = 0};
726     
727     dwc_otg_dev_out_ep_regs_t *out_regs;
728
729         DWC_DEBUGPL(DBG_PCDV,"%s(%p,%p)\n", __func__, _ep, _req);
730                 
731         ep = container_of(_ep, dwc_otg_pcd_ep_t, ep);
732         if (!_ep || !_req || (!ep->desc && ep->dwc_ep.num != 0)) 
733         {
734                 DWC_WARN("%s, bad argument\n", __func__);
735                 return -EINVAL;
736         }
737         pcd = ep->pcd;
738         if (!pcd->driver || pcd->gadget.speed == USB_SPEED_UNKNOWN) 
739         {
740                 DWC_WARN("%s, bogus device state, %p, speed %d\n", __func__, pcd->driver, pcd->gadget.speed);
741                 return -ESHUTDOWN;
742         }
743
744         SPIN_LOCK_IRQSAVE(&pcd->lock, flags);
745         DWC_DEBUGPL(DBG_PCDV, "%s %s %s %p\n", __func__, _ep->name,
746                                         ep->dwc_ep.is_in ? "IN" : "OUT",
747                                         _req);
748
749         /* make sure it's actually queued on this endpoint */
750         list_for_each_entry( req, &ep->queue, queue) 
751         {
752                 if (&req->req == _req) 
753                 {
754                         break;
755                 }
756         }
757
758         if (&req->req != _req) 
759         {
760                 SPIN_UNLOCK_IRQRESTORE(&pcd->lock, flags);
761                 return -EINVAL;
762         }
763
764         if (!list_empty(&req->queue)) 
765         {                
766                 request_done(ep, req, -ECONNRESET);
767         } 
768         else 
769         {
770                 req = 0;
771         }
772
773         // kevery@20120602 NAK out request before new queue request
774         if(!ep->dwc_ep.is_in){
775         out_regs = GET_CORE_IF(pcd)->dev_if->out_ep_regs[ep->dwc_ep.num];
776         depctl.d32 = dwc_read_reg32(&(out_regs->doepctl));
777         depctl.b.snak = 1;
778         dwc_write_reg32( &(out_regs->doepctl), depctl.d32 );
779     }
780         SPIN_UNLOCK_IRQRESTORE(&pcd->lock, flags);
781
782         return req ? 0 : -EOPNOTSUPP;
783 }
784
785 /**
786  * usb_ep_set_halt stalls an endpoint. 
787  *
788  * usb_ep_clear_halt clears an endpoint halt and resets its data
789  * toggle.
790  *
791  * Both of these functions are implemented with the same underlying
792  * function. The behavior depends on the value argument.
793  * 
794  * @param[in] _ep the Endpoint to halt or clear halt.
795  * @param[in] _value 
796  *      - 0 means clear_halt.
797  *      - 1 means set_halt, 
798  *      - 2 means clear stall lock flag.
799  *      - 3 means set  stall lock flag.
800  */
801 static int dwc_otg_pcd_ep_set_halt(struct usb_ep *_ep, int _value)
802 {
803         int retval = 0;
804         unsigned long flags;
805         dwc_otg_pcd_ep_t *ep = 0;
806                 
807                 
808         DWC_DEBUGPL(DBG_PCD,"HALT %s %d\n", _ep->name, _value);
809
810         ep = container_of(_ep, dwc_otg_pcd_ep_t, ep);
811
812         if (!_ep || (!ep->desc && ep != &ep->pcd->ep0) ||
813                         ep->desc->bmAttributes == USB_ENDPOINT_XFER_ISOC) 
814         {
815                 DWC_WARN("%s, bad ep\n", __func__);
816                 return -EINVAL;
817         }
818                 
819         SPIN_LOCK_IRQSAVE(&ep->pcd->lock, flags);
820         if (!list_empty(&ep->queue))
821         {
822                 DWC_WARN("%s() %s XFer In process\n", __func__, _ep->name);
823                 retval = -EAGAIN;
824         }
825         else if (_value == 0) 
826         {
827                 dwc_otg_ep_clear_stall( ep->pcd->otg_dev->core_if, 
828                                                                         &ep->dwc_ep );           
829         }
830         else if(_value == 1)
831         {
832                 if (ep->dwc_ep.num == 0) 
833                 {
834                         ep->pcd->ep0state = EP0_STALL;
835                 }
836                 
837                 ep->stopped = 1;
838                 dwc_otg_ep_set_stall( ep->pcd->otg_dev->core_if, 
839                                                                 &ep->dwc_ep );
840         }
841         else if (_value == 2) 
842         {
843                 ep->dwc_ep.stall_clear_flag = 0;
844         }
845         else if (_value == 3) 
846         {
847                 ep->dwc_ep.stall_clear_flag = 1;
848         }
849         
850         SPIN_UNLOCK_IRQRESTORE(&ep->pcd->lock, flags);
851         return retval;
852 }
853
854
855 static struct usb_ep_ops dwc_otg_pcd_ep_ops = 
856 {
857         .enable         = dwc_otg_pcd_ep_enable,
858         .disable        = dwc_otg_pcd_ep_disable,
859
860         .alloc_request  = dwc_otg_pcd_alloc_request,
861         .free_request   = dwc_otg_pcd_free_request,
862
863 //      .alloc_buffer   = dwc_otg_pcd_alloc_buffer,
864 //      .free_buffer    = dwc_otg_pcd_free_buffer,
865
866         .queue          = dwc_otg_pcd_ep_queue,
867         .dequeue        = dwc_otg_pcd_ep_dequeue,
868
869         .set_halt       = dwc_otg_pcd_ep_set_halt,
870         .fifo_status    = 0,
871         .fifo_flush = 0,
872 };
873
874 /*      Gadget Operations */
875 /**
876  * The following gadget operations will be implemented in the DWC_otg
877  * PCD. Functions in the API that are not described below are not
878  * implemented.
879  *
880  * The Gadget API provides wrapper functions for each of the function
881  * pointers defined in usb_gadget_ops. The Gadget Driver calls the
882  * wrapper function, which then calls the underlying PCD function. The
883  * following sections are named according to the wrapper functions
884  * (except for ioctl, which doesn't have a wrapper function). Within
885  * each section, the corresponding DWC_otg PCD function name is
886  * specified.
887  *
888  */
889
890 /**
891  *Gets the USB Frame number of the last SOF.
892  */
893 static int dwc_otg_pcd_get_frame(struct usb_gadget *_gadget)
894 {
895         dwc_otg_pcd_t *pcd;
896         
897         DWC_DEBUGPL(DBG_PCDV,"%s(%p)\n", __func__, _gadget);
898                 
899         if (_gadget == 0)
900         {
901                 return -ENODEV;
902         } 
903         else 
904         {
905                 pcd = container_of(_gadget, dwc_otg_pcd_t, gadget);
906                 dwc_otg_get_frame_number( GET_CORE_IF(pcd) );
907         }
908                 
909         return 0;
910 }
911
912 void dwc_otg_pcd_initiate_srp(dwc_otg_pcd_t *_pcd)
913 {
914         uint32_t *addr = (uint32_t *)&(GET_CORE_IF(_pcd)->core_global_regs->gotgctl);
915         gotgctl_data_t mem;
916         gotgctl_data_t val;
917                 
918         val.d32 = dwc_read_reg32( addr );
919         if (val.b.sesreq) 
920         {
921                 DWC_ERROR("Session Request Already active!\n");
922                         return;
923         }
924
925         DWC_NOTICE("Session Request Initated\n");
926         mem.d32 = dwc_read_reg32(addr);
927         mem.b.sesreq = 1;
928         dwc_write_reg32(addr, mem.d32);
929
930         /* Start the SRP timer */
931         dwc_otg_pcd_start_srp_timer( _pcd );
932         return;
933 }
934
935 void dwc_otg_pcd_remote_wakeup(dwc_otg_pcd_t *_pcd, int set)
936 {
937         dctl_data_t dctl = {.d32=0};
938         volatile uint32_t *addr = 
939                                 &(GET_CORE_IF(_pcd)->dev_if->dev_global_regs->dctl);
940
941         if (dwc_otg_is_device_mode(GET_CORE_IF(_pcd))) 
942         {
943                 if (_pcd->remote_wakeup_enable) 
944                 {
945                         if (set) 
946                         {
947                                 dctl.b.rmtwkupsig = 1;
948                                 dwc_modify_reg32( addr, 0, dctl.d32 );
949                                 DWC_DEBUGPL(DBG_PCD, "Set Remote Wakeup\n");
950                                 mdelay(1);
951                                 dwc_modify_reg32( addr, dctl.d32, 0 );
952                                 DWC_DEBUGPL(DBG_PCD, "Clear Remote Wakeup\n");
953                         }
954                         else 
955                         {
956                         }
957                 }
958                 else 
959                 {
960                         DWC_DEBUGPL(DBG_PCD, "Remote Wakeup is disabled\n");
961                 }
962         }
963
964         return;
965 }
966
967 /**
968  * Initiates Session Request Protocol (SRP) to wakeup the host if no
969  * session is in progress. If a session is already in progress, but
970  * the device is suspended, remote wakeup signaling is started.
971  *
972  */
973 static int dwc_otg_pcd_wakeup(struct usb_gadget *_gadget)
974 {
975         unsigned long flags;
976         dwc_otg_pcd_t *pcd;
977         dsts_data_t             dsts;
978         gotgctl_data_t  gotgctl;
979                 
980         DWC_DEBUGPL(DBG_PCDV,"%s(%p)\n", __func__, _gadget);
981                 
982         if (_gadget == 0)
983         {
984                 return -ENODEV;
985         } 
986         else 
987         {
988                 pcd = container_of(_gadget, dwc_otg_pcd_t, gadget);
989         }
990         SPIN_LOCK_IRQSAVE(&pcd->lock, flags);
991
992         /*
993          * This function starts the Protocol if no session is in progress. If
994          * a session is already in progress, but the device is suspended,
995          * remote wakeup signaling is started.
996          */
997
998         /* Check if valid session */
999         gotgctl.d32 = dwc_read_reg32(&(GET_CORE_IF(pcd)->core_global_regs->gotgctl));
1000         if (gotgctl.b.bsesvld) 
1001         {
1002                 /* Check if suspend state */
1003                 dsts.d32 = dwc_read_reg32(&(GET_CORE_IF(pcd)->dev_if->dev_global_regs->dsts));
1004                 if (dsts.b.suspsts) 
1005                 {
1006                         dwc_otg_pcd_remote_wakeup(pcd, 1);
1007                 }
1008         }
1009         else 
1010         {
1011                 dwc_otg_pcd_initiate_srp(pcd);
1012         }
1013
1014         SPIN_UNLOCK_IRQRESTORE(&pcd->lock, flags);
1015         return 0;
1016 }
1017
1018 static int dwc_otg_pcd_pullup(struct usb_gadget *_gadget, int is_on)
1019 {
1020         //unsigned long flags;
1021         dwc_otg_pcd_t *pcd;
1022     dctl_data_t dctl = {.d32=0};
1023     dwc_otg_core_if_t *core_if;
1024         DWC_DEBUGPL(DBG_PCDV,"%s(%p)\n", __func__, _gadget);
1025                 
1026         if (_gadget == 0)
1027         {
1028                 return -ENODEV;
1029         } 
1030         else 
1031         {
1032                 pcd = container_of(_gadget, dwc_otg_pcd_t, gadget);
1033         core_if = GET_CORE_IF(pcd); 
1034         }
1035         if(is_on)   //connect
1036         {
1037 #ifdef CONFIG_DWC_CONN_EN
1038         pcd->conn_en = 1;
1039 #else
1040         pcd->conn_en = 0;
1041 #endif
1042         pcd->conn_status = 0;
1043     }
1044     else        //disconnect
1045     {
1046         dctl.d32 = dwc_read_reg32( &core_if->dev_if->dev_global_regs->dctl );
1047         dctl.b.sftdiscon = 1;
1048         dwc_write_reg32( &core_if->dev_if->dev_global_regs->dctl, dctl.d32 );
1049         pcd->conn_status = 3;
1050     }
1051     return 0;
1052 }
1053
1054 static const struct usb_gadget_ops dwc_otg_pcd_ops = 
1055 {
1056         .get_frame       = dwc_otg_pcd_get_frame,
1057         .wakeup          = dwc_otg_pcd_wakeup,
1058         .pullup      = dwc_otg_pcd_pullup,
1059         // current versions must always be self-powered
1060 };
1061
1062 /**
1063  * This function updates the otg values in the gadget structure. 
1064  */
1065 void dwc_otg_pcd_update_otg( dwc_otg_pcd_t *_pcd, const unsigned _reset )
1066 {
1067                 
1068         if (!_pcd->gadget.is_otg)
1069                 return;
1070
1071         if (_reset) 
1072         {
1073                 _pcd->b_hnp_enable = 0;
1074                 _pcd->a_hnp_support = 0;
1075                 _pcd->a_alt_hnp_support = 0;
1076         }
1077
1078         _pcd->gadget.b_hnp_enable = _pcd->b_hnp_enable;
1079         _pcd->gadget.a_hnp_support =  _pcd->a_hnp_support;
1080         _pcd->gadget.a_alt_hnp_support = _pcd->a_alt_hnp_support;
1081 }
1082
1083 /** 
1084  * This function is the top level PCD interrupt handler.
1085  */
1086 static irqreturn_t 
1087 dwc_otg_pcd_irq(int _irq, void *_dev)
1088 {
1089         dwc_otg_pcd_t *pcd = _dev;
1090         int32_t retval = IRQ_NONE;
1091
1092         retval = dwc_otg_pcd_handle_intr( pcd );
1093         return IRQ_RETVAL(retval);
1094 }
1095
1096 /**
1097  * PCD Callback function for initializing the PCD when switching to
1098  * device mode.
1099  *
1100  * @param _p void pointer to the <code>dwc_otg_pcd_t</code>
1101  */
1102  
1103 static int32_t dwc_otg_pcd_start_cb( void *_p )
1104 {
1105         dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *)_p;
1106         
1107         /*
1108          * Initialized the Core for Device mode.
1109          */
1110         if (dwc_otg_is_device_mode( GET_CORE_IF(pcd) ))
1111         {
1112         pcd->phy_suspend = 1;
1113         pcd->vbus_status = 0;
1114         dwc_otg_pcd_start_vbus_timer(pcd);
1115         }
1116         
1117         return 1;
1118 }
1119
1120 /**
1121  * PCD Callback function for stopping the PCD when switching to Host
1122  * mode.
1123  *
1124  * @param _p void pointer to the <code>dwc_otg_pcd_t</code>
1125  */
1126 static int32_t dwc_otg_pcd_stop_cb( void *_p )
1127 {
1128         dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *)_p;
1129         extern void dwc_otg_pcd_stop(dwc_otg_pcd_t *_pcd);
1130         
1131         dwc_otg_pcd_stop( pcd );
1132         return 1;
1133 }
1134
1135
1136 /**
1137  * PCD Callback function for notifying the PCD when resuming from
1138  * suspend.
1139  *
1140  * @param _p void pointer to the <code>dwc_otg_pcd_t</code>
1141  */
1142 static int32_t dwc_otg_pcd_suspend_cb( void *_p ,int suspend)
1143 {
1144         dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *)_p;
1145 //#ifdef CONFIG_ANDROID_POWER
1146 #if 0
1147         /* yk@rk 20100520
1148          * PC disconnect the USB, unlock the msc_lock and
1149          * system can enter level 2 sleep mode.
1150          */
1151         struct usb_composite_dev        *cdev;
1152         if (pcd->driver && pcd->driver->resume) 
1153         {
1154                 cdev = get_gadget_data(&pcd->gadget);
1155                 if(cdev->config)
1156                         pcd->conn_status = 3;
1157         }
1158 #endif
1159 //#endif                
1160         if (pcd->driver && pcd->driver->resume) 
1161         {
1162                 pcd->driver->suspend(&pcd->gadget);
1163         }
1164         return 1;
1165 }
1166
1167
1168 /**
1169  * PCD Callback function for notifying the PCD when resuming from
1170  * suspend.
1171  *
1172  * @param _p void pointer to the <code>dwc_otg_pcd_t</code>
1173  */
1174 static int32_t dwc_otg_pcd_resume_cb( void *_p )
1175 {
1176         dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *)_p;
1177         
1178         if (pcd->driver && pcd->driver->resume) 
1179         {
1180                         pcd->driver->resume(&pcd->gadget);
1181         }
1182         
1183         /* Stop the SRP timeout timer. */
1184         if ((GET_CORE_IF(pcd)->core_params->phy_type != DWC_PHY_TYPE_PARAM_FS) ||
1185                 (!GET_CORE_IF(pcd)->core_params->i2c_enable))
1186         {
1187                 if (GET_CORE_IF(pcd)->srp_timer_started) 
1188                 {
1189                         GET_CORE_IF(pcd)->srp_timer_started = 0;
1190                         del_timer( &pcd->srp_timer );
1191                 }
1192         }
1193         return 1;
1194 }
1195
1196
1197 /**
1198  * PCD Callback structure for handling mode switching.
1199  */
1200 static dwc_otg_cil_callbacks_t pcd_callbacks = 
1201 {
1202         .start = dwc_otg_pcd_start_cb,
1203         .stop = dwc_otg_pcd_stop_cb,
1204         .suspend = dwc_otg_pcd_suspend_cb,
1205         .resume_wakeup = dwc_otg_pcd_resume_cb,
1206         .p = 0, /* Set at registration */
1207 };
1208
1209 /**
1210  * This function is called when the SRP timer expires.  The SRP should
1211  * complete within 6 seconds. 
1212  */
1213 static void srp_timeout( unsigned long _ptr )
1214 {
1215         gotgctl_data_t gotgctl;
1216         dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *)_ptr;
1217         volatile uint32_t *addr = &core_if->core_global_regs->gotgctl;
1218
1219         gotgctl.d32 = dwc_read_reg32(addr);
1220
1221         core_if->srp_timer_started = 0;
1222
1223         if ((core_if->core_params->phy_type == DWC_PHY_TYPE_PARAM_FS) && 
1224                 (core_if->core_params->i2c_enable))
1225         {
1226                 DWC_PRINT( "SRP Timeout\n");
1227
1228                 if ((core_if->srp_success) && 
1229                         (gotgctl.b.bsesvld))
1230                 {
1231                         if (core_if->pcd_cb && core_if->pcd_cb->resume_wakeup ) 
1232                         {
1233                                 core_if->pcd_cb->resume_wakeup(core_if->pcd_cb->p);
1234                         }
1235                         
1236                         /* Clear Session Request */
1237                         gotgctl.d32 = 0;
1238                         gotgctl.b.sesreq = 1;
1239                         dwc_modify_reg32( &core_if->core_global_regs->gotgctl, 
1240                                           gotgctl.d32, 0);
1241         
1242                         core_if->srp_success = 0;
1243                 }
1244                 else 
1245                 {
1246                         DWC_ERROR( "Device not connected/responding\n");
1247                         gotgctl.b.sesreq = 0;
1248                         dwc_write_reg32(addr, gotgctl.d32);
1249                 }
1250         }
1251         else if (gotgctl.b.sesreq) 
1252         {
1253                 DWC_PRINT( "SRP Timeout\n");
1254
1255                 DWC_ERROR( "Device not connected/responding\n");
1256                 gotgctl.b.sesreq = 0;
1257                 dwc_write_reg32(addr, gotgctl.d32);
1258         } 
1259         else 
1260         {
1261                 DWC_PRINT( " SRP GOTGCTL=%0x\n", gotgctl.d32);
1262         } 
1263 }
1264
1265 /**
1266  * Start the SRP timer to detect when the SRP does not complete within 
1267  * 6 seconds.
1268  *
1269  * @param _pcd the pcd structure.
1270  */
1271 void dwc_otg_pcd_start_srp_timer(dwc_otg_pcd_t *_pcd )
1272 {
1273         struct timer_list *srp_timer = &_pcd->srp_timer;
1274         GET_CORE_IF(_pcd)->srp_timer_started = 1;
1275         init_timer( srp_timer );
1276         srp_timer->function = srp_timeout;
1277         srp_timer->data = (unsigned long)GET_CORE_IF(_pcd);
1278         srp_timer->expires = jiffies + (HZ*6);
1279         add_timer( srp_timer );
1280 }
1281
1282 /**
1283  * Tasklet
1284  *
1285  */
1286 extern void start_next_request( dwc_otg_pcd_ep_t *_ep );
1287
1288 static void start_xfer_tasklet_func (unsigned long data)
1289 {
1290         dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t*)data;
1291         dwc_otg_core_if_t *core_if = pcd->otg_dev->core_if;
1292
1293         int i;
1294         depctl_data_t diepctl;
1295
1296         DWC_DEBUGPL(DBG_PCDV, "Start xfer tasklet\n");
1297
1298         diepctl.d32 = dwc_read_reg32( &core_if->dev_if->in_ep_regs[0]->diepctl);
1299
1300         if (pcd->ep0.queue_sof) 
1301         {
1302                 pcd->ep0.queue_sof = 0;
1303                 start_next_request (&pcd->ep0);
1304                 // break;
1305         }
1306
1307         for (i=0; i<core_if->dev_if->num_in_eps; i++) 
1308         {
1309                 depctl_data_t diepctl;
1310                 diepctl.d32 = dwc_read_reg32( &core_if->dev_if->in_ep_regs[i]->diepctl);
1311
1312                 if (pcd->in_ep[i].queue_sof) 
1313                 {
1314                         pcd->in_ep[i].queue_sof = 0;
1315                         start_next_request (&pcd->in_ep[i]);
1316                         // break;
1317                 }
1318         }
1319
1320         return;
1321 }
1322
1323
1324
1325
1326
1327
1328
1329 static struct tasklet_struct start_xfer_tasklet = {
1330         .next = NULL,
1331         .state = 0,
1332         .count = ATOMIC_INIT(0),
1333         .func = start_xfer_tasklet_func,
1334         .data = 0,//pcd
1335 };
1336 /**
1337  * This function initialized the pcd Dp structures to there default
1338  * state.
1339  *
1340  * @param _pcd the pcd structure.
1341  */
1342 void dwc_otg_pcd_reinit(dwc_otg_pcd_t *_pcd)
1343 {
1344         dwc_otg_core_if_t * core_if = GET_CORE_IF(_pcd);
1345         
1346         //dwc_otg_dump_dev_registers(core_if);
1347         static const char * names[] = 
1348                 {
1349                         
1350                         "ep0",
1351                         "ep1in",        
1352                         "ep2in",        
1353                         "ep3in",        
1354                         "ep4in",        
1355 #ifdef CONFIG_ARCH_RK29
1356                         "ep5in-int",    
1357 #else
1358                         "ep5in",        
1359 #endif
1360                         "ep6in",        
1361                         "ep7in",        
1362                         "ep8in",        
1363                         "ep9in",        
1364                         "ep10in",       
1365                         "ep11in",       
1366                         "ep12in",       
1367                         "ep13in",       
1368                         "ep14in",       
1369                         "ep15in",       
1370                         "ep1out",  
1371                         "ep2out",  
1372                         "ep3out",
1373                         "ep4out",
1374                         "ep5out",
1375                         "ep6out",
1376                         "ep7out",
1377                         "ep8out",
1378                         "ep9out",
1379                         "ep10out",
1380                         "ep11out",
1381                         "ep12out",
1382                         "ep13out",
1383                         "ep14out",
1384                         "ep15out"
1385                         
1386         };
1387                 
1388         int i;
1389         int in_ep_cntr, out_ep_cntr;
1390         uint32_t hwcfg1;
1391         uint32_t num_in_eps = core_if->dev_if->num_in_eps; /* = 3 */
1392         uint32_t num_out_eps = core_if->dev_if->num_out_eps; /* = 3 */
1393         dwc_otg_pcd_ep_t *ep;
1394         DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, _pcd);
1395         
1396         INIT_LIST_HEAD (&_pcd->gadget.ep_list);
1397         _pcd->gadget.ep0 = &_pcd->ep0.ep;
1398         _pcd->gadget.speed = USB_SPEED_UNKNOWN;
1399
1400         INIT_LIST_HEAD (&_pcd->gadget.ep0->ep_list);
1401
1402         /**
1403          * Initialize the EP0 structure.
1404          */
1405         ep = &_pcd->ep0;
1406
1407         /* Init EP structure */
1408         ep->desc = 0;
1409         ep->pcd = _pcd;
1410         ep->stopped = 1;
1411
1412         /* Init DWC ep structure */
1413         ep->dwc_ep.num = 0;
1414         ep->dwc_ep.active = 0;
1415         ep->dwc_ep.tx_fifo_num = 0;
1416         /* Control until ep is actvated */
1417         ep->dwc_ep.type = DWC_OTG_EP_TYPE_CONTROL; 
1418         ep->dwc_ep.maxpacket = MAX_PACKET_SIZE;
1419         ep->dwc_ep.dma_addr = 0;
1420         ep->dwc_ep.start_xfer_buff = 0;
1421         ep->dwc_ep.xfer_buff = 0;
1422         ep->dwc_ep.xfer_len = 0;
1423         ep->dwc_ep.xfer_count = 0;
1424         ep->dwc_ep.sent_zlp = 0;
1425         ep->dwc_ep.total_len = 0;
1426         ep->queue_sof = 0;
1427
1428         /* Init the usb_ep structure. */
1429         ep->ep.name = names[0];
1430         ep->ep.ops = &dwc_otg_pcd_ep_ops;
1431
1432         /**
1433          * @todo NGS: What should the max packet size be set to
1434          * here?  Before EP type is set?
1435          */
1436         ep->ep.maxpacket = MAX_PACKET_SIZE;
1437
1438         list_add_tail (&ep->ep.ep_list, &_pcd->gadget.ep_list);
1439                 
1440         INIT_LIST_HEAD (&ep->queue);
1441         /**
1442          * Initialize the EP structures.
1443          */
1444         in_ep_cntr = 0;
1445         hwcfg1 = core_if->hwcfg1.d32 >> 3;
1446          
1447         for (i = 1; in_ep_cntr < num_in_eps; i++) 
1448         {
1449                 if((hwcfg1 & 0x1) == 0)
1450                 {
1451                         dwc_otg_pcd_ep_t *ep = &_pcd->in_ep[in_ep_cntr];
1452                         in_ep_cntr ++;
1453                         
1454                         /* Init EP structure */
1455                         ep->desc = 0;
1456                         ep->pcd = _pcd;
1457                         ep->stopped = 1;
1458         
1459                         /* Init DWC ep structure */
1460                         ep->dwc_ep.is_in = 1;
1461                         ep->dwc_ep.num = i;
1462                         ep->dwc_ep.active = 0;
1463                         ep->dwc_ep.tx_fifo_num = 0;
1464                         
1465                         /* Control until ep is actvated */
1466                         ep->dwc_ep.type = DWC_OTG_EP_TYPE_CONTROL; 
1467                         ep->dwc_ep.maxpacket = MAX_PACKET_SIZE;
1468                         ep->dwc_ep.dma_addr = 0;
1469                         ep->dwc_ep.start_xfer_buff = 0;
1470                         ep->dwc_ep.xfer_buff = 0;
1471                         ep->dwc_ep.xfer_len = 0;
1472                         ep->dwc_ep.xfer_count = 0;
1473                         ep->dwc_ep.sent_zlp = 0;
1474                         ep->dwc_ep.total_len = 0;
1475                         ep->queue_sof = 0;
1476         
1477                         /* Init the usb_ep structure. */
1478                         /**
1479                          * @todo NGS: Add direction to EP, based on contents
1480                          * of HWCFG1.  Need a copy of HWCFG1 in pcd structure?
1481                          * sprintf( ";r
1482                          */
1483                         ep->ep.name = names[i];
1484                         ep->ep.ops = &dwc_otg_pcd_ep_ops;
1485                         
1486                         /**
1487                          * @todo NGS: What should the max packet size be set to
1488                          * here?  Before EP type is set?
1489                          */
1490                         ep->ep.maxpacket = MAX_PACKET_SIZE;
1491                         
1492                         INIT_LIST_HEAD (&ep->queue);
1493
1494                         /**
1495                          * @yk@rk 20120329
1496                          * EP8&EP9 of rk30 are IN&OUT ep, we use ep8 as OUT EP default
1497                          */
1498                #ifndef CONFIG_ARCH_RK29
1499                 if(i == 8)
1500                     continue;
1501                 #endif
1502                         list_add_tail (&ep->ep.ep_list, &_pcd->gadget.ep_list);
1503                                 
1504                 }
1505                 hwcfg1 >>= 2;
1506         }
1507         out_ep_cntr = 0;
1508         hwcfg1 = core_if->hwcfg1.d32 >> 2;
1509
1510         for (i = 1; out_ep_cntr < num_out_eps; i++) 
1511         {
1512                 if((hwcfg1 & 0x1) == 0)
1513                 {
1514                         dwc_otg_pcd_ep_t *ep = &_pcd->out_ep[out_ep_cntr];
1515                         out_ep_cntr++;
1516         
1517                         /* Init EP structure */
1518                         ep->desc = 0;
1519                         ep->pcd = _pcd;
1520                         ep->stopped = 1;
1521         
1522                         /* Init DWC ep structure */
1523                         ep->dwc_ep.is_in = 0;
1524                         ep->dwc_ep.num = i;
1525                         ep->dwc_ep.active = 0;
1526                         ep->dwc_ep.tx_fifo_num = 0;
1527                         /* Control until ep is actvated */
1528                         ep->dwc_ep.type = DWC_OTG_EP_TYPE_CONTROL; 
1529                         ep->dwc_ep.maxpacket = MAX_PACKET_SIZE;
1530                         ep->dwc_ep.dma_addr = 0;
1531                         ep->dwc_ep.start_xfer_buff = 0;
1532                         ep->dwc_ep.xfer_buff = 0;
1533                         ep->dwc_ep.xfer_len = 0;
1534                         ep->dwc_ep.xfer_count = 0;
1535                         ep->dwc_ep.sent_zlp = 0;
1536                         ep->dwc_ep.total_len = 0;
1537                         ep->queue_sof = 0;
1538         
1539                         /* Init the usb_ep structure. */
1540                         /**
1541                          * @todo NGS: Add direction to EP, based on contents
1542                          * of HWCFG1.  Need a copy of HWCFG1 in pcd structure?
1543                          * sprintf( ";r
1544                          */
1545                         ep->ep.name = names[15 + i];
1546                         ep->ep.ops = &dwc_otg_pcd_ep_ops;
1547                         /**
1548                          * @todo NGS: What should the max packet size be set to
1549                          * here?  Before EP type is set?
1550                          */
1551                         ep->ep.maxpacket = MAX_PACKET_SIZE;
1552         
1553                         INIT_LIST_HEAD (&ep->queue);
1554                         
1555                         /**
1556                          * @yk@rk 20120329
1557                          * EP8&EP9 of rk30 are IN&OUT ep, we use ep9 as IN EP default
1558                          */
1559                 #ifndef CONFIG_ARCH_RK29
1560                 if(i == 9)
1561                     continue;
1562                 #endif
1563                         list_add_tail (&ep->ep.ep_list, &_pcd->gadget.ep_list);
1564                                 
1565                 }
1566                 hwcfg1 >>= 2;
1567         }
1568         
1569         /* remove ep0 from the list.  There is a ep0 pointer.*/
1570         list_del_init (&_pcd->ep0.ep.ep_list);
1571    
1572         _pcd->ep0state = EP0_DISCONNECT;
1573         _pcd->ep0.ep.maxpacket = MAX_EP0_SIZE;            
1574         _pcd->ep0.dwc_ep.maxpacket = MAX_EP0_SIZE;
1575         _pcd->ep0.dwc_ep.type = DWC_OTG_EP_TYPE_CONTROL;
1576 }
1577
1578 /**
1579  * This function releases the Gadget device.
1580  * required by device_unregister().
1581  *
1582  * @todo Should this do something?      Should it free the PCD? 
1583  */
1584 static void dwc_otg_pcd_gadget_release(struct device *_dev)
1585 {
1586         DWC_DEBUGPL(DBG_PCDV,"%s(%p)\n", __func__, _dev);
1587 }
1588
1589 int dwc_pcd_reset(dwc_otg_pcd_t *pcd)
1590 {
1591     dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
1592     dwc_otg_disable_global_interrupts( core_if );
1593 #ifdef CONFIG_ARCH_RK29
1594     cru_set_soft_reset(SOFT_RST_USB_OTG_2_0_AHB_BUS, true);
1595     cru_set_soft_reset(SOFT_RST_USB_OTG_2_0_PHY, true);
1596     cru_set_soft_reset(SOFT_RST_USB_OTG_2_0_CONTROLLER, true);
1597     udelay(1);
1598
1599     cru_set_soft_reset(SOFT_RST_USB_OTG_2_0_AHB_BUS, false);
1600     cru_set_soft_reset(SOFT_RST_USB_OTG_2_0_PHY, false);
1601     cru_set_soft_reset(SOFT_RST_USB_OTG_2_0_CONTROLLER, false);
1602     mdelay(1);
1603 #endif    
1604     //rockchip_scu_reset_unit(12);
1605     dwc_otg_pcd_reinit( pcd );
1606     dwc_otg_core_dev_init(core_if);
1607     //DWC_PRINT("%s\n" , __func__ );
1608     dwc_otg_enable_global_interrupts( core_if );
1609     return 0;
1610 }
1611
1612
1613
1614 int dwc_otg_reset( void ) 
1615 {
1616     dwc_otg_pcd_t * pcd = s_pcd;
1617     dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
1618     dctl_data_t dctl = {.d32=0};
1619
1620     dctl.d32 = dwc_read_reg32( &core_if->dev_if->dev_global_regs->dctl );
1621     dctl.b.sftdiscon = 1;
1622     dwc_write_reg32( &core_if->dev_if->dev_global_regs->dctl, dctl.d32 );
1623     //DWC_PRINT("%s::otg reset connect!!!\n" , __func__ );
1624     return 0;
1625 }
1626 void dwc_otg_msc_lock(dwc_otg_pcd_t *pcd)
1627 {
1628         unsigned long           flags;
1629
1630         local_irq_save(flags);
1631     wake_lock(&pcd->wake_lock);
1632     local_irq_restore(flags);
1633
1634 }
1635
1636 void dwc_otg_msc_unlock(dwc_otg_pcd_t *pcd)
1637 {
1638         unsigned long           flags;
1639         local_irq_save(flags);
1640         wake_unlock(&pcd->wake_lock);
1641         local_irq_restore(flags);
1642 }
1643 static void dwc_phy_reconnect(struct work_struct *work)
1644 {
1645     dwc_otg_pcd_t *pcd;
1646     dwc_otg_core_if_t *core_if;
1647     gotgctl_data_t    gctrl;
1648     dctl_data_t dctl = {.d32=0};
1649
1650     pcd = container_of(work, dwc_otg_pcd_t, reconnect.work);
1651     core_if = GET_CORE_IF(pcd); 
1652     gctrl.d32 = dwc_read_reg32( &core_if->core_global_regs->gotgctl );
1653     if( gctrl.b.bsesvld  ) {
1654         pcd->conn_status++;
1655             dwc_pcd_reset(pcd);
1656         /*
1657          * Enable the global interrupt after all the interrupt
1658          * handlers are installed.
1659          */
1660         dctl.d32 = dwc_read_reg32( &core_if->dev_if->dev_global_regs->dctl );
1661         dctl.b.sftdiscon = 0;
1662         dwc_write_reg32( &core_if->dev_if->dev_global_regs->dctl, dctl.d32 );      
1663         DWC_PRINT("********soft connect!!!*****************************************\n");
1664     } 
1665 }
1666 #if defined(CONFIG_ARCH_RK29)
1667 static void dwc_otg_pcd_check_vbus_timer( unsigned long pdata )
1668 {
1669     struct device *_dev = (struct device *)pdata;
1670         struct dwc_otg_platform_data *pldata = _dev->platform_data;
1671         dwc_otg_device_t *otg_dev = (dwc_otg_device_t *)(*((uint32_t *)_dev->platform_data));
1672     dwc_otg_pcd_t * _pcd = otg_dev->pcd;
1673     dwc_otg_core_if_t *core_if = GET_CORE_IF(_pcd);
1674     gotgctl_data_t    gctrl;
1675     dctl_data_t dctl = {.d32=0};
1676     //dsts_data_t           gsts;
1677         unsigned long flags;
1678         local_irq_save(flags);
1679     gctrl.d32 = dwc_read_reg32( &core_if->core_global_regs->gotgctl );
1680     //gsts.d32 = dwc_read_reg32( &core_if->dev_if->dev_global_regs->dsts);
1681
1682     _pcd->check_vbus_timer.expires = jiffies + (HZ); /* 1 s */
1683     if( gctrl.b.bsesvld ) {
1684         /* if usb not connect before ,then start connect */
1685          if( _pcd->vbus_status == 0 ) {
1686             dwc_otg_msc_lock(_pcd);
1687             DWC_PRINT("********vbus detect*********************************************\n");
1688             _pcd->vbus_status = 1; 
1689             /* soft disconnect */
1690             dctl.d32 = dwc_read_reg32( &core_if->dev_if->dev_global_regs->dctl );
1691             dctl.b.sftdiscon = 1;
1692             dwc_write_reg32( &core_if->dev_if->dev_global_regs->dctl, dctl.d32 );
1693             if(_pcd->conn_en)
1694             {
1695                     schedule_delayed_work( &_pcd->reconnect , 8 ); /* delay 1 jiffies */
1696                      _pcd->check_vbus_timer.expires = jiffies + (HZ<<1); /* 1 s */
1697             }
1698
1699         } else if((_pcd->conn_status>0)&&(_pcd->conn_status <3)) {
1700             //dwc_otg_msc_unlock(_pcd);
1701             DWC_PRINT("********soft reconnect******************************************\n");
1702             //_pcd->vbus_status =0;
1703
1704             /* soft disconnect */
1705                 dctl.d32 = dwc_read_reg32( &core_if->dev_if->dev_global_regs->dctl );
1706                 dctl.b.sftdiscon = 1;
1707                 dwc_write_reg32( &core_if->dev_if->dev_global_regs->dctl, dctl.d32 );
1708             /* Clear any pending interrupts */
1709             dwc_write_reg32( &core_if->core_global_regs->gintsts, 0xFFFFFFFF);
1710             if(_pcd->conn_en)
1711             {
1712                     schedule_delayed_work( &_pcd->reconnect , 8 ); /* delay 1 jiffies */
1713                      _pcd->check_vbus_timer.expires = jiffies + (HZ<<1); /* 1 s */
1714             }
1715         }
1716         else if((_pcd->conn_en)&&(_pcd->conn_status == 0))
1717         {
1718
1719             schedule_delayed_work( &_pcd->reconnect , 8 ); /* delay 1 jiffies */
1720                      _pcd->check_vbus_timer.expires = jiffies + (HZ<<1); /* 1 s */
1721         }
1722         else if(_pcd->conn_status ==3)
1723         {
1724                         //*????????,??????????,yk@rk,20100331*//
1725             dwc_otg_msc_unlock(_pcd);
1726             _pcd->conn_status++;
1727                 _pcd->vbus_status = 2;
1728         }
1729     } else {
1730         //DWC_PRINT("new vbus=%d,old vbus=%d\n" , gctrl.b.bsesvld , _pcd->vbus_status );
1731         _pcd->vbus_status = 0;
1732         if(_pcd->conn_status)
1733         {
1734              _pcd->conn_status = 0;
1735              dwc_otg_msc_unlock(_pcd);
1736         }
1737         /* every 500 ms open usb phy power and start 1 jiffies timer to get vbus */
1738         if( pldata->phy_status == 0 ) {
1739                 /* no vbus detect here , close usb phy for 500ms */
1740             pldata->phy_suspend(pldata, USB_PHY_SUSPEND);
1741             //dwc_otg20phy_suspend( 0 );
1742               _pcd->check_vbus_timer.expires = jiffies + (HZ/2); /* 500 ms */
1743         } else if( pldata->phy_status  == 1 ) {
1744             pldata->phy_suspend(pldata, USB_PHY_ENABLED);
1745            // dwc_otg20phy_suspend( 1 );
1746             /*20100325 yk@rk,delay 2-->8,for host connect id detect*/
1747             _pcd->check_vbus_timer.expires = jiffies + 8; /* 20091127,HSL@RK,1-->2  */
1748
1749         }
1750     }
1751     //DWC_PRINT("%s:restart check vbus timer\n" , __func__ );
1752     add_timer(&_pcd->check_vbus_timer);
1753         local_irq_restore(flags);
1754 }
1755 #else
1756
1757 static void dwc_otg_pcd_check_vbus_timer( unsigned long data )
1758 {
1759     struct device *_dev = (struct device *)data;
1760         struct dwc_otg_platform_data *pldata = _dev->platform_data;
1761         dwc_otg_device_t *otg_dev = (dwc_otg_device_t *)(*((uint32_t *)_dev->platform_data));
1762     dwc_otg_pcd_t * _pcd = otg_dev->pcd;
1763     unsigned long flags;
1764     local_irq_save(flags);
1765     _pcd->check_vbus_timer.expires = jiffies + (HZ); /* 1 s */
1766     if(!pldata->get_status(USB_STATUS_ID))
1767     {  // id low
1768         if( pldata->dwc_otg_uart_mode != NULL )
1769         {//exit phy bypass to uart & enable usb phy
1770             pldata->dwc_otg_uart_mode( pldata, PHY_USB_MODE);
1771         }
1772         if( pldata->phy_status)
1773         { 
1774             pldata->clock_enable( pldata, 1);           
1775             pldata->phy_suspend(pldata, USB_PHY_ENABLED);
1776         } 
1777     }
1778         else if(pldata->get_status(USB_STATUS_BVABLID))
1779         {  // bvalid
1780         /* if usb not connect before ,then start connect */
1781          if( _pcd->vbus_status == 0 ) 
1782          {
1783             DWC_PRINT("********vbus detect*********************************************\n");
1784             _pcd->vbus_status = 1;
1785             if(_pcd->conn_en)
1786                 goto connect;
1787             else if( pldata->phy_status == USB_PHY_ENABLED )
1788             {
1789                 // not connect, suspend phy
1790                 pldata->phy_suspend(pldata, USB_PHY_SUSPEND);
1791                 udelay(3);
1792                 pldata->clock_enable( pldata, 0);
1793             }
1794         } 
1795         else if((_pcd->conn_en)&&(_pcd->conn_status>=0)&&(_pcd->conn_status <3))
1796         {
1797             DWC_PRINT("********soft reconnect******************************************\n");
1798             goto connect;
1799         }
1800         else if(_pcd->conn_status ==3)
1801         {
1802                         //*Á¬½Ó²»ÉÏʱÊÍ·ÅËø£¬ÔÊÐíϵͳ½øÈë¶þ¼¶Ë¯Ãߣ¬yk@rk,20100331*//
1803             dwc_otg_msc_unlock(_pcd);
1804             _pcd->conn_status++;
1805             if((dwc_read_reg32((uint32_t*)((uint8_t *)_pcd->otg_dev->base + DWC_OTG_HOST_PORT_REGS_OFFSET))&0xc00) == 0xc00)
1806                 _pcd->vbus_status = 2;
1807                 
1808             // not connect, suspend phy
1809             if( pldata->phy_status == USB_PHY_ENABLED )
1810             {
1811                 pldata->phy_suspend(pldata, USB_PHY_SUSPEND);
1812                 udelay(3);
1813                 pldata->clock_enable( pldata, 0);
1814             }
1815         }
1816         }
1817     else 
1818     {
1819         _pcd->vbus_status = 0;
1820         if(_pcd->conn_status)
1821         {
1822              _pcd->conn_status = 0;
1823         }
1824         else if( pldata->phy_status == USB_PHY_ENABLED )
1825         { 
1826             /* no vbus detect here , close usb phy  */
1827             pldata->phy_suspend(pldata, USB_PHY_SUSPEND);
1828             udelay(3);
1829             pldata->clock_enable( pldata, 0);
1830             /* usb phy bypass to uart mode  */
1831             if( pldata->dwc_otg_uart_mode != NULL )
1832                 pldata->dwc_otg_uart_mode( pldata, PHY_UART_MODE);    
1833             /* release wake lock */
1834             dwc_otg_msc_unlock(_pcd);
1835         }  
1836     }
1837     add_timer(&_pcd->check_vbus_timer); 
1838         local_irq_restore(flags);
1839     return;
1840
1841 connect:
1842     if(_pcd->conn_status==0)
1843         dwc_otg_msc_lock(_pcd);
1844     if( pldata->phy_status)
1845     {
1846         pldata->clock_enable( pldata, 1);       
1847         pldata->phy_suspend(pldata, USB_PHY_ENABLED);
1848     }
1849     schedule_delayed_work( &_pcd->reconnect , 8 ); /* delay 1 jiffies */
1850     _pcd->check_vbus_timer.expires = jiffies + (HZ<<1); /* 1 s */
1851     add_timer(&_pcd->check_vbus_timer); 
1852         local_irq_restore(flags);
1853     return;
1854 }
1855 #endif
1856 void dwc_otg_pcd_start_vbus_timer( dwc_otg_pcd_t * _pcd )
1857 {
1858         struct timer_list *vbus_timer = &_pcd->check_vbus_timer;
1859
1860         /* 
1861          * when receive reset int,the vbus state may not be update,so 
1862          * always start timer here.
1863          */                
1864         mod_timer( vbus_timer , jiffies + (HZ));
1865 }
1866
1867 /*
1868 * 20091228,HSL@RK,to get the current vbus status.
1869 */
1870 int dwc_vbus_status( void )
1871 {
1872     dwc_otg_pcd_t *pcd = s_pcd;
1873     if(!pcd)
1874        return 0;
1875     else
1876         return pcd->vbus_status ;
1877 }
1878 EXPORT_SYMBOL(dwc_vbus_status);
1879
1880 int dwc_otg_set_phy_status(uint8_t status)
1881 {
1882     dwc_otg_pcd_t *pcd = s_pcd;
1883     pcd->phy_suspend = status;
1884     return pcd->phy_suspend;
1885 }
1886 /** 
1887  * This function initialized the PCD portion of the driver.
1888  *
1889  */
1890 int dwc_otg_pcd_init(struct device *dev)
1891 {
1892         static char pcd_name[] = "dwc_otg_pcd";
1893         dwc_otg_pcd_t *pcd;
1894         dwc_otg_device_t *otg_dev = (dwc_otg_device_t *)(*((uint32_t *)dev->platform_data));
1895     dwc_otg_core_if_t *core_if = otg_dev->core_if; 
1896         struct dwc_otg_platform_data *pldata = dev->platform_data;
1897         int retval = 0;
1898         int irq;
1899          /*
1900          * Allocate PCD structure
1901          */
1902         pcd = kmalloc( sizeof(dwc_otg_pcd_t), GFP_KERNEL);
1903         
1904         if (pcd == 0) 
1905         {
1906                         return -ENOMEM;
1907         }
1908         
1909         memset( pcd, 0, sizeof(dwc_otg_pcd_t));
1910         spin_lock_init( &pcd->lock );
1911         otg_dev->pcd = pcd;
1912         s_pcd = pcd;
1913         pcd->gadget.name = pcd_name;
1914         //strcpy(pcd->gadget.dev.bus_id, "gadget");
1915         
1916         pcd->otg_dev = otg_dev;
1917         
1918         pcd->gadget.dev.parent = dev;
1919         pcd->gadget.dev.release = dwc_otg_pcd_gadget_release;
1920         pcd->gadget.dev.init_name= "gadget";
1921         pcd->gadget.ops = &dwc_otg_pcd_ops;
1922         
1923         pcd->gadget.is_dualspeed = 0;
1924         pcd->gadget.is_otg = 0;
1925         pcd->driver = 0;
1926     pcd->conn_en = 0;
1927         /* Register the gadget device */
1928         retval = device_register( &pcd->gadget.dev );
1929         if(retval != 0)
1930         {
1931                 DWC_ERROR("device_register failed\n");
1932                 return -EBUSY;
1933         }
1934         
1935         /*
1936          * Register the PCD Callbacks. 
1937          */
1938         dwc_otg_cil_register_pcd_callbacks( otg_dev->core_if, &pcd_callbacks, 
1939                                                                                 pcd );
1940         /*
1941          * Setup interupt handler
1942          */
1943         irq = platform_get_irq(to_platform_device(dev),0);
1944         DWC_DEBUGPL( DBG_ANY, "registering handler for irq%d\n", irq);
1945         retval = request_irq(irq, dwc_otg_pcd_irq,
1946                                                  IRQF_SHARED, pcd->gadget.name, pcd);
1947         if (retval != 0) 
1948         {
1949                 DWC_ERROR("request of irq%d failed\n", irq);
1950                 kfree (pcd);
1951                 return -EBUSY;
1952         }
1953     
1954         wake_lock_init(&pcd->wake_lock, WAKE_LOCK_SUSPEND,
1955                            "usb_pcd");
1956
1957         /*
1958          * Initialize EP structures
1959          */
1960         dwc_otg_pcd_reinit( pcd );
1961         /* 
1962          * Initialize the DMA buffer for SETUP packets
1963          */
1964         if (GET_CORE_IF(pcd)->dma_enable) 
1965         {
1966                 pcd->setup_pkt = dma_alloc_coherent (NULL, sizeof (*pcd->setup_pkt) * 5, &pcd->setup_pkt_dma_handle, 0);
1967                 pcd->status_buf = dma_alloc_coherent (NULL, sizeof (uint16_t), &pcd->status_buf_dma_handle, 0);
1968         }
1969         else 
1970         {
1971                 pcd->setup_pkt = kmalloc (sizeof (*pcd->setup_pkt) * 5, GFP_KERNEL);
1972                 pcd->status_buf = kmalloc (sizeof (uint16_t), GFP_KERNEL);
1973         }
1974         
1975         if (pcd->setup_pkt == 0) 
1976         {
1977                 DWC_PRINT("pcd->setup_pkt alloc fail,everest\n");
1978                 kfree (pcd);
1979                 return -ENOMEM;
1980         }
1981         /* Initialize tasklet */
1982         start_xfer_tasklet.data = (unsigned long)pcd;
1983         pcd->start_xfer_tasklet = &start_xfer_tasklet;
1984
1985
1986
1987     init_timer( &pcd->check_vbus_timer );
1988     pcd->check_vbus_timer.function = dwc_otg_pcd_check_vbus_timer;
1989     pcd->check_vbus_timer.data = (unsigned long)(dev);
1990     
1991     INIT_DELAYED_WORK(&pcd->reconnect , dwc_phy_reconnect);
1992     pcd->vbus_status  = 0;
1993     pcd->phy_suspend  = 0; 
1994     if(dwc_otg_is_device_mode(core_if)){
1995 #ifdef CONFIG_RK_USB_UART        
1996         if(pldata->get_status(USB_STATUS_BVABLID))
1997         {
1998              pldata->dwc_otg_uart_mode(pldata, PHY_USB_MODE);
1999         }//phy usb mode
2000         else
2001         {
2002             pldata->phy_suspend(pldata,USB_PHY_SUSPEND);
2003             pldata->dwc_otg_uart_mode(pldata, PHY_UART_MODE);
2004         }//phy bypass to uart mode
2005 #endif
2006         mod_timer(&pcd->check_vbus_timer, jiffies+(HZ<<4)); // delay 16 S 
2007     }
2008 #ifdef CONFIG_RK_USB_UART
2009     else if(pldata->dwc_otg_uart_mode != NULL)
2010         pldata->dwc_otg_uart_mode(pldata, PHY_USB_MODE);//disable phy bypass uart  
2011 #endif
2012
2013         return 0;
2014 }
2015 /**
2016  * Cleanup the PCD.
2017  */
2018 void dwc_otg_pcd_remove( struct device *dev )
2019 {
2020         dwc_otg_device_t *otg_dev = (dwc_otg_device_t *)(*((uint32_t *)dev->platform_data));
2021         dwc_otg_pcd_t *pcd = otg_dev->pcd;
2022         
2023         DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, dev);
2024
2025         wake_lock_destroy(&pcd->wake_lock);
2026
2027         /*
2028          * Free the IRQ 
2029          */
2030         free_irq( platform_get_irq(to_platform_device(dev),0), pcd );
2031         
2032          /* start with the driver above us */
2033         if (pcd->driver) 
2034         {
2035                 /* should have been done already by driver model core */
2036                 DWC_WARN("driver '%s' is still registered\n",
2037                                          pcd->driver->driver.name);
2038                 usb_gadget_unregister_driver( pcd->driver);
2039         }
2040         device_unregister(&pcd->gadget.dev);
2041                 
2042         if (GET_CORE_IF(pcd)->dma_enable) 
2043         {
2044                 dma_free_coherent (NULL, sizeof (*pcd->setup_pkt) * 5, pcd->setup_pkt, pcd->setup_pkt_dma_handle);
2045                 dma_free_coherent (NULL, sizeof (uint16_t), pcd->status_buf, pcd->status_buf_dma_handle);
2046         }
2047         else 
2048         {
2049                 kfree (pcd->setup_pkt);
2050                 kfree (pcd->status_buf);
2051         }
2052         
2053         kfree( pcd );
2054         otg_dev->pcd = 0;
2055     s_pcd = 0; 
2056 }
2057
2058 /**
2059  * This function registers a gadget driver with the PCD.
2060  *
2061  * When a driver is successfully registered, it will receive control
2062  * requests including set_configuration(), which enables non-control
2063  * requests.  then usb traffic follows until a disconnect is reported.
2064  * then a host may connect again, or the driver might get unbound.
2065  *
2066  * @param _driver The driver being registered
2067  */
2068  
2069 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37))
2070 int usb_gadget_probe_driver(struct usb_gadget_driver *_driver,
2071                 int (*bind)(struct usb_gadget *))
2072 #else
2073 int usb_gadget_register_driver(struct usb_gadget_driver *_driver)
2074 #endif
2075 {
2076         int retval;
2077
2078         DWC_DEBUGPL(DBG_PCD, "registering gadget driver '%s'\n", _driver->driver.name);
2079                 
2080         if (!_driver || _driver->speed == USB_SPEED_UNKNOWN || 
2081 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37))
2082                 !bind ||
2083 #else
2084                 !_driver->bind || 
2085 #endif
2086                 !_driver->unbind || 
2087                 !_driver->disconnect || 
2088                 !_driver->setup) 
2089         {
2090                 DWC_ERROR("EINVAL\n");  
2091                 return -EINVAL;
2092         }
2093         if (s_pcd == 0) 
2094         {
2095                 DWC_ERROR("ENODEV\n");  
2096                 return -ENODEV;
2097         }
2098         if (s_pcd->driver != 0) 
2099         {
2100                 DWC_ERROR("EBUSY (%p)\n", s_pcd->driver);   
2101                 return -EBUSY;
2102         }
2103         
2104         /* hook up the driver */
2105         s_pcd->driver = _driver;
2106         s_pcd->gadget.dev.driver = &_driver->driver;
2107
2108         DWC_DEBUGPL(DBG_PCD, "bind to driver %s\n", _driver->driver.name);
2109 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37))
2110         retval = bind(&s_pcd->gadget);
2111 #else
2112         retval = _driver->bind(&s_pcd->gadget);
2113 #endif
2114         if (retval) 
2115         {
2116                 DWC_ERROR("bind to driver %s --> error %d\n",
2117                                         _driver->driver.name, retval);
2118                 s_pcd->driver = 0;
2119                 s_pcd->gadget.dev.driver = 0;
2120                 return retval;
2121         }
2122         DWC_DEBUGPL(DBG_ANY, "registered gadget driver '%s'\n", 
2123                                         _driver->driver.name);
2124         return 0;
2125 }
2126 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37))
2127 EXPORT_SYMBOL(usb_gadget_probe_driver);
2128 #else
2129 EXPORT_SYMBOL(usb_gadget_register_driver);
2130 #endif
2131
2132 /**
2133  * This function unregisters a gadget driver
2134  *
2135  * @param _driver The driver being unregistered
2136  */
2137 int usb_gadget_unregister_driver(struct usb_gadget_driver *_driver)
2138 {
2139         DWC_DEBUGPL(DBG_PCDV,"%s(%p)\n", __func__, _driver);
2140
2141         if (s_pcd == 0) 
2142         {
2143                 DWC_DEBUGPL(DBG_ANY, "%s Return(%d): s_pcd==0\n", __func__, 
2144                                         -ENODEV);
2145                 return -ENODEV;
2146         }
2147         if (_driver == 0 || _driver != s_pcd->driver) 
2148         {
2149                 DWC_DEBUGPL( DBG_ANY, "%s Return(%d): driver?\n", __func__, 
2150                                         -EINVAL);
2151                 return -EINVAL;
2152         }
2153
2154         _driver->unbind(&s_pcd->gadget);
2155         s_pcd->driver = 0;
2156
2157         DWC_DEBUGPL(DBG_ANY, "unregistered driver '%s'\n", 
2158                                         _driver->driver.name);
2159         return 0;
2160 }
2161 EXPORT_SYMBOL(usb_gadget_unregister_driver);
2162 #endif /* DWC_HOST_ONLY */