Removed the EVENT_USB_InitFailure() event, as not specifying a USB mode to USB_Init...
[pub/USBasp.git] / LUFA / Drivers / USB / LowLevel / USBController.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2010.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, distribute, and sell this
13 software and its documentation for any purpose is hereby granted
14 without fee, provided that the above copyright notice appear in
15 all copies and that both that the copyright notice and this
16 permission notice and warranty disclaimer appear in supporting
17 documentation, and that the name of the author not be used in
18 advertising or publicity pertaining to distribution of the
19 software without specific, written prior permission.
20
21 The author disclaim all warranties with regard to this
22 software, including all implied warranties of merchantability
23 and fitness. In no event shall the author be liable for any
24 special, indirect or consequential damages or any damages
25 whatsoever resulting from loss of use, data or profits, whether
26 in an action of contract, negligence or other tortious action,
27 arising out of or in connection with the use or performance of
28 this software.
29 */
30
31 #define __INCLUDE_FROM_USB_DRIVER
32 #include "USBController.h"
33
34 #if (!defined(USB_HOST_ONLY) && !defined(USB_DEVICE_ONLY))
35 volatile uint8_t USB_CurrentMode = USB_MODE_NONE;
36 #endif
37
38 #if !defined(USE_STATIC_OPTIONS)
39 volatile uint8_t USB_Options;
40 #endif
41
42 void USB_Init(
43 #if defined(USB_CAN_BE_BOTH)
44 const uint8_t Mode
45 #endif
46
47 #if (defined(USB_CAN_BE_BOTH) && !defined(USE_STATIC_OPTIONS))
48 ,
49 #elif (!defined(USB_CAN_BE_BOTH) && defined(USE_STATIC_OPTIONS))
50 void
51 #endif
52
53 #if !defined(USE_STATIC_OPTIONS)
54 const uint8_t Options
55 #endif
56 )
57 {
58 #if defined(USB_CAN_BE_BOTH)
59 USB_CurrentMode = Mode;
60 #endif
61
62 #if !defined(USE_STATIC_OPTIONS)
63 USB_Options = Options;
64 #endif
65
66 #if defined(USB_CAN_BE_HOST)
67 USB_ControlPipeSize = PIPE_CONTROLPIPE_DEFAULT_SIZE;
68 #endif
69
70 #if defined(USB_DEVICE_ONLY) && (defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR))
71 UHWCON |= (1 << UIMOD);
72 #elif defined(USB_HOST_ONLY)
73 UHWCON &= ~(1 << UIMOD);
74 #elif defined(USB_CAN_BE_BOTH)
75 if (Mode == USB_MODE_DEVICE)
76 {
77 UHWCON |= (1 << UIMOD);
78 }
79 else if (Mode == USB_MODE_HOST)
80 {
81 UHWCON &= ~(1 << UIMOD);
82 }
83 else
84 {
85 UHWCON |= (1 << UIDE);
86 }
87 #endif
88
89 USB_ResetInterface();
90
91 #if defined(USB_SERIES_4_AVR) || defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR)
92 USB_OTGPAD_On();
93 #endif
94
95 USB_IsInitialized = true;
96 }
97
98 void USB_ShutDown(void)
99 {
100 USB_ResetInterface();
101 USB_Detach();
102 USB_Controller_Disable();
103
104 USB_INT_DisableAllInterrupts();
105 USB_INT_ClearAllInterrupts();
106
107 #if defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR)
108 UHWCON &= ~(1 << UIMOD);
109 #endif
110
111 if (!(USB_Options & USB_OPT_MANUAL_PLL))
112 USB_PLL_Off();
113
114 USB_REG_Off();
115
116 #if defined(USB_SERIES_4_AVR) || defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR)
117 USB_OTGPAD_Off();
118 #endif
119
120 #if defined(USB_CAN_BE_BOTH)
121 UHWCON &= ~(1 << UIDE);
122 #endif
123
124 USB_IsInitialized = false;
125
126 #if defined(USB_CAN_BE_BOTH)
127 USB_CurrentMode = USB_MODE_NONE;
128 #endif
129 }
130
131 void USB_ResetInterface(void)
132 {
133 USB_INT_DisableAllInterrupts();
134 USB_INT_ClearAllInterrupts();
135
136 #if defined(USB_CAN_BE_HOST)
137 USB_HostState = HOST_STATE_Unattached;
138 #endif
139
140 #if defined(USB_CAN_BE_DEVICE)
141 USB_DeviceState = DEVICE_STATE_Unattached;
142 USB_ConfigurationNumber = 0;
143
144 #if !defined(NO_DEVICE_REMOTE_WAKEUP)
145 USB_RemoteWakeupEnabled = false;
146 #endif
147
148 #if !defined(NO_DEVICE_SELF_POWER)
149 USB_CurrentlySelfPowered = false;
150 #endif
151 #endif
152
153 if (!(USB_Options & USB_OPT_MANUAL_PLL))
154 {
155 #if defined(USB_SERIES_4_AVR)
156 PLLFRQ = ((1 << PLLUSB) | (1 << PDIV3) | (1 << PDIV1));
157 #endif
158
159 USB_PLL_On();
160 while (!(USB_PLL_IsReady()));
161 }
162
163 USB_Controller_Reset();
164
165 #if defined(USB_CAN_BE_BOTH)
166 if (UHWCON & (1 << UIDE))
167 {
168 USB_INT_Clear(USB_INT_IDTI);
169 USB_INT_Enable(USB_INT_IDTI);
170 USB_CurrentMode = USB_GetUSBModeFromUID();
171 }
172 #endif
173
174 if (!(USB_Options & USB_OPT_REG_DISABLED))
175 USB_REG_On();
176 else
177 USB_REG_Off();
178
179 USB_CLK_Unfreeze();
180
181 #if (defined(USB_CAN_BE_DEVICE) && (defined(USB_SERIES_4_AVR) || defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR)))
182 if (USB_CurrentMode == USB_MODE_DEVICE)
183 {
184 if (USB_Options & USB_DEVICE_OPT_LOWSPEED)
185 USB_Device_SetLowSpeed();
186 else
187 USB_Device_SetFullSpeed();
188 }
189 #endif
190
191 #if (defined(USB_CAN_BE_DEVICE) && !defined(FIXED_CONTROL_ENDPOINT_SIZE))
192 if (USB_CurrentMode == USB_MODE_DEVICE)
193 {
194 USB_Descriptor_Device_t* DeviceDescriptorPtr;
195
196 if (CALLBACK_USB_GetDescriptor((DTYPE_Device << 8), 0, (void*)&DeviceDescriptorPtr) != NO_DESCRIPTOR)
197 {
198 #if defined(USE_RAM_DESCRIPTORS)
199 USB_ControlEndpointSize = DeviceDescriptorPtr->Endpoint0Size;
200 #elif defined(USE_EEPROM_DESCRIPTORS)
201 USB_ControlEndpointSize = eeprom_read_byte(&DeviceDescriptorPtr->Endpoint0Size);
202 #else
203 USB_ControlEndpointSize = pgm_read_byte(&DeviceDescriptorPtr->Endpoint0Size);
204 #endif
205 }
206 }
207 #endif
208
209 USB_Attach();
210
211 #if defined(USB_DEVICE_ONLY)
212 USB_INT_Clear(USB_INT_SUSPEND);
213 USB_INT_Enable(USB_INT_SUSPEND);
214 USB_INT_Clear(USB_INT_EORSTI);
215 USB_INT_Enable(USB_INT_EORSTI);
216
217 #if defined(USB_SERIES_4_AVR) || defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR)
218 USB_INT_Enable(USB_INT_VBUS);
219 #endif
220 #elif defined(USB_HOST_ONLY)
221 USB_Host_HostMode_On();
222
223 USB_Host_VBUS_Auto_Off();
224 USB_OTGPAD_Off();
225
226 USB_Host_VBUS_Manual_Enable();
227 USB_Host_VBUS_Manual_On();
228
229 USB_INT_Enable(USB_INT_SRPI);
230 USB_INT_Enable(USB_INT_BCERRI);
231 #else
232 if (USB_CurrentMode == USB_MODE_DEVICE)
233 {
234 USB_INT_Clear(USB_INT_SUSPEND);
235 USB_INT_Enable(USB_INT_SUSPEND);
236 USB_INT_Clear(USB_INT_EORSTI);
237 USB_INT_Enable(USB_INT_EORSTI);
238
239 #if defined(USB_SERIES_4_AVR) || defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR)
240 USB_INT_Enable(USB_INT_VBUS);
241 #endif
242
243 Endpoint_ConfigureEndpoint(ENDPOINT_CONTROLEP, EP_TYPE_CONTROL,
244 ENDPOINT_DIR_OUT, USB_ControlEndpointSize,
245 ENDPOINT_BANK_SINGLE);
246 }
247 else if (USB_CurrentMode == USB_MODE_HOST)
248 {
249 USB_Host_HostMode_On();
250
251 USB_Host_VBUS_Auto_Off();
252 USB_OTGPAD_Off();
253
254 USB_Host_VBUS_Manual_Enable();
255 USB_Host_VBUS_Manual_On();
256
257 USB_INT_Enable(USB_INT_SRPI);
258 USB_INT_Enable(USB_INT_BCERRI);
259 }
260 #endif
261 }