Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]
Version: [ 2.6.11.8 ] [ 2.6.25 ] [ 2.6.25.8 ] [ 2.6.31.13 ] Architecture: [ i386 ]

Diff markup

Differences between /linux/drivers/usb/gadget/at91_udc.c (Version 2.6.25) and /linux/drivers/usb/gadget/at91_udc.c (Version 2.6.31.13)


  1 /*                                                  1 /*
  2  * at91_udc -- driver for at91-series USB peri      2  * at91_udc -- driver for at91-series USB peripheral controller
  3  *                                                  3  *
  4  * Copyright (C) 2004 by Thomas Rathbone            4  * Copyright (C) 2004 by Thomas Rathbone
  5  * Copyright (C) 2005 by HP Labs                    5  * Copyright (C) 2005 by HP Labs
  6  * Copyright (C) 2005 by David Brownell             6  * Copyright (C) 2005 by David Brownell
  7  *                                                  7  *
  8  * This program is free software; you can redi      8  * This program is free software; you can redistribute it and/or modify
  9  * it under the terms of the GNU General Publi      9  * it under the terms of the GNU General Public License as published by
 10  * the Free Software Foundation; either versio     10  * the Free Software Foundation; either version 2 of the License, or
 11  * (at your option) any later version.             11  * (at your option) any later version.
 12  *                                                 12  *
 13  * This program is distributed in the hope tha     13  * This program is distributed in the hope that it will be useful,
 14  * but WITHOUT ANY WARRANTY; without even the      14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR     15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 16  * GNU General Public License for more details     16  * GNU General Public License for more details.
 17  *                                                 17  *
 18  * You should have received a copy of the GNU      18  * You should have received a copy of the GNU General Public License
 19  * along with this program; if not, write to t     19  * along with this program; if not, write to the
 20  * Free Software Foundation, Inc., 59 Temple P     20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 21  * Boston, MA  02111-1307, USA.                    21  * Boston, MA  02111-1307, USA.
 22  */                                                22  */
 23                                                    23 
 24 #undef  VERBOSE_DEBUG                              24 #undef  VERBOSE_DEBUG
 25 #undef  PACKET_TRACE                               25 #undef  PACKET_TRACE
 26                                                    26 
 27 #include <linux/kernel.h>                          27 #include <linux/kernel.h>
 28 #include <linux/module.h>                          28 #include <linux/module.h>
 29 #include <linux/platform_device.h>                 29 #include <linux/platform_device.h>
 30 #include <linux/delay.h>                           30 #include <linux/delay.h>
 31 #include <linux/ioport.h>                          31 #include <linux/ioport.h>
 32 #include <linux/slab.h>                            32 #include <linux/slab.h>
 33 #include <linux/errno.h>                           33 #include <linux/errno.h>
 34 #include <linux/init.h>                            34 #include <linux/init.h>
 35 #include <linux/list.h>                            35 #include <linux/list.h>
 36 #include <linux/interrupt.h>                       36 #include <linux/interrupt.h>
 37 #include <linux/proc_fs.h>                         37 #include <linux/proc_fs.h>
 38 #include <linux/clk.h>                             38 #include <linux/clk.h>
 39 #include <linux/usb/ch9.h>                         39 #include <linux/usb/ch9.h>
 40 #include <linux/usb/gadget.h>                      40 #include <linux/usb/gadget.h>
 41                                                    41 
 42 #include <asm/byteorder.h>                         42 #include <asm/byteorder.h>
 43 #include <asm/hardware.h>                      !!  43 #include <mach/hardware.h>
 44 #include <asm/io.h>                                44 #include <asm/io.h>
 45 #include <asm/irq.h>                               45 #include <asm/irq.h>
 46 #include <asm/system.h>                            46 #include <asm/system.h>
 47 #include <asm/mach-types.h>                    << 
 48 #include <asm/gpio.h>                              47 #include <asm/gpio.h>
 49                                                    48 
 50 #include <asm/arch/board.h>                    !!  49 #include <mach/board.h>
 51 #include <asm/arch/cpu.h>                      !!  50 #include <mach/cpu.h>
 52 #include <asm/arch/at91sam9261_matrix.h>       !!  51 #include <mach/at91sam9261_matrix.h>
 53                                                    52 
 54 #include "at91_udc.h"                              53 #include "at91_udc.h"
 55                                                    54 
 56                                                    55 
 57 /*                                                 56 /*
 58  * This controller is simple and PIO-only.  It     57  * This controller is simple and PIO-only.  It's used in many AT91-series
 59  * full speed USB controllers, including the a     58  * full speed USB controllers, including the at91rm9200 (arm920T, with MMU),
 60  * at91sam926x (arm926ejs, with MMU), and seve     59  * at91sam926x (arm926ejs, with MMU), and several no-mmu versions.
 61  *                                                 60  *
 62  * This driver expects the board has been wire     61  * This driver expects the board has been wired with two GPIOs suppporting
 63  * a VBUS sensing IRQ, and a D+ pullup.  (They     62  * a VBUS sensing IRQ, and a D+ pullup.  (They may be omitted, but the
 64  * testing hasn't covered such cases.)             63  * testing hasn't covered such cases.)
 65  *                                                 64  *
 66  * The pullup is most important (so it's integ     65  * The pullup is most important (so it's integrated on sam926x parts).  It
 67  * provides software control over whether the      66  * provides software control over whether the host enumerates the device.
 68  *                                                 67  *
 69  * The VBUS sensing helps during enumeration,      68  * The VBUS sensing helps during enumeration, and allows both USB clocks
 70  * (and the transceiver) to stay gated off unt     69  * (and the transceiver) to stay gated off until they're necessary, saving
 71  * power.  During USB suspend, the 48 MHz cloc     70  * power.  During USB suspend, the 48 MHz clock is gated off in hardware;
 72  * it may also be gated off by software during     71  * it may also be gated off by software during some Linux sleep states.
 73  */                                                72  */
 74                                                    73 
 75 #define DRIVER_VERSION  "3 May 2006"               74 #define DRIVER_VERSION  "3 May 2006"
 76                                                    75 
 77 static const char driver_name [] = "at91_udc";     76 static const char driver_name [] = "at91_udc";
 78 static const char ep0name[] = "ep0";               77 static const char ep0name[] = "ep0";
 79                                                    78 
 80                                                    79 
 81 #define at91_udp_read(dev, reg) \                  80 #define at91_udp_read(dev, reg) \
 82         __raw_readl((dev)->udp_baseaddr + (reg     81         __raw_readl((dev)->udp_baseaddr + (reg))
 83 #define at91_udp_write(dev, reg, val) \            82 #define at91_udp_write(dev, reg, val) \
 84         __raw_writel((val), (dev)->udp_baseadd     83         __raw_writel((val), (dev)->udp_baseaddr + (reg))
 85                                                    84 
 86 /*--------------------------------------------     85 /*-------------------------------------------------------------------------*/
 87                                                    86 
 88 #ifdef CONFIG_USB_GADGET_DEBUG_FILES               87 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
 89                                                    88 
 90 #include <linux/seq_file.h>                        89 #include <linux/seq_file.h>
 91                                                    90 
 92 static const char debug_filename[] = "driver/u     91 static const char debug_filename[] = "driver/udc";
 93                                                    92 
 94 #define FOURBITS "%s%s%s%s"                        93 #define FOURBITS "%s%s%s%s"
 95 #define EIGHTBITS FOURBITS FOURBITS                94 #define EIGHTBITS FOURBITS FOURBITS
 96                                                    95 
 97 static void proc_ep_show(struct seq_file *s, s     96 static void proc_ep_show(struct seq_file *s, struct at91_ep *ep)
 98 {                                                  97 {
 99         static char             *types[] = {       98         static char             *types[] = {
100                 "control", "out-iso", "out-bul     99                 "control", "out-iso", "out-bulk", "out-int",
101                 "BOGUS",   "in-iso",  "in-bulk    100                 "BOGUS",   "in-iso",  "in-bulk",  "in-int"};
102                                                   101 
103         u32                     csr;              102         u32                     csr;
104         struct at91_request     *req;             103         struct at91_request     *req;
105         unsigned long   flags;                    104         unsigned long   flags;
106                                                   105 
107         local_irq_save(flags);                    106         local_irq_save(flags);
108                                                   107 
109         csr = __raw_readl(ep->creg);              108         csr = __raw_readl(ep->creg);
110                                                   109 
111         /* NOTE:  not collecting per-endpoint     110         /* NOTE:  not collecting per-endpoint irq statistics... */
112                                                   111 
113         seq_printf(s, "\n");                      112         seq_printf(s, "\n");
114         seq_printf(s, "%s, maxpacket %d %s%s %    113         seq_printf(s, "%s, maxpacket %d %s%s %s%s\n",
115                         ep->ep.name, ep->ep.ma    114                         ep->ep.name, ep->ep.maxpacket,
116                         ep->is_in ? "in" : "ou    115                         ep->is_in ? "in" : "out",
117                         ep->is_iso ? " iso" :     116                         ep->is_iso ? " iso" : "",
118                         ep->is_pingpong           117                         ep->is_pingpong
119                                 ? (ep->fifo_ba    118                                 ? (ep->fifo_bank ? "pong" : "ping")
120                                 : "",             119                                 : "",
121                         ep->stopped ? " stoppe    120                         ep->stopped ? " stopped" : "");
122         seq_printf(s, "csr %08x rxbytes=%d %s     121         seq_printf(s, "csr %08x rxbytes=%d %s %s %s" EIGHTBITS "\n",
123                 csr,                              122                 csr,
124                 (csr & 0x07ff0000) >> 16,         123                 (csr & 0x07ff0000) >> 16,
125                 (csr & (1 << 15)) ? "enabled"     124                 (csr & (1 << 15)) ? "enabled" : "disabled",
126                 (csr & (1 << 11)) ? "DATA1" :     125                 (csr & (1 << 11)) ? "DATA1" : "DATA0",
127                 types[(csr & 0x700) >> 8],        126                 types[(csr & 0x700) >> 8],
128                                                   127 
129                 /* iff type is control then pr    128                 /* iff type is control then print current direction */
130                 (!(csr & 0x700))                  129                 (!(csr & 0x700))
131                         ? ((csr & (1 << 7)) ?     130                         ? ((csr & (1 << 7)) ? " IN" : " OUT")
132                         : "",                     131                         : "",
133                 (csr & (1 << 6)) ? " rxdatabk1    132                 (csr & (1 << 6)) ? " rxdatabk1" : "",
134                 (csr & (1 << 5)) ? " forcestal    133                 (csr & (1 << 5)) ? " forcestall" : "",
135                 (csr & (1 << 4)) ? " txpktrdy"    134                 (csr & (1 << 4)) ? " txpktrdy" : "",
136                                                   135 
137                 (csr & (1 << 3)) ? " stallsent    136                 (csr & (1 << 3)) ? " stallsent" : "",
138                 (csr & (1 << 2)) ? " rxsetup"     137                 (csr & (1 << 2)) ? " rxsetup" : "",
139                 (csr & (1 << 1)) ? " rxdatabk0    138                 (csr & (1 << 1)) ? " rxdatabk0" : "",
140                 (csr & (1 << 0)) ? " txcomp" :    139                 (csr & (1 << 0)) ? " txcomp" : "");
141         if (list_empty (&ep->queue))              140         if (list_empty (&ep->queue))
142                 seq_printf(s, "\t(queue empty)    141                 seq_printf(s, "\t(queue empty)\n");
143                                                   142 
144         else list_for_each_entry (req, &ep->qu    143         else list_for_each_entry (req, &ep->queue, queue) {
145                 unsigned        length = req->    144                 unsigned        length = req->req.actual;
146                                                   145 
147                 seq_printf(s, "\treq %p len %d    146                 seq_printf(s, "\treq %p len %d/%d buf %p\n",
148                                 &req->req, len    147                                 &req->req, length,
149                                 req->req.lengt    148                                 req->req.length, req->req.buf);
150         }                                         149         }
151         local_irq_restore(flags);                 150         local_irq_restore(flags);
152 }                                                 151 }
153                                                   152 
154 static void proc_irq_show(struct seq_file *s,     153 static void proc_irq_show(struct seq_file *s, const char *label, u32 mask)
155 {                                                 154 {
156         int i;                                    155         int i;
157                                                   156 
158         seq_printf(s, "%s %04x:%s%s" FOURBITS,    157         seq_printf(s, "%s %04x:%s%s" FOURBITS, label, mask,
159                 (mask & (1 << 13)) ? " wakeup"    158                 (mask & (1 << 13)) ? " wakeup" : "",
160                 (mask & (1 << 12)) ? " endbusr    159                 (mask & (1 << 12)) ? " endbusres" : "",
161                                                   160 
162                 (mask & (1 << 11)) ? " sofint"    161                 (mask & (1 << 11)) ? " sofint" : "",
163                 (mask & (1 << 10)) ? " extrsm"    162                 (mask & (1 << 10)) ? " extrsm" : "",
164                 (mask & (1 << 9)) ? " rxrsm" :    163                 (mask & (1 << 9)) ? " rxrsm" : "",
165                 (mask & (1 << 8)) ? " rxsusp"     164                 (mask & (1 << 8)) ? " rxsusp" : "");
166         for (i = 0; i < 8; i++) {                 165         for (i = 0; i < 8; i++) {
167                 if (mask & (1 << i))              166                 if (mask & (1 << i))
168                         seq_printf(s, " ep%d",    167                         seq_printf(s, " ep%d", i);
169         }                                         168         }
170         seq_printf(s, "\n");                      169         seq_printf(s, "\n");
171 }                                                 170 }
172                                                   171 
173 static int proc_udc_show(struct seq_file *s, v    172 static int proc_udc_show(struct seq_file *s, void *unused)
174 {                                                 173 {
175         struct at91_udc *udc = s->private;        174         struct at91_udc *udc = s->private;
176         struct at91_ep  *ep;                      175         struct at91_ep  *ep;
177         u32             tmp;                      176         u32             tmp;
178                                                   177 
179         seq_printf(s, "%s: version %s\n", driv    178         seq_printf(s, "%s: version %s\n", driver_name, DRIVER_VERSION);
180                                                   179 
181         seq_printf(s, "vbus %s, pullup %s, %s     180         seq_printf(s, "vbus %s, pullup %s, %s powered%s, gadget %s\n\n",
182                 udc->vbus ? "present" : "off",    181                 udc->vbus ? "present" : "off",
183                 udc->enabled                      182                 udc->enabled
184                         ? (udc->vbus ? "active    183                         ? (udc->vbus ? "active" : "enabled")
185                         : "disabled",             184                         : "disabled",
186                 udc->selfpowered ? "self" : "V    185                 udc->selfpowered ? "self" : "VBUS",
187                 udc->suspended ? ", suspended"    186                 udc->suspended ? ", suspended" : "",
188                 udc->driver ? udc->driver->dri    187                 udc->driver ? udc->driver->driver.name : "(none)");
189                                                   188 
190         /* don't access registers when interfa    189         /* don't access registers when interface isn't clocked */
191         if (!udc->clocked) {                      190         if (!udc->clocked) {
192                 seq_printf(s, "(not clocked)\n    191                 seq_printf(s, "(not clocked)\n");
193                 return 0;                         192                 return 0;
194         }                                         193         }
195                                                   194 
196         tmp = at91_udp_read(udc, AT91_UDP_FRM_    195         tmp = at91_udp_read(udc, AT91_UDP_FRM_NUM);
197         seq_printf(s, "frame %05x:%s%s frame=%    196         seq_printf(s, "frame %05x:%s%s frame=%d\n", tmp,
198                 (tmp & AT91_UDP_FRM_OK) ? " ok    197                 (tmp & AT91_UDP_FRM_OK) ? " ok" : "",
199                 (tmp & AT91_UDP_FRM_ERR) ? " e    198                 (tmp & AT91_UDP_FRM_ERR) ? " err" : "",
200                 (tmp & AT91_UDP_NUM));            199                 (tmp & AT91_UDP_NUM));
201                                                   200 
202         tmp = at91_udp_read(udc, AT91_UDP_GLB_    201         tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT);
203         seq_printf(s, "glbstate %02x:%s" FOURB    202         seq_printf(s, "glbstate %02x:%s" FOURBITS "\n", tmp,
204                 (tmp & AT91_UDP_RMWUPE) ? " rm    203                 (tmp & AT91_UDP_RMWUPE) ? " rmwupe" : "",
205                 (tmp & AT91_UDP_RSMINPR) ? " r    204                 (tmp & AT91_UDP_RSMINPR) ? " rsminpr" : "",
206                 (tmp & AT91_UDP_ESR) ? " esr"     205                 (tmp & AT91_UDP_ESR) ? " esr" : "",
207                 (tmp & AT91_UDP_CONFG) ? " con    206                 (tmp & AT91_UDP_CONFG) ? " confg" : "",
208                 (tmp & AT91_UDP_FADDEN) ? " fa    207                 (tmp & AT91_UDP_FADDEN) ? " fadden" : "");
209                                                   208 
210         tmp = at91_udp_read(udc, AT91_UDP_FADD    209         tmp = at91_udp_read(udc, AT91_UDP_FADDR);
211         seq_printf(s, "faddr   %03x:%s fadd=%d    210         seq_printf(s, "faddr   %03x:%s fadd=%d\n", tmp,
212                 (tmp & AT91_UDP_FEN) ? " fen"     211                 (tmp & AT91_UDP_FEN) ? " fen" : "",
213                 (tmp & AT91_UDP_FADD));           212                 (tmp & AT91_UDP_FADD));
214                                                   213 
215         proc_irq_show(s, "imr   ", at91_udp_re    214         proc_irq_show(s, "imr   ", at91_udp_read(udc, AT91_UDP_IMR));
216         proc_irq_show(s, "isr   ", at91_udp_re    215         proc_irq_show(s, "isr   ", at91_udp_read(udc, AT91_UDP_ISR));
217                                                   216 
218         if (udc->enabled && udc->vbus) {          217         if (udc->enabled && udc->vbus) {
219                 proc_ep_show(s, &udc->ep[0]);     218                 proc_ep_show(s, &udc->ep[0]);
220                 list_for_each_entry (ep, &udc-    219                 list_for_each_entry (ep, &udc->gadget.ep_list, ep.ep_list) {
221                         if (ep->desc)             220                         if (ep->desc)
222                                 proc_ep_show(s    221                                 proc_ep_show(s, ep);
223                 }                                 222                 }
224         }                                         223         }
225         return 0;                                 224         return 0;
226 }                                                 225 }
227                                                   226 
228 static int proc_udc_open(struct inode *inode,     227 static int proc_udc_open(struct inode *inode, struct file *file)
229 {                                                 228 {
230         return single_open(file, proc_udc_show    229         return single_open(file, proc_udc_show, PDE(inode)->data);
231 }                                                 230 }
232                                                   231 
233 static const struct file_operations proc_ops =    232 static const struct file_operations proc_ops = {
                                                   >> 233         .owner          = THIS_MODULE,
234         .open           = proc_udc_open,          234         .open           = proc_udc_open,
235         .read           = seq_read,               235         .read           = seq_read,
236         .llseek         = seq_lseek,              236         .llseek         = seq_lseek,
237         .release        = single_release,         237         .release        = single_release,
238 };                                                238 };
239                                                   239 
240 static void create_debug_file(struct at91_udc     240 static void create_debug_file(struct at91_udc *udc)
241 {                                                 241 {
242         struct proc_dir_entry *pde;            !! 242         udc->pde = proc_create_data(debug_filename, 0, NULL, &proc_ops, udc);
243                                                << 
244         pde = create_proc_entry (debug_filenam << 
245         udc->pde = pde;                        << 
246         if (pde == NULL)                       << 
247                 return;                        << 
248                                                << 
249         pde->proc_fops = &proc_ops;            << 
250         pde->data = udc;                       << 
251 }                                                 243 }
252                                                   244 
253 static void remove_debug_file(struct at91_udc     245 static void remove_debug_file(struct at91_udc *udc)
254 {                                                 246 {
255         if (udc->pde)                             247         if (udc->pde)
256                 remove_proc_entry(debug_filena    248                 remove_proc_entry(debug_filename, NULL);
257 }                                                 249 }
258                                                   250 
259 #else                                             251 #else
260                                                   252 
261 static inline void create_debug_file(struct at    253 static inline void create_debug_file(struct at91_udc *udc) {}
262 static inline void remove_debug_file(struct at    254 static inline void remove_debug_file(struct at91_udc *udc) {}
263                                                   255 
264 #endif                                            256 #endif
265                                                   257 
266                                                   258 
267 /*--------------------------------------------    259 /*-------------------------------------------------------------------------*/
268                                                   260 
269 static void done(struct at91_ep *ep, struct at    261 static void done(struct at91_ep *ep, struct at91_request *req, int status)
270 {                                                 262 {
271         unsigned        stopped = ep->stopped;    263         unsigned        stopped = ep->stopped;
272         struct at91_udc *udc = ep->udc;           264         struct at91_udc *udc = ep->udc;
273                                                   265 
274         list_del_init(&req->queue);               266         list_del_init(&req->queue);
275         if (req->req.status == -EINPROGRESS)      267         if (req->req.status == -EINPROGRESS)
276                 req->req.status = status;         268                 req->req.status = status;
277         else                                      269         else
278                 status = req->req.status;         270                 status = req->req.status;
279         if (status && status != -ESHUTDOWN)       271         if (status && status != -ESHUTDOWN)
280                 VDBG("%s done %p, status %d\n"    272                 VDBG("%s done %p, status %d\n", ep->ep.name, req, status);
281                                                   273 
282         ep->stopped = 1;                          274         ep->stopped = 1;
283         req->req.complete(&ep->ep, &req->req);    275         req->req.complete(&ep->ep, &req->req);
284         ep->stopped = stopped;                    276         ep->stopped = stopped;
285                                                   277 
286         /* ep0 is always ready; other endpoint    278         /* ep0 is always ready; other endpoints need a non-empty queue */
287         if (list_empty(&ep->queue) && ep->int_    279         if (list_empty(&ep->queue) && ep->int_mask != (1 << 0))
288                 at91_udp_write(udc, AT91_UDP_I    280                 at91_udp_write(udc, AT91_UDP_IDR, ep->int_mask);
289 }                                                 281 }
290                                                   282 
291 /*--------------------------------------------    283 /*-------------------------------------------------------------------------*/
292                                                   284 
293 /* bits indicating OUT fifo has data ready */     285 /* bits indicating OUT fifo has data ready */
294 #define RX_DATA_READY   (AT91_UDP_RX_DATA_BK0     286 #define RX_DATA_READY   (AT91_UDP_RX_DATA_BK0 | AT91_UDP_RX_DATA_BK1)
295                                                   287 
296 /*                                                288 /*
297  * Endpoint FIFO CSR bits have a mix of bits,     289  * Endpoint FIFO CSR bits have a mix of bits, making it unsafe to just write
298  * back most of the value you just read (becau    290  * back most of the value you just read (because of side effects, including
299  * bits that may change after reading and befo    291  * bits that may change after reading and before writing).
300  *                                                292  *
301  * Except when changing a specific bit, always    293  * Except when changing a specific bit, always write values which:
302  *  - clear SET_FX bits (setting them could ch    294  *  - clear SET_FX bits (setting them could change something)
303  *  - set CLR_FX bits (clearing them could cha    295  *  - set CLR_FX bits (clearing them could change something)
304  *                                                296  *
305  * There are also state bits like FORCESTALL,     297  * There are also state bits like FORCESTALL, EPEDS, DIR, and EPTYPE
306  * that shouldn't normally be changed.            298  * that shouldn't normally be changed.
307  *                                                299  *
308  * NOTE at91sam9260 docs mention synch between    300  * NOTE at91sam9260 docs mention synch between UDPCK and MCK clock domains,
309  * implying a need to wait for one write to co    301  * implying a need to wait for one write to complete (test relevant bits)
310  * before starting the next write.  This shoul    302  * before starting the next write.  This shouldn't be an issue given how
311  * infrequently we write, except maybe for wri    303  * infrequently we write, except maybe for write-then-read idioms.
312  */                                               304  */
313 #define SET_FX  (AT91_UDP_TXPKTRDY)               305 #define SET_FX  (AT91_UDP_TXPKTRDY)
314 #define CLR_FX  (RX_DATA_READY | AT91_UDP_RXSE    306 #define CLR_FX  (RX_DATA_READY | AT91_UDP_RXSETUP \
315                 | AT91_UDP_STALLSENT | AT91_UD    307                 | AT91_UDP_STALLSENT | AT91_UDP_TXCOMP)
316                                                   308 
317 /* pull OUT packet data from the endpoint's fi    309 /* pull OUT packet data from the endpoint's fifo */
318 static int read_fifo (struct at91_ep *ep, stru    310 static int read_fifo (struct at91_ep *ep, struct at91_request *req)
319 {                                                 311 {
320         u32 __iomem     *creg = ep->creg;         312         u32 __iomem     *creg = ep->creg;
321         u8 __iomem      *dreg = ep->creg + (AT    313         u8 __iomem      *dreg = ep->creg + (AT91_UDP_FDR(0) - AT91_UDP_CSR(0));
322         u32             csr;                      314         u32             csr;
323         u8              *buf;                     315         u8              *buf;
324         unsigned int    count, bufferspace, is    316         unsigned int    count, bufferspace, is_done;
325                                                   317 
326         buf = req->req.buf + req->req.actual;     318         buf = req->req.buf + req->req.actual;
327         bufferspace = req->req.length - req->r    319         bufferspace = req->req.length - req->req.actual;
328                                                   320 
329         /*                                        321         /*
330          * there might be nothing to read if e    322          * there might be nothing to read if ep_queue() calls us,
331          * or if we already emptied both pingp    323          * or if we already emptied both pingpong buffers
332          */                                       324          */
333 rescan:                                           325 rescan:
334         csr = __raw_readl(creg);                  326         csr = __raw_readl(creg);
335         if ((csr & RX_DATA_READY) == 0)           327         if ((csr & RX_DATA_READY) == 0)
336                 return 0;                         328                 return 0;
337                                                   329 
338         count = (csr & AT91_UDP_RXBYTECNT) >>     330         count = (csr & AT91_UDP_RXBYTECNT) >> 16;
339         if (count > ep->ep.maxpacket)             331         if (count > ep->ep.maxpacket)
340                 count = ep->ep.maxpacket;         332                 count = ep->ep.maxpacket;
341         if (count > bufferspace) {                333         if (count > bufferspace) {
342                 DBG("%s buffer overflow\n", ep    334                 DBG("%s buffer overflow\n", ep->ep.name);
343                 req->req.status = -EOVERFLOW;     335                 req->req.status = -EOVERFLOW;
344                 count = bufferspace;              336                 count = bufferspace;
345         }                                         337         }
346         __raw_readsb(dreg, buf, count);           338         __raw_readsb(dreg, buf, count);
347                                                   339 
348         /* release and swap pingpong mem bank     340         /* release and swap pingpong mem bank */
349         csr |= CLR_FX;                            341         csr |= CLR_FX;
350         if (ep->is_pingpong) {                    342         if (ep->is_pingpong) {
351                 if (ep->fifo_bank == 0) {         343                 if (ep->fifo_bank == 0) {
352                         csr &= ~(SET_FX | AT91    344                         csr &= ~(SET_FX | AT91_UDP_RX_DATA_BK0);
353                         ep->fifo_bank = 1;        345                         ep->fifo_bank = 1;
354                 } else {                          346                 } else {
355                         csr &= ~(SET_FX | AT91    347                         csr &= ~(SET_FX | AT91_UDP_RX_DATA_BK1);
356                         ep->fifo_bank = 0;        348                         ep->fifo_bank = 0;
357                 }                                 349                 }
358         } else                                    350         } else
359                 csr &= ~(SET_FX | AT91_UDP_RX_    351                 csr &= ~(SET_FX | AT91_UDP_RX_DATA_BK0);
360         __raw_writel(csr, creg);                  352         __raw_writel(csr, creg);
361                                                   353 
362         req->req.actual += count;                 354         req->req.actual += count;
363         is_done = (count < ep->ep.maxpacket);     355         is_done = (count < ep->ep.maxpacket);
364         if (count == bufferspace)                 356         if (count == bufferspace)
365                 is_done = 1;                      357                 is_done = 1;
366                                                   358 
367         PACKET("%s %p out/%d%s\n", ep->ep.name    359         PACKET("%s %p out/%d%s\n", ep->ep.name, &req->req, count,
368                         is_done ? " (done)" :     360                         is_done ? " (done)" : "");
369                                                   361 
370         /*                                        362         /*
371          * avoid extra trips through IRQ logic    363          * avoid extra trips through IRQ logic for packets already in
372          * the fifo ... maybe preventing an ex    364          * the fifo ... maybe preventing an extra (expensive) OUT-NAK
373          */                                       365          */
374         if (is_done)                              366         if (is_done)
375                 done(ep, req, 0);                 367                 done(ep, req, 0);
376         else if (ep->is_pingpong) {               368         else if (ep->is_pingpong) {
377                 bufferspace -= count;             369                 bufferspace -= count;
378                 buf += count;                     370                 buf += count;
379                 goto rescan;                      371                 goto rescan;
380         }                                         372         }
381                                                   373 
382         return is_done;                           374         return is_done;
383 }                                                 375 }
384                                                   376 
385 /* load fifo for an IN packet */                  377 /* load fifo for an IN packet */
386 static int write_fifo(struct at91_ep *ep, stru    378 static int write_fifo(struct at91_ep *ep, struct at91_request *req)
387 {                                                 379 {
388         u32 __iomem     *creg = ep->creg;         380         u32 __iomem     *creg = ep->creg;
389         u32             csr = __raw_readl(creg    381         u32             csr = __raw_readl(creg);
390         u8 __iomem      *dreg = ep->creg + (AT    382         u8 __iomem      *dreg = ep->creg + (AT91_UDP_FDR(0) - AT91_UDP_CSR(0));
391         unsigned        total, count, is_last;    383         unsigned        total, count, is_last;
                                                   >> 384         u8              *buf;
392                                                   385 
393         /*                                        386         /*
394          * TODO: allow for writing two packets    387          * TODO: allow for writing two packets to the fifo ... that'll
395          * reduce the amount of IN-NAKing, but    388          * reduce the amount of IN-NAKing, but probably won't affect
396          * throughput much.  (Unlike preventin    389          * throughput much.  (Unlike preventing OUT-NAKing!)
397          */                                       390          */
398                                                   391 
399         /*                                        392         /*
400          * If ep_queue() calls us, the queue i    393          * If ep_queue() calls us, the queue is empty and possibly in
401          * odd states like TXCOMP not yet clea    394          * odd states like TXCOMP not yet cleared (we do it, saving at
402          * least one IRQ) or the fifo not yet     395          * least one IRQ) or the fifo not yet being free.  Those aren't
403          * issues normally (IRQ handler fast p    396          * issues normally (IRQ handler fast path).
404          */                                       397          */
405         if (unlikely(csr & (AT91_UDP_TXCOMP |     398         if (unlikely(csr & (AT91_UDP_TXCOMP | AT91_UDP_TXPKTRDY))) {
406                 if (csr & AT91_UDP_TXCOMP) {      399                 if (csr & AT91_UDP_TXCOMP) {
407                         csr |= CLR_FX;            400                         csr |= CLR_FX;
408                         csr &= ~(SET_FX | AT91    401                         csr &= ~(SET_FX | AT91_UDP_TXCOMP);
409                         __raw_writel(csr, creg    402                         __raw_writel(csr, creg);
410                         csr = __raw_readl(creg    403                         csr = __raw_readl(creg);
411                 }                                 404                 }
412                 if (csr & AT91_UDP_TXPKTRDY)      405                 if (csr & AT91_UDP_TXPKTRDY)
413                         return 0;                 406                         return 0;
414         }                                         407         }
415                                                   408 
                                                   >> 409         buf = req->req.buf + req->req.actual;
                                                   >> 410         prefetch(buf);
416         total = req->req.length - req->req.act    411         total = req->req.length - req->req.actual;
417         if (ep->ep.maxpacket < total) {           412         if (ep->ep.maxpacket < total) {
418                 count = ep->ep.maxpacket;         413                 count = ep->ep.maxpacket;
419                 is_last = 0;                      414                 is_last = 0;
420         } else {                                  415         } else {
421                 count = total;                    416                 count = total;
422                 is_last = (count < ep->ep.maxp    417                 is_last = (count < ep->ep.maxpacket) || !req->req.zero;
423         }                                         418         }
424                                                   419 
425         /*                                        420         /*
426          * Write the packet, maybe it's a ZLP.    421          * Write the packet, maybe it's a ZLP.
427          *                                        422          *
428          * NOTE:  incrementing req->actual bef    423          * NOTE:  incrementing req->actual before we receive the ACK means
429          * gadget driver IN bytecounts can be     424          * gadget driver IN bytecounts can be wrong in fault cases.  That's
430          * fixable with PIO drivers like this     425          * fixable with PIO drivers like this one (save "count" here, and
431          * do the increment later on TX irq),     426          * do the increment later on TX irq), but not for most DMA hardware.
432          *                                        427          *
433          * So all gadget drivers must accept t    428          * So all gadget drivers must accept that potential error.  Some
434          * hardware supports precise fifo stat    429          * hardware supports precise fifo status reporting, letting them
435          * recover when the actual bytecount m    430          * recover when the actual bytecount matters (e.g. for USB Test
436          * and Measurement Class devices).        431          * and Measurement Class devices).
437          */                                       432          */
438         __raw_writesb(dreg, req->req.buf + req !! 433         __raw_writesb(dreg, buf, count);
439         csr &= ~SET_FX;                           434         csr &= ~SET_FX;
440         csr |= CLR_FX | AT91_UDP_TXPKTRDY;        435         csr |= CLR_FX | AT91_UDP_TXPKTRDY;
441         __raw_writel(csr, creg);                  436         __raw_writel(csr, creg);
442         req->req.actual += count;                 437         req->req.actual += count;
443                                                   438 
444         PACKET("%s %p in/%d%s\n", ep->ep.name,    439         PACKET("%s %p in/%d%s\n", ep->ep.name, &req->req, count,
445                         is_last ? " (done)" :     440                         is_last ? " (done)" : "");
446         if (is_last)                              441         if (is_last)
447                 done(ep, req, 0);                 442                 done(ep, req, 0);
448         return is_last;                           443         return is_last;
449 }                                                 444 }
450                                                   445 
451 static void nuke(struct at91_ep *ep, int statu    446 static void nuke(struct at91_ep *ep, int status)
452 {                                                 447 {
453         struct at91_request *req;                 448         struct at91_request *req;
454                                                   449 
455         // terminer chaque requete dans la que    450         // terminer chaque requete dans la queue
456         ep->stopped = 1;                          451         ep->stopped = 1;
457         if (list_empty(&ep->queue))               452         if (list_empty(&ep->queue))
458                 return;                           453                 return;
459                                                   454 
460         VDBG("%s %s\n", __FUNCTION__, ep->ep.n !! 455         VDBG("%s %s\n", __func__, ep->ep.name);
461         while (!list_empty(&ep->queue)) {         456         while (!list_empty(&ep->queue)) {
462                 req = list_entry(ep->queue.nex    457                 req = list_entry(ep->queue.next, struct at91_request, queue);
463                 done(ep, req, status);            458                 done(ep, req, status);
464         }                                         459         }
465 }                                                 460 }
466                                                   461 
467 /*--------------------------------------------    462 /*-------------------------------------------------------------------------*/
468                                                   463 
469 static int at91_ep_enable(struct usb_ep *_ep,     464 static int at91_ep_enable(struct usb_ep *_ep,
470                                 const struct u    465                                 const struct usb_endpoint_descriptor *desc)
471 {                                                 466 {
472         struct at91_ep  *ep = container_of(_ep    467         struct at91_ep  *ep = container_of(_ep, struct at91_ep, ep);
473         struct at91_udc *dev = ep->udc;           468         struct at91_udc *dev = ep->udc;
474         u16             maxpacket;                469         u16             maxpacket;
475         u32             tmp;                      470         u32             tmp;
476         unsigned long   flags;                    471         unsigned long   flags;
477                                                   472 
478         if (!_ep || !ep                           473         if (!_ep || !ep
479                         || !desc || ep->desc      474                         || !desc || ep->desc
480                         || _ep->name == ep0nam    475                         || _ep->name == ep0name
481                         || desc->bDescriptorTy    476                         || desc->bDescriptorType != USB_DT_ENDPOINT
482                         || (maxpacket = le16_t    477                         || (maxpacket = le16_to_cpu(desc->wMaxPacketSize)) == 0
483                         || maxpacket > ep->max    478                         || maxpacket > ep->maxpacket) {
484                 DBG("bad ep or descriptor\n");    479                 DBG("bad ep or descriptor\n");
485                 return -EINVAL;                   480                 return -EINVAL;
486         }                                         481         }
487                                                   482 
488         if (!dev->driver || dev->gadget.speed     483         if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
489                 DBG("bogus device state\n");      484                 DBG("bogus device state\n");
490                 return -ESHUTDOWN;                485                 return -ESHUTDOWN;
491         }                                         486         }
492                                                   487 
493         tmp = desc->bmAttributes & USB_ENDPOIN !! 488         tmp = usb_endpoint_type(desc);
494         switch (tmp) {                            489         switch (tmp) {
495         case USB_ENDPOINT_XFER_CONTROL:           490         case USB_ENDPOINT_XFER_CONTROL:
496                 DBG("only one control endpoint    491                 DBG("only one control endpoint\n");
497                 return -EINVAL;                   492                 return -EINVAL;
498         case USB_ENDPOINT_XFER_INT:               493         case USB_ENDPOINT_XFER_INT:
499                 if (maxpacket > 64)               494                 if (maxpacket > 64)
500                         goto bogus_max;           495                         goto bogus_max;
501                 break;                            496                 break;
502         case USB_ENDPOINT_XFER_BULK:              497         case USB_ENDPOINT_XFER_BULK:
503                 switch (maxpacket) {              498                 switch (maxpacket) {
504                 case 8:                           499                 case 8:
505                 case 16:                          500                 case 16:
506                 case 32:                          501                 case 32:
507                 case 64:                          502                 case 64:
508                         goto ok;                  503                         goto ok;
509                 }                                 504                 }
510 bogus_max:                                        505 bogus_max:
511                 DBG("bogus maxpacket %d\n", ma    506                 DBG("bogus maxpacket %d\n", maxpacket);
512                 return -EINVAL;                   507                 return -EINVAL;
513         case USB_ENDPOINT_XFER_ISOC:              508         case USB_ENDPOINT_XFER_ISOC:
514                 if (!ep->is_pingpong) {           509                 if (!ep->is_pingpong) {
515                         DBG("iso requires doub    510                         DBG("iso requires double buffering\n");
516                         return -EINVAL;           511                         return -EINVAL;
517                 }                                 512                 }
518                 break;                            513                 break;
519         }                                         514         }
520                                                   515 
521 ok:                                               516 ok:
522         local_irq_save(flags);                    517         local_irq_save(flags);
523                                                   518 
524         /* initialize endpoint to match this d    519         /* initialize endpoint to match this descriptor */
525         ep->is_in = (desc->bEndpointAddress &  !! 520         ep->is_in = usb_endpoint_dir_in(desc);
526         ep->is_iso = (tmp == USB_ENDPOINT_XFER    521         ep->is_iso = (tmp == USB_ENDPOINT_XFER_ISOC);
527         ep->stopped = 0;                          522         ep->stopped = 0;
528         if (ep->is_in)                            523         if (ep->is_in)
529                 tmp |= 0x04;                      524                 tmp |= 0x04;
530         tmp <<= 8;                                525         tmp <<= 8;
531         tmp |= AT91_UDP_EPEDS;                    526         tmp |= AT91_UDP_EPEDS;
532         __raw_writel(tmp, ep->creg);              527         __raw_writel(tmp, ep->creg);
533                                                   528 
534         ep->desc = desc;                          529         ep->desc = desc;
535         ep->ep.maxpacket = maxpacket;             530         ep->ep.maxpacket = maxpacket;
536                                                   531 
537         /*                                        532         /*
538          * reset/init endpoint fifo.  NOTE:  l    533          * reset/init endpoint fifo.  NOTE:  leaves fifo_bank alone,
539          * since endpoint resets don't reset h    534          * since endpoint resets don't reset hw pingpong state.
540          */                                       535          */
541         at91_udp_write(dev, AT91_UDP_RST_EP, e    536         at91_udp_write(dev, AT91_UDP_RST_EP, ep->int_mask);
542         at91_udp_write(dev, AT91_UDP_RST_EP, 0    537         at91_udp_write(dev, AT91_UDP_RST_EP, 0);
543                                                   538 
544         local_irq_restore(flags);                 539         local_irq_restore(flags);
545         return 0;                                 540         return 0;
546 }                                                 541 }
547                                                   542 
548 static int at91_ep_disable (struct usb_ep * _e    543 static int at91_ep_disable (struct usb_ep * _ep)
549 {                                                 544 {
550         struct at91_ep  *ep = container_of(_ep    545         struct at91_ep  *ep = container_of(_ep, struct at91_ep, ep);
551         struct at91_udc *udc = ep->udc;           546         struct at91_udc *udc = ep->udc;
552         unsigned long   flags;                    547         unsigned long   flags;
553                                                   548 
554         if (ep == &ep->udc->ep[0])                549         if (ep == &ep->udc->ep[0])
555                 return -EINVAL;                   550                 return -EINVAL;
556                                                   551 
557         local_irq_save(flags);                    552         local_irq_save(flags);
558                                                   553 
559         nuke(ep, -ESHUTDOWN);                     554         nuke(ep, -ESHUTDOWN);
560                                                   555 
561         /* restore the endpoint's pristine con    556         /* restore the endpoint's pristine config */
562         ep->desc = NULL;                          557         ep->desc = NULL;
563         ep->ep.maxpacket = ep->maxpacket;         558         ep->ep.maxpacket = ep->maxpacket;
564                                                   559 
565         /* reset fifos and endpoint */            560         /* reset fifos and endpoint */
566         if (ep->udc->clocked) {                   561         if (ep->udc->clocked) {
567                 at91_udp_write(udc, AT91_UDP_R    562                 at91_udp_write(udc, AT91_UDP_RST_EP, ep->int_mask);
568                 at91_udp_write(udc, AT91_UDP_R    563                 at91_udp_write(udc, AT91_UDP_RST_EP, 0);
569                 __raw_writel(0, ep->creg);        564                 __raw_writel(0, ep->creg);
570         }                                         565         }
571                                                   566 
572         local_irq_restore(flags);                 567         local_irq_restore(flags);
573         return 0;                                 568         return 0;
574 }                                                 569 }
575                                                   570 
576 /*                                                571 /*
577  * this is a PIO-only driver, so there's nothi    572  * this is a PIO-only driver, so there's nothing
578  * interesting for request or buffer allocatio    573  * interesting for request or buffer allocation.
579  */                                               574  */
580                                                   575 
581 static struct usb_request *                       576 static struct usb_request *
582 at91_ep_alloc_request(struct usb_ep *_ep, gfp_    577 at91_ep_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
583 {                                                 578 {
584         struct at91_request *req;                 579         struct at91_request *req;
585                                                   580 
586         req = kzalloc(sizeof (struct at91_requ    581         req = kzalloc(sizeof (struct at91_request), gfp_flags);
587         if (!req)                                 582         if (!req)
588                 return NULL;                      583                 return NULL;
589                                                   584 
590         INIT_LIST_HEAD(&req->queue);              585         INIT_LIST_HEAD(&req->queue);
591         return &req->req;                         586         return &req->req;
592 }                                                 587 }
593                                                   588 
594 static void at91_ep_free_request(struct usb_ep    589 static void at91_ep_free_request(struct usb_ep *_ep, struct usb_request *_req)
595 {                                                 590 {
596         struct at91_request *req;                 591         struct at91_request *req;
597                                                   592 
598         req = container_of(_req, struct at91_r    593         req = container_of(_req, struct at91_request, req);
599         BUG_ON(!list_empty(&req->queue));         594         BUG_ON(!list_empty(&req->queue));
600         kfree(req);                               595         kfree(req);
601 }                                                 596 }
602                                                   597 
603 static int at91_ep_queue(struct usb_ep *_ep,      598 static int at91_ep_queue(struct usb_ep *_ep,
604                         struct usb_request *_r    599                         struct usb_request *_req, gfp_t gfp_flags)
605 {                                                 600 {
606         struct at91_request     *req;             601         struct at91_request     *req;
607         struct at91_ep          *ep;              602         struct at91_ep          *ep;
608         struct at91_udc         *dev;             603         struct at91_udc         *dev;
609         int                     status;           604         int                     status;
610         unsigned long           flags;            605         unsigned long           flags;
611                                                   606 
612         req = container_of(_req, struct at91_r    607         req = container_of(_req, struct at91_request, req);
613         ep = container_of(_ep, struct at91_ep,    608         ep = container_of(_ep, struct at91_ep, ep);
614                                                   609 
615         if (!_req || !_req->complete              610         if (!_req || !_req->complete
616                         || !_req->buf || !list    611                         || !_req->buf || !list_empty(&req->queue)) {
617                 DBG("invalid request\n");         612                 DBG("invalid request\n");
618                 return -EINVAL;                   613                 return -EINVAL;
619         }                                         614         }
620                                                   615 
621         if (!_ep || (!ep->desc && ep->ep.name     616         if (!_ep || (!ep->desc && ep->ep.name != ep0name)) {
622                 DBG("invalid ep\n");              617                 DBG("invalid ep\n");
623                 return -EINVAL;                   618                 return -EINVAL;
624         }                                         619         }
625                                                   620 
626         dev = ep->udc;                            621         dev = ep->udc;
627                                                   622 
628         if (!dev || !dev->driver || dev->gadge    623         if (!dev || !dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
629                 DBG("invalid device\n");          624                 DBG("invalid device\n");
630                 return -EINVAL;                   625                 return -EINVAL;
631         }                                         626         }
632                                                   627 
633         _req->status = -EINPROGRESS;              628         _req->status = -EINPROGRESS;
634         _req->actual = 0;                         629         _req->actual = 0;
635                                                   630 
636         local_irq_save(flags);                    631         local_irq_save(flags);
637                                                   632 
638         /* try to kickstart any empty and idle    633         /* try to kickstart any empty and idle queue */
639         if (list_empty(&ep->queue) && !ep->sto    634         if (list_empty(&ep->queue) && !ep->stopped) {
640                 int     is_ep0;                   635                 int     is_ep0;
641                                                   636 
642                 /*                                637                 /*
643                  * If this control request has    638                  * If this control request has a non-empty DATA stage, this
644                  * will start that stage.  It     639                  * will start that stage.  It works just like a non-control
645                  * request (until the status s    640                  * request (until the status stage starts, maybe early).
646                  *                                641                  *
647                  * If the data stage is empty,    642                  * If the data stage is empty, then this starts a successful
648                  * IN/STATUS stage.  (Unsucces    643                  * IN/STATUS stage.  (Unsuccessful ones use set_halt.)
649                  */                               644                  */
650                 is_ep0 = (ep->ep.name == ep0na    645                 is_ep0 = (ep->ep.name == ep0name);
651                 if (is_ep0) {                     646                 if (is_ep0) {
652                         u32     tmp;              647                         u32     tmp;
653                                                   648 
654                         if (!dev->req_pending)    649                         if (!dev->req_pending) {
655                                 status = -EINV    650                                 status = -EINVAL;
656                                 goto done;        651                                 goto done;
657                         }                         652                         }
658                                                   653 
659                         /*                        654                         /*
660                          * defer changing CONF    655                          * defer changing CONFG until after the gadget driver
661                          * reconfigures the en    656                          * reconfigures the endpoints.
662                          */                       657                          */
663                         if (dev->wait_for_conf    658                         if (dev->wait_for_config_ack) {
664                                 tmp = at91_udp    659                                 tmp = at91_udp_read(dev, AT91_UDP_GLB_STAT);
665                                 tmp ^= AT91_UD    660                                 tmp ^= AT91_UDP_CONFG;
666                                 VDBG("toggle c    661                                 VDBG("toggle config\n");
667                                 at91_udp_write    662                                 at91_udp_write(dev, AT91_UDP_GLB_STAT, tmp);
668                         }                         663                         }
669                         if (req->req.length ==    664                         if (req->req.length == 0) {
670 ep0_in_status:                                    665 ep0_in_status:
671                                 PACKET("ep0 in    666                                 PACKET("ep0 in/status\n");
672                                 status = 0;       667                                 status = 0;
673                                 tmp = __raw_re    668                                 tmp = __raw_readl(ep->creg);
674                                 tmp &= ~SET_FX    669                                 tmp &= ~SET_FX;
675                                 tmp |= CLR_FX     670                                 tmp |= CLR_FX | AT91_UDP_TXPKTRDY;
676                                 __raw_writel(t    671                                 __raw_writel(tmp, ep->creg);
677                                 dev->req_pendi    672                                 dev->req_pending = 0;
678                                 goto done;        673                                 goto done;
679                         }                         674                         }
680                 }                                 675                 }
681                                                   676 
682                 if (ep->is_in)                    677                 if (ep->is_in)
683                         status = write_fifo(ep    678                         status = write_fifo(ep, req);
684                 else {                            679                 else {
685                         status = read_fifo(ep,    680                         status = read_fifo(ep, req);
686                                                   681 
687                         /* IN/STATUS stage is     682                         /* IN/STATUS stage is otherwise triggered by irq */
688                         if (status && is_ep0)     683                         if (status && is_ep0)
689                                 goto ep0_in_st    684                                 goto ep0_in_status;
690                 }                                 685                 }
691         } else                                    686         } else
692                 status = 0;                       687                 status = 0;
693                                                   688 
694         if (req && !status) {                     689         if (req && !status) {
695                 list_add_tail (&req->queue, &e    690                 list_add_tail (&req->queue, &ep->queue);
696                 at91_udp_write(dev, AT91_UDP_I    691                 at91_udp_write(dev, AT91_UDP_IER, ep->int_mask);
697         }                                         692         }
698 done:                                             693 done:
699         local_irq_restore(flags);                 694         local_irq_restore(flags);
700         return (status < 0) ? status : 0;         695         return (status < 0) ? status : 0;
701 }                                                 696 }
702                                                   697 
703 static int at91_ep_dequeue(struct usb_ep *_ep,    698 static int at91_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
704 {                                                 699 {
705         struct at91_ep  *ep;                      700         struct at91_ep  *ep;
706         struct at91_request     *req;             701         struct at91_request     *req;
707                                                   702 
708         ep = container_of(_ep, struct at91_ep,    703         ep = container_of(_ep, struct at91_ep, ep);
709         if (!_ep || ep->ep.name == ep0name)       704         if (!_ep || ep->ep.name == ep0name)
710                 return -EINVAL;                   705                 return -EINVAL;
711                                                   706 
712         /* make sure it's actually queued on t    707         /* make sure it's actually queued on this endpoint */
713         list_for_each_entry (req, &ep->queue,     708         list_for_each_entry (req, &ep->queue, queue) {
714                 if (&req->req == _req)            709                 if (&req->req == _req)
715                         break;                    710                         break;
716         }                                         711         }
717         if (&req->req != _req)                    712         if (&req->req != _req)
718                 return -EINVAL;                   713                 return -EINVAL;
719                                                   714 
720         done(ep, req, -ECONNRESET);               715         done(ep, req, -ECONNRESET);
721         return 0;                                 716         return 0;
722 }                                                 717 }
723                                                   718 
724 static int at91_ep_set_halt(struct usb_ep *_ep    719 static int at91_ep_set_halt(struct usb_ep *_ep, int value)
725 {                                                 720 {
726         struct at91_ep  *ep = container_of(_ep    721         struct at91_ep  *ep = container_of(_ep, struct at91_ep, ep);
727         struct at91_udc *udc = ep->udc;           722         struct at91_udc *udc = ep->udc;
728         u32 __iomem     *creg;                    723         u32 __iomem     *creg;
729         u32             csr;                      724         u32             csr;
730         unsigned long   flags;                    725         unsigned long   flags;
731         int             status = 0;               726         int             status = 0;
732                                                   727 
733         if (!_ep || ep->is_iso || !ep->udc->cl    728         if (!_ep || ep->is_iso || !ep->udc->clocked)
734                 return -EINVAL;                   729                 return -EINVAL;
735                                                   730 
736         creg = ep->creg;                          731         creg = ep->creg;
737         local_irq_save(flags);                    732         local_irq_save(flags);
738                                                   733 
739         csr = __raw_readl(creg);                  734         csr = __raw_readl(creg);
740                                                   735 
741         /*                                        736         /*
742          * fail with still-busy IN endpoints,     737          * fail with still-busy IN endpoints, ensuring correct sequencing
743          * of data tx then stall.  note that t    738          * of data tx then stall.  note that the fifo rx bytecount isn't
744          * completely accurate as a tx bytecou    739          * completely accurate as a tx bytecount.
745          */                                       740          */
746         if (ep->is_in && (!list_empty(&ep->que    741         if (ep->is_in && (!list_empty(&ep->queue) || (csr >> 16) != 0))
747                 status = -EAGAIN;                 742                 status = -EAGAIN;
748         else {                                    743         else {
749                 csr |= CLR_FX;                    744                 csr |= CLR_FX;
750                 csr &= ~SET_FX;                   745                 csr &= ~SET_FX;
751                 if (value) {                      746                 if (value) {
752                         csr |= AT91_UDP_FORCES    747                         csr |= AT91_UDP_FORCESTALL;
753                         VDBG("halt %s\n", ep->    748                         VDBG("halt %s\n", ep->ep.name);
754                 } else {                          749                 } else {
755                         at91_udp_write(udc, AT    750                         at91_udp_write(udc, AT91_UDP_RST_EP, ep->int_mask);
756                         at91_udp_write(udc, AT    751                         at91_udp_write(udc, AT91_UDP_RST_EP, 0);
757                         csr &= ~AT91_UDP_FORCE    752                         csr &= ~AT91_UDP_FORCESTALL;
758                 }                                 753                 }
759                 __raw_writel(csr, creg);          754                 __raw_writel(csr, creg);
760         }                                         755         }
761                                                   756 
762         local_irq_restore(flags);                 757         local_irq_restore(flags);
763         return status;                            758         return status;
764 }                                                 759 }
765                                                   760 
766 static const struct usb_ep_ops at91_ep_ops = {    761 static const struct usb_ep_ops at91_ep_ops = {
767         .enable         = at91_ep_enable,         762         .enable         = at91_ep_enable,
768         .disable        = at91_ep_disable,        763         .disable        = at91_ep_disable,
769         .alloc_request  = at91_ep_alloc_reques    764         .alloc_request  = at91_ep_alloc_request,
770         .free_request   = at91_ep_free_request    765         .free_request   = at91_ep_free_request,
771         .queue          = at91_ep_queue,          766         .queue          = at91_ep_queue,
772         .dequeue        = at91_ep_dequeue,        767         .dequeue        = at91_ep_dequeue,
773         .set_halt       = at91_ep_set_halt,       768         .set_halt       = at91_ep_set_halt,
774         // there's only imprecise fifo status     769         // there's only imprecise fifo status reporting
775 };                                                770 };
776                                                   771 
777 /*--------------------------------------------    772 /*-------------------------------------------------------------------------*/
778                                                   773 
779 static int at91_get_frame(struct usb_gadget *g    774 static int at91_get_frame(struct usb_gadget *gadget)
780 {                                                 775 {
781         struct at91_udc *udc = to_udc(gadget);    776         struct at91_udc *udc = to_udc(gadget);
782                                                   777 
783         if (!to_udc(gadget)->clocked)             778         if (!to_udc(gadget)->clocked)
784                 return -EINVAL;                   779                 return -EINVAL;
785         return at91_udp_read(udc, AT91_UDP_FRM    780         return at91_udp_read(udc, AT91_UDP_FRM_NUM) & AT91_UDP_NUM;
786 }                                                 781 }
787                                                   782 
788 static int at91_wakeup(struct usb_gadget *gadg    783 static int at91_wakeup(struct usb_gadget *gadget)
789 {                                                 784 {
790         struct at91_udc *udc = to_udc(gadget);    785         struct at91_udc *udc = to_udc(gadget);
791         u32             glbstate;                 786         u32             glbstate;
792         int             status = -EINVAL;         787         int             status = -EINVAL;
793         unsigned long   flags;                    788         unsigned long   flags;
794                                                   789 
795         DBG("%s\n", __FUNCTION__ );            !! 790         DBG("%s\n", __func__ );
796         local_irq_save(flags);                    791         local_irq_save(flags);
797                                                   792 
798         if (!udc->clocked || !udc->suspended)     793         if (!udc->clocked || !udc->suspended)
799                 goto done;                        794                 goto done;
800                                                   795 
801         /* NOTE:  some "early versions" handle    796         /* NOTE:  some "early versions" handle ESR differently ... */
802                                                   797 
803         glbstate = at91_udp_read(udc, AT91_UDP    798         glbstate = at91_udp_read(udc, AT91_UDP_GLB_STAT);
804         if (!(glbstate & AT91_UDP_ESR))           799         if (!(glbstate & AT91_UDP_ESR))
805                 goto done;                        800                 goto done;
806         glbstate |= AT91_UDP_ESR;                 801         glbstate |= AT91_UDP_ESR;
807         at91_udp_write(udc, AT91_UDP_GLB_STAT,    802         at91_udp_write(udc, AT91_UDP_GLB_STAT, glbstate);
808                                                   803 
809 done:                                             804 done:
810         local_irq_restore(flags);                 805         local_irq_restore(flags);
811         return status;                            806         return status;
812 }                                                 807 }
813                                                   808 
814 /* reinit == restore inital software state */     809 /* reinit == restore inital software state */
815 static void udc_reinit(struct at91_udc *udc)      810 static void udc_reinit(struct at91_udc *udc)
816 {                                                 811 {
817         u32 i;                                    812         u32 i;
818                                                   813 
819         INIT_LIST_HEAD(&udc->gadget.ep_list);     814         INIT_LIST_HEAD(&udc->gadget.ep_list);
820         INIT_LIST_HEAD(&udc->gadget.ep0->ep_li    815         INIT_LIST_HEAD(&udc->gadget.ep0->ep_list);
821                                                   816 
822         for (i = 0; i < NUM_ENDPOINTS; i++) {     817         for (i = 0; i < NUM_ENDPOINTS; i++) {
823                 struct at91_ep *ep = &udc->ep[    818                 struct at91_ep *ep = &udc->ep[i];
824                                                   819 
825                 if (i != 0)                       820                 if (i != 0)
826                         list_add_tail(&ep->ep.    821                         list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
827                 ep->desc = NULL;                  822                 ep->desc = NULL;
828                 ep->stopped = 0;                  823                 ep->stopped = 0;
829                 ep->fifo_bank = 0;                824                 ep->fifo_bank = 0;
830                 ep->ep.maxpacket = ep->maxpack    825                 ep->ep.maxpacket = ep->maxpacket;
831                 ep->creg = (void __iomem *) ud    826                 ep->creg = (void __iomem *) udc->udp_baseaddr + AT91_UDP_CSR(i);
832                 // initialiser une queue par e    827                 // initialiser une queue par endpoint
833                 INIT_LIST_HEAD(&ep->queue);       828                 INIT_LIST_HEAD(&ep->queue);
834         }                                         829         }
835 }                                                 830 }
836                                                   831 
837 static void stop_activity(struct at91_udc *udc    832 static void stop_activity(struct at91_udc *udc)
838 {                                                 833 {
839         struct usb_gadget_driver *driver = udc    834         struct usb_gadget_driver *driver = udc->driver;
840         int i;                                    835         int i;
841                                                   836 
842         if (udc->gadget.speed == USB_SPEED_UNK    837         if (udc->gadget.speed == USB_SPEED_UNKNOWN)
843                 driver = NULL;                    838                 driver = NULL;
844         udc->gadget.speed = USB_SPEED_UNKNOWN;    839         udc->gadget.speed = USB_SPEED_UNKNOWN;
845         udc->suspended = 0;                       840         udc->suspended = 0;
846                                                   841 
847         for (i = 0; i < NUM_ENDPOINTS; i++) {     842         for (i = 0; i < NUM_ENDPOINTS; i++) {
848                 struct at91_ep *ep = &udc->ep[    843                 struct at91_ep *ep = &udc->ep[i];
849                 ep->stopped = 1;                  844                 ep->stopped = 1;
850                 nuke(ep, -ESHUTDOWN);             845                 nuke(ep, -ESHUTDOWN);
851         }                                         846         }
852         if (driver)                               847         if (driver)
853                 driver->disconnect(&udc->gadge    848                 driver->disconnect(&udc->gadget);
854                                                   849 
855         udc_reinit(udc);                          850         udc_reinit(udc);
856 }                                                 851 }
857                                                   852 
858 static void clk_on(struct at91_udc *udc)          853 static void clk_on(struct at91_udc *udc)
859 {                                                 854 {
860         if (udc->clocked)                         855         if (udc->clocked)
861                 return;                           856                 return;
862         udc->clocked = 1;                         857         udc->clocked = 1;
863         clk_enable(udc->iclk);                    858         clk_enable(udc->iclk);
864         clk_enable(udc->fclk);                    859         clk_enable(udc->fclk);
865 }                                                 860 }
866                                                   861 
867 static void clk_off(struct at91_udc *udc)         862 static void clk_off(struct at91_udc *udc)
868 {                                                 863 {
869         if (!udc->clocked)                        864         if (!udc->clocked)
870                 return;                           865                 return;
871         udc->clocked = 0;                         866         udc->clocked = 0;
872         udc->gadget.speed = USB_SPEED_UNKNOWN;    867         udc->gadget.speed = USB_SPEED_UNKNOWN;
873         clk_disable(udc->fclk);                   868         clk_disable(udc->fclk);
874         clk_disable(udc->iclk);                   869         clk_disable(udc->iclk);
875 }                                                 870 }
876                                                   871 
877 /*                                                872 /*
878  * activate/deactivate link with host; minimiz    873  * activate/deactivate link with host; minimize power usage for
879  * inactive links by cutting clocks and transc    874  * inactive links by cutting clocks and transceiver power.
880  */                                               875  */
881 static void pullup(struct at91_udc *udc, int i    876 static void pullup(struct at91_udc *udc, int is_on)
882 {                                                 877 {
883         int     active = !udc->board.pullup_ac    878         int     active = !udc->board.pullup_active_low;
884                                                   879 
885         if (!udc->enabled || !udc->vbus)          880         if (!udc->enabled || !udc->vbus)
886                 is_on = 0;                        881                 is_on = 0;
887         DBG("%sactive\n", is_on ? "" : "in");     882         DBG("%sactive\n", is_on ? "" : "in");
888                                                   883 
889         if (is_on) {                              884         if (is_on) {
890                 clk_on(udc);                      885                 clk_on(udc);
891                 at91_udp_write(udc, AT91_UDP_I    886                 at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_RXRSM);
892                 at91_udp_write(udc, AT91_UDP_T    887                 at91_udp_write(udc, AT91_UDP_TXVC, 0);
893                 if (cpu_is_at91rm9200())          888                 if (cpu_is_at91rm9200())
894                         gpio_set_value(udc->bo    889                         gpio_set_value(udc->board.pullup_pin, active);
895                 else if (cpu_is_at91sam9260()  !! 890                 else if (cpu_is_at91sam9260() || cpu_is_at91sam9263() || cpu_is_at91sam9g20()) {
896                         u32     txvc = at91_ud    891                         u32     txvc = at91_udp_read(udc, AT91_UDP_TXVC);
897                                                   892 
898                         txvc |= AT91_UDP_TXVC_    893                         txvc |= AT91_UDP_TXVC_PUON;
899                         at91_udp_write(udc, AT    894                         at91_udp_write(udc, AT91_UDP_TXVC, txvc);
900                 } else if (cpu_is_at91sam9261(    895                 } else if (cpu_is_at91sam9261()) {
901                         u32     usbpucr;          896                         u32     usbpucr;
902                                                   897 
903                         usbpucr = at91_sys_rea    898                         usbpucr = at91_sys_read(AT91_MATRIX_USBPUCR);
904                         usbpucr |= AT91_MATRIX    899                         usbpucr |= AT91_MATRIX_USBPUCR_PUON;
905                         at91_sys_write(AT91_MA    900                         at91_sys_write(AT91_MATRIX_USBPUCR, usbpucr);
906                 }                                 901                 }
907         } else {                                  902         } else {
908                 stop_activity(udc);               903                 stop_activity(udc);
909                 at91_udp_write(udc, AT91_UDP_I    904                 at91_udp_write(udc, AT91_UDP_IDR, AT91_UDP_RXRSM);
910                 at91_udp_write(udc, AT91_UDP_T    905                 at91_udp_write(udc, AT91_UDP_TXVC, AT91_UDP_TXVC_TXVDIS);
911                 if (cpu_is_at91rm9200())          906                 if (cpu_is_at91rm9200())
912                         gpio_set_value(udc->bo    907                         gpio_set_value(udc->board.pullup_pin, !active);
913                 else if (cpu_is_at91sam9260()  !! 908                 else if (cpu_is_at91sam9260() || cpu_is_at91sam9263() || cpu_is_at91sam9g20()) {
914                         u32     txvc = at91_ud    909                         u32     txvc = at91_udp_read(udc, AT91_UDP_TXVC);
915                                                   910 
916                         txvc &= ~AT91_UDP_TXVC    911                         txvc &= ~AT91_UDP_TXVC_PUON;
917                         at91_udp_write(udc, AT    912                         at91_udp_write(udc, AT91_UDP_TXVC, txvc);
918                 } else if (cpu_is_at91sam9261(    913                 } else if (cpu_is_at91sam9261()) {
919                         u32     usbpucr;          914                         u32     usbpucr;
920                                                   915 
921                         usbpucr = at91_sys_rea    916                         usbpucr = at91_sys_read(AT91_MATRIX_USBPUCR);
922                         usbpucr &= ~AT91_MATRI    917                         usbpucr &= ~AT91_MATRIX_USBPUCR_PUON;
923                         at91_sys_write(AT91_MA    918                         at91_sys_write(AT91_MATRIX_USBPUCR, usbpucr);
924                 }                                 919                 }
925                 clk_off(udc);                     920                 clk_off(udc);
926         }                                         921         }
927 }                                                 922 }
928                                                   923 
929 /* vbus is here!  turn everything on that's re    924 /* vbus is here!  turn everything on that's ready */
930 static int at91_vbus_session(struct usb_gadget    925 static int at91_vbus_session(struct usb_gadget *gadget, int is_active)
931 {                                                 926 {
932         struct at91_udc *udc = to_udc(gadget);    927         struct at91_udc *udc = to_udc(gadget);
933         unsigned long   flags;                    928         unsigned long   flags;
934                                                   929 
935         // VDBG("vbus %s\n", is_active ? "on"     930         // VDBG("vbus %s\n", is_active ? "on" : "off");
936         local_irq_save(flags);                    931         local_irq_save(flags);
937         udc->vbus = (is_active != 0);             932         udc->vbus = (is_active != 0);
938         if (udc->driver)                          933         if (udc->driver)
939                 pullup(udc, is_active);           934                 pullup(udc, is_active);
940         else                                      935         else
941                 pullup(udc, 0);                   936                 pullup(udc, 0);
942         local_irq_restore(flags);                 937         local_irq_restore(flags);
943         return 0;                                 938         return 0;
944 }                                                 939 }
945                                                   940 
946 static int at91_pullup(struct usb_gadget *gadg    941 static int at91_pullup(struct usb_gadget *gadget, int is_on)
947 {                                                 942 {
948         struct at91_udc *udc = to_udc(gadget);    943         struct at91_udc *udc = to_udc(gadget);
949         unsigned long   flags;                    944         unsigned long   flags;
950                                                   945 
951         local_irq_save(flags);                    946         local_irq_save(flags);
952         udc->enabled = is_on = !!is_on;           947         udc->enabled = is_on = !!is_on;
953         pullup(udc, is_on);                       948         pullup(udc, is_on);
954         local_irq_restore(flags);                 949         local_irq_restore(flags);
955         return 0;                                 950         return 0;
956 }                                                 951 }
957                                                   952 
958 static int at91_set_selfpowered(struct usb_gad    953 static int at91_set_selfpowered(struct usb_gadget *gadget, int is_on)
959 {                                                 954 {
960         struct at91_udc *udc = to_udc(gadget);    955         struct at91_udc *udc = to_udc(gadget);
961         unsigned long   flags;                    956         unsigned long   flags;
962                                                   957 
963         local_irq_save(flags);                    958         local_irq_save(flags);
964         udc->selfpowered = (is_on != 0);          959         udc->selfpowered = (is_on != 0);
965         local_irq_restore(flags);                 960         local_irq_restore(flags);
966         return 0;                                 961         return 0;
967 }                                                 962 }
968                                                   963 
969 static const struct usb_gadget_ops at91_udc_op    964 static const struct usb_gadget_ops at91_udc_ops = {
970         .get_frame              = at91_get_fra    965         .get_frame              = at91_get_frame,
971         .wakeup                 = at91_wakeup,    966         .wakeup                 = at91_wakeup,
972         .set_selfpowered        = at91_set_sel    967         .set_selfpowered        = at91_set_selfpowered,
973         .vbus_session           = at91_vbus_se    968         .vbus_session           = at91_vbus_session,
974         .pullup                 = at91_pullup,    969         .pullup                 = at91_pullup,
975                                                   970 
976         /*                                        971         /*
977          * VBUS-powered devices may also also     972          * VBUS-powered devices may also also want to support bigger
978          * power budgets after an appropriate     973          * power budgets after an appropriate SET_CONFIGURATION.
979          */                                       974          */
980         // .vbus_power          = at91_vbus_po    975         // .vbus_power          = at91_vbus_power,
981 };                                                976 };
982                                                   977 
983 /*--------------------------------------------    978 /*-------------------------------------------------------------------------*/
984                                                   979 
985 static int handle_ep(struct at91_ep *ep)          980 static int handle_ep(struct at91_ep *ep)
986 {                                                 981 {
987         struct at91_request     *req;             982         struct at91_request     *req;
988         u32 __iomem             *creg = ep->cr    983         u32 __iomem             *creg = ep->creg;
989         u32                     csr = __raw_re    984         u32                     csr = __raw_readl(creg);
990                                                   985 
991         if (!list_empty(&ep->queue))              986         if (!list_empty(&ep->queue))
992                 req = list_entry(ep->queue.nex    987                 req = list_entry(ep->queue.next,
993                         struct at91_request, q    988                         struct at91_request, queue);
994         else                                      989         else
995                 req = NULL;                       990                 req = NULL;
996                                                   991 
997         if (ep->is_in) {                          992         if (ep->is_in) {
998                 if (csr & (AT91_UDP_STALLSENT     993                 if (csr & (AT91_UDP_STALLSENT | AT91_UDP_TXCOMP)) {
999                         csr |= CLR_FX;            994                         csr |= CLR_FX;
1000                         csr &= ~(SET_FX | AT9    995                         csr &= ~(SET_FX | AT91_UDP_STALLSENT | AT91_UDP_TXCOMP);
1001                         __raw_writel(csr, cre    996                         __raw_writel(csr, creg);
1002                 }                                997                 }
1003                 if (req)                         998                 if (req)
1004                         return write_fifo(ep,    999                         return write_fifo(ep, req);
1005                                                  1000 
1006         } else {                                 1001         } else {
1007                 if (csr & AT91_UDP_STALLSENT)    1002                 if (csr & AT91_UDP_STALLSENT) {
1008                         /* STALLSENT bit == I    1003                         /* STALLSENT bit == ISOERR */
1009                         if (ep->is_iso && req    1004                         if (ep->is_iso && req)
1010                                 req->req.stat    1005                                 req->req.status = -EILSEQ;
1011                         csr |= CLR_FX;           1006                         csr |= CLR_FX;
1012                         csr &= ~(SET_FX | AT9    1007                         csr &= ~(SET_FX | AT91_UDP_STALLSENT);
1013                         __raw_writel(csr, cre    1008                         __raw_writel(csr, creg);
1014                         csr = __raw_readl(cre    1009                         csr = __raw_readl(creg);
1015                 }                                1010                 }
1016                 if (req && (csr & RX_DATA_REA    1011                 if (req && (csr & RX_DATA_READY))
1017                         return read_fifo(ep,     1012                         return read_fifo(ep, req);
1018         }                                        1013         }
1019         return 0;                                1014         return 0;
1020 }                                                1015 }
1021                                                  1016 
1022 union setup {                                    1017 union setup {
1023         u8                      raw[8];          1018         u8                      raw[8];
1024         struct usb_ctrlrequest  r;               1019         struct usb_ctrlrequest  r;
1025 };                                               1020 };
1026                                                  1021 
1027 static void handle_setup(struct at91_udc *udc    1022 static void handle_setup(struct at91_udc *udc, struct at91_ep *ep, u32 csr)
1028 {                                                1023 {
1029         u32 __iomem     *creg = ep->creg;        1024         u32 __iomem     *creg = ep->creg;
1030         u8 __iomem      *dreg = ep->creg + (A    1025         u8 __iomem      *dreg = ep->creg + (AT91_UDP_FDR(0) - AT91_UDP_CSR(0));
1031         unsigned        rxcount, i = 0;          1026         unsigned        rxcount, i = 0;
1032         u32             tmp;                     1027         u32             tmp;
1033         union setup     pkt;                     1028         union setup     pkt;
1034         int             status = 0;              1029         int             status = 0;
1035                                                  1030 
1036         /* read and ack SETUP; hard-fail for     1031         /* read and ack SETUP; hard-fail for bogus packets */
1037         rxcount = (csr & AT91_UDP_RXBYTECNT)     1032         rxcount = (csr & AT91_UDP_RXBYTECNT) >> 16;
1038         if (likely(rxcount == 8)) {              1033         if (likely(rxcount == 8)) {
1039                 while (rxcount--)                1034                 while (rxcount--)
1040                         pkt.raw[i++] = __raw_    1035                         pkt.raw[i++] = __raw_readb(dreg);
1041                 if (pkt.r.bRequestType & USB_    1036                 if (pkt.r.bRequestType & USB_DIR_IN) {
1042                         csr |= AT91_UDP_DIR;     1037                         csr |= AT91_UDP_DIR;
1043                         ep->is_in = 1;           1038                         ep->is_in = 1;
1044                 } else {                         1039                 } else {
1045                         csr &= ~AT91_UDP_DIR;    1040                         csr &= ~AT91_UDP_DIR;
1046                         ep->is_in = 0;           1041                         ep->is_in = 0;
1047                 }                                1042                 }
1048         } else {                                 1043         } else {
1049                 // REVISIT this happens somet    1044                 // REVISIT this happens sometimes under load; why??
1050                 ERR("SETUP len %d, csr %08x\n    1045                 ERR("SETUP len %d, csr %08x\n", rxcount, csr);
1051                 status = -EINVAL;                1046                 status = -EINVAL;
1052         }                                        1047         }
1053         csr |= CLR_FX;                           1048         csr |= CLR_FX;
1054         csr &= ~(SET_FX | AT91_UDP_RXSETUP);     1049         csr &= ~(SET_FX | AT91_UDP_RXSETUP);
1055         __raw_writel(csr, creg);                 1050         __raw_writel(csr, creg);
1056         udc->wait_for_addr_ack = 0;              1051         udc->wait_for_addr_ack = 0;
1057         udc->wait_for_config_ack = 0;            1052         udc->wait_for_config_ack = 0;
1058         ep->stopped = 0;                         1053         ep->stopped = 0;
1059         if (unlikely(status != 0))               1054         if (unlikely(status != 0))
1060                 goto stall;                      1055                 goto stall;
1061                                                  1056 
1062 #define w_index         le16_to_cpu(pkt.r.wIn    1057 #define w_index         le16_to_cpu(pkt.r.wIndex)
1063 #define w_value         le16_to_cpu(pkt.r.wVa    1058 #define w_value         le16_to_cpu(pkt.r.wValue)
1064 #define w_length        le16_to_cpu(pkt.r.wLe    1059 #define w_length        le16_to_cpu(pkt.r.wLength)
1065                                                  1060 
1066         VDBG("SETUP %02x.%02x v%04x i%04x l%0    1061         VDBG("SETUP %02x.%02x v%04x i%04x l%04x\n",
1067                         pkt.r.bRequestType, p    1062                         pkt.r.bRequestType, pkt.r.bRequest,
1068                         w_value, w_index, w_l    1063                         w_value, w_index, w_length);
1069                                                  1064 
1070         /*                                       1065         /*
1071          * A few standard requests get handle    1066          * A few standard requests get handled here, ones that touch
1072          * hardware ... notably for device an    1067          * hardware ... notably for device and endpoint features.
1073          */                                      1068          */
1074         udc->req_pending = 1;                    1069         udc->req_pending = 1;
1075         csr = __raw_readl(creg);                 1070         csr = __raw_readl(creg);
1076         csr |= CLR_FX;                           1071         csr |= CLR_FX;
1077         csr &= ~SET_FX;                          1072         csr &= ~SET_FX;
1078         switch ((pkt.r.bRequestType << 8) | p    1073         switch ((pkt.r.bRequestType << 8) | pkt.r.bRequest) {
1079                                                  1074 
1080         case ((USB_TYPE_STANDARD|USB_RECIP_DE    1075         case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
1081                         | USB_REQ_SET_ADDRESS    1076                         | USB_REQ_SET_ADDRESS:
1082                 __raw_writel(csr | AT91_UDP_T    1077                 __raw_writel(csr | AT91_UDP_TXPKTRDY, creg);
1083                 udc->addr = w_value;             1078                 udc->addr = w_value;
1084                 udc->wait_for_addr_ack = 1;      1079                 udc->wait_for_addr_ack = 1;
1085                 udc->req_pending = 0;            1080                 udc->req_pending = 0;
1086                 /* FADDR is set later, when w    1081                 /* FADDR is set later, when we ack host STATUS */
1087                 return;                          1082                 return;
1088                                                  1083 
1089         case ((USB_TYPE_STANDARD|USB_RECIP_DE    1084         case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
1090                         | USB_REQ_SET_CONFIGU    1085                         | USB_REQ_SET_CONFIGURATION:
1091                 tmp = at91_udp_read(udc, AT91    1086                 tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT) & AT91_UDP_CONFG;
1092                 if (pkt.r.wValue)                1087                 if (pkt.r.wValue)
1093                         udc->wait_for_config_    1088                         udc->wait_for_config_ack = (tmp == 0);
1094                 else                             1089                 else
1095                         udc->wait_for_config_    1090                         udc->wait_for_config_ack = (tmp != 0);
1096                 if (udc->wait_for_config_ack)    1091                 if (udc->wait_for_config_ack)
1097                         VDBG("wait for config    1092                         VDBG("wait for config\n");
1098                 /* CONFG is toggled later, if    1093                 /* CONFG is toggled later, if gadget driver succeeds */
1099                 break;                           1094                 break;
1100                                                  1095 
1101         /*                                       1096         /*
1102          * Hosts may set or clear remote wake    1097          * Hosts may set or clear remote wakeup status, and
1103          * devices may report they're VBUS po    1098          * devices may report they're VBUS powered.
1104          */                                      1099          */
1105         case ((USB_DIR_IN|USB_TYPE_STANDARD|U    1100         case ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
1106                         | USB_REQ_GET_STATUS:    1101                         | USB_REQ_GET_STATUS:
1107                 tmp = (udc->selfpowered << US    1102                 tmp = (udc->selfpowered << USB_DEVICE_SELF_POWERED);
1108                 if (at91_udp_read(udc, AT91_U    1103                 if (at91_udp_read(udc, AT91_UDP_GLB_STAT) & AT91_UDP_ESR)
1109                         tmp |= (1 << USB_DEVI    1104                         tmp |= (1 << USB_DEVICE_REMOTE_WAKEUP);
1110                 PACKET("get device status\n")    1105                 PACKET("get device status\n");
1111                 __raw_writeb(tmp, dreg);         1106                 __raw_writeb(tmp, dreg);
1112                 __raw_writeb(0, dreg);           1107                 __raw_writeb(0, dreg);
1113                 goto write_in;                   1108                 goto write_in;
1114                 /* then STATUS starts later,     1109                 /* then STATUS starts later, automatically */
1115         case ((USB_TYPE_STANDARD|USB_RECIP_DE    1110         case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
1116                         | USB_REQ_SET_FEATURE    1111                         | USB_REQ_SET_FEATURE:
1117                 if (w_value != USB_DEVICE_REM    1112                 if (w_value != USB_DEVICE_REMOTE_WAKEUP)
1118                         goto stall;              1113                         goto stall;
1119                 tmp = at91_udp_read(udc, AT91    1114                 tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT);
1120                 tmp |= AT91_UDP_ESR;             1115                 tmp |= AT91_UDP_ESR;
1121                 at91_udp_write(udc, AT91_UDP_    1116                 at91_udp_write(udc, AT91_UDP_GLB_STAT, tmp);
1122                 goto succeed;                    1117                 goto succeed;
1123         case ((USB_TYPE_STANDARD|USB_RECIP_DE    1118         case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
1124                         | USB_REQ_CLEAR_FEATU    1119                         | USB_REQ_CLEAR_FEATURE:
1125                 if (w_value != USB_DEVICE_REM    1120                 if (w_value != USB_DEVICE_REMOTE_WAKEUP)
1126                         goto stall;              1121                         goto stall;
1127                 tmp = at91_udp_read(udc, AT91    1122                 tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT);
1128                 tmp &= ~AT91_UDP_ESR;            1123                 tmp &= ~AT91_UDP_ESR;
1129                 at91_udp_write(udc, AT91_UDP_    1124                 at91_udp_write(udc, AT91_UDP_GLB_STAT, tmp);
1130                 goto succeed;                    1125                 goto succeed;
1131                                                  1126 
1132         /*                                       1127         /*
1133          * Interfaces have no feature setting    1128          * Interfaces have no feature settings; this is pretty useless.
1134          * we won't even insist the interface    1129          * we won't even insist the interface exists...
1135          */                                      1130          */
1136         case ((USB_DIR_IN|USB_TYPE_STANDARD|U    1131         case ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_INTERFACE) << 8)
1137                         | USB_REQ_GET_STATUS:    1132                         | USB_REQ_GET_STATUS:
1138                 PACKET("get interface status\    1133                 PACKET("get interface status\n");
1139                 __raw_writeb(0, dreg);           1134                 __raw_writeb(0, dreg);
1140                 __raw_writeb(0, dreg);           1135                 __raw_writeb(0, dreg);
1141                 goto write_in;                   1136                 goto write_in;
1142                 /* then STATUS starts later,     1137                 /* then STATUS starts later, automatically */
1143         case ((USB_TYPE_STANDARD|USB_RECIP_IN    1138         case ((USB_TYPE_STANDARD|USB_RECIP_INTERFACE) << 8)
1144                         | USB_REQ_SET_FEATURE    1139                         | USB_REQ_SET_FEATURE:
1145         case ((USB_TYPE_STANDARD|USB_RECIP_IN    1140         case ((USB_TYPE_STANDARD|USB_RECIP_INTERFACE) << 8)
1146                         | USB_REQ_CLEAR_FEATU    1141                         | USB_REQ_CLEAR_FEATURE:
1147                 goto stall;                      1142                 goto stall;
1148                                                  1143 
1149         /*                                       1144         /*
1150          * Hosts may clear bulk/intr endpoint    1145          * Hosts may clear bulk/intr endpoint halt after the gadget
1151          * driver sets it (not widely used);     1146          * driver sets it (not widely used); or set it (for testing)
1152          */                                      1147          */
1153         case ((USB_DIR_IN|USB_TYPE_STANDARD|U    1148         case ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_ENDPOINT) << 8)
1154                         | USB_REQ_GET_STATUS:    1149                         | USB_REQ_GET_STATUS:
1155                 tmp = w_index & USB_ENDPOINT_    1150                 tmp = w_index & USB_ENDPOINT_NUMBER_MASK;
1156                 ep = &udc->ep[tmp];              1151                 ep = &udc->ep[tmp];
1157                 if (tmp >= NUM_ENDPOINTS || (    1152                 if (tmp >= NUM_ENDPOINTS || (tmp && !ep->desc))
1158                         goto stall;              1153                         goto stall;
1159                                                  1154 
1160                 if (tmp) {                       1155                 if (tmp) {
1161                         if ((w_index & USB_DI    1156                         if ((w_index & USB_DIR_IN)) {
1162                                 if (!ep->is_i    1157                                 if (!ep->is_in)
1163                                         goto     1158                                         goto stall;
1164                         } else if (ep->is_in)    1159                         } else if (ep->is_in)
1165                                 goto stall;      1160                                 goto stall;
1166                 }                                1161                 }
1167                 PACKET("get %s status\n", ep-    1162                 PACKET("get %s status\n", ep->ep.name);
1168                 if (__raw_readl(ep->creg) & A    1163                 if (__raw_readl(ep->creg) & AT91_UDP_FORCESTALL)
1169                         tmp = (1 << USB_ENDPO    1164                         tmp = (1 << USB_ENDPOINT_HALT);
1170                 else                             1165                 else
1171                         tmp = 0;                 1166                         tmp = 0;
1172                 __raw_writeb(tmp, dreg);         1167                 __raw_writeb(tmp, dreg);
1173                 __raw_writeb(0, dreg);           1168                 __raw_writeb(0, dreg);
1174                 goto write_in;                   1169                 goto write_in;
1175                 /* then STATUS starts later,     1170                 /* then STATUS starts later, automatically */
1176         case ((USB_TYPE_STANDARD|USB_RECIP_EN    1171         case ((USB_TYPE_STANDARD|USB_RECIP_ENDPOINT) << 8)
1177                         | USB_REQ_SET_FEATURE    1172                         | USB_REQ_SET_FEATURE:
1178                 tmp = w_index & USB_ENDPOINT_    1173                 tmp = w_index & USB_ENDPOINT_NUMBER_MASK;
1179                 ep = &udc->ep[tmp];              1174                 ep = &udc->ep[tmp];
1180                 if (w_value != USB_ENDPOINT_H    1175                 if (w_value != USB_ENDPOINT_HALT || tmp >= NUM_ENDPOINTS)
1181                         goto stall;              1176                         goto stall;
1182                 if (!ep->desc || ep->is_iso)     1177                 if (!ep->desc || ep->is_iso)
1183                         goto stall;              1178                         goto stall;
1184                 if ((w_index & USB_DIR_IN)) {    1179                 if ((w_index & USB_DIR_IN)) {
1185                         if (!ep->is_in)          1180                         if (!ep->is_in)
1186                                 goto stall;      1181                                 goto stall;
1187                 } else if (ep->is_in)            1182                 } else if (ep->is_in)
1188                         goto stall;              1183                         goto stall;
1189                                                  1184 
1190                 tmp = __raw_readl(ep->creg);     1185                 tmp = __raw_readl(ep->creg);
1191                 tmp &= ~SET_FX;                  1186                 tmp &= ~SET_FX;
1192                 tmp |= CLR_FX | AT91_UDP_FORC    1187                 tmp |= CLR_FX | AT91_UDP_FORCESTALL;
1193                 __raw_writel(tmp, ep->creg);     1188                 __raw_writel(tmp, ep->creg);
1194                 goto succeed;                    1189                 goto succeed;
1195         case ((USB_TYPE_STANDARD|USB_RECIP_EN    1190         case ((USB_TYPE_STANDARD|USB_RECIP_ENDPOINT) << 8)
1196                         | USB_REQ_CLEAR_FEATU    1191                         | USB_REQ_CLEAR_FEATURE:
1197                 tmp = w_index & USB_ENDPOINT_    1192                 tmp = w_index & USB_ENDPOINT_NUMBER_MASK;
1198                 ep = &udc->ep[tmp];              1193                 ep = &udc->ep[tmp];
1199                 if (w_value != USB_ENDPOINT_H    1194                 if (w_value != USB_ENDPOINT_HALT || tmp >= NUM_ENDPOINTS)
1200                         goto stall;              1195                         goto stall;
1201                 if (tmp == 0)                    1196                 if (tmp == 0)
1202                         goto succeed;            1197                         goto succeed;
1203                 if (!ep->desc || ep->is_iso)     1198                 if (!ep->desc || ep->is_iso)
1204                         goto stall;              1199                         goto stall;
1205                 if ((w_index & USB_DIR_IN)) {    1200                 if ((w_index & USB_DIR_IN)) {
1206                         if (!ep->is_in)          1201                         if (!ep->is_in)
1207                                 goto stall;      1202                                 goto stall;
1208                 } else if (ep->is_in)            1203                 } else if (ep->is_in)
1209                         goto stall;              1204                         goto stall;
1210                                                  1205 
1211                 at91_udp_write(udc, AT91_UDP_    1206                 at91_udp_write(udc, AT91_UDP_RST_EP, ep->int_mask);
1212                 at91_udp_write(udc, AT91_UDP_    1207                 at91_udp_write(udc, AT91_UDP_RST_EP, 0);
1213                 tmp = __raw_readl(ep->creg);     1208                 tmp = __raw_readl(ep->creg);
1214                 tmp |= CLR_FX;                   1209                 tmp |= CLR_FX;
1215                 tmp &= ~(SET_FX | AT91_UDP_FO    1210                 tmp &= ~(SET_FX | AT91_UDP_FORCESTALL);
1216                 __raw_writel(tmp, ep->creg);     1211                 __raw_writel(tmp, ep->creg);
1217                 if (!list_empty(&ep->queue))     1212                 if (!list_empty(&ep->queue))
1218                         handle_ep(ep);           1213                         handle_ep(ep);
1219                 goto succeed;                    1214                 goto succeed;
1220         }                                        1215         }
1221                                                  1216 
1222 #undef w_value                                   1217 #undef w_value
1223 #undef w_index                                   1218 #undef w_index
1224 #undef w_length                                  1219 #undef w_length
1225                                                  1220 
1226         /* pass request up to the gadget driv    1221         /* pass request up to the gadget driver */
1227         if (udc->driver)                         1222         if (udc->driver)
1228                 status = udc->driver->setup(&    1223                 status = udc->driver->setup(&udc->gadget, &pkt.r);
1229         else                                     1224         else
1230                 status = -ENODEV;                1225                 status = -ENODEV;
1231         if (status < 0) {                        1226         if (status < 0) {
1232 stall:                                           1227 stall:
1233                 VDBG("req %02x.%02x protocol     1228                 VDBG("req %02x.%02x protocol STALL; stat %d\n",
1234                                 pkt.r.bReques    1229                                 pkt.r.bRequestType, pkt.r.bRequest, status);
1235                 csr |= AT91_UDP_FORCESTALL;      1230                 csr |= AT91_UDP_FORCESTALL;
1236                 __raw_writel(csr, creg);         1231                 __raw_writel(csr, creg);
1237                 udc->req_pending = 0;            1232                 udc->req_pending = 0;
1238         }                                        1233         }
1239         return;                                  1234         return;
1240                                                  1235 
1241 succeed:                                         1236 succeed:
1242         /* immediate successful (IN) STATUS a    1237         /* immediate successful (IN) STATUS after zero length DATA */
1243         PACKET("ep0 in/status\n");               1238         PACKET("ep0 in/status\n");
1244 write_in:                                        1239 write_in:
1245         csr |= AT91_UDP_TXPKTRDY;                1240         csr |= AT91_UDP_TXPKTRDY;
1246         __raw_writel(csr, creg);                 1241         __raw_writel(csr, creg);
1247         udc->req_pending = 0;                    1242         udc->req_pending = 0;
1248         return;                                  1243         return;
1249 }                                                1244 }
1250                                                  1245 
1251 static void handle_ep0(struct at91_udc *udc)     1246 static void handle_ep0(struct at91_udc *udc)
1252 {                                                1247 {
1253         struct at91_ep          *ep0 = &udc->    1248         struct at91_ep          *ep0 = &udc->ep[0];
1254         u32 __iomem             *creg = ep0->    1249         u32 __iomem             *creg = ep0->creg;
1255         u32                     csr = __raw_r    1250         u32                     csr = __raw_readl(creg);
1256         struct at91_request     *req;            1251         struct at91_request     *req;
1257                                                  1252 
1258         if (unlikely(csr & AT91_UDP_STALLSENT    1253         if (unlikely(csr & AT91_UDP_STALLSENT)) {
1259                 nuke(ep0, -EPROTO);              1254                 nuke(ep0, -EPROTO);
1260                 udc->req_pending = 0;            1255                 udc->req_pending = 0;
1261                 csr |= CLR_FX;                   1256                 csr |= CLR_FX;
1262                 csr &= ~(SET_FX | AT91_UDP_ST    1257                 csr &= ~(SET_FX | AT91_UDP_STALLSENT | AT91_UDP_FORCESTALL);
1263                 __raw_writel(csr, creg);         1258                 __raw_writel(csr, creg);
1264                 VDBG("ep0 stalled\n");           1259                 VDBG("ep0 stalled\n");
1265                 csr = __raw_readl(creg);         1260                 csr = __raw_readl(creg);
1266         }                                        1261         }
1267         if (csr & AT91_UDP_RXSETUP) {            1262         if (csr & AT91_UDP_RXSETUP) {
1268                 nuke(ep0, 0);                    1263                 nuke(ep0, 0);
1269                 udc->req_pending = 0;            1264                 udc->req_pending = 0;
1270                 handle_setup(udc, ep0, csr);     1265                 handle_setup(udc, ep0, csr);
1271                 return;                          1266                 return;
1272         }                                        1267         }
1273                                                  1268 
1274         if (list_empty(&ep0->queue))             1269         if (list_empty(&ep0->queue))
1275                 req = NULL;                      1270                 req = NULL;
1276         else                                     1271         else
1277                 req = list_entry(ep0->queue.n    1272                 req = list_entry(ep0->queue.next, struct at91_request, queue);
1278                                                  1273 
1279         /* host ACKed an IN packet that we se    1274         /* host ACKed an IN packet that we sent */
1280         if (csr & AT91_UDP_TXCOMP) {             1275         if (csr & AT91_UDP_TXCOMP) {
1281                 csr |= CLR_FX;                   1276                 csr |= CLR_FX;
1282                 csr &= ~(SET_FX | AT91_UDP_TX    1277                 csr &= ~(SET_FX | AT91_UDP_TXCOMP);
1283                                                  1278 
1284                 /* write more IN DATA? */        1279                 /* write more IN DATA? */
1285                 if (req && ep0->is_in) {         1280                 if (req && ep0->is_in) {
1286                         if (handle_ep(ep0))      1281                         if (handle_ep(ep0))
1287                                 udc->req_pend    1282                                 udc->req_pending = 0;
1288                                                  1283 
1289                 /*                               1284                 /*
1290                  * Ack after:                    1285                  * Ack after:
1291                  *  - last IN DATA packet (in    1286                  *  - last IN DATA packet (including GET_STATUS)
1292                  *  - IN/STATUS for OUT DATA     1287                  *  - IN/STATUS for OUT DATA
1293                  *  - IN/STATUS for any zero-    1288                  *  - IN/STATUS for any zero-length DATA stage
1294                  * except for the IN DATA cas    1289                  * except for the IN DATA case, the host should send
1295                  * an OUT status later, which    1290                  * an OUT status later, which we'll ack.
1296                  */                              1291                  */
1297                 } else {                         1292                 } else {
1298                         udc->req_pending = 0;    1293                         udc->req_pending = 0;
1299                         __raw_writel(csr, cre    1294                         __raw_writel(csr, creg);
1300                                                  1295 
1301                         /*                       1296                         /*
1302                          * SET_ADDRESS takes     1297                          * SET_ADDRESS takes effect only after the STATUS
1303                          * (to the original a    1298                          * (to the original address) gets acked.
1304                          */                      1299                          */
1305                         if (udc->wait_for_add    1300                         if (udc->wait_for_addr_ack) {
1306                                 u32     tmp;     1301                                 u32     tmp;
1307                                                  1302 
1308                                 at91_udp_writ    1303                                 at91_udp_write(udc, AT91_UDP_FADDR,
1309                                                  1304                                                 AT91_UDP_FEN | udc->addr);
1310                                 tmp = at91_ud    1305                                 tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT);
1311                                 tmp &= ~AT91_    1306                                 tmp &= ~AT91_UDP_FADDEN;
1312                                 if (udc->addr    1307                                 if (udc->addr)
1313                                         tmp |    1308                                         tmp |= AT91_UDP_FADDEN;
1314                                 at91_udp_writ    1309                                 at91_udp_write(udc, AT91_UDP_GLB_STAT, tmp);
1315                                                  1310 
1316                                 udc->wait_for    1311                                 udc->wait_for_addr_ack = 0;
1317                                 VDBG("address    1312                                 VDBG("address %d\n", udc->addr);
1318                         }                        1313                         }
1319                 }                                1314                 }
1320         }                                        1315         }
1321                                                  1316 
1322         /* OUT packet arrived ... */             1317         /* OUT packet arrived ... */
1323         else if (csr & AT91_UDP_RX_DATA_BK0)     1318         else if (csr & AT91_UDP_RX_DATA_BK0) {
1324                 csr |= CLR_FX;                   1319                 csr |= CLR_FX;
1325                 csr &= ~(SET_FX | AT91_UDP_RX    1320                 csr &= ~(SET_FX | AT91_UDP_RX_DATA_BK0);
1326                                                  1321 
1327                 /* OUT DATA stage */             1322                 /* OUT DATA stage */
1328                 if (!ep0->is_in) {               1323                 if (!ep0->is_in) {
1329                         if (req) {               1324                         if (req) {
1330                                 if (handle_ep    1325                                 if (handle_ep(ep0)) {
1331                                         /* se    1326                                         /* send IN/STATUS */
1332                                         PACKE    1327                                         PACKET("ep0 in/status\n");
1333                                         csr =    1328                                         csr = __raw_readl(creg);
1334                                         csr &    1329                                         csr &= ~SET_FX;
1335                                         csr |    1330                                         csr |= CLR_FX | AT91_UDP_TXPKTRDY;
1336                                         __raw    1331                                         __raw_writel(csr, creg);
1337                                         udc->    1332                                         udc->req_pending = 0;
1338                                 }                1333                                 }
1339                         } else if (udc->req_p    1334                         } else if (udc->req_pending) {
1340                                 /*               1335                                 /*
1341                                  * AT91 hardw    1336                                  * AT91 hardware has a hard time with this
1342                                  * "deferred     1337                                  * "deferred response" mode for control-OUT
1343                                  * transfers.    1338                                  * transfers.  (For control-IN it's fine.)
1344                                  *               1339                                  *
1345                                  * The normal    1340                                  * The normal solution leaves OUT data in the
1346                                  * fifo until    1341                                  * fifo until the gadget driver is ready.
1347                                  * We couldn'    1342                                  * We couldn't do that here without disabling
1348                                  * the IRQ th    1343                                  * the IRQ that tells about SETUP packets,
1349                                  * e.g. when     1344                                  * e.g. when the host gets impatient...
1350                                  *               1345                                  *
1351                                  * Working ar    1346                                  * Working around it by copying into a buffer
1352                                  * would almo    1347                                  * would almost be a non-deferred response,
1353                                  * except tha    1348                                  * except that it wouldn't permit reliable
1354                                  * stalling o    1349                                  * stalling of the request.  Instead, demand
1355                                  * that gadge    1350                                  * that gadget drivers not use this mode.
1356                                  */              1351                                  */
1357                                 DBG("no contr    1352                                 DBG("no control-OUT deferred responses!\n");
1358                                 __raw_writel(    1353                                 __raw_writel(csr | AT91_UDP_FORCESTALL, creg);
1359                                 udc->req_pend    1354                                 udc->req_pending = 0;
1360                         }                        1355                         }
1361                                                  1356 
1362                 /* STATUS stage for control-I    1357                 /* STATUS stage for control-IN; ack.  */
1363                 } else {                         1358                 } else {
1364                         PACKET("ep0 out/statu    1359                         PACKET("ep0 out/status ACK\n");
1365                         __raw_writel(csr, cre    1360                         __raw_writel(csr, creg);
1366                                                  1361 
1367                         /* "early" status sta    1362                         /* "early" status stage */
1368                         if (req)                 1363                         if (req)
1369                                 done(ep0, req    1364                                 done(ep0, req, 0);
1370                 }                                1365                 }
1371         }                                        1366         }
1372 }                                                1367 }
1373                                                  1368 
1374 static irqreturn_t at91_udc_irq (int irq, voi    1369 static irqreturn_t at91_udc_irq (int irq, void *_udc)
1375 {                                                1370 {
1376         struct at91_udc         *udc = _udc;     1371         struct at91_udc         *udc = _udc;
1377         u32                     rescans = 5;     1372         u32                     rescans = 5;
1378                                                  1373 
1379         while (rescans--) {                      1374         while (rescans--) {
1380                 u32 status;                      1375                 u32 status;
1381                                                  1376 
1382                 status = at91_udp_read(udc, A    1377                 status = at91_udp_read(udc, AT91_UDP_ISR)
1383                         & at91_udp_read(udc,     1378                         & at91_udp_read(udc, AT91_UDP_IMR);
1384                 if (!status)                     1379                 if (!status)
1385                         break;                   1380                         break;
1386                                                  1381 
1387                 /* USB reset irq:  not maskab    1382                 /* USB reset irq:  not maskable */
1388                 if (status & AT91_UDP_ENDBUSR    1383                 if (status & AT91_UDP_ENDBUSRES) {
1389                         at91_udp_write(udc, A    1384                         at91_udp_write(udc, AT91_UDP_IDR, ~MINIMUS_INTERRUPTUS);
1390                         at91_udp_write(udc, A    1385                         at91_udp_write(udc, AT91_UDP_IER, MINIMUS_INTERRUPTUS);
1391                         /* Atmel code clears     1386                         /* Atmel code clears this irq twice */
1392                         at91_udp_write(udc, A    1387                         at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_ENDBUSRES);
1393                         at91_udp_write(udc, A    1388                         at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_ENDBUSRES);
1394                         VDBG("end bus reset\n    1389                         VDBG("end bus reset\n");
1395                         udc->addr = 0;           1390                         udc->addr = 0;
1396                         stop_activity(udc);      1391                         stop_activity(udc);
1397                                                  1392 
1398                         /* enable ep0 */         1393                         /* enable ep0 */
1399                         at91_udp_write(udc, A    1394                         at91_udp_write(udc, AT91_UDP_CSR(0),
1400                                         AT91_    1395                                         AT91_UDP_EPEDS | AT91_UDP_EPTYPE_CTRL);
1401                         udc->gadget.speed = U    1396                         udc->gadget.speed = USB_SPEED_FULL;
1402                         udc->suspended = 0;      1397                         udc->suspended = 0;
1403                         at91_udp_write(udc, A    1398                         at91_udp_write(udc, AT91_UDP_IER, AT91_UDP_EP(0));
1404                                                  1399 
1405                         /*                       1400                         /*
1406                          * NOTE:  this driver    1401                          * NOTE:  this driver keeps clocks off unless the
1407                          * USB host is presen    1402                          * USB host is present.  That saves power, but for
1408                          * boards that don't     1403                          * boards that don't support VBUS detection, both
1409                          * clocks need to be     1404                          * clocks need to be active most of the time.
1410                          */                      1405                          */
1411                                                  1406 
1412                 /* host initiated suspend (3+    1407                 /* host initiated suspend (3+ms bus idle) */
1413                 } else if (status & AT91_UDP_    1408                 } else if (status & AT91_UDP_RXSUSP) {
1414                         at91_udp_write(udc, A    1409                         at91_udp_write(udc, AT91_UDP_IDR, AT91_UDP_RXSUSP);
1415                         at91_udp_write(udc, A    1410                         at91_udp_write(udc, AT91_UDP_IER, AT91_UDP_RXRSM);
1416                         at91_udp_write(udc, A    1411                         at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_RXSUSP);
1417                         // VDBG("bus suspend\    1412                         // VDBG("bus suspend\n");
1418                         if (udc->suspended)      1413                         if (udc->suspended)
1419                                 continue;        1414                                 continue;
1420                         udc->suspended = 1;      1415                         udc->suspended = 1;
1421                                                  1416 
1422                         /*                       1417                         /*
1423                          * NOTE:  when suspen    1418                          * NOTE:  when suspending a VBUS-powered device, the
1424                          * gadget driver shou    1419                          * gadget driver should switch into slow clock mode
1425                          * and then into stan    1420                          * and then into standby to avoid drawing more than
1426                          * 500uA power (2500u    1421                          * 500uA power (2500uA for some high-power configs).
1427                          */                      1422                          */
1428                         if (udc->driver && ud    1423                         if (udc->driver && udc->driver->suspend)
1429                                 udc->driver->    1424                                 udc->driver->suspend(&udc->gadget);
1430                                                  1425 
1431                 /* host initiated resume */      1426                 /* host initiated resume */
1432                 } else if (status & AT91_UDP_    1427                 } else if (status & AT91_UDP_RXRSM) {
1433                         at91_udp_write(udc, A    1428                         at91_udp_write(udc, AT91_UDP_IDR, AT91_UDP_RXRSM);
1434                         at91_udp_write(udc, A    1429                         at91_udp_write(udc, AT91_UDP_IER, AT91_UDP_RXSUSP);
1435                         at91_udp_write(udc, A    1430                         at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_RXRSM);
1436                         // VDBG("bus resume\n    1431                         // VDBG("bus resume\n");
1437                         if (!udc->suspended)     1432                         if (!udc->suspended)
1438                                 continue;        1433                                 continue;
1439                         udc->suspended = 0;      1434                         udc->suspended = 0;
1440                                                  1435 
1441                         /*                       1436                         /*
1442                          * NOTE:  for a VBUS-    1437                          * NOTE:  for a VBUS-powered device, the gadget driver
1443                          * would normally wan    1438                          * would normally want to switch out of slow clock
1444                          * mode into normal m    1439                          * mode into normal mode.
1445                          */                      1440                          */
1446                         if (udc->driver && ud    1441                         if (udc->driver && udc->driver->resume)
1447                                 udc->driver->    1442                                 udc->driver->resume(&udc->gadget);
1448                                                  1443 
1449                 /* endpoint IRQs are cleared     1444                 /* endpoint IRQs are cleared by handling them */
1450                 } else {                         1445                 } else {
1451                         int             i;       1446                         int             i;
1452                         unsigned        mask     1447                         unsigned        mask = 1;
1453                         struct at91_ep  *ep =    1448                         struct at91_ep  *ep = &udc->ep[1];
1454                                                  1449 
1455                         if (status & mask)       1450                         if (status & mask)
1456                                 handle_ep0(ud    1451                                 handle_ep0(udc);
1457                         for (i = 1; i < NUM_E    1452                         for (i = 1; i < NUM_ENDPOINTS; i++) {
1458                                 mask <<= 1;      1453                                 mask <<= 1;
1459                                 if (status &     1454                                 if (status & mask)
1460                                         handl    1455                                         handle_ep(ep);
1461                                 ep++;            1456                                 ep++;
1462                         }                        1457                         }
1463                 }                                1458                 }
1464         }                                        1459         }
1465                                                  1460 
1466         return IRQ_HANDLED;                      1461         return IRQ_HANDLED;
1467 }                                                1462 }
1468                                                  1463 
1469 /*-------------------------------------------    1464 /*-------------------------------------------------------------------------*/
1470                                                  1465 
1471 static void nop_release(struct device *dev)      1466 static void nop_release(struct device *dev)
1472 {                                                1467 {
1473         /* nothing to free */                    1468         /* nothing to free */
1474 }                                                1469 }
1475                                                  1470 
1476 static struct at91_udc controller = {            1471 static struct at91_udc controller = {
1477         .gadget = {                              1472         .gadget = {
1478                 .ops    = &at91_udc_ops,         1473                 .ops    = &at91_udc_ops,
1479                 .ep0    = &controller.ep[0].e    1474                 .ep0    = &controller.ep[0].ep,
1480                 .name   = driver_name,           1475                 .name   = driver_name,
1481                 .dev    = {                      1476                 .dev    = {
1482                         .bus_id = "gadget",   !! 1477                         .init_name = "gadget",
1483                         .release = nop_releas    1478                         .release = nop_release,
1484                 }                                1479                 }
1485         },                                       1480         },
1486         .ep[0] = {                               1481         .ep[0] = {
1487                 .ep = {                          1482                 .ep = {
1488                         .name   = ep0name,       1483                         .name   = ep0name,
1489                         .ops    = &at91_ep_op    1484                         .ops    = &at91_ep_ops,
1490                 },                               1485                 },
1491                 .udc            = &controller    1486                 .udc            = &controller,
1492                 .maxpacket      = 8,             1487                 .maxpacket      = 8,
1493                 .int_mask       = 1 << 0,        1488                 .int_mask       = 1 << 0,
1494         },                                       1489         },
1495         .ep[1] = {                               1490         .ep[1] = {
1496                 .ep = {                          1491                 .ep = {
1497                         .name   = "ep1",         1492                         .name   = "ep1",
1498                         .ops    = &at91_ep_op    1493                         .ops    = &at91_ep_ops,
1499                 },                               1494                 },
1500                 .udc            = &controller    1495                 .udc            = &controller,
1501                 .is_pingpong    = 1,             1496                 .is_pingpong    = 1,
1502                 .maxpacket      = 64,            1497                 .maxpacket      = 64,
1503                 .int_mask       = 1 << 1,        1498                 .int_mask       = 1 << 1,
1504         },                                       1499         },
1505         .ep[2] = {                               1500         .ep[2] = {
1506                 .ep = {                          1501                 .ep = {
1507                         .name   = "ep2",         1502                         .name   = "ep2",
1508                         .ops    = &at91_ep_op    1503                         .ops    = &at91_ep_ops,
1509                 },                               1504                 },
1510                 .udc            = &controller    1505                 .udc            = &controller,
1511                 .is_pingpong    = 1,             1506                 .is_pingpong    = 1,
1512                 .maxpacket      = 64,            1507                 .maxpacket      = 64,
1513                 .int_mask       = 1 << 2,        1508                 .int_mask       = 1 << 2,
1514         },                                       1509         },
1515         .ep[3] = {                               1510         .ep[3] = {
1516                 .ep = {                          1511                 .ep = {
1517                         /* could actually do     1512                         /* could actually do bulk too */
1518                         .name   = "ep3-int",     1513                         .name   = "ep3-int",
1519                         .ops    = &at91_ep_op    1514                         .ops    = &at91_ep_ops,
1520                 },                               1515                 },
1521                 .udc            = &controller    1516                 .udc            = &controller,
1522                 .maxpacket      = 8,             1517                 .maxpacket      = 8,
1523                 .int_mask       = 1 << 3,        1518                 .int_mask       = 1 << 3,
1524         },                                       1519         },
1525         .ep[4] = {                               1520         .ep[4] = {
1526                 .ep = {                          1521                 .ep = {
1527                         .name   = "ep4",         1522                         .name   = "ep4",
1528                         .ops    = &at91_ep_op    1523                         .ops    = &at91_ep_ops,
1529                 },                               1524                 },
1530                 .udc            = &controller    1525                 .udc            = &controller,
1531                 .is_pingpong    = 1,             1526                 .is_pingpong    = 1,
1532                 .maxpacket      = 256,           1527                 .maxpacket      = 256,
1533                 .int_mask       = 1 << 4,        1528                 .int_mask       = 1 << 4,
1534         },                                       1529         },
1535         .ep[5] = {                               1530         .ep[5] = {
1536                 .ep = {                          1531                 .ep = {
1537                         .name   = "ep5",         1532                         .name   = "ep5",
1538                         .ops    = &at91_ep_op    1533                         .ops    = &at91_ep_ops,
1539                 },                               1534                 },
1540                 .udc            = &controller    1535                 .udc            = &controller,
1541                 .is_pingpong    = 1,             1536                 .is_pingpong    = 1,
1542                 .maxpacket      = 256,           1537                 .maxpacket      = 256,
1543                 .int_mask       = 1 << 5,        1538                 .int_mask       = 1 << 5,
1544         },                                       1539         },
1545         /* ep6 and ep7 are also reserved (cus    1540         /* ep6 and ep7 are also reserved (custom silicon might use them) */
1546 };                                               1541 };
1547                                                  1542 
1548 static irqreturn_t at91_vbus_irq(int irq, voi    1543 static irqreturn_t at91_vbus_irq(int irq, void *_udc)
1549 {                                                1544 {
1550         struct at91_udc *udc = _udc;             1545         struct at91_udc *udc = _udc;
1551         unsigned        value;                   1546         unsigned        value;
1552                                                  1547 
1553         /* vbus needs at least brief debounci    1548         /* vbus needs at least brief debouncing */
1554         udelay(10);                              1549         udelay(10);
1555         value = gpio_get_value(udc->board.vbu    1550         value = gpio_get_value(udc->board.vbus_pin);
1556         if (value != udc->vbus)                  1551         if (value != udc->vbus)
1557                 at91_vbus_session(&udc->gadge    1552                 at91_vbus_session(&udc->gadget, value);
1558                                                  1553 
1559         return IRQ_HANDLED;                      1554         return IRQ_HANDLED;
1560 }                                                1555 }
1561                                                  1556 
1562 int usb_gadget_register_driver (struct usb_ga    1557 int usb_gadget_register_driver (struct usb_gadget_driver *driver)
1563 {                                                1558 {
1564         struct at91_udc *udc = &controller;      1559         struct at91_udc *udc = &controller;
1565         int             retval;                  1560         int             retval;
1566                                                  1561 
1567         if (!driver                              1562         if (!driver
1568                         || driver->speed < US    1563                         || driver->speed < USB_SPEED_FULL
1569                         || !driver->bind         1564                         || !driver->bind
1570                         || !driver->setup) {     1565                         || !driver->setup) {
1571                 DBG("bad parameter.\n");         1566                 DBG("bad parameter.\n");
1572                 return -EINVAL;                  1567                 return -EINVAL;
1573         }                                        1568         }
1574                                                  1569 
1575         if (udc->driver) {                       1570         if (udc->driver) {
1576                 DBG("UDC already has a gadget    1571                 DBG("UDC already has a gadget driver\n");
1577                 return -EBUSY;                   1572                 return -EBUSY;
1578         }                                        1573         }
1579                                                  1574 
1580         udc->driver = driver;                    1575         udc->driver = driver;
1581         udc->gadget.dev.driver = &driver->dri    1576         udc->gadget.dev.driver = &driver->driver;
1582         udc->gadget.dev.driver_data = &driver !! 1577         dev_set_drvdata(&udc->gadget.dev, &driver->driver);
1583         udc->enabled = 1;                        1578         udc->enabled = 1;
1584         udc->selfpowered = 1;                    1579         udc->selfpowered = 1;
1585                                                  1580 
1586         retval = driver->bind(&udc->gadget);     1581         retval = driver->bind(&udc->gadget);
1587         if (retval) {                            1582         if (retval) {
1588                 DBG("driver->bind() returned     1583                 DBG("driver->bind() returned %d\n", retval);
1589                 udc->driver = NULL;              1584                 udc->driver = NULL;
1590                 udc->gadget.dev.driver = NULL    1585                 udc->gadget.dev.driver = NULL;
1591                 udc->gadget.dev.driver_data = !! 1586                 dev_set_drvdata(&udc->gadget.dev, NULL);
1592                 udc->enabled = 0;                1587                 udc->enabled = 0;
1593                 udc->selfpowered = 0;            1588                 udc->selfpowered = 0;
1594                 return retval;                   1589                 return retval;
1595         }                                        1590         }
1596                                                  1591 
1597         local_irq_disable();                     1592         local_irq_disable();
1598         pullup(udc, 1);                          1593         pullup(udc, 1);
1599         local_irq_enable();                      1594         local_irq_enable();
1600                                                  1595 
1601         DBG("bound to %s\n", driver->driver.n    1596         DBG("bound to %s\n", driver->driver.name);
1602         return 0;                                1597         return 0;
1603 }                                                1598 }
1604 EXPORT_SYMBOL (usb_gadget_register_driver);      1599 EXPORT_SYMBOL (usb_gadget_register_driver);
1605                                                  1600 
1606 int usb_gadget_unregister_driver (struct usb_    1601 int usb_gadget_unregister_driver (struct usb_gadget_driver *driver)
1607 {                                                1602 {
1608         struct at91_udc *udc = &controller;      1603         struct at91_udc *udc = &controller;
1609                                                  1604 
1610         if (!driver || driver != udc->driver     1605         if (!driver || driver != udc->driver || !driver->unbind)
1611                 return -EINVAL;                  1606                 return -EINVAL;
1612                                                  1607 
1613         local_irq_disable();                     1608         local_irq_disable();
1614         udc->enabled = 0;                        1609         udc->enabled = 0;
1615         at91_udp_write(udc, AT91_UDP_IDR, ~0)    1610         at91_udp_write(udc, AT91_UDP_IDR, ~0);
1616         pullup(udc, 0);                          1611         pullup(udc, 0);
1617         local_irq_enable();                      1612         local_irq_enable();
1618                                                  1613 
1619         driver->unbind(&udc->gadget);            1614         driver->unbind(&udc->gadget);
1620         udc->gadget.dev.driver = NULL;           1615         udc->gadget.dev.driver = NULL;
1621         udc->gadget.dev.driver_data = NULL;   !! 1616         dev_set_drvdata(&udc->gadget.dev, NULL);
1622         udc->driver = NULL;                      1617         udc->driver = NULL;
1623                                                  1618 
1624         DBG("unbound from %s\n", driver->driv    1619         DBG("unbound from %s\n", driver->driver.name);
1625         return 0;                                1620         return 0;
1626 }                                                1621 }
1627 EXPORT_SYMBOL (usb_gadget_unregister_driver);    1622 EXPORT_SYMBOL (usb_gadget_unregister_driver);
1628                                                  1623 
1629 /*-------------------------------------------    1624 /*-------------------------------------------------------------------------*/
1630                                                  1625 
1631 static void at91udc_shutdown(struct platform_    1626 static void at91udc_shutdown(struct platform_device *dev)
1632 {                                                1627 {
1633         /* force disconnect on reboot */         1628         /* force disconnect on reboot */
1634         pullup(platform_get_drvdata(dev), 0);    1629         pullup(platform_get_drvdata(dev), 0);
1635 }                                                1630 }
1636                                                  1631 
1637 static int __init at91udc_probe(struct platfo    1632 static int __init at91udc_probe(struct platform_device *pdev)
1638 {                                                1633 {
1639         struct device   *dev = &pdev->dev;       1634         struct device   *dev = &pdev->dev;
1640         struct at91_udc *udc;                    1635         struct at91_udc *udc;
1641         int             retval;                  1636         int             retval;
1642         struct resource *res;                    1637         struct resource *res;
1643                                                  1638 
1644         if (!dev->platform_data) {               1639         if (!dev->platform_data) {
1645                 /* small (so we copy it) but     1640                 /* small (so we copy it) but critical! */
1646                 DBG("missing platform_data\n"    1641                 DBG("missing platform_data\n");
1647                 return -ENODEV;                  1642                 return -ENODEV;
1648         }                                        1643         }
1649                                                  1644 
1650         if (pdev->num_resources != 2) {          1645         if (pdev->num_resources != 2) {
1651                 DBG("invalid num_resources\n"    1646                 DBG("invalid num_resources\n");
1652                 return -ENODEV;                  1647                 return -ENODEV;
1653         }                                        1648         }
1654         if ((pdev->resource[0].flags != IORES    1649         if ((pdev->resource[0].flags != IORESOURCE_MEM)
1655                         || (pdev->resource[1]    1650                         || (pdev->resource[1].flags != IORESOURCE_IRQ)) {
1656                 DBG("invalid resource type\n"    1651                 DBG("invalid resource type\n");
1657                 return -ENODEV;                  1652                 return -ENODEV;
1658         }                                        1653         }
1659                                                  1654 
1660         res = platform_get_resource(pdev, IOR    1655         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1661         if (!res)                                1656         if (!res)
1662                 return -ENXIO;                   1657                 return -ENXIO;
1663                                                  1658 
1664         if (!request_mem_region(res->start,      1659         if (!request_mem_region(res->start,
1665                         res->end - res->start    1660                         res->end - res->start + 1,
1666                         driver_name)) {          1661                         driver_name)) {
1667                 DBG("someone's using UDC memo    1662                 DBG("someone's using UDC memory\n");
1668                 return -EBUSY;                   1663                 return -EBUSY;
1669         }                                        1664         }
1670                                                  1665 
1671         /* init software state */                1666         /* init software state */
1672         udc = &controller;                       1667         udc = &controller;
1673         udc->gadget.dev.parent = dev;            1668         udc->gadget.dev.parent = dev;
1674         udc->board = *(struct at91_udc_data *    1669         udc->board = *(struct at91_udc_data *) dev->platform_data;
1675         udc->pdev = pdev;                        1670         udc->pdev = pdev;
1676         udc->enabled = 0;                        1671         udc->enabled = 0;
1677                                                  1672 
1678         /* rm9200 needs manual D+ pullup; off    1673         /* rm9200 needs manual D+ pullup; off by default */
1679         if (cpu_is_at91rm9200()) {               1674         if (cpu_is_at91rm9200()) {
1680                 if (udc->board.pullup_pin <=     1675                 if (udc->board.pullup_pin <= 0) {
1681                         DBG("no D+ pullup?\n"    1676                         DBG("no D+ pullup?\n");
1682                         retval = -ENODEV;        1677                         retval = -ENODEV;
1683                         goto fail0;              1678                         goto fail0;
1684                 }                                1679                 }
1685                 retval = gpio_request(udc->bo    1680                 retval = gpio_request(udc->board.pullup_pin, "udc_pullup");
1686                 if (retval) {                    1681                 if (retval) {
1687                         DBG("D+ pullup is bus    1682                         DBG("D+ pullup is busy\n");
1688                         goto fail0;              1683                         goto fail0;
1689                 }                                1684                 }
1690                 gpio_direction_output(udc->bo    1685                 gpio_direction_output(udc->board.pullup_pin,
1691                                 udc->board.pu    1686                                 udc->board.pullup_active_low);
1692         }                                        1687         }
1693                                                  1688 
                                                   >> 1689         /* newer chips have more FIFO memory than rm9200 */
                                                   >> 1690         if (cpu_is_at91sam9260()) {
                                                   >> 1691                 udc->ep[0].maxpacket = 64;
                                                   >> 1692                 udc->ep[3].maxpacket = 64;
                                                   >> 1693                 udc->ep[4].maxpacket = 512;
                                                   >> 1694                 udc->ep[5].maxpacket = 512;
                                                   >> 1695         } else if (cpu_is_at91sam9261()) {
                                                   >> 1696                 udc->ep[3].maxpacket = 64;
                                                   >> 1697         } else if (cpu_is_at91sam9263()) {
                                                   >> 1698                 udc->ep[0].maxpacket = 64;
                                                   >> 1699                 udc->ep[3].maxpacket = 64;
                                                   >> 1700         }
                                                   >> 1701 
1694         udc->udp_baseaddr = ioremap(res->star    1702         udc->udp_baseaddr = ioremap(res->start, res->end - res->start + 1);
1695         if (!udc->udp_baseaddr) {                1703         if (!udc->udp_baseaddr) {
1696                 retval = -ENOMEM;                1704                 retval = -ENOMEM;
1697                 goto fail0a;                     1705                 goto fail0a;
1698         }                                        1706         }
1699                                                  1707 
1700         udc_reinit(udc);                         1708         udc_reinit(udc);
1701                                                  1709 
1702         /* get interface and function clocks     1710         /* get interface and function clocks */
1703         udc->iclk = clk_get(dev, "udc_clk");     1711         udc->iclk = clk_get(dev, "udc_clk");
1704         udc->fclk = clk_get(dev, "udpck");       1712         udc->fclk = clk_get(dev, "udpck");
1705         if (IS_ERR(udc->iclk) || IS_ERR(udc->    1713         if (IS_ERR(udc->iclk) || IS_ERR(udc->fclk)) {
1706                 DBG("clocks missing\n");         1714                 DBG("clocks missing\n");
1707                 retval = -ENODEV;                1715                 retval = -ENODEV;
1708                 /* NOTE: we "know" here that     1716                 /* NOTE: we "know" here that refcounts on these are NOPs */
1709                 goto fail0b;                     1717                 goto fail0b;
1710         }                                        1718         }
1711                                                  1719 
1712         retval = device_register(&udc->gadget    1720         retval = device_register(&udc->gadget.dev);
1713         if (retval < 0)                          1721         if (retval < 0)
1714                 goto fail0b;                     1722                 goto fail0b;
1715                                                  1723 
1716         /* don't do anything until we have bo    1724         /* don't do anything until we have both gadget driver and VBUS */
1717         clk_enable(udc->iclk);                   1725         clk_enable(udc->iclk);
1718         at91_udp_write(udc, AT91_UDP_TXVC, AT    1726         at91_udp_write(udc, AT91_UDP_TXVC, AT91_UDP_TXVC_TXVDIS);
1719         at91_udp_write(udc, AT91_UDP_IDR, 0xf    1727         at91_udp_write(udc, AT91_UDP_IDR, 0xffffffff);
1720         /* Clear all pending interrupts - UDP    1728         /* Clear all pending interrupts - UDP may be used by bootloader. */
1721         at91_udp_write(udc, AT91_UDP_ICR, 0xf    1729         at91_udp_write(udc, AT91_UDP_ICR, 0xffffffff);
1722         clk_disable(udc->iclk);                  1730         clk_disable(udc->iclk);
1723                                                  1731 
1724         /* request UDC and maybe VBUS irqs */    1732         /* request UDC and maybe VBUS irqs */
1725         udc->udp_irq = platform_get_irq(pdev,    1733         udc->udp_irq = platform_get_irq(pdev, 0);
1726         retval = request_irq(udc->udp_irq, at    1734         retval = request_irq(udc->udp_irq, at91_udc_irq,
1727                         IRQF_DISABLED, driver    1735                         IRQF_DISABLED, driver_name, udc);
1728         if (retval < 0) {                        1736         if (retval < 0) {
1729                 DBG("request irq %d failed\n"    1737                 DBG("request irq %d failed\n", udc->udp_irq);
1730                 goto fail1;                      1738                 goto fail1;
1731         }                                        1739         }
1732         if (udc->board.vbus_pin > 0) {           1740         if (udc->board.vbus_pin > 0) {
1733                 retval = gpio_request(udc->bo    1741                 retval = gpio_request(udc->board.vbus_pin, "udc_vbus");
1734                 if (retval < 0) {                1742                 if (retval < 0) {
1735                         DBG("request vbus pin    1743                         DBG("request vbus pin failed\n");
1736                         goto fail2;              1744                         goto fail2;
1737                 }                                1745                 }
1738                 gpio_direction_input(udc->boa    1746                 gpio_direction_input(udc->board.vbus_pin);
1739                                                  1747 
1740                 /*                               1748                 /*
1741                  * Get the initial state of V    1749                  * Get the initial state of VBUS - we cannot expect
1742                  * a pending interrupt.          1750                  * a pending interrupt.
1743                  */                              1751                  */
1744                 udc->vbus = gpio_get_value(ud    1752                 udc->vbus = gpio_get_value(udc->board.vbus_pin);
1745                 if (request_irq(udc->board.vb    1753                 if (request_irq(udc->board.vbus_pin, at91_vbus_irq,
1746                                 IRQF_DISABLED    1754                                 IRQF_DISABLED, driver_name, udc)) {
1747                         DBG("request vbus irq    1755                         DBG("request vbus irq %d failed\n",
1748                                         udc->    1756                                         udc->board.vbus_pin);
1749                         free_irq(udc->udp_irq    1757                         free_irq(udc->udp_irq, udc);
1750                         retval = -EBUSY;         1758                         retval = -EBUSY;
1751                         goto fail3;              1759                         goto fail3;
1752                 }                                1760                 }
1753         } else {                                 1761         } else {
1754                 DBG("no VBUS detection, assum    1762                 DBG("no VBUS detection, assuming always-on\n");
1755                 udc->vbus = 1;                   1763                 udc->vbus = 1;
1756         }                                        1764         }
1757         dev_set_drvdata(dev, udc);               1765         dev_set_drvdata(dev, udc);
1758         device_init_wakeup(dev, 1);              1766         device_init_wakeup(dev, 1);
1759         create_debug_file(udc);                  1767         create_debug_file(udc);
1760                                                  1768 
1761         INFO("%s version %s\n", driver_name,     1769         INFO("%s version %s\n", driver_name, DRIVER_VERSION);
1762         return 0;                                1770         return 0;
1763                                                  1771 
1764 fail3:                                           1772 fail3:
1765         if (udc->board.vbus_pin > 0)             1773         if (udc->board.vbus_pin > 0)
1766                 gpio_free(udc->board.vbus_pin    1774                 gpio_free(udc->board.vbus_pin);
1767 fail2:                                           1775 fail2:
1768         free_irq(udc->udp_irq, udc);             1776         free_irq(udc->udp_irq, udc);
1769 fail1:                                           1777 fail1:
1770         device_unregister(&udc->gadget.dev);     1778         device_unregister(&udc->gadget.dev);
1771 fail0b:                                          1779 fail0b:
1772         iounmap(udc->udp_baseaddr);              1780         iounmap(udc->udp_baseaddr);
1773 fail0a:                                          1781 fail0a:
1774         if (cpu_is_at91rm9200())                 1782         if (cpu_is_at91rm9200())
1775                 gpio_free(udc->board.pullup_p    1783                 gpio_free(udc->board.pullup_pin);
1776 fail0:                                           1784 fail0:
1777         release_mem_region(res->start, res->e    1785         release_mem_region(res->start, res->end - res->start + 1);
1778         DBG("%s probe failed, %d\n", driver_n    1786         DBG("%s probe failed, %d\n", driver_name, retval);
1779         return retval;                           1787         return retval;
1780 }                                                1788 }
1781                                                  1789 
1782 static int __exit at91udc_remove(struct platf    1790 static int __exit at91udc_remove(struct platform_device *pdev)
1783 {                                                1791 {
1784         struct at91_udc *udc = platform_get_d    1792         struct at91_udc *udc = platform_get_drvdata(pdev);
1785         struct resource *res;                    1793         struct resource *res;
1786                                                  1794 
1787         DBG("remove\n");                         1795         DBG("remove\n");
1788                                                  1796 
1789         if (udc->driver)                         1797         if (udc->driver)
1790                 return -EBUSY;                   1798                 return -EBUSY;
1791                                                  1799 
1792         pullup(udc, 0);                          1800         pullup(udc, 0);
1793                                                  1801 
1794         device_init_wakeup(&pdev->dev, 0);       1802         device_init_wakeup(&pdev->dev, 0);
1795         remove_debug_file(udc);                  1803         remove_debug_file(udc);
1796         if (udc->board.vbus_pin > 0) {           1804         if (udc->board.vbus_pin > 0) {
1797                 free_irq(udc->board.vbus_pin,    1805                 free_irq(udc->board.vbus_pin, udc);
1798                 gpio_free(udc->board.vbus_pin    1806                 gpio_free(udc->board.vbus_pin);
1799         }                                        1807         }
1800         free_irq(udc->udp_irq, udc);             1808         free_irq(udc->udp_irq, udc);
1801         device_unregister(&udc->gadget.dev);     1809         device_unregister(&udc->gadget.dev);
1802                                                  1810 
1803         iounmap(udc->udp_baseaddr);              1811         iounmap(udc->udp_baseaddr);
1804                                                  1812 
1805         if (cpu_is_at91rm9200())                 1813         if (cpu_is_at91rm9200())
1806                 gpio_free(udc->board.pullup_p    1814                 gpio_free(udc->board.pullup_pin);
1807                                                  1815 
1808         res = platform_get_resource(pdev, IOR    1816         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1809         release_mem_region(res->start, res->e    1817         release_mem_region(res->start, res->end - res->start + 1);
1810                                                  1818 
1811         clk_put(udc->iclk);                      1819         clk_put(udc->iclk);
1812         clk_put(udc->fclk);                      1820         clk_put(udc->fclk);
1813                                                  1821 
1814         return 0;                                1822         return 0;
1815 }                                                1823 }
1816                                                  1824 
1817 #ifdef CONFIG_PM                                 1825 #ifdef CONFIG_PM
1818 static int at91udc_suspend(struct platform_de    1826 static int at91udc_suspend(struct platform_device *pdev, pm_message_t mesg)
1819 {                                                1827 {
1820         struct at91_udc *udc = platform_get_d    1828         struct at91_udc *udc = platform_get_drvdata(pdev);
1821         int             wake = udc->driver &&    1829         int             wake = udc->driver && device_may_wakeup(&pdev->dev);
1822                                                  1830 
1823         /* Unless we can act normally to the     1831         /* Unless we can act normally to the host (letting it wake us up
1824          * whenever it has work for us) force    1832          * whenever it has work for us) force disconnect.  Wakeup requires
1825          * PLLB for USB events (signaling for    1833          * PLLB for USB events (signaling for reset, wakeup, or incoming
1826          * tokens) and VBUS irqs (on systems     1834          * tokens) and VBUS irqs (on systems which support them).
1827          */                                      1835          */
1828         if ((!udc->suspended && udc->addr)       1836         if ((!udc->suspended && udc->addr)
1829                         || !wake                 1837                         || !wake
1830                         || at91_suspend_enter    1838                         || at91_suspend_entering_slow_clock()) {
1831                 pullup(udc, 0);                  1839                 pullup(udc, 0);
1832                 wake = 0;                        1840                 wake = 0;
1833         } else                                   1841         } else
1834                 enable_irq_wake(udc->udp_irq)    1842                 enable_irq_wake(udc->udp_irq);
1835                                                  1843 
1836         udc->active_suspend = wake;              1844         udc->active_suspend = wake;
1837         if (udc->board.vbus_pin > 0 && wake)     1845         if (udc->board.vbus_pin > 0 && wake)
1838                 enable_irq_wake(udc->board.vb    1846                 enable_irq_wake(udc->board.vbus_pin);
1839         return 0;                                1847         return 0;
1840 }                                                1848 }
1841                                                  1849 
1842 static int at91udc_resume(struct platform_dev    1850 static int at91udc_resume(struct platform_device *pdev)
1843 {                                                1851 {
1844         struct at91_udc *udc = platform_get_d    1852         struct at91_udc *udc = platform_get_drvdata(pdev);
1845                                                  1853 
1846         if (udc->board.vbus_pin > 0 && udc->a    1854         if (udc->board.vbus_pin > 0 && udc->active_suspend)
1847                 disable_irq_wake(udc->board.v    1855                 disable_irq_wake(udc->board.vbus_pin);
1848                                                  1856 
1849         /* maybe reconnect to host; if so, cl    1857         /* maybe reconnect to host; if so, clocks on */
1850         if (udc->active_suspend)                 1858         if (udc->active_suspend)
1851                 disable_irq_wake(udc->udp_irq    1859                 disable_irq_wake(udc->udp_irq);
1852         else                                     1860         else
1853                 pullup(udc, 1);                  1861                 pullup(udc, 1);
1854         return 0;                                1862         return 0;
1855 }                                                1863 }
1856 #else                                            1864 #else
1857 #define at91udc_suspend NULL                     1865 #define at91udc_suspend NULL
1858 #define at91udc_resume  NULL                     1866 #define at91udc_resume  NULL
1859 #endif                                           1867 #endif
1860                                                  1868 
1861 static struct platform_driver at91_udc_driver    1869 static struct platform_driver at91_udc_driver = {
1862         .remove         = __exit_p(at91udc_re    1870         .remove         = __exit_p(at91udc_remove),
1863         .shutdown       = at91udc_shutdown,      1871         .shutdown       = at91udc_shutdown,
1864         .suspend        = at91udc_suspend,       1872         .suspend        = at91udc_suspend,
1865         .resume         = at91udc_resume,        1873         .resume         = at91udc_resume,
1866         .driver         = {                      1874         .driver         = {
1867                 .name   = (char *) driver_nam    1875                 .name   = (char *) driver_name,
1868                 .owner  = THIS_MODULE,           1876                 .owner  = THIS_MODULE,
1869         },                                       1877         },
1870 };                                               1878 };
1871                                                  1879 
1872 static int __init udc_init_module(void)          1880 static int __init udc_init_module(void)
1873 {                                                1881 {
1874         return platform_driver_probe(&at91_ud    1882         return platform_driver_probe(&at91_udc_driver, at91udc_probe);
1875 }                                                1883 }
1876 module_init(udc_init_module);                    1884 module_init(udc_init_module);
1877                                                  1885 
1878 static void __exit udc_exit_module(void)         1886 static void __exit udc_exit_module(void)
1879 {                                                1887 {
1880         platform_driver_unregister(&at91_udc_    1888         platform_driver_unregister(&at91_udc_driver);
1881 }                                                1889 }
1882 module_exit(udc_exit_module);                    1890 module_exit(udc_exit_module);
1883                                                  1891 
1884 MODULE_DESCRIPTION("AT91 udc driver");           1892 MODULE_DESCRIPTION("AT91 udc driver");
1885 MODULE_AUTHOR("Thomas Rathbone, David Brownel    1893 MODULE_AUTHOR("Thomas Rathbone, David Brownell");
1886 MODULE_LICENSE("GPL");                           1894 MODULE_LICENSE("GPL");
1887 MODULE_ALIAS("platform:at91_udc");               1895 MODULE_ALIAS("platform:at91_udc");
1888                                                  1896 
  This page was automatically generated by the LXR engine.