xref: /openbmc/linux/drivers/usb/gadget/udc/gr_udc.c (revision cfbb9be8)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * USB Peripheral Controller driver for Aeroflex Gaisler GRUSBDC.
4  *
5  * 2013 (c) Aeroflex Gaisler AB
6  *
7  * This driver supports GRUSBDC USB Device Controller cores available in the
8  * GRLIB VHDL IP core library.
9  *
10  * Full documentation of the GRUSBDC core can be found here:
11  * http://www.gaisler.com/products/grlib/grip.pdf
12  *
13  * Contributors:
14  * - Andreas Larsson <andreas@gaisler.com>
15  * - Marko Isomaki
16  */
17 
18 /*
19  * A GRUSBDC core can have up to 16 IN endpoints and 16 OUT endpoints each
20  * individually configurable to any of the four USB transfer types. This driver
21  * only supports cores in DMA mode.
22  */
23 
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/slab.h>
27 #include <linux/spinlock.h>
28 #include <linux/errno.h>
29 #include <linux/list.h>
30 #include <linux/interrupt.h>
31 #include <linux/device.h>
32 #include <linux/usb/ch9.h>
33 #include <linux/usb/gadget.h>
34 #include <linux/dma-mapping.h>
35 #include <linux/dmapool.h>
36 #include <linux/debugfs.h>
37 #include <linux/seq_file.h>
38 #include <linux/of_platform.h>
39 #include <linux/of_irq.h>
40 #include <linux/of_address.h>
41 
42 #include <asm/byteorder.h>
43 
44 #include "gr_udc.h"
45 
46 #define	DRIVER_NAME	"gr_udc"
47 #define	DRIVER_DESC	"Aeroflex Gaisler GRUSBDC USB Peripheral Controller"
48 
49 static const char driver_name[] = DRIVER_NAME;
50 static const char driver_desc[] = DRIVER_DESC;
51 
52 #define gr_read32(x) (ioread32be((x)))
53 #define gr_write32(x, v) (iowrite32be((v), (x)))
54 
55 /* USB speed and corresponding string calculated from status register value */
56 #define GR_SPEED(status) \
57 	((status & GR_STATUS_SP) ? USB_SPEED_FULL : USB_SPEED_HIGH)
58 #define GR_SPEED_STR(status) usb_speed_string(GR_SPEED(status))
59 
60 /* Size of hardware buffer calculated from epctrl register value */
61 #define GR_BUFFER_SIZE(epctrl)					      \
62 	((((epctrl) & GR_EPCTRL_BUFSZ_MASK) >> GR_EPCTRL_BUFSZ_POS) * \
63 	 GR_EPCTRL_BUFSZ_SCALER)
64 
65 /* ---------------------------------------------------------------------- */
66 /* Debug printout functionality */
67 
68 static const char * const gr_modestring[] = {"control", "iso", "bulk", "int"};
69 
70 static const char *gr_ep0state_string(enum gr_ep0state state)
71 {
72 	static const char *const names[] = {
73 		[GR_EP0_DISCONNECT] = "disconnect",
74 		[GR_EP0_SETUP] = "setup",
75 		[GR_EP0_IDATA] = "idata",
76 		[GR_EP0_ODATA] = "odata",
77 		[GR_EP0_ISTATUS] = "istatus",
78 		[GR_EP0_OSTATUS] = "ostatus",
79 		[GR_EP0_STALL] = "stall",
80 		[GR_EP0_SUSPEND] = "suspend",
81 	};
82 
83 	if (state < 0 || state >= ARRAY_SIZE(names))
84 		return "UNKNOWN";
85 
86 	return names[state];
87 }
88 
89 #ifdef VERBOSE_DEBUG
90 
91 static void gr_dbgprint_request(const char *str, struct gr_ep *ep,
92 				struct gr_request *req)
93 {
94 	int buflen = ep->is_in ? req->req.length : req->req.actual;
95 	int rowlen = 32;
96 	int plen = min(rowlen, buflen);
97 
98 	dev_dbg(ep->dev->dev, "%s: 0x%p, %d bytes data%s:\n", str, req, buflen,
99 		(buflen > plen ? " (truncated)" : ""));
100 	print_hex_dump_debug("   ", DUMP_PREFIX_NONE,
101 			     rowlen, 4, req->req.buf, plen, false);
102 }
103 
104 static void gr_dbgprint_devreq(struct gr_udc *dev, u8 type, u8 request,
105 			       u16 value, u16 index, u16 length)
106 {
107 	dev_vdbg(dev->dev, "REQ: %02x.%02x v%04x i%04x l%04x\n",
108 		 type, request, value, index, length);
109 }
110 #else /* !VERBOSE_DEBUG */
111 
112 static void gr_dbgprint_request(const char *str, struct gr_ep *ep,
113 				struct gr_request *req) {}
114 
115 static void gr_dbgprint_devreq(struct gr_udc *dev, u8 type, u8 request,
116 			       u16 value, u16 index, u16 length) {}
117 
118 #endif /* VERBOSE_DEBUG */
119 
120 /* ---------------------------------------------------------------------- */
121 /* Debugfs functionality */
122 
123 #ifdef CONFIG_USB_GADGET_DEBUG_FS
124 
125 static void gr_seq_ep_show(struct seq_file *seq, struct gr_ep *ep)
126 {
127 	u32 epctrl = gr_read32(&ep->regs->epctrl);
128 	u32 epstat = gr_read32(&ep->regs->epstat);
129 	int mode = (epctrl & GR_EPCTRL_TT_MASK) >> GR_EPCTRL_TT_POS;
130 	struct gr_request *req;
131 
132 	seq_printf(seq, "%s:\n", ep->ep.name);
133 	seq_printf(seq, "  mode = %s\n", gr_modestring[mode]);
134 	seq_printf(seq, "  halted: %d\n", !!(epctrl & GR_EPCTRL_EH));
135 	seq_printf(seq, "  disabled: %d\n", !!(epctrl & GR_EPCTRL_ED));
136 	seq_printf(seq, "  valid: %d\n", !!(epctrl & GR_EPCTRL_EV));
137 	seq_printf(seq, "  dma_start = %d\n", ep->dma_start);
138 	seq_printf(seq, "  stopped = %d\n", ep->stopped);
139 	seq_printf(seq, "  wedged = %d\n", ep->wedged);
140 	seq_printf(seq, "  callback = %d\n", ep->callback);
141 	seq_printf(seq, "  maxpacket = %d\n", ep->ep.maxpacket);
142 	seq_printf(seq, "  maxpacket_limit = %d\n", ep->ep.maxpacket_limit);
143 	seq_printf(seq, "  bytes_per_buffer = %d\n", ep->bytes_per_buffer);
144 	if (mode == 1 || mode == 3)
145 		seq_printf(seq, "  nt = %d\n",
146 			   (epctrl & GR_EPCTRL_NT_MASK) >> GR_EPCTRL_NT_POS);
147 
148 	seq_printf(seq, "  Buffer 0: %s %s%d\n",
149 		   epstat & GR_EPSTAT_B0 ? "valid" : "invalid",
150 		   epstat & GR_EPSTAT_BS ? " " : "selected ",
151 		   (epstat & GR_EPSTAT_B0CNT_MASK) >> GR_EPSTAT_B0CNT_POS);
152 	seq_printf(seq, "  Buffer 1: %s %s%d\n",
153 		   epstat & GR_EPSTAT_B1 ? "valid" : "invalid",
154 		   epstat & GR_EPSTAT_BS ? "selected " : " ",
155 		   (epstat & GR_EPSTAT_B1CNT_MASK) >> GR_EPSTAT_B1CNT_POS);
156 
157 	if (list_empty(&ep->queue)) {
158 		seq_puts(seq, "  Queue: empty\n\n");
159 		return;
160 	}
161 
162 	seq_puts(seq, "  Queue:\n");
163 	list_for_each_entry(req, &ep->queue, queue) {
164 		struct gr_dma_desc *desc;
165 		struct gr_dma_desc *next;
166 
167 		seq_printf(seq, "    0x%p: 0x%p %d %d\n", req,
168 			   &req->req.buf, req->req.actual, req->req.length);
169 
170 		next = req->first_desc;
171 		do {
172 			desc = next;
173 			next = desc->next_desc;
174 			seq_printf(seq, "    %c 0x%p (0x%08x): 0x%05x 0x%08x\n",
175 				   desc == req->curr_desc ? 'c' : ' ',
176 				   desc, desc->paddr, desc->ctrl, desc->data);
177 		} while (desc != req->last_desc);
178 	}
179 	seq_puts(seq, "\n");
180 }
181 
182 
183 static int gr_seq_show(struct seq_file *seq, void *v)
184 {
185 	struct gr_udc *dev = seq->private;
186 	u32 control = gr_read32(&dev->regs->control);
187 	u32 status = gr_read32(&dev->regs->status);
188 	struct gr_ep *ep;
189 
190 	seq_printf(seq, "usb state = %s\n",
191 		   usb_state_string(dev->gadget.state));
192 	seq_printf(seq, "address = %d\n",
193 		   (control & GR_CONTROL_UA_MASK) >> GR_CONTROL_UA_POS);
194 	seq_printf(seq, "speed = %s\n", GR_SPEED_STR(status));
195 	seq_printf(seq, "ep0state = %s\n", gr_ep0state_string(dev->ep0state));
196 	seq_printf(seq, "irq_enabled = %d\n", dev->irq_enabled);
197 	seq_printf(seq, "remote_wakeup = %d\n", dev->remote_wakeup);
198 	seq_printf(seq, "test_mode = %d\n", dev->test_mode);
199 	seq_puts(seq, "\n");
200 
201 	list_for_each_entry(ep, &dev->ep_list, ep_list)
202 		gr_seq_ep_show(seq, ep);
203 
204 	return 0;
205 }
206 
207 static int gr_dfs_open(struct inode *inode, struct file *file)
208 {
209 	return single_open(file, gr_seq_show, inode->i_private);
210 }
211 
212 static const struct file_operations gr_dfs_fops = {
213 	.owner		= THIS_MODULE,
214 	.open		= gr_dfs_open,
215 	.read		= seq_read,
216 	.llseek		= seq_lseek,
217 	.release	= single_release,
218 };
219 
220 static void gr_dfs_create(struct gr_udc *dev)
221 {
222 	const char *name = "gr_udc_state";
223 
224 	dev->dfs_root = debugfs_create_dir(dev_name(dev->dev), NULL);
225 	dev->dfs_state = debugfs_create_file(name, 0444, dev->dfs_root, dev,
226 					     &gr_dfs_fops);
227 }
228 
229 static void gr_dfs_delete(struct gr_udc *dev)
230 {
231 	/* Handles NULL and ERR pointers internally */
232 	debugfs_remove(dev->dfs_state);
233 	debugfs_remove(dev->dfs_root);
234 }
235 
236 #else /* !CONFIG_USB_GADGET_DEBUG_FS */
237 
238 static void gr_dfs_create(struct gr_udc *dev) {}
239 static void gr_dfs_delete(struct gr_udc *dev) {}
240 
241 #endif /* CONFIG_USB_GADGET_DEBUG_FS */
242 
243 /* ---------------------------------------------------------------------- */
244 /* DMA and request handling */
245 
246 /* Allocates a new struct gr_dma_desc, sets paddr and zeroes the rest */
247 static struct gr_dma_desc *gr_alloc_dma_desc(struct gr_ep *ep, gfp_t gfp_flags)
248 {
249 	dma_addr_t paddr;
250 	struct gr_dma_desc *dma_desc;
251 
252 	dma_desc = dma_pool_zalloc(ep->dev->desc_pool, gfp_flags, &paddr);
253 	if (!dma_desc) {
254 		dev_err(ep->dev->dev, "Could not allocate from DMA pool\n");
255 		return NULL;
256 	}
257 
258 	dma_desc->paddr = paddr;
259 
260 	return dma_desc;
261 }
262 
263 static inline void gr_free_dma_desc(struct gr_udc *dev,
264 				    struct gr_dma_desc *desc)
265 {
266 	dma_pool_free(dev->desc_pool, desc, (dma_addr_t)desc->paddr);
267 }
268 
269 /* Frees the chain of struct gr_dma_desc for the given request */
270 static void gr_free_dma_desc_chain(struct gr_udc *dev, struct gr_request *req)
271 {
272 	struct gr_dma_desc *desc;
273 	struct gr_dma_desc *next;
274 
275 	next = req->first_desc;
276 	if (!next)
277 		return;
278 
279 	do {
280 		desc = next;
281 		next = desc->next_desc;
282 		gr_free_dma_desc(dev, desc);
283 	} while (desc != req->last_desc);
284 
285 	req->first_desc = NULL;
286 	req->curr_desc = NULL;
287 	req->last_desc = NULL;
288 }
289 
290 static void gr_ep0_setup(struct gr_udc *dev, struct gr_request *req);
291 
292 /*
293  * Frees allocated resources and calls the appropriate completion function/setup
294  * package handler for a finished request.
295  *
296  * Must be called with dev->lock held and irqs disabled.
297  */
298 static void gr_finish_request(struct gr_ep *ep, struct gr_request *req,
299 			      int status)
300 	__releases(&dev->lock)
301 	__acquires(&dev->lock)
302 {
303 	struct gr_udc *dev;
304 
305 	list_del_init(&req->queue);
306 
307 	if (likely(req->req.status == -EINPROGRESS))
308 		req->req.status = status;
309 	else
310 		status = req->req.status;
311 
312 	dev = ep->dev;
313 	usb_gadget_unmap_request(&dev->gadget, &req->req, ep->is_in);
314 	gr_free_dma_desc_chain(dev, req);
315 
316 	if (ep->is_in) { /* For OUT, req->req.actual gets updated bit by bit */
317 		req->req.actual = req->req.length;
318 	} else if (req->oddlen && req->req.actual > req->evenlen) {
319 		/*
320 		 * Copy to user buffer in this case where length was not evenly
321 		 * divisible by ep->ep.maxpacket and the last descriptor was
322 		 * actually used.
323 		 */
324 		char *buftail = ((char *)req->req.buf + req->evenlen);
325 
326 		memcpy(buftail, ep->tailbuf, req->oddlen);
327 
328 		if (req->req.actual > req->req.length) {
329 			/* We got more data than was requested */
330 			dev_dbg(ep->dev->dev, "Overflow for ep %s\n",
331 				ep->ep.name);
332 			gr_dbgprint_request("OVFL", ep, req);
333 			req->req.status = -EOVERFLOW;
334 		}
335 	}
336 
337 	if (!status) {
338 		if (ep->is_in)
339 			gr_dbgprint_request("SENT", ep, req);
340 		else
341 			gr_dbgprint_request("RECV", ep, req);
342 	}
343 
344 	/* Prevent changes to ep->queue during callback */
345 	ep->callback = 1;
346 	if (req == dev->ep0reqo && !status) {
347 		if (req->setup)
348 			gr_ep0_setup(dev, req);
349 		else
350 			dev_err(dev->dev,
351 				"Unexpected non setup packet on ep0in\n");
352 	} else if (req->req.complete) {
353 		spin_unlock(&dev->lock);
354 
355 		usb_gadget_giveback_request(&ep->ep, &req->req);
356 
357 		spin_lock(&dev->lock);
358 	}
359 	ep->callback = 0;
360 }
361 
362 static struct usb_request *gr_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
363 {
364 	struct gr_request *req;
365 
366 	req = kzalloc(sizeof(*req), gfp_flags);
367 	if (!req)
368 		return NULL;
369 
370 	INIT_LIST_HEAD(&req->queue);
371 
372 	return &req->req;
373 }
374 
375 /*
376  * Starts DMA for endpoint ep if there are requests in the queue.
377  *
378  * Must be called with dev->lock held and with !ep->stopped.
379  */
380 static void gr_start_dma(struct gr_ep *ep)
381 {
382 	struct gr_request *req;
383 	u32 dmactrl;
384 
385 	if (list_empty(&ep->queue)) {
386 		ep->dma_start = 0;
387 		return;
388 	}
389 
390 	req = list_first_entry(&ep->queue, struct gr_request, queue);
391 
392 	/* A descriptor should already have been allocated */
393 	BUG_ON(!req->curr_desc);
394 
395 	/*
396 	 * The DMA controller can not handle smaller OUT buffers than
397 	 * ep->ep.maxpacket. It could lead to buffer overruns if an unexpectedly
398 	 * long packet are received. Therefore an internal bounce buffer gets
399 	 * used when such a request gets enabled.
400 	 */
401 	if (!ep->is_in && req->oddlen)
402 		req->last_desc->data = ep->tailbuf_paddr;
403 
404 	wmb(); /* Make sure all is settled before handing it over to DMA */
405 
406 	/* Set the descriptor pointer in the hardware */
407 	gr_write32(&ep->regs->dmaaddr, req->curr_desc->paddr);
408 
409 	/* Announce available descriptors */
410 	dmactrl = gr_read32(&ep->regs->dmactrl);
411 	gr_write32(&ep->regs->dmactrl, dmactrl | GR_DMACTRL_DA);
412 
413 	ep->dma_start = 1;
414 }
415 
416 /*
417  * Finishes the first request in the ep's queue and, if available, starts the
418  * next request in queue.
419  *
420  * Must be called with dev->lock held, irqs disabled and with !ep->stopped.
421  */
422 static void gr_dma_advance(struct gr_ep *ep, int status)
423 {
424 	struct gr_request *req;
425 
426 	req = list_first_entry(&ep->queue, struct gr_request, queue);
427 	gr_finish_request(ep, req, status);
428 	gr_start_dma(ep); /* Regardless of ep->dma_start */
429 }
430 
431 /*
432  * Abort DMA for an endpoint. Sets the abort DMA bit which causes an ongoing DMA
433  * transfer to be canceled and clears GR_DMACTRL_DA.
434  *
435  * Must be called with dev->lock held.
436  */
437 static void gr_abort_dma(struct gr_ep *ep)
438 {
439 	u32 dmactrl;
440 
441 	dmactrl = gr_read32(&ep->regs->dmactrl);
442 	gr_write32(&ep->regs->dmactrl, dmactrl | GR_DMACTRL_AD);
443 }
444 
445 /*
446  * Allocates and sets up a struct gr_dma_desc and putting it on the descriptor
447  * chain.
448  *
449  * Size is not used for OUT endpoints. Hardware can not be instructed to handle
450  * smaller buffer than MAXPL in the OUT direction.
451  */
452 static int gr_add_dma_desc(struct gr_ep *ep, struct gr_request *req,
453 			   dma_addr_t data, unsigned size, gfp_t gfp_flags)
454 {
455 	struct gr_dma_desc *desc;
456 
457 	desc = gr_alloc_dma_desc(ep, gfp_flags);
458 	if (!desc)
459 		return -ENOMEM;
460 
461 	desc->data = data;
462 	if (ep->is_in)
463 		desc->ctrl =
464 			(GR_DESC_IN_CTRL_LEN_MASK & size) | GR_DESC_IN_CTRL_EN;
465 	else
466 		desc->ctrl = GR_DESC_OUT_CTRL_IE;
467 
468 	if (!req->first_desc) {
469 		req->first_desc = desc;
470 		req->curr_desc = desc;
471 	} else {
472 		req->last_desc->next_desc = desc;
473 		req->last_desc->next = desc->paddr;
474 		req->last_desc->ctrl |= GR_DESC_OUT_CTRL_NX;
475 	}
476 	req->last_desc = desc;
477 
478 	return 0;
479 }
480 
481 /*
482  * Sets up a chain of struct gr_dma_descriptors pointing to buffers that
483  * together covers req->req.length bytes of the buffer at DMA address
484  * req->req.dma for the OUT direction.
485  *
486  * The first descriptor in the chain is enabled, the rest disabled. The
487  * interrupt handler will later enable them one by one when needed so we can
488  * find out when the transfer is finished. For OUT endpoints, all descriptors
489  * therefore generate interrutps.
490  */
491 static int gr_setup_out_desc_list(struct gr_ep *ep, struct gr_request *req,
492 				  gfp_t gfp_flags)
493 {
494 	u16 bytes_left; /* Bytes left to provide descriptors for */
495 	u16 bytes_used; /* Bytes accommodated for */
496 	int ret = 0;
497 
498 	req->first_desc = NULL; /* Signals that no allocation is done yet */
499 	bytes_left = req->req.length;
500 	bytes_used = 0;
501 	while (bytes_left > 0) {
502 		dma_addr_t start = req->req.dma + bytes_used;
503 		u16 size = min(bytes_left, ep->bytes_per_buffer);
504 
505 		if (size < ep->bytes_per_buffer) {
506 			/* Prepare using bounce buffer */
507 			req->evenlen = req->req.length - bytes_left;
508 			req->oddlen = size;
509 		}
510 
511 		ret = gr_add_dma_desc(ep, req, start, size, gfp_flags);
512 		if (ret)
513 			goto alloc_err;
514 
515 		bytes_left -= size;
516 		bytes_used += size;
517 	}
518 
519 	req->first_desc->ctrl |= GR_DESC_OUT_CTRL_EN;
520 
521 	return 0;
522 
523 alloc_err:
524 	gr_free_dma_desc_chain(ep->dev, req);
525 
526 	return ret;
527 }
528 
529 /*
530  * Sets up a chain of struct gr_dma_descriptors pointing to buffers that
531  * together covers req->req.length bytes of the buffer at DMA address
532  * req->req.dma for the IN direction.
533  *
534  * When more data is provided than the maximum payload size, the hardware splits
535  * this up into several payloads automatically. Moreover, ep->bytes_per_buffer
536  * is always set to a multiple of the maximum payload (restricted to the valid
537  * number of maximum payloads during high bandwidth isochronous or interrupt
538  * transfers)
539  *
540  * All descriptors are enabled from the beginning and we only generate an
541  * interrupt for the last one indicating that the entire request has been pushed
542  * to hardware.
543  */
544 static int gr_setup_in_desc_list(struct gr_ep *ep, struct gr_request *req,
545 				 gfp_t gfp_flags)
546 {
547 	u16 bytes_left; /* Bytes left in req to provide descriptors for */
548 	u16 bytes_used; /* Bytes in req accommodated for */
549 	int ret = 0;
550 
551 	req->first_desc = NULL; /* Signals that no allocation is done yet */
552 	bytes_left = req->req.length;
553 	bytes_used = 0;
554 	do { /* Allow for zero length packets */
555 		dma_addr_t start = req->req.dma + bytes_used;
556 		u16 size = min(bytes_left, ep->bytes_per_buffer);
557 
558 		ret = gr_add_dma_desc(ep, req, start, size, gfp_flags);
559 		if (ret)
560 			goto alloc_err;
561 
562 		bytes_left -= size;
563 		bytes_used += size;
564 	} while (bytes_left > 0);
565 
566 	/*
567 	 * Send an extra zero length packet to indicate that no more data is
568 	 * available when req->req.zero is set and the data length is even
569 	 * multiples of ep->ep.maxpacket.
570 	 */
571 	if (req->req.zero && (req->req.length % ep->ep.maxpacket == 0)) {
572 		ret = gr_add_dma_desc(ep, req, 0, 0, gfp_flags);
573 		if (ret)
574 			goto alloc_err;
575 	}
576 
577 	/*
578 	 * For IN packets we only want to know when the last packet has been
579 	 * transmitted (not just put into internal buffers).
580 	 */
581 	req->last_desc->ctrl |= GR_DESC_IN_CTRL_PI;
582 
583 	return 0;
584 
585 alloc_err:
586 	gr_free_dma_desc_chain(ep->dev, req);
587 
588 	return ret;
589 }
590 
591 /* Must be called with dev->lock held */
592 static int gr_queue(struct gr_ep *ep, struct gr_request *req, gfp_t gfp_flags)
593 {
594 	struct gr_udc *dev = ep->dev;
595 	int ret;
596 
597 	if (unlikely(!ep->ep.desc && ep->num != 0)) {
598 		dev_err(dev->dev, "No ep descriptor for %s\n", ep->ep.name);
599 		return -EINVAL;
600 	}
601 
602 	if (unlikely(!req->req.buf || !list_empty(&req->queue))) {
603 		dev_err(dev->dev,
604 			"Invalid request for %s: buf=%p list_empty=%d\n",
605 			ep->ep.name, req->req.buf, list_empty(&req->queue));
606 		return -EINVAL;
607 	}
608 
609 	if (unlikely(!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)) {
610 		dev_err(dev->dev, "-ESHUTDOWN");
611 		return -ESHUTDOWN;
612 	}
613 
614 	/* Can't touch registers when suspended */
615 	if (dev->ep0state == GR_EP0_SUSPEND) {
616 		dev_err(dev->dev, "-EBUSY");
617 		return -EBUSY;
618 	}
619 
620 	/* Set up DMA mapping in case the caller didn't */
621 	ret = usb_gadget_map_request(&dev->gadget, &req->req, ep->is_in);
622 	if (ret) {
623 		dev_err(dev->dev, "usb_gadget_map_request");
624 		return ret;
625 	}
626 
627 	if (ep->is_in)
628 		ret = gr_setup_in_desc_list(ep, req, gfp_flags);
629 	else
630 		ret = gr_setup_out_desc_list(ep, req, gfp_flags);
631 	if (ret)
632 		return ret;
633 
634 	req->req.status = -EINPROGRESS;
635 	req->req.actual = 0;
636 	list_add_tail(&req->queue, &ep->queue);
637 
638 	/* Start DMA if not started, otherwise interrupt handler handles it */
639 	if (!ep->dma_start && likely(!ep->stopped))
640 		gr_start_dma(ep);
641 
642 	return 0;
643 }
644 
645 /*
646  * Queue a request from within the driver.
647  *
648  * Must be called with dev->lock held.
649  */
650 static inline int gr_queue_int(struct gr_ep *ep, struct gr_request *req,
651 			       gfp_t gfp_flags)
652 {
653 	if (ep->is_in)
654 		gr_dbgprint_request("RESP", ep, req);
655 
656 	return gr_queue(ep, req, gfp_flags);
657 }
658 
659 /* ---------------------------------------------------------------------- */
660 /* General helper functions */
661 
662 /*
663  * Dequeue ALL requests.
664  *
665  * Must be called with dev->lock held and irqs disabled.
666  */
667 static void gr_ep_nuke(struct gr_ep *ep)
668 {
669 	struct gr_request *req;
670 
671 	ep->stopped = 1;
672 	ep->dma_start = 0;
673 	gr_abort_dma(ep);
674 
675 	while (!list_empty(&ep->queue)) {
676 		req = list_first_entry(&ep->queue, struct gr_request, queue);
677 		gr_finish_request(ep, req, -ESHUTDOWN);
678 	}
679 }
680 
681 /*
682  * Reset the hardware state of this endpoint.
683  *
684  * Must be called with dev->lock held.
685  */
686 static void gr_ep_reset(struct gr_ep *ep)
687 {
688 	gr_write32(&ep->regs->epctrl, 0);
689 	gr_write32(&ep->regs->dmactrl, 0);
690 
691 	ep->ep.maxpacket = MAX_CTRL_PL_SIZE;
692 	ep->ep.desc = NULL;
693 	ep->stopped = 1;
694 	ep->dma_start = 0;
695 }
696 
697 /*
698  * Generate STALL on ep0in/out.
699  *
700  * Must be called with dev->lock held.
701  */
702 static void gr_control_stall(struct gr_udc *dev)
703 {
704 	u32 epctrl;
705 
706 	epctrl = gr_read32(&dev->epo[0].regs->epctrl);
707 	gr_write32(&dev->epo[0].regs->epctrl, epctrl | GR_EPCTRL_CS);
708 	epctrl = gr_read32(&dev->epi[0].regs->epctrl);
709 	gr_write32(&dev->epi[0].regs->epctrl, epctrl | GR_EPCTRL_CS);
710 
711 	dev->ep0state = GR_EP0_STALL;
712 }
713 
714 /*
715  * Halts, halts and wedges, or clears halt for an endpoint.
716  *
717  * Must be called with dev->lock held.
718  */
719 static int gr_ep_halt_wedge(struct gr_ep *ep, int halt, int wedge, int fromhost)
720 {
721 	u32 epctrl;
722 	int retval = 0;
723 
724 	if (ep->num && !ep->ep.desc)
725 		return -EINVAL;
726 
727 	if (ep->num && ep->ep.desc->bmAttributes == USB_ENDPOINT_XFER_ISOC)
728 		return -EOPNOTSUPP;
729 
730 	/* Never actually halt ep0, and therefore never clear halt for ep0 */
731 	if (!ep->num) {
732 		if (halt && !fromhost) {
733 			/* ep0 halt from gadget - generate protocol stall */
734 			gr_control_stall(ep->dev);
735 			dev_dbg(ep->dev->dev, "EP: stall ep0\n");
736 			return 0;
737 		}
738 		return -EINVAL;
739 	}
740 
741 	dev_dbg(ep->dev->dev, "EP: %s halt %s\n",
742 		(halt ? (wedge ? "wedge" : "set") : "clear"), ep->ep.name);
743 
744 	epctrl = gr_read32(&ep->regs->epctrl);
745 	if (halt) {
746 		/* Set HALT */
747 		gr_write32(&ep->regs->epctrl, epctrl | GR_EPCTRL_EH);
748 		ep->stopped = 1;
749 		if (wedge)
750 			ep->wedged = 1;
751 	} else {
752 		gr_write32(&ep->regs->epctrl, epctrl & ~GR_EPCTRL_EH);
753 		ep->stopped = 0;
754 		ep->wedged = 0;
755 
756 		/* Things might have been queued up in the meantime */
757 		if (!ep->dma_start)
758 			gr_start_dma(ep);
759 	}
760 
761 	return retval;
762 }
763 
764 /* Must be called with dev->lock held */
765 static inline void gr_set_ep0state(struct gr_udc *dev, enum gr_ep0state value)
766 {
767 	if (dev->ep0state != value)
768 		dev_vdbg(dev->dev, "STATE:  ep0state=%s\n",
769 			 gr_ep0state_string(value));
770 	dev->ep0state = value;
771 }
772 
773 /*
774  * Should only be called when endpoints can not generate interrupts.
775  *
776  * Must be called with dev->lock held.
777  */
778 static void gr_disable_interrupts_and_pullup(struct gr_udc *dev)
779 {
780 	gr_write32(&dev->regs->control, 0);
781 	wmb(); /* Make sure that we do not deny one of our interrupts */
782 	dev->irq_enabled = 0;
783 }
784 
785 /*
786  * Stop all device activity and disable data line pullup.
787  *
788  * Must be called with dev->lock held and irqs disabled.
789  */
790 static void gr_stop_activity(struct gr_udc *dev)
791 {
792 	struct gr_ep *ep;
793 
794 	list_for_each_entry(ep, &dev->ep_list, ep_list)
795 		gr_ep_nuke(ep);
796 
797 	gr_disable_interrupts_and_pullup(dev);
798 
799 	gr_set_ep0state(dev, GR_EP0_DISCONNECT);
800 	usb_gadget_set_state(&dev->gadget, USB_STATE_NOTATTACHED);
801 }
802 
803 /* ---------------------------------------------------------------------- */
804 /* ep0 setup packet handling */
805 
806 static void gr_ep0_testmode_complete(struct usb_ep *_ep,
807 				     struct usb_request *_req)
808 {
809 	struct gr_ep *ep;
810 	struct gr_udc *dev;
811 	u32 control;
812 
813 	ep = container_of(_ep, struct gr_ep, ep);
814 	dev = ep->dev;
815 
816 	spin_lock(&dev->lock);
817 
818 	control = gr_read32(&dev->regs->control);
819 	control |= GR_CONTROL_TM | (dev->test_mode << GR_CONTROL_TS_POS);
820 	gr_write32(&dev->regs->control, control);
821 
822 	spin_unlock(&dev->lock);
823 }
824 
825 static void gr_ep0_dummy_complete(struct usb_ep *_ep, struct usb_request *_req)
826 {
827 	/* Nothing needs to be done here */
828 }
829 
830 /*
831  * Queue a response on ep0in.
832  *
833  * Must be called with dev->lock held.
834  */
835 static int gr_ep0_respond(struct gr_udc *dev, u8 *buf, int length,
836 			  void (*complete)(struct usb_ep *ep,
837 					   struct usb_request *req))
838 {
839 	u8 *reqbuf = dev->ep0reqi->req.buf;
840 	int status;
841 	int i;
842 
843 	for (i = 0; i < length; i++)
844 		reqbuf[i] = buf[i];
845 	dev->ep0reqi->req.length = length;
846 	dev->ep0reqi->req.complete = complete;
847 
848 	status = gr_queue_int(&dev->epi[0], dev->ep0reqi, GFP_ATOMIC);
849 	if (status < 0)
850 		dev_err(dev->dev,
851 			"Could not queue ep0in setup response: %d\n", status);
852 
853 	return status;
854 }
855 
856 /*
857  * Queue a 2 byte response on ep0in.
858  *
859  * Must be called with dev->lock held.
860  */
861 static inline int gr_ep0_respond_u16(struct gr_udc *dev, u16 response)
862 {
863 	__le16 le_response = cpu_to_le16(response);
864 
865 	return gr_ep0_respond(dev, (u8 *)&le_response, 2,
866 			      gr_ep0_dummy_complete);
867 }
868 
869 /*
870  * Queue a ZLP response on ep0in.
871  *
872  * Must be called with dev->lock held.
873  */
874 static inline int gr_ep0_respond_empty(struct gr_udc *dev)
875 {
876 	return gr_ep0_respond(dev, NULL, 0, gr_ep0_dummy_complete);
877 }
878 
879 /*
880  * This is run when a SET_ADDRESS request is received. First writes
881  * the new address to the control register which is updated internally
882  * when the next IN packet is ACKED.
883  *
884  * Must be called with dev->lock held.
885  */
886 static void gr_set_address(struct gr_udc *dev, u8 address)
887 {
888 	u32 control;
889 
890 	control = gr_read32(&dev->regs->control) & ~GR_CONTROL_UA_MASK;
891 	control |= (address << GR_CONTROL_UA_POS) & GR_CONTROL_UA_MASK;
892 	control |= GR_CONTROL_SU;
893 	gr_write32(&dev->regs->control, control);
894 }
895 
896 /*
897  * Returns negative for STALL, 0 for successful handling and positive for
898  * delegation.
899  *
900  * Must be called with dev->lock held.
901  */
902 static int gr_device_request(struct gr_udc *dev, u8 type, u8 request,
903 			     u16 value, u16 index)
904 {
905 	u16 response;
906 	u8 test;
907 
908 	switch (request) {
909 	case USB_REQ_SET_ADDRESS:
910 		dev_dbg(dev->dev, "STATUS: address %d\n", value & 0xff);
911 		gr_set_address(dev, value & 0xff);
912 		if (value)
913 			usb_gadget_set_state(&dev->gadget, USB_STATE_ADDRESS);
914 		else
915 			usb_gadget_set_state(&dev->gadget, USB_STATE_DEFAULT);
916 		return gr_ep0_respond_empty(dev);
917 
918 	case USB_REQ_GET_STATUS:
919 		/* Self powered | remote wakeup */
920 		response = 0x0001 | (dev->remote_wakeup ? 0x0002 : 0);
921 		return gr_ep0_respond_u16(dev, response);
922 
923 	case USB_REQ_SET_FEATURE:
924 		switch (value) {
925 		case USB_DEVICE_REMOTE_WAKEUP:
926 			/* Allow remote wakeup */
927 			dev->remote_wakeup = 1;
928 			return gr_ep0_respond_empty(dev);
929 
930 		case USB_DEVICE_TEST_MODE:
931 			/* The hardware does not support TEST_FORCE_EN */
932 			test = index >> 8;
933 			if (test >= TEST_J && test <= TEST_PACKET) {
934 				dev->test_mode = test;
935 				return gr_ep0_respond(dev, NULL, 0,
936 						      gr_ep0_testmode_complete);
937 			}
938 		}
939 		break;
940 
941 	case USB_REQ_CLEAR_FEATURE:
942 		switch (value) {
943 		case USB_DEVICE_REMOTE_WAKEUP:
944 			/* Disallow remote wakeup */
945 			dev->remote_wakeup = 0;
946 			return gr_ep0_respond_empty(dev);
947 		}
948 		break;
949 	}
950 
951 	return 1; /* Delegate the rest */
952 }
953 
954 /*
955  * Returns negative for STALL, 0 for successful handling and positive for
956  * delegation.
957  *
958  * Must be called with dev->lock held.
959  */
960 static int gr_interface_request(struct gr_udc *dev, u8 type, u8 request,
961 				u16 value, u16 index)
962 {
963 	if (dev->gadget.state != USB_STATE_CONFIGURED)
964 		return -1;
965 
966 	/*
967 	 * Should return STALL for invalid interfaces, but udc driver does not
968 	 * know anything about that. However, many gadget drivers do not handle
969 	 * GET_STATUS so we need to take care of that.
970 	 */
971 
972 	switch (request) {
973 	case USB_REQ_GET_STATUS:
974 		return gr_ep0_respond_u16(dev, 0x0000);
975 
976 	case USB_REQ_SET_FEATURE:
977 	case USB_REQ_CLEAR_FEATURE:
978 		/*
979 		 * No possible valid standard requests. Still let gadget drivers
980 		 * have a go at it.
981 		 */
982 		break;
983 	}
984 
985 	return 1; /* Delegate the rest */
986 }
987 
988 /*
989  * Returns negative for STALL, 0 for successful handling and positive for
990  * delegation.
991  *
992  * Must be called with dev->lock held.
993  */
994 static int gr_endpoint_request(struct gr_udc *dev, u8 type, u8 request,
995 			       u16 value, u16 index)
996 {
997 	struct gr_ep *ep;
998 	int status;
999 	int halted;
1000 	u8 epnum = index & USB_ENDPOINT_NUMBER_MASK;
1001 	u8 is_in = index & USB_ENDPOINT_DIR_MASK;
1002 
1003 	if ((is_in && epnum >= dev->nepi) || (!is_in && epnum >= dev->nepo))
1004 		return -1;
1005 
1006 	if (dev->gadget.state != USB_STATE_CONFIGURED && epnum != 0)
1007 		return -1;
1008 
1009 	ep = (is_in ? &dev->epi[epnum] : &dev->epo[epnum]);
1010 
1011 	switch (request) {
1012 	case USB_REQ_GET_STATUS:
1013 		halted = gr_read32(&ep->regs->epctrl) & GR_EPCTRL_EH;
1014 		return gr_ep0_respond_u16(dev, halted ? 0x0001 : 0);
1015 
1016 	case USB_REQ_SET_FEATURE:
1017 		switch (value) {
1018 		case USB_ENDPOINT_HALT:
1019 			status = gr_ep_halt_wedge(ep, 1, 0, 1);
1020 			if (status >= 0)
1021 				status = gr_ep0_respond_empty(dev);
1022 			return status;
1023 		}
1024 		break;
1025 
1026 	case USB_REQ_CLEAR_FEATURE:
1027 		switch (value) {
1028 		case USB_ENDPOINT_HALT:
1029 			if (ep->wedged)
1030 				return -1;
1031 			status = gr_ep_halt_wedge(ep, 0, 0, 1);
1032 			if (status >= 0)
1033 				status = gr_ep0_respond_empty(dev);
1034 			return status;
1035 		}
1036 		break;
1037 	}
1038 
1039 	return 1; /* Delegate the rest */
1040 }
1041 
1042 /* Must be called with dev->lock held */
1043 static void gr_ep0out_requeue(struct gr_udc *dev)
1044 {
1045 	int ret = gr_queue_int(&dev->epo[0], dev->ep0reqo, GFP_ATOMIC);
1046 
1047 	if (ret)
1048 		dev_err(dev->dev, "Could not queue ep0out setup request: %d\n",
1049 			ret);
1050 }
1051 
1052 /*
1053  * The main function dealing with setup requests on ep0.
1054  *
1055  * Must be called with dev->lock held and irqs disabled
1056  */
1057 static void gr_ep0_setup(struct gr_udc *dev, struct gr_request *req)
1058 	__releases(&dev->lock)
1059 	__acquires(&dev->lock)
1060 {
1061 	union {
1062 		struct usb_ctrlrequest ctrl;
1063 		u8 raw[8];
1064 		u32 word[2];
1065 	} u;
1066 	u8 type;
1067 	u8 request;
1068 	u16 value;
1069 	u16 index;
1070 	u16 length;
1071 	int i;
1072 	int status;
1073 
1074 	/* Restore from ep0 halt */
1075 	if (dev->ep0state == GR_EP0_STALL) {
1076 		gr_set_ep0state(dev, GR_EP0_SETUP);
1077 		if (!req->req.actual)
1078 			goto out;
1079 	}
1080 
1081 	if (dev->ep0state == GR_EP0_ISTATUS) {
1082 		gr_set_ep0state(dev, GR_EP0_SETUP);
1083 		if (req->req.actual > 0)
1084 			dev_dbg(dev->dev,
1085 				"Unexpected setup packet at state %s\n",
1086 				gr_ep0state_string(GR_EP0_ISTATUS));
1087 		else
1088 			goto out; /* Got expected ZLP */
1089 	} else if (dev->ep0state != GR_EP0_SETUP) {
1090 		dev_info(dev->dev,
1091 			 "Unexpected ep0out request at state %s - stalling\n",
1092 			 gr_ep0state_string(dev->ep0state));
1093 		gr_control_stall(dev);
1094 		gr_set_ep0state(dev, GR_EP0_SETUP);
1095 		goto out;
1096 	} else if (!req->req.actual) {
1097 		dev_dbg(dev->dev, "Unexpected ZLP at state %s\n",
1098 			gr_ep0state_string(dev->ep0state));
1099 		goto out;
1100 	}
1101 
1102 	/* Handle SETUP packet */
1103 	for (i = 0; i < req->req.actual; i++)
1104 		u.raw[i] = ((u8 *)req->req.buf)[i];
1105 
1106 	type = u.ctrl.bRequestType;
1107 	request = u.ctrl.bRequest;
1108 	value = le16_to_cpu(u.ctrl.wValue);
1109 	index = le16_to_cpu(u.ctrl.wIndex);
1110 	length = le16_to_cpu(u.ctrl.wLength);
1111 
1112 	gr_dbgprint_devreq(dev, type, request, value, index, length);
1113 
1114 	/* Check for data stage */
1115 	if (length) {
1116 		if (type & USB_DIR_IN)
1117 			gr_set_ep0state(dev, GR_EP0_IDATA);
1118 		else
1119 			gr_set_ep0state(dev, GR_EP0_ODATA);
1120 	}
1121 
1122 	status = 1; /* Positive status flags delegation */
1123 	if ((type & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1124 		switch (type & USB_RECIP_MASK) {
1125 		case USB_RECIP_DEVICE:
1126 			status = gr_device_request(dev, type, request,
1127 						   value, index);
1128 			break;
1129 		case USB_RECIP_ENDPOINT:
1130 			status =  gr_endpoint_request(dev, type, request,
1131 						      value, index);
1132 			break;
1133 		case USB_RECIP_INTERFACE:
1134 			status = gr_interface_request(dev, type, request,
1135 						      value, index);
1136 			break;
1137 		}
1138 	}
1139 
1140 	if (status > 0) {
1141 		spin_unlock(&dev->lock);
1142 
1143 		dev_vdbg(dev->dev, "DELEGATE\n");
1144 		status = dev->driver->setup(&dev->gadget, &u.ctrl);
1145 
1146 		spin_lock(&dev->lock);
1147 	}
1148 
1149 	/* Generate STALL on both ep0out and ep0in if requested */
1150 	if (unlikely(status < 0)) {
1151 		dev_vdbg(dev->dev, "STALL\n");
1152 		gr_control_stall(dev);
1153 	}
1154 
1155 	if ((type & USB_TYPE_MASK) == USB_TYPE_STANDARD &&
1156 	    request == USB_REQ_SET_CONFIGURATION) {
1157 		if (!value) {
1158 			dev_dbg(dev->dev, "STATUS: deconfigured\n");
1159 			usb_gadget_set_state(&dev->gadget, USB_STATE_ADDRESS);
1160 		} else if (status >= 0) {
1161 			/* Not configured unless gadget OK:s it */
1162 			dev_dbg(dev->dev, "STATUS: configured: %d\n", value);
1163 			usb_gadget_set_state(&dev->gadget,
1164 					     USB_STATE_CONFIGURED);
1165 		}
1166 	}
1167 
1168 	/* Get ready for next stage */
1169 	if (dev->ep0state == GR_EP0_ODATA)
1170 		gr_set_ep0state(dev, GR_EP0_OSTATUS);
1171 	else if (dev->ep0state == GR_EP0_IDATA)
1172 		gr_set_ep0state(dev, GR_EP0_ISTATUS);
1173 	else
1174 		gr_set_ep0state(dev, GR_EP0_SETUP);
1175 
1176 out:
1177 	gr_ep0out_requeue(dev);
1178 }
1179 
1180 /* ---------------------------------------------------------------------- */
1181 /* VBUS and USB reset handling */
1182 
1183 /* Must be called with dev->lock held and irqs disabled  */
1184 static void gr_vbus_connected(struct gr_udc *dev, u32 status)
1185 {
1186 	u32 control;
1187 
1188 	dev->gadget.speed = GR_SPEED(status);
1189 	usb_gadget_set_state(&dev->gadget, USB_STATE_POWERED);
1190 
1191 	/* Turn on full interrupts and pullup */
1192 	control = (GR_CONTROL_SI | GR_CONTROL_UI | GR_CONTROL_VI |
1193 		   GR_CONTROL_SP | GR_CONTROL_EP);
1194 	gr_write32(&dev->regs->control, control);
1195 }
1196 
1197 /* Must be called with dev->lock held */
1198 static void gr_enable_vbus_detect(struct gr_udc *dev)
1199 {
1200 	u32 status;
1201 
1202 	dev->irq_enabled = 1;
1203 	wmb(); /* Make sure we do not ignore an interrupt */
1204 	gr_write32(&dev->regs->control, GR_CONTROL_VI);
1205 
1206 	/* Take care of the case we are already plugged in at this point */
1207 	status = gr_read32(&dev->regs->status);
1208 	if (status & GR_STATUS_VB)
1209 		gr_vbus_connected(dev, status);
1210 }
1211 
1212 /* Must be called with dev->lock held and irqs disabled */
1213 static void gr_vbus_disconnected(struct gr_udc *dev)
1214 {
1215 	gr_stop_activity(dev);
1216 
1217 	/* Report disconnect */
1218 	if (dev->driver && dev->driver->disconnect) {
1219 		spin_unlock(&dev->lock);
1220 
1221 		dev->driver->disconnect(&dev->gadget);
1222 
1223 		spin_lock(&dev->lock);
1224 	}
1225 
1226 	gr_enable_vbus_detect(dev);
1227 }
1228 
1229 /* Must be called with dev->lock held and irqs disabled */
1230 static void gr_udc_usbreset(struct gr_udc *dev, u32 status)
1231 {
1232 	gr_set_address(dev, 0);
1233 	gr_set_ep0state(dev, GR_EP0_SETUP);
1234 	usb_gadget_set_state(&dev->gadget, USB_STATE_DEFAULT);
1235 	dev->gadget.speed = GR_SPEED(status);
1236 
1237 	gr_ep_nuke(&dev->epo[0]);
1238 	gr_ep_nuke(&dev->epi[0]);
1239 	dev->epo[0].stopped = 0;
1240 	dev->epi[0].stopped = 0;
1241 	gr_ep0out_requeue(dev);
1242 }
1243 
1244 /* ---------------------------------------------------------------------- */
1245 /* Irq handling */
1246 
1247 /*
1248  * Handles interrupts from in endpoints. Returns whether something was handled.
1249  *
1250  * Must be called with dev->lock held, irqs disabled and with !ep->stopped.
1251  */
1252 static int gr_handle_in_ep(struct gr_ep *ep)
1253 {
1254 	struct gr_request *req;
1255 
1256 	req = list_first_entry(&ep->queue, struct gr_request, queue);
1257 	if (!req->last_desc)
1258 		return 0;
1259 
1260 	if (READ_ONCE(req->last_desc->ctrl) & GR_DESC_IN_CTRL_EN)
1261 		return 0; /* Not put in hardware buffers yet */
1262 
1263 	if (gr_read32(&ep->regs->epstat) & (GR_EPSTAT_B1 | GR_EPSTAT_B0))
1264 		return 0; /* Not transmitted yet, still in hardware buffers */
1265 
1266 	/* Write complete */
1267 	gr_dma_advance(ep, 0);
1268 
1269 	return 1;
1270 }
1271 
1272 /*
1273  * Handles interrupts from out endpoints. Returns whether something was handled.
1274  *
1275  * Must be called with dev->lock held, irqs disabled and with !ep->stopped.
1276  */
1277 static int gr_handle_out_ep(struct gr_ep *ep)
1278 {
1279 	u32 ep_dmactrl;
1280 	u32 ctrl;
1281 	u16 len;
1282 	struct gr_request *req;
1283 	struct gr_udc *dev = ep->dev;
1284 
1285 	req = list_first_entry(&ep->queue, struct gr_request, queue);
1286 	if (!req->curr_desc)
1287 		return 0;
1288 
1289 	ctrl = READ_ONCE(req->curr_desc->ctrl);
1290 	if (ctrl & GR_DESC_OUT_CTRL_EN)
1291 		return 0; /* Not received yet */
1292 
1293 	/* Read complete */
1294 	len = ctrl & GR_DESC_OUT_CTRL_LEN_MASK;
1295 	req->req.actual += len;
1296 	if (ctrl & GR_DESC_OUT_CTRL_SE)
1297 		req->setup = 1;
1298 
1299 	if (len < ep->ep.maxpacket || req->req.actual >= req->req.length) {
1300 		/* Short packet or >= expected size - we are done */
1301 
1302 		if ((ep == &dev->epo[0]) && (dev->ep0state == GR_EP0_OSTATUS)) {
1303 			/*
1304 			 * Send a status stage ZLP to ack the DATA stage in the
1305 			 * OUT direction. This needs to be done before
1306 			 * gr_dma_advance as that can lead to a call to
1307 			 * ep0_setup that can change dev->ep0state.
1308 			 */
1309 			gr_ep0_respond_empty(dev);
1310 			gr_set_ep0state(dev, GR_EP0_SETUP);
1311 		}
1312 
1313 		gr_dma_advance(ep, 0);
1314 	} else {
1315 		/* Not done yet. Enable the next descriptor to receive more. */
1316 		req->curr_desc = req->curr_desc->next_desc;
1317 		req->curr_desc->ctrl |= GR_DESC_OUT_CTRL_EN;
1318 
1319 		ep_dmactrl = gr_read32(&ep->regs->dmactrl);
1320 		gr_write32(&ep->regs->dmactrl, ep_dmactrl | GR_DMACTRL_DA);
1321 	}
1322 
1323 	return 1;
1324 }
1325 
1326 /*
1327  * Handle state changes. Returns whether something was handled.
1328  *
1329  * Must be called with dev->lock held and irqs disabled.
1330  */
1331 static int gr_handle_state_changes(struct gr_udc *dev)
1332 {
1333 	u32 status = gr_read32(&dev->regs->status);
1334 	int handled = 0;
1335 	int powstate = !(dev->gadget.state == USB_STATE_NOTATTACHED ||
1336 			 dev->gadget.state == USB_STATE_ATTACHED);
1337 
1338 	/* VBUS valid detected */
1339 	if (!powstate && (status & GR_STATUS_VB)) {
1340 		dev_dbg(dev->dev, "STATUS: vbus valid detected\n");
1341 		gr_vbus_connected(dev, status);
1342 		handled = 1;
1343 	}
1344 
1345 	/* Disconnect */
1346 	if (powstate && !(status & GR_STATUS_VB)) {
1347 		dev_dbg(dev->dev, "STATUS: vbus invalid detected\n");
1348 		gr_vbus_disconnected(dev);
1349 		handled = 1;
1350 	}
1351 
1352 	/* USB reset detected */
1353 	if (status & GR_STATUS_UR) {
1354 		dev_dbg(dev->dev, "STATUS: USB reset - speed is %s\n",
1355 			GR_SPEED_STR(status));
1356 		gr_write32(&dev->regs->status, GR_STATUS_UR);
1357 		gr_udc_usbreset(dev, status);
1358 		handled = 1;
1359 	}
1360 
1361 	/* Speed change */
1362 	if (dev->gadget.speed != GR_SPEED(status)) {
1363 		dev_dbg(dev->dev, "STATUS: USB Speed change to %s\n",
1364 			GR_SPEED_STR(status));
1365 		dev->gadget.speed = GR_SPEED(status);
1366 		handled = 1;
1367 	}
1368 
1369 	/* Going into suspend */
1370 	if ((dev->ep0state != GR_EP0_SUSPEND) && !(status & GR_STATUS_SU)) {
1371 		dev_dbg(dev->dev, "STATUS: USB suspend\n");
1372 		gr_set_ep0state(dev, GR_EP0_SUSPEND);
1373 		dev->suspended_from = dev->gadget.state;
1374 		usb_gadget_set_state(&dev->gadget, USB_STATE_SUSPENDED);
1375 
1376 		if ((dev->gadget.speed != USB_SPEED_UNKNOWN) &&
1377 		    dev->driver && dev->driver->suspend) {
1378 			spin_unlock(&dev->lock);
1379 
1380 			dev->driver->suspend(&dev->gadget);
1381 
1382 			spin_lock(&dev->lock);
1383 		}
1384 		handled = 1;
1385 	}
1386 
1387 	/* Coming out of suspend */
1388 	if ((dev->ep0state == GR_EP0_SUSPEND) && (status & GR_STATUS_SU)) {
1389 		dev_dbg(dev->dev, "STATUS: USB resume\n");
1390 		if (dev->suspended_from == USB_STATE_POWERED)
1391 			gr_set_ep0state(dev, GR_EP0_DISCONNECT);
1392 		else
1393 			gr_set_ep0state(dev, GR_EP0_SETUP);
1394 		usb_gadget_set_state(&dev->gadget, dev->suspended_from);
1395 
1396 		if ((dev->gadget.speed != USB_SPEED_UNKNOWN) &&
1397 		    dev->driver && dev->driver->resume) {
1398 			spin_unlock(&dev->lock);
1399 
1400 			dev->driver->resume(&dev->gadget);
1401 
1402 			spin_lock(&dev->lock);
1403 		}
1404 		handled = 1;
1405 	}
1406 
1407 	return handled;
1408 }
1409 
1410 /* Non-interrupt context irq handler */
1411 static irqreturn_t gr_irq_handler(int irq, void *_dev)
1412 {
1413 	struct gr_udc *dev = _dev;
1414 	struct gr_ep *ep;
1415 	int handled = 0;
1416 	int i;
1417 	unsigned long flags;
1418 
1419 	spin_lock_irqsave(&dev->lock, flags);
1420 
1421 	if (!dev->irq_enabled)
1422 		goto out;
1423 
1424 	/*
1425 	 * Check IN ep interrupts. We check these before the OUT eps because
1426 	 * some gadgets reuse the request that might already be currently
1427 	 * outstanding and needs to be completed (mainly setup requests).
1428 	 */
1429 	for (i = 0; i < dev->nepi; i++) {
1430 		ep = &dev->epi[i];
1431 		if (!ep->stopped && !ep->callback && !list_empty(&ep->queue))
1432 			handled = gr_handle_in_ep(ep) || handled;
1433 	}
1434 
1435 	/* Check OUT ep interrupts */
1436 	for (i = 0; i < dev->nepo; i++) {
1437 		ep = &dev->epo[i];
1438 		if (!ep->stopped && !ep->callback && !list_empty(&ep->queue))
1439 			handled = gr_handle_out_ep(ep) || handled;
1440 	}
1441 
1442 	/* Check status interrupts */
1443 	handled = gr_handle_state_changes(dev) || handled;
1444 
1445 	/*
1446 	 * Check AMBA DMA errors. Only check if we didn't find anything else to
1447 	 * handle because this shouldn't happen if we did everything right.
1448 	 */
1449 	if (!handled) {
1450 		list_for_each_entry(ep, &dev->ep_list, ep_list) {
1451 			if (gr_read32(&ep->regs->dmactrl) & GR_DMACTRL_AE) {
1452 				dev_err(dev->dev,
1453 					"AMBA Error occurred for %s\n",
1454 					ep->ep.name);
1455 				handled = 1;
1456 			}
1457 		}
1458 	}
1459 
1460 out:
1461 	spin_unlock_irqrestore(&dev->lock, flags);
1462 
1463 	return handled ? IRQ_HANDLED : IRQ_NONE;
1464 }
1465 
1466 /* Interrupt context irq handler */
1467 static irqreturn_t gr_irq(int irq, void *_dev)
1468 {
1469 	struct gr_udc *dev = _dev;
1470 
1471 	if (!dev->irq_enabled)
1472 		return IRQ_NONE;
1473 
1474 	return IRQ_WAKE_THREAD;
1475 }
1476 
1477 /* ---------------------------------------------------------------------- */
1478 /* USB ep ops */
1479 
1480 /* Enable endpoint. Not for ep0in and ep0out that are handled separately. */
1481 static int gr_ep_enable(struct usb_ep *_ep,
1482 			const struct usb_endpoint_descriptor *desc)
1483 {
1484 	struct gr_udc *dev;
1485 	struct gr_ep *ep;
1486 	u8 mode;
1487 	u8 nt;
1488 	u16 max;
1489 	u16 buffer_size = 0;
1490 	u32 epctrl;
1491 
1492 	ep = container_of(_ep, struct gr_ep, ep);
1493 	if (!_ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT)
1494 		return -EINVAL;
1495 
1496 	dev = ep->dev;
1497 
1498 	/* 'ep0' IN and OUT are reserved */
1499 	if (ep == &dev->epo[0] || ep == &dev->epi[0])
1500 		return -EINVAL;
1501 
1502 	if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
1503 		return -ESHUTDOWN;
1504 
1505 	/* Make sure we are clear for enabling */
1506 	epctrl = gr_read32(&ep->regs->epctrl);
1507 	if (epctrl & GR_EPCTRL_EV)
1508 		return -EBUSY;
1509 
1510 	/* Check that directions match */
1511 	if (!ep->is_in != !usb_endpoint_dir_in(desc))
1512 		return -EINVAL;
1513 
1514 	/* Check ep num */
1515 	if ((!ep->is_in && ep->num >= dev->nepo) ||
1516 	    (ep->is_in && ep->num >= dev->nepi))
1517 		return -EINVAL;
1518 
1519 	if (usb_endpoint_xfer_control(desc)) {
1520 		mode = 0;
1521 	} else if (usb_endpoint_xfer_isoc(desc)) {
1522 		mode = 1;
1523 	} else if (usb_endpoint_xfer_bulk(desc)) {
1524 		mode = 2;
1525 	} else if (usb_endpoint_xfer_int(desc)) {
1526 		mode = 3;
1527 	} else {
1528 		dev_err(dev->dev, "Unknown transfer type for %s\n",
1529 			ep->ep.name);
1530 		return -EINVAL;
1531 	}
1532 
1533 	/*
1534 	 * Bits 10-0 set the max payload. 12-11 set the number of
1535 	 * additional transactions.
1536 	 */
1537 	max = usb_endpoint_maxp(desc);
1538 	nt = usb_endpoint_maxp_mult(desc) - 1;
1539 	buffer_size = GR_BUFFER_SIZE(epctrl);
1540 	if (nt && (mode == 0 || mode == 2)) {
1541 		dev_err(dev->dev,
1542 			"%s mode: multiple trans./microframe not valid\n",
1543 			(mode == 2 ? "Bulk" : "Control"));
1544 		return -EINVAL;
1545 	} else if (nt == 0x3) {
1546 		dev_err(dev->dev,
1547 			"Invalid value 0x3 for additional trans./microframe\n");
1548 		return -EINVAL;
1549 	} else if ((nt + 1) * max > buffer_size) {
1550 		dev_err(dev->dev, "Hw buffer size %d < max payload %d * %d\n",
1551 			buffer_size, (nt + 1), max);
1552 		return -EINVAL;
1553 	} else if (max == 0) {
1554 		dev_err(dev->dev, "Max payload cannot be set to 0\n");
1555 		return -EINVAL;
1556 	} else if (max > ep->ep.maxpacket_limit) {
1557 		dev_err(dev->dev, "Requested max payload %d > limit %d\n",
1558 			max, ep->ep.maxpacket_limit);
1559 		return -EINVAL;
1560 	}
1561 
1562 	spin_lock(&ep->dev->lock);
1563 
1564 	if (!ep->stopped) {
1565 		spin_unlock(&ep->dev->lock);
1566 		return -EBUSY;
1567 	}
1568 
1569 	ep->stopped = 0;
1570 	ep->wedged = 0;
1571 	ep->ep.desc = desc;
1572 	ep->ep.maxpacket = max;
1573 	ep->dma_start = 0;
1574 
1575 
1576 	if (nt) {
1577 		/*
1578 		 * Maximum possible size of all payloads in one microframe
1579 		 * regardless of direction when using high-bandwidth mode.
1580 		 */
1581 		ep->bytes_per_buffer = (nt + 1) * max;
1582 	} else if (ep->is_in) {
1583 		/*
1584 		 * The biggest multiple of maximum packet size that fits into
1585 		 * the buffer. The hardware will split up into many packets in
1586 		 * the IN direction.
1587 		 */
1588 		ep->bytes_per_buffer = (buffer_size / max) * max;
1589 	} else {
1590 		/*
1591 		 * Only single packets will be placed the buffers in the OUT
1592 		 * direction.
1593 		 */
1594 		ep->bytes_per_buffer = max;
1595 	}
1596 
1597 	epctrl = (max << GR_EPCTRL_MAXPL_POS)
1598 		| (nt << GR_EPCTRL_NT_POS)
1599 		| (mode << GR_EPCTRL_TT_POS)
1600 		| GR_EPCTRL_EV;
1601 	if (ep->is_in)
1602 		epctrl |= GR_EPCTRL_PI;
1603 	gr_write32(&ep->regs->epctrl, epctrl);
1604 
1605 	gr_write32(&ep->regs->dmactrl, GR_DMACTRL_IE | GR_DMACTRL_AI);
1606 
1607 	spin_unlock(&ep->dev->lock);
1608 
1609 	dev_dbg(ep->dev->dev, "EP: %s enabled - %s with %d bytes/buffer\n",
1610 		ep->ep.name, gr_modestring[mode], ep->bytes_per_buffer);
1611 	return 0;
1612 }
1613 
1614 /* Disable endpoint. Not for ep0in and ep0out that are handled separately. */
1615 static int gr_ep_disable(struct usb_ep *_ep)
1616 {
1617 	struct gr_ep *ep;
1618 	struct gr_udc *dev;
1619 	unsigned long flags;
1620 
1621 	ep = container_of(_ep, struct gr_ep, ep);
1622 	if (!_ep || !ep->ep.desc)
1623 		return -ENODEV;
1624 
1625 	dev = ep->dev;
1626 
1627 	/* 'ep0' IN and OUT are reserved */
1628 	if (ep == &dev->epo[0] || ep == &dev->epi[0])
1629 		return -EINVAL;
1630 
1631 	if (dev->ep0state == GR_EP0_SUSPEND)
1632 		return -EBUSY;
1633 
1634 	dev_dbg(ep->dev->dev, "EP: disable %s\n", ep->ep.name);
1635 
1636 	spin_lock_irqsave(&dev->lock, flags);
1637 
1638 	gr_ep_nuke(ep);
1639 	gr_ep_reset(ep);
1640 	ep->ep.desc = NULL;
1641 
1642 	spin_unlock_irqrestore(&dev->lock, flags);
1643 
1644 	return 0;
1645 }
1646 
1647 /*
1648  * Frees a request, but not any DMA buffers associated with it
1649  * (gr_finish_request should already have taken care of that).
1650  */
1651 static void gr_free_request(struct usb_ep *_ep, struct usb_request *_req)
1652 {
1653 	struct gr_request *req;
1654 
1655 	if (!_ep || !_req)
1656 		return;
1657 	req = container_of(_req, struct gr_request, req);
1658 
1659 	/* Leads to memory leak */
1660 	WARN(!list_empty(&req->queue),
1661 	     "request not dequeued properly before freeing\n");
1662 
1663 	kfree(req);
1664 }
1665 
1666 /* Queue a request from the gadget */
1667 static int gr_queue_ext(struct usb_ep *_ep, struct usb_request *_req,
1668 			gfp_t gfp_flags)
1669 {
1670 	struct gr_ep *ep;
1671 	struct gr_request *req;
1672 	struct gr_udc *dev;
1673 	int ret;
1674 
1675 	if (unlikely(!_ep || !_req))
1676 		return -EINVAL;
1677 
1678 	ep = container_of(_ep, struct gr_ep, ep);
1679 	req = container_of(_req, struct gr_request, req);
1680 	dev = ep->dev;
1681 
1682 	spin_lock(&ep->dev->lock);
1683 
1684 	/*
1685 	 * The ep0 pointer in the gadget struct is used both for ep0in and
1686 	 * ep0out. In a data stage in the out direction ep0out needs to be used
1687 	 * instead of the default ep0in. Completion functions might use
1688 	 * driver_data, so that needs to be copied as well.
1689 	 */
1690 	if ((ep == &dev->epi[0]) && (dev->ep0state == GR_EP0_ODATA)) {
1691 		ep = &dev->epo[0];
1692 		ep->ep.driver_data = dev->epi[0].ep.driver_data;
1693 	}
1694 
1695 	if (ep->is_in)
1696 		gr_dbgprint_request("EXTERN", ep, req);
1697 
1698 	ret = gr_queue(ep, req, GFP_ATOMIC);
1699 
1700 	spin_unlock(&ep->dev->lock);
1701 
1702 	return ret;
1703 }
1704 
1705 /* Dequeue JUST ONE request */
1706 static int gr_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1707 {
1708 	struct gr_request *req;
1709 	struct gr_ep *ep;
1710 	struct gr_udc *dev;
1711 	int ret = 0;
1712 	unsigned long flags;
1713 
1714 	ep = container_of(_ep, struct gr_ep, ep);
1715 	if (!_ep || !_req || (!ep->ep.desc && ep->num != 0))
1716 		return -EINVAL;
1717 	dev = ep->dev;
1718 	if (!dev->driver)
1719 		return -ESHUTDOWN;
1720 
1721 	/* We can't touch (DMA) registers when suspended */
1722 	if (dev->ep0state == GR_EP0_SUSPEND)
1723 		return -EBUSY;
1724 
1725 	spin_lock_irqsave(&dev->lock, flags);
1726 
1727 	/* Make sure it's actually queued on this endpoint */
1728 	list_for_each_entry(req, &ep->queue, queue) {
1729 		if (&req->req == _req)
1730 			break;
1731 	}
1732 	if (&req->req != _req) {
1733 		ret = -EINVAL;
1734 		goto out;
1735 	}
1736 
1737 	if (list_first_entry(&ep->queue, struct gr_request, queue) == req) {
1738 		/* This request is currently being processed */
1739 		gr_abort_dma(ep);
1740 		if (ep->stopped)
1741 			gr_finish_request(ep, req, -ECONNRESET);
1742 		else
1743 			gr_dma_advance(ep, -ECONNRESET);
1744 	} else if (!list_empty(&req->queue)) {
1745 		/* Not being processed - gr_finish_request dequeues it */
1746 		gr_finish_request(ep, req, -ECONNRESET);
1747 	} else {
1748 		ret = -EOPNOTSUPP;
1749 	}
1750 
1751 out:
1752 	spin_unlock_irqrestore(&dev->lock, flags);
1753 
1754 	return ret;
1755 }
1756 
1757 /* Helper for gr_set_halt and gr_set_wedge */
1758 static int gr_set_halt_wedge(struct usb_ep *_ep, int halt, int wedge)
1759 {
1760 	int ret;
1761 	struct gr_ep *ep;
1762 
1763 	if (!_ep)
1764 		return -ENODEV;
1765 	ep = container_of(_ep, struct gr_ep, ep);
1766 
1767 	spin_lock(&ep->dev->lock);
1768 
1769 	/* Halting an IN endpoint should fail if queue is not empty */
1770 	if (halt && ep->is_in && !list_empty(&ep->queue)) {
1771 		ret = -EAGAIN;
1772 		goto out;
1773 	}
1774 
1775 	ret = gr_ep_halt_wedge(ep, halt, wedge, 0);
1776 
1777 out:
1778 	spin_unlock(&ep->dev->lock);
1779 
1780 	return ret;
1781 }
1782 
1783 /* Halt endpoint */
1784 static int gr_set_halt(struct usb_ep *_ep, int halt)
1785 {
1786 	return gr_set_halt_wedge(_ep, halt, 0);
1787 }
1788 
1789 /* Halt and wedge endpoint */
1790 static int gr_set_wedge(struct usb_ep *_ep)
1791 {
1792 	return gr_set_halt_wedge(_ep, 1, 1);
1793 }
1794 
1795 /*
1796  * Return the total number of bytes currently stored in the internal buffers of
1797  * the endpoint.
1798  */
1799 static int gr_fifo_status(struct usb_ep *_ep)
1800 {
1801 	struct gr_ep *ep;
1802 	u32 epstat;
1803 	u32 bytes = 0;
1804 
1805 	if (!_ep)
1806 		return -ENODEV;
1807 	ep = container_of(_ep, struct gr_ep, ep);
1808 
1809 	epstat = gr_read32(&ep->regs->epstat);
1810 
1811 	if (epstat & GR_EPSTAT_B0)
1812 		bytes += (epstat & GR_EPSTAT_B0CNT_MASK) >> GR_EPSTAT_B0CNT_POS;
1813 	if (epstat & GR_EPSTAT_B1)
1814 		bytes += (epstat & GR_EPSTAT_B1CNT_MASK) >> GR_EPSTAT_B1CNT_POS;
1815 
1816 	return bytes;
1817 }
1818 
1819 
1820 /* Empty data from internal buffers of an endpoint. */
1821 static void gr_fifo_flush(struct usb_ep *_ep)
1822 {
1823 	struct gr_ep *ep;
1824 	u32 epctrl;
1825 
1826 	if (!_ep)
1827 		return;
1828 	ep = container_of(_ep, struct gr_ep, ep);
1829 	dev_vdbg(ep->dev->dev, "EP: flush fifo %s\n", ep->ep.name);
1830 
1831 	spin_lock(&ep->dev->lock);
1832 
1833 	epctrl = gr_read32(&ep->regs->epctrl);
1834 	epctrl |= GR_EPCTRL_CB;
1835 	gr_write32(&ep->regs->epctrl, epctrl);
1836 
1837 	spin_unlock(&ep->dev->lock);
1838 }
1839 
1840 static const struct usb_ep_ops gr_ep_ops = {
1841 	.enable		= gr_ep_enable,
1842 	.disable	= gr_ep_disable,
1843 
1844 	.alloc_request	= gr_alloc_request,
1845 	.free_request	= gr_free_request,
1846 
1847 	.queue		= gr_queue_ext,
1848 	.dequeue	= gr_dequeue,
1849 
1850 	.set_halt	= gr_set_halt,
1851 	.set_wedge	= gr_set_wedge,
1852 	.fifo_status	= gr_fifo_status,
1853 	.fifo_flush	= gr_fifo_flush,
1854 };
1855 
1856 /* ---------------------------------------------------------------------- */
1857 /* USB Gadget ops */
1858 
1859 static int gr_get_frame(struct usb_gadget *_gadget)
1860 {
1861 	struct gr_udc *dev;
1862 
1863 	if (!_gadget)
1864 		return -ENODEV;
1865 	dev = container_of(_gadget, struct gr_udc, gadget);
1866 	return gr_read32(&dev->regs->status) & GR_STATUS_FN_MASK;
1867 }
1868 
1869 static int gr_wakeup(struct usb_gadget *_gadget)
1870 {
1871 	struct gr_udc *dev;
1872 
1873 	if (!_gadget)
1874 		return -ENODEV;
1875 	dev = container_of(_gadget, struct gr_udc, gadget);
1876 
1877 	/* Remote wakeup feature not enabled by host*/
1878 	if (!dev->remote_wakeup)
1879 		return -EINVAL;
1880 
1881 	spin_lock(&dev->lock);
1882 
1883 	gr_write32(&dev->regs->control,
1884 		   gr_read32(&dev->regs->control) | GR_CONTROL_RW);
1885 
1886 	spin_unlock(&dev->lock);
1887 
1888 	return 0;
1889 }
1890 
1891 static int gr_pullup(struct usb_gadget *_gadget, int is_on)
1892 {
1893 	struct gr_udc *dev;
1894 	u32 control;
1895 
1896 	if (!_gadget)
1897 		return -ENODEV;
1898 	dev = container_of(_gadget, struct gr_udc, gadget);
1899 
1900 	spin_lock(&dev->lock);
1901 
1902 	control = gr_read32(&dev->regs->control);
1903 	if (is_on)
1904 		control |= GR_CONTROL_EP;
1905 	else
1906 		control &= ~GR_CONTROL_EP;
1907 	gr_write32(&dev->regs->control, control);
1908 
1909 	spin_unlock(&dev->lock);
1910 
1911 	return 0;
1912 }
1913 
1914 static int gr_udc_start(struct usb_gadget *gadget,
1915 			struct usb_gadget_driver *driver)
1916 {
1917 	struct gr_udc *dev = to_gr_udc(gadget);
1918 
1919 	spin_lock(&dev->lock);
1920 
1921 	/* Hook up the driver */
1922 	driver->driver.bus = NULL;
1923 	dev->driver = driver;
1924 
1925 	/* Get ready for host detection */
1926 	gr_enable_vbus_detect(dev);
1927 
1928 	spin_unlock(&dev->lock);
1929 
1930 	return 0;
1931 }
1932 
1933 static int gr_udc_stop(struct usb_gadget *gadget)
1934 {
1935 	struct gr_udc *dev = to_gr_udc(gadget);
1936 	unsigned long flags;
1937 
1938 	spin_lock_irqsave(&dev->lock, flags);
1939 
1940 	dev->driver = NULL;
1941 	gr_stop_activity(dev);
1942 
1943 	spin_unlock_irqrestore(&dev->lock, flags);
1944 
1945 	return 0;
1946 }
1947 
1948 static const struct usb_gadget_ops gr_ops = {
1949 	.get_frame	= gr_get_frame,
1950 	.wakeup         = gr_wakeup,
1951 	.pullup         = gr_pullup,
1952 	.udc_start	= gr_udc_start,
1953 	.udc_stop	= gr_udc_stop,
1954 	/* Other operations not supported */
1955 };
1956 
1957 /* ---------------------------------------------------------------------- */
1958 /* Module probe, removal and of-matching */
1959 
1960 static const char * const onames[] = {
1961 	"ep0out", "ep1out", "ep2out", "ep3out", "ep4out", "ep5out",
1962 	"ep6out", "ep7out", "ep8out", "ep9out", "ep10out", "ep11out",
1963 	"ep12out", "ep13out", "ep14out", "ep15out"
1964 };
1965 
1966 static const char * const inames[] = {
1967 	"ep0in", "ep1in", "ep2in", "ep3in", "ep4in", "ep5in",
1968 	"ep6in", "ep7in", "ep8in", "ep9in", "ep10in", "ep11in",
1969 	"ep12in", "ep13in", "ep14in", "ep15in"
1970 };
1971 
1972 /* Must be called with dev->lock held */
1973 static int gr_ep_init(struct gr_udc *dev, int num, int is_in, u32 maxplimit)
1974 {
1975 	struct gr_ep *ep;
1976 	struct gr_request *req;
1977 	struct usb_request *_req;
1978 	void *buf;
1979 
1980 	if (is_in) {
1981 		ep = &dev->epi[num];
1982 		ep->ep.name = inames[num];
1983 		ep->regs = &dev->regs->epi[num];
1984 	} else {
1985 		ep = &dev->epo[num];
1986 		ep->ep.name = onames[num];
1987 		ep->regs = &dev->regs->epo[num];
1988 	}
1989 
1990 	gr_ep_reset(ep);
1991 	ep->num = num;
1992 	ep->is_in = is_in;
1993 	ep->dev = dev;
1994 	ep->ep.ops = &gr_ep_ops;
1995 	INIT_LIST_HEAD(&ep->queue);
1996 
1997 	if (num == 0) {
1998 		_req = gr_alloc_request(&ep->ep, GFP_ATOMIC);
1999 		buf = devm_kzalloc(dev->dev, PAGE_SIZE, GFP_DMA | GFP_ATOMIC);
2000 		if (!_req || !buf) {
2001 			/* possible _req freed by gr_probe via gr_remove */
2002 			return -ENOMEM;
2003 		}
2004 
2005 		req = container_of(_req, struct gr_request, req);
2006 		req->req.buf = buf;
2007 		req->req.length = MAX_CTRL_PL_SIZE;
2008 
2009 		if (is_in)
2010 			dev->ep0reqi = req; /* Complete gets set as used */
2011 		else
2012 			dev->ep0reqo = req; /* Completion treated separately */
2013 
2014 		usb_ep_set_maxpacket_limit(&ep->ep, MAX_CTRL_PL_SIZE);
2015 		ep->bytes_per_buffer = MAX_CTRL_PL_SIZE;
2016 
2017 		ep->ep.caps.type_control = true;
2018 	} else {
2019 		usb_ep_set_maxpacket_limit(&ep->ep, (u16)maxplimit);
2020 		list_add_tail(&ep->ep.ep_list, &dev->gadget.ep_list);
2021 
2022 		ep->ep.caps.type_iso = true;
2023 		ep->ep.caps.type_bulk = true;
2024 		ep->ep.caps.type_int = true;
2025 	}
2026 	list_add_tail(&ep->ep_list, &dev->ep_list);
2027 
2028 	if (is_in)
2029 		ep->ep.caps.dir_in = true;
2030 	else
2031 		ep->ep.caps.dir_out = true;
2032 
2033 	ep->tailbuf = dma_alloc_coherent(dev->dev, ep->ep.maxpacket_limit,
2034 					 &ep->tailbuf_paddr, GFP_ATOMIC);
2035 	if (!ep->tailbuf)
2036 		return -ENOMEM;
2037 
2038 	return 0;
2039 }
2040 
2041 /* Must be called with dev->lock held */
2042 static int gr_udc_init(struct gr_udc *dev)
2043 {
2044 	struct device_node *np = dev->dev->of_node;
2045 	u32 epctrl_val;
2046 	u32 dmactrl_val;
2047 	int i;
2048 	int ret = 0;
2049 	u32 bufsize;
2050 
2051 	gr_set_address(dev, 0);
2052 
2053 	INIT_LIST_HEAD(&dev->gadget.ep_list);
2054 	dev->gadget.speed = USB_SPEED_UNKNOWN;
2055 	dev->gadget.ep0 = &dev->epi[0].ep;
2056 
2057 	INIT_LIST_HEAD(&dev->ep_list);
2058 	gr_set_ep0state(dev, GR_EP0_DISCONNECT);
2059 
2060 	for (i = 0; i < dev->nepo; i++) {
2061 		if (of_property_read_u32_index(np, "epobufsizes", i, &bufsize))
2062 			bufsize = 1024;
2063 		ret = gr_ep_init(dev, i, 0, bufsize);
2064 		if (ret)
2065 			return ret;
2066 	}
2067 
2068 	for (i = 0; i < dev->nepi; i++) {
2069 		if (of_property_read_u32_index(np, "epibufsizes", i, &bufsize))
2070 			bufsize = 1024;
2071 		ret = gr_ep_init(dev, i, 1, bufsize);
2072 		if (ret)
2073 			return ret;
2074 	}
2075 
2076 	/* Must be disabled by default */
2077 	dev->remote_wakeup = 0;
2078 
2079 	/* Enable ep0out and ep0in */
2080 	epctrl_val = (MAX_CTRL_PL_SIZE << GR_EPCTRL_MAXPL_POS) | GR_EPCTRL_EV;
2081 	dmactrl_val = GR_DMACTRL_IE | GR_DMACTRL_AI;
2082 	gr_write32(&dev->epo[0].regs->epctrl, epctrl_val);
2083 	gr_write32(&dev->epi[0].regs->epctrl, epctrl_val | GR_EPCTRL_PI);
2084 	gr_write32(&dev->epo[0].regs->dmactrl, dmactrl_val);
2085 	gr_write32(&dev->epi[0].regs->dmactrl, dmactrl_val);
2086 
2087 	return 0;
2088 }
2089 
2090 static void gr_ep_remove(struct gr_udc *dev, int num, int is_in)
2091 {
2092 	struct gr_ep *ep;
2093 
2094 	if (is_in)
2095 		ep = &dev->epi[num];
2096 	else
2097 		ep = &dev->epo[num];
2098 
2099 	if (ep->tailbuf)
2100 		dma_free_coherent(dev->dev, ep->ep.maxpacket_limit,
2101 				  ep->tailbuf, ep->tailbuf_paddr);
2102 }
2103 
2104 static int gr_remove(struct platform_device *pdev)
2105 {
2106 	struct gr_udc *dev = platform_get_drvdata(pdev);
2107 	int i;
2108 
2109 	if (dev->added)
2110 		usb_del_gadget_udc(&dev->gadget); /* Shuts everything down */
2111 	if (dev->driver)
2112 		return -EBUSY;
2113 
2114 	gr_dfs_delete(dev);
2115 	dma_pool_destroy(dev->desc_pool);
2116 	platform_set_drvdata(pdev, NULL);
2117 
2118 	gr_free_request(&dev->epi[0].ep, &dev->ep0reqi->req);
2119 	gr_free_request(&dev->epo[0].ep, &dev->ep0reqo->req);
2120 
2121 	for (i = 0; i < dev->nepo; i++)
2122 		gr_ep_remove(dev, i, 0);
2123 	for (i = 0; i < dev->nepi; i++)
2124 		gr_ep_remove(dev, i, 1);
2125 
2126 	return 0;
2127 }
2128 static int gr_request_irq(struct gr_udc *dev, int irq)
2129 {
2130 	return devm_request_threaded_irq(dev->dev, irq, gr_irq, gr_irq_handler,
2131 					 IRQF_SHARED, driver_name, dev);
2132 }
2133 
2134 static int gr_probe(struct platform_device *pdev)
2135 {
2136 	struct gr_udc *dev;
2137 	struct resource *res;
2138 	struct gr_regs __iomem *regs;
2139 	int retval;
2140 	u32 status;
2141 
2142 	dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
2143 	if (!dev)
2144 		return -ENOMEM;
2145 	dev->dev = &pdev->dev;
2146 
2147 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2148 	regs = devm_ioremap_resource(dev->dev, res);
2149 	if (IS_ERR(regs))
2150 		return PTR_ERR(regs);
2151 
2152 	dev->irq = platform_get_irq(pdev, 0);
2153 	if (dev->irq <= 0) {
2154 		dev_err(dev->dev, "No irq found\n");
2155 		return -ENODEV;
2156 	}
2157 
2158 	/* Some core configurations has separate irqs for IN and OUT events */
2159 	dev->irqi = platform_get_irq(pdev, 1);
2160 	if (dev->irqi > 0) {
2161 		dev->irqo = platform_get_irq(pdev, 2);
2162 		if (dev->irqo <= 0) {
2163 			dev_err(dev->dev, "Found irqi but not irqo\n");
2164 			return -ENODEV;
2165 		}
2166 	} else {
2167 		dev->irqi = 0;
2168 	}
2169 
2170 	dev->gadget.name = driver_name;
2171 	dev->gadget.max_speed = USB_SPEED_HIGH;
2172 	dev->gadget.ops = &gr_ops;
2173 
2174 	spin_lock_init(&dev->lock);
2175 	dev->regs = regs;
2176 
2177 	platform_set_drvdata(pdev, dev);
2178 
2179 	/* Determine number of endpoints and data interface mode */
2180 	status = gr_read32(&dev->regs->status);
2181 	dev->nepi = ((status & GR_STATUS_NEPI_MASK) >> GR_STATUS_NEPI_POS) + 1;
2182 	dev->nepo = ((status & GR_STATUS_NEPO_MASK) >> GR_STATUS_NEPO_POS) + 1;
2183 
2184 	if (!(status & GR_STATUS_DM)) {
2185 		dev_err(dev->dev, "Slave mode cores are not supported\n");
2186 		return -ENODEV;
2187 	}
2188 
2189 	/* --- Effects of the following calls might need explicit cleanup --- */
2190 
2191 	/* Create DMA pool for descriptors */
2192 	dev->desc_pool = dma_pool_create("desc_pool", dev->dev,
2193 					 sizeof(struct gr_dma_desc), 4, 0);
2194 	if (!dev->desc_pool) {
2195 		dev_err(dev->dev, "Could not allocate DMA pool");
2196 		return -ENOMEM;
2197 	}
2198 
2199 	spin_lock(&dev->lock);
2200 
2201 	/* Inside lock so that no gadget can use this udc until probe is done */
2202 	retval = usb_add_gadget_udc(dev->dev, &dev->gadget);
2203 	if (retval) {
2204 		dev_err(dev->dev, "Could not add gadget udc");
2205 		goto out;
2206 	}
2207 	dev->added = 1;
2208 
2209 	retval = gr_udc_init(dev);
2210 	if (retval)
2211 		goto out;
2212 
2213 	gr_dfs_create(dev);
2214 
2215 	/* Clear all interrupt enables that might be left on since last boot */
2216 	gr_disable_interrupts_and_pullup(dev);
2217 
2218 	retval = gr_request_irq(dev, dev->irq);
2219 	if (retval) {
2220 		dev_err(dev->dev, "Failed to request irq %d\n", dev->irq);
2221 		goto out;
2222 	}
2223 
2224 	if (dev->irqi) {
2225 		retval = gr_request_irq(dev, dev->irqi);
2226 		if (retval) {
2227 			dev_err(dev->dev, "Failed to request irqi %d\n",
2228 				dev->irqi);
2229 			goto out;
2230 		}
2231 		retval = gr_request_irq(dev, dev->irqo);
2232 		if (retval) {
2233 			dev_err(dev->dev, "Failed to request irqo %d\n",
2234 				dev->irqo);
2235 			goto out;
2236 		}
2237 	}
2238 
2239 	if (dev->irqi)
2240 		dev_info(dev->dev, "regs: %p, irqs %d, %d, %d\n", dev->regs,
2241 			 dev->irq, dev->irqi, dev->irqo);
2242 	else
2243 		dev_info(dev->dev, "regs: %p, irq %d\n", dev->regs, dev->irq);
2244 
2245 out:
2246 	spin_unlock(&dev->lock);
2247 
2248 	if (retval)
2249 		gr_remove(pdev);
2250 
2251 	return retval;
2252 }
2253 
2254 static const struct of_device_id gr_match[] = {
2255 	{.name = "GAISLER_USBDC"},
2256 	{.name = "01_021"},
2257 	{},
2258 };
2259 MODULE_DEVICE_TABLE(of, gr_match);
2260 
2261 static struct platform_driver gr_driver = {
2262 	.driver = {
2263 		.name = DRIVER_NAME,
2264 		.of_match_table = gr_match,
2265 	},
2266 	.probe = gr_probe,
2267 	.remove = gr_remove,
2268 };
2269 module_platform_driver(gr_driver);
2270 
2271 MODULE_AUTHOR("Aeroflex Gaisler AB.");
2272 MODULE_DESCRIPTION(DRIVER_DESC);
2273 MODULE_LICENSE("GPL");
2274