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 /******************** spin lock ********************/
338 usbhs_lock(priv, flags);
340 pipe = usbhsh_uep_to_pipe(uep);
342 if (unlikely(!pipe)) {
343 dev_err(dev, "uep doens't have pipe\n");
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);
379 dev_err(dev, "usbhsh_ep alloc fail\n");
383 /******************** spin lock ********************/
384 usbhs_lock(priv, flags);
389 INIT_LIST_HEAD(&uep->ep_list);
390 list_add_tail(&uep->ep_list, &udev->ep_list_head);
392 usbhsh_uep_to_udev(uep) = udev;
393 usbhsh_uep_to_ep(uep) = ep;
394 usbhsh_ep_to_uep(ep) = uep;
396 usbhs_unlock(priv, flags);
397 /******************** spin unlock ******************/
399 dev_dbg(dev, "%s [%d-%d]\n", __func__,
400 usbhsh_device_number(hpriv, udev),
401 usb_endpoint_num(desc));
406 static void usbhsh_endpoint_detach(struct usbhsh_hpriv *hpriv,
407 struct usb_host_endpoint *ep)
409 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
410 struct device *dev = usbhs_priv_to_dev(priv);
411 struct usbhsh_ep *uep = usbhsh_ep_to_uep(ep);
417 dev_dbg(dev, "%s [%d-%d]\n", __func__,
418 usbhsh_device_number(hpriv, usbhsh_uep_to_udev(uep)),
419 usb_endpoint_num(&ep->desc));
421 if (usbhsh_uep_to_pipe(uep))
422 usbhsh_pipe_detach(hpriv, uep);
424 /******************** spin lock ********************/
425 usbhs_lock(priv, flags);
427 /* remove this endpoint from udev */
428 list_del_init(&uep->ep_list);
430 usbhsh_uep_to_udev(uep) = NULL;
431 usbhsh_uep_to_ep(uep) = NULL;
432 usbhsh_ep_to_uep(ep) = NULL;
434 usbhs_unlock(priv, flags);
435 /******************** spin unlock ******************/
440 static void usbhsh_endpoint_detach_all(struct usbhsh_hpriv *hpriv,
441 struct usbhsh_device *udev)
443 struct usbhsh_ep *uep, *next;
445 list_for_each_entry_safe(uep, next, &udev->ep_list_head, ep_list)
446 usbhsh_endpoint_detach(hpriv, usbhsh_uep_to_ep(uep));
452 static int usbhsh_connected_to_rhdev(struct usb_hcd *hcd,
453 struct usbhsh_device *udev)
455 struct usb_device *usbv = usbhsh_udev_to_usbv(udev);
457 return hcd->self.root_hub == usbv->parent;
460 static int usbhsh_device_has_endpoint(struct usbhsh_device *udev)
462 return !list_empty(&udev->ep_list_head);
465 static struct usbhsh_device *usbhsh_device_get(struct usbhsh_hpriv *hpriv,
468 struct usb_device *usbv = usbhsh_urb_to_usbv(urb);
469 struct usbhsh_device *udev = usbhsh_usbv_to_udev(usbv);
471 /* usbhsh_device_attach() is still not called */
475 /* if it is device0, return it */
476 if (0 == usb_pipedevice(urb->pipe))
477 return usbhsh_device0(hpriv);
479 /* return attached device */
483 static struct usbhsh_device *usbhsh_device_attach(struct usbhsh_hpriv *hpriv,
486 struct usbhsh_device *udev = NULL;
487 struct usbhsh_device *udev0 = usbhsh_device0(hpriv);
488 struct usbhsh_device *pos;
489 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
490 struct device *dev = usbhsh_hcd_to_dev(hcd);
491 struct usb_device *usbv = usbhsh_urb_to_usbv(urb);
492 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
498 * This function should be called only while urb is pointing to device0.
499 * It will attach unused usbhsh_device to urb (usbv),
500 * and initialize device0.
501 * You can use usbhsh_device_get() to get "current" udev,
502 * and usbhsh_usbv_to_udev() is for "attached" udev.
504 if (0 != usb_pipedevice(urb->pipe)) {
505 dev_err(dev, "%s fail: urb isn't pointing device0\n", __func__);
509 /******************** spin lock ********************/
510 usbhs_lock(priv, flags);
515 usbhsh_for_each_udev(pos, hpriv, i) {
516 if (usbhsh_udev_is_used(pos))
524 * usbhsh_usbv_to_udev()
525 * usbhsh_udev_to_usbv()
528 dev_set_drvdata(&usbv->dev, udev);
532 usbhs_unlock(priv, flags);
533 /******************** spin unlock ******************/
536 dev_err(dev, "no free usbhsh_device\n");
540 if (usbhsh_device_has_endpoint(udev)) {
541 dev_warn(dev, "udev have old endpoint\n");
542 usbhsh_endpoint_detach_all(hpriv, udev);
545 if (usbhsh_device_has_endpoint(udev0)) {
546 dev_warn(dev, "udev0 have old endpoint\n");
547 usbhsh_endpoint_detach_all(hpriv, udev0);
550 /* uep will be attached */
551 INIT_LIST_HEAD(&udev0->ep_list_head);
552 INIT_LIST_HEAD(&udev->ep_list_head);
557 usbhs_set_device_config(priv,
558 0, 0, 0, usbv->speed);
561 * set new device config
565 if (!usbhsh_connected_to_rhdev(hcd, udev)) {
566 /* if udev is not connected to rhdev, it means parent is Hub */
567 struct usbhsh_device *parent = usbhsh_device_parent(udev);
569 upphub = usbhsh_device_number(hpriv, parent);
570 hubport = usbhsh_device_hubport(udev);
572 dev_dbg(dev, "%s connecte to Hub [%d:%d](%p)\n", __func__,
573 upphub, hubport, parent);
576 usbhs_set_device_config(priv,
577 usbhsh_device_number(hpriv, udev),
578 upphub, hubport, usbv->speed);
580 dev_dbg(dev, "%s [%d](%p)\n", __func__,
581 usbhsh_device_number(hpriv, udev), udev);
586 static void usbhsh_device_detach(struct usbhsh_hpriv *hpriv,
587 struct usbhsh_device *udev)
589 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
590 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
591 struct device *dev = usbhsh_hcd_to_dev(hcd);
592 struct usb_device *usbv = usbhsh_udev_to_usbv(udev);
595 dev_dbg(dev, "%s [%d](%p)\n", __func__,
596 usbhsh_device_number(hpriv, udev), udev);
598 if (usbhsh_device_has_endpoint(udev)) {
599 dev_warn(dev, "udev still have endpoint\n");
600 usbhsh_endpoint_detach_all(hpriv, udev);
604 * There is nothing to do if it is device0.
606 * usbhsh_device_attach()
607 * usbhsh_device_get()
609 if (0 == usbhsh_device_number(hpriv, udev))
612 /******************** spin lock ********************/
613 usbhs_lock(priv, flags);
616 * usbhsh_usbv_to_udev()
617 * usbhsh_udev_to_usbv()
620 dev_set_drvdata(&usbv->dev, NULL);
623 usbhs_unlock(priv, flags);
624 /******************** spin unlock ******************/
630 static void usbhsh_queue_done(struct usbhs_priv *priv, struct usbhs_pkt *pkt)
632 struct usbhsh_request *ureq = usbhsh_pkt_to_ureq(pkt);
633 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
634 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
635 struct urb *urb = ureq->urb;
636 struct usbhsh_ep *uep = usbhsh_ep_to_uep(urb->ep);
637 struct device *dev = usbhs_priv_to_dev(priv);
640 dev_dbg(dev, "%s\n", __func__);
643 dev_warn(dev, "pkt doesn't have urb\n");
647 if (!usbhsh_is_running(hpriv))
650 urb->actual_length = pkt->actual;
651 usbhsh_ureq_free(hpriv, ureq);
653 usbhsh_endpoint_sequence_save(hpriv, urb, pkt);
654 usbhsh_pipe_detach(hpriv, uep);
656 usb_hcd_unlink_urb_from_ep(hcd, urb);
657 usb_hcd_giveback_urb(hcd, urb, status);
660 static int usbhsh_queue_push(struct usb_hcd *hcd,
664 struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
665 struct usbhsh_ep *uep = usbhsh_ep_to_uep(urb->ep);
666 struct usbhs_pipe *pipe = usbhsh_uep_to_pipe(uep);
667 struct device *dev = usbhsh_hcd_to_dev(hcd);
668 struct usbhsh_request *ureq;
672 if (usb_pipeisoc(urb->pipe)) {
673 dev_err(dev, "pipe iso is not supported now\n");
677 /* this ureq will be freed on usbhsh_queue_done() */
678 ureq = usbhsh_ureq_alloc(hpriv, urb, mem_flags);
679 if (unlikely(!ureq)) {
680 dev_err(dev, "ureq alloc fail\n");
684 if (usb_pipein(urb->pipe))
685 pipe->handler = &usbhs_fifo_pio_pop_handler;
687 pipe->handler = &usbhs_fifo_pio_push_handler;
689 buf = (void *)(urb->transfer_buffer + urb->actual_length);
690 len = urb->transfer_buffer_length - urb->actual_length;
692 sequence = usb_gettoggle(urb->dev,
693 usb_pipeendpoint(urb->pipe),
694 usb_pipeout(urb->pipe));
696 dev_dbg(dev, "%s\n", __func__);
697 usbhs_pkt_push(pipe, &ureq->pkt, usbhsh_queue_done,
698 buf, len, (urb->transfer_flags & URB_ZERO_PACKET),
701 usbhs_pkt_start(pipe);
706 static void usbhsh_queue_force_pop(struct usbhs_priv *priv,
707 struct usbhs_pipe *pipe)
709 struct usbhs_pkt *pkt;
712 pkt = usbhs_pkt_pop(pipe, NULL);
717 * if all packet are gone, usbhsh_endpoint_disable()
719 * then, attached device/endpoint/pipe will be detached
721 usbhsh_queue_done(priv, pkt);
725 static void usbhsh_queue_force_pop_all(struct usbhs_priv *priv)
727 struct usbhs_pipe *pos;
730 usbhs_for_each_pipe_with_dcp(pos, priv, i)
731 usbhsh_queue_force_pop(priv, pos);
737 static int usbhsh_is_request_address(struct urb *urb)
739 struct usb_ctrlrequest *req;
741 req = (struct usb_ctrlrequest *)urb->setup_packet;
743 if ((DeviceOutRequest == req->bRequestType << 8) &&
744 (USB_REQ_SET_ADDRESS == req->bRequest))
750 static void usbhsh_setup_stage_packet_push(struct usbhsh_hpriv *hpriv,
752 struct usbhs_pipe *pipe)
754 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
755 struct usb_ctrlrequest req;
756 struct device *dev = usbhs_priv_to_dev(priv);
759 * wait setup packet ACK
761 * usbhsh_irq_setup_ack()
762 * usbhsh_irq_setup_err()
764 init_completion(&hpriv->setup_ack_done);
766 /* copy original request */
767 memcpy(&req, urb->setup_packet, sizeof(struct usb_ctrlrequest));
770 * renesas_usbhs can not use original usb address.
771 * see HARDWARE LIMITATION.
772 * modify usb address here to use attached device.
773 * see usbhsh_device_attach()
775 if (usbhsh_is_request_address(urb)) {
776 struct usb_device *usbv = usbhsh_urb_to_usbv(urb);
777 struct usbhsh_device *udev = usbhsh_usbv_to_udev(usbv);
779 /* udev is a attached device */
780 req.wValue = usbhsh_device_number(hpriv, udev);
781 dev_dbg(dev, "create new address - %d\n", req.wValue);
785 usbhs_usbreq_set_val(priv, &req);
788 * wait setup packet ACK
790 wait_for_completion(&hpriv->setup_ack_done);
792 dev_dbg(dev, "%s done\n", __func__);
798 static void usbhsh_data_stage_packet_done(struct usbhs_priv *priv,
799 struct usbhs_pkt *pkt)
801 struct usbhsh_request *ureq = usbhsh_pkt_to_ureq(pkt);
802 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
804 /* this ureq was connected to urb when usbhsh_urb_enqueue() */
806 usbhsh_ureq_free(hpriv, ureq);
809 static int usbhsh_data_stage_packet_push(struct usbhsh_hpriv *hpriv,
811 struct usbhs_pipe *pipe,
815 struct usbhsh_request *ureq;
817 /* this ureq will be freed on usbhsh_data_stage_packet_done() */
818 ureq = usbhsh_ureq_alloc(hpriv, urb, mem_flags);
822 if (usb_pipein(urb->pipe))
823 pipe->handler = &usbhs_dcp_data_stage_in_handler;
825 pipe->handler = &usbhs_dcp_data_stage_out_handler;
827 usbhs_pkt_push(pipe, &ureq->pkt,
828 usbhsh_data_stage_packet_done,
829 urb->transfer_buffer,
830 urb->transfer_buffer_length,
831 (urb->transfer_flags & URB_ZERO_PACKET),
840 static int usbhsh_status_stage_packet_push(struct usbhsh_hpriv *hpriv,
842 struct usbhs_pipe *pipe,
845 struct usbhsh_request *ureq;
847 /* This ureq will be freed on usbhsh_queue_done() */
848 ureq = usbhsh_ureq_alloc(hpriv, urb, mem_flags);
852 if (usb_pipein(urb->pipe))
853 pipe->handler = &usbhs_dcp_status_stage_in_handler;
855 pipe->handler = &usbhs_dcp_status_stage_out_handler;
857 usbhs_pkt_push(pipe, &ureq->pkt,
860 urb->transfer_buffer_length,
866 static int usbhsh_dcp_queue_push(struct usb_hcd *hcd,
870 struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
871 struct usbhsh_ep *uep = usbhsh_ep_to_uep(urb->ep);
872 struct usbhs_pipe *pipe = usbhsh_uep_to_pipe(uep);
873 struct device *dev = usbhsh_hcd_to_dev(hcd);
876 dev_dbg(dev, "%s\n", __func__);
881 * usbhsh_send_setup_stage_packet() wait SACK/SIGN
883 usbhsh_setup_stage_packet_push(hpriv, urb, pipe);
888 * It is pushed only when urb has buffer.
890 if (urb->transfer_buffer_length) {
891 ret = usbhsh_data_stage_packet_push(hpriv, urb, pipe, mflags);
893 dev_err(dev, "data stage failed\n");
901 ret = usbhsh_status_stage_packet_push(hpriv, urb, pipe, mflags);
903 dev_err(dev, "status stage failed\n");
908 * start pushed packets
910 usbhs_pkt_start(pipe);
918 static int usbhsh_dma_map_ctrl(struct usbhs_pkt *pkt, int map)
926 static int usbhsh_host_start(struct usb_hcd *hcd)
931 static void usbhsh_host_stop(struct usb_hcd *hcd)
935 static int usbhsh_urb_enqueue(struct usb_hcd *hcd,
939 struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
940 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
941 struct device *dev = usbhs_priv_to_dev(priv);
942 struct usb_host_endpoint *ep = urb->ep;
943 struct usbhsh_device *new_udev = NULL;
944 int is_dir_in = usb_pipein(urb->pipe);
948 dev_dbg(dev, "%s (%s)\n", __func__, is_dir_in ? "in" : "out");
950 if (!usbhsh_is_running(hpriv)) {
952 dev_err(dev, "host is not running\n");
953 goto usbhsh_urb_enqueue_error_not_linked;
956 ret = usb_hcd_link_urb_to_ep(hcd, urb);
958 dev_err(dev, "urb link failed\n");
959 goto usbhsh_urb_enqueue_error_not_linked;
963 * attach udev if needed
964 * see [image of mod_host]
966 if (!usbhsh_device_get(hpriv, urb)) {
967 new_udev = usbhsh_device_attach(hpriv, urb);
970 dev_err(dev, "device attach failed\n");
971 goto usbhsh_urb_enqueue_error_not_linked;
976 * attach endpoint if needed
977 * see [image of mod_host]
979 if (!usbhsh_ep_to_uep(ep)) {
980 ret = usbhsh_endpoint_attach(hpriv, urb, mem_flags);
982 dev_err(dev, "endpoint attach failed\n");
983 goto usbhsh_urb_enqueue_error_free_device;
988 * attach pipe to endpoint
989 * see [image of mod_host]
991 for (i = 0; i < 1024; i++) {
992 ret = usbhsh_pipe_attach(hpriv, urb);
999 dev_err(dev, "pipe attach failed\n");
1000 goto usbhsh_urb_enqueue_error_free_endpoint;
1006 if (usb_pipecontrol(urb->pipe))
1007 ret = usbhsh_dcp_queue_push(hcd, urb, mem_flags);
1009 ret = usbhsh_queue_push(hcd, urb, mem_flags);
1013 usbhsh_urb_enqueue_error_free_endpoint:
1014 usbhsh_endpoint_detach(hpriv, ep);
1015 usbhsh_urb_enqueue_error_free_device:
1017 usbhsh_device_detach(hpriv, new_udev);
1018 usbhsh_urb_enqueue_error_not_linked:
1020 dev_dbg(dev, "%s error\n", __func__);
1025 static int usbhsh_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
1027 struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
1028 struct usbhsh_request *ureq = usbhsh_urb_to_ureq(urb);
1031 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
1032 struct usbhs_pkt *pkt = &ureq->pkt;
1034 usbhs_pkt_pop(pkt->pipe, pkt);
1035 usbhsh_queue_done(priv, pkt);
1041 static void usbhsh_endpoint_disable(struct usb_hcd *hcd,
1042 struct usb_host_endpoint *ep)
1044 struct usbhsh_ep *uep = usbhsh_ep_to_uep(ep);
1045 struct usbhsh_device *udev;
1046 struct usbhsh_hpriv *hpriv;
1049 * this function might be called manytimes by same hcd/ep
1050 * in-endpoint == out-endpoint if ep == dcp.
1055 udev = usbhsh_uep_to_udev(uep);
1056 hpriv = usbhsh_hcd_to_hpriv(hcd);
1058 usbhsh_endpoint_detach(hpriv, ep);
1061 * if there is no endpoint,
1064 if (!usbhsh_device_has_endpoint(udev))
1065 usbhsh_device_detach(hpriv, udev);
1068 static int usbhsh_hub_status_data(struct usb_hcd *hcd, char *buf)
1070 struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
1071 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
1072 struct device *dev = usbhs_priv_to_dev(priv);
1073 int roothub_id = 1; /* only 1 root hub */
1076 * does port stat was changed ?
1077 * check USB_PORT_STAT_C_xxx << 16
1079 if (usbhsh_port_stat_get(hpriv) & 0xFFFF0000)
1080 *buf = (1 << roothub_id);
1084 dev_dbg(dev, "%s (%02x)\n", __func__, *buf);
1089 static int __usbhsh_hub_hub_feature(struct usbhsh_hpriv *hpriv,
1090 u16 typeReq, u16 wValue,
1091 u16 wIndex, char *buf, u16 wLength)
1093 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
1094 struct device *dev = usbhs_priv_to_dev(priv);
1097 case C_HUB_OVER_CURRENT:
1098 case C_HUB_LOCAL_POWER:
1099 dev_dbg(dev, "%s :: C_HUB_xx\n", __func__);
1106 static int __usbhsh_hub_port_feature(struct usbhsh_hpriv *hpriv,
1107 u16 typeReq, u16 wValue,
1108 u16 wIndex, char *buf, u16 wLength)
1110 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
1111 struct device *dev = usbhs_priv_to_dev(priv);
1112 int enable = (typeReq == SetPortFeature);
1113 int speed, i, timeout = 128;
1114 int roothub_id = 1; /* only 1 root hub */
1117 if (wIndex > roothub_id || wLength != 0)
1122 case USB_PORT_FEAT_POWER:
1123 usbhs_vbus_ctrl(priv, enable);
1124 dev_dbg(dev, "%s :: USB_PORT_FEAT_POWER\n", __func__);
1127 case USB_PORT_FEAT_ENABLE:
1128 case USB_PORT_FEAT_SUSPEND:
1129 case USB_PORT_FEAT_C_ENABLE:
1130 case USB_PORT_FEAT_C_SUSPEND:
1131 case USB_PORT_FEAT_C_CONNECTION:
1132 case USB_PORT_FEAT_C_OVER_CURRENT:
1133 case USB_PORT_FEAT_C_RESET:
1134 dev_dbg(dev, "%s :: USB_PORT_FEAT_xxx\n", __func__);
1137 case USB_PORT_FEAT_RESET:
1141 usbhsh_port_stat_clear(hpriv,
1142 USB_PORT_STAT_HIGH_SPEED |
1143 USB_PORT_STAT_LOW_SPEED);
1145 usbhsh_queue_force_pop_all(priv);
1147 usbhs_bus_send_reset(priv);
1149 usbhs_bus_send_sof_enable(priv);
1151 for (i = 0; i < timeout ; i++) {
1152 switch (usbhs_bus_get_speed(priv)) {
1154 speed = USB_PORT_STAT_LOW_SPEED;
1155 goto got_usb_bus_speed;
1156 case USB_SPEED_HIGH:
1157 speed = USB_PORT_STAT_HIGH_SPEED;
1158 goto got_usb_bus_speed;
1159 case USB_SPEED_FULL:
1161 goto got_usb_bus_speed;
1169 usbhsh_port_stat_set(hpriv, speed);
1170 usbhsh_port_stat_set(hpriv, USB_PORT_STAT_ENABLE);
1172 dev_dbg(dev, "%s :: USB_PORT_FEAT_RESET (speed = %d)\n",
1175 /* status change is not needed */
1182 /* set/clear status */
1184 usbhsh_port_stat_set(hpriv, (1 << wValue));
1186 usbhsh_port_stat_clear(hpriv, (1 << wValue));
1191 static int __usbhsh_hub_get_status(struct usbhsh_hpriv *hpriv,
1192 u16 typeReq, u16 wValue,
1193 u16 wIndex, char *buf, u16 wLength)
1195 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
1196 struct usb_hub_descriptor *desc = (struct usb_hub_descriptor *)buf;
1197 struct device *dev = usbhs_priv_to_dev(priv);
1198 int roothub_id = 1; /* only 1 root hub */
1202 dev_dbg(dev, "%s :: GetHubStatus\n", __func__);
1208 if (wIndex != roothub_id)
1211 dev_dbg(dev, "%s :: GetPortStatus\n", __func__);
1212 *(__le32 *)buf = cpu_to_le32(usbhsh_port_stat_get(hpriv));
1215 case GetHubDescriptor:
1216 desc->bDescriptorType = 0x29;
1217 desc->bHubContrCurrent = 0;
1218 desc->bNbrPorts = roothub_id;
1219 desc->bDescLength = 9;
1220 desc->bPwrOn2PwrGood = 0;
1221 desc->wHubCharacteristics = cpu_to_le16(0x0011);
1222 desc->u.hs.DeviceRemovable[0] = (roothub_id << 1);
1223 desc->u.hs.DeviceRemovable[1] = ~0;
1224 dev_dbg(dev, "%s :: GetHubDescriptor\n", __func__);
1231 static int usbhsh_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
1232 u16 wIndex, char *buf, u16 wLength)
1234 struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
1235 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
1236 struct device *dev = usbhs_priv_to_dev(priv);
1242 case ClearHubFeature:
1244 ret = __usbhsh_hub_hub_feature(hpriv, typeReq,
1245 wValue, wIndex, buf, wLength);
1249 case SetPortFeature:
1250 case ClearPortFeature:
1251 ret = __usbhsh_hub_port_feature(hpriv, typeReq,
1252 wValue, wIndex, buf, wLength);
1258 case GetHubDescriptor:
1259 ret = __usbhsh_hub_get_status(hpriv, typeReq,
1260 wValue, wIndex, buf, wLength);
1264 dev_dbg(dev, "typeReq = %x, ret = %d, port_stat = %x\n",
1265 typeReq, ret, usbhsh_port_stat_get(hpriv));
1270 static struct hc_driver usbhsh_driver = {
1271 .description = usbhsh_hcd_name,
1272 .hcd_priv_size = sizeof(struct usbhsh_hpriv),
1275 * generic hardware linkage
1279 .start = usbhsh_host_start,
1280 .stop = usbhsh_host_stop,
1283 * managing i/o requests and associated device resources
1285 .urb_enqueue = usbhsh_urb_enqueue,
1286 .urb_dequeue = usbhsh_urb_dequeue,
1287 .endpoint_disable = usbhsh_endpoint_disable,
1292 .hub_status_data = usbhsh_hub_status_data,
1293 .hub_control = usbhsh_hub_control,
1297 * interrupt functions
1299 static int usbhsh_irq_attch(struct usbhs_priv *priv,
1300 struct usbhs_irq_state *irq_state)
1302 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1303 struct device *dev = usbhs_priv_to_dev(priv);
1305 dev_dbg(dev, "device attached\n");
1307 usbhsh_port_stat_set(hpriv, USB_PORT_STAT_CONNECTION);
1308 usbhsh_port_stat_set(hpriv, USB_PORT_STAT_C_CONNECTION << 16);
1311 * attch interrupt might happen infinitely on some device
1312 * (on self power USB hub ?)
1315 * usbhsh_is_running() becomes effective
1316 * according to this process.
1318 * usbhsh_is_running()
1319 * usbhsh_urb_enqueue()
1321 hpriv->mod.irq_attch = NULL;
1322 usbhs_irq_callback_update(priv, &hpriv->mod);
1327 static int usbhsh_irq_dtch(struct usbhs_priv *priv,
1328 struct usbhs_irq_state *irq_state)
1330 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1331 struct device *dev = usbhs_priv_to_dev(priv);
1333 dev_dbg(dev, "device detached\n");
1335 usbhsh_port_stat_clear(hpriv, USB_PORT_STAT_CONNECTION);
1336 usbhsh_port_stat_set(hpriv, USB_PORT_STAT_C_CONNECTION << 16);
1339 * enable attch interrupt again
1341 * usbhsh_is_running() becomes invalid
1342 * according to this process.
1344 * usbhsh_is_running()
1345 * usbhsh_urb_enqueue()
1347 hpriv->mod.irq_attch = usbhsh_irq_attch;
1348 usbhs_irq_callback_update(priv, &hpriv->mod);
1351 * usbhsh_queue_force_pop_all() should be called
1352 * after usbhsh_is_running() becomes invalid.
1354 usbhsh_queue_force_pop_all(priv);
1359 static int usbhsh_irq_setup_ack(struct usbhs_priv *priv,
1360 struct usbhs_irq_state *irq_state)
1362 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1363 struct device *dev = usbhs_priv_to_dev(priv);
1365 dev_dbg(dev, "setup packet OK\n");
1367 complete(&hpriv->setup_ack_done); /* see usbhsh_urb_enqueue() */
1372 static int usbhsh_irq_setup_err(struct usbhs_priv *priv,
1373 struct usbhs_irq_state *irq_state)
1375 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1376 struct device *dev = usbhs_priv_to_dev(priv);
1378 dev_dbg(dev, "setup packet Err\n");
1380 complete(&hpriv->setup_ack_done); /* see usbhsh_urb_enqueue() */
1388 static void usbhsh_pipe_init_for_host(struct usbhs_priv *priv)
1390 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1391 struct usbhs_pipe *pipe;
1392 u32 *pipe_type = usbhs_get_dparam(priv, pipe_type);
1393 int pipe_size = usbhs_get_dparam(priv, pipe_size);
1394 int old_type, dir_in, i;
1397 old_type = USB_ENDPOINT_XFER_CONTROL;
1398 for (i = 0; i < pipe_size; i++) {
1401 * data "output" will be finished as soon as possible,
1402 * but there is no guaranty at data "input" case.
1404 * "input" needs "standby" pipe.
1405 * So, "input" direction pipe > "output" direction pipe
1408 * 1st USB_ENDPOINT_XFER_xxx will be output direction,
1409 * and the other will be input direction here.
1413 * USB_ENDPOINT_XFER_ISOC -> dir out
1414 * USB_ENDPOINT_XFER_ISOC -> dir in
1415 * USB_ENDPOINT_XFER_BULK -> dir out
1416 * USB_ENDPOINT_XFER_BULK -> dir in
1417 * USB_ENDPOINT_XFER_BULK -> dir in
1420 dir_in = (pipe_type[i] == old_type);
1421 old_type = pipe_type[i];
1423 if (USB_ENDPOINT_XFER_CONTROL == pipe_type[i]) {
1424 pipe = usbhs_dcp_malloc(priv);
1425 usbhsh_hpriv_to_dcp(hpriv) = pipe;
1427 pipe = usbhs_pipe_malloc(priv,
1432 pipe->mod_private = NULL;
1436 static int usbhsh_start(struct usbhs_priv *priv)
1438 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1439 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
1440 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
1441 struct device *dev = usbhs_priv_to_dev(priv);
1445 ret = usb_add_hcd(hcd, 0, 0);
1450 * pipe initialize and enable DCP
1452 usbhs_pipe_init(priv,
1453 usbhsh_dma_map_ctrl);
1454 usbhs_fifo_init(priv);
1455 usbhsh_pipe_init_for_host(priv);
1458 * system config enble
1463 usbhs_sys_host_ctrl(priv, 1);
1466 * enable irq callback
1468 mod->irq_attch = usbhsh_irq_attch;
1469 mod->irq_dtch = usbhsh_irq_dtch;
1470 mod->irq_sack = usbhsh_irq_setup_ack;
1471 mod->irq_sign = usbhsh_irq_setup_err;
1472 usbhs_irq_callback_update(priv, mod);
1474 dev_dbg(dev, "start host\n");
1479 static int usbhsh_stop(struct usbhs_priv *priv)
1481 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1482 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
1483 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
1484 struct device *dev = usbhs_priv_to_dev(priv);
1487 * disable irq callback
1489 mod->irq_attch = NULL;
1490 mod->irq_dtch = NULL;
1491 mod->irq_sack = NULL;
1492 mod->irq_sign = NULL;
1493 usbhs_irq_callback_update(priv, mod);
1495 usb_remove_hcd(hcd);
1498 usbhs_sys_host_ctrl(priv, 0);
1500 dev_dbg(dev, "quit host\n");
1505 int usbhs_mod_host_probe(struct usbhs_priv *priv)
1507 struct usbhsh_hpriv *hpriv;
1508 struct usb_hcd *hcd;
1509 struct usbhsh_device *udev;
1510 struct device *dev = usbhs_priv_to_dev(priv);
1513 /* initialize hcd */
1514 hcd = usb_create_hcd(&usbhsh_driver, dev, usbhsh_hcd_name);
1516 dev_err(dev, "Failed to create hcd\n");
1523 * There is no guarantee that it is possible to access usb module here.
1524 * Don't accesses to it.
1525 * The accesse will be enable after "usbhsh_start"
1528 hpriv = usbhsh_hcd_to_hpriv(hcd);
1533 usbhs_mod_register(priv, &hpriv->mod, USBHS_HOST);
1536 hpriv->mod.name = "host";
1537 hpriv->mod.start = usbhsh_start;
1538 hpriv->mod.stop = usbhsh_stop;
1539 usbhsh_port_stat_init(hpriv);
1541 /* init all device */
1542 usbhsh_for_each_udev_with_dev0(udev, hpriv, i) {
1544 INIT_LIST_HEAD(&udev->ep_list_head);
1547 dev_info(dev, "host probed\n");
1552 int usbhs_mod_host_remove(struct usbhs_priv *priv)
1554 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1555 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);