2 #include <avr/interrupt.h>
6 #include "usb_keycodes.h"
12 static report_keyboard_t report0
;
13 static report_keyboard_t report1
;
14 report_keyboard_t
*keyboard_report
= &report0
;
15 report_keyboard_t
*keyboard_report_prev
= &report1
;
17 static uint8_t keyboard_leds
= 0;
18 static uchar idleRate
= 0;
20 uint8_t host_keyboard_leds(void)
26 /*------------------------------------------------------------------*
27 * Keyboard report operations
28 *------------------------------------------------------------------*/
29 void host_add_key(uint8_t code
)
33 for (; i
< REPORT_KEYS
; i
++) {
34 if (keyboard_report_prev
->keys
[i
] == code
) {
35 keyboard_report
->keys
[i
] = code
;
38 if (empty
== -1 && keyboard_report_prev
->keys
[i
] == KB_NO
&& keyboard_report
->keys
[i
] == KB_NO
) {
42 if (i
== REPORT_KEYS
&& empty
!= -1) {
43 keyboard_report
->keys
[empty
] = code
;
47 void host_add_mod_bit(uint8_t mod
)
49 keyboard_report
->mods
|= mod
;
52 void host_set_mods(uint8_t mods
)
54 keyboard_report
->mods
= mods
;
57 void host_add_code(uint8_t code
)
60 host_add_mod_bit(MOD_BIT(code
));
66 void host_swap_keyboard_report(void)
70 report_keyboard_t
*tmp
= keyboard_report_prev
;
71 keyboard_report_prev
= keyboard_report
;
72 keyboard_report
= tmp
;
76 void host_clear_keyboard_report(void)
78 keyboard_report
->mods
= 0;
79 for (int8_t i
= 0; i
< REPORT_KEYS
; i
++) {
80 keyboard_report
->keys
[i
] = 0;
84 uint8_t host_has_anykey(void)
87 for (int i
= 0; i
< REPORT_KEYS
; i
++) {
88 if (keyboard_report
->keys
[i
])
94 uint8_t host_get_first_key(void)
96 #ifdef USB_NKRO_ENABLE
99 for (; i
< REPORT_KEYS
&& !keyboard_report
->keys
[i
]; i
++)
101 return i
<<3 | biton(keyboard_report
->keys
[i
]);
104 return keyboard_report
->keys
[0];
108 /*------------------------------------------------------------------*
109 * Keyboard report send buffer
110 *------------------------------------------------------------------*/
112 static report_keyboard_t kbuf
[KBUF_SIZE
];
113 static uint8_t kbuf_head
= 0;
114 static uint8_t kbuf_tail
= 0;
116 void host_vusb_keyboard_send(void)
118 while (usbInterruptIsReady() && kbuf_head
!= kbuf_tail
) {
119 usbSetInterrupt((void *)&kbuf
[kbuf_tail
], sizeof(report_keyboard_t
));
120 kbuf_tail
= (kbuf_tail
+ 1) % KBUF_SIZE
;
124 void host_send_keyboard_report(void)
126 uint8_t next
= (kbuf_head
+ 1) % KBUF_SIZE
;
127 if (next
!= kbuf_tail
) {
128 kbuf
[kbuf_head
] = *keyboard_report
;
131 debug("kbuf: full\n");
136 void host_mouse_send(report_mouse_t
*report
)
138 if (usbInterruptIsReady3()) {
139 usbSetInterrupt3((void *)report
, sizeof(*report
));
141 debug("Int3 not ready\n");
147 /*------------------------------------------------------------------*
148 * Request from host *
149 *------------------------------------------------------------------*/
158 usbMsgLen_t
usbFunctionSetup(uchar data
[8])
160 usbRequest_t
*rq
= (void *)data
;
162 if((rq
->bmRequestType
& USBRQ_TYPE_MASK
) == USBRQ_TYPE_CLASS
){ /* class request type */
163 if(rq
->bRequest
== USBRQ_HID_GET_REPORT
){
164 debug(" GET_REPORT");
165 /* we only have one report type, so don't look at wValue */
166 usbMsgPtr
= (void *)keyboard_report_prev
;
167 return sizeof(*keyboard_report_prev
);
168 }else if(rq
->bRequest
== USBRQ_HID_GET_IDLE
){
169 debug(" GET_IDLE: ");
171 usbMsgPtr
= &idleRate
;
173 }else if(rq
->bRequest
== USBRQ_HID_SET_IDLE
){
174 idleRate
= rq
->wValue
.bytes
[1];
175 debug(" SET_IDLE: ");
177 }else if(rq
->bRequest
== USBRQ_HID_SET_REPORT
){
178 //debug(" SET_REPORT: ");
179 if (rq
->wValue
.word
== 0x0200 && rq
->wIndex
.word
== 0) {
180 last_req
.kind
= SET_LED
;
181 last_req
.len
= rq
->wLength
.word
;
183 return USB_NO_MSG
; // to get data in usbFunctionWrite
188 /* no vendor specific requests implemented */
190 return 0; /* default for not implemented requests: return no data back to host */
193 uchar
usbFunctionWrite(uchar
*data
, uchar len
)
195 if (last_req
.len
== 0) {
198 switch (last_req
.kind
) {
200 //debug("SET_LED\n");
201 keyboard_leds
= data
[0];
215 /*------------------------------------------------------------------*
217 *------------------------------------------------------------------*/
220 * Report Descriptor for keyboard
222 * from an example in HID spec appendix
224 PROGMEM uchar keyboard_hid_report
[] = {
225 0x05, 0x01, // Usage Page (Generic Desktop),
226 0x09, 0x06, // Usage (Keyboard),
227 0xA1, 0x01, // Collection (Application),
228 0x75, 0x01, // Report Size (1),
229 0x95, 0x08, // Report Count (8),
230 0x05, 0x07, // Usage Page (Key Codes),
231 0x19, 0xE0, // Usage Minimum (224),
232 0x29, 0xE7, // Usage Maximum (231),
233 0x15, 0x00, // Logical Minimum (0),
234 0x25, 0x01, // Logical Maximum (1),
235 0x81, 0x02, // Input (Data, Variable, Absolute), ;Modifier byte
236 0x95, 0x01, // Report Count (1),
237 0x75, 0x08, // Report Size (8),
238 0x81, 0x03, // Input (Constant), ;Reserved byte
239 0x95, 0x05, // Report Count (5),
240 0x75, 0x01, // Report Size (1),
241 0x05, 0x08, // Usage Page (LEDs),
242 0x19, 0x01, // Usage Minimum (1),
243 0x29, 0x05, // Usage Maximum (5),
244 0x91, 0x02, // Output (Data, Variable, Absolute), ;LED report
245 0x95, 0x01, // Report Count (1),
246 0x75, 0x03, // Report Size (3),
247 0x91, 0x03, // Output (Constant), ;LED report padding
248 0x95, 0x06, // Report Count (6),
249 0x75, 0x08, // Report Size (8),
250 0x15, 0x00, // Logical Minimum (0),
251 0x25, 0xFF, // Logical Maximum(255),
252 0x05, 0x07, // Usage Page (Key Codes),
253 0x19, 0x00, // Usage Minimum (0),
254 0x29, 0xFF, // Usage Maximum (255),
255 0x81, 0x00, // Input (Data, Array),
256 0xc0 // End Collection
260 * Report Descriptor for mouse
262 * Mouse Protocol 1, HID 1.11 spec, Appendix B, page 59-60, with wheel extension
263 * http://www.microchip.com/forums/tm.aspx?high=&m=391435&mpage=1#391521
264 * http://www.keil.com/forum/15671/
265 * http://www.microsoft.com/whdc/device/input/wheel.mspx
267 PROGMEM uchar mouse_hid_report
[] = {
268 /* from HID 1.11 spec example */
269 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
270 0x09, 0x02, // USAGE (Mouse)
271 0xa1, 0x01, // COLLECTION (Application)
272 0x09, 0x02, // USAGE (Mouse)
273 0xa1, 0x02, // COLLECTION (Logical)
274 0x09, 0x01, // USAGE (Pointer)
275 0xa1, 0x00, // COLLECTION (Physical)
276 // ------------------------------ Buttons
277 0x05, 0x09, // USAGE_PAGE (Button)
278 0x19, 0x01, // USAGE_MINIMUM (Button 1)
279 0x29, 0x05, // USAGE_MAXIMUM (Button 5)
280 0x15, 0x00, // LOGICAL_MINIMUM (0)
281 0x25, 0x01, // LOGICAL_MAXIMUM (1)
282 0x75, 0x01, // REPORT_SIZE (1)
283 0x95, 0x05, // REPORT_COUNT (5)
284 0x81, 0x02, // INPUT (Data,Var,Abs)
285 // ------------------------------ Padding
286 0x75, 0x03, // REPORT_SIZE (3)
287 0x95, 0x01, // REPORT_COUNT (1)
288 0x81, 0x03, // INPUT (Cnst,Var,Abs)
289 // ------------------------------ X,Y position
290 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
291 0x09, 0x30, // USAGE (X)
292 0x09, 0x31, // USAGE (Y)
293 0x15, 0x81, // LOGICAL_MINIMUM (-127)
294 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
295 0x75, 0x08, // REPORT_SIZE (8)
296 0x95, 0x02, // REPORT_COUNT (2)
297 0x81, 0x06, // INPUT (Data,Var,Rel)
298 0xa1, 0x02, // COLLECTION (Logical)
299 // ------------------------------ Vertical wheel res multiplier
300 0x09, 0x48, // USAGE (Resolution Multiplier)
301 0x15, 0x00, // LOGICAL_MINIMUM (0)
302 0x25, 0x01, // LOGICAL_MAXIMUM (1)
303 0x35, 0x01, // PHYSICAL_MINIMUM (1)
304 0x45, 0x04, // PHYSICAL_MAXIMUM (4)
305 0x75, 0x02, // REPORT_SIZE (2)
306 0x95, 0x01, // REPORT_COUNT (1)
308 0xb1, 0x02, // FEATURE (Data,Var,Abs)
309 // ------------------------------ Vertical wheel
310 0x09, 0x38, // USAGE (Wheel)
311 0x15, 0x81, // LOGICAL_MINIMUM (-127)
312 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
313 0x35, 0x00, // PHYSICAL_MINIMUM (0) - reset physical
314 0x45, 0x00, // PHYSICAL_MAXIMUM (0)
315 0x75, 0x08, // REPORT_SIZE (8)
316 0x81, 0x06, // INPUT (Data,Var,Rel)
317 0xc0, // END_COLLECTION
318 0xa1, 0x02, // COLLECTION (Logical)
319 // ------------------------------ Horizontal wheel res multiplier
320 0x09, 0x48, // USAGE (Resolution Multiplier)
322 0xb1, 0x02, // FEATURE (Data,Var,Abs)
323 // ------------------------------ Padding for Feature report
324 0x35, 0x00, // PHYSICAL_MINIMUM (0) - reset physical
325 0x45, 0x00, // PHYSICAL_MAXIMUM (0)
326 0x75, 0x04, // REPORT_SIZE (4)
327 0xb1, 0x03, // FEATURE (Cnst,Var,Abs)
328 // ------------------------------ Horizontal wheel
329 0x05, 0x0c, // USAGE_PAGE (Consumer Devices)
330 0x0a, 0x38, 0x02, // USAGE (AC Pan)
331 0x15, 0x81, // LOGICAL_MINIMUM (-127)
332 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
333 0x75, 0x08, // REPORT_SIZE (8)
334 0x81, 0x06, // INPUT (Data,Var,Rel)
335 0xc0, // END_COLLECTION
336 0xc0, // END_COLLECTION
337 0xc0, // END_COLLECTION
338 0xc0 // END_COLLECTION
343 * Descriptor for compite device: Keyboard + Mouse
345 * contains: device, interface, HID and endpoint descriptors
347 #if USB_CFG_DESCR_PROPS_CONFIGURATION
348 PROGMEM
char usbDescriptorConfiguration
[] = { /* USB configuration descriptor */
349 9, /* sizeof(usbDescriptorConfiguration): length of descriptor in bytes */
350 USBDESCR_CONFIG
, /* descriptor type */
351 9 + (9 + 9 + 7) + (9 + 9 + 7), 0,
352 //18 + 7 * USB_CFG_HAVE_INTRIN_ENDPOINT + 7 * USB_CFG_HAVE_INTRIN_ENDPOINT3 + 9, 0,
353 /* total length of data returned (including inlined descriptors) */
354 2, /* number of interfaces in this configuration */
355 1, /* index of this configuration */
356 0, /* configuration name string index */
357 #if USB_CFG_IS_SELF_POWERED
358 (1 << 7) | USBATTR_SELFPOWER
, /* attributes */
360 (1 << 7), /* attributes */
362 USB_CFG_MAX_BUS_POWER
/2, /* max USB current in 2mA units */
367 /* Interface descriptor */
368 9, /* sizeof(usbDescrInterface): length of descriptor in bytes */
369 USBDESCR_INTERFACE
, /* descriptor type */
370 0, /* index of this interface */
371 0, /* alternate setting for this interface */
372 USB_CFG_HAVE_INTRIN_ENDPOINT
, /* endpoints excl 0: number of endpoint descriptors to follow */
373 USB_CFG_INTERFACE_CLASS
,
374 USB_CFG_INTERFACE_SUBCLASS
,
375 USB_CFG_INTERFACE_PROTOCOL
,
376 0, /* string index for interface */
378 9, /* sizeof(usbDescrHID): length of descriptor in bytes */
379 USBDESCR_HID
, /* descriptor type: HID */
380 0x01, 0x01, /* BCD representation of HID version */
381 0x00, /* target country code */
382 0x01, /* number of HID Report (or other HID class) Descriptor infos to follow */
383 0x22, /* descriptor type: report */
384 sizeof(keyboard_hid_report
), 0, /* total length of report descriptor */
385 /* Endpoint descriptor */
386 #if USB_CFG_HAVE_INTRIN_ENDPOINT /* endpoint descriptor for endpoint 1 */
387 7, /* sizeof(usbDescrEndpoint) */
388 USBDESCR_ENDPOINT
, /* descriptor type = endpoint */
389 (char)0x81, /* IN endpoint number 1 */
390 0x03, /* attrib: Interrupt endpoint */
391 8, 0, /* maximum packet size */
392 USB_CFG_INTR_POLL_INTERVAL
, /* in ms */
398 /* Interface descriptor */
399 9, /* sizeof(usbDescrInterface): length of descriptor in bytes */
400 USBDESCR_INTERFACE
, /* descriptor type */
401 1, /* index of this interface */
402 0, /* alternate setting for this interface */
403 USB_CFG_HAVE_INTRIN_ENDPOINT3
, /* endpoints excl 0: number of endpoint descriptors to follow */
404 0x03, /* CLASS: HID */
405 0, /* SUBCLASS: none */
406 0, /* PROTOCOL: none */
407 0, /* string index for interface */
409 9, /* sizeof(usbDescrHID): length of descriptor in bytes */
410 USBDESCR_HID
, /* descriptor type: HID */
411 0x01, 0x01, /* BCD representation of HID version */
412 0x00, /* target country code */
413 0x01, /* number of HID Report (or other HID class) Descriptor infos to follow */
414 0x22, /* descriptor type: report */
415 sizeof(mouse_hid_report
), 0, /* total length of report descriptor */
416 #if USB_CFG_HAVE_INTRIN_ENDPOINT3 /* endpoint descriptor for endpoint 3 */
417 /* Endpoint descriptor */
418 7, /* sizeof(usbDescrEndpoint) */
419 USBDESCR_ENDPOINT
, /* descriptor type = endpoint */
420 (char)(0x80 | USB_CFG_EP3_NUMBER
), /* IN endpoint number 3 */
421 0x03, /* attrib: Interrupt endpoint */
422 8, 0, /* maximum packet size */
423 USB_CFG_INTR_POLL_INTERVAL
, /* in ms */
429 USB_PUBLIC usbMsgLen_t
usbFunctionDescriptor(struct usbRequest
*rq
)
433 debug("usbFunctionDescriptor: ");
434 debug_hex(rq
->bmRequestType
); debug(" ");
435 debug_hex(rq
->bRequest
); debug(" ");
436 debug_hex16(rq
->wValue
.word
); debug(" ");
437 debug_hex16(rq
->wIndex
.word
); debug(" ");
438 debug_hex16(rq
->wLength
.word
); debug("\n");
440 switch (rq
->wValue
.bytes
[1]) {
441 #if USB_CFG_DESCR_PROPS_CONFIGURATION
442 case USBDESCR_CONFIG
:
443 usbMsgPtr
= (unsigned char *)usbDescriptorConfiguration
;
444 len
= sizeof(usbDescriptorConfiguration
);
448 usbMsgPtr
= (unsigned char *)(usbDescriptorConfiguration
+ 18);
451 case USBDESCR_HID_REPORT
:
452 /* interface index */
453 switch (rq
->wIndex
.word
) {
455 usbMsgPtr
= keyboard_hid_report
;
456 len
= sizeof(keyboard_hid_report
);
459 usbMsgPtr
= mouse_hid_report
;
460 len
= sizeof(mouse_hid_report
);
465 debug("desc len: "); debug_hex(len
); debug("\n");