usb: renesas_usbhs: shrink spin lock area
[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_dev(gp)         usbhs_priv_to_dev((gp)->mod.priv)
96 #define usbhsg_gpriv_to_priv(gp)        ((gp)->mod.priv)
97 #define usbhsg_gpriv_to_dcp(gp)         ((gp)->uep)
98 #define usbhsg_gpriv_to_nth_uep(gp, i)  ((gp)->uep + i)
99 #define usbhsg_uep_to_gpriv(u)          ((u)->gpriv)
100 #define usbhsg_uep_to_pipe(u)           ((u)->pipe)
101 #define usbhsg_pipe_to_uep(p)           ((p)->mod_private)
102 #define usbhsg_is_dcp(u)                ((u) == usbhsg_gpriv_to_dcp((u)->gpriv))
103
104 #define usbhsg_ureq_to_pkt(u)           (&(u)->pkt)
105 #define usbhsg_pkt_to_ureq(i)   \
106         container_of(i, struct usbhsg_request, pkt)
107
108 #define usbhsg_is_not_connected(gp) ((gp)->gadget.speed == USB_SPEED_UNKNOWN)
109
110 /* status */
111 #define usbhsg_status_init(gp)   do {(gp)->status = 0; } while (0)
112 #define usbhsg_status_set(gp, b) (gp->status |=  b)
113 #define usbhsg_status_clr(gp, b) (gp->status &= ~b)
114 #define usbhsg_status_has(gp, b) (gp->status &   b)
115
116 /*
117  *              list push/pop
118  */
119 static void usbhsg_queue_push(struct usbhsg_uep *uep,
120                               struct usbhsg_request *ureq)
121 {
122         struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
123         struct device *dev = usbhsg_gpriv_to_dev(gpriv);
124         struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
125         struct usbhs_pkt *pkt = usbhsg_ureq_to_pkt(ureq);
126         struct usb_request *req = &ureq->req;
127
128         usbhs_pkt_push(pipe, pkt, uep->handler,
129                        req->buf, req->length, req->zero);
130         req->actual = 0;
131         req->status = -EINPROGRESS;
132
133         dev_dbg(dev, "pipe %d : queue push (%d)\n",
134                 usbhs_pipe_number(pipe),
135                 req->length);
136 }
137
138 static void usbhsg_queue_start(struct usbhsg_uep *uep)
139 {
140         struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
141
142         usbhs_pkt_start(pipe);
143 }
144
145 static void usbhsg_queue_pop(struct usbhsg_uep *uep,
146                              struct usbhsg_request *ureq,
147                              int status)
148 {
149         struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
150         struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
151         struct device *dev = usbhsg_gpriv_to_dev(gpriv);
152
153         dev_dbg(dev, "pipe %d : queue pop\n", usbhs_pipe_number(pipe));
154
155         ureq->req.status = status;
156         ureq->req.complete(&uep->ep, &ureq->req);
157
158         /* more request ? */
159         if (0 == status)
160                 usbhsg_queue_start(uep);
161 }
162
163 static void usbhsg_queue_done(struct usbhs_pkt *pkt)
164 {
165         struct usbhs_pipe *pipe = pkt->pipe;
166         struct usbhsg_uep *uep = usbhsg_pipe_to_uep(pipe);
167         struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt);
168
169         ureq->req.actual = pkt->actual;
170
171         usbhsg_queue_pop(uep, ureq, 0);
172 }
173
174 /*
175  *              USB_TYPE_STANDARD / clear feature functions
176  */
177 static int usbhsg_recip_handler_std_control_done(struct usbhs_priv *priv,
178                                                  struct usbhsg_uep *uep,
179                                                  struct usb_ctrlrequest *ctrl)
180 {
181         struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
182         struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
183         struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
184
185         usbhs_dcp_control_transfer_done(pipe);
186
187         return 0;
188 }
189
190 static int usbhsg_recip_handler_std_clear_endpoint(struct usbhs_priv *priv,
191                                                    struct usbhsg_uep *uep,
192                                                    struct usb_ctrlrequest *ctrl)
193 {
194         struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
195         struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
196
197         if (!usbhsg_status_has(gpriv, USBHSG_STATUS_WEDGE)) {
198                 usbhs_pipe_disable(pipe);
199                 usbhs_pipe_clear_sequence(pipe);
200                 usbhs_pipe_enable(pipe);
201         }
202
203         usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
204
205         return 0;
206 }
207
208 struct usbhsg_recip_handle req_clear_feature = {
209         .name           = "clear feature",
210         .device         = usbhsg_recip_handler_std_control_done,
211         .interface      = usbhsg_recip_handler_std_control_done,
212         .endpoint       = usbhsg_recip_handler_std_clear_endpoint,
213 };
214
215 /*
216  *              USB_TYPE handler
217  */
218 static int usbhsg_recip_run_handle(struct usbhs_priv *priv,
219                                    struct usbhsg_recip_handle *handler,
220                                    struct usb_ctrlrequest *ctrl)
221 {
222         struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
223         struct device *dev = usbhsg_gpriv_to_dev(gpriv);
224         struct usbhsg_uep *uep;
225         int recip = ctrl->bRequestType & USB_RECIP_MASK;
226         int nth = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK;
227         int ret;
228         int (*func)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
229                     struct usb_ctrlrequest *ctrl);
230         char *msg;
231
232         uep = usbhsg_gpriv_to_nth_uep(gpriv, nth);
233         if (!usbhsg_uep_to_pipe(uep)) {
234                 dev_err(dev, "wrong recip request\n");
235                 ret = -EINVAL;
236                 goto usbhsg_recip_run_handle_end;
237         }
238
239         switch (recip) {
240         case USB_RECIP_DEVICE:
241                 msg     = "DEVICE";
242                 func    = handler->device;
243                 break;
244         case USB_RECIP_INTERFACE:
245                 msg     = "INTERFACE";
246                 func    = handler->interface;
247                 break;
248         case USB_RECIP_ENDPOINT:
249                 msg     = "ENDPOINT";
250                 func    = handler->endpoint;
251                 break;
252         default:
253                 dev_warn(dev, "unsupported RECIP(%d)\n", recip);
254                 func = NULL;
255                 ret = -EINVAL;
256         }
257
258         if (func) {
259                 unsigned long flags;
260
261                 dev_dbg(dev, "%s (pipe %d :%s)\n", handler->name, nth, msg);
262
263                 /********************  spin lock ********************/
264                 usbhs_lock(priv, flags);
265                 ret = func(priv, uep, ctrl);
266                 usbhs_unlock(priv, flags);
267                 /********************  spin unlock ******************/
268         }
269
270 usbhsg_recip_run_handle_end:
271         usbhsg_queue_start(uep);
272
273         return ret;
274 }
275
276 /*
277  *              irq functions
278  *
279  * it will be called from usbhs_interrupt
280  */
281 static int usbhsg_irq_dev_state(struct usbhs_priv *priv,
282                                 struct usbhs_irq_state *irq_state)
283 {
284         struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
285         struct device *dev = usbhsg_gpriv_to_dev(gpriv);
286
287         gpriv->gadget.speed = usbhs_status_get_usb_speed(irq_state);
288
289         dev_dbg(dev, "state = %x : speed : %d\n",
290                 usbhs_status_get_device_state(irq_state),
291                 gpriv->gadget.speed);
292
293         return 0;
294 }
295
296 static int usbhsg_irq_ctrl_stage(struct usbhs_priv *priv,
297                                  struct usbhs_irq_state *irq_state)
298 {
299         struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
300         struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
301         struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
302         struct device *dev = usbhsg_gpriv_to_dev(gpriv);
303         struct usb_ctrlrequest ctrl;
304         struct usbhsg_recip_handle *recip_handler = NULL;
305         int stage = usbhs_status_get_ctrl_stage(irq_state);
306         int ret = 0;
307
308         dev_dbg(dev, "stage = %d\n", stage);
309
310         /*
311          * see Manual
312          *
313          *  "Operation"
314          *  - "Interrupt Function"
315          *    - "Control Transfer Stage Transition Interrupt"
316          *      - Fig. "Control Transfer Stage Transitions"
317          */
318
319         switch (stage) {
320         case READ_DATA_STAGE:
321                 dcp->handler = &usbhs_fifo_push_handler;
322                 break;
323         case WRITE_DATA_STAGE:
324                 dcp->handler = &usbhs_fifo_pop_handler;
325                 break;
326         case NODATA_STATUS_STAGE:
327                 dcp->handler = &usbhs_ctrl_stage_end_handler;
328                 break;
329         default:
330                 return ret;
331         }
332
333         /*
334          * get usb request
335          */
336         usbhs_usbreq_get_val(priv, &ctrl);
337
338         switch (ctrl.bRequestType & USB_TYPE_MASK) {
339         case USB_TYPE_STANDARD:
340                 switch (ctrl.bRequest) {
341                 case USB_REQ_CLEAR_FEATURE:
342                         recip_handler = &req_clear_feature;
343                         break;
344                 }
345         }
346
347         /*
348          * setup stage / run recip
349          */
350         if (recip_handler)
351                 ret = usbhsg_recip_run_handle(priv, recip_handler, &ctrl);
352         else
353                 ret = gpriv->driver->setup(&gpriv->gadget, &ctrl);
354
355         if (ret < 0)
356                 usbhs_pipe_stall(pipe);
357
358         return ret;
359 }
360
361 /*
362  *
363  *              usb_dcp_ops
364  *
365  */
366 static int usbhsg_pipe_disable(struct usbhsg_uep *uep)
367 {
368         struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
369         struct usbhs_pkt *pkt;
370
371         usbhs_pipe_disable(pipe);
372
373         while (1) {
374                 pkt = usbhs_pkt_pop(pipe, NULL);
375                 if (!pkt)
376                         break;
377         }
378
379         return 0;
380 }
381
382 static void usbhsg_uep_init(struct usbhsg_gpriv *gpriv)
383 {
384         int i;
385         struct usbhsg_uep *uep;
386
387         usbhsg_for_each_uep_with_dcp(uep, gpriv, i)
388                 uep->pipe = NULL;
389 }
390
391 /*
392  *
393  *              usb_ep_ops
394  *
395  */
396 static int usbhsg_ep_enable(struct usb_ep *ep,
397                          const struct usb_endpoint_descriptor *desc)
398 {
399         struct usbhsg_uep *uep   = usbhsg_ep_to_uep(ep);
400         struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
401         struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
402         struct usbhs_pipe *pipe;
403         int ret = -EIO;
404
405         /*
406          * if it already have pipe,
407          * nothing to do
408          */
409         if (uep->pipe)
410                 return 0;
411
412         pipe = usbhs_pipe_malloc(priv, desc);
413         if (pipe) {
414                 uep->pipe               = pipe;
415                 pipe->mod_private       = uep;
416
417                 if (usb_endpoint_dir_in(desc))
418                         uep->handler = &usbhs_fifo_push_handler;
419                 else
420                         uep->handler = &usbhs_fifo_pop_handler;
421
422                 ret = 0;
423         }
424
425         return ret;
426 }
427
428 static int usbhsg_ep_disable(struct usb_ep *ep)
429 {
430         struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
431
432         return usbhsg_pipe_disable(uep);
433 }
434
435 static struct usb_request *usbhsg_ep_alloc_request(struct usb_ep *ep,
436                                                    gfp_t gfp_flags)
437 {
438         struct usbhsg_request *ureq;
439
440         ureq = kzalloc(sizeof *ureq, gfp_flags);
441         if (!ureq)
442                 return NULL;
443
444         usbhs_pkt_init(usbhsg_ureq_to_pkt(ureq));
445
446         return &ureq->req;
447 }
448
449 static void usbhsg_ep_free_request(struct usb_ep *ep,
450                                    struct usb_request *req)
451 {
452         struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
453
454         WARN_ON(!list_empty(&ureq->pkt.node));
455         kfree(ureq);
456 }
457
458 static int usbhsg_ep_queue(struct usb_ep *ep, struct usb_request *req,
459                           gfp_t gfp_flags)
460 {
461         struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
462         struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
463         struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
464         struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
465
466         /* param check */
467         if (usbhsg_is_not_connected(gpriv)      ||
468             unlikely(!gpriv->driver)            ||
469             unlikely(!pipe))
470                 return -ESHUTDOWN;
471
472         usbhsg_queue_push(uep, ureq);
473         usbhsg_queue_start(uep);
474
475         return 0;
476 }
477
478 static int usbhsg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
479 {
480         struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
481         struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
482         struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
483
484         usbhs_pkt_pop(pipe, usbhsg_ureq_to_pkt(ureq));
485         usbhsg_queue_pop(uep, ureq, -ECONNRESET);
486
487         return 0;
488 }
489
490 static int __usbhsg_ep_set_halt_wedge(struct usb_ep *ep, int halt, int wedge)
491 {
492         struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
493         struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
494         struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
495         struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
496         struct device *dev = usbhsg_gpriv_to_dev(gpriv);
497         unsigned long flags;
498
499         usbhsg_pipe_disable(uep);
500
501         dev_dbg(dev, "set halt %d (pipe %d)\n",
502                 halt, usbhs_pipe_number(pipe));
503
504         /********************  spin lock ********************/
505         usbhs_lock(priv, flags);
506
507         if (halt)
508                 usbhs_pipe_stall(pipe);
509         else
510                 usbhs_pipe_disable(pipe);
511
512         if (halt && wedge)
513                 usbhsg_status_set(gpriv, USBHSG_STATUS_WEDGE);
514         else
515                 usbhsg_status_clr(gpriv, USBHSG_STATUS_WEDGE);
516
517         usbhs_unlock(priv, flags);
518         /********************  spin unlock ******************/
519
520         return 0;
521 }
522
523 static int usbhsg_ep_set_halt(struct usb_ep *ep, int value)
524 {
525         return __usbhsg_ep_set_halt_wedge(ep, value, 0);
526 }
527
528 static int usbhsg_ep_set_wedge(struct usb_ep *ep)
529 {
530         return __usbhsg_ep_set_halt_wedge(ep, 1, 1);
531 }
532
533 static struct usb_ep_ops usbhsg_ep_ops = {
534         .enable         = usbhsg_ep_enable,
535         .disable        = usbhsg_ep_disable,
536
537         .alloc_request  = usbhsg_ep_alloc_request,
538         .free_request   = usbhsg_ep_free_request,
539
540         .queue          = usbhsg_ep_queue,
541         .dequeue        = usbhsg_ep_dequeue,
542
543         .set_halt       = usbhsg_ep_set_halt,
544         .set_wedge      = usbhsg_ep_set_wedge,
545 };
546
547 /*
548  *              usb module start/end
549  */
550 static int usbhsg_try_start(struct usbhs_priv *priv, u32 status)
551 {
552         struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
553         struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
554         struct usbhs_mod *mod = usbhs_mod_get_current(priv);
555         struct device *dev = usbhs_priv_to_dev(priv);
556         unsigned long flags;
557         int ret = 0;
558
559         /********************  spin lock ********************/
560         usbhs_lock(priv, flags);
561
562         usbhsg_status_set(gpriv, status);
563         if (!(usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
564               usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD)))
565                 ret = -1; /* not ready */
566
567         usbhs_unlock(priv, flags);
568         /********************  spin unlock ********************/
569
570         if (ret < 0)
571                 return 0; /* not ready is not error */
572
573         /*
574          * enable interrupt and systems if ready
575          */
576         dev_dbg(dev, "start gadget\n");
577
578         /*
579          * pipe initialize and enable DCP
580          */
581         usbhs_pipe_init(priv,
582                         usbhsg_queue_done);
583         usbhs_fifo_init(priv);
584         usbhsg_uep_init(gpriv);
585
586         /* dcp init */
587         dcp->pipe               = usbhs_dcp_malloc(priv);
588         dcp->pipe->mod_private  = dcp;
589
590         /*
591          * system config enble
592          * - HI speed
593          * - function
594          * - usb module
595          */
596         usbhs_sys_hispeed_ctrl(priv, 1);
597         usbhs_sys_function_ctrl(priv, 1);
598         usbhs_sys_usb_ctrl(priv, 1);
599
600         /*
601          * enable irq callback
602          */
603         mod->irq_dev_state      = usbhsg_irq_dev_state;
604         mod->irq_ctrl_stage     = usbhsg_irq_ctrl_stage;
605         usbhs_irq_callback_update(priv, mod);
606
607         return 0;
608 }
609
610 static int usbhsg_try_stop(struct usbhs_priv *priv, u32 status)
611 {
612         struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
613         struct usbhs_mod *mod = usbhs_mod_get_current(priv);
614         struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
615         struct device *dev = usbhs_priv_to_dev(priv);
616         unsigned long flags;
617         int ret = 0;
618
619         /********************  spin lock ********************/
620         usbhs_lock(priv, flags);
621
622         usbhsg_status_clr(gpriv, status);
623         if (!usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
624             !usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD))
625                 ret = -1; /* already done */
626
627         usbhs_unlock(priv, flags);
628         /********************  spin unlock ********************/
629
630         if (ret < 0)
631                 return 0; /* already done is not error */
632
633         /*
634          * disable interrupt and systems if 1st try
635          */
636         usbhs_fifo_quit(priv);
637
638         /* disable all irq */
639         mod->irq_dev_state      = NULL;
640         mod->irq_ctrl_stage     = NULL;
641         usbhs_irq_callback_update(priv, mod);
642
643         gpriv->gadget.speed = USB_SPEED_UNKNOWN;
644
645         /* disable sys */
646         usbhs_sys_hispeed_ctrl(priv, 0);
647         usbhs_sys_function_ctrl(priv, 0);
648         usbhs_sys_usb_ctrl(priv, 0);
649
650         usbhsg_pipe_disable(dcp);
651
652         if (gpriv->driver &&
653             gpriv->driver->disconnect)
654                 gpriv->driver->disconnect(&gpriv->gadget);
655
656         dev_dbg(dev, "stop gadget\n");
657
658         return 0;
659 }
660
661 /*
662  *
663  *              linux usb function
664  *
665  */
666 struct usbhsg_gpriv *the_controller;
667 int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
668                             int (*bind)(struct usb_gadget *))
669 {
670         struct usbhsg_gpriv *gpriv = the_controller;
671         struct usbhs_priv *priv;
672         struct device *dev;
673         int ret;
674
675         if (!bind               ||
676             !driver             ||
677             !driver->setup      ||
678             driver->speed != USB_SPEED_HIGH)
679                 return -EINVAL;
680         if (!gpriv)
681                 return -ENODEV;
682         if (gpriv->driver)
683                 return -EBUSY;
684
685         dev  = usbhsg_gpriv_to_dev(gpriv);
686         priv = usbhsg_gpriv_to_priv(gpriv);
687
688         /* first hook up the driver ... */
689         gpriv->driver = driver;
690         gpriv->gadget.dev.driver = &driver->driver;
691
692         ret = device_add(&gpriv->gadget.dev);
693         if (ret) {
694                 dev_err(dev, "device_add error %d\n", ret);
695                 goto add_fail;
696         }
697
698         ret = bind(&gpriv->gadget);
699         if (ret) {
700                 dev_err(dev, "bind to driver %s error %d\n",
701                         driver->driver.name, ret);
702                 goto bind_fail;
703         }
704
705         dev_dbg(dev, "bind %s\n", driver->driver.name);
706
707         return usbhsg_try_start(priv, USBHSG_STATUS_REGISTERD);
708
709 bind_fail:
710         device_del(&gpriv->gadget.dev);
711 add_fail:
712         gpriv->driver = NULL;
713         gpriv->gadget.dev.driver = NULL;
714
715         return ret;
716 }
717 EXPORT_SYMBOL(usb_gadget_probe_driver);
718
719 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
720 {
721         struct usbhsg_gpriv *gpriv = the_controller;
722         struct usbhs_priv *priv;
723         struct device *dev = usbhsg_gpriv_to_dev(gpriv);
724
725         if (!gpriv)
726                 return -ENODEV;
727
728         if (!driver             ||
729             !driver->unbind     ||
730             driver != gpriv->driver)
731                 return -EINVAL;
732
733         dev  = usbhsg_gpriv_to_dev(gpriv);
734         priv = usbhsg_gpriv_to_priv(gpriv);
735
736         usbhsg_try_stop(priv, USBHSG_STATUS_REGISTERD);
737         device_del(&gpriv->gadget.dev);
738         gpriv->driver = NULL;
739
740         if (driver->disconnect)
741                 driver->disconnect(&gpriv->gadget);
742
743         driver->unbind(&gpriv->gadget);
744         dev_dbg(dev, "unbind %s\n", driver->driver.name);
745
746         return 0;
747 }
748 EXPORT_SYMBOL(usb_gadget_unregister_driver);
749
750 /*
751  *              usb gadget ops
752  */
753 static int usbhsg_get_frame(struct usb_gadget *gadget)
754 {
755         struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
756         struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
757
758         return usbhs_frame_get_num(priv);
759 }
760
761 static struct usb_gadget_ops usbhsg_gadget_ops = {
762         .get_frame              = usbhsg_get_frame,
763 };
764
765 static int usbhsg_start(struct usbhs_priv *priv)
766 {
767         return usbhsg_try_start(priv, USBHSG_STATUS_STARTED);
768 }
769
770 static int usbhsg_stop(struct usbhs_priv *priv)
771 {
772         return usbhsg_try_stop(priv, USBHSG_STATUS_STARTED);
773 }
774
775 int __devinit usbhs_mod_gadget_probe(struct usbhs_priv *priv)
776 {
777         struct usbhsg_gpriv *gpriv;
778         struct usbhsg_uep *uep;
779         struct device *dev = usbhs_priv_to_dev(priv);
780         int pipe_size = usbhs_get_dparam(priv, pipe_size);
781         int i;
782
783         gpriv = kzalloc(sizeof(struct usbhsg_gpriv), GFP_KERNEL);
784         if (!gpriv) {
785                 dev_err(dev, "Could not allocate gadget priv\n");
786                 return -ENOMEM;
787         }
788
789         uep = kzalloc(sizeof(struct usbhsg_uep) * pipe_size, GFP_KERNEL);
790         if (!uep) {
791                 dev_err(dev, "Could not allocate ep\n");
792                 goto usbhs_mod_gadget_probe_err_gpriv;
793         }
794
795         /*
796          * CAUTION
797          *
798          * There is no guarantee that it is possible to access usb module here.
799          * Don't accesses to it.
800          * The accesse will be enable after "usbhsg_start"
801          */
802
803         /*
804          * register itself
805          */
806         usbhs_mod_register(priv, &gpriv->mod, USBHS_GADGET);
807
808         /* init gpriv */
809         gpriv->mod.name         = "gadget";
810         gpriv->mod.start        = usbhsg_start;
811         gpriv->mod.stop         = usbhsg_stop;
812         gpriv->uep              = uep;
813         gpriv->uep_size         = pipe_size;
814         usbhsg_status_init(gpriv);
815
816         /*
817          * init gadget
818          */
819         device_initialize(&gpriv->gadget.dev);
820         dev_set_name(&gpriv->gadget.dev, "gadget");
821         gpriv->gadget.dev.parent        = dev;
822         gpriv->gadget.name              = "renesas_usbhs_udc";
823         gpriv->gadget.ops               = &usbhsg_gadget_ops;
824         gpriv->gadget.is_dualspeed      = 1;
825
826         INIT_LIST_HEAD(&gpriv->gadget.ep_list);
827
828         /*
829          * init usb_ep
830          */
831         usbhsg_for_each_uep_with_dcp(uep, gpriv, i) {
832                 uep->gpriv      = gpriv;
833                 snprintf(uep->ep_name, EP_NAME_SIZE, "ep%d", i);
834
835                 uep->ep.name            = uep->ep_name;
836                 uep->ep.ops             = &usbhsg_ep_ops;
837                 INIT_LIST_HEAD(&uep->ep.ep_list);
838
839                 /* init DCP */
840                 if (usbhsg_is_dcp(uep)) {
841                         gpriv->gadget.ep0 = &uep->ep;
842                         uep->ep.maxpacket = 64;
843                 }
844                 /* init normal pipe */
845                 else {
846                         uep->ep.maxpacket = 512;
847                         list_add_tail(&uep->ep.ep_list, &gpriv->gadget.ep_list);
848                 }
849         }
850
851         the_controller = gpriv;
852
853         dev_info(dev, "gadget probed\n");
854
855         return 0;
856
857 usbhs_mod_gadget_probe_err_gpriv:
858         kfree(gpriv);
859
860         return -ENOMEM;
861 }
862
863 void __devexit usbhs_mod_gadget_remove(struct usbhs_priv *priv)
864 {
865         struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
866
867         kfree(gpriv);
868 }