blob: 484de6e97662fff5f0eb1cc7a4edf73f165a013e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/usb/gadget/pxa2xx_udc.c
David Brownell91987692005-05-07 13:20:19 -07003 * Intel PXA25x and IXP4xx on-chip full speed USB device controllers
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * Copyright (C) 2002 Intrinsyc, Inc. (Frank Becker)
6 * Copyright (C) 2003 Robert Schwebel, Pengutronix
7 * Copyright (C) 2003 Benedikt Spranger, Pengutronix
8 * Copyright (C) 2003 David Brownell
9 * Copyright (C) 2003 Joshua Wise
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 *
25 */
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027// #define VERBOSE DBG_VERBOSE
28
Milan Svoboda9068a4c2007-06-30 06:25:35 -070029#include <linux/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/module.h>
31#include <linux/kernel.h>
32#include <linux/ioport.h>
33#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/errno.h>
35#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/slab.h>
37#include <linux/init.h>
38#include <linux/timer.h>
39#include <linux/list.h>
40#include <linux/interrupt.h>
41#include <linux/proc_fs.h>
42#include <linux/mm.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010043#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/dma-mapping.h>
Nicolas Pitrec7a3bd12006-10-20 14:20:17 -070045#include <linux/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#include <asm/byteorder.h>
48#include <asm/dma.h>
Milan Svoboda9068a4c2007-06-30 06:25:35 -070049#include <asm/gpio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <asm/system.h>
52#include <asm/mach-types.h>
53#include <asm/unaligned.h>
54#include <asm/hardware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
David Brownell5f848132006-12-16 15:34:53 -080056#include <linux/usb/ch9.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <linux/usb_gadget.h>
58
Milan Svoboda9068a4c2007-06-30 06:25:35 -070059#include <asm/mach/udc_pxa2xx.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61
62/*
David Brownell91987692005-05-07 13:20:19 -070063 * This driver handles the USB Device Controller (UDC) in Intel's PXA 25x
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 * series processors. The UDC for the IXP 4xx series is very similar.
65 * There are fifteen endpoints, in addition to ep0.
66 *
67 * Such controller drivers work with a gadget driver. The gadget driver
68 * returns descriptors, implements configuration and data protocols used
69 * by the host to interact with this device, and allocates endpoints to
70 * the different protocol interfaces. The controller driver virtualizes
71 * usb hardware so that the gadget drivers will be more portable.
David Brownell34ebcd22007-02-24 12:23:52 -080072 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 * This UDC hardware wants to implement a bit too much USB protocol, so
74 * it constrains the sorts of USB configuration change events that work.
75 * The errata for these chips are misleading; some "fixed" bugs from
76 * pxa250 a0/a1 b0/b1/b2 sure act like they're still there.
David Brownellad8c6232007-06-30 06:30:04 -070077 *
78 * Note that the UDC hardware supports DMA (except on IXP) but that's
79 * not used here. IN-DMA (to host) is simple enough, when the data is
80 * suitably aligned (16 bytes) ... the network stack doesn't do that,
81 * other software can. OUT-DMA is buggy in most chip versions, as well
82 * as poorly designed (data toggle not automatic). So this driver won't
83 * bother using DMA. (Mostly-working IN-DMA support was available in
84 * kernels before 2.6.23, but was never enabled or well tested.)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 */
86
David Brownellad8c6232007-06-30 06:30:04 -070087#define DRIVER_VERSION "30-June-2007"
David Brownell91987692005-05-07 13:20:19 -070088#define DRIVER_DESC "PXA 25x USB Device Controller driver"
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90
91static const char driver_name [] = "pxa2xx_udc";
92
93static const char ep0name [] = "ep0";
94
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096// #define DISABLE_TEST_MODE
97
98#ifdef CONFIG_ARCH_IXP4XX
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100/* cpu-specific register addresses are compiled in to this code */
101#ifdef CONFIG_ARCH_PXA
102#error "Can't configure both IXP and PXA"
103#endif
104
105#endif
106
107#include "pxa2xx_udc.h"
108
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110#ifdef CONFIG_USB_PXA2XX_SMALL
111#define SIZE_STR " (small)"
112#else
113#define SIZE_STR ""
114#endif
115
116#ifdef DISABLE_TEST_MODE
117/* (mode == 0) == no undocumented chip tweaks
118 * (mode & 1) == double buffer bulk IN
119 * (mode & 2) == double buffer bulk OUT
120 * ... so mode = 3 (or 7, 15, etc) does it for both
121 */
122static ushort fifo_mode = 0;
123module_param(fifo_mode, ushort, 0);
124MODULE_PARM_DESC (fifo_mode, "pxa2xx udc fifo mode");
125#endif
126
127/* ---------------------------------------------------------------------------
David Brownell34ebcd22007-02-24 12:23:52 -0800128 * endpoint related parts of the api to the usb controller hardware,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 * used by gadget driver; and the inner talker-to-hardware core.
130 * ---------------------------------------------------------------------------
131 */
132
133static void pxa2xx_ep_fifo_flush (struct usb_ep *ep);
134static void nuke (struct pxa2xx_ep *, int status);
135
David Brownellb2bbb202006-06-29 12:25:39 -0700136/* one GPIO should be used to detect VBUS from the host */
137static int is_vbus_present(void)
138{
139 struct pxa2xx_udc_mach_info *mach = the_controller->mach;
140
141 if (mach->gpio_vbus)
Milan Svoboda9068a4c2007-06-30 06:25:35 -0700142 return gpio_get_value(mach->gpio_vbus);
David Brownellb2bbb202006-06-29 12:25:39 -0700143 if (mach->udc_is_connected)
144 return mach->udc_is_connected();
145 return 1;
146}
147
148/* one GPIO should control a D+ pullup, so host sees this device (or not) */
149static void pullup_off(void)
150{
151 struct pxa2xx_udc_mach_info *mach = the_controller->mach;
152
153 if (mach->gpio_pullup)
Milan Svoboda9068a4c2007-06-30 06:25:35 -0700154 gpio_set_value(mach->gpio_pullup, 0);
David Brownellb2bbb202006-06-29 12:25:39 -0700155 else if (mach->udc_command)
156 mach->udc_command(PXA2XX_UDC_CMD_DISCONNECT);
157}
158
159static void pullup_on(void)
160{
161 struct pxa2xx_udc_mach_info *mach = the_controller->mach;
162
163 if (mach->gpio_pullup)
Milan Svoboda9068a4c2007-06-30 06:25:35 -0700164 gpio_set_value(mach->gpio_pullup, 1);
David Brownellb2bbb202006-06-29 12:25:39 -0700165 else if (mach->udc_command)
166 mach->udc_command(PXA2XX_UDC_CMD_CONNECT);
167}
168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169static void pio_irq_enable(int bEndpointAddress)
170{
171 bEndpointAddress &= 0xf;
172 if (bEndpointAddress < 8)
173 UICR0 &= ~(1 << bEndpointAddress);
174 else {
175 bEndpointAddress -= 8;
176 UICR1 &= ~(1 << bEndpointAddress);
177 }
178}
179
180static void pio_irq_disable(int bEndpointAddress)
181{
182 bEndpointAddress &= 0xf;
183 if (bEndpointAddress < 8)
184 UICR0 |= 1 << bEndpointAddress;
185 else {
186 bEndpointAddress -= 8;
187 UICR1 |= 1 << bEndpointAddress;
188 }
189}
190
191/* The UDCCR reg contains mask and interrupt status bits,
192 * so using '|=' isn't safe as it may ack an interrupt.
193 */
194#define UDCCR_MASK_BITS (UDCCR_REM | UDCCR_SRM | UDCCR_UDE)
195
196static inline void udc_set_mask_UDCCR(int mask)
197{
198 UDCCR = (UDCCR & UDCCR_MASK_BITS) | (mask & UDCCR_MASK_BITS);
199}
200
201static inline void udc_clear_mask_UDCCR(int mask)
202{
203 UDCCR = (UDCCR & UDCCR_MASK_BITS) & ~(mask & UDCCR_MASK_BITS);
204}
205
206static inline void udc_ack_int_UDCCR(int mask)
207{
208 /* udccr contains the bits we dont want to change */
209 __u32 udccr = UDCCR & UDCCR_MASK_BITS;
210
211 UDCCR = udccr | (mask & ~UDCCR_MASK_BITS);
212}
213
214/*
215 * endpoint enable/disable
216 *
217 * we need to verify the descriptors used to enable endpoints. since pxa2xx
218 * endpoint configurations are fixed, and are pretty much always enabled,
219 * there's not a lot to manage here.
220 *
221 * because pxa2xx can't selectively initialize bulk (or interrupt) endpoints,
222 * (resetting endpoint halt and toggle), SET_INTERFACE is unusable except
223 * for a single interface (with only the default altsetting) and for gadget
224 * drivers that don't halt endpoints (not reset by set_interface). that also
225 * means that if you use ISO, you must violate the USB spec rule that all
226 * iso endpoints must be in non-default altsettings.
227 */
228static int pxa2xx_ep_enable (struct usb_ep *_ep,
229 const struct usb_endpoint_descriptor *desc)
230{
231 struct pxa2xx_ep *ep;
232 struct pxa2xx_udc *dev;
233
234 ep = container_of (_ep, struct pxa2xx_ep, ep);
235 if (!_ep || !desc || ep->desc || _ep->name == ep0name
236 || desc->bDescriptorType != USB_DT_ENDPOINT
237 || ep->bEndpointAddress != desc->bEndpointAddress
238 || ep->fifo_size < le16_to_cpu
239 (desc->wMaxPacketSize)) {
240 DMSG("%s, bad ep or descriptor\n", __FUNCTION__);
241 return -EINVAL;
242 }
243
244 /* xfer types must match, except that interrupt ~= bulk */
245 if (ep->bmAttributes != desc->bmAttributes
246 && ep->bmAttributes != USB_ENDPOINT_XFER_BULK
247 && desc->bmAttributes != USB_ENDPOINT_XFER_INT) {
248 DMSG("%s, %s type mismatch\n", __FUNCTION__, _ep->name);
249 return -EINVAL;
250 }
251
252 /* hardware _could_ do smaller, but driver doesn't */
253 if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK
254 && le16_to_cpu (desc->wMaxPacketSize)
255 != BULK_FIFO_SIZE)
256 || !desc->wMaxPacketSize) {
257 DMSG("%s, bad %s maxpacket\n", __FUNCTION__, _ep->name);
258 return -ERANGE;
259 }
260
261 dev = ep->dev;
262 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
263 DMSG("%s, bogus device state\n", __FUNCTION__);
264 return -ESHUTDOWN;
265 }
266
267 ep->desc = desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 ep->stopped = 0;
David Brownellad8c6232007-06-30 06:30:04 -0700269 ep->pio_irqs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 ep->ep.maxpacket = le16_to_cpu (desc->wMaxPacketSize);
271
272 /* flush fifo (mostly for OUT buffers) */
273 pxa2xx_ep_fifo_flush (_ep);
274
275 /* ... reset halt state too, if we could ... */
276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 DBG(DBG_VERBOSE, "enabled %s\n", _ep->name);
278 return 0;
279}
280
281static int pxa2xx_ep_disable (struct usb_ep *_ep)
282{
283 struct pxa2xx_ep *ep;
David Brownell91987692005-05-07 13:20:19 -0700284 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 ep = container_of (_ep, struct pxa2xx_ep, ep);
287 if (!_ep || !ep->desc) {
288 DMSG("%s, %s not enabled\n", __FUNCTION__,
289 _ep ? ep->ep.name : NULL);
290 return -EINVAL;
291 }
David Brownell91987692005-05-07 13:20:19 -0700292 local_irq_save(flags);
293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 nuke (ep, -ESHUTDOWN);
295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 /* flush fifo (mostly for IN buffers) */
297 pxa2xx_ep_fifo_flush (_ep);
298
299 ep->desc = NULL;
300 ep->stopped = 1;
301
David Brownell91987692005-05-07 13:20:19 -0700302 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 DBG(DBG_VERBOSE, "%s disabled\n", _ep->name);
304 return 0;
305}
306
307/*-------------------------------------------------------------------------*/
308
309/* for the pxa2xx, these can just wrap kmalloc/kfree. gadget drivers
310 * must still pass correctly initialized endpoints, since other controller
311 * drivers may care about how it's currently set up (dma issues etc).
312 */
313
314/*
David Brownell34ebcd22007-02-24 12:23:52 -0800315 * pxa2xx_ep_alloc_request - allocate a request data structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 */
317static struct usb_request *
Al Viro55016f12005-10-21 03:21:58 -0400318pxa2xx_ep_alloc_request (struct usb_ep *_ep, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
320 struct pxa2xx_request *req;
321
Eric Sesterhenn7039f422006-02-27 13:34:10 -0800322 req = kzalloc(sizeof(*req), gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 if (!req)
324 return NULL;
325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 INIT_LIST_HEAD (&req->queue);
327 return &req->req;
328}
329
330
331/*
David Brownell34ebcd22007-02-24 12:23:52 -0800332 * pxa2xx_ep_free_request - deallocate a request data structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 */
334static void
335pxa2xx_ep_free_request (struct usb_ep *_ep, struct usb_request *_req)
336{
337 struct pxa2xx_request *req;
338
339 req = container_of (_req, struct pxa2xx_request, req);
340 WARN_ON (!list_empty (&req->queue));
341 kfree(req);
342}
343
344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345static void *
346pxa2xx_ep_alloc_buffer(struct usb_ep *_ep, unsigned bytes,
Al Viro55016f12005-10-21 03:21:58 -0400347 dma_addr_t *dma, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
349 char *retval;
350
351 retval = kmalloc (bytes, gfp_flags & ~(__GFP_DMA|__GFP_HIGHMEM));
352 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 *dma = (dma_addr_t)~0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return retval;
355}
356
357static void
358pxa2xx_ep_free_buffer(struct usb_ep *_ep, void *buf, dma_addr_t dma,
359 unsigned bytes)
360{
361 kfree (buf);
362}
363
364/*-------------------------------------------------------------------------*/
365
366/*
367 * done - retire a request; caller blocked irqs
368 */
369static void done(struct pxa2xx_ep *ep, struct pxa2xx_request *req, int status)
370{
371 unsigned stopped = ep->stopped;
372
373 list_del_init(&req->queue);
374
375 if (likely (req->req.status == -EINPROGRESS))
376 req->req.status = status;
377 else
378 status = req->req.status;
379
380 if (status && status != -ESHUTDOWN)
381 DBG(DBG_VERBOSE, "complete %s req %p stat %d len %u/%u\n",
382 ep->ep.name, &req->req, status,
383 req->req.actual, req->req.length);
384
385 /* don't modify queue heads during completion callback */
386 ep->stopped = 1;
387 req->req.complete(&ep->ep, &req->req);
388 ep->stopped = stopped;
389}
390
391
392static inline void ep0_idle (struct pxa2xx_udc *dev)
393{
394 dev->ep0state = EP0_IDLE;
395}
396
397static int
Ian Campbell63a4b522005-10-28 15:26:42 +0100398write_packet(volatile u32 *uddr, struct pxa2xx_request *req, unsigned max)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
400 u8 *buf;
401 unsigned length, count;
402
403 buf = req->req.buf + req->req.actual;
404 prefetch(buf);
405
406 /* how big will this packet be? */
407 length = min(req->req.length - req->req.actual, max);
408 req->req.actual += length;
409
410 count = length;
411 while (likely(count--))
412 *uddr = *buf++;
413
414 return length;
415}
416
417/*
418 * write to an IN endpoint fifo, as many packets as possible.
419 * irqs will use this to write the rest later.
420 * caller guarantees at least one packet buffer is ready (or a zlp).
421 */
422static int
423write_fifo (struct pxa2xx_ep *ep, struct pxa2xx_request *req)
424{
425 unsigned max;
426
427 max = le16_to_cpu(ep->desc->wMaxPacketSize);
428 do {
429 unsigned count;
430 int is_last, is_short;
431
432 count = write_packet(ep->reg_uddr, req, max);
433
434 /* last packet is usually short (or a zlp) */
435 if (unlikely (count != max))
436 is_last = is_short = 1;
437 else {
438 if (likely(req->req.length != req->req.actual)
439 || req->req.zero)
440 is_last = 0;
441 else
442 is_last = 1;
443 /* interrupt/iso maxpacket may not fill the fifo */
444 is_short = unlikely (max < ep->fifo_size);
445 }
446
447 DBG(DBG_VERY_NOISY, "wrote %s %d bytes%s%s %d left %p\n",
448 ep->ep.name, count,
449 is_last ? "/L" : "", is_short ? "/S" : "",
450 req->req.length - req->req.actual, req);
451
452 /* let loose that packet. maybe try writing another one,
453 * double buffering might work. TSP, TPC, and TFS
454 * bit values are the same for all normal IN endpoints.
455 */
456 *ep->reg_udccs = UDCCS_BI_TPC;
457 if (is_short)
458 *ep->reg_udccs = UDCCS_BI_TSP;
459
460 /* requests complete when all IN data is in the FIFO */
461 if (is_last) {
462 done (ep, req, 0);
David Brownellad8c6232007-06-30 06:30:04 -0700463 if (list_empty(&ep->queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 pio_irq_disable (ep->bEndpointAddress);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 return 1;
466 }
467
468 // TODO experiment: how robust can fifo mode tweaking be?
469 // double buffering is off in the default fifo mode, which
470 // prevents TFS from being set here.
471
472 } while (*ep->reg_udccs & UDCCS_BI_TFS);
473 return 0;
474}
475
476/* caller asserts req->pending (ep0 irq status nyet cleared); starts
477 * ep0 data stage. these chips want very simple state transitions.
478 */
479static inline
480void ep0start(struct pxa2xx_udc *dev, u32 flags, const char *tag)
481{
482 UDCCS0 = flags|UDCCS0_SA|UDCCS0_OPR;
483 USIR0 = USIR0_IR0;
484 dev->req_pending = 0;
485 DBG(DBG_VERY_NOISY, "%s %s, %02x/%02x\n",
486 __FUNCTION__, tag, UDCCS0, flags);
487}
488
489static int
490write_ep0_fifo (struct pxa2xx_ep *ep, struct pxa2xx_request *req)
491{
492 unsigned count;
493 int is_short;
494
495 count = write_packet(&UDDR0, req, EP0_FIFO_SIZE);
496 ep->dev->stats.write.bytes += count;
497
498 /* last packet "must be" short (or a zlp) */
499 is_short = (count != EP0_FIFO_SIZE);
500
501 DBG(DBG_VERY_NOISY, "ep0in %d bytes %d left %p\n", count,
502 req->req.length - req->req.actual, req);
503
504 if (unlikely (is_short)) {
505 if (ep->dev->req_pending)
506 ep0start(ep->dev, UDCCS0_IPR, "short IN");
507 else
508 UDCCS0 = UDCCS0_IPR;
509
510 count = req->req.length;
511 done (ep, req, 0);
512 ep0_idle(ep->dev);
Milan Svoboda043ea182006-05-29 03:34:00 -0700513#ifndef CONFIG_ARCH_IXP4XX
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514#if 1
515 /* This seems to get rid of lost status irqs in some cases:
516 * host responds quickly, or next request involves config
517 * change automagic, or should have been hidden, or ...
518 *
519 * FIXME get rid of all udelays possible...
520 */
521 if (count >= EP0_FIFO_SIZE) {
522 count = 100;
523 do {
524 if ((UDCCS0 & UDCCS0_OPR) != 0) {
525 /* clear OPR, generate ack */
526 UDCCS0 = UDCCS0_OPR;
527 break;
528 }
529 count--;
530 udelay(1);
531 } while (count);
532 }
533#endif
Milan Svoboda043ea182006-05-29 03:34:00 -0700534#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 } else if (ep->dev->req_pending)
536 ep0start(ep->dev, 0, "IN");
537 return is_short;
538}
539
540
541/*
542 * read_fifo - unload packet(s) from the fifo we use for usb OUT
543 * transfers and put them into the request. caller should have made
544 * sure there's at least one packet ready.
545 *
546 * returns true if the request completed because of short packet or the
547 * request buffer having filled (and maybe overran till end-of-packet).
548 */
549static int
550read_fifo (struct pxa2xx_ep *ep, struct pxa2xx_request *req)
551{
552 for (;;) {
553 u32 udccs;
554 u8 *buf;
555 unsigned bufferspace, count, is_short;
556
557 /* make sure there's a packet in the FIFO.
558 * UDCCS_{BO,IO}_RPC are all the same bit value.
559 * UDCCS_{BO,IO}_RNE are all the same bit value.
560 */
561 udccs = *ep->reg_udccs;
562 if (unlikely ((udccs & UDCCS_BO_RPC) == 0))
563 break;
564 buf = req->req.buf + req->req.actual;
565 prefetchw(buf);
566 bufferspace = req->req.length - req->req.actual;
567
568 /* read all bytes from this packet */
569 if (likely (udccs & UDCCS_BO_RNE)) {
570 count = 1 + (0x0ff & *ep->reg_ubcr);
571 req->req.actual += min (count, bufferspace);
572 } else /* zlp */
573 count = 0;
574 is_short = (count < ep->ep.maxpacket);
575 DBG(DBG_VERY_NOISY, "read %s %02x, %d bytes%s req %p %d/%d\n",
576 ep->ep.name, udccs, count,
577 is_short ? "/S" : "",
578 req, req->req.actual, req->req.length);
579 while (likely (count-- != 0)) {
580 u8 byte = (u8) *ep->reg_uddr;
581
582 if (unlikely (bufferspace == 0)) {
583 /* this happens when the driver's buffer
584 * is smaller than what the host sent.
585 * discard the extra data.
586 */
587 if (req->req.status != -EOVERFLOW)
588 DMSG("%s overflow %d\n",
589 ep->ep.name, count);
590 req->req.status = -EOVERFLOW;
591 } else {
592 *buf++ = byte;
593 bufferspace--;
594 }
595 }
596 *ep->reg_udccs = UDCCS_BO_RPC;
597 /* RPC/RSP/RNE could now reflect the other packet buffer */
598
599 /* iso is one request per packet */
600 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
601 if (udccs & UDCCS_IO_ROF)
602 req->req.status = -EHOSTUNREACH;
603 /* more like "is_done" */
604 is_short = 1;
605 }
606
607 /* completion */
608 if (is_short || req->req.actual == req->req.length) {
609 done (ep, req, 0);
610 if (list_empty(&ep->queue))
611 pio_irq_disable (ep->bEndpointAddress);
612 return 1;
613 }
614
615 /* finished that packet. the next one may be waiting... */
616 }
617 return 0;
618}
619
620/*
621 * special ep0 version of the above. no UBCR0 or double buffering; status
622 * handshaking is magic. most device protocols don't need control-OUT.
623 * CDC vendor commands (and RNDIS), mass storage CB/CBI, and some other
624 * protocols do use them.
625 */
626static int
627read_ep0_fifo (struct pxa2xx_ep *ep, struct pxa2xx_request *req)
628{
629 u8 *buf, byte;
630 unsigned bufferspace;
631
632 buf = req->req.buf + req->req.actual;
633 bufferspace = req->req.length - req->req.actual;
634
635 while (UDCCS0 & UDCCS0_RNE) {
636 byte = (u8) UDDR0;
637
638 if (unlikely (bufferspace == 0)) {
639 /* this happens when the driver's buffer
640 * is smaller than what the host sent.
641 * discard the extra data.
642 */
643 if (req->req.status != -EOVERFLOW)
644 DMSG("%s overflow\n", ep->ep.name);
645 req->req.status = -EOVERFLOW;
646 } else {
647 *buf++ = byte;
648 req->req.actual++;
649 bufferspace--;
650 }
651 }
652
653 UDCCS0 = UDCCS0_OPR | UDCCS0_IPR;
654
655 /* completion */
656 if (req->req.actual >= req->req.length)
657 return 1;
658
659 /* finished that packet. the next one may be waiting... */
660 return 0;
661}
662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663/*-------------------------------------------------------------------------*/
664
665static int
Al Viro55016f12005-10-21 03:21:58 -0400666pxa2xx_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
668 struct pxa2xx_request *req;
669 struct pxa2xx_ep *ep;
670 struct pxa2xx_udc *dev;
671 unsigned long flags;
672
673 req = container_of(_req, struct pxa2xx_request, req);
674 if (unlikely (!_req || !_req->complete || !_req->buf
675 || !list_empty(&req->queue))) {
676 DMSG("%s, bad params\n", __FUNCTION__);
677 return -EINVAL;
678 }
679
680 ep = container_of(_ep, struct pxa2xx_ep, ep);
681 if (unlikely (!_ep || (!ep->desc && ep->ep.name != ep0name))) {
682 DMSG("%s, bad ep\n", __FUNCTION__);
683 return -EINVAL;
684 }
685
686 dev = ep->dev;
687 if (unlikely (!dev->driver
688 || dev->gadget.speed == USB_SPEED_UNKNOWN)) {
689 DMSG("%s, bogus device state\n", __FUNCTION__);
690 return -ESHUTDOWN;
691 }
692
693 /* iso is always one packet per request, that's the only way
694 * we can report per-packet status. that also helps with dma.
695 */
696 if (unlikely (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
697 && req->req.length > le16_to_cpu
698 (ep->desc->wMaxPacketSize)))
699 return -EMSGSIZE;
700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 DBG(DBG_NOISY, "%s queue req %p, len %d buf %p\n",
David Brownellad8c6232007-06-30 06:30:04 -0700702 _ep->name, _req, _req->length, _req->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 local_irq_save(flags);
705
706 _req->status = -EINPROGRESS;
707 _req->actual = 0;
708
709 /* kickstart this i/o queue? */
710 if (list_empty(&ep->queue) && !ep->stopped) {
711 if (ep->desc == 0 /* ep0 */) {
712 unsigned length = _req->length;
713
714 switch (dev->ep0state) {
715 case EP0_IN_DATA_PHASE:
716 dev->stats.write.ops++;
717 if (write_ep0_fifo(ep, req))
718 req = NULL;
719 break;
720
721 case EP0_OUT_DATA_PHASE:
722 dev->stats.read.ops++;
723 /* messy ... */
724 if (dev->req_config) {
725 DBG(DBG_VERBOSE, "ep0 config ack%s\n",
726 dev->has_cfr ? "" : " raced");
727 if (dev->has_cfr)
728 UDCCFR = UDCCFR_AREN|UDCCFR_ACM
729 |UDCCFR_MB1;
730 done(ep, req, 0);
731 dev->ep0state = EP0_END_XFER;
732 local_irq_restore (flags);
733 return 0;
734 }
735 if (dev->req_pending)
736 ep0start(dev, UDCCS0_IPR, "OUT");
737 if (length == 0 || ((UDCCS0 & UDCCS0_RNE) != 0
738 && read_ep0_fifo(ep, req))) {
739 ep0_idle(dev);
740 done(ep, req, 0);
741 req = NULL;
742 }
743 break;
744
745 default:
746 DMSG("ep0 i/o, odd state %d\n", dev->ep0state);
747 local_irq_restore (flags);
748 return -EL2HLT;
749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 /* can the FIFO can satisfy the request immediately? */
David Brownell91987692005-05-07 13:20:19 -0700751 } else if ((ep->bEndpointAddress & USB_DIR_IN) != 0) {
752 if ((*ep->reg_udccs & UDCCS_BI_TFS) != 0
753 && write_fifo(ep, req))
754 req = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 } else if ((*ep->reg_udccs & UDCCS_BO_RFS) != 0
756 && read_fifo(ep, req)) {
757 req = NULL;
758 }
759
David Brownellad8c6232007-06-30 06:30:04 -0700760 if (likely (req && ep->desc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 pio_irq_enable(ep->bEndpointAddress);
762 }
763
764 /* pio or dma irq handler advances the queue. */
765 if (likely (req != 0))
766 list_add_tail(&req->queue, &ep->queue);
767 local_irq_restore(flags);
768
769 return 0;
770}
771
772
773/*
David Brownell34ebcd22007-02-24 12:23:52 -0800774 * nuke - dequeue ALL requests
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 */
776static void nuke(struct pxa2xx_ep *ep, int status)
777{
778 struct pxa2xx_request *req;
779
780 /* called with irqs blocked */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 while (!list_empty(&ep->queue)) {
782 req = list_entry(ep->queue.next,
783 struct pxa2xx_request,
784 queue);
785 done(ep, req, status);
786 }
787 if (ep->desc)
788 pio_irq_disable (ep->bEndpointAddress);
789}
790
791
792/* dequeue JUST ONE request */
793static int pxa2xx_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
794{
795 struct pxa2xx_ep *ep;
796 struct pxa2xx_request *req;
797 unsigned long flags;
798
799 ep = container_of(_ep, struct pxa2xx_ep, ep);
800 if (!_ep || ep->ep.name == ep0name)
801 return -EINVAL;
802
803 local_irq_save(flags);
804
805 /* make sure it's actually queued on this endpoint */
806 list_for_each_entry (req, &ep->queue, queue) {
807 if (&req->req == _req)
808 break;
809 }
810 if (&req->req != _req) {
811 local_irq_restore(flags);
812 return -EINVAL;
813 }
814
David Brownellad8c6232007-06-30 06:30:04 -0700815 done(ep, req, -ECONNRESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
817 local_irq_restore(flags);
818 return 0;
819}
820
821/*-------------------------------------------------------------------------*/
822
823static int pxa2xx_ep_set_halt(struct usb_ep *_ep, int value)
824{
825 struct pxa2xx_ep *ep;
826 unsigned long flags;
827
828 ep = container_of(_ep, struct pxa2xx_ep, ep);
829 if (unlikely (!_ep
830 || (!ep->desc && ep->ep.name != ep0name))
831 || ep->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
832 DMSG("%s, bad ep\n", __FUNCTION__);
833 return -EINVAL;
834 }
835 if (value == 0) {
836 /* this path (reset toggle+halt) is needed to implement
837 * SET_INTERFACE on normal hardware. but it can't be
838 * done from software on the PXA UDC, and the hardware
839 * forgets to do it as part of SET_INTERFACE automagic.
840 */
841 DMSG("only host can clear %s halt\n", _ep->name);
842 return -EROFS;
843 }
844
845 local_irq_save(flags);
846
847 if ((ep->bEndpointAddress & USB_DIR_IN) != 0
848 && ((*ep->reg_udccs & UDCCS_BI_TFS) == 0
849 || !list_empty(&ep->queue))) {
850 local_irq_restore(flags);
851 return -EAGAIN;
852 }
853
854 /* FST bit is the same for control, bulk in, bulk out, interrupt in */
855 *ep->reg_udccs = UDCCS_BI_FST|UDCCS_BI_FTF;
856
857 /* ep0 needs special care */
858 if (!ep->desc) {
859 start_watchdog(ep->dev);
860 ep->dev->req_pending = 0;
861 ep->dev->ep0state = EP0_STALL;
862
David Brownell34ebcd22007-02-24 12:23:52 -0800863 /* and bulk/intr endpoints like dropping stalls too */
864 } else {
865 unsigned i;
866 for (i = 0; i < 1000; i += 20) {
867 if (*ep->reg_udccs & UDCCS_BI_SST)
868 break;
869 udelay(20);
870 }
871 }
872 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
874 DBG(DBG_VERBOSE, "%s halt\n", _ep->name);
875 return 0;
876}
877
878static int pxa2xx_ep_fifo_status(struct usb_ep *_ep)
879{
880 struct pxa2xx_ep *ep;
881
882 ep = container_of(_ep, struct pxa2xx_ep, ep);
883 if (!_ep) {
884 DMSG("%s, bad ep\n", __FUNCTION__);
885 return -ENODEV;
886 }
887 /* pxa can't report unclaimed bytes from IN fifos */
888 if ((ep->bEndpointAddress & USB_DIR_IN) != 0)
889 return -EOPNOTSUPP;
890 if (ep->dev->gadget.speed == USB_SPEED_UNKNOWN
891 || (*ep->reg_udccs & UDCCS_BO_RFS) == 0)
892 return 0;
893 else
894 return (*ep->reg_ubcr & 0xfff) + 1;
895}
896
897static void pxa2xx_ep_fifo_flush(struct usb_ep *_ep)
898{
899 struct pxa2xx_ep *ep;
900
901 ep = container_of(_ep, struct pxa2xx_ep, ep);
902 if (!_ep || ep->ep.name == ep0name || !list_empty(&ep->queue)) {
903 DMSG("%s, bad ep\n", __FUNCTION__);
904 return;
905 }
906
907 /* toggle and halt bits stay unchanged */
908
909 /* for OUT, just read and discard the FIFO contents. */
910 if ((ep->bEndpointAddress & USB_DIR_IN) == 0) {
911 while (((*ep->reg_udccs) & UDCCS_BO_RNE) != 0)
912 (void) *ep->reg_uddr;
913 return;
914 }
915
916 /* most IN status is the same, but ISO can't stall */
917 *ep->reg_udccs = UDCCS_BI_TPC|UDCCS_BI_FTF|UDCCS_BI_TUR
918 | (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC)
919 ? 0 : UDCCS_BI_SST;
920}
921
922
923static struct usb_ep_ops pxa2xx_ep_ops = {
924 .enable = pxa2xx_ep_enable,
925 .disable = pxa2xx_ep_disable,
926
927 .alloc_request = pxa2xx_ep_alloc_request,
928 .free_request = pxa2xx_ep_free_request,
929
930 .alloc_buffer = pxa2xx_ep_alloc_buffer,
931 .free_buffer = pxa2xx_ep_free_buffer,
932
933 .queue = pxa2xx_ep_queue,
934 .dequeue = pxa2xx_ep_dequeue,
935
936 .set_halt = pxa2xx_ep_set_halt,
937 .fifo_status = pxa2xx_ep_fifo_status,
938 .fifo_flush = pxa2xx_ep_fifo_flush,
939};
940
941
942/* ---------------------------------------------------------------------------
David Brownell34ebcd22007-02-24 12:23:52 -0800943 * device-scoped parts of the api to the usb controller hardware
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 * ---------------------------------------------------------------------------
945 */
946
947static int pxa2xx_udc_get_frame(struct usb_gadget *_gadget)
948{
949 return ((UFNRH & 0x07) << 8) | (UFNRL & 0xff);
950}
951
952static int pxa2xx_udc_wakeup(struct usb_gadget *_gadget)
953{
954 /* host may not have enabled remote wakeup */
955 if ((UDCCS0 & UDCCS0_DRWF) == 0)
956 return -EHOSTUNREACH;
957 udc_set_mask_UDCCR(UDCCR_RSM);
958 return 0;
959}
960
961static void stop_activity(struct pxa2xx_udc *, struct usb_gadget_driver *);
962static void udc_enable (struct pxa2xx_udc *);
963static void udc_disable(struct pxa2xx_udc *);
964
965/* We disable the UDC -- and its 48 MHz clock -- whenever it's not
David Brownell34ebcd22007-02-24 12:23:52 -0800966 * in active use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 */
968static int pullup(struct pxa2xx_udc *udc, int is_active)
969{
970 is_active = is_active && udc->vbus && udc->pullup;
971 DMSG("%s\n", is_active ? "active" : "inactive");
972 if (is_active)
973 udc_enable(udc);
974 else {
975 if (udc->gadget.speed != USB_SPEED_UNKNOWN) {
976 DMSG("disconnect %s\n", udc->driver
977 ? udc->driver->driver.name
978 : "(no driver)");
979 stop_activity(udc, udc->driver);
980 }
981 udc_disable(udc);
982 }
983 return 0;
984}
985
986/* VBUS reporting logically comes from a transceiver */
987static int pxa2xx_udc_vbus_session(struct usb_gadget *_gadget, int is_active)
988{
989 struct pxa2xx_udc *udc;
990
991 udc = container_of(_gadget, struct pxa2xx_udc, gadget);
992 udc->vbus = is_active = (is_active != 0);
993 DMSG("vbus %s\n", is_active ? "supplied" : "inactive");
994 pullup(udc, is_active);
995 return 0;
996}
997
998/* drivers may have software control over D+ pullup */
999static int pxa2xx_udc_pullup(struct usb_gadget *_gadget, int is_active)
1000{
1001 struct pxa2xx_udc *udc;
1002
1003 udc = container_of(_gadget, struct pxa2xx_udc, gadget);
1004
1005 /* not all boards support pullup control */
1006 if (!udc->mach->udc_command)
1007 return -EOPNOTSUPP;
1008
1009 is_active = (is_active != 0);
1010 udc->pullup = is_active;
1011 pullup(udc, is_active);
1012 return 0;
1013}
1014
1015static const struct usb_gadget_ops pxa2xx_udc_ops = {
1016 .get_frame = pxa2xx_udc_get_frame,
1017 .wakeup = pxa2xx_udc_wakeup,
1018 .vbus_session = pxa2xx_udc_vbus_session,
1019 .pullup = pxa2xx_udc_pullup,
1020
1021 // .vbus_draw ... boards may consume current from VBUS, up to
1022 // 100-500mA based on config. the 500uA suspend ceiling means
1023 // that exclusively vbus-powered PXA designs violate USB specs.
1024};
1025
1026/*-------------------------------------------------------------------------*/
1027
1028#ifdef CONFIG_USB_GADGET_DEBUG_FILES
1029
1030static const char proc_node_name [] = "driver/udc";
1031
1032static int
1033udc_proc_read(char *page, char **start, off_t off, int count,
1034 int *eof, void *_dev)
1035{
1036 char *buf = page;
1037 struct pxa2xx_udc *dev = _dev;
1038 char *next = buf;
1039 unsigned size = count;
1040 unsigned long flags;
1041 int i, t;
1042 u32 tmp;
1043
1044 if (off != 0)
1045 return 0;
1046
1047 local_irq_save(flags);
1048
1049 /* basic device status */
1050 t = scnprintf(next, size, DRIVER_DESC "\n"
1051 "%s version: %s\nGadget driver: %s\nHost %s\n\n",
David Brownellad8c6232007-06-30 06:30:04 -07001052 driver_name, DRIVER_VERSION SIZE_STR "(pio)",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 dev->driver ? dev->driver->driver.name : "(none)",
David Brownell91987692005-05-07 13:20:19 -07001054 is_vbus_present() ? "full speed" : "disconnected");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 size -= t;
1056 next += t;
1057
1058 /* registers for device and ep0 */
1059 t = scnprintf(next, size,
1060 "uicr %02X.%02X, usir %02X.%02x, ufnr %02X.%02X\n",
1061 UICR1, UICR0, USIR1, USIR0, UFNRH, UFNRL);
1062 size -= t;
1063 next += t;
1064
1065 tmp = UDCCR;
1066 t = scnprintf(next, size,
1067 "udccr %02X =%s%s%s%s%s%s%s%s\n", tmp,
1068 (tmp & UDCCR_REM) ? " rem" : "",
1069 (tmp & UDCCR_RSTIR) ? " rstir" : "",
1070 (tmp & UDCCR_SRM) ? " srm" : "",
1071 (tmp & UDCCR_SUSIR) ? " susir" : "",
1072 (tmp & UDCCR_RESIR) ? " resir" : "",
1073 (tmp & UDCCR_RSM) ? " rsm" : "",
1074 (tmp & UDCCR_UDA) ? " uda" : "",
1075 (tmp & UDCCR_UDE) ? " ude" : "");
1076 size -= t;
1077 next += t;
1078
1079 tmp = UDCCS0;
1080 t = scnprintf(next, size,
1081 "udccs0 %02X =%s%s%s%s%s%s%s%s\n", tmp,
1082 (tmp & UDCCS0_SA) ? " sa" : "",
1083 (tmp & UDCCS0_RNE) ? " rne" : "",
1084 (tmp & UDCCS0_FST) ? " fst" : "",
1085 (tmp & UDCCS0_SST) ? " sst" : "",
1086 (tmp & UDCCS0_DRWF) ? " dwrf" : "",
1087 (tmp & UDCCS0_FTF) ? " ftf" : "",
1088 (tmp & UDCCS0_IPR) ? " ipr" : "",
1089 (tmp & UDCCS0_OPR) ? " opr" : "");
1090 size -= t;
1091 next += t;
1092
1093 if (dev->has_cfr) {
1094 tmp = UDCCFR;
1095 t = scnprintf(next, size,
1096 "udccfr %02X =%s%s\n", tmp,
1097 (tmp & UDCCFR_AREN) ? " aren" : "",
1098 (tmp & UDCCFR_ACM) ? " acm" : "");
1099 size -= t;
1100 next += t;
1101 }
1102
David Brownell91987692005-05-07 13:20:19 -07001103 if (!is_vbus_present() || !dev->driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 goto done;
1105
1106 t = scnprintf(next, size, "ep0 IN %lu/%lu, OUT %lu/%lu\nirqs %lu\n\n",
1107 dev->stats.write.bytes, dev->stats.write.ops,
1108 dev->stats.read.bytes, dev->stats.read.ops,
1109 dev->stats.irqs);
1110 size -= t;
1111 next += t;
1112
1113 /* dump endpoint queues */
1114 for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
1115 struct pxa2xx_ep *ep = &dev->ep [i];
1116 struct pxa2xx_request *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
1118 if (i != 0) {
1119 const struct usb_endpoint_descriptor *d;
1120
1121 d = ep->desc;
1122 if (!d)
1123 continue;
1124 tmp = *dev->ep [i].reg_udccs;
1125 t = scnprintf(next, size,
David Brownellad8c6232007-06-30 06:30:04 -07001126 "%s max %d %s udccs %02x irqs %lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 ep->ep.name, le16_to_cpu (d->wMaxPacketSize),
David Brownellad8c6232007-06-30 06:30:04 -07001128 "pio", tmp, ep->pio_irqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 /* TODO translate all five groups of udccs bits! */
1130
1131 } else /* ep0 should only have one transfer queued */
1132 t = scnprintf(next, size, "ep0 max 16 pio irqs %lu\n",
1133 ep->pio_irqs);
1134 if (t <= 0 || t > size)
1135 goto done;
1136 size -= t;
1137 next += t;
1138
1139 if (list_empty(&ep->queue)) {
1140 t = scnprintf(next, size, "\t(nothing queued)\n");
1141 if (t <= 0 || t > size)
1142 goto done;
1143 size -= t;
1144 next += t;
1145 continue;
1146 }
1147 list_for_each_entry(req, &ep->queue, queue) {
David Brownellad8c6232007-06-30 06:30:04 -07001148 t = scnprintf(next, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 "\treq %p len %d/%d buf %p\n",
1150 &req->req, req->req.actual,
1151 req->req.length, req->req.buf);
1152 if (t <= 0 || t > size)
1153 goto done;
1154 size -= t;
1155 next += t;
1156 }
1157 }
1158
1159done:
1160 local_irq_restore(flags);
1161 *eof = 1;
1162 return count - size;
1163}
1164
1165#define create_proc_files() \
1166 create_proc_read_entry(proc_node_name, 0, NULL, udc_proc_read, dev)
1167#define remove_proc_files() \
1168 remove_proc_entry(proc_node_name, NULL)
1169
1170#else /* !CONFIG_USB_GADGET_DEBUG_FILES */
1171
1172#define create_proc_files() do {} while (0)
1173#define remove_proc_files() do {} while (0)
1174
1175#endif /* CONFIG_USB_GADGET_DEBUG_FILES */
1176
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177/*-------------------------------------------------------------------------*/
1178
1179/*
David Brownell34ebcd22007-02-24 12:23:52 -08001180 * udc_disable - disable USB device controller
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 */
1182static void udc_disable(struct pxa2xx_udc *dev)
1183{
1184 /* block all irqs */
1185 udc_set_mask_UDCCR(UDCCR_SRM|UDCCR_REM);
1186 UICR0 = UICR1 = 0xff;
1187 UFNRH = UFNRH_SIM;
1188
1189 /* if hardware supports it, disconnect from usb */
David Brownell91987692005-05-07 13:20:19 -07001190 pullup_off();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
1192 udc_clear_mask_UDCCR(UDCCR_UDE);
1193
1194#ifdef CONFIG_ARCH_PXA
1195 /* Disable clock for USB device */
Eric Miao7053acb2007-04-05 04:07:20 +01001196 pxa_set_cken(CKEN_USB, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197#endif
1198
1199 ep0_idle (dev);
1200 dev->gadget.speed = USB_SPEED_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201}
1202
1203
1204/*
David Brownell34ebcd22007-02-24 12:23:52 -08001205 * udc_reinit - initialize software state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 */
1207static void udc_reinit(struct pxa2xx_udc *dev)
1208{
1209 u32 i;
1210
1211 /* device/ep0 records init */
1212 INIT_LIST_HEAD (&dev->gadget.ep_list);
1213 INIT_LIST_HEAD (&dev->gadget.ep0->ep_list);
1214 dev->ep0state = EP0_IDLE;
1215
1216 /* basic endpoint records init */
1217 for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
1218 struct pxa2xx_ep *ep = &dev->ep[i];
1219
1220 if (i != 0)
1221 list_add_tail (&ep->ep.ep_list, &dev->gadget.ep_list);
1222
1223 ep->desc = NULL;
1224 ep->stopped = 0;
1225 INIT_LIST_HEAD (&ep->queue);
David Brownellad8c6232007-06-30 06:30:04 -07001226 ep->pio_irqs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 }
1228
1229 /* the rest was statically initialized, and is read-only */
1230}
1231
1232/* until it's enabled, this UDC should be completely invisible
1233 * to any USB host.
1234 */
1235static void udc_enable (struct pxa2xx_udc *dev)
1236{
1237 udc_clear_mask_UDCCR(UDCCR_UDE);
1238
1239#ifdef CONFIG_ARCH_PXA
1240 /* Enable clock for USB device */
Eric Miao7053acb2007-04-05 04:07:20 +01001241 pxa_set_cken(CKEN_USB, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 udelay(5);
1243#endif
1244
1245 /* try to clear these bits before we enable the udc */
1246 udc_ack_int_UDCCR(UDCCR_SUSIR|/*UDCCR_RSTIR|*/UDCCR_RESIR);
1247
1248 ep0_idle(dev);
1249 dev->gadget.speed = USB_SPEED_UNKNOWN;
1250 dev->stats.irqs = 0;
1251
1252 /*
1253 * sequence taken from chapter 12.5.10, PXA250 AppProcDevManual:
1254 * - enable UDC
1255 * - if RESET is already in progress, ack interrupt
1256 * - unmask reset interrupt
1257 */
1258 udc_set_mask_UDCCR(UDCCR_UDE);
1259 if (!(UDCCR & UDCCR_UDA))
1260 udc_ack_int_UDCCR(UDCCR_RSTIR);
1261
1262 if (dev->has_cfr /* UDC_RES2 is defined */) {
1263 /* pxa255 (a0+) can avoid a set_config race that could
1264 * prevent gadget drivers from configuring correctly
1265 */
1266 UDCCFR = UDCCFR_ACM | UDCCFR_MB1;
1267 } else {
1268 /* "USB test mode" for pxa250 errata 40-42 (stepping a0, a1)
1269 * which could result in missing packets and interrupts.
1270 * supposedly one bit per endpoint, controlling whether it
1271 * double buffers or not; ACM/AREN bits fit into the holes.
1272 * zero bits (like USIR0_IRx) disable double buffering.
1273 */
1274 UDC_RES1 = 0x00;
1275 UDC_RES2 = 0x00;
1276 }
1277
1278#ifdef DISABLE_TEST_MODE
1279 /* "test mode" seems to have become the default in later chip
1280 * revs, preventing double buffering (and invalidating docs).
1281 * this EXPERIMENT enables it for bulk endpoints by tweaking
1282 * undefined/reserved register bits (that other drivers clear).
1283 * Belcarra code comments noted this usage.
1284 */
1285 if (fifo_mode & 1) { /* IN endpoints */
1286 UDC_RES1 |= USIR0_IR1|USIR0_IR6;
1287 UDC_RES2 |= USIR1_IR11;
1288 }
1289 if (fifo_mode & 2) { /* OUT endpoints */
1290 UDC_RES1 |= USIR0_IR2|USIR0_IR7;
1291 UDC_RES2 |= USIR1_IR12;
1292 }
1293#endif
1294
1295 /* enable suspend/resume and reset irqs */
1296 udc_clear_mask_UDCCR(UDCCR_SRM | UDCCR_REM);
1297
1298 /* enable ep0 irqs */
1299 UICR0 &= ~UICR0_IM0;
1300
1301 /* if hardware supports it, pullup D+ and wait for reset */
David Brownell91987692005-05-07 13:20:19 -07001302 pullup_on();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303}
1304
1305
1306/* when a driver is successfully registered, it will receive
1307 * control requests including set_configuration(), which enables
1308 * non-control requests. then usb traffic follows until a
1309 * disconnect is reported. then a host may connect again, or
1310 * the driver might get unbound.
1311 */
1312int usb_gadget_register_driver(struct usb_gadget_driver *driver)
1313{
1314 struct pxa2xx_udc *dev = the_controller;
1315 int retval;
1316
1317 if (!driver
Milan Svoboda7c0642c2006-05-29 03:34:00 -07001318 || driver->speed < USB_SPEED_FULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 || !driver->bind
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 || !driver->disconnect
1321 || !driver->setup)
1322 return -EINVAL;
1323 if (!dev)
1324 return -ENODEV;
1325 if (dev->driver)
1326 return -EBUSY;
1327
1328 /* first hook up the driver ... */
1329 dev->driver = driver;
1330 dev->gadget.dev.driver = &driver->driver;
1331 dev->pullup = 1;
1332
David Brownell34ebcd22007-02-24 12:23:52 -08001333 retval = device_add (&dev->gadget.dev);
1334 if (retval) {
1335fail:
1336 dev->driver = NULL;
1337 dev->gadget.dev.driver = NULL;
1338 return retval;
1339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 retval = driver->bind(&dev->gadget);
1341 if (retval) {
1342 DMSG("bind to driver %s --> error %d\n",
1343 driver->driver.name, retval);
1344 device_del (&dev->gadget.dev);
David Brownell34ebcd22007-02-24 12:23:52 -08001345 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
1348 /* ... then enable host detection and ep0; and we're ready
1349 * for set_configuration as well as eventual disconnect.
1350 */
1351 DMSG("registered gadget driver '%s'\n", driver->driver.name);
1352 pullup(dev, 1);
1353 dump_state(dev);
1354 return 0;
1355}
1356EXPORT_SYMBOL(usb_gadget_register_driver);
1357
1358static void
1359stop_activity(struct pxa2xx_udc *dev, struct usb_gadget_driver *driver)
1360{
1361 int i;
1362
1363 /* don't disconnect drivers more than once */
1364 if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1365 driver = NULL;
1366 dev->gadget.speed = USB_SPEED_UNKNOWN;
1367
1368 /* prevent new request submissions, kill any outstanding requests */
1369 for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
1370 struct pxa2xx_ep *ep = &dev->ep[i];
1371
1372 ep->stopped = 1;
1373 nuke(ep, -ESHUTDOWN);
1374 }
1375 del_timer_sync(&dev->timer);
1376
1377 /* report disconnect; the driver is already quiesced */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 if (driver)
1379 driver->disconnect(&dev->gadget);
1380
1381 /* re-init driver-visible data structures */
1382 udc_reinit(dev);
1383}
1384
1385int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1386{
1387 struct pxa2xx_udc *dev = the_controller;
1388
1389 if (!dev)
1390 return -ENODEV;
David Brownell6bea4762006-12-05 03:15:33 -08001391 if (!driver || driver != dev->driver || !driver->unbind)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 return -EINVAL;
1393
1394 local_irq_disable();
1395 pullup(dev, 0);
1396 stop_activity(dev, driver);
1397 local_irq_enable();
1398
1399 driver->unbind(&dev->gadget);
1400 dev->driver = NULL;
1401
1402 device_del (&dev->gadget.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
1404 DMSG("unregistered gadget driver '%s'\n", driver->driver.name);
1405 dump_state(dev);
1406 return 0;
1407}
1408EXPORT_SYMBOL(usb_gadget_unregister_driver);
1409
1410
1411/*-------------------------------------------------------------------------*/
1412
1413#ifdef CONFIG_ARCH_LUBBOCK
1414
1415/* Lubbock has separate connect and disconnect irqs. More typical designs
1416 * use one GPIO as the VBUS IRQ, and another to control the D+ pullup.
1417 */
1418
1419static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +01001420lubbock_vbus_irq(int irq, void *_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421{
1422 struct pxa2xx_udc *dev = _dev;
1423 int vbus;
1424
1425 dev->stats.irqs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 switch (irq) {
1427 case LUBBOCK_USB_IRQ:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 vbus = 1;
1429 disable_irq(LUBBOCK_USB_IRQ);
1430 enable_irq(LUBBOCK_USB_DISC_IRQ);
1431 break;
1432 case LUBBOCK_USB_DISC_IRQ:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 vbus = 0;
1434 disable_irq(LUBBOCK_USB_DISC_IRQ);
1435 enable_irq(LUBBOCK_USB_IRQ);
1436 break;
1437 default:
1438 return IRQ_NONE;
1439 }
1440
1441 pxa2xx_udc_vbus_session(&dev->gadget, vbus);
1442 return IRQ_HANDLED;
1443}
1444
1445#endif
1446
David Howells7d12e782006-10-05 14:55:46 +01001447static irqreturn_t udc_vbus_irq(int irq, void *_dev)
David Brownellb2bbb202006-06-29 12:25:39 -07001448{
1449 struct pxa2xx_udc *dev = _dev;
Milan Svoboda9068a4c2007-06-30 06:25:35 -07001450 int vbus = gpio_get_value(dev->mach->gpio_vbus);
David Brownellb2bbb202006-06-29 12:25:39 -07001451
1452 pxa2xx_udc_vbus_session(&dev->gadget, vbus);
1453 return IRQ_HANDLED;
1454}
1455
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
1457/*-------------------------------------------------------------------------*/
1458
1459static inline void clear_ep_state (struct pxa2xx_udc *dev)
1460{
1461 unsigned i;
1462
1463 /* hardware SET_{CONFIGURATION,INTERFACE} automagic resets endpoint
1464 * fifos, and pending transactions mustn't be continued in any case.
1465 */
1466 for (i = 1; i < PXA_UDC_NUM_ENDPOINTS; i++)
1467 nuke(&dev->ep[i], -ECONNABORTED);
1468}
1469
1470static void udc_watchdog(unsigned long _dev)
1471{
1472 struct pxa2xx_udc *dev = (void *)_dev;
1473
1474 local_irq_disable();
1475 if (dev->ep0state == EP0_STALL
1476 && (UDCCS0 & UDCCS0_FST) == 0
1477 && (UDCCS0 & UDCCS0_SST) == 0) {
1478 UDCCS0 = UDCCS0_FST|UDCCS0_FTF;
1479 DBG(DBG_VERBOSE, "ep0 re-stall\n");
1480 start_watchdog(dev);
1481 }
1482 local_irq_enable();
1483}
1484
1485static void handle_ep0 (struct pxa2xx_udc *dev)
1486{
1487 u32 udccs0 = UDCCS0;
1488 struct pxa2xx_ep *ep = &dev->ep [0];
1489 struct pxa2xx_request *req;
1490 union {
1491 struct usb_ctrlrequest r;
1492 u8 raw [8];
1493 u32 word [2];
1494 } u;
1495
1496 if (list_empty(&ep->queue))
1497 req = NULL;
1498 else
1499 req = list_entry(ep->queue.next, struct pxa2xx_request, queue);
1500
1501 /* clear stall status */
1502 if (udccs0 & UDCCS0_SST) {
1503 nuke(ep, -EPIPE);
1504 UDCCS0 = UDCCS0_SST;
1505 del_timer(&dev->timer);
1506 ep0_idle(dev);
1507 }
1508
1509 /* previous request unfinished? non-error iff back-to-back ... */
1510 if ((udccs0 & UDCCS0_SA) != 0 && dev->ep0state != EP0_IDLE) {
1511 nuke(ep, 0);
1512 del_timer(&dev->timer);
1513 ep0_idle(dev);
1514 }
1515
1516 switch (dev->ep0state) {
1517 case EP0_IDLE:
1518 /* late-breaking status? */
1519 udccs0 = UDCCS0;
1520
1521 /* start control request? */
1522 if (likely((udccs0 & (UDCCS0_OPR|UDCCS0_SA|UDCCS0_RNE))
1523 == (UDCCS0_OPR|UDCCS0_SA|UDCCS0_RNE))) {
1524 int i;
1525
1526 nuke (ep, -EPROTO);
1527
1528 /* read SETUP packet */
1529 for (i = 0; i < 8; i++) {
1530 if (unlikely(!(UDCCS0 & UDCCS0_RNE))) {
1531bad_setup:
1532 DMSG("SETUP %d!\n", i);
1533 goto stall;
1534 }
1535 u.raw [i] = (u8) UDDR0;
1536 }
1537 if (unlikely((UDCCS0 & UDCCS0_RNE) != 0))
1538 goto bad_setup;
1539
1540got_setup:
1541 DBG(DBG_VERBOSE, "SETUP %02x.%02x v%04x i%04x l%04x\n",
1542 u.r.bRequestType, u.r.bRequest,
1543 le16_to_cpu(u.r.wValue),
1544 le16_to_cpu(u.r.wIndex),
1545 le16_to_cpu(u.r.wLength));
1546
1547 /* cope with automagic for some standard requests. */
1548 dev->req_std = (u.r.bRequestType & USB_TYPE_MASK)
1549 == USB_TYPE_STANDARD;
1550 dev->req_config = 0;
1551 dev->req_pending = 1;
1552 switch (u.r.bRequest) {
1553 /* hardware restricts gadget drivers here! */
1554 case USB_REQ_SET_CONFIGURATION:
1555 if (u.r.bRequestType == USB_RECIP_DEVICE) {
1556 /* reflect hardware's automagic
1557 * up to the gadget driver.
1558 */
1559config_change:
1560 dev->req_config = 1;
1561 clear_ep_state(dev);
1562 /* if !has_cfr, there's no synch
1563 * else use AREN (later) not SA|OPR
1564 * USIR0_IR0 acts edge sensitive
1565 */
1566 }
1567 break;
1568 /* ... and here, even more ... */
1569 case USB_REQ_SET_INTERFACE:
1570 if (u.r.bRequestType == USB_RECIP_INTERFACE) {
1571 /* udc hardware is broken by design:
1572 * - altsetting may only be zero;
1573 * - hw resets all interfaces' eps;
1574 * - ep reset doesn't include halt(?).
1575 */
1576 DMSG("broken set_interface (%d/%d)\n",
1577 le16_to_cpu(u.r.wIndex),
1578 le16_to_cpu(u.r.wValue));
1579 goto config_change;
1580 }
1581 break;
1582 /* hardware was supposed to hide this */
1583 case USB_REQ_SET_ADDRESS:
1584 if (u.r.bRequestType == USB_RECIP_DEVICE) {
1585 ep0start(dev, 0, "address");
1586 return;
1587 }
1588 break;
1589 }
1590
1591 if (u.r.bRequestType & USB_DIR_IN)
1592 dev->ep0state = EP0_IN_DATA_PHASE;
1593 else
1594 dev->ep0state = EP0_OUT_DATA_PHASE;
1595
1596 i = dev->driver->setup(&dev->gadget, &u.r);
1597 if (i < 0) {
1598 /* hardware automagic preventing STALL... */
1599 if (dev->req_config) {
1600 /* hardware sometimes neglects to tell
1601 * tell us about config change events,
1602 * so later ones may fail...
1603 */
1604 WARN("config change %02x fail %d?\n",
1605 u.r.bRequest, i);
1606 return;
1607 /* TODO experiment: if has_cfr,
1608 * hardware didn't ACK; maybe we
1609 * could actually STALL!
1610 */
1611 }
1612 DBG(DBG_VERBOSE, "protocol STALL, "
1613 "%02x err %d\n", UDCCS0, i);
1614stall:
1615 /* the watchdog timer helps deal with cases
1616 * where udc seems to clear FST wrongly, and
1617 * then NAKs instead of STALLing.
1618 */
1619 ep0start(dev, UDCCS0_FST|UDCCS0_FTF, "stall");
1620 start_watchdog(dev);
1621 dev->ep0state = EP0_STALL;
1622
1623 /* deferred i/o == no response yet */
1624 } else if (dev->req_pending) {
1625 if (likely(dev->ep0state == EP0_IN_DATA_PHASE
1626 || dev->req_std || u.r.wLength))
1627 ep0start(dev, 0, "defer");
1628 else
1629 ep0start(dev, UDCCS0_IPR, "defer/IPR");
1630 }
1631
1632 /* expect at least one data or status stage irq */
1633 return;
1634
1635 } else if (likely((udccs0 & (UDCCS0_OPR|UDCCS0_SA))
1636 == (UDCCS0_OPR|UDCCS0_SA))) {
1637 unsigned i;
1638
1639 /* pxa210/250 erratum 131 for B0/B1 says RNE lies.
1640 * still observed on a pxa255 a0.
1641 */
1642 DBG(DBG_VERBOSE, "e131\n");
1643 nuke(ep, -EPROTO);
1644
1645 /* read SETUP data, but don't trust it too much */
1646 for (i = 0; i < 8; i++)
1647 u.raw [i] = (u8) UDDR0;
1648 if ((u.r.bRequestType & USB_RECIP_MASK)
1649 > USB_RECIP_OTHER)
1650 goto stall;
1651 if (u.word [0] == 0 && u.word [1] == 0)
1652 goto stall;
1653 goto got_setup;
1654 } else {
1655 /* some random early IRQ:
1656 * - we acked FST
1657 * - IPR cleared
1658 * - OPR got set, without SA (likely status stage)
1659 */
1660 UDCCS0 = udccs0 & (UDCCS0_SA|UDCCS0_OPR);
1661 }
1662 break;
1663 case EP0_IN_DATA_PHASE: /* GET_DESCRIPTOR etc */
1664 if (udccs0 & UDCCS0_OPR) {
1665 UDCCS0 = UDCCS0_OPR|UDCCS0_FTF;
1666 DBG(DBG_VERBOSE, "ep0in premature status\n");
1667 if (req)
1668 done(ep, req, 0);
1669 ep0_idle(dev);
1670 } else /* irq was IPR clearing */ {
1671 if (req) {
1672 /* this IN packet might finish the request */
1673 (void) write_ep0_fifo(ep, req);
1674 } /* else IN token before response was written */
1675 }
1676 break;
1677 case EP0_OUT_DATA_PHASE: /* SET_DESCRIPTOR etc */
1678 if (udccs0 & UDCCS0_OPR) {
1679 if (req) {
1680 /* this OUT packet might finish the request */
1681 if (read_ep0_fifo(ep, req))
1682 done(ep, req, 0);
1683 /* else more OUT packets expected */
1684 } /* else OUT token before read was issued */
1685 } else /* irq was IPR clearing */ {
1686 DBG(DBG_VERBOSE, "ep0out premature status\n");
1687 if (req)
1688 done(ep, req, 0);
1689 ep0_idle(dev);
1690 }
1691 break;
1692 case EP0_END_XFER:
1693 if (req)
1694 done(ep, req, 0);
1695 /* ack control-IN status (maybe in-zlp was skipped)
1696 * also appears after some config change events.
1697 */
1698 if (udccs0 & UDCCS0_OPR)
1699 UDCCS0 = UDCCS0_OPR;
1700 ep0_idle(dev);
1701 break;
1702 case EP0_STALL:
1703 UDCCS0 = UDCCS0_FST;
1704 break;
1705 }
1706 USIR0 = USIR0_IR0;
1707}
1708
1709static void handle_ep(struct pxa2xx_ep *ep)
1710{
1711 struct pxa2xx_request *req;
1712 int is_in = ep->bEndpointAddress & USB_DIR_IN;
1713 int completed;
1714 u32 udccs, tmp;
1715
1716 do {
1717 completed = 0;
1718 if (likely (!list_empty(&ep->queue)))
1719 req = list_entry(ep->queue.next,
1720 struct pxa2xx_request, queue);
1721 else
1722 req = NULL;
1723
1724 // TODO check FST handling
1725
1726 udccs = *ep->reg_udccs;
1727 if (unlikely(is_in)) { /* irq from TPC, SST, or (ISO) TUR */
1728 tmp = UDCCS_BI_TUR;
1729 if (likely(ep->bmAttributes == USB_ENDPOINT_XFER_BULK))
1730 tmp |= UDCCS_BI_SST;
1731 tmp &= udccs;
1732 if (likely (tmp))
1733 *ep->reg_udccs = tmp;
1734 if (req && likely ((udccs & UDCCS_BI_TFS) != 0))
1735 completed = write_fifo(ep, req);
1736
1737 } else { /* irq from RPC (or for ISO, ROF) */
1738 if (likely(ep->bmAttributes == USB_ENDPOINT_XFER_BULK))
1739 tmp = UDCCS_BO_SST | UDCCS_BO_DME;
1740 else
1741 tmp = UDCCS_IO_ROF | UDCCS_IO_DME;
1742 tmp &= udccs;
1743 if (likely(tmp))
1744 *ep->reg_udccs = tmp;
1745
1746 /* fifos can hold packets, ready for reading... */
1747 if (likely(req)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 completed = read_fifo(ep, req);
1749 } else
1750 pio_irq_disable (ep->bEndpointAddress);
1751 }
1752 ep->pio_irqs++;
1753 } while (completed);
1754}
1755
1756/*
1757 * pxa2xx_udc_irq - interrupt handler
1758 *
1759 * avoid delays in ep0 processing. the control handshaking isn't always
1760 * under software control (pxa250c0 and the pxa255 are better), and delays
1761 * could cause usb protocol errors.
1762 */
1763static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +01001764pxa2xx_udc_irq(int irq, void *_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765{
1766 struct pxa2xx_udc *dev = _dev;
1767 int handled;
1768
1769 dev->stats.irqs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 do {
1771 u32 udccr = UDCCR;
1772
1773 handled = 0;
1774
1775 /* SUSpend Interrupt Request */
1776 if (unlikely(udccr & UDCCR_SUSIR)) {
1777 udc_ack_int_UDCCR(UDCCR_SUSIR);
1778 handled = 1;
David Brownell91987692005-05-07 13:20:19 -07001779 DBG(DBG_VERBOSE, "USB suspend%s\n", is_vbus_present()
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 ? "" : "+disconnect");
1781
David Brownell91987692005-05-07 13:20:19 -07001782 if (!is_vbus_present())
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 stop_activity(dev, dev->driver);
1784 else if (dev->gadget.speed != USB_SPEED_UNKNOWN
1785 && dev->driver
1786 && dev->driver->suspend)
1787 dev->driver->suspend(&dev->gadget);
1788 ep0_idle (dev);
1789 }
1790
1791 /* RESume Interrupt Request */
1792 if (unlikely(udccr & UDCCR_RESIR)) {
1793 udc_ack_int_UDCCR(UDCCR_RESIR);
1794 handled = 1;
1795 DBG(DBG_VERBOSE, "USB resume\n");
1796
1797 if (dev->gadget.speed != USB_SPEED_UNKNOWN
1798 && dev->driver
1799 && dev->driver->resume
David Brownell91987692005-05-07 13:20:19 -07001800 && is_vbus_present())
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 dev->driver->resume(&dev->gadget);
1802 }
1803
1804 /* ReSeT Interrupt Request - USB reset */
1805 if (unlikely(udccr & UDCCR_RSTIR)) {
1806 udc_ack_int_UDCCR(UDCCR_RSTIR);
1807 handled = 1;
1808
1809 if ((UDCCR & UDCCR_UDA) == 0) {
1810 DBG(DBG_VERBOSE, "USB reset start\n");
1811
1812 /* reset driver and endpoints,
1813 * in case that's not yet done
1814 */
1815 stop_activity (dev, dev->driver);
1816
1817 } else {
1818 DBG(DBG_VERBOSE, "USB reset end\n");
1819 dev->gadget.speed = USB_SPEED_FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 memset(&dev->stats, 0, sizeof dev->stats);
1821 /* driver and endpoints are still reset */
1822 }
1823
1824 } else {
1825 u32 usir0 = USIR0 & ~UICR0;
1826 u32 usir1 = USIR1 & ~UICR1;
1827 int i;
1828
1829 if (unlikely (!usir0 && !usir1))
1830 continue;
1831
1832 DBG(DBG_VERY_NOISY, "irq %02x.%02x\n", usir1, usir0);
1833
1834 /* control traffic */
1835 if (usir0 & USIR0_IR0) {
1836 dev->ep[0].pio_irqs++;
1837 handle_ep0(dev);
1838 handled = 1;
1839 }
1840
1841 /* endpoint data transfers */
1842 for (i = 0; i < 8; i++) {
1843 u32 tmp = 1 << i;
1844
1845 if (i && (usir0 & tmp)) {
1846 handle_ep(&dev->ep[i]);
1847 USIR0 |= tmp;
1848 handled = 1;
1849 }
1850 if (usir1 & tmp) {
1851 handle_ep(&dev->ep[i+8]);
1852 USIR1 |= tmp;
1853 handled = 1;
1854 }
1855 }
1856 }
1857
1858 /* we could also ask for 1 msec SOF (SIR) interrupts */
1859
1860 } while (handled);
1861 return IRQ_HANDLED;
1862}
1863
1864/*-------------------------------------------------------------------------*/
1865
1866static void nop_release (struct device *dev)
1867{
1868 DMSG("%s %s\n", __FUNCTION__, dev->bus_id);
1869}
1870
1871/* this uses load-time allocation and initialization (instead of
1872 * doing it at run-time) to save code, eliminate fault paths, and
1873 * be more obviously correct.
1874 */
1875static struct pxa2xx_udc memory = {
1876 .gadget = {
1877 .ops = &pxa2xx_udc_ops,
1878 .ep0 = &memory.ep[0].ep,
1879 .name = driver_name,
1880 .dev = {
1881 .bus_id = "gadget",
1882 .release = nop_release,
1883 },
1884 },
1885
1886 /* control endpoint */
1887 .ep[0] = {
1888 .ep = {
1889 .name = ep0name,
1890 .ops = &pxa2xx_ep_ops,
1891 .maxpacket = EP0_FIFO_SIZE,
1892 },
1893 .dev = &memory,
1894 .reg_udccs = &UDCCS0,
1895 .reg_uddr = &UDDR0,
1896 },
1897
1898 /* first group of endpoints */
1899 .ep[1] = {
1900 .ep = {
1901 .name = "ep1in-bulk",
1902 .ops = &pxa2xx_ep_ops,
1903 .maxpacket = BULK_FIFO_SIZE,
1904 },
1905 .dev = &memory,
1906 .fifo_size = BULK_FIFO_SIZE,
1907 .bEndpointAddress = USB_DIR_IN | 1,
1908 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1909 .reg_udccs = &UDCCS1,
1910 .reg_uddr = &UDDR1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 },
1912 .ep[2] = {
1913 .ep = {
1914 .name = "ep2out-bulk",
1915 .ops = &pxa2xx_ep_ops,
1916 .maxpacket = BULK_FIFO_SIZE,
1917 },
1918 .dev = &memory,
1919 .fifo_size = BULK_FIFO_SIZE,
1920 .bEndpointAddress = 2,
1921 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1922 .reg_udccs = &UDCCS2,
1923 .reg_ubcr = &UBCR2,
1924 .reg_uddr = &UDDR2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 },
1926#ifndef CONFIG_USB_PXA2XX_SMALL
1927 .ep[3] = {
1928 .ep = {
1929 .name = "ep3in-iso",
1930 .ops = &pxa2xx_ep_ops,
1931 .maxpacket = ISO_FIFO_SIZE,
1932 },
1933 .dev = &memory,
1934 .fifo_size = ISO_FIFO_SIZE,
1935 .bEndpointAddress = USB_DIR_IN | 3,
1936 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
1937 .reg_udccs = &UDCCS3,
1938 .reg_uddr = &UDDR3,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 },
1940 .ep[4] = {
1941 .ep = {
1942 .name = "ep4out-iso",
1943 .ops = &pxa2xx_ep_ops,
1944 .maxpacket = ISO_FIFO_SIZE,
1945 },
1946 .dev = &memory,
1947 .fifo_size = ISO_FIFO_SIZE,
1948 .bEndpointAddress = 4,
1949 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
1950 .reg_udccs = &UDCCS4,
1951 .reg_ubcr = &UBCR4,
1952 .reg_uddr = &UDDR4,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 },
1954 .ep[5] = {
1955 .ep = {
1956 .name = "ep5in-int",
1957 .ops = &pxa2xx_ep_ops,
1958 .maxpacket = INT_FIFO_SIZE,
1959 },
1960 .dev = &memory,
1961 .fifo_size = INT_FIFO_SIZE,
1962 .bEndpointAddress = USB_DIR_IN | 5,
1963 .bmAttributes = USB_ENDPOINT_XFER_INT,
1964 .reg_udccs = &UDCCS5,
1965 .reg_uddr = &UDDR5,
1966 },
1967
1968 /* second group of endpoints */
1969 .ep[6] = {
1970 .ep = {
1971 .name = "ep6in-bulk",
1972 .ops = &pxa2xx_ep_ops,
1973 .maxpacket = BULK_FIFO_SIZE,
1974 },
1975 .dev = &memory,
1976 .fifo_size = BULK_FIFO_SIZE,
1977 .bEndpointAddress = USB_DIR_IN | 6,
1978 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1979 .reg_udccs = &UDCCS6,
1980 .reg_uddr = &UDDR6,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 },
1982 .ep[7] = {
1983 .ep = {
1984 .name = "ep7out-bulk",
1985 .ops = &pxa2xx_ep_ops,
1986 .maxpacket = BULK_FIFO_SIZE,
1987 },
1988 .dev = &memory,
1989 .fifo_size = BULK_FIFO_SIZE,
1990 .bEndpointAddress = 7,
1991 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1992 .reg_udccs = &UDCCS7,
1993 .reg_ubcr = &UBCR7,
1994 .reg_uddr = &UDDR7,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 },
1996 .ep[8] = {
1997 .ep = {
1998 .name = "ep8in-iso",
1999 .ops = &pxa2xx_ep_ops,
2000 .maxpacket = ISO_FIFO_SIZE,
2001 },
2002 .dev = &memory,
2003 .fifo_size = ISO_FIFO_SIZE,
2004 .bEndpointAddress = USB_DIR_IN | 8,
2005 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
2006 .reg_udccs = &UDCCS8,
2007 .reg_uddr = &UDDR8,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 },
2009 .ep[9] = {
2010 .ep = {
2011 .name = "ep9out-iso",
2012 .ops = &pxa2xx_ep_ops,
2013 .maxpacket = ISO_FIFO_SIZE,
2014 },
2015 .dev = &memory,
2016 .fifo_size = ISO_FIFO_SIZE,
2017 .bEndpointAddress = 9,
2018 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
2019 .reg_udccs = &UDCCS9,
2020 .reg_ubcr = &UBCR9,
2021 .reg_uddr = &UDDR9,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 },
2023 .ep[10] = {
2024 .ep = {
2025 .name = "ep10in-int",
2026 .ops = &pxa2xx_ep_ops,
2027 .maxpacket = INT_FIFO_SIZE,
2028 },
2029 .dev = &memory,
2030 .fifo_size = INT_FIFO_SIZE,
2031 .bEndpointAddress = USB_DIR_IN | 10,
2032 .bmAttributes = USB_ENDPOINT_XFER_INT,
2033 .reg_udccs = &UDCCS10,
2034 .reg_uddr = &UDDR10,
2035 },
2036
2037 /* third group of endpoints */
2038 .ep[11] = {
2039 .ep = {
2040 .name = "ep11in-bulk",
2041 .ops = &pxa2xx_ep_ops,
2042 .maxpacket = BULK_FIFO_SIZE,
2043 },
2044 .dev = &memory,
2045 .fifo_size = BULK_FIFO_SIZE,
2046 .bEndpointAddress = USB_DIR_IN | 11,
2047 .bmAttributes = USB_ENDPOINT_XFER_BULK,
2048 .reg_udccs = &UDCCS11,
2049 .reg_uddr = &UDDR11,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 },
2051 .ep[12] = {
2052 .ep = {
2053 .name = "ep12out-bulk",
2054 .ops = &pxa2xx_ep_ops,
2055 .maxpacket = BULK_FIFO_SIZE,
2056 },
2057 .dev = &memory,
2058 .fifo_size = BULK_FIFO_SIZE,
2059 .bEndpointAddress = 12,
2060 .bmAttributes = USB_ENDPOINT_XFER_BULK,
2061 .reg_udccs = &UDCCS12,
2062 .reg_ubcr = &UBCR12,
2063 .reg_uddr = &UDDR12,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 },
2065 .ep[13] = {
2066 .ep = {
2067 .name = "ep13in-iso",
2068 .ops = &pxa2xx_ep_ops,
2069 .maxpacket = ISO_FIFO_SIZE,
2070 },
2071 .dev = &memory,
2072 .fifo_size = ISO_FIFO_SIZE,
2073 .bEndpointAddress = USB_DIR_IN | 13,
2074 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
2075 .reg_udccs = &UDCCS13,
2076 .reg_uddr = &UDDR13,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 },
2078 .ep[14] = {
2079 .ep = {
2080 .name = "ep14out-iso",
2081 .ops = &pxa2xx_ep_ops,
2082 .maxpacket = ISO_FIFO_SIZE,
2083 },
2084 .dev = &memory,
2085 .fifo_size = ISO_FIFO_SIZE,
2086 .bEndpointAddress = 14,
2087 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
2088 .reg_udccs = &UDCCS14,
2089 .reg_ubcr = &UBCR14,
2090 .reg_uddr = &UDDR14,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 },
2092 .ep[15] = {
2093 .ep = {
2094 .name = "ep15in-int",
2095 .ops = &pxa2xx_ep_ops,
2096 .maxpacket = INT_FIFO_SIZE,
2097 },
2098 .dev = &memory,
2099 .fifo_size = INT_FIFO_SIZE,
2100 .bEndpointAddress = USB_DIR_IN | 15,
2101 .bmAttributes = USB_ENDPOINT_XFER_INT,
2102 .reg_udccs = &UDCCS15,
2103 .reg_uddr = &UDDR15,
2104 },
2105#endif /* !CONFIG_USB_PXA2XX_SMALL */
2106};
2107
2108#define CP15R0_VENDOR_MASK 0xffffe000
2109
2110#if defined(CONFIG_ARCH_PXA)
2111#define CP15R0_XSCALE_VALUE 0x69052000 /* intel/arm/xscale */
2112
2113#elif defined(CONFIG_ARCH_IXP4XX)
2114#define CP15R0_XSCALE_VALUE 0x69054000 /* intel/arm/ixp4xx */
2115
2116#endif
2117
2118#define CP15R0_PROD_MASK 0x000003f0
2119#define PXA25x 0x00000100 /* and PXA26x */
2120#define PXA210 0x00000120
2121
2122#define CP15R0_REV_MASK 0x0000000f
2123
2124#define CP15R0_PRODREV_MASK (CP15R0_PROD_MASK | CP15R0_REV_MASK)
2125
2126#define PXA255_A0 0x00000106 /* or PXA260_B1 */
2127#define PXA250_C0 0x00000105 /* or PXA26x_B0 */
2128#define PXA250_B2 0x00000104
2129#define PXA250_B1 0x00000103 /* or PXA260_A0 */
2130#define PXA250_B0 0x00000102
2131#define PXA250_A1 0x00000101
2132#define PXA250_A0 0x00000100
2133
2134#define PXA210_C0 0x00000125
2135#define PXA210_B2 0x00000124
2136#define PXA210_B1 0x00000123
2137#define PXA210_B0 0x00000122
2138#define IXP425_A0 0x000001c1
David Brownell827982c2006-11-20 11:38:57 -08002139#define IXP425_B0 0x000001f1
Milan Svoboda043ea182006-05-29 03:34:00 -07002140#define IXP465_AD 0x00000200
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
2142/*
David Brownell34ebcd22007-02-24 12:23:52 -08002143 * probe - binds to the platform device
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 */
Russell King3ae5eae2005-11-09 22:32:44 +00002145static int __init pxa2xx_udc_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146{
2147 struct pxa2xx_udc *dev = &memory;
David Brownellad8c6232007-06-30 06:30:04 -07002148 int retval, vbus_irq, irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 u32 chiprev;
2150
2151 /* insist on Intel/ARM/XScale */
2152 asm("mrc%? p15, 0, %0, c0, c0" : "=r" (chiprev));
2153 if ((chiprev & CP15R0_VENDOR_MASK) != CP15R0_XSCALE_VALUE) {
2154 printk(KERN_ERR "%s: not XScale!\n", driver_name);
2155 return -ENODEV;
2156 }
2157
2158 /* trigger chiprev-specific logic */
2159 switch (chiprev & CP15R0_PRODREV_MASK) {
2160#if defined(CONFIG_ARCH_PXA)
2161 case PXA255_A0:
2162 dev->has_cfr = 1;
2163 break;
2164 case PXA250_A0:
2165 case PXA250_A1:
2166 /* A0/A1 "not released"; ep 13, 15 unusable */
2167 /* fall through */
2168 case PXA250_B2: case PXA210_B2:
2169 case PXA250_B1: case PXA210_B1:
2170 case PXA250_B0: case PXA210_B0:
David Brownellad8c6232007-06-30 06:30:04 -07002171 /* OUT-DMA is broken ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 /* fall through */
2173 case PXA250_C0: case PXA210_C0:
2174 break;
2175#elif defined(CONFIG_ARCH_IXP4XX)
2176 case IXP425_A0:
David Brownell827982c2006-11-20 11:38:57 -08002177 case IXP425_B0:
Milan Svoboda043ea182006-05-29 03:34:00 -07002178 case IXP465_AD:
2179 dev->has_cfr = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 break;
2181#endif
2182 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 printk(KERN_ERR "%s: unrecognized processor: %08x\n",
2184 driver_name, chiprev);
2185 /* iop3xx, ixp4xx, ... */
2186 return -ENODEV;
2187 }
2188
David Brownell34ebcd22007-02-24 12:23:52 -08002189 irq = platform_get_irq(pdev, 0);
2190 if (irq < 0)
2191 return -ENODEV;
2192
David Brownellad8c6232007-06-30 06:30:04 -07002193 pr_debug("%s: IRQ %d%s%s\n", driver_name, irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 dev->has_cfr ? "" : " (!cfr)",
David Brownellad8c6232007-06-30 06:30:04 -07002195 SIZE_STR "(pio)"
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 );
2197
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 /* other non-static parts of init */
Russell King3ae5eae2005-11-09 22:32:44 +00002199 dev->dev = &pdev->dev;
2200 dev->mach = pdev->dev.platform_data;
Milan Svoboda9068a4c2007-06-30 06:25:35 -07002201
David Brownellb2bbb202006-06-29 12:25:39 -07002202 if (dev->mach->gpio_vbus) {
Milan Svoboda9068a4c2007-06-30 06:25:35 -07002203 if ((retval = gpio_request(dev->mach->gpio_vbus,
2204 "pxa2xx_udc GPIO VBUS"))) {
2205 dev_dbg(&pdev->dev,
2206 "can't get vbus gpio %d, err: %d\n",
2207 dev->mach->gpio_vbus, retval);
2208 return -EBUSY;
2209 }
2210 gpio_direction_input(dev->mach->gpio_vbus);
2211 vbus_irq = gpio_to_irq(dev->mach->gpio_vbus);
David Brownellb2bbb202006-06-29 12:25:39 -07002212 set_irq_type(vbus_irq, IRQT_BOTHEDGE);
2213 } else
2214 vbus_irq = 0;
Milan Svoboda9068a4c2007-06-30 06:25:35 -07002215
2216 if (dev->mach->gpio_pullup) {
2217 if ((retval = gpio_request(dev->mach->gpio_pullup,
2218 "pca2xx_udc GPIO PULLUP"))) {
2219 dev_dbg(&pdev->dev,
2220 "can't get pullup gpio %d, err: %d\n",
2221 dev->mach->gpio_pullup, retval);
2222 if (dev->mach->gpio_vbus)
2223 gpio_free(dev->mach->gpio_vbus);
2224 return -EBUSY;
2225 }
2226 gpio_direction_output(dev->mach->gpio_pullup, 0);
2227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
2229 init_timer(&dev->timer);
2230 dev->timer.function = udc_watchdog;
2231 dev->timer.data = (unsigned long) dev;
2232
2233 device_initialize(&dev->gadget.dev);
Russell King3ae5eae2005-11-09 22:32:44 +00002234 dev->gadget.dev.parent = &pdev->dev;
2235 dev->gadget.dev.dma_mask = pdev->dev.dma_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
2237 the_controller = dev;
Russell King3ae5eae2005-11-09 22:32:44 +00002238 platform_set_drvdata(pdev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
2240 udc_disable(dev);
2241 udc_reinit(dev);
2242
David Brownell91987692005-05-07 13:20:19 -07002243 dev->vbus = is_vbus_present();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244
2245 /* irq setup after old hardware state is cleaned up */
David Brownell34ebcd22007-02-24 12:23:52 -08002246 retval = request_irq(irq, pxa2xx_udc_irq,
Thomas Gleixnerd54b5ca2006-07-01 19:29:44 -07002247 IRQF_DISABLED, driver_name, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 if (retval != 0) {
David Brownell34ebcd22007-02-24 12:23:52 -08002249 printk(KERN_ERR "%s: can't get irq %d, err %d\n",
2250 driver_name, irq, retval);
Milan Svoboda9068a4c2007-06-30 06:25:35 -07002251 if (dev->mach->gpio_pullup)
2252 gpio_free(dev->mach->gpio_pullup);
2253 if (dev->mach->gpio_vbus)
2254 gpio_free(dev->mach->gpio_vbus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 return -EBUSY;
2256 }
2257 dev->got_irq = 1;
2258
2259#ifdef CONFIG_ARCH_LUBBOCK
2260 if (machine_is_lubbock()) {
2261 retval = request_irq(LUBBOCK_USB_DISC_IRQ,
2262 lubbock_vbus_irq,
Thomas Gleixnerd54b5ca2006-07-01 19:29:44 -07002263 IRQF_DISABLED | IRQF_SAMPLE_RANDOM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 driver_name, dev);
2265 if (retval != 0) {
2266 printk(KERN_ERR "%s: can't get irq %i, err %d\n",
2267 driver_name, LUBBOCK_USB_DISC_IRQ, retval);
2268lubbock_fail0:
David Brownell34ebcd22007-02-24 12:23:52 -08002269 free_irq(irq, dev);
Milan Svoboda9068a4c2007-06-30 06:25:35 -07002270 if (dev->mach->gpio_pullup)
2271 gpio_free(dev->mach->gpio_pullup);
2272 if (dev->mach->gpio_vbus)
2273 gpio_free(dev->mach->gpio_vbus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 return -EBUSY;
2275 }
2276 retval = request_irq(LUBBOCK_USB_IRQ,
2277 lubbock_vbus_irq,
Thomas Gleixnerd54b5ca2006-07-01 19:29:44 -07002278 IRQF_DISABLED | IRQF_SAMPLE_RANDOM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 driver_name, dev);
2280 if (retval != 0) {
2281 printk(KERN_ERR "%s: can't get irq %i, err %d\n",
2282 driver_name, LUBBOCK_USB_IRQ, retval);
2283 free_irq(LUBBOCK_USB_DISC_IRQ, dev);
2284 goto lubbock_fail0;
2285 }
David Brownellb2bbb202006-06-29 12:25:39 -07002286 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287#endif
David Brownellb2bbb202006-06-29 12:25:39 -07002288 if (vbus_irq) {
2289 retval = request_irq(vbus_irq, udc_vbus_irq,
Thomas Gleixner38515e92007-02-14 00:33:16 -08002290 IRQF_DISABLED | IRQF_SAMPLE_RANDOM,
David Brownellb2bbb202006-06-29 12:25:39 -07002291 driver_name, dev);
2292 if (retval != 0) {
2293 printk(KERN_ERR "%s: can't get irq %i, err %d\n",
2294 driver_name, vbus_irq, retval);
Milan Svoboda08a78cb2007-02-27 09:20:09 +00002295 free_irq(irq, dev);
Milan Svoboda9068a4c2007-06-30 06:25:35 -07002296 if (dev->mach->gpio_pullup)
2297 gpio_free(dev->mach->gpio_pullup);
2298 if (dev->mach->gpio_vbus)
2299 gpio_free(dev->mach->gpio_vbus);
David Brownellb2bbb202006-06-29 12:25:39 -07002300 return -EBUSY;
2301 }
2302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 create_proc_files();
2304
2305 return 0;
2306}
David Brownell91987692005-05-07 13:20:19 -07002307
Russell King3ae5eae2005-11-09 22:32:44 +00002308static void pxa2xx_udc_shutdown(struct platform_device *_dev)
David Brownell91987692005-05-07 13:20:19 -07002309{
2310 pullup_off();
2311}
2312
Russell King3ae5eae2005-11-09 22:32:44 +00002313static int __exit pxa2xx_udc_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314{
Russell King3ae5eae2005-11-09 22:32:44 +00002315 struct pxa2xx_udc *dev = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
David Brownell6bea4762006-12-05 03:15:33 -08002317 if (dev->driver)
2318 return -EBUSY;
2319
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 udc_disable(dev);
2321 remove_proc_files();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
2323 if (dev->got_irq) {
David Brownell34ebcd22007-02-24 12:23:52 -08002324 free_irq(platform_get_irq(pdev, 0), dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 dev->got_irq = 0;
2326 }
Milan Svoboda44df45a2006-05-29 03:34:00 -07002327#ifdef CONFIG_ARCH_LUBBOCK
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 if (machine_is_lubbock()) {
2329 free_irq(LUBBOCK_USB_DISC_IRQ, dev);
2330 free_irq(LUBBOCK_USB_IRQ, dev);
2331 }
Milan Svoboda44df45a2006-05-29 03:34:00 -07002332#endif
Milan Svoboda9068a4c2007-06-30 06:25:35 -07002333 if (dev->mach->gpio_vbus) {
2334 free_irq(gpio_to_irq(dev->mach->gpio_vbus), dev);
2335 gpio_free(dev->mach->gpio_vbus);
2336 }
2337 if (dev->mach->gpio_pullup)
2338 gpio_free(dev->mach->gpio_pullup);
2339
Russell King3ae5eae2005-11-09 22:32:44 +00002340 platform_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 the_controller = NULL;
2342 return 0;
2343}
2344
2345/*-------------------------------------------------------------------------*/
2346
2347#ifdef CONFIG_PM
2348
2349/* USB suspend (controlled by the host) and system suspend (controlled
2350 * by the PXA) don't necessarily work well together. If USB is active,
2351 * the 48 MHz clock is required; so the system can't enter 33 MHz idle
2352 * mode, or any deeper PM saving state.
2353 *
2354 * For now, we punt and forcibly disconnect from the USB host when PXA
2355 * enters any suspend state. While we're disconnected, we always disable
David Brownell34ebcd22007-02-24 12:23:52 -08002356 * the 48MHz USB clock ... allowing PXA sleep and/or 33 MHz idle states.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 * Boards without software pullup control shouldn't use those states.
2358 * VBUS IRQs should probably be ignored so that the PXA device just acts
2359 * "dead" to USB hosts until system resume.
2360 */
Russell King3ae5eae2005-11-09 22:32:44 +00002361static int pxa2xx_udc_suspend(struct platform_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362{
Russell King3ae5eae2005-11-09 22:32:44 +00002363 struct pxa2xx_udc *udc = platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364
Russell King9480e302005-10-28 09:52:56 -07002365 if (!udc->mach->udc_command)
2366 WARN("USB host won't detect disconnect!\n");
2367 pullup(udc, 0);
2368
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 return 0;
2370}
2371
Russell King3ae5eae2005-11-09 22:32:44 +00002372static int pxa2xx_udc_resume(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373{
Russell King3ae5eae2005-11-09 22:32:44 +00002374 struct pxa2xx_udc *udc = platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375
Russell King9480e302005-10-28 09:52:56 -07002376 pullup(udc, 1);
2377
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 return 0;
2379}
2380
2381#else
2382#define pxa2xx_udc_suspend NULL
2383#define pxa2xx_udc_resume NULL
2384#endif
2385
2386/*-------------------------------------------------------------------------*/
2387
Russell King3ae5eae2005-11-09 22:32:44 +00002388static struct platform_driver udc_driver = {
David Brownell91987692005-05-07 13:20:19 -07002389 .shutdown = pxa2xx_udc_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 .remove = __exit_p(pxa2xx_udc_remove),
2391 .suspend = pxa2xx_udc_suspend,
2392 .resume = pxa2xx_udc_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00002393 .driver = {
2394 .owner = THIS_MODULE,
2395 .name = "pxa2xx-udc",
2396 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397};
2398
2399static int __init udc_init(void)
2400{
2401 printk(KERN_INFO "%s: version %s\n", driver_name, DRIVER_VERSION);
David Brownell34ebcd22007-02-24 12:23:52 -08002402 return platform_driver_probe(&udc_driver, pxa2xx_udc_probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403}
2404module_init(udc_init);
2405
2406static void __exit udc_exit(void)
2407{
Russell King3ae5eae2005-11-09 22:32:44 +00002408 platform_driver_unregister(&udc_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409}
2410module_exit(udc_exit);
2411
2412MODULE_DESCRIPTION(DRIVER_DESC);
2413MODULE_AUTHOR("Frank Becker, Robert Schwebel, David Brownell");
2414MODULE_LICENSE("GPL");
2415