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