4 * Copyright (C) 2011 Renesas Solutions Corp.
5 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
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.
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
18 #include <linux/module.h>
19 #include <linux/platform_device.h>
20 #include <linux/usb/ch9.h>
21 #include <linux/usb/gadget.h>
27 struct usbhsg_request {
28 struct usb_request req;
32 #define EP_NAME_SIZE 8
34 struct usbhsg_pipe_handle;
37 struct usbhs_pipe *pipe;
39 char ep_name[EP_NAME_SIZE];
41 struct usbhsg_gpriv *gpriv;
42 struct usbhsg_pipe_handle *handler;
46 struct usb_gadget gadget;
49 struct usbhsg_uep *uep;
52 struct usb_gadget_driver *driver;
55 #define USBHSG_STATUS_STARTED (1 << 0)
56 #define USBHSG_STATUS_REGISTERD (1 << 1)
57 #define USBHSG_STATUS_WEDGE (1 << 2)
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);
65 struct usbhsg_recip_handle {
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);
78 #define usbhsg_priv_to_gpriv(priv) \
80 usbhs_mod_get(priv, USBHS_GADGET), \
81 struct usbhsg_gpriv, mod)
83 #define __usbhsg_for_each_uep(start, pos, g, i) \
84 for (i = start, pos = (g)->uep; \
86 i++, pos = (g)->uep + i)
88 #define usbhsg_for_each_uep(pos, gpriv, i) \
89 __usbhsg_for_each_uep(1, pos, gpriv, i)
91 #define usbhsg_for_each_uep_with_dcp(pos, gpriv, i) \
92 __usbhsg_for_each_uep(0, pos, gpriv, i)
94 #define usbhsg_gadget_to_gpriv(g)\
95 container_of(g, struct usbhsg_gpriv, gadget)
97 #define usbhsg_req_to_ureq(r)\
98 container_of(r, struct usbhsg_request, req)
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))
111 #define usbhsg_ureq_to_pkt(u) (&(u)->pkt)
112 #define usbhsg_pkt_to_ureq(i) \
113 container_of(i, struct usbhsg_request, pkt)
115 #define usbhsg_is_not_connected(gp) ((gp)->gadget.speed == USB_SPEED_UNKNOWN)
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)
126 * This driver don't use spin_try_lock
127 * to avoid warning of CONFIG_DEBUG_SPINLOCK
129 static spinlock_t *usbhsg_trylock(struct usbhsg_gpriv *gpriv,
130 unsigned long *flags)
132 spinlock_t *lock = usbhsg_gpriv_to_lock(gpriv);
134 /* check spin lock status
135 * to avoid deadlock/nest */
136 if (spin_is_locked(lock))
139 spin_lock_irqsave(lock, *flags);
144 static void usbhsg_unlock(spinlock_t *lock, unsigned long *flags)
149 spin_unlock_irqrestore(lock, *flags);
155 static void usbhsg_queue_push(struct usbhsg_uep *uep,
156 struct usbhsg_request *ureq)
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;
165 ********* assume under spin lock *********
167 usbhs_pkt_push(pipe, pkt, req->buf, req->length, req->zero);
169 req->status = -EINPROGRESS;
171 dev_dbg(dev, "pipe %d : queue push (%d)\n",
172 usbhs_pipe_number(pipe),
176 static struct usbhsg_request *usbhsg_queue_get(struct usbhsg_uep *uep)
178 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
179 struct usbhs_pkt *pkt = usbhs_pkt_get(pipe);
182 ********* assume under spin lock *********
187 return usbhsg_pkt_to_ureq(pkt);
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)
194 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
195 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
196 struct usbhsg_request *ureq;
202 dev_err(dev, "no handler function\n");
207 * CAUTION [*queue handler*]
209 * This function will be called for start/restart queue operation.
210 * OTOH the most much worry for USB driver is spinlock nest.
212 * - usb_ep_ops :: queue
213 * - usb_request :: complete
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.
220 * CAUTION [*endpoint queue*]
221 * CAUTION [*request complete*]
224 /****************** spin try lock *******************/
225 lock = usbhsg_trylock(gpriv, &flags);
227 ureq = usbhsg_queue_get(uep);
230 ret = uep->handler->prepare(uep, ureq);
232 ret = uep->handler->try_run(uep, ureq);
234 usbhsg_unlock(lock, &flags);
235 /******************** spin unlock ******************/
240 static void usbhsg_queue_pop(struct usbhsg_uep *uep,
241 struct usbhsg_request *ureq,
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);
250 ********* assume under spin lock *********
254 * CAUTION [*request complete*]
256 * There is a possibility not to be called in correct order
257 * if "complete" is called without spinlock.
259 * So, this function assume it is under spinlock,
260 * and call usb_request :: complete.
262 * But this "complete" will push next usb_request.
263 * It mean "usb_ep_ops :: queue" which is using spinlock is called
266 * To avoid dead-lock, this driver is using usbhsg_trylock.
267 * CAUTION [*endpoint queue*]
268 * CAUTION [*queue handler*]
271 dev_dbg(dev, "pipe %d : queue pop\n", usbhs_pipe_number(pipe));
275 ureq->req.status = status;
276 ureq->req.complete(&uep->ep, &ureq->req);
280 usbhsg_queue_prepare(uep);
284 * irq enable/disable function
286 #define usbhsg_irq_callback_ctrl(uep, status, enable) \
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); \
295 mod->irq_##status |= (1 << usbhs_pipe_number(pipe)); \
297 mod->irq_##status &= ~(1 << usbhs_pipe_number(pipe)); \
298 usbhs_irq_callback_update(priv, mod); \
301 static void usbhsg_irq_empty_ctrl(struct usbhsg_uep *uep, int enable)
303 usbhsg_irq_callback_ctrl(uep, bempsts, enable);
306 static void usbhsg_irq_ready_ctrl(struct usbhsg_uep *uep, int enable)
308 usbhsg_irq_callback_ctrl(uep, brdysts, enable);
314 static int usbhsg_try_run_ctrl_stage_end(struct usbhsg_uep *uep,
315 struct usbhsg_request *ureq)
317 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
320 ********* assume under spin lock *********
323 usbhs_dcp_control_transfer_done(pipe);
324 usbhsg_queue_pop(uep, ureq, 0);
332 static void usbhsg_send_packet_done(struct usbhs_pkt *pkt)
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);
338 ureq->req.actual = pkt->actual;
340 usbhsg_queue_pop(uep, ureq, 0);
343 static int usbhsg_try_run_send_packet(struct usbhsg_uep *uep,
344 struct usbhsg_request *ureq)
346 struct usbhs_pkt *pkt = usbhsg_ureq_to_pkt(ureq);
349 ********* assume under spin lock *********
352 usbhs_fifo_write(pkt);
357 static int usbhsg_prepare_send_packet(struct usbhsg_uep *uep,
358 struct usbhsg_request *ureq)
360 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
363 ********* assume under spin lock *********
366 usbhs_fifo_prepare_write(pipe);
367 usbhsg_try_run_send_packet(uep, ureq);
375 static void usbhsg_receive_packet_done(struct usbhs_pkt *pkt)
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);
381 ureq->req.actual = pkt->actual;
383 usbhsg_queue_pop(uep, ureq, 0);
386 static int usbhsg_try_run_receive_packet(struct usbhsg_uep *uep,
387 struct usbhsg_request *ureq)
389 struct usbhs_pkt *pkt = usbhsg_ureq_to_pkt(ureq);
392 ********* assume under spin lock *********
395 return usbhs_fifo_read(pkt);
398 static int usbhsg_prepare_receive_packet(struct usbhsg_uep *uep,
399 struct usbhsg_request *ureq)
401 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
404 ********* assume under spin lock *********
407 return usbhs_fifo_prepare_read(pipe);
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,
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,
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,
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,
431 * DCP pipe can NOT use "ready interrupt" for "send"
432 * it should use "empty" interrupt.
434 * "Operation" - "Interrupt Function" - "BRDY Interrupt"
436 * on the other hand, normal pipe can use "ready interrupt" for "send"
437 * even though it is single/double buffer
439 #define usbhsg_handler_send_ctrl usbhsg_handler_send_by_empty
440 #define usbhsg_handler_recv_ctrl usbhsg_handler_recv_by_ready
442 #define usbhsg_handler_send_packet usbhsg_handler_send_by_ready
443 #define usbhsg_handler_recv_packet usbhsg_handler_recv_by_ready
446 * USB_TYPE_STANDARD / clear feature functions
448 static int usbhsg_recip_handler_std_control_done(struct usbhs_priv *priv,
449 struct usbhsg_uep *uep,
450 struct usb_ctrlrequest *ctrl)
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);
456 usbhs_dcp_control_transfer_done(pipe);
461 static int usbhsg_recip_handler_std_clear_endpoint(struct usbhs_priv *priv,
462 struct usbhsg_uep *uep,
463 struct usb_ctrlrequest *ctrl)
465 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
466 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
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);
474 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
476 usbhsg_queue_prepare(uep);
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,
491 static int usbhsg_recip_run_handle(struct usbhs_priv *priv,
492 struct usbhsg_recip_handle *handler,
493 struct usb_ctrlrequest *ctrl)
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;
501 int (*func)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
502 struct usb_ctrlrequest *ctrl);
505 uep = usbhsg_gpriv_to_nth_uep(gpriv, nth);
506 if (!usbhsg_uep_to_pipe(uep)) {
507 dev_err(dev, "wrong recip request\n");
512 case USB_RECIP_DEVICE:
514 func = handler->device;
516 case USB_RECIP_INTERFACE:
518 func = handler->interface;
520 case USB_RECIP_ENDPOINT:
522 func = handler->endpoint;
525 dev_warn(dev, "unsupported RECIP(%d)\n", recip);
531 dev_dbg(dev, "%s (pipe %d :%s)\n", handler->name, nth, msg);
532 ret = func(priv, uep, ctrl);
541 * it will be called from usbhs_interrupt
543 static int usbhsg_irq_dev_state(struct usbhs_priv *priv,
544 struct usbhs_irq_state *irq_state)
546 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
547 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
549 gpriv->gadget.speed = usbhs_status_get_usb_speed(irq_state);
551 dev_dbg(dev, "state = %x : speed : %d\n",
552 usbhs_status_get_device_state(irq_state),
553 gpriv->gadget.speed);
558 static int usbhsg_irq_ctrl_stage(struct usbhs_priv *priv,
559 struct usbhs_irq_state *irq_state)
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);
570 dev_dbg(dev, "stage = %d\n", stage);
576 * - "Interrupt Function"
577 * - "Control Transfer Stage Transition Interrupt"
578 * - Fig. "Control Transfer Stage Transitions"
582 case READ_DATA_STAGE:
583 dcp->handler = &usbhsg_handler_send_ctrl;
585 case WRITE_DATA_STAGE:
586 dcp->handler = &usbhsg_handler_recv_ctrl;
588 case NODATA_STATUS_STAGE:
589 dcp->handler = &usbhsg_handler_ctrl_stage_end;
598 usbhs_usbreq_get_val(priv, &ctrl);
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;
610 * setup stage / run recip
613 ret = usbhsg_recip_run_handle(priv, recip_handler, &ctrl);
615 ret = gpriv->driver->setup(&gpriv->gadget, &ctrl);
618 usbhs_pipe_stall(pipe);
623 static int usbhsg_irq_empty(struct usbhs_priv *priv,
624 struct usbhs_irq_state *irq_state)
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);
632 if (!irq_state->bempsts) {
633 dev_err(dev, "debug %s !!\n", __func__);
637 dev_dbg(dev, "irq empty [0x%04x]\n", irq_state->bempsts);
640 * search interrupted "pipe"
643 usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
644 if (!(irq_state->bempsts & (1 << i)))
647 uep = usbhsg_pipe_to_uep(pipe);
648 ret = usbhsg_queue_handle(uep);
650 dev_err(dev, "send error %d : %d\n", i, ret);
656 static int usbhsg_irq_ready(struct usbhs_priv *priv,
657 struct usbhs_irq_state *irq_state)
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);
665 if (!irq_state->brdysts) {
666 dev_err(dev, "debug %s !!\n", __func__);
670 dev_dbg(dev, "irq ready [0x%04x]\n", irq_state->brdysts);
673 * search interrupted "pipe"
676 usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
677 if (!(irq_state->brdysts & (1 << i)))
680 uep = usbhsg_pipe_to_uep(pipe);
681 ret = usbhsg_queue_handle(uep);
683 dev_err(dev, "receive error %d : %d\n", i, ret);
694 static int usbhsg_dcp_enable(struct usbhsg_uep *uep)
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;
701 ********* assume under spin lock *********
704 pipe = usbhs_dcp_malloc(priv);
709 uep->pipe->mod_private = uep;
714 #define usbhsg_dcp_disable usbhsg_pipe_disable
715 static int usbhsg_pipe_disable(struct usbhsg_uep *uep)
717 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
718 struct usbhsg_request *ureq;
722 ********* assume under spin lock *********
725 usbhs_pipe_disable(pipe);
730 usbhsg_irq_empty_ctrl(uep, disable);
731 usbhsg_irq_ready_ctrl(uep, disable);
734 ureq = usbhsg_queue_get(uep);
738 usbhsg_queue_pop(uep, ureq, -ECONNRESET);
744 static void usbhsg_uep_init(struct usbhsg_gpriv *gpriv)
747 struct usbhsg_uep *uep;
749 usbhsg_for_each_uep_with_dcp(uep, gpriv, i)
758 static int usbhsg_ep_enable(struct usb_ep *ep,
759 const struct usb_endpoint_descriptor *desc)
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;
770 * if it already have pipe,
776 /******************** spin lock ********************/
777 lock = usbhsg_trylock(gpriv, &flags);
779 pipe = usbhs_pipe_malloc(priv, desc);
782 pipe->mod_private = uep;
784 if (usb_endpoint_dir_in(desc))
785 uep->handler = &usbhsg_handler_send_packet;
787 uep->handler = &usbhsg_handler_recv_packet;
792 usbhsg_unlock(lock, &flags);
793 /******************** spin unlock ******************/
798 static int usbhsg_ep_disable(struct usb_ep *ep)
800 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
801 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
806 /******************** spin lock ********************/
807 lock = usbhsg_trylock(gpriv, &flags);
809 ret = usbhsg_pipe_disable(uep);
811 usbhsg_unlock(lock, &flags);
812 /******************** spin unlock ******************/
817 static struct usb_request *usbhsg_ep_alloc_request(struct usb_ep *ep,
820 struct usbhsg_request *ureq;
822 ureq = kzalloc(sizeof *ureq, gfp_flags);
826 usbhs_pkt_init(usbhsg_ureq_to_pkt(ureq));
831 static void usbhsg_ep_free_request(struct usb_ep *ep,
832 struct usb_request *req)
834 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
836 WARN_ON(!list_empty(&ureq->pkt.node));
840 static int usbhsg_ep_queue(struct usb_ep *ep, struct usb_request *req,
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);
852 * CAUTION [*endpoint queue*]
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.
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.
864 * CAUTION [*queue handler*]
865 * CAUTION [*request complete*]
868 /******************** spin lock ********************/
869 lock = usbhsg_trylock(gpriv, &flags);
872 if (usbhsg_is_not_connected(gpriv) ||
873 unlikely(!gpriv->driver) ||
877 usbhsg_queue_push(uep, ureq);
879 usbhsg_unlock(lock, &flags);
880 /******************** spin unlock ******************/
882 usbhsg_queue_prepare(uep);
887 static int usbhsg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
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);
897 * CAUTION [*queue handler*]
898 * CAUTION [*endpoint queue*]
899 * CAUTION [*request complete*]
902 /******************** spin lock ********************/
903 lock = usbhsg_trylock(gpriv, &flags);
905 usbhsg_queue_pop(uep, ureq, -ECONNRESET);
907 usbhsg_unlock(lock, &flags);
908 /******************** spin unlock ******************/
913 static int __usbhsg_ep_set_halt_wedge(struct usb_ep *ep, int halt, int wedge)
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);
925 * CAUTION [*queue handler*]
926 * CAUTION [*endpoint queue*]
927 * CAUTION [*request complete*]
930 /******************** spin lock ********************/
931 lock = usbhsg_trylock(gpriv, &flags);
932 if (!usbhsg_queue_get(uep)) {
934 dev_dbg(dev, "set halt %d (pipe %d)\n",
935 halt, usbhs_pipe_number(pipe));
938 usbhs_pipe_stall(pipe);
940 usbhs_pipe_disable(pipe);
943 usbhsg_status_set(gpriv, USBHSG_STATUS_WEDGE);
945 usbhsg_status_clr(gpriv, USBHSG_STATUS_WEDGE);
950 usbhsg_unlock(lock, &flags);
951 /******************** spin unlock ******************/
956 static int usbhsg_ep_set_halt(struct usb_ep *ep, int value)
958 return __usbhsg_ep_set_halt_wedge(ep, value, 0);
961 static int usbhsg_ep_set_wedge(struct usb_ep *ep)
963 return __usbhsg_ep_set_halt_wedge(ep, 1, 1);
966 static struct usb_ep_ops usbhsg_ep_ops = {
967 .enable = usbhsg_ep_enable,
968 .disable = usbhsg_ep_disable,
970 .alloc_request = usbhsg_ep_alloc_request,
971 .free_request = usbhsg_ep_free_request,
973 .queue = usbhsg_ep_queue,
974 .dequeue = usbhsg_ep_dequeue,
976 .set_halt = usbhsg_ep_set_halt,
977 .set_wedge = usbhsg_ep_set_wedge,
981 * usb module start/end
983 static int usbhsg_try_start(struct usbhs_priv *priv, u32 status)
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);
992 /******************** spin lock ********************/
993 lock = usbhsg_trylock(gpriv, &flags);
996 * enable interrupt and systems if ready
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;
1003 dev_dbg(dev, "start gadget\n");
1006 * pipe initialize and enable DCP
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);
1015 * system config enble
1020 usbhs_sys_hispeed_ctrl(priv, 1);
1021 usbhs_sys_function_ctrl(priv, 1);
1022 usbhs_sys_usb_ctrl(priv, 1);
1025 * enable irq callback
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);
1035 usbhsg_try_start_unlock:
1036 usbhsg_unlock(lock, &flags);
1037 /******************** spin unlock ********************/
1042 static int usbhsg_try_stop(struct usbhs_priv *priv, u32 status)
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);
1049 unsigned long flags;
1051 /******************** spin lock ********************/
1052 lock = usbhsg_trylock(gpriv, &flags);
1055 * disable interrupt and systems if 1st try
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;
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);
1071 usbhsg_dcp_disable(dcp);
1073 gpriv->gadget.speed = USB_SPEED_UNKNOWN;
1076 usbhs_sys_hispeed_ctrl(priv, 0);
1077 usbhs_sys_function_ctrl(priv, 0);
1078 usbhs_sys_usb_ctrl(priv, 0);
1080 usbhsg_unlock(lock, &flags);
1081 /******************** spin unlock ********************/
1083 if (gpriv->driver &&
1084 gpriv->driver->disconnect)
1085 gpriv->driver->disconnect(&gpriv->gadget);
1087 dev_dbg(dev, "stop gadget\n");
1091 usbhsg_try_stop_unlock:
1092 usbhsg_unlock(lock, &flags);
1099 * linux usb function
1102 struct usbhsg_gpriv *the_controller;
1103 int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
1104 int (*bind)(struct usb_gadget *))
1106 struct usbhsg_gpriv *gpriv = the_controller;
1107 struct usbhs_priv *priv;
1114 driver->speed != USB_SPEED_HIGH)
1121 dev = usbhsg_gpriv_to_dev(gpriv);
1122 priv = usbhsg_gpriv_to_priv(gpriv);
1124 /* first hook up the driver ... */
1125 gpriv->driver = driver;
1126 gpriv->gadget.dev.driver = &driver->driver;
1128 ret = device_add(&gpriv->gadget.dev);
1130 dev_err(dev, "device_add error %d\n", ret);
1134 ret = bind(&gpriv->gadget);
1136 dev_err(dev, "bind to driver %s error %d\n",
1137 driver->driver.name, ret);
1141 dev_dbg(dev, "bind %s\n", driver->driver.name);
1143 return usbhsg_try_start(priv, USBHSG_STATUS_REGISTERD);
1146 device_del(&gpriv->gadget.dev);
1148 gpriv->driver = NULL;
1149 gpriv->gadget.dev.driver = NULL;
1153 EXPORT_SYMBOL(usb_gadget_probe_driver);
1155 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1157 struct usbhsg_gpriv *gpriv = the_controller;
1158 struct usbhs_priv *priv;
1159 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
1166 driver != gpriv->driver)
1169 dev = usbhsg_gpriv_to_dev(gpriv);
1170 priv = usbhsg_gpriv_to_priv(gpriv);
1172 usbhsg_try_stop(priv, USBHSG_STATUS_REGISTERD);
1173 device_del(&gpriv->gadget.dev);
1174 gpriv->driver = NULL;
1176 if (driver->disconnect)
1177 driver->disconnect(&gpriv->gadget);
1179 driver->unbind(&gpriv->gadget);
1180 dev_dbg(dev, "unbind %s\n", driver->driver.name);
1184 EXPORT_SYMBOL(usb_gadget_unregister_driver);
1189 static int usbhsg_get_frame(struct usb_gadget *gadget)
1191 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
1192 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
1194 return usbhs_frame_get_num(priv);
1197 static struct usb_gadget_ops usbhsg_gadget_ops = {
1198 .get_frame = usbhsg_get_frame,
1201 static int usbhsg_start(struct usbhs_priv *priv)
1203 return usbhsg_try_start(priv, USBHSG_STATUS_STARTED);
1206 static int usbhsg_stop(struct usbhs_priv *priv)
1208 return usbhsg_try_stop(priv, USBHSG_STATUS_STARTED);
1211 int __devinit usbhs_mod_gadget_probe(struct usbhs_priv *priv)
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);
1219 gpriv = kzalloc(sizeof(struct usbhsg_gpriv), GFP_KERNEL);
1221 dev_err(dev, "Could not allocate gadget priv\n");
1225 uep = kzalloc(sizeof(struct usbhsg_uep) * pipe_size, GFP_KERNEL);
1227 dev_err(dev, "Could not allocate ep\n");
1228 goto usbhs_mod_gadget_probe_err_gpriv;
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"
1242 usbhs_mod_register(priv, &gpriv->mod, USBHS_GADGET);
1245 gpriv->mod.name = "gadget";
1246 gpriv->mod.start = usbhsg_start;
1247 gpriv->mod.stop = usbhsg_stop;
1249 gpriv->uep_size = pipe_size;
1250 usbhsg_status_init(gpriv);
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;
1262 INIT_LIST_HEAD(&gpriv->gadget.ep_list);
1267 usbhsg_for_each_uep_with_dcp(uep, gpriv, i) {
1269 snprintf(uep->ep_name, EP_NAME_SIZE, "ep%d", i);
1271 uep->ep.name = uep->ep_name;
1272 uep->ep.ops = &usbhsg_ep_ops;
1273 INIT_LIST_HEAD(&uep->ep.ep_list);
1276 if (usbhsg_is_dcp(uep)) {
1277 gpriv->gadget.ep0 = &uep->ep;
1278 uep->ep.maxpacket = 64;
1280 /* init normal pipe */
1282 uep->ep.maxpacket = 512;
1283 list_add_tail(&uep->ep.ep_list, &gpriv->gadget.ep_list);
1287 the_controller = gpriv;
1289 dev_info(dev, "gadget probed\n");
1293 usbhs_mod_gadget_probe_err_gpriv:
1299 void __devexit usbhs_mod_gadget_remove(struct usbhs_priv *priv)
1301 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);