298984f533dd15586c33c58e3eb846c22a6f4ee6
[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_pipe_handle;
35 struct usbhsg_uep {
36         struct usb_ep            ep;
37         struct usbhs_pipe       *pipe;
38
39         char ep_name[EP_NAME_SIZE];
40
41         struct usbhsg_gpriv *gpriv;
42         struct usbhsg_pipe_handle *handler;
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 };
59
60 struct usbhsg_pipe_handle {
61         int (*prepare)(struct usbhsg_uep *uep, struct usbhsg_request *ureq);
62         int (*try_run)(struct usbhsg_uep *uep, struct usbhsg_request *ureq);
63 };
64
65 struct usbhsg_recip_handle {
66         char *name;
67         int (*device)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
68                       struct usb_ctrlrequest *ctrl);
69         int (*interface)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
70                          struct usb_ctrlrequest *ctrl);
71         int (*endpoint)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
72                         struct usb_ctrlrequest *ctrl);
73 };
74
75 /*
76  *              macro
77  */
78 #define usbhsg_priv_to_gpriv(priv)                      \
79         container_of(                                   \
80                 usbhs_mod_get(priv, USBHS_GADGET),      \
81                 struct usbhsg_gpriv, mod)
82
83 #define __usbhsg_for_each_uep(start, pos, g, i) \
84         for (i = start, pos = (g)->uep;         \
85              i < (g)->uep_size;                 \
86              i++, pos = (g)->uep + i)
87
88 #define usbhsg_for_each_uep(pos, gpriv, i)      \
89         __usbhsg_for_each_uep(1, pos, gpriv, i)
90
91 #define usbhsg_for_each_uep_with_dcp(pos, gpriv, i)     \
92         __usbhsg_for_each_uep(0, pos, gpriv, i)
93
94 #define usbhsg_gadget_to_gpriv(g)\
95         container_of(g, struct usbhsg_gpriv, gadget)
96
97 #define usbhsg_req_to_ureq(r)\
98         container_of(r, struct usbhsg_request, req)
99
100 #define usbhsg_ep_to_uep(e)             container_of(e, struct usbhsg_uep, ep)
101 #define usbhsg_gpriv_to_lock(gp)        usbhs_priv_to_lock((gp)->mod.priv)
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  *              usbhsg_trylock
125  *
126  * This driver don't use spin_try_lock
127  * to avoid warning of CONFIG_DEBUG_SPINLOCK
128  */
129 static spinlock_t *usbhsg_trylock(struct usbhsg_gpriv *gpriv,
130                                   unsigned long *flags)
131 {
132         spinlock_t *lock = usbhsg_gpriv_to_lock(gpriv);
133
134         /* check spin lock status
135          * to avoid deadlock/nest */
136         if (spin_is_locked(lock))
137                 return NULL;
138
139         spin_lock_irqsave(lock, *flags);
140
141         return lock;
142 }
143
144 static void usbhsg_unlock(spinlock_t *lock, unsigned long *flags)
145 {
146         if (!lock)
147                 return;
148
149         spin_unlock_irqrestore(lock, *flags);
150 }
151
152 /*
153  *              list push/pop
154  */
155 static void usbhsg_queue_push(struct usbhsg_uep *uep,
156                               struct usbhsg_request *ureq)
157 {
158         struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
159         struct device *dev = usbhsg_gpriv_to_dev(gpriv);
160         struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
161         struct usbhs_pkt *pkt = usbhsg_ureq_to_pkt(ureq);
162         struct usb_request *req = &ureq->req;
163
164         /*
165          *********  assume under spin lock  *********
166          */
167         usbhs_pkt_push(pipe, pkt, req->buf, req->length, req->zero);
168         req->actual = 0;
169         req->status = -EINPROGRESS;
170
171         dev_dbg(dev, "pipe %d : queue push (%d)\n",
172                 usbhs_pipe_number(pipe),
173                 req->length);
174 }
175
176 static struct usbhsg_request *usbhsg_queue_get(struct usbhsg_uep *uep)
177 {
178         struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
179         struct usbhs_pkt *pkt = usbhs_pkt_get(pipe);
180
181         /*
182          *********  assume under spin lock  *********
183          */
184         if (!pkt)
185                 return 0;
186
187         return usbhsg_pkt_to_ureq(pkt);
188 }
189
190 #define usbhsg_queue_prepare(uep) __usbhsg_queue_handler(uep, 1);
191 #define usbhsg_queue_handle(uep)  __usbhsg_queue_handler(uep, 0);
192 static int __usbhsg_queue_handler(struct usbhsg_uep *uep, int prepare)
193 {
194         struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
195         struct device *dev = usbhsg_gpriv_to_dev(gpriv);
196         struct usbhsg_request *ureq;
197         spinlock_t *lock;
198         unsigned long flags;
199         int ret = 0;
200
201         if (!uep->handler) {
202                 dev_err(dev, "no handler function\n");
203                 return -EIO;
204         }
205
206         /*
207          * CAUTION [*queue handler*]
208          *
209          * This function will be called for start/restart queue operation.
210          * OTOH the most much worry for USB driver is spinlock nest.
211          * Specially it are
212          *   - usb_ep_ops  :: queue
213          *   - usb_request :: complete
214          *
215          * But the caller of this function need not care about spinlock.
216          * This function is using usbhsg_trylock for it.
217          * if "is_locked" is 1, this mean this function lock it.
218          * but if it is 0, this mean it is already under spin lock.
219          * see also
220          *   CAUTION [*endpoint queue*]
221          *   CAUTION [*request complete*]
222          */
223
224         /******************  spin try lock *******************/
225         lock = usbhsg_trylock(gpriv, &flags);
226
227         ureq = usbhsg_queue_get(uep);
228         if (ureq) {
229                 if (prepare)
230                         ret = uep->handler->prepare(uep, ureq);
231                 else
232                         ret = uep->handler->try_run(uep, ureq);
233         }
234         usbhsg_unlock(lock, &flags);
235         /********************  spin unlock ******************/
236
237         return ret;
238 }
239
240 static void usbhsg_queue_pop(struct usbhsg_uep *uep,
241                              struct usbhsg_request *ureq,
242                              int status)
243 {
244         struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
245         struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
246         struct device *dev = usbhsg_gpriv_to_dev(gpriv);
247         struct usbhs_pkt *pkt = usbhsg_ureq_to_pkt(ureq);
248
249         /*
250          *********  assume under spin lock  *********
251          */
252
253         /*
254          * CAUTION [*request complete*]
255          *
256          * There is a possibility not to be called in correct order
257          * if "complete" is called without spinlock.
258          *
259          * So, this function assume it is under spinlock,
260          * and call usb_request :: complete.
261          *
262          * But this "complete" will push next usb_request.
263          * It mean "usb_ep_ops :: queue" which is using spinlock is called
264          * under spinlock.
265          *
266          * To avoid dead-lock, this driver is using usbhsg_trylock.
267          *   CAUTION [*endpoint queue*]
268          *   CAUTION [*queue handler*]
269          */
270
271         dev_dbg(dev, "pipe %d : queue pop\n", usbhs_pipe_number(pipe));
272
273         usbhs_pkt_pop(pkt);
274
275         ureq->req.status = status;
276         ureq->req.complete(&uep->ep, &ureq->req);
277
278         /* more request ? */
279         if (0 == status)
280                 usbhsg_queue_prepare(uep);
281 }
282
283 /*
284  *              irq enable/disable function
285  */
286 #define usbhsg_irq_callback_ctrl(uep, status, enable)                   \
287         ({                                                              \
288                 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);  \
289                 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);      \
290                 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);  \
291                 struct usbhs_mod *mod = usbhs_mod_get_current(priv);    \
292                 if (!mod)                                               \
293                         return;                                         \
294                 if (enable)                                             \
295                         mod->irq_##status |= (1 << usbhs_pipe_number(pipe)); \
296                 else                                                    \
297                         mod->irq_##status &= ~(1 << usbhs_pipe_number(pipe)); \
298                 usbhs_irq_callback_update(priv, mod);                   \
299         })
300
301 static void usbhsg_irq_empty_ctrl(struct usbhsg_uep *uep, int enable)
302 {
303         usbhsg_irq_callback_ctrl(uep, bempsts, enable);
304 }
305
306 static void usbhsg_irq_ready_ctrl(struct usbhsg_uep *uep, int enable)
307 {
308         usbhsg_irq_callback_ctrl(uep, brdysts, enable);
309 }
310
311 /*
312  *              handler function
313  */
314 static int usbhsg_try_run_ctrl_stage_end(struct usbhsg_uep *uep,
315                                          struct usbhsg_request *ureq)
316 {
317         struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
318
319         /*
320          *********  assume under spin lock  *********
321          */
322
323         usbhs_dcp_control_transfer_done(pipe);
324         usbhsg_queue_pop(uep, ureq, 0);
325
326         return 0;
327 }
328
329 /*
330  *              packet send hander
331  */
332 static void usbhsg_send_packet_done(struct usbhs_pkt *pkt)
333 {
334         struct usbhs_pipe *pipe = pkt->pipe;
335         struct usbhsg_uep *uep = usbhsg_pipe_to_uep(pipe);
336         struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt);
337
338         ureq->req.actual = pkt->actual;
339
340         usbhsg_queue_pop(uep, ureq, 0);
341 }
342
343 static int usbhsg_try_run_send_packet(struct usbhsg_uep *uep,
344                                       struct usbhsg_request *ureq)
345 {
346         struct usbhs_pkt *pkt = usbhsg_ureq_to_pkt(ureq);
347
348         /*
349          *********  assume under spin lock  *********
350          */
351
352         usbhs_fifo_write(pkt);
353
354         return 0;
355 }
356
357 static int usbhsg_prepare_send_packet(struct usbhsg_uep *uep,
358                                       struct usbhsg_request *ureq)
359 {
360         struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
361
362         /*
363          *********  assume under spin lock  *********
364          */
365
366         usbhs_fifo_prepare_write(pipe);
367         usbhsg_try_run_send_packet(uep, ureq);
368
369         return 0;
370 }
371
372 /*
373  *              packet recv hander
374  */
375 static void usbhsg_receive_packet_done(struct usbhs_pkt *pkt)
376 {
377         struct usbhs_pipe *pipe = pkt->pipe;
378         struct usbhsg_uep *uep = usbhsg_pipe_to_uep(pipe);
379         struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt);
380
381         ureq->req.actual = pkt->actual;
382
383         usbhsg_queue_pop(uep, ureq, 0);
384 }
385
386 static int usbhsg_try_run_receive_packet(struct usbhsg_uep *uep,
387                                          struct usbhsg_request *ureq)
388 {
389         struct usbhs_pkt *pkt = usbhsg_ureq_to_pkt(ureq);
390
391         /*
392          *********  assume under spin lock  *********
393          */
394
395         return usbhs_fifo_read(pkt);
396 }
397
398 static int usbhsg_prepare_receive_packet(struct usbhsg_uep *uep,
399                                          struct usbhsg_request *ureq)
400 {
401         struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
402
403         /*
404          *********  assume under spin lock  *********
405          */
406
407         return usbhs_fifo_prepare_read(pipe);
408 }
409
410 static struct usbhsg_pipe_handle usbhsg_handler_send_by_empty = {
411         .prepare        = usbhsg_prepare_send_packet,
412         .try_run        = usbhsg_try_run_send_packet,
413 };
414
415 static struct usbhsg_pipe_handle usbhsg_handler_send_by_ready = {
416         .prepare        = usbhsg_prepare_send_packet,
417         .try_run        = usbhsg_try_run_send_packet,
418 };
419
420 static struct usbhsg_pipe_handle usbhsg_handler_recv_by_ready = {
421         .prepare        = usbhsg_prepare_receive_packet,
422         .try_run        = usbhsg_try_run_receive_packet,
423 };
424
425 static struct usbhsg_pipe_handle usbhsg_handler_ctrl_stage_end = {
426         .prepare        = usbhsg_try_run_ctrl_stage_end,
427         .try_run        = usbhsg_try_run_ctrl_stage_end,
428 };
429
430 /*
431  * DCP pipe can NOT use "ready interrupt" for "send"
432  * it should use "empty" interrupt.
433  * see
434  *   "Operation" - "Interrupt Function" - "BRDY Interrupt"
435  *
436  * on the other hand, normal pipe can use "ready interrupt" for "send"
437  * even though it is single/double buffer
438  */
439 #define usbhsg_handler_send_ctrl        usbhsg_handler_send_by_empty
440 #define usbhsg_handler_recv_ctrl        usbhsg_handler_recv_by_ready
441
442 #define usbhsg_handler_send_packet      usbhsg_handler_send_by_ready
443 #define usbhsg_handler_recv_packet      usbhsg_handler_recv_by_ready
444
445 /*
446  *              USB_TYPE_STANDARD / clear feature functions
447  */
448 static int usbhsg_recip_handler_std_control_done(struct usbhs_priv *priv,
449                                                  struct usbhsg_uep *uep,
450                                                  struct usb_ctrlrequest *ctrl)
451 {
452         struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
453         struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
454         struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
455
456         usbhs_dcp_control_transfer_done(pipe);
457
458         return 0;
459 }
460
461 static int usbhsg_recip_handler_std_clear_endpoint(struct usbhs_priv *priv,
462                                                    struct usbhsg_uep *uep,
463                                                    struct usb_ctrlrequest *ctrl)
464 {
465         struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
466         struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
467
468         if (!usbhsg_status_has(gpriv, USBHSG_STATUS_WEDGE)) {
469                 usbhs_pipe_disable(pipe);
470                 usbhs_pipe_clear_sequence(pipe);
471                 usbhs_pipe_enable(pipe);
472         }
473
474         usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
475
476         usbhsg_queue_prepare(uep);
477
478         return 0;
479 }
480
481 struct usbhsg_recip_handle req_clear_feature = {
482         .name           = "clear feature",
483         .device         = usbhsg_recip_handler_std_control_done,
484         .interface      = usbhsg_recip_handler_std_control_done,
485         .endpoint       = usbhsg_recip_handler_std_clear_endpoint,
486 };
487
488 /*
489  *              USB_TYPE handler
490  */
491 static int usbhsg_recip_run_handle(struct usbhs_priv *priv,
492                                    struct usbhsg_recip_handle *handler,
493                                    struct usb_ctrlrequest *ctrl)
494 {
495         struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
496         struct device *dev = usbhsg_gpriv_to_dev(gpriv);
497         struct usbhsg_uep *uep;
498         int recip = ctrl->bRequestType & USB_RECIP_MASK;
499         int nth = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK;
500         int ret;
501         int (*func)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
502                     struct usb_ctrlrequest *ctrl);
503         char *msg;
504
505         uep = usbhsg_gpriv_to_nth_uep(gpriv, nth);
506         if (!usbhsg_uep_to_pipe(uep)) {
507                 dev_err(dev, "wrong recip request\n");
508                 return -EINVAL;
509         }
510
511         switch (recip) {
512         case USB_RECIP_DEVICE:
513                 msg     = "DEVICE";
514                 func    = handler->device;
515                 break;
516         case USB_RECIP_INTERFACE:
517                 msg     = "INTERFACE";
518                 func    = handler->interface;
519                 break;
520         case USB_RECIP_ENDPOINT:
521                 msg     = "ENDPOINT";
522                 func    = handler->endpoint;
523                 break;
524         default:
525                 dev_warn(dev, "unsupported RECIP(%d)\n", recip);
526                 func = NULL;
527                 ret = -EINVAL;
528         }
529
530         if (func) {
531                 dev_dbg(dev, "%s (pipe %d :%s)\n", handler->name, nth, msg);
532                 ret = func(priv, uep, ctrl);
533         }
534
535         return ret;
536 }
537
538 /*
539  *              irq functions
540  *
541  * it will be called from usbhs_interrupt
542  */
543 static int usbhsg_irq_dev_state(struct usbhs_priv *priv,
544                                 struct usbhs_irq_state *irq_state)
545 {
546         struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
547         struct device *dev = usbhsg_gpriv_to_dev(gpriv);
548
549         gpriv->gadget.speed = usbhs_status_get_usb_speed(irq_state);
550
551         dev_dbg(dev, "state = %x : speed : %d\n",
552                 usbhs_status_get_device_state(irq_state),
553                 gpriv->gadget.speed);
554
555         return 0;
556 }
557
558 static int usbhsg_irq_ctrl_stage(struct usbhs_priv *priv,
559                                  struct usbhs_irq_state *irq_state)
560 {
561         struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
562         struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
563         struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
564         struct device *dev = usbhsg_gpriv_to_dev(gpriv);
565         struct usb_ctrlrequest ctrl;
566         struct usbhsg_recip_handle *recip_handler = NULL;
567         int stage = usbhs_status_get_ctrl_stage(irq_state);
568         int ret = 0;
569
570         dev_dbg(dev, "stage = %d\n", stage);
571
572         /*
573          * see Manual
574          *
575          *  "Operation"
576          *  - "Interrupt Function"
577          *    - "Control Transfer Stage Transition Interrupt"
578          *      - Fig. "Control Transfer Stage Transitions"
579          */
580
581         switch (stage) {
582         case READ_DATA_STAGE:
583                 dcp->handler = &usbhsg_handler_send_ctrl;
584                 break;
585         case WRITE_DATA_STAGE:
586                 dcp->handler = &usbhsg_handler_recv_ctrl;
587                 break;
588         case NODATA_STATUS_STAGE:
589                 dcp->handler = &usbhsg_handler_ctrl_stage_end;
590                 break;
591         default:
592                 return ret;
593         }
594
595         /*
596          * get usb request
597          */
598         usbhs_usbreq_get_val(priv, &ctrl);
599
600         switch (ctrl.bRequestType & USB_TYPE_MASK) {
601         case USB_TYPE_STANDARD:
602                 switch (ctrl.bRequest) {
603                 case USB_REQ_CLEAR_FEATURE:
604                         recip_handler = &req_clear_feature;
605                         break;
606                 }
607         }
608
609         /*
610          * setup stage / run recip
611          */
612         if (recip_handler)
613                 ret = usbhsg_recip_run_handle(priv, recip_handler, &ctrl);
614         else
615                 ret = gpriv->driver->setup(&gpriv->gadget, &ctrl);
616
617         if (ret < 0)
618                 usbhs_pipe_stall(pipe);
619
620         return ret;
621 }
622
623 static int usbhsg_irq_empty(struct usbhs_priv *priv,
624                             struct usbhs_irq_state *irq_state)
625 {
626         struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
627         struct usbhsg_uep *uep;
628         struct usbhs_pipe *pipe;
629         struct device *dev = usbhsg_gpriv_to_dev(gpriv);
630         int i, ret;
631
632         if (!irq_state->bempsts) {
633                 dev_err(dev, "debug %s !!\n", __func__);
634                 return -EIO;
635         }
636
637         dev_dbg(dev, "irq empty [0x%04x]\n", irq_state->bempsts);
638
639         /*
640          * search interrupted "pipe"
641          * not "uep".
642          */
643         usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
644                 if (!(irq_state->bempsts & (1 << i)))
645                         continue;
646
647                 uep     = usbhsg_pipe_to_uep(pipe);
648                 ret     = usbhsg_queue_handle(uep);
649                 if (ret < 0)
650                         dev_err(dev, "send error %d : %d\n", i, ret);
651         }
652
653         return 0;
654 }
655
656 static int usbhsg_irq_ready(struct usbhs_priv *priv,
657                             struct usbhs_irq_state *irq_state)
658 {
659         struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
660         struct usbhsg_uep *uep;
661         struct usbhs_pipe *pipe;
662         struct device *dev = usbhsg_gpriv_to_dev(gpriv);
663         int i, ret;
664
665         if (!irq_state->brdysts) {
666                 dev_err(dev, "debug %s !!\n", __func__);
667                 return -EIO;
668         }
669
670         dev_dbg(dev, "irq ready [0x%04x]\n", irq_state->brdysts);
671
672         /*
673          * search interrupted "pipe"
674          * not "uep".
675          */
676         usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
677                 if (!(irq_state->brdysts & (1 << i)))
678                         continue;
679
680                 uep     = usbhsg_pipe_to_uep(pipe);
681                 ret     = usbhsg_queue_handle(uep);
682                 if (ret < 0)
683                         dev_err(dev, "receive error %d : %d\n", i, ret);
684         }
685
686         return 0;
687 }
688
689 /*
690  *
691  *              usb_dcp_ops
692  *
693  */
694 static int usbhsg_dcp_enable(struct usbhsg_uep *uep)
695 {
696         struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
697         struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
698         struct usbhs_pipe *pipe;
699
700         /*
701          *********  assume under spin lock  *********
702          */
703
704         pipe = usbhs_dcp_malloc(priv);
705         if (!pipe)
706                 return -EIO;
707
708         uep->pipe               = pipe;
709         uep->pipe->mod_private  = uep;
710
711         return 0;
712 }
713
714 #define usbhsg_dcp_disable usbhsg_pipe_disable
715 static int usbhsg_pipe_disable(struct usbhsg_uep *uep)
716 {
717         struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
718         struct usbhsg_request *ureq;
719         int disable = 0;
720
721         /*
722          *********  assume under spin lock  *********
723          */
724
725         usbhs_pipe_disable(pipe);
726
727         /*
728          * disable pipe irq
729          */
730         usbhsg_irq_empty_ctrl(uep, disable);
731         usbhsg_irq_ready_ctrl(uep, disable);
732
733         while (1) {
734                 ureq = usbhsg_queue_get(uep);
735                 if (!ureq)
736                         break;
737
738                 usbhsg_queue_pop(uep, ureq, -ECONNRESET);
739         }
740
741         return 0;
742 }
743
744 static void usbhsg_uep_init(struct usbhsg_gpriv *gpriv)
745 {
746         int i;
747         struct usbhsg_uep *uep;
748
749         usbhsg_for_each_uep_with_dcp(uep, gpriv, i)
750                 uep->pipe = NULL;
751 }
752
753 /*
754  *
755  *              usb_ep_ops
756  *
757  */
758 static int usbhsg_ep_enable(struct usb_ep *ep,
759                          const struct usb_endpoint_descriptor *desc)
760 {
761         struct usbhsg_uep *uep   = usbhsg_ep_to_uep(ep);
762         struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
763         struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
764         struct usbhs_pipe *pipe;
765         spinlock_t *lock;
766         unsigned long flags;
767         int ret = -EIO;
768
769         /*
770          * if it already have pipe,
771          * nothing to do
772          */
773         if (uep->pipe)
774                 return 0;
775
776         /********************  spin lock ********************/
777         lock = usbhsg_trylock(gpriv, &flags);
778
779         pipe = usbhs_pipe_malloc(priv, desc);
780         if (pipe) {
781                 uep->pipe               = pipe;
782                 pipe->mod_private       = uep;
783
784                 if (usb_endpoint_dir_in(desc))
785                         uep->handler = &usbhsg_handler_send_packet;
786                 else
787                         uep->handler = &usbhsg_handler_recv_packet;
788
789                 ret = 0;
790         }
791
792         usbhsg_unlock(lock, &flags);
793         /********************  spin unlock ******************/
794
795         return ret;
796 }
797
798 static int usbhsg_ep_disable(struct usb_ep *ep)
799 {
800         struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
801         struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
802         spinlock_t *lock;
803         unsigned long flags;
804         int ret;
805
806         /********************  spin lock ********************/
807         lock = usbhsg_trylock(gpriv, &flags);
808
809         ret = usbhsg_pipe_disable(uep);
810
811         usbhsg_unlock(lock, &flags);
812         /********************  spin unlock ******************/
813
814         return ret;
815 }
816
817 static struct usb_request *usbhsg_ep_alloc_request(struct usb_ep *ep,
818                                                    gfp_t gfp_flags)
819 {
820         struct usbhsg_request *ureq;
821
822         ureq = kzalloc(sizeof *ureq, gfp_flags);
823         if (!ureq)
824                 return NULL;
825
826         usbhs_pkt_init(usbhsg_ureq_to_pkt(ureq));
827
828         return &ureq->req;
829 }
830
831 static void usbhsg_ep_free_request(struct usb_ep *ep,
832                                    struct usb_request *req)
833 {
834         struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
835
836         WARN_ON(!list_empty(&ureq->pkt.node));
837         kfree(ureq);
838 }
839
840 static int usbhsg_ep_queue(struct usb_ep *ep, struct usb_request *req,
841                           gfp_t gfp_flags)
842 {
843         struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
844         struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
845         struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
846         struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
847         spinlock_t *lock;
848         unsigned long flags;
849         int ret = 0;
850
851         /*
852          * CAUTION [*endpoint queue*]
853          *
854          * This function will be called from usb_request :: complete
855          * or usb driver timing.
856          * If this function is called from usb_request :: complete,
857          * it is already under spinlock on this driver.
858          * but it is called frm usb driver, this function should call spinlock.
859          *
860          * This function is using usbshg_trylock to solve this issue.
861          * if "is_locked" is 1, this mean this function lock it.
862          * but if it is 0, this mean it is already under spin lock.
863          * see also
864          *   CAUTION [*queue handler*]
865          *   CAUTION [*request complete*]
866          */
867
868         /********************  spin lock ********************/
869         lock = usbhsg_trylock(gpriv, &flags);
870
871         /* param check */
872         if (usbhsg_is_not_connected(gpriv)      ||
873             unlikely(!gpriv->driver)            ||
874             unlikely(!pipe))
875                 ret = -ESHUTDOWN;
876         else
877                 usbhsg_queue_push(uep, ureq);
878
879         usbhsg_unlock(lock, &flags);
880         /********************  spin unlock ******************/
881
882         usbhsg_queue_prepare(uep);
883
884         return ret;
885 }
886
887 static int usbhsg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
888 {
889         struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
890         struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
891         struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
892         spinlock_t *lock;
893         unsigned long flags;
894
895         /*
896          * see
897          *   CAUTION [*queue handler*]
898          *   CAUTION [*endpoint queue*]
899          *   CAUTION [*request complete*]
900          */
901
902         /********************  spin lock ********************/
903         lock = usbhsg_trylock(gpriv, &flags);
904
905         usbhsg_queue_pop(uep, ureq, -ECONNRESET);
906
907         usbhsg_unlock(lock, &flags);
908         /********************  spin unlock ******************/
909
910         return 0;
911 }
912
913 static int __usbhsg_ep_set_halt_wedge(struct usb_ep *ep, int halt, int wedge)
914 {
915         struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
916         struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
917         struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
918         struct device *dev = usbhsg_gpriv_to_dev(gpriv);
919         spinlock_t *lock;
920         unsigned long flags;
921         int ret = -EAGAIN;
922
923         /*
924          * see
925          *   CAUTION [*queue handler*]
926          *   CAUTION [*endpoint queue*]
927          *   CAUTION [*request complete*]
928          */
929
930         /********************  spin lock ********************/
931         lock = usbhsg_trylock(gpriv, &flags);
932         if (!usbhsg_queue_get(uep)) {
933
934                 dev_dbg(dev, "set halt %d (pipe %d)\n",
935                         halt, usbhs_pipe_number(pipe));
936
937                 if (halt)
938                         usbhs_pipe_stall(pipe);
939                 else
940                         usbhs_pipe_disable(pipe);
941
942                 if (halt && wedge)
943                         usbhsg_status_set(gpriv, USBHSG_STATUS_WEDGE);
944                 else
945                         usbhsg_status_clr(gpriv, USBHSG_STATUS_WEDGE);
946
947                 ret = 0;
948         }
949
950         usbhsg_unlock(lock, &flags);
951         /********************  spin unlock ******************/
952
953         return ret;
954 }
955
956 static int usbhsg_ep_set_halt(struct usb_ep *ep, int value)
957 {
958         return __usbhsg_ep_set_halt_wedge(ep, value, 0);
959 }
960
961 static int usbhsg_ep_set_wedge(struct usb_ep *ep)
962 {
963         return __usbhsg_ep_set_halt_wedge(ep, 1, 1);
964 }
965
966 static struct usb_ep_ops usbhsg_ep_ops = {
967         .enable         = usbhsg_ep_enable,
968         .disable        = usbhsg_ep_disable,
969
970         .alloc_request  = usbhsg_ep_alloc_request,
971         .free_request   = usbhsg_ep_free_request,
972
973         .queue          = usbhsg_ep_queue,
974         .dequeue        = usbhsg_ep_dequeue,
975
976         .set_halt       = usbhsg_ep_set_halt,
977         .set_wedge      = usbhsg_ep_set_wedge,
978 };
979
980 /*
981  *              usb module start/end
982  */
983 static int usbhsg_try_start(struct usbhs_priv *priv, u32 status)
984 {
985         struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
986         struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
987         struct usbhs_mod *mod = usbhs_mod_get_current(priv);
988         struct device *dev = usbhs_priv_to_dev(priv);
989         spinlock_t *lock;
990         unsigned long flags;
991
992         /********************  spin lock ********************/
993         lock = usbhsg_trylock(gpriv, &flags);
994
995         /*
996          * enable interrupt and systems if ready
997          */
998         usbhsg_status_set(gpriv, status);
999         if (!(usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
1000               usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD)))
1001                 goto usbhsg_try_start_unlock;
1002
1003         dev_dbg(dev, "start gadget\n");
1004
1005         /*
1006          * pipe initialize and enable DCP
1007          */
1008         usbhs_pipe_init(priv,
1009                         usbhsg_send_packet_done,
1010                         usbhsg_receive_packet_done);
1011         usbhsg_uep_init(gpriv);
1012         usbhsg_dcp_enable(dcp);
1013
1014         /*
1015          * system config enble
1016          * - HI speed
1017          * - function
1018          * - usb module
1019          */
1020         usbhs_sys_hispeed_ctrl(priv, 1);
1021         usbhs_sys_function_ctrl(priv, 1);
1022         usbhs_sys_usb_ctrl(priv, 1);
1023
1024         /*
1025          * enable irq callback
1026          */
1027         mod->irq_dev_state      = usbhsg_irq_dev_state;
1028         mod->irq_ctrl_stage     = usbhsg_irq_ctrl_stage;
1029         mod->irq_empty          = usbhsg_irq_empty;
1030         mod->irq_ready          = usbhsg_irq_ready;
1031         mod->irq_bempsts        = 0;
1032         mod->irq_brdysts        = 0;
1033         usbhs_irq_callback_update(priv, mod);
1034
1035 usbhsg_try_start_unlock:
1036         usbhsg_unlock(lock, &flags);
1037         /********************  spin unlock ********************/
1038
1039         return 0;
1040 }
1041
1042 static int usbhsg_try_stop(struct usbhs_priv *priv, u32 status)
1043 {
1044         struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
1045         struct usbhs_mod *mod = usbhs_mod_get_current(priv);
1046         struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
1047         struct device *dev = usbhs_priv_to_dev(priv);
1048         spinlock_t *lock;
1049         unsigned long flags;
1050
1051         /********************  spin lock ********************/
1052         lock = usbhsg_trylock(gpriv, &flags);
1053
1054         /*
1055          * disable interrupt and systems if 1st try
1056          */
1057         usbhsg_status_clr(gpriv, status);
1058         if (!usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
1059             !usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD))
1060                 goto usbhsg_try_stop_unlock;
1061
1062         /* disable all irq */
1063         mod->irq_dev_state      = NULL;
1064         mod->irq_ctrl_stage     = NULL;
1065         mod->irq_empty          = NULL;
1066         mod->irq_ready          = NULL;
1067         mod->irq_bempsts        = 0;
1068         mod->irq_brdysts        = 0;
1069         usbhs_irq_callback_update(priv, mod);
1070
1071         usbhsg_dcp_disable(dcp);
1072
1073         gpriv->gadget.speed = USB_SPEED_UNKNOWN;
1074
1075         /* disable sys */
1076         usbhs_sys_hispeed_ctrl(priv, 0);
1077         usbhs_sys_function_ctrl(priv, 0);
1078         usbhs_sys_usb_ctrl(priv, 0);
1079
1080         usbhsg_unlock(lock, &flags);
1081         /********************  spin unlock ********************/
1082
1083         if (gpriv->driver &&
1084             gpriv->driver->disconnect)
1085                 gpriv->driver->disconnect(&gpriv->gadget);
1086
1087         dev_dbg(dev, "stop gadget\n");
1088
1089         return 0;
1090
1091 usbhsg_try_stop_unlock:
1092         usbhsg_unlock(lock, &flags);
1093
1094         return 0;
1095 }
1096
1097 /*
1098  *
1099  *              linux usb function
1100  *
1101  */
1102 struct usbhsg_gpriv *the_controller;
1103 int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
1104                             int (*bind)(struct usb_gadget *))
1105 {
1106         struct usbhsg_gpriv *gpriv = the_controller;
1107         struct usbhs_priv *priv;
1108         struct device *dev;
1109         int ret;
1110
1111         if (!bind               ||
1112             !driver             ||
1113             !driver->setup      ||
1114             driver->speed != USB_SPEED_HIGH)
1115                 return -EINVAL;
1116         if (!gpriv)
1117                 return -ENODEV;
1118         if (gpriv->driver)
1119                 return -EBUSY;
1120
1121         dev  = usbhsg_gpriv_to_dev(gpriv);
1122         priv = usbhsg_gpriv_to_priv(gpriv);
1123
1124         /* first hook up the driver ... */
1125         gpriv->driver = driver;
1126         gpriv->gadget.dev.driver = &driver->driver;
1127
1128         ret = device_add(&gpriv->gadget.dev);
1129         if (ret) {
1130                 dev_err(dev, "device_add error %d\n", ret);
1131                 goto add_fail;
1132         }
1133
1134         ret = bind(&gpriv->gadget);
1135         if (ret) {
1136                 dev_err(dev, "bind to driver %s error %d\n",
1137                         driver->driver.name, ret);
1138                 goto bind_fail;
1139         }
1140
1141         dev_dbg(dev, "bind %s\n", driver->driver.name);
1142
1143         return usbhsg_try_start(priv, USBHSG_STATUS_REGISTERD);
1144
1145 bind_fail:
1146         device_del(&gpriv->gadget.dev);
1147 add_fail:
1148         gpriv->driver = NULL;
1149         gpriv->gadget.dev.driver = NULL;
1150
1151         return ret;
1152 }
1153 EXPORT_SYMBOL(usb_gadget_probe_driver);
1154
1155 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1156 {
1157         struct usbhsg_gpriv *gpriv = the_controller;
1158         struct usbhs_priv *priv;
1159         struct device *dev = usbhsg_gpriv_to_dev(gpriv);
1160
1161         if (!gpriv)
1162                 return -ENODEV;
1163
1164         if (!driver             ||
1165             !driver->unbind     ||
1166             driver != gpriv->driver)
1167                 return -EINVAL;
1168
1169         dev  = usbhsg_gpriv_to_dev(gpriv);
1170         priv = usbhsg_gpriv_to_priv(gpriv);
1171
1172         usbhsg_try_stop(priv, USBHSG_STATUS_REGISTERD);
1173         device_del(&gpriv->gadget.dev);
1174         gpriv->driver = NULL;
1175
1176         if (driver->disconnect)
1177                 driver->disconnect(&gpriv->gadget);
1178
1179         driver->unbind(&gpriv->gadget);
1180         dev_dbg(dev, "unbind %s\n", driver->driver.name);
1181
1182         return 0;
1183 }
1184 EXPORT_SYMBOL(usb_gadget_unregister_driver);
1185
1186 /*
1187  *              usb gadget ops
1188  */
1189 static int usbhsg_get_frame(struct usb_gadget *gadget)
1190 {
1191         struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
1192         struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
1193
1194         return usbhs_frame_get_num(priv);
1195 }
1196
1197 static struct usb_gadget_ops usbhsg_gadget_ops = {
1198         .get_frame              = usbhsg_get_frame,
1199 };
1200
1201 static int usbhsg_start(struct usbhs_priv *priv)
1202 {
1203         return usbhsg_try_start(priv, USBHSG_STATUS_STARTED);
1204 }
1205
1206 static int usbhsg_stop(struct usbhs_priv *priv)
1207 {
1208         return usbhsg_try_stop(priv, USBHSG_STATUS_STARTED);
1209 }
1210
1211 int __devinit usbhs_mod_gadget_probe(struct usbhs_priv *priv)
1212 {
1213         struct usbhsg_gpriv *gpriv;
1214         struct usbhsg_uep *uep;
1215         struct device *dev = usbhs_priv_to_dev(priv);
1216         int pipe_size = usbhs_get_dparam(priv, pipe_size);
1217         int i;
1218
1219         gpriv = kzalloc(sizeof(struct usbhsg_gpriv), GFP_KERNEL);
1220         if (!gpriv) {
1221                 dev_err(dev, "Could not allocate gadget priv\n");
1222                 return -ENOMEM;
1223         }
1224
1225         uep = kzalloc(sizeof(struct usbhsg_uep) * pipe_size, GFP_KERNEL);
1226         if (!uep) {
1227                 dev_err(dev, "Could not allocate ep\n");
1228                 goto usbhs_mod_gadget_probe_err_gpriv;
1229         }
1230
1231         /*
1232          * CAUTION
1233          *
1234          * There is no guarantee that it is possible to access usb module here.
1235          * Don't accesses to it.
1236          * The accesse will be enable after "usbhsg_start"
1237          */
1238
1239         /*
1240          * register itself
1241          */
1242         usbhs_mod_register(priv, &gpriv->mod, USBHS_GADGET);
1243
1244         /* init gpriv */
1245         gpriv->mod.name         = "gadget";
1246         gpriv->mod.start        = usbhsg_start;
1247         gpriv->mod.stop         = usbhsg_stop;
1248         gpriv->uep              = uep;
1249         gpriv->uep_size         = pipe_size;
1250         usbhsg_status_init(gpriv);
1251
1252         /*
1253          * init gadget
1254          */
1255         device_initialize(&gpriv->gadget.dev);
1256         dev_set_name(&gpriv->gadget.dev, "gadget");
1257         gpriv->gadget.dev.parent        = dev;
1258         gpriv->gadget.name              = "renesas_usbhs_udc";
1259         gpriv->gadget.ops               = &usbhsg_gadget_ops;
1260         gpriv->gadget.is_dualspeed      = 1;
1261
1262         INIT_LIST_HEAD(&gpriv->gadget.ep_list);
1263
1264         /*
1265          * init usb_ep
1266          */
1267         usbhsg_for_each_uep_with_dcp(uep, gpriv, i) {
1268                 uep->gpriv      = gpriv;
1269                 snprintf(uep->ep_name, EP_NAME_SIZE, "ep%d", i);
1270
1271                 uep->ep.name            = uep->ep_name;
1272                 uep->ep.ops             = &usbhsg_ep_ops;
1273                 INIT_LIST_HEAD(&uep->ep.ep_list);
1274
1275                 /* init DCP */
1276                 if (usbhsg_is_dcp(uep)) {
1277                         gpriv->gadget.ep0 = &uep->ep;
1278                         uep->ep.maxpacket = 64;
1279                 }
1280                 /* init normal pipe */
1281                 else {
1282                         uep->ep.maxpacket = 512;
1283                         list_add_tail(&uep->ep.ep_list, &gpriv->gadget.ep_list);
1284                 }
1285         }
1286
1287         the_controller = gpriv;
1288
1289         dev_info(dev, "gadget probed\n");
1290
1291         return 0;
1292
1293 usbhs_mod_gadget_probe_err_gpriv:
1294         kfree(gpriv);
1295
1296         return -ENOMEM;
1297 }
1298
1299 void __devexit usbhs_mod_gadget_remove(struct usbhs_priv *priv)
1300 {
1301         struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
1302
1303         kfree(gpriv);
1304 }