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");
94 static void send_mouse(report_mouse_t
*report
)
96 report
->report_id
= REPORT_ID_MOUSE
;
97 if (usbInterruptIsReady3()) {
98 usbSetInterrupt3((void *)report
, sizeof(*report
));
102 static void send_system(uint16_t data
)
105 static uint8_t report
[] = { REPORT_ID_SYSTEM
, 0, 0 };
106 report
[1] = data
&0xFF;
107 report
[2] = (data
>>8)&0xFF;
108 if (usbInterruptIsReady3()) {
109 usbSetInterrupt3((void *)&report
, sizeof(report
));
113 static void send_consumer(uint16_t data
)
115 static uint16_t last_data
= 0;
116 if (data
== last_data
) return;
120 static uint8_t report
[] = { REPORT_ID_CONSUMER
, 0, 0 };
121 report
[1] = data
&0xFF;
122 report
[2] = (data
>>8)&0xFF;
123 if (usbInterruptIsReady3()) {
124 usbSetInterrupt3((void *)&report
, sizeof(report
));
130 /*------------------------------------------------------------------*
131 * Request from host *
132 *------------------------------------------------------------------*/
141 usbMsgLen_t
usbFunctionSetup(uchar data
[8])
143 usbRequest_t
*rq
= (void *)data
;
145 if((rq
->bmRequestType
& USBRQ_TYPE_MASK
) == USBRQ_TYPE_CLASS
){ /* class request type */
146 if(rq
->bRequest
== USBRQ_HID_GET_REPORT
){
147 debug("GET_REPORT:");
148 /* we only have one report type, so don't look at wValue */
149 usbMsgPtr
= (void *)keyboard_report_prev
;
150 return sizeof(*keyboard_report_prev
);
151 }else if(rq
->bRequest
== USBRQ_HID_GET_IDLE
){
153 //debug_hex(vusb_idle_rate);
154 usbMsgPtr
= &vusb_idle_rate
;
156 }else if(rq
->bRequest
== USBRQ_HID_SET_IDLE
){
157 vusb_idle_rate
= rq
->wValue
.bytes
[1];
159 debug_hex(vusb_idle_rate
);
160 }else if(rq
->bRequest
== USBRQ_HID_SET_REPORT
){
161 debug("SET_REPORT: ");
162 // Report Type: 0x02(Out)/ReportID: 0x00(none) && Interface: 0(keyboard)
163 if (rq
->wValue
.word
== 0x0200 && rq
->wIndex
.word
== 0) {
165 last_req
.kind
= SET_LED
;
166 last_req
.len
= rq
->wLength
.word
;
168 return USB_NO_MSG
; // to get data in usbFunctionWrite
174 /* no vendor specific requests implemented */
177 return 0; /* default for not implemented requests: return no data back to host */
180 uchar
usbFunctionWrite(uchar
*data
, uchar len
)
182 if (last_req
.len
== 0) {
185 switch (last_req
.kind
) {
190 vusb_keyboard_leds
= data
[0];
204 /*------------------------------------------------------------------*
206 *------------------------------------------------------------------*/
209 * Report Descriptor for keyboard
211 * from an example in HID spec appendix
213 PROGMEM uchar keyboard_hid_report
[] = {
214 0x05, 0x01, // Usage Page (Generic Desktop),
215 0x09, 0x06, // Usage (Keyboard),
216 0xA1, 0x01, // Collection (Application),
217 0x75, 0x01, // Report Size (1),
218 0x95, 0x08, // Report Count (8),
219 0x05, 0x07, // Usage Page (Key Codes),
220 0x19, 0xE0, // Usage Minimum (224),
221 0x29, 0xE7, // Usage Maximum (231),
222 0x15, 0x00, // Logical Minimum (0),
223 0x25, 0x01, // Logical Maximum (1),
224 0x81, 0x02, // Input (Data, Variable, Absolute), ;Modifier byte
225 0x95, 0x01, // Report Count (1),
226 0x75, 0x08, // Report Size (8),
227 0x81, 0x03, // Input (Constant), ;Reserved byte
228 0x95, 0x05, // Report Count (5),
229 0x75, 0x01, // Report Size (1),
230 0x05, 0x08, // Usage Page (LEDs),
231 0x19, 0x01, // Usage Minimum (1),
232 0x29, 0x05, // Usage Maximum (5),
233 0x91, 0x02, // Output (Data, Variable, Absolute), ;LED report
234 0x95, 0x01, // Report Count (1),
235 0x75, 0x03, // Report Size (3),
236 0x91, 0x03, // Output (Constant), ;LED report padding
237 0x95, 0x06, // Report Count (6),
238 0x75, 0x08, // Report Size (8),
239 0x15, 0x00, // Logical Minimum (0),
240 0x25, 0xFF, // Logical Maximum(255),
241 0x05, 0x07, // Usage Page (Key Codes),
242 0x19, 0x00, // Usage Minimum (0),
243 0x29, 0xFF, // Usage Maximum (255),
244 0x81, 0x00, // Input (Data, Array),
245 0xc0 // End Collection
249 * Report Descriptor for mouse
251 * Mouse Protocol 1, HID 1.11 spec, Appendix B, page 59-60, with wheel extension
252 * http://www.microchip.com/forums/tm.aspx?high=&m=391435&mpage=1#391521
253 * http://www.keil.com/forum/15671/
254 * http://www.microsoft.com/whdc/device/input/wheel.mspx
256 PROGMEM uchar mouse_hid_report
[] = {
258 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
259 0x09, 0x02, // USAGE (Mouse)
260 0xa1, 0x01, // COLLECTION (Application)
261 0x85, REPORT_ID_MOUSE
, // REPORT_ID (1)
262 0x09, 0x01, // USAGE (Pointer)
263 0xa1, 0x00, // COLLECTION (Physical)
264 // ---------------------------- Buttons
265 0x05, 0x09, // USAGE_PAGE (Button)
266 0x19, 0x01, // USAGE_MINIMUM (Button 1)
267 0x29, 0x05, // USAGE_MAXIMUM (Button 5)
268 0x15, 0x00, // LOGICAL_MINIMUM (0)
269 0x25, 0x01, // LOGICAL_MAXIMUM (1)
270 0x75, 0x01, // REPORT_SIZE (1)
271 0x95, 0x05, // REPORT_COUNT (5)
272 0x81, 0x02, // INPUT (Data,Var,Abs)
273 0x75, 0x03, // REPORT_SIZE (3)
274 0x95, 0x01, // REPORT_COUNT (1)
275 0x81, 0x03, // INPUT (Cnst,Var,Abs)
276 // ---------------------------- X,Y position
277 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
278 0x09, 0x30, // USAGE (X)
279 0x09, 0x31, // USAGE (Y)
280 0x15, 0x81, // LOGICAL_MINIMUM (-127)
281 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
282 0x75, 0x08, // REPORT_SIZE (8)
283 0x95, 0x02, // REPORT_COUNT (2)
284 0x81, 0x06, // INPUT (Data,Var,Rel)
285 // ---------------------------- Vertical wheel
286 0x09, 0x38, // USAGE (Wheel)
287 0x15, 0x81, // LOGICAL_MINIMUM (-127)
288 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
289 0x35, 0x00, // PHYSICAL_MINIMUM (0) - reset physical
290 0x45, 0x00, // PHYSICAL_MAXIMUM (0)
291 0x75, 0x08, // REPORT_SIZE (8)
292 0x95, 0x01, // REPORT_COUNT (1)
293 0x81, 0x06, // INPUT (Data,Var,Rel)
294 // ---------------------------- Horizontal wheel
295 0x05, 0x0c, // USAGE_PAGE (Consumer Devices)
296 0x0a, 0x38, 0x02, // USAGE (AC Pan)
297 0x15, 0x81, // LOGICAL_MINIMUM (-127)
298 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
299 0x75, 0x08, // REPORT_SIZE (8)
300 0x95, 0x01, // REPORT_COUNT (1)
301 0x81, 0x06, // INPUT (Data,Var,Rel)
302 0xc0, // END_COLLECTION
303 0xc0, // END_COLLECTION
305 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
306 0x09, 0x80, // USAGE (System Control)
307 0xa1, 0x01, // COLLECTION (Application)
308 0x85, REPORT_ID_SYSTEM
, // REPORT_ID (2)
309 0x15, 0x01, // LOGICAL_MINIMUM (0x1)
310 0x25, 0xb7, // LOGICAL_MAXIMUM (0xb7)
311 0x19, 0x01, // USAGE_MINIMUM (0x1)
312 0x29, 0xb7, // USAGE_MAXIMUM (0xb7)
313 0x75, 0x10, // REPORT_SIZE (16)
314 0x95, 0x01, // REPORT_COUNT (1)
315 0x81, 0x00, // INPUT (Data,Array,Abs)
316 0xc0, // END_COLLECTION
318 0x05, 0x0c, // USAGE_PAGE (Consumer Devices)
319 0x09, 0x01, // USAGE (Consumer Control)
320 0xa1, 0x01, // COLLECTION (Application)
321 0x85, REPORT_ID_CONSUMER
, // REPORT_ID (3)
322 0x15, 0x01, // LOGICAL_MINIMUM (0x1)
323 0x26, 0x9c, 0x02, // LOGICAL_MAXIMUM (0x29c)
324 0x19, 0x01, // USAGE_MINIMUM (0x1)
325 0x2a, 0x9c, 0x02, // USAGE_MAXIMUM (0x29c)
326 0x75, 0x10, // REPORT_SIZE (16)
327 0x95, 0x01, // REPORT_COUNT (1)
328 0x81, 0x00, // INPUT (Data,Array,Abs)
329 0xc0, // END_COLLECTION
334 * Descriptor for compite device: Keyboard + Mouse
336 * contains: device, interface, HID and endpoint descriptors
338 #if USB_CFG_DESCR_PROPS_CONFIGURATION
339 PROGMEM
char usbDescriptorConfiguration
[] = { /* USB configuration descriptor */
340 9, /* sizeof(usbDescriptorConfiguration): length of descriptor in bytes */
341 USBDESCR_CONFIG
, /* descriptor type */
342 9 + (9 + 9 + 7) + (9 + 9 + 7), 0,
343 //18 + 7 * USB_CFG_HAVE_INTRIN_ENDPOINT + 7 * USB_CFG_HAVE_INTRIN_ENDPOINT3 + 9, 0,
344 /* total length of data returned (including inlined descriptors) */
345 2, /* number of interfaces in this configuration */
346 1, /* index of this configuration */
347 0, /* configuration name string index */
348 #if USB_CFG_IS_SELF_POWERED
349 (1 << 7) | USBATTR_SELFPOWER
, /* attributes */
351 (1 << 7), /* attributes */
353 USB_CFG_MAX_BUS_POWER
/2, /* max USB current in 2mA units */
358 /* Interface descriptor */
359 9, /* sizeof(usbDescrInterface): length of descriptor in bytes */
360 USBDESCR_INTERFACE
, /* descriptor type */
361 0, /* index of this interface */
362 0, /* alternate setting for this interface */
363 USB_CFG_HAVE_INTRIN_ENDPOINT
, /* endpoints excl 0: number of endpoint descriptors to follow */
364 USB_CFG_INTERFACE_CLASS
,
365 USB_CFG_INTERFACE_SUBCLASS
,
366 USB_CFG_INTERFACE_PROTOCOL
,
367 0, /* string index for interface */
369 9, /* sizeof(usbDescrHID): length of descriptor in bytes */
370 USBDESCR_HID
, /* descriptor type: HID */
371 0x01, 0x01, /* BCD representation of HID version */
372 0x00, /* target country code */
373 0x01, /* number of HID Report (or other HID class) Descriptor infos to follow */
374 0x22, /* descriptor type: report */
375 sizeof(keyboard_hid_report
), 0, /* total length of report descriptor */
376 /* Endpoint descriptor */
377 #if USB_CFG_HAVE_INTRIN_ENDPOINT /* endpoint descriptor for endpoint 1 */
378 7, /* sizeof(usbDescrEndpoint) */
379 USBDESCR_ENDPOINT
, /* descriptor type = endpoint */
380 (char)0x81, /* IN endpoint number 1 */
381 0x03, /* attrib: Interrupt endpoint */
382 8, 0, /* maximum packet size */
383 USB_CFG_INTR_POLL_INTERVAL
, /* in ms */
389 /* Interface descriptor */
390 9, /* sizeof(usbDescrInterface): length of descriptor in bytes */
391 USBDESCR_INTERFACE
, /* descriptor type */
392 1, /* index of this interface */
393 0, /* alternate setting for this interface */
394 USB_CFG_HAVE_INTRIN_ENDPOINT3
, /* endpoints excl 0: number of endpoint descriptors to follow */
395 0x03, /* CLASS: HID */
396 0, /* SUBCLASS: none */
397 0, /* PROTOCOL: none */
398 0, /* string index for interface */
400 9, /* sizeof(usbDescrHID): length of descriptor in bytes */
401 USBDESCR_HID
, /* descriptor type: HID */
402 0x01, 0x01, /* BCD representation of HID version */
403 0x00, /* target country code */
404 0x01, /* number of HID Report (or other HID class) Descriptor infos to follow */
405 0x22, /* descriptor type: report */
406 sizeof(mouse_hid_report
), 0, /* total length of report descriptor */
407 #if USB_CFG_HAVE_INTRIN_ENDPOINT3 /* endpoint descriptor for endpoint 3 */
408 /* Endpoint descriptor */
409 7, /* sizeof(usbDescrEndpoint) */
410 USBDESCR_ENDPOINT
, /* descriptor type = endpoint */
411 (char)(0x80 | USB_CFG_EP3_NUMBER
), /* IN endpoint number 3 */
412 0x03, /* attrib: Interrupt endpoint */
413 8, 0, /* maximum packet size */
414 USB_CFG_INTR_POLL_INTERVAL
, /* in ms */
420 USB_PUBLIC usbMsgLen_t
usbFunctionDescriptor(struct usbRequest
*rq
)
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");
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
);
440 switch (rq
->wValue
.bytes
[0]) {
442 usbMsgPtr
= (unsigned char *)(usbDescriptorConfiguration
+ 9 + 9);
446 usbMsgPtr
= (unsigned char *)(usbDescriptorConfiguration
+ 9 + (9 + 9 + 7) + 9);
451 case USBDESCR_HID_REPORT
:
452 /* interface index */
453 switch (rq
->wIndex
.word
) {
455 usbMsgPtr
= keyboard_hid_report
;
456 len
= sizeof(keyboard_hid_report
);
459 usbMsgPtr
= mouse_hid_report
;
460 len
= sizeof(mouse_hid_report
);
465 //debug("desc len: "); debug_hex(len); debug("\n");