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