]> git.gir.st - tmk_keyboard.git/blob - vusb/host.c
added initial V-USB support for HHKB
[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 while (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 void host_mouse_send(report_mouse_t *report)
137 {
138 if (usbInterruptIsReady3()) {
139 usbSetInterrupt3((void *)report, sizeof(*report));
140 } else {
141 debug("Int3 not ready\n");
142 }
143 }
144
145
146
147 /*------------------------------------------------------------------*
148 * Request from host *
149 *------------------------------------------------------------------*/
150 static struct {
151 uint16_t len;
152 enum {
153 NONE,
154 SET_LED
155 } kind;
156 } last_req;
157
158 usbMsgLen_t usbFunctionSetup(uchar data[8])
159 {
160 usbRequest_t *rq = (void *)data;
161
162 if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS){ /* class request type */
163 if(rq->bRequest == USBRQ_HID_GET_REPORT){
164 debug(" GET_REPORT");
165 /* we only have one report type, so don't look at wValue */
166 usbMsgPtr = (void *)keyboard_report_prev;
167 return sizeof(*keyboard_report_prev);
168 }else if(rq->bRequest == USBRQ_HID_GET_IDLE){
169 debug(" GET_IDLE: ");
170 debug_hex(idleRate);
171 usbMsgPtr = &idleRate;
172 return 1;
173 }else if(rq->bRequest == USBRQ_HID_SET_IDLE){
174 idleRate = rq->wValue.bytes[1];
175 debug(" SET_IDLE: ");
176 debug_hex(idleRate);
177 }else if(rq->bRequest == USBRQ_HID_SET_REPORT){
178 //debug(" SET_REPORT: ");
179 if (rq->wValue.word == 0x0200 && rq->wIndex.word == 0) {
180 last_req.kind = SET_LED;
181 last_req.len = rq->wLength.word;
182 }
183 return USB_NO_MSG; // to get data in usbFunctionWrite
184 }
185 debug("\n");
186 }else{
187 debug("VENDOR\n");
188 /* no vendor specific requests implemented */
189 }
190 return 0; /* default for not implemented requests: return no data back to host */
191 }
192
193 uchar usbFunctionWrite(uchar *data, uchar len)
194 {
195 if (last_req.len == 0) {
196 return -1;
197 }
198 switch (last_req.kind) {
199 case SET_LED:
200 //debug("SET_LED\n");
201 keyboard_leds = data[0];
202 last_req.len = 0;
203 return 1;
204 break;
205 case NONE:
206 default:
207 return -1;
208 break;
209 }
210 return 1;
211 }
212
213
214
215 /*------------------------------------------------------------------*
216 * Descriptors *
217 *------------------------------------------------------------------*/
218
219 /*
220 * Report Descriptor for keyboard
221 *
222 * from an example in HID spec appendix
223 */
224 PROGMEM uchar keyboard_hid_report[] = {
225 0x05, 0x01, // Usage Page (Generic Desktop),
226 0x09, 0x06, // Usage (Keyboard),
227 0xA1, 0x01, // Collection (Application),
228 0x75, 0x01, // Report Size (1),
229 0x95, 0x08, // Report Count (8),
230 0x05, 0x07, // Usage Page (Key Codes),
231 0x19, 0xE0, // Usage Minimum (224),
232 0x29, 0xE7, // Usage Maximum (231),
233 0x15, 0x00, // Logical Minimum (0),
234 0x25, 0x01, // Logical Maximum (1),
235 0x81, 0x02, // Input (Data, Variable, Absolute), ;Modifier byte
236 0x95, 0x01, // Report Count (1),
237 0x75, 0x08, // Report Size (8),
238 0x81, 0x03, // Input (Constant), ;Reserved byte
239 0x95, 0x05, // Report Count (5),
240 0x75, 0x01, // Report Size (1),
241 0x05, 0x08, // Usage Page (LEDs),
242 0x19, 0x01, // Usage Minimum (1),
243 0x29, 0x05, // Usage Maximum (5),
244 0x91, 0x02, // Output (Data, Variable, Absolute), ;LED report
245 0x95, 0x01, // Report Count (1),
246 0x75, 0x03, // Report Size (3),
247 0x91, 0x03, // Output (Constant), ;LED report padding
248 0x95, 0x06, // Report Count (6),
249 0x75, 0x08, // Report Size (8),
250 0x15, 0x00, // Logical Minimum (0),
251 0x25, 0xFF, // Logical Maximum(255),
252 0x05, 0x07, // Usage Page (Key Codes),
253 0x19, 0x00, // Usage Minimum (0),
254 0x29, 0xFF, // Usage Maximum (255),
255 0x81, 0x00, // Input (Data, Array),
256 0xc0 // End Collection
257 };
258
259 /*
260 * Report Descriptor for mouse
261 *
262 * Mouse Protocol 1, HID 1.11 spec, Appendix B, page 59-60, with wheel extension
263 * http://www.microchip.com/forums/tm.aspx?high=&m=391435&mpage=1#391521
264 * http://www.keil.com/forum/15671/
265 * http://www.microsoft.com/whdc/device/input/wheel.mspx
266 */
267 PROGMEM uchar mouse_hid_report[] = {
268 /* from HID 1.11 spec example */
269 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
270 0x09, 0x02, // USAGE (Mouse)
271 0xa1, 0x01, // COLLECTION (Application)
272 0x09, 0x02, // USAGE (Mouse)
273 0xa1, 0x02, // COLLECTION (Logical)
274 0x09, 0x01, // USAGE (Pointer)
275 0xa1, 0x00, // COLLECTION (Physical)
276 // ------------------------------ Buttons
277 0x05, 0x09, // USAGE_PAGE (Button)
278 0x19, 0x01, // USAGE_MINIMUM (Button 1)
279 0x29, 0x05, // USAGE_MAXIMUM (Button 5)
280 0x15, 0x00, // LOGICAL_MINIMUM (0)
281 0x25, 0x01, // LOGICAL_MAXIMUM (1)
282 0x75, 0x01, // REPORT_SIZE (1)
283 0x95, 0x05, // REPORT_COUNT (5)
284 0x81, 0x02, // INPUT (Data,Var,Abs)
285 // ------------------------------ Padding
286 0x75, 0x03, // REPORT_SIZE (3)
287 0x95, 0x01, // REPORT_COUNT (1)
288 0x81, 0x03, // INPUT (Cnst,Var,Abs)
289 // ------------------------------ X,Y position
290 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
291 0x09, 0x30, // USAGE (X)
292 0x09, 0x31, // USAGE (Y)
293 0x15, 0x81, // LOGICAL_MINIMUM (-127)
294 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
295 0x75, 0x08, // REPORT_SIZE (8)
296 0x95, 0x02, // REPORT_COUNT (2)
297 0x81, 0x06, // INPUT (Data,Var,Rel)
298 0xa1, 0x02, // COLLECTION (Logical)
299 // ------------------------------ Vertical wheel res multiplier
300 0x09, 0x48, // USAGE (Resolution Multiplier)
301 0x15, 0x00, // LOGICAL_MINIMUM (0)
302 0x25, 0x01, // LOGICAL_MAXIMUM (1)
303 0x35, 0x01, // PHYSICAL_MINIMUM (1)
304 0x45, 0x04, // PHYSICAL_MAXIMUM (4)
305 0x75, 0x02, // REPORT_SIZE (2)
306 0x95, 0x01, // REPORT_COUNT (1)
307 0xa4, // PUSH
308 0xb1, 0x02, // FEATURE (Data,Var,Abs)
309 // ------------------------------ Vertical wheel
310 0x09, 0x38, // USAGE (Wheel)
311 0x15, 0x81, // LOGICAL_MINIMUM (-127)
312 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
313 0x35, 0x00, // PHYSICAL_MINIMUM (0) - reset physical
314 0x45, 0x00, // PHYSICAL_MAXIMUM (0)
315 0x75, 0x08, // REPORT_SIZE (8)
316 0x81, 0x06, // INPUT (Data,Var,Rel)
317 0xc0, // END_COLLECTION
318 0xa1, 0x02, // COLLECTION (Logical)
319 // ------------------------------ Horizontal wheel res multiplier
320 0x09, 0x48, // USAGE (Resolution Multiplier)
321 0xb4, // POP
322 0xb1, 0x02, // FEATURE (Data,Var,Abs)
323 // ------------------------------ Padding for Feature report
324 0x35, 0x00, // PHYSICAL_MINIMUM (0) - reset physical
325 0x45, 0x00, // PHYSICAL_MAXIMUM (0)
326 0x75, 0x04, // REPORT_SIZE (4)
327 0xb1, 0x03, // FEATURE (Cnst,Var,Abs)
328 // ------------------------------ Horizontal wheel
329 0x05, 0x0c, // USAGE_PAGE (Consumer Devices)
330 0x0a, 0x38, 0x02, // USAGE (AC Pan)
331 0x15, 0x81, // LOGICAL_MINIMUM (-127)
332 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
333 0x75, 0x08, // REPORT_SIZE (8)
334 0x81, 0x06, // INPUT (Data,Var,Rel)
335 0xc0, // END_COLLECTION
336 0xc0, // END_COLLECTION
337 0xc0, // END_COLLECTION
338 0xc0 // END_COLLECTION
339 };
340
341
342 /*
343 * Descriptor for compite device: Keyboard + Mouse
344 *
345 * contains: device, interface, HID and endpoint descriptors
346 */
347 #if USB_CFG_DESCR_PROPS_CONFIGURATION
348 PROGMEM char usbDescriptorConfiguration[] = { /* USB configuration descriptor */
349 9, /* sizeof(usbDescriptorConfiguration): length of descriptor in bytes */
350 USBDESCR_CONFIG, /* descriptor type */
351 9 + (9 + 9 + 7) + (9 + 9 + 7), 0,
352 //18 + 7 * USB_CFG_HAVE_INTRIN_ENDPOINT + 7 * USB_CFG_HAVE_INTRIN_ENDPOINT3 + 9, 0,
353 /* total length of data returned (including inlined descriptors) */
354 2, /* number of interfaces in this configuration */
355 1, /* index of this configuration */
356 0, /* configuration name string index */
357 #if USB_CFG_IS_SELF_POWERED
358 (1 << 7) | USBATTR_SELFPOWER, /* attributes */
359 #else
360 (1 << 7), /* attributes */
361 #endif
362 USB_CFG_MAX_BUS_POWER/2, /* max USB current in 2mA units */
363
364 /*
365 * Keyboard interface
366 */
367 /* Interface descriptor */
368 9, /* sizeof(usbDescrInterface): length of descriptor in bytes */
369 USBDESCR_INTERFACE, /* descriptor type */
370 0, /* index of this interface */
371 0, /* alternate setting for this interface */
372 USB_CFG_HAVE_INTRIN_ENDPOINT, /* endpoints excl 0: number of endpoint descriptors to follow */
373 USB_CFG_INTERFACE_CLASS,
374 USB_CFG_INTERFACE_SUBCLASS,
375 USB_CFG_INTERFACE_PROTOCOL,
376 0, /* string index for interface */
377 /* HID descriptor */
378 9, /* sizeof(usbDescrHID): length of descriptor in bytes */
379 USBDESCR_HID, /* descriptor type: HID */
380 0x01, 0x01, /* BCD representation of HID version */
381 0x00, /* target country code */
382 0x01, /* number of HID Report (or other HID class) Descriptor infos to follow */
383 0x22, /* descriptor type: report */
384 sizeof(keyboard_hid_report), 0, /* total length of report descriptor */
385 /* Endpoint descriptor */
386 #if USB_CFG_HAVE_INTRIN_ENDPOINT /* endpoint descriptor for endpoint 1 */
387 7, /* sizeof(usbDescrEndpoint) */
388 USBDESCR_ENDPOINT, /* descriptor type = endpoint */
389 (char)0x81, /* IN endpoint number 1 */
390 0x03, /* attrib: Interrupt endpoint */
391 8, 0, /* maximum packet size */
392 USB_CFG_INTR_POLL_INTERVAL, /* in ms */
393 #endif
394
395 /*
396 * Mouse interface
397 */
398 /* Interface descriptor */
399 9, /* sizeof(usbDescrInterface): length of descriptor in bytes */
400 USBDESCR_INTERFACE, /* descriptor type */
401 1, /* index of this interface */
402 0, /* alternate setting for this interface */
403 USB_CFG_HAVE_INTRIN_ENDPOINT3, /* endpoints excl 0: number of endpoint descriptors to follow */
404 0x03, /* CLASS: HID */
405 0, /* SUBCLASS: none */
406 0, /* PROTOCOL: none */
407 0, /* string index for interface */
408 /* HID descriptor */
409 9, /* sizeof(usbDescrHID): length of descriptor in bytes */
410 USBDESCR_HID, /* descriptor type: HID */
411 0x01, 0x01, /* BCD representation of HID version */
412 0x00, /* target country code */
413 0x01, /* number of HID Report (or other HID class) Descriptor infos to follow */
414 0x22, /* descriptor type: report */
415 sizeof(mouse_hid_report), 0, /* total length of report descriptor */
416 #if USB_CFG_HAVE_INTRIN_ENDPOINT3 /* endpoint descriptor for endpoint 3 */
417 /* Endpoint descriptor */
418 7, /* sizeof(usbDescrEndpoint) */
419 USBDESCR_ENDPOINT, /* descriptor type = endpoint */
420 (char)(0x80 | USB_CFG_EP3_NUMBER), /* IN endpoint number 3 */
421 0x03, /* attrib: Interrupt endpoint */
422 8, 0, /* maximum packet size */
423 USB_CFG_INTR_POLL_INTERVAL, /* in ms */
424 #endif
425 };
426 #endif
427
428
429 USB_PUBLIC usbMsgLen_t usbFunctionDescriptor(struct usbRequest *rq)
430 {
431 usbMsgLen_t len = 0;
432
433 debug("usbFunctionDescriptor: ");
434 debug_hex(rq->bmRequestType); debug(" ");
435 debug_hex(rq->bRequest); debug(" ");
436 debug_hex16(rq->wValue.word); debug(" ");
437 debug_hex16(rq->wIndex.word); debug(" ");
438 debug_hex16(rq->wLength.word); debug("\n");
439
440 switch (rq->wValue.bytes[1]) {
441 #if USB_CFG_DESCR_PROPS_CONFIGURATION
442 case USBDESCR_CONFIG:
443 usbMsgPtr = (unsigned char *)usbDescriptorConfiguration;
444 len = sizeof(usbDescriptorConfiguration);
445 break;
446 #endif
447 case USBDESCR_HID:
448 usbMsgPtr = (unsigned char *)(usbDescriptorConfiguration + 18);
449 len = 9;
450 break;
451 case USBDESCR_HID_REPORT:
452 /* interface index */
453 switch (rq->wIndex.word) {
454 case 0:
455 usbMsgPtr = keyboard_hid_report;
456 len = sizeof(keyboard_hid_report);
457 break;
458 case 1:
459 usbMsgPtr = mouse_hid_report;
460 len = sizeof(mouse_hid_report);
461 break;
462 }
463 break;
464 }
465 debug("desc len: "); debug_hex(len); debug("\n");
466 return len;
467 }
Imprint / Impressum