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"
30 uint8_t keyboard_protocol=1;
31 uint8_t keyboard_idle = 0;
33 static uint8_t vusb_keyboard_leds = 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 *------------------------------------------------------------------*/
173 usbMsgLen_t usbFunctionSetup(uchar data[8])
175 usbRequest_t *rq = (void *)data;
177 if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS){ /* class request type */
178 if(rq->bRequest == USBRQ_HID_GET_REPORT){
179 debug("GET_REPORT:");
180 /* we only have one report type, so don't look at wValue */
181 usbMsgPtr = (void *)&keyboard_report;
182 return sizeof(keyboard_report);
183 }else if(rq->bRequest == USBRQ_HID_GET_IDLE){
185 //debug_hex(keyboard_idle);
186 usbMsgPtr = &keyboard_idle;
188 }else if(rq->bRequest == USBRQ_HID_SET_IDLE){
189 keyboard_idle = rq->wValue.bytes[1];
191 debug_hex(keyboard_idle);
192 }else if(rq->bRequest == USBRQ_HID_SET_REPORT){
193 debug("SET_REPORT: ");
194 // Report Type: 0x02(Out)/ReportID: 0x00(none) && Interface: 0(keyboard)
195 if (rq->wValue.word == 0x0200 && rq->wIndex.word == 0) {
197 last_req.kind = SET_LED;
198 last_req.len = rq->wLength.word;
200 return USB_NO_MSG; // to get data in usbFunctionWrite
206 /* no vendor specific requests implemented */
209 return 0; /* default for not implemented requests: return no data back to host */
212 uchar usbFunctionWrite(uchar *data, uchar len)
214 if (last_req.len == 0) {
217 switch (last_req.kind) {
222 vusb_keyboard_leds = data[0];
236 /*------------------------------------------------------------------*
238 *------------------------------------------------------------------*/
241 * Report Descriptor for keyboard
243 * from an example in HID spec appendix
245 const PROGMEM uchar keyboard_hid_report[] = {
246 0x05, 0x01, // Usage Page (Generic Desktop),
247 0x09, 0x06, // Usage (Keyboard),
248 0xA1, 0x01, // Collection (Application),
249 0x75, 0x01, // Report Size (1),
250 0x95, 0x08, // Report Count (8),
251 0x05, 0x07, // Usage Page (Key Codes),
252 0x19, 0xE0, // Usage Minimum (224),
253 0x29, 0xE7, // Usage Maximum (231),
254 0x15, 0x00, // Logical Minimum (0),
255 0x25, 0x01, // Logical Maximum (1),
256 0x81, 0x02, // Input (Data, Variable, Absolute), ;Modifier byte
257 0x95, 0x01, // Report Count (1),
258 0x75, 0x08, // Report Size (8),
259 0x81, 0x03, // Input (Constant), ;Reserved byte
260 0x95, 0x05, // Report Count (5),
261 0x75, 0x01, // Report Size (1),
262 0x05, 0x08, // Usage Page (LEDs),
263 0x19, 0x01, // Usage Minimum (1),
264 0x29, 0x05, // Usage Maximum (5),
265 0x91, 0x02, // Output (Data, Variable, Absolute), ;LED report
266 0x95, 0x01, // Report Count (1),
267 0x75, 0x03, // Report Size (3),
268 0x91, 0x03, // Output (Constant), ;LED report padding
269 0x95, 0x06, // Report Count (6),
270 0x75, 0x08, // Report Size (8),
271 0x15, 0x00, // Logical Minimum (0),
272 0x26, 0xFF, 0x00, // Logical Maximum(255),
273 0x05, 0x07, // Usage Page (Key Codes),
274 0x19, 0x00, // Usage Minimum (0),
275 0x29, 0xFF, // Usage Maximum (255),
276 0x81, 0x00, // Input (Data, Array),
277 0xc0 // End Collection
281 * Report Descriptor for mouse
283 * Mouse Protocol 1, HID 1.11 spec, Appendix B, page 59-60, with wheel extension
284 * http://www.microchip.com/forums/tm.aspx?high=&m=391435&mpage=1#391521
285 * http://www.keil.com/forum/15671/
286 * http://www.microsoft.com/whdc/device/input/wheel.mspx
288 const PROGMEM uchar mouse_hid_report[] = {
290 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
291 0x09, 0x02, // USAGE (Mouse)
292 0xa1, 0x01, // COLLECTION (Application)
293 0x85, REPORT_ID_MOUSE, // REPORT_ID (1)
294 0x09, 0x01, // USAGE (Pointer)
295 0xa1, 0x00, // COLLECTION (Physical)
296 // ---------------------------- Buttons
297 0x05, 0x09, // USAGE_PAGE (Button)
298 0x19, 0x01, // USAGE_MINIMUM (Button 1)
299 0x29, 0x05, // USAGE_MAXIMUM (Button 5)
300 0x15, 0x00, // LOGICAL_MINIMUM (0)
301 0x25, 0x01, // LOGICAL_MAXIMUM (1)
302 0x75, 0x01, // REPORT_SIZE (1)
303 0x95, 0x05, // REPORT_COUNT (5)
304 0x81, 0x02, // INPUT (Data,Var,Abs)
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 // ---------------------------- Vertical wheel
318 0x09, 0x38, // USAGE (Wheel)
319 0x15, 0x81, // LOGICAL_MINIMUM (-127)
320 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
321 0x35, 0x00, // PHYSICAL_MINIMUM (0) - reset physical
322 0x45, 0x00, // PHYSICAL_MAXIMUM (0)
323 0x75, 0x08, // REPORT_SIZE (8)
324 0x95, 0x01, // REPORT_COUNT (1)
325 0x81, 0x06, // INPUT (Data,Var,Rel)
326 // ---------------------------- Horizontal wheel
327 0x05, 0x0c, // USAGE_PAGE (Consumer Devices)
328 0x0a, 0x38, 0x02, // USAGE (AC Pan)
329 0x15, 0x81, // LOGICAL_MINIMUM (-127)
330 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
331 0x75, 0x08, // REPORT_SIZE (8)
332 0x95, 0x01, // REPORT_COUNT (1)
333 0x81, 0x06, // INPUT (Data,Var,Rel)
334 0xc0, // END_COLLECTION
335 0xc0, // END_COLLECTION
337 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
338 0x09, 0x80, // USAGE (System Control)
339 0xa1, 0x01, // COLLECTION (Application)
340 0x85, REPORT_ID_SYSTEM, // REPORT_ID (2)
341 0x15, 0x01, // LOGICAL_MINIMUM (0x1)
342 0x26, 0xb7, 0x00, // LOGICAL_MAXIMUM (0xb7)
343 0x19, 0x01, // USAGE_MINIMUM (0x1)
344 0x29, 0xb7, // USAGE_MAXIMUM (0xb7)
345 0x75, 0x10, // REPORT_SIZE (16)
346 0x95, 0x01, // REPORT_COUNT (1)
347 0x81, 0x00, // INPUT (Data,Array,Abs)
348 0xc0, // END_COLLECTION
350 0x05, 0x0c, // USAGE_PAGE (Consumer Devices)
351 0x09, 0x01, // USAGE (Consumer Control)
352 0xa1, 0x01, // COLLECTION (Application)
353 0x85, REPORT_ID_CONSUMER, // REPORT_ID (3)
354 0x15, 0x01, // LOGICAL_MINIMUM (0x1)
355 0x26, 0x9c, 0x02, // LOGICAL_MAXIMUM (0x29c)
356 0x19, 0x01, // USAGE_MINIMUM (0x1)
357 0x2a, 0x9c, 0x02, // USAGE_MAXIMUM (0x29c)
358 0x75, 0x10, // REPORT_SIZE (16)
359 0x95, 0x01, // REPORT_COUNT (1)
360 0x81, 0x00, // INPUT (Data,Array,Abs)
361 0xc0, // END_COLLECTION
366 * Descriptor for compite device: Keyboard + Mouse
368 * contains: device, interface, HID and endpoint descriptors
370 #if USB_CFG_DESCR_PROPS_CONFIGURATION
371 const PROGMEM char usbDescriptorConfiguration[] = { /* USB configuration descriptor */
372 9, /* sizeof(usbDescriptorConfiguration): length of descriptor in bytes */
373 USBDESCR_CONFIG, /* descriptor type */
374 9 + (9 + 9 + 7) + (9 + 9 + 7), 0,
375 //18 + 7 * USB_CFG_HAVE_INTRIN_ENDPOINT + 7 * USB_CFG_HAVE_INTRIN_ENDPOINT3 + 9, 0,
376 /* total length of data returned (including inlined descriptors) */
377 2, /* number of interfaces in this configuration */
378 1, /* index of this configuration */
379 0, /* configuration name string index */
380 #if USB_CFG_IS_SELF_POWERED
381 (1 << 7) | USBATTR_SELFPOWER, /* attributes */
383 (1 << 7), /* attributes */
385 USB_CFG_MAX_BUS_POWER/2, /* max USB current in 2mA units */
390 /* Interface descriptor */
391 9, /* sizeof(usbDescrInterface): length of descriptor in bytes */
392 USBDESCR_INTERFACE, /* descriptor type */
393 0, /* index of this interface */
394 0, /* alternate setting for this interface */
395 USB_CFG_HAVE_INTRIN_ENDPOINT, /* endpoints excl 0: number of endpoint descriptors to follow */
396 USB_CFG_INTERFACE_CLASS,
397 USB_CFG_INTERFACE_SUBCLASS,
398 USB_CFG_INTERFACE_PROTOCOL,
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(keyboard_hid_report), 0, /* total length of report descriptor */
408 /* Endpoint descriptor */
409 #if USB_CFG_HAVE_INTRIN_ENDPOINT /* endpoint descriptor for endpoint 1 */
410 7, /* sizeof(usbDescrEndpoint) */
411 USBDESCR_ENDPOINT, /* descriptor type = endpoint */
412 (char)0x81, /* IN endpoint number 1 */
413 0x03, /* attrib: Interrupt endpoint */
414 8, 0, /* maximum packet size */
415 USB_CFG_INTR_POLL_INTERVAL, /* in ms */
421 /* Interface descriptor */
422 9, /* sizeof(usbDescrInterface): length of descriptor in bytes */
423 USBDESCR_INTERFACE, /* descriptor type */
424 1, /* index of this interface */
425 0, /* alternate setting for this interface */
426 USB_CFG_HAVE_INTRIN_ENDPOINT3, /* endpoints excl 0: number of endpoint descriptors to follow */
427 0x03, /* CLASS: HID */
428 0, /* SUBCLASS: none */
429 0, /* PROTOCOL: none */
430 0, /* string index for interface */
432 9, /* sizeof(usbDescrHID): length of descriptor in bytes */
433 USBDESCR_HID, /* descriptor type: HID */
434 0x01, 0x01, /* BCD representation of HID version */
435 0x00, /* target country code */
436 0x01, /* number of HID Report (or other HID class) Descriptor infos to follow */
437 0x22, /* descriptor type: report */
438 sizeof(mouse_hid_report), 0, /* total length of report descriptor */
439 #if USB_CFG_HAVE_INTRIN_ENDPOINT3 /* endpoint descriptor for endpoint 3 */
440 /* Endpoint descriptor */
441 7, /* sizeof(usbDescrEndpoint) */
442 USBDESCR_ENDPOINT, /* descriptor type = endpoint */
443 (char)(0x80 | USB_CFG_EP3_NUMBER), /* IN endpoint number 3 */
444 0x03, /* attrib: Interrupt endpoint */
445 8, 0, /* maximum packet size */
446 USB_CFG_INTR_POLL_INTERVAL, /* in ms */
452 USB_PUBLIC usbMsgLen_t usbFunctionDescriptor(struct usbRequest *rq)
457 debug("usbFunctionDescriptor: ");
458 debug_hex(rq->bmRequestType); debug(" ");
459 debug_hex(rq->bRequest); debug(" ");
460 debug_hex16(rq->wValue.word); debug(" ");
461 debug_hex16(rq->wIndex.word); debug(" ");
462 debug_hex16(rq->wLength.word); debug("\n");
464 switch (rq->wValue.bytes[1]) {
465 #if USB_CFG_DESCR_PROPS_CONFIGURATION
466 case USBDESCR_CONFIG:
467 usbMsgPtr = (unsigned char *)usbDescriptorConfiguration;
468 len = sizeof(usbDescriptorConfiguration);
472 switch (rq->wValue.bytes[0]) {
474 usbMsgPtr = (unsigned char *)(usbDescriptorConfiguration + 9 + 9);
478 usbMsgPtr = (unsigned char *)(usbDescriptorConfiguration + 9 + (9 + 9 + 7) + 9);
483 case USBDESCR_HID_REPORT:
484 /* interface index */
485 switch (rq->wIndex.word) {
487 usbMsgPtr = keyboard_hid_report;
488 len = sizeof(keyboard_hid_report);
491 usbMsgPtr = mouse_hid_report;
492 len = sizeof(mouse_hid_report);
497 //debug("desc len: "); debug_hex(len); debug("\n");