]> git.gir.st - tmk_keyboard.git/blob - protocol/vusb/vusb.c
Add LUFA mouse feature and fix mouse report.
[tmk_keyboard.git] / protocol / vusb / vusb.c
1 /*
2 Copyright 2011 Jun Wako <wakojun@gmail.com>
3
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.
8
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.
13
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/>.
16 */
17
18 #include <stdint.h>
19 #include "usbdrv.h"
20 #include "usbconfig.h"
21 #include "host.h"
22 #include "report.h"
23 #include "print.h"
24 #include "debug.h"
25 #include "host_driver.h"
26 #include "vusb.h"
27
28
29 static uint8_t vusb_keyboard_leds = 0;
30 static uint8_t vusb_idle_rate = 0;
31
32 /* Keyboard report send buffer */
33 #define KBUF_SIZE 16
34 static report_keyboard_t kbuf[KBUF_SIZE];
35 static uint8_t kbuf_head = 0;
36 static uint8_t kbuf_tail = 0;
37
38
39 /* transfer keyboard report from buffer */
40 void vusb_transfer_keyboard(void)
41 {
42 if (usbInterruptIsReady()) {
43 if (kbuf_head != kbuf_tail) {
44 usbSetInterrupt((void *)&kbuf[kbuf_tail], sizeof(report_keyboard_t));
45 if (!debug_keyboard) {
46 print("keys: ");
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");
49 }
50 kbuf_tail = (kbuf_tail + 1) % KBUF_SIZE;
51 }
52 }
53 }
54
55
56 /*------------------------------------------------------------------*
57 * Host driver
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);
64
65 static host_driver_t driver = {
66 keyboard_leds,
67 send_keyboard,
68 send_mouse,
69 send_system,
70 send_consumer
71 };
72
73 host_driver_t *vusb_driver(void)
74 {
75 return &driver;
76 }
77
78 static uint8_t keyboard_leds(void) {
79 return vusb_keyboard_leds;
80 }
81
82 static void send_keyboard(report_keyboard_t *report)
83 {
84 uint8_t next = (kbuf_head + 1) % KBUF_SIZE;
85 if (next != kbuf_tail) {
86 kbuf[kbuf_head] = *report;
87 kbuf_head = next;
88 } else {
89 debug("kbuf: full\n");
90 }
91 }
92
93
94 typedef struct {
95 uint8_t report_id;
96 report_mouse_t report;
97 } __attribute__ ((packed)) vusb_mouse_report_t;
98
99 static void send_mouse(report_mouse_t *report)
100 {
101 vusb_mouse_report_t r = {
102 .report_id = REPORT_ID_MOUSE,
103 .report = *report
104 };
105 if (usbInterruptIsReady3()) {
106 usbSetInterrupt3((void *)&r, sizeof(vusb_mouse_report_t));
107 }
108 }
109
110 /*
111 typedef struct {
112 uint8_t report_id;
113 uint8_t data0;
114 uint8_t data1;
115 } __attribute__ ((packed)) vusb_system_report_t;
116 */
117
118 static void send_system(uint16_t data)
119 {
120 /*
121 // Not need static?
122 static uint8_t report[] = { REPORT_ID_SYSTEM, 0, 0 };
123 report[1] = data&0xFF;
124 report[2] = (data>>8)&0xFF;
125 */
126 /*
127 vusb_system_report_t r = {
128 .report_id = REPORT_ID_SYSTEM,
129 .data0 = data&0xFF,
130 .data1 = (data>>8)&0xFF
131 };
132 if (usbInterruptIsReady3()) {
133 usbSetInterrupt3((void *)&r, sizeof(vusb_system_report_t));
134 }
135 */
136 }
137
138 static void send_consumer(uint16_t data)
139 {
140 static uint16_t last_data = 0;
141 if (data == last_data) return;
142 last_data = data;
143
144 // Not need static?
145 static uint8_t report[] = { REPORT_ID_CONSUMER, 0, 0 };
146 report[1] = data&0xFF;
147 report[2] = (data>>8)&0xFF;
148 if (usbInterruptIsReady3()) {
149 usbSetInterrupt3((void *)&report, sizeof(report));
150 }
151 }
152
153
154
155 /*------------------------------------------------------------------*
156 * Request from host *
157 *------------------------------------------------------------------*/
158 static struct {
159 uint16_t len;
160 enum {
161 NONE,
162 SET_LED
163 } kind;
164 } last_req;
165
166 usbMsgLen_t usbFunctionSetup(uchar data[8])
167 {
168 usbRequest_t *rq = (void *)data;
169
170 if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS){ /* class request type */
171 if(rq->bRequest == USBRQ_HID_GET_REPORT){
172 debug("GET_REPORT:");
173 /* we only have one report type, so don't look at wValue */
174 usbMsgPtr = (void *)keyboard_report_prev;
175 return sizeof(*keyboard_report_prev);
176 }else if(rq->bRequest == USBRQ_HID_GET_IDLE){
177 debug("GET_IDLE: ");
178 //debug_hex(vusb_idle_rate);
179 usbMsgPtr = &vusb_idle_rate;
180 return 1;
181 }else if(rq->bRequest == USBRQ_HID_SET_IDLE){
182 vusb_idle_rate = rq->wValue.bytes[1];
183 debug("SET_IDLE: ");
184 debug_hex(vusb_idle_rate);
185 }else if(rq->bRequest == USBRQ_HID_SET_REPORT){
186 debug("SET_REPORT: ");
187 // Report Type: 0x02(Out)/ReportID: 0x00(none) && Interface: 0(keyboard)
188 if (rq->wValue.word == 0x0200 && rq->wIndex.word == 0) {
189 debug("SET_LED: ");
190 last_req.kind = SET_LED;
191 last_req.len = rq->wLength.word;
192 }
193 return USB_NO_MSG; // to get data in usbFunctionWrite
194 } else {
195 debug("UNKNOWN:");
196 }
197 }else{
198 debug("VENDOR:");
199 /* no vendor specific requests implemented */
200 }
201 debug("\n");
202 return 0; /* default for not implemented requests: return no data back to host */
203 }
204
205 uchar usbFunctionWrite(uchar *data, uchar len)
206 {
207 if (last_req.len == 0) {
208 return -1;
209 }
210 switch (last_req.kind) {
211 case SET_LED:
212 debug("SET_LED: ");
213 debug_hex(data[0]);
214 debug("\n");
215 vusb_keyboard_leds = data[0];
216 last_req.len = 0;
217 return 1;
218 break;
219 case NONE:
220 default:
221 return -1;
222 break;
223 }
224 return 1;
225 }
226
227
228
229 /*------------------------------------------------------------------*
230 * Descriptors *
231 *------------------------------------------------------------------*/
232
233 /*
234 * Report Descriptor for keyboard
235 *
236 * from an example in HID spec appendix
237 */
238 PROGMEM uchar keyboard_hid_report[] = {
239 0x05, 0x01, // Usage Page (Generic Desktop),
240 0x09, 0x06, // Usage (Keyboard),
241 0xA1, 0x01, // Collection (Application),
242 0x75, 0x01, // Report Size (1),
243 0x95, 0x08, // Report Count (8),
244 0x05, 0x07, // Usage Page (Key Codes),
245 0x19, 0xE0, // Usage Minimum (224),
246 0x29, 0xE7, // Usage Maximum (231),
247 0x15, 0x00, // Logical Minimum (0),
248 0x25, 0x01, // Logical Maximum (1),
249 0x81, 0x02, // Input (Data, Variable, Absolute), ;Modifier byte
250 0x95, 0x01, // Report Count (1),
251 0x75, 0x08, // Report Size (8),
252 0x81, 0x03, // Input (Constant), ;Reserved byte
253 0x95, 0x05, // Report Count (5),
254 0x75, 0x01, // Report Size (1),
255 0x05, 0x08, // Usage Page (LEDs),
256 0x19, 0x01, // Usage Minimum (1),
257 0x29, 0x05, // Usage Maximum (5),
258 0x91, 0x02, // Output (Data, Variable, Absolute), ;LED report
259 0x95, 0x01, // Report Count (1),
260 0x75, 0x03, // Report Size (3),
261 0x91, 0x03, // Output (Constant), ;LED report padding
262 0x95, 0x06, // Report Count (6),
263 0x75, 0x08, // Report Size (8),
264 0x15, 0x00, // Logical Minimum (0),
265 0x25, 0xFF, // Logical Maximum(255),
266 0x05, 0x07, // Usage Page (Key Codes),
267 0x19, 0x00, // Usage Minimum (0),
268 0x29, 0xFF, // Usage Maximum (255),
269 0x81, 0x00, // Input (Data, Array),
270 0xc0 // End Collection
271 };
272
273 /*
274 * Report Descriptor for mouse
275 *
276 * Mouse Protocol 1, HID 1.11 spec, Appendix B, page 59-60, with wheel extension
277 * http://www.microchip.com/forums/tm.aspx?high=&m=391435&mpage=1#391521
278 * http://www.keil.com/forum/15671/
279 * http://www.microsoft.com/whdc/device/input/wheel.mspx
280 */
281 PROGMEM uchar mouse_hid_report[] = {
282 /* mouse */
283 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
284 0x09, 0x02, // USAGE (Mouse)
285 0xa1, 0x01, // COLLECTION (Application)
286 0x85, REPORT_ID_MOUSE, // REPORT_ID (1)
287 0x09, 0x01, // USAGE (Pointer)
288 0xa1, 0x00, // COLLECTION (Physical)
289 // ---------------------------- Buttons
290 0x05, 0x09, // USAGE_PAGE (Button)
291 0x19, 0x01, // USAGE_MINIMUM (Button 1)
292 0x29, 0x05, // USAGE_MAXIMUM (Button 5)
293 0x15, 0x00, // LOGICAL_MINIMUM (0)
294 0x25, 0x01, // LOGICAL_MAXIMUM (1)
295 0x75, 0x01, // REPORT_SIZE (1)
296 0x95, 0x05, // REPORT_COUNT (5)
297 0x81, 0x02, // INPUT (Data,Var,Abs)
298 0x75, 0x03, // REPORT_SIZE (3)
299 0x95, 0x01, // REPORT_COUNT (1)
300 0x81, 0x03, // INPUT (Cnst,Var,Abs)
301 // ---------------------------- X,Y position
302 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
303 0x09, 0x30, // USAGE (X)
304 0x09, 0x31, // USAGE (Y)
305 0x15, 0x81, // LOGICAL_MINIMUM (-127)
306 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
307 0x75, 0x08, // REPORT_SIZE (8)
308 0x95, 0x02, // REPORT_COUNT (2)
309 0x81, 0x06, // INPUT (Data,Var,Rel)
310 // ---------------------------- Vertical wheel
311 0x09, 0x38, // USAGE (Wheel)
312 0x15, 0x81, // LOGICAL_MINIMUM (-127)
313 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
314 0x35, 0x00, // PHYSICAL_MINIMUM (0) - reset physical
315 0x45, 0x00, // PHYSICAL_MAXIMUM (0)
316 0x75, 0x08, // REPORT_SIZE (8)
317 0x95, 0x01, // REPORT_COUNT (1)
318 0x81, 0x06, // INPUT (Data,Var,Rel)
319 // ---------------------------- Horizontal wheel
320 0x05, 0x0c, // USAGE_PAGE (Consumer Devices)
321 0x0a, 0x38, 0x02, // USAGE (AC Pan)
322 0x15, 0x81, // LOGICAL_MINIMUM (-127)
323 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
324 0x75, 0x08, // REPORT_SIZE (8)
325 0x95, 0x01, // REPORT_COUNT (1)
326 0x81, 0x06, // INPUT (Data,Var,Rel)
327 0xc0, // END_COLLECTION
328 0xc0, // END_COLLECTION
329 /* system control */
330 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
331 0x09, 0x80, // USAGE (System Control)
332 0xa1, 0x01, // COLLECTION (Application)
333 0x85, REPORT_ID_SYSTEM, // REPORT_ID (2)
334 0x15, 0x01, // LOGICAL_MINIMUM (0x1)
335 0x25, 0xb7, // LOGICAL_MAXIMUM (0xb7)
336 0x19, 0x01, // USAGE_MINIMUM (0x1)
337 0x29, 0xb7, // USAGE_MAXIMUM (0xb7)
338 0x75, 0x10, // REPORT_SIZE (16)
339 0x95, 0x01, // REPORT_COUNT (1)
340 0x81, 0x00, // INPUT (Data,Array,Abs)
341 0xc0, // END_COLLECTION
342 /* consumer */
343 0x05, 0x0c, // USAGE_PAGE (Consumer Devices)
344 0x09, 0x01, // USAGE (Consumer Control)
345 0xa1, 0x01, // COLLECTION (Application)
346 0x85, REPORT_ID_CONSUMER, // REPORT_ID (3)
347 0x15, 0x01, // LOGICAL_MINIMUM (0x1)
348 0x26, 0x9c, 0x02, // LOGICAL_MAXIMUM (0x29c)
349 0x19, 0x01, // USAGE_MINIMUM (0x1)
350 0x2a, 0x9c, 0x02, // USAGE_MAXIMUM (0x29c)
351 0x75, 0x10, // REPORT_SIZE (16)
352 0x95, 0x01, // REPORT_COUNT (1)
353 0x81, 0x00, // INPUT (Data,Array,Abs)
354 0xc0, // END_COLLECTION
355 };
356
357
358 /*
359 * Descriptor for compite device: Keyboard + Mouse
360 *
361 * contains: device, interface, HID and endpoint descriptors
362 */
363 #if USB_CFG_DESCR_PROPS_CONFIGURATION
364 PROGMEM char usbDescriptorConfiguration[] = { /* USB configuration descriptor */
365 9, /* sizeof(usbDescriptorConfiguration): length of descriptor in bytes */
366 USBDESCR_CONFIG, /* descriptor type */
367 9 + (9 + 9 + 7) + (9 + 9 + 7), 0,
368 //18 + 7 * USB_CFG_HAVE_INTRIN_ENDPOINT + 7 * USB_CFG_HAVE_INTRIN_ENDPOINT3 + 9, 0,
369 /* total length of data returned (including inlined descriptors) */
370 2, /* number of interfaces in this configuration */
371 1, /* index of this configuration */
372 0, /* configuration name string index */
373 #if USB_CFG_IS_SELF_POWERED
374 (1 << 7) | USBATTR_SELFPOWER, /* attributes */
375 #else
376 (1 << 7), /* attributes */
377 #endif
378 USB_CFG_MAX_BUS_POWER/2, /* max USB current in 2mA units */
379
380 /*
381 * Keyboard interface
382 */
383 /* Interface descriptor */
384 9, /* sizeof(usbDescrInterface): length of descriptor in bytes */
385 USBDESCR_INTERFACE, /* descriptor type */
386 0, /* index of this interface */
387 0, /* alternate setting for this interface */
388 USB_CFG_HAVE_INTRIN_ENDPOINT, /* endpoints excl 0: number of endpoint descriptors to follow */
389 USB_CFG_INTERFACE_CLASS,
390 USB_CFG_INTERFACE_SUBCLASS,
391 USB_CFG_INTERFACE_PROTOCOL,
392 0, /* string index for interface */
393 /* HID descriptor */
394 9, /* sizeof(usbDescrHID): length of descriptor in bytes */
395 USBDESCR_HID, /* descriptor type: HID */
396 0x01, 0x01, /* BCD representation of HID version */
397 0x00, /* target country code */
398 0x01, /* number of HID Report (or other HID class) Descriptor infos to follow */
399 0x22, /* descriptor type: report */
400 sizeof(keyboard_hid_report), 0, /* total length of report descriptor */
401 /* Endpoint descriptor */
402 #if USB_CFG_HAVE_INTRIN_ENDPOINT /* endpoint descriptor for endpoint 1 */
403 7, /* sizeof(usbDescrEndpoint) */
404 USBDESCR_ENDPOINT, /* descriptor type = endpoint */
405 (char)0x81, /* IN endpoint number 1 */
406 0x03, /* attrib: Interrupt endpoint */
407 8, 0, /* maximum packet size */
408 USB_CFG_INTR_POLL_INTERVAL, /* in ms */
409 #endif
410
411 /*
412 * Mouse interface
413 */
414 /* Interface descriptor */
415 9, /* sizeof(usbDescrInterface): length of descriptor in bytes */
416 USBDESCR_INTERFACE, /* descriptor type */
417 1, /* index of this interface */
418 0, /* alternate setting for this interface */
419 USB_CFG_HAVE_INTRIN_ENDPOINT3, /* endpoints excl 0: number of endpoint descriptors to follow */
420 0x03, /* CLASS: HID */
421 0, /* SUBCLASS: none */
422 0, /* PROTOCOL: none */
423 0, /* string index for interface */
424 /* HID descriptor */
425 9, /* sizeof(usbDescrHID): length of descriptor in bytes */
426 USBDESCR_HID, /* descriptor type: HID */
427 0x01, 0x01, /* BCD representation of HID version */
428 0x00, /* target country code */
429 0x01, /* number of HID Report (or other HID class) Descriptor infos to follow */
430 0x22, /* descriptor type: report */
431 sizeof(mouse_hid_report), 0, /* total length of report descriptor */
432 #if USB_CFG_HAVE_INTRIN_ENDPOINT3 /* endpoint descriptor for endpoint 3 */
433 /* Endpoint descriptor */
434 7, /* sizeof(usbDescrEndpoint) */
435 USBDESCR_ENDPOINT, /* descriptor type = endpoint */
436 (char)(0x80 | USB_CFG_EP3_NUMBER), /* IN endpoint number 3 */
437 0x03, /* attrib: Interrupt endpoint */
438 8, 0, /* maximum packet size */
439 USB_CFG_INTR_POLL_INTERVAL, /* in ms */
440 #endif
441 };
442 #endif
443
444
445 USB_PUBLIC usbMsgLen_t usbFunctionDescriptor(struct usbRequest *rq)
446 {
447 usbMsgLen_t len = 0;
448
449 /*
450 debug("usbFunctionDescriptor: ");
451 debug_hex(rq->bmRequestType); debug(" ");
452 debug_hex(rq->bRequest); debug(" ");
453 debug_hex16(rq->wValue.word); debug(" ");
454 debug_hex16(rq->wIndex.word); debug(" ");
455 debug_hex16(rq->wLength.word); debug("\n");
456 */
457 switch (rq->wValue.bytes[1]) {
458 #if USB_CFG_DESCR_PROPS_CONFIGURATION
459 case USBDESCR_CONFIG:
460 usbMsgPtr = (unsigned char *)usbDescriptorConfiguration;
461 len = sizeof(usbDescriptorConfiguration);
462 break;
463 #endif
464 case USBDESCR_HID:
465 switch (rq->wValue.bytes[0]) {
466 case 0:
467 usbMsgPtr = (unsigned char *)(usbDescriptorConfiguration + 9 + 9);
468 len = 9;
469 break;
470 case 1:
471 usbMsgPtr = (unsigned char *)(usbDescriptorConfiguration + 9 + (9 + 9 + 7) + 9);
472 len = 9;
473 break;
474 }
475 break;
476 case USBDESCR_HID_REPORT:
477 /* interface index */
478 switch (rq->wIndex.word) {
479 case 0:
480 usbMsgPtr = keyboard_hid_report;
481 len = sizeof(keyboard_hid_report);
482 break;
483 case 1:
484 usbMsgPtr = mouse_hid_report;
485 len = sizeof(mouse_hid_report);
486 break;
487 }
488 break;
489 }
490 //debug("desc len: "); debug_hex(len); debug("\n");
491 return len;
492 }
Imprint / Impressum