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