3 * Copyright (c) 2010 Hans Petter Selasky. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * This file contains the USB template for an USB Keyboard Device.
31 #ifdef USB_GLOBAL_INCLUDE_FILE
32 #include USB_GLOBAL_INCLUDE_FILE
34 #include <sys/stdint.h>
35 #include <sys/stddef.h>
36 #include <sys/param.h>
37 #include <sys/queue.h>
38 #include <sys/types.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
42 #include <sys/module.h>
44 #include <sys/mutex.h>
45 #include <sys/condvar.h>
46 #include <sys/sysctl.h>
48 #include <sys/unistd.h>
49 #include <sys/callout.h>
50 #include <sys/malloc.h>
53 #include <dev/usb/usb.h>
54 #include <dev/usb/usbdi.h>
55 #include <dev/usb/usb_core.h>
56 #include <dev/usb/usb_cdc.h>
58 #include <dev/usb/template/usb_template.h>
59 #endif /* USB_GLOBAL_INCLUDE_FILE */
68 #define STRING_PRODUCT \
69 "K\0e\0y\0b\0o\0a\0r\0d\0 \0T\0e\0s\0t\0 \0D\0e\0v\0i\0c\0e"
71 #define STRING_KEYBOARD \
72 "K\0e\0y\0b\0o\0a\0r\0d\0 \0i\0n\0t\0e\0r\0f\0a\0c\0e"
74 /* make the real string descriptors */
76 USB_MAKE_STRING_DESC(STRING_KEYBOARD, string_keyboard);
77 USB_MAKE_STRING_DESC(STRING_PRODUCT, string_product);
81 static const struct usb_temp_packet_size keyboard_intr_mps = {
82 .mps[USB_SPEED_LOW] = 16,
83 .mps[USB_SPEED_FULL] = 16,
84 .mps[USB_SPEED_HIGH] = 16,
87 static const struct usb_temp_interval keyboard_intr_interval = {
88 .bInterval[USB_SPEED_LOW] = 2, /* 2 ms */
89 .bInterval[USB_SPEED_FULL] = 2, /* 2 ms */
90 .bInterval[USB_SPEED_HIGH] = 5, /* 2 ms */
93 /* The following HID descriptor was dumped from a HP keyboard. */
95 static uint8_t keyboard_hid_descriptor[] = {
96 0x05, 0x01, 0x09, 0x06, 0xa1, 0x01, 0x05, 0x07,
97 0x19, 0xe0, 0x29, 0xe7, 0x15, 0x00, 0x25, 0x01,
98 0x75, 0x01, 0x95, 0x08, 0x81, 0x02, 0x95, 0x01,
99 0x75, 0x08, 0x81, 0x01, 0x95, 0x03, 0x75, 0x01,
100 0x05, 0x08, 0x19, 0x01, 0x29, 0x03, 0x91, 0x02,
101 0x95, 0x05, 0x75, 0x01, 0x91, 0x01, 0x95, 0x06,
102 0x75, 0x08, 0x15, 0x00, 0x26, 0xff, 0x00, 0x05,
103 0x07, 0x19, 0x00, 0x2a, 0xff, 0x00, 0x81, 0x00,
107 static const struct usb_temp_endpoint_desc keyboard_ep_0 = {
108 .ppRawDesc = NULL, /* no raw descriptors */
109 .pPacketSize = &keyboard_intr_mps,
110 .pIntervals = &keyboard_intr_interval,
111 .bEndpointAddress = UE_DIR_IN,
112 .bmAttributes = UE_INTERRUPT,
115 static const struct usb_temp_endpoint_desc *keyboard_endpoints[] = {
120 static const uint8_t keyboard_raw_desc[] = {
121 0x09, 0x21, 0x10, 0x01, 0x00, 0x01, 0x22, sizeof(keyboard_hid_descriptor),
125 static const void *keyboard_iface_0_desc[] = {
130 static const struct usb_temp_interface_desc keyboard_iface_0 = {
131 .ppRawDesc = keyboard_iface_0_desc,
132 .ppEndpoints = keyboard_endpoints,
133 .bInterfaceClass = 3,
134 .bInterfaceSubClass = 1,
135 .bInterfaceProtocol = 1,
136 .iInterface = INDEX_KEYBOARD,
139 static const struct usb_temp_interface_desc *keyboard_interfaces[] = {
144 static const struct usb_temp_config_desc keyboard_config_desc = {
145 .ppIfaceDesc = keyboard_interfaces,
146 .bmAttributes = UC_BUS_POWERED,
147 .bMaxPower = 25, /* 50 mA */
148 .iConfiguration = INDEX_PRODUCT,
151 static const struct usb_temp_config_desc *keyboard_configs[] = {
152 &keyboard_config_desc,
156 static usb_temp_get_string_desc_t keyboard_get_string_desc;
157 static usb_temp_get_vendor_desc_t keyboard_get_vendor_desc;
159 const struct usb_temp_device_desc usb_template_kbd = {
160 .getStringDesc = &keyboard_get_string_desc,
161 .getVendorDesc = &keyboard_get_vendor_desc,
162 .ppConfigDesc = keyboard_configs,
163 .idVendor = USB_TEMPLATE_VENDOR,
166 .bDeviceClass = UDCLASS_COMM,
167 .bDeviceSubClass = 0,
168 .bDeviceProtocol = 0,
170 .iProduct = INDEX_PRODUCT,
174 /*------------------------------------------------------------------------*
175 * keyboard_get_vendor_desc
178 * NULL: Failure. No such vendor descriptor.
179 * Else: Success. Pointer to vendor descriptor is returned.
180 *------------------------------------------------------------------------*/
182 keyboard_get_vendor_desc(const struct usb_device_request *req, uint16_t *plen)
184 if ((req->bmRequestType == 0x81) && (req->bRequest == 0x06) &&
185 (req->wValue[0] == 0x00) && (req->wValue[1] == 0x22) &&
186 (req->wIndex[1] == 0) && (req->wIndex[0] == 0)) {
188 *plen = sizeof(keyboard_hid_descriptor);
189 return (keyboard_hid_descriptor);
194 /*------------------------------------------------------------------------*
195 * keyboard_get_string_desc
198 * NULL: Failure. No such string.
199 * Else: Success. Pointer to string descriptor is returned.
200 *------------------------------------------------------------------------*/
202 keyboard_get_string_desc(uint16_t lang_id, uint8_t string_index)
204 static const void *ptr[INDEX_MAX] = {
205 [INDEX_LANG] = &usb_string_lang_en,
206 [INDEX_KEYBOARD] = &string_keyboard,
207 [INDEX_PRODUCT] = &string_product,
210 if (string_index == 0) {
211 return (&usb_string_lang_en);
213 if (lang_id != 0x0409) {
216 if (string_index < INDEX_MAX) {
217 return (ptr[string_index]);