]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright 2012 Jun Wako <wakojun@gmail.com> | |
3 | * This file is based on: | |
4 | * LUFA-120219/Demos/Device/Lowlevel/KeyboardMouse | |
5 | * LUFA-120219/Demos/Device/Lowlevel/GenericHID | |
6 | */ | |
7 | ||
8 | /* | |
9 | LUFA Library | |
10 | Copyright (C) Dean Camera, 2012. | |
11 | ||
12 | dean [at] fourwalledcubicle [dot] com | |
13 | www.lufa-lib.org | |
14 | */ | |
15 | ||
16 | /* | |
17 | Copyright 2012 Dean Camera (dean [at] fourwalledcubicle [dot] com) | |
18 | Copyright 2010 Denver Gingerich (denver [at] ossguy [dot] com) | |
19 | ||
20 | Permission to use, copy, modify, distribute, and sell this | |
21 | software and its documentation for any purpose is hereby granted | |
22 | without fee, provided that the above copyright notice appear in | |
23 | all copies and that both that the copyright notice and this | |
24 | permission notice and warranty disclaimer appear in supporting | |
25 | documentation, and that the name of the author not be used in | |
26 | advertising or publicity pertaining to distribution of the | |
27 | software without specific, written prior permission. | |
28 | ||
29 | The author disclaim all warranties with regard to this | |
30 | software, including all implied warranties of merchantability | |
31 | and fitness. In no event shall the author be liable for any | |
32 | special, indirect or consequential damages or any damages | |
33 | whatsoever resulting from loss of use, data or profits, whether | |
34 | in an action of contract, negligence or other tortious action, | |
35 | arising out of or in connection with the use or performance of | |
36 | this software. | |
37 | */ | |
38 | ||
39 | #include "report.h" | |
40 | #include "host.h" | |
41 | #include "host_driver.h" | |
42 | #include "keyboard.h" | |
43 | #include "sendchar.h" | |
44 | #include "debug.h" | |
45 | ||
46 | #include "descriptor.h" | |
47 | #include "lufa.h" | |
48 | ||
49 | static uint8_t idle_duration = 0; | |
50 | static uint8_t protocol_report = 1; | |
51 | static uint8_t keyboard_led_stats = 0; | |
52 | ||
53 | static report_keyboard_t keyboard_report_sent; | |
54 | ||
55 | ||
56 | /* Host driver */ | |
57 | static uint8_t keyboard_leds(void); | |
58 | static void send_keyboard(report_keyboard_t *report); | |
59 | static void send_mouse(report_mouse_t *report); | |
60 | static void send_system(uint16_t data); | |
61 | static void send_consumer(uint16_t data); | |
62 | static host_driver_t lufa_driver = { | |
63 | keyboard_leds, | |
64 | send_keyboard, | |
65 | send_mouse, | |
66 | send_system, | |
67 | send_consumer | |
68 | }; | |
69 | ||
70 | ||
71 | static void SetupHardware(void); | |
72 | static void Console_Task(void); | |
73 | ||
74 | int main(void) | |
75 | { | |
76 | SetupHardware(); | |
77 | sei(); | |
78 | ||
79 | print_enable = true; | |
80 | debug_enable = true; | |
81 | debug_matrix = true; | |
82 | debug_keyboard = true; | |
83 | debug_mouse = true; | |
84 | ||
85 | // TODO: can't print here | |
86 | debug("LUFA init\n"); | |
87 | ||
88 | keyboard_init(); | |
89 | host_set_driver(&lufa_driver); | |
90 | while (1) { | |
91 | keyboard_proc(); | |
92 | ||
93 | #if !defined(INTERRUPT_CONTROL_ENDPOINT) | |
94 | USB_USBTask(); | |
95 | #endif | |
96 | } | |
97 | } | |
98 | ||
99 | void SetupHardware(void) | |
100 | { | |
101 | /* Disable watchdog if enabled by bootloader/fuses */ | |
102 | MCUSR &= ~(1 << WDRF); | |
103 | wdt_disable(); | |
104 | ||
105 | /* Disable clock division */ | |
106 | clock_prescale_set(clock_div_1); | |
107 | ||
108 | USB_Init(); | |
109 | ||
110 | // for Console_Task | |
111 | USB_Device_EnableSOFEvents(); | |
112 | } | |
113 | ||
114 | static void Console_Task(void) | |
115 | { | |
116 | /* Device must be connected and configured for the task to run */ | |
117 | if (USB_DeviceState != DEVICE_STATE_Configured) | |
118 | return; | |
119 | ||
120 | uint8_t ep = Endpoint_GetCurrentEndpoint(); | |
121 | ||
122 | #if 0 | |
123 | // TODO: impl receivechar()/recvchar() | |
124 | Endpoint_SelectEndpoint(CONSOLE_OUT_EPNUM); | |
125 | ||
126 | /* Check to see if a packet has been sent from the host */ | |
127 | if (Endpoint_IsOUTReceived()) | |
128 | { | |
129 | /* Check to see if the packet contains data */ | |
130 | if (Endpoint_IsReadWriteAllowed()) | |
131 | { | |
132 | /* Create a temporary buffer to hold the read in report from the host */ | |
133 | uint8_t ConsoleData[CONSOLE_EPSIZE]; | |
134 | ||
135 | /* Read Console Report Data */ | |
136 | Endpoint_Read_Stream_LE(&ConsoleData, sizeof(ConsoleData), NULL); | |
137 | ||
138 | /* Process Console Report Data */ | |
139 | //ProcessConsoleHIDReport(ConsoleData); | |
140 | } | |
141 | ||
142 | /* Finalize the stream transfer to send the last packet */ | |
143 | Endpoint_ClearOUT(); | |
144 | } | |
145 | #endif | |
146 | ||
147 | /* IN packet */ | |
148 | Endpoint_SelectEndpoint(CONSOLE_IN_EPNUM); | |
149 | // flash senchar packet | |
150 | if (Endpoint_IsINReady()) { | |
151 | Endpoint_ClearIN(); | |
152 | } | |
153 | ||
154 | Endpoint_SelectEndpoint(ep); | |
155 | } | |
156 | ||
157 | ||
158 | /******************************************************************************* | |
159 | * USB Events | |
160 | ******************************************************************************/ | |
161 | /** Event handler for the USB_Connect event. */ | |
162 | void EVENT_USB_Device_Connect(void) | |
163 | { | |
164 | } | |
165 | ||
166 | /** Event handler for the USB_Disconnect event. */ | |
167 | void EVENT_USB_Device_Disconnect(void) | |
168 | { | |
169 | } | |
170 | ||
171 | #define CONSOLE_TASK_INTERVAL 50 | |
172 | void EVENT_USB_Device_StartOfFrame(void) | |
173 | { | |
174 | static uint8_t interval; | |
175 | if (++interval == CONSOLE_TASK_INTERVAL) { | |
176 | Console_Task(); | |
177 | interval = 0; | |
178 | } | |
179 | } | |
180 | ||
181 | /** Event handler for the USB_ConfigurationChanged event. | |
182 | * This is fired when the host sets the current configuration of the USB device after enumeration. | |
183 | */ | |
184 | void EVENT_USB_Device_ConfigurationChanged(void) | |
185 | { | |
186 | bool ConfigSuccess = true; | |
187 | ||
188 | /* Setup Keyboard HID Report Endpoints */ | |
189 | ConfigSuccess &= Endpoint_ConfigureEndpoint(KEYBOARD_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN, | |
190 | KEYBOARD_EPSIZE, ENDPOINT_BANK_SINGLE); | |
191 | ||
192 | #ifdef MOUSE_ENABLE | |
193 | /* Setup Mouse HID Report Endpoint */ | |
194 | ConfigSuccess &= Endpoint_ConfigureEndpoint(MOUSE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN, | |
195 | MOUSE_EPSIZE, ENDPOINT_BANK_SINGLE); | |
196 | #endif | |
197 | ||
198 | #ifdef EXTRAKEY_ENABLE | |
199 | /* Setup Extra HID Report Endpoint */ | |
200 | ConfigSuccess &= Endpoint_ConfigureEndpoint(EXTRA_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN, | |
201 | EXTRA_EPSIZE, ENDPOINT_BANK_SINGLE); | |
202 | #endif | |
203 | ||
204 | /* Setup Console HID Report Endpoints */ | |
205 | ConfigSuccess &= Endpoint_ConfigureEndpoint(CONSOLE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN, | |
206 | CONSOLE_EPSIZE, ENDPOINT_BANK_DOUBLE); | |
207 | ConfigSuccess &= Endpoint_ConfigureEndpoint(CONSOLE_OUT_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_OUT, | |
208 | CONSOLE_EPSIZE, ENDPOINT_BANK_SINGLE); | |
209 | } | |
210 | ||
211 | /* | |
212 | Appendix G: HID Request Support Requirements | |
213 | ||
214 | The following table enumerates the requests that need to be supported by various types of HID class devices. | |
215 | ||
216 | Device type GetReport SetReport GetIdle SetIdle GetProtocol SetProtocol | |
217 | ------------------------------------------------------------------------------------------ | |
218 | Boot Mouse Required Optional Optional Optional Required Required | |
219 | Non-Boot Mouse Required Optional Optional Optional Optional Optional | |
220 | Boot Keyboard Required Optional Required Required Required Required | |
221 | Non-Boot Keybrd Required Optional Required Required Optional Optional | |
222 | Other Device Required Optional Optional Optional Optional Optional | |
223 | */ | |
224 | /** Event handler for the USB_ControlRequest event. | |
225 | * This is fired before passing along unhandled control requests to the library for processing internally. | |
226 | */ | |
227 | void EVENT_USB_Device_ControlRequest(void) | |
228 | { | |
229 | uint8_t* ReportData = NULL; | |
230 | uint8_t ReportSize = 0; | |
231 | ||
232 | /* Handle HID Class specific requests */ | |
233 | switch (USB_ControlRequest.bRequest) | |
234 | { | |
235 | case HID_REQ_GetReport: | |
236 | if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE)) | |
237 | { | |
238 | Endpoint_ClearSETUP(); | |
239 | ||
240 | // Interface | |
241 | switch (USB_ControlRequest.wIndex) { | |
242 | case KEYBOARD_INTERFACE: | |
243 | // TODO: test/check | |
244 | ReportData = (uint8_t*)&keyboard_report_sent; | |
245 | ReportSize = sizeof(keyboard_report_sent); | |
246 | break; | |
247 | } | |
248 | ||
249 | /* Write the report data to the control endpoint */ | |
250 | Endpoint_Write_Control_Stream_LE(ReportData, ReportSize); | |
251 | Endpoint_ClearOUT(); | |
252 | } | |
253 | ||
254 | break; | |
255 | case HID_REQ_SetReport: | |
256 | if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE)) | |
257 | { | |
258 | ||
259 | // Interface | |
260 | switch (USB_ControlRequest.wIndex) { | |
261 | case KEYBOARD_INTERFACE: | |
262 | Endpoint_ClearSETUP(); | |
263 | ||
264 | while (!(Endpoint_IsOUTReceived())) { | |
265 | if (USB_DeviceState == DEVICE_STATE_Unattached) | |
266 | return; | |
267 | } | |
268 | keyboard_led_stats = Endpoint_Read_8(); | |
269 | ||
270 | Endpoint_ClearOUT(); | |
271 | Endpoint_ClearStatusStage(); | |
272 | break; | |
273 | } | |
274 | ||
275 | } | |
276 | ||
277 | break; | |
278 | ||
279 | case HID_REQ_GetProtocol: | |
280 | if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE)) | |
281 | { | |
282 | Endpoint_ClearSETUP(); | |
283 | while (!(Endpoint_IsINReady())); | |
284 | Endpoint_Write_8(protocol_report); | |
285 | Endpoint_ClearIN(); | |
286 | Endpoint_ClearStatusStage(); | |
287 | } | |
288 | ||
289 | break; | |
290 | case HID_REQ_SetProtocol: | |
291 | if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE)) | |
292 | { | |
293 | Endpoint_ClearSETUP(); | |
294 | Endpoint_ClearStatusStage(); | |
295 | ||
296 | protocol_report = ((USB_ControlRequest.wValue & 0xFF) != 0x00); | |
297 | } | |
298 | ||
299 | break; | |
300 | case HID_REQ_SetIdle: | |
301 | if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE)) | |
302 | { | |
303 | Endpoint_ClearSETUP(); | |
304 | Endpoint_ClearStatusStage(); | |
305 | ||
306 | idle_duration = ((USB_ControlRequest.wValue & 0xFF00) >> 8); | |
307 | } | |
308 | ||
309 | break; | |
310 | case HID_REQ_GetIdle: | |
311 | if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE)) | |
312 | { | |
313 | Endpoint_ClearSETUP(); | |
314 | while (!(Endpoint_IsINReady())); | |
315 | Endpoint_Write_8(idle_duration); | |
316 | Endpoint_ClearIN(); | |
317 | Endpoint_ClearStatusStage(); | |
318 | } | |
319 | ||
320 | break; | |
321 | } | |
322 | } | |
323 | ||
324 | /******************************************************************************* | |
325 | * Host driver | |
326 | ******************************************************************************/ | |
327 | static uint8_t keyboard_leds(void) | |
328 | { | |
329 | return keyboard_led_stats; | |
330 | } | |
331 | ||
332 | static void send_keyboard(report_keyboard_t *report) | |
333 | { | |
334 | // TODO: handle NKRO report | |
335 | /* Select the Keyboard Report Endpoint */ | |
336 | Endpoint_SelectEndpoint(KEYBOARD_IN_EPNUM); | |
337 | ||
338 | /* Check if Keyboard Endpoint Ready for Read/Write */ | |
339 | if (Endpoint_IsReadWriteAllowed()) | |
340 | { | |
341 | /* Write Keyboard Report Data */ | |
342 | Endpoint_Write_Stream_LE(report, sizeof(report_keyboard_t), NULL); | |
343 | ||
344 | /* Finalize the stream transfer to send the last packet */ | |
345 | Endpoint_ClearIN(); | |
346 | } | |
347 | keyboard_report_sent = *report; | |
348 | } | |
349 | ||
350 | static void send_mouse(report_mouse_t *report) | |
351 | { | |
352 | #ifdef MOUSE_ENABLE | |
353 | /* Select the Mouse Report Endpoint */ | |
354 | Endpoint_SelectEndpoint(MOUSE_IN_EPNUM); | |
355 | ||
356 | /* Check if Mouse Endpoint Ready for Read/Write */ | |
357 | if (Endpoint_IsReadWriteAllowed()) | |
358 | { | |
359 | /* Write Mouse Report Data */ | |
360 | Endpoint_Write_Stream_LE(report, sizeof(report_mouse_t), NULL); | |
361 | ||
362 | /* Finalize the stream transfer to send the last packet */ | |
363 | Endpoint_ClearIN(); | |
364 | } | |
365 | #endif | |
366 | } | |
367 | ||
368 | static void send_system(uint16_t data) | |
369 | { | |
370 | report_extra_t r = { | |
371 | .report_id = REPORT_ID_SYSTEM, | |
372 | .usage = data | |
373 | }; | |
374 | Endpoint_SelectEndpoint(EXTRA_IN_EPNUM); | |
375 | if (Endpoint_IsReadWriteAllowed()) { | |
376 | Endpoint_Write_Stream_LE(&r, sizeof(report_extra_t), NULL); | |
377 | Endpoint_ClearIN(); | |
378 | } | |
379 | } | |
380 | ||
381 | static void send_consumer(uint16_t data) | |
382 | { | |
383 | report_extra_t r = { | |
384 | .report_id = REPORT_ID_CONSUMER, | |
385 | .usage = data | |
386 | }; | |
387 | Endpoint_SelectEndpoint(EXTRA_IN_EPNUM); | |
388 | if (Endpoint_IsReadWriteAllowed()) { | |
389 | Endpoint_Write_Stream_LE(&r, sizeof(report_extra_t), NULL); | |
390 | Endpoint_ClearIN(); | |
391 | } | |
392 | } | |
393 | ||
394 | ||
395 | /******************************************************************************* | |
396 | * sendchar | |
397 | ******************************************************************************/ | |
398 | #define SEND_TIMEOUT 10 | |
399 | int8_t sendchar(uint8_t c) | |
400 | { | |
401 | if (USB_DeviceState != DEVICE_STATE_Configured) | |
402 | return -1; | |
403 | ||
404 | Endpoint_SelectEndpoint(CONSOLE_IN_EPNUM); | |
405 | ||
406 | uint8_t timeout = SEND_TIMEOUT; | |
407 | uint16_t prevFN = USB_Device_GetFrameNumber(); | |
408 | while (!Endpoint_IsReadWriteAllowed()) { | |
409 | switch (USB_DeviceState) { | |
410 | case DEVICE_STATE_Unattached: | |
411 | case DEVICE_STATE_Suspended: | |
412 | return -1; | |
413 | } | |
414 | if (Endpoint_IsStalled()) | |
415 | return -1; | |
416 | if (prevFN != USB_Device_GetFrameNumber()) { | |
417 | if (!(timeout--)) | |
418 | return -1; | |
419 | prevFN = USB_Device_GetFrameNumber(); | |
420 | } | |
421 | } | |
422 | ||
423 | Endpoint_Write_8(c); | |
424 | ||
425 | // send when bank is full | |
426 | if (!Endpoint_IsReadWriteAllowed()) | |
427 | Endpoint_ClearIN(); | |
428 | ||
429 | return 0; | |
430 | } |