xref: /linux/drivers/usb/renesas_usbhs/mod_host.c (revision 5fd54ace4721fc5ce2bb5aef6318fcf17f421460)
1 // SPDX-License-Identifier: GPL-1.0+
2 /*
3  * Renesas USB driver
4  *
5  * Copyright (C) 2011 Renesas Solutions Corp.
6  * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
16  *
17  */
18 #include <linux/io.h>
19 #include <linux/list.h>
20 #include <linux/module.h>
21 #include <linux/platform_device.h>
22 #include <linux/slab.h>
23 #include <linux/usb.h>
24 #include <linux/usb/hcd.h>
25 #include "common.h"
26 
27 /*
28  *** HARDWARE LIMITATION ***
29  *
30  * 1) renesas_usbhs has a limited number of controllable devices.
31  *    it can control only 9 devices in generally.
32  *	see DEVADDn / DCPMAXP / PIPEMAXP.
33  *
34  * 2) renesas_usbhs pipe number is limited.
35  *    the pipe will be re-used for each devices.
36  *    so, software should control DATA0/1 sequence of each devices.
37  */
38 
39 
40 /*
41  *		image of mod_host
42  *
43  * +--------+
44  * | udev 0 | --> it is used when set address
45  * +--------+
46  *
47  * +--------+					pipes are reused for each uep.
48  * | udev 1 |-+- [uep 0 (dcp) ] --+		pipe will be switched when
49  * +--------+ |			  |		other device requested
50  *	      +- [uep 1 (bulk)]	--|---+		   +--------------+
51  *	      |			  +--------------> | pipe0 (dcp)  |
52  *	      +- [uep 2 (bulk)]	-@    |		   +--------------+
53  *				      |		   | pipe1 (isoc) |
54  * +--------+			      |		   +--------------+
55  * | udev 2 |-+- [uep 0 (dcp) ]	-@    +----------> | pipe2 (bulk) |
56  * +--------+ |					   +--------------+
57  *	      +- [uep 1 (int) ]	----+	  +------> | pipe3 (bulk) |
58  *				    |	  |	   +--------------+
59  * +--------+			    +-----|------> | pipe4 (int)  |
60  * | udev 3 |-+- [uep 0 (dcp) ]	-@	  |	   +--------------+
61  * +--------+ |				  |	   | ....	  |
62  *	      +- [uep 1 (bulk)]	-@	  |	   | ....	  |
63  *	      |				  |
64  *	      +- [uep 2 (bulk)]-----------+
65  *
66  * @ :	uep requested free pipe, but all have been used.
67  *	now it is waiting for free pipe
68  */
69 
70 
71 /*
72  *		struct
73  */
74 struct usbhsh_request {
75 	struct urb		*urb;
76 	struct usbhs_pkt	pkt;
77 };
78 
79 struct usbhsh_device {
80 	struct usb_device	*usbv;
81 	struct list_head	ep_list_head; /* list of usbhsh_ep */
82 };
83 
84 struct usbhsh_ep {
85 	struct usbhs_pipe	*pipe;   /* attached pipe */
86 	struct usbhsh_device	*udev;   /* attached udev */
87 	struct usb_host_endpoint *ep;
88 	struct list_head	ep_list; /* list to usbhsh_device */
89 	unsigned int		counter; /* pipe attach counter */
90 };
91 
92 #define USBHSH_DEVICE_MAX	10 /* see DEVADDn / DCPMAXP / PIPEMAXP */
93 #define USBHSH_PORT_MAX		 7 /* see DEVADDn :: HUBPORT */
94 struct usbhsh_hpriv {
95 	struct usbhs_mod	mod;
96 	struct usbhs_pipe	*dcp;
97 
98 	struct usbhsh_device	udev[USBHSH_DEVICE_MAX];
99 
100 	u32	port_stat;	/* USB_PORT_STAT_xxx */
101 
102 	struct completion	setup_ack_done;
103 };
104 
105 
106 static const char usbhsh_hcd_name[] = "renesas_usbhs host";
107 
108 /*
109  *		macro
110  */
111 #define usbhsh_priv_to_hpriv(priv) \
112 	container_of(usbhs_mod_get(priv, USBHS_HOST), struct usbhsh_hpriv, mod)
113 
114 #define __usbhsh_for_each_udev(start, pos, h, i)	\
115 	for ((i) = start;						\
116 	     ((i) < USBHSH_DEVICE_MAX) && ((pos) = (h)->udev + (i));	\
117 	     (i)++)
118 
119 #define usbhsh_for_each_udev(pos, hpriv, i)	\
120 	__usbhsh_for_each_udev(1, pos, hpriv, i)
121 
122 #define usbhsh_for_each_udev_with_dev0(pos, hpriv, i)	\
123 	__usbhsh_for_each_udev(0, pos, hpriv, i)
124 
125 #define usbhsh_hcd_to_hpriv(h)	(struct usbhsh_hpriv *)((h)->hcd_priv)
126 #define usbhsh_hcd_to_dev(h)	((h)->self.controller)
127 
128 #define usbhsh_hpriv_to_priv(h)	((h)->mod.priv)
129 #define usbhsh_hpriv_to_dcp(h)	((h)->dcp)
130 #define usbhsh_hpriv_to_hcd(h)	\
131 	container_of((void *)h, struct usb_hcd, hcd_priv)
132 
133 #define usbhsh_ep_to_uep(u)	((u)->hcpriv)
134 #define usbhsh_uep_to_pipe(u)	((u)->pipe)
135 #define usbhsh_uep_to_udev(u)	((u)->udev)
136 #define usbhsh_uep_to_ep(u)	((u)->ep)
137 
138 #define usbhsh_urb_to_ureq(u)	((u)->hcpriv)
139 #define usbhsh_urb_to_usbv(u)	((u)->dev)
140 
141 #define usbhsh_usbv_to_udev(d)	dev_get_drvdata(&(d)->dev)
142 
143 #define usbhsh_udev_to_usbv(h)	((h)->usbv)
144 #define usbhsh_udev_is_used(h)	usbhsh_udev_to_usbv(h)
145 
146 #define usbhsh_pipe_to_uep(p)	((p)->mod_private)
147 
148 #define usbhsh_device_parent(d)		(usbhsh_usbv_to_udev((d)->usbv->parent))
149 #define usbhsh_device_hubport(d)	((d)->usbv->portnum)
150 #define usbhsh_device_number(h, d)	((int)((d) - (h)->udev))
151 #define usbhsh_device_nth(h, d)		((h)->udev + d)
152 #define usbhsh_device0(h)		usbhsh_device_nth(h, 0)
153 
154 #define usbhsh_port_stat_init(h)	((h)->port_stat = 0)
155 #define usbhsh_port_stat_set(h, s)	((h)->port_stat |= (s))
156 #define usbhsh_port_stat_clear(h, s)	((h)->port_stat &= ~(s))
157 #define usbhsh_port_stat_get(h)		((h)->port_stat)
158 
159 #define usbhsh_pkt_to_ureq(p)	\
160 	container_of((void *)p, struct usbhsh_request, pkt)
161 
162 /*
163  *		req alloc/free
164  */
165 static struct usbhsh_request *usbhsh_ureq_alloc(struct usbhsh_hpriv *hpriv,
166 					       struct urb *urb,
167 					       gfp_t mem_flags)
168 {
169 	struct usbhsh_request *ureq;
170 
171 	ureq = kzalloc(sizeof(struct usbhsh_request), mem_flags);
172 	if (!ureq)
173 		return NULL;
174 
175 	usbhs_pkt_init(&ureq->pkt);
176 	ureq->urb = urb;
177 	usbhsh_urb_to_ureq(urb) = ureq;
178 
179 	return ureq;
180 }
181 
182 static void usbhsh_ureq_free(struct usbhsh_hpriv *hpriv,
183 			    struct usbhsh_request *ureq)
184 {
185 	usbhsh_urb_to_ureq(ureq->urb) = NULL;
186 	ureq->urb = NULL;
187 
188 	kfree(ureq);
189 }
190 
191 /*
192  *		status
193  */
194 static int usbhsh_is_running(struct usbhsh_hpriv *hpriv)
195 {
196 	/*
197 	 * we can decide some device is attached or not
198 	 * by checking mod.irq_attch
199 	 * see
200 	 *	usbhsh_irq_attch()
201 	 *	usbhsh_irq_dtch()
202 	 */
203 	return (hpriv->mod.irq_attch == NULL);
204 }
205 
206 /*
207  *		pipe control
208  */
209 static void usbhsh_endpoint_sequence_save(struct usbhsh_hpriv *hpriv,
210 					  struct urb *urb,
211 					  struct usbhs_pkt *pkt)
212 {
213 	int len = urb->actual_length;
214 	int maxp = usb_endpoint_maxp(&urb->ep->desc);
215 	int t = 0;
216 
217 	/* DCP is out of sequence control */
218 	if (usb_pipecontrol(urb->pipe))
219 		return;
220 
221 	/*
222 	 * renesas_usbhs pipe has a limitation in a number.
223 	 * So, driver should re-use the limited pipe for each device/endpoint.
224 	 * DATA0/1 sequence should be saved for it.
225 	 * see [image of mod_host]
226 	 *     [HARDWARE LIMITATION]
227 	 */
228 
229 	/*
230 	 * next sequence depends on actual_length
231 	 *
232 	 * ex) actual_length = 1147, maxp = 512
233 	 * data0 : 512
234 	 * data1 : 512
235 	 * data0 : 123
236 	 * data1 is the next sequence
237 	 */
238 	t = len / maxp;
239 	if (len % maxp)
240 		t++;
241 	if (pkt->zero)
242 		t++;
243 	t %= 2;
244 
245 	if (t)
246 		usb_dotoggle(urb->dev,
247 			     usb_pipeendpoint(urb->pipe),
248 			     usb_pipeout(urb->pipe));
249 }
250 
251 static struct usbhsh_device *usbhsh_device_get(struct usbhsh_hpriv *hpriv,
252 					       struct urb *urb);
253 
254 static int usbhsh_pipe_attach(struct usbhsh_hpriv *hpriv,
255 			      struct urb *urb)
256 {
257 	struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
258 	struct usbhsh_ep *uep = usbhsh_ep_to_uep(urb->ep);
259 	struct usbhsh_device *udev = usbhsh_device_get(hpriv, urb);
260 	struct usbhs_pipe *pipe;
261 	struct usb_endpoint_descriptor *desc = &urb->ep->desc;
262 	struct device *dev = usbhs_priv_to_dev(priv);
263 	unsigned long flags;
264 	int dir_in_req = !!usb_pipein(urb->pipe);
265 	int is_dcp = usb_endpoint_xfer_control(desc);
266 	int i, dir_in;
267 	int ret = -EBUSY;
268 
269 	/********************  spin lock ********************/
270 	usbhs_lock(priv, flags);
271 
272 	/*
273 	 * if uep has been attached to pipe,
274 	 * reuse it
275 	 */
276 	if (usbhsh_uep_to_pipe(uep)) {
277 		ret = 0;
278 		goto usbhsh_pipe_attach_done;
279 	}
280 
281 	usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
282 
283 		/* check pipe type */
284 		if (!usbhs_pipe_type_is(pipe, usb_endpoint_type(desc)))
285 			continue;
286 
287 		/* check pipe direction if normal pipe */
288 		if (!is_dcp) {
289 			dir_in = !!usbhs_pipe_is_dir_in(pipe);
290 			if (0 != (dir_in - dir_in_req))
291 				continue;
292 		}
293 
294 		/* check pipe is free */
295 		if (usbhsh_pipe_to_uep(pipe))
296 			continue;
297 
298 		/*
299 		 * attach pipe to uep
300 		 *
301 		 * usbhs_pipe_config_update() should be called after
302 		 * usbhs_set_device_config()
303 		 * see
304 		 *  DCPMAXP/PIPEMAXP
305 		 */
306 		usbhsh_uep_to_pipe(uep)		= pipe;
307 		usbhsh_pipe_to_uep(pipe)	= uep;
308 
309 		usbhs_pipe_config_update(pipe,
310 					 usbhsh_device_number(hpriv, udev),
311 					 usb_endpoint_num(desc),
312 					 usb_endpoint_maxp(desc));
313 
314 		dev_dbg(dev, "%s [%d-%d(%s:%s)]\n", __func__,
315 			usbhsh_device_number(hpriv, udev),
316 			usb_endpoint_num(desc),
317 			usbhs_pipe_name(pipe),
318 			dir_in_req ? "in" : "out");
319 
320 		ret = 0;
321 		break;
322 	}
323 
324 usbhsh_pipe_attach_done:
325 	if (0 == ret)
326 		uep->counter++;
327 
328 	usbhs_unlock(priv, flags);
329 	/********************  spin unlock ******************/
330 
331 	return ret;
332 }
333 
334 static void usbhsh_pipe_detach(struct usbhsh_hpriv *hpriv,
335 			       struct usbhsh_ep *uep)
336 {
337 	struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
338 	struct usbhs_pipe *pipe;
339 	struct device *dev = usbhs_priv_to_dev(priv);
340 	unsigned long flags;
341 
342 	if (unlikely(!uep)) {
343 		dev_err(dev, "no uep\n");
344 		return;
345 	}
346 
347 	/********************  spin lock ********************/
348 	usbhs_lock(priv, flags);
349 
350 	pipe = usbhsh_uep_to_pipe(uep);
351 
352 	if (unlikely(!pipe)) {
353 		dev_err(dev, "uep doens't have pipe\n");
354 	} else if (1 == uep->counter--) { /* last user */
355 		struct usb_host_endpoint *ep = usbhsh_uep_to_ep(uep);
356 		struct usbhsh_device *udev = usbhsh_uep_to_udev(uep);
357 
358 		/* detach pipe from uep */
359 		usbhsh_uep_to_pipe(uep)		= NULL;
360 		usbhsh_pipe_to_uep(pipe)	= NULL;
361 
362 		dev_dbg(dev, "%s [%d-%d(%s)]\n", __func__,
363 			usbhsh_device_number(hpriv, udev),
364 			usb_endpoint_num(&ep->desc),
365 			usbhs_pipe_name(pipe));
366 	}
367 
368 	usbhs_unlock(priv, flags);
369 	/********************  spin unlock ******************/
370 }
371 
372 /*
373  *		endpoint control
374  */
375 static int usbhsh_endpoint_attach(struct usbhsh_hpriv *hpriv,
376 				  struct urb *urb,
377 				  gfp_t mem_flags)
378 {
379 	struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
380 	struct usbhsh_device *udev = usbhsh_device_get(hpriv, urb);
381 	struct usb_host_endpoint *ep = urb->ep;
382 	struct usbhsh_ep *uep;
383 	struct device *dev = usbhs_priv_to_dev(priv);
384 	struct usb_endpoint_descriptor *desc = &ep->desc;
385 	unsigned long flags;
386 
387 	uep = kzalloc(sizeof(struct usbhsh_ep), mem_flags);
388 	if (!uep)
389 		return -ENOMEM;
390 
391 	/********************  spin lock ********************/
392 	usbhs_lock(priv, flags);
393 
394 	/*
395 	 * init endpoint
396 	 */
397 	uep->counter = 0;
398 	INIT_LIST_HEAD(&uep->ep_list);
399 	list_add_tail(&uep->ep_list, &udev->ep_list_head);
400 
401 	usbhsh_uep_to_udev(uep)	= udev;
402 	usbhsh_uep_to_ep(uep)	= ep;
403 	usbhsh_ep_to_uep(ep)	= uep;
404 
405 	usbhs_unlock(priv, flags);
406 	/********************  spin unlock ******************/
407 
408 	dev_dbg(dev, "%s [%d-%d]\n", __func__,
409 		usbhsh_device_number(hpriv, udev),
410 		usb_endpoint_num(desc));
411 
412 	return 0;
413 }
414 
415 static void usbhsh_endpoint_detach(struct usbhsh_hpriv *hpriv,
416 				   struct usb_host_endpoint *ep)
417 {
418 	struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
419 	struct device *dev = usbhs_priv_to_dev(priv);
420 	struct usbhsh_ep *uep = usbhsh_ep_to_uep(ep);
421 	unsigned long flags;
422 
423 	if (!uep)
424 		return;
425 
426 	dev_dbg(dev, "%s [%d-%d]\n", __func__,
427 		usbhsh_device_number(hpriv, usbhsh_uep_to_udev(uep)),
428 		usb_endpoint_num(&ep->desc));
429 
430 	if (usbhsh_uep_to_pipe(uep))
431 		usbhsh_pipe_detach(hpriv, uep);
432 
433 	/********************  spin lock ********************/
434 	usbhs_lock(priv, flags);
435 
436 	/* remove this endpoint from udev */
437 	list_del_init(&uep->ep_list);
438 
439 	usbhsh_uep_to_udev(uep)	= NULL;
440 	usbhsh_uep_to_ep(uep)	= NULL;
441 	usbhsh_ep_to_uep(ep)	= NULL;
442 
443 	usbhs_unlock(priv, flags);
444 	/********************  spin unlock ******************/
445 
446 	kfree(uep);
447 }
448 
449 static void usbhsh_endpoint_detach_all(struct usbhsh_hpriv *hpriv,
450 				       struct usbhsh_device *udev)
451 {
452 	struct usbhsh_ep *uep, *next;
453 
454 	list_for_each_entry_safe(uep, next, &udev->ep_list_head, ep_list)
455 		usbhsh_endpoint_detach(hpriv, usbhsh_uep_to_ep(uep));
456 }
457 
458 /*
459  *		device control
460  */
461 static int usbhsh_connected_to_rhdev(struct usb_hcd *hcd,
462 				     struct usbhsh_device *udev)
463 {
464 	struct usb_device *usbv = usbhsh_udev_to_usbv(udev);
465 
466 	return hcd->self.root_hub == usbv->parent;
467 }
468 
469 static int usbhsh_device_has_endpoint(struct usbhsh_device *udev)
470 {
471 	return !list_empty(&udev->ep_list_head);
472 }
473 
474 static struct usbhsh_device *usbhsh_device_get(struct usbhsh_hpriv *hpriv,
475 					       struct urb *urb)
476 {
477 	struct usb_device *usbv = usbhsh_urb_to_usbv(urb);
478 	struct usbhsh_device *udev = usbhsh_usbv_to_udev(usbv);
479 
480 	/* usbhsh_device_attach() is still not called */
481 	if (!udev)
482 		return NULL;
483 
484 	/* if it is device0, return it */
485 	if (0 == usb_pipedevice(urb->pipe))
486 		return usbhsh_device0(hpriv);
487 
488 	/* return attached device */
489 	return udev;
490 }
491 
492 static struct usbhsh_device *usbhsh_device_attach(struct usbhsh_hpriv *hpriv,
493 						 struct urb *urb)
494 {
495 	struct usbhsh_device *udev = NULL;
496 	struct usbhsh_device *udev0 = usbhsh_device0(hpriv);
497 	struct usbhsh_device *pos;
498 	struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
499 	struct device *dev = usbhsh_hcd_to_dev(hcd);
500 	struct usb_device *usbv = usbhsh_urb_to_usbv(urb);
501 	struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
502 	unsigned long flags;
503 	u16 upphub, hubport;
504 	int i;
505 
506 	/*
507 	 * This function should be called only while urb is pointing to device0.
508 	 * It will attach unused usbhsh_device to urb (usbv),
509 	 * and initialize device0.
510 	 * You can use usbhsh_device_get() to get "current" udev,
511 	 * and usbhsh_usbv_to_udev() is for "attached" udev.
512 	 */
513 	if (0 != usb_pipedevice(urb->pipe)) {
514 		dev_err(dev, "%s fail: urb isn't pointing device0\n", __func__);
515 		return NULL;
516 	}
517 
518 	/********************  spin lock ********************/
519 	usbhs_lock(priv, flags);
520 
521 	/*
522 	 * find unused device
523 	 */
524 	usbhsh_for_each_udev(pos, hpriv, i) {
525 		if (usbhsh_udev_is_used(pos))
526 			continue;
527 		udev = pos;
528 		break;
529 	}
530 
531 	if (udev) {
532 		/*
533 		 * usbhsh_usbv_to_udev()
534 		 * usbhsh_udev_to_usbv()
535 		 * will be enable
536 		 */
537 		dev_set_drvdata(&usbv->dev, udev);
538 		udev->usbv = usbv;
539 	}
540 
541 	usbhs_unlock(priv, flags);
542 	/********************  spin unlock ******************/
543 
544 	if (!udev) {
545 		dev_err(dev, "no free usbhsh_device\n");
546 		return NULL;
547 	}
548 
549 	if (usbhsh_device_has_endpoint(udev)) {
550 		dev_warn(dev, "udev have old endpoint\n");
551 		usbhsh_endpoint_detach_all(hpriv, udev);
552 	}
553 
554 	if (usbhsh_device_has_endpoint(udev0)) {
555 		dev_warn(dev, "udev0 have old endpoint\n");
556 		usbhsh_endpoint_detach_all(hpriv, udev0);
557 	}
558 
559 	/* uep will be attached */
560 	INIT_LIST_HEAD(&udev0->ep_list_head);
561 	INIT_LIST_HEAD(&udev->ep_list_head);
562 
563 	/*
564 	 * set device0 config
565 	 */
566 	usbhs_set_device_config(priv,
567 				0, 0, 0, usbv->speed);
568 
569 	/*
570 	 * set new device config
571 	 */
572 	upphub	= 0;
573 	hubport	= 0;
574 	if (!usbhsh_connected_to_rhdev(hcd, udev)) {
575 		/* if udev is not connected to rhdev, it means parent is Hub */
576 		struct usbhsh_device *parent = usbhsh_device_parent(udev);
577 
578 		upphub	= usbhsh_device_number(hpriv, parent);
579 		hubport	= usbhsh_device_hubport(udev);
580 
581 		dev_dbg(dev, "%s connected to Hub [%d:%d](%p)\n", __func__,
582 			upphub, hubport, parent);
583 	}
584 
585 	usbhs_set_device_config(priv,
586 			       usbhsh_device_number(hpriv, udev),
587 			       upphub, hubport, usbv->speed);
588 
589 	dev_dbg(dev, "%s [%d](%p)\n", __func__,
590 		usbhsh_device_number(hpriv, udev), udev);
591 
592 	return udev;
593 }
594 
595 static void usbhsh_device_detach(struct usbhsh_hpriv *hpriv,
596 			       struct usbhsh_device *udev)
597 {
598 	struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
599 	struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
600 	struct device *dev = usbhsh_hcd_to_dev(hcd);
601 	struct usb_device *usbv = usbhsh_udev_to_usbv(udev);
602 	unsigned long flags;
603 
604 	dev_dbg(dev, "%s [%d](%p)\n", __func__,
605 		usbhsh_device_number(hpriv, udev), udev);
606 
607 	if (usbhsh_device_has_endpoint(udev)) {
608 		dev_warn(dev, "udev still have endpoint\n");
609 		usbhsh_endpoint_detach_all(hpriv, udev);
610 	}
611 
612 	/*
613 	 * There is nothing to do if it is device0.
614 	 * see
615 	 *  usbhsh_device_attach()
616 	 *  usbhsh_device_get()
617 	 */
618 	if (0 == usbhsh_device_number(hpriv, udev))
619 		return;
620 
621 	/********************  spin lock ********************/
622 	usbhs_lock(priv, flags);
623 
624 	/*
625 	 * usbhsh_usbv_to_udev()
626 	 * usbhsh_udev_to_usbv()
627 	 * will be disable
628 	 */
629 	dev_set_drvdata(&usbv->dev, NULL);
630 	udev->usbv = NULL;
631 
632 	usbhs_unlock(priv, flags);
633 	/********************  spin unlock ******************/
634 }
635 
636 /*
637  *		queue push/pop
638  */
639 static void usbhsh_queue_done(struct usbhs_priv *priv, struct usbhs_pkt *pkt)
640 {
641 	struct usbhsh_request *ureq = usbhsh_pkt_to_ureq(pkt);
642 	struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
643 	struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
644 	struct urb *urb = ureq->urb;
645 	struct device *dev = usbhs_priv_to_dev(priv);
646 	int status = 0;
647 
648 	dev_dbg(dev, "%s\n", __func__);
649 
650 	if (!urb) {
651 		dev_warn(dev, "pkt doesn't have urb\n");
652 		return;
653 	}
654 
655 	if (!usbhsh_is_running(hpriv))
656 		status = -ESHUTDOWN;
657 
658 	urb->actual_length = pkt->actual;
659 
660 	usbhsh_endpoint_sequence_save(hpriv, urb, pkt);
661 	usbhsh_ureq_free(hpriv, ureq);
662 
663 	usbhsh_pipe_detach(hpriv, usbhsh_ep_to_uep(urb->ep));
664 
665 	usb_hcd_unlink_urb_from_ep(hcd, urb);
666 	usb_hcd_giveback_urb(hcd, urb, status);
667 }
668 
669 static int usbhsh_queue_push(struct usb_hcd *hcd,
670 			     struct urb *urb,
671 			     gfp_t mem_flags)
672 {
673 	struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
674 	struct usbhsh_ep *uep = usbhsh_ep_to_uep(urb->ep);
675 	struct usbhs_pipe *pipe = usbhsh_uep_to_pipe(uep);
676 	struct device *dev = usbhsh_hcd_to_dev(hcd);
677 	struct usbhsh_request *ureq;
678 	void *buf;
679 	int len, sequence;
680 
681 	if (usb_pipeisoc(urb->pipe)) {
682 		dev_err(dev, "pipe iso is not supported now\n");
683 		return -EIO;
684 	}
685 
686 	/* this ureq will be freed on usbhsh_queue_done() */
687 	ureq = usbhsh_ureq_alloc(hpriv, urb, mem_flags);
688 	if (unlikely(!ureq)) {
689 		dev_err(dev, "ureq alloc fail\n");
690 		return -ENOMEM;
691 	}
692 
693 	if (usb_pipein(urb->pipe))
694 		pipe->handler = &usbhs_fifo_dma_pop_handler;
695 	else
696 		pipe->handler = &usbhs_fifo_dma_push_handler;
697 
698 	buf = (void *)(urb->transfer_buffer + urb->actual_length);
699 	len = urb->transfer_buffer_length - urb->actual_length;
700 
701 	sequence = usb_gettoggle(urb->dev,
702 				 usb_pipeendpoint(urb->pipe),
703 				 usb_pipeout(urb->pipe));
704 
705 	dev_dbg(dev, "%s\n", __func__);
706 	usbhs_pkt_push(pipe, &ureq->pkt, usbhsh_queue_done,
707 		       buf, len, (urb->transfer_flags & URB_ZERO_PACKET),
708 		       sequence);
709 
710 	usbhs_pkt_start(pipe);
711 
712 	return 0;
713 }
714 
715 static void usbhsh_queue_force_pop(struct usbhs_priv *priv,
716 				   struct usbhs_pipe *pipe)
717 {
718 	struct usbhs_pkt *pkt;
719 
720 	while (1) {
721 		pkt = usbhs_pkt_pop(pipe, NULL);
722 		if (!pkt)
723 			break;
724 
725 		/*
726 		 * if all packet are gone, usbhsh_endpoint_disable()
727 		 * will be called.
728 		 * then, attached device/endpoint/pipe will be detached
729 		 */
730 		usbhsh_queue_done(priv, pkt);
731 	}
732 }
733 
734 static void usbhsh_queue_force_pop_all(struct usbhs_priv *priv)
735 {
736 	struct usbhs_pipe *pos;
737 	int i;
738 
739 	usbhs_for_each_pipe_with_dcp(pos, priv, i)
740 		usbhsh_queue_force_pop(priv, pos);
741 }
742 
743 /*
744  *		DCP setup stage
745  */
746 static int usbhsh_is_request_address(struct urb *urb)
747 {
748 	struct usb_ctrlrequest *req;
749 
750 	req = (struct usb_ctrlrequest *)urb->setup_packet;
751 
752 	if ((DeviceOutRequest    == req->bRequestType << 8) &&
753 	    (USB_REQ_SET_ADDRESS == req->bRequest))
754 		return 1;
755 	else
756 		return 0;
757 }
758 
759 static void usbhsh_setup_stage_packet_push(struct usbhsh_hpriv *hpriv,
760 					   struct urb *urb,
761 					   struct usbhs_pipe *pipe)
762 {
763 	struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
764 	struct usb_ctrlrequest req;
765 	struct device *dev = usbhs_priv_to_dev(priv);
766 
767 	/*
768 	 * wait setup packet ACK
769 	 * see
770 	 *	usbhsh_irq_setup_ack()
771 	 *	usbhsh_irq_setup_err()
772 	 */
773 	init_completion(&hpriv->setup_ack_done);
774 
775 	/* copy original request */
776 	memcpy(&req, urb->setup_packet, sizeof(struct usb_ctrlrequest));
777 
778 	/*
779 	 * renesas_usbhs can not use original usb address.
780 	 * see HARDWARE LIMITATION.
781 	 * modify usb address here to use attached device.
782 	 * see usbhsh_device_attach()
783 	 */
784 	if (usbhsh_is_request_address(urb)) {
785 		struct usb_device *usbv = usbhsh_urb_to_usbv(urb);
786 		struct usbhsh_device *udev = usbhsh_usbv_to_udev(usbv);
787 
788 		/* udev is a attached device */
789 		req.wValue = usbhsh_device_number(hpriv, udev);
790 		dev_dbg(dev, "create new address - %d\n", req.wValue);
791 	}
792 
793 	/* set request */
794 	usbhs_usbreq_set_val(priv, &req);
795 
796 	/*
797 	 * wait setup packet ACK
798 	 */
799 	wait_for_completion(&hpriv->setup_ack_done);
800 
801 	dev_dbg(dev, "%s done\n", __func__);
802 }
803 
804 /*
805  *		DCP data stage
806  */
807 static void usbhsh_data_stage_packet_done(struct usbhs_priv *priv,
808 					  struct usbhs_pkt *pkt)
809 {
810 	struct usbhsh_request *ureq = usbhsh_pkt_to_ureq(pkt);
811 	struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
812 
813 	/* this ureq was connected to urb when usbhsh_urb_enqueue()  */
814 
815 	usbhsh_ureq_free(hpriv, ureq);
816 }
817 
818 static int usbhsh_data_stage_packet_push(struct usbhsh_hpriv *hpriv,
819 					 struct urb *urb,
820 					 struct usbhs_pipe *pipe,
821 					 gfp_t mem_flags)
822 
823 {
824 	struct usbhsh_request *ureq;
825 
826 	/* this ureq will be freed on usbhsh_data_stage_packet_done() */
827 	ureq = usbhsh_ureq_alloc(hpriv, urb, mem_flags);
828 	if (unlikely(!ureq))
829 		return -ENOMEM;
830 
831 	if (usb_pipein(urb->pipe))
832 		pipe->handler = &usbhs_dcp_data_stage_in_handler;
833 	else
834 		pipe->handler = &usbhs_dcp_data_stage_out_handler;
835 
836 	usbhs_pkt_push(pipe, &ureq->pkt,
837 		       usbhsh_data_stage_packet_done,
838 		       urb->transfer_buffer,
839 		       urb->transfer_buffer_length,
840 		       (urb->transfer_flags & URB_ZERO_PACKET),
841 		       -1);
842 
843 	return 0;
844 }
845 
846 /*
847  *		DCP status stage
848  */
849 static int usbhsh_status_stage_packet_push(struct usbhsh_hpriv *hpriv,
850 					    struct urb *urb,
851 					    struct usbhs_pipe *pipe,
852 					    gfp_t mem_flags)
853 {
854 	struct usbhsh_request *ureq;
855 
856 	/* This ureq will be freed on usbhsh_queue_done() */
857 	ureq = usbhsh_ureq_alloc(hpriv, urb, mem_flags);
858 	if (unlikely(!ureq))
859 		return -ENOMEM;
860 
861 	if (usb_pipein(urb->pipe))
862 		pipe->handler = &usbhs_dcp_status_stage_in_handler;
863 	else
864 		pipe->handler = &usbhs_dcp_status_stage_out_handler;
865 
866 	usbhs_pkt_push(pipe, &ureq->pkt,
867 		       usbhsh_queue_done,
868 		       NULL,
869 		       urb->transfer_buffer_length,
870 		       0, -1);
871 
872 	return 0;
873 }
874 
875 static int usbhsh_dcp_queue_push(struct usb_hcd *hcd,
876 				 struct urb *urb,
877 				 gfp_t mflags)
878 {
879 	struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
880 	struct usbhsh_ep *uep = usbhsh_ep_to_uep(urb->ep);
881 	struct usbhs_pipe *pipe = usbhsh_uep_to_pipe(uep);
882 	struct device *dev = usbhsh_hcd_to_dev(hcd);
883 	int ret;
884 
885 	dev_dbg(dev, "%s\n", __func__);
886 
887 	/*
888 	 * setup stage
889 	 *
890 	 * usbhsh_send_setup_stage_packet() wait SACK/SIGN
891 	 */
892 	usbhsh_setup_stage_packet_push(hpriv, urb, pipe);
893 
894 	/*
895 	 * data stage
896 	 *
897 	 * It is pushed only when urb has buffer.
898 	 */
899 	if (urb->transfer_buffer_length) {
900 		ret = usbhsh_data_stage_packet_push(hpriv, urb, pipe, mflags);
901 		if (ret < 0) {
902 			dev_err(dev, "data stage failed\n");
903 			return ret;
904 		}
905 	}
906 
907 	/*
908 	 * status stage
909 	 */
910 	ret = usbhsh_status_stage_packet_push(hpriv, urb, pipe, mflags);
911 	if (ret < 0) {
912 		dev_err(dev, "status stage failed\n");
913 		return ret;
914 	}
915 
916 	/*
917 	 * start pushed packets
918 	 */
919 	usbhs_pkt_start(pipe);
920 
921 	return 0;
922 }
923 
924 /*
925  *		dma map functions
926  */
927 static int usbhsh_dma_map_ctrl(struct device *dma_dev, struct usbhs_pkt *pkt,
928 			       int map)
929 {
930 	if (map) {
931 		struct usbhsh_request *ureq = usbhsh_pkt_to_ureq(pkt);
932 		struct urb *urb = ureq->urb;
933 
934 		/* it can not use scatter/gather */
935 		if (urb->num_sgs)
936 			return -EINVAL;
937 
938 		pkt->dma = urb->transfer_dma;
939 		if (!pkt->dma)
940 			return -EINVAL;
941 	}
942 
943 	return 0;
944 }
945 
946 /*
947  *		for hc_driver
948  */
949 static int usbhsh_host_start(struct usb_hcd *hcd)
950 {
951 	return 0;
952 }
953 
954 static void usbhsh_host_stop(struct usb_hcd *hcd)
955 {
956 }
957 
958 static int usbhsh_urb_enqueue(struct usb_hcd *hcd,
959 			      struct urb *urb,
960 			      gfp_t mem_flags)
961 {
962 	struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
963 	struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
964 	struct device *dev = usbhs_priv_to_dev(priv);
965 	struct usb_host_endpoint *ep = urb->ep;
966 	struct usbhsh_device *new_udev = NULL;
967 	int is_dir_in = usb_pipein(urb->pipe);
968 	int ret;
969 
970 	dev_dbg(dev, "%s (%s)\n", __func__, is_dir_in ? "in" : "out");
971 
972 	if (!usbhsh_is_running(hpriv)) {
973 		ret = -EIO;
974 		dev_err(dev, "host is not running\n");
975 		goto usbhsh_urb_enqueue_error_not_linked;
976 	}
977 
978 	ret = usb_hcd_link_urb_to_ep(hcd, urb);
979 	if (ret) {
980 		dev_err(dev, "urb link failed\n");
981 		goto usbhsh_urb_enqueue_error_not_linked;
982 	}
983 
984 	/*
985 	 * attach udev if needed
986 	 * see [image of mod_host]
987 	 */
988 	if (!usbhsh_device_get(hpriv, urb)) {
989 		new_udev = usbhsh_device_attach(hpriv, urb);
990 		if (!new_udev) {
991 			ret = -EIO;
992 			dev_err(dev, "device attach failed\n");
993 			goto usbhsh_urb_enqueue_error_not_linked;
994 		}
995 	}
996 
997 	/*
998 	 * attach endpoint if needed
999 	 * see [image of mod_host]
1000 	 */
1001 	if (!usbhsh_ep_to_uep(ep)) {
1002 		ret = usbhsh_endpoint_attach(hpriv, urb, mem_flags);
1003 		if (ret < 0) {
1004 			dev_err(dev, "endpoint attach failed\n");
1005 			goto usbhsh_urb_enqueue_error_free_device;
1006 		}
1007 	}
1008 
1009 	/*
1010 	 * attach pipe to endpoint
1011 	 * see [image of mod_host]
1012 	 */
1013 	ret = usbhsh_pipe_attach(hpriv, urb);
1014 	if (ret < 0) {
1015 		dev_err(dev, "pipe attach failed\n");
1016 		goto usbhsh_urb_enqueue_error_free_endpoint;
1017 	}
1018 
1019 	/*
1020 	 * push packet
1021 	 */
1022 	if (usb_pipecontrol(urb->pipe))
1023 		ret = usbhsh_dcp_queue_push(hcd, urb, mem_flags);
1024 	else
1025 		ret = usbhsh_queue_push(hcd, urb, mem_flags);
1026 
1027 	return ret;
1028 
1029 usbhsh_urb_enqueue_error_free_endpoint:
1030 	usbhsh_endpoint_detach(hpriv, ep);
1031 usbhsh_urb_enqueue_error_free_device:
1032 	if (new_udev)
1033 		usbhsh_device_detach(hpriv, new_udev);
1034 usbhsh_urb_enqueue_error_not_linked:
1035 
1036 	dev_dbg(dev, "%s error\n", __func__);
1037 
1038 	return ret;
1039 }
1040 
1041 static int usbhsh_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
1042 {
1043 	struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
1044 	struct usbhsh_request *ureq = usbhsh_urb_to_ureq(urb);
1045 
1046 	if (ureq) {
1047 		struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
1048 		struct usbhs_pkt *pkt = &ureq->pkt;
1049 
1050 		usbhs_pkt_pop(pkt->pipe, pkt);
1051 		usbhsh_queue_done(priv, pkt);
1052 	}
1053 
1054 	return 0;
1055 }
1056 
1057 static void usbhsh_endpoint_disable(struct usb_hcd *hcd,
1058 				    struct usb_host_endpoint *ep)
1059 {
1060 	struct usbhsh_ep *uep = usbhsh_ep_to_uep(ep);
1061 	struct usbhsh_device *udev;
1062 	struct usbhsh_hpriv *hpriv;
1063 
1064 	/*
1065 	 * this function might be called manytimes by same hcd/ep
1066 	 * in-endpoint == out-endpoint if ep == dcp.
1067 	 */
1068 	if (!uep)
1069 		return;
1070 
1071 	udev	= usbhsh_uep_to_udev(uep);
1072 	hpriv	= usbhsh_hcd_to_hpriv(hcd);
1073 
1074 	usbhsh_endpoint_detach(hpriv, ep);
1075 
1076 	/*
1077 	 * if there is no endpoint,
1078 	 * free device
1079 	 */
1080 	if (!usbhsh_device_has_endpoint(udev))
1081 		usbhsh_device_detach(hpriv, udev);
1082 }
1083 
1084 static int usbhsh_hub_status_data(struct usb_hcd *hcd, char *buf)
1085 {
1086 	struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
1087 	int roothub_id = 1; /* only 1 root hub */
1088 
1089 	/*
1090 	 * does port stat was changed ?
1091 	 * check USB_PORT_STAT_C_xxx << 16
1092 	 */
1093 	if (usbhsh_port_stat_get(hpriv) & 0xFFFF0000)
1094 		*buf = (1 << roothub_id);
1095 	else
1096 		*buf = 0;
1097 
1098 	return !!(*buf);
1099 }
1100 
1101 static int __usbhsh_hub_hub_feature(struct usbhsh_hpriv *hpriv,
1102 				    u16 typeReq, u16 wValue,
1103 				    u16 wIndex, char *buf, u16 wLength)
1104 {
1105 	struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
1106 	struct device *dev = usbhs_priv_to_dev(priv);
1107 
1108 	switch (wValue) {
1109 	case C_HUB_OVER_CURRENT:
1110 	case C_HUB_LOCAL_POWER:
1111 		dev_dbg(dev, "%s :: C_HUB_xx\n", __func__);
1112 		return 0;
1113 	}
1114 
1115 	return -EPIPE;
1116 }
1117 
1118 static int __usbhsh_hub_port_feature(struct usbhsh_hpriv *hpriv,
1119 				     u16 typeReq, u16 wValue,
1120 				     u16 wIndex, char *buf, u16 wLength)
1121 {
1122 	struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
1123 	struct device *dev = usbhs_priv_to_dev(priv);
1124 	int enable = (typeReq == SetPortFeature);
1125 	int speed, i, timeout = 128;
1126 	int roothub_id = 1; /* only 1 root hub */
1127 
1128 	/* common error */
1129 	if (wIndex > roothub_id || wLength != 0)
1130 		return -EPIPE;
1131 
1132 	/* check wValue */
1133 	switch (wValue) {
1134 	case USB_PORT_FEAT_POWER:
1135 		usbhs_vbus_ctrl(priv, enable);
1136 		dev_dbg(dev, "%s :: USB_PORT_FEAT_POWER\n", __func__);
1137 		break;
1138 
1139 	case USB_PORT_FEAT_ENABLE:
1140 	case USB_PORT_FEAT_SUSPEND:
1141 	case USB_PORT_FEAT_C_ENABLE:
1142 	case USB_PORT_FEAT_C_SUSPEND:
1143 	case USB_PORT_FEAT_C_CONNECTION:
1144 	case USB_PORT_FEAT_C_OVER_CURRENT:
1145 	case USB_PORT_FEAT_C_RESET:
1146 		dev_dbg(dev, "%s :: USB_PORT_FEAT_xxx\n", __func__);
1147 		break;
1148 
1149 	case USB_PORT_FEAT_RESET:
1150 		if (!enable)
1151 			break;
1152 
1153 		usbhsh_port_stat_clear(hpriv,
1154 				       USB_PORT_STAT_HIGH_SPEED |
1155 				       USB_PORT_STAT_LOW_SPEED);
1156 
1157 		usbhsh_queue_force_pop_all(priv);
1158 
1159 		usbhs_bus_send_reset(priv);
1160 		msleep(20);
1161 		usbhs_bus_send_sof_enable(priv);
1162 
1163 		for (i = 0; i < timeout ; i++) {
1164 			switch (usbhs_bus_get_speed(priv)) {
1165 			case USB_SPEED_LOW:
1166 				speed = USB_PORT_STAT_LOW_SPEED;
1167 				goto got_usb_bus_speed;
1168 			case USB_SPEED_HIGH:
1169 				speed = USB_PORT_STAT_HIGH_SPEED;
1170 				goto got_usb_bus_speed;
1171 			case USB_SPEED_FULL:
1172 				speed = 0;
1173 				goto got_usb_bus_speed;
1174 			}
1175 
1176 			msleep(20);
1177 		}
1178 		return -EPIPE;
1179 
1180 got_usb_bus_speed:
1181 		usbhsh_port_stat_set(hpriv, speed);
1182 		usbhsh_port_stat_set(hpriv, USB_PORT_STAT_ENABLE);
1183 
1184 		dev_dbg(dev, "%s :: USB_PORT_FEAT_RESET (speed = %d)\n",
1185 			__func__, speed);
1186 
1187 		/* status change is not needed */
1188 		return 0;
1189 
1190 	default:
1191 		return -EPIPE;
1192 	}
1193 
1194 	/* set/clear status */
1195 	if (enable)
1196 		usbhsh_port_stat_set(hpriv, (1 << wValue));
1197 	else
1198 		usbhsh_port_stat_clear(hpriv, (1 << wValue));
1199 
1200 	return 0;
1201 }
1202 
1203 static int __usbhsh_hub_get_status(struct usbhsh_hpriv *hpriv,
1204 				   u16 typeReq, u16 wValue,
1205 				   u16 wIndex, char *buf, u16 wLength)
1206 {
1207 	struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
1208 	struct usb_hub_descriptor *desc = (struct usb_hub_descriptor *)buf;
1209 	struct device *dev = usbhs_priv_to_dev(priv);
1210 	int roothub_id = 1; /* only 1 root hub */
1211 
1212 	switch (typeReq) {
1213 	case GetHubStatus:
1214 		dev_dbg(dev, "%s :: GetHubStatus\n", __func__);
1215 
1216 		*buf = 0x00;
1217 		break;
1218 
1219 	case GetPortStatus:
1220 		if (wIndex != roothub_id)
1221 			return -EPIPE;
1222 
1223 		dev_dbg(dev, "%s :: GetPortStatus\n", __func__);
1224 		*(__le32 *)buf = cpu_to_le32(usbhsh_port_stat_get(hpriv));
1225 		break;
1226 
1227 	case GetHubDescriptor:
1228 		desc->bDescriptorType		= USB_DT_HUB;
1229 		desc->bHubContrCurrent		= 0;
1230 		desc->bNbrPorts			= roothub_id;
1231 		desc->bDescLength		= 9;
1232 		desc->bPwrOn2PwrGood		= 0;
1233 		desc->wHubCharacteristics	=
1234 			cpu_to_le16(HUB_CHAR_INDV_PORT_LPSM | HUB_CHAR_NO_OCPM);
1235 		desc->u.hs.DeviceRemovable[0]	= (roothub_id << 1);
1236 		desc->u.hs.DeviceRemovable[1]	= ~0;
1237 		dev_dbg(dev, "%s :: GetHubDescriptor\n", __func__);
1238 		break;
1239 	}
1240 
1241 	return 0;
1242 }
1243 
1244 static int usbhsh_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
1245 			      u16 wIndex, char *buf, u16 wLength)
1246 {
1247 	struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
1248 	struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
1249 	struct device *dev = usbhs_priv_to_dev(priv);
1250 	int ret = -EPIPE;
1251 
1252 	switch (typeReq) {
1253 
1254 	/* Hub Feature */
1255 	case ClearHubFeature:
1256 	case SetHubFeature:
1257 		ret = __usbhsh_hub_hub_feature(hpriv, typeReq,
1258 					       wValue, wIndex, buf, wLength);
1259 		break;
1260 
1261 	/* Port Feature */
1262 	case SetPortFeature:
1263 	case ClearPortFeature:
1264 		ret = __usbhsh_hub_port_feature(hpriv, typeReq,
1265 						wValue, wIndex, buf, wLength);
1266 		break;
1267 
1268 	/* Get status */
1269 	case GetHubStatus:
1270 	case GetPortStatus:
1271 	case GetHubDescriptor:
1272 		ret = __usbhsh_hub_get_status(hpriv, typeReq,
1273 					      wValue, wIndex, buf, wLength);
1274 		break;
1275 	}
1276 
1277 	dev_dbg(dev, "typeReq = %x, ret = %d, port_stat = %x\n",
1278 		typeReq, ret, usbhsh_port_stat_get(hpriv));
1279 
1280 	return ret;
1281 }
1282 
1283 static int usbhsh_bus_nop(struct usb_hcd *hcd)
1284 {
1285 	/* nothing to do */
1286 	return 0;
1287 }
1288 
1289 static const struct hc_driver usbhsh_driver = {
1290 	.description =		usbhsh_hcd_name,
1291 	.hcd_priv_size =	sizeof(struct usbhsh_hpriv),
1292 
1293 	/*
1294 	 * generic hardware linkage
1295 	 */
1296 	.flags =		HCD_USB2,
1297 
1298 	.start =		usbhsh_host_start,
1299 	.stop =			usbhsh_host_stop,
1300 
1301 	/*
1302 	 * managing i/o requests and associated device resources
1303 	 */
1304 	.urb_enqueue =		usbhsh_urb_enqueue,
1305 	.urb_dequeue =		usbhsh_urb_dequeue,
1306 	.endpoint_disable =	usbhsh_endpoint_disable,
1307 
1308 	/*
1309 	 * root hub
1310 	 */
1311 	.hub_status_data =	usbhsh_hub_status_data,
1312 	.hub_control =		usbhsh_hub_control,
1313 	.bus_suspend =		usbhsh_bus_nop,
1314 	.bus_resume =		usbhsh_bus_nop,
1315 };
1316 
1317 /*
1318  *		interrupt functions
1319  */
1320 static int usbhsh_irq_attch(struct usbhs_priv *priv,
1321 			    struct usbhs_irq_state *irq_state)
1322 {
1323 	struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1324 	struct device *dev = usbhs_priv_to_dev(priv);
1325 
1326 	dev_dbg(dev, "device attached\n");
1327 
1328 	usbhsh_port_stat_set(hpriv, USB_PORT_STAT_CONNECTION);
1329 	usbhsh_port_stat_set(hpriv, USB_PORT_STAT_C_CONNECTION << 16);
1330 
1331 	/*
1332 	 * attch interrupt might happen infinitely on some device
1333 	 * (on self power USB hub ?)
1334 	 * disable it here.
1335 	 *
1336 	 * usbhsh_is_running() becomes effective
1337 	 * according to this process.
1338 	 * see
1339 	 *	usbhsh_is_running()
1340 	 *	usbhsh_urb_enqueue()
1341 	 */
1342 	hpriv->mod.irq_attch = NULL;
1343 	usbhs_irq_callback_update(priv, &hpriv->mod);
1344 
1345 	return 0;
1346 }
1347 
1348 static int usbhsh_irq_dtch(struct usbhs_priv *priv,
1349 			   struct usbhs_irq_state *irq_state)
1350 {
1351 	struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1352 	struct device *dev = usbhs_priv_to_dev(priv);
1353 
1354 	dev_dbg(dev, "device detached\n");
1355 
1356 	usbhsh_port_stat_clear(hpriv, USB_PORT_STAT_CONNECTION);
1357 	usbhsh_port_stat_set(hpriv, USB_PORT_STAT_C_CONNECTION << 16);
1358 
1359 	/*
1360 	 * enable attch interrupt again
1361 	 *
1362 	 * usbhsh_is_running() becomes invalid
1363 	 * according to this process.
1364 	 * see
1365 	 *	usbhsh_is_running()
1366 	 *	usbhsh_urb_enqueue()
1367 	 */
1368 	hpriv->mod.irq_attch = usbhsh_irq_attch;
1369 	usbhs_irq_callback_update(priv, &hpriv->mod);
1370 
1371 	/*
1372 	 * usbhsh_queue_force_pop_all() should be called
1373 	 * after usbhsh_is_running() becomes invalid.
1374 	 */
1375 	usbhsh_queue_force_pop_all(priv);
1376 
1377 	return 0;
1378 }
1379 
1380 static int usbhsh_irq_setup_ack(struct usbhs_priv *priv,
1381 				struct usbhs_irq_state *irq_state)
1382 {
1383 	struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1384 	struct device *dev = usbhs_priv_to_dev(priv);
1385 
1386 	dev_dbg(dev, "setup packet OK\n");
1387 
1388 	complete(&hpriv->setup_ack_done); /* see usbhsh_urb_enqueue() */
1389 
1390 	return 0;
1391 }
1392 
1393 static int usbhsh_irq_setup_err(struct usbhs_priv *priv,
1394 				struct usbhs_irq_state *irq_state)
1395 {
1396 	struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1397 	struct device *dev = usbhs_priv_to_dev(priv);
1398 
1399 	dev_dbg(dev, "setup packet Err\n");
1400 
1401 	complete(&hpriv->setup_ack_done); /* see usbhsh_urb_enqueue() */
1402 
1403 	return 0;
1404 }
1405 
1406 /*
1407  *		module start/stop
1408  */
1409 static void usbhsh_pipe_init_for_host(struct usbhs_priv *priv)
1410 {
1411 	struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1412 	struct usbhs_pipe *pipe;
1413 	struct renesas_usbhs_driver_pipe_config *pipe_configs =
1414 					usbhs_get_dparam(priv, pipe_configs);
1415 	int pipe_size = usbhs_get_dparam(priv, pipe_size);
1416 	int old_type, dir_in, i;
1417 
1418 	/* init all pipe */
1419 	old_type = USB_ENDPOINT_XFER_CONTROL;
1420 	for (i = 0; i < pipe_size; i++) {
1421 
1422 		/*
1423 		 * data "output" will be finished as soon as possible,
1424 		 * but there is no guaranty at data "input" case.
1425 		 *
1426 		 * "input" needs "standby" pipe.
1427 		 * So, "input" direction pipe > "output" direction pipe
1428 		 * is good idea.
1429 		 *
1430 		 * 1st USB_ENDPOINT_XFER_xxx will be output direction,
1431 		 * and the other will be input direction here.
1432 		 *
1433 		 * ex)
1434 		 * ...
1435 		 * USB_ENDPOINT_XFER_ISOC -> dir out
1436 		 * USB_ENDPOINT_XFER_ISOC -> dir in
1437 		 * USB_ENDPOINT_XFER_BULK -> dir out
1438 		 * USB_ENDPOINT_XFER_BULK -> dir in
1439 		 * USB_ENDPOINT_XFER_BULK -> dir in
1440 		 * ...
1441 		 */
1442 		dir_in = (pipe_configs[i].type == old_type);
1443 		old_type = pipe_configs[i].type;
1444 
1445 		if (USB_ENDPOINT_XFER_CONTROL == pipe_configs[i].type) {
1446 			pipe = usbhs_dcp_malloc(priv);
1447 			usbhsh_hpriv_to_dcp(hpriv) = pipe;
1448 		} else {
1449 			pipe = usbhs_pipe_malloc(priv,
1450 						 pipe_configs[i].type,
1451 						 dir_in);
1452 		}
1453 
1454 		pipe->mod_private = NULL;
1455 	}
1456 }
1457 
1458 static int usbhsh_start(struct usbhs_priv *priv)
1459 {
1460 	struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1461 	struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
1462 	struct usbhs_mod *mod = usbhs_mod_get_current(priv);
1463 	struct device *dev = usbhs_priv_to_dev(priv);
1464 	int ret;
1465 
1466 	/* add hcd */
1467 	ret = usb_add_hcd(hcd, 0, 0);
1468 	if (ret < 0)
1469 		return 0;
1470 	device_wakeup_enable(hcd->self.controller);
1471 
1472 	/*
1473 	 * pipe initialize and enable DCP
1474 	 */
1475 	usbhs_fifo_init(priv);
1476 	usbhs_pipe_init(priv,
1477 			usbhsh_dma_map_ctrl);
1478 	usbhsh_pipe_init_for_host(priv);
1479 
1480 	/*
1481 	 * system config enble
1482 	 * - HI speed
1483 	 * - host
1484 	 * - usb module
1485 	 */
1486 	usbhs_sys_host_ctrl(priv, 1);
1487 
1488 	/*
1489 	 * enable irq callback
1490 	 */
1491 	mod->irq_attch		= usbhsh_irq_attch;
1492 	mod->irq_dtch		= usbhsh_irq_dtch;
1493 	mod->irq_sack		= usbhsh_irq_setup_ack;
1494 	mod->irq_sign		= usbhsh_irq_setup_err;
1495 	usbhs_irq_callback_update(priv, mod);
1496 
1497 	dev_dbg(dev, "start host\n");
1498 
1499 	return ret;
1500 }
1501 
1502 static int usbhsh_stop(struct usbhs_priv *priv)
1503 {
1504 	struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1505 	struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
1506 	struct usbhs_mod *mod = usbhs_mod_get_current(priv);
1507 	struct device *dev = usbhs_priv_to_dev(priv);
1508 
1509 	/*
1510 	 * disable irq callback
1511 	 */
1512 	mod->irq_attch	= NULL;
1513 	mod->irq_dtch	= NULL;
1514 	mod->irq_sack	= NULL;
1515 	mod->irq_sign	= NULL;
1516 	usbhs_irq_callback_update(priv, mod);
1517 
1518 	usb_remove_hcd(hcd);
1519 
1520 	/* disable sys */
1521 	usbhs_sys_host_ctrl(priv, 0);
1522 
1523 	dev_dbg(dev, "quit host\n");
1524 
1525 	return 0;
1526 }
1527 
1528 int usbhs_mod_host_probe(struct usbhs_priv *priv)
1529 {
1530 	struct usbhsh_hpriv *hpriv;
1531 	struct usb_hcd *hcd;
1532 	struct usbhsh_device *udev;
1533 	struct device *dev = usbhs_priv_to_dev(priv);
1534 	int i;
1535 
1536 	/* initialize hcd */
1537 	hcd = usb_create_hcd(&usbhsh_driver, dev, usbhsh_hcd_name);
1538 	if (!hcd) {
1539 		dev_err(dev, "Failed to create hcd\n");
1540 		return -ENOMEM;
1541 	}
1542 	hcd->has_tt = 1; /* for low/full speed */
1543 
1544 	/*
1545 	 * CAUTION
1546 	 *
1547 	 * There is no guarantee that it is possible to access usb module here.
1548 	 * Don't accesses to it.
1549 	 * The accesse will be enable after "usbhsh_start"
1550 	 */
1551 
1552 	hpriv = usbhsh_hcd_to_hpriv(hcd);
1553 
1554 	/*
1555 	 * register itself
1556 	 */
1557 	usbhs_mod_register(priv, &hpriv->mod, USBHS_HOST);
1558 
1559 	/* init hpriv */
1560 	hpriv->mod.name		= "host";
1561 	hpriv->mod.start	= usbhsh_start;
1562 	hpriv->mod.stop		= usbhsh_stop;
1563 	usbhsh_port_stat_init(hpriv);
1564 
1565 	/* init all device */
1566 	usbhsh_for_each_udev_with_dev0(udev, hpriv, i) {
1567 		udev->usbv	= NULL;
1568 		INIT_LIST_HEAD(&udev->ep_list_head);
1569 	}
1570 
1571 	dev_info(dev, "host probed\n");
1572 
1573 	return 0;
1574 }
1575 
1576 int usbhs_mod_host_remove(struct usbhs_priv *priv)
1577 {
1578 	struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1579 	struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
1580 
1581 	usb_put_hcd(hcd);
1582 
1583 	return 0;
1584 }
1585