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