20 #include <linux/module.h>
22 #include <linux/usb/ch9.h>
34 #define EP_NAME_SIZE 8
55 #define USBHSG_STATUS_STARTED (1 << 0)
56 #define USBHSG_STATUS_REGISTERD (1 << 1)
57 #define USBHSG_STATUS_WEDGE (1 << 2)
58 #define USBHSG_STATUS_SELF_POWERED (1 << 3)
74 #define usbhsg_priv_to_gpriv(priv) \
76 usbhs_mod_get(priv, USBHS_GADGET), \
77 struct usbhsg_gpriv, mod)
79 #define __usbhsg_for_each_uep(start, pos, g, i) \
80 for (i = start, pos = (g)->uep + i; \
82 i++, pos = (g)->uep + i)
84 #define usbhsg_for_each_uep(pos, gpriv, i) \
85 __usbhsg_for_each_uep(1, pos, gpriv, i)
87 #define usbhsg_for_each_uep_with_dcp(pos, gpriv, i) \
88 __usbhsg_for_each_uep(0, pos, gpriv, i)
90 #define usbhsg_gadget_to_gpriv(g)\
91 container_of(g, struct usbhsg_gpriv, gadget)
93 #define usbhsg_req_to_ureq(r)\
94 container_of(r, struct usbhsg_request, req)
96 #define usbhsg_ep_to_uep(e) container_of(e, struct usbhsg_uep, ep)
97 #define usbhsg_gpriv_to_dev(gp) usbhs_priv_to_dev((gp)->mod.priv)
98 #define usbhsg_gpriv_to_priv(gp) ((gp)->mod.priv)
99 #define usbhsg_gpriv_to_dcp(gp) ((gp)->uep)
100 #define usbhsg_gpriv_to_nth_uep(gp, i) ((gp)->uep + i)
101 #define usbhsg_uep_to_gpriv(u) ((u)->gpriv)
102 #define usbhsg_uep_to_pipe(u) ((u)->pipe)
103 #define usbhsg_pipe_to_uep(p) ((p)->mod_private)
104 #define usbhsg_is_dcp(u) ((u) == usbhsg_gpriv_to_dcp((u)->gpriv))
106 #define usbhsg_ureq_to_pkt(u) (&(u)->pkt)
107 #define usbhsg_pkt_to_ureq(i) \
108 container_of(i, struct usbhsg_request, pkt)
110 #define usbhsg_is_not_connected(gp) ((gp)->gadget.speed == USB_SPEED_UNKNOWN)
113 #define usbhsg_status_init(gp) do {(gp)->status = 0; } while (0)
114 #define usbhsg_status_set(gp, b) (gp->status |= b)
115 #define usbhsg_status_clr(gp, b) (gp->status &= ~b)
116 #define usbhsg_status_has(gp, b) (gp->status & b)
121 static void usbhsg_queue_pop(
struct usbhsg_uep *uep,
132 ureq->
req.complete(&uep->
ep, &ureq->
req);
143 usbhsg_queue_pop(uep, ureq, 0);
146 static void usbhsg_queue_push(
struct usbhsg_uep *uep,
161 dev_dbg(dev,
"pipe %d : queue push (%d)\n",
169 static int usbhsg_dma_map_ctrl(
struct usbhs_pkt *pkt,
int map)
200 static int usbhsg_recip_handler_std_control_done(
struct usbhs_priv *
priv,
213 static int usbhsg_recip_handler_std_clear_endpoint(
struct usbhs_priv *
priv,
226 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
234 .name =
"clear feature",
235 .device = usbhsg_recip_handler_std_control_done,
236 .interface = usbhsg_recip_handler_std_control_done,
237 .endpoint = usbhsg_recip_handler_std_clear_endpoint,
243 static int usbhsg_recip_handler_std_set_device(
struct usbhs_priv *priv,
249 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
254 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
261 static int usbhsg_recip_handler_std_set_endpoint(
struct usbhs_priv *priv,
269 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
275 .name =
"set feature",
276 .device = usbhsg_recip_handler_std_set_device,
277 .interface = usbhsg_recip_handler_std_control_done,
278 .endpoint = usbhsg_recip_handler_std_set_endpoint,
284 static void __usbhsg_recip_send_complete(
struct usb_ep *ep,
291 usb_ep_free_request(ep, req);
294 static void __usbhsg_recip_send_status(
struct usbhsg_gpriv *gpriv,
306 dev_err(dev,
"recip request allocation fail\n");
313 usb_ep_free_request(&dcp->
ep, req);
314 dev_err(dev,
"recip data allocation fail\n");
322 req->
complete = __usbhsg_recip_send_complete;
324 req->
length =
sizeof(*buf);
332 static int usbhsg_recip_handler_std_get_device(
struct usbhs_priv *priv,
337 unsigned short status = 0;
342 __usbhsg_recip_send_status(gpriv, status);
347 static int usbhsg_recip_handler_std_get_interface(
struct usbhs_priv *priv,
352 unsigned short status = 0;
354 __usbhsg_recip_send_status(gpriv, status);
359 static int usbhsg_recip_handler_std_get_endpoint(
struct usbhs_priv *priv,
365 unsigned short status = 0;
370 __usbhsg_recip_send_status(gpriv, status);
376 .name =
"get status",
377 .device = usbhsg_recip_handler_std_get_device,
378 .interface = usbhsg_recip_handler_std_get_interface,
379 .endpoint = usbhsg_recip_handler_std_get_endpoint,
385 static int usbhsg_recip_run_handle(
struct usbhs_priv *priv,
403 dev_err(dev,
"wrong recip request\n");
421 dev_warn(dev,
"unsupported RECIP(%d)\n", recip);
427 dev_dbg(dev,
"%s (pipe %d :%s)\n", handler->
name, nth, msg);
428 ret =
func(priv, uep, ctrl);
439 static int usbhsg_irq_dev_state(
struct usbhs_priv *priv,
447 dev_dbg(dev,
"state = %x : speed : %d\n",
454 static int usbhsg_irq_ctrl_stage(
struct usbhs_priv *priv,
466 dev_dbg(dev,
"stage = %d\n", stage);
515 ret = usbhsg_recip_run_handle(priv, recip_handler, &ctrl);
530 static int usbhsg_pipe_disable(
struct usbhsg_uep *uep)
582 usb_endpoint_type(desc),
583 usb_endpoint_dir_in(desc));
590 usb_endpoint_num(desc),
591 usb_endpoint_maxp(desc));
598 if (usb_endpoint_dir_in(desc))
609 static int usbhsg_ep_disable(
struct usb_ep *ep)
613 return usbhsg_pipe_disable(uep);
621 ureq = kzalloc(
sizeof *ureq, gfp_flags);
630 static void usbhsg_ep_free_request(
struct usb_ep *ep,
653 usbhsg_queue_push(uep, ureq);
670 static int __usbhsg_ep_set_halt_wedge(
struct usb_ep *ep,
int halt,
int wedge)
679 usbhsg_pipe_disable(uep);
681 dev_dbg(dev,
"set halt %d (pipe %d)\n",
703 static int usbhsg_ep_set_halt(
struct usb_ep *ep,
int value)
705 return __usbhsg_ep_set_halt_wedge(ep, value, 0);
708 static int usbhsg_ep_set_wedge(
struct usb_ep *ep)
710 return __usbhsg_ep_set_halt_wedge(ep, 1, 1);
714 .enable = usbhsg_ep_enable,
715 .disable = usbhsg_ep_disable,
717 .alloc_request = usbhsg_ep_alloc_request,
718 .free_request = usbhsg_ep_free_request,
720 .queue = usbhsg_ep_queue,
721 .dequeue = usbhsg_ep_dequeue,
723 .set_halt = usbhsg_ep_set_halt,
724 .set_wedge = usbhsg_ep_set_wedge,
730 static int usbhsg_try_start(
struct usbhs_priv *priv,
u32 status)
756 dev_dbg(dev,
"start gadget\n");
762 usbhsg_dma_map_ctrl);
764 usbhsg_uep_init(gpriv);
768 dcp->
pipe->mod_private = dcp;
789 static int usbhsg_try_stop(
struct usbhs_priv *priv,
u32 status)
828 usbhsg_pipe_disable(dcp);
840 static int usbhsg_gadget_start(
struct usb_gadget *gadget,
858 static int usbhsg_gadget_stop(
struct usb_gadget *gadget,
878 static int usbhsg_get_frame(
struct usb_gadget *gadget)
886 static int usbhsg_set_selfpowered(
struct usb_gadget *gadget,
int is_self)
899 .get_frame = usbhsg_get_frame,
900 .set_selfpowered = usbhsg_set_selfpowered,
901 .udc_start = usbhsg_gadget_start,
902 .udc_stop = usbhsg_gadget_stop,
905 static int usbhsg_start(
struct usbhs_priv *priv)
910 static int usbhsg_stop(
struct usbhs_priv *priv)
916 gpriv->
driver->disconnect)
922 static void usbhs_mod_gadget_release(
struct device *pdev)
938 dev_err(dev,
"Could not allocate gadget priv\n");
944 dev_err(dev,
"Could not allocate ep\n");
946 goto usbhs_mod_gadget_probe_err_gpriv;
963 gpriv->
mod.name =
"gadget";
964 gpriv->
mod.start = usbhsg_start;
965 gpriv->
mod.stop = usbhsg_stop;
975 gpriv->
gadget.dev.release = usbhs_mod_gadget_release;
976 gpriv->
gadget.name =
"renesas_usbhs_udc";
977 gpriv->
gadget.ops = &usbhsg_gadget_ops;
983 INIT_LIST_HEAD(&gpriv->
gadget.ep_list);
993 uep->
ep.ops = &usbhsg_ep_ops;
994 INIT_LIST_HEAD(&uep->
ep.ep_list);
999 uep->
ep.maxpacket = 64;
1003 uep->
ep.maxpacket = 512;
1022 usbhs_mod_gadget_probe_err_gpriv: