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 * +--------+ | | target device was changed
49 * +- [uep 1 (bulk)] --|---+ +--------------+
50 * | +--------------> | pipe0 (dcp) |
51 * +- [uep 2 (bulk)] --|---|---+ +--------------+
52 * | | | | pipe1 (isoc) |
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)]-----------+
70 struct usbhsh_pipe_info {
71 unsigned int usr_cnt; /* see usbhsh_endpoint_alloc() */
74 struct usbhsh_request {
77 struct list_head ureq_link; /* see hpriv :: ureq_link_xxx */
80 struct usbhsh_device {
81 struct usb_device *usbv;
82 struct list_head ep_list_head; /* list of usbhsh_ep */
86 struct usbhs_pipe *pipe;
87 struct usbhsh_device *udev; /* attached udev */
88 struct list_head ep_list; /* list to usbhsh_device */
93 #define USBHSH_DEVICE_MAX 10 /* see DEVADDn / DCPMAXP / PIPEMAXP */
94 #define USBHSH_PORT_MAX 7 /* see DEVADDn :: HUBPORT */
97 struct usbhs_pipe *dcp;
99 struct usbhsh_device udev[USBHSH_DEVICE_MAX];
101 struct usbhsh_pipe_info *pipe_info;
104 u32 port_stat; /* USB_PORT_STAT_xxx */
106 struct completion setup_ack_done;
108 /* see usbhsh_req_alloc/free */
109 struct list_head ureq_link_active;
110 struct list_head ureq_link_free;
114 static const char usbhsh_hcd_name[] = "renesas_usbhs host";
119 #define usbhsh_priv_to_hpriv(priv) \
120 container_of(usbhs_mod_get(priv, USBHS_HOST), struct usbhsh_hpriv, mod)
122 #define __usbhsh_for_each_hpipe(start, pos, h, i) \
123 for (i = start, pos = (h)->hpipe + i; \
124 i < (h)->hpipe_size; \
125 i++, pos = (h)->hpipe + i)
127 #define usbhsh_for_each_hpipe(pos, hpriv, i) \
128 __usbhsh_for_each_hpipe(1, pos, hpriv, i)
130 #define usbhsh_for_each_hpipe_with_dcp(pos, hpriv, i) \
131 __usbhsh_for_each_hpipe(0, pos, hpriv, i)
133 #define __usbhsh_for_each_udev(start, pos, h, i) \
134 for (i = start, pos = (h)->udev + i; \
135 i < USBHSH_DEVICE_MAX; \
136 i++, pos = (h)->udev + i)
138 #define usbhsh_for_each_udev(pos, hpriv, i) \
139 __usbhsh_for_each_udev(1, pos, hpriv, i)
141 #define usbhsh_for_each_udev_with_dev0(pos, hpriv, i) \
142 __usbhsh_for_each_udev(0, pos, hpriv, i)
144 #define usbhsh_hcd_to_hpriv(h) (struct usbhsh_hpriv *)((h)->hcd_priv)
145 #define usbhsh_hcd_to_dev(h) ((h)->self.controller)
147 #define usbhsh_hpriv_to_priv(h) ((h)->mod.priv)
148 #define usbhsh_hpriv_to_dcp(h) ((h)->dcp)
149 #define usbhsh_hpriv_to_hcd(h) \
150 container_of((void *)h, struct usb_hcd, hcd_priv)
152 #define usbhsh_ep_to_uep(u) ((u)->hcpriv)
153 #define usbhsh_uep_to_pipe(u) ((u)->pipe)
154 #define usbhsh_uep_to_udev(u) ((u)->udev)
155 #define usbhsh_urb_to_ureq(u) ((u)->hcpriv)
156 #define usbhsh_urb_to_usbv(u) ((u)->dev)
158 #define usbhsh_usbv_to_udev(d) dev_get_drvdata(&(d)->dev)
160 #define usbhsh_udev_to_usbv(h) ((h)->usbv)
162 #define usbhsh_pipe_info(p) ((p)->mod_private)
164 #define usbhsh_device_number(h, d) ((int)((d) - (h)->udev))
165 #define usbhsh_device_nth(h, d) ((h)->udev + d)
166 #define usbhsh_device0(h) usbhsh_device_nth(h, 0)
168 #define usbhsh_port_stat_init(h) ((h)->port_stat = 0)
169 #define usbhsh_port_stat_set(h, s) ((h)->port_stat |= (s))
170 #define usbhsh_port_stat_clear(h, s) ((h)->port_stat &= ~(s))
171 #define usbhsh_port_stat_get(h) ((h)->port_stat)
173 #define usbhsh_pkt_to_req(p) \
174 container_of((void *)p, struct usbhsh_request, pkt)
179 static void usbhsh_req_list_init(struct usbhsh_hpriv *hpriv)
181 INIT_LIST_HEAD(&hpriv->ureq_link_active);
182 INIT_LIST_HEAD(&hpriv->ureq_link_free);
185 static void usbhsh_req_list_quit(struct usbhsh_hpriv *hpriv)
187 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
188 struct device *dev = usbhsh_hcd_to_dev(hcd);
189 struct usbhsh_request *ureq, *next;
191 /* kfree all active ureq */
192 list_for_each_entry_safe(ureq, next,
193 &hpriv->ureq_link_active,
195 dev_err(dev, "active ureq (%p) is force freed\n", ureq);
199 /* kfree all free ureq */
200 list_for_each_entry_safe(ureq, next, &hpriv->ureq_link_free, ureq_link)
204 static struct usbhsh_request *usbhsh_req_alloc(struct usbhsh_hpriv *hpriv,
208 struct usbhsh_request *ureq;
209 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
210 struct device *dev = usbhs_priv_to_dev(priv);
212 if (list_empty(&hpriv->ureq_link_free)) {
214 * create new one if there is no free ureq
216 ureq = kzalloc(sizeof(struct usbhsh_request), mem_flags);
218 INIT_LIST_HEAD(&ureq->ureq_link);
221 * reuse "free" ureq if exist
223 ureq = list_entry(hpriv->ureq_link_free.next,
224 struct usbhsh_request,
227 list_del_init(&ureq->ureq_link);
231 dev_err(dev, "ureq alloc fail\n");
235 usbhs_pkt_init(&ureq->pkt);
238 * push it to "active" list
240 list_add_tail(&ureq->ureq_link, &hpriv->ureq_link_active);
246 static void usbhsh_req_free(struct usbhsh_hpriv *hpriv,
247 struct usbhsh_request *ureq)
249 struct usbhs_pkt *pkt = &ureq->pkt;
254 * removed from "active" list,
255 * and push it to "free" list
258 list_del_init(&ureq->ureq_link);
259 list_add_tail(&ureq->ureq_link, &hpriv->ureq_link_free);
266 static int usbhsh_device_has_endpoint(struct usbhsh_device *udev)
268 return !list_empty(&udev->ep_list_head);
271 static struct usbhsh_device *usbhsh_device_alloc(struct usbhsh_hpriv *hpriv,
274 struct usbhsh_device *udev = NULL;
275 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
276 struct device *dev = usbhsh_hcd_to_dev(hcd);
277 struct usb_device *usbv = usbhsh_urb_to_usbv(urb);
278 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
284 if (0 == usb_pipedevice(urb->pipe)) {
285 udev = usbhsh_device0(hpriv);
286 goto usbhsh_device_find;
292 usbhsh_for_each_udev(udev, hpriv, i) {
293 if (usbhsh_udev_to_usbv(udev))
295 goto usbhsh_device_find;
298 dev_err(dev, "no free usbhsh_device\n");
303 if (usbhsh_device_has_endpoint(udev))
304 dev_warn(dev, "udev have old endpoint\n");
306 /* uep will be attached */
307 INIT_LIST_HEAD(&udev->ep_list_head);
310 * usbhsh_usbv_to_udev()
311 * usbhsh_udev_to_usbv()
314 dev_set_drvdata(&usbv->dev, udev);
317 /* set device config */
318 usbhs_set_device_speed(priv,
319 usbhsh_device_number(hpriv, udev),
320 usbhsh_device_number(hpriv, udev),
321 0, /* FIXME no parent */
324 dev_dbg(dev, "%s [%d](%p)\n", __func__,
325 usbhsh_device_number(hpriv, udev), udev);
330 static void usbhsh_device_free(struct usbhsh_hpriv *hpriv,
331 struct usbhsh_device *udev)
333 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
334 struct device *dev = usbhsh_hcd_to_dev(hcd);
335 struct usb_device *usbv = usbhsh_udev_to_usbv(udev);
337 dev_dbg(dev, "%s [%d](%p)\n", __func__,
338 usbhsh_device_number(hpriv, udev), udev);
340 if (usbhsh_device_has_endpoint(udev))
341 dev_warn(dev, "udev still have endpoint\n");
344 * usbhsh_usbv_to_udev()
345 * usbhsh_udev_to_usbv()
348 dev_set_drvdata(&usbv->dev, NULL);
355 struct usbhsh_ep *usbhsh_endpoint_alloc(struct usbhsh_hpriv *hpriv,
356 struct usbhsh_device *udev,
357 struct usb_host_endpoint *ep,
360 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
361 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
362 struct usbhsh_ep *uep;
363 struct usbhsh_pipe_info *info;
364 struct usbhs_pipe *pipe, *best_pipe;
365 struct device *dev = usbhsh_hcd_to_dev(hcd);
366 struct usb_endpoint_descriptor *desc = &ep->desc;
368 unsigned int min_usr;
370 uep = kzalloc(sizeof(struct usbhsh_ep), mem_flags);
372 dev_err(dev, "usbhsh_ep alloc fail\n");
375 type = usb_endpoint_type(desc);
378 * find best pipe for endpoint
380 * HARDWARE LIMITATION
384 usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
385 if (!usbhs_pipe_type_is(pipe, type))
388 info = usbhsh_pipe_info(pipe);
390 if (min_usr > info->usr_cnt) {
391 min_usr = info->usr_cnt;
396 if (unlikely(!best_pipe)) {
397 dev_err(dev, "couldn't find best pipe\n");
405 uep->pipe = best_pipe;
406 uep->maxp = usb_endpoint_maxp(desc);
407 usbhsh_uep_to_udev(uep) = udev;
408 usbhsh_ep_to_uep(ep) = uep;
411 * update pipe user count
413 info = usbhsh_pipe_info(best_pipe);
416 /* init this endpoint, and attach it to udev */
417 INIT_LIST_HEAD(&uep->ep_list);
418 list_add_tail(&uep->ep_list, &udev->ep_list_head);
421 * usbhs_pipe_config_update() should be called after
422 * usbhs_device_config()
426 usbhs_pipe_config_update(uep->pipe,
427 usbhsh_device_number(hpriv, udev),
428 usb_endpoint_num(desc),
431 dev_dbg(dev, "%s [%d-%s](%p)\n", __func__,
432 usbhsh_device_number(hpriv, udev),
433 usbhs_pipe_name(pipe), uep);
438 void usbhsh_endpoint_free(struct usbhsh_hpriv *hpriv,
439 struct usb_host_endpoint *ep)
441 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
442 struct device *dev = usbhs_priv_to_dev(priv);
443 struct usbhsh_ep *uep = usbhsh_ep_to_uep(ep);
444 struct usbhsh_pipe_info *info;
449 dev_dbg(dev, "%s [%d-%s](%p)\n", __func__,
450 usbhsh_device_number(hpriv, usbhsh_uep_to_udev(uep)),
451 usbhs_pipe_name(uep->pipe), uep);
453 info = usbhsh_pipe_info(uep->pipe);
456 /* remove this endpoint from udev */
457 list_del_init(&uep->ep_list);
459 usbhsh_uep_to_udev(uep) = NULL;
460 usbhsh_ep_to_uep(ep) = NULL;
468 static void usbhsh_queue_done(struct usbhs_priv *priv, struct usbhs_pkt *pkt)
470 struct usbhsh_request *ureq = usbhsh_pkt_to_req(pkt);
471 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
472 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
473 struct urb *urb = ureq->urb;
474 struct device *dev = usbhs_priv_to_dev(priv);
476 dev_dbg(dev, "%s\n", __func__);
479 dev_warn(dev, "pkt doesn't have urb\n");
483 urb->actual_length = pkt->actual;
484 usbhsh_req_free(hpriv, ureq);
485 usbhsh_urb_to_ureq(urb) = NULL;
487 usb_hcd_unlink_urb_from_ep(hcd, urb);
488 usb_hcd_giveback_urb(hcd, urb, 0);
491 static int usbhsh_queue_push(struct usb_hcd *hcd,
492 struct usbhs_pipe *pipe,
495 struct usbhsh_request *ureq = usbhsh_urb_to_ureq(urb);
496 struct usbhs_pkt *pkt = &ureq->pkt;
497 struct device *dev = usbhsh_hcd_to_dev(hcd);
501 if (usb_pipeisoc(urb->pipe)) {
502 dev_err(dev, "pipe iso is not supported now\n");
506 if (usb_pipein(urb->pipe))
507 pipe->handler = &usbhs_fifo_pio_pop_handler;
509 pipe->handler = &usbhs_fifo_pio_push_handler;
511 buf = (void *)(urb->transfer_buffer + urb->actual_length);
512 len = urb->transfer_buffer_length - urb->actual_length;
514 dev_dbg(dev, "%s\n", __func__);
515 usbhs_pkt_push(pipe, pkt, usbhsh_queue_done,
516 buf, len, (urb->transfer_flags & URB_ZERO_PACKET));
517 usbhs_pkt_start(pipe);
525 static int usbhsh_is_request_address(struct urb *urb)
527 struct usb_ctrlrequest *cmd;
529 cmd = (struct usb_ctrlrequest *)urb->setup_packet;
531 if ((DeviceOutRequest == cmd->bRequestType << 8) &&
532 (USB_REQ_SET_ADDRESS == cmd->bRequest))
538 static void usbhsh_setup_stage_packet_push(struct usbhsh_hpriv *hpriv,
540 struct usbhs_pipe *pipe)
542 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
543 struct usb_ctrlrequest req;
544 struct device *dev = usbhs_priv_to_dev(priv);
547 * wait setup packet ACK
549 * usbhsh_irq_setup_ack()
550 * usbhsh_irq_setup_err()
552 init_completion(&hpriv->setup_ack_done);
554 /* copy original request */
555 memcpy(&req, urb->setup_packet, sizeof(struct usb_ctrlrequest));
558 * renesas_usbhs can not use original usb address.
559 * see HARDWARE LIMITATION.
560 * modify usb address here.
562 if (usbhsh_is_request_address(urb)) {
565 dev_dbg(dev, "create new address - %d\n", req.wValue);
569 usbhs_usbreq_set_val(priv, &req);
572 * wait setup packet ACK
574 wait_for_completion(&hpriv->setup_ack_done);
576 dev_dbg(dev, "%s done\n", __func__);
582 static void usbhsh_data_stage_packet_done(struct usbhs_priv *priv,
583 struct usbhs_pkt *pkt)
585 struct usbhsh_request *ureq = usbhsh_pkt_to_req(pkt);
586 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
587 struct urb *urb = ureq->urb;
589 /* this ureq was connected to urb when usbhsh_urb_enqueue() */
591 usbhsh_req_free(hpriv, ureq);
592 usbhsh_urb_to_ureq(urb) = NULL;
595 static void usbhsh_data_stage_packet_push(struct usbhsh_hpriv *hpriv,
597 struct usbhs_pipe *pipe)
599 struct usbhsh_request *ureq;
600 struct usbhs_pkt *pkt;
605 * data stage uses ureq which is connected to urb
606 * see usbhsh_urb_enqueue() :: alloc new request.
607 * it will be freed in usbhsh_data_stage_packet_done()
609 ureq = usbhsh_urb_to_ureq(urb);
612 if (usb_pipein(urb->pipe))
613 pipe->handler = &usbhs_dcp_data_stage_in_handler;
615 pipe->handler = &usbhs_dcp_data_stage_out_handler;
617 usbhs_pkt_push(pipe, pkt,
618 usbhsh_data_stage_packet_done,
619 urb->transfer_buffer,
620 urb->transfer_buffer_length,
621 (urb->transfer_flags & URB_ZERO_PACKET));
627 static void usbhsh_status_stage_packet_push(struct usbhsh_hpriv *hpriv,
629 struct usbhs_pipe *pipe)
631 struct usbhsh_request *ureq;
632 struct usbhs_pkt *pkt;
637 * status stage uses allocated ureq.
638 * it will be freed on usbhsh_queue_done()
640 ureq = usbhsh_req_alloc(hpriv, urb, GFP_KERNEL);
643 if (usb_pipein(urb->pipe))
644 pipe->handler = &usbhs_dcp_status_stage_in_handler;
646 pipe->handler = &usbhs_dcp_status_stage_out_handler;
648 usbhs_pkt_push(pipe, pkt,
651 urb->transfer_buffer_length,
655 static int usbhsh_dcp_queue_push(struct usb_hcd *hcd,
656 struct usbhsh_hpriv *hpriv,
657 struct usbhs_pipe *pipe,
660 struct device *dev = usbhsh_hcd_to_dev(hcd);
662 dev_dbg(dev, "%s\n", __func__);
667 * usbhsh_send_setup_stage_packet() wait SACK/SIGN
669 usbhsh_setup_stage_packet_push(hpriv, urb, pipe);
674 * It is pushed only when urb has buffer.
676 if (urb->transfer_buffer_length)
677 usbhsh_data_stage_packet_push(hpriv, urb, pipe);
682 usbhsh_status_stage_packet_push(hpriv, urb, pipe);
685 * start pushed packets
687 usbhs_pkt_start(pipe);
695 static int usbhsh_dma_map_ctrl(struct usbhs_pkt *pkt, int map)
703 static int usbhsh_host_start(struct usb_hcd *hcd)
708 static void usbhsh_host_stop(struct usb_hcd *hcd)
712 static int usbhsh_urb_enqueue(struct usb_hcd *hcd,
716 struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
717 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
718 struct device *dev = usbhs_priv_to_dev(priv);
719 struct usb_device *usbv = usbhsh_urb_to_usbv(urb);
720 struct usb_host_endpoint *ep = urb->ep;
721 struct usbhsh_request *ureq;
722 struct usbhsh_device *udev, *new_udev = NULL;
723 struct usbhs_pipe *pipe;
724 struct usbhsh_ep *uep;
728 dev_dbg(dev, "%s (%s)\n",
729 __func__, usb_pipein(urb->pipe) ? "in" : "out");
731 ret = usb_hcd_link_urb_to_ep(hcd, urb);
733 goto usbhsh_urb_enqueue_error_not_linked;
738 udev = usbhsh_usbv_to_udev(usbv);
740 new_udev = usbhsh_device_alloc(hpriv, urb);
742 goto usbhsh_urb_enqueue_error_not_linked;
750 uep = usbhsh_ep_to_uep(ep);
752 uep = usbhsh_endpoint_alloc(hpriv, udev, ep, mem_flags);
754 goto usbhsh_urb_enqueue_error_free_device;
756 pipe = usbhsh_uep_to_pipe(uep);
761 ureq = usbhsh_req_alloc(hpriv, urb, mem_flags);
762 if (unlikely(!ureq)) {
764 goto usbhsh_urb_enqueue_error_free_endpoint;
766 usbhsh_urb_to_ureq(urb) = ureq;
771 if (usb_pipecontrol(urb->pipe))
772 usbhsh_dcp_queue_push(hcd, hpriv, pipe, urb);
774 usbhsh_queue_push(hcd, pipe, urb);
778 usbhsh_urb_enqueue_error_free_endpoint:
779 usbhsh_endpoint_free(hpriv, ep);
780 usbhsh_urb_enqueue_error_free_device:
782 usbhsh_device_free(hpriv, new_udev);
783 usbhsh_urb_enqueue_error_not_linked:
785 dev_dbg(dev, "%s error\n", __func__);
790 static int usbhsh_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
792 struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
793 struct usbhsh_request *ureq = usbhsh_urb_to_ureq(urb);
796 usbhsh_req_free(hpriv, ureq);
797 usbhsh_urb_to_ureq(urb) = NULL;
803 static void usbhsh_endpoint_disable(struct usb_hcd *hcd,
804 struct usb_host_endpoint *ep)
806 struct usbhsh_ep *uep = usbhsh_ep_to_uep(ep);
807 struct usbhsh_device *udev;
808 struct usbhsh_hpriv *hpriv;
811 * this function might be called manytimes by same hcd/ep
812 * in-endpoitn == out-endpoint if ep == dcp.
817 udev = usbhsh_uep_to_udev(uep);
818 hpriv = usbhsh_hcd_to_hpriv(hcd);
820 usbhsh_endpoint_free(hpriv, ep);
824 * if there is no endpoint,
827 if (!usbhsh_device_has_endpoint(udev))
828 usbhsh_device_free(hpriv, udev);
831 static int usbhsh_hub_status_data(struct usb_hcd *hcd, char *buf)
833 struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
834 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
835 struct device *dev = usbhs_priv_to_dev(priv);
836 int roothub_id = 1; /* only 1 root hub */
839 * does port stat was changed ?
840 * check USB_PORT_STAT_C_xxx << 16
842 if (usbhsh_port_stat_get(hpriv) & 0xFFFF0000)
843 *buf = (1 << roothub_id);
847 dev_dbg(dev, "%s (%02x)\n", __func__, *buf);
852 static int __usbhsh_hub_hub_feature(struct usbhsh_hpriv *hpriv,
853 u16 typeReq, u16 wValue,
854 u16 wIndex, char *buf, u16 wLength)
856 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
857 struct device *dev = usbhs_priv_to_dev(priv);
860 case C_HUB_OVER_CURRENT:
861 case C_HUB_LOCAL_POWER:
862 dev_dbg(dev, "%s :: C_HUB_xx\n", __func__);
869 static int __usbhsh_hub_port_feature(struct usbhsh_hpriv *hpriv,
870 u16 typeReq, u16 wValue,
871 u16 wIndex, char *buf, u16 wLength)
873 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
874 struct device *dev = usbhs_priv_to_dev(priv);
875 int enable = (typeReq == SetPortFeature);
876 int speed, i, timeout = 128;
877 int roothub_id = 1; /* only 1 root hub */
880 if (wIndex > roothub_id || wLength != 0)
885 case USB_PORT_FEAT_POWER:
886 usbhs_vbus_ctrl(priv, enable);
887 dev_dbg(dev, "%s :: USB_PORT_FEAT_POWER\n", __func__);
890 case USB_PORT_FEAT_ENABLE:
891 case USB_PORT_FEAT_SUSPEND:
892 case USB_PORT_FEAT_C_ENABLE:
893 case USB_PORT_FEAT_C_SUSPEND:
894 case USB_PORT_FEAT_C_CONNECTION:
895 case USB_PORT_FEAT_C_OVER_CURRENT:
896 case USB_PORT_FEAT_C_RESET:
897 dev_dbg(dev, "%s :: USB_PORT_FEAT_xxx\n", __func__);
900 case USB_PORT_FEAT_RESET:
904 usbhsh_port_stat_clear(hpriv,
905 USB_PORT_STAT_HIGH_SPEED |
906 USB_PORT_STAT_LOW_SPEED);
908 usbhs_bus_send_reset(priv);
910 usbhs_bus_send_sof_enable(priv);
912 for (i = 0; i < timeout ; i++) {
913 switch (usbhs_bus_get_speed(priv)) {
915 speed = USB_PORT_STAT_LOW_SPEED;
916 goto got_usb_bus_speed;
918 speed = USB_PORT_STAT_HIGH_SPEED;
919 goto got_usb_bus_speed;
922 goto got_usb_bus_speed;
930 usbhsh_port_stat_set(hpriv, speed);
931 usbhsh_port_stat_set(hpriv, USB_PORT_STAT_ENABLE);
933 dev_dbg(dev, "%s :: USB_PORT_FEAT_RESET (speed = %d)\n",
936 /* status change is not needed */
943 /* set/clear status */
945 usbhsh_port_stat_set(hpriv, (1 << wValue));
947 usbhsh_port_stat_clear(hpriv, (1 << wValue));
952 static int __usbhsh_hub_get_status(struct usbhsh_hpriv *hpriv,
953 u16 typeReq, u16 wValue,
954 u16 wIndex, char *buf, u16 wLength)
956 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
957 struct usb_hub_descriptor *desc = (struct usb_hub_descriptor *)buf;
958 struct device *dev = usbhs_priv_to_dev(priv);
959 int roothub_id = 1; /* only 1 root hub */
963 dev_dbg(dev, "%s :: GetHubStatus\n", __func__);
969 if (wIndex != roothub_id)
972 dev_dbg(dev, "%s :: GetPortStatus\n", __func__);
973 *(__le32 *)buf = cpu_to_le32(usbhsh_port_stat_get(hpriv));
976 case GetHubDescriptor:
977 desc->bDescriptorType = 0x29;
978 desc->bHubContrCurrent = 0;
979 desc->bNbrPorts = roothub_id;
980 desc->bDescLength = 9;
981 desc->bPwrOn2PwrGood = 0;
982 desc->wHubCharacteristics = cpu_to_le16(0x0011);
983 desc->u.hs.DeviceRemovable[0] = (roothub_id << 1);
984 desc->u.hs.DeviceRemovable[1] = ~0;
985 dev_dbg(dev, "%s :: GetHubDescriptor\n", __func__);
992 static int usbhsh_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
993 u16 wIndex, char *buf, u16 wLength)
995 struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
996 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
997 struct device *dev = usbhs_priv_to_dev(priv);
1003 case ClearHubFeature:
1005 ret = __usbhsh_hub_hub_feature(hpriv, typeReq,
1006 wValue, wIndex, buf, wLength);
1010 case SetPortFeature:
1011 case ClearPortFeature:
1012 ret = __usbhsh_hub_port_feature(hpriv, typeReq,
1013 wValue, wIndex, buf, wLength);
1019 case GetHubDescriptor:
1020 ret = __usbhsh_hub_get_status(hpriv, typeReq,
1021 wValue, wIndex, buf, wLength);
1025 dev_dbg(dev, "typeReq = %x, ret = %d, port_stat = %x\n",
1026 typeReq, ret, usbhsh_port_stat_get(hpriv));
1031 static struct hc_driver usbhsh_driver = {
1032 .description = usbhsh_hcd_name,
1033 .hcd_priv_size = sizeof(struct usbhsh_hpriv),
1036 * generic hardware linkage
1040 .start = usbhsh_host_start,
1041 .stop = usbhsh_host_stop,
1044 * managing i/o requests and associated device resources
1046 .urb_enqueue = usbhsh_urb_enqueue,
1047 .urb_dequeue = usbhsh_urb_dequeue,
1048 .endpoint_disable = usbhsh_endpoint_disable,
1053 .hub_status_data = usbhsh_hub_status_data,
1054 .hub_control = usbhsh_hub_control,
1058 * interrupt functions
1060 static int usbhsh_irq_attch(struct usbhs_priv *priv,
1061 struct usbhs_irq_state *irq_state)
1063 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1064 struct device *dev = usbhs_priv_to_dev(priv);
1066 dev_dbg(dev, "device attached\n");
1068 usbhsh_port_stat_set(hpriv, USB_PORT_STAT_CONNECTION);
1069 usbhsh_port_stat_set(hpriv, USB_PORT_STAT_C_CONNECTION << 16);
1074 static int usbhsh_irq_dtch(struct usbhs_priv *priv,
1075 struct usbhs_irq_state *irq_state)
1077 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1078 struct device *dev = usbhs_priv_to_dev(priv);
1080 dev_dbg(dev, "device detached\n");
1082 usbhsh_port_stat_clear(hpriv, USB_PORT_STAT_CONNECTION);
1083 usbhsh_port_stat_set(hpriv, USB_PORT_STAT_C_CONNECTION << 16);
1088 static int usbhsh_irq_setup_ack(struct usbhs_priv *priv,
1089 struct usbhs_irq_state *irq_state)
1091 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1092 struct device *dev = usbhs_priv_to_dev(priv);
1094 dev_dbg(dev, "setup packet OK\n");
1096 complete(&hpriv->setup_ack_done); /* see usbhsh_urb_enqueue() */
1101 static int usbhsh_irq_setup_err(struct usbhs_priv *priv,
1102 struct usbhs_irq_state *irq_state)
1104 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1105 struct device *dev = usbhs_priv_to_dev(priv);
1107 dev_dbg(dev, "setup packet Err\n");
1109 complete(&hpriv->setup_ack_done); /* see usbhsh_urb_enqueue() */
1117 static void usbhsh_pipe_init_for_host(struct usbhs_priv *priv)
1119 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1120 struct usbhsh_pipe_info *pipe_info = hpriv->pipe_info;
1121 struct usbhs_pipe *pipe;
1122 u32 *pipe_type = usbhs_get_dparam(priv, pipe_type);
1123 int pipe_size = usbhs_get_dparam(priv, pipe_size);
1124 int old_type, dir_in, i;
1127 old_type = USB_ENDPOINT_XFER_CONTROL;
1128 for (i = 0; i < pipe_size; i++) {
1129 pipe_info[i].usr_cnt = 0;
1132 * data "output" will be finished as soon as possible,
1133 * but there is no guaranty at data "input" case.
1135 * "input" needs "standby" pipe.
1136 * So, "input" direction pipe > "output" direction pipe
1139 * 1st USB_ENDPOINT_XFER_xxx will be output direction,
1140 * and the other will be input direction here.
1144 * USB_ENDPOINT_XFER_ISOC -> dir out
1145 * USB_ENDPOINT_XFER_ISOC -> dir in
1146 * USB_ENDPOINT_XFER_BULK -> dir out
1147 * USB_ENDPOINT_XFER_BULK -> dir in
1148 * USB_ENDPOINT_XFER_BULK -> dir in
1151 dir_in = (pipe_type[i] == old_type);
1152 old_type = pipe_type[i];
1154 if (USB_ENDPOINT_XFER_CONTROL == pipe_type[i]) {
1155 pipe = usbhs_dcp_malloc(priv);
1156 usbhsh_hpriv_to_dcp(hpriv) = pipe;
1158 pipe = usbhs_pipe_malloc(priv,
1163 pipe->mod_private = pipe_info + i;
1167 static int usbhsh_start(struct usbhs_priv *priv)
1169 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1170 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
1171 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
1172 struct device *dev = usbhs_priv_to_dev(priv);
1176 ret = usb_add_hcd(hcd, 0, 0);
1181 * pipe initialize and enable DCP
1183 usbhs_pipe_init(priv,
1184 usbhsh_dma_map_ctrl);
1185 usbhs_fifo_init(priv);
1186 usbhsh_pipe_init_for_host(priv);
1189 * system config enble
1194 usbhs_sys_hispeed_ctrl(priv, 1);
1195 usbhs_sys_host_ctrl(priv, 1);
1196 usbhs_sys_usb_ctrl(priv, 1);
1199 * enable irq callback
1201 mod->irq_attch = usbhsh_irq_attch;
1202 mod->irq_dtch = usbhsh_irq_dtch;
1203 mod->irq_sack = usbhsh_irq_setup_ack;
1204 mod->irq_sign = usbhsh_irq_setup_err;
1205 usbhs_irq_callback_update(priv, mod);
1207 dev_dbg(dev, "start host\n");
1212 static int usbhsh_stop(struct usbhs_priv *priv)
1214 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1215 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
1216 struct device *dev = usbhs_priv_to_dev(priv);
1218 usb_remove_hcd(hcd);
1221 usbhs_sys_hispeed_ctrl(priv, 0);
1222 usbhs_sys_host_ctrl(priv, 0);
1223 usbhs_sys_usb_ctrl(priv, 0);
1225 dev_dbg(dev, "quit host\n");
1230 int __devinit usbhs_mod_host_probe(struct usbhs_priv *priv)
1232 struct usbhsh_hpriv *hpriv;
1233 struct usb_hcd *hcd;
1234 struct usbhsh_pipe_info *pipe_info;
1235 struct usbhsh_device *udev;
1236 struct device *dev = usbhs_priv_to_dev(priv);
1237 int pipe_size = usbhs_get_dparam(priv, pipe_size);
1240 /* initialize hcd */
1241 hcd = usb_create_hcd(&usbhsh_driver, dev, usbhsh_hcd_name);
1243 dev_err(dev, "Failed to create hcd\n");
1247 pipe_info = kzalloc(sizeof(*pipe_info) * pipe_size, GFP_KERNEL);
1249 dev_err(dev, "Could not allocate pipe_info\n");
1250 goto usbhs_mod_host_probe_err;
1256 * There is no guarantee that it is possible to access usb module here.
1257 * Don't accesses to it.
1258 * The accesse will be enable after "usbhsh_start"
1261 hpriv = usbhsh_hcd_to_hpriv(hcd);
1266 usbhs_mod_register(priv, &hpriv->mod, USBHS_HOST);
1269 hpriv->mod.name = "host";
1270 hpriv->mod.start = usbhsh_start;
1271 hpriv->mod.stop = usbhsh_stop;
1272 hpriv->pipe_info = pipe_info;
1273 hpriv->pipe_size = pipe_size;
1274 usbhsh_req_list_init(hpriv);
1275 usbhsh_port_stat_init(hpriv);
1277 /* init all device */
1278 usbhsh_for_each_udev_with_dev0(udev, hpriv, i) {
1280 INIT_LIST_HEAD(&udev->ep_list_head);
1283 dev_info(dev, "host probed\n");
1287 usbhs_mod_host_probe_err:
1293 int __devexit usbhs_mod_host_remove(struct usbhs_priv *priv)
1295 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1296 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
1298 usbhsh_req_list_quit(hpriv);