50c7566369eb7756abe841acb51c418be45b5937
[pandora-kernel.git] / drivers / usb / renesas_usbhs / mod_gadget.c
1 /*
2  * Renesas USB driver
3  *
4  * Copyright (C) 2011 Renesas Solutions Corp.
5  * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
15  *
16  */
17 #include <linux/io.h>
18 #include <linux/module.h>
19 #include <linux/platform_device.h>
20 #include <linux/usb/ch9.h>
21 #include <linux/usb/gadget.h>
22 #include "common.h"
23
24 /*
25  *              struct
26  */
27 struct usbhsg_request {
28         struct usb_request      req;
29         struct usbhs_pkt        pkt;
30 };
31
32 #define EP_NAME_SIZE 8
33 struct usbhsg_gpriv;
34 struct usbhsg_uep {
35         struct usb_ep            ep;
36         struct usbhs_pipe       *pipe;
37
38         char ep_name[EP_NAME_SIZE];
39
40         struct usbhsg_gpriv *gpriv;
41         struct usbhs_pkt_handle *handler;
42 };
43
44 struct usbhsg_gpriv {
45         struct usb_gadget        gadget;
46         struct usbhs_mod         mod;
47
48         struct usbhsg_uep       *uep;
49         int                      uep_size;
50
51         struct usb_gadget_driver        *driver;
52
53         u32     status;
54 #define USBHSG_STATUS_STARTED           (1 << 0)
55 #define USBHSG_STATUS_REGISTERD         (1 << 1)
56 #define USBHSG_STATUS_WEDGE             (1 << 2)
57 };
58
59 struct usbhsg_recip_handle {
60         char *name;
61         int (*device)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
62                       struct usb_ctrlrequest *ctrl);
63         int (*interface)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
64                          struct usb_ctrlrequest *ctrl);
65         int (*endpoint)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
66                         struct usb_ctrlrequest *ctrl);
67 };
68
69 /*
70  *              macro
71  */
72 #define usbhsg_priv_to_gpriv(priv)                      \
73         container_of(                                   \
74                 usbhs_mod_get(priv, USBHS_GADGET),      \
75                 struct usbhsg_gpriv, mod)
76
77 #define __usbhsg_for_each_uep(start, pos, g, i) \
78         for (i = start, pos = (g)->uep;         \
79              i < (g)->uep_size;                 \
80              i++, pos = (g)->uep + i)
81
82 #define usbhsg_for_each_uep(pos, gpriv, i)      \
83         __usbhsg_for_each_uep(1, pos, gpriv, i)
84
85 #define usbhsg_for_each_uep_with_dcp(pos, gpriv, i)     \
86         __usbhsg_for_each_uep(0, pos, gpriv, i)
87
88 #define usbhsg_gadget_to_gpriv(g)\
89         container_of(g, struct usbhsg_gpriv, gadget)
90
91 #define usbhsg_req_to_ureq(r)\
92         container_of(r, struct usbhsg_request, req)
93
94 #define usbhsg_ep_to_uep(e)             container_of(e, struct usbhsg_uep, ep)
95 #define usbhsg_gpriv_to_lock(gp)        usbhs_priv_to_lock((gp)->mod.priv)
96 #define usbhsg_gpriv_to_dev(gp)         usbhs_priv_to_dev((gp)->mod.priv)
97 #define usbhsg_gpriv_to_priv(gp)        ((gp)->mod.priv)
98 #define usbhsg_gpriv_to_dcp(gp)         ((gp)->uep)
99 #define usbhsg_gpriv_to_nth_uep(gp, i)  ((gp)->uep + i)
100 #define usbhsg_uep_to_gpriv(u)          ((u)->gpriv)
101 #define usbhsg_uep_to_pipe(u)           ((u)->pipe)
102 #define usbhsg_pipe_to_uep(p)           ((p)->mod_private)
103 #define usbhsg_is_dcp(u)                ((u) == usbhsg_gpriv_to_dcp((u)->gpriv))
104
105 #define usbhsg_ureq_to_pkt(u)           (&(u)->pkt)
106 #define usbhsg_pkt_to_ureq(i)   \
107         container_of(i, struct usbhsg_request, pkt)
108
109 #define usbhsg_is_not_connected(gp) ((gp)->gadget.speed == USB_SPEED_UNKNOWN)
110
111 /* status */
112 #define usbhsg_status_init(gp)   do {(gp)->status = 0; } while (0)
113 #define usbhsg_status_set(gp, b) (gp->status |=  b)
114 #define usbhsg_status_clr(gp, b) (gp->status &= ~b)
115 #define usbhsg_status_has(gp, b) (gp->status &   b)
116
117 /*
118  *              usbhsg_trylock
119  *
120  * This driver don't use spin_try_lock
121  * to avoid warning of CONFIG_DEBUG_SPINLOCK
122  */
123 static spinlock_t *usbhsg_trylock(struct usbhsg_gpriv *gpriv,
124                                   unsigned long *flags)
125 {
126         spinlock_t *lock = usbhsg_gpriv_to_lock(gpriv);
127
128         /* check spin lock status
129          * to avoid deadlock/nest */
130         if (spin_is_locked(lock))
131                 return NULL;
132
133         spin_lock_irqsave(lock, *flags);
134
135         return lock;
136 }
137
138 static void usbhsg_unlock(spinlock_t *lock, unsigned long *flags)
139 {
140         if (!lock)
141                 return;
142
143         spin_unlock_irqrestore(lock, *flags);
144 }
145
146 /*
147  *              list push/pop
148  */
149 static void usbhsg_queue_push(struct usbhsg_uep *uep,
150                               struct usbhsg_request *ureq)
151 {
152         struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
153         struct device *dev = usbhsg_gpriv_to_dev(gpriv);
154         struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
155         struct usbhs_pkt *pkt = usbhsg_ureq_to_pkt(ureq);
156         struct usb_request *req = &ureq->req;
157
158         /*
159          *********  assume under spin lock  *********
160          */
161         usbhs_pkt_push(pipe, pkt, uep->handler,
162                        req->buf, req->length, req->zero);
163         req->actual = 0;
164         req->status = -EINPROGRESS;
165
166         dev_dbg(dev, "pipe %d : queue push (%d)\n",
167                 usbhs_pipe_number(pipe),
168                 req->length);
169 }
170
171 static struct usbhsg_request *usbhsg_queue_get(struct usbhsg_uep *uep)
172 {
173         struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
174         struct usbhs_pkt *pkt = usbhs_pkt_get(pipe);
175
176         /*
177          *********  assume under spin lock  *********
178          */
179         if (!pkt)
180                 return 0;
181
182         return usbhsg_pkt_to_ureq(pkt);
183 }
184
185 static int usbhsg_queue_start(struct usbhsg_uep *uep)
186 {
187         struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
188         struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
189         struct usbhs_pkt *pkt;
190         spinlock_t *lock;
191         unsigned long flags;
192         int ret = 0;
193
194         /*
195          * CAUTION [*queue handler*]
196          *
197          * This function will be called for start/restart queue operation.
198          * OTOH the most much worry for USB driver is spinlock nest.
199          * Specially it are
200          *   - usb_ep_ops  :: queue
201          *   - usb_request :: complete
202          *
203          * But the caller of this function need not care about spinlock.
204          * This function is using usbhsg_trylock for it.
205          * if "is_locked" is 1, this mean this function lock it.
206          * but if it is 0, this mean it is already under spin lock.
207          * see also
208          *   CAUTION [*endpoint queue*]
209          *   CAUTION [*request complete*]
210          */
211
212         /******************  spin try lock *******************/
213         lock = usbhsg_trylock(gpriv, &flags);
214
215         pkt = usbhs_pkt_get(pipe);
216         if (pkt)
217                 ret = usbhs_pkt_start(pkt);
218
219         usbhsg_unlock(lock, &flags);
220         /********************  spin unlock ******************/
221
222         return ret;
223 }
224
225 static void usbhsg_queue_pop(struct usbhsg_uep *uep,
226                              struct usbhsg_request *ureq,
227                              int status)
228 {
229         struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
230         struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
231         struct device *dev = usbhsg_gpriv_to_dev(gpriv);
232         struct usbhs_pkt *pkt = usbhsg_ureq_to_pkt(ureq);
233
234         /*
235          *********  assume under spin lock  *********
236          */
237
238         /*
239          * CAUTION [*request complete*]
240          *
241          * There is a possibility not to be called in correct order
242          * if "complete" is called without spinlock.
243          *
244          * So, this function assume it is under spinlock,
245          * and call usb_request :: complete.
246          *
247          * But this "complete" will push next usb_request.
248          * It mean "usb_ep_ops :: queue" which is using spinlock is called
249          * under spinlock.
250          *
251          * To avoid dead-lock, this driver is using usbhsg_trylock.
252          *   CAUTION [*endpoint queue*]
253          *   CAUTION [*queue handler*]
254          */
255
256         dev_dbg(dev, "pipe %d : queue pop\n", usbhs_pipe_number(pipe));
257
258         usbhs_pkt_pop(pkt);
259
260         ureq->req.status = status;
261         ureq->req.complete(&uep->ep, &ureq->req);
262
263         /* more request ? */
264         if (0 == status)
265                 usbhsg_queue_start(uep);
266 }
267
268 static void usbhsg_queue_done(struct usbhs_pkt *pkt)
269 {
270         struct usbhs_pipe *pipe = pkt->pipe;
271         struct usbhsg_uep *uep = usbhsg_pipe_to_uep(pipe);
272         struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt);
273
274         ureq->req.actual = pkt->actual;
275
276         usbhsg_queue_pop(uep, ureq, 0);
277 }
278
279 /*
280  *              USB_TYPE_STANDARD / clear feature functions
281  */
282 static int usbhsg_recip_handler_std_control_done(struct usbhs_priv *priv,
283                                                  struct usbhsg_uep *uep,
284                                                  struct usb_ctrlrequest *ctrl)
285 {
286         struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
287         struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
288         struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
289
290         usbhs_dcp_control_transfer_done(pipe);
291
292         return 0;
293 }
294
295 static int usbhsg_recip_handler_std_clear_endpoint(struct usbhs_priv *priv,
296                                                    struct usbhsg_uep *uep,
297                                                    struct usb_ctrlrequest *ctrl)
298 {
299         struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
300         struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
301
302         if (!usbhsg_status_has(gpriv, USBHSG_STATUS_WEDGE)) {
303                 usbhs_pipe_disable(pipe);
304                 usbhs_pipe_clear_sequence(pipe);
305                 usbhs_pipe_enable(pipe);
306         }
307
308         usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
309
310         usbhsg_queue_start(uep);
311
312         return 0;
313 }
314
315 struct usbhsg_recip_handle req_clear_feature = {
316         .name           = "clear feature",
317         .device         = usbhsg_recip_handler_std_control_done,
318         .interface      = usbhsg_recip_handler_std_control_done,
319         .endpoint       = usbhsg_recip_handler_std_clear_endpoint,
320 };
321
322 /*
323  *              USB_TYPE handler
324  */
325 static int usbhsg_recip_run_handle(struct usbhs_priv *priv,
326                                    struct usbhsg_recip_handle *handler,
327                                    struct usb_ctrlrequest *ctrl)
328 {
329         struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
330         struct device *dev = usbhsg_gpriv_to_dev(gpriv);
331         struct usbhsg_uep *uep;
332         int recip = ctrl->bRequestType & USB_RECIP_MASK;
333         int nth = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK;
334         int ret;
335         int (*func)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
336                     struct usb_ctrlrequest *ctrl);
337         char *msg;
338
339         uep = usbhsg_gpriv_to_nth_uep(gpriv, nth);
340         if (!usbhsg_uep_to_pipe(uep)) {
341                 dev_err(dev, "wrong recip request\n");
342                 return -EINVAL;
343         }
344
345         switch (recip) {
346         case USB_RECIP_DEVICE:
347                 msg     = "DEVICE";
348                 func    = handler->device;
349                 break;
350         case USB_RECIP_INTERFACE:
351                 msg     = "INTERFACE";
352                 func    = handler->interface;
353                 break;
354         case USB_RECIP_ENDPOINT:
355                 msg     = "ENDPOINT";
356                 func    = handler->endpoint;
357                 break;
358         default:
359                 dev_warn(dev, "unsupported RECIP(%d)\n", recip);
360                 func = NULL;
361                 ret = -EINVAL;
362         }
363
364         if (func) {
365                 dev_dbg(dev, "%s (pipe %d :%s)\n", handler->name, nth, msg);
366                 ret = func(priv, uep, ctrl);
367         }
368
369         return ret;
370 }
371
372 /*
373  *              irq functions
374  *
375  * it will be called from usbhs_interrupt
376  */
377 static int usbhsg_irq_dev_state(struct usbhs_priv *priv,
378                                 struct usbhs_irq_state *irq_state)
379 {
380         struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
381         struct device *dev = usbhsg_gpriv_to_dev(gpriv);
382
383         gpriv->gadget.speed = usbhs_status_get_usb_speed(irq_state);
384
385         dev_dbg(dev, "state = %x : speed : %d\n",
386                 usbhs_status_get_device_state(irq_state),
387                 gpriv->gadget.speed);
388
389         return 0;
390 }
391
392 static int usbhsg_irq_ctrl_stage(struct usbhs_priv *priv,
393                                  struct usbhs_irq_state *irq_state)
394 {
395         struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
396         struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
397         struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
398         struct device *dev = usbhsg_gpriv_to_dev(gpriv);
399         struct usb_ctrlrequest ctrl;
400         struct usbhsg_recip_handle *recip_handler = NULL;
401         int stage = usbhs_status_get_ctrl_stage(irq_state);
402         int ret = 0;
403
404         dev_dbg(dev, "stage = %d\n", stage);
405
406         /*
407          * see Manual
408          *
409          *  "Operation"
410          *  - "Interrupt Function"
411          *    - "Control Transfer Stage Transition Interrupt"
412          *      - Fig. "Control Transfer Stage Transitions"
413          */
414
415         switch (stage) {
416         case READ_DATA_STAGE:
417                 dcp->handler = &usbhs_fifo_push_handler;
418                 break;
419         case WRITE_DATA_STAGE:
420                 dcp->handler = &usbhs_fifo_pop_handler;
421                 break;
422         case NODATA_STATUS_STAGE:
423                 dcp->handler = &usbhs_ctrl_stage_end_handler;
424                 break;
425         default:
426                 return ret;
427         }
428
429         /*
430          * get usb request
431          */
432         usbhs_usbreq_get_val(priv, &ctrl);
433
434         switch (ctrl.bRequestType & USB_TYPE_MASK) {
435         case USB_TYPE_STANDARD:
436                 switch (ctrl.bRequest) {
437                 case USB_REQ_CLEAR_FEATURE:
438                         recip_handler = &req_clear_feature;
439                         break;
440                 }
441         }
442
443         /*
444          * setup stage / run recip
445          */
446         if (recip_handler)
447                 ret = usbhsg_recip_run_handle(priv, recip_handler, &ctrl);
448         else
449                 ret = gpriv->driver->setup(&gpriv->gadget, &ctrl);
450
451         if (ret < 0)
452                 usbhs_pipe_stall(pipe);
453
454         return ret;
455 }
456
457 /*
458  *
459  *              usb_dcp_ops
460  *
461  */
462 static int usbhsg_dcp_enable(struct usbhsg_uep *uep)
463 {
464         struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
465         struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
466         struct usbhs_pipe *pipe;
467
468         /*
469          *********  assume under spin lock  *********
470          */
471
472         pipe = usbhs_dcp_malloc(priv);
473         if (!pipe)
474                 return -EIO;
475
476         uep->pipe               = pipe;
477         uep->pipe->mod_private  = uep;
478
479         return 0;
480 }
481
482 #define usbhsg_dcp_disable usbhsg_pipe_disable
483 static int usbhsg_pipe_disable(struct usbhsg_uep *uep)
484 {
485         struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
486         struct usbhsg_request *ureq;
487
488         /*
489          *********  assume under spin lock  *********
490          */
491
492         usbhs_pipe_disable(pipe);
493
494         while (1) {
495                 ureq = usbhsg_queue_get(uep);
496                 if (!ureq)
497                         break;
498
499                 usbhsg_queue_pop(uep, ureq, -ECONNRESET);
500         }
501
502         return 0;
503 }
504
505 static void usbhsg_uep_init(struct usbhsg_gpriv *gpriv)
506 {
507         int i;
508         struct usbhsg_uep *uep;
509
510         usbhsg_for_each_uep_with_dcp(uep, gpriv, i)
511                 uep->pipe = NULL;
512 }
513
514 /*
515  *
516  *              usb_ep_ops
517  *
518  */
519 static int usbhsg_ep_enable(struct usb_ep *ep,
520                          const struct usb_endpoint_descriptor *desc)
521 {
522         struct usbhsg_uep *uep   = usbhsg_ep_to_uep(ep);
523         struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
524         struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
525         struct usbhs_pipe *pipe;
526         spinlock_t *lock;
527         unsigned long flags;
528         int ret = -EIO;
529
530         /*
531          * if it already have pipe,
532          * nothing to do
533          */
534         if (uep->pipe)
535                 return 0;
536
537         /********************  spin lock ********************/
538         lock = usbhsg_trylock(gpriv, &flags);
539
540         pipe = usbhs_pipe_malloc(priv, desc);
541         if (pipe) {
542                 uep->pipe               = pipe;
543                 pipe->mod_private       = uep;
544
545                 if (usb_endpoint_dir_in(desc))
546                         uep->handler = &usbhs_fifo_push_handler;
547                 else
548                         uep->handler = &usbhs_fifo_pop_handler;
549
550                 ret = 0;
551         }
552
553         usbhsg_unlock(lock, &flags);
554         /********************  spin unlock ******************/
555
556         return ret;
557 }
558
559 static int usbhsg_ep_disable(struct usb_ep *ep)
560 {
561         struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
562         struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
563         spinlock_t *lock;
564         unsigned long flags;
565         int ret;
566
567         /********************  spin lock ********************/
568         lock = usbhsg_trylock(gpriv, &flags);
569
570         ret = usbhsg_pipe_disable(uep);
571
572         usbhsg_unlock(lock, &flags);
573         /********************  spin unlock ******************/
574
575         return ret;
576 }
577
578 static struct usb_request *usbhsg_ep_alloc_request(struct usb_ep *ep,
579                                                    gfp_t gfp_flags)
580 {
581         struct usbhsg_request *ureq;
582
583         ureq = kzalloc(sizeof *ureq, gfp_flags);
584         if (!ureq)
585                 return NULL;
586
587         usbhs_pkt_init(usbhsg_ureq_to_pkt(ureq));
588
589         return &ureq->req;
590 }
591
592 static void usbhsg_ep_free_request(struct usb_ep *ep,
593                                    struct usb_request *req)
594 {
595         struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
596
597         WARN_ON(!list_empty(&ureq->pkt.node));
598         kfree(ureq);
599 }
600
601 static int usbhsg_ep_queue(struct usb_ep *ep, struct usb_request *req,
602                           gfp_t gfp_flags)
603 {
604         struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
605         struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
606         struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
607         struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
608         spinlock_t *lock;
609         unsigned long flags;
610         int ret = 0;
611
612         /*
613          * CAUTION [*endpoint queue*]
614          *
615          * This function will be called from usb_request :: complete
616          * or usb driver timing.
617          * If this function is called from usb_request :: complete,
618          * it is already under spinlock on this driver.
619          * but it is called frm usb driver, this function should call spinlock.
620          *
621          * This function is using usbshg_trylock to solve this issue.
622          * if "is_locked" is 1, this mean this function lock it.
623          * but if it is 0, this mean it is already under spin lock.
624          * see also
625          *   CAUTION [*queue handler*]
626          *   CAUTION [*request complete*]
627          */
628
629         /********************  spin lock ********************/
630         lock = usbhsg_trylock(gpriv, &flags);
631
632         /* param check */
633         if (usbhsg_is_not_connected(gpriv)      ||
634             unlikely(!gpriv->driver)            ||
635             unlikely(!pipe))
636                 ret = -ESHUTDOWN;
637         else
638                 usbhsg_queue_push(uep, ureq);
639
640         usbhsg_unlock(lock, &flags);
641         /********************  spin unlock ******************/
642
643         usbhsg_queue_start(uep);
644
645         return ret;
646 }
647
648 static int usbhsg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
649 {
650         struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
651         struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
652         struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
653         spinlock_t *lock;
654         unsigned long flags;
655
656         /*
657          * see
658          *   CAUTION [*queue handler*]
659          *   CAUTION [*endpoint queue*]
660          *   CAUTION [*request complete*]
661          */
662
663         /********************  spin lock ********************/
664         lock = usbhsg_trylock(gpriv, &flags);
665
666         usbhsg_queue_pop(uep, ureq, -ECONNRESET);
667
668         usbhsg_unlock(lock, &flags);
669         /********************  spin unlock ******************/
670
671         return 0;
672 }
673
674 static int __usbhsg_ep_set_halt_wedge(struct usb_ep *ep, int halt, int wedge)
675 {
676         struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
677         struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
678         struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
679         struct device *dev = usbhsg_gpriv_to_dev(gpriv);
680         spinlock_t *lock;
681         unsigned long flags;
682         int ret = -EAGAIN;
683
684         /*
685          * see
686          *   CAUTION [*queue handler*]
687          *   CAUTION [*endpoint queue*]
688          *   CAUTION [*request complete*]
689          */
690
691         /********************  spin lock ********************/
692         lock = usbhsg_trylock(gpriv, &flags);
693         if (!usbhsg_queue_get(uep)) {
694
695                 dev_dbg(dev, "set halt %d (pipe %d)\n",
696                         halt, usbhs_pipe_number(pipe));
697
698                 if (halt)
699                         usbhs_pipe_stall(pipe);
700                 else
701                         usbhs_pipe_disable(pipe);
702
703                 if (halt && wedge)
704                         usbhsg_status_set(gpriv, USBHSG_STATUS_WEDGE);
705                 else
706                         usbhsg_status_clr(gpriv, USBHSG_STATUS_WEDGE);
707
708                 ret = 0;
709         }
710
711         usbhsg_unlock(lock, &flags);
712         /********************  spin unlock ******************/
713
714         return ret;
715 }
716
717 static int usbhsg_ep_set_halt(struct usb_ep *ep, int value)
718 {
719         return __usbhsg_ep_set_halt_wedge(ep, value, 0);
720 }
721
722 static int usbhsg_ep_set_wedge(struct usb_ep *ep)
723 {
724         return __usbhsg_ep_set_halt_wedge(ep, 1, 1);
725 }
726
727 static struct usb_ep_ops usbhsg_ep_ops = {
728         .enable         = usbhsg_ep_enable,
729         .disable        = usbhsg_ep_disable,
730
731         .alloc_request  = usbhsg_ep_alloc_request,
732         .free_request   = usbhsg_ep_free_request,
733
734         .queue          = usbhsg_ep_queue,
735         .dequeue        = usbhsg_ep_dequeue,
736
737         .set_halt       = usbhsg_ep_set_halt,
738         .set_wedge      = usbhsg_ep_set_wedge,
739 };
740
741 /*
742  *              usb module start/end
743  */
744 static int usbhsg_try_start(struct usbhs_priv *priv, u32 status)
745 {
746         struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
747         struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
748         struct usbhs_mod *mod = usbhs_mod_get_current(priv);
749         struct device *dev = usbhs_priv_to_dev(priv);
750         spinlock_t *lock;
751         unsigned long flags;
752
753         /********************  spin lock ********************/
754         lock = usbhsg_trylock(gpriv, &flags);
755
756         /*
757          * enable interrupt and systems if ready
758          */
759         usbhsg_status_set(gpriv, status);
760         if (!(usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
761               usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD)))
762                 goto usbhsg_try_start_unlock;
763
764         dev_dbg(dev, "start gadget\n");
765
766         /*
767          * pipe initialize and enable DCP
768          */
769         usbhs_pipe_init(priv,
770                         usbhsg_queue_done);
771         usbhs_fifo_init(priv);
772         usbhsg_uep_init(gpriv);
773         usbhsg_dcp_enable(dcp);
774
775         /*
776          * system config enble
777          * - HI speed
778          * - function
779          * - usb module
780          */
781         usbhs_sys_hispeed_ctrl(priv, 1);
782         usbhs_sys_function_ctrl(priv, 1);
783         usbhs_sys_usb_ctrl(priv, 1);
784
785         /*
786          * enable irq callback
787          */
788         mod->irq_dev_state      = usbhsg_irq_dev_state;
789         mod->irq_ctrl_stage     = usbhsg_irq_ctrl_stage;
790         usbhs_irq_callback_update(priv, mod);
791
792 usbhsg_try_start_unlock:
793         usbhsg_unlock(lock, &flags);
794         /********************  spin unlock ********************/
795
796         return 0;
797 }
798
799 static int usbhsg_try_stop(struct usbhs_priv *priv, u32 status)
800 {
801         struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
802         struct usbhs_mod *mod = usbhs_mod_get_current(priv);
803         struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
804         struct device *dev = usbhs_priv_to_dev(priv);
805         spinlock_t *lock;
806         unsigned long flags;
807
808         /********************  spin lock ********************/
809         lock = usbhsg_trylock(gpriv, &flags);
810
811         /*
812          * disable interrupt and systems if 1st try
813          */
814         usbhsg_status_clr(gpriv, status);
815         if (!usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
816             !usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD))
817                 goto usbhsg_try_stop_unlock;
818
819         usbhs_fifo_quit(priv);
820
821         /* disable all irq */
822         mod->irq_dev_state      = NULL;
823         mod->irq_ctrl_stage     = NULL;
824         usbhs_irq_callback_update(priv, mod);
825
826         usbhsg_dcp_disable(dcp);
827
828         gpriv->gadget.speed = USB_SPEED_UNKNOWN;
829
830         /* disable sys */
831         usbhs_sys_hispeed_ctrl(priv, 0);
832         usbhs_sys_function_ctrl(priv, 0);
833         usbhs_sys_usb_ctrl(priv, 0);
834
835         usbhsg_unlock(lock, &flags);
836         /********************  spin unlock ********************/
837
838         if (gpriv->driver &&
839             gpriv->driver->disconnect)
840                 gpriv->driver->disconnect(&gpriv->gadget);
841
842         dev_dbg(dev, "stop gadget\n");
843
844         return 0;
845
846 usbhsg_try_stop_unlock:
847         usbhsg_unlock(lock, &flags);
848
849         return 0;
850 }
851
852 /*
853  *
854  *              linux usb function
855  *
856  */
857 struct usbhsg_gpriv *the_controller;
858 int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
859                             int (*bind)(struct usb_gadget *))
860 {
861         struct usbhsg_gpriv *gpriv = the_controller;
862         struct usbhs_priv *priv;
863         struct device *dev;
864         int ret;
865
866         if (!bind               ||
867             !driver             ||
868             !driver->setup      ||
869             driver->speed != USB_SPEED_HIGH)
870                 return -EINVAL;
871         if (!gpriv)
872                 return -ENODEV;
873         if (gpriv->driver)
874                 return -EBUSY;
875
876         dev  = usbhsg_gpriv_to_dev(gpriv);
877         priv = usbhsg_gpriv_to_priv(gpriv);
878
879         /* first hook up the driver ... */
880         gpriv->driver = driver;
881         gpriv->gadget.dev.driver = &driver->driver;
882
883         ret = device_add(&gpriv->gadget.dev);
884         if (ret) {
885                 dev_err(dev, "device_add error %d\n", ret);
886                 goto add_fail;
887         }
888
889         ret = bind(&gpriv->gadget);
890         if (ret) {
891                 dev_err(dev, "bind to driver %s error %d\n",
892                         driver->driver.name, ret);
893                 goto bind_fail;
894         }
895
896         dev_dbg(dev, "bind %s\n", driver->driver.name);
897
898         return usbhsg_try_start(priv, USBHSG_STATUS_REGISTERD);
899
900 bind_fail:
901         device_del(&gpriv->gadget.dev);
902 add_fail:
903         gpriv->driver = NULL;
904         gpriv->gadget.dev.driver = NULL;
905
906         return ret;
907 }
908 EXPORT_SYMBOL(usb_gadget_probe_driver);
909
910 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
911 {
912         struct usbhsg_gpriv *gpriv = the_controller;
913         struct usbhs_priv *priv;
914         struct device *dev = usbhsg_gpriv_to_dev(gpriv);
915
916         if (!gpriv)
917                 return -ENODEV;
918
919         if (!driver             ||
920             !driver->unbind     ||
921             driver != gpriv->driver)
922                 return -EINVAL;
923
924         dev  = usbhsg_gpriv_to_dev(gpriv);
925         priv = usbhsg_gpriv_to_priv(gpriv);
926
927         usbhsg_try_stop(priv, USBHSG_STATUS_REGISTERD);
928         device_del(&gpriv->gadget.dev);
929         gpriv->driver = NULL;
930
931         if (driver->disconnect)
932                 driver->disconnect(&gpriv->gadget);
933
934         driver->unbind(&gpriv->gadget);
935         dev_dbg(dev, "unbind %s\n", driver->driver.name);
936
937         return 0;
938 }
939 EXPORT_SYMBOL(usb_gadget_unregister_driver);
940
941 /*
942  *              usb gadget ops
943  */
944 static int usbhsg_get_frame(struct usb_gadget *gadget)
945 {
946         struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
947         struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
948
949         return usbhs_frame_get_num(priv);
950 }
951
952 static struct usb_gadget_ops usbhsg_gadget_ops = {
953         .get_frame              = usbhsg_get_frame,
954 };
955
956 static int usbhsg_start(struct usbhs_priv *priv)
957 {
958         return usbhsg_try_start(priv, USBHSG_STATUS_STARTED);
959 }
960
961 static int usbhsg_stop(struct usbhs_priv *priv)
962 {
963         return usbhsg_try_stop(priv, USBHSG_STATUS_STARTED);
964 }
965
966 int __devinit usbhs_mod_gadget_probe(struct usbhs_priv *priv)
967 {
968         struct usbhsg_gpriv *gpriv;
969         struct usbhsg_uep *uep;
970         struct device *dev = usbhs_priv_to_dev(priv);
971         int pipe_size = usbhs_get_dparam(priv, pipe_size);
972         int i;
973
974         gpriv = kzalloc(sizeof(struct usbhsg_gpriv), GFP_KERNEL);
975         if (!gpriv) {
976                 dev_err(dev, "Could not allocate gadget priv\n");
977                 return -ENOMEM;
978         }
979
980         uep = kzalloc(sizeof(struct usbhsg_uep) * pipe_size, GFP_KERNEL);
981         if (!uep) {
982                 dev_err(dev, "Could not allocate ep\n");
983                 goto usbhs_mod_gadget_probe_err_gpriv;
984         }
985
986         /*
987          * CAUTION
988          *
989          * There is no guarantee that it is possible to access usb module here.
990          * Don't accesses to it.
991          * The accesse will be enable after "usbhsg_start"
992          */
993
994         /*
995          * register itself
996          */
997         usbhs_mod_register(priv, &gpriv->mod, USBHS_GADGET);
998
999         /* init gpriv */
1000         gpriv->mod.name         = "gadget";
1001         gpriv->mod.start        = usbhsg_start;
1002         gpriv->mod.stop         = usbhsg_stop;
1003         gpriv->uep              = uep;
1004         gpriv->uep_size         = pipe_size;
1005         usbhsg_status_init(gpriv);
1006
1007         /*
1008          * init gadget
1009          */
1010         device_initialize(&gpriv->gadget.dev);
1011         dev_set_name(&gpriv->gadget.dev, "gadget");
1012         gpriv->gadget.dev.parent        = dev;
1013         gpriv->gadget.name              = "renesas_usbhs_udc";
1014         gpriv->gadget.ops               = &usbhsg_gadget_ops;
1015         gpriv->gadget.is_dualspeed      = 1;
1016
1017         INIT_LIST_HEAD(&gpriv->gadget.ep_list);
1018
1019         /*
1020          * init usb_ep
1021          */
1022         usbhsg_for_each_uep_with_dcp(uep, gpriv, i) {
1023                 uep->gpriv      = gpriv;
1024                 snprintf(uep->ep_name, EP_NAME_SIZE, "ep%d", i);
1025
1026                 uep->ep.name            = uep->ep_name;
1027                 uep->ep.ops             = &usbhsg_ep_ops;
1028                 INIT_LIST_HEAD(&uep->ep.ep_list);
1029
1030                 /* init DCP */
1031                 if (usbhsg_is_dcp(uep)) {
1032                         gpriv->gadget.ep0 = &uep->ep;
1033                         uep->ep.maxpacket = 64;
1034                 }
1035                 /* init normal pipe */
1036                 else {
1037                         uep->ep.maxpacket = 512;
1038                         list_add_tail(&uep->ep.ep_list, &gpriv->gadget.ep_list);
1039                 }
1040         }
1041
1042         the_controller = gpriv;
1043
1044         dev_info(dev, "gadget probed\n");
1045
1046         return 0;
1047
1048 usbhs_mod_gadget_probe_err_gpriv:
1049         kfree(gpriv);
1050
1051         return -ENOMEM;
1052 }
1053
1054 void __devexit usbhs_mod_gadget_remove(struct usbhs_priv *priv)
1055 {
1056         struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
1057
1058         kfree(gpriv);
1059 }