1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Handles the Intel 27x USB Device Controller (UDC)
4  *
5  * Inspired by original driver by Frank Becker, David Brownell, and others.
6  * Copyright (C) 2008 Robert Jarzmik
7  */
8 #include <linux/module.h>
9 #include <linux/kernel.h>
10 #include <linux/types.h>
11 #include <linux/errno.h>
12 #include <linux/err.h>
13 #include <linux/platform_device.h>
14 #include <linux/delay.h>
15 #include <linux/list.h>
16 #include <linux/interrupt.h>
17 #include <linux/proc_fs.h>
18 #include <linux/clk.h>
19 #include <linux/irq.h>
20 #include <linux/gpio.h>
21 #include <linux/gpio/consumer.h>
22 #include <linux/slab.h>
23 #include <linux/prefetch.h>
24 #include <linux/byteorder/generic.h>
25 #include <linux/platform_data/pxa2xx_udc.h>
26 #include <linux/of_device.h>
27 #include <linux/of_gpio.h>
28 
29 #include <linux/usb.h>
30 #include <linux/usb/ch9.h>
31 #include <linux/usb/gadget.h>
32 #include <linux/usb/phy.h>
33 
34 #include "pxa27x_udc.h"
35 
36 /*
37  * This driver handles the USB Device Controller (UDC) in Intel's PXA 27x
38  * series processors.
39  *
40  * Such controller drivers work with a gadget driver.  The gadget driver
41  * returns descriptors, implements configuration and data protocols used
42  * by the host to interact with this device, and allocates endpoints to
43  * the different protocol interfaces.  The controller driver virtualizes
44  * usb hardware so that the gadget drivers will be more portable.
45  *
46  * This UDC hardware wants to implement a bit too much USB protocol. The
47  * biggest issues are:  that the endpoints have to be set up before the
48  * controller can be enabled (minor, and not uncommon); and each endpoint
49  * can only have one configuration, interface and alternative interface
50  * number (major, and very unusual). Once set up, these cannot be changed
51  * without a controller reset.
52  *
53  * The workaround is to setup all combinations necessary for the gadgets which
54  * will work with this driver. This is done in pxa_udc structure, statically.
55  * See pxa_udc, udc_usb_ep versus pxa_ep, and matching function find_pxa_ep.
56  * (You could modify this if needed.  Some drivers have a "fifo_mode" module
57  * parameter to facilitate such changes.)
58  *
59  * The combinations have been tested with these gadgets :
60  *  - zero gadget
61  *  - file storage gadget
62  *  - ether gadget
63  *
64  * The driver doesn't use DMA, only IO access and IRQ callbacks. No use is
65  * made of UDC's double buffering either. USB "On-The-Go" is not implemented.
66  *
67  * All the requests are handled the same way :
68  *  - the drivers tries to handle the request directly to the IO
69  *  - if the IO fifo is not big enough, the remaining is send/received in
70  *    interrupt handling.
71  */
72 
73 #define	DRIVER_VERSION	"2008-04-18"
74 #define	DRIVER_DESC	"PXA 27x USB Device Controller driver"
75 
76 static const char driver_name[] = "pxa27x_udc";
77 static struct pxa_udc *the_controller;
78 
79 static void handle_ep(struct pxa_ep *ep);
80 
81 /*
82  * Debug filesystem
83  */
84 #ifdef CONFIG_USB_GADGET_DEBUG_FS
85 
86 #include <linux/debugfs.h>
87 #include <linux/uaccess.h>
88 #include <linux/seq_file.h>
89 
90 static int state_dbg_show(struct seq_file *s, void *p)
91 {
92 	struct pxa_udc *udc = s->private;
93 	u32 tmp;
94 
95 	if (!udc->driver)
96 		return -ENODEV;
97 
98 	/* basic device status */
99 	seq_printf(s, DRIVER_DESC "\n"
100 		   "%s version: %s\n"
101 		   "Gadget driver: %s\n",
102 		   driver_name, DRIVER_VERSION,
103 		   udc->driver ? udc->driver->driver.name : "(none)");
104 
105 	tmp = udc_readl(udc, UDCCR);
106 	seq_printf(s,
107 		   "udccr=0x%0x(%s%s%s%s%s%s%s%s%s%s), con=%d,inter=%d,altinter=%d\n",
108 		   tmp,
109 		   (tmp & UDCCR_OEN) ? " oen":"",
110 		   (tmp & UDCCR_AALTHNP) ? " aalthnp":"",
111 		   (tmp & UDCCR_AHNP) ? " rem" : "",
112 		   (tmp & UDCCR_BHNP) ? " rstir" : "",
113 		   (tmp & UDCCR_DWRE) ? " dwre" : "",
114 		   (tmp & UDCCR_SMAC) ? " smac" : "",
115 		   (tmp & UDCCR_EMCE) ? " emce" : "",
116 		   (tmp & UDCCR_UDR) ? " udr" : "",
117 		   (tmp & UDCCR_UDA) ? " uda" : "",
118 		   (tmp & UDCCR_UDE) ? " ude" : "",
119 		   (tmp & UDCCR_ACN) >> UDCCR_ACN_S,
120 		   (tmp & UDCCR_AIN) >> UDCCR_AIN_S,
121 		   (tmp & UDCCR_AAISN) >> UDCCR_AAISN_S);
122 	/* registers for device and ep0 */
123 	seq_printf(s, "udcicr0=0x%08x udcicr1=0x%08x\n",
124 		   udc_readl(udc, UDCICR0), udc_readl(udc, UDCICR1));
125 	seq_printf(s, "udcisr0=0x%08x udcisr1=0x%08x\n",
126 		   udc_readl(udc, UDCISR0), udc_readl(udc, UDCISR1));
127 	seq_printf(s, "udcfnr=%d\n", udc_readl(udc, UDCFNR));
128 	seq_printf(s, "irqs: reset=%lu, suspend=%lu, resume=%lu, reconfig=%lu\n",
129 		   udc->stats.irqs_reset, udc->stats.irqs_suspend,
130 		   udc->stats.irqs_resume, udc->stats.irqs_reconfig);
131 
132 	return 0;
133 }
134 
135 static int queues_dbg_show(struct seq_file *s, void *p)
136 {
137 	struct pxa_udc *udc = s->private;
138 	struct pxa_ep *ep;
139 	struct pxa27x_request *req;
140 	int i, maxpkt;
141 
142 	if (!udc->driver)
143 		return -ENODEV;
144 
145 	/* dump endpoint queues */
146 	for (i = 0; i < NR_PXA_ENDPOINTS; i++) {
147 		ep = &udc->pxa_ep[i];
148 		maxpkt = ep->fifo_size;
149 		seq_printf(s,  "%-12s max_pkt=%d %s\n",
150 			   EPNAME(ep), maxpkt, "pio");
151 
152 		if (list_empty(&ep->queue)) {
153 			seq_puts(s, "\t(nothing queued)\n");
154 			continue;
155 		}
156 
157 		list_for_each_entry(req, &ep->queue, queue) {
158 			seq_printf(s,  "\treq %p len %d/%d buf %p\n",
159 				   &req->req, req->req.actual,
160 				   req->req.length, req->req.buf);
161 		}
162 	}
163 
164 	return 0;
165 }
166 
167 static int eps_dbg_show(struct seq_file *s, void *p)
168 {
169 	struct pxa_udc *udc = s->private;
170 	struct pxa_ep *ep;
171 	int i;
172 	u32 tmp;
173 
174 	if (!udc->driver)
175 		return -ENODEV;
176 
177 	ep = &udc->pxa_ep[0];
178 	tmp = udc_ep_readl(ep, UDCCSR);
179 	seq_printf(s, "udccsr0=0x%03x(%s%s%s%s%s%s%s)\n",
180 		   tmp,
181 		   (tmp & UDCCSR0_SA) ? " sa" : "",
182 		   (tmp & UDCCSR0_RNE) ? " rne" : "",
183 		   (tmp & UDCCSR0_FST) ? " fst" : "",
184 		   (tmp & UDCCSR0_SST) ? " sst" : "",
185 		   (tmp & UDCCSR0_DME) ? " dme" : "",
186 		   (tmp & UDCCSR0_IPR) ? " ipr" : "",
187 		   (tmp & UDCCSR0_OPC) ? " opc" : "");
188 	for (i = 0; i < NR_PXA_ENDPOINTS; i++) {
189 		ep = &udc->pxa_ep[i];
190 		tmp = i? udc_ep_readl(ep, UDCCR) : udc_readl(udc, UDCCR);
191 		seq_printf(s, "%-12s: IN %lu(%lu reqs), OUT %lu(%lu reqs), irqs=%lu, udccr=0x%08x, udccsr=0x%03x, udcbcr=%d\n",
192 			   EPNAME(ep),
193 			   ep->stats.in_bytes, ep->stats.in_ops,
194 			   ep->stats.out_bytes, ep->stats.out_ops,
195 			   ep->stats.irqs,
196 			   tmp, udc_ep_readl(ep, UDCCSR),
197 			   udc_ep_readl(ep, UDCBCR));
198 	}
199 
200 	return 0;
201 }
202 
203 static int eps_dbg_open(struct inode *inode, struct file *file)
204 {
205 	return single_open(file, eps_dbg_show, inode->i_private);
206 }
207 
208 static int queues_dbg_open(struct inode *inode, struct file *file)
209 {
210 	return single_open(file, queues_dbg_show, inode->i_private);
211 }
212 
213 static int state_dbg_open(struct inode *inode, struct file *file)
214 {
215 	return single_open(file, state_dbg_show, inode->i_private);
216 }
217 
218 static const struct file_operations state_dbg_fops = {
219 	.owner		= THIS_MODULE,
220 	.open		= state_dbg_open,
221 	.llseek		= seq_lseek,
222 	.read		= seq_read,
223 	.release	= single_release,
224 };
225 
226 static const struct file_operations queues_dbg_fops = {
227 	.owner		= THIS_MODULE,
228 	.open		= queues_dbg_open,
229 	.llseek		= seq_lseek,
230 	.read		= seq_read,
231 	.release	= single_release,
232 };
233 
234 static const struct file_operations eps_dbg_fops = {
235 	.owner		= THIS_MODULE,
236 	.open		= eps_dbg_open,
237 	.llseek		= seq_lseek,
238 	.read		= seq_read,
239 	.release	= single_release,
240 };
241 
242 static void pxa_init_debugfs(struct pxa_udc *udc)
243 {
244 	struct dentry *root, *state, *queues, *eps;
245 
246 	root = debugfs_create_dir(udc->gadget.name, NULL);
247 	if (IS_ERR(root) || !root)
248 		goto err_root;
249 
250 	state = debugfs_create_file("udcstate", 0400, root, udc,
251 			&state_dbg_fops);
252 	if (!state)
253 		goto err_state;
254 	queues = debugfs_create_file("queues", 0400, root, udc,
255 			&queues_dbg_fops);
256 	if (!queues)
257 		goto err_queues;
258 	eps = debugfs_create_file("epstate", 0400, root, udc,
259 			&eps_dbg_fops);
260 	if (!eps)
261 		goto err_eps;
262 
263 	udc->debugfs_root = root;
264 	udc->debugfs_state = state;
265 	udc->debugfs_queues = queues;
266 	udc->debugfs_eps = eps;
267 	return;
268 err_eps:
269 	debugfs_remove(eps);
270 err_queues:
271 	debugfs_remove(queues);
272 err_state:
273 	debugfs_remove(root);
274 err_root:
275 	dev_err(udc->dev, "debugfs is not available\n");
276 }
277 
278 static void pxa_cleanup_debugfs(struct pxa_udc *udc)
279 {
280 	debugfs_remove(udc->debugfs_eps);
281 	debugfs_remove(udc->debugfs_queues);
282 	debugfs_remove(udc->debugfs_state);
283 	debugfs_remove(udc->debugfs_root);
284 	udc->debugfs_eps = NULL;
285 	udc->debugfs_queues = NULL;
286 	udc->debugfs_state = NULL;
287 	udc->debugfs_root = NULL;
288 }
289 
290 #else
291 static inline void pxa_init_debugfs(struct pxa_udc *udc)
292 {
293 }
294 
295 static inline void pxa_cleanup_debugfs(struct pxa_udc *udc)
296 {
297 }
298 #endif
299 
300 /**
301  * is_match_usb_pxa - check if usb_ep and pxa_ep match
302  * @udc_usb_ep: usb endpoint
303  * @ep: pxa endpoint
304  * @config: configuration required in pxa_ep
305  * @interface: interface required in pxa_ep
306  * @altsetting: altsetting required in pxa_ep
307  *
308  * Returns 1 if all criteria match between pxa and usb endpoint, 0 otherwise
309  */
310 static int is_match_usb_pxa(struct udc_usb_ep *udc_usb_ep, struct pxa_ep *ep,
311 		int config, int interface, int altsetting)
312 {
313 	if (usb_endpoint_num(&udc_usb_ep->desc) != ep->addr)
314 		return 0;
315 	if (usb_endpoint_dir_in(&udc_usb_ep->desc) != ep->dir_in)
316 		return 0;
317 	if (usb_endpoint_type(&udc_usb_ep->desc) != ep->type)
318 		return 0;
319 	if ((ep->config != config) || (ep->interface != interface)
320 			|| (ep->alternate != altsetting))
321 		return 0;
322 	return 1;
323 }
324 
325 /**
326  * find_pxa_ep - find pxa_ep structure matching udc_usb_ep
327  * @udc: pxa udc
328  * @udc_usb_ep: udc_usb_ep structure
329  *
330  * Match udc_usb_ep and all pxa_ep available, to see if one matches.
331  * This is necessary because of the strong pxa hardware restriction requiring
332  * that once pxa endpoints are initialized, their configuration is freezed, and
333  * no change can be made to their address, direction, or in which configuration,
334  * interface or altsetting they are active ... which differs from more usual
335  * models which have endpoints be roughly just addressable fifos, and leave
336  * configuration events up to gadget drivers (like all control messages).
337  *
338  * Note that there is still a blurred point here :
339  *   - we rely on UDCCR register "active interface" and "active altsetting".
340  *     This is a nonsense in regard of USB spec, where multiple interfaces are
341  *     active at the same time.
342  *   - if we knew for sure that the pxa can handle multiple interface at the
343  *     same time, assuming Intel's Developer Guide is wrong, this function
344  *     should be reviewed, and a cache of couples (iface, altsetting) should
345  *     be kept in the pxa_udc structure. In this case this function would match
346  *     against the cache of couples instead of the "last altsetting" set up.
347  *
348  * Returns the matched pxa_ep structure or NULL if none found
349  */
350 static struct pxa_ep *find_pxa_ep(struct pxa_udc *udc,
351 		struct udc_usb_ep *udc_usb_ep)
352 {
353 	int i;
354 	struct pxa_ep *ep;
355 	int cfg = udc->config;
356 	int iface = udc->last_interface;
357 	int alt = udc->last_alternate;
358 
359 	if (udc_usb_ep == &udc->udc_usb_ep[0])
360 		return &udc->pxa_ep[0];
361 
362 	for (i = 1; i < NR_PXA_ENDPOINTS; i++) {
363 		ep = &udc->pxa_ep[i];
364 		if (is_match_usb_pxa(udc_usb_ep, ep, cfg, iface, alt))
365 			return ep;
366 	}
367 	return NULL;
368 }
369 
370 /**
371  * update_pxa_ep_matches - update pxa_ep cached values in all udc_usb_ep
372  * @udc: pxa udc
373  *
374  * Context: in_interrupt()
375  *
376  * Updates all pxa_ep fields in udc_usb_ep structures, if this field was
377  * previously set up (and is not NULL). The update is necessary is a
378  * configuration change or altsetting change was issued by the USB host.
379  */
380 static void update_pxa_ep_matches(struct pxa_udc *udc)
381 {
382 	int i;
383 	struct udc_usb_ep *udc_usb_ep;
384 
385 	for (i = 1; i < NR_USB_ENDPOINTS; i++) {
386 		udc_usb_ep = &udc->udc_usb_ep[i];
387 		if (udc_usb_ep->pxa_ep)
388 			udc_usb_ep->pxa_ep = find_pxa_ep(udc, udc_usb_ep);
389 	}
390 }
391 
392 /**
393  * pio_irq_enable - Enables irq generation for one endpoint
394  * @ep: udc endpoint
395  */
396 static void pio_irq_enable(struct pxa_ep *ep)
397 {
398 	struct pxa_udc *udc = ep->dev;
399 	int index = EPIDX(ep);
400 	u32 udcicr0 = udc_readl(udc, UDCICR0);
401 	u32 udcicr1 = udc_readl(udc, UDCICR1);
402 
403 	if (index < 16)
404 		udc_writel(udc, UDCICR0, udcicr0 | (3 << (index * 2)));
405 	else
406 		udc_writel(udc, UDCICR1, udcicr1 | (3 << ((index - 16) * 2)));
407 }
408 
409 /**
410  * pio_irq_disable - Disables irq generation for one endpoint
411  * @ep: udc endpoint
412  */
413 static void pio_irq_disable(struct pxa_ep *ep)
414 {
415 	struct pxa_udc *udc = ep->dev;
416 	int index = EPIDX(ep);
417 	u32 udcicr0 = udc_readl(udc, UDCICR0);
418 	u32 udcicr1 = udc_readl(udc, UDCICR1);
419 
420 	if (index < 16)
421 		udc_writel(udc, UDCICR0, udcicr0 & ~(3 << (index * 2)));
422 	else
423 		udc_writel(udc, UDCICR1, udcicr1 & ~(3 << ((index - 16) * 2)));
424 }
425 
426 /**
427  * udc_set_mask_UDCCR - set bits in UDCCR
428  * @udc: udc device
429  * @mask: bits to set in UDCCR
430  *
431  * Sets bits in UDCCR, leaving DME and FST bits as they were.
432  */
433 static inline void udc_set_mask_UDCCR(struct pxa_udc *udc, int mask)
434 {
435 	u32 udccr = udc_readl(udc, UDCCR);
436 	udc_writel(udc, UDCCR,
437 			(udccr & UDCCR_MASK_BITS) | (mask & UDCCR_MASK_BITS));
438 }
439 
440 /**
441  * udc_clear_mask_UDCCR - clears bits in UDCCR
442  * @udc: udc device
443  * @mask: bit to clear in UDCCR
444  *
445  * Clears bits in UDCCR, leaving DME and FST bits as they were.
446  */
447 static inline void udc_clear_mask_UDCCR(struct pxa_udc *udc, int mask)
448 {
449 	u32 udccr = udc_readl(udc, UDCCR);
450 	udc_writel(udc, UDCCR,
451 			(udccr & UDCCR_MASK_BITS) & ~(mask & UDCCR_MASK_BITS));
452 }
453 
454 /**
455  * ep_write_UDCCSR - set bits in UDCCSR
456  * @udc: udc device
457  * @mask: bits to set in UDCCR
458  *
459  * Sets bits in UDCCSR (UDCCSR0 and UDCCSR*).
460  *
461  * A specific case is applied to ep0 : the ACM bit is always set to 1, for
462  * SET_INTERFACE and SET_CONFIGURATION.
463  */
464 static inline void ep_write_UDCCSR(struct pxa_ep *ep, int mask)
465 {
466 	if (is_ep0(ep))
467 		mask |= UDCCSR0_ACM;
468 	udc_ep_writel(ep, UDCCSR, mask);
469 }
470 
471 /**
472  * ep_count_bytes_remain - get how many bytes in udc endpoint
473  * @ep: udc endpoint
474  *
475  * Returns number of bytes in OUT fifos. Broken for IN fifos (-EOPNOTSUPP)
476  */
477 static int ep_count_bytes_remain(struct pxa_ep *ep)
478 {
479 	if (ep->dir_in)
480 		return -EOPNOTSUPP;
481 	return udc_ep_readl(ep, UDCBCR) & 0x3ff;
482 }
483 
484 /**
485  * ep_is_empty - checks if ep has byte ready for reading
486  * @ep: udc endpoint
487  *
488  * If endpoint is the control endpoint, checks if there are bytes in the
489  * control endpoint fifo. If endpoint is a data endpoint, checks if bytes
490  * are ready for reading on OUT endpoint.
491  *
492  * Returns 0 if ep not empty, 1 if ep empty, -EOPNOTSUPP if IN endpoint
493  */
494 static int ep_is_empty(struct pxa_ep *ep)
495 {
496 	int ret;
497 
498 	if (!is_ep0(ep) && ep->dir_in)
499 		return -EOPNOTSUPP;
500 	if (is_ep0(ep))
501 		ret = !(udc_ep_readl(ep, UDCCSR) & UDCCSR0_RNE);
502 	else
503 		ret = !(udc_ep_readl(ep, UDCCSR) & UDCCSR_BNE);
504 	return ret;
505 }
506 
507 /**
508  * ep_is_full - checks if ep has place to write bytes
509  * @ep: udc endpoint
510  *
511  * If endpoint is not the control endpoint and is an IN endpoint, checks if
512  * there is place to write bytes into the endpoint.
513  *
514  * Returns 0 if ep not full, 1 if ep full, -EOPNOTSUPP if OUT endpoint
515  */
516 static int ep_is_full(struct pxa_ep *ep)
517 {
518 	if (is_ep0(ep))
519 		return (udc_ep_readl(ep, UDCCSR) & UDCCSR0_IPR);
520 	if (!ep->dir_in)
521 		return -EOPNOTSUPP;
522 	return (!(udc_ep_readl(ep, UDCCSR) & UDCCSR_BNF));
523 }
524 
525 /**
526  * epout_has_pkt - checks if OUT endpoint fifo has a packet available
527  * @ep: pxa endpoint
528  *
529  * Returns 1 if a complete packet is available, 0 if not, -EOPNOTSUPP for IN ep.
530  */
531 static int epout_has_pkt(struct pxa_ep *ep)
532 {
533 	if (!is_ep0(ep) && ep->dir_in)
534 		return -EOPNOTSUPP;
535 	if (is_ep0(ep))
536 		return (udc_ep_readl(ep, UDCCSR) & UDCCSR0_OPC);
537 	return (udc_ep_readl(ep, UDCCSR) & UDCCSR_PC);
538 }
539 
540 /**
541  * set_ep0state - Set ep0 automata state
542  * @dev: udc device
543  * @state: state
544  */
545 static void set_ep0state(struct pxa_udc *udc, int state)
546 {
547 	struct pxa_ep *ep = &udc->pxa_ep[0];
548 	char *old_stname = EP0_STNAME(udc);
549 
550 	udc->ep0state = state;
551 	ep_dbg(ep, "state=%s->%s, udccsr0=0x%03x, udcbcr=%d\n", old_stname,
552 		EP0_STNAME(udc), udc_ep_readl(ep, UDCCSR),
553 		udc_ep_readl(ep, UDCBCR));
554 }
555 
556 /**
557  * ep0_idle - Put control endpoint into idle state
558  * @dev: udc device
559  */
560 static void ep0_idle(struct pxa_udc *dev)
561 {
562 	set_ep0state(dev, WAIT_FOR_SETUP);
563 }
564 
565 /**
566  * inc_ep_stats_reqs - Update ep stats counts
567  * @ep: physical endpoint
568  * @req: usb request
569  * @is_in: ep direction (USB_DIR_IN or 0)
570  *
571  */
572 static void inc_ep_stats_reqs(struct pxa_ep *ep, int is_in)
573 {
574 	if (is_in)
575 		ep->stats.in_ops++;
576 	else
577 		ep->stats.out_ops++;
578 }
579 
580 /**
581  * inc_ep_stats_bytes - Update ep stats counts
582  * @ep: physical endpoint
583  * @count: bytes transferred on endpoint
584  * @is_in: ep direction (USB_DIR_IN or 0)
585  */
586 static void inc_ep_stats_bytes(struct pxa_ep *ep, int count, int is_in)
587 {
588 	if (is_in)
589 		ep->stats.in_bytes += count;
590 	else
591 		ep->stats.out_bytes += count;
592 }
593 
594 /**
595  * pxa_ep_setup - Sets up an usb physical endpoint
596  * @ep: pxa27x physical endpoint
597  *
598  * Find the physical pxa27x ep, and setup its UDCCR
599  */
600 static void pxa_ep_setup(struct pxa_ep *ep)
601 {
602 	u32 new_udccr;
603 
604 	new_udccr = ((ep->config << UDCCONR_CN_S) & UDCCONR_CN)
605 		| ((ep->interface << UDCCONR_IN_S) & UDCCONR_IN)
606 		| ((ep->alternate << UDCCONR_AISN_S) & UDCCONR_AISN)
607 		| ((EPADDR(ep) << UDCCONR_EN_S) & UDCCONR_EN)
608 		| ((EPXFERTYPE(ep) << UDCCONR_ET_S) & UDCCONR_ET)
609 		| ((ep->dir_in) ? UDCCONR_ED : 0)
610 		| ((ep->fifo_size << UDCCONR_MPS_S) & UDCCONR_MPS)
611 		| UDCCONR_EE;
612 
613 	udc_ep_writel(ep, UDCCR, new_udccr);
614 }
615 
616 /**
617  * pxa_eps_setup - Sets up all usb physical endpoints
618  * @dev: udc device
619  *
620  * Setup all pxa physical endpoints, except ep0
621  */
622 static void pxa_eps_setup(struct pxa_udc *dev)
623 {
624 	unsigned int i;
625 
626 	dev_dbg(dev->dev, "%s: dev=%p\n", __func__, dev);
627 
628 	for (i = 1; i < NR_PXA_ENDPOINTS; i++)
629 		pxa_ep_setup(&dev->pxa_ep[i]);
630 }
631 
632 /**
633  * pxa_ep_alloc_request - Allocate usb request
634  * @_ep: usb endpoint
635  * @gfp_flags:
636  *
637  * For the pxa27x, these can just wrap kmalloc/kfree.  gadget drivers
638  * must still pass correctly initialized endpoints, since other controller
639  * drivers may care about how it's currently set up (dma issues etc).
640   */
641 static struct usb_request *
642 pxa_ep_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
643 {
644 	struct pxa27x_request *req;
645 
646 	req = kzalloc(sizeof *req, gfp_flags);
647 	if (!req)
648 		return NULL;
649 
650 	INIT_LIST_HEAD(&req->queue);
651 	req->in_use = 0;
652 	req->udc_usb_ep = container_of(_ep, struct udc_usb_ep, usb_ep);
653 
654 	return &req->req;
655 }
656 
657 /**
658  * pxa_ep_free_request - Free usb request
659  * @_ep: usb endpoint
660  * @_req: usb request
661  *
662  * Wrapper around kfree to free _req
663  */
664 static void pxa_ep_free_request(struct usb_ep *_ep, struct usb_request *_req)
665 {
666 	struct pxa27x_request *req;
667 
668 	req = container_of(_req, struct pxa27x_request, req);
669 	WARN_ON(!list_empty(&req->queue));
670 	kfree(req);
671 }
672 
673 /**
674  * ep_add_request - add a request to the endpoint's queue
675  * @ep: usb endpoint
676  * @req: usb request
677  *
678  * Context: ep->lock held
679  *
680  * Queues the request in the endpoint's queue, and enables the interrupts
681  * on the endpoint.
682  */
683 static void ep_add_request(struct pxa_ep *ep, struct pxa27x_request *req)
684 {
685 	if (unlikely(!req))
686 		return;
687 	ep_vdbg(ep, "req:%p, lg=%d, udccsr=0x%03x\n", req,
688 		req->req.length, udc_ep_readl(ep, UDCCSR));
689 
690 	req->in_use = 1;
691 	list_add_tail(&req->queue, &ep->queue);
692 	pio_irq_enable(ep);
693 }
694 
695 /**
696  * ep_del_request - removes a request from the endpoint's queue
697  * @ep: usb endpoint
698  * @req: usb request
699  *
700  * Context: ep->lock held
701  *
702  * Unqueue the request from the endpoint's queue. If there are no more requests
703  * on the endpoint, and if it's not the control endpoint, interrupts are
704  * disabled on the endpoint.
705  */
706 static void ep_del_request(struct pxa_ep *ep, struct pxa27x_request *req)
707 {
708 	if (unlikely(!req))
709 		return;
710 	ep_vdbg(ep, "req:%p, lg=%d, udccsr=0x%03x\n", req,
711 		req->req.length, udc_ep_readl(ep, UDCCSR));
712 
713 	list_del_init(&req->queue);
714 	req->in_use = 0;
715 	if (!is_ep0(ep) && list_empty(&ep->queue))
716 		pio_irq_disable(ep);
717 }
718 
719 /**
720  * req_done - Complete an usb request
721  * @ep: pxa physical endpoint
722  * @req: pxa request
723  * @status: usb request status sent to gadget API
724  * @pflags: flags of previous spinlock_irq_save() or NULL if no lock held
725  *
726  * Context: ep->lock held if flags not NULL, else ep->lock released
727  *
728  * Retire a pxa27x usb request. Endpoint must be locked.
729  */
730 static void req_done(struct pxa_ep *ep, struct pxa27x_request *req, int status,
731 	unsigned long *pflags)
732 {
733 	unsigned long	flags;
734 
735 	ep_del_request(ep, req);
736 	if (likely(req->req.status == -EINPROGRESS))
737 		req->req.status = status;
738 	else
739 		status = req->req.status;
740 
741 	if (status && status != -ESHUTDOWN)
742 		ep_dbg(ep, "complete req %p stat %d len %u/%u\n",
743 			&req->req, status,
744 			req->req.actual, req->req.length);
745 
746 	if (pflags)
747 		spin_unlock_irqrestore(&ep->lock, *pflags);
748 	local_irq_save(flags);
749 	usb_gadget_giveback_request(&req->udc_usb_ep->usb_ep, &req->req);
750 	local_irq_restore(flags);
751 	if (pflags)
752 		spin_lock_irqsave(&ep->lock, *pflags);
753 }
754 
755 /**
756  * ep_end_out_req - Ends endpoint OUT request
757  * @ep: physical endpoint
758  * @req: pxa request
759  * @pflags: flags of previous spinlock_irq_save() or NULL if no lock held
760  *
761  * Context: ep->lock held or released (see req_done())
762  *
763  * Ends endpoint OUT request (completes usb request).
764  */
765 static void ep_end_out_req(struct pxa_ep *ep, struct pxa27x_request *req,
766 	unsigned long *pflags)
767 {
768 	inc_ep_stats_reqs(ep, !USB_DIR_IN);
769 	req_done(ep, req, 0, pflags);
770 }
771 
772 /**
773  * ep0_end_out_req - Ends control endpoint OUT request (ends data stage)
774  * @ep: physical endpoint
775  * @req: pxa request
776  * @pflags: flags of previous spinlock_irq_save() or NULL if no lock held
777  *
778  * Context: ep->lock held or released (see req_done())
779  *
780  * Ends control endpoint OUT request (completes usb request), and puts
781  * control endpoint into idle state
782  */
783 static void ep0_end_out_req(struct pxa_ep *ep, struct pxa27x_request *req,
784 	unsigned long *pflags)
785 {
786 	set_ep0state(ep->dev, OUT_STATUS_STAGE);
787 	ep_end_out_req(ep, req, pflags);
788 	ep0_idle(ep->dev);
789 }
790 
791 /**
792  * ep_end_in_req - Ends endpoint IN request
793  * @ep: physical endpoint
794  * @req: pxa request
795  * @pflags: flags of previous spinlock_irq_save() or NULL if no lock held
796  *
797  * Context: ep->lock held or released (see req_done())
798  *
799  * Ends endpoint IN request (completes usb request).
800  */
801 static void ep_end_in_req(struct pxa_ep *ep, struct pxa27x_request *req,
802 	unsigned long *pflags)
803 {
804 	inc_ep_stats_reqs(ep, USB_DIR_IN);
805 	req_done(ep, req, 0, pflags);
806 }
807 
808 /**
809  * ep0_end_in_req - Ends control endpoint IN request (ends data stage)
810  * @ep: physical endpoint
811  * @req: pxa request
812  * @pflags: flags of previous spinlock_irq_save() or NULL if no lock held
813  *
814  * Context: ep->lock held or released (see req_done())
815  *
816  * Ends control endpoint IN request (completes usb request), and puts
817  * control endpoint into status state
818  */
819 static void ep0_end_in_req(struct pxa_ep *ep, struct pxa27x_request *req,
820 	unsigned long *pflags)
821 {
822 	set_ep0state(ep->dev, IN_STATUS_STAGE);
823 	ep_end_in_req(ep, req, pflags);
824 }
825 
826 /**
827  * nuke - Dequeue all requests
828  * @ep: pxa endpoint
829  * @status: usb request status
830  *
831  * Context: ep->lock released
832  *
833  * Dequeues all requests on an endpoint. As a side effect, interrupts will be
834  * disabled on that endpoint (because no more requests).
835  */
836 static void nuke(struct pxa_ep *ep, int status)
837 {
838 	struct pxa27x_request	*req;
839 	unsigned long		flags;
840 
841 	spin_lock_irqsave(&ep->lock, flags);
842 	while (!list_empty(&ep->queue)) {
843 		req = list_entry(ep->queue.next, struct pxa27x_request, queue);
844 		req_done(ep, req, status, &flags);
845 	}
846 	spin_unlock_irqrestore(&ep->lock, flags);
847 }
848 
849 /**
850  * read_packet - transfer 1 packet from an OUT endpoint into request
851  * @ep: pxa physical endpoint
852  * @req: usb request
853  *
854  * Takes bytes from OUT endpoint and transfers them info the usb request.
855  * If there is less space in request than bytes received in OUT endpoint,
856  * bytes are left in the OUT endpoint.
857  *
858  * Returns how many bytes were actually transferred
859  */
860 static int read_packet(struct pxa_ep *ep, struct pxa27x_request *req)
861 {
862 	u32 *buf;
863 	int bytes_ep, bufferspace, count, i;
864 
865 	bytes_ep = ep_count_bytes_remain(ep);
866 	bufferspace = req->req.length - req->req.actual;
867 
868 	buf = (u32 *)(req->req.buf + req->req.actual);
869 	prefetchw(buf);
870 
871 	if (likely(!ep_is_empty(ep)))
872 		count = min(bytes_ep, bufferspace);
873 	else /* zlp */
874 		count = 0;
875 
876 	for (i = count; i > 0; i -= 4)
877 		*buf++ = udc_ep_readl(ep, UDCDR);
878 	req->req.actual += count;
879 
880 	ep_write_UDCCSR(ep, UDCCSR_PC);
881 
882 	return count;
883 }
884 
885 /**
886  * write_packet - transfer 1 packet from request into an IN endpoint
887  * @ep: pxa physical endpoint
888  * @req: usb request
889  * @max: max bytes that fit into endpoint
890  *
891  * Takes bytes from usb request, and transfers them into the physical
892  * endpoint. If there are no bytes to transfer, doesn't write anything
893  * to physical endpoint.
894  *
895  * Returns how many bytes were actually transferred.
896  */
897 static int write_packet(struct pxa_ep *ep, struct pxa27x_request *req,
898 			unsigned int max)
899 {
900 	int length, count, remain, i;
901 	u32 *buf;
902 	u8 *buf_8;
903 
904 	buf = (u32 *)(req->req.buf + req->req.actual);
905 	prefetch(buf);
906 
907 	length = min(req->req.length - req->req.actual, max);
908 	req->req.actual += length;
909 
910 	remain = length & 0x3;
911 	count = length & ~(0x3);
912 	for (i = count; i > 0 ; i -= 4)
913 		udc_ep_writel(ep, UDCDR, *buf++);
914 
915 	buf_8 = (u8 *)buf;
916 	for (i = remain; i > 0; i--)
917 		udc_ep_writeb(ep, UDCDR, *buf_8++);
918 
919 	ep_vdbg(ep, "length=%d+%d, udccsr=0x%03x\n", count, remain,
920 		udc_ep_readl(ep, UDCCSR));
921 
922 	return length;
923 }
924 
925 /**
926  * read_fifo - Transfer packets from OUT endpoint into usb request
927  * @ep: pxa physical endpoint
928  * @req: usb request
929  *
930  * Context: callable when in_interrupt()
931  *
932  * Unload as many packets as possible from the fifo we use for usb OUT
933  * transfers and put them into the request. Caller should have made sure
934  * there's at least one packet ready.
935  * Doesn't complete the request, that's the caller's job
936  *
937  * Returns 1 if the request completed, 0 otherwise
938  */
939 static int read_fifo(struct pxa_ep *ep, struct pxa27x_request *req)
940 {
941 	int count, is_short, completed = 0;
942 
943 	while (epout_has_pkt(ep)) {
944 		count = read_packet(ep, req);
945 		inc_ep_stats_bytes(ep, count, !USB_DIR_IN);
946 
947 		is_short = (count < ep->fifo_size);
948 		ep_dbg(ep, "read udccsr:%03x, count:%d bytes%s req %p %d/%d\n",
949 			udc_ep_readl(ep, UDCCSR), count, is_short ? "/S" : "",
950 			&req->req, req->req.actual, req->req.length);
951 
952 		/* completion */
953 		if (is_short || req->req.actual == req->req.length) {
954 			completed = 1;
955 			break;
956 		}
957 		/* finished that packet.  the next one may be waiting... */
958 	}
959 	return completed;
960 }
961 
962 /**
963  * write_fifo - transfer packets from usb request into an IN endpoint
964  * @ep: pxa physical endpoint
965  * @req: pxa usb request
966  *
967  * Write to an IN endpoint fifo, as many packets as possible.
968  * irqs will use this to write the rest later.
969  * caller guarantees at least one packet buffer is ready (or a zlp).
970  * Doesn't complete the request, that's the caller's job
971  *
972  * Returns 1 if request fully transferred, 0 if partial transfer
973  */
974 static int write_fifo(struct pxa_ep *ep, struct pxa27x_request *req)
975 {
976 	unsigned max;
977 	int count, is_short, is_last = 0, completed = 0, totcount = 0;
978 	u32 udccsr;
979 
980 	max = ep->fifo_size;
981 	do {
982 		is_short = 0;
983 
984 		udccsr = udc_ep_readl(ep, UDCCSR);
985 		if (udccsr & UDCCSR_PC) {
986 			ep_vdbg(ep, "Clearing Transmit Complete, udccsr=%x\n",
987 				udccsr);
988 			ep_write_UDCCSR(ep, UDCCSR_PC);
989 		}
990 		if (udccsr & UDCCSR_TRN) {
991 			ep_vdbg(ep, "Clearing Underrun on, udccsr=%x\n",
992 				udccsr);
993 			ep_write_UDCCSR(ep, UDCCSR_TRN);
994 		}
995 
996 		count = write_packet(ep, req, max);
997 		inc_ep_stats_bytes(ep, count, USB_DIR_IN);
998 		totcount += count;
999 
1000 		/* last packet is usually short (or a zlp) */
1001 		if (unlikely(count < max)) {
1002 			is_last = 1;
1003 			is_short = 1;
1004 		} else {
1005 			if (likely(req->req.length > req->req.actual)
1006 					|| req->req.zero)
1007 				is_last = 0;
1008 			else
1009 				is_last = 1;
1010 			/* interrupt/iso maxpacket may not fill the fifo */
1011 			is_short = unlikely(max < ep->fifo_size);
1012 		}
1013 
1014 		if (is_short)
1015 			ep_write_UDCCSR(ep, UDCCSR_SP);
1016 
1017 		/* requests complete when all IN data is in the FIFO */
1018 		if (is_last) {
1019 			completed = 1;
1020 			break;
1021 		}
1022 	} while (!ep_is_full(ep));
1023 
1024 	ep_dbg(ep, "wrote count:%d bytes%s%s, left:%d req=%p\n",
1025 			totcount, is_last ? "/L" : "", is_short ? "/S" : "",
1026 			req->req.length - req->req.actual, &req->req);
1027 
1028 	return completed;
1029 }
1030 
1031 /**
1032  * read_ep0_fifo - Transfer packets from control endpoint into usb request
1033  * @ep: control endpoint
1034  * @req: pxa usb request
1035  *
1036  * Special ep0 version of the above read_fifo. Reads as many bytes from control
1037  * endpoint as can be read, and stores them into usb request (limited by request
1038  * maximum length).
1039  *
1040  * Returns 0 if usb request only partially filled, 1 if fully filled
1041  */
1042 static int read_ep0_fifo(struct pxa_ep *ep, struct pxa27x_request *req)
1043 {
1044 	int count, is_short, completed = 0;
1045 
1046 	while (epout_has_pkt(ep)) {
1047 		count = read_packet(ep, req);
1048 		ep_write_UDCCSR(ep, UDCCSR0_OPC);
1049 		inc_ep_stats_bytes(ep, count, !USB_DIR_IN);
1050 
1051 		is_short = (count < ep->fifo_size);
1052 		ep_dbg(ep, "read udccsr:%03x, count:%d bytes%s req %p %d/%d\n",
1053 			udc_ep_readl(ep, UDCCSR), count, is_short ? "/S" : "",
1054 			&req->req, req->req.actual, req->req.length);
1055 
1056 		if (is_short || req->req.actual >= req->req.length) {
1057 			completed = 1;
1058 			break;
1059 		}
1060 	}
1061 
1062 	return completed;
1063 }
1064 
1065 /**
1066  * write_ep0_fifo - Send a request to control endpoint (ep0 in)
1067  * @ep: control endpoint
1068  * @req: request
1069  *
1070  * Context: callable when in_interrupt()
1071  *
1072  * Sends a request (or a part of the request) to the control endpoint (ep0 in).
1073  * If the request doesn't fit, the remaining part will be sent from irq.
1074  * The request is considered fully written only if either :
1075  *   - last write transferred all remaining bytes, but fifo was not fully filled
1076  *   - last write was a 0 length write
1077  *
1078  * Returns 1 if request fully written, 0 if request only partially sent
1079  */
1080 static int write_ep0_fifo(struct pxa_ep *ep, struct pxa27x_request *req)
1081 {
1082 	unsigned	count;
1083 	int		is_last, is_short;
1084 
1085 	count = write_packet(ep, req, EP0_FIFO_SIZE);
1086 	inc_ep_stats_bytes(ep, count, USB_DIR_IN);
1087 
1088 	is_short = (count < EP0_FIFO_SIZE);
1089 	is_last = ((count == 0) || (count < EP0_FIFO_SIZE));
1090 
1091 	/* Sends either a short packet or a 0 length packet */
1092 	if (unlikely(is_short))
1093 		ep_write_UDCCSR(ep, UDCCSR0_IPR);
1094 
1095 	ep_dbg(ep, "in %d bytes%s%s, %d left, req=%p, udccsr0=0x%03x\n",
1096 		count, is_short ? "/S" : "", is_last ? "/L" : "",
1097 		req->req.length - req->req.actual,
1098 		&req->req, udc_ep_readl(ep, UDCCSR));
1099 
1100 	return is_last;
1101 }
1102 
1103 /**
1104  * pxa_ep_queue - Queue a request into an IN endpoint
1105  * @_ep: usb endpoint
1106  * @_req: usb request
1107  * @gfp_flags: flags
1108  *
1109  * Context: normally called when !in_interrupt, but callable when in_interrupt()
1110  * in the special case of ep0 setup :
1111  *   (irq->handle_ep0_ctrl_req->gadget_setup->pxa_ep_queue)
1112  *
1113  * Returns 0 if succedeed, error otherwise
1114  */
1115 static int pxa_ep_queue(struct usb_ep *_ep, struct usb_request *_req,
1116 			gfp_t gfp_flags)
1117 {
1118 	struct udc_usb_ep	*udc_usb_ep;
1119 	struct pxa_ep		*ep;
1120 	struct pxa27x_request	*req;
1121 	struct pxa_udc		*dev;
1122 	unsigned long		flags;
1123 	int			rc = 0;
1124 	int			is_first_req;
1125 	unsigned		length;
1126 	int			recursion_detected;
1127 
1128 	req = container_of(_req, struct pxa27x_request, req);
1129 	udc_usb_ep = container_of(_ep, struct udc_usb_ep, usb_ep);
1130 
1131 	if (unlikely(!_req || !_req->complete || !_req->buf))
1132 		return -EINVAL;
1133 
1134 	if (unlikely(!_ep))
1135 		return -EINVAL;
1136 
1137 	dev = udc_usb_ep->dev;
1138 	ep = udc_usb_ep->pxa_ep;
1139 	if (unlikely(!ep))
1140 		return -EINVAL;
1141 
1142 	dev = ep->dev;
1143 	if (unlikely(!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)) {
1144 		ep_dbg(ep, "bogus device state\n");
1145 		return -ESHUTDOWN;
1146 	}
1147 
1148 	/* iso is always one packet per request, that's the only way
1149 	 * we can report per-packet status.  that also helps with dma.
1150 	 */
1151 	if (unlikely(EPXFERTYPE_is_ISO(ep)
1152 			&& req->req.length > ep->fifo_size))
1153 		return -EMSGSIZE;
1154 
1155 	spin_lock_irqsave(&ep->lock, flags);
1156 	recursion_detected = ep->in_handle_ep;
1157 
1158 	is_first_req = list_empty(&ep->queue);
1159 	ep_dbg(ep, "queue req %p(first=%s), len %d buf %p\n",
1160 			_req, is_first_req ? "yes" : "no",
1161 			_req->length, _req->buf);
1162 
1163 	if (!ep->enabled) {
1164 		_req->status = -ESHUTDOWN;
1165 		rc = -ESHUTDOWN;
1166 		goto out_locked;
1167 	}
1168 
1169 	if (req->in_use) {
1170 		ep_err(ep, "refusing to queue req %p (already queued)\n", req);
1171 		goto out_locked;
1172 	}
1173 
1174 	length = _req->length;
1175 	_req->status = -EINPROGRESS;
1176 	_req->actual = 0;
1177 
1178 	ep_add_request(ep, req);
1179 	spin_unlock_irqrestore(&ep->lock, flags);
1180 
1181 	if (is_ep0(ep)) {
1182 		switch (dev->ep0state) {
1183 		case WAIT_ACK_SET_CONF_INTERF:
1184 			if (length == 0) {
1185 				ep_end_in_req(ep, req, NULL);
1186 			} else {
1187 				ep_err(ep, "got a request of %d bytes while"
1188 					"in state WAIT_ACK_SET_CONF_INTERF\n",
1189 					length);
1190 				ep_del_request(ep, req);
1191 				rc = -EL2HLT;
1192 			}
1193 			ep0_idle(ep->dev);
1194 			break;
1195 		case IN_DATA_STAGE:
1196 			if (!ep_is_full(ep))
1197 				if (write_ep0_fifo(ep, req))
1198 					ep0_end_in_req(ep, req, NULL);
1199 			break;
1200 		case OUT_DATA_STAGE:
1201 			if ((length == 0) || !epout_has_pkt(ep))
1202 				if (read_ep0_fifo(ep, req))
1203 					ep0_end_out_req(ep, req, NULL);
1204 			break;
1205 		default:
1206 			ep_err(ep, "odd state %s to send me a request\n",
1207 				EP0_STNAME(ep->dev));
1208 			ep_del_request(ep, req);
1209 			rc = -EL2HLT;
1210 			break;
1211 		}
1212 	} else {
1213 		if (!recursion_detected)
1214 			handle_ep(ep);
1215 	}
1216 
1217 out:
1218 	return rc;
1219 out_locked:
1220 	spin_unlock_irqrestore(&ep->lock, flags);
1221 	goto out;
1222 }
1223 
1224 /**
1225  * pxa_ep_dequeue - Dequeue one request
1226  * @_ep: usb endpoint
1227  * @_req: usb request
1228  *
1229  * Return 0 if no error, -EINVAL or -ECONNRESET otherwise
1230  */
1231 static int pxa_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1232 {
1233 	struct pxa_ep		*ep;
1234 	struct udc_usb_ep	*udc_usb_ep;
1235 	struct pxa27x_request	*req;
1236 	unsigned long		flags;
1237 	int			rc = -EINVAL;
1238 
1239 	if (!_ep)
1240 		return rc;
1241 	udc_usb_ep = container_of(_ep, struct udc_usb_ep, usb_ep);
1242 	ep = udc_usb_ep->pxa_ep;
1243 	if (!ep || is_ep0(ep))
1244 		return rc;
1245 
1246 	spin_lock_irqsave(&ep->lock, flags);
1247 
1248 	/* make sure it's actually queued on this endpoint */
1249 	list_for_each_entry(req, &ep->queue, queue) {
1250 		if (&req->req == _req) {
1251 			rc = 0;
1252 			break;
1253 		}
1254 	}
1255 
1256 	spin_unlock_irqrestore(&ep->lock, flags);
1257 	if (!rc)
1258 		req_done(ep, req, -ECONNRESET, NULL);
1259 	return rc;
1260 }
1261 
1262 /**
1263  * pxa_ep_set_halt - Halts operations on one endpoint
1264  * @_ep: usb endpoint
1265  * @value:
1266  *
1267  * Returns 0 if no error, -EINVAL, -EROFS, -EAGAIN otherwise
1268  */
1269 static int pxa_ep_set_halt(struct usb_ep *_ep, int value)
1270 {
1271 	struct pxa_ep		*ep;
1272 	struct udc_usb_ep	*udc_usb_ep;
1273 	unsigned long flags;
1274 	int rc;
1275 
1276 
1277 	if (!_ep)
1278 		return -EINVAL;
1279 	udc_usb_ep = container_of(_ep, struct udc_usb_ep, usb_ep);
1280 	ep = udc_usb_ep->pxa_ep;
1281 	if (!ep || is_ep0(ep))
1282 		return -EINVAL;
1283 
1284 	if (value == 0) {
1285 		/*
1286 		 * This path (reset toggle+halt) is needed to implement
1287 		 * SET_INTERFACE on normal hardware.  but it can't be
1288 		 * done from software on the PXA UDC, and the hardware
1289 		 * forgets to do it as part of SET_INTERFACE automagic.
1290 		 */
1291 		ep_dbg(ep, "only host can clear halt\n");
1292 		return -EROFS;
1293 	}
1294 
1295 	spin_lock_irqsave(&ep->lock, flags);
1296 
1297 	rc = -EAGAIN;
1298 	if (ep->dir_in	&& (ep_is_full(ep) || !list_empty(&ep->queue)))
1299 		goto out;
1300 
1301 	/* FST, FEF bits are the same for control and non control endpoints */
1302 	rc = 0;
1303 	ep_write_UDCCSR(ep, UDCCSR_FST | UDCCSR_FEF);
1304 	if (is_ep0(ep))
1305 		set_ep0state(ep->dev, STALL);
1306 
1307 out:
1308 	spin_unlock_irqrestore(&ep->lock, flags);
1309 	return rc;
1310 }
1311 
1312 /**
1313  * pxa_ep_fifo_status - Get how many bytes in physical endpoint
1314  * @_ep: usb endpoint
1315  *
1316  * Returns number of bytes in OUT fifos. Broken for IN fifos.
1317  */
1318 static int pxa_ep_fifo_status(struct usb_ep *_ep)
1319 {
1320 	struct pxa_ep		*ep;
1321 	struct udc_usb_ep	*udc_usb_ep;
1322 
1323 	if (!_ep)
1324 		return -ENODEV;
1325 	udc_usb_ep = container_of(_ep, struct udc_usb_ep, usb_ep);
1326 	ep = udc_usb_ep->pxa_ep;
1327 	if (!ep || is_ep0(ep))
1328 		return -ENODEV;
1329 
1330 	if (ep->dir_in)
1331 		return -EOPNOTSUPP;
1332 	if (ep->dev->gadget.speed == USB_SPEED_UNKNOWN || ep_is_empty(ep))
1333 		return 0;
1334 	else
1335 		return ep_count_bytes_remain(ep) + 1;
1336 }
1337 
1338 /**
1339  * pxa_ep_fifo_flush - Flushes one endpoint
1340  * @_ep: usb endpoint
1341  *
1342  * Discards all data in one endpoint(IN or OUT), except control endpoint.
1343  */
1344 static void pxa_ep_fifo_flush(struct usb_ep *_ep)
1345 {
1346 	struct pxa_ep		*ep;
1347 	struct udc_usb_ep	*udc_usb_ep;
1348 	unsigned long		flags;
1349 
1350 	if (!_ep)
1351 		return;
1352 	udc_usb_ep = container_of(_ep, struct udc_usb_ep, usb_ep);
1353 	ep = udc_usb_ep->pxa_ep;
1354 	if (!ep || is_ep0(ep))
1355 		return;
1356 
1357 	spin_lock_irqsave(&ep->lock, flags);
1358 
1359 	if (unlikely(!list_empty(&ep->queue)))
1360 		ep_dbg(ep, "called while queue list not empty\n");
1361 	ep_dbg(ep, "called\n");
1362 
1363 	/* for OUT, just read and discard the FIFO contents. */
1364 	if (!ep->dir_in) {
1365 		while (!ep_is_empty(ep))
1366 			udc_ep_readl(ep, UDCDR);
1367 	} else {
1368 		/* most IN status is the same, but ISO can't stall */
1369 		ep_write_UDCCSR(ep,
1370 				UDCCSR_PC | UDCCSR_FEF | UDCCSR_TRN
1371 				| (EPXFERTYPE_is_ISO(ep) ? 0 : UDCCSR_SST));
1372 	}
1373 
1374 	spin_unlock_irqrestore(&ep->lock, flags);
1375 }
1376 
1377 /**
1378  * pxa_ep_enable - Enables usb endpoint
1379  * @_ep: usb endpoint
1380  * @desc: usb endpoint descriptor
1381  *
1382  * Nothing much to do here, as ep configuration is done once and for all
1383  * before udc is enabled. After udc enable, no physical endpoint configuration
1384  * can be changed.
1385  * Function makes sanity checks and flushes the endpoint.
1386  */
1387 static int pxa_ep_enable(struct usb_ep *_ep,
1388 	const struct usb_endpoint_descriptor *desc)
1389 {
1390 	struct pxa_ep		*ep;
1391 	struct udc_usb_ep	*udc_usb_ep;
1392 	struct pxa_udc		*udc;
1393 
1394 	if (!_ep || !desc)
1395 		return -EINVAL;
1396 
1397 	udc_usb_ep = container_of(_ep, struct udc_usb_ep, usb_ep);
1398 	if (udc_usb_ep->pxa_ep) {
1399 		ep = udc_usb_ep->pxa_ep;
1400 		ep_warn(ep, "usb_ep %s already enabled, doing nothing\n",
1401 			_ep->name);
1402 	} else {
1403 		ep = find_pxa_ep(udc_usb_ep->dev, udc_usb_ep);
1404 	}
1405 
1406 	if (!ep || is_ep0(ep)) {
1407 		dev_err(udc_usb_ep->dev->dev,
1408 			"unable to match pxa_ep for ep %s\n",
1409 			_ep->name);
1410 		return -EINVAL;
1411 	}
1412 
1413 	if ((desc->bDescriptorType != USB_DT_ENDPOINT)
1414 			|| (ep->type != usb_endpoint_type(desc))) {
1415 		ep_err(ep, "type mismatch\n");
1416 		return -EINVAL;
1417 	}
1418 
1419 	if (ep->fifo_size < usb_endpoint_maxp(desc)) {
1420 		ep_err(ep, "bad maxpacket\n");
1421 		return -ERANGE;
1422 	}
1423 
1424 	udc_usb_ep->pxa_ep = ep;
1425 	udc = ep->dev;
1426 
1427 	if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN) {
1428 		ep_err(ep, "bogus device state\n");
1429 		return -ESHUTDOWN;
1430 	}
1431 
1432 	ep->enabled = 1;
1433 
1434 	/* flush fifo (mostly for OUT buffers) */
1435 	pxa_ep_fifo_flush(_ep);
1436 
1437 	ep_dbg(ep, "enabled\n");
1438 	return 0;
1439 }
1440 
1441 /**
1442  * pxa_ep_disable - Disable usb endpoint
1443  * @_ep: usb endpoint
1444  *
1445  * Same as for pxa_ep_enable, no physical endpoint configuration can be
1446  * changed.
1447  * Function flushes the endpoint and related requests.
1448  */
1449 static int pxa_ep_disable(struct usb_ep *_ep)
1450 {
1451 	struct pxa_ep		*ep;
1452 	struct udc_usb_ep	*udc_usb_ep;
1453 
1454 	if (!_ep)
1455 		return -EINVAL;
1456 
1457 	udc_usb_ep = container_of(_ep, struct udc_usb_ep, usb_ep);
1458 	ep = udc_usb_ep->pxa_ep;
1459 	if (!ep || is_ep0(ep) || !list_empty(&ep->queue))
1460 		return -EINVAL;
1461 
1462 	ep->enabled = 0;
1463 	nuke(ep, -ESHUTDOWN);
1464 
1465 	pxa_ep_fifo_flush(_ep);
1466 	udc_usb_ep->pxa_ep = NULL;
1467 
1468 	ep_dbg(ep, "disabled\n");
1469 	return 0;
1470 }
1471 
1472 static const struct usb_ep_ops pxa_ep_ops = {
1473 	.enable		= pxa_ep_enable,
1474 	.disable	= pxa_ep_disable,
1475 
1476 	.alloc_request	= pxa_ep_alloc_request,
1477 	.free_request	= pxa_ep_free_request,
1478 
1479 	.queue		= pxa_ep_queue,
1480 	.dequeue	= pxa_ep_dequeue,
1481 
1482 	.set_halt	= pxa_ep_set_halt,
1483 	.fifo_status	= pxa_ep_fifo_status,
1484 	.fifo_flush	= pxa_ep_fifo_flush,
1485 };
1486 
1487 /**
1488  * dplus_pullup - Connect or disconnect pullup resistor to D+ pin
1489  * @udc: udc device
1490  * @on: 0 if disconnect pullup resistor, 1 otherwise
1491  * Context: any
1492  *
1493  * Handle D+ pullup resistor, make the device visible to the usb bus, and
1494  * declare it as a full speed usb device
1495  */
1496 static void dplus_pullup(struct pxa_udc *udc, int on)
1497 {
1498 	if (udc->gpiod) {
1499 		gpiod_set_value(udc->gpiod, on);
1500 	} else if (udc->udc_command) {
1501 		if (on)
1502 			udc->udc_command(PXA2XX_UDC_CMD_CONNECT);
1503 		else
1504 			udc->udc_command(PXA2XX_UDC_CMD_DISCONNECT);
1505 	}
1506 	udc->pullup_on = on;
1507 }
1508 
1509 /**
1510  * pxa_udc_get_frame - Returns usb frame number
1511  * @_gadget: usb gadget
1512  */
1513 static int pxa_udc_get_frame(struct usb_gadget *_gadget)
1514 {
1515 	struct pxa_udc *udc = to_gadget_udc(_gadget);
1516 
1517 	return (udc_readl(udc, UDCFNR) & 0x7ff);
1518 }
1519 
1520 /**
1521  * pxa_udc_wakeup - Force udc device out of suspend
1522  * @_gadget: usb gadget
1523  *
1524  * Returns 0 if successful, error code otherwise
1525  */
1526 static int pxa_udc_wakeup(struct usb_gadget *_gadget)
1527 {
1528 	struct pxa_udc *udc = to_gadget_udc(_gadget);
1529 
1530 	/* host may not have enabled remote wakeup */
1531 	if ((udc_readl(udc, UDCCR) & UDCCR_DWRE) == 0)
1532 		return -EHOSTUNREACH;
1533 	udc_set_mask_UDCCR(udc, UDCCR_UDR);
1534 	return 0;
1535 }
1536 
1537 static void udc_enable(struct pxa_udc *udc);
1538 static void udc_disable(struct pxa_udc *udc);
1539 
1540 /**
1541  * should_enable_udc - Tells if UDC should be enabled
1542  * @udc: udc device
1543  * Context: any
1544  *
1545  * The UDC should be enabled if :
1546 
1547  *  - the pullup resistor is connected
1548  *  - and a gadget driver is bound
1549  *  - and vbus is sensed (or no vbus sense is available)
1550  *
1551  * Returns 1 if UDC should be enabled, 0 otherwise
1552  */
1553 static int should_enable_udc(struct pxa_udc *udc)
1554 {
1555 	int put_on;
1556 
1557 	put_on = ((udc->pullup_on) && (udc->driver));
1558 	put_on &= ((udc->vbus_sensed) || (IS_ERR_OR_NULL(udc->transceiver)));
1559 	return put_on;
1560 }
1561 
1562 /**
1563  * should_disable_udc - Tells if UDC should be disabled
1564  * @udc: udc device
1565  * Context: any
1566  *
1567  * The UDC should be disabled if :
1568  *  - the pullup resistor is not connected
1569  *  - or no gadget driver is bound
1570  *  - or no vbus is sensed (when vbus sesing is available)
1571  *
1572  * Returns 1 if UDC should be disabled
1573  */
1574 static int should_disable_udc(struct pxa_udc *udc)
1575 {
1576 	int put_off;
1577 
1578 	put_off = ((!udc->pullup_on) || (!udc->driver));
1579 	put_off |= ((!udc->vbus_sensed) && (!IS_ERR_OR_NULL(udc->transceiver)));
1580 	return put_off;
1581 }
1582 
1583 /**
1584  * pxa_udc_pullup - Offer manual D+ pullup control
1585  * @_gadget: usb gadget using the control
1586  * @is_active: 0 if disconnect, else connect D+ pullup resistor
1587  * Context: !in_interrupt()
1588  *
1589  * Returns 0 if OK, -EOPNOTSUPP if udc driver doesn't handle D+ pullup
1590  */
1591 static int pxa_udc_pullup(struct usb_gadget *_gadget, int is_active)
1592 {
1593 	struct pxa_udc *udc = to_gadget_udc(_gadget);
1594 
1595 	if (!udc->gpiod && !udc->udc_command)
1596 		return -EOPNOTSUPP;
1597 
1598 	dplus_pullup(udc, is_active);
1599 
1600 	if (should_enable_udc(udc))
1601 		udc_enable(udc);
1602 	if (should_disable_udc(udc))
1603 		udc_disable(udc);
1604 	return 0;
1605 }
1606 
1607 /**
1608  * pxa_udc_vbus_session - Called by external transceiver to enable/disable udc
1609  * @_gadget: usb gadget
1610  * @is_active: 0 if should disable the udc, 1 if should enable
1611  *
1612  * Enables the udc, and optionnaly activates D+ pullup resistor. Or disables the
1613  * udc, and deactivates D+ pullup resistor.
1614  *
1615  * Returns 0
1616  */
1617 static int pxa_udc_vbus_session(struct usb_gadget *_gadget, int is_active)
1618 {
1619 	struct pxa_udc *udc = to_gadget_udc(_gadget);
1620 
1621 	udc->vbus_sensed = is_active;
1622 	if (should_enable_udc(udc))
1623 		udc_enable(udc);
1624 	if (should_disable_udc(udc))
1625 		udc_disable(udc);
1626 
1627 	return 0;
1628 }
1629 
1630 /**
1631  * pxa_udc_vbus_draw - Called by gadget driver after SET_CONFIGURATION completed
1632  * @_gadget: usb gadget
1633  * @mA: current drawn
1634  *
1635  * Context: !in_interrupt()
1636  *
1637  * Called after a configuration was chosen by a USB host, to inform how much
1638  * current can be drawn by the device from VBus line.
1639  *
1640  * Returns 0 or -EOPNOTSUPP if no transceiver is handling the udc
1641  */
1642 static int pxa_udc_vbus_draw(struct usb_gadget *_gadget, unsigned mA)
1643 {
1644 	struct pxa_udc *udc;
1645 
1646 	udc = to_gadget_udc(_gadget);
1647 	if (!IS_ERR_OR_NULL(udc->transceiver))
1648 		return usb_phy_set_power(udc->transceiver, mA);
1649 	return -EOPNOTSUPP;
1650 }
1651 
1652 /**
1653  * pxa_udc_phy_event - Called by phy upon VBus event
1654  * @nb: notifier block
1655  * @action: phy action, is vbus connect or disconnect
1656  * @data: the usb_gadget structure in pxa_udc
1657  *
1658  * Called by the USB Phy when a cable connect or disconnect is sensed.
1659  *
1660  * Returns 0
1661  */
1662 static int pxa_udc_phy_event(struct notifier_block *nb, unsigned long action,
1663 			     void *data)
1664 {
1665 	struct usb_gadget *gadget = data;
1666 
1667 	switch (action) {
1668 	case USB_EVENT_VBUS:
1669 		usb_gadget_vbus_connect(gadget);
1670 		return NOTIFY_OK;
1671 	case USB_EVENT_NONE:
1672 		usb_gadget_vbus_disconnect(gadget);
1673 		return NOTIFY_OK;
1674 	default:
1675 		return NOTIFY_DONE;
1676 	}
1677 }
1678 
1679 static struct notifier_block pxa27x_udc_phy = {
1680 	.notifier_call = pxa_udc_phy_event,
1681 };
1682 
1683 static int pxa27x_udc_start(struct usb_gadget *g,
1684 		struct usb_gadget_driver *driver);
1685 static int pxa27x_udc_stop(struct usb_gadget *g);
1686 
1687 static const struct usb_gadget_ops pxa_udc_ops = {
1688 	.get_frame	= pxa_udc_get_frame,
1689 	.wakeup		= pxa_udc_wakeup,
1690 	.pullup		= pxa_udc_pullup,
1691 	.vbus_session	= pxa_udc_vbus_session,
1692 	.vbus_draw	= pxa_udc_vbus_draw,
1693 	.udc_start	= pxa27x_udc_start,
1694 	.udc_stop	= pxa27x_udc_stop,
1695 };
1696 
1697 /**
1698  * udc_disable - disable udc device controller
1699  * @udc: udc device
1700  * Context: any
1701  *
1702  * Disables the udc device : disables clocks, udc interrupts, control endpoint
1703  * interrupts.
1704  */
1705 static void udc_disable(struct pxa_udc *udc)
1706 {
1707 	if (!udc->enabled)
1708 		return;
1709 
1710 	udc_writel(udc, UDCICR0, 0);
1711 	udc_writel(udc, UDCICR1, 0);
1712 
1713 	udc_clear_mask_UDCCR(udc, UDCCR_UDE);
1714 
1715 	ep0_idle(udc);
1716 	udc->gadget.speed = USB_SPEED_UNKNOWN;
1717 	clk_disable(udc->clk);
1718 
1719 	udc->enabled = 0;
1720 }
1721 
1722 /**
1723  * udc_init_data - Initialize udc device data structures
1724  * @dev: udc device
1725  *
1726  * Initializes gadget endpoint list, endpoints locks. No action is taken
1727  * on the hardware.
1728  */
1729 static void udc_init_data(struct pxa_udc *dev)
1730 {
1731 	int i;
1732 	struct pxa_ep *ep;
1733 
1734 	/* device/ep0 records init */
1735 	INIT_LIST_HEAD(&dev->gadget.ep_list);
1736 	INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
1737 	dev->udc_usb_ep[0].pxa_ep = &dev->pxa_ep[0];
1738 	dev->gadget.quirk_altset_not_supp = 1;
1739 	ep0_idle(dev);
1740 
1741 	/* PXA endpoints init */
1742 	for (i = 0; i < NR_PXA_ENDPOINTS; i++) {
1743 		ep = &dev->pxa_ep[i];
1744 
1745 		ep->enabled = is_ep0(ep);
1746 		INIT_LIST_HEAD(&ep->queue);
1747 		spin_lock_init(&ep->lock);
1748 	}
1749 
1750 	/* USB endpoints init */
1751 	for (i = 1; i < NR_USB_ENDPOINTS; i++) {
1752 		list_add_tail(&dev->udc_usb_ep[i].usb_ep.ep_list,
1753 				&dev->gadget.ep_list);
1754 		usb_ep_set_maxpacket_limit(&dev->udc_usb_ep[i].usb_ep,
1755 					   dev->udc_usb_ep[i].usb_ep.maxpacket);
1756 	}
1757 }
1758 
1759 /**
1760  * udc_enable - Enables the udc device
1761  * @dev: udc device
1762  *
1763  * Enables the udc device : enables clocks, udc interrupts, control endpoint
1764  * interrupts, sets usb as UDC client and setups endpoints.
1765  */
1766 static void udc_enable(struct pxa_udc *udc)
1767 {
1768 	if (udc->enabled)
1769 		return;
1770 
1771 	clk_enable(udc->clk);
1772 	udc_writel(udc, UDCICR0, 0);
1773 	udc_writel(udc, UDCICR1, 0);
1774 	udc_clear_mask_UDCCR(udc, UDCCR_UDE);
1775 
1776 	ep0_idle(udc);
1777 	udc->gadget.speed = USB_SPEED_FULL;
1778 	memset(&udc->stats, 0, sizeof(udc->stats));
1779 
1780 	pxa_eps_setup(udc);
1781 	udc_set_mask_UDCCR(udc, UDCCR_UDE);
1782 	ep_write_UDCCSR(&udc->pxa_ep[0], UDCCSR0_ACM);
1783 	udelay(2);
1784 	if (udc_readl(udc, UDCCR) & UDCCR_EMCE)
1785 		dev_err(udc->dev, "Configuration errors, udc disabled\n");
1786 
1787 	/*
1788 	 * Caller must be able to sleep in order to cope with startup transients
1789 	 */
1790 	msleep(100);
1791 
1792 	/* enable suspend/resume and reset irqs */
1793 	udc_writel(udc, UDCICR1,
1794 			UDCICR1_IECC | UDCICR1_IERU
1795 			| UDCICR1_IESU | UDCICR1_IERS);
1796 
1797 	/* enable ep0 irqs */
1798 	pio_irq_enable(&udc->pxa_ep[0]);
1799 
1800 	udc->enabled = 1;
1801 }
1802 
1803 /**
1804  * pxa27x_start - Register gadget driver
1805  * @driver: gadget driver
1806  * @bind: bind function
1807  *
1808  * When a driver is successfully registered, it will receive control requests
1809  * including set_configuration(), which enables non-control requests.  Then
1810  * usb traffic follows until a disconnect is reported.  Then a host may connect
1811  * again, or the driver might get unbound.
1812  *
1813  * Note that the udc is not automatically enabled. Check function
1814  * should_enable_udc().
1815  *
1816  * Returns 0 if no error, -EINVAL, -ENODEV, -EBUSY otherwise
1817  */
1818 static int pxa27x_udc_start(struct usb_gadget *g,
1819 		struct usb_gadget_driver *driver)
1820 {
1821 	struct pxa_udc *udc = to_pxa(g);
1822 	int retval;
1823 
1824 	/* first hook up the driver ... */
1825 	udc->driver = driver;
1826 
1827 	if (!IS_ERR_OR_NULL(udc->transceiver)) {
1828 		retval = otg_set_peripheral(udc->transceiver->otg,
1829 						&udc->gadget);
1830 		if (retval) {
1831 			dev_err(udc->dev, "can't bind to transceiver\n");
1832 			goto fail;
1833 		}
1834 	}
1835 
1836 	if (should_enable_udc(udc))
1837 		udc_enable(udc);
1838 	return 0;
1839 
1840 fail:
1841 	udc->driver = NULL;
1842 	return retval;
1843 }
1844 
1845 /**
1846  * stop_activity - Stops udc endpoints
1847  * @udc: udc device
1848  * @driver: gadget driver
1849  *
1850  * Disables all udc endpoints (even control endpoint), report disconnect to
1851  * the gadget user.
1852  */
1853 static void stop_activity(struct pxa_udc *udc)
1854 {
1855 	int i;
1856 
1857 	udc->gadget.speed = USB_SPEED_UNKNOWN;
1858 
1859 	for (i = 0; i < NR_USB_ENDPOINTS; i++)
1860 		pxa_ep_disable(&udc->udc_usb_ep[i].usb_ep);
1861 }
1862 
1863 /**
1864  * pxa27x_udc_stop - Unregister the gadget driver
1865  * @driver: gadget driver
1866  *
1867  * Returns 0 if no error, -ENODEV, -EINVAL otherwise
1868  */
1869 static int pxa27x_udc_stop(struct usb_gadget *g)
1870 {
1871 	struct pxa_udc *udc = to_pxa(g);
1872 
1873 	stop_activity(udc);
1874 	udc_disable(udc);
1875 
1876 	udc->driver = NULL;
1877 
1878 	if (!IS_ERR_OR_NULL(udc->transceiver))
1879 		return otg_set_peripheral(udc->transceiver->otg, NULL);
1880 	return 0;
1881 }
1882 
1883 /**
1884  * handle_ep0_ctrl_req - handle control endpoint control request
1885  * @udc: udc device
1886  * @req: control request
1887  */
1888 static void handle_ep0_ctrl_req(struct pxa_udc *udc,
1889 				struct pxa27x_request *req)
1890 {
1891 	struct pxa_ep *ep = &udc->pxa_ep[0];
1892 	union {
1893 		struct usb_ctrlrequest	r;
1894 		u32			word[2];
1895 	} u;
1896 	int i;
1897 	int have_extrabytes = 0;
1898 	unsigned long flags;
1899 
1900 	nuke(ep, -EPROTO);
1901 	spin_lock_irqsave(&ep->lock, flags);
1902 
1903 	/*
1904 	 * In the PXA320 manual, in the section about Back-to-Back setup
1905 	 * packets, it describes this situation.  The solution is to set OPC to
1906 	 * get rid of the status packet, and then continue with the setup
1907 	 * packet. Generalize to pxa27x CPUs.
1908 	 */
1909 	if (epout_has_pkt(ep) && (ep_count_bytes_remain(ep) == 0))
1910 		ep_write_UDCCSR(ep, UDCCSR0_OPC);
1911 
1912 	/* read SETUP packet */
1913 	for (i = 0; i < 2; i++) {
1914 		if (unlikely(ep_is_empty(ep)))
1915 			goto stall;
1916 		u.word[i] = udc_ep_readl(ep, UDCDR);
1917 	}
1918 
1919 	have_extrabytes = !ep_is_empty(ep);
1920 	while (!ep_is_empty(ep)) {
1921 		i = udc_ep_readl(ep, UDCDR);
1922 		ep_err(ep, "wrong to have extra bytes for setup : 0x%08x\n", i);
1923 	}
1924 
1925 	ep_dbg(ep, "SETUP %02x.%02x v%04x i%04x l%04x\n",
1926 		u.r.bRequestType, u.r.bRequest,
1927 		le16_to_cpu(u.r.wValue), le16_to_cpu(u.r.wIndex),
1928 		le16_to_cpu(u.r.wLength));
1929 	if (unlikely(have_extrabytes))
1930 		goto stall;
1931 
1932 	if (u.r.bRequestType & USB_DIR_IN)
1933 		set_ep0state(udc, IN_DATA_STAGE);
1934 	else
1935 		set_ep0state(udc, OUT_DATA_STAGE);
1936 
1937 	/* Tell UDC to enter Data Stage */
1938 	ep_write_UDCCSR(ep, UDCCSR0_SA | UDCCSR0_OPC);
1939 
1940 	spin_unlock_irqrestore(&ep->lock, flags);
1941 	i = udc->driver->setup(&udc->gadget, &u.r);
1942 	spin_lock_irqsave(&ep->lock, flags);
1943 	if (i < 0)
1944 		goto stall;
1945 out:
1946 	spin_unlock_irqrestore(&ep->lock, flags);
1947 	return;
1948 stall:
1949 	ep_dbg(ep, "protocol STALL, udccsr0=%03x err %d\n",
1950 		udc_ep_readl(ep, UDCCSR), i);
1951 	ep_write_UDCCSR(ep, UDCCSR0_FST | UDCCSR0_FTF);
1952 	set_ep0state(udc, STALL);
1953 	goto out;
1954 }
1955 
1956 /**
1957  * handle_ep0 - Handle control endpoint data transfers
1958  * @udc: udc device
1959  * @fifo_irq: 1 if triggered by fifo service type irq
1960  * @opc_irq: 1 if triggered by output packet complete type irq
1961  *
1962  * Context : when in_interrupt() or with ep->lock held
1963  *
1964  * Tries to transfer all pending request data into the endpoint and/or
1965  * transfer all pending data in the endpoint into usb requests.
1966  * Handles states of ep0 automata.
1967  *
1968  * PXA27x hardware handles several standard usb control requests without
1969  * driver notification.  The requests fully handled by hardware are :
1970  *  SET_ADDRESS, SET_FEATURE, CLEAR_FEATURE, GET_CONFIGURATION, GET_INTERFACE,
1971  *  GET_STATUS
1972  * The requests handled by hardware, but with irq notification are :
1973  *  SYNCH_FRAME, SET_CONFIGURATION, SET_INTERFACE
1974  * The remaining standard requests really handled by handle_ep0 are :
1975  *  GET_DESCRIPTOR, SET_DESCRIPTOR, specific requests.
1976  * Requests standardized outside of USB 2.0 chapter 9 are handled more
1977  * uniformly, by gadget drivers.
1978  *
1979  * The control endpoint state machine is _not_ USB spec compliant, it's even
1980  * hardly compliant with Intel PXA270 developers guide.
1981  * The key points which inferred this state machine are :
1982  *   - on every setup token, bit UDCCSR0_SA is raised and held until cleared by
1983  *     software.
1984  *   - on every OUT packet received, UDCCSR0_OPC is raised and held until
1985  *     cleared by software.
1986  *   - clearing UDCCSR0_OPC always flushes ep0. If in setup stage, never do it
1987  *     before reading ep0.
1988  *     This is true only for PXA27x. This is not true anymore for PXA3xx family
1989  *     (check Back-to-Back setup packet in developers guide).
1990  *   - irq can be called on a "packet complete" event (opc_irq=1), while
1991  *     UDCCSR0_OPC is not yet raised (delta can be as big as 100ms
1992  *     from experimentation).
1993  *   - as UDCCSR0_SA can be activated while in irq handling, and clearing
1994  *     UDCCSR0_OPC would flush the setup data, we almost never clear UDCCSR0_OPC
1995  *     => we never actually read the "status stage" packet of an IN data stage
1996  *     => this is not documented in Intel documentation
1997  *   - hardware as no idea of STATUS STAGE, it only handle SETUP STAGE and DATA
1998  *     STAGE. The driver add STATUS STAGE to send last zero length packet in
1999  *     OUT_STATUS_STAGE.
2000  *   - special attention was needed for IN_STATUS_STAGE. If a packet complete
2001  *     event is detected, we terminate the status stage without ackowledging the
2002  *     packet (not to risk to loose a potential SETUP packet)
2003  */
2004 static void handle_ep0(struct pxa_udc *udc, int fifo_irq, int opc_irq)
2005 {
2006 	u32			udccsr0;
2007 	struct pxa_ep		*ep = &udc->pxa_ep[0];
2008 	struct pxa27x_request	*req = NULL;
2009 	int			completed = 0;
2010 
2011 	if (!list_empty(&ep->queue))
2012 		req = list_entry(ep->queue.next, struct pxa27x_request, queue);
2013 
2014 	udccsr0 = udc_ep_readl(ep, UDCCSR);
2015 	ep_dbg(ep, "state=%s, req=%p, udccsr0=0x%03x, udcbcr=%d, irq_msk=%x\n",
2016 		EP0_STNAME(udc), req, udccsr0, udc_ep_readl(ep, UDCBCR),
2017 		(fifo_irq << 1 | opc_irq));
2018 
2019 	if (udccsr0 & UDCCSR0_SST) {
2020 		ep_dbg(ep, "clearing stall status\n");
2021 		nuke(ep, -EPIPE);
2022 		ep_write_UDCCSR(ep, UDCCSR0_SST);
2023 		ep0_idle(udc);
2024 	}
2025 
2026 	if (udccsr0 & UDCCSR0_SA) {
2027 		nuke(ep, 0);
2028 		set_ep0state(udc, SETUP_STAGE);
2029 	}
2030 
2031 	switch (udc->ep0state) {
2032 	case WAIT_FOR_SETUP:
2033 		/*
2034 		 * Hardware bug : beware, we cannot clear OPC, since we would
2035 		 * miss a potential OPC irq for a setup packet.
2036 		 * So, we only do ... nothing, and hope for a next irq with
2037 		 * UDCCSR0_SA set.
2038 		 */
2039 		break;
2040 	case SETUP_STAGE:
2041 		udccsr0 &= UDCCSR0_CTRL_REQ_MASK;
2042 		if (likely(udccsr0 == UDCCSR0_CTRL_REQ_MASK))
2043 			handle_ep0_ctrl_req(udc, req);
2044 		break;
2045 	case IN_DATA_STAGE:			/* GET_DESCRIPTOR */
2046 		if (epout_has_pkt(ep))
2047 			ep_write_UDCCSR(ep, UDCCSR0_OPC);
2048 		if (req && !ep_is_full(ep))
2049 			completed = write_ep0_fifo(ep, req);
2050 		if (completed)
2051 			ep0_end_in_req(ep, req, NULL);
2052 		break;
2053 	case OUT_DATA_STAGE:			/* SET_DESCRIPTOR */
2054 		if (epout_has_pkt(ep) && req)
2055 			completed = read_ep0_fifo(ep, req);
2056 		if (completed)
2057 			ep0_end_out_req(ep, req, NULL);
2058 		break;
2059 	case STALL:
2060 		ep_write_UDCCSR(ep, UDCCSR0_FST);
2061 		break;
2062 	case IN_STATUS_STAGE:
2063 		/*
2064 		 * Hardware bug : beware, we cannot clear OPC, since we would
2065 		 * miss a potential PC irq for a setup packet.
2066 		 * So, we only put the ep0 into WAIT_FOR_SETUP state.
2067 		 */
2068 		if (opc_irq)
2069 			ep0_idle(udc);
2070 		break;
2071 	case OUT_STATUS_STAGE:
2072 	case WAIT_ACK_SET_CONF_INTERF:
2073 		ep_warn(ep, "should never get in %s state here!!!\n",
2074 				EP0_STNAME(ep->dev));
2075 		ep0_idle(udc);
2076 		break;
2077 	}
2078 }
2079 
2080 /**
2081  * handle_ep - Handle endpoint data tranfers
2082  * @ep: pxa physical endpoint
2083  *
2084  * Tries to transfer all pending request data into the endpoint and/or
2085  * transfer all pending data in the endpoint into usb requests.
2086  *
2087  * Is always called when in_interrupt() and with ep->lock released.
2088  */
2089 static void handle_ep(struct pxa_ep *ep)
2090 {
2091 	struct pxa27x_request	*req;
2092 	int completed;
2093 	u32 udccsr;
2094 	int is_in = ep->dir_in;
2095 	int loop = 0;
2096 	unsigned long		flags;
2097 
2098 	spin_lock_irqsave(&ep->lock, flags);
2099 	if (ep->in_handle_ep)
2100 		goto recursion_detected;
2101 	ep->in_handle_ep = 1;
2102 
2103 	do {
2104 		completed = 0;
2105 		udccsr = udc_ep_readl(ep, UDCCSR);
2106 
2107 		if (likely(!list_empty(&ep->queue)))
2108 			req = list_entry(ep->queue.next,
2109 					struct pxa27x_request, queue);
2110 		else
2111 			req = NULL;
2112 
2113 		ep_dbg(ep, "req:%p, udccsr 0x%03x loop=%d\n",
2114 				req, udccsr, loop++);
2115 
2116 		if (unlikely(udccsr & (UDCCSR_SST | UDCCSR_TRN)))
2117 			udc_ep_writel(ep, UDCCSR,
2118 					udccsr & (UDCCSR_SST | UDCCSR_TRN));
2119 		if (!req)
2120 			break;
2121 
2122 		if (unlikely(is_in)) {
2123 			if (likely(!ep_is_full(ep)))
2124 				completed = write_fifo(ep, req);
2125 		} else {
2126 			if (likely(epout_has_pkt(ep)))
2127 				completed = read_fifo(ep, req);
2128 		}
2129 
2130 		if (completed) {
2131 			if (is_in)
2132 				ep_end_in_req(ep, req, &flags);
2133 			else
2134 				ep_end_out_req(ep, req, &flags);
2135 		}
2136 	} while (completed);
2137 
2138 	ep->in_handle_ep = 0;
2139 recursion_detected:
2140 	spin_unlock_irqrestore(&ep->lock, flags);
2141 }
2142 
2143 /**
2144  * pxa27x_change_configuration - Handle SET_CONF usb request notification
2145  * @udc: udc device
2146  * @config: usb configuration
2147  *
2148  * Post the request to upper level.
2149  * Don't use any pxa specific harware configuration capabilities
2150  */
2151 static void pxa27x_change_configuration(struct pxa_udc *udc, int config)
2152 {
2153 	struct usb_ctrlrequest req ;
2154 
2155 	dev_dbg(udc->dev, "config=%d\n", config);
2156 
2157 	udc->config = config;
2158 	udc->last_interface = 0;
2159 	udc->last_alternate = 0;
2160 
2161 	req.bRequestType = 0;
2162 	req.bRequest = USB_REQ_SET_CONFIGURATION;
2163 	req.wValue = config;
2164 	req.wIndex = 0;
2165 	req.wLength = 0;
2166 
2167 	set_ep0state(udc, WAIT_ACK_SET_CONF_INTERF);
2168 	udc->driver->setup(&udc->gadget, &req);
2169 	ep_write_UDCCSR(&udc->pxa_ep[0], UDCCSR0_AREN);
2170 }
2171 
2172 /**
2173  * pxa27x_change_interface - Handle SET_INTERF usb request notification
2174  * @udc: udc device
2175  * @iface: interface number
2176  * @alt: alternate setting number
2177  *
2178  * Post the request to upper level.
2179  * Don't use any pxa specific harware configuration capabilities
2180  */
2181 static void pxa27x_change_interface(struct pxa_udc *udc, int iface, int alt)
2182 {
2183 	struct usb_ctrlrequest  req;
2184 
2185 	dev_dbg(udc->dev, "interface=%d, alternate setting=%d\n", iface, alt);
2186 
2187 	udc->last_interface = iface;
2188 	udc->last_alternate = alt;
2189 
2190 	req.bRequestType = USB_RECIP_INTERFACE;
2191 	req.bRequest = USB_REQ_SET_INTERFACE;
2192 	req.wValue = alt;
2193 	req.wIndex = iface;
2194 	req.wLength = 0;
2195 
2196 	set_ep0state(udc, WAIT_ACK_SET_CONF_INTERF);
2197 	udc->driver->setup(&udc->gadget, &req);
2198 	ep_write_UDCCSR(&udc->pxa_ep[0], UDCCSR0_AREN);
2199 }
2200 
2201 /*
2202  * irq_handle_data - Handle data transfer
2203  * @irq: irq IRQ number
2204  * @udc: dev pxa_udc device structure
2205  *
2206  * Called from irq handler, transferts data to or from endpoint to queue
2207  */
2208 static void irq_handle_data(int irq, struct pxa_udc *udc)
2209 {
2210 	int i;
2211 	struct pxa_ep *ep;
2212 	u32 udcisr0 = udc_readl(udc, UDCISR0) & UDCCISR0_EP_MASK;
2213 	u32 udcisr1 = udc_readl(udc, UDCISR1) & UDCCISR1_EP_MASK;
2214 
2215 	if (udcisr0 & UDCISR_INT_MASK) {
2216 		udc->pxa_ep[0].stats.irqs++;
2217 		udc_writel(udc, UDCISR0, UDCISR_INT(0, UDCISR_INT_MASK));
2218 		handle_ep0(udc, !!(udcisr0 & UDCICR_FIFOERR),
2219 				!!(udcisr0 & UDCICR_PKTCOMPL));
2220 	}
2221 
2222 	udcisr0 >>= 2;
2223 	for (i = 1; udcisr0 != 0 && i < 16; udcisr0 >>= 2, i++) {
2224 		if (!(udcisr0 & UDCISR_INT_MASK))
2225 			continue;
2226 
2227 		udc_writel(udc, UDCISR0, UDCISR_INT(i, UDCISR_INT_MASK));
2228 
2229 		WARN_ON(i >= ARRAY_SIZE(udc->pxa_ep));
2230 		if (i < ARRAY_SIZE(udc->pxa_ep)) {
2231 			ep = &udc->pxa_ep[i];
2232 			ep->stats.irqs++;
2233 			handle_ep(ep);
2234 		}
2235 	}
2236 
2237 	for (i = 16; udcisr1 != 0 && i < 24; udcisr1 >>= 2, i++) {
2238 		udc_writel(udc, UDCISR1, UDCISR_INT(i - 16, UDCISR_INT_MASK));
2239 		if (!(udcisr1 & UDCISR_INT_MASK))
2240 			continue;
2241 
2242 		WARN_ON(i >= ARRAY_SIZE(udc->pxa_ep));
2243 		if (i < ARRAY_SIZE(udc->pxa_ep)) {
2244 			ep = &udc->pxa_ep[i];
2245 			ep->stats.irqs++;
2246 			handle_ep(ep);
2247 		}
2248 	}
2249 
2250 }
2251 
2252 /**
2253  * irq_udc_suspend - Handle IRQ "UDC Suspend"
2254  * @udc: udc device
2255  */
2256 static void irq_udc_suspend(struct pxa_udc *udc)
2257 {
2258 	udc_writel(udc, UDCISR1, UDCISR1_IRSU);
2259 	udc->stats.irqs_suspend++;
2260 
2261 	if (udc->gadget.speed != USB_SPEED_UNKNOWN
2262 			&& udc->driver && udc->driver->suspend)
2263 		udc->driver->suspend(&udc->gadget);
2264 	ep0_idle(udc);
2265 }
2266 
2267 /**
2268   * irq_udc_resume - Handle IRQ "UDC Resume"
2269   * @udc: udc device
2270   */
2271 static void irq_udc_resume(struct pxa_udc *udc)
2272 {
2273 	udc_writel(udc, UDCISR1, UDCISR1_IRRU);
2274 	udc->stats.irqs_resume++;
2275 
2276 	if (udc->gadget.speed != USB_SPEED_UNKNOWN
2277 			&& udc->driver && udc->driver->resume)
2278 		udc->driver->resume(&udc->gadget);
2279 }
2280 
2281 /**
2282  * irq_udc_reconfig - Handle IRQ "UDC Change Configuration"
2283  * @udc: udc device
2284  */
2285 static void irq_udc_reconfig(struct pxa_udc *udc)
2286 {
2287 	unsigned config, interface, alternate, config_change;
2288 	u32 udccr = udc_readl(udc, UDCCR);
2289 
2290 	udc_writel(udc, UDCISR1, UDCISR1_IRCC);
2291 	udc->stats.irqs_reconfig++;
2292 
2293 	config = (udccr & UDCCR_ACN) >> UDCCR_ACN_S;
2294 	config_change = (config != udc->config);
2295 	pxa27x_change_configuration(udc, config);
2296 
2297 	interface = (udccr & UDCCR_AIN) >> UDCCR_AIN_S;
2298 	alternate = (udccr & UDCCR_AAISN) >> UDCCR_AAISN_S;
2299 	pxa27x_change_interface(udc, interface, alternate);
2300 
2301 	if (config_change)
2302 		update_pxa_ep_matches(udc);
2303 	udc_set_mask_UDCCR(udc, UDCCR_SMAC);
2304 }
2305 
2306 /**
2307  * irq_udc_reset - Handle IRQ "UDC Reset"
2308  * @udc: udc device
2309  */
2310 static void irq_udc_reset(struct pxa_udc *udc)
2311 {
2312 	u32 udccr = udc_readl(udc, UDCCR);
2313 	struct pxa_ep *ep = &udc->pxa_ep[0];
2314 
2315 	dev_info(udc->dev, "USB reset\n");
2316 	udc_writel(udc, UDCISR1, UDCISR1_IRRS);
2317 	udc->stats.irqs_reset++;
2318 
2319 	if ((udccr & UDCCR_UDA) == 0) {
2320 		dev_dbg(udc->dev, "USB reset start\n");
2321 		stop_activity(udc);
2322 	}
2323 	udc->gadget.speed = USB_SPEED_FULL;
2324 	memset(&udc->stats, 0, sizeof udc->stats);
2325 
2326 	nuke(ep, -EPROTO);
2327 	ep_write_UDCCSR(ep, UDCCSR0_FTF | UDCCSR0_OPC);
2328 	ep0_idle(udc);
2329 }
2330 
2331 /**
2332  * pxa_udc_irq - Main irq handler
2333  * @irq: irq number
2334  * @_dev: udc device
2335  *
2336  * Handles all udc interrupts
2337  */
2338 static irqreturn_t pxa_udc_irq(int irq, void *_dev)
2339 {
2340 	struct pxa_udc *udc = _dev;
2341 	u32 udcisr0 = udc_readl(udc, UDCISR0);
2342 	u32 udcisr1 = udc_readl(udc, UDCISR1);
2343 	u32 udccr = udc_readl(udc, UDCCR);
2344 	u32 udcisr1_spec;
2345 
2346 	dev_vdbg(udc->dev, "Interrupt, UDCISR0:0x%08x, UDCISR1:0x%08x, "
2347 		 "UDCCR:0x%08x\n", udcisr0, udcisr1, udccr);
2348 
2349 	udcisr1_spec = udcisr1 & 0xf8000000;
2350 	if (unlikely(udcisr1_spec & UDCISR1_IRSU))
2351 		irq_udc_suspend(udc);
2352 	if (unlikely(udcisr1_spec & UDCISR1_IRRU))
2353 		irq_udc_resume(udc);
2354 	if (unlikely(udcisr1_spec & UDCISR1_IRCC))
2355 		irq_udc_reconfig(udc);
2356 	if (unlikely(udcisr1_spec & UDCISR1_IRRS))
2357 		irq_udc_reset(udc);
2358 
2359 	if ((udcisr0 & UDCCISR0_EP_MASK) | (udcisr1 & UDCCISR1_EP_MASK))
2360 		irq_handle_data(irq, udc);
2361 
2362 	return IRQ_HANDLED;
2363 }
2364 
2365 static struct pxa_udc memory = {
2366 	.gadget = {
2367 		.ops		= &pxa_udc_ops,
2368 		.ep0		= &memory.udc_usb_ep[0].usb_ep,
2369 		.name		= driver_name,
2370 		.dev = {
2371 			.init_name	= "gadget",
2372 		},
2373 	},
2374 
2375 	.udc_usb_ep = {
2376 		USB_EP_CTRL,
2377 		USB_EP_OUT_BULK(1),
2378 		USB_EP_IN_BULK(2),
2379 		USB_EP_IN_ISO(3),
2380 		USB_EP_OUT_ISO(4),
2381 		USB_EP_IN_INT(5),
2382 	},
2383 
2384 	.pxa_ep = {
2385 		PXA_EP_CTRL,
2386 		/* Endpoints for gadget zero */
2387 		PXA_EP_OUT_BULK(1, 1, 3, 0, 0),
2388 		PXA_EP_IN_BULK(2,  2, 3, 0, 0),
2389 		/* Endpoints for ether gadget, file storage gadget */
2390 		PXA_EP_OUT_BULK(3, 1, 1, 0, 0),
2391 		PXA_EP_IN_BULK(4,  2, 1, 0, 0),
2392 		PXA_EP_IN_ISO(5,   3, 1, 0, 0),
2393 		PXA_EP_OUT_ISO(6,  4, 1, 0, 0),
2394 		PXA_EP_IN_INT(7,   5, 1, 0, 0),
2395 		/* Endpoints for RNDIS, serial */
2396 		PXA_EP_OUT_BULK(8, 1, 2, 0, 0),
2397 		PXA_EP_IN_BULK(9,  2, 2, 0, 0),
2398 		PXA_EP_IN_INT(10,  5, 2, 0, 0),
2399 		/*
2400 		 * All the following endpoints are only for completion.  They
2401 		 * won't never work, as multiple interfaces are really broken on
2402 		 * the pxa.
2403 		*/
2404 		PXA_EP_OUT_BULK(11, 1, 2, 1, 0),
2405 		PXA_EP_IN_BULK(12,  2, 2, 1, 0),
2406 		/* Endpoint for CDC Ether */
2407 		PXA_EP_OUT_BULK(13, 1, 1, 1, 1),
2408 		PXA_EP_IN_BULK(14,  2, 1, 1, 1),
2409 	}
2410 };
2411 
2412 #if defined(CONFIG_OF)
2413 static const struct of_device_id udc_pxa_dt_ids[] = {
2414 	{ .compatible = "marvell,pxa270-udc" },
2415 	{}
2416 };
2417 MODULE_DEVICE_TABLE(of, udc_pxa_dt_ids);
2418 #endif
2419 
2420 /**
2421  * pxa_udc_probe - probes the udc device
2422  * @_dev: platform device
2423  *
2424  * Perform basic init : allocates udc clock, creates sysfs files, requests
2425  * irq.
2426  */
2427 static int pxa_udc_probe(struct platform_device *pdev)
2428 {
2429 	struct resource *regs;
2430 	struct pxa_udc *udc = &memory;
2431 	int retval = 0, gpio;
2432 	struct pxa2xx_udc_mach_info *mach = dev_get_platdata(&pdev->dev);
2433 	unsigned long gpio_flags;
2434 
2435 	if (mach) {
2436 		gpio_flags = mach->gpio_pullup_inverted ? GPIOF_ACTIVE_LOW : 0;
2437 		gpio = mach->gpio_pullup;
2438 		if (gpio_is_valid(gpio)) {
2439 			retval = devm_gpio_request_one(&pdev->dev, gpio,
2440 						       gpio_flags,
2441 						       "USB D+ pullup");
2442 			if (retval)
2443 				return retval;
2444 			udc->gpiod = gpio_to_desc(mach->gpio_pullup);
2445 		}
2446 		udc->udc_command = mach->udc_command;
2447 	} else {
2448 		udc->gpiod = devm_gpiod_get(&pdev->dev, NULL, GPIOD_ASIS);
2449 	}
2450 
2451 	regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2452 	udc->regs = devm_ioremap_resource(&pdev->dev, regs);
2453 	if (IS_ERR(udc->regs))
2454 		return PTR_ERR(udc->regs);
2455 	udc->irq = platform_get_irq(pdev, 0);
2456 	if (udc->irq < 0)
2457 		return udc->irq;
2458 
2459 	udc->dev = &pdev->dev;
2460 	if (of_have_populated_dt()) {
2461 		udc->transceiver =
2462 			devm_usb_get_phy_by_phandle(udc->dev, "phys", 0);
2463 		if (IS_ERR(udc->transceiver))
2464 			return PTR_ERR(udc->transceiver);
2465 	} else {
2466 		udc->transceiver = usb_get_phy(USB_PHY_TYPE_USB2);
2467 	}
2468 
2469 	if (IS_ERR(udc->gpiod)) {
2470 		dev_err(&pdev->dev, "Couldn't find or request D+ gpio : %ld\n",
2471 			PTR_ERR(udc->gpiod));
2472 		return PTR_ERR(udc->gpiod);
2473 	}
2474 	if (udc->gpiod)
2475 		gpiod_direction_output(udc->gpiod, 0);
2476 
2477 	udc->clk = devm_clk_get(&pdev->dev, NULL);
2478 	if (IS_ERR(udc->clk))
2479 		return PTR_ERR(udc->clk);
2480 
2481 	retval = clk_prepare(udc->clk);
2482 	if (retval)
2483 		return retval;
2484 
2485 	udc->vbus_sensed = 0;
2486 
2487 	the_controller = udc;
2488 	platform_set_drvdata(pdev, udc);
2489 	udc_init_data(udc);
2490 
2491 	/* irq setup after old hardware state is cleaned up */
2492 	retval = devm_request_irq(&pdev->dev, udc->irq, pxa_udc_irq,
2493 				  IRQF_SHARED, driver_name, udc);
2494 	if (retval != 0) {
2495 		dev_err(udc->dev, "%s: can't get irq %i, err %d\n",
2496 			driver_name, udc->irq, retval);
2497 		goto err;
2498 	}
2499 
2500 	if (!IS_ERR_OR_NULL(udc->transceiver))
2501 		usb_register_notifier(udc->transceiver, &pxa27x_udc_phy);
2502 	retval = usb_add_gadget_udc(&pdev->dev, &udc->gadget);
2503 	if (retval)
2504 		goto err_add_gadget;
2505 
2506 	pxa_init_debugfs(udc);
2507 	if (should_enable_udc(udc))
2508 		udc_enable(udc);
2509 	return 0;
2510 
2511 err_add_gadget:
2512 	if (!IS_ERR_OR_NULL(udc->transceiver))
2513 		usb_unregister_notifier(udc->transceiver, &pxa27x_udc_phy);
2514 err:
2515 	clk_unprepare(udc->clk);
2516 	return retval;
2517 }
2518 
2519 /**
2520  * pxa_udc_remove - removes the udc device driver
2521  * @_dev: platform device
2522  */
2523 static int pxa_udc_remove(struct platform_device *_dev)
2524 {
2525 	struct pxa_udc *udc = platform_get_drvdata(_dev);
2526 
2527 	usb_del_gadget_udc(&udc->gadget);
2528 	pxa_cleanup_debugfs(udc);
2529 
2530 	if (!IS_ERR_OR_NULL(udc->transceiver)) {
2531 		usb_unregister_notifier(udc->transceiver, &pxa27x_udc_phy);
2532 		usb_put_phy(udc->transceiver);
2533 	}
2534 
2535 	udc->transceiver = NULL;
2536 	the_controller = NULL;
2537 	clk_unprepare(udc->clk);
2538 
2539 	return 0;
2540 }
2541 
2542 static void pxa_udc_shutdown(struct platform_device *_dev)
2543 {
2544 	struct pxa_udc *udc = platform_get_drvdata(_dev);
2545 
2546 	if (udc_readl(udc, UDCCR) & UDCCR_UDE)
2547 		udc_disable(udc);
2548 }
2549 
2550 #ifdef CONFIG_PXA27x
2551 extern void pxa27x_clear_otgph(void);
2552 #else
2553 #define pxa27x_clear_otgph()   do {} while (0)
2554 #endif
2555 
2556 #ifdef CONFIG_PM
2557 /**
2558  * pxa_udc_suspend - Suspend udc device
2559  * @_dev: platform device
2560  * @state: suspend state
2561  *
2562  * Suspends udc : saves configuration registers (UDCCR*), then disables the udc
2563  * device.
2564  */
2565 static int pxa_udc_suspend(struct platform_device *_dev, pm_message_t state)
2566 {
2567 	struct pxa_udc *udc = platform_get_drvdata(_dev);
2568 	struct pxa_ep *ep;
2569 
2570 	ep = &udc->pxa_ep[0];
2571 	udc->udccsr0 = udc_ep_readl(ep, UDCCSR);
2572 
2573 	udc_disable(udc);
2574 	udc->pullup_resume = udc->pullup_on;
2575 	dplus_pullup(udc, 0);
2576 
2577 	if (udc->driver)
2578 		udc->driver->disconnect(&udc->gadget);
2579 
2580 	return 0;
2581 }
2582 
2583 /**
2584  * pxa_udc_resume - Resume udc device
2585  * @_dev: platform device
2586  *
2587  * Resumes udc : restores configuration registers (UDCCR*), then enables the udc
2588  * device.
2589  */
2590 static int pxa_udc_resume(struct platform_device *_dev)
2591 {
2592 	struct pxa_udc *udc = platform_get_drvdata(_dev);
2593 	struct pxa_ep *ep;
2594 
2595 	ep = &udc->pxa_ep[0];
2596 	udc_ep_writel(ep, UDCCSR, udc->udccsr0 & (UDCCSR0_FST | UDCCSR0_DME));
2597 
2598 	dplus_pullup(udc, udc->pullup_resume);
2599 	if (should_enable_udc(udc))
2600 		udc_enable(udc);
2601 	/*
2602 	 * We do not handle OTG yet.
2603 	 *
2604 	 * OTGPH bit is set when sleep mode is entered.
2605 	 * it indicates that OTG pad is retaining its state.
2606 	 * Upon exit from sleep mode and before clearing OTGPH,
2607 	 * Software must configure the USB OTG pad, UDC, and UHC
2608 	 * to the state they were in before entering sleep mode.
2609 	 */
2610 	pxa27x_clear_otgph();
2611 
2612 	return 0;
2613 }
2614 #endif
2615 
2616 /* work with hotplug and coldplug */
2617 MODULE_ALIAS("platform:pxa27x-udc");
2618 
2619 static struct platform_driver udc_driver = {
2620 	.driver		= {
2621 		.name	= "pxa27x-udc",
2622 		.of_match_table = of_match_ptr(udc_pxa_dt_ids),
2623 	},
2624 	.probe		= pxa_udc_probe,
2625 	.remove		= pxa_udc_remove,
2626 	.shutdown	= pxa_udc_shutdown,
2627 #ifdef CONFIG_PM
2628 	.suspend	= pxa_udc_suspend,
2629 	.resume		= pxa_udc_resume
2630 #endif
2631 };
2632 
2633 module_platform_driver(udc_driver);
2634 
2635 MODULE_DESCRIPTION(DRIVER_DESC);
2636 MODULE_AUTHOR("Robert Jarzmik");
2637 MODULE_LICENSE("GPL");
2638