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