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_leds
= 0;
16 static uchar idleRate
= 0;
18 uint8_t host_keyboard_leds(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_leds
= 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, 0x02, // USAGE (Mouse)
265 0xa1, 0x02, // COLLECTION (Logical)
266 0x09, 0x01, // USAGE (Pointer)
267 0xa1, 0x00, // COLLECTION (Physical)
268 // ------------------------------ Buttons
269 0x05, 0x09, // USAGE_PAGE (Button)
270 0x19, 0x01, // USAGE_MINIMUM (Button 1)
271 0x29, 0x05, // USAGE_MAXIMUM (Button 5)
272 0x15, 0x00, // LOGICAL_MINIMUM (0)
273 0x25, 0x01, // LOGICAL_MAXIMUM (1)
274 0x75, 0x01, // REPORT_SIZE (1)
275 0x95, 0x05, // REPORT_COUNT (5)
276 0x81, 0x02, // INPUT (Data,Var,Abs)
277 // ------------------------------ Padding
278 0x75, 0x03, // REPORT_SIZE (3)
279 0x95, 0x01, // REPORT_COUNT (1)
280 0x81, 0x03, // INPUT (Cnst,Var,Abs)
281 // ------------------------------ X,Y position
282 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
283 0x09, 0x30, // USAGE (X)
284 0x09, 0x31, // USAGE (Y)
285 0x15, 0x81, // LOGICAL_MINIMUM (-127)
286 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
287 0x75, 0x08, // REPORT_SIZE (8)
288 0x95, 0x02, // REPORT_COUNT (2)
289 0x81, 0x06, // INPUT (Data,Var,Rel)
290 0xa1, 0x02, // COLLECTION (Logical)
291 // ------------------------------ Vertical wheel res multiplier
292 0x09, 0x48, // USAGE (Resolution Multiplier)
293 0x15, 0x00, // LOGICAL_MINIMUM (0)
294 0x25, 0x01, // LOGICAL_MAXIMUM (1)
295 0x35, 0x01, // PHYSICAL_MINIMUM (1)
296 0x45, 0x04, // PHYSICAL_MAXIMUM (4)
297 0x75, 0x02, // REPORT_SIZE (2)
298 0x95, 0x01, // REPORT_COUNT (1)
300 0xb1, 0x02, // FEATURE (Data,Var,Abs)
301 // ------------------------------ Vertical wheel
302 0x09, 0x38, // USAGE (Wheel)
303 0x15, 0x81, // LOGICAL_MINIMUM (-127)
304 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
305 0x35, 0x00, // PHYSICAL_MINIMUM (0) - reset physical
306 0x45, 0x00, // PHYSICAL_MAXIMUM (0)
307 0x75, 0x08, // REPORT_SIZE (8)
308 0x81, 0x06, // INPUT (Data,Var,Rel)
309 0xc0, // END_COLLECTION
310 0xa1, 0x02, // COLLECTION (Logical)
311 // ------------------------------ Horizontal wheel res multiplier
312 0x09, 0x48, // USAGE (Resolution Multiplier)
314 0xb1, 0x02, // FEATURE (Data,Var,Abs)
315 // ------------------------------ Padding for Feature report
316 0x35, 0x00, // PHYSICAL_MINIMUM (0) - reset physical
317 0x45, 0x00, // PHYSICAL_MAXIMUM (0)
318 0x75, 0x04, // REPORT_SIZE (4)
319 0xb1, 0x03, // FEATURE (Cnst,Var,Abs)
320 // ------------------------------ Horizontal wheel
321 0x05, 0x0c, // USAGE_PAGE (Consumer Devices)
322 0x0a, 0x38, 0x02, // USAGE (AC Pan)
323 0x15, 0x81, // LOGICAL_MINIMUM (-127)
324 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
325 0x75, 0x08, // REPORT_SIZE (8)
326 0x81, 0x06, // INPUT (Data,Var,Rel)
327 0xc0, // END_COLLECTION
328 0xc0, // END_COLLECTION
329 0xc0, // END_COLLECTION
330 0xc0 // END_COLLECTION
335 * Descriptor for compite device: Keyboard + Mouse
337 * contains: device, interface, HID and endpoint descriptors
339 #if USB_CFG_DESCR_PROPS_CONFIGURATION
340 PROGMEM
char usbDescriptorConfiguration
[] = { /* USB configuration descriptor */
341 9, /* sizeof(usbDescriptorConfiguration): length of descriptor in bytes */
342 USBDESCR_CONFIG
, /* descriptor type */
343 9 + (9 + 9 + 7) + (9 + 9 + 7), 0,
344 //18 + 7 * USB_CFG_HAVE_INTRIN_ENDPOINT + 7 * USB_CFG_HAVE_INTRIN_ENDPOINT3 + 9, 0,
345 /* total length of data returned (including inlined descriptors) */
346 2, /* number of interfaces in this configuration */
347 1, /* index of this configuration */
348 0, /* configuration name string index */
349 #if USB_CFG_IS_SELF_POWERED
350 (1 << 7) | USBATTR_SELFPOWER
, /* attributes */
352 (1 << 7), /* attributes */
354 USB_CFG_MAX_BUS_POWER
/2, /* max USB current in 2mA units */
359 /* Interface descriptor */
360 9, /* sizeof(usbDescrInterface): length of descriptor in bytes */
361 USBDESCR_INTERFACE
, /* descriptor type */
362 0, /* index of this interface */
363 0, /* alternate setting for this interface */
364 USB_CFG_HAVE_INTRIN_ENDPOINT
, /* endpoints excl 0: number of endpoint descriptors to follow */
365 USB_CFG_INTERFACE_CLASS
,
366 USB_CFG_INTERFACE_SUBCLASS
,
367 USB_CFG_INTERFACE_PROTOCOL
,
368 0, /* string index for interface */
370 9, /* sizeof(usbDescrHID): length of descriptor in bytes */
371 USBDESCR_HID
, /* descriptor type: HID */
372 0x01, 0x01, /* BCD representation of HID version */
373 0x00, /* target country code */
374 0x01, /* number of HID Report (or other HID class) Descriptor infos to follow */
375 0x22, /* descriptor type: report */
376 sizeof(keyboard_hid_report
), 0, /* total length of report descriptor */
377 /* Endpoint descriptor */
378 #if USB_CFG_HAVE_INTRIN_ENDPOINT /* endpoint descriptor for endpoint 1 */
379 7, /* sizeof(usbDescrEndpoint) */
380 USBDESCR_ENDPOINT
, /* descriptor type = endpoint */
381 (char)0x81, /* IN endpoint number 1 */
382 0x03, /* attrib: Interrupt endpoint */
383 8, 0, /* maximum packet size */
384 USB_CFG_INTR_POLL_INTERVAL
, /* in ms */
390 /* Interface descriptor */
391 9, /* sizeof(usbDescrInterface): length of descriptor in bytes */
392 USBDESCR_INTERFACE
, /* descriptor type */
393 1, /* index of this interface */
394 0, /* alternate setting for this interface */
395 USB_CFG_HAVE_INTRIN_ENDPOINT3
, /* endpoints excl 0: number of endpoint descriptors to follow */
396 0x03, /* CLASS: HID */
397 0, /* SUBCLASS: none */
398 0, /* PROTOCOL: none */
399 0, /* string index for interface */
401 9, /* sizeof(usbDescrHID): length of descriptor in bytes */
402 USBDESCR_HID
, /* descriptor type: HID */
403 0x01, 0x01, /* BCD representation of HID version */
404 0x00, /* target country code */
405 0x01, /* number of HID Report (or other HID class) Descriptor infos to follow */
406 0x22, /* descriptor type: report */
407 sizeof(mouse_hid_report
), 0, /* total length of report descriptor */
408 #if USB_CFG_HAVE_INTRIN_ENDPOINT3 /* endpoint descriptor for endpoint 3 */
409 /* Endpoint descriptor */
410 7, /* sizeof(usbDescrEndpoint) */
411 USBDESCR_ENDPOINT
, /* descriptor type = endpoint */
412 (char)(0x80 | USB_CFG_EP3_NUMBER
), /* IN endpoint number 3 */
413 0x03, /* attrib: Interrupt endpoint */
414 8, 0, /* maximum packet size */
415 USB_CFG_INTR_POLL_INTERVAL
, /* in ms */
421 USB_PUBLIC usbMsgLen_t
usbFunctionDescriptor(struct usbRequest
*rq
)
425 debug("usbFunctionDescriptor: ");
426 debug_hex(rq
->bmRequestType
); debug(" ");
427 debug_hex(rq
->bRequest
); debug(" ");
428 debug_hex16(rq
->wValue
.word
); debug(" ");
429 debug_hex16(rq
->wIndex
.word
); debug(" ");
430 debug_hex16(rq
->wLength
.word
); debug("\n");
432 switch (rq
->wValue
.bytes
[1]) {
433 #if USB_CFG_DESCR_PROPS_CONFIGURATION
434 case USBDESCR_CONFIG
:
435 usbMsgPtr
= (unsigned char *)usbDescriptorConfiguration
;
436 len
= sizeof(usbDescriptorConfiguration
);
440 usbMsgPtr
= (unsigned char *)(usbDescriptorConfiguration
+ 18);
443 case USBDESCR_HID_REPORT
:
444 /* interface index */
445 switch (rq
->wIndex
.word
) {
447 usbMsgPtr
= keyboard_hid_report
;
448 len
= sizeof(keyboard_hid_report
);
451 usbMsgPtr
= mouse_hid_report
;
452 len
= sizeof(mouse_hid_report
);
457 debug("desc len: "); debug_hex(len
); debug("\n");