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/>.
20 #include "usbconfig.h"
25 #include "host_driver.h"
29 static uint8_t vusb_keyboard_leds = 0;
30 static uint8_t vusb_idle_rate = 0;
32 /* Keyboard report send buffer */
34 static report_keyboard_t kbuf[KBUF_SIZE];
35 static uint8_t kbuf_head = 0;
36 static uint8_t kbuf_tail = 0;
39 /* transfer keyboard report from buffer */
40 void vusb_transfer_keyboard(void)
42 if (usbInterruptIsReady()) {
43 if (kbuf_head != kbuf_tail) {
44 usbSetInterrupt((void *)&kbuf[kbuf_tail], sizeof(report_keyboard_t));
45 kbuf_tail = (kbuf_tail + 1) % KBUF_SIZE;
47 print("V-USB: kbuf["); pdec(kbuf_tail); print("->"); pdec(kbuf_head); print("](");
48 phex((kbuf_head < kbuf_tail) ? (KBUF_SIZE - kbuf_tail + kbuf_head) : (kbuf_head - kbuf_tail));
56 /*------------------------------------------------------------------*
58 *------------------------------------------------------------------*/
59 static uint8_t keyboard_leds(void);
60 static void send_keyboard(report_keyboard_t *report);
61 static void send_mouse(report_mouse_t *report);
62 static void send_system(uint16_t data);
63 static void send_consumer(uint16_t data);
65 static host_driver_t driver = {
73 host_driver_t *vusb_driver(void)
78 static uint8_t keyboard_leds(void) {
79 return vusb_keyboard_leds;
82 static void send_keyboard(report_keyboard_t *report)
84 uint8_t next = (kbuf_head + 1) % KBUF_SIZE;
85 if (next != kbuf_tail) {
86 kbuf[kbuf_head] = *report;
89 debug("kbuf: full\n");
92 // NOTE: send key strokes of Macro
94 vusb_transfer_keyboard();
100 report_mouse_t report;
101 } __attribute__ ((packed)) vusb_mouse_report_t;
103 static void send_mouse(report_mouse_t *report)
105 vusb_mouse_report_t r = {
106 .report_id = REPORT_ID_MOUSE,
109 if (usbInterruptIsReady3()) {
110 usbSetInterrupt3((void *)&r, sizeof(vusb_mouse_report_t));
118 } __attribute__ ((packed)) report_extra_t;
120 static void send_system(uint16_t data)
122 static uint16_t last_data = 0;
123 if (data == last_data) return;
126 report_extra_t report = {
127 .report_id = REPORT_ID_SYSTEM,
130 if (usbInterruptIsReady3()) {
131 usbSetInterrupt3((void *)&report, sizeof(report));
135 static void send_consumer(uint16_t data)
137 static uint16_t last_data = 0;
138 if (data == last_data) return;
141 report_extra_t report = {
142 .report_id = REPORT_ID_CONSUMER,
145 if (usbInterruptIsReady3()) {
146 usbSetInterrupt3((void *)&report, sizeof(report));
152 /*------------------------------------------------------------------*
153 * Request from host *
154 *------------------------------------------------------------------*/
163 usbMsgLen_t usbFunctionSetup(uchar data[8])
165 usbRequest_t *rq = (void *)data;
167 if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS){ /* class request type */
168 if(rq->bRequest == USBRQ_HID_GET_REPORT){
169 debug("GET_REPORT:");
170 /* we only have one report type, so don't look at wValue */
171 usbMsgPtr = (void *)keyboard_report;
172 return sizeof(*keyboard_report);
173 }else if(rq->bRequest == USBRQ_HID_GET_IDLE){
175 //debug_hex(vusb_idle_rate);
176 usbMsgPtr = &vusb_idle_rate;
178 }else if(rq->bRequest == USBRQ_HID_SET_IDLE){
179 vusb_idle_rate = rq->wValue.bytes[1];
181 debug_hex(vusb_idle_rate);
182 }else if(rq->bRequest == USBRQ_HID_SET_REPORT){
183 debug("SET_REPORT: ");
184 // Report Type: 0x02(Out)/ReportID: 0x00(none) && Interface: 0(keyboard)
185 if (rq->wValue.word == 0x0200 && rq->wIndex.word == 0) {
187 last_req.kind = SET_LED;
188 last_req.len = rq->wLength.word;
190 return USB_NO_MSG; // to get data in usbFunctionWrite
196 /* no vendor specific requests implemented */
199 return 0; /* default for not implemented requests: return no data back to host */
202 uchar usbFunctionWrite(uchar *data, uchar len)
204 if (last_req.len == 0) {
207 switch (last_req.kind) {
212 vusb_keyboard_leds = data[0];
226 /*------------------------------------------------------------------*
228 *------------------------------------------------------------------*/
231 * Report Descriptor for keyboard
233 * from an example in HID spec appendix
235 PROGMEM uchar keyboard_hid_report[] = {
236 0x05, 0x01, // Usage Page (Generic Desktop),
237 0x09, 0x06, // Usage (Keyboard),
238 0xA1, 0x01, // Collection (Application),
239 0x75, 0x01, // Report Size (1),
240 0x95, 0x08, // Report Count (8),
241 0x05, 0x07, // Usage Page (Key Codes),
242 0x19, 0xE0, // Usage Minimum (224),
243 0x29, 0xE7, // Usage Maximum (231),
244 0x15, 0x00, // Logical Minimum (0),
245 0x25, 0x01, // Logical Maximum (1),
246 0x81, 0x02, // Input (Data, Variable, Absolute), ;Modifier byte
247 0x95, 0x01, // Report Count (1),
248 0x75, 0x08, // Report Size (8),
249 0x81, 0x03, // Input (Constant), ;Reserved byte
250 0x95, 0x05, // Report Count (5),
251 0x75, 0x01, // Report Size (1),
252 0x05, 0x08, // Usage Page (LEDs),
253 0x19, 0x01, // Usage Minimum (1),
254 0x29, 0x05, // Usage Maximum (5),
255 0x91, 0x02, // Output (Data, Variable, Absolute), ;LED report
256 0x95, 0x01, // Report Count (1),
257 0x75, 0x03, // Report Size (3),
258 0x91, 0x03, // Output (Constant), ;LED report padding
259 0x95, 0x06, // Report Count (6),
260 0x75, 0x08, // Report Size (8),
261 0x15, 0x00, // Logical Minimum (0),
262 0x25, 0xFF, // Logical Maximum(255),
263 0x05, 0x07, // Usage Page (Key Codes),
264 0x19, 0x00, // Usage Minimum (0),
265 0x29, 0xFF, // Usage Maximum (255),
266 0x81, 0x00, // Input (Data, Array),
267 0xc0 // End Collection
271 * Report Descriptor for mouse
273 * Mouse Protocol 1, HID 1.11 spec, Appendix B, page 59-60, with wheel extension
274 * http://www.microchip.com/forums/tm.aspx?high=&m=391435&mpage=1#391521
275 * http://www.keil.com/forum/15671/
276 * http://www.microsoft.com/whdc/device/input/wheel.mspx
278 PROGMEM uchar mouse_hid_report[] = {
280 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
281 0x09, 0x02, // USAGE (Mouse)
282 0xa1, 0x01, // COLLECTION (Application)
283 0x85, REPORT_ID_MOUSE, // REPORT_ID (1)
284 0x09, 0x01, // USAGE (Pointer)
285 0xa1, 0x00, // COLLECTION (Physical)
286 // ---------------------------- Buttons
287 0x05, 0x09, // USAGE_PAGE (Button)
288 0x19, 0x01, // USAGE_MINIMUM (Button 1)
289 0x29, 0x05, // USAGE_MAXIMUM (Button 5)
290 0x15, 0x00, // LOGICAL_MINIMUM (0)
291 0x25, 0x01, // LOGICAL_MAXIMUM (1)
292 0x75, 0x01, // REPORT_SIZE (1)
293 0x95, 0x05, // REPORT_COUNT (5)
294 0x81, 0x02, // INPUT (Data,Var,Abs)
295 0x75, 0x03, // REPORT_SIZE (3)
296 0x95, 0x01, // REPORT_COUNT (1)
297 0x81, 0x03, // INPUT (Cnst,Var,Abs)
298 // ---------------------------- X,Y position
299 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
300 0x09, 0x30, // USAGE (X)
301 0x09, 0x31, // USAGE (Y)
302 0x15, 0x81, // LOGICAL_MINIMUM (-127)
303 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
304 0x75, 0x08, // REPORT_SIZE (8)
305 0x95, 0x02, // REPORT_COUNT (2)
306 0x81, 0x06, // INPUT (Data,Var,Rel)
307 // ---------------------------- Vertical wheel
308 0x09, 0x38, // USAGE (Wheel)
309 0x15, 0x81, // LOGICAL_MINIMUM (-127)
310 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
311 0x35, 0x00, // PHYSICAL_MINIMUM (0) - reset physical
312 0x45, 0x00, // PHYSICAL_MAXIMUM (0)
313 0x75, 0x08, // REPORT_SIZE (8)
314 0x95, 0x01, // REPORT_COUNT (1)
315 0x81, 0x06, // INPUT (Data,Var,Rel)
316 // ---------------------------- Horizontal wheel
317 0x05, 0x0c, // USAGE_PAGE (Consumer Devices)
318 0x0a, 0x38, 0x02, // USAGE (AC Pan)
319 0x15, 0x81, // LOGICAL_MINIMUM (-127)
320 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
321 0x75, 0x08, // REPORT_SIZE (8)
322 0x95, 0x01, // REPORT_COUNT (1)
323 0x81, 0x06, // INPUT (Data,Var,Rel)
324 0xc0, // END_COLLECTION
325 0xc0, // END_COLLECTION
327 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
328 0x09, 0x80, // USAGE (System Control)
329 0xa1, 0x01, // COLLECTION (Application)
330 0x85, REPORT_ID_SYSTEM, // REPORT_ID (2)
331 0x15, 0x01, // LOGICAL_MINIMUM (0x1)
332 0x25, 0xb7, // LOGICAL_MAXIMUM (0xb7)
333 0x19, 0x01, // USAGE_MINIMUM (0x1)
334 0x29, 0xb7, // USAGE_MAXIMUM (0xb7)
335 0x75, 0x10, // REPORT_SIZE (16)
336 0x95, 0x01, // REPORT_COUNT (1)
337 0x81, 0x00, // INPUT (Data,Array,Abs)
338 0xc0, // END_COLLECTION
340 0x05, 0x0c, // USAGE_PAGE (Consumer Devices)
341 0x09, 0x01, // USAGE (Consumer Control)
342 0xa1, 0x01, // COLLECTION (Application)
343 0x85, REPORT_ID_CONSUMER, // REPORT_ID (3)
344 0x15, 0x01, // LOGICAL_MINIMUM (0x1)
345 0x26, 0x9c, 0x02, // LOGICAL_MAXIMUM (0x29c)
346 0x19, 0x01, // USAGE_MINIMUM (0x1)
347 0x2a, 0x9c, 0x02, // USAGE_MAXIMUM (0x29c)
348 0x75, 0x10, // REPORT_SIZE (16)
349 0x95, 0x01, // REPORT_COUNT (1)
350 0x81, 0x00, // INPUT (Data,Array,Abs)
351 0xc0, // END_COLLECTION
356 * Descriptor for compite device: Keyboard + Mouse
358 * contains: device, interface, HID and endpoint descriptors
360 #if USB_CFG_DESCR_PROPS_CONFIGURATION
361 PROGMEM char usbDescriptorConfiguration[] = { /* USB configuration descriptor */
362 9, /* sizeof(usbDescriptorConfiguration): length of descriptor in bytes */
363 USBDESCR_CONFIG, /* descriptor type */
364 9 + (9 + 9 + 7) + (9 + 9 + 7), 0,
365 //18 + 7 * USB_CFG_HAVE_INTRIN_ENDPOINT + 7 * USB_CFG_HAVE_INTRIN_ENDPOINT3 + 9, 0,
366 /* total length of data returned (including inlined descriptors) */
367 2, /* number of interfaces in this configuration */
368 1, /* index of this configuration */
369 0, /* configuration name string index */
370 #if USB_CFG_IS_SELF_POWERED
371 (1 << 7) | USBATTR_SELFPOWER, /* attributes */
373 (1 << 7), /* attributes */
375 USB_CFG_MAX_BUS_POWER/2, /* max USB current in 2mA units */
380 /* Interface descriptor */
381 9, /* sizeof(usbDescrInterface): length of descriptor in bytes */
382 USBDESCR_INTERFACE, /* descriptor type */
383 0, /* index of this interface */
384 0, /* alternate setting for this interface */
385 USB_CFG_HAVE_INTRIN_ENDPOINT, /* endpoints excl 0: number of endpoint descriptors to follow */
386 USB_CFG_INTERFACE_CLASS,
387 USB_CFG_INTERFACE_SUBCLASS,
388 USB_CFG_INTERFACE_PROTOCOL,
389 0, /* string index for interface */
391 9, /* sizeof(usbDescrHID): length of descriptor in bytes */
392 USBDESCR_HID, /* descriptor type: HID */
393 0x01, 0x01, /* BCD representation of HID version */
394 0x00, /* target country code */
395 0x01, /* number of HID Report (or other HID class) Descriptor infos to follow */
396 0x22, /* descriptor type: report */
397 sizeof(keyboard_hid_report), 0, /* total length of report descriptor */
398 /* Endpoint descriptor */
399 #if USB_CFG_HAVE_INTRIN_ENDPOINT /* endpoint descriptor for endpoint 1 */
400 7, /* sizeof(usbDescrEndpoint) */
401 USBDESCR_ENDPOINT, /* descriptor type = endpoint */
402 (char)0x81, /* IN endpoint number 1 */
403 0x03, /* attrib: Interrupt endpoint */
404 8, 0, /* maximum packet size */
405 USB_CFG_INTR_POLL_INTERVAL, /* in ms */
411 /* Interface descriptor */
412 9, /* sizeof(usbDescrInterface): length of descriptor in bytes */
413 USBDESCR_INTERFACE, /* descriptor type */
414 1, /* index of this interface */
415 0, /* alternate setting for this interface */
416 USB_CFG_HAVE_INTRIN_ENDPOINT3, /* endpoints excl 0: number of endpoint descriptors to follow */
417 0x03, /* CLASS: HID */
418 0, /* SUBCLASS: none */
419 0, /* PROTOCOL: none */
420 0, /* string index for interface */
422 9, /* sizeof(usbDescrHID): length of descriptor in bytes */
423 USBDESCR_HID, /* descriptor type: HID */
424 0x01, 0x01, /* BCD representation of HID version */
425 0x00, /* target country code */
426 0x01, /* number of HID Report (or other HID class) Descriptor infos to follow */
427 0x22, /* descriptor type: report */
428 sizeof(mouse_hid_report), 0, /* total length of report descriptor */
429 #if USB_CFG_HAVE_INTRIN_ENDPOINT3 /* endpoint descriptor for endpoint 3 */
430 /* Endpoint descriptor */
431 7, /* sizeof(usbDescrEndpoint) */
432 USBDESCR_ENDPOINT, /* descriptor type = endpoint */
433 (char)(0x80 | USB_CFG_EP3_NUMBER), /* IN endpoint number 3 */
434 0x03, /* attrib: Interrupt endpoint */
435 8, 0, /* maximum packet size */
436 USB_CFG_INTR_POLL_INTERVAL, /* in ms */
442 USB_PUBLIC usbMsgLen_t usbFunctionDescriptor(struct usbRequest *rq)
447 debug("usbFunctionDescriptor: ");
448 debug_hex(rq->bmRequestType); debug(" ");
449 debug_hex(rq->bRequest); debug(" ");
450 debug_hex16(rq->wValue.word); debug(" ");
451 debug_hex16(rq->wIndex.word); debug(" ");
452 debug_hex16(rq->wLength.word); debug("\n");
454 switch (rq->wValue.bytes[1]) {
455 #if USB_CFG_DESCR_PROPS_CONFIGURATION
456 case USBDESCR_CONFIG:
457 usbMsgPtr = (unsigned char *)usbDescriptorConfiguration;
458 len = sizeof(usbDescriptorConfiguration);
462 switch (rq->wValue.bytes[0]) {
464 usbMsgPtr = (unsigned char *)(usbDescriptorConfiguration + 9 + 9);
468 usbMsgPtr = (unsigned char *)(usbDescriptorConfiguration + 9 + (9 + 9 + 7) + 9);
473 case USBDESCR_HID_REPORT:
474 /* interface index */
475 switch (rq->wIndex.word) {
477 usbMsgPtr = keyboard_hid_report;
478 len = sizeof(keyboard_hid_report);
481 usbMsgPtr = mouse_hid_report;
482 len = sizeof(mouse_hid_report);
487 //debug("desc len: "); debug_hex(len); debug("\n");