18 #include <linux/list.h>
19 #include <linux/module.h>
21 #include <linux/slab.h>
86 struct usb_host_endpoint *
ep;
90 #define USBHSH_DEVICE_MAX 10
91 #define USBHSH_PORT_MAX 7
104 static const char usbhsh_hcd_name[] =
"renesas_usbhs host";
109 #define usbhsh_priv_to_hpriv(priv) \
110 container_of(usbhs_mod_get(priv, USBHS_HOST), struct usbhsh_hpriv, mod)
112 #define __usbhsh_for_each_udev(start, pos, h, i) \
113 for (i = start, pos = (h)->udev + i; \
114 i < USBHSH_DEVICE_MAX; \
115 i++, pos = (h)->udev + i)
117 #define usbhsh_for_each_udev(pos, hpriv, i) \
118 __usbhsh_for_each_udev(1, pos, hpriv, i)
120 #define usbhsh_for_each_udev_with_dev0(pos, hpriv, i) \
121 __usbhsh_for_each_udev(0, pos, hpriv, i)
123 #define usbhsh_hcd_to_hpriv(h) (struct usbhsh_hpriv *)((h)->hcd_priv)
124 #define usbhsh_hcd_to_dev(h) ((h)->self.controller)
126 #define usbhsh_hpriv_to_priv(h) ((h)->mod.priv)
127 #define usbhsh_hpriv_to_dcp(h) ((h)->dcp)
128 #define usbhsh_hpriv_to_hcd(h) \
129 container_of((void *)h, struct usb_hcd, hcd_priv)
131 #define usbhsh_ep_to_uep(u) ((u)->hcpriv)
132 #define usbhsh_uep_to_pipe(u) ((u)->pipe)
133 #define usbhsh_uep_to_udev(u) ((u)->udev)
134 #define usbhsh_uep_to_ep(u) ((u)->ep)
136 #define usbhsh_urb_to_ureq(u) ((u)->hcpriv)
137 #define usbhsh_urb_to_usbv(u) ((u)->dev)
139 #define usbhsh_usbv_to_udev(d) dev_get_drvdata(&(d)->dev)
141 #define usbhsh_udev_to_usbv(h) ((h)->usbv)
142 #define usbhsh_udev_is_used(h) usbhsh_udev_to_usbv(h)
144 #define usbhsh_pipe_to_uep(p) ((p)->mod_private)
146 #define usbhsh_device_parent(d) (usbhsh_usbv_to_udev((d)->usbv->parent))
147 #define usbhsh_device_hubport(d) ((d)->usbv->portnum)
148 #define usbhsh_device_number(h, d) ((int)((d) - (h)->udev))
149 #define usbhsh_device_nth(h, d) ((h)->udev + d)
150 #define usbhsh_device0(h) usbhsh_device_nth(h, 0)
152 #define usbhsh_port_stat_init(h) ((h)->port_stat = 0)
153 #define usbhsh_port_stat_set(h, s) ((h)->port_stat |= (s))
154 #define usbhsh_port_stat_clear(h, s) ((h)->port_stat &= ~(s))
155 #define usbhsh_port_stat_get(h) ((h)->port_stat)
157 #define usbhsh_pkt_to_ureq(p) \
158 container_of((void *)p, struct usbhsh_request, pkt)
173 dev_err(dev,
"ureq alloc fail\n");
184 static void usbhsh_ureq_free(
struct usbhsh_hpriv *hpriv,
196 static int usbhsh_is_running(
struct usbhsh_hpriv *hpriv)
205 return (hpriv->
mod.irq_attch ==
NULL);
211 static void usbhsh_endpoint_sequence_save(
struct usbhsh_hpriv *hpriv,
215 int len = urb->actual_length;
216 int maxp = usb_endpoint_maxp(&urb->ep->desc);
220 if (usb_pipecontrol(urb->pipe))
248 usb_dotoggle(urb->dev,
249 usb_pipeendpoint(urb->pipe),
250 usb_pipeout(urb->pipe));
256 static int usbhsh_pipe_attach(
struct usbhsh_hpriv *hpriv,
266 int dir_in_req = !!usb_pipein(urb->pipe);
267 int is_dcp = usb_endpoint_xfer_control(desc);
275 dev_err(dev,
"uep already has pipe\n");
276 goto usbhsh_pipe_attach_done;
288 if (0 != (dir_in - dir_in_req))
309 usb_endpoint_num(desc),
310 usb_endpoint_maxp(desc));
312 dev_dbg(dev,
"%s [%d-%d(%s:%s)]\n", __func__,
314 usb_endpoint_num(desc),
316 dir_in_req ?
"in" :
"out");
322 usbhsh_pipe_attach_done:
329 static void usbhsh_pipe_detach(
struct usbhsh_hpriv *hpriv,
348 dev_err(dev,
"uep doens't have pipe\n");
357 dev_dbg(dev,
"%s [%d-%d(%s)]\n", __func__,
359 usb_endpoint_num(&ep->desc),
370 static int usbhsh_endpoint_attach(
struct usbhsh_hpriv *hpriv,
376 struct usb_host_endpoint *ep = urb->ep;
382 uep = kzalloc(
sizeof(
struct usbhsh_ep), mem_flags);
384 dev_err(dev,
"usbhsh_ep alloc fail\n");
404 dev_dbg(dev,
"%s [%d-%d]\n", __func__,
406 usb_endpoint_num(desc));
411 static void usbhsh_endpoint_detach(
struct usbhsh_hpriv *hpriv,
412 struct usb_host_endpoint *ep)
422 dev_dbg(dev,
"%s [%d-%d]\n", __func__,
424 usb_endpoint_num(&ep->desc));
427 usbhsh_pipe_detach(hpriv, uep);
445 static void usbhsh_endpoint_detach_all(
struct usbhsh_hpriv *hpriv,
457 static
int usbhsh_connected_to_rhdev(
struct usb_hcd *hcd,
462 return hcd->self.root_hub == usbv->parent;
465 static int usbhsh_device_has_endpoint(
struct usbhsh_device *udev)
481 if (0 == usb_pipedevice(urb->pipe))
509 if (0 != usb_pipedevice(urb->pipe)) {
510 dev_err(dev,
"%s fail: urb isn't pointing device0\n", __func__);
541 dev_err(dev,
"no free usbhsh_device\n");
545 if (usbhsh_device_has_endpoint(udev)) {
546 dev_warn(dev,
"udev have old endpoint\n");
547 usbhsh_endpoint_detach_all(hpriv, udev);
550 if (usbhsh_device_has_endpoint(udev0)) {
551 dev_warn(dev,
"udev0 have old endpoint\n");
552 usbhsh_endpoint_detach_all(hpriv, udev0);
563 0, 0, 0, usbv->speed);
570 if (!usbhsh_connected_to_rhdev(hcd, udev)) {
577 dev_dbg(dev,
"%s connecte to Hub [%d:%d](%p)\n", __func__,
578 upphub, hubport, parent);
583 upphub, hubport, usbv->speed);
585 dev_dbg(dev,
"%s [%d](%p)\n", __func__,
591 static void usbhsh_device_detach(
struct usbhsh_hpriv *hpriv,
600 dev_dbg(dev,
"%s [%d](%p)\n", __func__,
603 if (usbhsh_device_has_endpoint(udev)) {
604 dev_warn(dev,
"udev still have endpoint\n");
605 usbhsh_endpoint_detach_all(hpriv, udev);
640 struct urb *urb = ureq->
urb;
644 dev_dbg(dev,
"%s\n", __func__);
647 dev_warn(dev,
"pkt doesn't have urb\n");
651 if (!usbhsh_is_running(hpriv))
654 urb->actual_length = pkt->
actual;
655 usbhsh_ureq_free(hpriv, ureq);
657 usbhsh_endpoint_sequence_save(hpriv, urb, pkt);
664 static int usbhsh_queue_push(
struct usb_hcd *hcd,
676 if (usb_pipeisoc(urb->pipe)) {
677 dev_err(dev,
"pipe iso is not supported now\n");
682 ureq = usbhsh_ureq_alloc(hpriv, urb, mem_flags);
684 dev_err(dev,
"ureq alloc fail\n");
688 if (usb_pipein(urb->pipe))
693 buf = (
void *)(urb->transfer_buffer + urb->actual_length);
694 len = urb->transfer_buffer_length - urb->actual_length;
696 sequence = usb_gettoggle(urb->dev,
697 usb_pipeendpoint(urb->pipe),
698 usb_pipeout(urb->pipe));
700 dev_dbg(dev,
"%s\n", __func__);
702 buf, len, (urb->transfer_flags & URB_ZERO_PACKET),
710 static void usbhsh_queue_force_pop(
struct usbhs_priv *priv,
725 usbhsh_queue_done(priv, pkt);
729 static void usbhsh_queue_force_pop_all(
struct usbhs_priv *priv)
735 usbhsh_queue_force_pop(priv, pos);
741 static
int usbhsh_is_request_address(
struct urb *urb)
754 static void usbhsh_setup_stage_packet_push(
struct usbhsh_hpriv *hpriv,
779 if (usbhsh_is_request_address(urb)) {
796 dev_dbg(dev,
"%s done\n", __func__);
802 static void usbhsh_data_stage_packet_done(
struct usbhs_priv *priv,
810 usbhsh_ureq_free(hpriv, ureq);
813 static int usbhsh_data_stage_packet_push(
struct usbhsh_hpriv *hpriv,
822 ureq = usbhsh_ureq_alloc(hpriv, urb, mem_flags);
826 if (usb_pipein(urb->pipe))
832 usbhsh_data_stage_packet_done,
833 urb->transfer_buffer,
834 urb->transfer_buffer_length,
835 (urb->transfer_flags & URB_ZERO_PACKET),
844 static int usbhsh_status_stage_packet_push(
struct usbhsh_hpriv *hpriv,
852 ureq = usbhsh_ureq_alloc(hpriv, urb, mem_flags);
856 if (usb_pipein(urb->pipe))
864 urb->transfer_buffer_length,
870 static int usbhsh_dcp_queue_push(
struct usb_hcd *hcd,
880 dev_dbg(dev,
"%s\n", __func__);
887 usbhsh_setup_stage_packet_push(hpriv, urb, pipe);
894 if (urb->transfer_buffer_length) {
895 ret = usbhsh_data_stage_packet_push(hpriv, urb, pipe, mflags);
897 dev_err(dev,
"data stage failed\n");
905 ret = usbhsh_status_stage_packet_push(hpriv, urb, pipe, mflags);
907 dev_err(dev,
"status stage failed\n");
922 static int usbhsh_dma_map_ctrl(
struct usbhs_pkt *pkt,
int map)
930 static int usbhsh_host_start(
struct usb_hcd *hcd)
935 static void usbhsh_host_stop(
struct usb_hcd *hcd)
939 static int usbhsh_urb_enqueue(
struct usb_hcd *hcd,
946 struct usb_host_endpoint *ep = urb->ep;
948 int is_dir_in = usb_pipein(urb->pipe);
952 dev_dbg(dev,
"%s (%s)\n", __func__, is_dir_in ?
"in" :
"out");
954 if (!usbhsh_is_running(hpriv)) {
956 dev_err(dev,
"host is not running\n");
957 goto usbhsh_urb_enqueue_error_not_linked;
962 dev_err(dev,
"urb link failed\n");
963 goto usbhsh_urb_enqueue_error_not_linked;
970 if (!usbhsh_device_get(hpriv, urb)) {
971 new_udev = usbhsh_device_attach(hpriv, urb);
974 dev_err(dev,
"device attach failed\n");
975 goto usbhsh_urb_enqueue_error_not_linked;
984 ret = usbhsh_endpoint_attach(hpriv, urb, mem_flags);
986 dev_err(dev,
"endpoint attach failed\n");
987 goto usbhsh_urb_enqueue_error_free_device;
995 for (i = 0; i < 1024; i++) {
996 ret = usbhsh_pipe_attach(hpriv, urb);
1003 dev_err(dev,
"pipe attach failed\n");
1004 goto usbhsh_urb_enqueue_error_free_endpoint;
1010 if (usb_pipecontrol(urb->pipe))
1011 ret = usbhsh_dcp_queue_push(hcd, urb, mem_flags);
1013 ret = usbhsh_queue_push(hcd, urb, mem_flags);
1017 usbhsh_urb_enqueue_error_free_endpoint:
1018 usbhsh_endpoint_detach(hpriv, ep);
1019 usbhsh_urb_enqueue_error_free_device:
1021 usbhsh_device_detach(hpriv, new_udev);
1022 usbhsh_urb_enqueue_error_not_linked:
1024 dev_dbg(dev,
"%s error\n", __func__);
1029 static int usbhsh_urb_dequeue(
struct usb_hcd *hcd,
struct urb *urb,
int status)
1039 usbhsh_queue_done(priv, pkt);
1045 static void usbhsh_endpoint_disable(
struct usb_hcd *hcd,
1046 struct usb_host_endpoint *ep)
1062 usbhsh_endpoint_detach(hpriv, ep);
1068 if (!usbhsh_device_has_endpoint(udev))
1069 usbhsh_device_detach(hpriv, udev);
1072 static int usbhsh_hub_status_data(
struct usb_hcd *hcd,
char *buf)
1084 *buf = (1 << roothub_id);
1088 dev_dbg(dev,
"%s (%02x)\n", __func__, *buf);
1093 static int __usbhsh_hub_hub_feature(
struct usbhsh_hpriv *hpriv,
1103 dev_dbg(dev,
"%s :: C_HUB_xx\n", __func__);
1110 static int __usbhsh_hub_port_feature(
struct usbhsh_hpriv *hpriv,
1112 u16 wIndex,
char *buf,
u16 wLength)
1116 int enable = (typeReq == SetPortFeature);
1121 if (wIndex > roothub_id || wLength != 0)
1128 dev_dbg(dev,
"%s :: USB_PORT_FEAT_POWER\n", __func__);
1138 dev_dbg(dev,
"%s :: USB_PORT_FEAT_xxx\n", __func__);
1149 usbhsh_queue_force_pop_all(priv);
1155 for (i = 0; i < timeout ; i++) {
1159 goto got_usb_bus_speed;
1162 goto got_usb_bus_speed;
1165 goto got_usb_bus_speed;
1176 dev_dbg(dev,
"%s :: USB_PORT_FEAT_RESET (speed = %d)\n",
1195 static int __usbhsh_hub_get_status(
struct usbhsh_hpriv *hpriv,
1197 u16 wIndex,
char *buf,
u16 wLength)
1206 dev_dbg(dev,
"%s :: GetHubStatus\n", __func__);
1212 if (wIndex != roothub_id)
1215 dev_dbg(dev,
"%s :: GetPortStatus\n", __func__);
1219 case GetHubDescriptor:
1228 dev_dbg(dev,
"%s :: GetHubDescriptor\n", __func__);
1235 static int usbhsh_hub_control(
struct usb_hcd *hcd,
u16 typeReq,
u16 wValue,
1236 u16 wIndex,
char *buf,
u16 wLength)
1246 case ClearHubFeature:
1248 ret = __usbhsh_hub_hub_feature(hpriv, typeReq,
1249 wValue, wIndex, buf, wLength);
1253 case SetPortFeature:
1254 case ClearPortFeature:
1255 ret = __usbhsh_hub_port_feature(hpriv, typeReq,
1256 wValue, wIndex, buf, wLength);
1262 case GetHubDescriptor:
1263 ret = __usbhsh_hub_get_status(hpriv, typeReq,
1264 wValue, wIndex, buf, wLength);
1268 dev_dbg(dev,
"typeReq = %x, ret = %d, port_stat = %x\n",
1274 static int usbhsh_bus_nop(
struct usb_hcd *hcd)
1280 static struct hc_driver usbhsh_driver = {
1281 .description = usbhsh_hcd_name,
1289 .start = usbhsh_host_start,
1290 .stop = usbhsh_host_stop,
1295 .urb_enqueue = usbhsh_urb_enqueue,
1296 .urb_dequeue = usbhsh_urb_dequeue,
1297 .endpoint_disable = usbhsh_endpoint_disable,
1302 .hub_status_data = usbhsh_hub_status_data,
1303 .hub_control = usbhsh_hub_control,
1304 .bus_suspend = usbhsh_bus_nop,
1305 .bus_resume = usbhsh_bus_nop,
1311 static int usbhsh_irq_attch(
struct usbhs_priv *priv,
1317 dev_dbg(dev,
"device attached\n");
1339 static int usbhsh_irq_dtch(
struct usbhs_priv *priv,
1345 dev_dbg(dev,
"device detached\n");
1359 hpriv->
mod.irq_attch = usbhsh_irq_attch;
1366 usbhsh_queue_force_pop_all(priv);
1371 static int usbhsh_irq_setup_ack(
struct usbhs_priv *priv,
1377 dev_dbg(dev,
"setup packet OK\n");
1384 static int usbhsh_irq_setup_err(
struct usbhs_priv *priv,
1390 dev_dbg(dev,
"setup packet Err\n");
1400 static void usbhsh_pipe_init_for_host(
struct usbhs_priv *priv)
1406 int old_type, dir_in,
i;
1410 for (i = 0; i < pipe_size; i++) {
1432 dir_in = (pipe_type[
i] == old_type);
1433 old_type = pipe_type[
i];
1448 static int usbhsh_start(
struct usbhs_priv *priv)
1465 usbhsh_dma_map_ctrl);
1467 usbhsh_pipe_init_for_host(priv);
1482 mod->
irq_sack = usbhsh_irq_setup_ack;
1483 mod->
irq_sign = usbhsh_irq_setup_err;
1491 static int usbhsh_stop(
struct usbhs_priv *priv)
1520 struct usb_hcd *hcd;
1528 dev_err(dev,
"Failed to create hcd\n");
1549 hpriv->
mod.name =
"host";
1550 hpriv->
mod.start = usbhsh_start;
1551 hpriv->
mod.stop = usbhsh_stop;