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