first adaption of 'usbport' by Benedikt Sauter
[ppcskel.git] / usb / host / at90usb1287-hcd.c
1 /*
2   //
3  * Copyright (c) 2007, Benedikt Sauter <sauter@ixbat.de>
4  * All rights reserved.
5  *
6  * Short descripton of file: at90usb1287-hcdi.c
7  *
8  *
9  * Redistribution and use in source and binary forms, with or without 
10  * modification, are permitted provided that the following conditions 
11  * are met:
12  *
13  *   * Redistributions of source code must retain the above copyright 
14  *     notice, this list of conditions and the following disclaimer.
15  *   * Redistributions in binary form must reproduce the above 
16  *     copyright notice, this list of conditions and the following 
17  *     disclaimer in the documentation and/or other materials provided 
18  *     with the distribution.
19  *   * Neither the name of the FH Augsburg nor the names of its 
20  *     contributors may be used to endorse or promote products derived 
21  *     from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
26  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
27  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
29  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
30  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
31  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
32  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
33  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35
36 #include "host.h"
37 #include <wait.h>
38 #include <stdlib.h>
39
40 #include "at90usb1287.h"
41 #include <avr/io.h>
42
43 #include "uart.h"
44 #include "wait.h"
45
46 #include <core/core.h>
47 #include <usbspec/usb11spec.h>
48
49 void at90usb1287_roothub_probe();
50 void at90usb1287_roothub_check();
51
52 void at90usb1287_start_transfer();
53
54 usb_device * device_on_downstream;
55
56
57 /* cuurent transferdescriptor on port a and port b */
58 usb_transfer_descriptor * td_usba;
59 usb_transfer_descriptor * td_usbb;
60
61 usb_driver at90usb1287_roothub = {
62   .name   = "at90usb1287_roothub",
63   .probe  = at90usb1287_roothub_probe,
64   .check  = at90usb1287_roothub_check,
65   .data   = NULL,
66 };
67
68 usb_device * device_on_downstream;
69
70 /**
71  * Find and initial root hub
72  */
73 void at90usb1287_roothub_probe()
74 {
75   // called on n every new enumeration and at usb_register_driver  
76   // der sollte nach dem ersten aufruf igonriert werden
77   #if DEBUG
78   core.stdout("Probe: AT90USB1287 Root Hub\r\n");
79   #endif
80 }
81
82 /**
83  * This function is called periodical, to notice
84  * port changes after an hub
85  */
86 void at90usb1287_roothub_check()
87 {
88   //USART_WriteHex(UHINT);
89   //UHINT = 0x00;
90   // disconnect 
91   if(UHINT & 0x02) {
92     device_on_downstream=NULL;
93     #if DEBUG
94     core.stdout("Device: Removed!\r\n");
95     #endif
96     //UHINT &= ~(1<<DDISCI);
97   } else {
98     // if connect 
99     if(device_on_downstream!=NULL) {
100       // normal transfer check for interrupt and isochonous endpoints
101       //USART_WriteHex(0x88);
102     } 
103     // check if device is connected 
104     else {
105       if(UHINT & 0x01) {
106         // found new device
107         #if DEBUG
108         core.stdout("Device: Found new one!\r\n");
109         #endif
110
111         if((USBSTA &   (1<<SPEED)) &&(USBSTA &   (1<<SPEED))){
112           // fullspeed
113           #if DEBUG
114           core.stdout("FS\r\n");
115           #endif
116         } else {
117           //lowspeed
118           #if DEBUG
119           core.stdout("LS\r\n");
120           #endif
121           // mark device as low speed
122         }
123         
124         //UHCON = (1<<SOFEN); /* host generate for FS SOF, and for LS keep-alive! */
125         //UHCON = (1<<RESET);
126
127         // SOF on etx
128         device_on_downstream = 1;       
129         //device_on_downstream = usb_add_device();      
130         //UHINT &= ~(1<<DCONNI);
131       }
132       else {
133         // wenn geraet nicht aktiviert ist das immer aufrufen! 
134       //USART_WriteHex(0x55);
135       }
136     }
137   }
138   UHINT = 0x00;
139         /* Usb_disable */
140         USBCON &= ~(1<<USBE);
141         /* Usb_enable */
142         USBCON |= (1<<USBE);
143         /* unfreeze clock */
144         UsbEnableClock();
145         /* usb attach */
146         UDCON   &= ~(1<<DETACH);
147         /* select host */
148         USBCON  |=  (1<<HOST);
149         /* disable vbus hw control */
150         OTGCON  |=  (1<<VBUSHWC);
151         /* enable vbus */
152         OTGCON  |=  (1<<VBUSREQ);
153
154  }
155
156
157 void hcdi_init()
158
159  /* register virtual root hub driver */
160   usb_register_driver(&at90usb1287_roothub);
161
162   /* enable regulator */
163   UHWCON |= (1<<UVREGE); 
164   /* force host mode */
165   UHWCON &= ~(1<<UIDE);
166   UHWCON &= ~(1<<UIMOD);
167
168   /* pll sart */
169   UsbSetPLL_CPU_Frequency();
170   UsbEnablePLL();
171   UsbWaitPLL_Locked();
172
173   /* Usb_disable */
174   USBCON &= ~(1<<USBE);
175   
176   /* Usb_enable */
177   USBCON |= (1<<USBE);
178   
179   /* unfreeze clock */
180   UsbEnableClock();
181
182   /* usb attach */
183   UDCON   &= ~(1<<DETACH);
184
185   /* enable uconv pin */
186   UsbDisableUVCON_PinControl();
187   UsbEnableUID_ModeSelection();
188
189   /* select host */
190   USBCON  |=  (1<<HOST);
191
192   /* disable vbus hw control */
193   OTGCON  |=  (1<<VBUSHWC);
194
195   // 4
196   /* enable vbus */
197   OTGCON  |=  (1<<VBUSREQ);
198   
199   UHIEN |= (1<<DDISCE);
200   UHIEN |= (1<<DCONNE);
201
202   UHIEN = 0xff;
203  
204  }
205
206 /**
207  * hcdi_enqueue takes usb_irp and split it into
208  * several usb packeges (SETUP,IN,OUT)
209  * In the usbstack they are transported with the
210  * usb_transfer_descriptor data structure.
211  */
212
213 u8 hcdi_enqueue(usb_transfer_descriptor *td)
214 {
215   #if LIBMODE
216   td_usba = td;
217   at90usb1287_start_transfer();
218   #endif
219   return 1;
220 }
221
222
223 u8 hcdi_dequeue(usb_transfer_descriptor *td)
224 {
225   return 1;
226 }
227
228
229
230 void hcdi_irq()
231 {
232 }
233
234
235
236 void at90usb1287_start_transfer()
237 {
238   usb_transfer_descriptor * td;
239   //core.stdout("Transfer: Start!\r\n");
240
241   #if LIBMODE
242   /* choose next free port */
243   td = td_usba;
244   #endif
245
246   switch(td->pid) {
247     case USB_PID_SETUP:
248     break;
249     
250     case USB_PID_IN:
251     break;
252     
253     case USB_PID_OUT:
254     break;
255   }
256
257
258 }
259
260