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 if (!debug_keyboard) {
47 for (int i = 0; i < REPORT_KEYS; i++) { phex(kbuf[kbuf_tail].keys[i]); print(" "); }
48 print(" mods: "); phex((kbuf[kbuf_tail]).mods); print("\n");
50 kbuf_tail = (kbuf_tail + 1) % KBUF_SIZE;
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");
96 report_mouse_t report;
97 } __attribute__ ((packed)) vusb_mouse_report_t;
99 static void send_mouse(report_mouse_t *report)
101 vusb_mouse_report_t r = {
102 .report_id = REPORT_ID_MOUSE,
105 if (usbInterruptIsReady3()) {
106 usbSetInterrupt3((void *)&r, sizeof(vusb_mouse_report_t));
114 } __attribute__ ((packed)) report_extra_t;
116 static void send_system(uint16_t data)
118 static uint16_t last_data = 0;
119 if (data == last_data) return;
122 report_extra_t report = {
123 .report_id = REPORT_ID_SYSTEM,
126 if (usbInterruptIsReady3()) {
127 usbSetInterrupt3((void *)&report, sizeof(report));
131 static void send_consumer(uint16_t data)
133 static uint16_t last_data = 0;
134 if (data == last_data) return;
137 report_extra_t report = {
138 .report_id = REPORT_ID_CONSUMER,
141 if (usbInterruptIsReady3()) {
142 usbSetInterrupt3((void *)&report, sizeof(report));
148 /*------------------------------------------------------------------*
149 * Request from host *
150 *------------------------------------------------------------------*/
159 usbMsgLen_t usbFunctionSetup(uchar data[8])
161 usbRequest_t *rq = (void *)data;
163 if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS){ /* class request type */
164 if(rq->bRequest == USBRQ_HID_GET_REPORT){
165 debug("GET_REPORT:");
166 /* we only have one report type, so don't look at wValue */
167 usbMsgPtr = (void *)keyboard_report_prev;
168 return sizeof(*keyboard_report_prev);
169 }else if(rq->bRequest == USBRQ_HID_GET_IDLE){
171 //debug_hex(vusb_idle_rate);
172 usbMsgPtr = &vusb_idle_rate;
174 }else if(rq->bRequest == USBRQ_HID_SET_IDLE){
175 vusb_idle_rate = rq->wValue.bytes[1];
177 debug_hex(vusb_idle_rate);
178 }else if(rq->bRequest == USBRQ_HID_SET_REPORT){
179 debug("SET_REPORT: ");
180 // Report Type: 0x02(Out)/ReportID: 0x00(none) && Interface: 0(keyboard)
181 if (rq->wValue.word == 0x0200 && rq->wIndex.word == 0) {
183 last_req.kind = SET_LED;
184 last_req.len = rq->wLength.word;
186 return USB_NO_MSG; // to get data in usbFunctionWrite
192 /* no vendor specific requests implemented */
195 return 0; /* default for not implemented requests: return no data back to host */
198 uchar usbFunctionWrite(uchar *data, uchar len)
200 if (last_req.len == 0) {
203 switch (last_req.kind) {
208 vusb_keyboard_leds = data[0];
222 /*------------------------------------------------------------------*
224 *------------------------------------------------------------------*/
227 * Report Descriptor for keyboard
229 * from an example in HID spec appendix
231 PROGMEM uchar keyboard_hid_report[] = {
232 0x05, 0x01, // Usage Page (Generic Desktop),
233 0x09, 0x06, // Usage (Keyboard),
234 0xA1, 0x01, // Collection (Application),
235 0x75, 0x01, // Report Size (1),
236 0x95, 0x08, // Report Count (8),
237 0x05, 0x07, // Usage Page (Key Codes),
238 0x19, 0xE0, // Usage Minimum (224),
239 0x29, 0xE7, // Usage Maximum (231),
240 0x15, 0x00, // Logical Minimum (0),
241 0x25, 0x01, // Logical Maximum (1),
242 0x81, 0x02, // Input (Data, Variable, Absolute), ;Modifier byte
243 0x95, 0x01, // Report Count (1),
244 0x75, 0x08, // Report Size (8),
245 0x81, 0x03, // Input (Constant), ;Reserved byte
246 0x95, 0x05, // Report Count (5),
247 0x75, 0x01, // Report Size (1),
248 0x05, 0x08, // Usage Page (LEDs),
249 0x19, 0x01, // Usage Minimum (1),
250 0x29, 0x05, // Usage Maximum (5),
251 0x91, 0x02, // Output (Data, Variable, Absolute), ;LED report
252 0x95, 0x01, // Report Count (1),
253 0x75, 0x03, // Report Size (3),
254 0x91, 0x03, // Output (Constant), ;LED report padding
255 0x95, 0x06, // Report Count (6),
256 0x75, 0x08, // Report Size (8),
257 0x15, 0x00, // Logical Minimum (0),
258 0x25, 0xFF, // Logical Maximum(255),
259 0x05, 0x07, // Usage Page (Key Codes),
260 0x19, 0x00, // Usage Minimum (0),
261 0x29, 0xFF, // Usage Maximum (255),
262 0x81, 0x00, // Input (Data, Array),
263 0xc0 // End Collection
267 * Report Descriptor for mouse
269 * Mouse Protocol 1, HID 1.11 spec, Appendix B, page 59-60, with wheel extension
270 * http://www.microchip.com/forums/tm.aspx?high=&m=391435&mpage=1#391521
271 * http://www.keil.com/forum/15671/
272 * http://www.microsoft.com/whdc/device/input/wheel.mspx
274 PROGMEM uchar mouse_hid_report[] = {
276 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
277 0x09, 0x02, // USAGE (Mouse)
278 0xa1, 0x01, // COLLECTION (Application)
279 0x85, REPORT_ID_MOUSE, // REPORT_ID (1)
280 0x09, 0x01, // USAGE (Pointer)
281 0xa1, 0x00, // COLLECTION (Physical)
282 // ---------------------------- Buttons
283 0x05, 0x09, // USAGE_PAGE (Button)
284 0x19, 0x01, // USAGE_MINIMUM (Button 1)
285 0x29, 0x05, // USAGE_MAXIMUM (Button 5)
286 0x15, 0x00, // LOGICAL_MINIMUM (0)
287 0x25, 0x01, // LOGICAL_MAXIMUM (1)
288 0x75, 0x01, // REPORT_SIZE (1)
289 0x95, 0x05, // REPORT_COUNT (5)
290 0x81, 0x02, // INPUT (Data,Var,Abs)
291 0x75, 0x03, // REPORT_SIZE (3)
292 0x95, 0x01, // REPORT_COUNT (1)
293 0x81, 0x03, // INPUT (Cnst,Var,Abs)
294 // ---------------------------- X,Y position
295 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
296 0x09, 0x30, // USAGE (X)
297 0x09, 0x31, // USAGE (Y)
298 0x15, 0x81, // LOGICAL_MINIMUM (-127)
299 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
300 0x75, 0x08, // REPORT_SIZE (8)
301 0x95, 0x02, // REPORT_COUNT (2)
302 0x81, 0x06, // INPUT (Data,Var,Rel)
303 // ---------------------------- Vertical wheel
304 0x09, 0x38, // USAGE (Wheel)
305 0x15, 0x81, // LOGICAL_MINIMUM (-127)
306 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
307 0x35, 0x00, // PHYSICAL_MINIMUM (0) - reset physical
308 0x45, 0x00, // PHYSICAL_MAXIMUM (0)
309 0x75, 0x08, // REPORT_SIZE (8)
310 0x95, 0x01, // REPORT_COUNT (1)
311 0x81, 0x06, // INPUT (Data,Var,Rel)
312 // ---------------------------- Horizontal wheel
313 0x05, 0x0c, // USAGE_PAGE (Consumer Devices)
314 0x0a, 0x38, 0x02, // USAGE (AC Pan)
315 0x15, 0x81, // LOGICAL_MINIMUM (-127)
316 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
317 0x75, 0x08, // REPORT_SIZE (8)
318 0x95, 0x01, // REPORT_COUNT (1)
319 0x81, 0x06, // INPUT (Data,Var,Rel)
320 0xc0, // END_COLLECTION
321 0xc0, // END_COLLECTION
323 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
324 0x09, 0x80, // USAGE (System Control)
325 0xa1, 0x01, // COLLECTION (Application)
326 0x85, REPORT_ID_SYSTEM, // REPORT_ID (2)
327 0x15, 0x01, // LOGICAL_MINIMUM (0x1)
328 0x25, 0xb7, // LOGICAL_MAXIMUM (0xb7)
329 0x19, 0x01, // USAGE_MINIMUM (0x1)
330 0x29, 0xb7, // USAGE_MAXIMUM (0xb7)
331 0x75, 0x10, // REPORT_SIZE (16)
332 0x95, 0x01, // REPORT_COUNT (1)
333 0x81, 0x00, // INPUT (Data,Array,Abs)
334 0xc0, // END_COLLECTION
336 0x05, 0x0c, // USAGE_PAGE (Consumer Devices)
337 0x09, 0x01, // USAGE (Consumer Control)
338 0xa1, 0x01, // COLLECTION (Application)
339 0x85, REPORT_ID_CONSUMER, // REPORT_ID (3)
340 0x15, 0x01, // LOGICAL_MINIMUM (0x1)
341 0x26, 0x9c, 0x02, // LOGICAL_MAXIMUM (0x29c)
342 0x19, 0x01, // USAGE_MINIMUM (0x1)
343 0x2a, 0x9c, 0x02, // USAGE_MAXIMUM (0x29c)
344 0x75, 0x10, // REPORT_SIZE (16)
345 0x95, 0x01, // REPORT_COUNT (1)
346 0x81, 0x00, // INPUT (Data,Array,Abs)
347 0xc0, // END_COLLECTION
352 * Descriptor for compite device: Keyboard + Mouse
354 * contains: device, interface, HID and endpoint descriptors
356 #if USB_CFG_DESCR_PROPS_CONFIGURATION
357 PROGMEM char usbDescriptorConfiguration[] = { /* USB configuration descriptor */
358 9, /* sizeof(usbDescriptorConfiguration): length of descriptor in bytes */
359 USBDESCR_CONFIG, /* descriptor type */
360 9 + (9 + 9 + 7) + (9 + 9 + 7), 0,
361 //18 + 7 * USB_CFG_HAVE_INTRIN_ENDPOINT + 7 * USB_CFG_HAVE_INTRIN_ENDPOINT3 + 9, 0,
362 /* total length of data returned (including inlined descriptors) */
363 2, /* number of interfaces in this configuration */
364 1, /* index of this configuration */
365 0, /* configuration name string index */
366 #if USB_CFG_IS_SELF_POWERED
367 (1 << 7) | USBATTR_SELFPOWER, /* attributes */
369 (1 << 7), /* attributes */
371 USB_CFG_MAX_BUS_POWER/2, /* max USB current in 2mA units */
376 /* Interface descriptor */
377 9, /* sizeof(usbDescrInterface): length of descriptor in bytes */
378 USBDESCR_INTERFACE, /* descriptor type */
379 0, /* index of this interface */
380 0, /* alternate setting for this interface */
381 USB_CFG_HAVE_INTRIN_ENDPOINT, /* endpoints excl 0: number of endpoint descriptors to follow */
382 USB_CFG_INTERFACE_CLASS,
383 USB_CFG_INTERFACE_SUBCLASS,
384 USB_CFG_INTERFACE_PROTOCOL,
385 0, /* string index for interface */
387 9, /* sizeof(usbDescrHID): length of descriptor in bytes */
388 USBDESCR_HID, /* descriptor type: HID */
389 0x01, 0x01, /* BCD representation of HID version */
390 0x00, /* target country code */
391 0x01, /* number of HID Report (or other HID class) Descriptor infos to follow */
392 0x22, /* descriptor type: report */
393 sizeof(keyboard_hid_report), 0, /* total length of report descriptor */
394 /* Endpoint descriptor */
395 #if USB_CFG_HAVE_INTRIN_ENDPOINT /* endpoint descriptor for endpoint 1 */
396 7, /* sizeof(usbDescrEndpoint) */
397 USBDESCR_ENDPOINT, /* descriptor type = endpoint */
398 (char)0x81, /* IN endpoint number 1 */
399 0x03, /* attrib: Interrupt endpoint */
400 8, 0, /* maximum packet size */
401 USB_CFG_INTR_POLL_INTERVAL, /* in ms */
407 /* Interface descriptor */
408 9, /* sizeof(usbDescrInterface): length of descriptor in bytes */
409 USBDESCR_INTERFACE, /* descriptor type */
410 1, /* index of this interface */
411 0, /* alternate setting for this interface */
412 USB_CFG_HAVE_INTRIN_ENDPOINT3, /* endpoints excl 0: number of endpoint descriptors to follow */
413 0x03, /* CLASS: HID */
414 0, /* SUBCLASS: none */
415 0, /* PROTOCOL: none */
416 0, /* string index for interface */
418 9, /* sizeof(usbDescrHID): length of descriptor in bytes */
419 USBDESCR_HID, /* descriptor type: HID */
420 0x01, 0x01, /* BCD representation of HID version */
421 0x00, /* target country code */
422 0x01, /* number of HID Report (or other HID class) Descriptor infos to follow */
423 0x22, /* descriptor type: report */
424 sizeof(mouse_hid_report), 0, /* total length of report descriptor */
425 #if USB_CFG_HAVE_INTRIN_ENDPOINT3 /* endpoint descriptor for endpoint 3 */
426 /* Endpoint descriptor */
427 7, /* sizeof(usbDescrEndpoint) */
428 USBDESCR_ENDPOINT, /* descriptor type = endpoint */
429 (char)(0x80 | USB_CFG_EP3_NUMBER), /* IN endpoint number 3 */
430 0x03, /* attrib: Interrupt endpoint */
431 8, 0, /* maximum packet size */
432 USB_CFG_INTR_POLL_INTERVAL, /* in ms */
438 USB_PUBLIC usbMsgLen_t usbFunctionDescriptor(struct usbRequest *rq)
443 debug("usbFunctionDescriptor: ");
444 debug_hex(rq->bmRequestType); debug(" ");
445 debug_hex(rq->bRequest); debug(" ");
446 debug_hex16(rq->wValue.word); debug(" ");
447 debug_hex16(rq->wIndex.word); debug(" ");
448 debug_hex16(rq->wLength.word); debug("\n");
450 switch (rq->wValue.bytes[1]) {
451 #if USB_CFG_DESCR_PROPS_CONFIGURATION
452 case USBDESCR_CONFIG:
453 usbMsgPtr = (unsigned char *)usbDescriptorConfiguration;
454 len = sizeof(usbDescriptorConfiguration);
458 switch (rq->wValue.bytes[0]) {
460 usbMsgPtr = (unsigned char *)(usbDescriptorConfiguration + 9 + 9);
464 usbMsgPtr = (unsigned char *)(usbDescriptorConfiguration + 9 + (9 + 9 + 7) + 9);
469 case USBDESCR_HID_REPORT:
470 /* interface index */
471 switch (rq->wIndex.word) {
473 usbMsgPtr = keyboard_hid_report;
474 len = sizeof(keyboard_hid_report);
477 usbMsgPtr = mouse_hid_report;
478 len = sizeof(mouse_hid_report);
483 //debug("desc len: "); debug_hex(len); debug("\n");