PageRenderTime 53ms CodeModel.GetById 12ms app.highlight 30ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/usb/renesas_usbhs/mod_gadget.c

https://bitbucket.org/slukk/jb-tsm-kernel-4.2
C | 1385 lines | 843 code | 249 blank | 293 comment | 69 complexity | f75862a590c0b05f0db50fb5a52bddfe MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
   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/module.h>
  19#include <linux/platform_device.h>
  20#include <linux/usb/ch9.h>
  21#include <linux/usb/gadget.h>
  22#include "common.h"
  23
  24/*
  25 *		struct
  26 */
  27struct usbhsg_request {
  28	struct usb_request	req;
  29	struct list_head	node;
  30};
  31
  32#define EP_NAME_SIZE 8
  33struct usbhsg_gpriv;
  34struct usbhsg_pipe_handle;
  35struct usbhsg_uep {
  36	struct usb_ep		 ep;
  37	struct usbhs_pipe	*pipe;
  38	struct list_head	 list;
  39
  40	char ep_name[EP_NAME_SIZE];
  41
  42	struct usbhsg_gpriv *gpriv;
  43	struct usbhsg_pipe_handle *handler;
  44};
  45
  46struct usbhsg_gpriv {
  47	struct usb_gadget	 gadget;
  48	struct usbhs_mod	 mod;
  49
  50	struct usbhsg_uep	*uep;
  51	int			 uep_size;
  52
  53	struct usb_gadget_driver	*driver;
  54
  55	u32	status;
  56#define USBHSG_STATUS_STARTED		(1 << 0)
  57#define USBHSG_STATUS_REGISTERD		(1 << 1)
  58#define USBHSG_STATUS_WEDGE		(1 << 2)
  59};
  60
  61struct usbhsg_pipe_handle {
  62	int (*prepare)(struct usbhsg_uep *uep, struct usbhsg_request *ureq);
  63	int (*try_run)(struct usbhsg_uep *uep, struct usbhsg_request *ureq);
  64	void (*irq_mask)(struct usbhsg_uep *uep, int enable);
  65};
  66
  67struct usbhsg_recip_handle {
  68	char *name;
  69	int (*device)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
  70		      struct usb_ctrlrequest *ctrl);
  71	int (*interface)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
  72			 struct usb_ctrlrequest *ctrl);
  73	int (*endpoint)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
  74			struct usb_ctrlrequest *ctrl);
  75};
  76
  77/*
  78 *		macro
  79 */
  80#define usbhsg_priv_to_gpriv(priv)			\
  81	container_of(					\
  82		usbhs_mod_get(priv, USBHS_GADGET),	\
  83		struct usbhsg_gpriv, mod)
  84
  85#define __usbhsg_for_each_uep(start, pos, g, i)	\
  86	for (i = start, pos = (g)->uep;		\
  87	     i < (g)->uep_size;			\
  88	     i++, pos = (g)->uep + i)
  89
  90#define usbhsg_for_each_uep(pos, gpriv, i)	\
  91	__usbhsg_for_each_uep(1, pos, gpriv, i)
  92
  93#define usbhsg_for_each_uep_with_dcp(pos, gpriv, i)	\
  94	__usbhsg_for_each_uep(0, pos, gpriv, i)
  95
  96#define usbhsg_gadget_to_gpriv(g)\
  97	container_of(g, struct usbhsg_gpriv, gadget)
  98
  99#define usbhsg_req_to_ureq(r)\
 100	container_of(r, struct usbhsg_request, req)
 101
 102#define usbhsg_ep_to_uep(e)		container_of(e, struct usbhsg_uep, ep)
 103#define usbhsg_gpriv_to_lock(gp)	usbhs_priv_to_lock((gp)->mod.priv)
 104#define usbhsg_gpriv_to_dev(gp)		usbhs_priv_to_dev((gp)->mod.priv)
 105#define usbhsg_gpriv_to_priv(gp)	((gp)->mod.priv)
 106#define usbhsg_gpriv_to_dcp(gp)		((gp)->uep)
 107#define usbhsg_gpriv_to_nth_uep(gp, i)	((gp)->uep + i)
 108#define usbhsg_uep_to_gpriv(u)		((u)->gpriv)
 109#define usbhsg_uep_to_pipe(u)		((u)->pipe)
 110#define usbhsg_pipe_to_uep(p)		((p)->mod_private)
 111#define usbhsg_is_dcp(u)		((u) == usbhsg_gpriv_to_dcp((u)->gpriv))
 112
 113#define usbhsg_is_not_connected(gp) ((gp)->gadget.speed == USB_SPEED_UNKNOWN)
 114
 115/* status */
 116#define usbhsg_status_init(gp)   do {(gp)->status = 0; } while (0)
 117#define usbhsg_status_set(gp, b) (gp->status |=  b)
 118#define usbhsg_status_clr(gp, b) (gp->status &= ~b)
 119#define usbhsg_status_has(gp, b) (gp->status &   b)
 120
 121/*
 122 *		usbhsg_trylock
 123 *
 124 * This driver don't use spin_try_lock
 125 * to avoid warning of CONFIG_DEBUG_SPINLOCK
 126 */
 127static spinlock_t *usbhsg_trylock(struct usbhsg_gpriv *gpriv,
 128				  unsigned long *flags)
 129{
 130	spinlock_t *lock = usbhsg_gpriv_to_lock(gpriv);
 131
 132	/* check spin lock status
 133	 * to avoid deadlock/nest */
 134	if (spin_is_locked(lock))
 135		return NULL;
 136
 137	spin_lock_irqsave(lock, *flags);
 138
 139	return lock;
 140}
 141
 142static void usbhsg_unlock(spinlock_t *lock, unsigned long *flags)
 143{
 144	if (!lock)
 145		return;
 146
 147	spin_unlock_irqrestore(lock, *flags);
 148}
 149
 150/*
 151 *		list push/pop
 152 */
 153static void usbhsg_queue_push(struct usbhsg_uep *uep,
 154			      struct usbhsg_request *ureq)
 155{
 156	struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
 157	struct device *dev = usbhsg_gpriv_to_dev(gpriv);
 158	struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
 159
 160	/*
 161	 *********  assume under spin lock  *********
 162	 */
 163	list_del_init(&ureq->node);
 164	list_add_tail(&ureq->node, &uep->list);
 165	ureq->req.actual = 0;
 166	ureq->req.status = -EINPROGRESS;
 167
 168	dev_dbg(dev, "pipe %d : queue push (%d)\n",
 169		usbhs_pipe_number(pipe),
 170		ureq->req.length);
 171}
 172
 173static struct usbhsg_request *usbhsg_queue_get(struct usbhsg_uep *uep)
 174{
 175	/*
 176	 *********  assume under spin lock  *********
 177	 */
 178	if (list_empty(&uep->list))
 179		return NULL;
 180
 181	return list_entry(uep->list.next, struct usbhsg_request, node);
 182}
 183
 184#define usbhsg_queue_prepare(uep) __usbhsg_queue_handler(uep, 1);
 185#define usbhsg_queue_handle(uep)  __usbhsg_queue_handler(uep, 0);
 186static int __usbhsg_queue_handler(struct usbhsg_uep *uep, int prepare)
 187{
 188	struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
 189	struct device *dev = usbhsg_gpriv_to_dev(gpriv);
 190	struct usbhsg_request *ureq;
 191	spinlock_t *lock;
 192	unsigned long flags;
 193	int ret = 0;
 194
 195	if (!uep->handler) {
 196		dev_err(dev, "no handler function\n");
 197		return -EIO;
 198	}
 199
 200	/*
 201	 * CAUTION [*queue handler*]
 202	 *
 203	 * This function will be called for start/restart queue operation.
 204	 * OTOH the most much worry for USB driver is spinlock nest.
 205	 * Specially it are
 206	 *   - usb_ep_ops  :: queue
 207	 *   - usb_request :: complete
 208	 *
 209	 * But the caller of this function need not care about spinlock.
 210	 * This function is using usbhsg_trylock for it.
 211	 * if "is_locked" is 1, this mean this function lock it.
 212	 * but if it is 0, this mean it is already under spin lock.
 213	 * see also
 214	 *   CAUTION [*endpoint queue*]
 215	 *   CAUTION [*request complete*]
 216	 */
 217
 218	/******************  spin try lock *******************/
 219	lock = usbhsg_trylock(gpriv, &flags);
 220
 221	ureq = usbhsg_queue_get(uep);
 222	if (ureq) {
 223		if (prepare)
 224			ret = uep->handler->prepare(uep, ureq);
 225		else
 226			ret = uep->handler->try_run(uep, ureq);
 227	}
 228	usbhsg_unlock(lock, &flags);
 229	/********************  spin unlock ******************/
 230
 231	return ret;
 232}
 233
 234static void usbhsg_queue_pop(struct usbhsg_uep *uep,
 235			     struct usbhsg_request *ureq,
 236			     int status)
 237{
 238	struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
 239	struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
 240	struct device *dev = usbhsg_gpriv_to_dev(gpriv);
 241
 242	/*
 243	 *********  assume under spin lock  *********
 244	 */
 245
 246	/*
 247	 * CAUTION [*request complete*]
 248	 *
 249	 * There is a possibility not to be called in correct order
 250	 * if "complete" is called without spinlock.
 251	 *
 252	 * So, this function assume it is under spinlock,
 253	 * and call usb_request :: complete.
 254	 *
 255	 * But this "complete" will push next usb_request.
 256	 * It mean "usb_ep_ops :: queue" which is using spinlock is called
 257	 * under spinlock.
 258	 *
 259	 * To avoid dead-lock, this driver is using usbhsg_trylock.
 260	 *   CAUTION [*endpoint queue*]
 261	 *   CAUTION [*queue handler*]
 262	 */
 263
 264	dev_dbg(dev, "pipe %d : queue pop\n", usbhs_pipe_number(pipe));
 265
 266	list_del_init(&ureq->node);
 267
 268	ureq->req.status = status;
 269	ureq->req.complete(&uep->ep, &ureq->req);
 270
 271	/* more request ? */
 272	if (0 == status)
 273		usbhsg_queue_prepare(uep);
 274}
 275
 276/*
 277 *		irq enable/disable function
 278 */
 279#define usbhsg_irq_callback_ctrl(uep, status, enable)			\
 280	({								\
 281		struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);	\
 282		struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);	\
 283		struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);	\
 284		struct usbhs_mod *mod = usbhs_mod_get_current(priv);	\
 285		if (!mod)						\
 286			return;						\
 287		if (enable)						\
 288			mod->irq_##status |= (1 << usbhs_pipe_number(pipe)); \
 289		else							\
 290			mod->irq_##status &= ~(1 << usbhs_pipe_number(pipe)); \
 291		usbhs_irq_callback_update(priv, mod);			\
 292	})
 293
 294static void usbhsg_irq_empty_ctrl(struct usbhsg_uep *uep, int enable)
 295{
 296	usbhsg_irq_callback_ctrl(uep, bempsts, enable);
 297}
 298
 299static void usbhsg_irq_ready_ctrl(struct usbhsg_uep *uep, int enable)
 300{
 301	usbhsg_irq_callback_ctrl(uep, brdysts, enable);
 302}
 303
 304/*
 305 *		handler function
 306 */
 307static int usbhsg_try_run_ctrl_stage_end(struct usbhsg_uep *uep,
 308					 struct usbhsg_request *ureq)
 309{
 310	struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
 311
 312	/*
 313	 *********  assume under spin lock  *********
 314	 */
 315
 316	usbhs_dcp_control_transfer_done(pipe);
 317	usbhsg_queue_pop(uep, ureq, 0);
 318
 319	return 0;
 320}
 321
 322static int usbhsg_try_run_send_packet(struct usbhsg_uep *uep,
 323				      struct usbhsg_request *ureq)
 324{
 325	struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
 326	struct usb_request *req = &ureq->req;
 327	struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
 328	struct device *dev = usbhsg_gpriv_to_dev(gpriv);
 329	void *buf;
 330	int remainder, send;
 331	int is_done = 0;
 332	int enable;
 333	int maxp;
 334
 335	/*
 336	 *********  assume under spin lock  *********
 337	 */
 338
 339	maxp		= usbhs_pipe_get_maxpacket(pipe);
 340	buf		= req->buf    + req->actual;
 341	remainder	= req->length - req->actual;
 342
 343	send = usbhs_fifo_write(pipe, buf, remainder);
 344
 345	/*
 346	 * send < 0 : pipe busy
 347	 * send = 0 : send zero packet
 348	 * send > 0 : send data
 349	 *
 350	 * send <= max_packet
 351	 */
 352	if (send > 0)
 353		req->actual += send;
 354
 355	/* send all packet ? */
 356	if (send < remainder)
 357		is_done = 0;		/* there are remainder data */
 358	else if (send < maxp)
 359		is_done = 1;		/* short packet */
 360	else
 361		is_done = !req->zero;	/* send zero packet ? */
 362
 363	dev_dbg(dev, "  send %d (%d/ %d/ %d/ %d)\n",
 364		usbhs_pipe_number(pipe),
 365		remainder, send, is_done, req->zero);
 366
 367	/*
 368	 * enable interrupt and send again in irq handler
 369	 * if it still have remainder data which should be sent.
 370	 */
 371	enable = !is_done;
 372	uep->handler->irq_mask(uep, enable);
 373
 374	/*
 375	 * usbhs_fifo_enable execute
 376	 *  - after callback_update,
 377	 *  - before queue_pop / stage_end
 378	 */
 379	usbhs_fifo_enable(pipe);
 380
 381	/*
 382	 * all data were sent ?
 383	 */
 384	if (is_done) {
 385		/* it care below call in
 386		   "function mode" */
 387		if (usbhsg_is_dcp(uep))
 388			usbhs_dcp_control_transfer_done(pipe);
 389
 390		usbhsg_queue_pop(uep, ureq, 0);
 391	}
 392
 393	return 0;
 394}
 395
 396static int usbhsg_prepare_send_packet(struct usbhsg_uep *uep,
 397				      struct usbhsg_request *ureq)
 398{
 399	struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
 400
 401	/*
 402	 *********  assume under spin lock  *********
 403	 */
 404
 405	usbhs_fifo_prepare_write(pipe);
 406	usbhsg_try_run_send_packet(uep, ureq);
 407
 408	return 0;
 409}
 410
 411static int usbhsg_try_run_receive_packet(struct usbhsg_uep *uep,
 412					 struct usbhsg_request *ureq)
 413{
 414	struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
 415	struct usb_request *req = &ureq->req;
 416	struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
 417	struct device *dev = usbhsg_gpriv_to_dev(gpriv);
 418	void *buf;
 419	int maxp;
 420	int remainder, recv;
 421	int is_done = 0;
 422
 423	/*
 424	 *********  assume under spin lock  *********
 425	 */
 426
 427	maxp		= usbhs_pipe_get_maxpacket(pipe);
 428	buf		= req->buf    + req->actual;
 429	remainder	= req->length - req->actual;
 430
 431	recv = usbhs_fifo_read(pipe, buf, remainder);
 432	/*
 433	 * recv < 0  : pipe busy
 434	 * recv >= 0 : receive data
 435	 *
 436	 * recv <= max_packet
 437	 */
 438	if (recv < 0)
 439		return -EBUSY;
 440
 441	/* update parameters */
 442	req->actual += recv;
 443
 444	if ((recv == remainder) ||	/* receive all data */
 445	    (recv < maxp))		/* short packet */
 446		is_done = 1;
 447
 448	dev_dbg(dev, "  recv %d (%d/ %d/ %d/ %d)\n",
 449		usbhs_pipe_number(pipe),
 450		remainder, recv, is_done, req->zero);
 451
 452	/* read all data ? */
 453	if (is_done) {
 454		int disable = 0;
 455
 456		uep->handler->irq_mask(uep, disable);
 457		usbhs_fifo_disable(pipe);
 458		usbhsg_queue_pop(uep, ureq, 0);
 459	}
 460
 461	return 0;
 462}
 463
 464static int usbhsg_prepare_receive_packet(struct usbhsg_uep *uep,
 465					 struct usbhsg_request *ureq)
 466{
 467	struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
 468	int enable = 1;
 469	int ret;
 470
 471	/*
 472	 *********  assume under spin lock  *********
 473	 */
 474
 475	ret = usbhs_fifo_prepare_read(pipe);
 476	if (ret < 0)
 477		return ret;
 478
 479	/*
 480	 * data will be read in interrupt handler
 481	 */
 482	uep->handler->irq_mask(uep, enable);
 483
 484	return ret;
 485}
 486
 487static struct usbhsg_pipe_handle usbhsg_handler_send_by_empty = {
 488	.prepare	= usbhsg_prepare_send_packet,
 489	.try_run	= usbhsg_try_run_send_packet,
 490	.irq_mask	= usbhsg_irq_empty_ctrl,
 491};
 492
 493static struct usbhsg_pipe_handle usbhsg_handler_send_by_ready = {
 494	.prepare	= usbhsg_prepare_send_packet,
 495	.try_run	= usbhsg_try_run_send_packet,
 496	.irq_mask	= usbhsg_irq_ready_ctrl,
 497};
 498
 499static struct usbhsg_pipe_handle usbhsg_handler_recv_by_ready = {
 500	.prepare	= usbhsg_prepare_receive_packet,
 501	.try_run	= usbhsg_try_run_receive_packet,
 502	.irq_mask	= usbhsg_irq_ready_ctrl,
 503};
 504
 505static struct usbhsg_pipe_handle usbhsg_handler_ctrl_stage_end = {
 506	.prepare	= usbhsg_try_run_ctrl_stage_end,
 507	.try_run	= usbhsg_try_run_ctrl_stage_end,
 508};
 509
 510/*
 511 * DCP pipe can NOT use "ready interrupt" for "send"
 512 * it should use "empty" interrupt.
 513 * see
 514 *   "Operation" - "Interrupt Function" - "BRDY Interrupt"
 515 *
 516 * on the other hand, normal pipe can use "ready interrupt" for "send"
 517 * even though it is single/double buffer
 518 */
 519#define usbhsg_handler_send_ctrl	usbhsg_handler_send_by_empty
 520#define usbhsg_handler_recv_ctrl	usbhsg_handler_recv_by_ready
 521
 522#define usbhsg_handler_send_packet	usbhsg_handler_send_by_ready
 523#define usbhsg_handler_recv_packet	usbhsg_handler_recv_by_ready
 524
 525/*
 526 *		USB_TYPE_STANDARD / clear feature functions
 527 */
 528static int usbhsg_recip_handler_std_control_done(struct usbhs_priv *priv,
 529						 struct usbhsg_uep *uep,
 530						 struct usb_ctrlrequest *ctrl)
 531{
 532	struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
 533	struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
 534	struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
 535
 536	usbhs_dcp_control_transfer_done(pipe);
 537
 538	return 0;
 539}
 540
 541static int usbhsg_recip_handler_std_clear_endpoint(struct usbhs_priv *priv,
 542						   struct usbhsg_uep *uep,
 543						   struct usb_ctrlrequest *ctrl)
 544{
 545	struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
 546	struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
 547
 548	if (!usbhsg_status_has(gpriv, USBHSG_STATUS_WEDGE)) {
 549		usbhs_fifo_disable(pipe);
 550		usbhs_pipe_clear_sequence(pipe);
 551		usbhs_fifo_enable(pipe);
 552	}
 553
 554	usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
 555
 556	usbhsg_queue_prepare(uep);
 557
 558	return 0;
 559}
 560
 561struct usbhsg_recip_handle req_clear_feature = {
 562	.name		= "clear feature",
 563	.device		= usbhsg_recip_handler_std_control_done,
 564	.interface	= usbhsg_recip_handler_std_control_done,
 565	.endpoint	= usbhsg_recip_handler_std_clear_endpoint,
 566};
 567
 568/*
 569 *		USB_TYPE handler
 570 */
 571static int usbhsg_recip_run_handle(struct usbhs_priv *priv,
 572				   struct usbhsg_recip_handle *handler,
 573				   struct usb_ctrlrequest *ctrl)
 574{
 575	struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
 576	struct device *dev = usbhsg_gpriv_to_dev(gpriv);
 577	struct usbhsg_uep *uep;
 578	int recip = ctrl->bRequestType & USB_RECIP_MASK;
 579	int nth = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK;
 580	int ret;
 581	int (*func)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
 582		    struct usb_ctrlrequest *ctrl);
 583	char *msg;
 584
 585	uep = usbhsg_gpriv_to_nth_uep(gpriv, nth);
 586	if (!usbhsg_uep_to_pipe(uep)) {
 587		dev_err(dev, "wrong recip request\n");
 588		return -EINVAL;
 589	}
 590
 591	switch (recip) {
 592	case USB_RECIP_DEVICE:
 593		msg	= "DEVICE";
 594		func	= handler->device;
 595		break;
 596	case USB_RECIP_INTERFACE:
 597		msg	= "INTERFACE";
 598		func	= handler->interface;
 599		break;
 600	case USB_RECIP_ENDPOINT:
 601		msg	= "ENDPOINT";
 602		func	= handler->endpoint;
 603		break;
 604	default:
 605		dev_warn(dev, "unsupported RECIP(%d)\n", recip);
 606		func = NULL;
 607		ret = -EINVAL;
 608	}
 609
 610	if (func) {
 611		dev_dbg(dev, "%s (pipe %d :%s)\n", handler->name, nth, msg);
 612		ret = func(priv, uep, ctrl);
 613	}
 614
 615	return ret;
 616}
 617
 618/*
 619 *		irq functions
 620 *
 621 * it will be called from usbhs_interrupt
 622 */
 623static int usbhsg_irq_dev_state(struct usbhs_priv *priv,
 624				struct usbhs_irq_state *irq_state)
 625{
 626	struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
 627	struct device *dev = usbhsg_gpriv_to_dev(gpriv);
 628
 629	gpriv->gadget.speed = usbhs_status_get_usb_speed(irq_state);
 630
 631	dev_dbg(dev, "state = %x : speed : %d\n",
 632		usbhs_status_get_device_state(irq_state),
 633		gpriv->gadget.speed);
 634
 635	return 0;
 636}
 637
 638static int usbhsg_irq_ctrl_stage(struct usbhs_priv *priv,
 639				 struct usbhs_irq_state *irq_state)
 640{
 641	struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
 642	struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
 643	struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
 644	struct device *dev = usbhsg_gpriv_to_dev(gpriv);
 645	struct usb_ctrlrequest ctrl;
 646	struct usbhsg_recip_handle *recip_handler = NULL;
 647	int stage = usbhs_status_get_ctrl_stage(irq_state);
 648	int ret = 0;
 649
 650	dev_dbg(dev, "stage = %d\n", stage);
 651
 652	/*
 653	 * see Manual
 654	 *
 655	 *  "Operation"
 656	 *  - "Interrupt Function"
 657	 *    - "Control Transfer Stage Transition Interrupt"
 658	 *      - Fig. "Control Transfer Stage Transitions"
 659	 */
 660
 661	switch (stage) {
 662	case READ_DATA_STAGE:
 663		dcp->handler = &usbhsg_handler_send_ctrl;
 664		break;
 665	case WRITE_DATA_STAGE:
 666		dcp->handler = &usbhsg_handler_recv_ctrl;
 667		break;
 668	case NODATA_STATUS_STAGE:
 669		dcp->handler = &usbhsg_handler_ctrl_stage_end;
 670		break;
 671	default:
 672		return ret;
 673	}
 674
 675	/*
 676	 * get usb request
 677	 */
 678	usbhs_usbreq_get_val(priv, &ctrl);
 679
 680	switch (ctrl.bRequestType & USB_TYPE_MASK) {
 681	case USB_TYPE_STANDARD:
 682		switch (ctrl.bRequest) {
 683		case USB_REQ_CLEAR_FEATURE:
 684			recip_handler = &req_clear_feature;
 685			break;
 686		}
 687	}
 688
 689	/*
 690	 * setup stage / run recip
 691	 */
 692	if (recip_handler)
 693		ret = usbhsg_recip_run_handle(priv, recip_handler, &ctrl);
 694	else
 695		ret = gpriv->driver->setup(&gpriv->gadget, &ctrl);
 696
 697	if (ret < 0)
 698		usbhs_fifo_stall(pipe);
 699
 700	return ret;
 701}
 702
 703static int usbhsg_irq_empty(struct usbhs_priv *priv,
 704			    struct usbhs_irq_state *irq_state)
 705{
 706	struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
 707	struct usbhsg_uep *uep;
 708	struct usbhs_pipe *pipe;
 709	struct device *dev = usbhsg_gpriv_to_dev(gpriv);
 710	int i, ret;
 711
 712	if (!irq_state->bempsts) {
 713		dev_err(dev, "debug %s !!\n", __func__);
 714		return -EIO;
 715	}
 716
 717	dev_dbg(dev, "irq empty [0x%04x]\n", irq_state->bempsts);
 718
 719	/*
 720	 * search interrupted "pipe"
 721	 * not "uep".
 722	 */
 723	usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
 724		if (!(irq_state->bempsts & (1 << i)))
 725			continue;
 726
 727		uep	= usbhsg_pipe_to_uep(pipe);
 728		ret	= usbhsg_queue_handle(uep);
 729		if (ret < 0)
 730			dev_err(dev, "send error %d : %d\n", i, ret);
 731	}
 732
 733	return 0;
 734}
 735
 736static int usbhsg_irq_ready(struct usbhs_priv *priv,
 737			    struct usbhs_irq_state *irq_state)
 738{
 739	struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
 740	struct usbhsg_uep *uep;
 741	struct usbhs_pipe *pipe;
 742	struct device *dev = usbhsg_gpriv_to_dev(gpriv);
 743	int i, ret;
 744
 745	if (!irq_state->brdysts) {
 746		dev_err(dev, "debug %s !!\n", __func__);
 747		return -EIO;
 748	}
 749
 750	dev_dbg(dev, "irq ready [0x%04x]\n", irq_state->brdysts);
 751
 752	/*
 753	 * search interrupted "pipe"
 754	 * not "uep".
 755	 */
 756	usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
 757		if (!(irq_state->brdysts & (1 << i)))
 758			continue;
 759
 760		uep	= usbhsg_pipe_to_uep(pipe);
 761		ret	= usbhsg_queue_handle(uep);
 762		if (ret < 0)
 763			dev_err(dev, "receive error %d : %d\n", i, ret);
 764	}
 765
 766	return 0;
 767}
 768
 769/*
 770 *
 771 *		usb_dcp_ops
 772 *
 773 */
 774static int usbhsg_dcp_enable(struct usbhsg_uep *uep)
 775{
 776	struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
 777	struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
 778	struct usbhs_pipe *pipe;
 779
 780	/*
 781	 *********  assume under spin lock  *********
 782	 */
 783
 784	pipe = usbhs_dcp_malloc(priv);
 785	if (!pipe)
 786		return -EIO;
 787
 788	uep->pipe		= pipe;
 789	uep->pipe->mod_private	= uep;
 790	INIT_LIST_HEAD(&uep->list);
 791
 792	return 0;
 793}
 794
 795#define usbhsg_dcp_disable usbhsg_pipe_disable
 796static int usbhsg_pipe_disable(struct usbhsg_uep *uep)
 797{
 798	struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
 799	struct usbhsg_request *ureq;
 800	int disable = 0;
 801
 802	/*
 803	 *********  assume under spin lock  *********
 804	 */
 805
 806	usbhs_fifo_disable(pipe);
 807
 808	/*
 809	 * disable pipe irq
 810	 */
 811	usbhsg_irq_empty_ctrl(uep, disable);
 812	usbhsg_irq_ready_ctrl(uep, disable);
 813
 814	while (1) {
 815		ureq = usbhsg_queue_get(uep);
 816		if (!ureq)
 817			break;
 818
 819		usbhsg_queue_pop(uep, ureq, -ECONNRESET);
 820	}
 821
 822	return 0;
 823}
 824
 825static void usbhsg_uep_init(struct usbhsg_gpriv *gpriv)
 826{
 827	int i;
 828	struct usbhsg_uep *uep;
 829
 830	usbhsg_for_each_uep_with_dcp(uep, gpriv, i)
 831		uep->pipe = NULL;
 832}
 833
 834/*
 835 *
 836 *		usb_ep_ops
 837 *
 838 */
 839static int usbhsg_ep_enable(struct usb_ep *ep,
 840			 const struct usb_endpoint_descriptor *desc)
 841{
 842	struct usbhsg_uep *uep   = usbhsg_ep_to_uep(ep);
 843	struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
 844	struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
 845	struct usbhs_pipe *pipe;
 846	spinlock_t *lock;
 847	unsigned long flags;
 848	int ret = -EIO;
 849
 850	/*
 851	 * if it already have pipe,
 852	 * nothing to do
 853	 */
 854	if (uep->pipe)
 855		return 0;
 856
 857	/********************  spin lock ********************/
 858	lock = usbhsg_trylock(gpriv, &flags);
 859
 860	pipe = usbhs_pipe_malloc(priv, desc);
 861	if (pipe) {
 862		uep->pipe		= pipe;
 863		pipe->mod_private	= uep;
 864		INIT_LIST_HEAD(&uep->list);
 865
 866		if (usb_endpoint_dir_in(desc))
 867			uep->handler = &usbhsg_handler_send_packet;
 868		else
 869			uep->handler = &usbhsg_handler_recv_packet;
 870
 871		ret = 0;
 872	}
 873
 874	usbhsg_unlock(lock, &flags);
 875	/********************  spin unlock ******************/
 876
 877	return ret;
 878}
 879
 880static int usbhsg_ep_disable(struct usb_ep *ep)
 881{
 882	struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
 883	struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
 884	spinlock_t *lock;
 885	unsigned long flags;
 886	int ret;
 887
 888	/********************  spin lock ********************/
 889	lock = usbhsg_trylock(gpriv, &flags);
 890
 891	ret = usbhsg_pipe_disable(uep);
 892
 893	usbhsg_unlock(lock, &flags);
 894	/********************  spin unlock ******************/
 895
 896	return ret;
 897}
 898
 899static struct usb_request *usbhsg_ep_alloc_request(struct usb_ep *ep,
 900						   gfp_t gfp_flags)
 901{
 902	struct usbhsg_request *ureq;
 903
 904	ureq = kzalloc(sizeof *ureq, gfp_flags);
 905	if (!ureq)
 906		return NULL;
 907
 908	INIT_LIST_HEAD(&ureq->node);
 909	return &ureq->req;
 910}
 911
 912static void usbhsg_ep_free_request(struct usb_ep *ep,
 913				   struct usb_request *req)
 914{
 915	struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
 916
 917	WARN_ON(!list_empty(&ureq->node));
 918	kfree(ureq);
 919}
 920
 921static int usbhsg_ep_queue(struct usb_ep *ep, struct usb_request *req,
 922			  gfp_t gfp_flags)
 923{
 924	struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
 925	struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
 926	struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
 927	struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
 928	spinlock_t *lock;
 929	unsigned long flags;
 930	int ret = 0;
 931
 932	/*
 933	 * CAUTION [*endpoint queue*]
 934	 *
 935	 * This function will be called from usb_request :: complete
 936	 * or usb driver timing.
 937	 * If this function is called from usb_request :: complete,
 938	 * it is already under spinlock on this driver.
 939	 * but it is called frm usb driver, this function should call spinlock.
 940	 *
 941	 * This function is using usbshg_trylock to solve this issue.
 942	 * if "is_locked" is 1, this mean this function lock it.
 943	 * but if it is 0, this mean it is already under spin lock.
 944	 * see also
 945	 *   CAUTION [*queue handler*]
 946	 *   CAUTION [*request complete*]
 947	 */
 948
 949	/********************  spin lock ********************/
 950	lock = usbhsg_trylock(gpriv, &flags);
 951
 952	/* param check */
 953	if (usbhsg_is_not_connected(gpriv)	||
 954	    unlikely(!gpriv->driver)		||
 955	    unlikely(!pipe))
 956		ret = -ESHUTDOWN;
 957	else
 958		usbhsg_queue_push(uep, ureq);
 959
 960	usbhsg_unlock(lock, &flags);
 961	/********************  spin unlock ******************/
 962
 963	usbhsg_queue_prepare(uep);
 964
 965	return ret;
 966}
 967
 968static int usbhsg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
 969{
 970	struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
 971	struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
 972	struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
 973	spinlock_t *lock;
 974	unsigned long flags;
 975
 976	/*
 977	 * see
 978	 *   CAUTION [*queue handler*]
 979	 *   CAUTION [*endpoint queue*]
 980	 *   CAUTION [*request complete*]
 981	 */
 982
 983	/********************  spin lock ********************/
 984	lock = usbhsg_trylock(gpriv, &flags);
 985
 986	usbhsg_queue_pop(uep, ureq, -ECONNRESET);
 987
 988	usbhsg_unlock(lock, &flags);
 989	/********************  spin unlock ******************/
 990
 991	return 0;
 992}
 993
 994static int __usbhsg_ep_set_halt_wedge(struct usb_ep *ep, int halt, int wedge)
 995{
 996	struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
 997	struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
 998	struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
 999	struct device *dev = usbhsg_gpriv_to_dev(gpriv);
1000	spinlock_t *lock;
1001	unsigned long flags;
1002	int ret = -EAGAIN;
1003
1004	/*
1005	 * see
1006	 *   CAUTION [*queue handler*]
1007	 *   CAUTION [*endpoint queue*]
1008	 *   CAUTION [*request complete*]
1009	 */
1010
1011	/********************  spin lock ********************/
1012	lock = usbhsg_trylock(gpriv, &flags);
1013	if (!usbhsg_queue_get(uep)) {
1014
1015		dev_dbg(dev, "set halt %d (pipe %d)\n",
1016			halt, usbhs_pipe_number(pipe));
1017
1018		if (halt)
1019			usbhs_fifo_stall(pipe);
1020		else
1021			usbhs_fifo_disable(pipe);
1022
1023		if (halt && wedge)
1024			usbhsg_status_set(gpriv, USBHSG_STATUS_WEDGE);
1025		else
1026			usbhsg_status_clr(gpriv, USBHSG_STATUS_WEDGE);
1027
1028		ret = 0;
1029	}
1030
1031	usbhsg_unlock(lock, &flags);
1032	/********************  spin unlock ******************/
1033
1034	return ret;
1035}
1036
1037static int usbhsg_ep_set_halt(struct usb_ep *ep, int value)
1038{
1039	return __usbhsg_ep_set_halt_wedge(ep, value, 0);
1040}
1041
1042static int usbhsg_ep_set_wedge(struct usb_ep *ep)
1043{
1044	return __usbhsg_ep_set_halt_wedge(ep, 1, 1);
1045}
1046
1047static struct usb_ep_ops usbhsg_ep_ops = {
1048	.enable		= usbhsg_ep_enable,
1049	.disable	= usbhsg_ep_disable,
1050
1051	.alloc_request	= usbhsg_ep_alloc_request,
1052	.free_request	= usbhsg_ep_free_request,
1053
1054	.queue		= usbhsg_ep_queue,
1055	.dequeue	= usbhsg_ep_dequeue,
1056
1057	.set_halt	= usbhsg_ep_set_halt,
1058	.set_wedge	= usbhsg_ep_set_wedge,
1059};
1060
1061/*
1062 *		usb module start/end
1063 */
1064static int usbhsg_try_start(struct usbhs_priv *priv, u32 status)
1065{
1066	struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
1067	struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
1068	struct usbhs_mod *mod = usbhs_mod_get_current(priv);
1069	struct device *dev = usbhs_priv_to_dev(priv);
1070	spinlock_t *lock;
1071	unsigned long flags;
1072
1073	/********************  spin lock ********************/
1074	lock = usbhsg_trylock(gpriv, &flags);
1075
1076	/*
1077	 * enable interrupt and systems if ready
1078	 */
1079	usbhsg_status_set(gpriv, status);
1080	if (!(usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
1081	      usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD)))
1082		goto usbhsg_try_start_unlock;
1083
1084	dev_dbg(dev, "start gadget\n");
1085
1086	/*
1087	 * pipe initialize and enable DCP
1088	 */
1089	usbhs_pipe_init(priv);
1090	usbhsg_uep_init(gpriv);
1091	usbhsg_dcp_enable(dcp);
1092
1093	/*
1094	 * system config enble
1095	 * - HI speed
1096	 * - function
1097	 * - usb module
1098	 */
1099	usbhs_sys_hispeed_ctrl(priv, 1);
1100	usbhs_sys_function_ctrl(priv, 1);
1101	usbhs_sys_usb_ctrl(priv, 1);
1102
1103	/*
1104	 * enable irq callback
1105	 */
1106	mod->irq_dev_state	= usbhsg_irq_dev_state;
1107	mod->irq_ctrl_stage	= usbhsg_irq_ctrl_stage;
1108	mod->irq_empty		= usbhsg_irq_empty;
1109	mod->irq_ready		= usbhsg_irq_ready;
1110	mod->irq_bempsts	= 0;
1111	mod->irq_brdysts	= 0;
1112	usbhs_irq_callback_update(priv, mod);
1113
1114usbhsg_try_start_unlock:
1115	usbhsg_unlock(lock, &flags);
1116	/********************  spin unlock ********************/
1117
1118	return 0;
1119}
1120
1121static int usbhsg_try_stop(struct usbhs_priv *priv, u32 status)
1122{
1123	struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
1124	struct usbhs_mod *mod = usbhs_mod_get_current(priv);
1125	struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
1126	struct device *dev = usbhs_priv_to_dev(priv);
1127	spinlock_t *lock;
1128	unsigned long flags;
1129
1130	/********************  spin lock ********************/
1131	lock = usbhsg_trylock(gpriv, &flags);
1132
1133	/*
1134	 * disable interrupt and systems if 1st try
1135	 */
1136	usbhsg_status_clr(gpriv, status);
1137	if (!usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
1138	    !usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD))
1139		goto usbhsg_try_stop_unlock;
1140
1141	/* disable all irq */
1142	mod->irq_dev_state	= NULL;
1143	mod->irq_ctrl_stage	= NULL;
1144	mod->irq_empty		= NULL;
1145	mod->irq_ready		= NULL;
1146	mod->irq_bempsts	= 0;
1147	mod->irq_brdysts	= 0;
1148	usbhs_irq_callback_update(priv, mod);
1149
1150	usbhsg_dcp_disable(dcp);
1151
1152	gpriv->gadget.speed = USB_SPEED_UNKNOWN;
1153
1154	/* disable sys */
1155	usbhs_sys_hispeed_ctrl(priv, 0);
1156	usbhs_sys_function_ctrl(priv, 0);
1157	usbhs_sys_usb_ctrl(priv, 0);
1158
1159	usbhsg_unlock(lock, &flags);
1160	/********************  spin unlock ********************/
1161
1162	if (gpriv->driver &&
1163	    gpriv->driver->disconnect)
1164		gpriv->driver->disconnect(&gpriv->gadget);
1165
1166	dev_dbg(dev, "stop gadget\n");
1167
1168	return 0;
1169
1170usbhsg_try_stop_unlock:
1171	usbhsg_unlock(lock, &flags);
1172
1173	return 0;
1174}
1175
1176/*
1177 *
1178 *		linux usb function
1179 *
1180 */
1181struct usbhsg_gpriv *the_controller;
1182int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
1183			    int (*bind)(struct usb_gadget *))
1184{
1185	struct usbhsg_gpriv *gpriv = the_controller;
1186	struct usbhs_priv *priv;
1187	struct device *dev;
1188	int ret;
1189
1190	if (!bind		||
1191	    !driver		||
1192	    !driver->setup	||
1193	    driver->speed != USB_SPEED_HIGH)
1194		return -EINVAL;
1195	if (!gpriv)
1196		return -ENODEV;
1197	if (gpriv->driver)
1198		return -EBUSY;
1199
1200	dev  = usbhsg_gpriv_to_dev(gpriv);
1201	priv = usbhsg_gpriv_to_priv(gpriv);
1202
1203	/* first hook up the driver ... */
1204	gpriv->driver = driver;
1205	gpriv->gadget.dev.driver = &driver->driver;
1206
1207	ret = device_add(&gpriv->gadget.dev);
1208	if (ret) {
1209		dev_err(dev, "device_add error %d\n", ret);
1210		goto add_fail;
1211	}
1212
1213	ret = bind(&gpriv->gadget);
1214	if (ret) {
1215		dev_err(dev, "bind to driver %s error %d\n",
1216			driver->driver.name, ret);
1217		goto bind_fail;
1218	}
1219
1220	dev_dbg(dev, "bind %s\n", driver->driver.name);
1221
1222	return usbhsg_try_start(priv, USBHSG_STATUS_REGISTERD);
1223
1224bind_fail:
1225	device_del(&gpriv->gadget.dev);
1226add_fail:
1227	gpriv->driver = NULL;
1228	gpriv->gadget.dev.driver = NULL;
1229
1230	return ret;
1231}
1232EXPORT_SYMBOL(usb_gadget_probe_driver);
1233
1234int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1235{
1236	struct usbhsg_gpriv *gpriv = the_controller;
1237	struct usbhs_priv *priv;
1238	struct device *dev = usbhsg_gpriv_to_dev(gpriv);
1239
1240	if (!gpriv)
1241		return -ENODEV;
1242
1243	if (!driver		||
1244	    !driver->unbind	||
1245	    driver != gpriv->driver)
1246		return -EINVAL;
1247
1248	dev  = usbhsg_gpriv_to_dev(gpriv);
1249	priv = usbhsg_gpriv_to_priv(gpriv);
1250
1251	usbhsg_try_stop(priv, USBHSG_STATUS_REGISTERD);
1252	device_del(&gpriv->gadget.dev);
1253	gpriv->driver = NULL;
1254
1255	if (driver->disconnect)
1256		driver->disconnect(&gpriv->gadget);
1257
1258	driver->unbind(&gpriv->gadget);
1259	dev_dbg(dev, "unbind %s\n", driver->driver.name);
1260
1261	return 0;
1262}
1263EXPORT_SYMBOL(usb_gadget_unregister_driver);
1264
1265/*
1266 *		usb gadget ops
1267 */
1268static int usbhsg_get_frame(struct usb_gadget *gadget)
1269{
1270	struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
1271	struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
1272
1273	return usbhs_frame_get_num(priv);
1274}
1275
1276static struct usb_gadget_ops usbhsg_gadget_ops = {
1277	.get_frame		= usbhsg_get_frame,
1278};
1279
1280static int usbhsg_start(struct usbhs_priv *priv)
1281{
1282	return usbhsg_try_start(priv, USBHSG_STATUS_STARTED);
1283}
1284
1285static int usbhsg_stop(struct usbhs_priv *priv)
1286{
1287	return usbhsg_try_stop(priv, USBHSG_STATUS_STARTED);
1288}
1289
1290int __devinit usbhs_mod_gadget_probe(struct usbhs_priv *priv)
1291{
1292	struct usbhsg_gpriv *gpriv;
1293	struct usbhsg_uep *uep;
1294	struct device *dev = usbhs_priv_to_dev(priv);
1295	int pipe_size = usbhs_get_dparam(priv, pipe_size);
1296	int i;
1297
1298	gpriv = kzalloc(sizeof(struct usbhsg_gpriv), GFP_KERNEL);
1299	if (!gpriv) {
1300		dev_err(dev, "Could not allocate gadget priv\n");
1301		return -ENOMEM;
1302	}
1303
1304	uep = kzalloc(sizeof(struct usbhsg_uep) * pipe_size, GFP_KERNEL);
1305	if (!uep) {
1306		dev_err(dev, "Could not allocate ep\n");
1307		goto usbhs_mod_gadget_probe_err_gpriv;
1308	}
1309
1310	/*
1311	 * CAUTION
1312	 *
1313	 * There is no guarantee that it is possible to access usb module here.
1314	 * Don't accesses to it.
1315	 * The accesse will be enable after "usbhsg_start"
1316	 */
1317
1318	/*
1319	 * register itself
1320	 */
1321	usbhs_mod_register(priv, &gpriv->mod, USBHS_GADGET);
1322
1323	/* init gpriv */
1324	gpriv->mod.name		= "gadget";
1325	gpriv->mod.start	= usbhsg_start;
1326	gpriv->mod.stop		= usbhsg_stop;
1327	gpriv->uep		= uep;
1328	gpriv->uep_size		= pipe_size;
1329	usbhsg_status_init(gpriv);
1330
1331	/*
1332	 * init gadget
1333	 */
1334	device_initialize(&gpriv->gadget.dev);
1335	dev_set_name(&gpriv->gadget.dev, "gadget");
1336	gpriv->gadget.dev.parent	= dev;
1337	gpriv->gadget.name		= "renesas_usbhs_udc";
1338	gpriv->gadget.ops		= &usbhsg_gadget_ops;
1339	gpriv->gadget.is_dualspeed	= 1;
1340
1341	INIT_LIST_HEAD(&gpriv->gadget.ep_list);
1342
1343	/*
1344	 * init usb_ep
1345	 */
1346	usbhsg_for_each_uep_with_dcp(uep, gpriv, i) {
1347		uep->gpriv	= gpriv;
1348		snprintf(uep->ep_name, EP_NAME_SIZE, "ep%d", i);
1349
1350		uep->ep.name		= uep->ep_name;
1351		uep->ep.ops		= &usbhsg_ep_ops;
1352		INIT_LIST_HEAD(&uep->ep.ep_list);
1353		INIT_LIST_HEAD(&uep->list);
1354
1355		/* init DCP */
1356		if (usbhsg_is_dcp(uep)) {
1357			gpriv->gadget.ep0 = &uep->ep;
1358			uep->ep.maxpacket = 64;
1359		}
1360		/* init normal pipe */
1361		else {
1362			uep->ep.maxpacket = 512;
1363			list_add_tail(&uep->ep.ep_list, &gpriv->gadget.ep_list);
1364		}
1365	}
1366
1367	the_controller = gpriv;
1368
1369	dev_info(dev, "gadget probed\n");
1370
1371	return 0;
1372
1373usbhs_mod_gadget_probe_err_gpriv:
1374	kfree(gpriv);
1375
1376	return -ENOMEM;
1377}
1378
1379void __devexit usbhs_mod_gadget_remove(struct usbhs_priv *priv)
1380{
1381	struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
1382
1383	kfree(gpriv->uep);
1384	kfree(gpriv);
1385}