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