2 Copyright 2011 Jun Wako <wakojun@gmail.com>
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include <avr/eeprom.h>
22 #include "usbconfig.h"
27 #include "host_driver.h"
29 #include "bootloader.h"
32 static uint8_t vusb_keyboard_leds = 0;
33 static uint8_t vusb_idle_rate = 0;
35 /* Keyboard report send buffer */
37 static report_keyboard_t kbuf[KBUF_SIZE];
38 static uint8_t kbuf_head = 0;
39 static uint8_t kbuf_tail = 0;
47 static keyboard_report_t keyboard_report; // sent to PC
49 /* transfer keyboard report from buffer */
50 void vusb_transfer_keyboard(void)
52 if (usbInterruptIsReady()) {
53 if (kbuf_head != kbuf_tail) {
54 usbSetInterrupt((void *)&kbuf[kbuf_tail], sizeof(report_keyboard_t));
55 kbuf_tail = (kbuf_tail + 1) % KBUF_SIZE;
57 print("V-USB: kbuf["); pdec(kbuf_tail); print("->"); pdec(kbuf_head); print("](");
58 phex((kbuf_head < kbuf_tail) ? (KBUF_SIZE - kbuf_tail + kbuf_head) : (kbuf_head - kbuf_tail));
66 /*------------------------------------------------------------------*
68 *------------------------------------------------------------------*/
69 static uint8_t keyboard_leds(void);
70 static void send_keyboard(report_keyboard_t *report);
71 static void send_mouse(report_mouse_t *report);
72 static void send_system(uint16_t data);
73 static void send_consumer(uint16_t data);
75 static host_driver_t driver = {
83 host_driver_t *vusb_driver(void)
88 static uint8_t keyboard_leds(void) {
89 return vusb_keyboard_leds;
92 static void send_keyboard(report_keyboard_t *report)
94 uint8_t next = (kbuf_head + 1) % KBUF_SIZE;
95 if (next != kbuf_tail) {
96 kbuf[kbuf_head] = *report;
99 debug("kbuf: full\n");
102 // NOTE: send key strokes of Macro
104 vusb_transfer_keyboard();
110 report_mouse_t report;
111 } __attribute__ ((packed)) vusb_mouse_report_t;
113 static void send_mouse(report_mouse_t *report)
115 vusb_mouse_report_t r = {
116 .report_id = REPORT_ID_MOUSE,
119 if (usbInterruptIsReady3()) {
120 usbSetInterrupt3((void *)&r, sizeof(vusb_mouse_report_t));
128 } __attribute__ ((packed)) report_extra_t;
130 static void send_system(uint16_t data)
132 static uint16_t last_data = 0;
133 if (data == last_data) return;
136 report_extra_t report = {
137 .report_id = REPORT_ID_SYSTEM,
140 if (usbInterruptIsReady3()) {
141 usbSetInterrupt3((void *)&report, sizeof(report));
145 static void send_consumer(uint16_t data)
147 static uint16_t last_data = 0;
148 if (data == last_data) return;
151 report_extra_t report = {
152 .report_id = REPORT_ID_CONSUMER,
155 if (usbInterruptIsReady3()) {
156 usbSetInterrupt3((void *)&report, sizeof(report));
162 /*------------------------------------------------------------------*
163 * Request from host *
164 *------------------------------------------------------------------*/
174 usbMsgLen_t usbFunctionSetup(uchar data[8])
176 usbRequest_t *rq = (void *)data;
178 if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS){ /* class request type */
179 if(rq->bRequest == USBRQ_HID_GET_REPORT){
180 debug("GET_REPORT:");
181 /* we only have one report type, so don't look at wValue */
182 usbMsgPtr = (void *)&keyboard_report;
183 return sizeof(keyboard_report);
184 }else if(rq->bRequest == USBRQ_HID_GET_IDLE){
186 //debug_hex(vusb_idle_rate);
187 usbMsgPtr = &vusb_idle_rate;
189 }else if(rq->bRequest == USBRQ_HID_SET_IDLE){
190 vusb_idle_rate = rq->wValue.bytes[1];
192 debug_hex(vusb_idle_rate);
193 }else if(rq->bRequest == USBRQ_HID_SET_REPORT){
194 debug("SET_REPORT: ");
195 // Report Type: 0x02(Out)/ReportID: 0x00(none) && Interface: 0(keyboard)
196 if (rq->wValue.word == 0x0200 && rq->wIndex.word == 0) {
198 last_req.kind = SET_LED;
199 last_req.len = rq->wLength.word;
200 #ifdef BOOTLOADER_SIZE
201 } else if(rq->wValue.word == 0x0301) {
202 last_req.kind = BOOTLOADER;
203 last_req.len = rq->wLength.word;
206 return USB_NO_MSG; // to get data in usbFunctionWrite
212 /* no vendor specific requests implemented */
215 return 0; /* default for not implemented requests: return no data back to host */
218 uchar usbFunctionWrite(uchar *data, uchar len)
220 if (last_req.len == 0) {
223 switch (last_req.kind) {
228 vusb_keyboard_leds = data[0];
233 usbDeviceDisconnect();
247 /*------------------------------------------------------------------*
249 *------------------------------------------------------------------*/
252 * Report Descriptor for keyboard
254 * from an example in HID spec appendix
256 const PROGMEM uchar keyboard_hid_report[] = {
257 0x05, 0x01, // Usage Page (Generic Desktop),
258 0x09, 0x06, // Usage (Keyboard),
259 0xA1, 0x01, // Collection (Application),
260 0x75, 0x01, // Report Size (1),
261 0x95, 0x08, // Report Count (8),
262 0x05, 0x07, // Usage Page (Key Codes),
263 0x19, 0xE0, // Usage Minimum (224),
264 0x29, 0xE7, // Usage Maximum (231),
265 0x15, 0x00, // Logical Minimum (0),
266 0x25, 0x01, // Logical Maximum (1),
267 0x81, 0x02, // Input (Data, Variable, Absolute), ;Modifier byte
268 0x95, 0x01, // Report Count (1),
269 0x75, 0x08, // Report Size (8),
270 0x81, 0x03, // Input (Constant), ;Reserved byte
271 0x95, 0x05, // Report Count (5),
272 0x75, 0x01, // Report Size (1),
273 0x05, 0x08, // Usage Page (LEDs),
274 0x19, 0x01, // Usage Minimum (1),
275 0x29, 0x05, // Usage Maximum (5),
276 0x91, 0x02, // Output (Data, Variable, Absolute), ;LED report
277 0x95, 0x01, // Report Count (1),
278 0x75, 0x03, // Report Size (3),
279 0x91, 0x03, // Output (Constant), ;LED report padding
280 0x95, 0x06, // Report Count (6),
281 0x75, 0x08, // Report Size (8),
282 0x15, 0x00, // Logical Minimum (0),
283 0x25, 0xFF, 0x00, // Logical Maximum(255),
284 0x05, 0x07, // Usage Page (Key Codes),
285 0x19, 0x00, // Usage Minimum (0),
286 0x29, 0xFF, // Usage Maximum (255),
287 0x81, 0x00, // Input (Data, Array),
288 0xc0 // End Collection
292 * Report Descriptor for mouse
294 * Mouse Protocol 1, HID 1.11 spec, Appendix B, page 59-60, with wheel extension
295 * http://www.microchip.com/forums/tm.aspx?high=&m=391435&mpage=1#391521
296 * http://www.keil.com/forum/15671/
297 * http://www.microsoft.com/whdc/device/input/wheel.mspx
299 const PROGMEM uchar mouse_hid_report[] = {
301 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
302 0x09, 0x02, // USAGE (Mouse)
303 0xa1, 0x01, // COLLECTION (Application)
304 0x85, REPORT_ID_MOUSE, // REPORT_ID (1)
305 0x09, 0x01, // USAGE (Pointer)
306 0xa1, 0x00, // COLLECTION (Physical)
307 // ---------------------------- Buttons
308 0x05, 0x09, // USAGE_PAGE (Button)
309 0x19, 0x01, // USAGE_MINIMUM (Button 1)
310 0x29, 0x05, // USAGE_MAXIMUM (Button 5)
311 0x15, 0x00, // LOGICAL_MINIMUM (0)
312 0x25, 0x01, // LOGICAL_MAXIMUM (1)
313 0x75, 0x01, // REPORT_SIZE (1)
314 0x95, 0x05, // REPORT_COUNT (5)
315 0x81, 0x02, // INPUT (Data,Var,Abs)
316 0x75, 0x03, // REPORT_SIZE (3)
317 0x95, 0x01, // REPORT_COUNT (1)
318 0x81, 0x03, // INPUT (Cnst,Var,Abs)
319 // ---------------------------- X,Y position
320 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
321 0x09, 0x30, // USAGE (X)
322 0x09, 0x31, // USAGE (Y)
323 0x15, 0x81, // LOGICAL_MINIMUM (-127)
324 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
325 0x75, 0x08, // REPORT_SIZE (8)
326 0x95, 0x02, // REPORT_COUNT (2)
327 0x81, 0x06, // INPUT (Data,Var,Rel)
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 0x95, 0x01, // REPORT_COUNT (1)
336 0x81, 0x06, // INPUT (Data,Var,Rel)
337 // ---------------------------- Horizontal wheel
338 0x05, 0x0c, // USAGE_PAGE (Consumer Devices)
339 0x0a, 0x38, 0x02, // USAGE (AC Pan)
340 0x15, 0x81, // LOGICAL_MINIMUM (-127)
341 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
342 0x75, 0x08, // REPORT_SIZE (8)
343 0x95, 0x01, // REPORT_COUNT (1)
344 0x81, 0x06, // INPUT (Data,Var,Rel)
345 0xc0, // END_COLLECTION
346 0xc0, // END_COLLECTION
348 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
349 0x09, 0x80, // USAGE (System Control)
350 0xa1, 0x01, // COLLECTION (Application)
351 0x85, REPORT_ID_SYSTEM, // REPORT_ID (2)
352 0x15, 0x01, // LOGICAL_MINIMUM (0x1)
353 0x25, 0xb7, 0x00, // LOGICAL_MAXIMUM (0xb7)
354 0x19, 0x01, // USAGE_MINIMUM (0x1)
355 0x29, 0xb7, // USAGE_MAXIMUM (0xb7)
356 0x75, 0x10, // REPORT_SIZE (16)
357 0x95, 0x01, // REPORT_COUNT (1)
358 0x81, 0x00, // INPUT (Data,Array,Abs)
359 0xc0, // END_COLLECTION
361 0x05, 0x0c, // USAGE_PAGE (Consumer Devices)
362 0x09, 0x01, // USAGE (Consumer Control)
363 0xa1, 0x01, // COLLECTION (Application)
364 0x85, REPORT_ID_CONSUMER, // REPORT_ID (3)
365 0x15, 0x01, // LOGICAL_MINIMUM (0x1)
366 0x26, 0x9c, 0x02, // LOGICAL_MAXIMUM (0x29c)
367 0x19, 0x01, // USAGE_MINIMUM (0x1)
368 0x2a, 0x9c, 0x02, // USAGE_MAXIMUM (0x29c)
369 0x75, 0x10, // REPORT_SIZE (16)
370 0x95, 0x01, // REPORT_COUNT (1)
371 0x81, 0x00, // INPUT (Data,Array,Abs)
372 0xc0, // END_COLLECTION
377 * Descriptor for compite device: Keyboard + Mouse
379 * contains: device, interface, HID and endpoint descriptors
381 #if USB_CFG_DESCR_PROPS_CONFIGURATION
382 const PROGMEM char usbDescriptorConfiguration[] = { /* USB configuration descriptor */
383 9, /* sizeof(usbDescriptorConfiguration): length of descriptor in bytes */
384 USBDESCR_CONFIG, /* descriptor type */
385 9 + (9 + 9 + 7) + (9 + 9 + 7), 0,
386 //18 + 7 * USB_CFG_HAVE_INTRIN_ENDPOINT + 7 * USB_CFG_HAVE_INTRIN_ENDPOINT3 + 9, 0,
387 /* total length of data returned (including inlined descriptors) */
388 2, /* number of interfaces in this configuration */
389 1, /* index of this configuration */
390 0, /* configuration name string index */
391 #if USB_CFG_IS_SELF_POWERED
392 (1 << 7) | USBATTR_SELFPOWER, /* attributes */
394 (1 << 7), /* attributes */
396 USB_CFG_MAX_BUS_POWER/2, /* max USB current in 2mA units */
401 /* Interface descriptor */
402 9, /* sizeof(usbDescrInterface): length of descriptor in bytes */
403 USBDESCR_INTERFACE, /* descriptor type */
404 0, /* index of this interface */
405 0, /* alternate setting for this interface */
406 USB_CFG_HAVE_INTRIN_ENDPOINT, /* endpoints excl 0: number of endpoint descriptors to follow */
407 USB_CFG_INTERFACE_CLASS,
408 USB_CFG_INTERFACE_SUBCLASS,
409 USB_CFG_INTERFACE_PROTOCOL,
410 0, /* string index for interface */
412 9, /* sizeof(usbDescrHID): length of descriptor in bytes */
413 USBDESCR_HID, /* descriptor type: HID */
414 0x01, 0x01, /* BCD representation of HID version */
415 0x00, /* target country code */
416 0x01, /* number of HID Report (or other HID class) Descriptor infos to follow */
417 0x22, /* descriptor type: report */
418 sizeof(keyboard_hid_report), 0, /* total length of report descriptor */
419 /* Endpoint descriptor */
420 #if USB_CFG_HAVE_INTRIN_ENDPOINT /* endpoint descriptor for endpoint 1 */
421 7, /* sizeof(usbDescrEndpoint) */
422 USBDESCR_ENDPOINT, /* descriptor type = endpoint */
423 (char)0x81, /* IN endpoint number 1 */
424 0x03, /* attrib: Interrupt endpoint */
425 8, 0, /* maximum packet size */
426 USB_CFG_INTR_POLL_INTERVAL, /* in ms */
432 /* Interface descriptor */
433 9, /* sizeof(usbDescrInterface): length of descriptor in bytes */
434 USBDESCR_INTERFACE, /* descriptor type */
435 1, /* index of this interface */
436 0, /* alternate setting for this interface */
437 USB_CFG_HAVE_INTRIN_ENDPOINT3, /* endpoints excl 0: number of endpoint descriptors to follow */
438 0x03, /* CLASS: HID */
439 0, /* SUBCLASS: none */
440 0, /* PROTOCOL: none */
441 0, /* string index for interface */
443 9, /* sizeof(usbDescrHID): length of descriptor in bytes */
444 USBDESCR_HID, /* descriptor type: HID */
445 0x01, 0x01, /* BCD representation of HID version */
446 0x00, /* target country code */
447 0x01, /* number of HID Report (or other HID class) Descriptor infos to follow */
448 0x22, /* descriptor type: report */
449 sizeof(mouse_hid_report), 0, /* total length of report descriptor */
450 #if USB_CFG_HAVE_INTRIN_ENDPOINT3 /* endpoint descriptor for endpoint 3 */
451 /* Endpoint descriptor */
452 7, /* sizeof(usbDescrEndpoint) */
453 USBDESCR_ENDPOINT, /* descriptor type = endpoint */
454 (char)(0x80 | USB_CFG_EP3_NUMBER), /* IN endpoint number 3 */
455 0x03, /* attrib: Interrupt endpoint */
456 8, 0, /* maximum packet size */
457 USB_CFG_INTR_POLL_INTERVAL, /* in ms */
463 USB_PUBLIC usbMsgLen_t usbFunctionDescriptor(struct usbRequest *rq)
468 debug("usbFunctionDescriptor: ");
469 debug_hex(rq->bmRequestType); debug(" ");
470 debug_hex(rq->bRequest); debug(" ");
471 debug_hex16(rq->wValue.word); debug(" ");
472 debug_hex16(rq->wIndex.word); debug(" ");
473 debug_hex16(rq->wLength.word); debug("\n");
475 switch (rq->wValue.bytes[1]) {
476 #if USB_CFG_DESCR_PROPS_CONFIGURATION
477 case USBDESCR_CONFIG:
478 usbMsgPtr = (unsigned char *)usbDescriptorConfiguration;
479 len = sizeof(usbDescriptorConfiguration);
483 switch (rq->wValue.bytes[0]) {
485 usbMsgPtr = (unsigned char *)(usbDescriptorConfiguration + 9 + 9);
489 usbMsgPtr = (unsigned char *)(usbDescriptorConfiguration + 9 + (9 + 9 + 7) + 9);
494 case USBDESCR_HID_REPORT:
495 /* interface index */
496 switch (rq->wIndex.word) {
498 usbMsgPtr = (unsigned char *)keyboard_hid_report;
499 len = sizeof(keyboard_hid_report);
502 usbMsgPtr = (unsigned char *)mouse_hid_report;
503 len = sizeof(mouse_hid_report);
508 //debug("desc len: "); debug_hex(len); debug("\n");