4 * Copyright (C) 2011 Renesas Solutions Corp.
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;
41 char ep_name[EP_NAME_SIZE];
43 struct usbhsg_gpriv *gpriv;
47 struct usb_gadget gadget;
50 struct usbhsg_uep *uep;
53 struct usb_gadget_driver *driver;
54 struct usb_phy *transceiver;
58 #define USBHSG_STATUS_STARTED (1 << 0)
59 #define USBHSG_STATUS_REGISTERD (1 << 1)
60 #define USBHSG_STATUS_WEDGE (1 << 2)
61 #define USBHSG_STATUS_SELF_POWERED (1 << 3)
62 #define USBHSG_STATUS_SOFT_CONNECT (1 << 4)
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) \
85 ((i) < (g)->uep_size) && ((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_dev(gp) usbhs_priv_to_dev((gp)->mod.priv)
102 #define usbhsg_gpriv_to_priv(gp) ((gp)->mod.priv)
103 #define usbhsg_gpriv_to_dcp(gp) ((gp)->uep)
104 #define usbhsg_gpriv_to_nth_uep(gp, i) ((gp)->uep + i)
105 #define usbhsg_uep_to_gpriv(u) ((u)->gpriv)
106 #define usbhsg_uep_to_pipe(u) ((u)->pipe)
107 #define usbhsg_pipe_to_uep(p) ((p)->mod_private)
108 #define usbhsg_is_dcp(u) ((u) == usbhsg_gpriv_to_dcp((u)->gpriv))
110 #define usbhsg_ureq_to_pkt(u) (&(u)->pkt)
111 #define usbhsg_pkt_to_ureq(i) \
112 container_of(i, struct usbhsg_request, pkt)
114 #define usbhsg_is_not_connected(gp) ((gp)->gadget.speed == USB_SPEED_UNKNOWN)
117 #define usbhsg_status_init(gp) do {(gp)->status = 0; } while (0)
118 #define usbhsg_status_set(gp, b) (gp->status |= b)
119 #define usbhsg_status_clr(gp, b) (gp->status &= ~b)
120 #define usbhsg_status_has(gp, b) (gp->status & b)
125 static void __usbhsg_queue_pop(struct usbhsg_uep *uep,
126 struct usbhsg_request *ureq,
129 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
130 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
131 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
132 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
135 dev_dbg(dev, "pipe %d : queue pop\n", usbhs_pipe_number(pipe));
137 ureq->req.status = status;
138 spin_unlock(usbhs_priv_to_lock(priv));
139 usb_gadget_giveback_request(&uep->ep, &ureq->req);
140 spin_lock(usbhs_priv_to_lock(priv));
143 static void usbhsg_queue_pop(struct usbhsg_uep *uep,
144 struct usbhsg_request *ureq,
147 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
148 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
151 usbhs_lock(priv, flags);
152 __usbhsg_queue_pop(uep, ureq, status);
153 usbhs_unlock(priv, flags);
156 static void usbhsg_queue_done(struct usbhs_priv *priv, struct usbhs_pkt *pkt)
158 struct usbhs_pipe *pipe = pkt->pipe;
159 struct usbhsg_uep *uep = usbhsg_pipe_to_uep(pipe);
160 struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt);
162 ureq->req.actual = pkt->actual;
164 usbhsg_queue_pop(uep, ureq, 0);
167 static void usbhsg_queue_push(struct usbhsg_uep *uep,
168 struct usbhsg_request *ureq)
170 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
171 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
172 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
173 struct usbhs_pkt *pkt = usbhsg_ureq_to_pkt(ureq);
174 struct usb_request *req = &ureq->req;
177 req->status = -EINPROGRESS;
178 usbhs_pkt_push(pipe, pkt, usbhsg_queue_done,
179 req->buf, req->length, req->zero, -1);
180 usbhs_pkt_start(pipe);
182 dev_dbg(dev, "pipe %d : queue push (%d)\n",
183 usbhs_pipe_number(pipe),
190 static int usbhsg_dma_map_ctrl(struct usbhs_pkt *pkt, int map)
192 struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt);
193 struct usb_request *req = &ureq->req;
194 struct usbhs_pipe *pipe = pkt->pipe;
195 struct usbhsg_uep *uep = usbhsg_pipe_to_uep(pipe);
196 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
197 enum dma_data_direction dir;
200 dir = usbhs_pipe_is_dir_host(pipe);
203 /* it can not use scatter/gather */
204 WARN_ON(req->num_sgs);
206 ret = usb_gadget_map_request(&gpriv->gadget, req, dir);
212 usb_gadget_unmap_request(&gpriv->gadget, req, dir);
219 * USB_TYPE_STANDARD / clear feature functions
221 static int usbhsg_recip_handler_std_control_done(struct usbhs_priv *priv,
222 struct usbhsg_uep *uep,
223 struct usb_ctrlrequest *ctrl)
225 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
226 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
227 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
229 usbhs_dcp_control_transfer_done(pipe);
234 static int usbhsg_recip_handler_std_clear_endpoint(struct usbhs_priv *priv,
235 struct usbhsg_uep *uep,
236 struct usb_ctrlrequest *ctrl)
238 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
239 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
241 if (!usbhsg_status_has(gpriv, USBHSG_STATUS_WEDGE)) {
242 usbhs_pipe_disable(pipe);
243 usbhs_pipe_sequence_data0(pipe);
244 usbhs_pipe_enable(pipe);
247 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
249 usbhs_pkt_start(pipe);
254 static struct usbhsg_recip_handle req_clear_feature = {
255 .name = "clear feature",
256 .device = usbhsg_recip_handler_std_control_done,
257 .interface = usbhsg_recip_handler_std_control_done,
258 .endpoint = usbhsg_recip_handler_std_clear_endpoint,
262 * USB_TYPE_STANDARD / set feature functions
264 static int usbhsg_recip_handler_std_set_device(struct usbhs_priv *priv,
265 struct usbhsg_uep *uep,
266 struct usb_ctrlrequest *ctrl)
268 switch (le16_to_cpu(ctrl->wValue)) {
269 case USB_DEVICE_TEST_MODE:
270 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
272 usbhs_sys_set_test_mode(priv, le16_to_cpu(ctrl->wIndex >> 8));
275 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
282 static int usbhsg_recip_handler_std_set_endpoint(struct usbhs_priv *priv,
283 struct usbhsg_uep *uep,
284 struct usb_ctrlrequest *ctrl)
286 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
288 usbhs_pipe_stall(pipe);
290 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
295 static struct usbhsg_recip_handle req_set_feature = {
296 .name = "set feature",
297 .device = usbhsg_recip_handler_std_set_device,
298 .interface = usbhsg_recip_handler_std_control_done,
299 .endpoint = usbhsg_recip_handler_std_set_endpoint,
303 * USB_TYPE_STANDARD / get status functions
305 static void __usbhsg_recip_send_complete(struct usb_ep *ep,
306 struct usb_request *req)
308 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
310 /* free allocated recip-buffer/usb_request */
311 kfree(ureq->pkt.buf);
312 usb_ep_free_request(ep, req);
315 static void __usbhsg_recip_send_status(struct usbhsg_gpriv *gpriv,
316 unsigned short status)
318 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
319 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
320 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
321 struct usb_request *req;
324 /* alloc new usb_request for recip */
325 req = usb_ep_alloc_request(&dcp->ep, GFP_ATOMIC);
327 dev_err(dev, "recip request allocation fail\n");
331 /* alloc recip data buffer */
332 buf = kmalloc(sizeof(*buf), GFP_ATOMIC);
334 usb_ep_free_request(&dcp->ep, req);
335 dev_err(dev, "recip data allocation fail\n");
339 /* recip data is status */
340 *buf = cpu_to_le16(status);
342 /* allocated usb_request/buffer will be freed */
343 req->complete = __usbhsg_recip_send_complete;
345 req->length = sizeof(*buf);
349 pipe->handler = &usbhs_fifo_pio_push_handler;
350 usbhsg_queue_push(dcp, usbhsg_req_to_ureq(req));
353 static int usbhsg_recip_handler_std_get_device(struct usbhs_priv *priv,
354 struct usbhsg_uep *uep,
355 struct usb_ctrlrequest *ctrl)
357 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
358 unsigned short status = 0;
360 if (usbhsg_status_has(gpriv, USBHSG_STATUS_SELF_POWERED))
361 status = 1 << USB_DEVICE_SELF_POWERED;
363 __usbhsg_recip_send_status(gpriv, status);
368 static int usbhsg_recip_handler_std_get_interface(struct usbhs_priv *priv,
369 struct usbhsg_uep *uep,
370 struct usb_ctrlrequest *ctrl)
372 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
373 unsigned short status = 0;
375 __usbhsg_recip_send_status(gpriv, status);
380 static int usbhsg_recip_handler_std_get_endpoint(struct usbhs_priv *priv,
381 struct usbhsg_uep *uep,
382 struct usb_ctrlrequest *ctrl)
384 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
385 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
386 unsigned short status = 0;
388 if (usbhs_pipe_is_stall(pipe))
389 status = 1 << USB_ENDPOINT_HALT;
391 __usbhsg_recip_send_status(gpriv, status);
396 static struct usbhsg_recip_handle req_get_status = {
397 .name = "get status",
398 .device = usbhsg_recip_handler_std_get_device,
399 .interface = usbhsg_recip_handler_std_get_interface,
400 .endpoint = usbhsg_recip_handler_std_get_endpoint,
406 static int usbhsg_recip_run_handle(struct usbhs_priv *priv,
407 struct usbhsg_recip_handle *handler,
408 struct usb_ctrlrequest *ctrl)
410 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
411 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
412 struct usbhsg_uep *uep;
413 struct usbhs_pipe *pipe;
414 int recip = ctrl->bRequestType & USB_RECIP_MASK;
415 int nth = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK;
417 int (*func)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
418 struct usb_ctrlrequest *ctrl);
421 uep = usbhsg_gpriv_to_nth_uep(gpriv, nth);
422 pipe = usbhsg_uep_to_pipe(uep);
424 dev_err(dev, "wrong recip request\n");
429 case USB_RECIP_DEVICE:
431 func = handler->device;
433 case USB_RECIP_INTERFACE:
435 func = handler->interface;
437 case USB_RECIP_ENDPOINT:
439 func = handler->endpoint;
442 dev_warn(dev, "unsupported RECIP(%d)\n", recip);
448 dev_dbg(dev, "%s (pipe %d :%s)\n", handler->name, nth, msg);
449 ret = func(priv, uep, ctrl);
458 * it will be called from usbhs_interrupt
460 static int usbhsg_irq_dev_state(struct usbhs_priv *priv,
461 struct usbhs_irq_state *irq_state)
463 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
464 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
466 gpriv->gadget.speed = usbhs_bus_get_speed(priv);
468 dev_dbg(dev, "state = %x : speed : %d\n",
469 usbhs_status_get_device_state(irq_state),
470 gpriv->gadget.speed);
475 static int usbhsg_irq_ctrl_stage(struct usbhs_priv *priv,
476 struct usbhs_irq_state *irq_state)
478 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
479 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
480 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
481 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
482 struct usb_ctrlrequest ctrl;
483 struct usbhsg_recip_handle *recip_handler = NULL;
484 int stage = usbhs_status_get_ctrl_stage(irq_state);
487 dev_dbg(dev, "stage = %d\n", stage);
493 * - "Interrupt Function"
494 * - "Control Transfer Stage Transition Interrupt"
495 * - Fig. "Control Transfer Stage Transitions"
499 case READ_DATA_STAGE:
500 pipe->handler = &usbhs_fifo_pio_push_handler;
502 case WRITE_DATA_STAGE:
503 pipe->handler = &usbhs_fifo_pio_pop_handler;
505 case NODATA_STATUS_STAGE:
506 pipe->handler = &usbhs_ctrl_stage_end_handler;
508 case READ_STATUS_STAGE:
509 case WRITE_STATUS_STAGE:
510 usbhs_dcp_control_transfer_done(pipe);
518 usbhs_usbreq_get_val(priv, &ctrl);
520 switch (ctrl.bRequestType & USB_TYPE_MASK) {
521 case USB_TYPE_STANDARD:
522 switch (ctrl.bRequest) {
523 case USB_REQ_CLEAR_FEATURE:
524 recip_handler = &req_clear_feature;
526 case USB_REQ_SET_FEATURE:
527 recip_handler = &req_set_feature;
529 case USB_REQ_GET_STATUS:
530 recip_handler = &req_get_status;
536 * setup stage / run recip
539 ret = usbhsg_recip_run_handle(priv, recip_handler, &ctrl);
541 ret = gpriv->driver->setup(&gpriv->gadget, &ctrl);
544 usbhs_pipe_stall(pipe);
554 static int usbhsg_pipe_disable(struct usbhsg_uep *uep)
556 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
557 struct usbhs_pkt *pkt;
560 pkt = usbhs_pkt_pop(pipe, NULL);
564 usbhsg_queue_pop(uep, usbhsg_pkt_to_ureq(pkt), -ECONNRESET);
567 usbhs_pipe_disable(pipe);
577 static int usbhsg_ep_enable(struct usb_ep *ep,
578 const struct usb_endpoint_descriptor *desc)
580 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
581 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
582 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
583 struct usbhs_pipe *pipe;
587 * if it already have pipe,
591 usbhs_pipe_clear(uep->pipe);
592 usbhs_pipe_sequence_data0(uep->pipe);
596 pipe = usbhs_pipe_malloc(priv,
597 usb_endpoint_type(desc),
598 usb_endpoint_dir_in(desc));
601 pipe->mod_private = uep;
603 /* set epnum / maxp */
604 usbhs_pipe_config_update(pipe, 0,
605 usb_endpoint_num(desc),
606 usb_endpoint_maxp(desc));
609 * usbhs_fifo_dma_push/pop_handler try to
610 * use dmaengine if possible.
611 * It will use pio handler if impossible.
613 if (usb_endpoint_dir_in(desc))
614 pipe->handler = &usbhs_fifo_dma_push_handler;
616 pipe->handler = &usbhs_fifo_dma_pop_handler;
624 static int usbhsg_ep_disable(struct usb_ep *ep)
626 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
627 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
632 usbhsg_pipe_disable(uep);
633 usbhs_pipe_free(pipe);
635 uep->pipe->mod_private = NULL;
641 static struct usb_request *usbhsg_ep_alloc_request(struct usb_ep *ep,
644 struct usbhsg_request *ureq;
646 ureq = kzalloc(sizeof *ureq, gfp_flags);
650 usbhs_pkt_init(usbhsg_ureq_to_pkt(ureq));
655 static void usbhsg_ep_free_request(struct usb_ep *ep,
656 struct usb_request *req)
658 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
660 WARN_ON(!list_empty(&ureq->pkt.node));
664 static int usbhsg_ep_queue(struct usb_ep *ep, struct usb_request *req,
667 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
668 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
669 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
670 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
673 if (usbhsg_is_not_connected(gpriv) ||
674 unlikely(!gpriv->driver) ||
678 usbhsg_queue_push(uep, ureq);
683 static int usbhsg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
685 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
686 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
687 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
690 usbhs_pkt_pop(pipe, usbhsg_ureq_to_pkt(ureq));
693 * To dequeue a request, this driver should call the usbhsg_queue_pop()
694 * even if the pipe is NULL.
696 usbhsg_queue_pop(uep, ureq, -ECONNRESET);
701 static int __usbhsg_ep_set_halt_wedge(struct usb_ep *ep, int halt, int wedge)
703 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
704 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
705 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
706 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
707 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
710 usbhsg_pipe_disable(uep);
712 dev_dbg(dev, "set halt %d (pipe %d)\n",
713 halt, usbhs_pipe_number(pipe));
715 /******************** spin lock ********************/
716 usbhs_lock(priv, flags);
719 usbhs_pipe_stall(pipe);
721 usbhs_pipe_disable(pipe);
724 usbhsg_status_set(gpriv, USBHSG_STATUS_WEDGE);
726 usbhsg_status_clr(gpriv, USBHSG_STATUS_WEDGE);
728 usbhs_unlock(priv, flags);
729 /******************** spin unlock ******************/
734 static int usbhsg_ep_set_halt(struct usb_ep *ep, int value)
736 return __usbhsg_ep_set_halt_wedge(ep, value, 0);
739 static int usbhsg_ep_set_wedge(struct usb_ep *ep)
741 return __usbhsg_ep_set_halt_wedge(ep, 1, 1);
744 static struct usb_ep_ops usbhsg_ep_ops = {
745 .enable = usbhsg_ep_enable,
746 .disable = usbhsg_ep_disable,
748 .alloc_request = usbhsg_ep_alloc_request,
749 .free_request = usbhsg_ep_free_request,
751 .queue = usbhsg_ep_queue,
752 .dequeue = usbhsg_ep_dequeue,
754 .set_halt = usbhsg_ep_set_halt,
755 .set_wedge = usbhsg_ep_set_wedge,
761 static int usbhsg_can_pullup(struct usbhs_priv *priv)
763 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
765 return gpriv->driver &&
766 usbhsg_status_has(gpriv, USBHSG_STATUS_SOFT_CONNECT);
769 static void usbhsg_update_pullup(struct usbhs_priv *priv)
771 if (usbhsg_can_pullup(priv))
772 usbhs_sys_function_pullup(priv, 1);
774 usbhs_sys_function_pullup(priv, 0);
778 * usb module start/end
780 static int usbhsg_try_start(struct usbhs_priv *priv, u32 status)
782 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
783 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
784 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
785 struct device *dev = usbhs_priv_to_dev(priv);
789 /******************** spin lock ********************/
790 usbhs_lock(priv, flags);
792 usbhsg_status_set(gpriv, status);
793 if (!(usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
794 usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD)))
795 ret = -1; /* not ready */
797 usbhs_unlock(priv, flags);
798 /******************** spin unlock ********************/
801 return 0; /* not ready is not error */
804 * enable interrupt and systems if ready
806 dev_dbg(dev, "start gadget\n");
809 * pipe initialize and enable DCP
811 usbhs_fifo_init(priv);
812 usbhs_pipe_init(priv,
813 usbhsg_dma_map_ctrl);
815 /* dcp init instead of usbhsg_ep_enable() */
816 dcp->pipe = usbhs_dcp_malloc(priv);
817 dcp->pipe->mod_private = dcp;
818 usbhs_pipe_config_update(dcp->pipe, 0, 0, 64);
821 * system config enble
826 usbhs_sys_function_ctrl(priv, 1);
827 usbhsg_update_pullup(priv);
830 * enable irq callback
832 mod->irq_dev_state = usbhsg_irq_dev_state;
833 mod->irq_ctrl_stage = usbhsg_irq_ctrl_stage;
834 usbhs_irq_callback_update(priv, mod);
839 static int usbhsg_try_stop(struct usbhs_priv *priv, u32 status)
841 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
842 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
843 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
844 struct device *dev = usbhs_priv_to_dev(priv);
848 /******************** spin lock ********************/
849 usbhs_lock(priv, flags);
851 usbhsg_status_clr(gpriv, status);
852 if (!usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
853 !usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD))
854 ret = -1; /* already done */
856 usbhs_unlock(priv, flags);
857 /******************** spin unlock ********************/
860 return 0; /* already done is not error */
863 * disable interrupt and systems if 1st try
865 usbhs_fifo_quit(priv);
867 /* disable all irq */
868 mod->irq_dev_state = NULL;
869 mod->irq_ctrl_stage = NULL;
870 usbhs_irq_callback_update(priv, mod);
872 gpriv->gadget.speed = USB_SPEED_UNKNOWN;
875 usbhs_sys_set_test_mode(priv, 0);
876 usbhs_sys_function_ctrl(priv, 0);
878 usbhsg_ep_disable(&dcp->ep);
880 dev_dbg(dev, "stop gadget\n");
886 * VBUS provided by the PHY
888 static int usbhsm_phy_get_vbus(struct platform_device *pdev)
890 struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
891 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
893 return gpriv->vbus_active;
896 static void usbhs_mod_phy_mode(struct usbhs_priv *priv)
898 struct usbhs_mod_info *info = &priv->mod_info;
900 info->irq_vbus = NULL;
901 priv->pfunc.get_vbus = usbhsm_phy_get_vbus;
903 usbhs_irq_callback_update(priv, NULL);
911 static int usbhsg_gadget_start(struct usb_gadget *gadget,
912 struct usb_gadget_driver *driver)
914 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
915 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
916 struct device *dev = usbhs_priv_to_dev(priv);
921 driver->max_speed < USB_SPEED_FULL)
924 /* connect to bus through transceiver */
925 if (!IS_ERR_OR_NULL(gpriv->transceiver)) {
926 ret = otg_set_peripheral(gpriv->transceiver->otg,
929 dev_err(dev, "%s: can't bind to transceiver\n",
934 /* get vbus using phy versions */
935 usbhs_mod_phy_mode(priv);
938 /* first hook up the driver ... */
939 gpriv->driver = driver;
941 return usbhsg_try_start(priv, USBHSG_STATUS_REGISTERD);
944 static int usbhsg_gadget_stop(struct usb_gadget *gadget)
946 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
947 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
949 usbhsg_try_stop(priv, USBHSG_STATUS_REGISTERD);
951 if (!IS_ERR_OR_NULL(gpriv->transceiver))
952 otg_set_peripheral(gpriv->transceiver->otg, NULL);
954 gpriv->driver = NULL;
962 static int usbhsg_get_frame(struct usb_gadget *gadget)
964 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
965 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
967 return usbhs_frame_get_num(priv);
970 static int usbhsg_pullup(struct usb_gadget *gadget, int is_on)
972 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
973 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
976 usbhs_lock(priv, flags);
978 usbhsg_status_set(gpriv, USBHSG_STATUS_SOFT_CONNECT);
980 usbhsg_status_clr(gpriv, USBHSG_STATUS_SOFT_CONNECT);
981 usbhsg_update_pullup(priv);
982 usbhs_unlock(priv, flags);
987 static int usbhsg_set_selfpowered(struct usb_gadget *gadget, int is_self)
989 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
992 usbhsg_status_set(gpriv, USBHSG_STATUS_SELF_POWERED);
994 usbhsg_status_clr(gpriv, USBHSG_STATUS_SELF_POWERED);
996 gadget->is_selfpowered = (is_self != 0);
1001 static int usbhsg_vbus_session(struct usb_gadget *gadget, int is_active)
1003 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
1004 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
1005 struct platform_device *pdev = usbhs_priv_to_pdev(priv);
1007 gpriv->vbus_active = !!is_active;
1009 renesas_usbhs_call_notify_hotplug(pdev);
1014 static const struct usb_gadget_ops usbhsg_gadget_ops = {
1015 .get_frame = usbhsg_get_frame,
1016 .set_selfpowered = usbhsg_set_selfpowered,
1017 .udc_start = usbhsg_gadget_start,
1018 .udc_stop = usbhsg_gadget_stop,
1019 .pullup = usbhsg_pullup,
1020 .vbus_session = usbhsg_vbus_session,
1023 static int usbhsg_start(struct usbhs_priv *priv)
1025 return usbhsg_try_start(priv, USBHSG_STATUS_STARTED);
1028 static int usbhsg_stop(struct usbhs_priv *priv)
1030 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
1032 /* cable disconnect */
1033 if (gpriv->driver &&
1034 gpriv->driver->disconnect)
1035 gpriv->driver->disconnect(&gpriv->gadget);
1037 return usbhsg_try_stop(priv, USBHSG_STATUS_STARTED);
1040 int usbhs_mod_gadget_probe(struct usbhs_priv *priv)
1042 struct usbhsg_gpriv *gpriv;
1043 struct usbhsg_uep *uep;
1044 struct device *dev = usbhs_priv_to_dev(priv);
1045 struct renesas_usbhs_driver_pipe_config *pipe_configs =
1046 usbhs_get_dparam(priv, pipe_configs);
1047 int pipe_size = usbhs_get_dparam(priv, pipe_size);
1051 gpriv = kzalloc(sizeof(struct usbhsg_gpriv), GFP_KERNEL);
1053 dev_err(dev, "Could not allocate gadget priv\n");
1057 uep = kzalloc(sizeof(struct usbhsg_uep) * pipe_size, GFP_KERNEL);
1059 dev_err(dev, "Could not allocate ep\n");
1061 goto usbhs_mod_gadget_probe_err_gpriv;
1064 gpriv->transceiver = usb_get_phy(USB_PHY_TYPE_UNDEFINED);
1065 dev_info(dev, "%stransceiver found\n",
1066 gpriv->transceiver ? "" : "no ");
1071 * There is no guarantee that it is possible to access usb module here.
1072 * Don't accesses to it.
1073 * The accesse will be enable after "usbhsg_start"
1079 usbhs_mod_register(priv, &gpriv->mod, USBHS_GADGET);
1082 gpriv->mod.name = "gadget";
1083 gpriv->mod.start = usbhsg_start;
1084 gpriv->mod.stop = usbhsg_stop;
1086 gpriv->uep_size = pipe_size;
1087 usbhsg_status_init(gpriv);
1092 gpriv->gadget.dev.parent = dev;
1093 gpriv->gadget.name = "renesas_usbhs_udc";
1094 gpriv->gadget.ops = &usbhsg_gadget_ops;
1095 gpriv->gadget.max_speed = USB_SPEED_HIGH;
1097 INIT_LIST_HEAD(&gpriv->gadget.ep_list);
1102 usbhsg_for_each_uep_with_dcp(uep, gpriv, i) {
1105 snprintf(uep->ep_name, EP_NAME_SIZE, "ep%d", i);
1107 uep->ep.name = uep->ep_name;
1108 uep->ep.ops = &usbhsg_ep_ops;
1109 INIT_LIST_HEAD(&uep->ep.ep_list);
1112 if (usbhsg_is_dcp(uep)) {
1113 gpriv->gadget.ep0 = &uep->ep;
1114 usb_ep_set_maxpacket_limit(&uep->ep, 64);
1115 uep->ep.caps.type_control = true;
1117 /* init normal pipe */
1118 if (pipe_configs[i].type == USB_ENDPOINT_XFER_ISOC)
1119 uep->ep.caps.type_iso = true;
1120 if (pipe_configs[i].type == USB_ENDPOINT_XFER_BULK)
1121 uep->ep.caps.type_bulk = true;
1122 if (pipe_configs[i].type == USB_ENDPOINT_XFER_INT)
1123 uep->ep.caps.type_int = true;
1124 usb_ep_set_maxpacket_limit(&uep->ep,
1125 pipe_configs[i].bufsize);
1126 list_add_tail(&uep->ep.ep_list, &gpriv->gadget.ep_list);
1128 uep->ep.caps.dir_in = true;
1129 uep->ep.caps.dir_out = true;
1132 ret = usb_add_gadget_udc(dev, &gpriv->gadget);
1137 dev_info(dev, "gadget probed\n");
1144 usbhs_mod_gadget_probe_err_gpriv:
1150 void usbhs_mod_gadget_remove(struct usbhs_priv *priv)
1152 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
1154 usb_del_gadget_udc(&gpriv->gadget);