1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  drivers/usb/gadget/emxx_udc.c
4  *     EMXX FCD (Function Controller Driver) for USB.
5  *
6  *  Copyright (C) 2010 Renesas Electronics Corporation
7  */
8 
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/platform_device.h>
12 #include <linux/delay.h>
13 #include <linux/ioport.h>
14 #include <linux/slab.h>
15 #include <linux/errno.h>
16 #include <linux/list.h>
17 #include <linux/interrupt.h>
18 #include <linux/proc_fs.h>
19 #include <linux/clk.h>
20 #include <linux/ctype.h>
21 #include <linux/string.h>
22 #include <linux/dma-mapping.h>
23 #include <linux/workqueue.h>
24 #include <linux/device.h>
25 
26 #include <linux/usb/ch9.h>
27 #include <linux/usb/gadget.h>
28 
29 #include <linux/irq.h>
30 #include <linux/gpio.h>
31 
32 #include "emxx_udc.h"
33 
34 #define	DRIVER_DESC	"EMXX UDC driver"
35 #define	DMA_ADDR_INVALID	(~(dma_addr_t)0)
36 
37 static const char	driver_name[] = "emxx_udc";
38 static const char	driver_desc[] = DRIVER_DESC;
39 
40 /*===========================================================================*/
41 /* Prototype */
42 static void _nbu2ss_ep_dma_abort(struct nbu2ss_udc *, struct nbu2ss_ep *);
43 static void _nbu2ss_ep0_enable(struct nbu2ss_udc *);
44 /*static void _nbu2ss_ep0_disable(struct nbu2ss_udc *);*/
45 static void _nbu2ss_ep_done(struct nbu2ss_ep *, struct nbu2ss_req *, int);
46 static void _nbu2ss_set_test_mode(struct nbu2ss_udc *, u32 mode);
47 static void _nbu2ss_endpoint_toggle_reset(struct nbu2ss_udc *udc, u8 ep_adrs);
48 
49 static int _nbu2ss_pullup(struct nbu2ss_udc *, int);
50 static void _nbu2ss_fifo_flush(struct nbu2ss_udc *, struct nbu2ss_ep *);
51 
52 /*===========================================================================*/
53 /* Macro */
54 #define	_nbu2ss_zero_len_pkt(udc, epnum)	\
55 	_nbu2ss_ep_in_end(udc, epnum, 0, 0)
56 
57 /*===========================================================================*/
58 /* Global */
59 struct nbu2ss_udc udc_controller;
60 
61 /*-------------------------------------------------------------------------*/
62 /* Read */
63 static inline u32 _nbu2ss_readl(void *address)
64 {
65 	return __raw_readl(address);
66 }
67 
68 /*-------------------------------------------------------------------------*/
69 /* Write */
70 static inline void _nbu2ss_writel(void *address, u32 udata)
71 {
72 	__raw_writel(udata, address);
73 }
74 
75 /*-------------------------------------------------------------------------*/
76 /* Set Bit */
77 static inline void _nbu2ss_bitset(void *address, u32 udata)
78 {
79 	u32	reg_dt = __raw_readl(address) | (udata);
80 
81 	__raw_writel(reg_dt, address);
82 }
83 
84 /*-------------------------------------------------------------------------*/
85 /* Clear Bit */
86 static inline void _nbu2ss_bitclr(void *address, u32 udata)
87 {
88 	u32	reg_dt = __raw_readl(address) & ~(udata);
89 
90 	__raw_writel(reg_dt, address);
91 }
92 
93 #ifdef UDC_DEBUG_DUMP
94 /*-------------------------------------------------------------------------*/
95 static void _nbu2ss_dump_register(struct nbu2ss_udc *udc)
96 {
97 	int		i;
98 	u32 reg_data;
99 
100 	pr_info("=== %s()\n", __func__);
101 
102 	if (!udc) {
103 		pr_err("%s udc == NULL\n", __func__);
104 		return;
105 	}
106 
107 	spin_unlock(&udc->lock);
108 
109 	dev_dbg(&udc->dev, "\n-USB REG-\n");
110 	for (i = 0x0 ; i < USB_BASE_SIZE ; i += 16) {
111 		reg_data =   _nbu2ss_readl(
112 			(u32 *)IO_ADDRESS(USB_BASE_ADDRESS + i));
113 		dev_dbg(&udc->dev, "USB%04x =%08x", i, (int)reg_data);
114 
115 		reg_data =  _nbu2ss_readl(
116 			(u32 *)IO_ADDRESS(USB_BASE_ADDRESS + i + 4));
117 		dev_dbg(&udc->dev, " %08x", (int)reg_data);
118 
119 		reg_data =  _nbu2ss_readl(
120 			(u32 *)IO_ADDRESS(USB_BASE_ADDRESS + i + 8));
121 		dev_dbg(&udc->dev, " %08x", (int)reg_data);
122 
123 		reg_data =  _nbu2ss_readl(
124 			(u32 *)IO_ADDRESS(USB_BASE_ADDRESS + i + 12));
125 		dev_dbg(&udc->dev, " %08x\n", (int)reg_data);
126 	}
127 
128 	spin_lock(&udc->lock);
129 }
130 #endif /* UDC_DEBUG_DUMP */
131 
132 /*-------------------------------------------------------------------------*/
133 /* Endpoint 0 Callback (Complete) */
134 static void _nbu2ss_ep0_complete(struct usb_ep *_ep, struct usb_request *_req)
135 {
136 	u8		recipient;
137 	u16		selector;
138 	u32		test_mode;
139 	struct usb_ctrlrequest	*p_ctrl;
140 	struct nbu2ss_udc *udc;
141 
142 	if ((!_ep) || (!_req))
143 		return;
144 
145 	udc = (struct nbu2ss_udc *)_req->context;
146 	p_ctrl = &udc->ctrl;
147 	if ((p_ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
148 		if (p_ctrl->bRequest == USB_REQ_SET_FEATURE) {
149 			/*-------------------------------------------------*/
150 			/* SET_FEATURE */
151 			recipient = (u8)(p_ctrl->bRequestType & USB_RECIP_MASK);
152 			selector  = p_ctrl->wValue;
153 			if ((recipient == USB_RECIP_DEVICE) &&
154 			    (selector == USB_DEVICE_TEST_MODE)) {
155 				test_mode = (u32)(p_ctrl->wIndex >> 8);
156 				_nbu2ss_set_test_mode(udc, test_mode);
157 			}
158 		}
159 	}
160 }
161 
162 /*-------------------------------------------------------------------------*/
163 /* Initialization usb_request */
164 static void _nbu2ss_create_ep0_packet(
165 	struct nbu2ss_udc *udc,
166 	void *p_buf,
167 	unsigned length
168 )
169 {
170 	udc->ep0_req.req.buf		= p_buf;
171 	udc->ep0_req.req.length		= length;
172 	udc->ep0_req.req.dma		= 0;
173 	udc->ep0_req.req.zero		= TRUE;
174 	udc->ep0_req.req.complete	= _nbu2ss_ep0_complete;
175 	udc->ep0_req.req.status		= -EINPROGRESS;
176 	udc->ep0_req.req.context	= udc;
177 	udc->ep0_req.req.actual		= 0;
178 }
179 
180 /*-------------------------------------------------------------------------*/
181 /* Acquisition of the first address of RAM(FIFO) */
182 static u32 _nbu2ss_get_begin_ram_address(struct nbu2ss_udc *udc)
183 {
184 	u32		num, buf_type;
185 	u32		data, last_ram_adr, use_ram_size;
186 
187 	struct ep_regs *p_ep_regs;
188 
189 	last_ram_adr = (D_RAM_SIZE_CTRL / sizeof(u32)) * 2;
190 	use_ram_size = 0;
191 
192 	for (num = 0; num < NUM_ENDPOINTS - 1; num++) {
193 		p_ep_regs = &udc->p_regs->EP_REGS[num];
194 		data = _nbu2ss_readl(&p_ep_regs->EP_PCKT_ADRS);
195 		buf_type = _nbu2ss_readl(&p_ep_regs->EP_CONTROL) & EPN_BUF_TYPE;
196 		if (buf_type == 0) {
197 			/* Single Buffer */
198 			use_ram_size += (data & EPN_MPKT) / sizeof(u32);
199 		} else {
200 			/* Double Buffer */
201 			use_ram_size += ((data & EPN_MPKT) / sizeof(u32)) * 2;
202 		}
203 
204 		if ((data >> 16) > last_ram_adr)
205 			last_ram_adr = data >> 16;
206 	}
207 
208 	return last_ram_adr + use_ram_size;
209 }
210 
211 /*-------------------------------------------------------------------------*/
212 /* Construction of Endpoint */
213 static int _nbu2ss_ep_init(struct nbu2ss_udc *udc, struct nbu2ss_ep *ep)
214 {
215 	u32		num;
216 	u32		data;
217 	u32		begin_adrs;
218 
219 	if (ep->epnum == 0)
220 		return	-EINVAL;
221 
222 	num = ep->epnum - 1;
223 
224 	/*-------------------------------------------------------------*/
225 	/* RAM Transfer Address */
226 	begin_adrs = _nbu2ss_get_begin_ram_address(udc);
227 	data = (begin_adrs << 16) | ep->ep.maxpacket;
228 	_nbu2ss_writel(&udc->p_regs->EP_REGS[num].EP_PCKT_ADRS, data);
229 
230 	/*-------------------------------------------------------------*/
231 	/* Interrupt Enable */
232 	data = 1 << (ep->epnum + 8);
233 	_nbu2ss_bitset(&udc->p_regs->USB_INT_ENA, data);
234 
235 	/*-------------------------------------------------------------*/
236 	/* Endpoint Type(Mode) */
237 	/*   Bulk, Interrupt, ISO */
238 	switch (ep->ep_type) {
239 	case USB_ENDPOINT_XFER_BULK:
240 		data = EPN_BULK;
241 		break;
242 
243 	case USB_ENDPOINT_XFER_INT:
244 		data = EPN_BUF_SINGLE | EPN_INTERRUPT;
245 		break;
246 
247 	case USB_ENDPOINT_XFER_ISOC:
248 		data = EPN_ISO;
249 		break;
250 
251 	default:
252 		data = 0;
253 		break;
254 	}
255 
256 	_nbu2ss_bitset(&udc->p_regs->EP_REGS[num].EP_CONTROL, data);
257 	_nbu2ss_endpoint_toggle_reset(udc, (ep->epnum | ep->direct));
258 
259 	if (ep->direct == USB_DIR_OUT) {
260 		/*---------------------------------------------------------*/
261 		/* OUT */
262 		data = EPN_EN | EPN_BCLR | EPN_DIR0;
263 		_nbu2ss_bitset(&udc->p_regs->EP_REGS[num].EP_CONTROL, data);
264 
265 		data = EPN_ONAK | EPN_OSTL_EN | EPN_OSTL;
266 		_nbu2ss_bitclr(&udc->p_regs->EP_REGS[num].EP_CONTROL, data);
267 
268 		data = EPN_OUT_EN | EPN_OUT_END_EN;
269 		_nbu2ss_bitset(&udc->p_regs->EP_REGS[num].EP_INT_ENA, data);
270 	} else {
271 		/*---------------------------------------------------------*/
272 		/* IN */
273 		data = EPN_EN | EPN_BCLR | EPN_AUTO;
274 		_nbu2ss_bitset(&udc->p_regs->EP_REGS[num].EP_CONTROL, data);
275 
276 		data = EPN_ISTL;
277 		_nbu2ss_bitclr(&udc->p_regs->EP_REGS[num].EP_CONTROL, data);
278 
279 		data = EPN_IN_EN | EPN_IN_END_EN;
280 		_nbu2ss_bitset(&udc->p_regs->EP_REGS[num].EP_INT_ENA, data);
281 	}
282 
283 	return 0;
284 }
285 
286 /*-------------------------------------------------------------------------*/
287 /* Release of Endpoint */
288 static int _nbu2ss_epn_exit(struct nbu2ss_udc *udc, struct nbu2ss_ep *ep)
289 {
290 	u32		num;
291 	u32		data;
292 
293 	if ((ep->epnum == 0) || (udc->vbus_active == 0))
294 		return	-EINVAL;
295 
296 	num = ep->epnum - 1;
297 
298 	/*-------------------------------------------------------------*/
299 	/* RAM Transfer Address */
300 	_nbu2ss_writel(&udc->p_regs->EP_REGS[num].EP_PCKT_ADRS, 0);
301 
302 	/*-------------------------------------------------------------*/
303 	/* Interrupt Disable */
304 	data = 1 << (ep->epnum + 8);
305 	_nbu2ss_bitclr(&udc->p_regs->USB_INT_ENA, data);
306 
307 	if (ep->direct == USB_DIR_OUT) {
308 		/*---------------------------------------------------------*/
309 		/* OUT */
310 		data = EPN_ONAK | EPN_BCLR;
311 		_nbu2ss_bitset(&udc->p_regs->EP_REGS[num].EP_CONTROL, data);
312 
313 		data = EPN_EN | EPN_DIR0;
314 		_nbu2ss_bitclr(&udc->p_regs->EP_REGS[num].EP_CONTROL, data);
315 
316 		data = EPN_OUT_EN | EPN_OUT_END_EN;
317 		_nbu2ss_bitclr(&udc->p_regs->EP_REGS[num].EP_INT_ENA, data);
318 	} else {
319 		/*---------------------------------------------------------*/
320 		/* IN */
321 		data = EPN_BCLR;
322 		_nbu2ss_bitset(&udc->p_regs->EP_REGS[num].EP_CONTROL, data);
323 
324 		data = EPN_EN | EPN_AUTO;
325 		_nbu2ss_bitclr(&udc->p_regs->EP_REGS[num].EP_CONTROL, data);
326 
327 		data = EPN_IN_EN | EPN_IN_END_EN;
328 		_nbu2ss_bitclr(&udc->p_regs->EP_REGS[num].EP_INT_ENA, data);
329 	}
330 
331 	return 0;
332 }
333 
334 /*-------------------------------------------------------------------------*/
335 /* DMA setting (without Endpoint 0) */
336 static void _nbu2ss_ep_dma_init(struct nbu2ss_udc *udc, struct nbu2ss_ep *ep)
337 {
338 	u32		num;
339 	u32		data;
340 
341 	data = _nbu2ss_readl(&udc->p_regs->USBSSCONF);
342 	if (((ep->epnum == 0) || (data & (1 << ep->epnum)) == 0))
343 		return;		/* Not Support DMA */
344 
345 	num = ep->epnum - 1;
346 
347 	if (ep->direct == USB_DIR_OUT) {
348 		/*---------------------------------------------------------*/
349 		/* OUT */
350 		data = ep->ep.maxpacket;
351 		_nbu2ss_writel(&udc->p_regs->EP_DCR[num].EP_DCR2, data);
352 
353 		/*---------------------------------------------------------*/
354 		/* Transfer Direct */
355 		data = DCR1_EPN_DIR0;
356 		_nbu2ss_bitset(&udc->p_regs->EP_DCR[num].EP_DCR1, data);
357 
358 		/*---------------------------------------------------------*/
359 		/* DMA Mode etc. */
360 		data = EPN_STOP_MODE | EPN_STOP_SET  | EPN_DMAMODE0;
361 		_nbu2ss_writel(&udc->p_regs->EP_REGS[num].EP_DMA_CTRL, data);
362 	} else {
363 		/*---------------------------------------------------------*/
364 		/* IN */
365 		_nbu2ss_bitset(&udc->p_regs->EP_REGS[num].EP_CONTROL, EPN_AUTO);
366 
367 		/*---------------------------------------------------------*/
368 		/* DMA Mode etc. */
369 		data = EPN_BURST_SET | EPN_DMAMODE0;
370 		_nbu2ss_writel(&udc->p_regs->EP_REGS[num].EP_DMA_CTRL, data);
371 	}
372 }
373 
374 /*-------------------------------------------------------------------------*/
375 /* DMA setting release */
376 static void _nbu2ss_ep_dma_exit(struct nbu2ss_udc *udc, struct nbu2ss_ep *ep)
377 {
378 	u32		num;
379 	u32		data;
380 	struct fc_regs	*preg = udc->p_regs;
381 
382 	if (udc->vbus_active == 0)
383 		return;		/* VBUS OFF */
384 
385 	data = _nbu2ss_readl(&preg->USBSSCONF);
386 	if ((ep->epnum == 0) || ((data & (1 << ep->epnum)) == 0))
387 		return;		/* Not Support DMA */
388 
389 	num = ep->epnum - 1;
390 
391 	_nbu2ss_ep_dma_abort(udc, ep);
392 
393 	if (ep->direct == USB_DIR_OUT) {
394 		/*---------------------------------------------------------*/
395 		/* OUT */
396 		_nbu2ss_writel(&preg->EP_DCR[num].EP_DCR2, 0);
397 		_nbu2ss_bitclr(&preg->EP_DCR[num].EP_DCR1, DCR1_EPN_DIR0);
398 		_nbu2ss_writel(&preg->EP_REGS[num].EP_DMA_CTRL, 0);
399 	} else {
400 		/*---------------------------------------------------------*/
401 		/* IN */
402 		_nbu2ss_bitclr(&preg->EP_REGS[num].EP_CONTROL, EPN_AUTO);
403 		_nbu2ss_writel(&preg->EP_REGS[num].EP_DMA_CTRL, 0);
404 	}
405 }
406 
407 /*-------------------------------------------------------------------------*/
408 /* Abort DMA */
409 static void _nbu2ss_ep_dma_abort(struct nbu2ss_udc *udc, struct nbu2ss_ep *ep)
410 {
411 	struct fc_regs	*preg = udc->p_regs;
412 
413 	_nbu2ss_bitclr(&preg->EP_DCR[ep->epnum - 1].EP_DCR1, DCR1_EPN_REQEN);
414 	mdelay(DMA_DISABLE_TIME);	/* DCR1_EPN_REQEN Clear */
415 	_nbu2ss_bitclr(&preg->EP_REGS[ep->epnum - 1].EP_DMA_CTRL, EPN_DMA_EN);
416 }
417 
418 /*-------------------------------------------------------------------------*/
419 /* Start IN Transfer */
420 static void _nbu2ss_ep_in_end(
421 	struct nbu2ss_udc *udc,
422 	u32 epnum,
423 	u32 data32,
424 	u32 length
425 )
426 {
427 	u32		data;
428 	u32		num;
429 	struct fc_regs	*preg = udc->p_regs;
430 
431 	if (length >= sizeof(u32))
432 		return;
433 
434 	if (epnum == 0) {
435 		_nbu2ss_bitclr(&preg->EP0_CONTROL, EP0_AUTO);
436 
437 		/* Writing of 1-4 bytes */
438 		if (length)
439 			_nbu2ss_writel(&preg->EP0_WRITE, data32);
440 
441 		data = ((length << 5) & EP0_DW) | EP0_DEND;
442 		_nbu2ss_writel(&preg->EP0_CONTROL, data);
443 
444 		_nbu2ss_bitset(&preg->EP0_CONTROL, EP0_AUTO);
445 	} else {
446 		num = epnum - 1;
447 
448 		_nbu2ss_bitclr(&preg->EP_REGS[num].EP_CONTROL, EPN_AUTO);
449 
450 		/* Writing of 1-4 bytes */
451 		if (length)
452 			_nbu2ss_writel(&preg->EP_REGS[num].EP_WRITE, data32);
453 
454 		data = (((length) << 5) & EPN_DW) | EPN_DEND;
455 		_nbu2ss_bitset(&preg->EP_REGS[num].EP_CONTROL, data);
456 
457 		_nbu2ss_bitset(&preg->EP_REGS[num].EP_CONTROL, EPN_AUTO);
458 	}
459 }
460 
461 #ifdef USE_DMA
462 /*-------------------------------------------------------------------------*/
463 static void _nbu2ss_dma_map_single(
464 	struct nbu2ss_udc *udc,
465 	struct nbu2ss_ep *ep,
466 	struct nbu2ss_req *req,
467 	u8		direct
468 )
469 {
470 	if (req->req.dma == DMA_ADDR_INVALID) {
471 		if (req->unaligned) {
472 			req->req.dma = ep->phys_buf;
473 		} else {
474 			req->req.dma = dma_map_single(
475 				udc->gadget.dev.parent,
476 				req->req.buf,
477 				req->req.length,
478 				(direct == USB_DIR_IN)
479 				? DMA_TO_DEVICE : DMA_FROM_DEVICE);
480 		}
481 		req->mapped = 1;
482 	} else {
483 		if (!req->unaligned)
484 			dma_sync_single_for_device(
485 				udc->gadget.dev.parent,
486 				req->req.dma,
487 				req->req.length,
488 				(direct == USB_DIR_IN)
489 				? DMA_TO_DEVICE : DMA_FROM_DEVICE);
490 
491 		req->mapped = 0;
492 	}
493 }
494 
495 /*-------------------------------------------------------------------------*/
496 static void _nbu2ss_dma_unmap_single(
497 	struct nbu2ss_udc *udc,
498 	struct nbu2ss_ep *ep,
499 	struct nbu2ss_req *req,
500 	u8		direct
501 )
502 {
503 	u8		data[4];
504 	u8		*p;
505 	u32		count = 0;
506 
507 	if (direct == USB_DIR_OUT) {
508 		count = req->req.actual % 4;
509 		if (count) {
510 			p = req->req.buf;
511 			p += (req->req.actual - count);
512 			memcpy(data, p, count);
513 		}
514 	}
515 
516 	if (req->mapped) {
517 		if (req->unaligned) {
518 			if (direct == USB_DIR_OUT)
519 				memcpy(req->req.buf, ep->virt_buf,
520 				       req->req.actual & 0xfffffffc);
521 		} else {
522 			dma_unmap_single(udc->gadget.dev.parent,
523 					 req->req.dma, req->req.length,
524 				(direct == USB_DIR_IN)
525 				? DMA_TO_DEVICE
526 				: DMA_FROM_DEVICE);
527 		}
528 		req->req.dma = DMA_ADDR_INVALID;
529 		req->mapped = 0;
530 	} else {
531 		if (!req->unaligned)
532 			dma_sync_single_for_cpu(udc->gadget.dev.parent,
533 						req->req.dma, req->req.length,
534 				(direct == USB_DIR_IN)
535 				? DMA_TO_DEVICE
536 				: DMA_FROM_DEVICE);
537 	}
538 
539 	if (count) {
540 		p = req->req.buf;
541 		p += (req->req.actual - count);
542 		memcpy(p, data, count);
543 	}
544 }
545 #endif
546 
547 /*-------------------------------------------------------------------------*/
548 /* Endpoint 0 OUT Transfer (PIO) */
549 static int ep0_out_pio(struct nbu2ss_udc *udc, u8 *buf, u32 length)
550 {
551 	u32		i;
552 	u32 numreads = length / sizeof(u32);
553 	union usb_reg_access *buf32 = (union usb_reg_access *)buf;
554 
555 	if (!numreads)
556 		return 0;
557 
558 	/* PIO Read */
559 	for (i = 0; i < numreads; i++) {
560 		buf32->dw = _nbu2ss_readl(&udc->p_regs->EP0_READ);
561 		buf32++;
562 	}
563 
564 	return  numreads * sizeof(u32);
565 }
566 
567 /*-------------------------------------------------------------------------*/
568 /* Endpoint 0 OUT Transfer (PIO, OverBytes) */
569 static int ep0_out_overbytes(struct nbu2ss_udc *udc, u8 *p_buf, u32 length)
570 {
571 	u32		i;
572 	u32		i_read_size = 0;
573 	union usb_reg_access  temp_32;
574 	union usb_reg_access  *p_buf_32 = (union usb_reg_access *)p_buf;
575 
576 	if ((length > 0) && (length < sizeof(u32))) {
577 		temp_32.dw = _nbu2ss_readl(&udc->p_regs->EP0_READ);
578 		for (i = 0 ; i < length ; i++)
579 			p_buf_32->byte.DATA[i] = temp_32.byte.DATA[i];
580 		i_read_size += length;
581 	}
582 
583 	return i_read_size;
584 }
585 
586 /*-------------------------------------------------------------------------*/
587 /* Endpoint 0 IN Transfer (PIO) */
588 static int EP0_in_PIO(struct nbu2ss_udc *udc, u8 *p_buf, u32 length)
589 {
590 	u32		i;
591 	u32		i_max_length   = EP0_PACKETSIZE;
592 	u32		i_word_length  = 0;
593 	u32		i_write_length = 0;
594 	union usb_reg_access  *p_buf_32 = (union usb_reg_access *)p_buf;
595 
596 	/*------------------------------------------------------------*/
597 	/* Transfer Length */
598 	if (i_max_length < length)
599 		i_word_length = i_max_length / sizeof(u32);
600 	else
601 		i_word_length = length / sizeof(u32);
602 
603 	/*------------------------------------------------------------*/
604 	/* PIO */
605 	for (i = 0; i < i_word_length; i++) {
606 		_nbu2ss_writel(&udc->p_regs->EP0_WRITE, p_buf_32->dw);
607 		p_buf_32++;
608 		i_write_length += sizeof(u32);
609 	}
610 
611 	return i_write_length;
612 }
613 
614 /*-------------------------------------------------------------------------*/
615 /* Endpoint 0 IN Transfer (PIO, OverBytes) */
616 static int ep0_in_overbytes(struct nbu2ss_udc *udc,
617 			    u8 *p_buf,
618 			    u32 i_remain_size)
619 {
620 	u32		i;
621 	union usb_reg_access  temp_32;
622 	union usb_reg_access  *p_buf_32 = (union usb_reg_access *)p_buf;
623 
624 	if ((i_remain_size > 0) && (i_remain_size < sizeof(u32))) {
625 		for (i = 0 ; i < i_remain_size ; i++)
626 			temp_32.byte.DATA[i] = p_buf_32->byte.DATA[i];
627 		_nbu2ss_ep_in_end(udc, 0, temp_32.dw, i_remain_size);
628 
629 		return i_remain_size;
630 	}
631 
632 	return 0;
633 }
634 
635 /*-------------------------------------------------------------------------*/
636 /* Transfer NULL Packet (Epndoint 0) */
637 static int EP0_send_NULL(struct nbu2ss_udc *udc, bool pid_flag)
638 {
639 	u32		data;
640 
641 	data = _nbu2ss_readl(&udc->p_regs->EP0_CONTROL);
642 	data &= ~(u32)EP0_INAK;
643 
644 	if (pid_flag)
645 		data |= (EP0_INAK_EN | EP0_PIDCLR | EP0_DEND);
646 	else
647 		data |= (EP0_INAK_EN | EP0_DEND);
648 
649 	_nbu2ss_writel(&udc->p_regs->EP0_CONTROL, data);
650 
651 	return 0;
652 }
653 
654 /*-------------------------------------------------------------------------*/
655 /* Receive NULL Packet (Endpoint 0) */
656 static int EP0_receive_NULL(struct nbu2ss_udc *udc, bool pid_flag)
657 {
658 	u32		data;
659 
660 	data = _nbu2ss_readl(&udc->p_regs->EP0_CONTROL);
661 	data &= ~(u32)EP0_ONAK;
662 
663 	if (pid_flag)
664 		data |= EP0_PIDCLR;
665 
666 	_nbu2ss_writel(&udc->p_regs->EP0_CONTROL, data);
667 
668 	return 0;
669 }
670 
671 /*-------------------------------------------------------------------------*/
672 static int _nbu2ss_ep0_in_transfer(
673 	struct nbu2ss_udc *udc,
674 	struct nbu2ss_req *req
675 )
676 {
677 	u8		*p_buffer;			/* IN Data Buffer */
678 	u32		data;
679 	u32		i_remain_size = 0;
680 	int		result = 0;
681 
682 	/*-------------------------------------------------------------*/
683 	/* End confirmation */
684 	if (req->req.actual == req->req.length) {
685 		if ((req->req.actual % EP0_PACKETSIZE) == 0) {
686 			if (req->zero) {
687 				req->zero = false;
688 				EP0_send_NULL(udc, FALSE);
689 				return 1;
690 			}
691 		}
692 
693 		return 0;		/* Transfer End */
694 	}
695 
696 	/*-------------------------------------------------------------*/
697 	/* NAK release */
698 	data = _nbu2ss_readl(&udc->p_regs->EP0_CONTROL);
699 	data |= EP0_INAK_EN;
700 	data &= ~(u32)EP0_INAK;
701 	_nbu2ss_writel(&udc->p_regs->EP0_CONTROL, data);
702 
703 	i_remain_size = req->req.length - req->req.actual;
704 	p_buffer = (u8 *)req->req.buf;
705 	p_buffer += req->req.actual;
706 
707 	/*-------------------------------------------------------------*/
708 	/* Data transfer */
709 	result = EP0_in_PIO(udc, p_buffer, i_remain_size);
710 
711 	req->div_len = result;
712 	i_remain_size -= result;
713 
714 	if (i_remain_size == 0) {
715 		EP0_send_NULL(udc, FALSE);
716 		return result;
717 	}
718 
719 	if ((i_remain_size < sizeof(u32)) && (result != EP0_PACKETSIZE)) {
720 		p_buffer += result;
721 		result += ep0_in_overbytes(udc, p_buffer, i_remain_size);
722 		req->div_len = result;
723 	}
724 
725 	return result;
726 }
727 
728 /*-------------------------------------------------------------------------*/
729 static int _nbu2ss_ep0_out_transfer(
730 	struct nbu2ss_udc *udc,
731 	struct nbu2ss_req *req
732 )
733 {
734 	u8		*p_buffer;
735 	u32		i_remain_size;
736 	u32		i_recv_length;
737 	int		result = 0;
738 	int		f_rcv_zero;
739 
740 	/*-------------------------------------------------------------*/
741 	/* Receive data confirmation */
742 	i_recv_length = _nbu2ss_readl(&udc->p_regs->EP0_LENGTH) & EP0_LDATA;
743 	if (i_recv_length != 0) {
744 		f_rcv_zero = 0;
745 
746 		i_remain_size = req->req.length - req->req.actual;
747 		p_buffer = (u8 *)req->req.buf;
748 		p_buffer += req->req.actual;
749 
750 		result = ep0_out_pio(udc, p_buffer
751 					, min(i_remain_size, i_recv_length));
752 		if (result < 0)
753 			return result;
754 
755 		req->req.actual += result;
756 		i_recv_length -= result;
757 
758 		if ((i_recv_length > 0) && (i_recv_length < sizeof(u32))) {
759 			p_buffer += result;
760 			i_remain_size -= result;
761 
762 			result = ep0_out_overbytes(udc, p_buffer
763 					, min(i_remain_size, i_recv_length));
764 			req->req.actual += result;
765 		}
766 	} else {
767 		f_rcv_zero = 1;
768 	}
769 
770 	/*-------------------------------------------------------------*/
771 	/* End confirmation */
772 	if (req->req.actual == req->req.length) {
773 		if ((req->req.actual % EP0_PACKETSIZE) == 0) {
774 			if (req->zero) {
775 				req->zero = false;
776 				EP0_receive_NULL(udc, FALSE);
777 				return 1;
778 			}
779 		}
780 
781 		return 0;		/* Transfer End */
782 	}
783 
784 	if ((req->req.actual % EP0_PACKETSIZE) != 0)
785 		return 0;		/* Short Packet Transfer End */
786 
787 	if (req->req.actual > req->req.length) {
788 		dev_err(udc->dev, " *** Overrun Error\n");
789 		return -EOVERFLOW;
790 	}
791 
792 	if (f_rcv_zero != 0) {
793 		i_remain_size = _nbu2ss_readl(&udc->p_regs->EP0_CONTROL);
794 		if (i_remain_size & EP0_ONAK) {
795 			/*---------------------------------------------------*/
796 			/* NACK release */
797 			_nbu2ss_bitclr(&udc->p_regs->EP0_CONTROL, EP0_ONAK);
798 		}
799 		result = 1;
800 	}
801 
802 	return result;
803 }
804 
805 /*-------------------------------------------------------------------------*/
806 static int _nbu2ss_out_dma(
807 	struct nbu2ss_udc *udc,
808 	struct nbu2ss_req *req,
809 	u32		num,
810 	u32		length
811 )
812 {
813 	dma_addr_t	p_buffer;
814 	u32		mpkt;
815 	u32		lmpkt;
816 	u32		dmacnt;
817 	u32		burst = 1;
818 	u32		data;
819 	int		result = -EINVAL;
820 	struct fc_regs	*preg = udc->p_regs;
821 
822 	if (req->dma_flag)
823 		return 1;		/* DMA is forwarded */
824 
825 	req->dma_flag = TRUE;
826 	p_buffer = req->req.dma;
827 	p_buffer += req->req.actual;
828 
829 	/* DMA Address */
830 	_nbu2ss_writel(&preg->EP_DCR[num].EP_TADR, (u32)p_buffer);
831 
832 	/* Number of transfer packets */
833 	mpkt = _nbu2ss_readl(&preg->EP_REGS[num].EP_PCKT_ADRS) & EPN_MPKT;
834 	dmacnt = length / mpkt;
835 	lmpkt = (length % mpkt) & ~(u32)0x03;
836 
837 	if (dmacnt > DMA_MAX_COUNT) {
838 		dmacnt = DMA_MAX_COUNT;
839 		lmpkt = 0;
840 	} else if (lmpkt != 0) {
841 		if (dmacnt == 0)
842 			burst = 0;	/* Burst OFF */
843 		dmacnt++;
844 	}
845 
846 	data = mpkt | (lmpkt << 16);
847 	_nbu2ss_writel(&preg->EP_DCR[num].EP_DCR2, data);
848 
849 	data = ((dmacnt & 0xff) << 16) | DCR1_EPN_DIR0 | DCR1_EPN_REQEN;
850 	_nbu2ss_writel(&preg->EP_DCR[num].EP_DCR1, data);
851 
852 	if (burst == 0) {
853 		_nbu2ss_writel(&preg->EP_REGS[num].EP_LEN_DCNT, 0);
854 		_nbu2ss_bitclr(&preg->EP_REGS[num].EP_DMA_CTRL, EPN_BURST_SET);
855 	} else {
856 		_nbu2ss_writel(&preg->EP_REGS[num].EP_LEN_DCNT
857 				, (dmacnt << 16));
858 		_nbu2ss_bitset(&preg->EP_REGS[num].EP_DMA_CTRL, EPN_BURST_SET);
859 	}
860 	_nbu2ss_bitset(&preg->EP_REGS[num].EP_DMA_CTRL, EPN_DMA_EN);
861 
862 	result = length & ~(u32)0x03;
863 	req->div_len = result;
864 
865 	return result;
866 }
867 
868 /*-------------------------------------------------------------------------*/
869 static int _nbu2ss_epn_out_pio(
870 	struct nbu2ss_udc *udc,
871 	struct nbu2ss_ep *ep,
872 	struct nbu2ss_req *req,
873 	u32		length
874 )
875 {
876 	u8		*p_buffer;
877 	u32		i;
878 	u32		data;
879 	u32		i_word_length;
880 	union usb_reg_access	temp_32;
881 	union usb_reg_access	*p_buf_32;
882 	int		result = 0;
883 	struct fc_regs	*preg = udc->p_regs;
884 
885 	if (req->dma_flag)
886 		return 1;		/* DMA is forwarded */
887 
888 	if (length == 0)
889 		return 0;
890 
891 	p_buffer = (u8 *)req->req.buf;
892 	p_buf_32 = (union usb_reg_access *)(p_buffer + req->req.actual);
893 
894 	i_word_length = length / sizeof(u32);
895 	if (i_word_length > 0) {
896 		/*---------------------------------------------------------*/
897 		/* Copy of every four bytes */
898 		for (i = 0; i < i_word_length; i++) {
899 			p_buf_32->dw =
900 			_nbu2ss_readl(&preg->EP_REGS[ep->epnum - 1].EP_READ);
901 			p_buf_32++;
902 		}
903 		result = i_word_length * sizeof(u32);
904 	}
905 
906 	data = length - result;
907 	if (data > 0) {
908 		/*---------------------------------------------------------*/
909 		/* Copy of fraction byte */
910 		temp_32.dw =
911 			_nbu2ss_readl(&preg->EP_REGS[ep->epnum - 1].EP_READ);
912 		for (i = 0 ; i < data ; i++)
913 			p_buf_32->byte.DATA[i] = temp_32.byte.DATA[i];
914 		result += data;
915 	}
916 
917 	req->req.actual += result;
918 
919 	if ((req->req.actual == req->req.length) ||
920 	    ((req->req.actual % ep->ep.maxpacket) != 0)) {
921 		result = 0;
922 	}
923 
924 	return result;
925 }
926 
927 /*-------------------------------------------------------------------------*/
928 static int _nbu2ss_epn_out_data(
929 	struct nbu2ss_udc *udc,
930 	struct nbu2ss_ep *ep,
931 	struct nbu2ss_req *req,
932 	u32		data_size
933 )
934 {
935 	u32		num;
936 	u32		i_buf_size;
937 	int		nret = 1;
938 
939 	if (ep->epnum == 0)
940 		return -EINVAL;
941 
942 	num = ep->epnum - 1;
943 
944 	i_buf_size = min((req->req.length - req->req.actual), data_size);
945 
946 	if ((ep->ep_type != USB_ENDPOINT_XFER_INT) && (req->req.dma != 0) &&
947 	    (i_buf_size  >= sizeof(u32))) {
948 		nret = _nbu2ss_out_dma(udc, req, num, i_buf_size);
949 	} else {
950 		i_buf_size = min_t(u32, i_buf_size, ep->ep.maxpacket);
951 		nret = _nbu2ss_epn_out_pio(udc, ep, req, i_buf_size);
952 	}
953 
954 	return nret;
955 }
956 
957 /*-------------------------------------------------------------------------*/
958 static int _nbu2ss_epn_out_transfer(
959 	struct nbu2ss_udc *udc,
960 	struct nbu2ss_ep *ep,
961 	struct nbu2ss_req *req
962 )
963 {
964 	u32		num;
965 	u32		i_recv_length;
966 	int		result = 1;
967 	struct fc_regs	*preg = udc->p_regs;
968 
969 	if (ep->epnum == 0)
970 		return -EINVAL;
971 
972 	num = ep->epnum - 1;
973 
974 	/*-------------------------------------------------------------*/
975 	/* Receive Length */
976 	i_recv_length
977 		= _nbu2ss_readl(&preg->EP_REGS[num].EP_LEN_DCNT) & EPN_LDATA;
978 
979 	if (i_recv_length != 0) {
980 		result = _nbu2ss_epn_out_data(udc, ep, req, i_recv_length);
981 		if (i_recv_length < ep->ep.maxpacket) {
982 			if (i_recv_length == result) {
983 				req->req.actual += result;
984 				result = 0;
985 			}
986 		}
987 	} else {
988 		if ((req->req.actual == req->req.length) ||
989 		    ((req->req.actual % ep->ep.maxpacket) != 0)) {
990 			result = 0;
991 		}
992 	}
993 
994 	if (result == 0) {
995 		if ((req->req.actual % ep->ep.maxpacket) == 0) {
996 			if (req->zero) {
997 				req->zero = false;
998 				return 1;
999 			}
1000 		}
1001 	}
1002 
1003 	if (req->req.actual > req->req.length) {
1004 		dev_err(udc->dev, " Overrun Error\n");
1005 		dev_err(udc->dev, " actual = %d, length = %d\n",
1006 			req->req.actual, req->req.length);
1007 		result = -EOVERFLOW;
1008 	}
1009 
1010 	return result;
1011 }
1012 
1013 /*-------------------------------------------------------------------------*/
1014 static int _nbu2ss_in_dma(
1015 	struct nbu2ss_udc *udc,
1016 	struct nbu2ss_ep *ep,
1017 	struct nbu2ss_req *req,
1018 	u32		num,
1019 	u32		length
1020 )
1021 {
1022 	dma_addr_t	p_buffer;
1023 	u32		mpkt;		/* MaxPacketSize */
1024 	u32		lmpkt;		/* Last Packet Data Size */
1025 	u32		dmacnt;		/* IN Data Size */
1026 	u32		i_write_length;
1027 	u32		data;
1028 	int		result = -EINVAL;
1029 	struct fc_regs	*preg = udc->p_regs;
1030 
1031 	if (req->dma_flag)
1032 		return 1;		/* DMA is forwarded */
1033 
1034 #ifdef USE_DMA
1035 	if (req->req.actual == 0)
1036 		_nbu2ss_dma_map_single(udc, ep, req, USB_DIR_IN);
1037 #endif
1038 	req->dma_flag = TRUE;
1039 
1040 	/* MAX Packet Size */
1041 	mpkt = _nbu2ss_readl(&preg->EP_REGS[num].EP_PCKT_ADRS) & EPN_MPKT;
1042 
1043 	if ((DMA_MAX_COUNT * mpkt) < length)
1044 		i_write_length = DMA_MAX_COUNT * mpkt;
1045 	else
1046 		i_write_length = length;
1047 
1048 	/*------------------------------------------------------------*/
1049 	/* Number of transmission packets */
1050 	if (mpkt < i_write_length) {
1051 		dmacnt = i_write_length / mpkt;
1052 		lmpkt  = (i_write_length % mpkt) & ~(u32)0x3;
1053 		if (lmpkt != 0)
1054 			dmacnt++;
1055 		else
1056 			lmpkt = mpkt & ~(u32)0x3;
1057 
1058 	} else {
1059 		dmacnt = 1;
1060 		lmpkt  = i_write_length & ~(u32)0x3;
1061 	}
1062 
1063 	/* Packet setting */
1064 	data = mpkt | (lmpkt << 16);
1065 	_nbu2ss_writel(&preg->EP_DCR[num].EP_DCR2, data);
1066 
1067 	/* Address setting */
1068 	p_buffer = req->req.dma;
1069 	p_buffer += req->req.actual;
1070 	_nbu2ss_writel(&preg->EP_DCR[num].EP_TADR, (u32)p_buffer);
1071 
1072 	/* Packet and DMA setting */
1073 	data = ((dmacnt & 0xff) << 16) | DCR1_EPN_REQEN;
1074 	_nbu2ss_writel(&preg->EP_DCR[num].EP_DCR1, data);
1075 
1076 	/* Packet setting of EPC */
1077 	data = dmacnt << 16;
1078 	_nbu2ss_writel(&preg->EP_REGS[num].EP_LEN_DCNT, data);
1079 
1080 	/*DMA setting of EPC */
1081 	_nbu2ss_bitset(&preg->EP_REGS[num].EP_DMA_CTRL, EPN_DMA_EN);
1082 
1083 	result = i_write_length & ~(u32)0x3;
1084 	req->div_len = result;
1085 
1086 	return result;
1087 }
1088 
1089 /*-------------------------------------------------------------------------*/
1090 static int _nbu2ss_epn_in_pio(
1091 	struct nbu2ss_udc *udc,
1092 	struct nbu2ss_ep *ep,
1093 	struct nbu2ss_req *req,
1094 	u32		length
1095 )
1096 {
1097 	u8		*p_buffer;
1098 	u32		i;
1099 	u32		data;
1100 	u32		i_word_length;
1101 	union usb_reg_access	temp_32;
1102 	union usb_reg_access	*p_buf_32 = NULL;
1103 	int		result = 0;
1104 	struct fc_regs	*preg = udc->p_regs;
1105 
1106 	if (req->dma_flag)
1107 		return 1;		/* DMA is forwarded */
1108 
1109 	if (length > 0) {
1110 		p_buffer = (u8 *)req->req.buf;
1111 		p_buf_32 = (union usb_reg_access *)(p_buffer + req->req.actual);
1112 
1113 		i_word_length = length / sizeof(u32);
1114 		if (i_word_length > 0) {
1115 			for (i = 0; i < i_word_length; i++) {
1116 				_nbu2ss_writel(
1117 					&preg->EP_REGS[ep->epnum - 1].EP_WRITE
1118 					, p_buf_32->dw
1119 				);
1120 
1121 				p_buf_32++;
1122 			}
1123 			result = i_word_length * sizeof(u32);
1124 		}
1125 	}
1126 
1127 	if (result != ep->ep.maxpacket) {
1128 		data = length - result;
1129 		temp_32.dw = 0;
1130 		for (i = 0 ; i < data ; i++)
1131 			temp_32.byte.DATA[i] = p_buf_32->byte.DATA[i];
1132 
1133 		_nbu2ss_ep_in_end(udc, ep->epnum, temp_32.dw, data);
1134 		result += data;
1135 	}
1136 
1137 	req->div_len = result;
1138 
1139 	return result;
1140 }
1141 
1142 /*-------------------------------------------------------------------------*/
1143 static int _nbu2ss_epn_in_data(
1144 	struct nbu2ss_udc *udc,
1145 	struct nbu2ss_ep *ep,
1146 	struct nbu2ss_req *req,
1147 	u32		data_size
1148 )
1149 {
1150 	u32		num;
1151 	int		nret = 1;
1152 
1153 	if (ep->epnum == 0)
1154 		return -EINVAL;
1155 
1156 	num = ep->epnum - 1;
1157 
1158 	if ((ep->ep_type != USB_ENDPOINT_XFER_INT) && (req->req.dma != 0) &&
1159 	    (data_size >= sizeof(u32))) {
1160 		nret = _nbu2ss_in_dma(udc, ep, req, num, data_size);
1161 	} else {
1162 		data_size = min_t(u32, data_size, ep->ep.maxpacket);
1163 		nret = _nbu2ss_epn_in_pio(udc, ep, req, data_size);
1164 	}
1165 
1166 	return nret;
1167 }
1168 
1169 /*-------------------------------------------------------------------------*/
1170 static int _nbu2ss_epn_in_transfer(
1171 	struct nbu2ss_udc *udc,
1172 	struct nbu2ss_ep *ep,
1173 	struct nbu2ss_req *req
1174 )
1175 {
1176 	u32		num;
1177 	u32		i_buf_size;
1178 	int		result = 0;
1179 	u32		status;
1180 
1181 	if (ep->epnum == 0)
1182 		return -EINVAL;
1183 
1184 	num = ep->epnum - 1;
1185 
1186 	status = _nbu2ss_readl(&udc->p_regs->EP_REGS[num].EP_STATUS);
1187 
1188 	/*-------------------------------------------------------------*/
1189 	/* State confirmation of FIFO */
1190 	if (req->req.actual == 0) {
1191 		if ((status & EPN_IN_EMPTY) == 0)
1192 			return 1;	/* Not Empty */
1193 
1194 	} else {
1195 		if ((status & EPN_IN_FULL) != 0)
1196 			return 1;	/* Not Empty */
1197 	}
1198 
1199 	/*-------------------------------------------------------------*/
1200 	/* Start transfer */
1201 	i_buf_size = req->req.length - req->req.actual;
1202 	if (i_buf_size > 0)
1203 		result = _nbu2ss_epn_in_data(udc, ep, req, i_buf_size);
1204 	else if (req->req.length == 0)
1205 		_nbu2ss_zero_len_pkt(udc, ep->epnum);
1206 
1207 	return result;
1208 }
1209 
1210 /*-------------------------------------------------------------------------*/
1211 static int _nbu2ss_start_transfer(
1212 	struct nbu2ss_udc *udc,
1213 	struct nbu2ss_ep *ep,
1214 	struct nbu2ss_req *req,
1215 	bool	bflag)
1216 {
1217 	int		nret = -EINVAL;
1218 
1219 	req->dma_flag = FALSE;
1220 	req->div_len = 0;
1221 
1222 	if (req->req.length == 0) {
1223 		req->zero = false;
1224 	} else {
1225 		if ((req->req.length % ep->ep.maxpacket) == 0)
1226 			req->zero = req->req.zero;
1227 		else
1228 			req->zero = false;
1229 	}
1230 
1231 	if (ep->epnum == 0) {
1232 		/* EP0 */
1233 		switch (udc->ep0state) {
1234 		case EP0_IN_DATA_PHASE:
1235 			nret = _nbu2ss_ep0_in_transfer(udc, req);
1236 			break;
1237 
1238 		case EP0_OUT_DATA_PHASE:
1239 			nret = _nbu2ss_ep0_out_transfer(udc, req);
1240 			break;
1241 
1242 		case EP0_IN_STATUS_PHASE:
1243 			nret = EP0_send_NULL(udc, TRUE);
1244 			break;
1245 
1246 		default:
1247 			break;
1248 		}
1249 
1250 	} else {
1251 		/* EPN */
1252 		if (ep->direct == USB_DIR_OUT) {
1253 			/* OUT */
1254 			if (!bflag)
1255 				nret = _nbu2ss_epn_out_transfer(udc, ep, req);
1256 		} else {
1257 			/* IN */
1258 			nret = _nbu2ss_epn_in_transfer(udc, ep, req);
1259 		}
1260 	}
1261 
1262 	return nret;
1263 }
1264 
1265 /*-------------------------------------------------------------------------*/
1266 static void _nbu2ss_restert_transfer(struct nbu2ss_ep *ep)
1267 {
1268 	u32		length;
1269 	bool	bflag = FALSE;
1270 	struct nbu2ss_req *req;
1271 
1272 	req = list_first_entry_or_null(&ep->queue, struct nbu2ss_req, queue);
1273 	if (!req)
1274 		return;
1275 
1276 	if (ep->epnum > 0) {
1277 		length = _nbu2ss_readl(
1278 			&ep->udc->p_regs->EP_REGS[ep->epnum - 1].EP_LEN_DCNT);
1279 
1280 		length &= EPN_LDATA;
1281 		if (length < ep->ep.maxpacket)
1282 			bflag = TRUE;
1283 	}
1284 
1285 	_nbu2ss_start_transfer(ep->udc, ep, req, bflag);
1286 }
1287 
1288 /*-------------------------------------------------------------------------*/
1289 /*	Endpoint Toggle Reset */
1290 static void _nbu2ss_endpoint_toggle_reset(
1291 	struct nbu2ss_udc *udc,
1292 	u8 ep_adrs)
1293 {
1294 	u8		num;
1295 	u32		data;
1296 
1297 	if ((ep_adrs == 0) || (ep_adrs == 0x80))
1298 		return;
1299 
1300 	num = (ep_adrs & 0x7F) - 1;
1301 
1302 	if (ep_adrs & USB_DIR_IN)
1303 		data = EPN_IPIDCLR;
1304 	else
1305 		data = EPN_BCLR | EPN_OPIDCLR;
1306 
1307 	_nbu2ss_bitset(&udc->p_regs->EP_REGS[num].EP_CONTROL, data);
1308 }
1309 
1310 /*-------------------------------------------------------------------------*/
1311 /*	Endpoint STALL set */
1312 static void _nbu2ss_set_endpoint_stall(
1313 	struct nbu2ss_udc *udc,
1314 	u8 ep_adrs,
1315 	bool bstall)
1316 {
1317 	u8		num, epnum;
1318 	u32		data;
1319 	struct nbu2ss_ep *ep;
1320 	struct fc_regs	*preg = udc->p_regs;
1321 
1322 	if ((ep_adrs == 0) || (ep_adrs == 0x80)) {
1323 		if (bstall) {
1324 			/* Set STALL */
1325 			_nbu2ss_bitset(&preg->EP0_CONTROL, EP0_STL);
1326 		} else {
1327 			/* Clear STALL */
1328 			_nbu2ss_bitclr(&preg->EP0_CONTROL, EP0_STL);
1329 		}
1330 	} else {
1331 		epnum = ep_adrs & USB_ENDPOINT_NUMBER_MASK;
1332 		num = epnum - 1;
1333 		ep = &udc->ep[epnum];
1334 
1335 		if (bstall) {
1336 			/* Set STALL */
1337 			ep->halted = TRUE;
1338 
1339 			if (ep_adrs & USB_DIR_IN)
1340 				data = EPN_BCLR | EPN_ISTL;
1341 			else
1342 				data = EPN_OSTL_EN | EPN_OSTL;
1343 
1344 			_nbu2ss_bitset(&preg->EP_REGS[num].EP_CONTROL, data);
1345 		} else {
1346 			/* Clear STALL */
1347 			ep->stalled = FALSE;
1348 			if (ep_adrs & USB_DIR_IN) {
1349 				_nbu2ss_bitclr(&preg->EP_REGS[num].EP_CONTROL
1350 						, EPN_ISTL);
1351 			} else {
1352 				data =
1353 				_nbu2ss_readl(&preg->EP_REGS[num].EP_CONTROL);
1354 
1355 				data &= ~EPN_OSTL;
1356 				data |= EPN_OSTL_EN;
1357 
1358 				_nbu2ss_writel(&preg->EP_REGS[num].EP_CONTROL
1359 						, data);
1360 			}
1361 
1362 			ep->stalled = FALSE;
1363 			if (ep->halted) {
1364 				ep->halted = FALSE;
1365 				_nbu2ss_restert_transfer(ep);
1366 			}
1367 		}
1368 	}
1369 }
1370 
1371 /*-------------------------------------------------------------------------*/
1372 /* Device Descriptor */
1373 static struct usb_device_descriptor device_desc = {
1374 	.bLength              = sizeof(device_desc),
1375 	.bDescriptorType      = USB_DT_DEVICE,
1376 	.bcdUSB               = cpu_to_le16(0x0200),
1377 	.bDeviceClass         = USB_CLASS_VENDOR_SPEC,
1378 	.bDeviceSubClass      = 0x00,
1379 	.bDeviceProtocol      = 0x00,
1380 	.bMaxPacketSize0      = 64,
1381 	.idVendor             = cpu_to_le16(0x0409),
1382 	.idProduct            = cpu_to_le16(0xfff0),
1383 	.bcdDevice            = 0xffff,
1384 	.iManufacturer        = 0x00,
1385 	.iProduct             = 0x00,
1386 	.iSerialNumber        = 0x00,
1387 	.bNumConfigurations   = 0x01,
1388 };
1389 
1390 /*-------------------------------------------------------------------------*/
1391 static void _nbu2ss_set_test_mode(struct nbu2ss_udc *udc, u32 mode)
1392 {
1393 	u32		data;
1394 
1395 	if (mode > MAX_TEST_MODE_NUM)
1396 		return;
1397 
1398 	dev_info(udc->dev, "SET FEATURE : test mode = %d\n", mode);
1399 
1400 	data = _nbu2ss_readl(&udc->p_regs->USB_CONTROL);
1401 	data &= ~TEST_FORCE_ENABLE;
1402 	data |= mode << TEST_MODE_SHIFT;
1403 
1404 	_nbu2ss_writel(&udc->p_regs->USB_CONTROL, data);
1405 	_nbu2ss_bitset(&udc->p_regs->TEST_CONTROL, CS_TESTMODEEN);
1406 }
1407 
1408 /*-------------------------------------------------------------------------*/
1409 static int _nbu2ss_set_feature_device(
1410 	struct nbu2ss_udc *udc,
1411 	u16 selector,
1412 	u16 wIndex
1413 )
1414 {
1415 	int	result = -EOPNOTSUPP;
1416 
1417 	switch (selector) {
1418 	case USB_DEVICE_REMOTE_WAKEUP:
1419 		if (wIndex == 0x0000) {
1420 			udc->remote_wakeup = U2F_ENABLE;
1421 			result = 0;
1422 		}
1423 		break;
1424 
1425 	case USB_DEVICE_TEST_MODE:
1426 		wIndex >>= 8;
1427 		if (wIndex <= MAX_TEST_MODE_NUM)
1428 			result = 0;
1429 		break;
1430 
1431 	default:
1432 		break;
1433 	}
1434 
1435 	return result;
1436 }
1437 
1438 /*-------------------------------------------------------------------------*/
1439 static int _nbu2ss_get_ep_stall(struct nbu2ss_udc *udc, u8 ep_adrs)
1440 {
1441 	u8		epnum;
1442 	u32		data = 0, bit_data;
1443 	struct fc_regs	*preg = udc->p_regs;
1444 
1445 	epnum = ep_adrs & ~USB_ENDPOINT_DIR_MASK;
1446 	if (epnum == 0) {
1447 		data = _nbu2ss_readl(&preg->EP0_CONTROL);
1448 		bit_data = EP0_STL;
1449 
1450 	} else {
1451 		data = _nbu2ss_readl(&preg->EP_REGS[epnum - 1].EP_CONTROL);
1452 		if ((data & EPN_EN) == 0)
1453 			return -1;
1454 
1455 		if (ep_adrs & USB_ENDPOINT_DIR_MASK)
1456 			bit_data = EPN_ISTL;
1457 		else
1458 			bit_data = EPN_OSTL;
1459 	}
1460 
1461 	if ((data & bit_data) == 0)
1462 		return 0;
1463 	return 1;
1464 }
1465 
1466 /*-------------------------------------------------------------------------*/
1467 static inline int _nbu2ss_req_feature(struct nbu2ss_udc *udc, bool bset)
1468 {
1469 	u8	recipient = (u8)(udc->ctrl.bRequestType & USB_RECIP_MASK);
1470 	u8	direction = (u8)(udc->ctrl.bRequestType & USB_DIR_IN);
1471 	u16	selector  = udc->ctrl.wValue;
1472 	u16	wIndex    = udc->ctrl.wIndex;
1473 	u8	ep_adrs;
1474 	int	result = -EOPNOTSUPP;
1475 
1476 	if ((udc->ctrl.wLength != 0x0000) ||
1477 	    (direction != USB_DIR_OUT)) {
1478 		return -EINVAL;
1479 	}
1480 
1481 	switch (recipient) {
1482 	case USB_RECIP_DEVICE:
1483 		if (bset)
1484 			result =
1485 			_nbu2ss_set_feature_device(udc, selector, wIndex);
1486 		break;
1487 
1488 	case USB_RECIP_ENDPOINT:
1489 		if (0x0000 == (wIndex & 0xFF70)) {
1490 			if (selector == USB_ENDPOINT_HALT) {
1491 				ep_adrs = wIndex & 0xFF;
1492 				if (!bset) {
1493 					_nbu2ss_endpoint_toggle_reset(
1494 						udc, ep_adrs);
1495 				}
1496 
1497 				_nbu2ss_set_endpoint_stall(
1498 					udc, ep_adrs, bset);
1499 
1500 				result = 0;
1501 			}
1502 		}
1503 		break;
1504 
1505 	default:
1506 		break;
1507 	}
1508 
1509 	if (result >= 0)
1510 		_nbu2ss_create_ep0_packet(udc, udc->ep0_buf, 0);
1511 
1512 	return result;
1513 }
1514 
1515 /*-------------------------------------------------------------------------*/
1516 static inline enum usb_device_speed _nbu2ss_get_speed(struct nbu2ss_udc *udc)
1517 {
1518 	u32		data;
1519 	enum usb_device_speed speed = USB_SPEED_FULL;
1520 
1521 	data = _nbu2ss_readl(&udc->p_regs->USB_STATUS);
1522 	if (data & HIGH_SPEED)
1523 		speed = USB_SPEED_HIGH;
1524 
1525 	return speed;
1526 }
1527 
1528 /*-------------------------------------------------------------------------*/
1529 static void _nbu2ss_epn_set_stall(
1530 	struct nbu2ss_udc *udc,
1531 	struct nbu2ss_ep *ep
1532 )
1533 {
1534 	u8	ep_adrs;
1535 	u32	regdata;
1536 	int	limit_cnt = 0;
1537 
1538 	struct fc_regs	*preg = udc->p_regs;
1539 
1540 	if (ep->direct == USB_DIR_IN) {
1541 		for (limit_cnt = 0
1542 			; limit_cnt < IN_DATA_EMPTY_COUNT
1543 			; limit_cnt++) {
1544 			regdata = _nbu2ss_readl(
1545 				&preg->EP_REGS[ep->epnum - 1].EP_STATUS);
1546 
1547 			if ((regdata & EPN_IN_DATA) == 0)
1548 				break;
1549 
1550 			mdelay(1);
1551 		}
1552 	}
1553 
1554 	ep_adrs = ep->epnum | ep->direct;
1555 	_nbu2ss_set_endpoint_stall(udc, ep_adrs, 1);
1556 }
1557 
1558 /*-------------------------------------------------------------------------*/
1559 static int std_req_get_status(struct nbu2ss_udc *udc)
1560 {
1561 	u32	length;
1562 	u16	status_data = 0;
1563 	u8	recipient = (u8)(udc->ctrl.bRequestType & USB_RECIP_MASK);
1564 	u8	direction = (u8)(udc->ctrl.bRequestType & USB_DIR_IN);
1565 	u8	ep_adrs;
1566 	int	result = -EINVAL;
1567 
1568 	if ((udc->ctrl.wValue != 0x0000) || (direction != USB_DIR_IN))
1569 		return result;
1570 
1571 	length = min_t(u16, udc->ctrl.wLength, sizeof(status_data));
1572 
1573 	switch (recipient) {
1574 	case USB_RECIP_DEVICE:
1575 		if (udc->ctrl.wIndex == 0x0000) {
1576 			if (udc->gadget.is_selfpowered)
1577 				status_data |= (1 << USB_DEVICE_SELF_POWERED);
1578 
1579 			if (udc->remote_wakeup)
1580 				status_data |= (1 << USB_DEVICE_REMOTE_WAKEUP);
1581 
1582 			result = 0;
1583 		}
1584 		break;
1585 
1586 	case USB_RECIP_ENDPOINT:
1587 		if (0x0000 == (udc->ctrl.wIndex & 0xFF70)) {
1588 			ep_adrs = (u8)(udc->ctrl.wIndex & 0xFF);
1589 			result = _nbu2ss_get_ep_stall(udc, ep_adrs);
1590 
1591 			if (result > 0)
1592 				status_data |= (1 << USB_ENDPOINT_HALT);
1593 		}
1594 		break;
1595 
1596 	default:
1597 		break;
1598 	}
1599 
1600 	if (result >= 0) {
1601 		memcpy(udc->ep0_buf, &status_data, length);
1602 		_nbu2ss_create_ep0_packet(udc, udc->ep0_buf, length);
1603 		_nbu2ss_ep0_in_transfer(udc, &udc->ep0_req);
1604 
1605 	} else {
1606 		dev_err(udc->dev, " Error GET_STATUS\n");
1607 	}
1608 
1609 	return result;
1610 }
1611 
1612 /*-------------------------------------------------------------------------*/
1613 static int std_req_clear_feature(struct nbu2ss_udc *udc)
1614 {
1615 	return _nbu2ss_req_feature(udc, FALSE);
1616 }
1617 
1618 /*-------------------------------------------------------------------------*/
1619 static int std_req_set_feature(struct nbu2ss_udc *udc)
1620 {
1621 	return _nbu2ss_req_feature(udc, TRUE);
1622 }
1623 
1624 /*-------------------------------------------------------------------------*/
1625 static int std_req_set_address(struct nbu2ss_udc *udc)
1626 {
1627 	int		result = 0;
1628 	u32		wValue = udc->ctrl.wValue;
1629 
1630 	if ((udc->ctrl.bRequestType != 0x00)	||
1631 	    (udc->ctrl.wIndex != 0x0000)	||
1632 		(udc->ctrl.wLength != 0x0000)) {
1633 		return -EINVAL;
1634 	}
1635 
1636 	if (wValue != (wValue & 0x007F))
1637 		return -EINVAL;
1638 
1639 	wValue <<= USB_ADRS_SHIFT;
1640 
1641 	_nbu2ss_writel(&udc->p_regs->USB_ADDRESS, wValue);
1642 	_nbu2ss_create_ep0_packet(udc, udc->ep0_buf, 0);
1643 
1644 	return result;
1645 }
1646 
1647 /*-------------------------------------------------------------------------*/
1648 static int std_req_set_configuration(struct nbu2ss_udc *udc)
1649 {
1650 	u32 config_value = (u32)(udc->ctrl.wValue & 0x00ff);
1651 
1652 	if ((udc->ctrl.wIndex != 0x0000)	||
1653 	    (udc->ctrl.wLength != 0x0000)	||
1654 		(udc->ctrl.bRequestType != 0x00)) {
1655 		return -EINVAL;
1656 	}
1657 
1658 	udc->curr_config = config_value;
1659 
1660 	if (config_value > 0) {
1661 		_nbu2ss_bitset(&udc->p_regs->USB_CONTROL, CONF);
1662 		udc->devstate = USB_STATE_CONFIGURED;
1663 
1664 	} else {
1665 		_nbu2ss_bitclr(&udc->p_regs->USB_CONTROL, CONF);
1666 		udc->devstate = USB_STATE_ADDRESS;
1667 	}
1668 
1669 	return 0;
1670 }
1671 
1672 /*-------------------------------------------------------------------------*/
1673 static inline void _nbu2ss_read_request_data(struct nbu2ss_udc *udc, u32 *pdata)
1674 {
1675 	if ((!udc) && (!pdata))
1676 		return;
1677 
1678 	*pdata = _nbu2ss_readl(&udc->p_regs->SETUP_DATA0);
1679 	pdata++;
1680 	*pdata = _nbu2ss_readl(&udc->p_regs->SETUP_DATA1);
1681 }
1682 
1683 /*-------------------------------------------------------------------------*/
1684 static inline int _nbu2ss_decode_request(struct nbu2ss_udc *udc)
1685 {
1686 	bool			bcall_back = TRUE;
1687 	int			nret = -EINVAL;
1688 	struct usb_ctrlrequest	*p_ctrl;
1689 
1690 	p_ctrl = &udc->ctrl;
1691 	_nbu2ss_read_request_data(udc, (u32 *)p_ctrl);
1692 
1693 	/* ep0 state control */
1694 	if (p_ctrl->wLength == 0) {
1695 		udc->ep0state = EP0_IN_STATUS_PHASE;
1696 
1697 	} else {
1698 		if (p_ctrl->bRequestType & USB_DIR_IN)
1699 			udc->ep0state = EP0_IN_DATA_PHASE;
1700 		else
1701 			udc->ep0state = EP0_OUT_DATA_PHASE;
1702 	}
1703 
1704 	if ((p_ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1705 		switch (p_ctrl->bRequest) {
1706 		case USB_REQ_GET_STATUS:
1707 			nret = std_req_get_status(udc);
1708 			bcall_back = FALSE;
1709 			break;
1710 
1711 		case USB_REQ_CLEAR_FEATURE:
1712 			nret = std_req_clear_feature(udc);
1713 			bcall_back = FALSE;
1714 			break;
1715 
1716 		case USB_REQ_SET_FEATURE:
1717 			nret = std_req_set_feature(udc);
1718 			bcall_back = FALSE;
1719 			break;
1720 
1721 		case USB_REQ_SET_ADDRESS:
1722 			nret = std_req_set_address(udc);
1723 			bcall_back = FALSE;
1724 			break;
1725 
1726 		case USB_REQ_SET_CONFIGURATION:
1727 			nret = std_req_set_configuration(udc);
1728 			break;
1729 
1730 		default:
1731 			break;
1732 		}
1733 	}
1734 
1735 	if (!bcall_back) {
1736 		if (udc->ep0state == EP0_IN_STATUS_PHASE) {
1737 			if (nret >= 0) {
1738 				/*--------------------------------------*/
1739 				/* Status Stage */
1740 				nret = EP0_send_NULL(udc, TRUE);
1741 			}
1742 		}
1743 
1744 	} else {
1745 		spin_unlock(&udc->lock);
1746 		nret = udc->driver->setup(&udc->gadget, &udc->ctrl);
1747 		spin_lock(&udc->lock);
1748 	}
1749 
1750 	if (nret < 0)
1751 		udc->ep0state = EP0_IDLE;
1752 
1753 	return nret;
1754 }
1755 
1756 /*-------------------------------------------------------------------------*/
1757 static inline int _nbu2ss_ep0_in_data_stage(struct nbu2ss_udc *udc)
1758 {
1759 	int			nret;
1760 	struct nbu2ss_req	*req;
1761 	struct nbu2ss_ep	*ep = &udc->ep[0];
1762 
1763 	req = list_first_entry_or_null(&ep->queue, struct nbu2ss_req, queue);
1764 	if (!req)
1765 		req = &udc->ep0_req;
1766 
1767 	req->req.actual += req->div_len;
1768 	req->div_len = 0;
1769 
1770 	nret = _nbu2ss_ep0_in_transfer(udc, req);
1771 	if (nret == 0) {
1772 		udc->ep0state = EP0_OUT_STATUS_PAHSE;
1773 		EP0_receive_NULL(udc, TRUE);
1774 	}
1775 
1776 	return 0;
1777 }
1778 
1779 /*-------------------------------------------------------------------------*/
1780 static inline int _nbu2ss_ep0_out_data_stage(struct nbu2ss_udc *udc)
1781 {
1782 	int			nret;
1783 	struct nbu2ss_req	*req;
1784 	struct nbu2ss_ep	*ep = &udc->ep[0];
1785 
1786 	req = list_first_entry_or_null(&ep->queue, struct nbu2ss_req, queue);
1787 	if (!req)
1788 		req = &udc->ep0_req;
1789 
1790 	nret = _nbu2ss_ep0_out_transfer(udc, req);
1791 	if (nret == 0) {
1792 		udc->ep0state = EP0_IN_STATUS_PHASE;
1793 		EP0_send_NULL(udc, TRUE);
1794 
1795 	} else if (nret < 0) {
1796 		_nbu2ss_bitset(&udc->p_regs->EP0_CONTROL, EP0_BCLR);
1797 		req->req.status = nret;
1798 	}
1799 
1800 	return 0;
1801 }
1802 
1803 /*-------------------------------------------------------------------------*/
1804 static inline int _nbu2ss_ep0_status_stage(struct nbu2ss_udc *udc)
1805 {
1806 	struct nbu2ss_req	*req;
1807 	struct nbu2ss_ep	*ep = &udc->ep[0];
1808 
1809 	req = list_first_entry_or_null(&ep->queue, struct nbu2ss_req, queue);
1810 	if (!req) {
1811 		req = &udc->ep0_req;
1812 		if (req->req.complete)
1813 			req->req.complete(&ep->ep, &req->req);
1814 
1815 	} else {
1816 		if (req->req.complete)
1817 			_nbu2ss_ep_done(ep, req, 0);
1818 	}
1819 
1820 	udc->ep0state = EP0_IDLE;
1821 
1822 	return 0;
1823 }
1824 
1825 /*-------------------------------------------------------------------------*/
1826 static inline void _nbu2ss_ep0_int(struct nbu2ss_udc *udc)
1827 {
1828 	int		i;
1829 	u32		status;
1830 	u32		intr;
1831 	int		nret = -1;
1832 
1833 	status = _nbu2ss_readl(&udc->p_regs->EP0_STATUS);
1834 	intr = status & EP0_STATUS_RW_BIT;
1835 	_nbu2ss_writel(&udc->p_regs->EP0_STATUS, ~intr);
1836 
1837 	status &= (SETUP_INT | EP0_IN_INT | EP0_OUT_INT
1838 			| STG_END_INT | EP0_OUT_NULL_INT);
1839 
1840 	if (status == 0) {
1841 		dev_info(udc->dev, "%s Not Decode Interrupt\n", __func__);
1842 		dev_info(udc->dev, "EP0_STATUS = 0x%08x\n", intr);
1843 		return;
1844 	}
1845 
1846 	if (udc->gadget.speed == USB_SPEED_UNKNOWN)
1847 		udc->gadget.speed = _nbu2ss_get_speed(udc);
1848 
1849 	for (i = 0; i < EP0_END_XFER; i++) {
1850 		switch (udc->ep0state) {
1851 		case EP0_IDLE:
1852 			if (status & SETUP_INT) {
1853 				status = 0;
1854 				nret = _nbu2ss_decode_request(udc);
1855 			}
1856 			break;
1857 
1858 		case EP0_IN_DATA_PHASE:
1859 			if (status & EP0_IN_INT) {
1860 				status &= ~EP0_IN_INT;
1861 				nret = _nbu2ss_ep0_in_data_stage(udc);
1862 			}
1863 			break;
1864 
1865 		case EP0_OUT_DATA_PHASE:
1866 			if (status & EP0_OUT_INT) {
1867 				status &= ~EP0_OUT_INT;
1868 				nret = _nbu2ss_ep0_out_data_stage(udc);
1869 			}
1870 			break;
1871 
1872 		case EP0_IN_STATUS_PHASE:
1873 			if ((status & STG_END_INT) || (status & SETUP_INT)) {
1874 				status &= ~(STG_END_INT | EP0_IN_INT);
1875 				nret = _nbu2ss_ep0_status_stage(udc);
1876 			}
1877 			break;
1878 
1879 		case EP0_OUT_STATUS_PAHSE:
1880 			if ((status & STG_END_INT) || (status & SETUP_INT) ||
1881 			    (status & EP0_OUT_NULL_INT)) {
1882 				status &= ~(STG_END_INT
1883 						| EP0_OUT_INT
1884 						| EP0_OUT_NULL_INT);
1885 
1886 				nret = _nbu2ss_ep0_status_stage(udc);
1887 			}
1888 
1889 			break;
1890 
1891 		default:
1892 			status = 0;
1893 			break;
1894 		}
1895 
1896 		if (status == 0)
1897 			break;
1898 	}
1899 
1900 	if (nret < 0) {
1901 		/* Send Stall */
1902 		_nbu2ss_set_endpoint_stall(udc, 0, TRUE);
1903 	}
1904 }
1905 
1906 /*-------------------------------------------------------------------------*/
1907 static void _nbu2ss_ep_done(
1908 	struct nbu2ss_ep *ep,
1909 	struct nbu2ss_req *req,
1910 	int status)
1911 {
1912 	struct nbu2ss_udc *udc = ep->udc;
1913 
1914 	list_del_init(&req->queue);
1915 
1916 	if (status == -ECONNRESET)
1917 		_nbu2ss_fifo_flush(udc, ep);
1918 
1919 	if (likely(req->req.status == -EINPROGRESS))
1920 		req->req.status = status;
1921 
1922 	if (ep->stalled) {
1923 		_nbu2ss_epn_set_stall(udc, ep);
1924 	} else {
1925 		if (!list_empty(&ep->queue))
1926 			_nbu2ss_restert_transfer(ep);
1927 	}
1928 
1929 #ifdef USE_DMA
1930 	if ((ep->direct == USB_DIR_OUT) && (ep->epnum > 0) &&
1931 	    (req->req.dma != 0))
1932 		_nbu2ss_dma_unmap_single(udc, ep, req, USB_DIR_OUT);
1933 #endif
1934 
1935 	spin_unlock(&udc->lock);
1936 	req->req.complete(&ep->ep, &req->req);
1937 	spin_lock(&udc->lock);
1938 }
1939 
1940 /*-------------------------------------------------------------------------*/
1941 static inline void _nbu2ss_epn_in_int(
1942 	struct nbu2ss_udc *udc,
1943 	struct nbu2ss_ep *ep,
1944 	struct nbu2ss_req *req)
1945 {
1946 	int	result = 0;
1947 	u32	status;
1948 
1949 	struct fc_regs	*preg = udc->p_regs;
1950 
1951 	if (req->dma_flag)
1952 		return;		/* DMA is forwarded */
1953 
1954 	req->req.actual += req->div_len;
1955 	req->div_len = 0;
1956 
1957 	if (req->req.actual != req->req.length) {
1958 		/*---------------------------------------------------------*/
1959 		/* remainder of data */
1960 		result = _nbu2ss_epn_in_transfer(udc, ep, req);
1961 
1962 	} else {
1963 		if (req->zero && ((req->req.actual % ep->ep.maxpacket) == 0)) {
1964 			status =
1965 			_nbu2ss_readl(&preg->EP_REGS[ep->epnum - 1].EP_STATUS);
1966 
1967 			if ((status & EPN_IN_FULL) == 0) {
1968 				/*-----------------------------------------*/
1969 				/* 0 Length Packet */
1970 				req->zero = false;
1971 				_nbu2ss_zero_len_pkt(udc, ep->epnum);
1972 			}
1973 			return;
1974 		}
1975 	}
1976 
1977 	if (result <= 0) {
1978 		/*---------------------------------------------------------*/
1979 		/* Complete */
1980 		_nbu2ss_ep_done(ep, req, result);
1981 	}
1982 }
1983 
1984 /*-------------------------------------------------------------------------*/
1985 static inline void _nbu2ss_epn_out_int(
1986 	struct nbu2ss_udc *udc,
1987 	struct nbu2ss_ep *ep,
1988 	struct nbu2ss_req *req)
1989 {
1990 	int	result;
1991 
1992 	result = _nbu2ss_epn_out_transfer(udc, ep, req);
1993 	if (result <= 0)
1994 		_nbu2ss_ep_done(ep, req, result);
1995 }
1996 
1997 /*-------------------------------------------------------------------------*/
1998 static inline void _nbu2ss_epn_in_dma_int(
1999 	struct nbu2ss_udc *udc,
2000 	struct nbu2ss_ep *ep,
2001 	struct nbu2ss_req *req)
2002 {
2003 	u32		mpkt;
2004 	u32		size;
2005 	struct usb_request *preq;
2006 
2007 	preq = &req->req;
2008 
2009 	if (!req->dma_flag)
2010 		return;
2011 
2012 	preq->actual += req->div_len;
2013 	req->div_len = 0;
2014 	req->dma_flag = FALSE;
2015 
2016 #ifdef USE_DMA
2017 	_nbu2ss_dma_unmap_single(udc, ep, req, USB_DIR_IN);
2018 #endif
2019 
2020 	if (preq->actual != preq->length) {
2021 		_nbu2ss_epn_in_transfer(udc, ep, req);
2022 	} else {
2023 		mpkt = ep->ep.maxpacket;
2024 		size = preq->actual % mpkt;
2025 		if (size > 0) {
2026 			if (((preq->actual & 0x03) == 0) && (size < mpkt))
2027 				_nbu2ss_ep_in_end(udc, ep->epnum, 0, 0);
2028 		} else {
2029 			_nbu2ss_epn_in_int(udc, ep, req);
2030 		}
2031 	}
2032 }
2033 
2034 /*-------------------------------------------------------------------------*/
2035 static inline void _nbu2ss_epn_out_dma_int(
2036 	struct nbu2ss_udc *udc,
2037 	struct nbu2ss_ep *ep,
2038 	struct nbu2ss_req *req)
2039 {
2040 	int		i;
2041 	u32		num;
2042 	u32		dmacnt, ep_dmacnt;
2043 	u32		mpkt;
2044 	struct fc_regs	*preg = udc->p_regs;
2045 
2046 	num = ep->epnum - 1;
2047 
2048 	if (req->req.actual == req->req.length) {
2049 		if ((req->req.length % ep->ep.maxpacket) && !req->zero) {
2050 			req->div_len = 0;
2051 			req->dma_flag = FALSE;
2052 			_nbu2ss_ep_done(ep, req, 0);
2053 			return;
2054 		}
2055 	}
2056 
2057 	ep_dmacnt = _nbu2ss_readl(&preg->EP_REGS[num].EP_LEN_DCNT)
2058 		 & EPN_DMACNT;
2059 	ep_dmacnt >>= 16;
2060 
2061 	for (i = 0; i < EPC_PLL_LOCK_COUNT; i++) {
2062 		dmacnt = _nbu2ss_readl(&preg->EP_DCR[num].EP_DCR1)
2063 			 & DCR1_EPN_DMACNT;
2064 		dmacnt >>= 16;
2065 		if (ep_dmacnt == dmacnt)
2066 			break;
2067 	}
2068 
2069 	_nbu2ss_bitclr(&preg->EP_DCR[num].EP_DCR1, DCR1_EPN_REQEN);
2070 
2071 	if (dmacnt != 0) {
2072 		mpkt = ep->ep.maxpacket;
2073 		if ((req->div_len % mpkt) == 0)
2074 			req->div_len -= mpkt * dmacnt;
2075 	}
2076 
2077 	if ((req->req.actual % ep->ep.maxpacket) > 0) {
2078 		if (req->req.actual == req->div_len) {
2079 			req->div_len = 0;
2080 			req->dma_flag = FALSE;
2081 			_nbu2ss_ep_done(ep, req, 0);
2082 			return;
2083 		}
2084 	}
2085 
2086 	req->req.actual += req->div_len;
2087 	req->div_len = 0;
2088 	req->dma_flag = FALSE;
2089 
2090 	_nbu2ss_epn_out_int(udc, ep, req);
2091 }
2092 
2093 /*-------------------------------------------------------------------------*/
2094 static inline void _nbu2ss_epn_int(struct nbu2ss_udc *udc, u32 epnum)
2095 {
2096 	u32	num;
2097 	u32	status;
2098 
2099 	struct nbu2ss_req	*req;
2100 	struct nbu2ss_ep	*ep = &udc->ep[epnum];
2101 
2102 	num = epnum - 1;
2103 
2104 	/* Interrupt Status */
2105 	status = _nbu2ss_readl(&udc->p_regs->EP_REGS[num].EP_STATUS);
2106 
2107 	/* Interrupt Clear */
2108 	_nbu2ss_writel(&udc->p_regs->EP_REGS[num].EP_STATUS, ~status);
2109 
2110 	req = list_first_entry_or_null(&ep->queue, struct nbu2ss_req, queue);
2111 	if (!req) {
2112 		/* pr_warn("=== %s(%d) req == NULL\n", __func__, epnum); */
2113 		return;
2114 	}
2115 
2116 	if (status & EPN_OUT_END_INT) {
2117 		status &= ~EPN_OUT_INT;
2118 		_nbu2ss_epn_out_dma_int(udc, ep, req);
2119 	}
2120 
2121 	if (status & EPN_OUT_INT)
2122 		_nbu2ss_epn_out_int(udc, ep, req);
2123 
2124 	if (status & EPN_IN_END_INT) {
2125 		status &= ~EPN_IN_INT;
2126 		_nbu2ss_epn_in_dma_int(udc, ep, req);
2127 	}
2128 
2129 	if (status & EPN_IN_INT)
2130 		_nbu2ss_epn_in_int(udc, ep, req);
2131 }
2132 
2133 /*-------------------------------------------------------------------------*/
2134 static inline void _nbu2ss_ep_int(struct nbu2ss_udc *udc, u32 epnum)
2135 {
2136 	if (epnum == 0)
2137 		_nbu2ss_ep0_int(udc);
2138 	else
2139 		_nbu2ss_epn_int(udc, epnum);
2140 }
2141 
2142 /*-------------------------------------------------------------------------*/
2143 static void _nbu2ss_ep0_enable(struct nbu2ss_udc *udc)
2144 {
2145 	_nbu2ss_bitset(&udc->p_regs->EP0_CONTROL, (EP0_AUTO | EP0_BCLR));
2146 	_nbu2ss_writel(&udc->p_regs->EP0_INT_ENA, EP0_INT_EN_BIT);
2147 }
2148 
2149 /*-------------------------------------------------------------------------*/
2150 static int _nbu2ss_nuke(struct nbu2ss_udc *udc,
2151 			struct nbu2ss_ep *ep,
2152 			int status)
2153 {
2154 	struct nbu2ss_req *req;
2155 
2156 	/* Endpoint Disable */
2157 	_nbu2ss_epn_exit(udc, ep);
2158 
2159 	/* DMA Disable */
2160 	_nbu2ss_ep_dma_exit(udc, ep);
2161 
2162 	if (list_empty(&ep->queue))
2163 		return 0;
2164 
2165 	/* called with irqs blocked */
2166 	list_for_each_entry(req, &ep->queue, queue) {
2167 		_nbu2ss_ep_done(ep, req, status);
2168 	}
2169 
2170 	return 0;
2171 }
2172 
2173 /*-------------------------------------------------------------------------*/
2174 static void _nbu2ss_quiesce(struct nbu2ss_udc *udc)
2175 {
2176 	struct nbu2ss_ep	*ep;
2177 
2178 	udc->gadget.speed = USB_SPEED_UNKNOWN;
2179 
2180 	_nbu2ss_nuke(udc, &udc->ep[0], -ESHUTDOWN);
2181 
2182 	/* Endpoint n */
2183 	list_for_each_entry(ep, &udc->gadget.ep_list, ep.ep_list) {
2184 		_nbu2ss_nuke(udc, ep, -ESHUTDOWN);
2185 	}
2186 }
2187 
2188 /*-------------------------------------------------------------------------*/
2189 static int _nbu2ss_pullup(struct nbu2ss_udc *udc, int is_on)
2190 {
2191 	u32	reg_dt;
2192 
2193 	if (udc->vbus_active == 0)
2194 		return -ESHUTDOWN;
2195 
2196 	if (is_on) {
2197 		/* D+ Pullup */
2198 		if (udc->driver) {
2199 			reg_dt = (_nbu2ss_readl(&udc->p_regs->USB_CONTROL)
2200 				| PUE2) & ~(u32)CONNECTB;
2201 
2202 			_nbu2ss_writel(&udc->p_regs->USB_CONTROL, reg_dt);
2203 		}
2204 
2205 	} else {
2206 		/* D+ Pulldown */
2207 		reg_dt = (_nbu2ss_readl(&udc->p_regs->USB_CONTROL) | CONNECTB)
2208 			& ~(u32)PUE2;
2209 
2210 		_nbu2ss_writel(&udc->p_regs->USB_CONTROL, reg_dt);
2211 		udc->gadget.speed = USB_SPEED_UNKNOWN;
2212 	}
2213 
2214 	return 0;
2215 }
2216 
2217 /*-------------------------------------------------------------------------*/
2218 static void _nbu2ss_fifo_flush(struct nbu2ss_udc *udc, struct nbu2ss_ep *ep)
2219 {
2220 	struct fc_regs	*p = udc->p_regs;
2221 
2222 	if (udc->vbus_active == 0)
2223 		return;
2224 
2225 	if (ep->epnum == 0) {
2226 		/* EP0 */
2227 		_nbu2ss_bitset(&p->EP0_CONTROL, EP0_BCLR);
2228 
2229 	} else {
2230 		/* EPN */
2231 		_nbu2ss_ep_dma_abort(udc, ep);
2232 		_nbu2ss_bitset(&p->EP_REGS[ep->epnum - 1].EP_CONTROL, EPN_BCLR);
2233 	}
2234 }
2235 
2236 /*-------------------------------------------------------------------------*/
2237 static int _nbu2ss_enable_controller(struct nbu2ss_udc *udc)
2238 {
2239 	int	waitcnt = 0;
2240 
2241 	if (udc->udc_enabled)
2242 		return 0;
2243 
2244 	/* Reset */
2245 	_nbu2ss_bitset(&udc->p_regs->EPCTR, (DIRPD | EPC_RST));
2246 	udelay(EPC_RST_DISABLE_TIME);	/* 1us wait */
2247 
2248 	_nbu2ss_bitclr(&udc->p_regs->EPCTR, DIRPD);
2249 	mdelay(EPC_DIRPD_DISABLE_TIME);	/* 1ms wait */
2250 
2251 	_nbu2ss_bitclr(&udc->p_regs->EPCTR, EPC_RST);
2252 
2253 	_nbu2ss_writel(&udc->p_regs->AHBSCTR, WAIT_MODE);
2254 
2255 		_nbu2ss_writel(&udc->p_regs->AHBMCTR,
2256 			       HBUSREQ_MODE | HTRANS_MODE | WBURST_TYPE);
2257 
2258 	while (!(_nbu2ss_readl(&udc->p_regs->EPCTR) & PLL_LOCK)) {
2259 		waitcnt++;
2260 		udelay(1);	/* 1us wait */
2261 		if (waitcnt == EPC_PLL_LOCK_COUNT) {
2262 			dev_err(udc->dev, "*** Reset Cancel failed\n");
2263 			return -EINVAL;
2264 		}
2265 	}
2266 
2267 		_nbu2ss_bitset(&udc->p_regs->UTMI_CHARACTER_1, USB_SQUSET);
2268 
2269 	_nbu2ss_bitset(&udc->p_regs->USB_CONTROL, (INT_SEL | SOF_RCV));
2270 
2271 	/* EP0 */
2272 	_nbu2ss_ep0_enable(udc);
2273 
2274 	/* USB Interrupt Enable */
2275 	_nbu2ss_bitset(&udc->p_regs->USB_INT_ENA, USB_INT_EN_BIT);
2276 
2277 	udc->udc_enabled = TRUE;
2278 
2279 	return 0;
2280 }
2281 
2282 /*-------------------------------------------------------------------------*/
2283 static void _nbu2ss_reset_controller(struct nbu2ss_udc *udc)
2284 {
2285 	_nbu2ss_bitset(&udc->p_regs->EPCTR, EPC_RST);
2286 	_nbu2ss_bitclr(&udc->p_regs->EPCTR, EPC_RST);
2287 }
2288 
2289 /*-------------------------------------------------------------------------*/
2290 static void _nbu2ss_disable_controller(struct nbu2ss_udc *udc)
2291 {
2292 	if (udc->udc_enabled) {
2293 		udc->udc_enabled = FALSE;
2294 		_nbu2ss_reset_controller(udc);
2295 		_nbu2ss_bitset(&udc->p_regs->EPCTR, (DIRPD | EPC_RST));
2296 	}
2297 }
2298 
2299 /*-------------------------------------------------------------------------*/
2300 static inline void _nbu2ss_check_vbus(struct nbu2ss_udc *udc)
2301 {
2302 	int	nret;
2303 	u32	reg_dt;
2304 
2305 	/* chattering */
2306 	mdelay(VBUS_CHATTERING_MDELAY);		/* wait (ms) */
2307 
2308 	/* VBUS ON Check*/
2309 	reg_dt = gpio_get_value(VBUS_VALUE);
2310 	if (reg_dt == 0) {
2311 		udc->linux_suspended = 0;
2312 
2313 		_nbu2ss_reset_controller(udc);
2314 		dev_info(udc->dev, " ----- VBUS OFF\n");
2315 
2316 		if (udc->vbus_active == 1) {
2317 			/* VBUS OFF */
2318 			udc->vbus_active = 0;
2319 			if (udc->usb_suspended) {
2320 				udc->usb_suspended = 0;
2321 				/* _nbu2ss_reset_controller(udc); */
2322 			}
2323 			udc->devstate = USB_STATE_NOTATTACHED;
2324 
2325 			_nbu2ss_quiesce(udc);
2326 			if (udc->driver) {
2327 				spin_unlock(&udc->lock);
2328 				udc->driver->disconnect(&udc->gadget);
2329 				spin_lock(&udc->lock);
2330 			}
2331 
2332 			_nbu2ss_disable_controller(udc);
2333 		}
2334 	} else {
2335 		mdelay(5);		/* wait (5ms) */
2336 		reg_dt = gpio_get_value(VBUS_VALUE);
2337 		if (reg_dt == 0)
2338 			return;
2339 
2340 		dev_info(udc->dev, " ----- VBUS ON\n");
2341 
2342 		if (udc->linux_suspended)
2343 			return;
2344 
2345 		if (udc->vbus_active == 0) {
2346 			/* VBUS ON */
2347 			udc->vbus_active = 1;
2348 			udc->devstate = USB_STATE_POWERED;
2349 
2350 			nret = _nbu2ss_enable_controller(udc);
2351 			if (nret < 0) {
2352 				_nbu2ss_disable_controller(udc);
2353 				udc->vbus_active = 0;
2354 				return;
2355 			}
2356 
2357 			_nbu2ss_pullup(udc, 1);
2358 
2359 #ifdef UDC_DEBUG_DUMP
2360 			_nbu2ss_dump_register(udc);
2361 #endif /* UDC_DEBUG_DUMP */
2362 
2363 		} else {
2364 			if (udc->devstate == USB_STATE_POWERED)
2365 				_nbu2ss_pullup(udc, 1);
2366 		}
2367 	}
2368 }
2369 
2370 /*-------------------------------------------------------------------------*/
2371 static inline void _nbu2ss_int_bus_reset(struct nbu2ss_udc *udc)
2372 {
2373 	udc->devstate		= USB_STATE_DEFAULT;
2374 	udc->remote_wakeup	= 0;
2375 
2376 	_nbu2ss_quiesce(udc);
2377 
2378 	udc->ep0state = EP0_IDLE;
2379 }
2380 
2381 /*-------------------------------------------------------------------------*/
2382 static inline void _nbu2ss_int_usb_resume(struct nbu2ss_udc *udc)
2383 {
2384 	if (udc->usb_suspended == 1) {
2385 		udc->usb_suspended = 0;
2386 		if (udc->driver && udc->driver->resume) {
2387 			spin_unlock(&udc->lock);
2388 			udc->driver->resume(&udc->gadget);
2389 			spin_lock(&udc->lock);
2390 		}
2391 	}
2392 }
2393 
2394 /*-------------------------------------------------------------------------*/
2395 static inline void _nbu2ss_int_usb_suspend(struct nbu2ss_udc *udc)
2396 {
2397 	u32	reg_dt;
2398 
2399 	if (udc->usb_suspended == 0) {
2400 		reg_dt = gpio_get_value(VBUS_VALUE);
2401 
2402 		if (reg_dt == 0)
2403 			return;
2404 
2405 		udc->usb_suspended = 1;
2406 		if (udc->driver && udc->driver->suspend) {
2407 			spin_unlock(&udc->lock);
2408 			udc->driver->suspend(&udc->gadget);
2409 			spin_lock(&udc->lock);
2410 		}
2411 
2412 		_nbu2ss_bitset(&udc->p_regs->USB_CONTROL, SUSPEND);
2413 	}
2414 }
2415 
2416 /*-------------------------------------------------------------------------*/
2417 /* VBUS (GPIO153) Interrupt */
2418 static irqreturn_t _nbu2ss_vbus_irq(int irq, void *_udc)
2419 {
2420 	struct nbu2ss_udc	*udc = (struct nbu2ss_udc *)_udc;
2421 
2422 	spin_lock(&udc->lock);
2423 	_nbu2ss_check_vbus(udc);
2424 	spin_unlock(&udc->lock);
2425 
2426 	return IRQ_HANDLED;
2427 }
2428 
2429 /*-------------------------------------------------------------------------*/
2430 /* Interrupt (udc) */
2431 static irqreturn_t _nbu2ss_udc_irq(int irq, void *_udc)
2432 {
2433 	u8	suspend_flag = 0;
2434 	u32	status;
2435 	u32	epnum, int_bit;
2436 
2437 	struct nbu2ss_udc	*udc = (struct nbu2ss_udc *)_udc;
2438 	struct fc_regs	*preg = udc->p_regs;
2439 
2440 	if (gpio_get_value(VBUS_VALUE) == 0) {
2441 		_nbu2ss_writel(&preg->USB_INT_STA, ~USB_INT_STA_RW);
2442 		_nbu2ss_writel(&preg->USB_INT_ENA, 0);
2443 		return IRQ_HANDLED;
2444 	}
2445 
2446 	spin_lock(&udc->lock);
2447 
2448 	for (;;) {
2449 		if (gpio_get_value(VBUS_VALUE) == 0) {
2450 			_nbu2ss_writel(&preg->USB_INT_STA, ~USB_INT_STA_RW);
2451 			_nbu2ss_writel(&preg->USB_INT_ENA, 0);
2452 			status = 0;
2453 		} else {
2454 			status = _nbu2ss_readl(&preg->USB_INT_STA);
2455 		}
2456 
2457 		if (status == 0)
2458 			break;
2459 
2460 		_nbu2ss_writel(&preg->USB_INT_STA, ~(status & USB_INT_STA_RW));
2461 
2462 		if (status & USB_RST_INT) {
2463 			/* USB Reset */
2464 			_nbu2ss_int_bus_reset(udc);
2465 		}
2466 
2467 		if (status & RSUM_INT) {
2468 			/* Resume */
2469 			_nbu2ss_int_usb_resume(udc);
2470 		}
2471 
2472 		if (status & SPND_INT) {
2473 			/* Suspend */
2474 			suspend_flag = 1;
2475 		}
2476 
2477 		if (status & EPN_INT) {
2478 			/* EP INT */
2479 			int_bit = status >> 8;
2480 
2481 			for (epnum = 0; epnum < NUM_ENDPOINTS; epnum++) {
2482 				if (0x01 & int_bit)
2483 					_nbu2ss_ep_int(udc, epnum);
2484 
2485 				int_bit >>= 1;
2486 
2487 				if (int_bit == 0)
2488 					break;
2489 			}
2490 		}
2491 	}
2492 
2493 	if (suspend_flag)
2494 		_nbu2ss_int_usb_suspend(udc);
2495 
2496 	spin_unlock(&udc->lock);
2497 
2498 	return IRQ_HANDLED;
2499 }
2500 
2501 /*-------------------------------------------------------------------------*/
2502 /* usb_ep_ops */
2503 static int nbu2ss_ep_enable(
2504 	struct usb_ep *_ep,
2505 	const struct usb_endpoint_descriptor *desc)
2506 {
2507 	u8		ep_type;
2508 	unsigned long	flags;
2509 
2510 	struct nbu2ss_ep	*ep;
2511 	struct nbu2ss_udc	*udc;
2512 
2513 	if ((!_ep) || (!desc)) {
2514 		pr_err(" *** %s, bad param\n", __func__);
2515 		return -EINVAL;
2516 	}
2517 
2518 	ep = container_of(_ep, struct nbu2ss_ep, ep);
2519 	if ((!ep) || (!ep->udc)) {
2520 		pr_err(" *** %s, ep == NULL !!\n", __func__);
2521 		return -EINVAL;
2522 	}
2523 
2524 	ep_type = usb_endpoint_type(desc);
2525 	if ((ep_type == USB_ENDPOINT_XFER_CONTROL) ||
2526 	    (ep_type == USB_ENDPOINT_XFER_ISOC)) {
2527 		pr_err(" *** %s, bat bmAttributes\n", __func__);
2528 		return -EINVAL;
2529 	}
2530 
2531 	udc = ep->udc;
2532 	if (udc->vbus_active == 0)
2533 		return -ESHUTDOWN;
2534 
2535 	if ((!udc->driver) || (udc->gadget.speed == USB_SPEED_UNKNOWN)) {
2536 		dev_err(ep->udc->dev, " *** %s, udc !!\n", __func__);
2537 		return -ESHUTDOWN;
2538 	}
2539 
2540 	spin_lock_irqsave(&udc->lock, flags);
2541 
2542 	ep->desc = desc;
2543 	ep->epnum = usb_endpoint_num(desc);
2544 	ep->direct = desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK;
2545 	ep->ep_type = ep_type;
2546 	ep->wedged = 0;
2547 	ep->halted = FALSE;
2548 	ep->stalled = FALSE;
2549 
2550 	ep->ep.maxpacket = le16_to_cpu(desc->wMaxPacketSize);
2551 
2552 	/* DMA setting */
2553 	_nbu2ss_ep_dma_init(udc, ep);
2554 
2555 	/* Endpoint setting */
2556 	_nbu2ss_ep_init(udc, ep);
2557 
2558 	spin_unlock_irqrestore(&udc->lock, flags);
2559 
2560 	return 0;
2561 }
2562 
2563 /*-------------------------------------------------------------------------*/
2564 static int nbu2ss_ep_disable(struct usb_ep *_ep)
2565 {
2566 	struct nbu2ss_ep	*ep;
2567 	struct nbu2ss_udc	*udc;
2568 	unsigned long		flags;
2569 
2570 	if (!_ep) {
2571 		pr_err(" *** %s, bad param\n", __func__);
2572 		return -EINVAL;
2573 	}
2574 
2575 	ep = container_of(_ep, struct nbu2ss_ep, ep);
2576 	if ((!ep) || (!ep->udc)) {
2577 		pr_err("udc: *** %s, ep == NULL !!\n", __func__);
2578 		return -EINVAL;
2579 	}
2580 
2581 	udc = ep->udc;
2582 	if (udc->vbus_active == 0)
2583 		return -ESHUTDOWN;
2584 
2585 	spin_lock_irqsave(&udc->lock, flags);
2586 	_nbu2ss_nuke(udc, ep, -EINPROGRESS);		/* dequeue request */
2587 	spin_unlock_irqrestore(&udc->lock, flags);
2588 
2589 	return 0;
2590 }
2591 
2592 /*-------------------------------------------------------------------------*/
2593 static struct usb_request *nbu2ss_ep_alloc_request(
2594 	struct usb_ep *ep,
2595 	gfp_t gfp_flags)
2596 {
2597 	struct nbu2ss_req *req;
2598 
2599 	req = kzalloc(sizeof(*req), gfp_flags);
2600 	if (!req)
2601 		return NULL;
2602 
2603 #ifdef USE_DMA
2604 	req->req.dma = DMA_ADDR_INVALID;
2605 #endif
2606 	INIT_LIST_HEAD(&req->queue);
2607 
2608 	return &req->req;
2609 }
2610 
2611 /*-------------------------------------------------------------------------*/
2612 static void nbu2ss_ep_free_request(
2613 	struct usb_ep *_ep,
2614 	struct usb_request *_req)
2615 {
2616 	struct nbu2ss_req *req;
2617 
2618 	if (_req) {
2619 		req = container_of(_req, struct nbu2ss_req, req);
2620 
2621 		kfree(req);
2622 	}
2623 }
2624 
2625 /*-------------------------------------------------------------------------*/
2626 static int nbu2ss_ep_queue(
2627 	struct usb_ep *_ep,
2628 	struct usb_request *_req,
2629 	gfp_t gfp_flags)
2630 {
2631 	struct nbu2ss_req	*req;
2632 	struct nbu2ss_ep	*ep;
2633 	struct nbu2ss_udc	*udc;
2634 	unsigned long		flags;
2635 	bool			bflag;
2636 	int			result = -EINVAL;
2637 
2638 	/* catch various bogus parameters */
2639 	if ((!_ep) || (!_req)) {
2640 		if (!_ep)
2641 			pr_err("udc: %s --- _ep == NULL\n", __func__);
2642 
2643 		if (!_req)
2644 			pr_err("udc: %s --- _req == NULL\n", __func__);
2645 
2646 		return -EINVAL;
2647 	}
2648 
2649 	req = container_of(_req, struct nbu2ss_req, req);
2650 	if (unlikely(!_req->complete ||
2651 		     !_req->buf ||
2652 		     !list_empty(&req->queue))) {
2653 		if (!_req->complete)
2654 			pr_err("udc: %s --- !_req->complete\n", __func__);
2655 
2656 		if (!_req->buf)
2657 			pr_err("udc:%s --- !_req->buf\n", __func__);
2658 
2659 		if (!list_empty(&req->queue))
2660 			pr_err("%s --- !list_empty(&req->queue)\n", __func__);
2661 
2662 		return -EINVAL;
2663 	}
2664 
2665 	ep = container_of(_ep, struct nbu2ss_ep, ep);
2666 	udc = ep->udc;
2667 
2668 	if (udc->vbus_active == 0) {
2669 		dev_info(udc->dev, "Can't ep_queue (VBUS OFF)\n");
2670 		return -ESHUTDOWN;
2671 	}
2672 
2673 	if (unlikely(!udc->driver)) {
2674 		dev_err(udc->dev, "%s, bogus device state %p\n", __func__,
2675 			udc->driver);
2676 		return -ESHUTDOWN;
2677 	}
2678 
2679 	spin_lock_irqsave(&udc->lock, flags);
2680 
2681 #ifdef USE_DMA
2682 	if ((uintptr_t)req->req.buf & 0x3)
2683 		req->unaligned = TRUE;
2684 	else
2685 		req->unaligned = FALSE;
2686 
2687 	if (req->unaligned) {
2688 		if (!ep->virt_buf)
2689 			ep->virt_buf = (u8 *)dma_alloc_coherent(
2690 				NULL, PAGE_SIZE,
2691 				&ep->phys_buf, GFP_ATOMIC | GFP_DMA);
2692 		if (ep->epnum > 0)  {
2693 			if (ep->direct == USB_DIR_IN)
2694 				memcpy(ep->virt_buf, req->req.buf,
2695 				       req->req.length);
2696 		}
2697 	}
2698 
2699 	if ((ep->epnum > 0) && (ep->direct == USB_DIR_OUT) &&
2700 	    (req->req.dma != 0))
2701 		_nbu2ss_dma_map_single(udc, ep, req, USB_DIR_OUT);
2702 #endif
2703 
2704 	_req->status = -EINPROGRESS;
2705 	_req->actual = 0;
2706 
2707 	bflag = list_empty(&ep->queue);
2708 	list_add_tail(&req->queue, &ep->queue);
2709 
2710 	if (bflag && !ep->stalled) {
2711 		result = _nbu2ss_start_transfer(udc, ep, req, FALSE);
2712 		if (result < 0) {
2713 			dev_err(udc->dev, " *** %s, result = %d\n", __func__,
2714 				result);
2715 			list_del(&req->queue);
2716 		} else if ((ep->epnum > 0) && (ep->direct == USB_DIR_OUT)) {
2717 #ifdef USE_DMA
2718 			if (req->req.length < 4 &&
2719 			    req->req.length == req->req.actual)
2720 #else
2721 			if (req->req.length == req->req.actual)
2722 #endif
2723 				_nbu2ss_ep_done(ep, req, result);
2724 		}
2725 	}
2726 
2727 	spin_unlock_irqrestore(&udc->lock, flags);
2728 
2729 	return 0;
2730 }
2731 
2732 /*-------------------------------------------------------------------------*/
2733 static int nbu2ss_ep_dequeue(
2734 	struct usb_ep *_ep,
2735 	struct usb_request *_req)
2736 {
2737 	struct nbu2ss_req	*req;
2738 	struct nbu2ss_ep	*ep;
2739 	struct nbu2ss_udc	*udc;
2740 	unsigned long flags;
2741 
2742 	/* catch various bogus parameters */
2743 	if ((!_ep) || (!_req)) {
2744 		/* pr_err("%s, bad param(1)\n", __func__); */
2745 		return -EINVAL;
2746 	}
2747 
2748 	ep = container_of(_ep, struct nbu2ss_ep, ep);
2749 	if (!ep) {
2750 		pr_err("%s, ep == NULL !!\n", __func__);
2751 		return -EINVAL;
2752 	}
2753 
2754 	udc = ep->udc;
2755 	if (!udc)
2756 		return -EINVAL;
2757 
2758 	spin_lock_irqsave(&udc->lock, flags);
2759 
2760 	/* make sure it's actually queued on this endpoint */
2761 	list_for_each_entry(req, &ep->queue, queue) {
2762 		if (&req->req == _req)
2763 			break;
2764 	}
2765 	if (&req->req != _req) {
2766 		spin_unlock_irqrestore(&udc->lock, flags);
2767 		pr_debug("%s no queue(EINVAL)\n", __func__);
2768 		return -EINVAL;
2769 	}
2770 
2771 	_nbu2ss_ep_done(ep, req, -ECONNRESET);
2772 
2773 	spin_unlock_irqrestore(&udc->lock, flags);
2774 
2775 	return 0;
2776 }
2777 
2778 /*-------------------------------------------------------------------------*/
2779 static int nbu2ss_ep_set_halt(struct usb_ep *_ep, int value)
2780 {
2781 	u8		ep_adrs;
2782 	unsigned long	flags;
2783 
2784 	struct nbu2ss_ep	*ep;
2785 	struct nbu2ss_udc	*udc;
2786 
2787 	if (!_ep) {
2788 		pr_err("%s, bad param\n", __func__);
2789 		return -EINVAL;
2790 	}
2791 
2792 	ep = container_of(_ep, struct nbu2ss_ep, ep);
2793 	if (!ep) {
2794 		pr_err("%s, bad ep\n", __func__);
2795 		return -EINVAL;
2796 	}
2797 
2798 	udc = ep->udc;
2799 	if (!udc) {
2800 		dev_err(ep->udc->dev, " *** %s, bad udc\n", __func__);
2801 		return -EINVAL;
2802 	}
2803 
2804 	spin_lock_irqsave(&udc->lock, flags);
2805 
2806 	ep_adrs = ep->epnum | ep->direct;
2807 	if (value == 0) {
2808 		_nbu2ss_set_endpoint_stall(udc, ep_adrs, value);
2809 		ep->stalled = FALSE;
2810 	} else {
2811 		if (list_empty(&ep->queue))
2812 			_nbu2ss_epn_set_stall(udc, ep);
2813 		else
2814 			ep->stalled = TRUE;
2815 	}
2816 
2817 	if (value == 0)
2818 		ep->wedged = 0;
2819 
2820 	spin_unlock_irqrestore(&udc->lock, flags);
2821 
2822 	return 0;
2823 }
2824 
2825 static int nbu2ss_ep_set_wedge(struct usb_ep *_ep)
2826 {
2827 	return nbu2ss_ep_set_halt(_ep, 1);
2828 }
2829 
2830 /*-------------------------------------------------------------------------*/
2831 static int nbu2ss_ep_fifo_status(struct usb_ep *_ep)
2832 {
2833 	u32		data;
2834 	struct nbu2ss_ep	*ep;
2835 	struct nbu2ss_udc	*udc;
2836 	unsigned long		flags;
2837 	struct fc_regs		*preg;
2838 
2839 	if (!_ep) {
2840 		pr_err("%s, bad param\n", __func__);
2841 		return -EINVAL;
2842 	}
2843 
2844 	ep = container_of(_ep, struct nbu2ss_ep, ep);
2845 	if (!ep) {
2846 		pr_err("%s, bad ep\n", __func__);
2847 		return -EINVAL;
2848 	}
2849 
2850 	udc = ep->udc;
2851 	if (!udc) {
2852 		dev_err(ep->udc->dev, "%s, bad udc\n", __func__);
2853 		return -EINVAL;
2854 	}
2855 
2856 	preg = udc->p_regs;
2857 
2858 	data = gpio_get_value(VBUS_VALUE);
2859 	if (data == 0)
2860 		return -EINVAL;
2861 
2862 	spin_lock_irqsave(&udc->lock, flags);
2863 
2864 	if (ep->epnum == 0) {
2865 		data = _nbu2ss_readl(&preg->EP0_LENGTH) & EP0_LDATA;
2866 
2867 	} else {
2868 		data = _nbu2ss_readl(&preg->EP_REGS[ep->epnum - 1].EP_LEN_DCNT)
2869 			& EPN_LDATA;
2870 	}
2871 
2872 	spin_unlock_irqrestore(&udc->lock, flags);
2873 
2874 	return 0;
2875 }
2876 
2877 /*-------------------------------------------------------------------------*/
2878 static void  nbu2ss_ep_fifo_flush(struct usb_ep *_ep)
2879 {
2880 	u32			data;
2881 	struct nbu2ss_ep	*ep;
2882 	struct nbu2ss_udc	*udc;
2883 	unsigned long		flags;
2884 
2885 	if (!_ep) {
2886 		pr_err("udc: %s, bad param\n", __func__);
2887 		return;
2888 	}
2889 
2890 	ep = container_of(_ep, struct nbu2ss_ep, ep);
2891 	if (!ep) {
2892 		pr_err("udc: %s, bad ep\n", __func__);
2893 		return;
2894 	}
2895 
2896 	udc = ep->udc;
2897 	if (!udc) {
2898 		dev_err(ep->udc->dev, "%s, bad udc\n", __func__);
2899 		return;
2900 	}
2901 
2902 	data = gpio_get_value(VBUS_VALUE);
2903 	if (data == 0)
2904 		return;
2905 
2906 	spin_lock_irqsave(&udc->lock, flags);
2907 	_nbu2ss_fifo_flush(udc, ep);
2908 	spin_unlock_irqrestore(&udc->lock, flags);
2909 }
2910 
2911 /*-------------------------------------------------------------------------*/
2912 static const struct usb_ep_ops nbu2ss_ep_ops = {
2913 	.enable		= nbu2ss_ep_enable,
2914 	.disable	= nbu2ss_ep_disable,
2915 
2916 	.alloc_request	= nbu2ss_ep_alloc_request,
2917 	.free_request	= nbu2ss_ep_free_request,
2918 
2919 	.queue		= nbu2ss_ep_queue,
2920 	.dequeue	= nbu2ss_ep_dequeue,
2921 
2922 	.set_halt	= nbu2ss_ep_set_halt,
2923 	.set_wedge	= nbu2ss_ep_set_wedge,
2924 
2925 	.fifo_status	= nbu2ss_ep_fifo_status,
2926 	.fifo_flush	= nbu2ss_ep_fifo_flush,
2927 };
2928 
2929 /*-------------------------------------------------------------------------*/
2930 /* usb_gadget_ops */
2931 
2932 /*-------------------------------------------------------------------------*/
2933 static int nbu2ss_gad_get_frame(struct usb_gadget *pgadget)
2934 {
2935 	u32			data;
2936 	struct nbu2ss_udc	*udc;
2937 
2938 	if (!pgadget) {
2939 		pr_err("udc: %s, bad param\n", __func__);
2940 		return -EINVAL;
2941 	}
2942 
2943 	udc = container_of(pgadget, struct nbu2ss_udc, gadget);
2944 	if (!udc) {
2945 		dev_err(&pgadget->dev, "%s, udc == NULL\n", __func__);
2946 		return -EINVAL;
2947 	}
2948 
2949 	data = gpio_get_value(VBUS_VALUE);
2950 	if (data == 0)
2951 		return -EINVAL;
2952 
2953 	return _nbu2ss_readl(&udc->p_regs->USB_ADDRESS) & FRAME;
2954 }
2955 
2956 /*-------------------------------------------------------------------------*/
2957 static int nbu2ss_gad_wakeup(struct usb_gadget *pgadget)
2958 {
2959 	int	i;
2960 	u32	data;
2961 
2962 	struct nbu2ss_udc	*udc;
2963 
2964 	if (!pgadget) {
2965 		pr_err("%s, bad param\n", __func__);
2966 		return -EINVAL;
2967 	}
2968 
2969 	udc = container_of(pgadget, struct nbu2ss_udc, gadget);
2970 	if (!udc) {
2971 		dev_err(&pgadget->dev, "%s, udc == NULL\n", __func__);
2972 		return -EINVAL;
2973 	}
2974 
2975 	data = gpio_get_value(VBUS_VALUE);
2976 	if (data == 0) {
2977 		dev_warn(&pgadget->dev, "VBUS LEVEL = %d\n", data);
2978 		return -EINVAL;
2979 	}
2980 
2981 	_nbu2ss_bitset(&udc->p_regs->EPCTR, PLL_RESUME);
2982 
2983 	for (i = 0; i < EPC_PLL_LOCK_COUNT; i++) {
2984 		data = _nbu2ss_readl(&udc->p_regs->EPCTR);
2985 
2986 		if (data & PLL_LOCK)
2987 			break;
2988 	}
2989 
2990 	_nbu2ss_bitclr(&udc->p_regs->EPCTR, PLL_RESUME);
2991 
2992 	return 0;
2993 }
2994 
2995 /*-------------------------------------------------------------------------*/
2996 static int nbu2ss_gad_set_selfpowered(struct usb_gadget *pgadget,
2997 				      int is_selfpowered)
2998 {
2999 	struct nbu2ss_udc       *udc;
3000 	unsigned long		flags;
3001 
3002 	if (!pgadget) {
3003 		pr_err("%s, bad param\n", __func__);
3004 		return -EINVAL;
3005 	}
3006 
3007 	udc = container_of(pgadget, struct nbu2ss_udc, gadget);
3008 
3009 	spin_lock_irqsave(&udc->lock, flags);
3010 	pgadget->is_selfpowered = (is_selfpowered != 0);
3011 	spin_unlock_irqrestore(&udc->lock, flags);
3012 
3013 	return 0;
3014 }
3015 
3016 /*-------------------------------------------------------------------------*/
3017 static int nbu2ss_gad_vbus_session(struct usb_gadget *pgadget, int is_active)
3018 {
3019 	return 0;
3020 }
3021 
3022 /*-------------------------------------------------------------------------*/
3023 static int nbu2ss_gad_vbus_draw(struct usb_gadget *pgadget, unsigned int mA)
3024 {
3025 	struct nbu2ss_udc	*udc;
3026 	unsigned long		flags;
3027 
3028 	if (!pgadget) {
3029 		pr_err("%s, bad param\n", __func__);
3030 		return -EINVAL;
3031 	}
3032 
3033 	udc = container_of(pgadget, struct nbu2ss_udc, gadget);
3034 
3035 	spin_lock_irqsave(&udc->lock, flags);
3036 	udc->mA = mA;
3037 	spin_unlock_irqrestore(&udc->lock, flags);
3038 
3039 	return 0;
3040 }
3041 
3042 /*-------------------------------------------------------------------------*/
3043 static int nbu2ss_gad_pullup(struct usb_gadget *pgadget, int is_on)
3044 {
3045 	struct nbu2ss_udc	*udc;
3046 	unsigned long		flags;
3047 
3048 	if (!pgadget) {
3049 		pr_err("%s, bad param\n", __func__);
3050 		return -EINVAL;
3051 	}
3052 
3053 	udc = container_of(pgadget, struct nbu2ss_udc, gadget);
3054 
3055 	if (!udc->driver) {
3056 		pr_warn("%s, Not Regist Driver\n", __func__);
3057 		return -EINVAL;
3058 	}
3059 
3060 	if (udc->vbus_active == 0)
3061 		return -ESHUTDOWN;
3062 
3063 	spin_lock_irqsave(&udc->lock, flags);
3064 	_nbu2ss_pullup(udc, is_on);
3065 	spin_unlock_irqrestore(&udc->lock, flags);
3066 
3067 	return 0;
3068 }
3069 
3070 /*-------------------------------------------------------------------------*/
3071 static int nbu2ss_gad_ioctl(
3072 	struct usb_gadget *pgadget,
3073 	unsigned int code,
3074 	unsigned long param)
3075 {
3076 	return 0;
3077 }
3078 
3079 static const struct usb_gadget_ops nbu2ss_gadget_ops = {
3080 	.get_frame		= nbu2ss_gad_get_frame,
3081 	.wakeup			= nbu2ss_gad_wakeup,
3082 	.set_selfpowered	= nbu2ss_gad_set_selfpowered,
3083 	.vbus_session		= nbu2ss_gad_vbus_session,
3084 	.vbus_draw		= nbu2ss_gad_vbus_draw,
3085 	.pullup			= nbu2ss_gad_pullup,
3086 	.ioctl			= nbu2ss_gad_ioctl,
3087 };
3088 
3089 static const struct {
3090 	const char *name;
3091 	const struct usb_ep_caps caps;
3092 } ep_info[NUM_ENDPOINTS] = {
3093 #define EP_INFO(_name, _caps) \
3094 	{ \
3095 		.name = _name, \
3096 		.caps = _caps, \
3097 	}
3098 
3099 	EP_INFO("ep0",
3100 		USB_EP_CAPS(USB_EP_CAPS_TYPE_CONTROL, USB_EP_CAPS_DIR_ALL)),
3101 	EP_INFO("ep1-bulk",
3102 		USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_ALL)),
3103 	EP_INFO("ep2-bulk",
3104 		USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_ALL)),
3105 	EP_INFO("ep3in-int",
3106 		USB_EP_CAPS(USB_EP_CAPS_TYPE_INT, USB_EP_CAPS_DIR_IN)),
3107 	EP_INFO("ep4-iso",
3108 		USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO, USB_EP_CAPS_DIR_ALL)),
3109 	EP_INFO("ep5-iso",
3110 		USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO, USB_EP_CAPS_DIR_ALL)),
3111 	EP_INFO("ep6-bulk",
3112 		USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_ALL)),
3113 	EP_INFO("ep7-bulk",
3114 		USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_ALL)),
3115 	EP_INFO("ep8in-int",
3116 		USB_EP_CAPS(USB_EP_CAPS_TYPE_INT, USB_EP_CAPS_DIR_IN)),
3117 	EP_INFO("ep9-iso",
3118 		USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO, USB_EP_CAPS_DIR_ALL)),
3119 	EP_INFO("epa-iso",
3120 		USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO, USB_EP_CAPS_DIR_ALL)),
3121 	EP_INFO("epb-bulk",
3122 		USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_ALL)),
3123 	EP_INFO("epc-bulk",
3124 		USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_ALL)),
3125 	EP_INFO("epdin-int",
3126 		USB_EP_CAPS(USB_EP_CAPS_TYPE_INT, USB_EP_CAPS_DIR_IN)),
3127 
3128 #undef EP_INFO
3129 };
3130 
3131 /*-------------------------------------------------------------------------*/
3132 static void nbu2ss_drv_ep_init(struct nbu2ss_udc *udc)
3133 {
3134 	int	i;
3135 
3136 	INIT_LIST_HEAD(&udc->gadget.ep_list);
3137 	udc->gadget.ep0 = &udc->ep[0].ep;
3138 
3139 	for (i = 0; i < NUM_ENDPOINTS; i++) {
3140 		struct nbu2ss_ep *ep = &udc->ep[i];
3141 
3142 		ep->udc = udc;
3143 		ep->desc = NULL;
3144 
3145 		ep->ep.driver_data = NULL;
3146 		ep->ep.name = ep_info[i].name;
3147 		ep->ep.caps = ep_info[i].caps;
3148 		ep->ep.ops = &nbu2ss_ep_ops;
3149 
3150 		usb_ep_set_maxpacket_limit(&ep->ep,
3151 					   i == 0 ? EP0_PACKETSIZE
3152 					   : EP_PACKETSIZE);
3153 
3154 		list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
3155 		INIT_LIST_HEAD(&ep->queue);
3156 	}
3157 
3158 	list_del_init(&udc->ep[0].ep.ep_list);
3159 }
3160 
3161 /*-------------------------------------------------------------------------*/
3162 /* platform_driver */
3163 static int nbu2ss_drv_contest_init(
3164 	struct platform_device *pdev,
3165 	struct nbu2ss_udc *udc)
3166 {
3167 	spin_lock_init(&udc->lock);
3168 	udc->dev = &pdev->dev;
3169 
3170 	udc->gadget.is_selfpowered = 1;
3171 	udc->devstate = USB_STATE_NOTATTACHED;
3172 	udc->pdev = pdev;
3173 	udc->mA = 0;
3174 
3175 	udc->pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
3176 
3177 	/* init Endpoint */
3178 	nbu2ss_drv_ep_init(udc);
3179 
3180 	/* init Gadget */
3181 	udc->gadget.ops = &nbu2ss_gadget_ops;
3182 	udc->gadget.ep0 = &udc->ep[0].ep;
3183 	udc->gadget.speed = USB_SPEED_UNKNOWN;
3184 	udc->gadget.name = driver_name;
3185 	/* udc->gadget.is_dualspeed = 1; */
3186 
3187 	device_initialize(&udc->gadget.dev);
3188 
3189 	dev_set_name(&udc->gadget.dev, "gadget");
3190 	udc->gadget.dev.parent = &pdev->dev;
3191 	udc->gadget.dev.dma_mask = pdev->dev.dma_mask;
3192 
3193 	return 0;
3194 }
3195 
3196 /*
3197  *	probe - binds to the platform device
3198  */
3199 static int nbu2ss_drv_probe(struct platform_device *pdev)
3200 {
3201 	int	status = -ENODEV;
3202 	struct nbu2ss_udc	*udc;
3203 	struct resource *r;
3204 	int irq;
3205 	void __iomem *mmio_base;
3206 
3207 	udc = &udc_controller;
3208 	memset(udc, 0, sizeof(struct nbu2ss_udc));
3209 
3210 	platform_set_drvdata(pdev, udc);
3211 
3212 	/* require I/O memory and IRQ to be provided as resources */
3213 	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3214 	mmio_base = devm_ioremap_resource(&pdev->dev, r);
3215 	if (IS_ERR(mmio_base))
3216 		return PTR_ERR(mmio_base);
3217 
3218 	irq = platform_get_irq(pdev, 0);
3219 	if (irq < 0) {
3220 		dev_err(&pdev->dev, "failed to get IRQ\n");
3221 		return irq;
3222 	}
3223 	status = devm_request_irq(&pdev->dev, irq, _nbu2ss_udc_irq,
3224 				  0, driver_name, udc);
3225 
3226 	/* IO Memory */
3227 	udc->p_regs = (struct fc_regs *)mmio_base;
3228 
3229 	/* USB Function Controller Interrupt */
3230 	if (status != 0) {
3231 		dev_err(udc->dev, "request_irq(USB_UDC_IRQ_1) failed\n");
3232 		return status;
3233 	}
3234 
3235 	/* Driver Initialization */
3236 	status = nbu2ss_drv_contest_init(pdev, udc);
3237 	if (status < 0) {
3238 		/* Error */
3239 		return status;
3240 	}
3241 
3242 	/* VBUS Interrupt */
3243 	irq_set_irq_type(INT_VBUS, IRQ_TYPE_EDGE_BOTH);
3244 	status = request_irq(INT_VBUS,
3245 			     _nbu2ss_vbus_irq, IRQF_SHARED, driver_name, udc);
3246 
3247 	if (status != 0) {
3248 		dev_err(udc->dev, "request_irq(INT_VBUS) failed\n");
3249 		return status;
3250 	}
3251 
3252 	return status;
3253 }
3254 
3255 /*-------------------------------------------------------------------------*/
3256 static void nbu2ss_drv_shutdown(struct platform_device *pdev)
3257 {
3258 	struct nbu2ss_udc	*udc;
3259 
3260 	udc = platform_get_drvdata(pdev);
3261 	if (!udc)
3262 		return;
3263 
3264 	_nbu2ss_disable_controller(udc);
3265 }
3266 
3267 /*-------------------------------------------------------------------------*/
3268 static int nbu2ss_drv_remove(struct platform_device *pdev)
3269 {
3270 	struct nbu2ss_udc	*udc;
3271 	struct nbu2ss_ep	*ep;
3272 	int	i;
3273 
3274 	udc = &udc_controller;
3275 
3276 	for (i = 0; i < NUM_ENDPOINTS; i++) {
3277 		ep = &udc->ep[i];
3278 		if (ep->virt_buf)
3279 			dma_free_coherent(NULL, PAGE_SIZE, (void *)ep->virt_buf,
3280 					  ep->phys_buf);
3281 	}
3282 
3283 	/* Interrupt Handler - Release */
3284 	free_irq(INT_VBUS, udc);
3285 
3286 	return 0;
3287 }
3288 
3289 /*-------------------------------------------------------------------------*/
3290 static int nbu2ss_drv_suspend(struct platform_device *pdev, pm_message_t state)
3291 {
3292 	struct nbu2ss_udc	*udc;
3293 
3294 	udc = platform_get_drvdata(pdev);
3295 	if (!udc)
3296 		return 0;
3297 
3298 	if (udc->vbus_active) {
3299 		udc->vbus_active = 0;
3300 		udc->devstate = USB_STATE_NOTATTACHED;
3301 		udc->linux_suspended = 1;
3302 
3303 		if (udc->usb_suspended) {
3304 			udc->usb_suspended = 0;
3305 			_nbu2ss_reset_controller(udc);
3306 		}
3307 
3308 		_nbu2ss_quiesce(udc);
3309 	}
3310 	_nbu2ss_disable_controller(udc);
3311 
3312 	return 0;
3313 }
3314 
3315 /*-------------------------------------------------------------------------*/
3316 static int nbu2ss_drv_resume(struct platform_device *pdev)
3317 {
3318 	u32	data;
3319 	struct nbu2ss_udc	*udc;
3320 
3321 	udc = platform_get_drvdata(pdev);
3322 	if (!udc)
3323 		return 0;
3324 
3325 	data = gpio_get_value(VBUS_VALUE);
3326 	if (data) {
3327 		udc->vbus_active = 1;
3328 		udc->devstate = USB_STATE_POWERED;
3329 		_nbu2ss_enable_controller(udc);
3330 		_nbu2ss_pullup(udc, 1);
3331 	}
3332 
3333 	udc->linux_suspended = 0;
3334 
3335 	return 0;
3336 }
3337 
3338 static struct platform_driver udc_driver = {
3339 	.probe		= nbu2ss_drv_probe,
3340 	.shutdown	= nbu2ss_drv_shutdown,
3341 	.remove		= nbu2ss_drv_remove,
3342 	.suspend	= nbu2ss_drv_suspend,
3343 	.resume		= nbu2ss_drv_resume,
3344 	.driver		= {
3345 		.name	= driver_name,
3346 	},
3347 };
3348 
3349 module_platform_driver(udc_driver);
3350 
3351 MODULE_DESCRIPTION(DRIVER_DESC);
3352 MODULE_AUTHOR("Renesas Electronics Corporation");
3353 MODULE_LICENSE("GPL");
3354