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;
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);
134 dev_dbg(dev, "pipe %d : queue pop\n", usbhs_pipe_number(pipe));
136 ureq->req.status = status;
137 spin_unlock(usbhs_priv_to_lock(priv));
138 usb_gadget_giveback_request(&uep->ep, &ureq->req);
139 spin_lock(usbhs_priv_to_lock(priv));
142 static void usbhsg_queue_pop(struct usbhsg_uep *uep,
143 struct usbhsg_request *ureq,
146 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
147 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
150 usbhs_lock(priv, flags);
151 __usbhsg_queue_pop(uep, ureq, status);
152 usbhs_unlock(priv, flags);
155 static void usbhsg_queue_done(struct usbhs_priv *priv, struct usbhs_pkt *pkt)
157 struct usbhs_pipe *pipe = pkt->pipe;
158 struct usbhsg_uep *uep = usbhsg_pipe_to_uep(pipe);
159 struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt);
161 ureq->req.actual = pkt->actual;
163 usbhsg_queue_pop(uep, ureq, 0);
166 static void usbhsg_queue_push(struct usbhsg_uep *uep,
167 struct usbhsg_request *ureq)
169 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
170 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
171 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
172 struct usbhs_pkt *pkt = usbhsg_ureq_to_pkt(ureq);
173 struct usb_request *req = &ureq->req;
176 req->status = -EINPROGRESS;
177 usbhs_pkt_push(pipe, pkt, usbhsg_queue_done,
178 req->buf, req->length, req->zero, -1);
179 usbhs_pkt_start(pipe);
181 dev_dbg(dev, "pipe %d : queue push (%d)\n",
182 usbhs_pipe_number(pipe),
189 static int usbhsg_dma_map_ctrl(struct usbhs_pkt *pkt, int map)
191 struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt);
192 struct usb_request *req = &ureq->req;
193 struct usbhs_pipe *pipe = pkt->pipe;
194 struct usbhsg_uep *uep = usbhsg_pipe_to_uep(pipe);
195 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
196 enum dma_data_direction dir;
199 dir = usbhs_pipe_is_dir_host(pipe);
202 /* it can not use scatter/gather */
203 WARN_ON(req->num_sgs);
205 ret = usb_gadget_map_request(&gpriv->gadget, req, dir);
211 usb_gadget_unmap_request(&gpriv->gadget, req, dir);
218 * USB_TYPE_STANDARD / clear feature functions
220 static int usbhsg_recip_handler_std_control_done(struct usbhs_priv *priv,
221 struct usbhsg_uep *uep,
222 struct usb_ctrlrequest *ctrl)
224 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
225 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
226 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
228 usbhs_dcp_control_transfer_done(pipe);
233 static int usbhsg_recip_handler_std_clear_endpoint(struct usbhs_priv *priv,
234 struct usbhsg_uep *uep,
235 struct usb_ctrlrequest *ctrl)
237 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
238 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
240 if (!usbhsg_status_has(gpriv, USBHSG_STATUS_WEDGE)) {
241 usbhs_pipe_disable(pipe);
242 usbhs_pipe_sequence_data0(pipe);
243 usbhs_pipe_enable(pipe);
246 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
248 usbhs_pkt_start(pipe);
253 static struct usbhsg_recip_handle req_clear_feature = {
254 .name = "clear feature",
255 .device = usbhsg_recip_handler_std_control_done,
256 .interface = usbhsg_recip_handler_std_control_done,
257 .endpoint = usbhsg_recip_handler_std_clear_endpoint,
261 * USB_TYPE_STANDARD / set feature functions
263 static int usbhsg_recip_handler_std_set_device(struct usbhs_priv *priv,
264 struct usbhsg_uep *uep,
265 struct usb_ctrlrequest *ctrl)
267 switch (le16_to_cpu(ctrl->wValue)) {
268 case USB_DEVICE_TEST_MODE:
269 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
271 usbhs_sys_set_test_mode(priv, le16_to_cpu(ctrl->wIndex >> 8));
274 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
281 static int usbhsg_recip_handler_std_set_endpoint(struct usbhs_priv *priv,
282 struct usbhsg_uep *uep,
283 struct usb_ctrlrequest *ctrl)
285 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
287 usbhs_pipe_stall(pipe);
289 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
294 static struct usbhsg_recip_handle req_set_feature = {
295 .name = "set feature",
296 .device = usbhsg_recip_handler_std_set_device,
297 .interface = usbhsg_recip_handler_std_control_done,
298 .endpoint = usbhsg_recip_handler_std_set_endpoint,
302 * USB_TYPE_STANDARD / get status functions
304 static void __usbhsg_recip_send_complete(struct usb_ep *ep,
305 struct usb_request *req)
307 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
309 /* free allocated recip-buffer/usb_request */
310 kfree(ureq->pkt.buf);
311 usb_ep_free_request(ep, req);
314 static void __usbhsg_recip_send_status(struct usbhsg_gpriv *gpriv,
315 unsigned short status)
317 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
318 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
319 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
320 struct usb_request *req;
323 /* alloc new usb_request for recip */
324 req = usb_ep_alloc_request(&dcp->ep, GFP_ATOMIC);
326 dev_err(dev, "recip request allocation fail\n");
330 /* alloc recip data buffer */
331 buf = kmalloc(sizeof(*buf), GFP_ATOMIC);
333 usb_ep_free_request(&dcp->ep, req);
334 dev_err(dev, "recip data allocation fail\n");
338 /* recip data is status */
339 *buf = cpu_to_le16(status);
341 /* allocated usb_request/buffer will be freed */
342 req->complete = __usbhsg_recip_send_complete;
344 req->length = sizeof(*buf);
348 pipe->handler = &usbhs_fifo_pio_push_handler;
349 usbhsg_queue_push(dcp, usbhsg_req_to_ureq(req));
352 static int usbhsg_recip_handler_std_get_device(struct usbhs_priv *priv,
353 struct usbhsg_uep *uep,
354 struct usb_ctrlrequest *ctrl)
356 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
357 unsigned short status = 0;
359 if (usbhsg_status_has(gpriv, USBHSG_STATUS_SELF_POWERED))
360 status = 1 << USB_DEVICE_SELF_POWERED;
362 __usbhsg_recip_send_status(gpriv, status);
367 static int usbhsg_recip_handler_std_get_interface(struct usbhs_priv *priv,
368 struct usbhsg_uep *uep,
369 struct usb_ctrlrequest *ctrl)
371 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
372 unsigned short status = 0;
374 __usbhsg_recip_send_status(gpriv, status);
379 static int usbhsg_recip_handler_std_get_endpoint(struct usbhs_priv *priv,
380 struct usbhsg_uep *uep,
381 struct usb_ctrlrequest *ctrl)
383 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
384 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
385 unsigned short status = 0;
387 if (usbhs_pipe_is_stall(pipe))
388 status = 1 << USB_ENDPOINT_HALT;
390 __usbhsg_recip_send_status(gpriv, status);
395 static struct usbhsg_recip_handle req_get_status = {
396 .name = "get status",
397 .device = usbhsg_recip_handler_std_get_device,
398 .interface = usbhsg_recip_handler_std_get_interface,
399 .endpoint = usbhsg_recip_handler_std_get_endpoint,
405 static int usbhsg_recip_run_handle(struct usbhs_priv *priv,
406 struct usbhsg_recip_handle *handler,
407 struct usb_ctrlrequest *ctrl)
409 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
410 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
411 struct usbhsg_uep *uep;
412 struct usbhs_pipe *pipe;
413 int recip = ctrl->bRequestType & USB_RECIP_MASK;
414 int nth = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK;
416 int (*func)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
417 struct usb_ctrlrequest *ctrl);
420 uep = usbhsg_gpriv_to_nth_uep(gpriv, nth);
421 pipe = usbhsg_uep_to_pipe(uep);
423 dev_err(dev, "wrong recip request\n");
428 case USB_RECIP_DEVICE:
430 func = handler->device;
432 case USB_RECIP_INTERFACE:
434 func = handler->interface;
436 case USB_RECIP_ENDPOINT:
438 func = handler->endpoint;
441 dev_warn(dev, "unsupported RECIP(%d)\n", recip);
447 dev_dbg(dev, "%s (pipe %d :%s)\n", handler->name, nth, msg);
448 ret = func(priv, uep, ctrl);
457 * it will be called from usbhs_interrupt
459 static int usbhsg_irq_dev_state(struct usbhs_priv *priv,
460 struct usbhs_irq_state *irq_state)
462 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
463 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
465 gpriv->gadget.speed = usbhs_bus_get_speed(priv);
467 dev_dbg(dev, "state = %x : speed : %d\n",
468 usbhs_status_get_device_state(irq_state),
469 gpriv->gadget.speed);
474 static int usbhsg_irq_ctrl_stage(struct usbhs_priv *priv,
475 struct usbhs_irq_state *irq_state)
477 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
478 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
479 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
480 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
481 struct usb_ctrlrequest ctrl;
482 struct usbhsg_recip_handle *recip_handler = NULL;
483 int stage = usbhs_status_get_ctrl_stage(irq_state);
486 dev_dbg(dev, "stage = %d\n", stage);
492 * - "Interrupt Function"
493 * - "Control Transfer Stage Transition Interrupt"
494 * - Fig. "Control Transfer Stage Transitions"
498 case READ_DATA_STAGE:
499 pipe->handler = &usbhs_fifo_pio_push_handler;
501 case WRITE_DATA_STAGE:
502 pipe->handler = &usbhs_fifo_pio_pop_handler;
504 case NODATA_STATUS_STAGE:
505 pipe->handler = &usbhs_ctrl_stage_end_handler;
507 case READ_STATUS_STAGE:
508 case WRITE_STATUS_STAGE:
509 usbhs_dcp_control_transfer_done(pipe);
517 usbhs_usbreq_get_val(priv, &ctrl);
519 switch (ctrl.bRequestType & USB_TYPE_MASK) {
520 case USB_TYPE_STANDARD:
521 switch (ctrl.bRequest) {
522 case USB_REQ_CLEAR_FEATURE:
523 recip_handler = &req_clear_feature;
525 case USB_REQ_SET_FEATURE:
526 recip_handler = &req_set_feature;
528 case USB_REQ_GET_STATUS:
529 recip_handler = &req_get_status;
535 * setup stage / run recip
538 ret = usbhsg_recip_run_handle(priv, recip_handler, &ctrl);
540 ret = gpriv->driver->setup(&gpriv->gadget, &ctrl);
543 usbhs_pipe_stall(pipe);
553 static int usbhsg_pipe_disable(struct usbhsg_uep *uep)
555 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
556 struct usbhs_pkt *pkt;
559 pkt = usbhs_pkt_pop(pipe, NULL);
563 usbhsg_queue_pop(uep, usbhsg_pkt_to_ureq(pkt), -ECONNRESET);
566 usbhs_pipe_disable(pipe);
576 static int usbhsg_ep_enable(struct usb_ep *ep,
577 const struct usb_endpoint_descriptor *desc)
579 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
580 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
581 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
582 struct usbhs_pipe *pipe;
586 * if it already have pipe,
590 usbhs_pipe_clear(uep->pipe);
591 usbhs_pipe_sequence_data0(uep->pipe);
595 pipe = usbhs_pipe_malloc(priv,
596 usb_endpoint_type(desc),
597 usb_endpoint_dir_in(desc));
600 pipe->mod_private = uep;
602 /* set epnum / maxp */
603 usbhs_pipe_config_update(pipe, 0,
604 usb_endpoint_num(desc),
605 usb_endpoint_maxp(desc));
608 * usbhs_fifo_dma_push/pop_handler try to
609 * use dmaengine if possible.
610 * It will use pio handler if impossible.
612 if (usb_endpoint_dir_in(desc))
613 pipe->handler = &usbhs_fifo_dma_push_handler;
615 pipe->handler = &usbhs_fifo_dma_pop_handler;
623 static int usbhsg_ep_disable(struct usb_ep *ep)
625 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
626 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
631 usbhsg_pipe_disable(uep);
632 usbhs_pipe_free(pipe);
634 uep->pipe->mod_private = NULL;
640 static struct usb_request *usbhsg_ep_alloc_request(struct usb_ep *ep,
643 struct usbhsg_request *ureq;
645 ureq = kzalloc(sizeof *ureq, gfp_flags);
649 usbhs_pkt_init(usbhsg_ureq_to_pkt(ureq));
654 static void usbhsg_ep_free_request(struct usb_ep *ep,
655 struct usb_request *req)
657 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
659 WARN_ON(!list_empty(&ureq->pkt.node));
663 static int usbhsg_ep_queue(struct usb_ep *ep, struct usb_request *req,
666 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
667 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
668 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
669 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
672 if (usbhsg_is_not_connected(gpriv) ||
673 unlikely(!gpriv->driver) ||
677 usbhsg_queue_push(uep, ureq);
682 static int usbhsg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
684 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
685 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
686 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
688 usbhs_pkt_pop(pipe, usbhsg_ureq_to_pkt(ureq));
689 usbhsg_queue_pop(uep, ureq, -ECONNRESET);
694 static int __usbhsg_ep_set_halt_wedge(struct usb_ep *ep, int halt, int wedge)
696 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
697 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
698 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
699 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
700 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
703 usbhsg_pipe_disable(uep);
705 dev_dbg(dev, "set halt %d (pipe %d)\n",
706 halt, usbhs_pipe_number(pipe));
708 /******************** spin lock ********************/
709 usbhs_lock(priv, flags);
712 usbhs_pipe_stall(pipe);
714 usbhs_pipe_disable(pipe);
717 usbhsg_status_set(gpriv, USBHSG_STATUS_WEDGE);
719 usbhsg_status_clr(gpriv, USBHSG_STATUS_WEDGE);
721 usbhs_unlock(priv, flags);
722 /******************** spin unlock ******************/
727 static int usbhsg_ep_set_halt(struct usb_ep *ep, int value)
729 return __usbhsg_ep_set_halt_wedge(ep, value, 0);
732 static int usbhsg_ep_set_wedge(struct usb_ep *ep)
734 return __usbhsg_ep_set_halt_wedge(ep, 1, 1);
737 static struct usb_ep_ops usbhsg_ep_ops = {
738 .enable = usbhsg_ep_enable,
739 .disable = usbhsg_ep_disable,
741 .alloc_request = usbhsg_ep_alloc_request,
742 .free_request = usbhsg_ep_free_request,
744 .queue = usbhsg_ep_queue,
745 .dequeue = usbhsg_ep_dequeue,
747 .set_halt = usbhsg_ep_set_halt,
748 .set_wedge = usbhsg_ep_set_wedge,
754 static int usbhsg_can_pullup(struct usbhs_priv *priv)
756 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
758 return gpriv->driver &&
759 usbhsg_status_has(gpriv, USBHSG_STATUS_SOFT_CONNECT);
762 static void usbhsg_update_pullup(struct usbhs_priv *priv)
764 if (usbhsg_can_pullup(priv))
765 usbhs_sys_function_pullup(priv, 1);
767 usbhs_sys_function_pullup(priv, 0);
771 * usb module start/end
773 static int usbhsg_try_start(struct usbhs_priv *priv, u32 status)
775 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
776 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
777 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
778 struct device *dev = usbhs_priv_to_dev(priv);
782 /******************** spin lock ********************/
783 usbhs_lock(priv, flags);
785 usbhsg_status_set(gpriv, status);
786 if (!(usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
787 usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD)))
788 ret = -1; /* not ready */
790 usbhs_unlock(priv, flags);
791 /******************** spin unlock ********************/
794 return 0; /* not ready is not error */
797 * enable interrupt and systems if ready
799 dev_dbg(dev, "start gadget\n");
802 * pipe initialize and enable DCP
804 usbhs_fifo_init(priv);
805 usbhs_pipe_init(priv,
806 usbhsg_dma_map_ctrl);
808 /* dcp init instead of usbhsg_ep_enable() */
809 dcp->pipe = usbhs_dcp_malloc(priv);
810 dcp->pipe->mod_private = dcp;
811 usbhs_pipe_config_update(dcp->pipe, 0, 0, 64);
814 * system config enble
819 usbhs_sys_function_ctrl(priv, 1);
820 usbhsg_update_pullup(priv);
823 * enable irq callback
825 mod->irq_dev_state = usbhsg_irq_dev_state;
826 mod->irq_ctrl_stage = usbhsg_irq_ctrl_stage;
827 usbhs_irq_callback_update(priv, mod);
832 static int usbhsg_try_stop(struct usbhs_priv *priv, u32 status)
834 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
835 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
836 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
837 struct device *dev = usbhs_priv_to_dev(priv);
841 /******************** spin lock ********************/
842 usbhs_lock(priv, flags);
844 usbhsg_status_clr(gpriv, status);
845 if (!usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
846 !usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD))
847 ret = -1; /* already done */
849 usbhs_unlock(priv, flags);
850 /******************** spin unlock ********************/
853 return 0; /* already done is not error */
856 * disable interrupt and systems if 1st try
858 usbhs_fifo_quit(priv);
860 /* disable all irq */
861 mod->irq_dev_state = NULL;
862 mod->irq_ctrl_stage = NULL;
863 usbhs_irq_callback_update(priv, mod);
865 gpriv->gadget.speed = USB_SPEED_UNKNOWN;
868 usbhs_sys_set_test_mode(priv, 0);
869 usbhs_sys_function_ctrl(priv, 0);
871 usbhsg_ep_disable(&dcp->ep);
873 dev_dbg(dev, "stop gadget\n");
879 * VBUS provided by the PHY
881 static int usbhsm_phy_get_vbus(struct platform_device *pdev)
883 struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
884 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
886 return gpriv->vbus_active;
889 static void usbhs_mod_phy_mode(struct usbhs_priv *priv)
891 struct usbhs_mod_info *info = &priv->mod_info;
893 info->irq_vbus = NULL;
894 priv->pfunc.get_vbus = usbhsm_phy_get_vbus;
896 usbhs_irq_callback_update(priv, NULL);
904 static int usbhsg_gadget_start(struct usb_gadget *gadget,
905 struct usb_gadget_driver *driver)
907 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
908 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
909 struct device *dev = usbhs_priv_to_dev(priv);
914 driver->max_speed < USB_SPEED_FULL)
917 /* connect to bus through transceiver */
918 if (!IS_ERR_OR_NULL(gpriv->transceiver)) {
919 ret = otg_set_peripheral(gpriv->transceiver->otg,
922 dev_err(dev, "%s: can't bind to transceiver\n",
927 /* get vbus using phy versions */
928 usbhs_mod_phy_mode(priv);
931 /* first hook up the driver ... */
932 gpriv->driver = driver;
934 return usbhsg_try_start(priv, USBHSG_STATUS_REGISTERD);
937 static int usbhsg_gadget_stop(struct usb_gadget *gadget)
939 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
940 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
942 usbhsg_try_stop(priv, USBHSG_STATUS_REGISTERD);
944 if (!IS_ERR_OR_NULL(gpriv->transceiver))
945 otg_set_peripheral(gpriv->transceiver->otg, NULL);
947 gpriv->driver = NULL;
955 static int usbhsg_get_frame(struct usb_gadget *gadget)
957 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
958 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
960 return usbhs_frame_get_num(priv);
963 static int usbhsg_pullup(struct usb_gadget *gadget, int is_on)
965 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
966 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
969 usbhs_lock(priv, flags);
971 usbhsg_status_set(gpriv, USBHSG_STATUS_SOFT_CONNECT);
973 usbhsg_status_clr(gpriv, USBHSG_STATUS_SOFT_CONNECT);
974 usbhsg_update_pullup(priv);
975 usbhs_unlock(priv, flags);
980 static int usbhsg_set_selfpowered(struct usb_gadget *gadget, int is_self)
982 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
985 usbhsg_status_set(gpriv, USBHSG_STATUS_SELF_POWERED);
987 usbhsg_status_clr(gpriv, USBHSG_STATUS_SELF_POWERED);
989 gadget->is_selfpowered = (is_self != 0);
994 static int usbhsg_vbus_session(struct usb_gadget *gadget, int is_active)
996 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
997 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
998 struct platform_device *pdev = usbhs_priv_to_pdev(priv);
1000 gpriv->vbus_active = !!is_active;
1002 renesas_usbhs_call_notify_hotplug(pdev);
1007 static const struct usb_gadget_ops usbhsg_gadget_ops = {
1008 .get_frame = usbhsg_get_frame,
1009 .set_selfpowered = usbhsg_set_selfpowered,
1010 .udc_start = usbhsg_gadget_start,
1011 .udc_stop = usbhsg_gadget_stop,
1012 .pullup = usbhsg_pullup,
1013 .vbus_session = usbhsg_vbus_session,
1016 static int usbhsg_start(struct usbhs_priv *priv)
1018 return usbhsg_try_start(priv, USBHSG_STATUS_STARTED);
1021 static int usbhsg_stop(struct usbhs_priv *priv)
1023 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
1025 /* cable disconnect */
1026 if (gpriv->driver &&
1027 gpriv->driver->disconnect)
1028 gpriv->driver->disconnect(&gpriv->gadget);
1030 return usbhsg_try_stop(priv, USBHSG_STATUS_STARTED);
1033 int usbhs_mod_gadget_probe(struct usbhs_priv *priv)
1035 struct usbhsg_gpriv *gpriv;
1036 struct usbhsg_uep *uep;
1037 struct device *dev = usbhs_priv_to_dev(priv);
1038 int pipe_size = usbhs_get_dparam(priv, pipe_size);
1042 gpriv = kzalloc(sizeof(struct usbhsg_gpriv), GFP_KERNEL);
1044 dev_err(dev, "Could not allocate gadget priv\n");
1048 uep = kzalloc(sizeof(struct usbhsg_uep) * pipe_size, GFP_KERNEL);
1050 dev_err(dev, "Could not allocate ep\n");
1052 goto usbhs_mod_gadget_probe_err_gpriv;
1055 gpriv->transceiver = usb_get_phy(USB_PHY_TYPE_UNDEFINED);
1056 dev_info(dev, "%stransceiver found\n",
1057 gpriv->transceiver ? "" : "no ");
1062 * There is no guarantee that it is possible to access usb module here.
1063 * Don't accesses to it.
1064 * The accesse will be enable after "usbhsg_start"
1070 usbhs_mod_register(priv, &gpriv->mod, USBHS_GADGET);
1073 gpriv->mod.name = "gadget";
1074 gpriv->mod.start = usbhsg_start;
1075 gpriv->mod.stop = usbhsg_stop;
1077 gpriv->uep_size = pipe_size;
1078 usbhsg_status_init(gpriv);
1083 gpriv->gadget.dev.parent = dev;
1084 gpriv->gadget.name = "renesas_usbhs_udc";
1085 gpriv->gadget.ops = &usbhsg_gadget_ops;
1086 gpriv->gadget.max_speed = USB_SPEED_HIGH;
1088 INIT_LIST_HEAD(&gpriv->gadget.ep_list);
1093 usbhsg_for_each_uep_with_dcp(uep, gpriv, i) {
1096 snprintf(uep->ep_name, EP_NAME_SIZE, "ep%d", i);
1098 uep->ep.name = uep->ep_name;
1099 uep->ep.ops = &usbhsg_ep_ops;
1100 INIT_LIST_HEAD(&uep->ep.ep_list);
1103 if (usbhsg_is_dcp(uep)) {
1104 gpriv->gadget.ep0 = &uep->ep;
1105 usb_ep_set_maxpacket_limit(&uep->ep, 64);
1106 uep->ep.caps.type_control = true;
1108 /* init normal pipe */
1110 usb_ep_set_maxpacket_limit(&uep->ep, 512);
1111 uep->ep.caps.type_iso = true;
1112 uep->ep.caps.type_bulk = true;
1113 uep->ep.caps.type_int = true;
1114 list_add_tail(&uep->ep.ep_list, &gpriv->gadget.ep_list);
1116 uep->ep.caps.dir_in = true;
1117 uep->ep.caps.dir_out = true;
1120 ret = usb_add_gadget_udc(dev, &gpriv->gadget);
1125 dev_info(dev, "gadget probed\n");
1132 usbhs_mod_gadget_probe_err_gpriv:
1138 void usbhs_mod_gadget_remove(struct usbhs_priv *priv)
1140 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
1142 usb_del_gadget_udc(&gpriv->gadget);