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
18 #include <linux/list.h>
19 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <linux/slab.h>
22 #include <linux/usb.h>
23 #include <linux/usb/hcd.h>
27 *** HARDWARE LIMITATION ***
29 * 1) renesas_usbhs has a limited number of controllable devices.
30 * it can control only 9 devices in generally.
31 * see DEVADDn / DCPMAXP / PIPEMAXP.
33 * 2) renesas_usbhs pipe number is limited.
34 * the pipe will be re-used for each devices.
35 * so, software should control DATA0/1 sequence of each devices.
43 * | udev 0 | --> it is used when set address
46 * +--------+ pipes are reused for each uep.
47 * | udev 1 |-+- [uep 0 (dcp) ] --+ pipe will be switched when
48 * +--------+ | | other device requested
49 * +- [uep 1 (bulk)] --|---+ +--------------+
50 * | +--------------> | pipe0 (dcp) |
51 * +- [uep 2 (bulk)] -@ | +--------------+
53 * +--------+ | +--------------+
54 * | udev 2 |-+- [uep 0 (dcp) ] -@ +----------> | pipe2 (bulk) |
55 * +--------+ | +--------------+
56 * +- [uep 1 (int) ] ----+ +------> | pipe3 (bulk) |
57 * | | +--------------+
58 * +--------+ +-----|------> | pipe4 (int) |
59 * | udev 3 |-+- [uep 0 (dcp) ] -@ | +--------------+
60 * +--------+ | | | .... |
61 * +- [uep 1 (bulk)] -@ | | .... |
63 * +- [uep 2 (bulk)]-----------+
65 * @ : uep requested free pipe, but all have been used.
66 * now it is waiting for free pipe
73 struct usbhsh_request {
78 struct usbhsh_device {
79 struct usb_device *usbv;
80 struct list_head ep_list_head; /* list of usbhsh_ep */
84 struct usbhs_pipe *pipe; /* attached pipe */
85 struct usbhsh_device *udev; /* attached udev */
86 struct usb_host_endpoint *ep;
87 struct list_head ep_list; /* list to usbhsh_device */
90 #define USBHSH_DEVICE_MAX 10 /* see DEVADDn / DCPMAXP / PIPEMAXP */
91 #define USBHSH_PORT_MAX 7 /* see DEVADDn :: HUBPORT */
94 struct usbhs_pipe *dcp;
96 struct usbhsh_device udev[USBHSH_DEVICE_MAX];
98 u32 port_stat; /* USB_PORT_STAT_xxx */
100 struct completion setup_ack_done;
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)
163 static struct usbhsh_request *usbhsh_ureq_alloc(struct usbhsh_hpriv *hpriv,
167 struct usbhsh_request *ureq;
168 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
169 struct device *dev = usbhs_priv_to_dev(priv);
171 ureq = kzalloc(sizeof(struct usbhsh_request), mem_flags);
173 dev_err(dev, "ureq alloc fail\n");
177 usbhs_pkt_init(&ureq->pkt);
179 usbhsh_urb_to_ureq(urb) = ureq;
184 static void usbhsh_ureq_free(struct usbhsh_hpriv *hpriv,
185 struct usbhsh_request *ureq)
187 usbhsh_urb_to_ureq(ureq->urb) = NULL;
196 static int usbhsh_is_running(struct usbhsh_hpriv *hpriv)
199 * we can decide some device is attached or not
200 * by checking mod.irq_attch
205 return (hpriv->mod.irq_attch == NULL);
211 static void usbhsh_endpoint_sequence_save(struct usbhsh_hpriv *hpriv,
213 struct usbhs_pkt *pkt)
215 int len = urb->actual_length;
216 int maxp = usb_endpoint_maxp(&urb->ep->desc);
219 /* DCP is out of sequence control */
220 if (usb_pipecontrol(urb->pipe))
224 * renesas_usbhs pipe has a limitation in a number.
225 * So, driver should re-use the limited pipe for each device/endpoint.
226 * DATA0/1 sequence should be saved for it.
227 * see [image of mod_host]
228 * [HARDWARE LIMITATION]
232 * next sequence depends on actual_length
234 * ex) actual_length = 1147, maxp = 512
238 * data1 is the next sequence
248 usb_dotoggle(urb->dev,
249 usb_pipeendpoint(urb->pipe),
250 usb_pipeout(urb->pipe));
253 static struct usbhsh_device *usbhsh_device_get(struct usbhsh_hpriv *hpriv,
256 static int usbhsh_pipe_attach(struct usbhsh_hpriv *hpriv,
259 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
260 struct usbhsh_ep *uep = usbhsh_ep_to_uep(urb->ep);
261 struct usbhsh_device *udev = usbhsh_device_get(hpriv, urb);
262 struct usbhs_pipe *pipe;
263 struct usb_endpoint_descriptor *desc = &urb->ep->desc;
264 struct device *dev = usbhs_priv_to_dev(priv);
266 int dir_in_req = !!usb_pipein(urb->pipe);
267 int is_dcp = usb_endpoint_xfer_control(desc);
271 /******************** spin lock ********************/
272 usbhs_lock(priv, flags);
274 if (unlikely(usbhsh_uep_to_pipe(uep))) {
275 dev_err(dev, "uep already has pipe\n");
276 goto usbhsh_pipe_attach_done;
279 usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
281 /* check pipe type */
282 if (!usbhs_pipe_type_is(pipe, usb_endpoint_type(desc)))
285 /* check pipe direction if normal pipe */
287 dir_in = !!usbhs_pipe_is_dir_in(pipe);
288 if (0 != (dir_in - dir_in_req))
292 /* check pipe is free */
293 if (usbhsh_pipe_to_uep(pipe))
299 * usbhs_pipe_config_update() should be called after
300 * usbhs_set_device_config()
304 usbhsh_uep_to_pipe(uep) = pipe;
305 usbhsh_pipe_to_uep(pipe) = uep;
307 usbhs_pipe_config_update(pipe,
308 usbhsh_device_number(hpriv, udev),
309 usb_endpoint_num(desc),
310 usb_endpoint_maxp(desc));
312 dev_dbg(dev, "%s [%d-%d(%s:%s)]\n", __func__,
313 usbhsh_device_number(hpriv, udev),
314 usb_endpoint_num(desc),
315 usbhs_pipe_name(pipe),
316 dir_in_req ? "in" : "out");
322 usbhsh_pipe_attach_done:
323 usbhs_unlock(priv, flags);
324 /******************** spin unlock ******************/
329 static void usbhsh_pipe_detach(struct usbhsh_hpriv *hpriv,
330 struct usbhsh_ep *uep)
332 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
333 struct usbhs_pipe *pipe;
334 struct device *dev = usbhs_priv_to_dev(priv);
337 if (unlikely(!uep)) {
338 dev_err(dev, "no uep\n");
342 /******************** spin lock ********************/
343 usbhs_lock(priv, flags);
345 pipe = usbhsh_uep_to_pipe(uep);
347 if (unlikely(!pipe)) {
348 dev_err(dev, "uep doens't have pipe\n");
350 struct usb_host_endpoint *ep = usbhsh_uep_to_ep(uep);
351 struct usbhsh_device *udev = usbhsh_uep_to_udev(uep);
353 /* detach pipe from uep */
354 usbhsh_uep_to_pipe(uep) = NULL;
355 usbhsh_pipe_to_uep(pipe) = NULL;
357 dev_dbg(dev, "%s [%d-%d(%s)]\n", __func__,
358 usbhsh_device_number(hpriv, udev),
359 usb_endpoint_num(&ep->desc),
360 usbhs_pipe_name(pipe));
363 usbhs_unlock(priv, flags);
364 /******************** spin unlock ******************/
370 static int usbhsh_endpoint_attach(struct usbhsh_hpriv *hpriv,
374 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
375 struct usbhsh_device *udev = usbhsh_device_get(hpriv, urb);
376 struct usb_host_endpoint *ep = urb->ep;
377 struct usbhsh_ep *uep;
378 struct device *dev = usbhs_priv_to_dev(priv);
379 struct usb_endpoint_descriptor *desc = &ep->desc;
382 uep = kzalloc(sizeof(struct usbhsh_ep), mem_flags);
384 dev_err(dev, "usbhsh_ep alloc fail\n");
388 /******************** spin lock ********************/
389 usbhs_lock(priv, flags);
394 INIT_LIST_HEAD(&uep->ep_list);
395 list_add_tail(&uep->ep_list, &udev->ep_list_head);
397 usbhsh_uep_to_udev(uep) = udev;
398 usbhsh_uep_to_ep(uep) = ep;
399 usbhsh_ep_to_uep(ep) = uep;
401 usbhs_unlock(priv, flags);
402 /******************** spin unlock ******************/
404 dev_dbg(dev, "%s [%d-%d]\n", __func__,
405 usbhsh_device_number(hpriv, udev),
406 usb_endpoint_num(desc));
411 static void usbhsh_endpoint_detach(struct usbhsh_hpriv *hpriv,
412 struct usb_host_endpoint *ep)
414 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
415 struct device *dev = usbhs_priv_to_dev(priv);
416 struct usbhsh_ep *uep = usbhsh_ep_to_uep(ep);
422 dev_dbg(dev, "%s [%d-%d]\n", __func__,
423 usbhsh_device_number(hpriv, usbhsh_uep_to_udev(uep)),
424 usb_endpoint_num(&ep->desc));
426 if (usbhsh_uep_to_pipe(uep))
427 usbhsh_pipe_detach(hpriv, uep);
429 /******************** spin lock ********************/
430 usbhs_lock(priv, flags);
432 /* remove this endpoint from udev */
433 list_del_init(&uep->ep_list);
435 usbhsh_uep_to_udev(uep) = NULL;
436 usbhsh_uep_to_ep(uep) = NULL;
437 usbhsh_ep_to_uep(ep) = NULL;
439 usbhs_unlock(priv, flags);
440 /******************** spin unlock ******************/
445 static void usbhsh_endpoint_detach_all(struct usbhsh_hpriv *hpriv,
446 struct usbhsh_device *udev)
448 struct usbhsh_ep *uep, *next;
450 list_for_each_entry_safe(uep, next, &udev->ep_list_head, ep_list)
451 usbhsh_endpoint_detach(hpriv, usbhsh_uep_to_ep(uep));
457 static int usbhsh_connected_to_rhdev(struct usb_hcd *hcd,
458 struct usbhsh_device *udev)
460 struct usb_device *usbv = usbhsh_udev_to_usbv(udev);
462 return hcd->self.root_hub == usbv->parent;
465 static int usbhsh_device_has_endpoint(struct usbhsh_device *udev)
467 return !list_empty(&udev->ep_list_head);
470 static struct usbhsh_device *usbhsh_device_get(struct usbhsh_hpriv *hpriv,
473 struct usb_device *usbv = usbhsh_urb_to_usbv(urb);
474 struct usbhsh_device *udev = usbhsh_usbv_to_udev(usbv);
476 /* usbhsh_device_attach() is still not called */
480 /* if it is device0, return it */
481 if (0 == usb_pipedevice(urb->pipe))
482 return usbhsh_device0(hpriv);
484 /* return attached device */
488 static struct usbhsh_device *usbhsh_device_attach(struct usbhsh_hpriv *hpriv,
491 struct usbhsh_device *udev = NULL;
492 struct usbhsh_device *udev0 = usbhsh_device0(hpriv);
493 struct usbhsh_device *pos;
494 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
495 struct device *dev = usbhsh_hcd_to_dev(hcd);
496 struct usb_device *usbv = usbhsh_urb_to_usbv(urb);
497 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
503 * This function should be called only while urb is pointing to device0.
504 * It will attach unused usbhsh_device to urb (usbv),
505 * and initialize device0.
506 * You can use usbhsh_device_get() to get "current" udev,
507 * and usbhsh_usbv_to_udev() is for "attached" udev.
509 if (0 != usb_pipedevice(urb->pipe)) {
510 dev_err(dev, "%s fail: urb isn't pointing device0\n", __func__);
514 /******************** spin lock ********************/
515 usbhs_lock(priv, flags);
520 usbhsh_for_each_udev(pos, hpriv, i) {
521 if (usbhsh_udev_is_used(pos))
529 * usbhsh_usbv_to_udev()
530 * usbhsh_udev_to_usbv()
533 dev_set_drvdata(&usbv->dev, udev);
537 usbhs_unlock(priv, flags);
538 /******************** spin unlock ******************/
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);
555 /* uep will be attached */
556 INIT_LIST_HEAD(&udev0->ep_list_head);
557 INIT_LIST_HEAD(&udev->ep_list_head);
562 usbhs_set_device_config(priv,
563 0, 0, 0, usbv->speed);
566 * set new device config
570 if (!usbhsh_connected_to_rhdev(hcd, udev)) {
571 /* if udev is not connected to rhdev, it means parent is Hub */
572 struct usbhsh_device *parent = usbhsh_device_parent(udev);
574 upphub = usbhsh_device_number(hpriv, parent);
575 hubport = usbhsh_device_hubport(udev);
577 dev_dbg(dev, "%s connecte to Hub [%d:%d](%p)\n", __func__,
578 upphub, hubport, parent);
581 usbhs_set_device_config(priv,
582 usbhsh_device_number(hpriv, udev),
583 upphub, hubport, usbv->speed);
585 dev_dbg(dev, "%s [%d](%p)\n", __func__,
586 usbhsh_device_number(hpriv, udev), udev);
591 static void usbhsh_device_detach(struct usbhsh_hpriv *hpriv,
592 struct usbhsh_device *udev)
594 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
595 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
596 struct device *dev = usbhsh_hcd_to_dev(hcd);
597 struct usb_device *usbv = usbhsh_udev_to_usbv(udev);
600 dev_dbg(dev, "%s [%d](%p)\n", __func__,
601 usbhsh_device_number(hpriv, udev), udev);
603 if (usbhsh_device_has_endpoint(udev)) {
604 dev_warn(dev, "udev still have endpoint\n");
605 usbhsh_endpoint_detach_all(hpriv, udev);
609 * There is nothing to do if it is device0.
611 * usbhsh_device_attach()
612 * usbhsh_device_get()
614 if (0 == usbhsh_device_number(hpriv, udev))
617 /******************** spin lock ********************/
618 usbhs_lock(priv, flags);
621 * usbhsh_usbv_to_udev()
622 * usbhsh_udev_to_usbv()
625 dev_set_drvdata(&usbv->dev, NULL);
628 usbhs_unlock(priv, flags);
629 /******************** spin unlock ******************/
635 static void usbhsh_queue_done(struct usbhs_priv *priv, struct usbhs_pkt *pkt)
637 struct usbhsh_request *ureq = usbhsh_pkt_to_ureq(pkt);
638 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
639 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
640 struct urb *urb = ureq->urb;
641 struct device *dev = usbhs_priv_to_dev(priv);
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);
658 usbhsh_pipe_detach(hpriv, usbhsh_ep_to_uep(urb->ep));
660 usb_hcd_unlink_urb_from_ep(hcd, urb);
661 usb_hcd_giveback_urb(hcd, urb, status);
664 static int usbhsh_queue_push(struct usb_hcd *hcd,
668 struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
669 struct usbhsh_ep *uep = usbhsh_ep_to_uep(urb->ep);
670 struct usbhs_pipe *pipe = usbhsh_uep_to_pipe(uep);
671 struct device *dev = usbhsh_hcd_to_dev(hcd);
672 struct usbhsh_request *ureq;
676 if (usb_pipeisoc(urb->pipe)) {
677 dev_err(dev, "pipe iso is not supported now\n");
681 /* this ureq will be freed on usbhsh_queue_done() */
682 ureq = usbhsh_ureq_alloc(hpriv, urb, mem_flags);
683 if (unlikely(!ureq)) {
684 dev_err(dev, "ureq alloc fail\n");
688 if (usb_pipein(urb->pipe))
689 pipe->handler = &usbhs_fifo_pio_pop_handler;
691 pipe->handler = &usbhs_fifo_pio_push_handler;
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__);
701 usbhs_pkt_push(pipe, &ureq->pkt, usbhsh_queue_done,
702 buf, len, (urb->transfer_flags & URB_ZERO_PACKET),
705 usbhs_pkt_start(pipe);
710 static void usbhsh_queue_force_pop(struct usbhs_priv *priv,
711 struct usbhs_pipe *pipe)
713 struct usbhs_pkt *pkt;
716 pkt = usbhs_pkt_pop(pipe, NULL);
721 * if all packet are gone, usbhsh_endpoint_disable()
723 * then, attached device/endpoint/pipe will be detached
725 usbhsh_queue_done(priv, pkt);
729 static void usbhsh_queue_force_pop_all(struct usbhs_priv *priv)
731 struct usbhs_pipe *pos;
734 usbhs_for_each_pipe_with_dcp(pos, priv, i)
735 usbhsh_queue_force_pop(priv, pos);
741 static int usbhsh_is_request_address(struct urb *urb)
743 struct usb_ctrlrequest *req;
745 req = (struct usb_ctrlrequest *)urb->setup_packet;
747 if ((DeviceOutRequest == req->bRequestType << 8) &&
748 (USB_REQ_SET_ADDRESS == req->bRequest))
754 static void usbhsh_setup_stage_packet_push(struct usbhsh_hpriv *hpriv,
756 struct usbhs_pipe *pipe)
758 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
759 struct usb_ctrlrequest req;
760 struct device *dev = usbhs_priv_to_dev(priv);
763 * wait setup packet ACK
765 * usbhsh_irq_setup_ack()
766 * usbhsh_irq_setup_err()
768 init_completion(&hpriv->setup_ack_done);
770 /* copy original request */
771 memcpy(&req, urb->setup_packet, sizeof(struct usb_ctrlrequest));
774 * renesas_usbhs can not use original usb address.
775 * see HARDWARE LIMITATION.
776 * modify usb address here to use attached device.
777 * see usbhsh_device_attach()
779 if (usbhsh_is_request_address(urb)) {
780 struct usb_device *usbv = usbhsh_urb_to_usbv(urb);
781 struct usbhsh_device *udev = usbhsh_usbv_to_udev(usbv);
783 /* udev is a attached device */
784 req.wValue = usbhsh_device_number(hpriv, udev);
785 dev_dbg(dev, "create new address - %d\n", req.wValue);
789 usbhs_usbreq_set_val(priv, &req);
792 * wait setup packet ACK
794 wait_for_completion(&hpriv->setup_ack_done);
796 dev_dbg(dev, "%s done\n", __func__);
802 static void usbhsh_data_stage_packet_done(struct usbhs_priv *priv,
803 struct usbhs_pkt *pkt)
805 struct usbhsh_request *ureq = usbhsh_pkt_to_ureq(pkt);
806 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
808 /* this ureq was connected to urb when usbhsh_urb_enqueue() */
810 usbhsh_ureq_free(hpriv, ureq);
813 static int usbhsh_data_stage_packet_push(struct usbhsh_hpriv *hpriv,
815 struct usbhs_pipe *pipe,
819 struct usbhsh_request *ureq;
821 /* this ureq will be freed on usbhsh_data_stage_packet_done() */
822 ureq = usbhsh_ureq_alloc(hpriv, urb, mem_flags);
826 if (usb_pipein(urb->pipe))
827 pipe->handler = &usbhs_dcp_data_stage_in_handler;
829 pipe->handler = &usbhs_dcp_data_stage_out_handler;
831 usbhs_pkt_push(pipe, &ureq->pkt,
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,
846 struct usbhs_pipe *pipe,
849 struct usbhsh_request *ureq;
851 /* This ureq will be freed on usbhsh_queue_done() */
852 ureq = usbhsh_ureq_alloc(hpriv, urb, mem_flags);
856 if (usb_pipein(urb->pipe))
857 pipe->handler = &usbhs_dcp_status_stage_in_handler;
859 pipe->handler = &usbhs_dcp_status_stage_out_handler;
861 usbhs_pkt_push(pipe, &ureq->pkt,
864 urb->transfer_buffer_length,
870 static int usbhsh_dcp_queue_push(struct usb_hcd *hcd,
874 struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
875 struct usbhsh_ep *uep = usbhsh_ep_to_uep(urb->ep);
876 struct usbhs_pipe *pipe = usbhsh_uep_to_pipe(uep);
877 struct device *dev = usbhsh_hcd_to_dev(hcd);
880 dev_dbg(dev, "%s\n", __func__);
885 * usbhsh_send_setup_stage_packet() wait SACK/SIGN
887 usbhsh_setup_stage_packet_push(hpriv, urb, pipe);
892 * It is pushed only when urb has buffer.
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");
912 * start pushed packets
914 usbhs_pkt_start(pipe);
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,
943 struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
944 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
945 struct device *dev = usbhs_priv_to_dev(priv);
946 struct usb_host_endpoint *ep = urb->ep;
947 struct usbhsh_device *new_udev = NULL;
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;
960 ret = usb_hcd_link_urb_to_ep(hcd, urb);
962 dev_err(dev, "urb link failed\n");
963 goto usbhsh_urb_enqueue_error_not_linked;
967 * attach udev if needed
968 * see [image of mod_host]
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;
980 * attach endpoint if needed
981 * see [image of mod_host]
983 if (!usbhsh_ep_to_uep(ep)) {
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;
992 * attach pipe to endpoint
993 * see [image of mod_host]
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)
1031 struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
1032 struct usbhsh_request *ureq = usbhsh_urb_to_ureq(urb);
1035 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
1036 struct usbhs_pkt *pkt = &ureq->pkt;
1038 usbhs_pkt_pop(pkt->pipe, pkt);
1039 usbhsh_queue_done(priv, pkt);
1045 static void usbhsh_endpoint_disable(struct usb_hcd *hcd,
1046 struct usb_host_endpoint *ep)
1048 struct usbhsh_ep *uep = usbhsh_ep_to_uep(ep);
1049 struct usbhsh_device *udev;
1050 struct usbhsh_hpriv *hpriv;
1053 * this function might be called manytimes by same hcd/ep
1054 * in-endpoint == out-endpoint if ep == dcp.
1059 udev = usbhsh_uep_to_udev(uep);
1060 hpriv = usbhsh_hcd_to_hpriv(hcd);
1062 usbhsh_endpoint_detach(hpriv, ep);
1065 * if there is no endpoint,
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)
1074 struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
1075 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
1076 struct device *dev = usbhs_priv_to_dev(priv);
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);
1088 dev_dbg(dev, "%s (%02x)\n", __func__, *buf);
1093 static int __usbhsh_hub_hub_feature(struct usbhsh_hpriv *hpriv,
1094 u16 typeReq, u16 wValue,
1095 u16 wIndex, char *buf, u16 wLength)
1097 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
1098 struct device *dev = usbhs_priv_to_dev(priv);
1101 case C_HUB_OVER_CURRENT:
1102 case C_HUB_LOCAL_POWER:
1103 dev_dbg(dev, "%s :: C_HUB_xx\n", __func__);
1110 static int __usbhsh_hub_port_feature(struct usbhsh_hpriv *hpriv,
1111 u16 typeReq, u16 wValue,
1112 u16 wIndex, char *buf, u16 wLength)
1114 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
1115 struct device *dev = usbhs_priv_to_dev(priv);
1116 int enable = (typeReq == SetPortFeature);
1117 int speed, i, timeout = 128;
1118 int roothub_id = 1; /* only 1 root hub */
1121 if (wIndex > roothub_id || wLength != 0)
1126 case USB_PORT_FEAT_POWER:
1127 usbhs_vbus_ctrl(priv, enable);
1128 dev_dbg(dev, "%s :: USB_PORT_FEAT_POWER\n", __func__);
1131 case USB_PORT_FEAT_ENABLE:
1132 case USB_PORT_FEAT_SUSPEND:
1133 case USB_PORT_FEAT_C_ENABLE:
1134 case USB_PORT_FEAT_C_SUSPEND:
1135 case USB_PORT_FEAT_C_CONNECTION:
1136 case USB_PORT_FEAT_C_OVER_CURRENT:
1137 case USB_PORT_FEAT_C_RESET:
1138 dev_dbg(dev, "%s :: USB_PORT_FEAT_xxx\n", __func__);
1141 case USB_PORT_FEAT_RESET:
1145 usbhsh_port_stat_clear(hpriv,
1146 USB_PORT_STAT_HIGH_SPEED |
1147 USB_PORT_STAT_LOW_SPEED);
1149 usbhsh_queue_force_pop_all(priv);
1151 usbhs_bus_send_reset(priv);
1153 usbhs_bus_send_sof_enable(priv);
1155 for (i = 0; i < timeout ; i++) {
1156 switch (usbhs_bus_get_speed(priv)) {
1158 speed = USB_PORT_STAT_LOW_SPEED;
1159 goto got_usb_bus_speed;
1160 case USB_SPEED_HIGH:
1161 speed = USB_PORT_STAT_HIGH_SPEED;
1162 goto got_usb_bus_speed;
1163 case USB_SPEED_FULL:
1165 goto got_usb_bus_speed;
1173 usbhsh_port_stat_set(hpriv, speed);
1174 usbhsh_port_stat_set(hpriv, USB_PORT_STAT_ENABLE);
1176 dev_dbg(dev, "%s :: USB_PORT_FEAT_RESET (speed = %d)\n",
1179 /* status change is not needed */
1186 /* set/clear status */
1188 usbhsh_port_stat_set(hpriv, (1 << wValue));
1190 usbhsh_port_stat_clear(hpriv, (1 << wValue));
1195 static int __usbhsh_hub_get_status(struct usbhsh_hpriv *hpriv,
1196 u16 typeReq, u16 wValue,
1197 u16 wIndex, char *buf, u16 wLength)
1199 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
1200 struct usb_hub_descriptor *desc = (struct usb_hub_descriptor *)buf;
1201 struct device *dev = usbhs_priv_to_dev(priv);
1202 int roothub_id = 1; /* only 1 root hub */
1206 dev_dbg(dev, "%s :: GetHubStatus\n", __func__);
1212 if (wIndex != roothub_id)
1215 dev_dbg(dev, "%s :: GetPortStatus\n", __func__);
1216 *(__le32 *)buf = cpu_to_le32(usbhsh_port_stat_get(hpriv));
1219 case GetHubDescriptor:
1220 desc->bDescriptorType = 0x29;
1221 desc->bHubContrCurrent = 0;
1222 desc->bNbrPorts = roothub_id;
1223 desc->bDescLength = 9;
1224 desc->bPwrOn2PwrGood = 0;
1225 desc->wHubCharacteristics = cpu_to_le16(0x0011);
1226 desc->u.hs.DeviceRemovable[0] = (roothub_id << 1);
1227 desc->u.hs.DeviceRemovable[1] = ~0;
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)
1238 struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
1239 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
1240 struct device *dev = usbhs_priv_to_dev(priv);
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",
1269 typeReq, ret, usbhsh_port_stat_get(hpriv));
1274 static int usbhsh_bus_nop(struct usb_hcd *hcd)
1280 static struct hc_driver usbhsh_driver = {
1281 .description = usbhsh_hcd_name,
1282 .hcd_priv_size = sizeof(struct usbhsh_hpriv),
1285 * generic hardware linkage
1289 .start = usbhsh_host_start,
1290 .stop = usbhsh_host_stop,
1293 * managing i/o requests and associated device resources
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,
1309 * interrupt functions
1311 static int usbhsh_irq_attch(struct usbhs_priv *priv,
1312 struct usbhs_irq_state *irq_state)
1314 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1315 struct device *dev = usbhs_priv_to_dev(priv);
1317 dev_dbg(dev, "device attached\n");
1319 usbhsh_port_stat_set(hpriv, USB_PORT_STAT_CONNECTION);
1320 usbhsh_port_stat_set(hpriv, USB_PORT_STAT_C_CONNECTION << 16);
1323 * attch interrupt might happen infinitely on some device
1324 * (on self power USB hub ?)
1327 * usbhsh_is_running() becomes effective
1328 * according to this process.
1330 * usbhsh_is_running()
1331 * usbhsh_urb_enqueue()
1333 hpriv->mod.irq_attch = NULL;
1334 usbhs_irq_callback_update(priv, &hpriv->mod);
1339 static int usbhsh_irq_dtch(struct usbhs_priv *priv,
1340 struct usbhs_irq_state *irq_state)
1342 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1343 struct device *dev = usbhs_priv_to_dev(priv);
1345 dev_dbg(dev, "device detached\n");
1347 usbhsh_port_stat_clear(hpriv, USB_PORT_STAT_CONNECTION);
1348 usbhsh_port_stat_set(hpriv, USB_PORT_STAT_C_CONNECTION << 16);
1351 * enable attch interrupt again
1353 * usbhsh_is_running() becomes invalid
1354 * according to this process.
1356 * usbhsh_is_running()
1357 * usbhsh_urb_enqueue()
1359 hpriv->mod.irq_attch = usbhsh_irq_attch;
1360 usbhs_irq_callback_update(priv, &hpriv->mod);
1363 * usbhsh_queue_force_pop_all() should be called
1364 * after usbhsh_is_running() becomes invalid.
1366 usbhsh_queue_force_pop_all(priv);
1371 static int usbhsh_irq_setup_ack(struct usbhs_priv *priv,
1372 struct usbhs_irq_state *irq_state)
1374 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1375 struct device *dev = usbhs_priv_to_dev(priv);
1377 dev_dbg(dev, "setup packet OK\n");
1379 complete(&hpriv->setup_ack_done); /* see usbhsh_urb_enqueue() */
1384 static int usbhsh_irq_setup_err(struct usbhs_priv *priv,
1385 struct usbhs_irq_state *irq_state)
1387 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1388 struct device *dev = usbhs_priv_to_dev(priv);
1390 dev_dbg(dev, "setup packet Err\n");
1392 complete(&hpriv->setup_ack_done); /* see usbhsh_urb_enqueue() */
1400 static void usbhsh_pipe_init_for_host(struct usbhs_priv *priv)
1402 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1403 struct usbhs_pipe *pipe;
1404 u32 *pipe_type = usbhs_get_dparam(priv, pipe_type);
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_type[i] == old_type);
1433 old_type = pipe_type[i];
1435 if (USB_ENDPOINT_XFER_CONTROL == pipe_type[i]) {
1436 pipe = usbhs_dcp_malloc(priv);
1437 usbhsh_hpriv_to_dcp(hpriv) = pipe;
1439 pipe = usbhs_pipe_malloc(priv,
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);
1462 * pipe initialize and enable DCP
1464 usbhs_pipe_init(priv,
1465 usbhsh_dma_map_ctrl);
1466 usbhs_fifo_init(priv);
1467 usbhsh_pipe_init_for_host(priv);
1470 * system config enble
1475 usbhs_sys_host_ctrl(priv, 1);
1478 * enable irq callback
1480 mod->irq_attch = usbhsh_irq_attch;
1481 mod->irq_dtch = usbhsh_irq_dtch;
1482 mod->irq_sack = usbhsh_irq_setup_ack;
1483 mod->irq_sign = usbhsh_irq_setup_err;
1484 usbhs_irq_callback_update(priv, mod);
1486 dev_dbg(dev, "start host\n");
1491 static int usbhsh_stop(struct usbhs_priv *priv)
1493 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1494 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
1495 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
1496 struct device *dev = usbhs_priv_to_dev(priv);
1499 * disable irq callback
1501 mod->irq_attch = NULL;
1502 mod->irq_dtch = NULL;
1503 mod->irq_sack = NULL;
1504 mod->irq_sign = NULL;
1505 usbhs_irq_callback_update(priv, mod);
1507 usb_remove_hcd(hcd);
1510 usbhs_sys_host_ctrl(priv, 0);
1512 dev_dbg(dev, "quit host\n");
1517 int usbhs_mod_host_probe(struct usbhs_priv *priv)
1519 struct usbhsh_hpriv *hpriv;
1520 struct usb_hcd *hcd;
1521 struct usbhsh_device *udev;
1522 struct device *dev = usbhs_priv_to_dev(priv);
1525 /* initialize hcd */
1526 hcd = usb_create_hcd(&usbhsh_driver, dev, usbhsh_hcd_name);
1528 dev_err(dev, "Failed to create hcd\n");
1531 hcd->has_tt = 1; /* for low/full speed */
1536 * There is no guarantee that it is possible to access usb module here.
1537 * Don't accesses to it.
1538 * The accesse will be enable after "usbhsh_start"
1541 hpriv = usbhsh_hcd_to_hpriv(hcd);
1546 usbhs_mod_register(priv, &hpriv->mod, USBHS_HOST);
1549 hpriv->mod.name = "host";
1550 hpriv->mod.start = usbhsh_start;
1551 hpriv->mod.stop = usbhsh_stop;
1552 usbhsh_port_stat_init(hpriv);
1554 /* init all device */
1555 usbhsh_for_each_udev_with_dev0(udev, hpriv, i) {
1557 INIT_LIST_HEAD(&udev->ep_list_head);
1560 dev_info(dev, "host probed\n");
1565 int usbhs_mod_host_remove(struct usbhs_priv *priv)
1567 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1568 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);