Linux Audio

Check our new training course

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