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
17 #include <linux/delay.h>
18 #include <linux/dma-mapping.h>
20 #include <linux/module.h>
21 #include <linux/platform_device.h>
22 #include <linux/usb/ch9.h>
23 #include <linux/usb/gadget.h>
24 #include <linux/usb/otg.h>
30 struct usbhsg_request {
31 struct usb_request req;
35 #define EP_NAME_SIZE 8
39 struct usbhs_pipe *pipe;
40 spinlock_t lock; /* protect the pipe */
42 char ep_name[EP_NAME_SIZE];
44 struct usbhsg_gpriv *gpriv;
48 struct usb_gadget gadget;
51 struct usbhsg_uep *uep;
54 struct usb_gadget_driver *driver;
55 struct usb_phy *transceiver;
59 #define USBHSG_STATUS_STARTED (1 << 0)
60 #define USBHSG_STATUS_REGISTERD (1 << 1)
61 #define USBHSG_STATUS_WEDGE (1 << 2)
62 #define USBHSG_STATUS_SELF_POWERED (1 << 3)
63 #define USBHSG_STATUS_SOFT_CONNECT (1 << 4)
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) \
86 ((i) < (g)->uep_size) && ((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_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 static void __usbhsg_queue_pop(struct usbhsg_uep *uep,
127 struct usbhsg_request *ureq,
130 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
131 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
132 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
133 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
136 dev_dbg(dev, "pipe %d : queue pop\n", usbhs_pipe_number(pipe));
138 ureq->req.status = status;
139 spin_unlock(usbhs_priv_to_lock(priv));
140 usb_gadget_giveback_request(&uep->ep, &ureq->req);
141 spin_lock(usbhs_priv_to_lock(priv));
144 static void usbhsg_queue_pop(struct usbhsg_uep *uep,
145 struct usbhsg_request *ureq,
148 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
149 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
152 usbhs_lock(priv, flags);
153 __usbhsg_queue_pop(uep, ureq, status);
154 usbhs_unlock(priv, flags);
157 static void usbhsg_queue_done(struct usbhs_priv *priv, struct usbhs_pkt *pkt)
159 struct usbhs_pipe *pipe = pkt->pipe;
160 struct usbhsg_uep *uep = usbhsg_pipe_to_uep(pipe);
161 struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt);
164 ureq->req.actual = pkt->actual;
166 usbhs_lock(priv, flags);
168 __usbhsg_queue_pop(uep, ureq, 0);
169 usbhs_unlock(priv, flags);
172 static void usbhsg_queue_push(struct usbhsg_uep *uep,
173 struct usbhsg_request *ureq)
175 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
176 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
177 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
178 struct usbhs_pkt *pkt = usbhsg_ureq_to_pkt(ureq);
179 struct usb_request *req = &ureq->req;
182 req->status = -EINPROGRESS;
183 usbhs_pkt_push(pipe, pkt, usbhsg_queue_done,
184 req->buf, req->length, req->zero, -1);
185 usbhs_pkt_start(pipe);
187 dev_dbg(dev, "pipe %d : queue push (%d)\n",
188 usbhs_pipe_number(pipe),
195 static int usbhsg_dma_map_ctrl(struct usbhs_pkt *pkt, int map)
197 struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt);
198 struct usb_request *req = &ureq->req;
199 struct usbhs_pipe *pipe = pkt->pipe;
200 struct usbhsg_uep *uep = usbhsg_pipe_to_uep(pipe);
201 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
202 enum dma_data_direction dir;
205 dir = usbhs_pipe_is_dir_host(pipe);
208 /* it can not use scatter/gather */
209 WARN_ON(req->num_sgs);
211 ret = usb_gadget_map_request(&gpriv->gadget, req, dir);
217 usb_gadget_unmap_request(&gpriv->gadget, req, dir);
224 * USB_TYPE_STANDARD / clear feature functions
226 static int usbhsg_recip_handler_std_control_done(struct usbhs_priv *priv,
227 struct usbhsg_uep *uep,
228 struct usb_ctrlrequest *ctrl)
230 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
231 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
232 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
234 usbhs_dcp_control_transfer_done(pipe);
239 static int usbhsg_recip_handler_std_clear_endpoint(struct usbhs_priv *priv,
240 struct usbhsg_uep *uep,
241 struct usb_ctrlrequest *ctrl)
243 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
244 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
246 if (!usbhsg_status_has(gpriv, USBHSG_STATUS_WEDGE)) {
247 usbhs_pipe_disable(pipe);
248 usbhs_pipe_sequence_data0(pipe);
249 usbhs_pipe_enable(pipe);
252 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
254 usbhs_pkt_start(pipe);
259 static struct usbhsg_recip_handle req_clear_feature = {
260 .name = "clear feature",
261 .device = usbhsg_recip_handler_std_control_done,
262 .interface = usbhsg_recip_handler_std_control_done,
263 .endpoint = usbhsg_recip_handler_std_clear_endpoint,
267 * USB_TYPE_STANDARD / set feature functions
269 static int usbhsg_recip_handler_std_set_device(struct usbhs_priv *priv,
270 struct usbhsg_uep *uep,
271 struct usb_ctrlrequest *ctrl)
273 switch (le16_to_cpu(ctrl->wValue)) {
274 case USB_DEVICE_TEST_MODE:
275 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
277 usbhs_sys_set_test_mode(priv, le16_to_cpu(ctrl->wIndex >> 8));
280 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
287 static int usbhsg_recip_handler_std_set_endpoint(struct usbhs_priv *priv,
288 struct usbhsg_uep *uep,
289 struct usb_ctrlrequest *ctrl)
291 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
293 usbhs_pipe_stall(pipe);
295 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
300 static struct usbhsg_recip_handle req_set_feature = {
301 .name = "set feature",
302 .device = usbhsg_recip_handler_std_set_device,
303 .interface = usbhsg_recip_handler_std_control_done,
304 .endpoint = usbhsg_recip_handler_std_set_endpoint,
308 * USB_TYPE_STANDARD / get status functions
310 static void __usbhsg_recip_send_complete(struct usb_ep *ep,
311 struct usb_request *req)
313 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
315 /* free allocated recip-buffer/usb_request */
316 kfree(ureq->pkt.buf);
317 usb_ep_free_request(ep, req);
320 static void __usbhsg_recip_send_status(struct usbhsg_gpriv *gpriv,
321 unsigned short status)
323 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
324 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
325 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
326 struct usb_request *req;
329 /* alloc new usb_request for recip */
330 req = usb_ep_alloc_request(&dcp->ep, GFP_ATOMIC);
332 dev_err(dev, "recip request allocation fail\n");
336 /* alloc recip data buffer */
337 buf = kmalloc(sizeof(*buf), GFP_ATOMIC);
339 usb_ep_free_request(&dcp->ep, req);
340 dev_err(dev, "recip data allocation fail\n");
344 /* recip data is status */
345 *buf = cpu_to_le16(status);
347 /* allocated usb_request/buffer will be freed */
348 req->complete = __usbhsg_recip_send_complete;
350 req->length = sizeof(*buf);
354 pipe->handler = &usbhs_fifo_pio_push_handler;
355 usbhsg_queue_push(dcp, usbhsg_req_to_ureq(req));
358 static int usbhsg_recip_handler_std_get_device(struct usbhs_priv *priv,
359 struct usbhsg_uep *uep,
360 struct usb_ctrlrequest *ctrl)
362 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
363 unsigned short status = 0;
365 if (usbhsg_status_has(gpriv, USBHSG_STATUS_SELF_POWERED))
366 status = 1 << USB_DEVICE_SELF_POWERED;
368 __usbhsg_recip_send_status(gpriv, status);
373 static int usbhsg_recip_handler_std_get_interface(struct usbhs_priv *priv,
374 struct usbhsg_uep *uep,
375 struct usb_ctrlrequest *ctrl)
377 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
378 unsigned short status = 0;
380 __usbhsg_recip_send_status(gpriv, status);
385 static int usbhsg_recip_handler_std_get_endpoint(struct usbhs_priv *priv,
386 struct usbhsg_uep *uep,
387 struct usb_ctrlrequest *ctrl)
389 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
390 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
391 unsigned short status = 0;
393 if (usbhs_pipe_is_stall(pipe))
394 status = 1 << USB_ENDPOINT_HALT;
396 __usbhsg_recip_send_status(gpriv, status);
401 static struct usbhsg_recip_handle req_get_status = {
402 .name = "get status",
403 .device = usbhsg_recip_handler_std_get_device,
404 .interface = usbhsg_recip_handler_std_get_interface,
405 .endpoint = usbhsg_recip_handler_std_get_endpoint,
411 static int usbhsg_recip_run_handle(struct usbhs_priv *priv,
412 struct usbhsg_recip_handle *handler,
413 struct usb_ctrlrequest *ctrl)
415 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
416 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
417 struct usbhsg_uep *uep;
418 struct usbhs_pipe *pipe;
419 int recip = ctrl->bRequestType & USB_RECIP_MASK;
420 int nth = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK;
422 int (*func)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
423 struct usb_ctrlrequest *ctrl);
426 uep = usbhsg_gpriv_to_nth_uep(gpriv, nth);
427 pipe = usbhsg_uep_to_pipe(uep);
429 dev_err(dev, "wrong recip request\n");
434 case USB_RECIP_DEVICE:
436 func = handler->device;
438 case USB_RECIP_INTERFACE:
440 func = handler->interface;
442 case USB_RECIP_ENDPOINT:
444 func = handler->endpoint;
447 dev_warn(dev, "unsupported RECIP(%d)\n", recip);
453 dev_dbg(dev, "%s (pipe %d :%s)\n", handler->name, nth, msg);
454 ret = func(priv, uep, ctrl);
463 * it will be called from usbhs_interrupt
465 static int usbhsg_irq_dev_state(struct usbhs_priv *priv,
466 struct usbhs_irq_state *irq_state)
468 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
469 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
470 int state = usbhs_status_get_device_state(irq_state);
472 gpriv->gadget.speed = usbhs_bus_get_speed(priv);
474 dev_dbg(dev, "state = %x : speed : %d\n", state, gpriv->gadget.speed);
476 if (gpriv->gadget.speed != USB_SPEED_UNKNOWN &&
477 (state & SUSPENDED_STATE)) {
478 if (gpriv->driver && gpriv->driver->suspend)
479 gpriv->driver->suspend(&gpriv->gadget);
480 usb_gadget_set_state(&gpriv->gadget, USB_STATE_SUSPENDED);
486 static int usbhsg_irq_ctrl_stage(struct usbhs_priv *priv,
487 struct usbhs_irq_state *irq_state)
489 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
490 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
491 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
492 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
493 struct usb_ctrlrequest ctrl;
494 struct usbhsg_recip_handle *recip_handler = NULL;
495 int stage = usbhs_status_get_ctrl_stage(irq_state);
498 dev_dbg(dev, "stage = %d\n", stage);
504 * - "Interrupt Function"
505 * - "Control Transfer Stage Transition Interrupt"
506 * - Fig. "Control Transfer Stage Transitions"
510 case READ_DATA_STAGE:
511 pipe->handler = &usbhs_fifo_pio_push_handler;
513 case WRITE_DATA_STAGE:
514 pipe->handler = &usbhs_fifo_pio_pop_handler;
516 case NODATA_STATUS_STAGE:
517 pipe->handler = &usbhs_ctrl_stage_end_handler;
519 case READ_STATUS_STAGE:
520 case WRITE_STATUS_STAGE:
521 usbhs_dcp_control_transfer_done(pipe);
529 usbhs_usbreq_get_val(priv, &ctrl);
531 switch (ctrl.bRequestType & USB_TYPE_MASK) {
532 case USB_TYPE_STANDARD:
533 switch (ctrl.bRequest) {
534 case USB_REQ_CLEAR_FEATURE:
535 recip_handler = &req_clear_feature;
537 case USB_REQ_SET_FEATURE:
538 recip_handler = &req_set_feature;
540 case USB_REQ_GET_STATUS:
541 recip_handler = &req_get_status;
547 * setup stage / run recip
550 ret = usbhsg_recip_run_handle(priv, recip_handler, &ctrl);
552 ret = gpriv->driver->setup(&gpriv->gadget, &ctrl);
555 usbhs_pipe_stall(pipe);
565 static int usbhsg_pipe_disable(struct usbhsg_uep *uep)
567 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
568 struct usbhs_pkt *pkt;
571 pkt = usbhs_pkt_pop(pipe, NULL);
575 usbhsg_queue_pop(uep, usbhsg_pkt_to_ureq(pkt), -ECONNRESET);
578 usbhs_pipe_disable(pipe);
588 static int usbhsg_ep_enable(struct usb_ep *ep,
589 const struct usb_endpoint_descriptor *desc)
591 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
592 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
593 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
594 struct usbhs_pipe *pipe;
598 usbhs_lock(priv, flags);
601 * if it already have pipe,
605 usbhs_pipe_clear(uep->pipe);
606 usbhs_pipe_sequence_data0(uep->pipe);
608 goto usbhsg_ep_enable_end;
611 pipe = usbhs_pipe_malloc(priv,
612 usb_endpoint_type(desc),
613 usb_endpoint_dir_in(desc));
616 pipe->mod_private = uep;
618 /* set epnum / maxp */
619 usbhs_pipe_config_update(pipe, 0,
620 usb_endpoint_num(desc),
621 usb_endpoint_maxp(desc));
624 * usbhs_fifo_dma_push/pop_handler try to
625 * use dmaengine if possible.
626 * It will use pio handler if impossible.
628 if (usb_endpoint_dir_in(desc)) {
629 pipe->handler = &usbhs_fifo_dma_push_handler;
631 pipe->handler = &usbhs_fifo_dma_pop_handler;
632 usbhs_xxxsts_clear(priv, BRDYSTS,
633 usbhs_pipe_number(pipe));
639 usbhsg_ep_enable_end:
640 usbhs_unlock(priv, flags);
645 static int usbhsg_ep_disable(struct usb_ep *ep)
647 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
648 struct usbhs_pipe *pipe;
651 spin_lock_irqsave(&uep->lock, flags);
652 pipe = usbhsg_uep_to_pipe(uep);
656 usbhsg_pipe_disable(uep);
657 usbhs_pipe_free(pipe);
659 uep->pipe->mod_private = NULL;
663 spin_unlock_irqrestore(&uep->lock, flags);
668 static struct usb_request *usbhsg_ep_alloc_request(struct usb_ep *ep,
671 struct usbhsg_request *ureq;
673 ureq = kzalloc(sizeof *ureq, gfp_flags);
677 usbhs_pkt_init(usbhsg_ureq_to_pkt(ureq));
682 static void usbhsg_ep_free_request(struct usb_ep *ep,
683 struct usb_request *req)
685 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
687 WARN_ON(!list_empty(&ureq->pkt.node));
691 static int usbhsg_ep_queue(struct usb_ep *ep, struct usb_request *req,
694 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
695 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
696 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
697 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
700 if (usbhsg_is_not_connected(gpriv) ||
701 unlikely(!gpriv->driver) ||
705 usbhsg_queue_push(uep, ureq);
710 static int usbhsg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
712 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
713 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
714 struct usbhs_pipe *pipe;
717 spin_lock_irqsave(&uep->lock, flags);
718 pipe = usbhsg_uep_to_pipe(uep);
720 usbhs_pkt_pop(pipe, usbhsg_ureq_to_pkt(ureq));
723 * To dequeue a request, this driver should call the usbhsg_queue_pop()
724 * even if the pipe is NULL.
726 usbhsg_queue_pop(uep, ureq, -ECONNRESET);
727 spin_unlock_irqrestore(&uep->lock, flags);
732 static int __usbhsg_ep_set_halt_wedge(struct usb_ep *ep, int halt, int wedge)
734 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
735 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
736 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
737 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
738 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
742 dev_dbg(dev, "set halt %d (pipe %d)\n",
743 halt, usbhs_pipe_number(pipe));
745 /******************** spin lock ********************/
746 usbhs_lock(priv, flags);
749 * According to usb_ep_set_halt()'s description, this function should
750 * return -EAGAIN if the IN endpoint has any queue or data. Note
751 * that the usbhs_pipe_is_dir_in() returns false if the pipe is an
752 * IN endpoint in the gadget mode.
754 if (!usbhs_pipe_is_dir_in(pipe) && (__usbhsf_pkt_get(pipe) ||
755 usbhs_pipe_contains_transmittable_data(pipe))) {
761 usbhs_pipe_stall(pipe);
763 usbhs_pipe_disable(pipe);
766 usbhsg_status_set(gpriv, USBHSG_STATUS_WEDGE);
768 usbhsg_status_clr(gpriv, USBHSG_STATUS_WEDGE);
771 usbhs_unlock(priv, flags);
772 /******************** spin unlock ******************/
777 static int usbhsg_ep_set_halt(struct usb_ep *ep, int value)
779 return __usbhsg_ep_set_halt_wedge(ep, value, 0);
782 static int usbhsg_ep_set_wedge(struct usb_ep *ep)
784 return __usbhsg_ep_set_halt_wedge(ep, 1, 1);
787 static struct usb_ep_ops usbhsg_ep_ops = {
788 .enable = usbhsg_ep_enable,
789 .disable = usbhsg_ep_disable,
791 .alloc_request = usbhsg_ep_alloc_request,
792 .free_request = usbhsg_ep_free_request,
794 .queue = usbhsg_ep_queue,
795 .dequeue = usbhsg_ep_dequeue,
797 .set_halt = usbhsg_ep_set_halt,
798 .set_wedge = usbhsg_ep_set_wedge,
804 static int usbhsg_can_pullup(struct usbhs_priv *priv)
806 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
808 return gpriv->driver &&
809 usbhsg_status_has(gpriv, USBHSG_STATUS_SOFT_CONNECT);
812 static void usbhsg_update_pullup(struct usbhs_priv *priv)
814 if (usbhsg_can_pullup(priv))
815 usbhs_sys_function_pullup(priv, 1);
817 usbhs_sys_function_pullup(priv, 0);
821 * usb module start/end
823 static int usbhsg_try_start(struct usbhs_priv *priv, u32 status)
825 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
826 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
827 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
828 struct device *dev = usbhs_priv_to_dev(priv);
832 /******************** spin lock ********************/
833 usbhs_lock(priv, flags);
835 usbhsg_status_set(gpriv, status);
836 if (!(usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
837 usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD)))
838 ret = -1; /* not ready */
840 usbhs_unlock(priv, flags);
841 /******************** spin unlock ********************/
844 return 0; /* not ready is not error */
847 * enable interrupt and systems if ready
849 dev_dbg(dev, "start gadget\n");
852 * pipe initialize and enable DCP
854 usbhs_fifo_init(priv);
855 usbhs_pipe_init(priv,
856 usbhsg_dma_map_ctrl);
858 /* dcp init instead of usbhsg_ep_enable() */
859 dcp->pipe = usbhs_dcp_malloc(priv);
860 dcp->pipe->mod_private = dcp;
861 usbhs_pipe_config_update(dcp->pipe, 0, 0, 64);
864 * system config enble
869 usbhs_sys_function_ctrl(priv, 1);
870 usbhsg_update_pullup(priv);
873 * enable irq callback
875 mod->irq_dev_state = usbhsg_irq_dev_state;
876 mod->irq_ctrl_stage = usbhsg_irq_ctrl_stage;
877 usbhs_irq_callback_update(priv, mod);
882 static int usbhsg_try_stop(struct usbhs_priv *priv, u32 status)
884 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
885 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
886 struct usbhsg_uep *uep;
887 struct device *dev = usbhs_priv_to_dev(priv);
891 /******************** spin lock ********************/
892 usbhs_lock(priv, flags);
894 usbhsg_status_clr(gpriv, status);
895 if (!usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
896 !usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD))
897 ret = -1; /* already done */
899 usbhs_unlock(priv, flags);
900 /******************** spin unlock ********************/
903 return 0; /* already done is not error */
906 * disable interrupt and systems if 1st try
908 usbhs_fifo_quit(priv);
910 /* disable all irq */
911 mod->irq_dev_state = NULL;
912 mod->irq_ctrl_stage = NULL;
913 usbhs_irq_callback_update(priv, mod);
915 gpriv->gadget.speed = USB_SPEED_UNKNOWN;
918 usbhs_sys_set_test_mode(priv, 0);
919 usbhs_sys_function_ctrl(priv, 0);
921 /* disable all eps */
922 usbhsg_for_each_uep_with_dcp(uep, gpriv, i)
923 usbhsg_ep_disable(&uep->ep);
925 dev_dbg(dev, "stop gadget\n");
931 * VBUS provided by the PHY
933 static int usbhsm_phy_get_vbus(struct platform_device *pdev)
935 struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
936 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
938 return gpriv->vbus_active;
941 static void usbhs_mod_phy_mode(struct usbhs_priv *priv)
943 struct usbhs_mod_info *info = &priv->mod_info;
945 info->irq_vbus = NULL;
946 priv->pfunc.get_vbus = usbhsm_phy_get_vbus;
948 usbhs_irq_callback_update(priv, NULL);
956 static int usbhsg_gadget_start(struct usb_gadget *gadget,
957 struct usb_gadget_driver *driver)
959 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
960 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
961 struct device *dev = usbhs_priv_to_dev(priv);
966 driver->max_speed < USB_SPEED_FULL)
969 /* connect to bus through transceiver */
970 if (!IS_ERR_OR_NULL(gpriv->transceiver)) {
971 ret = otg_set_peripheral(gpriv->transceiver->otg,
974 dev_err(dev, "%s: can't bind to transceiver\n",
979 /* get vbus using phy versions */
980 usbhs_mod_phy_mode(priv);
983 /* first hook up the driver ... */
984 gpriv->driver = driver;
986 return usbhsg_try_start(priv, USBHSG_STATUS_REGISTERD);
989 static int usbhsg_gadget_stop(struct usb_gadget *gadget)
991 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
992 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
994 usbhsg_try_stop(priv, USBHSG_STATUS_REGISTERD);
996 if (!IS_ERR_OR_NULL(gpriv->transceiver))
997 otg_set_peripheral(gpriv->transceiver->otg, NULL);
999 gpriv->driver = NULL;
1007 static int usbhsg_get_frame(struct usb_gadget *gadget)
1009 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
1010 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
1012 return usbhs_frame_get_num(priv);
1015 static int usbhsg_pullup(struct usb_gadget *gadget, int is_on)
1017 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
1018 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
1019 unsigned long flags;
1021 usbhs_lock(priv, flags);
1023 usbhsg_status_set(gpriv, USBHSG_STATUS_SOFT_CONNECT);
1025 usbhsg_status_clr(gpriv, USBHSG_STATUS_SOFT_CONNECT);
1026 usbhsg_update_pullup(priv);
1027 usbhs_unlock(priv, flags);
1032 static int usbhsg_set_selfpowered(struct usb_gadget *gadget, int is_self)
1034 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
1037 usbhsg_status_set(gpriv, USBHSG_STATUS_SELF_POWERED);
1039 usbhsg_status_clr(gpriv, USBHSG_STATUS_SELF_POWERED);
1041 gadget->is_selfpowered = (is_self != 0);
1046 static int usbhsg_vbus_session(struct usb_gadget *gadget, int is_active)
1048 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
1049 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
1050 struct platform_device *pdev = usbhs_priv_to_pdev(priv);
1052 gpriv->vbus_active = !!is_active;
1054 renesas_usbhs_call_notify_hotplug(pdev);
1059 static const struct usb_gadget_ops usbhsg_gadget_ops = {
1060 .get_frame = usbhsg_get_frame,
1061 .set_selfpowered = usbhsg_set_selfpowered,
1062 .udc_start = usbhsg_gadget_start,
1063 .udc_stop = usbhsg_gadget_stop,
1064 .pullup = usbhsg_pullup,
1065 .vbus_session = usbhsg_vbus_session,
1068 static int usbhsg_start(struct usbhs_priv *priv)
1070 return usbhsg_try_start(priv, USBHSG_STATUS_STARTED);
1073 static int usbhsg_stop(struct usbhs_priv *priv)
1075 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
1077 /* cable disconnect */
1078 if (gpriv->driver &&
1079 gpriv->driver->disconnect)
1080 gpriv->driver->disconnect(&gpriv->gadget);
1082 return usbhsg_try_stop(priv, USBHSG_STATUS_STARTED);
1085 int usbhs_mod_gadget_probe(struct usbhs_priv *priv)
1087 struct usbhsg_gpriv *gpriv;
1088 struct usbhsg_uep *uep;
1089 struct device *dev = usbhs_priv_to_dev(priv);
1090 int pipe_size = usbhs_get_dparam(priv, pipe_size);
1094 gpriv = kzalloc(sizeof(struct usbhsg_gpriv), GFP_KERNEL);
1096 dev_err(dev, "Could not allocate gadget priv\n");
1100 uep = kzalloc(sizeof(struct usbhsg_uep) * pipe_size, GFP_KERNEL);
1102 dev_err(dev, "Could not allocate ep\n");
1104 goto usbhs_mod_gadget_probe_err_gpriv;
1107 gpriv->transceiver = usb_get_phy(USB_PHY_TYPE_UNDEFINED);
1108 dev_info(dev, "%stransceiver found\n",
1109 !IS_ERR(gpriv->transceiver) ? "" : "no ");
1114 * There is no guarantee that it is possible to access usb module here.
1115 * Don't accesses to it.
1116 * The accesse will be enable after "usbhsg_start"
1122 usbhs_mod_register(priv, &gpriv->mod, USBHS_GADGET);
1125 gpriv->mod.name = "gadget";
1126 gpriv->mod.start = usbhsg_start;
1127 gpriv->mod.stop = usbhsg_stop;
1129 gpriv->uep_size = pipe_size;
1130 usbhsg_status_init(gpriv);
1135 gpriv->gadget.dev.parent = dev;
1136 gpriv->gadget.name = "renesas_usbhs_udc";
1137 gpriv->gadget.ops = &usbhsg_gadget_ops;
1138 gpriv->gadget.max_speed = USB_SPEED_HIGH;
1140 INIT_LIST_HEAD(&gpriv->gadget.ep_list);
1145 usbhsg_for_each_uep_with_dcp(uep, gpriv, i) {
1148 snprintf(uep->ep_name, EP_NAME_SIZE, "ep%d", i);
1150 uep->ep.name = uep->ep_name;
1151 uep->ep.ops = &usbhsg_ep_ops;
1152 INIT_LIST_HEAD(&uep->ep.ep_list);
1153 spin_lock_init(&uep->lock);
1156 if (usbhsg_is_dcp(uep)) {
1157 gpriv->gadget.ep0 = &uep->ep;
1158 usb_ep_set_maxpacket_limit(&uep->ep, 64);
1159 uep->ep.caps.type_control = true;
1161 /* init normal pipe */
1163 usb_ep_set_maxpacket_limit(&uep->ep, 512);
1164 uep->ep.caps.type_iso = true;
1165 uep->ep.caps.type_bulk = true;
1166 uep->ep.caps.type_int = true;
1167 list_add_tail(&uep->ep.ep_list, &gpriv->gadget.ep_list);
1169 uep->ep.caps.dir_in = true;
1170 uep->ep.caps.dir_out = true;
1173 ret = usb_add_gadget_udc(dev, &gpriv->gadget);
1178 dev_info(dev, "gadget probed\n");
1185 usbhs_mod_gadget_probe_err_gpriv:
1191 void usbhs_mod_gadget_remove(struct usbhs_priv *priv)
1193 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
1195 usb_del_gadget_udc(&gpriv->gadget);