Merge branches 'release', 'asus', 'sony-laptop' and 'thinkpad' into release
[linux-2.6] / drivers / usb / gadget / atmel_usba_udc.c
1 /*
2  * Driver for the Atmel USBA high speed USB device controller
3  *
4  * Copyright (C) 2005-2007 Atmel Corporation
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10 #include <linux/clk.h>
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/interrupt.h>
14 #include <linux/io.h>
15 #include <linux/device.h>
16 #include <linux/dma-mapping.h>
17 #include <linux/list.h>
18 #include <linux/platform_device.h>
19 #include <linux/usb/ch9.h>
20 #include <linux/usb/gadget.h>
21 #include <linux/delay.h>
22
23 #include <asm/gpio.h>
24 #include <asm/arch/board.h>
25
26 #include "atmel_usba_udc.h"
27
28
29 static struct usba_udc the_udc;
30
31 #ifdef CONFIG_USB_GADGET_DEBUG_FS
32 #include <linux/debugfs.h>
33 #include <linux/uaccess.h>
34
35 static int queue_dbg_open(struct inode *inode, struct file *file)
36 {
37         struct usba_ep *ep = inode->i_private;
38         struct usba_request *req, *req_copy;
39         struct list_head *queue_data;
40
41         queue_data = kmalloc(sizeof(*queue_data), GFP_KERNEL);
42         if (!queue_data)
43                 return -ENOMEM;
44         INIT_LIST_HEAD(queue_data);
45
46         spin_lock_irq(&ep->udc->lock);
47         list_for_each_entry(req, &ep->queue, queue) {
48                 req_copy = kmalloc(sizeof(*req_copy), GFP_ATOMIC);
49                 if (!req_copy)
50                         goto fail;
51                 memcpy(req_copy, req, sizeof(*req_copy));
52                 list_add_tail(&req_copy->queue, queue_data);
53         }
54         spin_unlock_irq(&ep->udc->lock);
55
56         file->private_data = queue_data;
57         return 0;
58
59 fail:
60         spin_unlock_irq(&ep->udc->lock);
61         list_for_each_entry_safe(req, req_copy, queue_data, queue) {
62                 list_del(&req->queue);
63                 kfree(req);
64         }
65         kfree(queue_data);
66         return -ENOMEM;
67 }
68
69 /*
70  * bbbbbbbb llllllll IZS sssss nnnn FDL\n\0
71  *
72  * b: buffer address
73  * l: buffer length
74  * I/i: interrupt/no interrupt
75  * Z/z: zero/no zero
76  * S/s: short ok/short not ok
77  * s: status
78  * n: nr_packets
79  * F/f: submitted/not submitted to FIFO
80  * D/d: using/not using DMA
81  * L/l: last transaction/not last transaction
82  */
83 static ssize_t queue_dbg_read(struct file *file, char __user *buf,
84                 size_t nbytes, loff_t *ppos)
85 {
86         struct list_head *queue = file->private_data;
87         struct usba_request *req, *tmp_req;
88         size_t len, remaining, actual = 0;
89         char tmpbuf[38];
90
91         if (!access_ok(VERIFY_WRITE, buf, nbytes))
92                 return -EFAULT;
93
94         mutex_lock(&file->f_dentry->d_inode->i_mutex);
95         list_for_each_entry_safe(req, tmp_req, queue, queue) {
96                 len = snprintf(tmpbuf, sizeof(tmpbuf),
97                                 "%8p %08x %c%c%c %5d %c%c%c\n",
98                                 req->req.buf, req->req.length,
99                                 req->req.no_interrupt ? 'i' : 'I',
100                                 req->req.zero ? 'Z' : 'z',
101                                 req->req.short_not_ok ? 's' : 'S',
102                                 req->req.status,
103                                 req->submitted ? 'F' : 'f',
104                                 req->using_dma ? 'D' : 'd',
105                                 req->last_transaction ? 'L' : 'l');
106                 len = min(len, sizeof(tmpbuf));
107                 if (len > nbytes)
108                         break;
109
110                 list_del(&req->queue);
111                 kfree(req);
112
113                 remaining = __copy_to_user(buf, tmpbuf, len);
114                 actual += len - remaining;
115                 if (remaining)
116                         break;
117
118                 nbytes -= len;
119                 buf += len;
120         }
121         mutex_unlock(&file->f_dentry->d_inode->i_mutex);
122
123         return actual;
124 }
125
126 static int queue_dbg_release(struct inode *inode, struct file *file)
127 {
128         struct list_head *queue_data = file->private_data;
129         struct usba_request *req, *tmp_req;
130
131         list_for_each_entry_safe(req, tmp_req, queue_data, queue) {
132                 list_del(&req->queue);
133                 kfree(req);
134         }
135         kfree(queue_data);
136         return 0;
137 }
138
139 static int regs_dbg_open(struct inode *inode, struct file *file)
140 {
141         struct usba_udc *udc;
142         unsigned int i;
143         u32 *data;
144         int ret = -ENOMEM;
145
146         mutex_lock(&inode->i_mutex);
147         udc = inode->i_private;
148         data = kmalloc(inode->i_size, GFP_KERNEL);
149         if (!data)
150                 goto out;
151
152         spin_lock_irq(&udc->lock);
153         for (i = 0; i < inode->i_size / 4; i++)
154                 data[i] = __raw_readl(udc->regs + i * 4);
155         spin_unlock_irq(&udc->lock);
156
157         file->private_data = data;
158         ret = 0;
159
160 out:
161         mutex_unlock(&inode->i_mutex);
162
163         return ret;
164 }
165
166 static ssize_t regs_dbg_read(struct file *file, char __user *buf,
167                 size_t nbytes, loff_t *ppos)
168 {
169         struct inode *inode = file->f_dentry->d_inode;
170         int ret;
171
172         mutex_lock(&inode->i_mutex);
173         ret = simple_read_from_buffer(buf, nbytes, ppos,
174                         file->private_data,
175                         file->f_dentry->d_inode->i_size);
176         mutex_unlock(&inode->i_mutex);
177
178         return ret;
179 }
180
181 static int regs_dbg_release(struct inode *inode, struct file *file)
182 {
183         kfree(file->private_data);
184         return 0;
185 }
186
187 const struct file_operations queue_dbg_fops = {
188         .owner          = THIS_MODULE,
189         .open           = queue_dbg_open,
190         .llseek         = no_llseek,
191         .read           = queue_dbg_read,
192         .release        = queue_dbg_release,
193 };
194
195 const struct file_operations regs_dbg_fops = {
196         .owner          = THIS_MODULE,
197         .open           = regs_dbg_open,
198         .llseek         = generic_file_llseek,
199         .read           = regs_dbg_read,
200         .release        = regs_dbg_release,
201 };
202
203 static void usba_ep_init_debugfs(struct usba_udc *udc,
204                 struct usba_ep *ep)
205 {
206         struct dentry *ep_root;
207
208         ep_root = debugfs_create_dir(ep->ep.name, udc->debugfs_root);
209         if (!ep_root)
210                 goto err_root;
211         ep->debugfs_dir = ep_root;
212
213         ep->debugfs_queue = debugfs_create_file("queue", 0400, ep_root,
214                                                 ep, &queue_dbg_fops);
215         if (!ep->debugfs_queue)
216                 goto err_queue;
217
218         if (ep->can_dma) {
219                 ep->debugfs_dma_status
220                         = debugfs_create_u32("dma_status", 0400, ep_root,
221                                         &ep->last_dma_status);
222                 if (!ep->debugfs_dma_status)
223                         goto err_dma_status;
224         }
225         if (ep_is_control(ep)) {
226                 ep->debugfs_state
227                         = debugfs_create_u32("state", 0400, ep_root,
228                                         &ep->state);
229                 if (!ep->debugfs_state)
230                         goto err_state;
231         }
232
233         return;
234
235 err_state:
236         if (ep->can_dma)
237                 debugfs_remove(ep->debugfs_dma_status);
238 err_dma_status:
239         debugfs_remove(ep->debugfs_queue);
240 err_queue:
241         debugfs_remove(ep_root);
242 err_root:
243         dev_err(&ep->udc->pdev->dev,
244                 "failed to create debugfs directory for %s\n", ep->ep.name);
245 }
246
247 static void usba_ep_cleanup_debugfs(struct usba_ep *ep)
248 {
249         debugfs_remove(ep->debugfs_queue);
250         debugfs_remove(ep->debugfs_dma_status);
251         debugfs_remove(ep->debugfs_state);
252         debugfs_remove(ep->debugfs_dir);
253         ep->debugfs_dma_status = NULL;
254         ep->debugfs_dir = NULL;
255 }
256
257 static void usba_init_debugfs(struct usba_udc *udc)
258 {
259         struct dentry *root, *regs;
260         struct resource *regs_resource;
261
262         root = debugfs_create_dir(udc->gadget.name, NULL);
263         if (IS_ERR(root) || !root)
264                 goto err_root;
265         udc->debugfs_root = root;
266
267         regs = debugfs_create_file("regs", 0400, root, udc, &regs_dbg_fops);
268         if (!regs)
269                 goto err_regs;
270
271         regs_resource = platform_get_resource(udc->pdev, IORESOURCE_MEM,
272                                 CTRL_IOMEM_ID);
273         regs->d_inode->i_size = regs_resource->end - regs_resource->start + 1;
274         udc->debugfs_regs = regs;
275
276         usba_ep_init_debugfs(udc, to_usba_ep(udc->gadget.ep0));
277
278         return;
279
280 err_regs:
281         debugfs_remove(root);
282 err_root:
283         udc->debugfs_root = NULL;
284         dev_err(&udc->pdev->dev, "debugfs is not available\n");
285 }
286
287 static void usba_cleanup_debugfs(struct usba_udc *udc)
288 {
289         usba_ep_cleanup_debugfs(to_usba_ep(udc->gadget.ep0));
290         debugfs_remove(udc->debugfs_regs);
291         debugfs_remove(udc->debugfs_root);
292         udc->debugfs_regs = NULL;
293         udc->debugfs_root = NULL;
294 }
295 #else
296 static inline void usba_ep_init_debugfs(struct usba_udc *udc,
297                                          struct usba_ep *ep)
298 {
299
300 }
301
302 static inline void usba_ep_cleanup_debugfs(struct usba_ep *ep)
303 {
304
305 }
306
307 static inline void usba_init_debugfs(struct usba_udc *udc)
308 {
309
310 }
311
312 static inline void usba_cleanup_debugfs(struct usba_udc *udc)
313 {
314
315 }
316 #endif
317
318 static int vbus_is_present(struct usba_udc *udc)
319 {
320         if (udc->vbus_pin != -1)
321                 return gpio_get_value(udc->vbus_pin);
322
323         /* No Vbus detection: Assume always present */
324         return 1;
325 }
326
327 static void copy_to_fifo(void __iomem *fifo, const void *buf, int len)
328 {
329         unsigned long tmp;
330
331         DBG(DBG_FIFO, "copy to FIFO (len %d):\n", len);
332         for (; len > 0; len -= 4, buf += 4, fifo += 4) {
333                 tmp = *(unsigned long *)buf;
334                 if (len >= 4) {
335                         DBG(DBG_FIFO, "  -> %08lx\n", tmp);
336                         __raw_writel(tmp, fifo);
337                 } else {
338                         do {
339                                 DBG(DBG_FIFO, "  -> %02lx\n", tmp >> 24);
340                                 __raw_writeb(tmp >> 24, fifo);
341                                 fifo++;
342                                 tmp <<= 8;
343                         } while (--len);
344                         break;
345                 }
346         }
347 }
348
349 static void copy_from_fifo(void *buf, void __iomem *fifo, int len)
350 {
351         union {
352                 unsigned long *w;
353                 unsigned char *b;
354         } p;
355         unsigned long tmp;
356
357         DBG(DBG_FIFO, "copy from FIFO (len %d):\n", len);
358         for (p.w = buf; len > 0; len -= 4, p.w++, fifo += 4) {
359                 if (len >= 4) {
360                         tmp = __raw_readl(fifo);
361                         *p.w = tmp;
362                         DBG(DBG_FIFO, "  -> %08lx\n", tmp);
363                 } else {
364                         do {
365                                 tmp = __raw_readb(fifo);
366                                 *p.b = tmp;
367                                 DBG(DBG_FIFO, " -> %02lx\n", tmp);
368                                 fifo++, p.b++;
369                         } while (--len);
370                 }
371         }
372 }
373
374 static void next_fifo_transaction(struct usba_ep *ep, struct usba_request *req)
375 {
376         unsigned int transaction_len;
377
378         transaction_len = req->req.length - req->req.actual;
379         req->last_transaction = 1;
380         if (transaction_len > ep->ep.maxpacket) {
381                 transaction_len = ep->ep.maxpacket;
382                 req->last_transaction = 0;
383         } else if (transaction_len == ep->ep.maxpacket && req->req.zero)
384                 req->last_transaction = 0;
385
386         DBG(DBG_QUEUE, "%s: submit_transaction, req %p (length %d)%s\n",
387                 ep->ep.name, req, transaction_len,
388                 req->last_transaction ? ", done" : "");
389
390         copy_to_fifo(ep->fifo, req->req.buf + req->req.actual, transaction_len);
391         usba_ep_writel(ep, SET_STA, USBA_TX_PK_RDY);
392         req->req.actual += transaction_len;
393 }
394
395 static void submit_request(struct usba_ep *ep, struct usba_request *req)
396 {
397         DBG(DBG_QUEUE, "%s: submit_request: req %p (length %d)\n",
398                 ep->ep.name, req, req->req.length);
399
400         req->req.actual = 0;
401         req->submitted = 1;
402
403         if (req->using_dma) {
404                 if (req->req.length == 0) {
405                         usba_ep_writel(ep, CTL_ENB, USBA_TX_PK_RDY);
406                         return;
407                 }
408
409                 if (req->req.zero)
410                         usba_ep_writel(ep, CTL_ENB, USBA_SHORT_PACKET);
411                 else
412                         usba_ep_writel(ep, CTL_DIS, USBA_SHORT_PACKET);
413
414                 usba_dma_writel(ep, ADDRESS, req->req.dma);
415                 usba_dma_writel(ep, CONTROL, req->ctrl);
416         } else {
417                 next_fifo_transaction(ep, req);
418                 if (req->last_transaction) {
419                         usba_ep_writel(ep, CTL_DIS, USBA_TX_PK_RDY);
420                         usba_ep_writel(ep, CTL_ENB, USBA_TX_COMPLETE);
421                 } else {
422                         usba_ep_writel(ep, CTL_DIS, USBA_TX_COMPLETE);
423                         usba_ep_writel(ep, CTL_ENB, USBA_TX_PK_RDY);
424                 }
425         }
426 }
427
428 static void submit_next_request(struct usba_ep *ep)
429 {
430         struct usba_request *req;
431
432         if (list_empty(&ep->queue)) {
433                 usba_ep_writel(ep, CTL_DIS, USBA_TX_PK_RDY | USBA_RX_BK_RDY);
434                 return;
435         }
436
437         req = list_entry(ep->queue.next, struct usba_request, queue);
438         if (!req->submitted)
439                 submit_request(ep, req);
440 }
441
442 static void send_status(struct usba_udc *udc, struct usba_ep *ep)
443 {
444         ep->state = STATUS_STAGE_IN;
445         usba_ep_writel(ep, SET_STA, USBA_TX_PK_RDY);
446         usba_ep_writel(ep, CTL_ENB, USBA_TX_COMPLETE);
447 }
448
449 static void receive_data(struct usba_ep *ep)
450 {
451         struct usba_udc *udc = ep->udc;
452         struct usba_request *req;
453         unsigned long status;
454         unsigned int bytecount, nr_busy;
455         int is_complete = 0;
456
457         status = usba_ep_readl(ep, STA);
458         nr_busy = USBA_BFEXT(BUSY_BANKS, status);
459
460         DBG(DBG_QUEUE, "receive data: nr_busy=%u\n", nr_busy);
461
462         while (nr_busy > 0) {
463                 if (list_empty(&ep->queue)) {
464                         usba_ep_writel(ep, CTL_DIS, USBA_RX_BK_RDY);
465                         break;
466                 }
467                 req = list_entry(ep->queue.next,
468                                  struct usba_request, queue);
469
470                 bytecount = USBA_BFEXT(BYTE_COUNT, status);
471
472                 if (status & (1 << 31))
473                         is_complete = 1;
474                 if (req->req.actual + bytecount >= req->req.length) {
475                         is_complete = 1;
476                         bytecount = req->req.length - req->req.actual;
477                 }
478
479                 copy_from_fifo(req->req.buf + req->req.actual,
480                                 ep->fifo, bytecount);
481                 req->req.actual += bytecount;
482
483                 usba_ep_writel(ep, CLR_STA, USBA_RX_BK_RDY);
484
485                 if (is_complete) {
486                         DBG(DBG_QUEUE, "%s: request done\n", ep->ep.name);
487                         req->req.status = 0;
488                         list_del_init(&req->queue);
489                         usba_ep_writel(ep, CTL_DIS, USBA_RX_BK_RDY);
490                         spin_unlock(&udc->lock);
491                         req->req.complete(&ep->ep, &req->req);
492                         spin_lock(&udc->lock);
493                 }
494
495                 status = usba_ep_readl(ep, STA);
496                 nr_busy = USBA_BFEXT(BUSY_BANKS, status);
497
498                 if (is_complete && ep_is_control(ep)) {
499                         send_status(udc, ep);
500                         break;
501                 }
502         }
503 }
504
505 static void
506 request_complete(struct usba_ep *ep, struct usba_request *req, int status)
507 {
508         struct usba_udc *udc = ep->udc;
509
510         WARN_ON(!list_empty(&req->queue));
511
512         if (req->req.status == -EINPROGRESS)
513                 req->req.status = status;
514
515         if (req->mapped) {
516                 dma_unmap_single(
517                         &udc->pdev->dev, req->req.dma, req->req.length,
518                         ep->is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
519                 req->req.dma = DMA_ADDR_INVALID;
520                 req->mapped = 0;
521         }
522
523         DBG(DBG_GADGET | DBG_REQ,
524                 "%s: req %p complete: status %d, actual %u\n",
525                 ep->ep.name, req, req->req.status, req->req.actual);
526
527         spin_unlock(&udc->lock);
528         req->req.complete(&ep->ep, &req->req);
529         spin_lock(&udc->lock);
530 }
531
532 static void
533 request_complete_list(struct usba_ep *ep, struct list_head *list, int status)
534 {
535         struct usba_request *req, *tmp_req;
536
537         list_for_each_entry_safe(req, tmp_req, list, queue) {
538                 list_del_init(&req->queue);
539                 request_complete(ep, req, status);
540         }
541 }
542
543 static int
544 usba_ep_enable(struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
545 {
546         struct usba_ep *ep = to_usba_ep(_ep);
547         struct usba_udc *udc = ep->udc;
548         unsigned long flags, ept_cfg, maxpacket;
549         unsigned int nr_trans;
550
551         DBG(DBG_GADGET, "%s: ep_enable: desc=%p\n", ep->ep.name, desc);
552
553         maxpacket = le16_to_cpu(desc->wMaxPacketSize) & 0x7ff;
554
555         if (((desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK) != ep->index)
556                         || ep->index == 0
557                         || desc->bDescriptorType != USB_DT_ENDPOINT
558                         || maxpacket == 0
559                         || maxpacket > ep->fifo_size) {
560                 DBG(DBG_ERR, "ep_enable: Invalid argument");
561                 return -EINVAL;
562         }
563
564         ep->is_isoc = 0;
565         ep->is_in = 0;
566
567         if (maxpacket <= 8)
568                 ept_cfg = USBA_BF(EPT_SIZE, USBA_EPT_SIZE_8);
569         else
570                 /* LSB is bit 1, not 0 */
571                 ept_cfg = USBA_BF(EPT_SIZE, fls(maxpacket - 1) - 3);
572
573         DBG(DBG_HW, "%s: EPT_SIZE = %lu (maxpacket = %lu)\n",
574                         ep->ep.name, ept_cfg, maxpacket);
575
576         if ((desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) {
577                 ep->is_in = 1;
578                 ept_cfg |= USBA_EPT_DIR_IN;
579         }
580
581         switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
582         case USB_ENDPOINT_XFER_CONTROL:
583                 ept_cfg |= USBA_BF(EPT_TYPE, USBA_EPT_TYPE_CONTROL);
584                 ept_cfg |= USBA_BF(BK_NUMBER, USBA_BK_NUMBER_ONE);
585                 break;
586         case USB_ENDPOINT_XFER_ISOC:
587                 if (!ep->can_isoc) {
588                         DBG(DBG_ERR, "ep_enable: %s is not isoc capable\n",
589                                         ep->ep.name);
590                         return -EINVAL;
591                 }
592
593                 /*
594                  * Bits 11:12 specify number of _additional_
595                  * transactions per microframe.
596                  */
597                 nr_trans = ((le16_to_cpu(desc->wMaxPacketSize) >> 11) & 3) + 1;
598                 if (nr_trans > 3)
599                         return -EINVAL;
600
601                 ep->is_isoc = 1;
602                 ept_cfg |= USBA_BF(EPT_TYPE, USBA_EPT_TYPE_ISO);
603
604                 /*
605                  * Do triple-buffering on high-bandwidth iso endpoints.
606                  */
607                 if (nr_trans > 1 && ep->nr_banks == 3)
608                         ept_cfg |= USBA_BF(BK_NUMBER, USBA_BK_NUMBER_TRIPLE);
609                 else
610                         ept_cfg |= USBA_BF(BK_NUMBER, USBA_BK_NUMBER_DOUBLE);
611                 ept_cfg |= USBA_BF(NB_TRANS, nr_trans);
612                 break;
613         case USB_ENDPOINT_XFER_BULK:
614                 ept_cfg |= USBA_BF(EPT_TYPE, USBA_EPT_TYPE_BULK);
615                 ept_cfg |= USBA_BF(BK_NUMBER, USBA_BK_NUMBER_DOUBLE);
616                 break;
617         case USB_ENDPOINT_XFER_INT:
618                 ept_cfg |= USBA_BF(EPT_TYPE, USBA_EPT_TYPE_INT);
619                 ept_cfg |= USBA_BF(BK_NUMBER, USBA_BK_NUMBER_DOUBLE);
620                 break;
621         }
622
623         spin_lock_irqsave(&ep->udc->lock, flags);
624
625         if (ep->desc) {
626                 spin_unlock_irqrestore(&ep->udc->lock, flags);
627                 DBG(DBG_ERR, "ep%d already enabled\n", ep->index);
628                 return -EBUSY;
629         }
630
631         ep->desc = desc;
632         ep->ep.maxpacket = maxpacket;
633
634         usba_ep_writel(ep, CFG, ept_cfg);
635         usba_ep_writel(ep, CTL_ENB, USBA_EPT_ENABLE);
636
637         if (ep->can_dma) {
638                 u32 ctrl;
639
640                 usba_writel(udc, INT_ENB,
641                                 (usba_readl(udc, INT_ENB)
642                                         | USBA_BF(EPT_INT, 1 << ep->index)
643                                         | USBA_BF(DMA_INT, 1 << ep->index)));
644                 ctrl = USBA_AUTO_VALID | USBA_INTDIS_DMA;
645                 usba_ep_writel(ep, CTL_ENB, ctrl);
646         } else {
647                 usba_writel(udc, INT_ENB,
648                                 (usba_readl(udc, INT_ENB)
649                                         | USBA_BF(EPT_INT, 1 << ep->index)));
650         }
651
652         spin_unlock_irqrestore(&udc->lock, flags);
653
654         DBG(DBG_HW, "EPT_CFG%d after init: %#08lx\n", ep->index,
655                         (unsigned long)usba_ep_readl(ep, CFG));
656         DBG(DBG_HW, "INT_ENB after init: %#08lx\n",
657                         (unsigned long)usba_readl(udc, INT_ENB));
658
659         return 0;
660 }
661
662 static int usba_ep_disable(struct usb_ep *_ep)
663 {
664         struct usba_ep *ep = to_usba_ep(_ep);
665         struct usba_udc *udc = ep->udc;
666         LIST_HEAD(req_list);
667         unsigned long flags;
668
669         DBG(DBG_GADGET, "ep_disable: %s\n", ep->ep.name);
670
671         spin_lock_irqsave(&udc->lock, flags);
672
673         if (!ep->desc) {
674                 spin_unlock_irqrestore(&udc->lock, flags);
675                 DBG(DBG_ERR, "ep_disable: %s not enabled\n", ep->ep.name);
676                 return -EINVAL;
677         }
678         ep->desc = NULL;
679
680         list_splice_init(&ep->queue, &req_list);
681         if (ep->can_dma) {
682                 usba_dma_writel(ep, CONTROL, 0);
683                 usba_dma_writel(ep, ADDRESS, 0);
684                 usba_dma_readl(ep, STATUS);
685         }
686         usba_ep_writel(ep, CTL_DIS, USBA_EPT_ENABLE);
687         usba_writel(udc, INT_ENB,
688                         usba_readl(udc, INT_ENB)
689                         & ~USBA_BF(EPT_INT, 1 << ep->index));
690
691         request_complete_list(ep, &req_list, -ESHUTDOWN);
692
693         spin_unlock_irqrestore(&udc->lock, flags);
694
695         return 0;
696 }
697
698 static struct usb_request *
699 usba_ep_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
700 {
701         struct usba_request *req;
702
703         DBG(DBG_GADGET, "ep_alloc_request: %p, 0x%x\n", _ep, gfp_flags);
704
705         req = kzalloc(sizeof(*req), gfp_flags);
706         if (!req)
707                 return NULL;
708
709         INIT_LIST_HEAD(&req->queue);
710         req->req.dma = DMA_ADDR_INVALID;
711
712         return &req->req;
713 }
714
715 static void
716 usba_ep_free_request(struct usb_ep *_ep, struct usb_request *_req)
717 {
718         struct usba_request *req = to_usba_req(_req);
719
720         DBG(DBG_GADGET, "ep_free_request: %p, %p\n", _ep, _req);
721
722         kfree(req);
723 }
724
725 static int queue_dma(struct usba_udc *udc, struct usba_ep *ep,
726                 struct usba_request *req, gfp_t gfp_flags)
727 {
728         unsigned long flags;
729         int ret;
730
731         DBG(DBG_DMA, "%s: req l/%u d/%08x %c%c%c\n",
732                 ep->ep.name, req->req.length, req->req.dma,
733                 req->req.zero ? 'Z' : 'z',
734                 req->req.short_not_ok ? 'S' : 's',
735                 req->req.no_interrupt ? 'I' : 'i');
736
737         if (req->req.length > 0x10000) {
738                 /* Lengths from 0 to 65536 (inclusive) are supported */
739                 DBG(DBG_ERR, "invalid request length %u\n", req->req.length);
740                 return -EINVAL;
741         }
742
743         req->using_dma = 1;
744
745         if (req->req.dma == DMA_ADDR_INVALID) {
746                 req->req.dma = dma_map_single(
747                         &udc->pdev->dev, req->req.buf, req->req.length,
748                         ep->is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
749                 req->mapped = 1;
750         } else {
751                 dma_sync_single_for_device(
752                         &udc->pdev->dev, req->req.dma, req->req.length,
753                         ep->is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
754                 req->mapped = 0;
755         }
756
757         req->ctrl = USBA_BF(DMA_BUF_LEN, req->req.length)
758                         | USBA_DMA_CH_EN | USBA_DMA_END_BUF_IE
759                         | USBA_DMA_END_TR_EN | USBA_DMA_END_TR_IE;
760
761         if (ep->is_in)
762                 req->ctrl |= USBA_DMA_END_BUF_EN;
763
764         /*
765          * Add this request to the queue and submit for DMA if
766          * possible. Check if we're still alive first -- we may have
767          * received a reset since last time we checked.
768          */
769         ret = -ESHUTDOWN;
770         spin_lock_irqsave(&udc->lock, flags);
771         if (ep->desc) {
772                 if (list_empty(&ep->queue))
773                         submit_request(ep, req);
774
775                 list_add_tail(&req->queue, &ep->queue);
776                 ret = 0;
777         }
778         spin_unlock_irqrestore(&udc->lock, flags);
779
780         return ret;
781 }
782
783 static int
784 usba_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
785 {
786         struct usba_request *req = to_usba_req(_req);
787         struct usba_ep *ep = to_usba_ep(_ep);
788         struct usba_udc *udc = ep->udc;
789         unsigned long flags;
790         int ret;
791
792         DBG(DBG_GADGET | DBG_QUEUE | DBG_REQ, "%s: queue req %p, len %u\n",
793                         ep->ep.name, req, _req->length);
794
795         if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN || !ep->desc)
796                 return -ESHUTDOWN;
797
798         req->submitted = 0;
799         req->using_dma = 0;
800         req->last_transaction = 0;
801
802         _req->status = -EINPROGRESS;
803         _req->actual = 0;
804
805         if (ep->can_dma)
806                 return queue_dma(udc, ep, req, gfp_flags);
807
808         /* May have received a reset since last time we checked */
809         ret = -ESHUTDOWN;
810         spin_lock_irqsave(&udc->lock, flags);
811         if (ep->desc) {
812                 list_add_tail(&req->queue, &ep->queue);
813
814                 if (ep->is_in || (ep_is_control(ep)
815                                 && (ep->state == DATA_STAGE_IN
816                                         || ep->state == STATUS_STAGE_IN)))
817                         usba_ep_writel(ep, CTL_ENB, USBA_TX_PK_RDY);
818                 else
819                         usba_ep_writel(ep, CTL_ENB, USBA_RX_BK_RDY);
820                 ret = 0;
821         }
822         spin_unlock_irqrestore(&udc->lock, flags);
823
824         return ret;
825 }
826
827 static void
828 usba_update_req(struct usba_ep *ep, struct usba_request *req, u32 status)
829 {
830         req->req.actual = req->req.length - USBA_BFEXT(DMA_BUF_LEN, status);
831 }
832
833 static int stop_dma(struct usba_ep *ep, u32 *pstatus)
834 {
835         unsigned int timeout;
836         u32 status;
837
838         /*
839          * Stop the DMA controller. When writing both CH_EN
840          * and LINK to 0, the other bits are not affected.
841          */
842         usba_dma_writel(ep, CONTROL, 0);
843
844         /* Wait for the FIFO to empty */
845         for (timeout = 40; timeout; --timeout) {
846                 status = usba_dma_readl(ep, STATUS);
847                 if (!(status & USBA_DMA_CH_EN))
848                         break;
849                 udelay(1);
850         }
851
852         if (pstatus)
853                 *pstatus = status;
854
855         if (timeout == 0) {
856                 dev_err(&ep->udc->pdev->dev,
857                         "%s: timed out waiting for DMA FIFO to empty\n",
858                         ep->ep.name);
859                 return -ETIMEDOUT;
860         }
861
862         return 0;
863 }
864
865 static int usba_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
866 {
867         struct usba_ep *ep = to_usba_ep(_ep);
868         struct usba_udc *udc = ep->udc;
869         struct usba_request *req = to_usba_req(_req);
870         unsigned long flags;
871         u32 status;
872
873         DBG(DBG_GADGET | DBG_QUEUE, "ep_dequeue: %s, req %p\n",
874                         ep->ep.name, req);
875
876         spin_lock_irqsave(&udc->lock, flags);
877
878         if (req->using_dma) {
879                 /*
880                  * If this request is currently being transferred,
881                  * stop the DMA controller and reset the FIFO.
882                  */
883                 if (ep->queue.next == &req->queue) {
884                         status = usba_dma_readl(ep, STATUS);
885                         if (status & USBA_DMA_CH_EN)
886                                 stop_dma(ep, &status);
887
888 #ifdef CONFIG_USB_GADGET_DEBUG_FS
889                         ep->last_dma_status = status;
890 #endif
891
892                         usba_writel(udc, EPT_RST, 1 << ep->index);
893
894                         usba_update_req(ep, req, status);
895                 }
896         }
897
898         /*
899          * Errors should stop the queue from advancing until the
900          * completion function returns.
901          */
902         list_del_init(&req->queue);
903
904         request_complete(ep, req, -ECONNRESET);
905
906         /* Process the next request if any */
907         submit_next_request(ep);
908         spin_unlock_irqrestore(&udc->lock, flags);
909
910         return 0;
911 }
912
913 static int usba_ep_set_halt(struct usb_ep *_ep, int value)
914 {
915         struct usba_ep *ep = to_usba_ep(_ep);
916         struct usba_udc *udc = ep->udc;
917         unsigned long flags;
918         int ret = 0;
919
920         DBG(DBG_GADGET, "endpoint %s: %s HALT\n", ep->ep.name,
921                         value ? "set" : "clear");
922
923         if (!ep->desc) {
924                 DBG(DBG_ERR, "Attempted to halt uninitialized ep %s\n",
925                                 ep->ep.name);
926                 return -ENODEV;
927         }
928         if (ep->is_isoc) {
929                 DBG(DBG_ERR, "Attempted to halt isochronous ep %s\n",
930                                 ep->ep.name);
931                 return -ENOTTY;
932         }
933
934         spin_lock_irqsave(&udc->lock, flags);
935
936         /*
937          * We can't halt IN endpoints while there are still data to be
938          * transferred
939          */
940         if (!list_empty(&ep->queue)
941                         || ((value && ep->is_in && (usba_ep_readl(ep, STA)
942                                         & USBA_BF(BUSY_BANKS, -1L))))) {
943                 ret = -EAGAIN;
944         } else {
945                 if (value)
946                         usba_ep_writel(ep, SET_STA, USBA_FORCE_STALL);
947                 else
948                         usba_ep_writel(ep, CLR_STA,
949                                         USBA_FORCE_STALL | USBA_TOGGLE_CLR);
950                 usba_ep_readl(ep, STA);
951         }
952
953         spin_unlock_irqrestore(&udc->lock, flags);
954
955         return ret;
956 }
957
958 static int usba_ep_fifo_status(struct usb_ep *_ep)
959 {
960         struct usba_ep *ep = to_usba_ep(_ep);
961
962         return USBA_BFEXT(BYTE_COUNT, usba_ep_readl(ep, STA));
963 }
964
965 static void usba_ep_fifo_flush(struct usb_ep *_ep)
966 {
967         struct usba_ep *ep = to_usba_ep(_ep);
968         struct usba_udc *udc = ep->udc;
969
970         usba_writel(udc, EPT_RST, 1 << ep->index);
971 }
972
973 static const struct usb_ep_ops usba_ep_ops = {
974         .enable         = usba_ep_enable,
975         .disable        = usba_ep_disable,
976         .alloc_request  = usba_ep_alloc_request,
977         .free_request   = usba_ep_free_request,
978         .queue          = usba_ep_queue,
979         .dequeue        = usba_ep_dequeue,
980         .set_halt       = usba_ep_set_halt,
981         .fifo_status    = usba_ep_fifo_status,
982         .fifo_flush     = usba_ep_fifo_flush,
983 };
984
985 static int usba_udc_get_frame(struct usb_gadget *gadget)
986 {
987         struct usba_udc *udc = to_usba_udc(gadget);
988
989         return USBA_BFEXT(FRAME_NUMBER, usba_readl(udc, FNUM));
990 }
991
992 static int usba_udc_wakeup(struct usb_gadget *gadget)
993 {
994         struct usba_udc *udc = to_usba_udc(gadget);
995         unsigned long flags;
996         u32 ctrl;
997         int ret = -EINVAL;
998
999         spin_lock_irqsave(&udc->lock, flags);
1000         if (udc->devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP)) {
1001                 ctrl = usba_readl(udc, CTRL);
1002                 usba_writel(udc, CTRL, ctrl | USBA_REMOTE_WAKE_UP);
1003                 ret = 0;
1004         }
1005         spin_unlock_irqrestore(&udc->lock, flags);
1006
1007         return ret;
1008 }
1009
1010 static int
1011 usba_udc_set_selfpowered(struct usb_gadget *gadget, int is_selfpowered)
1012 {
1013         struct usba_udc *udc = to_usba_udc(gadget);
1014         unsigned long flags;
1015
1016         spin_lock_irqsave(&udc->lock, flags);
1017         if (is_selfpowered)
1018                 udc->devstatus |= 1 << USB_DEVICE_SELF_POWERED;
1019         else
1020                 udc->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
1021         spin_unlock_irqrestore(&udc->lock, flags);
1022
1023         return 0;
1024 }
1025
1026 static const struct usb_gadget_ops usba_udc_ops = {
1027         .get_frame              = usba_udc_get_frame,
1028         .wakeup                 = usba_udc_wakeup,
1029         .set_selfpowered        = usba_udc_set_selfpowered,
1030 };
1031
1032 #define EP(nam, idx, maxpkt, maxbk, dma, isoc)                  \
1033 {                                                               \
1034         .ep     = {                                             \
1035                 .ops            = &usba_ep_ops,                 \
1036                 .name           = nam,                          \
1037                 .maxpacket      = maxpkt,                       \
1038         },                                                      \
1039         .udc            = &the_udc,                             \
1040         .queue          = LIST_HEAD_INIT(usba_ep[idx].queue),   \
1041         .fifo_size      = maxpkt,                               \
1042         .nr_banks       = maxbk,                                \
1043         .index          = idx,                                  \
1044         .can_dma        = dma,                                  \
1045         .can_isoc       = isoc,                                 \
1046 }
1047
1048 static struct usba_ep usba_ep[] = {
1049         EP("ep0", 0, 64, 1, 0, 0),
1050         EP("ep1in-bulk", 1, 512, 2, 1, 1),
1051         EP("ep2out-bulk", 2, 512, 2, 1, 1),
1052         EP("ep3in-int", 3, 64, 3, 1, 0),
1053         EP("ep4out-int", 4, 64, 3, 1, 0),
1054         EP("ep5in-iso", 5, 1024, 3, 1, 1),
1055         EP("ep6out-iso", 6, 1024, 3, 1, 1),
1056 };
1057 #undef EP
1058
1059 static struct usb_endpoint_descriptor usba_ep0_desc = {
1060         .bLength = USB_DT_ENDPOINT_SIZE,
1061         .bDescriptorType = USB_DT_ENDPOINT,
1062         .bEndpointAddress = 0,
1063         .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1064         .wMaxPacketSize = __constant_cpu_to_le16(64),
1065         /* FIXME: I have no idea what to put here */
1066         .bInterval = 1,
1067 };
1068
1069 static void nop_release(struct device *dev)
1070 {
1071
1072 }
1073
1074 static struct usba_udc the_udc = {
1075         .gadget = {
1076                 .ops            = &usba_udc_ops,
1077                 .ep0            = &usba_ep[0].ep,
1078                 .ep_list        = LIST_HEAD_INIT(the_udc.gadget.ep_list),
1079                 .is_dualspeed   = 1,
1080                 .name           = "atmel_usba_udc",
1081                 .dev    = {
1082                         .bus_id         = "gadget",
1083                         .release        = nop_release,
1084                 },
1085         },
1086
1087         .lock   = SPIN_LOCK_UNLOCKED,
1088 };
1089
1090 /*
1091  * Called with interrupts disabled and udc->lock held.
1092  */
1093 static void reset_all_endpoints(struct usba_udc *udc)
1094 {
1095         struct usba_ep *ep;
1096         struct usba_request *req, *tmp_req;
1097
1098         usba_writel(udc, EPT_RST, ~0UL);
1099
1100         ep = to_usba_ep(udc->gadget.ep0);
1101         list_for_each_entry_safe(req, tmp_req, &ep->queue, queue) {
1102                 list_del_init(&req->queue);
1103                 request_complete(ep, req, -ECONNRESET);
1104         }
1105
1106         list_for_each_entry(ep, &udc->gadget.ep_list, ep.ep_list) {
1107                 if (ep->desc) {
1108                         spin_unlock(&udc->lock);
1109                         usba_ep_disable(&ep->ep);
1110                         spin_lock(&udc->lock);
1111                 }
1112         }
1113 }
1114
1115 static struct usba_ep *get_ep_by_addr(struct usba_udc *udc, u16 wIndex)
1116 {
1117         struct usba_ep *ep;
1118
1119         if ((wIndex & USB_ENDPOINT_NUMBER_MASK) == 0)
1120                 return to_usba_ep(udc->gadget.ep0);
1121
1122         list_for_each_entry (ep, &udc->gadget.ep_list, ep.ep_list) {
1123                 u8 bEndpointAddress;
1124
1125                 if (!ep->desc)
1126                         continue;
1127                 bEndpointAddress = ep->desc->bEndpointAddress;
1128                 if ((wIndex ^ bEndpointAddress) & USB_DIR_IN)
1129                         continue;
1130                 if ((bEndpointAddress & USB_ENDPOINT_NUMBER_MASK)
1131                                 == (wIndex & USB_ENDPOINT_NUMBER_MASK))
1132                         return ep;
1133         }
1134
1135         return NULL;
1136 }
1137
1138 /* Called with interrupts disabled and udc->lock held */
1139 static inline void set_protocol_stall(struct usba_udc *udc, struct usba_ep *ep)
1140 {
1141         usba_ep_writel(ep, SET_STA, USBA_FORCE_STALL);
1142         ep->state = WAIT_FOR_SETUP;
1143 }
1144
1145 static inline int is_stalled(struct usba_udc *udc, struct usba_ep *ep)
1146 {
1147         if (usba_ep_readl(ep, STA) & USBA_FORCE_STALL)
1148                 return 1;
1149         return 0;
1150 }
1151
1152 static inline void set_address(struct usba_udc *udc, unsigned int addr)
1153 {
1154         u32 regval;
1155
1156         DBG(DBG_BUS, "setting address %u...\n", addr);
1157         regval = usba_readl(udc, CTRL);
1158         regval = USBA_BFINS(DEV_ADDR, addr, regval);
1159         usba_writel(udc, CTRL, regval);
1160 }
1161
1162 static int do_test_mode(struct usba_udc *udc)
1163 {
1164         static const char test_packet_buffer[] = {
1165                 /* JKJKJKJK * 9 */
1166                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1167                 /* JJKKJJKK * 8 */
1168                 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
1169                 /* JJKKJJKK * 8 */
1170                 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE,
1171                 /* JJJJJJJKKKKKKK * 8 */
1172                 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
1173                 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
1174                 /* JJJJJJJK * 8 */
1175                 0x7F, 0xBF, 0xDF, 0xEF, 0xF7, 0xFB, 0xFD,
1176                 /* {JKKKKKKK * 10}, JK */
1177                 0xFC, 0x7E, 0xBF, 0xDF, 0xEF, 0xF7, 0xFB, 0xFD, 0x7E
1178         };
1179         struct usba_ep *ep;
1180         struct device *dev = &udc->pdev->dev;
1181         int test_mode;
1182
1183         test_mode = udc->test_mode;
1184
1185         /* Start from a clean slate */
1186         reset_all_endpoints(udc);
1187
1188         switch (test_mode) {
1189         case 0x0100:
1190                 /* Test_J */
1191                 usba_writel(udc, TST, USBA_TST_J_MODE);
1192                 dev_info(dev, "Entering Test_J mode...\n");
1193                 break;
1194         case 0x0200:
1195                 /* Test_K */
1196                 usba_writel(udc, TST, USBA_TST_K_MODE);
1197                 dev_info(dev, "Entering Test_K mode...\n");
1198                 break;
1199         case 0x0300:
1200                 /*
1201                  * Test_SE0_NAK: Force high-speed mode and set up ep0
1202                  * for Bulk IN transfers
1203                  */
1204                 ep = &usba_ep[0];
1205                 usba_writel(udc, TST,
1206                                 USBA_BF(SPEED_CFG, USBA_SPEED_CFG_FORCE_HIGH));
1207                 usba_ep_writel(ep, CFG,
1208                                 USBA_BF(EPT_SIZE, USBA_EPT_SIZE_64)
1209                                 | USBA_EPT_DIR_IN
1210                                 | USBA_BF(EPT_TYPE, USBA_EPT_TYPE_BULK)
1211                                 | USBA_BF(BK_NUMBER, 1));
1212                 if (!(usba_ep_readl(ep, CFG) & USBA_EPT_MAPPED)) {
1213                         set_protocol_stall(udc, ep);
1214                         dev_err(dev, "Test_SE0_NAK: ep0 not mapped\n");
1215                 } else {
1216                         usba_ep_writel(ep, CTL_ENB, USBA_EPT_ENABLE);
1217                         dev_info(dev, "Entering Test_SE0_NAK mode...\n");
1218                 }
1219                 break;
1220         case 0x0400:
1221                 /* Test_Packet */
1222                 ep = &usba_ep[0];
1223                 usba_ep_writel(ep, CFG,
1224                                 USBA_BF(EPT_SIZE, USBA_EPT_SIZE_64)
1225                                 | USBA_EPT_DIR_IN
1226                                 | USBA_BF(EPT_TYPE, USBA_EPT_TYPE_BULK)
1227                                 | USBA_BF(BK_NUMBER, 1));
1228                 if (!(usba_ep_readl(ep, CFG) & USBA_EPT_MAPPED)) {
1229                         set_protocol_stall(udc, ep);
1230                         dev_err(dev, "Test_Packet: ep0 not mapped\n");
1231                 } else {
1232                         usba_ep_writel(ep, CTL_ENB, USBA_EPT_ENABLE);
1233                         usba_writel(udc, TST, USBA_TST_PKT_MODE);
1234                         copy_to_fifo(ep->fifo, test_packet_buffer,
1235                                         sizeof(test_packet_buffer));
1236                         usba_ep_writel(ep, SET_STA, USBA_TX_PK_RDY);
1237                         dev_info(dev, "Entering Test_Packet mode...\n");
1238                 }
1239                 break;
1240         default:
1241                 dev_err(dev, "Invalid test mode: 0x%04x\n", test_mode);
1242                 return -EINVAL;
1243         }
1244
1245         return 0;
1246 }
1247
1248 /* Avoid overly long expressions */
1249 static inline bool feature_is_dev_remote_wakeup(struct usb_ctrlrequest *crq)
1250 {
1251         if (crq->wValue == __constant_cpu_to_le16(USB_DEVICE_REMOTE_WAKEUP))
1252                 return true;
1253         return false;
1254 }
1255
1256 static inline bool feature_is_dev_test_mode(struct usb_ctrlrequest *crq)
1257 {
1258         if (crq->wValue == __constant_cpu_to_le16(USB_DEVICE_TEST_MODE))
1259                 return true;
1260         return false;
1261 }
1262
1263 static inline bool feature_is_ep_halt(struct usb_ctrlrequest *crq)
1264 {
1265         if (crq->wValue == __constant_cpu_to_le16(USB_ENDPOINT_HALT))
1266                 return true;
1267         return false;
1268 }
1269
1270 static int handle_ep0_setup(struct usba_udc *udc, struct usba_ep *ep,
1271                 struct usb_ctrlrequest *crq)
1272 {
1273         int retval = 0;;
1274
1275         switch (crq->bRequest) {
1276         case USB_REQ_GET_STATUS: {
1277                 u16 status;
1278
1279                 if (crq->bRequestType == (USB_DIR_IN | USB_RECIP_DEVICE)) {
1280                         status = cpu_to_le16(udc->devstatus);
1281                 } else if (crq->bRequestType
1282                                 == (USB_DIR_IN | USB_RECIP_INTERFACE)) {
1283                         status = __constant_cpu_to_le16(0);
1284                 } else if (crq->bRequestType
1285                                 == (USB_DIR_IN | USB_RECIP_ENDPOINT)) {
1286                         struct usba_ep *target;
1287
1288                         target = get_ep_by_addr(udc, le16_to_cpu(crq->wIndex));
1289                         if (!target)
1290                                 goto stall;
1291
1292                         status = 0;
1293                         if (is_stalled(udc, target))
1294                                 status |= __constant_cpu_to_le16(1);
1295                 } else
1296                         goto delegate;
1297
1298                 /* Write directly to the FIFO. No queueing is done. */
1299                 if (crq->wLength != __constant_cpu_to_le16(sizeof(status)))
1300                         goto stall;
1301                 ep->state = DATA_STAGE_IN;
1302                 __raw_writew(status, ep->fifo);
1303                 usba_ep_writel(ep, SET_STA, USBA_TX_PK_RDY);
1304                 break;
1305         }
1306
1307         case USB_REQ_CLEAR_FEATURE: {
1308                 if (crq->bRequestType == USB_RECIP_DEVICE) {
1309                         if (feature_is_dev_remote_wakeup(crq))
1310                                 udc->devstatus
1311                                         &= ~(1 << USB_DEVICE_REMOTE_WAKEUP);
1312                         else
1313                                 /* Can't CLEAR_FEATURE TEST_MODE */
1314                                 goto stall;
1315                 } else if (crq->bRequestType == USB_RECIP_ENDPOINT) {
1316                         struct usba_ep *target;
1317
1318                         if (crq->wLength != __constant_cpu_to_le16(0)
1319                                         || !feature_is_ep_halt(crq))
1320                                 goto stall;
1321                         target = get_ep_by_addr(udc, le16_to_cpu(crq->wIndex));
1322                         if (!target)
1323                                 goto stall;
1324
1325                         usba_ep_writel(target, CLR_STA, USBA_FORCE_STALL);
1326                         if (target->index != 0)
1327                                 usba_ep_writel(target, CLR_STA,
1328                                                 USBA_TOGGLE_CLR);
1329                 } else {
1330                         goto delegate;
1331                 }
1332
1333                 send_status(udc, ep);
1334                 break;
1335         }
1336
1337         case USB_REQ_SET_FEATURE: {
1338                 if (crq->bRequestType == USB_RECIP_DEVICE) {
1339                         if (feature_is_dev_test_mode(crq)) {
1340                                 send_status(udc, ep);
1341                                 ep->state = STATUS_STAGE_TEST;
1342                                 udc->test_mode = le16_to_cpu(crq->wIndex);
1343                                 return 0;
1344                         } else if (feature_is_dev_remote_wakeup(crq)) {
1345                                 udc->devstatus |= 1 << USB_DEVICE_REMOTE_WAKEUP;
1346                         } else {
1347                                 goto stall;
1348                         }
1349                 } else if (crq->bRequestType == USB_RECIP_ENDPOINT) {
1350                         struct usba_ep *target;
1351
1352                         if (crq->wLength != __constant_cpu_to_le16(0)
1353                                         || !feature_is_ep_halt(crq))
1354                                 goto stall;
1355
1356                         target = get_ep_by_addr(udc, le16_to_cpu(crq->wIndex));
1357                         if (!target)
1358                                 goto stall;
1359
1360                         usba_ep_writel(target, SET_STA, USBA_FORCE_STALL);
1361                 } else
1362                         goto delegate;
1363
1364                 send_status(udc, ep);
1365                 break;
1366         }
1367
1368         case USB_REQ_SET_ADDRESS:
1369                 if (crq->bRequestType != (USB_DIR_OUT | USB_RECIP_DEVICE))
1370                         goto delegate;
1371
1372                 set_address(udc, le16_to_cpu(crq->wValue));
1373                 send_status(udc, ep);
1374                 ep->state = STATUS_STAGE_ADDR;
1375                 break;
1376
1377         default:
1378 delegate:
1379                 spin_unlock(&udc->lock);
1380                 retval = udc->driver->setup(&udc->gadget, crq);
1381                 spin_lock(&udc->lock);
1382         }
1383
1384         return retval;
1385
1386 stall:
1387         pr_err("udc: %s: Invalid setup request: %02x.%02x v%04x i%04x l%d, "
1388                 "halting endpoint...\n",
1389                 ep->ep.name, crq->bRequestType, crq->bRequest,
1390                 le16_to_cpu(crq->wValue), le16_to_cpu(crq->wIndex),
1391                 le16_to_cpu(crq->wLength));
1392         set_protocol_stall(udc, ep);
1393         return -1;
1394 }
1395
1396 static void usba_control_irq(struct usba_udc *udc, struct usba_ep *ep)
1397 {
1398         struct usba_request *req;
1399         u32 epstatus;
1400         u32 epctrl;
1401
1402 restart:
1403         epstatus = usba_ep_readl(ep, STA);
1404         epctrl = usba_ep_readl(ep, CTL);
1405
1406         DBG(DBG_INT, "%s [%d]: s/%08x c/%08x\n",
1407                         ep->ep.name, ep->state, epstatus, epctrl);
1408
1409         req = NULL;
1410         if (!list_empty(&ep->queue))
1411                 req = list_entry(ep->queue.next,
1412                                  struct usba_request, queue);
1413
1414         if ((epctrl & USBA_TX_PK_RDY) && !(epstatus & USBA_TX_PK_RDY)) {
1415                 if (req->submitted)
1416                         next_fifo_transaction(ep, req);
1417                 else
1418                         submit_request(ep, req);
1419
1420                 if (req->last_transaction) {
1421                         usba_ep_writel(ep, CTL_DIS, USBA_TX_PK_RDY);
1422                         usba_ep_writel(ep, CTL_ENB, USBA_TX_COMPLETE);
1423                 }
1424                 goto restart;
1425         }
1426         if ((epstatus & epctrl) & USBA_TX_COMPLETE) {
1427                 usba_ep_writel(ep, CLR_STA, USBA_TX_COMPLETE);
1428
1429                 switch (ep->state) {
1430                 case DATA_STAGE_IN:
1431                         usba_ep_writel(ep, CTL_ENB, USBA_RX_BK_RDY);
1432                         usba_ep_writel(ep, CTL_DIS, USBA_TX_COMPLETE);
1433                         ep->state = STATUS_STAGE_OUT;
1434                         break;
1435                 case STATUS_STAGE_ADDR:
1436                         /* Activate our new address */
1437                         usba_writel(udc, CTRL, (usba_readl(udc, CTRL)
1438                                                 | USBA_FADDR_EN));
1439                         usba_ep_writel(ep, CTL_DIS, USBA_TX_COMPLETE);
1440                         ep->state = WAIT_FOR_SETUP;
1441                         break;
1442                 case STATUS_STAGE_IN:
1443                         if (req) {
1444                                 list_del_init(&req->queue);
1445                                 request_complete(ep, req, 0);
1446                                 submit_next_request(ep);
1447                         }
1448                         usba_ep_writel(ep, CTL_DIS, USBA_TX_COMPLETE);
1449                         ep->state = WAIT_FOR_SETUP;
1450                         break;
1451                 case STATUS_STAGE_TEST:
1452                         usba_ep_writel(ep, CTL_DIS, USBA_TX_COMPLETE);
1453                         ep->state = WAIT_FOR_SETUP;
1454                         if (do_test_mode(udc))
1455                                 set_protocol_stall(udc, ep);
1456                         break;
1457                 default:
1458                         pr_err("udc: %s: TXCOMP: Invalid endpoint state %d, "
1459                                 "halting endpoint...\n",
1460                                 ep->ep.name, ep->state);
1461                         set_protocol_stall(udc, ep);
1462                         break;
1463                 }
1464
1465                 goto restart;
1466         }
1467         if ((epstatus & epctrl) & USBA_RX_BK_RDY) {
1468                 switch (ep->state) {
1469                 case STATUS_STAGE_OUT:
1470                         usba_ep_writel(ep, CLR_STA, USBA_RX_BK_RDY);
1471                         usba_ep_writel(ep, CTL_DIS, USBA_RX_BK_RDY);
1472
1473                         if (req) {
1474                                 list_del_init(&req->queue);
1475                                 request_complete(ep, req, 0);
1476                         }
1477                         ep->state = WAIT_FOR_SETUP;
1478                         break;
1479
1480                 case DATA_STAGE_OUT:
1481                         receive_data(ep);
1482                         break;
1483
1484                 default:
1485                         usba_ep_writel(ep, CLR_STA, USBA_RX_BK_RDY);
1486                         usba_ep_writel(ep, CTL_DIS, USBA_RX_BK_RDY);
1487                         pr_err("udc: %s: RXRDY: Invalid endpoint state %d, "
1488                                 "halting endpoint...\n",
1489                                 ep->ep.name, ep->state);
1490                         set_protocol_stall(udc, ep);
1491                         break;
1492                 }
1493
1494                 goto restart;
1495         }
1496         if (epstatus & USBA_RX_SETUP) {
1497                 union {
1498                         struct usb_ctrlrequest crq;
1499                         unsigned long data[2];
1500                 } crq;
1501                 unsigned int pkt_len;
1502                 int ret;
1503
1504                 if (ep->state != WAIT_FOR_SETUP) {
1505                         /*
1506                          * Didn't expect a SETUP packet at this
1507                          * point. Clean up any pending requests (which
1508                          * may be successful).
1509                          */
1510                         int status = -EPROTO;
1511
1512                         /*
1513                          * RXRDY and TXCOMP are dropped when SETUP
1514                          * packets arrive.  Just pretend we received
1515                          * the status packet.
1516                          */
1517                         if (ep->state == STATUS_STAGE_OUT
1518                                         || ep->state == STATUS_STAGE_IN) {
1519                                 usba_ep_writel(ep, CTL_DIS, USBA_RX_BK_RDY);
1520                                 status = 0;
1521                         }
1522
1523                         if (req) {
1524                                 list_del_init(&req->queue);
1525                                 request_complete(ep, req, status);
1526                         }
1527                 }
1528
1529                 pkt_len = USBA_BFEXT(BYTE_COUNT, usba_ep_readl(ep, STA));
1530                 DBG(DBG_HW, "Packet length: %u\n", pkt_len);
1531                 if (pkt_len != sizeof(crq)) {
1532                         pr_warning("udc: Invalid packet length %u "
1533                                 "(expected %lu)\n", pkt_len, sizeof(crq));
1534                         set_protocol_stall(udc, ep);
1535                         return;
1536                 }
1537
1538                 DBG(DBG_FIFO, "Copying ctrl request from 0x%p:\n", ep->fifo);
1539                 copy_from_fifo(crq.data, ep->fifo, sizeof(crq));
1540
1541                 /* Free up one bank in the FIFO so that we can
1542                  * generate or receive a reply right away. */
1543                 usba_ep_writel(ep, CLR_STA, USBA_RX_SETUP);
1544
1545                 /* printk(KERN_DEBUG "setup: %d: %02x.%02x\n",
1546                         ep->state, crq.crq.bRequestType,
1547                         crq.crq.bRequest); */
1548
1549                 if (crq.crq.bRequestType & USB_DIR_IN) {
1550                         /*
1551                          * The USB 2.0 spec states that "if wLength is
1552                          * zero, there is no data transfer phase."
1553                          * However, testusb #14 seems to actually
1554                          * expect a data phase even if wLength = 0...
1555                          */
1556                         ep->state = DATA_STAGE_IN;
1557                 } else {
1558                         if (crq.crq.wLength != __constant_cpu_to_le16(0))
1559                                 ep->state = DATA_STAGE_OUT;
1560                         else
1561                                 ep->state = STATUS_STAGE_IN;
1562                 }
1563
1564                 ret = -1;
1565                 if (ep->index == 0)
1566                         ret = handle_ep0_setup(udc, ep, &crq.crq);
1567                 else {
1568                         spin_unlock(&udc->lock);
1569                         ret = udc->driver->setup(&udc->gadget, &crq.crq);
1570                         spin_lock(&udc->lock);
1571                 }
1572
1573                 DBG(DBG_BUS, "req %02x.%02x, length %d, state %d, ret %d\n",
1574                         crq.crq.bRequestType, crq.crq.bRequest,
1575                         le16_to_cpu(crq.crq.wLength), ep->state, ret);
1576
1577                 if (ret < 0) {
1578                         /* Let the host know that we failed */
1579                         set_protocol_stall(udc, ep);
1580                 }
1581         }
1582 }
1583
1584 static void usba_ep_irq(struct usba_udc *udc, struct usba_ep *ep)
1585 {
1586         struct usba_request *req;
1587         u32 epstatus;
1588         u32 epctrl;
1589
1590         epstatus = usba_ep_readl(ep, STA);
1591         epctrl = usba_ep_readl(ep, CTL);
1592
1593         DBG(DBG_INT, "%s: interrupt, status: 0x%08x\n", ep->ep.name, epstatus);
1594
1595         while ((epctrl & USBA_TX_PK_RDY) && !(epstatus & USBA_TX_PK_RDY)) {
1596                 DBG(DBG_BUS, "%s: TX PK ready\n", ep->ep.name);
1597
1598                 if (list_empty(&ep->queue)) {
1599                         dev_warn(&udc->pdev->dev, "ep_irq: queue empty\n");
1600                         usba_ep_writel(ep, CTL_DIS, USBA_TX_PK_RDY);
1601                         return;
1602                 }
1603
1604                 req = list_entry(ep->queue.next, struct usba_request, queue);
1605
1606                 if (req->using_dma) {
1607                         /* Send a zero-length packet */
1608                         usba_ep_writel(ep, SET_STA,
1609                                         USBA_TX_PK_RDY);
1610                         usba_ep_writel(ep, CTL_DIS,
1611                                         USBA_TX_PK_RDY);
1612                         list_del_init(&req->queue);
1613                         submit_next_request(ep);
1614                         request_complete(ep, req, 0);
1615                 } else {
1616                         if (req->submitted)
1617                                 next_fifo_transaction(ep, req);
1618                         else
1619                                 submit_request(ep, req);
1620
1621                         if (req->last_transaction) {
1622                                 list_del_init(&req->queue);
1623                                 submit_next_request(ep);
1624                                 request_complete(ep, req, 0);
1625                         }
1626                 }
1627
1628                 epstatus = usba_ep_readl(ep, STA);
1629                 epctrl = usba_ep_readl(ep, CTL);
1630         }
1631         if ((epstatus & epctrl) & USBA_RX_BK_RDY) {
1632                 DBG(DBG_BUS, "%s: RX data ready\n", ep->ep.name);
1633                 receive_data(ep);
1634                 usba_ep_writel(ep, CLR_STA, USBA_RX_BK_RDY);
1635         }
1636 }
1637
1638 static void usba_dma_irq(struct usba_udc *udc, struct usba_ep *ep)
1639 {
1640         struct usba_request *req;
1641         u32 status, control, pending;
1642
1643         status = usba_dma_readl(ep, STATUS);
1644         control = usba_dma_readl(ep, CONTROL);
1645 #ifdef CONFIG_USB_GADGET_DEBUG_FS
1646         ep->last_dma_status = status;
1647 #endif
1648         pending = status & control;
1649         DBG(DBG_INT | DBG_DMA, "dma irq, s/%#08x, c/%#08x\n", status, control);
1650
1651         if (status & USBA_DMA_CH_EN) {
1652                 dev_err(&udc->pdev->dev,
1653                         "DMA_CH_EN is set after transfer is finished!\n");
1654                 dev_err(&udc->pdev->dev,
1655                         "status=%#08x, pending=%#08x, control=%#08x\n",
1656                         status, pending, control);
1657
1658                 /*
1659                  * try to pretend nothing happened. We might have to
1660                  * do something here...
1661                  */
1662         }
1663
1664         if (list_empty(&ep->queue))
1665                 /* Might happen if a reset comes along at the right moment */
1666                 return;
1667
1668         if (pending & (USBA_DMA_END_TR_ST | USBA_DMA_END_BUF_ST)) {
1669                 req = list_entry(ep->queue.next, struct usba_request, queue);
1670                 usba_update_req(ep, req, status);
1671
1672                 list_del_init(&req->queue);
1673                 submit_next_request(ep);
1674                 request_complete(ep, req, 0);
1675         }
1676 }
1677
1678 static irqreturn_t usba_udc_irq(int irq, void *devid)
1679 {
1680         struct usba_udc *udc = devid;
1681         u32 status;
1682         u32 dma_status;
1683         u32 ep_status;
1684
1685         spin_lock(&udc->lock);
1686
1687         status = usba_readl(udc, INT_STA);
1688         DBG(DBG_INT, "irq, status=%#08x\n", status);
1689
1690         if (status & USBA_DET_SUSPEND) {
1691                 usba_writel(udc, INT_CLR, USBA_DET_SUSPEND);
1692                 DBG(DBG_BUS, "Suspend detected\n");
1693                 if (udc->gadget.speed != USB_SPEED_UNKNOWN
1694                                 && udc->driver && udc->driver->suspend) {
1695                         spin_unlock(&udc->lock);
1696                         udc->driver->suspend(&udc->gadget);
1697                         spin_lock(&udc->lock);
1698                 }
1699         }
1700
1701         if (status & USBA_WAKE_UP) {
1702                 usba_writel(udc, INT_CLR, USBA_WAKE_UP);
1703                 DBG(DBG_BUS, "Wake Up CPU detected\n");
1704         }
1705
1706         if (status & USBA_END_OF_RESUME) {
1707                 usba_writel(udc, INT_CLR, USBA_END_OF_RESUME);
1708                 DBG(DBG_BUS, "Resume detected\n");
1709                 if (udc->gadget.speed != USB_SPEED_UNKNOWN
1710                                 && udc->driver && udc->driver->resume) {
1711                         spin_unlock(&udc->lock);
1712                         udc->driver->resume(&udc->gadget);
1713                         spin_lock(&udc->lock);
1714                 }
1715         }
1716
1717         dma_status = USBA_BFEXT(DMA_INT, status);
1718         if (dma_status) {
1719                 int i;
1720
1721                 for (i = 1; i < USBA_NR_ENDPOINTS; i++)
1722                         if (dma_status & (1 << i))
1723                                 usba_dma_irq(udc, &usba_ep[i]);
1724         }
1725
1726         ep_status = USBA_BFEXT(EPT_INT, status);
1727         if (ep_status) {
1728                 int i;
1729
1730                 for (i = 0; i < USBA_NR_ENDPOINTS; i++)
1731                         if (ep_status & (1 << i)) {
1732                                 if (ep_is_control(&usba_ep[i]))
1733                                         usba_control_irq(udc, &usba_ep[i]);
1734                                 else
1735                                         usba_ep_irq(udc, &usba_ep[i]);
1736                         }
1737         }
1738
1739         if (status & USBA_END_OF_RESET) {
1740                 struct usba_ep *ep0;
1741
1742                 usba_writel(udc, INT_CLR, USBA_END_OF_RESET);
1743                 reset_all_endpoints(udc);
1744
1745                 if (status & USBA_HIGH_SPEED) {
1746                         DBG(DBG_BUS, "High-speed bus reset detected\n");
1747                         udc->gadget.speed = USB_SPEED_HIGH;
1748                 } else {
1749                         DBG(DBG_BUS, "Full-speed bus reset detected\n");
1750                         udc->gadget.speed = USB_SPEED_FULL;
1751                 }
1752
1753                 ep0 = &usba_ep[0];
1754                 ep0->desc = &usba_ep0_desc;
1755                 ep0->state = WAIT_FOR_SETUP;
1756                 usba_ep_writel(ep0, CFG,
1757                                 (USBA_BF(EPT_SIZE, EP0_EPT_SIZE)
1758                                 | USBA_BF(EPT_TYPE, USBA_EPT_TYPE_CONTROL)
1759                                 | USBA_BF(BK_NUMBER, USBA_BK_NUMBER_ONE)));
1760                 usba_ep_writel(ep0, CTL_ENB,
1761                                 USBA_EPT_ENABLE | USBA_RX_SETUP);
1762                 usba_writel(udc, INT_ENB,
1763                                 (usba_readl(udc, INT_ENB)
1764                                 | USBA_BF(EPT_INT, 1)
1765                                 | USBA_DET_SUSPEND
1766                                 | USBA_END_OF_RESUME));
1767
1768                 if (!(usba_ep_readl(ep0, CFG) & USBA_EPT_MAPPED))
1769                         dev_warn(&udc->pdev->dev,
1770                                  "WARNING: EP0 configuration is invalid!\n");
1771         }
1772
1773         spin_unlock(&udc->lock);
1774
1775         return IRQ_HANDLED;
1776 }
1777
1778 static irqreturn_t usba_vbus_irq(int irq, void *devid)
1779 {
1780         struct usba_udc *udc = devid;
1781         int vbus;
1782
1783         /* debounce */
1784         udelay(10);
1785
1786         spin_lock(&udc->lock);
1787
1788         /* May happen if Vbus pin toggles during probe() */
1789         if (!udc->driver)
1790                 goto out;
1791
1792         vbus = gpio_get_value(udc->vbus_pin);
1793         if (vbus != udc->vbus_prev) {
1794                 if (vbus) {
1795                         usba_writel(udc, CTRL, USBA_EN_USBA);
1796                         usba_writel(udc, INT_ENB, USBA_END_OF_RESET);
1797                 } else {
1798                         udc->gadget.speed = USB_SPEED_UNKNOWN;
1799                         reset_all_endpoints(udc);
1800                         usba_writel(udc, CTRL, 0);
1801                         spin_unlock(&udc->lock);
1802                         udc->driver->disconnect(&udc->gadget);
1803                         spin_lock(&udc->lock);
1804                 }
1805                 udc->vbus_prev = vbus;
1806         }
1807
1808 out:
1809         spin_unlock(&udc->lock);
1810
1811         return IRQ_HANDLED;
1812 }
1813
1814 int usb_gadget_register_driver(struct usb_gadget_driver *driver)
1815 {
1816         struct usba_udc *udc = &the_udc;
1817         unsigned long flags;
1818         int ret;
1819
1820         if (!udc->pdev)
1821                 return -ENODEV;
1822
1823         spin_lock_irqsave(&udc->lock, flags);
1824         if (udc->driver) {
1825                 spin_unlock_irqrestore(&udc->lock, flags);
1826                 return -EBUSY;
1827         }
1828
1829         udc->devstatus = 1 << USB_DEVICE_SELF_POWERED;
1830         udc->driver = driver;
1831         udc->gadget.dev.driver = &driver->driver;
1832         spin_unlock_irqrestore(&udc->lock, flags);
1833
1834         clk_enable(udc->pclk);
1835         clk_enable(udc->hclk);
1836
1837         ret = driver->bind(&udc->gadget);
1838         if (ret) {
1839                 DBG(DBG_ERR, "Could not bind to driver %s: error %d\n",
1840                         driver->driver.name, ret);
1841                 goto err_driver_bind;
1842         }
1843
1844         DBG(DBG_GADGET, "registered driver `%s'\n", driver->driver.name);
1845
1846         udc->vbus_prev = 0;
1847         if (udc->vbus_pin != -1)
1848                 enable_irq(gpio_to_irq(udc->vbus_pin));
1849
1850         /* If Vbus is present, enable the controller and wait for reset */
1851         spin_lock_irqsave(&udc->lock, flags);
1852         if (vbus_is_present(udc) && udc->vbus_prev == 0) {
1853                 usba_writel(udc, CTRL, USBA_EN_USBA);
1854                 usba_writel(udc, INT_ENB, USBA_END_OF_RESET);
1855         }
1856         spin_unlock_irqrestore(&udc->lock, flags);
1857
1858         return 0;
1859
1860 err_driver_bind:
1861         udc->driver = NULL;
1862         udc->gadget.dev.driver = NULL;
1863         return ret;
1864 }
1865 EXPORT_SYMBOL(usb_gadget_register_driver);
1866
1867 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1868 {
1869         struct usba_udc *udc = &the_udc;
1870         unsigned long flags;
1871
1872         if (!udc->pdev)
1873                 return -ENODEV;
1874         if (driver != udc->driver)
1875                 return -EINVAL;
1876
1877         if (udc->vbus_pin != -1)
1878                 disable_irq(gpio_to_irq(udc->vbus_pin));
1879
1880         spin_lock_irqsave(&udc->lock, flags);
1881         udc->gadget.speed = USB_SPEED_UNKNOWN;
1882         reset_all_endpoints(udc);
1883         spin_unlock_irqrestore(&udc->lock, flags);
1884
1885         /* This will also disable the DP pullup */
1886         usba_writel(udc, CTRL, 0);
1887
1888         driver->unbind(&udc->gadget);
1889         udc->gadget.dev.driver = NULL;
1890         udc->driver = NULL;
1891
1892         clk_disable(udc->hclk);
1893         clk_disable(udc->pclk);
1894
1895         DBG(DBG_GADGET, "unregistered driver `%s'\n", driver->driver.name);
1896
1897         return 0;
1898 }
1899 EXPORT_SYMBOL(usb_gadget_unregister_driver);
1900
1901 static int __init usba_udc_probe(struct platform_device *pdev)
1902 {
1903         struct usba_platform_data *pdata = pdev->dev.platform_data;
1904         struct resource *regs, *fifo;
1905         struct clk *pclk, *hclk;
1906         struct usba_udc *udc = &the_udc;
1907         int irq, ret, i;
1908
1909         regs = platform_get_resource(pdev, IORESOURCE_MEM, CTRL_IOMEM_ID);
1910         fifo = platform_get_resource(pdev, IORESOURCE_MEM, FIFO_IOMEM_ID);
1911         if (!regs || !fifo)
1912                 return -ENXIO;
1913
1914         irq = platform_get_irq(pdev, 0);
1915         if (irq < 0)
1916                 return irq;
1917
1918         pclk = clk_get(&pdev->dev, "pclk");
1919         if (IS_ERR(pclk))
1920                 return PTR_ERR(pclk);
1921         hclk = clk_get(&pdev->dev, "hclk");
1922         if (IS_ERR(hclk)) {
1923                 ret = PTR_ERR(hclk);
1924                 goto err_get_hclk;
1925         }
1926
1927         udc->pdev = pdev;
1928         udc->pclk = pclk;
1929         udc->hclk = hclk;
1930         udc->vbus_pin = -1;
1931
1932         ret = -ENOMEM;
1933         udc->regs = ioremap(regs->start, regs->end - regs->start + 1);
1934         if (!udc->regs) {
1935                 dev_err(&pdev->dev, "Unable to map I/O memory, aborting.\n");
1936                 goto err_map_regs;
1937         }
1938         dev_info(&pdev->dev, "MMIO registers at 0x%08lx mapped at %p\n",
1939                  (unsigned long)regs->start, udc->regs);
1940         udc->fifo = ioremap(fifo->start, fifo->end - fifo->start + 1);
1941         if (!udc->fifo) {
1942                 dev_err(&pdev->dev, "Unable to map FIFO, aborting.\n");
1943                 goto err_map_fifo;
1944         }
1945         dev_info(&pdev->dev, "FIFO at 0x%08lx mapped at %p\n",
1946                  (unsigned long)fifo->start, udc->fifo);
1947
1948         device_initialize(&udc->gadget.dev);
1949         udc->gadget.dev.parent = &pdev->dev;
1950         udc->gadget.dev.dma_mask = pdev->dev.dma_mask;
1951
1952         platform_set_drvdata(pdev, udc);
1953
1954         /* Make sure we start from a clean slate */
1955         clk_enable(pclk);
1956         usba_writel(udc, CTRL, 0);
1957         clk_disable(pclk);
1958
1959         INIT_LIST_HEAD(&usba_ep[0].ep.ep_list);
1960         usba_ep[0].ep_regs = udc->regs + USBA_EPT_BASE(0);
1961         usba_ep[0].dma_regs = udc->regs + USBA_DMA_BASE(0);
1962         usba_ep[0].fifo = udc->fifo + USBA_FIFO_BASE(0);
1963         for (i = 1; i < ARRAY_SIZE(usba_ep); i++) {
1964                 struct usba_ep *ep = &usba_ep[i];
1965
1966                 ep->ep_regs = udc->regs + USBA_EPT_BASE(i);
1967                 ep->dma_regs = udc->regs + USBA_DMA_BASE(i);
1968                 ep->fifo = udc->fifo + USBA_FIFO_BASE(i);
1969
1970                 list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
1971         }
1972
1973         ret = request_irq(irq, usba_udc_irq, 0, "atmel_usba_udc", udc);
1974         if (ret) {
1975                 dev_err(&pdev->dev, "Cannot request irq %d (error %d)\n",
1976                         irq, ret);
1977                 goto err_request_irq;
1978         }
1979         udc->irq = irq;
1980
1981         ret = device_add(&udc->gadget.dev);
1982         if (ret) {
1983                 dev_dbg(&pdev->dev, "Could not add gadget: %d\n", ret);
1984                 goto err_device_add;
1985         }
1986
1987         if (pdata && pdata->vbus_pin != GPIO_PIN_NONE) {
1988                 if (!gpio_request(pdata->vbus_pin, "atmel_usba_udc")) {
1989                         udc->vbus_pin = pdata->vbus_pin;
1990
1991                         ret = request_irq(gpio_to_irq(udc->vbus_pin),
1992                                         usba_vbus_irq, 0,
1993                                         "atmel_usba_udc", udc);
1994                         if (ret) {
1995                                 gpio_free(udc->vbus_pin);
1996                                 udc->vbus_pin = -1;
1997                                 dev_warn(&udc->pdev->dev,
1998                                          "failed to request vbus irq; "
1999                                          "assuming always on\n");
2000                         } else {
2001                                 disable_irq(gpio_to_irq(udc->vbus_pin));
2002                         }
2003                 }
2004         }
2005
2006         usba_init_debugfs(udc);
2007         for (i = 1; i < ARRAY_SIZE(usba_ep); i++)
2008                 usba_ep_init_debugfs(udc, &usba_ep[i]);
2009
2010         return 0;
2011
2012 err_device_add:
2013         free_irq(irq, udc);
2014 err_request_irq:
2015         iounmap(udc->fifo);
2016 err_map_fifo:
2017         iounmap(udc->regs);
2018 err_map_regs:
2019         clk_put(hclk);
2020 err_get_hclk:
2021         clk_put(pclk);
2022
2023         platform_set_drvdata(pdev, NULL);
2024
2025         return ret;
2026 }
2027
2028 static int __exit usba_udc_remove(struct platform_device *pdev)
2029 {
2030         struct usba_udc *udc;
2031         int i;
2032
2033         udc = platform_get_drvdata(pdev);
2034
2035         for (i = 1; i < ARRAY_SIZE(usba_ep); i++)
2036                 usba_ep_cleanup_debugfs(&usba_ep[i]);
2037         usba_cleanup_debugfs(udc);
2038
2039         if (udc->vbus_pin != -1)
2040                 gpio_free(udc->vbus_pin);
2041
2042         free_irq(udc->irq, udc);
2043         iounmap(udc->fifo);
2044         iounmap(udc->regs);
2045         clk_put(udc->hclk);
2046         clk_put(udc->pclk);
2047
2048         device_unregister(&udc->gadget.dev);
2049
2050         return 0;
2051 }
2052
2053 static struct platform_driver udc_driver = {
2054         .remove         = __exit_p(usba_udc_remove),
2055         .driver         = {
2056                 .name           = "atmel_usba_udc",
2057         },
2058 };
2059
2060 static int __init udc_init(void)
2061 {
2062         return platform_driver_probe(&udc_driver, usba_udc_probe);
2063 }
2064 module_init(udc_init);
2065
2066 static void __exit udc_exit(void)
2067 {
2068         platform_driver_unregister(&udc_driver);
2069 }
2070 module_exit(udc_exit);
2071
2072 MODULE_DESCRIPTION("Atmel USBA UDC driver");
2073 MODULE_AUTHOR("Haavard Skinnemoen <hskinnemoen@atmel.com>");
2074 MODULE_LICENSE("GPL");