1 // SPDX-License-Identifier: GPL-1.0+
5 * Copyright (C) 2011 Renesas Solutions Corp.
9 #include <linux/list.h>
10 #include <linux/module.h>
11 #include <linux/platform_device.h>
12 #include <linux/slab.h>
13 #include <linux/usb.h>
14 #include <linux/usb/hcd.h>
18 *** HARDWARE LIMITATION ***
20 * 1) renesas_usbhs has a limited number of controllable devices.
21 * it can control only 9 devices in generally.
22 * see DEVADDn / DCPMAXP / PIPEMAXP.
24 * 2) renesas_usbhs pipe number is limited.
25 * the pipe will be re-used for each devices.
26 * so, software should control DATA0/1 sequence of each devices.
34 * | udev 0 | --> it is used when set address
37 * +--------+ pipes are reused for each uep.
38 * | udev 1 |-+- [uep 0 (dcp) ] --+ pipe will be switched when
39 * +--------+ | | other device requested
40 * +- [uep 1 (bulk)] --|---+ +--------------+
41 * | +--------------> | pipe0 (dcp) |
42 * +- [uep 2 (bulk)] -@ | +--------------+
44 * +--------+ | +--------------+
45 * | udev 2 |-+- [uep 0 (dcp) ] -@ +----------> | pipe2 (bulk) |
46 * +--------+ | +--------------+
47 * +- [uep 1 (int) ] ----+ +------> | pipe3 (bulk) |
48 * | | +--------------+
49 * +--------+ +-----|------> | pipe4 (int) |
50 * | udev 3 |-+- [uep 0 (dcp) ] -@ | +--------------+
51 * +--------+ | | | .... |
52 * +- [uep 1 (bulk)] -@ | | .... |
54 * +- [uep 2 (bulk)]-----------+
56 * @ : uep requested free pipe, but all have been used.
57 * now it is waiting for free pipe
64 struct usbhsh_request {
69 struct usbhsh_device {
70 struct usb_device *usbv;
71 struct list_head ep_list_head; /* list of usbhsh_ep */
75 struct usbhs_pipe *pipe; /* attached pipe */
76 struct usbhsh_device *udev; /* attached udev */
77 struct usb_host_endpoint *ep;
78 struct list_head ep_list; /* list to usbhsh_device */
79 unsigned int counter; /* pipe attach counter */
82 #define USBHSH_DEVICE_MAX 10 /* see DEVADDn / DCPMAXP / PIPEMAXP */
83 #define USBHSH_PORT_MAX 7 /* see DEVADDn :: HUBPORT */
86 struct usbhs_pipe *dcp;
88 struct usbhsh_device udev[USBHSH_DEVICE_MAX];
90 u32 port_stat; /* USB_PORT_STAT_xxx */
92 struct completion setup_ack_done;
96 static const char usbhsh_hcd_name[] = "renesas_usbhs host";
101 #define usbhsh_priv_to_hpriv(priv) \
102 container_of(usbhs_mod_get(priv, USBHS_HOST), struct usbhsh_hpriv, mod)
104 #define __usbhsh_for_each_udev(start, pos, h, i) \
106 ((i) < USBHSH_DEVICE_MAX) && ((pos) = (h)->udev + (i)); \
109 #define usbhsh_for_each_udev(pos, hpriv, i) \
110 __usbhsh_for_each_udev(1, pos, hpriv, i)
112 #define usbhsh_for_each_udev_with_dev0(pos, hpriv, i) \
113 __usbhsh_for_each_udev(0, pos, hpriv, i)
115 #define usbhsh_hcd_to_hpriv(h) (struct usbhsh_hpriv *)((h)->hcd_priv)
116 #define usbhsh_hcd_to_dev(h) ((h)->self.controller)
118 #define usbhsh_hpriv_to_priv(h) ((h)->mod.priv)
119 #define usbhsh_hpriv_to_dcp(h) ((h)->dcp)
120 #define usbhsh_hpriv_to_hcd(h) \
121 container_of((void *)h, struct usb_hcd, hcd_priv)
123 #define usbhsh_ep_to_uep(u) ((u)->hcpriv)
124 #define usbhsh_uep_to_pipe(u) ((u)->pipe)
125 #define usbhsh_uep_to_udev(u) ((u)->udev)
126 #define usbhsh_uep_to_ep(u) ((u)->ep)
128 #define usbhsh_urb_to_ureq(u) ((u)->hcpriv)
129 #define usbhsh_urb_to_usbv(u) ((u)->dev)
131 #define usbhsh_usbv_to_udev(d) dev_get_drvdata(&(d)->dev)
133 #define usbhsh_udev_to_usbv(h) ((h)->usbv)
134 #define usbhsh_udev_is_used(h) usbhsh_udev_to_usbv(h)
136 #define usbhsh_pipe_to_uep(p) ((p)->mod_private)
138 #define usbhsh_device_parent(d) (usbhsh_usbv_to_udev((d)->usbv->parent))
139 #define usbhsh_device_hubport(d) ((d)->usbv->portnum)
140 #define usbhsh_device_number(h, d) ((int)((d) - (h)->udev))
141 #define usbhsh_device_nth(h, d) ((h)->udev + d)
142 #define usbhsh_device0(h) usbhsh_device_nth(h, 0)
144 #define usbhsh_port_stat_init(h) ((h)->port_stat = 0)
145 #define usbhsh_port_stat_set(h, s) ((h)->port_stat |= (s))
146 #define usbhsh_port_stat_clear(h, s) ((h)->port_stat &= ~(s))
147 #define usbhsh_port_stat_get(h) ((h)->port_stat)
149 #define usbhsh_pkt_to_ureq(p) \
150 container_of((void *)p, struct usbhsh_request, pkt)
155 static struct usbhsh_request *usbhsh_ureq_alloc(struct usbhsh_hpriv *hpriv,
159 struct usbhsh_request *ureq;
161 ureq = kzalloc(sizeof(struct usbhsh_request), mem_flags);
165 usbhs_pkt_init(&ureq->pkt);
167 usbhsh_urb_to_ureq(urb) = ureq;
172 static void usbhsh_ureq_free(struct usbhsh_hpriv *hpriv,
173 struct usbhsh_request *ureq)
175 usbhsh_urb_to_ureq(ureq->urb) = NULL;
184 static int usbhsh_is_running(struct usbhsh_hpriv *hpriv)
187 * we can decide some device is attached or not
188 * by checking mod.irq_attch
193 return (hpriv->mod.irq_attch == NULL);
199 static void usbhsh_endpoint_sequence_save(struct usbhsh_hpriv *hpriv,
201 struct usbhs_pkt *pkt)
203 int len = urb->actual_length;
204 int maxp = usb_endpoint_maxp(&urb->ep->desc);
207 /* DCP is out of sequence control */
208 if (usb_pipecontrol(urb->pipe))
212 * renesas_usbhs pipe has a limitation in a number.
213 * So, driver should re-use the limited pipe for each device/endpoint.
214 * DATA0/1 sequence should be saved for it.
215 * see [image of mod_host]
216 * [HARDWARE LIMITATION]
220 * next sequence depends on actual_length
222 * ex) actual_length = 1147, maxp = 512
226 * data1 is the next sequence
236 usb_dotoggle(urb->dev,
237 usb_pipeendpoint(urb->pipe),
238 usb_pipeout(urb->pipe));
241 static struct usbhsh_device *usbhsh_device_get(struct usbhsh_hpriv *hpriv,
244 static int usbhsh_pipe_attach(struct usbhsh_hpriv *hpriv,
247 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
248 struct usbhsh_ep *uep = usbhsh_ep_to_uep(urb->ep);
249 struct usbhsh_device *udev = usbhsh_device_get(hpriv, urb);
250 struct usbhs_pipe *pipe;
251 struct usb_endpoint_descriptor *desc = &urb->ep->desc;
252 struct device *dev = usbhs_priv_to_dev(priv);
254 int dir_in_req = !!usb_pipein(urb->pipe);
255 int is_dcp = usb_endpoint_xfer_control(desc);
259 /******************** spin lock ********************/
260 usbhs_lock(priv, flags);
263 * if uep has been attached to pipe,
266 if (usbhsh_uep_to_pipe(uep)) {
268 goto usbhsh_pipe_attach_done;
271 usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
273 /* check pipe type */
274 if (!usbhs_pipe_type_is(pipe, usb_endpoint_type(desc)))
277 /* check pipe direction if normal pipe */
279 dir_in = !!usbhs_pipe_is_dir_in(pipe);
280 if (0 != (dir_in - dir_in_req))
284 /* check pipe is free */
285 if (usbhsh_pipe_to_uep(pipe))
291 * usbhs_pipe_config_update() should be called after
292 * usbhs_set_device_config()
296 usbhsh_uep_to_pipe(uep) = pipe;
297 usbhsh_pipe_to_uep(pipe) = uep;
299 usbhs_pipe_config_update(pipe,
300 usbhsh_device_number(hpriv, udev),
301 usb_endpoint_num(desc),
302 usb_endpoint_maxp(desc));
304 dev_dbg(dev, "%s [%d-%d(%s:%s)]\n", __func__,
305 usbhsh_device_number(hpriv, udev),
306 usb_endpoint_num(desc),
307 usbhs_pipe_name(pipe),
308 dir_in_req ? "in" : "out");
314 usbhsh_pipe_attach_done:
318 usbhs_unlock(priv, flags);
319 /******************** spin unlock ******************/
324 static void usbhsh_pipe_detach(struct usbhsh_hpriv *hpriv,
325 struct usbhsh_ep *uep)
327 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
328 struct usbhs_pipe *pipe;
329 struct device *dev = usbhs_priv_to_dev(priv);
332 if (unlikely(!uep)) {
333 dev_err(dev, "no uep\n");
337 /******************** spin lock ********************/
338 usbhs_lock(priv, flags);
340 pipe = usbhsh_uep_to_pipe(uep);
342 if (unlikely(!pipe)) {
343 dev_err(dev, "uep doesn't have pipe\n");
344 } else if (1 == uep->counter--) { /* last user */
345 struct usb_host_endpoint *ep = usbhsh_uep_to_ep(uep);
346 struct usbhsh_device *udev = usbhsh_uep_to_udev(uep);
348 /* detach pipe from uep */
349 usbhsh_uep_to_pipe(uep) = NULL;
350 usbhsh_pipe_to_uep(pipe) = NULL;
352 dev_dbg(dev, "%s [%d-%d(%s)]\n", __func__,
353 usbhsh_device_number(hpriv, udev),
354 usb_endpoint_num(&ep->desc),
355 usbhs_pipe_name(pipe));
358 usbhs_unlock(priv, flags);
359 /******************** spin unlock ******************/
365 static int usbhsh_endpoint_attach(struct usbhsh_hpriv *hpriv,
369 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
370 struct usbhsh_device *udev = usbhsh_device_get(hpriv, urb);
371 struct usb_host_endpoint *ep = urb->ep;
372 struct usbhsh_ep *uep;
373 struct device *dev = usbhs_priv_to_dev(priv);
374 struct usb_endpoint_descriptor *desc = &ep->desc;
377 uep = kzalloc(sizeof(struct usbhsh_ep), mem_flags);
381 /******************** spin lock ********************/
382 usbhs_lock(priv, flags);
388 INIT_LIST_HEAD(&uep->ep_list);
389 list_add_tail(&uep->ep_list, &udev->ep_list_head);
391 usbhsh_uep_to_udev(uep) = udev;
392 usbhsh_uep_to_ep(uep) = ep;
393 usbhsh_ep_to_uep(ep) = uep;
395 usbhs_unlock(priv, flags);
396 /******************** spin unlock ******************/
398 dev_dbg(dev, "%s [%d-%d]\n", __func__,
399 usbhsh_device_number(hpriv, udev),
400 usb_endpoint_num(desc));
405 static void usbhsh_endpoint_detach(struct usbhsh_hpriv *hpriv,
406 struct usb_host_endpoint *ep)
408 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
409 struct device *dev = usbhs_priv_to_dev(priv);
410 struct usbhsh_ep *uep = usbhsh_ep_to_uep(ep);
416 dev_dbg(dev, "%s [%d-%d]\n", __func__,
417 usbhsh_device_number(hpriv, usbhsh_uep_to_udev(uep)),
418 usb_endpoint_num(&ep->desc));
420 if (usbhsh_uep_to_pipe(uep))
421 usbhsh_pipe_detach(hpriv, uep);
423 /******************** spin lock ********************/
424 usbhs_lock(priv, flags);
426 /* remove this endpoint from udev */
427 list_del_init(&uep->ep_list);
429 usbhsh_uep_to_udev(uep) = NULL;
430 usbhsh_uep_to_ep(uep) = NULL;
431 usbhsh_ep_to_uep(ep) = NULL;
433 usbhs_unlock(priv, flags);
434 /******************** spin unlock ******************/
439 static void usbhsh_endpoint_detach_all(struct usbhsh_hpriv *hpriv,
440 struct usbhsh_device *udev)
442 struct usbhsh_ep *uep, *next;
444 list_for_each_entry_safe(uep, next, &udev->ep_list_head, ep_list)
445 usbhsh_endpoint_detach(hpriv, usbhsh_uep_to_ep(uep));
451 static int usbhsh_connected_to_rhdev(struct usb_hcd *hcd,
452 struct usbhsh_device *udev)
454 struct usb_device *usbv = usbhsh_udev_to_usbv(udev);
456 return hcd->self.root_hub == usbv->parent;
459 static int usbhsh_device_has_endpoint(struct usbhsh_device *udev)
461 return !list_empty(&udev->ep_list_head);
464 static struct usbhsh_device *usbhsh_device_get(struct usbhsh_hpriv *hpriv,
467 struct usb_device *usbv = usbhsh_urb_to_usbv(urb);
468 struct usbhsh_device *udev = usbhsh_usbv_to_udev(usbv);
470 /* usbhsh_device_attach() is still not called */
474 /* if it is device0, return it */
475 if (0 == usb_pipedevice(urb->pipe))
476 return usbhsh_device0(hpriv);
478 /* return attached device */
482 static struct usbhsh_device *usbhsh_device_attach(struct usbhsh_hpriv *hpriv,
485 struct usbhsh_device *udev = NULL;
486 struct usbhsh_device *udev0 = usbhsh_device0(hpriv);
487 struct usbhsh_device *pos;
488 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
489 struct device *dev = usbhsh_hcd_to_dev(hcd);
490 struct usb_device *usbv = usbhsh_urb_to_usbv(urb);
491 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
497 * This function should be called only while urb is pointing to device0.
498 * It will attach unused usbhsh_device to urb (usbv),
499 * and initialize device0.
500 * You can use usbhsh_device_get() to get "current" udev,
501 * and usbhsh_usbv_to_udev() is for "attached" udev.
503 if (0 != usb_pipedevice(urb->pipe)) {
504 dev_err(dev, "%s fail: urb isn't pointing device0\n", __func__);
508 /******************** spin lock ********************/
509 usbhs_lock(priv, flags);
514 usbhsh_for_each_udev(pos, hpriv, i) {
515 if (usbhsh_udev_is_used(pos))
523 * usbhsh_usbv_to_udev()
524 * usbhsh_udev_to_usbv()
527 dev_set_drvdata(&usbv->dev, udev);
531 usbhs_unlock(priv, flags);
532 /******************** spin unlock ******************/
535 dev_err(dev, "no free usbhsh_device\n");
539 if (usbhsh_device_has_endpoint(udev)) {
540 dev_warn(dev, "udev have old endpoint\n");
541 usbhsh_endpoint_detach_all(hpriv, udev);
544 if (usbhsh_device_has_endpoint(udev0)) {
545 dev_warn(dev, "udev0 have old endpoint\n");
546 usbhsh_endpoint_detach_all(hpriv, udev0);
549 /* uep will be attached */
550 INIT_LIST_HEAD(&udev0->ep_list_head);
551 INIT_LIST_HEAD(&udev->ep_list_head);
556 usbhs_set_device_config(priv,
557 0, 0, 0, usbv->speed);
560 * set new device config
564 if (!usbhsh_connected_to_rhdev(hcd, udev)) {
565 /* if udev is not connected to rhdev, it means parent is Hub */
566 struct usbhsh_device *parent = usbhsh_device_parent(udev);
568 upphub = usbhsh_device_number(hpriv, parent);
569 hubport = usbhsh_device_hubport(udev);
571 dev_dbg(dev, "%s connected to Hub [%d:%d](%p)\n", __func__,
572 upphub, hubport, parent);
575 usbhs_set_device_config(priv,
576 usbhsh_device_number(hpriv, udev),
577 upphub, hubport, usbv->speed);
579 dev_dbg(dev, "%s [%d](%p)\n", __func__,
580 usbhsh_device_number(hpriv, udev), udev);
585 static void usbhsh_device_detach(struct usbhsh_hpriv *hpriv,
586 struct usbhsh_device *udev)
588 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
589 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
590 struct device *dev = usbhsh_hcd_to_dev(hcd);
591 struct usb_device *usbv = usbhsh_udev_to_usbv(udev);
594 dev_dbg(dev, "%s [%d](%p)\n", __func__,
595 usbhsh_device_number(hpriv, udev), udev);
597 if (usbhsh_device_has_endpoint(udev)) {
598 dev_warn(dev, "udev still have endpoint\n");
599 usbhsh_endpoint_detach_all(hpriv, udev);
603 * There is nothing to do if it is device0.
605 * usbhsh_device_attach()
606 * usbhsh_device_get()
608 if (0 == usbhsh_device_number(hpriv, udev))
611 /******************** spin lock ********************/
612 usbhs_lock(priv, flags);
615 * usbhsh_usbv_to_udev()
616 * usbhsh_udev_to_usbv()
619 dev_set_drvdata(&usbv->dev, NULL);
622 usbhs_unlock(priv, flags);
623 /******************** spin unlock ******************/
629 static void usbhsh_queue_done(struct usbhs_priv *priv, struct usbhs_pkt *pkt)
631 struct usbhsh_request *ureq = usbhsh_pkt_to_ureq(pkt);
632 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
633 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
634 struct urb *urb = ureq->urb;
635 struct device *dev = usbhs_priv_to_dev(priv);
638 dev_dbg(dev, "%s\n", __func__);
641 dev_warn(dev, "pkt doesn't have urb\n");
645 if (!usbhsh_is_running(hpriv))
648 urb->actual_length = pkt->actual;
650 usbhsh_endpoint_sequence_save(hpriv, urb, pkt);
651 usbhsh_ureq_free(hpriv, ureq);
653 usbhsh_pipe_detach(hpriv, usbhsh_ep_to_uep(urb->ep));
655 usb_hcd_unlink_urb_from_ep(hcd, urb);
656 usb_hcd_giveback_urb(hcd, urb, status);
659 static int usbhsh_queue_push(struct usb_hcd *hcd,
663 struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
664 struct usbhsh_ep *uep = usbhsh_ep_to_uep(urb->ep);
665 struct usbhs_pipe *pipe = usbhsh_uep_to_pipe(uep);
666 struct device *dev = usbhsh_hcd_to_dev(hcd);
667 struct usbhsh_request *ureq;
671 if (usb_pipeisoc(urb->pipe)) {
672 dev_err(dev, "pipe iso is not supported now\n");
676 /* this ureq will be freed on usbhsh_queue_done() */
677 ureq = usbhsh_ureq_alloc(hpriv, urb, mem_flags);
678 if (unlikely(!ureq)) {
679 dev_err(dev, "ureq alloc fail\n");
683 if (usb_pipein(urb->pipe))
684 pipe->handler = &usbhs_fifo_dma_pop_handler;
686 pipe->handler = &usbhs_fifo_dma_push_handler;
688 buf = (void *)(urb->transfer_buffer + urb->actual_length);
689 len = urb->transfer_buffer_length - urb->actual_length;
691 sequence = usb_gettoggle(urb->dev,
692 usb_pipeendpoint(urb->pipe),
693 usb_pipeout(urb->pipe));
695 dev_dbg(dev, "%s\n", __func__);
696 usbhs_pkt_push(pipe, &ureq->pkt, usbhsh_queue_done,
697 buf, len, (urb->transfer_flags & URB_ZERO_PACKET),
700 usbhs_pkt_start(pipe);
705 static void usbhsh_queue_force_pop(struct usbhs_priv *priv,
706 struct usbhs_pipe *pipe)
708 struct usbhs_pkt *pkt;
711 pkt = usbhs_pkt_pop(pipe, NULL);
716 * if all packet are gone, usbhsh_endpoint_disable()
718 * then, attached device/endpoint/pipe will be detached
720 usbhsh_queue_done(priv, pkt);
724 static void usbhsh_queue_force_pop_all(struct usbhs_priv *priv)
726 struct usbhs_pipe *pos;
729 usbhs_for_each_pipe_with_dcp(pos, priv, i)
730 usbhsh_queue_force_pop(priv, pos);
736 static int usbhsh_is_request_address(struct urb *urb)
738 struct usb_ctrlrequest *req;
740 req = (struct usb_ctrlrequest *)urb->setup_packet;
742 if ((DeviceOutRequest == req->bRequestType << 8) &&
743 (USB_REQ_SET_ADDRESS == req->bRequest))
749 static void usbhsh_setup_stage_packet_push(struct usbhsh_hpriv *hpriv,
751 struct usbhs_pipe *pipe)
753 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
754 struct usb_ctrlrequest req;
755 struct device *dev = usbhs_priv_to_dev(priv);
758 * wait setup packet ACK
760 * usbhsh_irq_setup_ack()
761 * usbhsh_irq_setup_err()
763 init_completion(&hpriv->setup_ack_done);
765 /* copy original request */
766 memcpy(&req, urb->setup_packet, sizeof(struct usb_ctrlrequest));
769 * renesas_usbhs can not use original usb address.
770 * see HARDWARE LIMITATION.
771 * modify usb address here to use attached device.
772 * see usbhsh_device_attach()
774 if (usbhsh_is_request_address(urb)) {
775 struct usb_device *usbv = usbhsh_urb_to_usbv(urb);
776 struct usbhsh_device *udev = usbhsh_usbv_to_udev(usbv);
778 /* udev is a attached device */
779 req.wValue = usbhsh_device_number(hpriv, udev);
780 dev_dbg(dev, "create new address - %d\n", req.wValue);
784 usbhs_usbreq_set_val(priv, &req);
787 * wait setup packet ACK
789 wait_for_completion(&hpriv->setup_ack_done);
791 dev_dbg(dev, "%s done\n", __func__);
797 static void usbhsh_data_stage_packet_done(struct usbhs_priv *priv,
798 struct usbhs_pkt *pkt)
800 struct usbhsh_request *ureq = usbhsh_pkt_to_ureq(pkt);
801 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
803 /* this ureq was connected to urb when usbhsh_urb_enqueue() */
805 usbhsh_ureq_free(hpriv, ureq);
808 static int usbhsh_data_stage_packet_push(struct usbhsh_hpriv *hpriv,
810 struct usbhs_pipe *pipe,
814 struct usbhsh_request *ureq;
816 /* this ureq will be freed on usbhsh_data_stage_packet_done() */
817 ureq = usbhsh_ureq_alloc(hpriv, urb, mem_flags);
821 if (usb_pipein(urb->pipe))
822 pipe->handler = &usbhs_dcp_data_stage_in_handler;
824 pipe->handler = &usbhs_dcp_data_stage_out_handler;
826 usbhs_pkt_push(pipe, &ureq->pkt,
827 usbhsh_data_stage_packet_done,
828 urb->transfer_buffer,
829 urb->transfer_buffer_length,
830 (urb->transfer_flags & URB_ZERO_PACKET),
839 static int usbhsh_status_stage_packet_push(struct usbhsh_hpriv *hpriv,
841 struct usbhs_pipe *pipe,
844 struct usbhsh_request *ureq;
846 /* This ureq will be freed on usbhsh_queue_done() */
847 ureq = usbhsh_ureq_alloc(hpriv, urb, mem_flags);
851 if (usb_pipein(urb->pipe))
852 pipe->handler = &usbhs_dcp_status_stage_in_handler;
854 pipe->handler = &usbhs_dcp_status_stage_out_handler;
856 usbhs_pkt_push(pipe, &ureq->pkt,
859 urb->transfer_buffer_length,
865 static int usbhsh_dcp_queue_push(struct usb_hcd *hcd,
869 struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
870 struct usbhsh_ep *uep = usbhsh_ep_to_uep(urb->ep);
871 struct usbhs_pipe *pipe = usbhsh_uep_to_pipe(uep);
872 struct device *dev = usbhsh_hcd_to_dev(hcd);
875 dev_dbg(dev, "%s\n", __func__);
880 * usbhsh_send_setup_stage_packet() wait SACK/SIGN
882 usbhsh_setup_stage_packet_push(hpriv, urb, pipe);
887 * It is pushed only when urb has buffer.
889 if (urb->transfer_buffer_length) {
890 ret = usbhsh_data_stage_packet_push(hpriv, urb, pipe, mflags);
892 dev_err(dev, "data stage failed\n");
900 ret = usbhsh_status_stage_packet_push(hpriv, urb, pipe, mflags);
902 dev_err(dev, "status stage failed\n");
907 * start pushed packets
909 usbhs_pkt_start(pipe);
917 static int usbhsh_dma_map_ctrl(struct device *dma_dev, struct usbhs_pkt *pkt,
921 struct usbhsh_request *ureq = usbhsh_pkt_to_ureq(pkt);
922 struct urb *urb = ureq->urb;
924 /* it can not use scatter/gather */
928 pkt->dma = urb->transfer_dma;
939 static int usbhsh_host_start(struct usb_hcd *hcd)
944 static void usbhsh_host_stop(struct usb_hcd *hcd)
948 static int usbhsh_urb_enqueue(struct usb_hcd *hcd,
952 struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
953 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
954 struct device *dev = usbhs_priv_to_dev(priv);
955 struct usb_host_endpoint *ep = urb->ep;
956 struct usbhsh_device *new_udev = NULL;
957 int is_dir_in = usb_pipein(urb->pipe);
960 dev_dbg(dev, "%s (%s)\n", __func__, is_dir_in ? "in" : "out");
962 if (!usbhsh_is_running(hpriv)) {
964 dev_err(dev, "host is not running\n");
965 goto usbhsh_urb_enqueue_error_not_linked;
968 ret = usb_hcd_link_urb_to_ep(hcd, urb);
970 dev_err(dev, "urb link failed\n");
971 goto usbhsh_urb_enqueue_error_not_linked;
975 * attach udev if needed
976 * see [image of mod_host]
978 if (!usbhsh_device_get(hpriv, urb)) {
979 new_udev = usbhsh_device_attach(hpriv, urb);
982 dev_err(dev, "device attach failed\n");
983 goto usbhsh_urb_enqueue_error_not_linked;
988 * attach endpoint if needed
989 * see [image of mod_host]
991 if (!usbhsh_ep_to_uep(ep)) {
992 ret = usbhsh_endpoint_attach(hpriv, urb, mem_flags);
994 dev_err(dev, "endpoint attach failed\n");
995 goto usbhsh_urb_enqueue_error_free_device;
1000 * attach pipe to endpoint
1001 * see [image of mod_host]
1003 ret = usbhsh_pipe_attach(hpriv, urb);
1005 dev_err(dev, "pipe attach failed\n");
1006 goto usbhsh_urb_enqueue_error_free_endpoint;
1012 if (usb_pipecontrol(urb->pipe))
1013 ret = usbhsh_dcp_queue_push(hcd, urb, mem_flags);
1015 ret = usbhsh_queue_push(hcd, urb, mem_flags);
1019 usbhsh_urb_enqueue_error_free_endpoint:
1020 usbhsh_endpoint_detach(hpriv, ep);
1021 usbhsh_urb_enqueue_error_free_device:
1023 usbhsh_device_detach(hpriv, new_udev);
1024 usbhsh_urb_enqueue_error_not_linked:
1026 dev_dbg(dev, "%s error\n", __func__);
1031 static int usbhsh_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
1033 struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
1034 struct usbhsh_request *ureq = usbhsh_urb_to_ureq(urb);
1037 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
1038 struct usbhs_pkt *pkt = &ureq->pkt;
1040 usbhs_pkt_pop(pkt->pipe, pkt);
1041 usbhsh_queue_done(priv, pkt);
1047 static void usbhsh_endpoint_disable(struct usb_hcd *hcd,
1048 struct usb_host_endpoint *ep)
1050 struct usbhsh_ep *uep = usbhsh_ep_to_uep(ep);
1051 struct usbhsh_device *udev;
1052 struct usbhsh_hpriv *hpriv;
1055 * this function might be called manytimes by same hcd/ep
1056 * in-endpoint == out-endpoint if ep == dcp.
1061 udev = usbhsh_uep_to_udev(uep);
1062 hpriv = usbhsh_hcd_to_hpriv(hcd);
1064 usbhsh_endpoint_detach(hpriv, ep);
1067 * if there is no endpoint,
1070 if (!usbhsh_device_has_endpoint(udev))
1071 usbhsh_device_detach(hpriv, udev);
1074 static int usbhsh_hub_status_data(struct usb_hcd *hcd, char *buf)
1076 struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
1077 int roothub_id = 1; /* only 1 root hub */
1080 * does port stat was changed ?
1081 * check USB_PORT_STAT_C_xxx << 16
1083 if (usbhsh_port_stat_get(hpriv) & 0xFFFF0000)
1084 *buf = (1 << roothub_id);
1091 static int __usbhsh_hub_hub_feature(struct usbhsh_hpriv *hpriv,
1092 u16 typeReq, u16 wValue,
1093 u16 wIndex, char *buf, u16 wLength)
1095 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
1096 struct device *dev = usbhs_priv_to_dev(priv);
1099 case C_HUB_OVER_CURRENT:
1100 case C_HUB_LOCAL_POWER:
1101 dev_dbg(dev, "%s :: C_HUB_xx\n", __func__);
1108 static int __usbhsh_hub_port_feature(struct usbhsh_hpriv *hpriv,
1109 u16 typeReq, u16 wValue,
1110 u16 wIndex, char *buf, u16 wLength)
1112 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
1113 struct device *dev = usbhs_priv_to_dev(priv);
1114 int enable = (typeReq == SetPortFeature);
1115 int speed, i, timeout = 128;
1116 int roothub_id = 1; /* only 1 root hub */
1119 if (wIndex > roothub_id || wLength != 0)
1124 case USB_PORT_FEAT_POWER:
1125 usbhs_vbus_ctrl(priv, enable);
1126 dev_dbg(dev, "%s :: USB_PORT_FEAT_POWER\n", __func__);
1129 case USB_PORT_FEAT_ENABLE:
1130 case USB_PORT_FEAT_SUSPEND:
1131 case USB_PORT_FEAT_C_ENABLE:
1132 case USB_PORT_FEAT_C_SUSPEND:
1133 case USB_PORT_FEAT_C_CONNECTION:
1134 case USB_PORT_FEAT_C_OVER_CURRENT:
1135 case USB_PORT_FEAT_C_RESET:
1136 dev_dbg(dev, "%s :: USB_PORT_FEAT_xxx\n", __func__);
1139 case USB_PORT_FEAT_RESET:
1143 usbhsh_port_stat_clear(hpriv,
1144 USB_PORT_STAT_HIGH_SPEED |
1145 USB_PORT_STAT_LOW_SPEED);
1147 usbhsh_queue_force_pop_all(priv);
1149 usbhs_bus_send_reset(priv);
1151 usbhs_bus_send_sof_enable(priv);
1153 for (i = 0; i < timeout ; i++) {
1154 switch (usbhs_bus_get_speed(priv)) {
1156 speed = USB_PORT_STAT_LOW_SPEED;
1157 goto got_usb_bus_speed;
1158 case USB_SPEED_HIGH:
1159 speed = USB_PORT_STAT_HIGH_SPEED;
1160 goto got_usb_bus_speed;
1161 case USB_SPEED_FULL:
1163 goto got_usb_bus_speed;
1171 usbhsh_port_stat_set(hpriv, speed);
1172 usbhsh_port_stat_set(hpriv, USB_PORT_STAT_ENABLE);
1174 dev_dbg(dev, "%s :: USB_PORT_FEAT_RESET (speed = %d)\n",
1177 /* status change is not needed */
1184 /* set/clear status */
1186 usbhsh_port_stat_set(hpriv, (1 << wValue));
1188 usbhsh_port_stat_clear(hpriv, (1 << wValue));
1193 static int __usbhsh_hub_get_status(struct usbhsh_hpriv *hpriv,
1194 u16 typeReq, u16 wValue,
1195 u16 wIndex, char *buf, u16 wLength)
1197 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
1198 struct usb_hub_descriptor *desc = (struct usb_hub_descriptor *)buf;
1199 struct device *dev = usbhs_priv_to_dev(priv);
1200 int roothub_id = 1; /* only 1 root hub */
1204 dev_dbg(dev, "%s :: GetHubStatus\n", __func__);
1210 if (wIndex != roothub_id)
1213 dev_dbg(dev, "%s :: GetPortStatus\n", __func__);
1214 *(__le32 *)buf = cpu_to_le32(usbhsh_port_stat_get(hpriv));
1217 case GetHubDescriptor:
1218 desc->bDescriptorType = USB_DT_HUB;
1219 desc->bHubContrCurrent = 0;
1220 desc->bNbrPorts = roothub_id;
1221 desc->bDescLength = 9;
1222 desc->bPwrOn2PwrGood = 0;
1223 desc->wHubCharacteristics =
1224 cpu_to_le16(HUB_CHAR_INDV_PORT_LPSM | HUB_CHAR_NO_OCPM);
1225 desc->u.hs.DeviceRemovable[0] = (roothub_id << 1);
1226 desc->u.hs.DeviceRemovable[1] = ~0;
1227 dev_dbg(dev, "%s :: GetHubDescriptor\n", __func__);
1234 static int usbhsh_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
1235 u16 wIndex, char *buf, u16 wLength)
1237 struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
1238 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
1239 struct device *dev = usbhs_priv_to_dev(priv);
1245 case ClearHubFeature:
1247 ret = __usbhsh_hub_hub_feature(hpriv, typeReq,
1248 wValue, wIndex, buf, wLength);
1252 case SetPortFeature:
1253 case ClearPortFeature:
1254 ret = __usbhsh_hub_port_feature(hpriv, typeReq,
1255 wValue, wIndex, buf, wLength);
1261 case GetHubDescriptor:
1262 ret = __usbhsh_hub_get_status(hpriv, typeReq,
1263 wValue, wIndex, buf, wLength);
1267 dev_dbg(dev, "typeReq = %x, ret = %d, port_stat = %x\n",
1268 typeReq, ret, usbhsh_port_stat_get(hpriv));
1273 static int usbhsh_bus_nop(struct usb_hcd *hcd)
1279 static const struct hc_driver usbhsh_driver = {
1280 .description = usbhsh_hcd_name,
1281 .hcd_priv_size = sizeof(struct usbhsh_hpriv),
1284 * generic hardware linkage
1288 .start = usbhsh_host_start,
1289 .stop = usbhsh_host_stop,
1292 * managing i/o requests and associated device resources
1294 .urb_enqueue = usbhsh_urb_enqueue,
1295 .urb_dequeue = usbhsh_urb_dequeue,
1296 .endpoint_disable = usbhsh_endpoint_disable,
1301 .hub_status_data = usbhsh_hub_status_data,
1302 .hub_control = usbhsh_hub_control,
1303 .bus_suspend = usbhsh_bus_nop,
1304 .bus_resume = usbhsh_bus_nop,
1308 * interrupt functions
1310 static int usbhsh_irq_attch(struct usbhs_priv *priv,
1311 struct usbhs_irq_state *irq_state)
1313 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1314 struct device *dev = usbhs_priv_to_dev(priv);
1316 dev_dbg(dev, "device attached\n");
1318 usbhsh_port_stat_set(hpriv, USB_PORT_STAT_CONNECTION);
1319 usbhsh_port_stat_set(hpriv, USB_PORT_STAT_C_CONNECTION << 16);
1322 * attch interrupt might happen infinitely on some device
1323 * (on self power USB hub ?)
1326 * usbhsh_is_running() becomes effective
1327 * according to this process.
1329 * usbhsh_is_running()
1330 * usbhsh_urb_enqueue()
1332 hpriv->mod.irq_attch = NULL;
1333 usbhs_irq_callback_update(priv, &hpriv->mod);
1338 static int usbhsh_irq_dtch(struct usbhs_priv *priv,
1339 struct usbhs_irq_state *irq_state)
1341 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1342 struct device *dev = usbhs_priv_to_dev(priv);
1344 dev_dbg(dev, "device detached\n");
1346 usbhsh_port_stat_clear(hpriv, USB_PORT_STAT_CONNECTION);
1347 usbhsh_port_stat_set(hpriv, USB_PORT_STAT_C_CONNECTION << 16);
1350 * enable attch interrupt again
1352 * usbhsh_is_running() becomes invalid
1353 * according to this process.
1355 * usbhsh_is_running()
1356 * usbhsh_urb_enqueue()
1358 hpriv->mod.irq_attch = usbhsh_irq_attch;
1359 usbhs_irq_callback_update(priv, &hpriv->mod);
1362 * usbhsh_queue_force_pop_all() should be called
1363 * after usbhsh_is_running() becomes invalid.
1365 usbhsh_queue_force_pop_all(priv);
1370 static int usbhsh_irq_setup_ack(struct usbhs_priv *priv,
1371 struct usbhs_irq_state *irq_state)
1373 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1374 struct device *dev = usbhs_priv_to_dev(priv);
1376 dev_dbg(dev, "setup packet OK\n");
1378 complete(&hpriv->setup_ack_done); /* see usbhsh_urb_enqueue() */
1383 static int usbhsh_irq_setup_err(struct usbhs_priv *priv,
1384 struct usbhs_irq_state *irq_state)
1386 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1387 struct device *dev = usbhs_priv_to_dev(priv);
1389 dev_dbg(dev, "setup packet Err\n");
1391 complete(&hpriv->setup_ack_done); /* see usbhsh_urb_enqueue() */
1399 static void usbhsh_pipe_init_for_host(struct usbhs_priv *priv)
1401 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1402 struct usbhs_pipe *pipe;
1403 struct renesas_usbhs_driver_pipe_config *pipe_configs =
1404 usbhs_get_dparam(priv, pipe_configs);
1405 int pipe_size = usbhs_get_dparam(priv, pipe_size);
1406 int old_type, dir_in, i;
1409 old_type = USB_ENDPOINT_XFER_CONTROL;
1410 for (i = 0; i < pipe_size; i++) {
1413 * data "output" will be finished as soon as possible,
1414 * but there is no guaranty at data "input" case.
1416 * "input" needs "standby" pipe.
1417 * So, "input" direction pipe > "output" direction pipe
1420 * 1st USB_ENDPOINT_XFER_xxx will be output direction,
1421 * and the other will be input direction here.
1425 * USB_ENDPOINT_XFER_ISOC -> dir out
1426 * USB_ENDPOINT_XFER_ISOC -> dir in
1427 * USB_ENDPOINT_XFER_BULK -> dir out
1428 * USB_ENDPOINT_XFER_BULK -> dir in
1429 * USB_ENDPOINT_XFER_BULK -> dir in
1432 dir_in = (pipe_configs[i].type == old_type);
1433 old_type = pipe_configs[i].type;
1435 if (USB_ENDPOINT_XFER_CONTROL == pipe_configs[i].type) {
1436 pipe = usbhs_dcp_malloc(priv);
1437 usbhsh_hpriv_to_dcp(hpriv) = pipe;
1439 pipe = usbhs_pipe_malloc(priv,
1440 pipe_configs[i].type,
1444 pipe->mod_private = NULL;
1448 static int usbhsh_start(struct usbhs_priv *priv)
1450 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1451 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
1452 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
1453 struct device *dev = usbhs_priv_to_dev(priv);
1457 ret = usb_add_hcd(hcd, 0, 0);
1460 device_wakeup_enable(hcd->self.controller);
1463 * pipe initialize and enable DCP
1465 usbhs_fifo_init(priv);
1466 usbhs_pipe_init(priv,
1467 usbhsh_dma_map_ctrl);
1468 usbhsh_pipe_init_for_host(priv);
1471 * system config enble
1476 usbhs_sys_host_ctrl(priv, 1);
1479 * enable irq callback
1481 mod->irq_attch = usbhsh_irq_attch;
1482 mod->irq_dtch = usbhsh_irq_dtch;
1483 mod->irq_sack = usbhsh_irq_setup_ack;
1484 mod->irq_sign = usbhsh_irq_setup_err;
1485 usbhs_irq_callback_update(priv, mod);
1487 dev_dbg(dev, "start host\n");
1492 static int usbhsh_stop(struct usbhs_priv *priv)
1494 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1495 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
1496 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
1497 struct device *dev = usbhs_priv_to_dev(priv);
1500 * disable irq callback
1502 mod->irq_attch = NULL;
1503 mod->irq_dtch = NULL;
1504 mod->irq_sack = NULL;
1505 mod->irq_sign = NULL;
1506 usbhs_irq_callback_update(priv, mod);
1508 usb_remove_hcd(hcd);
1511 usbhs_sys_host_ctrl(priv, 0);
1513 dev_dbg(dev, "quit host\n");
1518 int usbhs_mod_host_probe(struct usbhs_priv *priv)
1520 struct usbhsh_hpriv *hpriv;
1521 struct usb_hcd *hcd;
1522 struct usbhsh_device *udev;
1523 struct device *dev = usbhs_priv_to_dev(priv);
1526 /* initialize hcd */
1527 hcd = usb_create_hcd(&usbhsh_driver, dev, usbhsh_hcd_name);
1529 dev_err(dev, "Failed to create hcd\n");
1532 hcd->has_tt = 1; /* for low/full speed */
1537 * There is no guarantee that it is possible to access usb module here.
1538 * Don't accesses to it.
1539 * The accesse will be enable after "usbhsh_start"
1542 hpriv = usbhsh_hcd_to_hpriv(hcd);
1547 usbhs_mod_register(priv, &hpriv->mod, USBHS_HOST);
1550 hpriv->mod.name = "host";
1551 hpriv->mod.start = usbhsh_start;
1552 hpriv->mod.stop = usbhsh_stop;
1553 usbhsh_port_stat_init(hpriv);
1555 /* init all device */
1556 usbhsh_for_each_udev_with_dev0(udev, hpriv, i) {
1558 INIT_LIST_HEAD(&udev->ep_list_head);
1561 dev_info(dev, "host probed\n");
1566 int usbhs_mod_host_remove(struct usbhs_priv *priv)
1568 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1569 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);