1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * FOTG210 UDC Driver supports Bulk transfer so far
4  *
5  * Copyright (C) 2013 Faraday Technology Corporation
6  *
7  * Author : Yuan-Hsin Chen <yhchen@faraday-tech.com>
8  */
9 
10 #include <linux/dma-mapping.h>
11 #include <linux/err.h>
12 #include <linux/interrupt.h>
13 #include <linux/io.h>
14 #include <linux/module.h>
15 #include <linux/platform_device.h>
16 #include <linux/usb/ch9.h>
17 #include <linux/usb/gadget.h>
18 #include <linux/usb/otg.h>
19 #include <linux/usb/phy.h>
20 
21 #include "fotg210.h"
22 #include "fotg210-udc.h"
23 
24 #define	DRIVER_DESC	"FOTG210 USB Device Controller Driver"
25 #define	DRIVER_VERSION	"30-April-2013"
26 
27 static const char udc_name[] = "fotg210_udc";
28 static const char * const fotg210_ep_name[] = {
29 	"ep0", "ep1", "ep2", "ep3", "ep4"};
30 
31 static void fotg210_ack_int(struct fotg210_udc *fotg210, u32 offset, u32 mask)
32 {
33 	u32 value = ioread32(fotg210->reg + offset);
34 
35 	value &= ~mask;
36 	iowrite32(value, fotg210->reg + offset);
37 }
38 
39 static void fotg210_disable_fifo_int(struct fotg210_ep *ep)
40 {
41 	u32 value = ioread32(ep->fotg210->reg + FOTG210_DMISGR1);
42 
43 	if (ep->dir_in)
44 		value |= DMISGR1_MF_IN_INT(ep->epnum - 1);
45 	else
46 		value |= DMISGR1_MF_OUTSPK_INT(ep->epnum - 1);
47 	iowrite32(value, ep->fotg210->reg + FOTG210_DMISGR1);
48 }
49 
50 static void fotg210_enable_fifo_int(struct fotg210_ep *ep)
51 {
52 	u32 value = ioread32(ep->fotg210->reg + FOTG210_DMISGR1);
53 
54 	if (ep->dir_in)
55 		value &= ~DMISGR1_MF_IN_INT(ep->epnum - 1);
56 	else
57 		value &= ~DMISGR1_MF_OUTSPK_INT(ep->epnum - 1);
58 	iowrite32(value, ep->fotg210->reg + FOTG210_DMISGR1);
59 }
60 
61 static void fotg210_set_cxdone(struct fotg210_udc *fotg210)
62 {
63 	u32 value = ioread32(fotg210->reg + FOTG210_DCFESR);
64 
65 	value |= DCFESR_CX_DONE;
66 	iowrite32(value, fotg210->reg + FOTG210_DCFESR);
67 }
68 
69 static void fotg210_done(struct fotg210_ep *ep, struct fotg210_request *req,
70 			int status)
71 {
72 	list_del_init(&req->queue);
73 
74 	/* don't modify queue heads during completion callback */
75 	if (ep->fotg210->gadget.speed == USB_SPEED_UNKNOWN)
76 		req->req.status = -ESHUTDOWN;
77 	else
78 		req->req.status = status;
79 
80 	spin_unlock(&ep->fotg210->lock);
81 	usb_gadget_giveback_request(&ep->ep, &req->req);
82 	spin_lock(&ep->fotg210->lock);
83 
84 	if (ep->epnum) {
85 		if (list_empty(&ep->queue))
86 			fotg210_disable_fifo_int(ep);
87 	} else {
88 		fotg210_set_cxdone(ep->fotg210);
89 	}
90 }
91 
92 static void fotg210_fifo_ep_mapping(struct fotg210_ep *ep, u32 epnum,
93 				u32 dir_in)
94 {
95 	struct fotg210_udc *fotg210 = ep->fotg210;
96 	u32 val;
97 
98 	/* Driver should map an ep to a fifo and then map the fifo
99 	 * to the ep. What a brain-damaged design!
100 	 */
101 
102 	/* map a fifo to an ep */
103 	val = ioread32(fotg210->reg + FOTG210_EPMAP);
104 	val &= ~EPMAP_FIFONOMSK(epnum, dir_in);
105 	val |= EPMAP_FIFONO(epnum, dir_in);
106 	iowrite32(val, fotg210->reg + FOTG210_EPMAP);
107 
108 	/* map the ep to the fifo */
109 	val = ioread32(fotg210->reg + FOTG210_FIFOMAP);
110 	val &= ~FIFOMAP_EPNOMSK(epnum);
111 	val |= FIFOMAP_EPNO(epnum);
112 	iowrite32(val, fotg210->reg + FOTG210_FIFOMAP);
113 
114 	/* enable fifo */
115 	val = ioread32(fotg210->reg + FOTG210_FIFOCF);
116 	val |= FIFOCF_FIFO_EN(epnum - 1);
117 	iowrite32(val, fotg210->reg + FOTG210_FIFOCF);
118 }
119 
120 static void fotg210_set_fifo_dir(struct fotg210_ep *ep, u32 epnum, u32 dir_in)
121 {
122 	struct fotg210_udc *fotg210 = ep->fotg210;
123 	u32 val;
124 
125 	val = ioread32(fotg210->reg + FOTG210_FIFOMAP);
126 	val |= (dir_in ? FIFOMAP_DIRIN(epnum - 1) : FIFOMAP_DIROUT(epnum - 1));
127 	iowrite32(val, fotg210->reg + FOTG210_FIFOMAP);
128 }
129 
130 static void fotg210_set_tfrtype(struct fotg210_ep *ep, u32 epnum, u32 type)
131 {
132 	struct fotg210_udc *fotg210 = ep->fotg210;
133 	u32 val;
134 
135 	val = ioread32(fotg210->reg + FOTG210_FIFOCF);
136 	val |= FIFOCF_TYPE(type, epnum - 1);
137 	iowrite32(val, fotg210->reg + FOTG210_FIFOCF);
138 }
139 
140 static void fotg210_set_mps(struct fotg210_ep *ep, u32 epnum, u32 mps,
141 				u32 dir_in)
142 {
143 	struct fotg210_udc *fotg210 = ep->fotg210;
144 	u32 val;
145 	u32 offset = dir_in ? FOTG210_INEPMPSR(epnum) :
146 				FOTG210_OUTEPMPSR(epnum);
147 
148 	val = ioread32(fotg210->reg + offset);
149 	val |= INOUTEPMPSR_MPS(mps);
150 	iowrite32(val, fotg210->reg + offset);
151 }
152 
153 static int fotg210_config_ep(struct fotg210_ep *ep,
154 		     const struct usb_endpoint_descriptor *desc)
155 {
156 	struct fotg210_udc *fotg210 = ep->fotg210;
157 
158 	fotg210_set_fifo_dir(ep, ep->epnum, ep->dir_in);
159 	fotg210_set_tfrtype(ep, ep->epnum, ep->type);
160 	fotg210_set_mps(ep, ep->epnum, ep->ep.maxpacket, ep->dir_in);
161 	fotg210_fifo_ep_mapping(ep, ep->epnum, ep->dir_in);
162 
163 	fotg210->ep[ep->epnum] = ep;
164 
165 	return 0;
166 }
167 
168 static int fotg210_ep_enable(struct usb_ep *_ep,
169 			  const struct usb_endpoint_descriptor *desc)
170 {
171 	struct fotg210_ep *ep;
172 
173 	ep = container_of(_ep, struct fotg210_ep, ep);
174 
175 	ep->desc = desc;
176 	ep->epnum = usb_endpoint_num(desc);
177 	ep->type = usb_endpoint_type(desc);
178 	ep->dir_in = usb_endpoint_dir_in(desc);
179 	ep->ep.maxpacket = usb_endpoint_maxp(desc);
180 
181 	return fotg210_config_ep(ep, desc);
182 }
183 
184 static void fotg210_reset_tseq(struct fotg210_udc *fotg210, u8 epnum)
185 {
186 	struct fotg210_ep *ep = fotg210->ep[epnum];
187 	u32 value;
188 	void __iomem *reg;
189 
190 	reg = (ep->dir_in) ?
191 		fotg210->reg + FOTG210_INEPMPSR(epnum) :
192 		fotg210->reg + FOTG210_OUTEPMPSR(epnum);
193 
194 	/* Note: Driver needs to set and clear INOUTEPMPSR_RESET_TSEQ
195 	 *	 bit. Controller wouldn't clear this bit. WTF!!!
196 	 */
197 
198 	value = ioread32(reg);
199 	value |= INOUTEPMPSR_RESET_TSEQ;
200 	iowrite32(value, reg);
201 
202 	value = ioread32(reg);
203 	value &= ~INOUTEPMPSR_RESET_TSEQ;
204 	iowrite32(value, reg);
205 }
206 
207 static int fotg210_ep_release(struct fotg210_ep *ep)
208 {
209 	if (!ep->epnum)
210 		return 0;
211 	ep->epnum = 0;
212 	ep->stall = 0;
213 	ep->wedged = 0;
214 
215 	fotg210_reset_tseq(ep->fotg210, ep->epnum);
216 
217 	return 0;
218 }
219 
220 static int fotg210_ep_disable(struct usb_ep *_ep)
221 {
222 	struct fotg210_ep *ep;
223 	struct fotg210_request *req;
224 	unsigned long flags;
225 
226 	BUG_ON(!_ep);
227 
228 	ep = container_of(_ep, struct fotg210_ep, ep);
229 
230 	while (!list_empty(&ep->queue)) {
231 		req = list_entry(ep->queue.next,
232 			struct fotg210_request, queue);
233 		spin_lock_irqsave(&ep->fotg210->lock, flags);
234 		fotg210_done(ep, req, -ECONNRESET);
235 		spin_unlock_irqrestore(&ep->fotg210->lock, flags);
236 	}
237 
238 	return fotg210_ep_release(ep);
239 }
240 
241 static struct usb_request *fotg210_ep_alloc_request(struct usb_ep *_ep,
242 						gfp_t gfp_flags)
243 {
244 	struct fotg210_request *req;
245 
246 	req = kzalloc(sizeof(struct fotg210_request), gfp_flags);
247 	if (!req)
248 		return NULL;
249 
250 	INIT_LIST_HEAD(&req->queue);
251 
252 	return &req->req;
253 }
254 
255 static void fotg210_ep_free_request(struct usb_ep *_ep,
256 					struct usb_request *_req)
257 {
258 	struct fotg210_request *req;
259 
260 	req = container_of(_req, struct fotg210_request, req);
261 	kfree(req);
262 }
263 
264 static void fotg210_enable_dma(struct fotg210_ep *ep,
265 			      dma_addr_t d, u32 len)
266 {
267 	u32 value;
268 	struct fotg210_udc *fotg210 = ep->fotg210;
269 
270 	/* set transfer length and direction */
271 	value = ioread32(fotg210->reg + FOTG210_DMACPSR1);
272 	value &= ~(DMACPSR1_DMA_LEN(0xFFFF) | DMACPSR1_DMA_TYPE(1));
273 	value |= DMACPSR1_DMA_LEN(len) | DMACPSR1_DMA_TYPE(ep->dir_in);
274 	iowrite32(value, fotg210->reg + FOTG210_DMACPSR1);
275 
276 	/* set device DMA target FIFO number */
277 	value = ioread32(fotg210->reg + FOTG210_DMATFNR);
278 	if (ep->epnum)
279 		value |= DMATFNR_ACC_FN(ep->epnum - 1);
280 	else
281 		value |= DMATFNR_ACC_CXF;
282 	iowrite32(value, fotg210->reg + FOTG210_DMATFNR);
283 
284 	/* set DMA memory address */
285 	iowrite32(d, fotg210->reg + FOTG210_DMACPSR2);
286 
287 	/* enable MDMA_EROR and MDMA_CMPLT interrupt */
288 	value = ioread32(fotg210->reg + FOTG210_DMISGR2);
289 	value &= ~(DMISGR2_MDMA_CMPLT | DMISGR2_MDMA_ERROR);
290 	iowrite32(value, fotg210->reg + FOTG210_DMISGR2);
291 
292 	/* start DMA */
293 	value = ioread32(fotg210->reg + FOTG210_DMACPSR1);
294 	value |= DMACPSR1_DMA_START;
295 	iowrite32(value, fotg210->reg + FOTG210_DMACPSR1);
296 }
297 
298 static void fotg210_disable_dma(struct fotg210_ep *ep)
299 {
300 	iowrite32(DMATFNR_DISDMA, ep->fotg210->reg + FOTG210_DMATFNR);
301 }
302 
303 static void fotg210_wait_dma_done(struct fotg210_ep *ep)
304 {
305 	u32 value;
306 
307 	do {
308 		value = ioread32(ep->fotg210->reg + FOTG210_DISGR2);
309 		if ((value & DISGR2_USBRST_INT) ||
310 		    (value & DISGR2_DMA_ERROR))
311 			goto dma_reset;
312 	} while (!(value & DISGR2_DMA_CMPLT));
313 
314 	fotg210_ack_int(ep->fotg210, FOTG210_DISGR2, DISGR2_DMA_CMPLT);
315 	return;
316 
317 dma_reset:
318 	value = ioread32(ep->fotg210->reg + FOTG210_DMACPSR1);
319 	value |= DMACPSR1_DMA_ABORT;
320 	iowrite32(value, ep->fotg210->reg + FOTG210_DMACPSR1);
321 
322 	/* reset fifo */
323 	if (ep->epnum) {
324 		value = ioread32(ep->fotg210->reg +
325 				FOTG210_FIBCR(ep->epnum - 1));
326 		value |= FIBCR_FFRST;
327 		iowrite32(value, ep->fotg210->reg +
328 				FOTG210_FIBCR(ep->epnum - 1));
329 	} else {
330 		value = ioread32(ep->fotg210->reg + FOTG210_DCFESR);
331 		value |= DCFESR_CX_CLR;
332 		iowrite32(value, ep->fotg210->reg + FOTG210_DCFESR);
333 	}
334 }
335 
336 static void fotg210_start_dma(struct fotg210_ep *ep,
337 			struct fotg210_request *req)
338 {
339 	struct device *dev = &ep->fotg210->gadget.dev;
340 	dma_addr_t d;
341 	u8 *buffer;
342 	u32 length;
343 
344 	if (ep->epnum) {
345 		if (ep->dir_in) {
346 			buffer = req->req.buf;
347 			length = req->req.length;
348 		} else {
349 			buffer = req->req.buf + req->req.actual;
350 			length = ioread32(ep->fotg210->reg +
351 					FOTG210_FIBCR(ep->epnum - 1)) & FIBCR_BCFX;
352 			if (length > req->req.length - req->req.actual)
353 				length = req->req.length - req->req.actual;
354 		}
355 	} else {
356 		buffer = req->req.buf + req->req.actual;
357 		if (req->req.length - req->req.actual > ep->ep.maxpacket)
358 			length = ep->ep.maxpacket;
359 		else
360 			length = req->req.length - req->req.actual;
361 	}
362 
363 	d = dma_map_single(dev, buffer, length,
364 			ep->dir_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
365 
366 	if (dma_mapping_error(dev, d)) {
367 		pr_err("dma_mapping_error\n");
368 		return;
369 	}
370 
371 	fotg210_enable_dma(ep, d, length);
372 
373 	/* check if dma is done */
374 	fotg210_wait_dma_done(ep);
375 
376 	fotg210_disable_dma(ep);
377 
378 	/* update actual transfer length */
379 	req->req.actual += length;
380 
381 	dma_unmap_single(dev, d, length, DMA_TO_DEVICE);
382 }
383 
384 static void fotg210_ep0_queue(struct fotg210_ep *ep,
385 				struct fotg210_request *req)
386 {
387 	if (!req->req.length) {
388 		fotg210_done(ep, req, 0);
389 		return;
390 	}
391 	if (ep->dir_in) { /* if IN */
392 		fotg210_start_dma(ep, req);
393 		if (req->req.length == req->req.actual)
394 			fotg210_done(ep, req, 0);
395 	} else { /* OUT */
396 		u32 value = ioread32(ep->fotg210->reg + FOTG210_DMISGR0);
397 
398 		value &= ~DMISGR0_MCX_OUT_INT;
399 		iowrite32(value, ep->fotg210->reg + FOTG210_DMISGR0);
400 	}
401 }
402 
403 static int fotg210_ep_queue(struct usb_ep *_ep, struct usb_request *_req,
404 				gfp_t gfp_flags)
405 {
406 	struct fotg210_ep *ep;
407 	struct fotg210_request *req;
408 	unsigned long flags;
409 	int request = 0;
410 
411 	ep = container_of(_ep, struct fotg210_ep, ep);
412 	req = container_of(_req, struct fotg210_request, req);
413 
414 	if (ep->fotg210->gadget.speed == USB_SPEED_UNKNOWN)
415 		return -ESHUTDOWN;
416 
417 	spin_lock_irqsave(&ep->fotg210->lock, flags);
418 
419 	if (list_empty(&ep->queue))
420 		request = 1;
421 
422 	list_add_tail(&req->queue, &ep->queue);
423 
424 	req->req.actual = 0;
425 	req->req.status = -EINPROGRESS;
426 
427 	if (!ep->epnum) /* ep0 */
428 		fotg210_ep0_queue(ep, req);
429 	else if (request && !ep->stall)
430 		fotg210_enable_fifo_int(ep);
431 
432 	spin_unlock_irqrestore(&ep->fotg210->lock, flags);
433 
434 	return 0;
435 }
436 
437 static int fotg210_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
438 {
439 	struct fotg210_ep *ep;
440 	struct fotg210_request *req;
441 	unsigned long flags;
442 
443 	ep = container_of(_ep, struct fotg210_ep, ep);
444 	req = container_of(_req, struct fotg210_request, req);
445 
446 	spin_lock_irqsave(&ep->fotg210->lock, flags);
447 	if (!list_empty(&ep->queue))
448 		fotg210_done(ep, req, -ECONNRESET);
449 	spin_unlock_irqrestore(&ep->fotg210->lock, flags);
450 
451 	return 0;
452 }
453 
454 static void fotg210_set_epnstall(struct fotg210_ep *ep)
455 {
456 	struct fotg210_udc *fotg210 = ep->fotg210;
457 	u32 value;
458 	void __iomem *reg;
459 
460 	/* check if IN FIFO is empty before stall */
461 	if (ep->dir_in) {
462 		do {
463 			value = ioread32(fotg210->reg + FOTG210_DCFESR);
464 		} while (!(value & DCFESR_FIFO_EMPTY(ep->epnum - 1)));
465 	}
466 
467 	reg = (ep->dir_in) ?
468 		fotg210->reg + FOTG210_INEPMPSR(ep->epnum) :
469 		fotg210->reg + FOTG210_OUTEPMPSR(ep->epnum);
470 	value = ioread32(reg);
471 	value |= INOUTEPMPSR_STL_EP;
472 	iowrite32(value, reg);
473 }
474 
475 static void fotg210_clear_epnstall(struct fotg210_ep *ep)
476 {
477 	struct fotg210_udc *fotg210 = ep->fotg210;
478 	u32 value;
479 	void __iomem *reg;
480 
481 	reg = (ep->dir_in) ?
482 		fotg210->reg + FOTG210_INEPMPSR(ep->epnum) :
483 		fotg210->reg + FOTG210_OUTEPMPSR(ep->epnum);
484 	value = ioread32(reg);
485 	value &= ~INOUTEPMPSR_STL_EP;
486 	iowrite32(value, reg);
487 }
488 
489 static int fotg210_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedge)
490 {
491 	struct fotg210_ep *ep;
492 	struct fotg210_udc *fotg210;
493 	unsigned long flags;
494 
495 	ep = container_of(_ep, struct fotg210_ep, ep);
496 
497 	fotg210 = ep->fotg210;
498 
499 	spin_lock_irqsave(&ep->fotg210->lock, flags);
500 
501 	if (value) {
502 		fotg210_set_epnstall(ep);
503 		ep->stall = 1;
504 		if (wedge)
505 			ep->wedged = 1;
506 	} else {
507 		fotg210_reset_tseq(fotg210, ep->epnum);
508 		fotg210_clear_epnstall(ep);
509 		ep->stall = 0;
510 		ep->wedged = 0;
511 		if (!list_empty(&ep->queue))
512 			fotg210_enable_fifo_int(ep);
513 	}
514 
515 	spin_unlock_irqrestore(&ep->fotg210->lock, flags);
516 	return 0;
517 }
518 
519 static int fotg210_ep_set_halt(struct usb_ep *_ep, int value)
520 {
521 	return fotg210_set_halt_and_wedge(_ep, value, 0);
522 }
523 
524 static int fotg210_ep_set_wedge(struct usb_ep *_ep)
525 {
526 	return fotg210_set_halt_and_wedge(_ep, 1, 1);
527 }
528 
529 static void fotg210_ep_fifo_flush(struct usb_ep *_ep)
530 {
531 }
532 
533 static const struct usb_ep_ops fotg210_ep_ops = {
534 	.enable		= fotg210_ep_enable,
535 	.disable	= fotg210_ep_disable,
536 
537 	.alloc_request	= fotg210_ep_alloc_request,
538 	.free_request	= fotg210_ep_free_request,
539 
540 	.queue		= fotg210_ep_queue,
541 	.dequeue	= fotg210_ep_dequeue,
542 
543 	.set_halt	= fotg210_ep_set_halt,
544 	.fifo_flush	= fotg210_ep_fifo_flush,
545 	.set_wedge	= fotg210_ep_set_wedge,
546 };
547 
548 static void fotg210_clear_tx0byte(struct fotg210_udc *fotg210)
549 {
550 	u32 value = ioread32(fotg210->reg + FOTG210_TX0BYTE);
551 
552 	value &= ~(TX0BYTE_EP1 | TX0BYTE_EP2 | TX0BYTE_EP3
553 		   | TX0BYTE_EP4);
554 	iowrite32(value, fotg210->reg + FOTG210_TX0BYTE);
555 }
556 
557 static void fotg210_clear_rx0byte(struct fotg210_udc *fotg210)
558 {
559 	u32 value = ioread32(fotg210->reg + FOTG210_RX0BYTE);
560 
561 	value &= ~(RX0BYTE_EP1 | RX0BYTE_EP2 | RX0BYTE_EP3
562 		   | RX0BYTE_EP4);
563 	iowrite32(value, fotg210->reg + FOTG210_RX0BYTE);
564 }
565 
566 /* read 8-byte setup packet only */
567 static void fotg210_rdsetupp(struct fotg210_udc *fotg210,
568 		   u8 *buffer)
569 {
570 	int i = 0;
571 	u8 *tmp = buffer;
572 	u32 data;
573 	u32 length = 8;
574 
575 	iowrite32(DMATFNR_ACC_CXF, fotg210->reg + FOTG210_DMATFNR);
576 
577 	for (i = (length >> 2); i > 0; i--) {
578 		data = ioread32(fotg210->reg + FOTG210_CXPORT);
579 		*tmp = data & 0xFF;
580 		*(tmp + 1) = (data >> 8) & 0xFF;
581 		*(tmp + 2) = (data >> 16) & 0xFF;
582 		*(tmp + 3) = (data >> 24) & 0xFF;
583 		tmp = tmp + 4;
584 	}
585 
586 	switch (length % 4) {
587 	case 1:
588 		data = ioread32(fotg210->reg + FOTG210_CXPORT);
589 		*tmp = data & 0xFF;
590 		break;
591 	case 2:
592 		data = ioread32(fotg210->reg + FOTG210_CXPORT);
593 		*tmp = data & 0xFF;
594 		*(tmp + 1) = (data >> 8) & 0xFF;
595 		break;
596 	case 3:
597 		data = ioread32(fotg210->reg + FOTG210_CXPORT);
598 		*tmp = data & 0xFF;
599 		*(tmp + 1) = (data >> 8) & 0xFF;
600 		*(tmp + 2) = (data >> 16) & 0xFF;
601 		break;
602 	default:
603 		break;
604 	}
605 
606 	iowrite32(DMATFNR_DISDMA, fotg210->reg + FOTG210_DMATFNR);
607 }
608 
609 static void fotg210_set_configuration(struct fotg210_udc *fotg210)
610 {
611 	u32 value = ioread32(fotg210->reg + FOTG210_DAR);
612 
613 	value |= DAR_AFT_CONF;
614 	iowrite32(value, fotg210->reg + FOTG210_DAR);
615 }
616 
617 static void fotg210_set_dev_addr(struct fotg210_udc *fotg210, u32 addr)
618 {
619 	u32 value = ioread32(fotg210->reg + FOTG210_DAR);
620 
621 	value |= (addr & 0x7F);
622 	iowrite32(value, fotg210->reg + FOTG210_DAR);
623 }
624 
625 static void fotg210_set_cxstall(struct fotg210_udc *fotg210)
626 {
627 	u32 value = ioread32(fotg210->reg + FOTG210_DCFESR);
628 
629 	value |= DCFESR_CX_STL;
630 	iowrite32(value, fotg210->reg + FOTG210_DCFESR);
631 }
632 
633 static void fotg210_request_error(struct fotg210_udc *fotg210)
634 {
635 	fotg210_set_cxstall(fotg210);
636 	pr_err("request error!!\n");
637 }
638 
639 static void fotg210_set_address(struct fotg210_udc *fotg210,
640 				struct usb_ctrlrequest *ctrl)
641 {
642 	if (le16_to_cpu(ctrl->wValue) >= 0x0100) {
643 		fotg210_request_error(fotg210);
644 	} else {
645 		fotg210_set_dev_addr(fotg210, le16_to_cpu(ctrl->wValue));
646 		fotg210_set_cxdone(fotg210);
647 	}
648 }
649 
650 static void fotg210_set_feature(struct fotg210_udc *fotg210,
651 				struct usb_ctrlrequest *ctrl)
652 {
653 	switch (ctrl->bRequestType & USB_RECIP_MASK) {
654 	case USB_RECIP_DEVICE:
655 		fotg210_set_cxdone(fotg210);
656 		break;
657 	case USB_RECIP_INTERFACE:
658 		fotg210_set_cxdone(fotg210);
659 		break;
660 	case USB_RECIP_ENDPOINT: {
661 		u8 epnum;
662 		epnum = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK;
663 		if (epnum)
664 			fotg210_set_epnstall(fotg210->ep[epnum]);
665 		else
666 			fotg210_set_cxstall(fotg210);
667 		fotg210_set_cxdone(fotg210);
668 		}
669 		break;
670 	default:
671 		fotg210_request_error(fotg210);
672 		break;
673 	}
674 }
675 
676 static void fotg210_clear_feature(struct fotg210_udc *fotg210,
677 				struct usb_ctrlrequest *ctrl)
678 {
679 	struct fotg210_ep *ep =
680 		fotg210->ep[ctrl->wIndex & USB_ENDPOINT_NUMBER_MASK];
681 
682 	switch (ctrl->bRequestType & USB_RECIP_MASK) {
683 	case USB_RECIP_DEVICE:
684 		fotg210_set_cxdone(fotg210);
685 		break;
686 	case USB_RECIP_INTERFACE:
687 		fotg210_set_cxdone(fotg210);
688 		break;
689 	case USB_RECIP_ENDPOINT:
690 		if (ctrl->wIndex & USB_ENDPOINT_NUMBER_MASK) {
691 			if (ep->wedged) {
692 				fotg210_set_cxdone(fotg210);
693 				break;
694 			}
695 			if (ep->stall)
696 				fotg210_set_halt_and_wedge(&ep->ep, 0, 0);
697 		}
698 		fotg210_set_cxdone(fotg210);
699 		break;
700 	default:
701 		fotg210_request_error(fotg210);
702 		break;
703 	}
704 }
705 
706 static int fotg210_is_epnstall(struct fotg210_ep *ep)
707 {
708 	struct fotg210_udc *fotg210 = ep->fotg210;
709 	u32 value;
710 	void __iomem *reg;
711 
712 	reg = (ep->dir_in) ?
713 		fotg210->reg + FOTG210_INEPMPSR(ep->epnum) :
714 		fotg210->reg + FOTG210_OUTEPMPSR(ep->epnum);
715 	value = ioread32(reg);
716 	return value & INOUTEPMPSR_STL_EP ? 1 : 0;
717 }
718 
719 /* For EP0 requests triggered by this driver (currently GET_STATUS response) */
720 static void fotg210_ep0_complete(struct usb_ep *_ep, struct usb_request *req)
721 {
722 	struct fotg210_ep *ep;
723 	struct fotg210_udc *fotg210;
724 
725 	ep = container_of(_ep, struct fotg210_ep, ep);
726 	fotg210 = ep->fotg210;
727 
728 	if (req->status || req->actual != req->length) {
729 		dev_warn(&fotg210->gadget.dev, "EP0 request failed: %d\n", req->status);
730 	}
731 }
732 
733 static void fotg210_get_status(struct fotg210_udc *fotg210,
734 				struct usb_ctrlrequest *ctrl)
735 {
736 	u8 epnum;
737 
738 	switch (ctrl->bRequestType & USB_RECIP_MASK) {
739 	case USB_RECIP_DEVICE:
740 		fotg210->ep0_data = cpu_to_le16(1 << USB_DEVICE_SELF_POWERED);
741 		break;
742 	case USB_RECIP_INTERFACE:
743 		fotg210->ep0_data = cpu_to_le16(0);
744 		break;
745 	case USB_RECIP_ENDPOINT:
746 		epnum = ctrl->wIndex & USB_ENDPOINT_NUMBER_MASK;
747 		if (epnum)
748 			fotg210->ep0_data =
749 				cpu_to_le16(fotg210_is_epnstall(fotg210->ep[epnum])
750 					    << USB_ENDPOINT_HALT);
751 		else
752 			fotg210_request_error(fotg210);
753 		break;
754 
755 	default:
756 		fotg210_request_error(fotg210);
757 		return;		/* exit */
758 	}
759 
760 	fotg210->ep0_req->buf = &fotg210->ep0_data;
761 	fotg210->ep0_req->length = 2;
762 
763 	spin_unlock(&fotg210->lock);
764 	fotg210_ep_queue(fotg210->gadget.ep0, fotg210->ep0_req, GFP_ATOMIC);
765 	spin_lock(&fotg210->lock);
766 }
767 
768 static int fotg210_setup_packet(struct fotg210_udc *fotg210,
769 				struct usb_ctrlrequest *ctrl)
770 {
771 	u8 *p = (u8 *)ctrl;
772 	u8 ret = 0;
773 
774 	fotg210_rdsetupp(fotg210, p);
775 
776 	fotg210->ep[0]->dir_in = ctrl->bRequestType & USB_DIR_IN;
777 
778 	if (fotg210->gadget.speed == USB_SPEED_UNKNOWN) {
779 		u32 value = ioread32(fotg210->reg + FOTG210_DMCR);
780 		fotg210->gadget.speed = value & DMCR_HS_EN ?
781 				USB_SPEED_HIGH : USB_SPEED_FULL;
782 	}
783 
784 	/* check request */
785 	if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
786 		switch (ctrl->bRequest) {
787 		case USB_REQ_GET_STATUS:
788 			fotg210_get_status(fotg210, ctrl);
789 			break;
790 		case USB_REQ_CLEAR_FEATURE:
791 			fotg210_clear_feature(fotg210, ctrl);
792 			break;
793 		case USB_REQ_SET_FEATURE:
794 			fotg210_set_feature(fotg210, ctrl);
795 			break;
796 		case USB_REQ_SET_ADDRESS:
797 			fotg210_set_address(fotg210, ctrl);
798 			break;
799 		case USB_REQ_SET_CONFIGURATION:
800 			fotg210_set_configuration(fotg210);
801 			ret = 1;
802 			break;
803 		default:
804 			ret = 1;
805 			break;
806 		}
807 	} else {
808 		ret = 1;
809 	}
810 
811 	return ret;
812 }
813 
814 static void fotg210_ep0out(struct fotg210_udc *fotg210)
815 {
816 	struct fotg210_ep *ep = fotg210->ep[0];
817 
818 	if (!list_empty(&ep->queue) && !ep->dir_in) {
819 		struct fotg210_request *req;
820 
821 		req = list_first_entry(&ep->queue,
822 			struct fotg210_request, queue);
823 
824 		if (req->req.length)
825 			fotg210_start_dma(ep, req);
826 
827 		if ((req->req.length - req->req.actual) < ep->ep.maxpacket)
828 			fotg210_done(ep, req, 0);
829 	} else {
830 		pr_err("%s : empty queue\n", __func__);
831 	}
832 }
833 
834 static void fotg210_ep0in(struct fotg210_udc *fotg210)
835 {
836 	struct fotg210_ep *ep = fotg210->ep[0];
837 
838 	if ((!list_empty(&ep->queue)) && (ep->dir_in)) {
839 		struct fotg210_request *req;
840 
841 		req = list_entry(ep->queue.next,
842 				struct fotg210_request, queue);
843 
844 		if (req->req.length)
845 			fotg210_start_dma(ep, req);
846 
847 		if (req->req.actual == req->req.length)
848 			fotg210_done(ep, req, 0);
849 	} else {
850 		fotg210_set_cxdone(fotg210);
851 	}
852 }
853 
854 static void fotg210_in_fifo_handler(struct fotg210_ep *ep)
855 {
856 	struct fotg210_request *req = list_entry(ep->queue.next,
857 					struct fotg210_request, queue);
858 
859 	if (req->req.length)
860 		fotg210_start_dma(ep, req);
861 	fotg210_done(ep, req, 0);
862 }
863 
864 static void fotg210_out_fifo_handler(struct fotg210_ep *ep)
865 {
866 	struct fotg210_request *req = list_entry(ep->queue.next,
867 						 struct fotg210_request, queue);
868 	int disgr1 = ioread32(ep->fotg210->reg + FOTG210_DISGR1);
869 
870 	fotg210_start_dma(ep, req);
871 
872 	/* Complete the request when it's full or a short packet arrived.
873 	 * Like other drivers, short_not_ok isn't handled.
874 	 */
875 
876 	if (req->req.length == req->req.actual ||
877 	    (disgr1 & DISGR1_SPK_INT(ep->epnum - 1)))
878 		fotg210_done(ep, req, 0);
879 }
880 
881 static irqreturn_t fotg210_irq(int irq, void *_fotg210)
882 {
883 	struct fotg210_udc *fotg210 = _fotg210;
884 	u32 int_grp = ioread32(fotg210->reg + FOTG210_DIGR);
885 	u32 int_msk = ioread32(fotg210->reg + FOTG210_DMIGR);
886 
887 	int_grp &= ~int_msk;
888 
889 	spin_lock(&fotg210->lock);
890 
891 	if (int_grp & DIGR_INT_G2) {
892 		void __iomem *reg = fotg210->reg + FOTG210_DISGR2;
893 		u32 int_grp2 = ioread32(reg);
894 		u32 int_msk2 = ioread32(fotg210->reg + FOTG210_DMISGR2);
895 
896 		int_grp2 &= ~int_msk2;
897 
898 		if (int_grp2 & DISGR2_USBRST_INT) {
899 			usb_gadget_udc_reset(&fotg210->gadget,
900 					     fotg210->driver);
901 			fotg210_ack_int(fotg210, FOTG210_DISGR2, DISGR2_USBRST_INT);
902 			pr_info("fotg210 udc reset\n");
903 		}
904 		if (int_grp2 & DISGR2_SUSP_INT) {
905 			fotg210_ack_int(fotg210, FOTG210_DISGR2, DISGR2_SUSP_INT);
906 			pr_info("fotg210 udc suspend\n");
907 		}
908 		if (int_grp2 & DISGR2_RESM_INT) {
909 			fotg210_ack_int(fotg210, FOTG210_DISGR2, DISGR2_RESM_INT);
910 			pr_info("fotg210 udc resume\n");
911 		}
912 		if (int_grp2 & DISGR2_ISO_SEQ_ERR_INT) {
913 			fotg210_ack_int(fotg210, FOTG210_DISGR2, DISGR2_ISO_SEQ_ERR_INT);
914 			pr_info("fotg210 iso sequence error\n");
915 		}
916 		if (int_grp2 & DISGR2_ISO_SEQ_ABORT_INT) {
917 			fotg210_ack_int(fotg210, FOTG210_DISGR2, DISGR2_ISO_SEQ_ABORT_INT);
918 			pr_info("fotg210 iso sequence abort\n");
919 		}
920 		if (int_grp2 & DISGR2_TX0BYTE_INT) {
921 			fotg210_clear_tx0byte(fotg210);
922 			fotg210_ack_int(fotg210, FOTG210_DISGR2, DISGR2_TX0BYTE_INT);
923 			pr_info("fotg210 transferred 0 byte\n");
924 		}
925 		if (int_grp2 & DISGR2_RX0BYTE_INT) {
926 			fotg210_clear_rx0byte(fotg210);
927 			fotg210_ack_int(fotg210, FOTG210_DISGR2, DISGR2_RX0BYTE_INT);
928 			pr_info("fotg210 received 0 byte\n");
929 		}
930 		if (int_grp2 & DISGR2_DMA_ERROR) {
931 			fotg210_ack_int(fotg210, FOTG210_DISGR2, DISGR2_DMA_ERROR);
932 		}
933 	}
934 
935 	if (int_grp & DIGR_INT_G0) {
936 		void __iomem *reg = fotg210->reg + FOTG210_DISGR0;
937 		u32 int_grp0 = ioread32(reg);
938 		u32 int_msk0 = ioread32(fotg210->reg + FOTG210_DMISGR0);
939 		struct usb_ctrlrequest ctrl;
940 
941 		int_grp0 &= ~int_msk0;
942 
943 		/* the highest priority in this source register */
944 		if (int_grp0 & DISGR0_CX_COMABT_INT) {
945 			fotg210_ack_int(fotg210, FOTG210_DISGR0, DISGR0_CX_COMABT_INT);
946 			pr_info("fotg210 CX command abort\n");
947 		}
948 
949 		if (int_grp0 & DISGR0_CX_SETUP_INT) {
950 			if (fotg210_setup_packet(fotg210, &ctrl)) {
951 				spin_unlock(&fotg210->lock);
952 				if (fotg210->driver->setup(&fotg210->gadget,
953 							   &ctrl) < 0)
954 					fotg210_set_cxstall(fotg210);
955 				spin_lock(&fotg210->lock);
956 			}
957 		}
958 		if (int_grp0 & DISGR0_CX_COMEND_INT)
959 			pr_info("fotg210 cmd end\n");
960 
961 		if (int_grp0 & DISGR0_CX_IN_INT)
962 			fotg210_ep0in(fotg210);
963 
964 		if (int_grp0 & DISGR0_CX_OUT_INT)
965 			fotg210_ep0out(fotg210);
966 
967 		if (int_grp0 & DISGR0_CX_COMFAIL_INT) {
968 			fotg210_set_cxstall(fotg210);
969 			pr_info("fotg210 ep0 fail\n");
970 		}
971 	}
972 
973 	if (int_grp & DIGR_INT_G1) {
974 		void __iomem *reg = fotg210->reg + FOTG210_DISGR1;
975 		u32 int_grp1 = ioread32(reg);
976 		u32 int_msk1 = ioread32(fotg210->reg + FOTG210_DMISGR1);
977 		int fifo;
978 
979 		int_grp1 &= ~int_msk1;
980 
981 		for (fifo = 0; fifo < FOTG210_MAX_FIFO_NUM; fifo++) {
982 			if (int_grp1 & DISGR1_IN_INT(fifo))
983 				fotg210_in_fifo_handler(fotg210->ep[fifo + 1]);
984 
985 			if ((int_grp1 & DISGR1_OUT_INT(fifo)) ||
986 			    (int_grp1 & DISGR1_SPK_INT(fifo)))
987 				fotg210_out_fifo_handler(fotg210->ep[fifo + 1]);
988 		}
989 	}
990 
991 	spin_unlock(&fotg210->lock);
992 
993 	return IRQ_HANDLED;
994 }
995 
996 static void fotg210_disable_unplug(struct fotg210_udc *fotg210)
997 {
998 	u32 reg = ioread32(fotg210->reg + FOTG210_PHYTMSR);
999 
1000 	reg &= ~PHYTMSR_UNPLUG;
1001 	iowrite32(reg, fotg210->reg + FOTG210_PHYTMSR);
1002 }
1003 
1004 static int fotg210_udc_start(struct usb_gadget *g,
1005 		struct usb_gadget_driver *driver)
1006 {
1007 	struct fotg210_udc *fotg210 = gadget_to_fotg210(g);
1008 	u32 value;
1009 	int ret;
1010 
1011 	/* hook up the driver */
1012 	driver->driver.bus = NULL;
1013 	fotg210->driver = driver;
1014 	fotg210->gadget.dev.of_node = fotg210->dev->of_node;
1015 	fotg210->gadget.speed = USB_SPEED_UNKNOWN;
1016 
1017 	dev_info(fotg210->dev, "bound driver %s\n", driver->driver.name);
1018 
1019 	if (!IS_ERR_OR_NULL(fotg210->phy)) {
1020 		ret = otg_set_peripheral(fotg210->phy->otg,
1021 					 &fotg210->gadget);
1022 		if (ret)
1023 			dev_err(fotg210->dev, "can't bind to phy\n");
1024 	}
1025 
1026 	/* enable device global interrupt */
1027 	value = ioread32(fotg210->reg + FOTG210_DMCR);
1028 	value |= DMCR_GLINT_EN;
1029 	iowrite32(value, fotg210->reg + FOTG210_DMCR);
1030 
1031 	return 0;
1032 }
1033 
1034 static void fotg210_init(struct fotg210_udc *fotg210)
1035 {
1036 	u32 value;
1037 
1038 	/* disable global interrupt and set int polarity to active high */
1039 	iowrite32(GMIR_MHC_INT | GMIR_MOTG_INT | GMIR_INT_POLARITY,
1040 		  fotg210->reg + FOTG210_GMIR);
1041 
1042 	/* disable device global interrupt */
1043 	value = ioread32(fotg210->reg + FOTG210_DMCR);
1044 	value &= ~DMCR_GLINT_EN;
1045 	iowrite32(value, fotg210->reg + FOTG210_DMCR);
1046 
1047 	/* enable only grp2 irqs we handle */
1048 	iowrite32(~(DISGR2_DMA_ERROR | DISGR2_RX0BYTE_INT | DISGR2_TX0BYTE_INT
1049 		    | DISGR2_ISO_SEQ_ABORT_INT | DISGR2_ISO_SEQ_ERR_INT
1050 		    | DISGR2_RESM_INT | DISGR2_SUSP_INT | DISGR2_USBRST_INT),
1051 		  fotg210->reg + FOTG210_DMISGR2);
1052 
1053 	/* disable all fifo interrupt */
1054 	iowrite32(~(u32)0, fotg210->reg + FOTG210_DMISGR1);
1055 
1056 	/* disable cmd end */
1057 	value = ioread32(fotg210->reg + FOTG210_DMISGR0);
1058 	value |= DMISGR0_MCX_COMEND;
1059 	iowrite32(value, fotg210->reg + FOTG210_DMISGR0);
1060 }
1061 
1062 static int fotg210_udc_stop(struct usb_gadget *g)
1063 {
1064 	struct fotg210_udc *fotg210 = gadget_to_fotg210(g);
1065 	unsigned long	flags;
1066 
1067 	if (!IS_ERR_OR_NULL(fotg210->phy))
1068 		return otg_set_peripheral(fotg210->phy->otg, NULL);
1069 
1070 	spin_lock_irqsave(&fotg210->lock, flags);
1071 
1072 	fotg210_init(fotg210);
1073 	fotg210->driver = NULL;
1074 	fotg210->gadget.speed = USB_SPEED_UNKNOWN;
1075 
1076 	spin_unlock_irqrestore(&fotg210->lock, flags);
1077 
1078 	return 0;
1079 }
1080 
1081 /**
1082  * fotg210_vbus_session - Called by external transceiver to enable/disable udc
1083  * @_gadget: usb gadget
1084  * @is_active: 0 if should disable UDC VBUS, 1 if should enable
1085  *
1086  * Returns 0
1087  */
1088 static int fotg210_vbus_session(struct usb_gadget *g, int is_active)
1089 {
1090 	struct fotg210_udc *fotg210 = gadget_to_fotg210(g);
1091 
1092 	/* Call down to core integration layer to drive or disable VBUS */
1093 	fotg210_vbus(fotg210->fotg, is_active);
1094 	return 0;
1095 }
1096 
1097 static const struct usb_gadget_ops fotg210_gadget_ops = {
1098 	.udc_start		= fotg210_udc_start,
1099 	.udc_stop		= fotg210_udc_stop,
1100 	.vbus_session		= fotg210_vbus_session,
1101 };
1102 
1103 /**
1104  * fotg210_phy_event - Called by phy upon VBus event
1105  * @nb: notifier block
1106  * @action: phy action, is vbus connect or disconnect
1107  * @data: the usb_gadget structure in fotg210
1108  *
1109  * Called by the USB Phy when a cable connect or disconnect is sensed.
1110  *
1111  * Returns NOTIFY_OK or NOTIFY_DONE
1112  */
1113 static int fotg210_phy_event(struct notifier_block *nb, unsigned long action,
1114 			     void *data)
1115 {
1116 	struct usb_gadget *gadget = data;
1117 
1118 	if (!gadget)
1119 		return NOTIFY_DONE;
1120 
1121 	switch (action) {
1122 	case USB_EVENT_VBUS:
1123 		usb_gadget_vbus_connect(gadget);
1124 		return NOTIFY_OK;
1125 	case USB_EVENT_NONE:
1126 		usb_gadget_vbus_disconnect(gadget);
1127 		return NOTIFY_OK;
1128 	default:
1129 		return NOTIFY_DONE;
1130 	}
1131 }
1132 
1133 static struct notifier_block fotg210_phy_notifier = {
1134 	.notifier_call = fotg210_phy_event,
1135 };
1136 
1137 int fotg210_udc_remove(struct platform_device *pdev)
1138 {
1139 	struct fotg210_udc *fotg210 = platform_get_drvdata(pdev);
1140 	int i;
1141 
1142 	usb_del_gadget_udc(&fotg210->gadget);
1143 	if (!IS_ERR_OR_NULL(fotg210->phy)) {
1144 		usb_unregister_notifier(fotg210->phy, &fotg210_phy_notifier);
1145 		usb_put_phy(fotg210->phy);
1146 	}
1147 	iounmap(fotg210->reg);
1148 	free_irq(platform_get_irq(pdev, 0), fotg210);
1149 
1150 	fotg210_ep_free_request(&fotg210->ep[0]->ep, fotg210->ep0_req);
1151 	for (i = 0; i < FOTG210_MAX_NUM_EP; i++)
1152 		kfree(fotg210->ep[i]);
1153 
1154 	kfree(fotg210);
1155 
1156 	return 0;
1157 }
1158 
1159 int fotg210_udc_probe(struct platform_device *pdev, struct fotg210 *fotg)
1160 {
1161 	struct fotg210_udc *fotg210 = NULL;
1162 	struct device *dev = &pdev->dev;
1163 	int irq;
1164 	int ret = 0;
1165 	int i;
1166 
1167 	irq = platform_get_irq(pdev, 0);
1168 	if (irq < 0) {
1169 		pr_err("could not get irq\n");
1170 		return -ENODEV;
1171 	}
1172 
1173 	/* initialize udc */
1174 	fotg210 = kzalloc(sizeof(struct fotg210_udc), GFP_KERNEL);
1175 	if (fotg210 == NULL)
1176 		return -ENOMEM;
1177 
1178 	fotg210->dev = dev;
1179 	fotg210->fotg = fotg;
1180 
1181 	fotg210->phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 0);
1182 	if (IS_ERR(fotg210->phy)) {
1183 		ret = PTR_ERR(fotg210->phy);
1184 		if (ret == -EPROBE_DEFER)
1185 			goto err_free;
1186 		dev_info(dev, "no PHY found\n");
1187 		fotg210->phy = NULL;
1188 	} else {
1189 		ret = usb_phy_init(fotg210->phy);
1190 		if (ret)
1191 			goto err_free;
1192 		dev_info(dev, "found and initialized PHY\n");
1193 	}
1194 
1195 	ret = -ENOMEM;
1196 
1197 	for (i = 0; i < FOTG210_MAX_NUM_EP; i++) {
1198 		fotg210->ep[i] = kzalloc(sizeof(struct fotg210_ep), GFP_KERNEL);
1199 		if (!fotg210->ep[i])
1200 			goto err_alloc;
1201 	}
1202 
1203 	fotg210->reg = fotg->base;
1204 
1205 	spin_lock_init(&fotg210->lock);
1206 
1207 	platform_set_drvdata(pdev, fotg210);
1208 
1209 	fotg210->gadget.ops = &fotg210_gadget_ops;
1210 
1211 	fotg210->gadget.max_speed = USB_SPEED_HIGH;
1212 	fotg210->gadget.dev.parent = dev;
1213 	fotg210->gadget.dev.dma_mask = dev->dma_mask;
1214 	fotg210->gadget.name = udc_name;
1215 
1216 	INIT_LIST_HEAD(&fotg210->gadget.ep_list);
1217 
1218 	for (i = 0; i < FOTG210_MAX_NUM_EP; i++) {
1219 		struct fotg210_ep *ep = fotg210->ep[i];
1220 
1221 		if (i) {
1222 			INIT_LIST_HEAD(&fotg210->ep[i]->ep.ep_list);
1223 			list_add_tail(&fotg210->ep[i]->ep.ep_list,
1224 				      &fotg210->gadget.ep_list);
1225 		}
1226 		ep->fotg210 = fotg210;
1227 		INIT_LIST_HEAD(&ep->queue);
1228 		ep->ep.name = fotg210_ep_name[i];
1229 		ep->ep.ops = &fotg210_ep_ops;
1230 		usb_ep_set_maxpacket_limit(&ep->ep, (unsigned short) ~0);
1231 
1232 		if (i == 0) {
1233 			ep->ep.caps.type_control = true;
1234 		} else {
1235 			ep->ep.caps.type_iso = true;
1236 			ep->ep.caps.type_bulk = true;
1237 			ep->ep.caps.type_int = true;
1238 		}
1239 
1240 		ep->ep.caps.dir_in = true;
1241 		ep->ep.caps.dir_out = true;
1242 	}
1243 	usb_ep_set_maxpacket_limit(&fotg210->ep[0]->ep, 0x40);
1244 	fotg210->gadget.ep0 = &fotg210->ep[0]->ep;
1245 	INIT_LIST_HEAD(&fotg210->gadget.ep0->ep_list);
1246 
1247 	fotg210->ep0_req = fotg210_ep_alloc_request(&fotg210->ep[0]->ep,
1248 				GFP_KERNEL);
1249 	if (fotg210->ep0_req == NULL)
1250 		goto err_map;
1251 
1252 	fotg210->ep0_req->complete = fotg210_ep0_complete;
1253 
1254 	fotg210_init(fotg210);
1255 
1256 	fotg210_disable_unplug(fotg210);
1257 
1258 	ret = request_irq(irq, fotg210_irq, IRQF_SHARED,
1259 			  udc_name, fotg210);
1260 	if (ret < 0) {
1261 		dev_err(dev, "request_irq error (%d)\n", ret);
1262 		goto err_req;
1263 	}
1264 
1265 	if (!IS_ERR_OR_NULL(fotg210->phy))
1266 		usb_register_notifier(fotg210->phy, &fotg210_phy_notifier);
1267 
1268 	ret = usb_add_gadget_udc(dev, &fotg210->gadget);
1269 	if (ret)
1270 		goto err_add_udc;
1271 
1272 	dev_info(dev, "version %s\n", DRIVER_VERSION);
1273 
1274 	return 0;
1275 
1276 err_add_udc:
1277 	if (!IS_ERR_OR_NULL(fotg210->phy))
1278 		usb_unregister_notifier(fotg210->phy, &fotg210_phy_notifier);
1279 	free_irq(irq, fotg210);
1280 
1281 err_req:
1282 	fotg210_ep_free_request(&fotg210->ep[0]->ep, fotg210->ep0_req);
1283 
1284 err_map:
1285 	iounmap(fotg210->reg);
1286 
1287 err_alloc:
1288 	for (i = 0; i < FOTG210_MAX_NUM_EP; i++)
1289 		kfree(fotg210->ep[i]);
1290 
1291 err_free:
1292 	kfree(fotg210);
1293 	return ret;
1294 }
1295