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;
29 struct list_head node;
33 #define EP_NAME_SIZE 8
35 struct usbhsg_pipe_handle;
38 struct usbhs_pipe *pipe;
39 struct list_head list;
41 char ep_name[EP_NAME_SIZE];
43 struct usbhsg_gpriv *gpriv;
44 struct usbhsg_pipe_handle *handler;
48 struct usb_gadget gadget;
51 struct usbhsg_uep *uep;
54 struct usb_gadget_driver *driver;
57 #define USBHSG_STATUS_STARTED (1 << 0)
58 #define USBHSG_STATUS_REGISTERD (1 << 1)
59 #define USBHSG_STATUS_WEDGE (1 << 2)
62 struct usbhsg_pipe_handle {
63 int (*prepare)(struct usbhsg_uep *uep, struct usbhsg_request *ureq);
64 int (*try_run)(struct usbhsg_uep *uep, struct usbhsg_request *ureq);
65 void (*irq_mask)(struct usbhsg_uep *uep, int enable);
68 struct usbhsg_recip_handle {
70 int (*device)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
71 struct usb_ctrlrequest *ctrl);
72 int (*interface)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
73 struct usb_ctrlrequest *ctrl);
74 int (*endpoint)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
75 struct usb_ctrlrequest *ctrl);
81 #define usbhsg_priv_to_gpriv(priv) \
83 usbhs_mod_get(priv, USBHS_GADGET), \
84 struct usbhsg_gpriv, mod)
86 #define __usbhsg_for_each_uep(start, pos, g, i) \
87 for (i = start, pos = (g)->uep; \
89 i++, pos = (g)->uep + i)
91 #define usbhsg_for_each_uep(pos, gpriv, i) \
92 __usbhsg_for_each_uep(1, pos, gpriv, i)
94 #define usbhsg_for_each_uep_with_dcp(pos, gpriv, i) \
95 __usbhsg_for_each_uep(0, pos, gpriv, i)
97 #define usbhsg_gadget_to_gpriv(g)\
98 container_of(g, struct usbhsg_gpriv, gadget)
100 #define usbhsg_req_to_ureq(r)\
101 container_of(r, struct usbhsg_request, req)
103 #define usbhsg_ep_to_uep(e) container_of(e, struct usbhsg_uep, ep)
104 #define usbhsg_gpriv_to_lock(gp) usbhs_priv_to_lock((gp)->mod.priv)
105 #define usbhsg_gpriv_to_dev(gp) usbhs_priv_to_dev((gp)->mod.priv)
106 #define usbhsg_gpriv_to_priv(gp) ((gp)->mod.priv)
107 #define usbhsg_gpriv_to_dcp(gp) ((gp)->uep)
108 #define usbhsg_gpriv_to_nth_uep(gp, i) ((gp)->uep + i)
109 #define usbhsg_uep_to_gpriv(u) ((u)->gpriv)
110 #define usbhsg_uep_to_pipe(u) ((u)->pipe)
111 #define usbhsg_pipe_to_uep(p) ((p)->mod_private)
112 #define usbhsg_is_dcp(u) ((u) == usbhsg_gpriv_to_dcp((u)->gpriv))
114 #define usbhsg_ureq_to_pkt(u) (&(u)->pkt)
115 #define usbhsg_pkt_to_ureq(i) \
116 container_of(i, struct usbhsg_request, pkt)
118 #define usbhsg_is_not_connected(gp) ((gp)->gadget.speed == USB_SPEED_UNKNOWN)
121 #define usbhsg_status_init(gp) do {(gp)->status = 0; } while (0)
122 #define usbhsg_status_set(gp, b) (gp->status |= b)
123 #define usbhsg_status_clr(gp, b) (gp->status &= ~b)
124 #define usbhsg_status_has(gp, b) (gp->status & b)
129 * This driver don't use spin_try_lock
130 * to avoid warning of CONFIG_DEBUG_SPINLOCK
132 static spinlock_t *usbhsg_trylock(struct usbhsg_gpriv *gpriv,
133 unsigned long *flags)
135 spinlock_t *lock = usbhsg_gpriv_to_lock(gpriv);
137 /* check spin lock status
138 * to avoid deadlock/nest */
139 if (spin_is_locked(lock))
142 spin_lock_irqsave(lock, *flags);
147 static void usbhsg_unlock(spinlock_t *lock, unsigned long *flags)
152 spin_unlock_irqrestore(lock, *flags);
158 static void usbhsg_queue_push(struct usbhsg_uep *uep,
159 struct usbhsg_request *ureq)
161 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
162 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
163 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
166 ********* assume under spin lock *********
168 list_del_init(&ureq->node);
169 list_add_tail(&ureq->node, &uep->list);
170 ureq->req.actual = 0;
171 ureq->req.status = -EINPROGRESS;
173 dev_dbg(dev, "pipe %d : queue push (%d)\n",
174 usbhs_pipe_number(pipe),
178 static struct usbhsg_request *usbhsg_queue_get(struct usbhsg_uep *uep)
181 ********* assume under spin lock *********
183 if (list_empty(&uep->list))
186 return list_entry(uep->list.next, struct usbhsg_request, node);
189 #define usbhsg_queue_prepare(uep) __usbhsg_queue_handler(uep, 1);
190 #define usbhsg_queue_handle(uep) __usbhsg_queue_handler(uep, 0);
191 static int __usbhsg_queue_handler(struct usbhsg_uep *uep, int prepare)
193 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
194 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
195 struct usbhsg_request *ureq;
201 dev_err(dev, "no handler function\n");
206 * CAUTION [*queue handler*]
208 * This function will be called for start/restart queue operation.
209 * OTOH the most much worry for USB driver is spinlock nest.
211 * - usb_ep_ops :: queue
212 * - usb_request :: complete
214 * But the caller of this function need not care about spinlock.
215 * This function is using usbhsg_trylock for it.
216 * if "is_locked" is 1, this mean this function lock it.
217 * but if it is 0, this mean it is already under spin lock.
219 * CAUTION [*endpoint queue*]
220 * CAUTION [*request complete*]
223 /****************** spin try lock *******************/
224 lock = usbhsg_trylock(gpriv, &flags);
226 ureq = usbhsg_queue_get(uep);
229 ret = uep->handler->prepare(uep, ureq);
231 ret = uep->handler->try_run(uep, ureq);
233 usbhsg_unlock(lock, &flags);
234 /******************** spin unlock ******************/
239 static void usbhsg_queue_pop(struct usbhsg_uep *uep,
240 struct usbhsg_request *ureq,
243 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
244 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
245 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
248 ********* assume under spin lock *********
252 * CAUTION [*request complete*]
254 * There is a possibility not to be called in correct order
255 * if "complete" is called without spinlock.
257 * So, this function assume it is under spinlock,
258 * and call usb_request :: complete.
260 * But this "complete" will push next usb_request.
261 * It mean "usb_ep_ops :: queue" which is using spinlock is called
264 * To avoid dead-lock, this driver is using usbhsg_trylock.
265 * CAUTION [*endpoint queue*]
266 * CAUTION [*queue handler*]
269 dev_dbg(dev, "pipe %d : queue pop\n", usbhs_pipe_number(pipe));
271 list_del_init(&ureq->node);
273 ureq->req.status = status;
274 ureq->req.complete(&uep->ep, &ureq->req);
278 usbhsg_queue_prepare(uep);
282 * irq enable/disable function
284 #define usbhsg_irq_callback_ctrl(uep, status, enable) \
286 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep); \
287 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep); \
288 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv); \
289 struct usbhs_mod *mod = usbhs_mod_get_current(priv); \
293 mod->irq_##status |= (1 << usbhs_pipe_number(pipe)); \
295 mod->irq_##status &= ~(1 << usbhs_pipe_number(pipe)); \
296 usbhs_irq_callback_update(priv, mod); \
299 static void usbhsg_irq_empty_ctrl(struct usbhsg_uep *uep, int enable)
301 usbhsg_irq_callback_ctrl(uep, bempsts, enable);
304 static void usbhsg_irq_ready_ctrl(struct usbhsg_uep *uep, int enable)
306 usbhsg_irq_callback_ctrl(uep, brdysts, enable);
312 static int usbhsg_try_run_ctrl_stage_end(struct usbhsg_uep *uep,
313 struct usbhsg_request *ureq)
315 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
318 ********* assume under spin lock *********
321 usbhs_dcp_control_transfer_done(pipe);
322 usbhsg_queue_pop(uep, ureq, 0);
330 static void usbhsg_try_run_send_packet_bh(struct usbhs_pkt *pkt)
332 struct usbhs_pipe *pipe = pkt->pipe;
333 struct usbhsg_uep *uep = usbhsg_pipe_to_uep(pipe);
334 struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt);
335 struct usb_request *req = &ureq->req;
336 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
337 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
338 int remainder, send, maxp;
344 remainder = pkt->length;
347 * send = 0 : send zero packet
348 * send > 0 : send data
354 /* send all packet ? */
355 if (send < remainder)
356 is_done = 0; /* there are remainder data */
357 else if (send < maxp)
358 is_done = 1; /* short packet */
360 is_done = !req->zero; /* send zero packet ? */
362 dev_dbg(dev, " send %d (%d/ %d/ %d/ %d)\n",
363 usbhs_pipe_number(pipe),
364 remainder, send, is_done, req->zero);
367 * enable interrupt and send again in irq handler
368 * if it still have remainder data which should be sent.
371 uep->handler->irq_mask(uep, enable);
374 * all data were sent ?
377 /* it care below call in
379 if (usbhsg_is_dcp(uep))
380 usbhs_dcp_control_transfer_done(pipe);
382 usbhsg_queue_pop(uep, ureq, 0);
386 static int usbhsg_try_run_send_packet(struct usbhsg_uep *uep,
387 struct usbhsg_request *ureq)
389 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
390 struct usb_request *req = &ureq->req;
391 struct usbhs_pkt *pkt = usbhsg_ureq_to_pkt(ureq);
395 ********* assume under spin lock *********
398 usbhs_pkt_update(pkt, pipe,
399 req->buf + req->actual,
400 req->length - req->actual);
402 ret = usbhs_fifo_write(pkt);
405 * retry in interrupt */
406 uep->handler->irq_mask(uep, 1);
412 static int usbhsg_prepare_send_packet(struct usbhsg_uep *uep,
413 struct usbhsg_request *ureq)
415 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
418 ********* assume under spin lock *********
421 usbhs_fifo_prepare_write(pipe);
422 usbhsg_try_run_send_packet(uep, ureq);
430 static void usbhsg_try_run_receive_packet_bh(struct usbhs_pkt *pkt)
432 struct usbhs_pipe *pipe = pkt->pipe;
433 struct usbhsg_uep *uep = usbhsg_pipe_to_uep(pipe);
434 struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt);
435 struct usb_request *req = &ureq->req;
436 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
437 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
438 int remainder, recv, maxp;
442 remainder = pkt->length;
446 * recv < 0 : pipe busy
447 * recv >= 0 : receive data
452 /* update parameters */
455 if ((recv == remainder) || /* receive all data */
456 (recv < maxp)) /* short packet */
459 dev_dbg(dev, " recv %d (%d/ %d/ %d/ %d)\n",
460 usbhs_pipe_number(pipe),
461 remainder, recv, is_done, req->zero);
463 /* read all data ? */
467 uep->handler->irq_mask(uep, disable);
468 usbhs_pipe_disable(pipe);
469 usbhsg_queue_pop(uep, ureq, 0);
473 static int usbhsg_try_run_receive_packet(struct usbhsg_uep *uep,
474 struct usbhsg_request *ureq)
476 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
477 struct usb_request *req = &ureq->req;
478 struct usbhs_pkt *pkt = usbhsg_ureq_to_pkt(ureq);
481 ********* assume under spin lock *********
484 usbhs_pkt_update(pkt, pipe,
485 req->buf + req->actual,
486 req->length - req->actual);
488 return usbhs_fifo_read(pkt);
491 static int usbhsg_prepare_receive_packet(struct usbhsg_uep *uep,
492 struct usbhsg_request *ureq)
494 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
499 ********* assume under spin lock *********
502 ret = usbhs_fifo_prepare_read(pipe);
507 * data will be read in interrupt handler
509 uep->handler->irq_mask(uep, enable);
514 static struct usbhsg_pipe_handle usbhsg_handler_send_by_empty = {
515 .prepare = usbhsg_prepare_send_packet,
516 .try_run = usbhsg_try_run_send_packet,
517 .irq_mask = usbhsg_irq_empty_ctrl,
520 static struct usbhsg_pipe_handle usbhsg_handler_send_by_ready = {
521 .prepare = usbhsg_prepare_send_packet,
522 .try_run = usbhsg_try_run_send_packet,
523 .irq_mask = usbhsg_irq_ready_ctrl,
526 static struct usbhsg_pipe_handle usbhsg_handler_recv_by_ready = {
527 .prepare = usbhsg_prepare_receive_packet,
528 .try_run = usbhsg_try_run_receive_packet,
529 .irq_mask = usbhsg_irq_ready_ctrl,
532 static struct usbhsg_pipe_handle usbhsg_handler_ctrl_stage_end = {
533 .prepare = usbhsg_try_run_ctrl_stage_end,
534 .try_run = usbhsg_try_run_ctrl_stage_end,
538 * DCP pipe can NOT use "ready interrupt" for "send"
539 * it should use "empty" interrupt.
541 * "Operation" - "Interrupt Function" - "BRDY Interrupt"
543 * on the other hand, normal pipe can use "ready interrupt" for "send"
544 * even though it is single/double buffer
546 #define usbhsg_handler_send_ctrl usbhsg_handler_send_by_empty
547 #define usbhsg_handler_recv_ctrl usbhsg_handler_recv_by_ready
549 #define usbhsg_handler_send_packet usbhsg_handler_send_by_ready
550 #define usbhsg_handler_recv_packet usbhsg_handler_recv_by_ready
553 * USB_TYPE_STANDARD / clear feature functions
555 static int usbhsg_recip_handler_std_control_done(struct usbhs_priv *priv,
556 struct usbhsg_uep *uep,
557 struct usb_ctrlrequest *ctrl)
559 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
560 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
561 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
563 usbhs_dcp_control_transfer_done(pipe);
568 static int usbhsg_recip_handler_std_clear_endpoint(struct usbhs_priv *priv,
569 struct usbhsg_uep *uep,
570 struct usb_ctrlrequest *ctrl)
572 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
573 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
575 if (!usbhsg_status_has(gpriv, USBHSG_STATUS_WEDGE)) {
576 usbhs_pipe_disable(pipe);
577 usbhs_pipe_clear_sequence(pipe);
578 usbhs_pipe_enable(pipe);
581 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
583 usbhsg_queue_prepare(uep);
588 struct usbhsg_recip_handle req_clear_feature = {
589 .name = "clear feature",
590 .device = usbhsg_recip_handler_std_control_done,
591 .interface = usbhsg_recip_handler_std_control_done,
592 .endpoint = usbhsg_recip_handler_std_clear_endpoint,
598 static int usbhsg_recip_run_handle(struct usbhs_priv *priv,
599 struct usbhsg_recip_handle *handler,
600 struct usb_ctrlrequest *ctrl)
602 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
603 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
604 struct usbhsg_uep *uep;
605 int recip = ctrl->bRequestType & USB_RECIP_MASK;
606 int nth = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK;
608 int (*func)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
609 struct usb_ctrlrequest *ctrl);
612 uep = usbhsg_gpriv_to_nth_uep(gpriv, nth);
613 if (!usbhsg_uep_to_pipe(uep)) {
614 dev_err(dev, "wrong recip request\n");
619 case USB_RECIP_DEVICE:
621 func = handler->device;
623 case USB_RECIP_INTERFACE:
625 func = handler->interface;
627 case USB_RECIP_ENDPOINT:
629 func = handler->endpoint;
632 dev_warn(dev, "unsupported RECIP(%d)\n", recip);
638 dev_dbg(dev, "%s (pipe %d :%s)\n", handler->name, nth, msg);
639 ret = func(priv, uep, ctrl);
648 * it will be called from usbhs_interrupt
650 static int usbhsg_irq_dev_state(struct usbhs_priv *priv,
651 struct usbhs_irq_state *irq_state)
653 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
654 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
656 gpriv->gadget.speed = usbhs_status_get_usb_speed(irq_state);
658 dev_dbg(dev, "state = %x : speed : %d\n",
659 usbhs_status_get_device_state(irq_state),
660 gpriv->gadget.speed);
665 static int usbhsg_irq_ctrl_stage(struct usbhs_priv *priv,
666 struct usbhs_irq_state *irq_state)
668 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
669 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
670 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
671 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
672 struct usb_ctrlrequest ctrl;
673 struct usbhsg_recip_handle *recip_handler = NULL;
674 int stage = usbhs_status_get_ctrl_stage(irq_state);
677 dev_dbg(dev, "stage = %d\n", stage);
683 * - "Interrupt Function"
684 * - "Control Transfer Stage Transition Interrupt"
685 * - Fig. "Control Transfer Stage Transitions"
689 case READ_DATA_STAGE:
690 dcp->handler = &usbhsg_handler_send_ctrl;
692 case WRITE_DATA_STAGE:
693 dcp->handler = &usbhsg_handler_recv_ctrl;
695 case NODATA_STATUS_STAGE:
696 dcp->handler = &usbhsg_handler_ctrl_stage_end;
705 usbhs_usbreq_get_val(priv, &ctrl);
707 switch (ctrl.bRequestType & USB_TYPE_MASK) {
708 case USB_TYPE_STANDARD:
709 switch (ctrl.bRequest) {
710 case USB_REQ_CLEAR_FEATURE:
711 recip_handler = &req_clear_feature;
717 * setup stage / run recip
720 ret = usbhsg_recip_run_handle(priv, recip_handler, &ctrl);
722 ret = gpriv->driver->setup(&gpriv->gadget, &ctrl);
725 usbhs_pipe_stall(pipe);
730 static int usbhsg_irq_empty(struct usbhs_priv *priv,
731 struct usbhs_irq_state *irq_state)
733 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
734 struct usbhsg_uep *uep;
735 struct usbhs_pipe *pipe;
736 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
739 if (!irq_state->bempsts) {
740 dev_err(dev, "debug %s !!\n", __func__);
744 dev_dbg(dev, "irq empty [0x%04x]\n", irq_state->bempsts);
747 * search interrupted "pipe"
750 usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
751 if (!(irq_state->bempsts & (1 << i)))
754 uep = usbhsg_pipe_to_uep(pipe);
755 ret = usbhsg_queue_handle(uep);
757 dev_err(dev, "send error %d : %d\n", i, ret);
763 static int usbhsg_irq_ready(struct usbhs_priv *priv,
764 struct usbhs_irq_state *irq_state)
766 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
767 struct usbhsg_uep *uep;
768 struct usbhs_pipe *pipe;
769 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
772 if (!irq_state->brdysts) {
773 dev_err(dev, "debug %s !!\n", __func__);
777 dev_dbg(dev, "irq ready [0x%04x]\n", irq_state->brdysts);
780 * search interrupted "pipe"
783 usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
784 if (!(irq_state->brdysts & (1 << i)))
787 uep = usbhsg_pipe_to_uep(pipe);
788 ret = usbhsg_queue_handle(uep);
790 dev_err(dev, "receive error %d : %d\n", i, ret);
801 static int usbhsg_dcp_enable(struct usbhsg_uep *uep)
803 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
804 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
805 struct usbhs_pipe *pipe;
808 ********* assume under spin lock *********
811 pipe = usbhs_dcp_malloc(priv);
816 uep->pipe->mod_private = uep;
817 INIT_LIST_HEAD(&uep->list);
822 #define usbhsg_dcp_disable usbhsg_pipe_disable
823 static int usbhsg_pipe_disable(struct usbhsg_uep *uep)
825 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
826 struct usbhsg_request *ureq;
830 ********* assume under spin lock *********
833 usbhs_pipe_disable(pipe);
838 usbhsg_irq_empty_ctrl(uep, disable);
839 usbhsg_irq_ready_ctrl(uep, disable);
842 ureq = usbhsg_queue_get(uep);
846 usbhsg_queue_pop(uep, ureq, -ECONNRESET);
852 static void usbhsg_uep_init(struct usbhsg_gpriv *gpriv)
855 struct usbhsg_uep *uep;
857 usbhsg_for_each_uep_with_dcp(uep, gpriv, i)
866 static int usbhsg_ep_enable(struct usb_ep *ep,
867 const struct usb_endpoint_descriptor *desc)
869 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
870 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
871 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
872 struct usbhs_pipe *pipe;
878 * if it already have pipe,
884 /******************** spin lock ********************/
885 lock = usbhsg_trylock(gpriv, &flags);
887 pipe = usbhs_pipe_malloc(priv, desc);
890 pipe->mod_private = uep;
891 INIT_LIST_HEAD(&uep->list);
893 if (usb_endpoint_dir_in(desc))
894 uep->handler = &usbhsg_handler_send_packet;
896 uep->handler = &usbhsg_handler_recv_packet;
901 usbhsg_unlock(lock, &flags);
902 /******************** spin unlock ******************/
907 static int usbhsg_ep_disable(struct usb_ep *ep)
909 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
910 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
915 /******************** spin lock ********************/
916 lock = usbhsg_trylock(gpriv, &flags);
918 ret = usbhsg_pipe_disable(uep);
920 usbhsg_unlock(lock, &flags);
921 /******************** spin unlock ******************/
926 static struct usb_request *usbhsg_ep_alloc_request(struct usb_ep *ep,
929 struct usbhsg_request *ureq;
931 ureq = kzalloc(sizeof *ureq, gfp_flags);
935 INIT_LIST_HEAD(&ureq->node);
939 static void usbhsg_ep_free_request(struct usb_ep *ep,
940 struct usb_request *req)
942 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
944 WARN_ON(!list_empty(&ureq->node));
948 static int usbhsg_ep_queue(struct usb_ep *ep, struct usb_request *req,
951 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
952 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
953 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
954 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
960 * CAUTION [*endpoint queue*]
962 * This function will be called from usb_request :: complete
963 * or usb driver timing.
964 * If this function is called from usb_request :: complete,
965 * it is already under spinlock on this driver.
966 * but it is called frm usb driver, this function should call spinlock.
968 * This function is using usbshg_trylock to solve this issue.
969 * if "is_locked" is 1, this mean this function lock it.
970 * but if it is 0, this mean it is already under spin lock.
972 * CAUTION [*queue handler*]
973 * CAUTION [*request complete*]
976 /******************** spin lock ********************/
977 lock = usbhsg_trylock(gpriv, &flags);
980 if (usbhsg_is_not_connected(gpriv) ||
981 unlikely(!gpriv->driver) ||
985 usbhsg_queue_push(uep, ureq);
987 usbhsg_unlock(lock, &flags);
988 /******************** spin unlock ******************/
990 usbhsg_queue_prepare(uep);
995 static int usbhsg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
997 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
998 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
999 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
1001 unsigned long flags;
1005 * CAUTION [*queue handler*]
1006 * CAUTION [*endpoint queue*]
1007 * CAUTION [*request complete*]
1010 /******************** spin lock ********************/
1011 lock = usbhsg_trylock(gpriv, &flags);
1013 usbhsg_queue_pop(uep, ureq, -ECONNRESET);
1015 usbhsg_unlock(lock, &flags);
1016 /******************** spin unlock ******************/
1021 static int __usbhsg_ep_set_halt_wedge(struct usb_ep *ep, int halt, int wedge)
1023 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
1024 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
1025 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
1026 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
1028 unsigned long flags;
1033 * CAUTION [*queue handler*]
1034 * CAUTION [*endpoint queue*]
1035 * CAUTION [*request complete*]
1038 /******************** spin lock ********************/
1039 lock = usbhsg_trylock(gpriv, &flags);
1040 if (!usbhsg_queue_get(uep)) {
1042 dev_dbg(dev, "set halt %d (pipe %d)\n",
1043 halt, usbhs_pipe_number(pipe));
1046 usbhs_pipe_stall(pipe);
1048 usbhs_pipe_disable(pipe);
1051 usbhsg_status_set(gpriv, USBHSG_STATUS_WEDGE);
1053 usbhsg_status_clr(gpriv, USBHSG_STATUS_WEDGE);
1058 usbhsg_unlock(lock, &flags);
1059 /******************** spin unlock ******************/
1064 static int usbhsg_ep_set_halt(struct usb_ep *ep, int value)
1066 return __usbhsg_ep_set_halt_wedge(ep, value, 0);
1069 static int usbhsg_ep_set_wedge(struct usb_ep *ep)
1071 return __usbhsg_ep_set_halt_wedge(ep, 1, 1);
1074 static struct usb_ep_ops usbhsg_ep_ops = {
1075 .enable = usbhsg_ep_enable,
1076 .disable = usbhsg_ep_disable,
1078 .alloc_request = usbhsg_ep_alloc_request,
1079 .free_request = usbhsg_ep_free_request,
1081 .queue = usbhsg_ep_queue,
1082 .dequeue = usbhsg_ep_dequeue,
1084 .set_halt = usbhsg_ep_set_halt,
1085 .set_wedge = usbhsg_ep_set_wedge,
1089 * usb module start/end
1091 static int usbhsg_try_start(struct usbhs_priv *priv, u32 status)
1093 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
1094 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
1095 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
1096 struct device *dev = usbhs_priv_to_dev(priv);
1098 unsigned long flags;
1100 /******************** spin lock ********************/
1101 lock = usbhsg_trylock(gpriv, &flags);
1104 * enable interrupt and systems if ready
1106 usbhsg_status_set(gpriv, status);
1107 if (!(usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
1108 usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD)))
1109 goto usbhsg_try_start_unlock;
1111 dev_dbg(dev, "start gadget\n");
1114 * pipe initialize and enable DCP
1116 usbhs_pipe_init(priv,
1117 usbhsg_try_run_send_packet_bh,
1118 usbhsg_try_run_receive_packet_bh);
1119 usbhsg_uep_init(gpriv);
1120 usbhsg_dcp_enable(dcp);
1123 * system config enble
1128 usbhs_sys_hispeed_ctrl(priv, 1);
1129 usbhs_sys_function_ctrl(priv, 1);
1130 usbhs_sys_usb_ctrl(priv, 1);
1133 * enable irq callback
1135 mod->irq_dev_state = usbhsg_irq_dev_state;
1136 mod->irq_ctrl_stage = usbhsg_irq_ctrl_stage;
1137 mod->irq_empty = usbhsg_irq_empty;
1138 mod->irq_ready = usbhsg_irq_ready;
1139 mod->irq_bempsts = 0;
1140 mod->irq_brdysts = 0;
1141 usbhs_irq_callback_update(priv, mod);
1143 usbhsg_try_start_unlock:
1144 usbhsg_unlock(lock, &flags);
1145 /******************** spin unlock ********************/
1150 static int usbhsg_try_stop(struct usbhs_priv *priv, u32 status)
1152 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
1153 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
1154 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
1155 struct device *dev = usbhs_priv_to_dev(priv);
1157 unsigned long flags;
1159 /******************** spin lock ********************/
1160 lock = usbhsg_trylock(gpriv, &flags);
1163 * disable interrupt and systems if 1st try
1165 usbhsg_status_clr(gpriv, status);
1166 if (!usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
1167 !usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD))
1168 goto usbhsg_try_stop_unlock;
1170 /* disable all irq */
1171 mod->irq_dev_state = NULL;
1172 mod->irq_ctrl_stage = NULL;
1173 mod->irq_empty = NULL;
1174 mod->irq_ready = NULL;
1175 mod->irq_bempsts = 0;
1176 mod->irq_brdysts = 0;
1177 usbhs_irq_callback_update(priv, mod);
1179 usbhsg_dcp_disable(dcp);
1181 gpriv->gadget.speed = USB_SPEED_UNKNOWN;
1184 usbhs_sys_hispeed_ctrl(priv, 0);
1185 usbhs_sys_function_ctrl(priv, 0);
1186 usbhs_sys_usb_ctrl(priv, 0);
1188 usbhsg_unlock(lock, &flags);
1189 /******************** spin unlock ********************/
1191 if (gpriv->driver &&
1192 gpriv->driver->disconnect)
1193 gpriv->driver->disconnect(&gpriv->gadget);
1195 dev_dbg(dev, "stop gadget\n");
1199 usbhsg_try_stop_unlock:
1200 usbhsg_unlock(lock, &flags);
1207 * linux usb function
1210 struct usbhsg_gpriv *the_controller;
1211 int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
1212 int (*bind)(struct usb_gadget *))
1214 struct usbhsg_gpriv *gpriv = the_controller;
1215 struct usbhs_priv *priv;
1222 driver->speed != USB_SPEED_HIGH)
1229 dev = usbhsg_gpriv_to_dev(gpriv);
1230 priv = usbhsg_gpriv_to_priv(gpriv);
1232 /* first hook up the driver ... */
1233 gpriv->driver = driver;
1234 gpriv->gadget.dev.driver = &driver->driver;
1236 ret = device_add(&gpriv->gadget.dev);
1238 dev_err(dev, "device_add error %d\n", ret);
1242 ret = bind(&gpriv->gadget);
1244 dev_err(dev, "bind to driver %s error %d\n",
1245 driver->driver.name, ret);
1249 dev_dbg(dev, "bind %s\n", driver->driver.name);
1251 return usbhsg_try_start(priv, USBHSG_STATUS_REGISTERD);
1254 device_del(&gpriv->gadget.dev);
1256 gpriv->driver = NULL;
1257 gpriv->gadget.dev.driver = NULL;
1261 EXPORT_SYMBOL(usb_gadget_probe_driver);
1263 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1265 struct usbhsg_gpriv *gpriv = the_controller;
1266 struct usbhs_priv *priv;
1267 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
1274 driver != gpriv->driver)
1277 dev = usbhsg_gpriv_to_dev(gpriv);
1278 priv = usbhsg_gpriv_to_priv(gpriv);
1280 usbhsg_try_stop(priv, USBHSG_STATUS_REGISTERD);
1281 device_del(&gpriv->gadget.dev);
1282 gpriv->driver = NULL;
1284 if (driver->disconnect)
1285 driver->disconnect(&gpriv->gadget);
1287 driver->unbind(&gpriv->gadget);
1288 dev_dbg(dev, "unbind %s\n", driver->driver.name);
1292 EXPORT_SYMBOL(usb_gadget_unregister_driver);
1297 static int usbhsg_get_frame(struct usb_gadget *gadget)
1299 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
1300 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
1302 return usbhs_frame_get_num(priv);
1305 static struct usb_gadget_ops usbhsg_gadget_ops = {
1306 .get_frame = usbhsg_get_frame,
1309 static int usbhsg_start(struct usbhs_priv *priv)
1311 return usbhsg_try_start(priv, USBHSG_STATUS_STARTED);
1314 static int usbhsg_stop(struct usbhs_priv *priv)
1316 return usbhsg_try_stop(priv, USBHSG_STATUS_STARTED);
1319 int __devinit usbhs_mod_gadget_probe(struct usbhs_priv *priv)
1321 struct usbhsg_gpriv *gpriv;
1322 struct usbhsg_uep *uep;
1323 struct device *dev = usbhs_priv_to_dev(priv);
1324 int pipe_size = usbhs_get_dparam(priv, pipe_size);
1327 gpriv = kzalloc(sizeof(struct usbhsg_gpriv), GFP_KERNEL);
1329 dev_err(dev, "Could not allocate gadget priv\n");
1333 uep = kzalloc(sizeof(struct usbhsg_uep) * pipe_size, GFP_KERNEL);
1335 dev_err(dev, "Could not allocate ep\n");
1336 goto usbhs_mod_gadget_probe_err_gpriv;
1342 * There is no guarantee that it is possible to access usb module here.
1343 * Don't accesses to it.
1344 * The accesse will be enable after "usbhsg_start"
1350 usbhs_mod_register(priv, &gpriv->mod, USBHS_GADGET);
1353 gpriv->mod.name = "gadget";
1354 gpriv->mod.start = usbhsg_start;
1355 gpriv->mod.stop = usbhsg_stop;
1357 gpriv->uep_size = pipe_size;
1358 usbhsg_status_init(gpriv);
1363 device_initialize(&gpriv->gadget.dev);
1364 dev_set_name(&gpriv->gadget.dev, "gadget");
1365 gpriv->gadget.dev.parent = dev;
1366 gpriv->gadget.name = "renesas_usbhs_udc";
1367 gpriv->gadget.ops = &usbhsg_gadget_ops;
1368 gpriv->gadget.is_dualspeed = 1;
1370 INIT_LIST_HEAD(&gpriv->gadget.ep_list);
1375 usbhsg_for_each_uep_with_dcp(uep, gpriv, i) {
1377 snprintf(uep->ep_name, EP_NAME_SIZE, "ep%d", i);
1379 uep->ep.name = uep->ep_name;
1380 uep->ep.ops = &usbhsg_ep_ops;
1381 INIT_LIST_HEAD(&uep->ep.ep_list);
1382 INIT_LIST_HEAD(&uep->list);
1385 if (usbhsg_is_dcp(uep)) {
1386 gpriv->gadget.ep0 = &uep->ep;
1387 uep->ep.maxpacket = 64;
1389 /* init normal pipe */
1391 uep->ep.maxpacket = 512;
1392 list_add_tail(&uep->ep.ep_list, &gpriv->gadget.ep_list);
1396 the_controller = gpriv;
1398 dev_info(dev, "gadget probed\n");
1402 usbhs_mod_gadget_probe_err_gpriv:
1408 void __devexit usbhs_mod_gadget_remove(struct usbhs_priv *priv)
1410 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);