2
* linux/drivers/usb/gadget/lh7a40x_udc.c
3
* Sharp LH7A40x on-chip full speed USB device controllers
5
* Copyright (C) 2004 Mikko Lahteenmaki, Nordic ID
6
* Copyright (C) 2004 Bo Henriksen, Nordic ID
8
* This program is free software; you can redistribute it and/or modify
9
* it under the terms of the GNU General Public License as published by
10
* the Free Software Foundation; either version 2 of the License, or
11
* (at your option) any later version.
13
* This program is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
* GNU General Public License for more details.
18
* You should have received a copy of the GNU General Public License
19
* along with this program; if not, write to the Free Software
20
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24
#include <linux/platform_device.h>
25
#include <linux/slab.h>
27
#include "lh7a40x_udc.h"
29
//#define DEBUG printk
30
//#define DEBUG_EP0 printk
31
//#define DEBUG_SETUP printk
34
# define DEBUG_EP0(fmt,args...)
37
# define DEBUG_SETUP(fmt,args...)
41
# define DEBUG(fmt,args...)
44
#define DRIVER_DESC "LH7A40x USB Device Controller"
45
#define DRIVER_VERSION __DATE__
47
#ifndef _BIT /* FIXME - what happended to _BIT in 2.6.7bk18? */
48
#define _BIT(x) (1<<(x))
51
struct lh7a40x_udc *the_controller;
53
static const char driver_name[] = "lh7a40x_udc";
54
static const char driver_desc[] = DRIVER_DESC;
55
static const char ep0name[] = "ep0-control";
62
static char *state_names[] = {
65
"DATA_STATE_NEED_ZLP",
66
"WAIT_FOR_OUT_STATUS",
74
static int lh7a40x_ep_enable(struct usb_ep *ep,
75
const struct usb_endpoint_descriptor *);
76
static int lh7a40x_ep_disable(struct usb_ep *ep);
77
static struct usb_request *lh7a40x_alloc_request(struct usb_ep *ep, gfp_t);
78
static void lh7a40x_free_request(struct usb_ep *ep, struct usb_request *);
79
static int lh7a40x_queue(struct usb_ep *ep, struct usb_request *, gfp_t);
80
static int lh7a40x_dequeue(struct usb_ep *ep, struct usb_request *);
81
static int lh7a40x_set_halt(struct usb_ep *ep, int);
82
static int lh7a40x_fifo_status(struct usb_ep *ep);
83
static void lh7a40x_fifo_flush(struct usb_ep *ep);
84
static void lh7a40x_ep0_kick(struct lh7a40x_udc *dev, struct lh7a40x_ep *ep);
85
static void lh7a40x_handle_ep0(struct lh7a40x_udc *dev, u32 intr);
87
static void done(struct lh7a40x_ep *ep, struct lh7a40x_request *req,
89
static void pio_irq_enable(int bEndpointAddress);
90
static void pio_irq_disable(int bEndpointAddress);
91
static void stop_activity(struct lh7a40x_udc *dev,
92
struct usb_gadget_driver *driver);
93
static void flush(struct lh7a40x_ep *ep);
94
static void udc_enable(struct lh7a40x_udc *dev);
95
static void udc_set_address(struct lh7a40x_udc *dev, unsigned char address);
97
static struct usb_ep_ops lh7a40x_ep_ops = {
98
.enable = lh7a40x_ep_enable,
99
.disable = lh7a40x_ep_disable,
101
.alloc_request = lh7a40x_alloc_request,
102
.free_request = lh7a40x_free_request,
104
.queue = lh7a40x_queue,
105
.dequeue = lh7a40x_dequeue,
107
.set_halt = lh7a40x_set_halt,
108
.fifo_status = lh7a40x_fifo_status,
109
.fifo_flush = lh7a40x_fifo_flush,
114
static __inline__ int write_packet(struct lh7a40x_ep *ep,
115
struct lh7a40x_request *req, int max)
119
volatile u32 *fifo = (volatile u32 *)ep->fifo;
121
buf = req->req.buf + req->req.actual;
124
length = req->req.length - req->req.actual;
125
length = min(length, max);
126
req->req.actual += length;
128
DEBUG("Write %d (max %d), fifo %p\n", length, max, fifo);
138
static __inline__ void usb_set_index(u32 ep)
140
*(volatile u32 *)io_p2v(USB_INDEX) = ep;
143
static __inline__ u32 usb_read(u32 port)
145
return *(volatile u32 *)io_p2v(port);
148
static __inline__ void usb_write(u32 val, u32 port)
150
*(volatile u32 *)io_p2v(port) = val;
153
static __inline__ void usb_set(u32 val, u32 port)
155
volatile u32 *ioport = (volatile u32 *)io_p2v(port);
156
u32 after = (*ioport) | val;
160
static __inline__ void usb_clear(u32 val, u32 port)
162
volatile u32 *ioport = (volatile u32 *)io_p2v(port);
163
u32 after = (*ioport) & ~val;
167
/*-------------------------------------------------------------------------*/
169
#define GPIO_PORTC_DR (0x80000E08)
170
#define GPIO_PORTC_DDR (0x80000E18)
171
#define GPIO_PORTC_PDR (0x80000E70)
173
/* get port C pin data register */
174
#define get_portc_pdr(bit) ((usb_read(GPIO_PORTC_PDR) & _BIT(bit)) != 0)
175
/* get port C data direction register */
176
#define get_portc_ddr(bit) ((usb_read(GPIO_PORTC_DDR) & _BIT(bit)) != 0)
177
/* set port C data register */
178
#define set_portc_dr(bit, val) (val ? usb_set(_BIT(bit), GPIO_PORTC_DR) : usb_clear(_BIT(bit), GPIO_PORTC_DR))
179
/* set port C data direction register */
180
#define set_portc_ddr(bit, val) (val ? usb_set(_BIT(bit), GPIO_PORTC_DDR) : usb_clear(_BIT(bit), GPIO_PORTC_DDR))
184
* Port C bit 1 = USB Port 1 Power Enable
185
* Port C bit 2 = USB Port 1 Data Carrier Detect
187
#define is_usb_connected() get_portc_pdr(2)
189
#ifdef CONFIG_USB_GADGET_DEBUG_FILES
191
static const char proc_node_name[] = "driver/udc";
194
udc_proc_read(char *page, char **start, off_t off, int count,
195
int *eof, void *_dev)
198
struct lh7a40x_udc *dev = _dev;
200
unsigned size = count;
207
local_irq_save(flags);
209
/* basic device status */
210
t = scnprintf(next, size,
213
"Gadget driver: %s\n"
215
driver_name, DRIVER_VERSION,
216
dev->driver ? dev->driver->driver.name : "(none)",
217
is_usb_connected()? "full speed" : "disconnected");
221
t = scnprintf(next, size,
223
" Port C bit 1: %d, dir %d\n"
224
" Port C bit 2: %d, dir %d\n\n",
225
get_portc_pdr(1), get_portc_ddr(1),
226
get_portc_pdr(2), get_portc_ddr(2)
231
t = scnprintf(next, size,
232
"DCP pullup: %d\n\n",
233
(usb_read(USB_PM) & PM_USB_DCP) != 0);
237
local_irq_restore(flags);
242
#define create_proc_files() create_proc_read_entry(proc_node_name, 0, NULL, udc_proc_read, dev)
243
#define remove_proc_files() remove_proc_entry(proc_node_name, NULL)
245
#else /* !CONFIG_USB_GADGET_DEBUG_FILES */
247
#define create_proc_files() do {} while (0)
248
#define remove_proc_files() do {} while (0)
250
#endif /* CONFIG_USB_GADGET_DEBUG_FILES */
253
* udc_disable - disable USB device controller
255
static void udc_disable(struct lh7a40x_udc *dev)
257
DEBUG("%s, %p\n", __func__, dev);
259
udc_set_address(dev, 0);
261
/* Disable interrupts */
262
usb_write(0, USB_IN_INT_EN);
263
usb_write(0, USB_OUT_INT_EN);
264
usb_write(0, USB_INT_EN);
266
/* Disable the USB */
267
usb_write(0, USB_PM);
269
#ifdef CONFIG_ARCH_LH7A404
270
/* Disable USB power */
274
/* if hardware supports it, disconnect from usb */
275
/* make_usb_disappear(); */
277
dev->ep0state = WAIT_FOR_SETUP;
278
dev->gadget.speed = USB_SPEED_UNKNOWN;
279
dev->usb_address = 0;
283
* udc_reinit - initialize software state
285
static void udc_reinit(struct lh7a40x_udc *dev)
289
DEBUG("%s, %p\n", __func__, dev);
291
/* device/ep0 records init */
292
INIT_LIST_HEAD(&dev->gadget.ep_list);
293
INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
294
dev->ep0state = WAIT_FOR_SETUP;
296
/* basic endpoint records init */
297
for (i = 0; i < UDC_MAX_ENDPOINTS; i++) {
298
struct lh7a40x_ep *ep = &dev->ep[i];
301
list_add_tail(&ep->ep.ep_list, &dev->gadget.ep_list);
305
INIT_LIST_HEAD(&ep->queue);
309
/* the rest was statically initialized, and is read-only */
312
#define BYTES2MAXP(x) (x / 8)
313
#define MAXP2BYTES(x) (x * 8)
315
/* until it's enabled, this UDC should be completely invisible
318
static void udc_enable(struct lh7a40x_udc *dev)
322
DEBUG("%s, %p\n", __func__, dev);
324
dev->gadget.speed = USB_SPEED_UNKNOWN;
326
#ifdef CONFIG_ARCH_LH7A404
327
/* Set Port C bit 1 & 2 as output */
331
/* Enable USB power */
336
* C.f Chapter 18.1.3.1 Initializing the USB
339
/* Disable the USB */
340
usb_clear(PM_USB_ENABLE, USB_PM);
342
/* Reset APB & I/O sides of the USB */
343
usb_set(USB_RESET_APB | USB_RESET_IO, USB_RESET);
345
usb_clear(USB_RESET_APB | USB_RESET_IO, USB_RESET);
347
/* Set MAXP values for each */
348
for (ep = 0; ep < UDC_MAX_ENDPOINTS; ep++) {
349
struct lh7a40x_ep *ep_reg = &dev->ep[ep];
354
switch (ep_reg->ep_type) {
357
usb_clear(USB_IN_CSR2_USB_DMA_EN | USB_IN_CSR2_AUTO_SET,
361
usb_write(BYTES2MAXP(ep_maxpacket(ep_reg)),
365
usb_clear(USB_OUT_CSR2_USB_DMA_EN |
366
USB_OUT_CSR2_AUTO_CLR, ep_reg->csr2);
367
usb_write(BYTES2MAXP(ep_maxpacket(ep_reg)),
372
/* Read & Write CSR1, just in case */
373
csr = usb_read(ep_reg->csr1);
374
usb_write(csr, ep_reg->csr1);
379
/* Disable interrupts */
380
usb_write(0, USB_IN_INT_EN);
381
usb_write(0, USB_OUT_INT_EN);
382
usb_write(0, USB_INT_EN);
384
/* Enable interrupts */
385
usb_set(USB_IN_INT_EP0, USB_IN_INT_EN);
386
usb_set(USB_INT_RESET_INT | USB_INT_RESUME_INT, USB_INT_EN);
387
/* Dont enable rest of the interrupts */
388
/* usb_set(USB_IN_INT_EP3 | USB_IN_INT_EP1 | USB_IN_INT_EP0, USB_IN_INT_EN);
389
usb_set(USB_OUT_INT_EP2, USB_OUT_INT_EN); */
392
usb_set(PM_ENABLE_SUSPEND, USB_PM);
395
usb_set(PM_USB_ENABLE, USB_PM);
397
#ifdef CONFIG_ARCH_LH7A404
398
/* NOTE: DOES NOT WORK! */
399
/* Let host detect UDC:
400
* Software must write a 0 to the PMR:DCP_CTRL bit to turn this
401
* transistor on and pull the USBDP pin HIGH.
403
/* usb_clear(PM_USB_DCP, USB_PM);
404
usb_set(PM_USB_DCP, USB_PM); */
409
Register entry point for the peripheral controller driver.
411
int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
412
int (*bind)(struct usb_gadget *))
414
struct lh7a40x_udc *dev = the_controller;
417
DEBUG("%s: %s\n", __func__, driver->driver.name);
420
|| driver->speed != USB_SPEED_FULL
422
|| !driver->disconnect
430
/* first hook up the driver ... */
431
dev->driver = driver;
432
dev->gadget.dev.driver = &driver->driver;
434
device_add(&dev->gadget.dev);
435
retval = bind(&dev->gadget);
437
printk(KERN_WARNING "%s: bind to driver %s --> error %d\n",
438
dev->gadget.name, driver->driver.name, retval);
439
device_del(&dev->gadget.dev);
442
dev->gadget.dev.driver = 0;
446
/* ... then enable host detection and ep0; and we're ready
447
* for set_configuration as well as eventual disconnect.
448
* NOTE: this shouldn't power up until later.
450
printk(KERN_WARNING "%s: registered gadget driver '%s'\n",
451
dev->gadget.name, driver->driver.name);
457
EXPORT_SYMBOL(usb_gadget_probe_driver);
460
Unregister entry point for the peripheral controller driver.
462
int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
464
struct lh7a40x_udc *dev = the_controller;
469
if (!driver || driver != dev->driver || !driver->unbind)
472
spin_lock_irqsave(&dev->lock, flags);
474
stop_activity(dev, driver);
475
spin_unlock_irqrestore(&dev->lock, flags);
477
driver->unbind(&dev->gadget);
478
dev->gadget.dev.driver = NULL;
479
device_del(&dev->gadget.dev);
483
DEBUG("unregistered gadget driver '%s'\n", driver->driver.name);
487
EXPORT_SYMBOL(usb_gadget_unregister_driver);
489
/*-------------------------------------------------------------------------*/
491
/** Write request to FIFO (max write == maxp size)
492
* Return: 0 = still running, 1 = completed, negative = errno
493
* NOTE: INDEX register must be set for EP
495
static int write_fifo(struct lh7a40x_ep *ep, struct lh7a40x_request *req)
500
max = le16_to_cpu(ep->desc->wMaxPacketSize);
502
csr = usb_read(ep->csr1);
503
DEBUG("CSR: %x %d\n", csr, csr & USB_IN_CSR1_FIFO_NOT_EMPTY);
505
if (!(csr & USB_IN_CSR1_FIFO_NOT_EMPTY)) {
507
int is_last, is_short;
509
count = write_packet(ep, req, max);
510
usb_set(USB_IN_CSR1_IN_PKT_RDY, ep->csr1);
512
/* last packet is usually short (or a zlp) */
513
if (unlikely(count != max))
514
is_last = is_short = 1;
516
if (likely(req->req.length != req->req.actual)
521
/* interrupt/iso maxpacket may not fill the fifo */
522
is_short = unlikely(max < ep_maxpacket(ep));
525
DEBUG("%s: wrote %s %d bytes%s%s %d left %p\n", __func__,
527
is_last ? "/L" : "", is_short ? "/S" : "",
528
req->req.length - req->req.actual, req);
530
/* requests complete when all IN data is in the FIFO */
533
if (list_empty(&ep->queue)) {
534
pio_irq_disable(ep_index(ep));
539
DEBUG("Hmm.. %d ep FIFO is not empty!\n", ep_index(ep));
545
/** Read to request from FIFO (max read == bytes in fifo)
546
* Return: 0 = still running, 1 = completed, negative = errno
547
* NOTE: INDEX register must be set for EP
549
static int read_fifo(struct lh7a40x_ep *ep, struct lh7a40x_request *req)
553
unsigned bufferspace, count, is_short;
554
volatile u32 *fifo = (volatile u32 *)ep->fifo;
556
/* make sure there's a packet in the FIFO. */
557
csr = usb_read(ep->csr1);
558
if (!(csr & USB_OUT_CSR1_OUT_PKT_RDY)) {
559
DEBUG("%s: Packet NOT ready!\n", __func__);
563
buf = req->req.buf + req->req.actual;
565
bufferspace = req->req.length - req->req.actual;
567
/* read all bytes from this packet */
568
count = usb_read(USB_OUT_FIFO_WC1);
569
req->req.actual += min(count, bufferspace);
571
is_short = (count < ep->ep.maxpacket);
572
DEBUG("read %s %02x, %d bytes%s req %p %d/%d\n",
573
ep->ep.name, csr, count,
574
is_short ? "/S" : "", req, req->req.actual, req->req.length);
576
while (likely(count-- != 0)) {
577
u8 byte = (u8) (*fifo & 0xff);
579
if (unlikely(bufferspace == 0)) {
580
/* this happens when the driver's buffer
581
* is smaller than what the host sent.
582
* discard the extra data.
584
if (req->req.status != -EOVERFLOW)
585
printk(KERN_WARNING "%s overflow %d\n",
587
req->req.status = -EOVERFLOW;
594
usb_clear(USB_OUT_CSR1_OUT_PKT_RDY, ep->csr1);
597
if (is_short || req->req.actual == req->req.length) {
599
usb_set(USB_OUT_CSR1_FIFO_FLUSH, ep->csr1);
601
if (list_empty(&ep->queue))
602
pio_irq_disable(ep_index(ep));
606
/* finished that packet. the next one may be waiting... */
611
* done - retire a request; caller blocked irqs
612
* INDEX register is preserved to keep same
614
static void done(struct lh7a40x_ep *ep, struct lh7a40x_request *req, int status)
616
unsigned int stopped = ep->stopped;
619
DEBUG("%s, %p\n", __func__, ep);
620
list_del_init(&req->queue);
622
if (likely(req->req.status == -EINPROGRESS))
623
req->req.status = status;
625
status = req->req.status;
627
if (status && status != -ESHUTDOWN)
628
DEBUG("complete %s req %p stat %d len %u/%u\n",
629
ep->ep.name, &req->req, status,
630
req->req.actual, req->req.length);
632
/* don't modify queue heads during completion callback */
634
/* Read current index (completion may modify it) */
635
index = usb_read(USB_INDEX);
637
spin_unlock(&ep->dev->lock);
638
req->req.complete(&ep->ep, &req->req);
639
spin_lock(&ep->dev->lock);
642
usb_set_index(index);
643
ep->stopped = stopped;
646
/** Enable EP interrupt */
647
static void pio_irq_enable(int ep)
649
DEBUG("%s: %d\n", __func__, ep);
653
usb_set(USB_IN_INT_EP1, USB_IN_INT_EN);
656
usb_set(USB_OUT_INT_EP2, USB_OUT_INT_EN);
659
usb_set(USB_IN_INT_EP3, USB_IN_INT_EN);
662
DEBUG("Unknown endpoint: %d\n", ep);
667
/** Disable EP interrupt */
668
static void pio_irq_disable(int ep)
670
DEBUG("%s: %d\n", __func__, ep);
674
usb_clear(USB_IN_INT_EP1, USB_IN_INT_EN);
677
usb_clear(USB_OUT_INT_EP2, USB_OUT_INT_EN);
680
usb_clear(USB_IN_INT_EP3, USB_IN_INT_EN);
683
DEBUG("Unknown endpoint: %d\n", ep);
689
* nuke - dequeue ALL requests
691
void nuke(struct lh7a40x_ep *ep, int status)
693
struct lh7a40x_request *req;
695
DEBUG("%s, %p\n", __func__, ep);
700
/* called with irqs blocked */
701
while (!list_empty(&ep->queue)) {
702
req = list_entry(ep->queue.next, struct lh7a40x_request, queue);
703
done(ep, req, status);
706
/* Disable IRQ if EP is enabled (has descriptor) */
708
pio_irq_disable(ep_index(ep));
712
void nuke_all(struct lh7a40x_udc *dev)
715
for(n=0; n<UDC_MAX_ENDPOINTS; n++) {
716
struct lh7a40x_ep *ep = &dev->ep[n];
723
static void flush_all(struct lh7a40x_udc *dev)
726
for (n = 0; n < UDC_MAX_ENDPOINTS; n++)
728
struct lh7a40x_ep *ep = &dev->ep[n];
735
* NOTE: INDEX register must be set before this call
737
static void flush(struct lh7a40x_ep *ep)
739
DEBUG("%s, %p\n", __func__, ep);
741
switch (ep->ep_type) {
743
/* check, by implication c.f. 15.1.2.11 */
748
/* if(csr & USB_IN_CSR1_IN_PKT_RDY) */
749
usb_set(USB_IN_CSR1_FIFO_FLUSH, ep->csr1);
753
/* if(csr & USB_OUT_CSR1_OUT_PKT_RDY) */
754
usb_set(USB_OUT_CSR1_FIFO_FLUSH, ep->csr1);
760
* lh7a40x_in_epn - handle IN interrupt
762
static void lh7a40x_in_epn(struct lh7a40x_udc *dev, u32 ep_idx, u32 intr)
765
struct lh7a40x_ep *ep = &dev->ep[ep_idx];
766
struct lh7a40x_request *req;
768
usb_set_index(ep_idx);
770
csr = usb_read(ep->csr1);
771
DEBUG("%s: %d, csr %x\n", __func__, ep_idx, csr);
773
if (csr & USB_IN_CSR1_SENT_STALL) {
774
DEBUG("USB_IN_CSR1_SENT_STALL\n");
775
usb_set(USB_IN_CSR1_SENT_STALL /*|USB_IN_CSR1_SEND_STALL */ ,
781
DEBUG("%s: NO EP DESC\n", __func__);
785
if (list_empty(&ep->queue))
788
req = list_entry(ep->queue.next, struct lh7a40x_request, queue);
790
DEBUG("req: %p\n", req);
798
/* ********************************************************************************************* */
802
static void lh7a40x_out_epn(struct lh7a40x_udc *dev, u32 ep_idx, u32 intr)
804
struct lh7a40x_ep *ep = &dev->ep[ep_idx];
805
struct lh7a40x_request *req;
807
DEBUG("%s: %d\n", __func__, ep_idx);
809
usb_set_index(ep_idx);
813
csr = usb_read(ep->csr1);
817
csr1)) & (USB_OUT_CSR1_OUT_PKT_RDY |
818
USB_OUT_CSR1_SENT_STALL)) {
819
DEBUG("%s: %x\n", __func__, csr);
821
if (csr & USB_OUT_CSR1_SENT_STALL) {
822
DEBUG("%s: stall sent, flush fifo\n",
824
/* usb_set(USB_OUT_CSR1_FIFO_FLUSH, ep->csr1); */
826
} else if (csr & USB_OUT_CSR1_OUT_PKT_RDY) {
827
if (list_empty(&ep->queue))
831
list_entry(ep->queue.next,
832
struct lh7a40x_request,
849
/* Throw packet away.. */
850
printk(KERN_WARNING "%s: No descriptor?!?\n", __func__);
855
static void stop_activity(struct lh7a40x_udc *dev,
856
struct usb_gadget_driver *driver)
860
/* don't disconnect drivers more than once */
861
if (dev->gadget.speed == USB_SPEED_UNKNOWN)
863
dev->gadget.speed = USB_SPEED_UNKNOWN;
865
/* prevent new request submissions, kill any outstanding requests */
866
for (i = 0; i < UDC_MAX_ENDPOINTS; i++) {
867
struct lh7a40x_ep *ep = &dev->ep[i];
871
nuke(ep, -ESHUTDOWN);
874
/* report disconnect; the driver is already quiesced */
876
spin_unlock(&dev->lock);
877
driver->disconnect(&dev->gadget);
878
spin_lock(&dev->lock);
881
/* re-init driver-visible data structures */
885
/** Handle USB RESET interrupt
887
static void lh7a40x_reset_intr(struct lh7a40x_udc *dev)
889
#if 0 /* def CONFIG_ARCH_LH7A404 */
890
/* Does not work always... */
892
DEBUG("%s: %d\n", __func__, dev->usb_address);
894
if (!dev->usb_address) {
895
/*usb_set(USB_RESET_IO, USB_RESET);
897
usb_clear(USB_RESET_IO, USB_RESET); */
900
/* Put the USB controller into reset. */
901
usb_set(USB_RESET_IO, USB_RESET);
903
/* Set Device ID to 0 */
904
udc_set_address(dev, 0);
906
/* Let PLL2 settle down */
909
/* Release the USB controller from reset */
910
usb_clear(USB_RESET_IO, USB_RESET);
916
dev->gadget.speed = USB_SPEED_FULL;
920
* lh7a40x usb client interrupt handler.
922
static irqreturn_t lh7a40x_udc_irq(int irq, void *_dev)
924
struct lh7a40x_udc *dev = _dev;
928
spin_lock(&dev->lock);
931
u32 intr_in = usb_read(USB_IN_INT);
932
u32 intr_out = usb_read(USB_OUT_INT);
933
u32 intr_int = usb_read(USB_INT);
935
/* Test also against enable bits.. (lh7a40x errata).. Sigh.. */
936
u32 in_en = usb_read(USB_IN_INT_EN);
937
u32 out_en = usb_read(USB_OUT_INT_EN);
939
if (!intr_out && !intr_in && !intr_int)
942
DEBUG("%s (on state %s)\n", __func__,
943
state_names[dev->ep0state]);
944
DEBUG("intr_out = %x\n", intr_out);
945
DEBUG("intr_in = %x\n", intr_in);
946
DEBUG("intr_int = %x\n", intr_int);
949
usb_write(intr_in, USB_IN_INT);
951
if ((intr_in & USB_IN_INT_EP1)
952
&& (in_en & USB_IN_INT_EP1)) {
953
DEBUG("USB_IN_INT_EP1\n");
954
lh7a40x_in_epn(dev, 1, intr_in);
956
if ((intr_in & USB_IN_INT_EP3)
957
&& (in_en & USB_IN_INT_EP3)) {
958
DEBUG("USB_IN_INT_EP3\n");
959
lh7a40x_in_epn(dev, 3, intr_in);
961
if (intr_in & USB_IN_INT_EP0) {
962
DEBUG("USB_IN_INT_EP0 (control)\n");
963
lh7a40x_handle_ep0(dev, intr_in);
968
usb_write(intr_out, USB_OUT_INT);
970
if ((intr_out & USB_OUT_INT_EP2)
971
&& (out_en & USB_OUT_INT_EP2)) {
972
DEBUG("USB_OUT_INT_EP2\n");
973
lh7a40x_out_epn(dev, 2, intr_out);
978
usb_write(intr_int, USB_INT);
980
if (intr_int & USB_INT_RESET_INT) {
981
lh7a40x_reset_intr(dev);
984
if (intr_int & USB_INT_RESUME_INT) {
985
DEBUG("USB resume\n");
987
if (dev->gadget.speed != USB_SPEED_UNKNOWN
989
&& dev->driver->resume
990
&& is_usb_connected()) {
991
dev->driver->resume(&dev->gadget);
995
if (intr_int & USB_INT_SUSPEND_INT) {
996
DEBUG("USB suspend%s\n",
997
is_usb_connected()? "" : "+disconnect");
998
if (!is_usb_connected()) {
999
stop_activity(dev, dev->driver);
1000
} else if (dev->gadget.speed !=
1001
USB_SPEED_UNKNOWN && dev->driver
1002
&& dev->driver->suspend) {
1003
dev->driver->suspend(&dev->gadget);
1010
spin_unlock(&dev->lock);
1015
static int lh7a40x_ep_enable(struct usb_ep *_ep,
1016
const struct usb_endpoint_descriptor *desc)
1018
struct lh7a40x_ep *ep;
1019
struct lh7a40x_udc *dev;
1020
unsigned long flags;
1022
DEBUG("%s, %p\n", __func__, _ep);
1024
ep = container_of(_ep, struct lh7a40x_ep, ep);
1025
if (!_ep || !desc || ep->desc || _ep->name == ep0name
1026
|| desc->bDescriptorType != USB_DT_ENDPOINT
1027
|| ep->bEndpointAddress != desc->bEndpointAddress
1028
|| ep_maxpacket(ep) < le16_to_cpu(desc->wMaxPacketSize)) {
1029
DEBUG("%s, bad ep or descriptor\n", __func__);
1033
/* xfer types must match, except that interrupt ~= bulk */
1034
if (ep->bmAttributes != desc->bmAttributes
1035
&& ep->bmAttributes != USB_ENDPOINT_XFER_BULK
1036
&& desc->bmAttributes != USB_ENDPOINT_XFER_INT) {
1037
DEBUG("%s, %s type mismatch\n", __func__, _ep->name);
1041
/* hardware _could_ do smaller, but driver doesn't */
1042
if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK
1043
&& le16_to_cpu(desc->wMaxPacketSize) != ep_maxpacket(ep))
1044
|| !desc->wMaxPacketSize) {
1045
DEBUG("%s, bad %s maxpacket\n", __func__, _ep->name);
1050
if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
1051
DEBUG("%s, bogus device state\n", __func__);
1055
spin_lock_irqsave(&ep->dev->lock, flags);
1060
ep->ep.maxpacket = le16_to_cpu(desc->wMaxPacketSize);
1062
spin_unlock_irqrestore(&ep->dev->lock, flags);
1064
/* Reset halt state (does flush) */
1065
lh7a40x_set_halt(_ep, 0);
1067
DEBUG("%s: enabled %s\n", __func__, _ep->name);
1072
* NOTE: Sets INDEX register
1074
static int lh7a40x_ep_disable(struct usb_ep *_ep)
1076
struct lh7a40x_ep *ep;
1077
unsigned long flags;
1079
DEBUG("%s, %p\n", __func__, _ep);
1081
ep = container_of(_ep, struct lh7a40x_ep, ep);
1082
if (!_ep || !ep->desc) {
1083
DEBUG("%s, %s not enabled\n", __func__,
1084
_ep ? ep->ep.name : NULL);
1088
spin_lock_irqsave(&ep->dev->lock, flags);
1090
usb_set_index(ep_index(ep));
1092
/* Nuke all pending requests (does flush) */
1093
nuke(ep, -ESHUTDOWN);
1095
/* Disable ep IRQ */
1096
pio_irq_disable(ep_index(ep));
1101
spin_unlock_irqrestore(&ep->dev->lock, flags);
1103
DEBUG("%s: disabled %s\n", __func__, _ep->name);
1107
static struct usb_request *lh7a40x_alloc_request(struct usb_ep *ep,
1110
struct lh7a40x_request *req;
1112
DEBUG("%s, %p\n", __func__, ep);
1114
req = kzalloc(sizeof(*req), gfp_flags);
1118
INIT_LIST_HEAD(&req->queue);
1123
static void lh7a40x_free_request(struct usb_ep *ep, struct usb_request *_req)
1125
struct lh7a40x_request *req;
1127
DEBUG("%s, %p\n", __func__, ep);
1129
req = container_of(_req, struct lh7a40x_request, req);
1130
WARN_ON(!list_empty(&req->queue));
1134
/** Queue one request
1135
* Kickstart transfer if needed
1136
* NOTE: Sets INDEX register
1138
static int lh7a40x_queue(struct usb_ep *_ep, struct usb_request *_req,
1141
struct lh7a40x_request *req;
1142
struct lh7a40x_ep *ep;
1143
struct lh7a40x_udc *dev;
1144
unsigned long flags;
1146
DEBUG("\n\n\n%s, %p\n", __func__, _ep);
1148
req = container_of(_req, struct lh7a40x_request, req);
1150
(!_req || !_req->complete || !_req->buf
1151
|| !list_empty(&req->queue))) {
1152
DEBUG("%s, bad params\n", __func__);
1156
ep = container_of(_ep, struct lh7a40x_ep, ep);
1157
if (unlikely(!_ep || (!ep->desc && ep->ep.name != ep0name))) {
1158
DEBUG("%s, bad ep\n", __func__);
1163
if (unlikely(!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)) {
1164
DEBUG("%s, bogus device state %p\n", __func__, dev->driver);
1168
DEBUG("%s queue req %p, len %d buf %p\n", _ep->name, _req, _req->length,
1171
spin_lock_irqsave(&dev->lock, flags);
1173
_req->status = -EINPROGRESS;
1176
/* kickstart this i/o queue? */
1177
DEBUG("Add to %d Q %d %d\n", ep_index(ep), list_empty(&ep->queue),
1179
if (list_empty(&ep->queue) && likely(!ep->stopped)) {
1182
if (unlikely(ep_index(ep) == 0)) {
1184
list_add_tail(&req->queue, &ep->queue);
1185
lh7a40x_ep0_kick(dev, ep);
1187
} else if (ep_is_in(ep)) {
1189
usb_set_index(ep_index(ep));
1190
csr = usb_read(ep->csr1);
1191
pio_irq_enable(ep_index(ep));
1192
if ((csr & USB_IN_CSR1_FIFO_NOT_EMPTY) == 0) {
1193
if (write_fifo(ep, req) == 1)
1198
usb_set_index(ep_index(ep));
1199
csr = usb_read(ep->csr1);
1200
pio_irq_enable(ep_index(ep));
1201
if (!(csr & USB_OUT_CSR1_FIFO_FULL)) {
1202
if (read_fifo(ep, req) == 1)
1208
/* pio or dma irq handler advances the queue. */
1209
if (likely(req != 0))
1210
list_add_tail(&req->queue, &ep->queue);
1212
spin_unlock_irqrestore(&dev->lock, flags);
1217
/* dequeue JUST ONE request */
1218
static int lh7a40x_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1220
struct lh7a40x_ep *ep;
1221
struct lh7a40x_request *req;
1222
unsigned long flags;
1224
DEBUG("%s, %p\n", __func__, _ep);
1226
ep = container_of(_ep, struct lh7a40x_ep, ep);
1227
if (!_ep || ep->ep.name == ep0name)
1230
spin_lock_irqsave(&ep->dev->lock, flags);
1232
/* make sure it's actually queued on this endpoint */
1233
list_for_each_entry(req, &ep->queue, queue) {
1234
if (&req->req == _req)
1237
if (&req->req != _req) {
1238
spin_unlock_irqrestore(&ep->dev->lock, flags);
1242
done(ep, req, -ECONNRESET);
1244
spin_unlock_irqrestore(&ep->dev->lock, flags);
1248
/** Halt specific EP
1249
* Return 0 if success
1250
* NOTE: Sets INDEX register to EP !
1252
static int lh7a40x_set_halt(struct usb_ep *_ep, int value)
1254
struct lh7a40x_ep *ep;
1255
unsigned long flags;
1257
ep = container_of(_ep, struct lh7a40x_ep, ep);
1258
if (unlikely(!_ep || (!ep->desc && ep->ep.name != ep0name))) {
1259
DEBUG("%s, bad ep\n", __func__);
1263
usb_set_index(ep_index(ep));
1265
DEBUG("%s, ep %d, val %d\n", __func__, ep_index(ep), value);
1267
spin_lock_irqsave(&ep->dev->lock, flags);
1269
if (ep_index(ep) == 0) {
1271
usb_set(EP0_SEND_STALL, ep->csr1);
1272
} else if (ep_is_in(ep)) {
1273
u32 csr = usb_read(ep->csr1);
1274
if (value && ((csr & USB_IN_CSR1_FIFO_NOT_EMPTY)
1275
|| !list_empty(&ep->queue))) {
1277
* Attempts to halt IN endpoints will fail (returning -EAGAIN)
1278
* if any transfer requests are still queued, or if the controller
1279
* FIFO still holds bytes that the host hasn't collected.
1281
spin_unlock_irqrestore(&ep->dev->lock, flags);
1283
("Attempt to halt IN endpoint failed (returning -EAGAIN) %d %d\n",
1284
(csr & USB_IN_CSR1_FIFO_NOT_EMPTY),
1285
!list_empty(&ep->queue));
1290
usb_set(USB_IN_CSR1_SEND_STALL, ep->csr1);
1292
usb_clear(USB_IN_CSR1_SEND_STALL, ep->csr1);
1293
usb_set(USB_IN_CSR1_CLR_DATA_TOGGLE, ep->csr1);
1300
usb_set(USB_OUT_CSR1_SEND_STALL, ep->csr1);
1302
usb_clear(USB_OUT_CSR1_SEND_STALL, ep->csr1);
1303
usb_set(USB_OUT_CSR1_CLR_DATA_REG, ep->csr1);
1313
spin_unlock_irqrestore(&ep->dev->lock, flags);
1315
DEBUG("%s %s halted\n", _ep->name, value == 0 ? "NOT" : "IS");
1320
/** Return bytes in EP FIFO
1321
* NOTE: Sets INDEX register to EP
1323
static int lh7a40x_fifo_status(struct usb_ep *_ep)
1327
struct lh7a40x_ep *ep;
1329
ep = container_of(_ep, struct lh7a40x_ep, ep);
1331
DEBUG("%s, bad ep\n", __func__);
1335
DEBUG("%s, %d\n", __func__, ep_index(ep));
1337
/* LPD can't report unclaimed bytes from IN fifos */
1341
usb_set_index(ep_index(ep));
1343
csr = usb_read(ep->csr1);
1344
if (ep->dev->gadget.speed != USB_SPEED_UNKNOWN ||
1345
csr & USB_OUT_CSR1_OUT_PKT_RDY) {
1346
count = usb_read(USB_OUT_FIFO_WC1);
1353
* NOTE: Sets INDEX register to EP
1355
static void lh7a40x_fifo_flush(struct usb_ep *_ep)
1357
struct lh7a40x_ep *ep;
1359
ep = container_of(_ep, struct lh7a40x_ep, ep);
1360
if (unlikely(!_ep || (!ep->desc && ep->ep.name != ep0name))) {
1361
DEBUG("%s, bad ep\n", __func__);
1365
usb_set_index(ep_index(ep));
1369
/****************************************************************/
1370
/* End Point 0 related functions */
1371
/****************************************************************/
1373
/* return: 0 = still running, 1 = completed, negative = errno */
1374
static int write_fifo_ep0(struct lh7a40x_ep *ep, struct lh7a40x_request *req)
1380
max = ep_maxpacket(ep);
1382
DEBUG_EP0("%s\n", __func__);
1384
count = write_packet(ep, req, max);
1386
/* last packet is usually short (or a zlp) */
1387
if (unlikely(count != max))
1390
if (likely(req->req.length != req->req.actual) || req->req.zero)
1396
DEBUG_EP0("%s: wrote %s %d bytes%s %d left %p\n", __func__,
1398
is_last ? "/L" : "", req->req.length - req->req.actual, req);
1400
/* requests complete when all IN data is in the FIFO */
1409
static __inline__ int lh7a40x_fifo_read(struct lh7a40x_ep *ep,
1410
unsigned char *cp, int max)
1413
int count = usb_read(USB_OUT_FIFO_WC1);
1414
volatile u32 *fifo = (volatile u32 *)ep->fifo;
1420
*cp++ = *fifo & 0xFF;
1424
static __inline__ void lh7a40x_fifo_write(struct lh7a40x_ep *ep,
1425
unsigned char *cp, int count)
1427
volatile u32 *fifo = (volatile u32 *)ep->fifo;
1428
DEBUG_EP0("fifo_write: %d %d\n", ep_index(ep), count);
1433
static int read_fifo_ep0(struct lh7a40x_ep *ep, struct lh7a40x_request *req)
1437
unsigned bufferspace, count, is_short;
1438
volatile u32 *fifo = (volatile u32 *)ep->fifo;
1440
DEBUG_EP0("%s\n", __func__);
1442
csr = usb_read(USB_EP0_CSR);
1443
if (!(csr & USB_OUT_CSR1_OUT_PKT_RDY))
1446
buf = req->req.buf + req->req.actual;
1448
bufferspace = req->req.length - req->req.actual;
1450
/* read all bytes from this packet */
1451
if (likely(csr & EP0_OUT_PKT_RDY)) {
1452
count = usb_read(USB_OUT_FIFO_WC1);
1453
req->req.actual += min(count, bufferspace);
1457
is_short = (count < ep->ep.maxpacket);
1458
DEBUG_EP0("read %s %02x, %d bytes%s req %p %d/%d\n",
1459
ep->ep.name, csr, count,
1460
is_short ? "/S" : "", req, req->req.actual, req->req.length);
1462
while (likely(count-- != 0)) {
1463
u8 byte = (u8) (*fifo & 0xff);
1465
if (unlikely(bufferspace == 0)) {
1466
/* this happens when the driver's buffer
1467
* is smaller than what the host sent.
1468
* discard the extra data.
1470
if (req->req.status != -EOVERFLOW)
1471
DEBUG_EP0("%s overflow %d\n", ep->ep.name,
1473
req->req.status = -EOVERFLOW;
1481
if (is_short || req->req.actual == req->req.length) {
1486
/* finished that packet. the next one may be waiting... */
1491
* udc_set_address - set the USB address for this device
1494
* Called from control endpoint function after it decodes a set address setup packet.
1496
static void udc_set_address(struct lh7a40x_udc *dev, unsigned char address)
1498
DEBUG_EP0("%s: %d\n", __func__, address);
1499
/* c.f. 15.1.2.2 Table 15-4 address will be used after DATA_END is set */
1500
dev->usb_address = address;
1501
usb_set((address & USB_FA_FUNCTION_ADDR), USB_FA);
1502
usb_set(USB_FA_ADDR_UPDATE | (address & USB_FA_FUNCTION_ADDR), USB_FA);
1503
/* usb_read(USB_FA); */
1507
* DATA_STATE_RECV (OUT_PKT_RDY)
1509
* set EP0_CLR_OUT | EP0_DATA_END | EP0_SEND_STALL bits
1511
* set EP0_CLR_OUT bit
1512
if last set EP0_DATA_END bit
1514
static void lh7a40x_ep0_out(struct lh7a40x_udc *dev, u32 csr)
1516
struct lh7a40x_request *req;
1517
struct lh7a40x_ep *ep = &dev->ep[0];
1520
DEBUG_EP0("%s: %x\n", __func__, csr);
1522
if (list_empty(&ep->queue))
1525
req = list_entry(ep->queue.next, struct lh7a40x_request, queue);
1529
if (req->req.length == 0) {
1530
DEBUG_EP0("ZERO LENGTH OUT!\n");
1531
usb_set((EP0_CLR_OUT | EP0_DATA_END), USB_EP0_CSR);
1532
dev->ep0state = WAIT_FOR_SETUP;
1535
ret = read_fifo_ep0(ep, req);
1538
DEBUG_EP0("%s: finished, waiting for status\n",
1541
usb_set((EP0_CLR_OUT | EP0_DATA_END), USB_EP0_CSR);
1542
dev->ep0state = WAIT_FOR_SETUP;
1544
/* Not done yet.. */
1545
DEBUG_EP0("%s: not finished\n", __func__);
1546
usb_set(EP0_CLR_OUT, USB_EP0_CSR);
1549
DEBUG_EP0("NO REQ??!\n");
1556
static int lh7a40x_ep0_in(struct lh7a40x_udc *dev, u32 csr)
1558
struct lh7a40x_request *req;
1559
struct lh7a40x_ep *ep = &dev->ep[0];
1560
int ret, need_zlp = 0;
1562
DEBUG_EP0("%s: %x\n", __func__, csr);
1564
if (list_empty(&ep->queue))
1567
req = list_entry(ep->queue.next, struct lh7a40x_request, queue);
1570
DEBUG_EP0("%s: NULL REQ\n", __func__);
1574
if (req->req.length == 0) {
1576
usb_set((EP0_IN_PKT_RDY | EP0_DATA_END), USB_EP0_CSR);
1577
dev->ep0state = WAIT_FOR_SETUP;
1581
if (req->req.length - req->req.actual == EP0_PACKETSIZE) {
1582
/* Next write will end with the packet size, */
1583
/* so we need Zero-length-packet */
1587
ret = write_fifo_ep0(ep, req);
1589
if (ret == 1 && !need_zlp) {
1591
DEBUG_EP0("%s: finished, waiting for status\n", __func__);
1593
usb_set((EP0_IN_PKT_RDY | EP0_DATA_END), USB_EP0_CSR);
1594
dev->ep0state = WAIT_FOR_SETUP;
1596
DEBUG_EP0("%s: not finished\n", __func__);
1597
usb_set(EP0_IN_PKT_RDY, USB_EP0_CSR);
1601
DEBUG_EP0("%s: Need ZLP!\n", __func__);
1602
usb_set(EP0_IN_PKT_RDY, USB_EP0_CSR);
1603
dev->ep0state = DATA_STATE_NEED_ZLP;
1609
static int lh7a40x_handle_get_status(struct lh7a40x_udc *dev,
1610
struct usb_ctrlrequest *ctrl)
1612
struct lh7a40x_ep *ep0 = &dev->ep[0];
1613
struct lh7a40x_ep *qep;
1614
int reqtype = (ctrl->bRequestType & USB_RECIP_MASK);
1617
if (reqtype == USB_RECIP_INTERFACE) {
1618
/* This is not supported.
1619
* And according to the USB spec, this one does nothing..
1622
DEBUG_SETUP("GET_STATUS: USB_RECIP_INTERFACE\n");
1623
} else if (reqtype == USB_RECIP_DEVICE) {
1624
DEBUG_SETUP("GET_STATUS: USB_RECIP_DEVICE\n");
1625
val |= (1 << 0); /* Self powered */
1626
/*val |= (1<<1); *//* Remote wakeup */
1627
} else if (reqtype == USB_RECIP_ENDPOINT) {
1628
int ep_num = (ctrl->wIndex & ~USB_DIR_IN);
1631
("GET_STATUS: USB_RECIP_ENDPOINT (%d), ctrl->wLength = %d\n",
1632
ep_num, ctrl->wLength);
1634
if (ctrl->wLength > 2 || ep_num > 3)
1637
qep = &dev->ep[ep_num];
1638
if (ep_is_in(qep) != ((ctrl->wIndex & USB_DIR_IN) ? 1 : 0)
1639
&& ep_index(qep) != 0) {
1643
usb_set_index(ep_index(qep));
1645
/* Return status on next IN token */
1646
switch (qep->ep_type) {
1649
(usb_read(qep->csr1) & EP0_SEND_STALL) ==
1655
(usb_read(qep->csr1) & USB_IN_CSR1_SEND_STALL) ==
1656
USB_IN_CSR1_SEND_STALL;
1660
(usb_read(qep->csr1) & USB_OUT_CSR1_SEND_STALL) ==
1661
USB_OUT_CSR1_SEND_STALL;
1665
/* Back to EP0 index */
1668
DEBUG_SETUP("GET_STATUS, ep: %d (%x), val = %d\n", ep_num,
1671
DEBUG_SETUP("Unknown REQ TYPE: %d\n", reqtype);
1675
/* Clear "out packet ready" */
1676
usb_set((EP0_CLR_OUT), USB_EP0_CSR);
1677
/* Put status to FIFO */
1678
lh7a40x_fifo_write(ep0, (u8 *) & val, sizeof(val));
1679
/* Issue "In packet ready" */
1680
usb_set((EP0_IN_PKT_RDY | EP0_DATA_END), USB_EP0_CSR);
1686
* WAIT_FOR_SETUP (OUT_PKT_RDY)
1687
* - read data packet from EP0 FIFO
1690
* set EP0_CLR_OUT | EP0_DATA_END | EP0_SEND_STALL bits
1692
* set EP0_CLR_OUT | EP0_DATA_END bits
1694
static void lh7a40x_ep0_setup(struct lh7a40x_udc *dev, u32 csr)
1696
struct lh7a40x_ep *ep = &dev->ep[0];
1697
struct usb_ctrlrequest ctrl;
1698
int i, bytes, is_in;
1700
DEBUG_SETUP("%s: %x\n", __func__, csr);
1702
/* Nuke all previous transfers */
1705
/* read control req from fifo (8 bytes) */
1706
bytes = lh7a40x_fifo_read(ep, (unsigned char *)&ctrl, 8);
1708
DEBUG_SETUP("Read CTRL REQ %d bytes\n", bytes);
1709
DEBUG_SETUP("CTRL.bRequestType = %d (is_in %d)\n", ctrl.bRequestType,
1710
ctrl.bRequestType == USB_DIR_IN);
1711
DEBUG_SETUP("CTRL.bRequest = %d\n", ctrl.bRequest);
1712
DEBUG_SETUP("CTRL.wLength = %d\n", ctrl.wLength);
1713
DEBUG_SETUP("CTRL.wValue = %d (%d)\n", ctrl.wValue, ctrl.wValue >> 8);
1714
DEBUG_SETUP("CTRL.wIndex = %d\n", ctrl.wIndex);
1716
/* Set direction of EP0 */
1717
if (likely(ctrl.bRequestType & USB_DIR_IN)) {
1718
ep->bEndpointAddress |= USB_DIR_IN;
1721
ep->bEndpointAddress &= ~USB_DIR_IN;
1725
dev->req_pending = 1;
1727
/* Handle some SETUP packets ourselves */
1728
switch (ctrl.bRequest) {
1729
case USB_REQ_SET_ADDRESS:
1730
if (ctrl.bRequestType != (USB_TYPE_STANDARD | USB_RECIP_DEVICE))
1733
DEBUG_SETUP("USB_REQ_SET_ADDRESS (%d)\n", ctrl.wValue);
1734
udc_set_address(dev, ctrl.wValue);
1735
usb_set((EP0_CLR_OUT | EP0_DATA_END), USB_EP0_CSR);
1738
case USB_REQ_GET_STATUS:{
1739
if (lh7a40x_handle_get_status(dev, &ctrl) == 0)
1742
case USB_REQ_CLEAR_FEATURE:
1743
case USB_REQ_SET_FEATURE:
1744
if (ctrl.bRequestType == USB_RECIP_ENDPOINT) {
1745
struct lh7a40x_ep *qep;
1746
int ep_num = (ctrl.wIndex & 0x0f);
1748
/* Support only HALT feature */
1749
if (ctrl.wValue != 0 || ctrl.wLength != 0
1750
|| ep_num > 3 || ep_num < 1)
1753
qep = &dev->ep[ep_num];
1754
spin_unlock(&dev->lock);
1755
if (ctrl.bRequest == USB_REQ_SET_FEATURE) {
1756
DEBUG_SETUP("SET_FEATURE (%d)\n",
1758
lh7a40x_set_halt(&qep->ep, 1);
1760
DEBUG_SETUP("CLR_FEATURE (%d)\n",
1762
lh7a40x_set_halt(&qep->ep, 0);
1764
spin_lock(&dev->lock);
1767
/* Reply with a ZLP on next IN token */
1768
usb_set((EP0_CLR_OUT | EP0_DATA_END),
1779
if (likely(dev->driver)) {
1780
/* device-2-host (IN) or no data setup command, process immediately */
1781
spin_unlock(&dev->lock);
1782
i = dev->driver->setup(&dev->gadget, &ctrl);
1783
spin_lock(&dev->lock);
1786
/* setup processing failed, force stall */
1788
(" --> ERROR: gadget setup FAILED (stalling), setup returned %d\n",
1791
usb_set((EP0_CLR_OUT | EP0_DATA_END | EP0_SEND_STALL),
1794
/* ep->stopped = 1; */
1795
dev->ep0state = WAIT_FOR_SETUP;
1801
* DATA_STATE_NEED_ZLP
1803
static void lh7a40x_ep0_in_zlp(struct lh7a40x_udc *dev, u32 csr)
1805
DEBUG_EP0("%s: %x\n", __func__, csr);
1807
/* c.f. Table 15-14 */
1808
usb_set((EP0_IN_PKT_RDY | EP0_DATA_END), USB_EP0_CSR);
1809
dev->ep0state = WAIT_FOR_SETUP;
1813
* handle ep0 interrupt
1815
static void lh7a40x_handle_ep0(struct lh7a40x_udc *dev, u32 intr)
1817
struct lh7a40x_ep *ep = &dev->ep[0];
1822
csr = usb_read(USB_EP0_CSR);
1824
DEBUG_EP0("%s: csr = %x\n", __func__, csr);
1827
* For overview of what we should be doing see c.f. Chapter 18.1.2.4
1828
* We will follow that outline here modified by our own global state
1829
* indication which provides hints as to what we think should be
1834
* if SENT_STALL is set
1835
* - clear the SENT_STALL bit
1837
if (csr & EP0_SENT_STALL) {
1838
DEBUG_EP0("%s: EP0_SENT_STALL is set: %x\n", __func__, csr);
1839
usb_clear((EP0_SENT_STALL | EP0_SEND_STALL), USB_EP0_CSR);
1840
nuke(ep, -ECONNABORTED);
1841
dev->ep0state = WAIT_FOR_SETUP;
1846
* if a transfer is in progress && IN_PKT_RDY and OUT_PKT_RDY are clear
1849
* - set IN_PKT_RDY | DATA_END
1853
if (!(csr & (EP0_IN_PKT_RDY | EP0_OUT_PKT_RDY))) {
1854
DEBUG_EP0("%s: IN_PKT_RDY and OUT_PKT_RDY are clear\n",
1857
switch (dev->ep0state) {
1858
case DATA_STATE_XMIT:
1859
DEBUG_EP0("continue with DATA_STATE_XMIT\n");
1860
lh7a40x_ep0_in(dev, csr);
1862
case DATA_STATE_NEED_ZLP:
1863
DEBUG_EP0("continue with DATA_STATE_NEED_ZLP\n");
1864
lh7a40x_ep0_in_zlp(dev, csr);
1868
DEBUG_EP0("Odd state!! state = %s\n",
1869
state_names[dev->ep0state]);
1870
dev->ep0state = WAIT_FOR_SETUP;
1872
/* usb_set(EP0_SEND_STALL, ep->csr1); */
1878
* if SETUP_END is set
1879
* - abort the last transfer
1880
* - set SERVICED_SETUP_END_BIT
1882
if (csr & EP0_SETUP_END) {
1883
DEBUG_EP0("%s: EP0_SETUP_END is set: %x\n", __func__, csr);
1885
usb_set(EP0_CLR_SETUP_END, USB_EP0_CSR);
1888
dev->ep0state = WAIT_FOR_SETUP;
1892
* if EP0_OUT_PKT_RDY is set
1893
* - read data packet from EP0 FIFO
1896
* set SERVICED_OUT_PKT_RDY | DATA_END bits | SEND_STALL
1898
* set SERVICED_OUT_PKT_RDY | DATA_END bits
1900
if (csr & EP0_OUT_PKT_RDY) {
1902
DEBUG_EP0("%s: EP0_OUT_PKT_RDY is set: %x\n", __func__,
1905
switch (dev->ep0state) {
1906
case WAIT_FOR_SETUP:
1907
DEBUG_EP0("WAIT_FOR_SETUP\n");
1908
lh7a40x_ep0_setup(dev, csr);
1911
case DATA_STATE_RECV:
1912
DEBUG_EP0("DATA_STATE_RECV\n");
1913
lh7a40x_ep0_out(dev, csr);
1918
DEBUG_EP0("strange state!! 2. send stall? state = %d\n",
1925
static void lh7a40x_ep0_kick(struct lh7a40x_udc *dev, struct lh7a40x_ep *ep)
1930
csr = usb_read(USB_EP0_CSR);
1932
DEBUG_EP0("%s: %x\n", __func__, csr);
1934
/* Clear "out packet ready" */
1935
usb_set(EP0_CLR_OUT, USB_EP0_CSR);
1938
dev->ep0state = DATA_STATE_XMIT;
1939
lh7a40x_ep0_in(dev, csr);
1941
dev->ep0state = DATA_STATE_RECV;
1942
lh7a40x_ep0_out(dev, csr);
1946
/* ---------------------------------------------------------------------------
1947
* device-scoped parts of the api to the usb controller hardware
1948
* ---------------------------------------------------------------------------
1951
static int lh7a40x_udc_get_frame(struct usb_gadget *_gadget)
1953
u32 frame1 = usb_read(USB_FRM_NUM1); /* Least significant 8 bits */
1954
u32 frame2 = usb_read(USB_FRM_NUM2); /* Most significant 3 bits */
1955
DEBUG("%s, %p\n", __func__, _gadget);
1956
return ((frame2 & 0x07) << 8) | (frame1 & 0xff);
1959
static int lh7a40x_udc_wakeup(struct usb_gadget *_gadget)
1961
/* host may not have enabled remote wakeup */
1962
/*if ((UDCCS0 & UDCCS0_DRWF) == 0)
1963
return -EHOSTUNREACH;
1964
udc_set_mask_UDCCR(UDCCR_RSM); */
1968
static const struct usb_gadget_ops lh7a40x_udc_ops = {
1969
.get_frame = lh7a40x_udc_get_frame,
1970
.wakeup = lh7a40x_udc_wakeup,
1971
/* current versions must always be self-powered */
1974
static void nop_release(struct device *dev)
1976
DEBUG("%s %s\n", __func__, dev_name(dev));
1979
static struct lh7a40x_udc memory = {
1983
.ops = &lh7a40x_udc_ops,
1984
.ep0 = &memory.ep[0].ep,
1985
.name = driver_name,
1987
.init_name = "gadget",
1988
.release = nop_release,
1992
/* control endpoint */
1996
.ops = &lh7a40x_ep_ops,
1997
.maxpacket = EP0_PACKETSIZE,
2001
.bEndpointAddress = 0,
2004
.ep_type = ep_control,
2005
.fifo = io_p2v(USB_EP0_FIFO),
2006
.csr1 = USB_EP0_CSR,
2007
.csr2 = USB_EP0_CSR,
2010
/* first group of endpoints */
2013
.name = "ep1in-bulk",
2014
.ops = &lh7a40x_ep_ops,
2019
.bEndpointAddress = USB_DIR_IN | 1,
2020
.bmAttributes = USB_ENDPOINT_XFER_BULK,
2022
.ep_type = ep_bulk_in,
2023
.fifo = io_p2v(USB_EP1_FIFO),
2024
.csr1 = USB_IN_CSR1,
2025
.csr2 = USB_IN_CSR2,
2030
.name = "ep2out-bulk",
2031
.ops = &lh7a40x_ep_ops,
2036
.bEndpointAddress = 2,
2037
.bmAttributes = USB_ENDPOINT_XFER_BULK,
2039
.ep_type = ep_bulk_out,
2040
.fifo = io_p2v(USB_EP2_FIFO),
2041
.csr1 = USB_OUT_CSR1,
2042
.csr2 = USB_OUT_CSR2,
2047
.name = "ep3in-int",
2048
.ops = &lh7a40x_ep_ops,
2053
.bEndpointAddress = USB_DIR_IN | 3,
2054
.bmAttributes = USB_ENDPOINT_XFER_INT,
2056
.ep_type = ep_interrupt,
2057
.fifo = io_p2v(USB_EP3_FIFO),
2058
.csr1 = USB_IN_CSR1,
2059
.csr2 = USB_IN_CSR2,
2064
* probe - binds to the platform device
2066
static int lh7a40x_udc_probe(struct platform_device *pdev)
2068
struct lh7a40x_udc *dev = &memory;
2071
DEBUG("%s: %p\n", __func__, pdev);
2073
spin_lock_init(&dev->lock);
2074
dev->dev = &pdev->dev;
2076
device_initialize(&dev->gadget.dev);
2077
dev->gadget.dev.parent = &pdev->dev;
2079
the_controller = dev;
2080
platform_set_drvdata(pdev, dev);
2085
/* irq setup after old hardware state is cleaned up */
2087
request_irq(IRQ_USBINTR, lh7a40x_udc_irq, IRQF_DISABLED, driver_name,
2090
DEBUG(KERN_ERR "%s: can't get irq %i, err %d\n", driver_name,
2091
IRQ_USBINTR, retval);
2095
create_proc_files();
2100
static int lh7a40x_udc_remove(struct platform_device *pdev)
2102
struct lh7a40x_udc *dev = platform_get_drvdata(pdev);
2104
DEBUG("%s: %p\n", __func__, pdev);
2110
remove_proc_files();
2112
free_irq(IRQ_USBINTR, dev);
2114
platform_set_drvdata(pdev, 0);
2121
/*-------------------------------------------------------------------------*/
2123
static struct platform_driver udc_driver = {
2124
.probe = lh7a40x_udc_probe,
2125
.remove = lh7a40x_udc_remove,
2126
/* FIXME power management support */
2127
/* .suspend = ... disable UDC */
2128
/* .resume = ... re-enable UDC */
2130
.name = (char *)driver_name,
2131
.owner = THIS_MODULE,
2135
static int __init udc_init(void)
2137
DEBUG("%s: %s version %s\n", __func__, driver_name, DRIVER_VERSION);
2138
return platform_driver_register(&udc_driver);
2141
static void __exit udc_exit(void)
2143
platform_driver_unregister(&udc_driver);
2146
module_init(udc_init);
2147
module_exit(udc_exit);
2149
MODULE_DESCRIPTION(DRIVER_DESC);
2150
MODULE_AUTHOR("Mikko Lahteenmaki, Bo Henriksen");
2151
MODULE_LICENSE("GPL");
2152
MODULE_ALIAS("platform:lh7a40x_udc");