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);
63 void (*irq_mask)(struct usbhsg_uep *uep, int enable);
66 struct usbhsg_recip_handle {
68 int (*device)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
69 struct usb_ctrlrequest *ctrl);
70 int (*interface)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
71 struct usb_ctrlrequest *ctrl);
72 int (*endpoint)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
73 struct usb_ctrlrequest *ctrl);
79 #define usbhsg_priv_to_gpriv(priv) \
81 usbhs_mod_get(priv, USBHS_GADGET), \
82 struct usbhsg_gpriv, mod)
84 #define __usbhsg_for_each_uep(start, pos, g, i) \
85 for (i = start, pos = (g)->uep; \
87 i++, pos = (g)->uep + i)
89 #define usbhsg_for_each_uep(pos, gpriv, i) \
90 __usbhsg_for_each_uep(1, pos, gpriv, i)
92 #define usbhsg_for_each_uep_with_dcp(pos, gpriv, i) \
93 __usbhsg_for_each_uep(0, pos, gpriv, i)
95 #define usbhsg_gadget_to_gpriv(g)\
96 container_of(g, struct usbhsg_gpriv, gadget)
98 #define usbhsg_req_to_ureq(r)\
99 container_of(r, struct usbhsg_request, req)
101 #define usbhsg_ep_to_uep(e) container_of(e, struct usbhsg_uep, ep)
102 #define usbhsg_gpriv_to_lock(gp) usbhs_priv_to_lock((gp)->mod.priv)
103 #define usbhsg_gpriv_to_dev(gp) usbhs_priv_to_dev((gp)->mod.priv)
104 #define usbhsg_gpriv_to_priv(gp) ((gp)->mod.priv)
105 #define usbhsg_gpriv_to_dcp(gp) ((gp)->uep)
106 #define usbhsg_gpriv_to_nth_uep(gp, i) ((gp)->uep + i)
107 #define usbhsg_uep_to_gpriv(u) ((u)->gpriv)
108 #define usbhsg_uep_to_pipe(u) ((u)->pipe)
109 #define usbhsg_pipe_to_uep(p) ((p)->mod_private)
110 #define usbhsg_is_dcp(u) ((u) == usbhsg_gpriv_to_dcp((u)->gpriv))
112 #define usbhsg_ureq_to_pkt(u) (&(u)->pkt)
113 #define usbhsg_pkt_to_ureq(i) \
114 container_of(i, struct usbhsg_request, pkt)
116 #define usbhsg_is_not_connected(gp) ((gp)->gadget.speed == USB_SPEED_UNKNOWN)
119 #define usbhsg_status_init(gp) do {(gp)->status = 0; } while (0)
120 #define usbhsg_status_set(gp, b) (gp->status |= b)
121 #define usbhsg_status_clr(gp, b) (gp->status &= ~b)
122 #define usbhsg_status_has(gp, b) (gp->status & b)
127 * This driver don't use spin_try_lock
128 * to avoid warning of CONFIG_DEBUG_SPINLOCK
130 static spinlock_t *usbhsg_trylock(struct usbhsg_gpriv *gpriv,
131 unsigned long *flags)
133 spinlock_t *lock = usbhsg_gpriv_to_lock(gpriv);
135 /* check spin lock status
136 * to avoid deadlock/nest */
137 if (spin_is_locked(lock))
140 spin_lock_irqsave(lock, *flags);
145 static void usbhsg_unlock(spinlock_t *lock, unsigned long *flags)
150 spin_unlock_irqrestore(lock, *flags);
156 static void usbhsg_queue_push(struct usbhsg_uep *uep,
157 struct usbhsg_request *ureq)
159 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
160 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
161 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
162 struct usbhs_pkt *pkt = usbhsg_ureq_to_pkt(ureq);
165 ********* assume under spin lock *********
167 usbhs_pkt_push(pipe, pkt);
168 ureq->req.actual = 0;
169 ureq->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_try_run_send_packet_bh(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);
337 struct usb_request *req = &ureq->req;
338 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
339 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
340 int remainder, send, maxp;
346 remainder = pkt->length;
349 * send = 0 : send zero packet
350 * send > 0 : send data
356 /* send all packet ? */
357 if (send < remainder)
358 is_done = 0; /* there are remainder data */
359 else if (send < maxp)
360 is_done = 1; /* short packet */
362 is_done = !req->zero; /* send zero packet ? */
364 dev_dbg(dev, " send %d (%d/ %d/ %d/ %d)\n",
365 usbhs_pipe_number(pipe),
366 remainder, send, is_done, req->zero);
369 * enable interrupt and send again in irq handler
370 * if it still have remainder data which should be sent.
373 uep->handler->irq_mask(uep, enable);
376 * all data were sent ?
379 /* it care below call in
381 if (usbhsg_is_dcp(uep))
382 usbhs_dcp_control_transfer_done(pipe);
384 usbhsg_queue_pop(uep, ureq, 0);
388 static int usbhsg_try_run_send_packet(struct usbhsg_uep *uep,
389 struct usbhsg_request *ureq)
391 struct usb_request *req = &ureq->req;
392 struct usbhs_pkt *pkt = usbhsg_ureq_to_pkt(ureq);
396 ********* assume under spin lock *********
399 usbhs_pkt_update(pkt,
400 req->buf + req->actual,
401 req->length - req->actual);
403 ret = usbhs_fifo_write(pkt);
406 * retry in interrupt */
407 uep->handler->irq_mask(uep, 1);
413 static int usbhsg_prepare_send_packet(struct usbhsg_uep *uep,
414 struct usbhsg_request *ureq)
416 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
419 ********* assume under spin lock *********
422 usbhs_fifo_prepare_write(pipe);
423 usbhsg_try_run_send_packet(uep, ureq);
431 static void usbhsg_try_run_receive_packet_bh(struct usbhs_pkt *pkt)
433 struct usbhs_pipe *pipe = pkt->pipe;
434 struct usbhsg_uep *uep = usbhsg_pipe_to_uep(pipe);
435 struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt);
436 struct usb_request *req = &ureq->req;
437 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
438 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
439 int remainder, recv, maxp;
443 remainder = pkt->length;
447 * recv < 0 : pipe busy
448 * recv >= 0 : receive data
453 /* update parameters */
456 if ((recv == remainder) || /* receive all data */
457 (recv < maxp)) /* short packet */
460 dev_dbg(dev, " recv %d (%d/ %d/ %d/ %d)\n",
461 usbhs_pipe_number(pipe),
462 remainder, recv, is_done, req->zero);
464 /* read all data ? */
468 uep->handler->irq_mask(uep, disable);
469 usbhs_pipe_disable(pipe);
470 usbhsg_queue_pop(uep, ureq, 0);
474 static int usbhsg_try_run_receive_packet(struct usbhsg_uep *uep,
475 struct usbhsg_request *ureq)
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,
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;
821 #define usbhsg_dcp_disable usbhsg_pipe_disable
822 static int usbhsg_pipe_disable(struct usbhsg_uep *uep)
824 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
825 struct usbhsg_request *ureq;
829 ********* assume under spin lock *********
832 usbhs_pipe_disable(pipe);
837 usbhsg_irq_empty_ctrl(uep, disable);
838 usbhsg_irq_ready_ctrl(uep, disable);
841 ureq = usbhsg_queue_get(uep);
845 usbhsg_queue_pop(uep, ureq, -ECONNRESET);
851 static void usbhsg_uep_init(struct usbhsg_gpriv *gpriv)
854 struct usbhsg_uep *uep;
856 usbhsg_for_each_uep_with_dcp(uep, gpriv, i)
865 static int usbhsg_ep_enable(struct usb_ep *ep,
866 const struct usb_endpoint_descriptor *desc)
868 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
869 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
870 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
871 struct usbhs_pipe *pipe;
877 * if it already have pipe,
883 /******************** spin lock ********************/
884 lock = usbhsg_trylock(gpriv, &flags);
886 pipe = usbhs_pipe_malloc(priv, desc);
889 pipe->mod_private = uep;
891 if (usb_endpoint_dir_in(desc))
892 uep->handler = &usbhsg_handler_send_packet;
894 uep->handler = &usbhsg_handler_recv_packet;
899 usbhsg_unlock(lock, &flags);
900 /******************** spin unlock ******************/
905 static int usbhsg_ep_disable(struct usb_ep *ep)
907 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
908 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
913 /******************** spin lock ********************/
914 lock = usbhsg_trylock(gpriv, &flags);
916 ret = usbhsg_pipe_disable(uep);
918 usbhsg_unlock(lock, &flags);
919 /******************** spin unlock ******************/
924 static struct usb_request *usbhsg_ep_alloc_request(struct usb_ep *ep,
927 struct usbhsg_request *ureq;
929 ureq = kzalloc(sizeof *ureq, gfp_flags);
933 usbhs_pkt_init(usbhsg_ureq_to_pkt(ureq));
938 static void usbhsg_ep_free_request(struct usb_ep *ep,
939 struct usb_request *req)
941 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
943 WARN_ON(!list_empty(&ureq->pkt.node));
947 static int usbhsg_ep_queue(struct usb_ep *ep, struct usb_request *req,
950 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
951 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
952 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
953 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
959 * CAUTION [*endpoint queue*]
961 * This function will be called from usb_request :: complete
962 * or usb driver timing.
963 * If this function is called from usb_request :: complete,
964 * it is already under spinlock on this driver.
965 * but it is called frm usb driver, this function should call spinlock.
967 * This function is using usbshg_trylock to solve this issue.
968 * if "is_locked" is 1, this mean this function lock it.
969 * but if it is 0, this mean it is already under spin lock.
971 * CAUTION [*queue handler*]
972 * CAUTION [*request complete*]
975 /******************** spin lock ********************/
976 lock = usbhsg_trylock(gpriv, &flags);
979 if (usbhsg_is_not_connected(gpriv) ||
980 unlikely(!gpriv->driver) ||
984 usbhsg_queue_push(uep, ureq);
986 usbhsg_unlock(lock, &flags);
987 /******************** spin unlock ******************/
989 usbhsg_queue_prepare(uep);
994 static int usbhsg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
996 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
997 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
998 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
1000 unsigned long flags;
1004 * CAUTION [*queue handler*]
1005 * CAUTION [*endpoint queue*]
1006 * CAUTION [*request complete*]
1009 /******************** spin lock ********************/
1010 lock = usbhsg_trylock(gpriv, &flags);
1012 usbhsg_queue_pop(uep, ureq, -ECONNRESET);
1014 usbhsg_unlock(lock, &flags);
1015 /******************** spin unlock ******************/
1020 static int __usbhsg_ep_set_halt_wedge(struct usb_ep *ep, int halt, int wedge)
1022 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
1023 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
1024 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
1025 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
1027 unsigned long flags;
1032 * CAUTION [*queue handler*]
1033 * CAUTION [*endpoint queue*]
1034 * CAUTION [*request complete*]
1037 /******************** spin lock ********************/
1038 lock = usbhsg_trylock(gpriv, &flags);
1039 if (!usbhsg_queue_get(uep)) {
1041 dev_dbg(dev, "set halt %d (pipe %d)\n",
1042 halt, usbhs_pipe_number(pipe));
1045 usbhs_pipe_stall(pipe);
1047 usbhs_pipe_disable(pipe);
1050 usbhsg_status_set(gpriv, USBHSG_STATUS_WEDGE);
1052 usbhsg_status_clr(gpriv, USBHSG_STATUS_WEDGE);
1057 usbhsg_unlock(lock, &flags);
1058 /******************** spin unlock ******************/
1063 static int usbhsg_ep_set_halt(struct usb_ep *ep, int value)
1065 return __usbhsg_ep_set_halt_wedge(ep, value, 0);
1068 static int usbhsg_ep_set_wedge(struct usb_ep *ep)
1070 return __usbhsg_ep_set_halt_wedge(ep, 1, 1);
1073 static struct usb_ep_ops usbhsg_ep_ops = {
1074 .enable = usbhsg_ep_enable,
1075 .disable = usbhsg_ep_disable,
1077 .alloc_request = usbhsg_ep_alloc_request,
1078 .free_request = usbhsg_ep_free_request,
1080 .queue = usbhsg_ep_queue,
1081 .dequeue = usbhsg_ep_dequeue,
1083 .set_halt = usbhsg_ep_set_halt,
1084 .set_wedge = usbhsg_ep_set_wedge,
1088 * usb module start/end
1090 static int usbhsg_try_start(struct usbhs_priv *priv, u32 status)
1092 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
1093 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
1094 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
1095 struct device *dev = usbhs_priv_to_dev(priv);
1097 unsigned long flags;
1099 /******************** spin lock ********************/
1100 lock = usbhsg_trylock(gpriv, &flags);
1103 * enable interrupt and systems if ready
1105 usbhsg_status_set(gpriv, status);
1106 if (!(usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
1107 usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD)))
1108 goto usbhsg_try_start_unlock;
1110 dev_dbg(dev, "start gadget\n");
1113 * pipe initialize and enable DCP
1115 usbhs_pipe_init(priv,
1116 usbhsg_try_run_send_packet_bh,
1117 usbhsg_try_run_receive_packet_bh);
1118 usbhsg_uep_init(gpriv);
1119 usbhsg_dcp_enable(dcp);
1122 * system config enble
1127 usbhs_sys_hispeed_ctrl(priv, 1);
1128 usbhs_sys_function_ctrl(priv, 1);
1129 usbhs_sys_usb_ctrl(priv, 1);
1132 * enable irq callback
1134 mod->irq_dev_state = usbhsg_irq_dev_state;
1135 mod->irq_ctrl_stage = usbhsg_irq_ctrl_stage;
1136 mod->irq_empty = usbhsg_irq_empty;
1137 mod->irq_ready = usbhsg_irq_ready;
1138 mod->irq_bempsts = 0;
1139 mod->irq_brdysts = 0;
1140 usbhs_irq_callback_update(priv, mod);
1142 usbhsg_try_start_unlock:
1143 usbhsg_unlock(lock, &flags);
1144 /******************** spin unlock ********************/
1149 static int usbhsg_try_stop(struct usbhs_priv *priv, u32 status)
1151 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
1152 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
1153 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
1154 struct device *dev = usbhs_priv_to_dev(priv);
1156 unsigned long flags;
1158 /******************** spin lock ********************/
1159 lock = usbhsg_trylock(gpriv, &flags);
1162 * disable interrupt and systems if 1st try
1164 usbhsg_status_clr(gpriv, status);
1165 if (!usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
1166 !usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD))
1167 goto usbhsg_try_stop_unlock;
1169 /* disable all irq */
1170 mod->irq_dev_state = NULL;
1171 mod->irq_ctrl_stage = NULL;
1172 mod->irq_empty = NULL;
1173 mod->irq_ready = NULL;
1174 mod->irq_bempsts = 0;
1175 mod->irq_brdysts = 0;
1176 usbhs_irq_callback_update(priv, mod);
1178 usbhsg_dcp_disable(dcp);
1180 gpriv->gadget.speed = USB_SPEED_UNKNOWN;
1183 usbhs_sys_hispeed_ctrl(priv, 0);
1184 usbhs_sys_function_ctrl(priv, 0);
1185 usbhs_sys_usb_ctrl(priv, 0);
1187 usbhsg_unlock(lock, &flags);
1188 /******************** spin unlock ********************/
1190 if (gpriv->driver &&
1191 gpriv->driver->disconnect)
1192 gpriv->driver->disconnect(&gpriv->gadget);
1194 dev_dbg(dev, "stop gadget\n");
1198 usbhsg_try_stop_unlock:
1199 usbhsg_unlock(lock, &flags);
1206 * linux usb function
1209 struct usbhsg_gpriv *the_controller;
1210 int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
1211 int (*bind)(struct usb_gadget *))
1213 struct usbhsg_gpriv *gpriv = the_controller;
1214 struct usbhs_priv *priv;
1221 driver->speed != USB_SPEED_HIGH)
1228 dev = usbhsg_gpriv_to_dev(gpriv);
1229 priv = usbhsg_gpriv_to_priv(gpriv);
1231 /* first hook up the driver ... */
1232 gpriv->driver = driver;
1233 gpriv->gadget.dev.driver = &driver->driver;
1235 ret = device_add(&gpriv->gadget.dev);
1237 dev_err(dev, "device_add error %d\n", ret);
1241 ret = bind(&gpriv->gadget);
1243 dev_err(dev, "bind to driver %s error %d\n",
1244 driver->driver.name, ret);
1248 dev_dbg(dev, "bind %s\n", driver->driver.name);
1250 return usbhsg_try_start(priv, USBHSG_STATUS_REGISTERD);
1253 device_del(&gpriv->gadget.dev);
1255 gpriv->driver = NULL;
1256 gpriv->gadget.dev.driver = NULL;
1260 EXPORT_SYMBOL(usb_gadget_probe_driver);
1262 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1264 struct usbhsg_gpriv *gpriv = the_controller;
1265 struct usbhs_priv *priv;
1266 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
1273 driver != gpriv->driver)
1276 dev = usbhsg_gpriv_to_dev(gpriv);
1277 priv = usbhsg_gpriv_to_priv(gpriv);
1279 usbhsg_try_stop(priv, USBHSG_STATUS_REGISTERD);
1280 device_del(&gpriv->gadget.dev);
1281 gpriv->driver = NULL;
1283 if (driver->disconnect)
1284 driver->disconnect(&gpriv->gadget);
1286 driver->unbind(&gpriv->gadget);
1287 dev_dbg(dev, "unbind %s\n", driver->driver.name);
1291 EXPORT_SYMBOL(usb_gadget_unregister_driver);
1296 static int usbhsg_get_frame(struct usb_gadget *gadget)
1298 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
1299 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
1301 return usbhs_frame_get_num(priv);
1304 static struct usb_gadget_ops usbhsg_gadget_ops = {
1305 .get_frame = usbhsg_get_frame,
1308 static int usbhsg_start(struct usbhs_priv *priv)
1310 return usbhsg_try_start(priv, USBHSG_STATUS_STARTED);
1313 static int usbhsg_stop(struct usbhs_priv *priv)
1315 return usbhsg_try_stop(priv, USBHSG_STATUS_STARTED);
1318 int __devinit usbhs_mod_gadget_probe(struct usbhs_priv *priv)
1320 struct usbhsg_gpriv *gpriv;
1321 struct usbhsg_uep *uep;
1322 struct device *dev = usbhs_priv_to_dev(priv);
1323 int pipe_size = usbhs_get_dparam(priv, pipe_size);
1326 gpriv = kzalloc(sizeof(struct usbhsg_gpriv), GFP_KERNEL);
1328 dev_err(dev, "Could not allocate gadget priv\n");
1332 uep = kzalloc(sizeof(struct usbhsg_uep) * pipe_size, GFP_KERNEL);
1334 dev_err(dev, "Could not allocate ep\n");
1335 goto usbhs_mod_gadget_probe_err_gpriv;
1341 * There is no guarantee that it is possible to access usb module here.
1342 * Don't accesses to it.
1343 * The accesse will be enable after "usbhsg_start"
1349 usbhs_mod_register(priv, &gpriv->mod, USBHS_GADGET);
1352 gpriv->mod.name = "gadget";
1353 gpriv->mod.start = usbhsg_start;
1354 gpriv->mod.stop = usbhsg_stop;
1356 gpriv->uep_size = pipe_size;
1357 usbhsg_status_init(gpriv);
1362 device_initialize(&gpriv->gadget.dev);
1363 dev_set_name(&gpriv->gadget.dev, "gadget");
1364 gpriv->gadget.dev.parent = dev;
1365 gpriv->gadget.name = "renesas_usbhs_udc";
1366 gpriv->gadget.ops = &usbhsg_gadget_ops;
1367 gpriv->gadget.is_dualspeed = 1;
1369 INIT_LIST_HEAD(&gpriv->gadget.ep_list);
1374 usbhsg_for_each_uep_with_dcp(uep, gpriv, i) {
1376 snprintf(uep->ep_name, EP_NAME_SIZE, "ep%d", i);
1378 uep->ep.name = uep->ep_name;
1379 uep->ep.ops = &usbhsg_ep_ops;
1380 INIT_LIST_HEAD(&uep->ep.ep_list);
1383 if (usbhsg_is_dcp(uep)) {
1384 gpriv->gadget.ep0 = &uep->ep;
1385 uep->ep.maxpacket = 64;
1387 /* init normal pipe */
1389 uep->ep.maxpacket = 512;
1390 list_add_tail(&uep->ep.ep_list, &gpriv->gadget.ep_list);
1394 the_controller = gpriv;
1396 dev_info(dev, "gadget probed\n");
1400 usbhs_mod_gadget_probe_err_gpriv:
1406 void __devexit usbhs_mod_gadget_remove(struct usbhs_priv *priv)
1408 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);