]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/devs/usb/at91/v2_0/src/usbs_at91.c
Initial revision
[karo-tx-redboot.git] / packages / devs / usb / at91 / v2_0 / src / usbs_at91.c
1 //==========================================================================
2 //
3 //      usbs_at91.c
4 //
5 //      Driver for the AT91 USB device
6 //
7 //==========================================================================
8 //####ECOSGPLCOPYRIGHTBEGIN####
9 // -------------------------------------------
10 // This file is part of eCos, the Embedded Configurable Operating System.
11 // Copyright (C) 2006 eCosCentric
12 // Copyright (C) 2006 Andrew Lunn
13 //
14 // eCos is free software; you can redistribute it and/or modify it under
15 // the terms of the GNU General Public License as published by the Free
16 // Software Foundation; either version 2 or (at your option) any later version.
17 //
18 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
19 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
20 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
21 // for more details.
22 //
23 // You should have received a copy of the GNU General Public License along
24 // with eCos; if not, write to the Free Software Foundation, Inc.,
25 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
26 //
27 // As a special exception, if other files instantiate templates or use macros
28 // or inline functions from this file, or you compile this file and link it
29 // with other works to produce a work based on this file, this file does not
30 // by itself cause the resulting work to be covered by the GNU General Public
31 // License. However the source code for this file must still be made available
32 // in accordance with section (3) of the GNU General Public License.
33 //
34 // This exception does not invalidate any other reasons why a work based on
35 // this file might be covered by the GNU General Public License.
36 // -------------------------------------------
37 //####ECOSGPLCOPYRIGHTEND####
38 //==========================================================================
39 //#####DESCRIPTIONBEGIN####
40 //
41 // Author(s):    Oliver Munz,
42 // Contributors: Andrew Lunn, bartv
43 // Date:         2006-02-22
44 //
45 // This code implements support for the on-chip USB port on the AT91
46 // family of processors. The code has been developed on the AT91SAM7S
47 // and may or may not work on other members of the AT91 family.
48 //
49 //####DESCRIPTIONEND####
50 //==========================================================================
51
52 #include <pkgconf/devs_usb_at91.h>
53 #include <cyg/io/usb/usb.h>
54 #include <cyg/io/usb/usbs.h>
55 #include <cyg/io/usb/usbs_at91.h>
56
57 #include <cyg/hal/hal_io.h>
58 #include <cyg/hal/drv_api.h>
59 #include <cyg/hal/hal_io.h>
60 #include <cyg/hal/hal_platform_ints.h>
61 #include <cyg/infra/cyg_ass.h>
62 #include <cyg/infra/diag.h>
63
64 #include "bitops.h"
65
66 #ifndef MIN
67 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
68 #endif
69
70 #define AT91_UDP_CSR0 (AT91_UDP_CSR)
71 #define AT91_UDP_FDR0 (AT91_UDP_FDR)
72
73 #define pIER (AT91_UDP + AT91_UDP_IER)
74 #define pIDR (AT91_UDP + AT91_UDP_IDR)
75 #define pISR (AT91_UDP + AT91_UDP_ISR)
76 #define pIMR (AT91_UDP + AT91_UDP_IMR)
77 #define pICR (AT91_UDP + AT91_UDP_ICR)
78
79 #define pCSR0 (AT91_UDP + AT91_UDP_CSR0)
80 #define pFDR0 (AT91_UDP + AT91_UDP_FDR0)
81
82 #define pCSRn(N) (pCSR0 + (N * 4))
83 #define pFDRn(N) (pFDR0 + (N * 4))
84
85 #define AT91_UDP_ALLOWED_IRQs \
86     (AT91_UDP_WAKEUP | AT91_UDP_ENDBUSRES | AT91_UDP_EXTRSM | \
87      AT91_UDP_RXRSM  | AT91_UDP_RXSUSP    | AT91_UDP_EPINT0 | \
88      AT91_UDP_EPINT1 | AT91_UDP_EPINT2    | AT91_UDP_EPINT3)
89
90 #define THERE_IS_A_NEW_PACKET_IN_THE_UDP 0xffff
91
92 // Fifo size for each end point.
93 static const cyg_uint16 usbs_at91_endpoint_fifo_size[AT91_USB_ENDPOINTS] = {
94   8,
95   64,
96   64,
97   64,
98 };
99
100 // Does an endpoint support ping pong buffering?
101 static const bool usbs_at91_endpoint_pingpong[AT91_USB_ENDPOINTS] = {
102   false,
103   true,
104   true,
105   false
106 };
107
108 static cyg_uint8 *usbs_at91_endpoint_pbegin[AT91_USB_ENDPOINTS] = 
109   { 0, 0, 0, 0 };
110 static cyg_uint8 *usbs_at91_endpoint_pend[AT91_USB_ENDPOINTS] = 
111   { 0, 0 ,0, 0 };
112 static bool usbs_at91_endpoint_bank1[AT91_USB_ENDPOINTS] = 
113   { false, false, false, false };
114 static cyg_uint16 usbs_at91_endpoint_bytes_in_fifo[AT91_USB_ENDPOINTS] =
115   { 0, 0, 0, 0 };
116 static cyg_uint16 usbs_at91_endpoint_bytes_received[AT91_USB_ENDPOINTS] =
117   { THERE_IS_A_NEW_PACKET_IN_THE_UDP, THERE_IS_A_NEW_PACKET_IN_THE_UDP,
118     THERE_IS_A_NEW_PACKET_IN_THE_UDP, THERE_IS_A_NEW_PACKET_IN_THE_UDP};
119
120 static cyg_interrupt usbs_at91_intr_data;
121 static cyg_handle_t usbs_at91_intr_handle;
122
123 static void usbs_at91_ep0_start(usbs_control_endpoint *);
124 static void usbs_at91_poll(usbs_control_endpoint *);
125
126 static void usbs_at91_endpoint_start(usbs_rx_endpoint * pep);
127 static void usbs_at91_endpoint_set_halted(usbs_rx_endpoint * pep, 
128                                           cyg_bool new_value);
129 void usbs_at91_endpoint_init(usbs_rx_endpoint * pep, 
130                              cyg_uint8 endpoint_type,
131                              cyg_bool enable);
132
133 // Endpoint 0, the control endpoint, structure. 
134 usbs_control_endpoint usbs_at91_ep0 = {
135   // The hardware does not distinguish  between detached, attached and powered.
136   state:                  USBS_STATE_POWERED,   
137   enumeration_data:       (usbs_enumeration_data *) 0,
138   start_fn:               usbs_at91_ep0_start,
139   poll_fn:                usbs_at91_poll,
140   interrupt_vector:       CYGNUM_HAL_INTERRUPT_UDP,
141   control_buffer:         {0, 0, 0, 0, 0, 0, 0, 0},
142   state_change_fn:        (void (*) (usbs_control_endpoint *, 
143                                      void *, usbs_state_change, int)) 0,
144   state_change_data:      (void *) 0,
145   standard_control_fn:    (usbs_control_return (*)
146                           (usbs_control_endpoint *, void *)) 0,
147   standard_control_data:  (void *) 0,
148   class_control_fn:       (usbs_control_return (*)
149                            (usbs_control_endpoint *, void *)) 0,
150   class_control_data:     (void *) 0,
151   vendor_control_fn:      (usbs_control_return (*) 
152                            (usbs_control_endpoint *, void *)) 0,
153   vendor_control_data:    (void *) 0,
154   reserved_control_fn:    (usbs_control_return (*) 
155                            (usbs_control_endpoint *, void *)) 0,
156   reserved_control_data:  (void *) 0,
157   buffer:                 (unsigned char *) 0,
158   buffer_size:            0,
159   fill_buffer_fn:         (void (*)(usbs_control_endpoint *)) 0,
160   fill_data:              (void *) 0,
161   fill_index:             0,
162   complete_fn:            (usbs_control_return (*)(usbs_control_endpoint *, 
163                                                    int)) 0
164 };
165
166 // Endpoint 1 receive control structure
167 usbs_rx_endpoint usbs_at91_ep1 = {
168   start_rx_fn:    usbs_at91_endpoint_start,
169   set_halted_fn:  usbs_at91_endpoint_set_halted,
170   complete_fn:    (void (*)(void *, int)) 0,
171   complete_data:  (void *) 0,
172   buffer:         (unsigned char *) 0,
173   buffer_size:    0,
174   halted:         0,
175 }; 
176
177 // Endpoint 2 Receive control structure
178 usbs_rx_endpoint usbs_at91_ep2 = {
179   start_rx_fn:    usbs_at91_endpoint_start,
180   set_halted_fn:  usbs_at91_endpoint_set_halted,
181   complete_fn:    (void (*)(void *, int)) 0,
182   complete_data:  (void *) 0,
183   buffer:         (unsigned char *) 0,
184   buffer_size:    0,
185   halted:         0,
186 };
187
188 // Endpoint 3 Receive control structure
189 usbs_rx_endpoint usbs_at91_ep3 = {
190   start_rx_fn:    usbs_at91_endpoint_start,
191   set_halted_fn:  usbs_at91_endpoint_set_halted,
192   complete_fn:    (void (*)(void *, int)) 0,
193   complete_data:  (void *) 0,
194   buffer:         (unsigned char *) 0,
195   buffer_size:    0,
196   halted:         0,
197 };
198
199 // Array of end points. Used for translating end point pointer to an
200 // end point number
201 static const void *usbs_at91_endpoints[AT91_USB_ENDPOINTS] = {
202   (void *) &usbs_at91_ep0,
203   (void *) &usbs_at91_ep1,
204   (void *) &usbs_at91_ep2,
205   (void *) &usbs_at91_ep3
206 };
207
208 // Convert an endpoint pointer to an endpoint number, using the array
209 // of endpoint structures
210 static int
211 usbs_at91_pep_to_number(const usbs_rx_endpoint * pep)
212 {
213   int epn;
214   
215   for(epn=0; epn < AT91_USB_ENDPOINTS; epn++) {
216     if (pep == usbs_at91_endpoints[epn])
217       return epn;
218   }
219   CYG_FAIL("Unknown endpoint");
220   return 0;
221 }
222
223 typedef enum ep0_low_level_status_t {
224   EP0_LL_IDLE = 0,
225   EP0_LL_REQUEST,
226   EP0_LL_SEND_READY,
227   EP0_LL_ACK,
228   EP0_LL_RECEIVE_READY,
229   EP0_LL_ISOERROR,
230   EP0_LL_STALL,
231   EP0_LL_SET_ADDRESS,
232 } ep0_low_level_status_t;
233
234 // Enable/Disable interrupts for a specific endpoint.
235 static void
236 usbs_at91_endpoint_interrupt_enable (cyg_uint8 epn, bool enable)
237 {
238   CYG_ASSERT (epn < AT91_USB_ENDPOINTS, "Invalid endpoint");
239   
240   if (enable) {
241     HAL_WRITE_UINT32 (pIER, 1 << epn);
242   } else {
243     HAL_WRITE_UINT32 (pIDR, 1 << epn);
244   }
245 }
246
247 static cyg_uint8 *
248 read_fifo_uint8 (cyg_uint8 * pdest, cyg_addrword_t psource, cyg_uint32 size)
249 {
250   cyg_uint8 *preqbyte = pdest;
251   cyg_uint8 reqbyte;
252   
253   while (size--) {
254     HAL_READ_UINT8 (psource, reqbyte);
255     *preqbyte = reqbyte;
256     preqbyte++;
257   }
258
259   return preqbyte;
260 }
261
262 static cyg_uint8 *
263 write_fifo_uint8 (cyg_addrword_t pdest, cyg_uint8 * psource,
264                       cyg_uint8 * psource_end)
265 {
266   cyg_uint8 *preqbyte;
267
268   for (preqbyte = psource; preqbyte < psource_end; preqbyte++) {
269     HAL_WRITE_UINT8 (pdest, (*preqbyte));
270   }
271   
272   return preqbyte;
273 }
274
275 /* Tell the host that the device is ready to start communication */
276 static void
277 usbs_at91_set_pullup (bool set)
278 {                
279
280 #ifndef CYGDAT_DEVS_USB_AT91_GPIO_SET_PULLUP_PIN_NONE
281   if (
282 #ifdef CYGNUM_DEVS_USB_AT91_GPIO_SET_PULLUP_INVERTED
283       !set
284 #else
285       set
286 #endif
287       ) {
288      HAL_ARM_AT91_GPIO_SET(CYGDAT_DEVS_USB_AT91_GPIO_SET_PULLUP_PIN);
289   } else {
290      HAL_ARM_AT91_GPIO_RESET(CYGDAT_DEVS_USB_AT91_GPIO_SET_PULLUP_PIN);
291   }
292 #endif
293 }
294   
295 /* Is the USB powered? */
296 bool
297 usbs_at91_read_power (void)
298 {
299 #ifndef CYGDAT_DEVS_USB_AT91_GPIO_READ_POWER_PIN_NONE
300   cyg_bool state;
301   
302   HAL_ARM_AT91_GPIO_GET(CYGDAT_DEVS_USB_AT91_GPIO_READ_POWER_PIN, state);
303 #ifdef CYGNUM_DEVS_USB_AT91_GPIO_READ_POWER_INVERTED
304   return !state;
305 #else
306   return state;
307 #endif
308 #endif
309   return true;
310 }
311
312 // Stop all transfers that are currently active.
313 static void
314 usbs_end_all_transfers (usbs_control_return returncode)
315 {
316   cyg_uint32 epn;
317   usbs_rx_endpoint *pep;
318   
319   for (epn = 1; epn < AT91_USB_ENDPOINTS; epn++) {
320     if (BITS_ARE_SET (pIMR, 1 << epn)) {
321       // If the end point is transmitting, call the complete function
322       // to terminate to transfer
323       pep = (usbs_rx_endpoint *) usbs_at91_endpoints[epn];
324
325       if (pep->complete_fn) {
326         (*pep->complete_fn) (pep->complete_data, returncode);
327       }
328       // Disable interrupts from the endpoint
329       usbs_at91_endpoint_interrupt_enable (epn, false);
330     }
331   }
332 }
333
334 // There has been a change in state. Update the end point.
335 static void
336 usbs_state_notify (usbs_control_endpoint * pcep)
337 {
338   static int old_state = USBS_STATE_CHANGE_POWERED;
339   int state = pcep->state & USBS_STATE_MASK;
340   
341   if (pcep->state != old_state) {
342     usbs_end_all_transfers (-EPIPE);
343     switch (state) {
344       case USBS_STATE_DETACHED:
345       case USBS_STATE_ATTACHED:
346       case USBS_STATE_POWERED:
347         // Nothing to do
348         break;
349       case USBS_STATE_DEFAULT:
350         HAL_WRITE_UINT32 (AT91_UDP + AT91_UDP_GLB_STATE, 0);
351         break;
352       case USBS_STATE_ADDRESSED:
353         HAL_WRITE_UINT32 (AT91_UDP + AT91_UDP_GLB_STATE, AT91_UDP_GLB_FADDEN);
354         break;
355       case USBS_STATE_CONFIGURED:
356         HAL_WRITE_UINT32 (AT91_UDP + AT91_UDP_GLB_STATE, AT91_UDP_GLB_CONFG);
357         break;
358       default:
359         CYG_FAIL("Unknown endpoint state");
360     }
361     
362     if (pcep->state_change_fn) {
363       (*pcep->state_change_fn) (pcep, 0, pcep->state, old_state);
364     }
365     
366     old_state = pcep->state;
367   }
368 }
369
370 static usbs_control_return
371 usbs_parse_host_get_command (usbs_control_endpoint * pcep)
372 {
373   usbs_control_return retcode;
374   cyg_uint8 dev_req_type =
375     (((usb_devreq *) pcep->control_buffer)->type) & USB_DEVREQ_TYPE_MASK;
376   
377   switch (dev_req_type) {
378     case USB_DEVREQ_TYPE_STANDARD: 
379       if (!pcep->standard_control_fn) {
380         return usbs_handle_standard_control (pcep);
381       }
382
383       retcode =
384         (*pcep->standard_control_fn) (pcep, pcep->standard_control_data);
385       
386       if (retcode == USBS_CONTROL_RETURN_UNKNOWN) {
387         return usbs_handle_standard_control (pcep);
388       }
389       return retcode;
390     
391     case USB_DEVREQ_TYPE_CLASS:
392       if (!pcep->class_control_fn) {
393         return USBS_CONTROL_RETURN_STALL;
394       }
395       return (*pcep->class_control_fn) (pcep, pcep->class_control_data);
396
397     case USB_DEVREQ_TYPE_VENDOR: 
398       if (!pcep->class_control_fn) {
399         return USBS_CONTROL_RETURN_STALL;
400       }
401       return (*pcep->class_control_fn) (pcep, pcep->vendor_control_data);
402
403     case USB_DEVREQ_TYPE_RESERVED:
404       if (!pcep->reserved_control_fn) {
405         return USBS_CONTROL_RETURN_STALL;
406       }
407       return (*pcep->reserved_control_fn) (pcep, pcep->reserved_control_data);
408     default:
409       return USBS_CONTROL_RETURN_STALL;
410   }
411 }
412
413 static void
414 usbs_at91_endpoint_set_halted (usbs_rx_endpoint * pep, cyg_bool new_value)
415 {
416   int epn = usbs_at91_pep_to_number(pep);
417   cyg_addrword_t pCSR = pCSRn(epn);
418   
419   cyg_drv_dsr_lock ();
420   
421   if (pep->halted != new_value) {       
422     /* There is something is to do */
423     pep->halted = new_value;
424     
425     if (new_value && BITS_ARE_SET (pIMR, 1 << epn)) {   
426       /* Ready to transmit */
427       if (pep->complete_fn) {
428         (*pep->complete_fn) (pep->complete_data, -EAGAIN);
429       }
430       usbs_at91_endpoint_interrupt_enable (epn, false);
431       SET_BITS (pCSR, AT91_UDP_CSR_FORCESTALL);
432     } else {
433       CLEAR_BITS (pCSR, AT91_UDP_CSR_FORCESTALL);
434     }
435   }
436   cyg_drv_dsr_unlock ();
437 }
438
439 void
440 usbs_at91_endpoint_init (usbs_rx_endpoint * pep, cyg_uint8 endpoint_type,
441                          cyg_bool enable)
442 {  
443   int epn = usbs_at91_pep_to_number(pep);
444   cyg_addrword_t pCSR = pCSRn(epn);
445   
446   CYG_ASSERT (AT91_USB_ENDPOINTS > epn, "Invalid end point");
447   
448   usbs_at91_endpoint_interrupt_enable (epn, false);
449   /* Reset endpoint */
450   HAL_WRITE_UINT32 (AT91_UDP + AT91_UDP_RST_EP, 1 << epn);      
451   HAL_WRITE_UINT32 (AT91_UDP + AT91_UDP_RST_EP, 0);
452   
453   pep->halted = false;
454
455   /* Type | In */
456   HAL_WRITE_UINT32 (pCSR, (((((cyg_uint32) endpoint_type) & 0x03) << 8) | 
457                            ((((cyg_uint32) endpoint_type) & 0x80) << 3)));
458
459   usbs_at91_endpoint_bytes_in_fifo[epn] = 0;
460   usbs_at91_endpoint_bytes_received[epn] = THERE_IS_A_NEW_PACKET_IN_THE_UDP;
461   usbs_at91_endpoint_bank1[epn] = false;
462
463   if (enable) {
464     SET_BITS (pCSR, AT91_UDP_CSR_EPEDS);
465   }
466 }
467
468 static void
469 usbs_at91_handle_reset (void)
470 {
471   int epn;
472   const usb_endpoint_descriptor *usb_endpoints;
473   cyg_uint8 endpoint_type;
474   
475    cyg_uint8 endpoint_number;
476
477   usbs_end_all_transfers (-EPIPE);
478   
479   HAL_WRITE_UINT32 (AT91_UDP + AT91_UDP_IDR, 0xffffffff);
480   HAL_WRITE_UINT32 (AT91_UDP + AT91_UDP_ICR, 0xffffffff);
481   HAL_WRITE_UINT32 (AT91_UDP + AT91_UDP_RST_EP, 0xffffffff);
482   HAL_WRITE_UINT32 (AT91_UDP + AT91_UDP_RST_EP, 0x00000000);
483   
484   HAL_WRITE_UINT32 (AT91_UDP + AT91_UDP_FADDR, AT91_UDP_FADDR_FEN);
485   HAL_WRITE_UINT32 (AT91_UDP + AT91_UDP_CSR0,
486                     AT91_UDP_CSR_EPEDS | AT91_UDP_CSR_EPTYPE_CTRL);
487   HAL_WRITE_UINT32 (AT91_UDP + AT91_UDP_IER, AT91_UDP_ALLOWED_IRQs);
488   
489   for (epn=1; epn < AT91_USB_ENDPOINTS; epn++) {
490     usbs_at91_endpoint_init ((usbs_rx_endpoint *)usbs_at91_endpoints[epn], 
491                              0, false);
492   }
493
494   // Now walk the endpoints configuring them correctly. This only
495   // works if there is one interface.
496   usb_endpoints = usbs_at91_ep0.enumeration_data->endpoints;
497   
498   for (epn = 1; 
499        epn <= usbs_at91_ep0.enumeration_data->total_number_endpoints; 
500        epn++) {
501     
502     endpoint_type = (usb_endpoints[epn-1].attributes |
503                      (usb_endpoints[epn-1].endpoint & 
504                       USB_ENDPOINT_DESCRIPTOR_ENDPOINT_IN ?
505                       USB_ENDPOINT_DESCRIPTOR_ENDPOINT_IN : 
506                       USB_ENDPOINT_DESCRIPTOR_ENDPOINT_OUT));
507     endpoint_number = usb_endpoints[epn-1].endpoint & ~(USB_ENDPOINT_DESCRIPTOR_ENDPOINT_IN); 
508     if ( endpoint_number < AT91_USB_ENDPOINTS )
509     {
510         usbs_at91_endpoint_init((usbs_rx_endpoint *)usbs_at91_endpoints[endpoint_number],
511                                 endpoint_type,
512                                 true);
513     }
514   }
515 }
516
517 static void
518 usbs_at91_ep0_start (usbs_control_endpoint * endpoint)
519 {
520   usbs_at91_handle_reset ();
521
522   // If there is additional platform-specific initialization to
523   // perform, do it now. This macro can come from the platform HAL,
524   // but may not be available on all platforms.
525 #ifdef AT91_USB_PLATFORM_INIT
526   AT91_USB_PLATFORM_INIT ();
527 #endif
528   
529   usbs_at91_set_pullup (true);
530 }
531
532 static void
533 usbs_at91_endpoint_start (usbs_rx_endpoint * pep)
534 {  
535   int epn = usbs_at91_pep_to_number(pep);
536   cyg_addrword_t pCSR = pCSRn(epn);
537   cyg_addrword_t pFDR = pFDRn(epn);
538   cyg_uint16 space = 0;
539   cyg_uint16 endpoint_size = usbs_at91_endpoint_fifo_size[epn];
540   cyg_uint8 **ppbegin = &usbs_at91_endpoint_pbegin[epn];
541   cyg_uint8 **ppend = &usbs_at91_endpoint_pend[epn];
542   
543   CYG_ASSERT (pep->complete_fn, "No complete_fn()");
544
545   cyg_drv_dsr_lock ();
546   if (usbs_at91_ep0.state != USBS_STATE_CONFIGURED) {   
547     /* If not configured it means there is nothing to do */
548     cyg_drv_dsr_unlock ();
549     
550     if (pep->complete_fn) {
551       (*pep->complete_fn) (pep->complete_data, -EPIPE);
552     }
553     return;
554   }
555   
556   if (pep->halted) {            
557     /* Halted means nothing to do */
558     cyg_drv_dsr_unlock ();
559
560     if (pep->complete_fn) {
561       (*pep->complete_fn) (pep->complete_data, -EAGAIN);
562     }
563     return;
564   }
565
566   if (BITS_ARE_SET (pIMR, 1 << epn)) {
567     cyg_drv_dsr_unlock ();
568
569     if (pep->complete_fn) {
570       (*pep->complete_fn) (pep->complete_data, -EIO);
571     }
572     
573     return;
574   }
575   
576   CYG_ASSERT (BITS_ARE_SET (pCSR, 1 << 9), "Wrong endpoint type");
577   
578   *ppbegin = pep->buffer;       /* Set the working pointers */
579   *ppend = (cyg_uint8 *) ((cyg_uint32) pep->buffer + pep->buffer_size);
580   
581   if (BITS_ARE_SET (pCSR, 0x400)) {     /* IN: tx_endpoint */
582     space = (cyg_uint32) * ppend - (cyg_uint32) * ppbegin;
583     if (space == endpoint_size) {
584       *ppend = *ppbegin;        /* Send zero-packet */
585     }
586     
587     *ppbegin =
588       write_fifo_uint8 (pFDR, *ppbegin,
589                         (cyg_uint8 *) ((cyg_uint32) * ppbegin +
590                                        MIN (space, endpoint_size)));
591     SET_BITS (pCSR, AT91_UDP_CSR_TXPKTRDY);
592     
593     if (*ppend == *ppbegin) {   /* Last packet ? */
594       *ppend = *ppbegin - 1;    /* The packet hasn't been sent yet */
595     }
596   }
597   
598   usbs_at91_endpoint_interrupt_enable (epn, true);
599   
600   cyg_drv_dsr_unlock ();
601 }
602
603 // Perform transmit handling on an endpoint
604 static bool 
605 usbs_at91_endpoint_isr_tx(cyg_uint8 epn)
606 {
607   cyg_addrword_t pCSR = pCSRn(epn);
608   cyg_addrword_t pFDR = pFDRn(epn);
609   cyg_uint8 **ppbegin = &usbs_at91_endpoint_pbegin[epn];
610   cyg_uint8 **ppend = &usbs_at91_endpoint_pend[epn];
611   cyg_uint32 space = 0;
612   cyg_uint16 endpoint_size = usbs_at91_endpoint_fifo_size[epn];
613
614   CLEAR_BITS (pCSR, AT91_UDP_CSR_TXCOMP);
615
616   if (BITS_ARE_CLEARED (pCSR, AT91_UDP_CSR_TXPKTRDY)) {       
617     /* Ready to transmit ? */
618     if (*ppend > *ppbegin) {  
619       /* Something to send */
620       
621       space = (cyg_uint32) * ppend - (cyg_uint32) * ppbegin;
622       if (space == endpoint_size) {
623         *ppend = *ppbegin;            /* Send zero-packet */
624       }
625       
626       *ppbegin =
627         write_fifo_uint8 (pFDR, *ppbegin,
628                           (cyg_uint8 *) ((cyg_uint32) * ppbegin +
629                                          MIN (space, endpoint_size)));
630       SET_BITS (pCSR, AT91_UDP_CSR_TXPKTRDY);
631       
632       if (*ppend == *ppbegin) {       /* Last packet ? */
633         *ppend = *ppbegin - 1;        /* The packet isn't sent yet */
634       }
635     } else {
636       if (*ppend + 1 == *ppbegin) {
637         *ppend = *ppbegin;    /* Flag for DSR */
638         return true;
639       } else {
640         *ppend = *ppbegin - 1;        /* Flag for zero-packet */
641         SET_BITS (pCSR, AT91_UDP_CSR_TXPKTRDY);       /* Send no data */
642       }
643     }
644   }
645   
646   CLEAR_BITS (pCSR,
647               AT91_UDP_CSR_RX_DATA_BK0 | AT91_UDP_CSR_RX_DATA_BK1 |
648               AT91_UDP_CSR_RXSETUP | AT91_UDP_CSR_ISOERROR);
649   return false;
650 }
651
652 static bool
653 usbs_at91_endpoint_isr_rx(cyg_uint8 epn)
654 {
655   cyg_addrword_t pCSR = pCSRn(epn);
656   cyg_addrword_t pFDR = pFDRn(epn);
657   cyg_uint16 *pinfifo = &usbs_at91_endpoint_bytes_in_fifo[epn];
658   cyg_uint8 **ppbegin = &usbs_at91_endpoint_pbegin[epn];
659   cyg_uint8 **ppend = &usbs_at91_endpoint_pend[epn];
660   cyg_uint16 *preceived = &usbs_at91_endpoint_bytes_received[epn];
661   cyg_uint16 endpoint_size = usbs_at91_endpoint_fifo_size[epn];
662
663   if (*preceived == THERE_IS_A_NEW_PACKET_IN_THE_UDP) {      
664     /* There is a new packet */
665     *preceived = ((*(cyg_uint32 *)pCSR) >> 16) & 0x7ff;    
666     *pinfifo = *preceived;           
667   }
668
669   while ((*ppbegin < *ppend) && *pinfifo) { 
670     /* If we have buffer-space AND data in the FIFO */
671     HAL_READ_UINT8(pFDR, **ppbegin);
672     (*ppbegin)++;
673     (*pinfifo)--;
674   }
675   
676   if (*ppbegin == *ppend) { 
677     /* The buffer is full... call the DSR */
678     return true;
679   }
680   
681   if (*pinfifo == 0) {      
682     /* If the FIFO is empty, then we can release it */
683     if (usbs_at91_endpoint_pingpong[epn]) { 
684       /* Time to clear the interrupt flag */
685       
686       if (usbs_at91_endpoint_bank1[epn]) {
687         CLEAR_BITS (pCSR, AT91_UDP_CSR_RX_DATA_BK1);
688       } else {
689         CLEAR_BITS (pCSR, AT91_UDP_CSR_RX_DATA_BK0);
690       }
691       usbs_at91_endpoint_bank1[epn] = !usbs_at91_endpoint_bank1[epn];
692     } else {
693       CLEAR_BITS (pCSR, AT91_UDP_CSR_RX_DATA_BK0);
694     }
695     
696     if (*preceived < endpoint_size) {
697       /* If the last packet was smaller then the endpoint-size... */
698       *ppend = *ppbegin;
699       *preceived = THERE_IS_A_NEW_PACKET_IN_THE_UDP; /* Set flag */
700       
701       return true;     /* We can call the completion-function in the DSR */
702     }
703     
704     *preceived = THERE_IS_A_NEW_PACKET_IN_THE_UDP;   /* Set flag */
705   }
706   return false;
707 }
708     
709 // ISR for an endpoint. Handle receive and transmit interrupts.
710 static bool
711 usbs_at91_endpoint_isr (cyg_uint8 epn)
712 {                               
713   cyg_addrword_t pCSR = pCSRn(epn);
714   
715   CYG_ASSERT (AT91_USB_ENDPOINTS > epn && epn, "Invalid end point");
716   
717   if (BITS_ARE_SET (pCSR, 0x400)) {      /* IN: tx_endpoint */
718     if (usbs_at91_endpoint_isr_tx(epn))
719       return true;  // Call the DSR
720   } else {                               /* OUT: rx_endpoint */
721     if (!BITS_ARE_CLEARED (pCSR, AT91_UDP_CSR_RX_DATA_BK0) || 
722         !BITS_ARE_CLEARED (pCSR, AT91_UDP_CSR_RX_DATA_BK1)) {   
723       /* Sometime something received ? */
724       if(usbs_at91_endpoint_isr_rx(epn))
725         return true; // Call the DSR;
726     }
727         
728         CLEAR_BITS (pCSR,
729                 AT91_UDP_CSR_TXCOMP | AT91_UDP_CSR_RXSETUP |
730                 AT91_UDP_CSR_ISOERROR);
731   }
732
733   return false;
734 }
735
736 // Handle a DSR for an endpoint
737 static void
738 usbs_at91_endpoint_dsr (cyg_uint8 epn)
739 {
740   usbs_rx_endpoint *pep = (usbs_rx_endpoint *) usbs_at91_endpoints[epn];
741   cyg_uint8 **ppbegin = &usbs_at91_endpoint_pbegin[epn];
742   cyg_uint8 **ppend = &usbs_at91_endpoint_pend[epn];
743
744   CYG_ASSERT (AT91_USB_ENDPOINTS > epn && epn, "Invalid end point");
745   CYG_ASSERT (pep->complete_fn, "No complete_fn()");
746
747   if (*ppend == *ppbegin) {     /* Transmitted/Received ? */
748     
749     pep->buffer_size = (cyg_uint32) * ppbegin - (cyg_uint32) pep->buffer;
750     if (pep->buffer_size && pep->complete_fn) {
751       if (!pep->halted) {
752         (*pep->complete_fn) (pep->complete_data, pep->buffer_size);
753       } else {
754         (*pep->complete_fn) (pep->complete_data, -EAGAIN);
755       }
756     }
757     usbs_at91_endpoint_interrupt_enable (epn, false);
758   }
759 }
760
761 // Handle an error condition on the control endpoint
762 static ep0_low_level_status_t
763 usbs_at91_control_error(ep0_low_level_status_t status)
764 {
765   cyg_uint8 **ppbegin = &usbs_at91_endpoint_pbegin[0];
766   cyg_uint8 **ppend = &usbs_at91_endpoint_pend[0];
767
768   usbs_at91_ep0.buffer_size = 0;
769   usbs_at91_ep0.fill_buffer_fn = 0;
770   usbs_at91_ep0.complete_fn = 0;
771   
772   *ppbegin = usbs_at91_ep0.buffer;
773   *ppend = *ppbegin;
774   
775   if (status == EP0_LL_IDLE) {
776     if (usbs_at91_ep0.complete_fn) {
777       (*usbs_at91_ep0.complete_fn) (&usbs_at91_ep0,
778                                     USBS_CONTROL_RETURN_STALL);
779     }
780   }
781   
782   status = EP0_LL_IDLE;
783   
784   CLEAR_BITS (pCSR0, AT91_UDP_CSR_ISOERROR | AT91_UDP_CSR_FORCESTALL);
785   
786   return status;
787 }
788
789 // Handle a get status setup message on the control end point
790 static ep0_low_level_status_t 
791 usbs_at91_control_setup_get_status(void)
792 {
793   ep0_low_level_status_t status;
794   usb_devreq *req = (usb_devreq *)usbs_at91_ep0.control_buffer;
795   cyg_uint8 recipient = req->type & USB_DEVREQ_RECIPIENT_MASK;
796   cyg_uint8 **ppbegin = &usbs_at91_endpoint_pbegin[0];
797   cyg_uint8 **ppend = &usbs_at91_endpoint_pend[0];
798   cyg_uint16 word = 0;
799   
800   status = EP0_LL_SEND_READY;
801   
802   switch (recipient) {
803     case USB_DEVREQ_RECIPIENT_DEVICE:
804     case USB_DEVREQ_RECIPIENT_INTERFACE:
805       // Nothing to do
806       break;
807     case USB_DEVREQ_RECIPIENT_ENDPOINT: 
808       if ((usbs_at91_ep0.state == USBS_STATE_CONFIGURED) && 
809           (req->index_lo > 0) && 
810           (req->index_lo < AT91_USB_ENDPOINTS)) {
811         cyg_uint32 CSR;
812         
813         HAL_READ_UINT32(pCSRn(req->index_lo), CSR);
814         if (CSR & AT91_UDP_CSR_EPEDS) {
815           word = 1;
816         }
817       } else {
818         status = EP0_LL_STALL;
819       }
820       break;
821     default:
822       status = EP0_LL_STALL;
823   }
824   
825   *ppbegin = (cyg_uint8 *)&word;
826   *ppend = *ppbegin + sizeof (word);
827   return status;
828 }
829
830 // Setup the begin and end pointers such that an ACK is sent
831 static void
832 usbs_at91_control_setup_send_ack(void)
833 {
834   cyg_uint8 **ppbegin = &usbs_at91_endpoint_pbegin[0];
835   cyg_uint8 **ppend = &usbs_at91_endpoint_pend[0];
836   
837   *ppbegin = usbs_at91_ep0.buffer;
838   *ppend = *ppbegin;
839 }
840
841 // Handle a get status set feature message on the control endpoint
842 static ep0_low_level_status_t 
843 usbs_at91_control_setup_set_feature(void)
844 {
845   ep0_low_level_status_t status;
846   usb_devreq *req = (usb_devreq *)usbs_at91_ep0.control_buffer;
847   cyg_uint8 recipient = req->type & USB_DEVREQ_RECIPIENT_MASK;
848   
849   usbs_at91_control_setup_send_ack();
850   status = EP0_LL_SEND_READY;
851   
852   switch(recipient) {
853     case USB_DEVREQ_RECIPIENT_DEVICE:
854       status = EP0_LL_STALL;
855       break;
856     case USB_DEVREQ_RECIPIENT_INTERFACE:
857       // Nothing to do
858       break;
859     case USB_DEVREQ_RECIPIENT_ENDPOINT: 
860       if ((usbs_at91_ep0.state == USBS_STATE_CONFIGURED) && 
861           (req->index_lo > 0) && 
862           (req->index_lo < AT91_USB_ENDPOINTS)) {
863         cyg_uint32 CSR;
864         
865         HAL_READ_UINT32(pCSRn(req->index_lo), CSR);
866         if (CSR & AT91_UDP_CSR_EPEDS) {
867           /* TODO */
868         }
869       } else {
870         status = EP0_LL_STALL;
871       }
872     default:
873       status = EP0_LL_STALL;
874   }
875   return status;
876 }
877
878 // Handle a get status clear feature message on the control endpoint
879 static ep0_low_level_status_t 
880 usbs_at91_control_setup_clear_feature(void)
881 {
882   ep0_low_level_status_t status;
883   usb_devreq *req = (usb_devreq *)usbs_at91_ep0.control_buffer;
884   cyg_uint8 recipient = req->type & USB_DEVREQ_RECIPIENT_MASK;
885   
886   usbs_at91_control_setup_send_ack();
887   status = EP0_LL_SEND_READY;
888
889   switch (recipient) {
890     case USB_DEVREQ_RECIPIENT_DEVICE:
891       status = EP0_LL_STALL;
892       break;
893     case USB_DEVREQ_RECIPIENT_INTERFACE:
894       // Nothing to do
895       break;
896     case USB_DEVREQ_RECIPIENT_ENDPOINT: 
897       if ((usbs_at91_ep0.state == USBS_STATE_CONFIGURED) && 
898           (req->index_lo > 0) && 
899           (req->index_lo < AT91_USB_ENDPOINTS)) {
900         cyg_uint32 CSR;
901         
902         HAL_READ_UINT32(pCSRn(req->index_lo), CSR);
903         if (CSR & AT91_UDP_CSR_EPEDS) {
904           /* TODO */
905         }
906       } else {
907         status = EP0_LL_STALL;
908       }
909     default:
910       status = EP0_LL_STALL;
911   }
912   return status;
913 }
914
915 // Handle a setup message from the host
916 static ep0_low_level_status_t
917 usbs_at91_control_setup(ep0_low_level_status_t status)
918 {
919   cyg_uint8 **ppbegin = &usbs_at91_endpoint_pbegin[0];
920   cyg_uint8 **ppend = &usbs_at91_endpoint_pend[0];
921   usb_devreq *req = (usb_devreq *) usbs_at91_ep0.control_buffer;
922   cyg_uint8   protocol;
923   cyg_uint16 length;
924   bool dev_to_host;
925   usbs_control_return usbcode;
926   bool handled = false;
927   
928   usbs_at91_ep0.buffer_size = 0;
929   usbs_at91_ep0.fill_buffer_fn = 0;
930   usbs_at91_ep0.complete_fn = 0;
931   
932   read_fifo_uint8 ((cyg_uint8 *)req, pFDR0, sizeof (usb_devreq));
933
934   length = (req->length_hi << 8) | req->length_lo;;
935   dev_to_host = req->type & USB_DEVREQ_DIRECTION_IN;
936   
937   CLEAR_BITS (pCSR0, AT91_UDP_CSR_DTGLE);
938
939   status = EP0_LL_REQUEST;
940
941   protocol = req->type & (USB_DEVREQ_TYPE_MASK);
942   
943   // Set the next transfer direction
944   if (dev_to_host) {        
945     SET_BITS (pCSR0, AT91_UDP_CSR_DIR);     /* Set IN direction */
946   } else {
947     CLEAR_BITS (pCSR0, AT91_UDP_CSR_DIR);   /* Set OUT direction */
948   }
949
950   if (protocol == USB_DEVREQ_TYPE_STANDARD) {
951     handled = true;
952     switch (req->request) {
953       case USB_DEVREQ_GET_STATUS:
954         status = usbs_at91_control_setup_get_status();
955         break;
956       case USB_DEVREQ_SET_ADDRESS:
957         // Most of the hard work is done by the hardware. We just need
958         // to send an ACK.
959         usbs_at91_control_setup_send_ack();
960         status = EP0_LL_SEND_READY;
961         break;
962       case USB_DEVREQ_SET_FEATURE:     
963         status = usbs_at91_control_setup_set_feature();
964         break;
965       case USB_DEVREQ_CLEAR_FEATURE:
966         status = usbs_at91_control_setup_clear_feature();
967         break;
968       default:
969         handled = false;
970     }
971   }
972   if ((protocol != USB_DEVREQ_TYPE_STANDARD) || !handled) {
973     // Ask the layer above to process the message
974     usbcode = usbs_parse_host_get_command (&usbs_at91_ep0);
975     usbs_at91_ep0.buffer_size = MIN (usbs_at91_ep0.buffer_size, length);
976       
977     *ppbegin = usbs_at91_ep0.buffer;
978     *ppend = *ppbegin + usbs_at91_ep0.buffer_size; /* Ready to send... */
979     
980     if (usbcode == USBS_CONTROL_RETURN_HANDLED) { /* OK */
981       if (dev_to_host) {  
982         status = EP0_LL_SEND_READY;
983       } else {
984         status = EP0_LL_RECEIVE_READY;
985       }
986     } else {
987       status = EP0_LL_STALL;
988     }
989   }
990   // Clear the setup bit so indicating we have processed the message
991   CLEAR_BITS (pCSR0, AT91_UDP_CSR_RXSETUP);   
992   
993   return status;
994 }
995
996 static ep0_low_level_status_t
997 usbs_at91_control_data_recv(ep0_low_level_status_t status)
998 {
999   cyg_uint32 received = 0;
1000   cyg_uint32 length;
1001   cyg_uint8 **ppbegin = &usbs_at91_endpoint_pbegin[0];
1002   cyg_uint8 **ppend = &usbs_at91_endpoint_pend[0];
1003   usbs_control_return usbcode;
1004   
1005   if (status == EP0_LL_RECEIVE_READY) {
1006     received = ((*(cyg_uint32 *) pCSR0) >> 16) & 0x7ff;
1007     length = MIN (received, (cyg_uint32) *ppend - (cyg_uint32) *ppbegin);
1008     *ppbegin = read_fifo_uint8 (*ppbegin, pFDR0, length);
1009     
1010     if (received < usbs_at91_endpoint_fifo_size[0]) {        /* Last packet ? */
1011       *ppend = *ppbegin;
1012     }
1013     
1014     if (*ppbegin == *ppend) {   /* All received ? */
1015       usbs_at91_ep0.buffer_size =
1016         (cyg_uint32) *ppend - (cyg_uint32) usbs_at91_ep0.buffer;
1017       usbcode = USBS_CONTROL_RETURN_STALL;
1018       
1019       if (usbs_at91_ep0.complete_fn) {
1020         usbcode = (*usbs_at91_ep0.complete_fn) (&usbs_at91_ep0, 0);
1021       }
1022       
1023       if (usbcode == USBS_CONTROL_RETURN_HANDLED) {
1024         status = EP0_LL_SEND_READY;
1025       } else {
1026         status = EP0_LL_STALL;
1027       }
1028     }
1029   }
1030   
1031   CLEAR_BITS (pCSR0, AT91_UDP_CSR_RX_DATA_BK0);
1032   
1033   return status;
1034 }
1035
1036 static ep0_low_level_status_t
1037 usbs_at91_control_data_sent(ep0_low_level_status_t status)
1038 {
1039   cyg_uint8 **ppbegin = &usbs_at91_endpoint_pbegin[0];
1040   cyg_uint8 **ppend = &usbs_at91_endpoint_pend[0];
1041   cyg_uint32 bytes_to_write = 0;
1042   usb_devreq *req = (usb_devreq *)usbs_at91_ep0.control_buffer;
1043   cyg_uint16 value;
1044
1045   switch (status) {
1046     case EP0_LL_SEND_READY: 
1047       if (*ppbegin == *ppend &&
1048           usbs_at91_ep0.fill_buffer_fn == NULL) {   
1049         // All bytes are sent, send ACK
1050         status = EP0_LL_ACK;
1051         SET_BITS (pCSR0, AT91_UDP_CSR_TXPKTRDY);      // Signal FIFO loaded 
1052       } else {
1053         // We have more bytes to send
1054         bytes_to_write =
1055           MIN (*ppend - *ppbegin, usbs_at91_endpoint_fifo_size[0]);
1056         *ppbegin = write_fifo_uint8 (pFDR0, *ppbegin, (cyg_uint8 *) 
1057                                      ((cyg_uint32) *ppbegin + bytes_to_write));
1058         // Send next few bytes 
1059         if (*ppbegin == *ppend) { /* Control-Endoints don't need ACK's */
1060           if (usbs_at91_ep0.fill_buffer_fn) { // More Records ?
1061             (*usbs_at91_ep0.fill_buffer_fn) (&usbs_at91_ep0);
1062             
1063             *ppbegin = usbs_at91_ep0.buffer;
1064             *ppend = *ppbegin + usbs_at91_ep0.buffer_size;        
1065             
1066             /* Ready to send... */
1067             bytes_to_write =
1068               MIN (*ppend - *ppbegin,
1069                    usbs_at91_endpoint_fifo_size[0] - bytes_to_write);
1070             
1071             *ppbegin = write_fifo_uint8 (pFDR0, *ppbegin, (cyg_uint8 *) 
1072                                          ((cyg_uint32) *ppbegin + bytes_to_write));
1073             // Send next few bytes 
1074           } else {
1075             if (bytes_to_write == usbs_at91_endpoint_fifo_size[0]) {
1076               // Last packet is full, so we need to send a zero bytes
1077               // packet next time
1078               status = EP0_LL_SEND_READY;
1079             } else {
1080               status = EP0_LL_IDLE;
1081             }
1082             
1083           }
1084         }
1085         SET_BITS (pCSR0, AT91_UDP_CSR_TXPKTRDY);      // Signal FIFO loaded 
1086       }
1087       break;
1088     case EP0_LL_RECEIVE_READY:       
1089       /* Maybe we have to send an ACK */
1090       if (*ppbegin == *ppend) {   // All bytes are received, send ACK
1091         status = EP0_LL_ACK;
1092         SET_BITS (pCSR0, AT91_UDP_CSR_TXPKTRDY);      // Signal FIFO loaded 
1093       }
1094       break;
1095     case EP0_LL_ACK:
1096       if (req->request == USB_DEVREQ_SET_ADDRESS) {   // Special-processing 
1097         HAL_WRITE_UINT32 (AT91_UDP + AT91_UDP_FADDR,
1098                           req->value_lo | AT91_UDP_FADDR_FEN);
1099         value = (req->value_hi << 8) | req->value_lo;
1100         if (value) {
1101           usbs_at91_ep0.state = USBS_STATE_ADDRESSED;
1102         }
1103       }
1104       
1105       if (usbs_at91_ep0.complete_fn) {
1106         (*usbs_at91_ep0.complete_fn) (&usbs_at91_ep0,
1107                                       USBS_CONTROL_RETURN_HANDLED);
1108       }
1109       status = EP0_LL_IDLE;
1110       usbs_state_notify (&usbs_at91_ep0);
1111       break;
1112     default:
1113       break;
1114   }
1115   return status;
1116 }
1117   
1118 static void
1119 usbs_at91_control_dsr (void)
1120 {
1121   static ep0_low_level_status_t status = EP0_LL_IDLE;
1122   
1123   while (!BITS_ARE_CLEARED(pCSR0,
1124                            AT91_UDP_CSR_TXCOMP | AT91_UDP_CSR_RX_DATA_BK0 |
1125                            AT91_UDP_CSR_RXSETUP | AT91_UDP_CSR_ISOERROR |
1126                            AT91_UDP_CSR_RX_DATA_BK1)) {
1127     
1128     // Check and handle any error conditions
1129     if (BITS_ARE_SET (pCSR0, AT91_UDP_CSR_ISOERROR)) {
1130       status = usbs_at91_control_error(status);
1131     }
1132     
1133     // Check for a setup message and handle it
1134     if (BITS_ARE_SET (pCSR0, AT91_UDP_CSR_RXSETUP)) {
1135       status = usbs_at91_control_setup(status);
1136     }
1137     
1138     // Check for received data on the control endpoint
1139     if (BITS_ARE_SET (pCSR0, AT91_UDP_CSR_RX_DATA_BK0)) {
1140       status = usbs_at91_control_data_recv(status);
1141     }
1142     
1143     // Check if the last packet has been sent
1144     if (BITS_ARE_CLEARED (pCSR0, AT91_UDP_CSR_TXPKTRDY)) {
1145       status = usbs_at91_control_data_sent(status);
1146     }
1147
1148     // Received an ACK packet
1149     if (BITS_ARE_SET (pCSR0, AT91_UDP_CSR_TXCOMP)) {
1150       CLEAR_BITS (pCSR0, AT91_UDP_CSR_TXCOMP);
1151     }
1152     
1153     if (status == EP0_LL_STALL) {
1154       CLEAR_BITS (pCSR0, 0x7f);
1155       SET_BITS (pCSR0, AT91_UDP_CSR_FORCESTALL);
1156     }
1157   }
1158 }
1159
1160 static void
1161 usbs_at91_dsr (cyg_vector_t vector, cyg_ucount32 count, cyg_addrword_t data)
1162 {
1163   cyg_uint8 n;
1164   
1165   CYG_ASSERT (CYGNUM_HAL_INTERRUPT_UDP == vector, "Wrong interrupts");
1166   CYG_ASSERT (0 == data, "DSR needs no data");
1167   
1168   CLEAR_BITS (AT91_UDP + AT91_UDP_GLB_STATE, 0x10);
1169
1170   if (BITS_ARE_SET (pISR, AT91_UDP_WAKEUP)) {
1171     usbs_at91_ep0.state = USBS_STATE_DEFAULT;
1172     usbs_state_notify (&usbs_at91_ep0);
1173
1174     HAL_WRITE_UINT32 (pICR, AT91_UDP_WAKEUP);
1175   }
1176   
1177   if (BITS_ARE_SET (pISR, AT91_UDP_ENDBUSRES)) {        // RESET UDP 
1178     usbs_at91_ep0.state = USBS_STATE_POWERED;
1179     usbs_state_notify (&usbs_at91_ep0);
1180     usbs_at91_handle_reset ();
1181     
1182     HAL_WRITE_UINT32 (pCSR0, AT91_UDP_CSR_EPEDS | AT91_UDP_CSR_EPTYPE_CTRL);
1183     HAL_WRITE_UINT32 (pIER, AT91_UDP_EPINT0);
1184     
1185     usbs_at91_ep0.state = USBS_STATE_DEFAULT;
1186     usbs_state_notify (&usbs_at91_ep0);
1187     
1188     HAL_WRITE_UINT32 (pICR, AT91_UDP_ENDBUSRES);
1189   }
1190
1191   if (BITS_ARE_SET (pISR, AT91_UDP_SOFINT)) {
1192     HAL_WRITE_UINT32 (pICR, AT91_UDP_SOFINT);
1193   }
1194
1195   if (BITS_ARE_SET (pISR, AT91_UDP_EXTRSM)) {
1196     usbs_at91_ep0.state = usbs_at91_ep0.state & ~USBS_STATE_SUSPENDED;
1197     usbs_state_notify (&usbs_at91_ep0);
1198     HAL_WRITE_UINT32 (pICR, AT91_UDP_EXTRSM);
1199   }
1200   
1201   if (BITS_ARE_SET (pISR, AT91_UDP_RXRSM)) {
1202     usbs_at91_ep0.state = usbs_at91_ep0.state & ~USBS_STATE_SUSPENDED;
1203     usbs_state_notify (&usbs_at91_ep0);
1204     HAL_WRITE_UINT32 (pICR, AT91_UDP_RXRSM);
1205   }
1206
1207   if (BITS_ARE_SET (pISR, AT91_UDP_RXSUSP)) {
1208     usbs_at91_ep0.state = usbs_at91_ep0.state | USBS_STATE_SUSPENDED;
1209     usbs_state_notify (&usbs_at91_ep0);
1210     HAL_WRITE_UINT32 (pICR, AT91_UDP_RXSUSP);
1211   }
1212
1213   if (BITS_ARE_SET (pISR, AT91_UDP_EPINT0)) {
1214     usbs_at91_control_dsr ();
1215   }
1216
1217   for (n = 1; n < AT91_USB_ENDPOINTS; n++) {
1218     if (*(cyg_uint32 *) pIMR & (1 << n)) {
1219       usbs_at91_endpoint_dsr (n);
1220     }
1221   }
1222   
1223   cyg_drv_interrupt_unmask (vector);
1224 }
1225
1226 static cyg_uint32
1227 usbs_at91_isr (cyg_vector_t vector, cyg_addrword_t data)
1228 {
1229   cyg_uint8 n;
1230   bool need_dsr = false;
1231   cyg_uint32 IMR;
1232   cyg_uint32 ISR;
1233   
1234   CYG_ASSERT (CYGNUM_HAL_INTERRUPT_UDP == vector, "Wrong interrupts");
1235   CYG_ASSERT (0 == data, "ISR needs no data");
1236
1237   HAL_READ_UINT32(pIMR, IMR);
1238   HAL_READ_UINT32(pISR, ISR);
1239   
1240   for (n = 1; n < AT91_USB_ENDPOINTS; n++) {    
1241     /* Do any data endpoint need a data transfer ? */
1242     if (IMR & ISR & (1 << n)) {
1243       need_dsr = usbs_at91_endpoint_isr (n) || need_dsr;
1244     }
1245   }
1246   /* If we don't need any DSR re-enable interrupts and finish */
1247   if (BITS_ARE_CLEARED (pISR, AT91_UDP_ALLOWED_IRQs & 0xffffff01)
1248       && !need_dsr) {           
1249     cyg_drv_interrupt_acknowledge (vector);
1250     return CYG_ISR_HANDLED;
1251   }
1252
1253   /* Call the DSR */
1254   cyg_drv_interrupt_mask (vector);      
1255   cyg_drv_interrupt_acknowledge (vector);
1256   
1257   return CYG_ISR_HANDLED | CYG_ISR_CALL_DSR;
1258 }
1259
1260
1261
1262 // ----------------------------------------------------------------------------
1263 // Polling support. It is not clear that this is going to work particularly
1264 // well since according to the documentation the hardware does not generate
1265 // NAKs automatically - instead the ISR has to set the appropriate bits
1266 // sufficiently quickly to avoid confusing the host.
1267 //
1268 // Calling the isr directly avoids duplicating code, but means that
1269 // cyg_drv_interrupt_acknowledge() will get called when not inside a
1270 // real interrupt handler. This should be harmless.
1271
1272 static void
1273 usbs_at91_poll (usbs_control_endpoint * endpoint)
1274 {
1275   CYG_ASSERT (endpoint == &usbs_at91_ep0, "Wrong endpoint");
1276   if (CYG_ISR_CALL_DSR == usbs_at91_isr (CYGNUM_HAL_INTERRUPT_UDP, 0)) {
1277     usbs_at91_dsr (CYGNUM_HAL_INTERRUPT_UDP, 0, 0);
1278   }
1279 }
1280
1281 // ----------------------------------------------------------------------------
1282 // Initialization
1283 //
1284 // This routine gets called from a prioritized static constructor during
1285 // eCos startup.
1286
1287 void
1288 usbs_at91_init (void)
1289 {
1290
1291   cyg_uint32 reg;
1292
1293   HAL_READ_UINT32 (AT91_PMC + AT91_PMC_PLLR, reg);      
1294   
1295   /* Set USB divider so we have a 48MHz clock */
1296 #if   ((CYGNUM_HAL_ARM_AT91_CLOCK_SPEED <  48120000) && \
1297        (CYGNUM_HAL_ARM_AT91_CLOCK_SPEED >  47880000))
1298   
1299   // 48MHz clock, divider set to 1
1300   HAL_WRITE_UINT32 (AT91_PMC + AT91_PMC_PLLR,
1301                     (reg & 0x0fffffff) | AT91_PMC_PLLR_USBDIV_1);
1302   
1303 #elif ((CYGNUM_HAL_ARM_AT91_CLOCK_SPEED <  96240000) && \
1304        (CYGNUM_HAL_ARM_AT91_CLOCK_SPEED >  95760000))
1305
1306   // 96MHz clock, divider set to 2
1307   HAL_WRITE_UINT32 (AT91_PMC + AT91_PMC_PLLR,
1308                     (reg & 0x0fffffff) | AT91_PMC_PLLR_USBDIV_2);
1309 #else
1310 #error CYGNUM_HAL_ARM_AT91_CLOCK_SPEED is not 48, 96 or 192MHz plusminus 0.25% ...
1311 #endif
1312
1313   /* Enable USB clock */
1314   HAL_WRITE_UINT32 (AT91_PMC + AT91_PMC_SCER, AT91_PMC_SCER_UDP);       
1315   HAL_WRITE_UINT32 (AT91_PMC + AT91_PMC_PCER, AT91_PMC_PCER_UDP); 
1316
1317   usbs_at91_set_pullup (false);
1318 #ifndef CYGDAT_DEVS_USB_AT91_GPIO_SET_PULLUP_PIN_NONE
1319   HAL_ARM_AT91_GPIO_CFG_DIRECTION(CYGDAT_DEVS_USB_AT91_GPIO_SET_PULLUP_PIN,
1320                                   AT91_PIN_OUT);
1321 #endif
1322 #ifndef CYGDAT_DEVS_USB_AT91_GPIO_READ_POWER_PIN_NONE
1323   HAL_ARM_AT91_GPIO_CFG_DIRECTION(CYGDAT_DEVS_USB_AT91_GPIO_READ_POWER_PIN,
1324                                   AT91_PIN_IN);
1325 #endif
1326   usbs_at91_handle_reset ();
1327   
1328   cyg_drv_interrupt_create (CYGNUM_HAL_INTERRUPT_UDP, 
1329                             6,  // priority
1330                             0,  // data
1331                             &usbs_at91_isr,
1332                             &usbs_at91_dsr,
1333                             &usbs_at91_intr_handle, &usbs_at91_intr_data);
1334   
1335   cyg_drv_interrupt_attach (usbs_at91_intr_handle);
1336   cyg_drv_interrupt_unmask (CYGNUM_HAL_INTERRUPT_UDP);
1337   
1338   HAL_WRITE_UINT32 (AT91_UDP + AT91_UDP_TXVC, 0);
1339   
1340   usbs_at91_ep0.state = USBS_STATE_POWERED;
1341   usbs_state_notify (&usbs_at91_ep0);
1342 }
1343
1344 // ----------------------------------------------------------------------------
1345 // Testing support.
1346 usbs_testing_endpoint usbs_testing_endpoints[] = {
1347     {
1348         endpoint_type       : USB_ENDPOINT_DESCRIPTOR_ATTR_CONTROL, 
1349         endpoint_number     : 0,
1350         endpoint_direction  : USB_ENDPOINT_DESCRIPTOR_ENDPOINT_IN,
1351         endpoint            : (void*) &usbs_at91_ep0,
1352 #ifdef CYGVAR_DEVS_USB_AT91_EP0_DEVTAB_ENTRY
1353         devtab_entry        : CYGDAT_DEVS_USB_AT91_DEVTAB_BASENAME "0c",
1354 #else        
1355         devtab_entry        : (const char*) 0,
1356 #endif        
1357         min_size            : 1,            // zero-byte control transfers are meaningless
1358         max_size            : 0x0FFFF,      // limit imposed by protocol
1359         max_in_padding      : 0,
1360         alignment           : 0
1361     },
1362     {
1363         endpoint_type       : USB_ENDPOINT_DESCRIPTOR_ATTR_BULK,
1364         endpoint_number     : 1,
1365         endpoint_direction  : USB_ENDPOINT_DESCRIPTOR_ENDPOINT_OUT,
1366         endpoint            : (void*) &usbs_at91_ep1,
1367 #ifdef CYGVAR_DEVS_USB_AT91_EP1_DEVTAB_ENTRY
1368         devtab_entry        : CYGDAT_DEVS_USB_AT91_DEVTAB_BASENAME "1r",
1369 #else        
1370         devtab_entry        : (const char*) 0,
1371 #endif        
1372         min_size            : 1,
1373         max_size            : -1,           // No hardware or driver limitation
1374         max_in_padding      : 0,
1375         alignment           : 0
1376     },
1377     {
1378         endpoint_type       : USB_ENDPOINT_DESCRIPTOR_ATTR_BULK,
1379         endpoint_number     : 2,
1380         endpoint_direction  : USB_ENDPOINT_DESCRIPTOR_ENDPOINT_IN,
1381         endpoint            : (void*) &usbs_at91_ep2,
1382 #ifdef CYGVAR_DEVS_USB_AT91_EP2_DEVTAB_ENTRY
1383         devtab_entry        : CYGDAT_DEVS_USB_AT91_DEVTAB_BASENAME "2w",
1384 #else        
1385         devtab_entry        : (const char*) 0,
1386 #endif        
1387         min_size            : 1,
1388         max_size            : -1,           // No hardware or driver limitation
1389         max_in_padding      : 1,            // hardware limitation
1390         alignment           : 0
1391     },
1392     {
1393         endpoint_type       : USB_ENDPOINT_DESCRIPTOR_ATTR_BULK,
1394         endpoint_number     : 3,
1395         endpoint_direction  : USB_ENDPOINT_DESCRIPTOR_ENDPOINT_IN,
1396         endpoint            : (void*) &usbs_at91_ep3,
1397 #ifdef CYGVAR_DEVS_USB_AT91_EP3_DEVTAB_ENTRY
1398         devtab_entry        : CYGDAT_DEVS_USB_AT91_DEVTAB_BASENAME "3w",
1399 #else        
1400         devtab_entry        : (const char*) 0,
1401 #endif        
1402         min_size            : 1,
1403         max_size            : -1,           // No hardware or driver limitation
1404         max_in_padding      : 1,            // hardware limitation
1405         alignment           : 0
1406     },
1407     USBS_TESTING_ENDPOINTS_TERMINATOR
1408 };