4 #include "usb_keycodes.h"
10 static report_keyboard_t report0
;
11 static report_keyboard_t report1
;
12 report_keyboard_t
*keyboard_report
= &report0
;
13 report_keyboard_t
*keyboard_report_prev
= &report1
;
15 static uint8_t keyboard_led
= 0;
16 static uchar idleRate
= 0;
18 uint8_t host_keyboard_led(void)
24 /*------------------------------------------------------------------*
25 * Keyboard report operations
26 *------------------------------------------------------------------*/
27 void host_add_key(uint8_t code
)
31 for (; i
< REPORT_KEYS
; i
++) {
32 if (keyboard_report_prev
->keys
[i
] == code
) {
33 keyboard_report
->keys
[i
] = code
;
36 if (empty
== -1 && keyboard_report_prev
->keys
[i
] == KB_NO
&& keyboard_report
->keys
[i
] == KB_NO
) {
40 if (i
== REPORT_KEYS
&& empty
!= -1) {
41 keyboard_report
->keys
[empty
] = code
;
45 void host_add_mod_bit(uint8_t mod
)
47 keyboard_report
->mods
|= mod
;
50 void host_set_mods(uint8_t mods
)
52 keyboard_report
->mods
= mods
;
55 void host_add_code(uint8_t code
)
58 host_add_mod_bit(MOD_BIT(code
));
64 void host_swap_keyboard_report(void)
66 report_keyboard_t
*tmp
= keyboard_report_prev
;
67 keyboard_report_prev
= keyboard_report
;
68 keyboard_report
= tmp
;
71 void host_clear_keyboard_report(void)
73 keyboard_report
->mods
= 0;
74 for (int8_t i
= 0; i
< REPORT_KEYS
; i
++) {
75 keyboard_report
->keys
[i
] = 0;
79 uint8_t host_has_anykey(void)
82 for (int i
= 0; i
< REPORT_KEYS
; i
++) {
83 if (keyboard_report
->keys
[i
])
89 uint8_t *host_get_keys(void)
91 return keyboard_report
->keys
;
94 uint8_t host_get_mods(void)
96 return keyboard_report
->mods
;
100 /*------------------------------------------------------------------*
101 * Keyboard report send buffer
102 *------------------------------------------------------------------*/
104 static report_keyboard_t kbuf
[KBUF_SIZE
];
105 static uint8_t kbuf_head
= 0;
106 static uint8_t kbuf_tail
= 0;
108 void host_vusb_keyboard_send(void)
110 while (usbInterruptIsReady() && kbuf_head
!= kbuf_tail
) {
111 usbSetInterrupt((void *)&kbuf
[kbuf_tail
], sizeof(report_keyboard_t
));
112 kbuf_tail
= (kbuf_tail
+ 1) % KBUF_SIZE
;
116 void host_send_keyboard_report(void)
118 uint8_t next
= (kbuf_head
+ 1) % KBUF_SIZE
;
119 if (next
!= kbuf_tail
) {
120 kbuf
[kbuf_head
] = *keyboard_report
;
123 debug("kbuf: full\n");
128 void host_mouse_send(report_mouse_t
*report
)
130 if (usbInterruptIsReady3()) {
131 usbSetInterrupt3((void *)report
, sizeof(*report
));
133 debug("Int3 not ready\n");
139 /*------------------------------------------------------------------*
140 * Request from host *
141 *------------------------------------------------------------------*/
150 usbMsgLen_t
usbFunctionSetup(uchar data
[8])
152 usbRequest_t
*rq
= (void *)data
;
154 if((rq
->bmRequestType
& USBRQ_TYPE_MASK
) == USBRQ_TYPE_CLASS
){ /* class request type */
155 if(rq
->bRequest
== USBRQ_HID_GET_REPORT
){
156 debug(" GET_REPORT");
157 /* we only have one report type, so don't look at wValue */
158 usbMsgPtr
= (void *)keyboard_report
;
159 return sizeof(*keyboard_report
);
160 }else if(rq
->bRequest
== USBRQ_HID_GET_IDLE
){
161 debug(" GET_IDLE: ");
163 usbMsgPtr
= &idleRate
;
165 }else if(rq
->bRequest
== USBRQ_HID_SET_IDLE
){
166 idleRate
= rq
->wValue
.bytes
[1];
167 debug(" SET_IDLE: ");
169 }else if(rq
->bRequest
== USBRQ_HID_SET_REPORT
){
170 //debug(" SET_REPORT: ");
171 if (rq
->wValue
.word
== 0x0200 && rq
->wIndex
.word
== 0) {
172 last_req
.kind
= SET_LED
;
173 last_req
.len
= rq
->wLength
.word
;
175 return USB_NO_MSG
; // to get data in usbFunctionWrite
180 /* no vendor specific requests implemented */
182 return 0; /* default for not implemented requests: return no data back to host */
185 uchar
usbFunctionWrite(uchar
*data
, uchar len
)
187 if (last_req
.len
== 0) {
190 switch (last_req
.kind
) {
192 //debug("SET_LED\n");
193 keyboard_led
= data
[0];
207 /*------------------------------------------------------------------*
209 *------------------------------------------------------------------*/
212 * Report Descriptor for keyboard
214 * from an example in HID spec appendix
216 PROGMEM uchar keyboard_hid_report
[] = {
217 0x05, 0x01, // Usage Page (Generic Desktop),
218 0x09, 0x06, // Usage (Keyboard),
219 0xA1, 0x01, // Collection (Application),
220 0x75, 0x01, // Report Size (1),
221 0x95, 0x08, // Report Count (8),
222 0x05, 0x07, // Usage Page (Key Codes),
223 0x19, 0xE0, // Usage Minimum (224),
224 0x29, 0xE7, // Usage Maximum (231),
225 0x15, 0x00, // Logical Minimum (0),
226 0x25, 0x01, // Logical Maximum (1),
227 0x81, 0x02, // Input (Data, Variable, Absolute), ;Modifier byte
228 0x95, 0x01, // Report Count (1),
229 0x75, 0x08, // Report Size (8),
230 0x81, 0x03, // Input (Constant), ;Reserved byte
231 0x95, 0x05, // Report Count (5),
232 0x75, 0x01, // Report Size (1),
233 0x05, 0x08, // Usage Page (LEDs),
234 0x19, 0x01, // Usage Minimum (1),
235 0x29, 0x05, // Usage Maximum (5),
236 0x91, 0x02, // Output (Data, Variable, Absolute), ;LED report
237 0x95, 0x01, // Report Count (1),
238 0x75, 0x03, // Report Size (3),
239 0x91, 0x03, // Output (Constant), ;LED report padding
240 0x95, 0x06, // Report Count (6),
241 0x75, 0x08, // Report Size (8),
242 0x15, 0x00, // Logical Minimum (0),
243 0x25, 0xFF, // Logical Maximum(255),
244 0x05, 0x07, // Usage Page (Key Codes),
245 0x19, 0x00, // Usage Minimum (0),
246 0x29, 0xFF, // Usage Maximum (255),
247 0x81, 0x00, // Input (Data, Array),
248 0xc0 // End Collection
252 * Report Descriptor for mouse
254 * Mouse Protocol 1, HID 1.11 spec, Appendix B, page 59-60, with wheel extension
255 * http://www.microchip.com/forums/tm.aspx?high=&m=391435&mpage=1#391521
256 * http://www.keil.com/forum/15671/
257 * http://www.microsoft.com/whdc/device/input/wheel.mspx
259 PROGMEM uchar mouse_hid_report
[] = {
260 /* from HID 1.11 spec example */
261 0x05, 0x01, // Usage Page (Generic Desktop),
262 0x09, 0x02, // Usage (Mouse),
263 0xA1, 0x01, // Collection (Application),
264 0x09, 0x01, // Usage (Pointer),
265 0xA1, 0x00, // Collection (Physical),
266 0x05, 0x09, // Usage Page (Buttons),
267 0x19, 0x01, // Usage Minimum (01),
268 0x29, 0x03, // Usage Maximun (03),
269 0x15, 0x00, // Logical Minimum (0),
270 0x25, 0x01, // Logical Maximum (1),
271 0x95, 0x03, // Report Count (3),
272 0x75, 0x01, // Report Size (1),
273 0x81, 0x02, // Input (Data, Variable, Absolute), ;3 button bits
274 0x95, 0x01, // Report Count (1),
275 0x75, 0x05, // Report Size (5),
276 0x81, 0x01, // Input (Constant), ;5 bit padding
277 0x05, 0x01, // Usage Page (Generic Desktop),
278 0x09, 0x30, // Usage (X),
279 0x09, 0x31, // Usage (Y),
280 0x15, 0x81, // Logical Minimum (-127),
281 0x25, 0x7F, // Logical Maximum (127),
282 0x75, 0x08, // Report Size (8),
283 0x95, 0x02, // Report Count (2),
284 0x81, 0x06, // Input (Data, Variable, Relative), ;2 position bytes (X & Y)
285 0xC0, // End Collection,
286 0xC0, // End Collection
288 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
289 0x09, 0x02, // USAGE (Mouse)
290 0xa1, 0x01, // COLLECTION (Application)
291 0x09, 0x02, // USAGE (Mouse)
292 0xa1, 0x02, // COLLECTION (Logical)
293 0x09, 0x01, // USAGE (Pointer)
294 0xa1, 0x00, // COLLECTION (Physical)
295 // ------------------------------ Buttons
296 0x05, 0x09, // USAGE_PAGE (Button)
297 0x19, 0x01, // USAGE_MINIMUM (Button 1)
298 0x29, 0x05, // USAGE_MAXIMUM (Button 5)
299 0x15, 0x00, // LOGICAL_MINIMUM (0)
300 0x25, 0x01, // LOGICAL_MAXIMUM (1)
301 0x75, 0x01, // REPORT_SIZE (1)
302 0x95, 0x05, // REPORT_COUNT (5)
303 0x81, 0x02, // INPUT (Data,Var,Abs)
304 // ------------------------------ Padding
305 0x75, 0x03, // REPORT_SIZE (3)
306 0x95, 0x01, // REPORT_COUNT (1)
307 0x81, 0x03, // INPUT (Cnst,Var,Abs)
308 // ------------------------------ X,Y position
309 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
310 0x09, 0x30, // USAGE (X)
311 0x09, 0x31, // USAGE (Y)
312 0x15, 0x81, // LOGICAL_MINIMUM (-127)
313 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
314 0x75, 0x08, // REPORT_SIZE (8)
315 0x95, 0x02, // REPORT_COUNT (2)
316 0x81, 0x06, // INPUT (Data,Var,Rel)
317 0xa1, 0x02, // COLLECTION (Logical)
318 // ------------------------------ Vertical wheel res multiplier
319 0x09, 0x48, // USAGE (Resolution Multiplier)
320 0x15, 0x00, // LOGICAL_MINIMUM (0)
321 0x25, 0x01, // LOGICAL_MAXIMUM (1)
322 0x35, 0x01, // PHYSICAL_MINIMUM (1)
323 0x45, 0x04, // PHYSICAL_MAXIMUM (4)
324 0x75, 0x02, // REPORT_SIZE (2)
325 0x95, 0x01, // REPORT_COUNT (1)
327 0xb1, 0x02, // FEATURE (Data,Var,Abs)
328 // ------------------------------ Vertical wheel
329 0x09, 0x38, // USAGE (Wheel)
330 0x15, 0x81, // LOGICAL_MINIMUM (-127)
331 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
332 0x35, 0x00, // PHYSICAL_MINIMUM (0) - reset physical
333 0x45, 0x00, // PHYSICAL_MAXIMUM (0)
334 0x75, 0x08, // REPORT_SIZE (8)
335 0x81, 0x06, // INPUT (Data,Var,Rel)
336 0xc0, // END_COLLECTION
337 0xa1, 0x02, // COLLECTION (Logical)
338 // ------------------------------ Horizontal wheel res multiplier
339 0x09, 0x48, // USAGE (Resolution Multiplier)
341 0xb1, 0x02, // FEATURE (Data,Var,Abs)
342 // ------------------------------ Padding for Feature report
343 0x35, 0x00, // PHYSICAL_MINIMUM (0) - reset physical
344 0x45, 0x00, // PHYSICAL_MAXIMUM (0)
345 0x75, 0x04, // REPORT_SIZE (4)
346 0xb1, 0x03, // FEATURE (Cnst,Var,Abs)
347 // ------------------------------ Horizontal wheel
348 0x05, 0x0c, // USAGE_PAGE (Consumer Devices)
349 0x0a, 0x38, 0x02, // USAGE (AC Pan)
350 0x15, 0x81, // LOGICAL_MINIMUM (-127)
351 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
352 0x75, 0x08, // REPORT_SIZE (8)
353 0x81, 0x06, // INPUT (Data,Var,Rel)
354 0xc0, // END_COLLECTION
355 0xc0, // END_COLLECTION
356 0xc0, // END_COLLECTION
357 0xc0 // END_COLLECTION
363 * Descriptor for compite device: Keyboard + Mouse
365 * contains: device, interface, HID and endpoint descriptors
367 #if USB_CFG_DESCR_PROPS_CONFIGURATION
368 PROGMEM
char usbDescriptorConfiguration
[] = { /* USB configuration descriptor */
369 9, /* sizeof(usbDescriptorConfiguration): length of descriptor in bytes */
370 USBDESCR_CONFIG
, /* descriptor type */
371 9 + (9 + 9 + 7) + (9 + 9 + 7), 0,
372 //18 + 7 * USB_CFG_HAVE_INTRIN_ENDPOINT + 7 * USB_CFG_HAVE_INTRIN_ENDPOINT3 + 9, 0,
373 /* total length of data returned (including inlined descriptors) */
374 2, /* number of interfaces in this configuration */
375 1, /* index of this configuration */
376 0, /* configuration name string index */
377 #if USB_CFG_IS_SELF_POWERED
378 (1 << 7) | USBATTR_SELFPOWER
, /* attributes */
380 (1 << 7), /* attributes */
382 USB_CFG_MAX_BUS_POWER
/2, /* max USB current in 2mA units */
387 /* Interface descriptor */
388 9, /* sizeof(usbDescrInterface): length of descriptor in bytes */
389 USBDESCR_INTERFACE
, /* descriptor type */
390 0, /* index of this interface */
391 0, /* alternate setting for this interface */
392 USB_CFG_HAVE_INTRIN_ENDPOINT
, /* endpoints excl 0: number of endpoint descriptors to follow */
393 USB_CFG_INTERFACE_CLASS
,
394 USB_CFG_INTERFACE_SUBCLASS
,
395 USB_CFG_INTERFACE_PROTOCOL
,
396 0, /* string index for interface */
398 9, /* sizeof(usbDescrHID): length of descriptor in bytes */
399 USBDESCR_HID
, /* descriptor type: HID */
400 0x01, 0x01, /* BCD representation of HID version */
401 0x00, /* target country code */
402 0x01, /* number of HID Report (or other HID class) Descriptor infos to follow */
403 0x22, /* descriptor type: report */
404 sizeof(keyboard_hid_report
), 0, /* total length of report descriptor */
405 /* Endpoint descriptor */
406 #if USB_CFG_HAVE_INTRIN_ENDPOINT /* endpoint descriptor for endpoint 1 */
407 7, /* sizeof(usbDescrEndpoint) */
408 USBDESCR_ENDPOINT
, /* descriptor type = endpoint */
409 (char)0x81, /* IN endpoint number 1 */
410 0x03, /* attrib: Interrupt endpoint */
411 8, 0, /* maximum packet size */
412 USB_CFG_INTR_POLL_INTERVAL
, /* in ms */
418 /* Interface descriptor */
419 9, /* sizeof(usbDescrInterface): length of descriptor in bytes */
420 USBDESCR_INTERFACE
, /* descriptor type */
421 1, /* index of this interface */
422 0, /* alternate setting for this interface */
423 USB_CFG_HAVE_INTRIN_ENDPOINT3
, /* endpoints excl 0: number of endpoint descriptors to follow */
424 0x03, /* CLASS: HID */
425 0, /* SUBCLASS: none */
426 0, /* PROTOCOL: none */
427 0, /* string index for interface */
429 9, /* sizeof(usbDescrHID): length of descriptor in bytes */
430 USBDESCR_HID
, /* descriptor type: HID */
431 0x01, 0x01, /* BCD representation of HID version */
432 0x00, /* target country code */
433 0x01, /* number of HID Report (or other HID class) Descriptor infos to follow */
434 0x22, /* descriptor type: report */
435 sizeof(mouse_hid_report
), 0, /* total length of report descriptor */
436 #if USB_CFG_HAVE_INTRIN_ENDPOINT3 /* endpoint descriptor for endpoint 3 */
437 /* Endpoint descriptor */
438 7, /* sizeof(usbDescrEndpoint) */
439 USBDESCR_ENDPOINT
, /* descriptor type = endpoint */
440 (char)(0x80 | USB_CFG_EP3_NUMBER
), /* IN endpoint number 3 */
441 0x03, /* attrib: Interrupt endpoint */
442 8, 0, /* maximum packet size */
443 USB_CFG_INTR_POLL_INTERVAL
, /* in ms */
449 USB_PUBLIC usbMsgLen_t
usbFunctionDescriptor(struct usbRequest
*rq
)
453 debug("usbFunctionDescriptor: ");
454 debug_hex(rq
->bmRequestType
); debug(" ");
455 debug_hex(rq
->bRequest
); debug(" ");
456 debug_hex16(rq
->wValue
.word
); debug(" ");
457 debug_hex16(rq
->wIndex
.word
); debug(" ");
458 debug_hex16(rq
->wLength
.word
); debug("\n");
460 switch (rq
->wValue
.bytes
[1]) {
461 #if USB_CFG_DESCR_PROPS_CONFIGURATION
462 case USBDESCR_CONFIG
:
463 usbMsgPtr
= (unsigned char *)usbDescriptorConfiguration
;
464 len
= sizeof(usbDescriptorConfiguration
);
468 usbMsgPtr
= (unsigned char *)(usbDescriptorConfiguration
+ 18);
471 case USBDESCR_HID_REPORT
:
472 /* interface index */
473 switch (rq
->wIndex
.word
) {
475 usbMsgPtr
= keyboard_hid_report
;
476 len
= sizeof(keyboard_hid_report
);
479 usbMsgPtr
= mouse_hid_report
;
480 len
= sizeof(mouse_hid_report
);
485 debug("desc len: "); debug_hex(len
); debug("\n");