usb_template_kbd.c revision 246122
159191Skris/* $FreeBSD: head/sys/dev/usb/template/usb_template_kbd.c 246122 2013-01-30 15:26:04Z hselasky $ */ 259191Skris/*- 359191Skris * Copyright (c) 2010 Hans Petter Selasky. All rights reserved. 459191Skris * 559191Skris * Redistribution and use in source and binary forms, with or without 659191Skris * modification, are permitted provided that the following conditions 759191Skris * are met: 859191Skris * 1. Redistributions of source code must retain the above copyright 959191Skris * notice, this list of conditions and the following disclaimer. 1059191Skris * 2. Redistributions in binary form must reproduce the above copyright 1159191Skris * notice, this list of conditions and the following disclaimer in the 1259191Skris * documentation and/or other materials provided with the distribution. 1359191Skris * 1459191Skris * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1559191Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1659191Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1759191Skris * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1859191Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1959191Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2059191Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2159191Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2259191Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2359191Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2459191Skris * SUCH DAMAGE. 2559191Skris */ 2659191Skris 2759191Skris/* 2859191Skris * This file contains the USB template for an USB Keyboard Device. 2959191Skris */ 3059191Skris 3159191Skris#ifdef USB_GLOBAL_INCLUDE_FILE 3259191Skris#include USB_GLOBAL_INCLUDE_FILE 3359191Skris#else 3459191Skris#include <sys/stdint.h> 3559191Skris#include <sys/stddef.h> 3659191Skris#include <sys/param.h> 3759191Skris#include <sys/queue.h> 3859191Skris#include <sys/types.h> 3959191Skris#include <sys/systm.h> 4059191Skris#include <sys/kernel.h> 4159191Skris#include <sys/bus.h> 4259191Skris#include <sys/module.h> 4359191Skris#include <sys/lock.h> 4459191Skris#include <sys/mutex.h> 4559191Skris#include <sys/condvar.h> 4659191Skris#include <sys/sysctl.h> 4759191Skris#include <sys/sx.h> 4859191Skris#include <sys/unistd.h> 4959191Skris#include <sys/callout.h> 5059191Skris#include <sys/malloc.h> 5159191Skris#include <sys/priv.h> 5259191Skris 5359191Skris#include <dev/usb/usb.h> 5459191Skris#include <dev/usb/usbdi.h> 5559191Skris#include <dev/usb/usb_cdc.h> 5659191Skris 5759191Skris#include <dev/usb/template/usb_template.h> 5859191Skris#endif /* USB_GLOBAL_INCLUDE_FILE */ 5959191Skris 6059191Skrisenum { 6159191Skris INDEX_LANG, 62109998Smarkm INDEX_KEYBOARD, 63109998Smarkm INDEX_PRODUCT, 6459191Skris INDEX_MAX, 6559191Skris}; 6659191Skris 6759191Skris#define STRING_LANG \ 6859191Skris 0x09, 0x04, /* American English */ 6959191Skris 7059191Skris#define STRING_PRODUCT \ 71 'K', 0, 'e', 0, 'y', 0, 'b', 0, 'o', 0, 'a', 0, 'r', 0, 'd', 0, ' ', 0, \ 72 'T', 0, 'e', 0, 's', 0, 't', 0, ' ', 0, \ 73 'D', 0, 'e', 0, 'v', 0, 'i', 0, 'c', 0, 'e', 0, 74 75#define STRING_KEYBOARD \ 76 'K', 0, 'e', 0, 'y', 0, 'b', 0, 'o', 0, 'a', 0, 'r', 0, 'd', 0, ' ', 0, \ 77 'i', 0, 'n', 0, 't', 0, 'e', 0, 'r', 0, 'f', 0, 'a', 0, 'c', 0, 'e', 0, 78 79/* make the real string descriptors */ 80 81USB_MAKE_STRING_DESC(STRING_LANG, string_lang); 82USB_MAKE_STRING_DESC(STRING_KEYBOARD, string_keyboard); 83USB_MAKE_STRING_DESC(STRING_PRODUCT, string_product); 84 85/* prototypes */ 86 87static const struct usb_temp_packet_size keyboard_intr_mps = { 88 .mps[USB_SPEED_LOW] = 16, 89 .mps[USB_SPEED_FULL] = 16, 90 .mps[USB_SPEED_HIGH] = 16, 91}; 92 93static const struct usb_temp_interval keyboard_intr_interval = { 94 .bInterval[USB_SPEED_LOW] = 2, /* 2 ms */ 95 .bInterval[USB_SPEED_FULL] = 2, /* 2 ms */ 96 .bInterval[USB_SPEED_HIGH] = 5, /* 2 ms */ 97}; 98 99/* The following HID descriptor was dumped from a HP keyboard. */ 100 101static uint8_t keyboard_hid_descriptor[] = { 102 0x05, 0x01, 0x09, 0x06, 0xa1, 0x01, 0x05, 0x07, 103 0x19, 0xe0, 0x29, 0xe7, 0x15, 0x00, 0x25, 0x01, 104 0x75, 0x01, 0x95, 0x08, 0x81, 0x02, 0x95, 0x01, 105 0x75, 0x08, 0x81, 0x01, 0x95, 0x03, 0x75, 0x01, 106 0x05, 0x08, 0x19, 0x01, 0x29, 0x03, 0x91, 0x02, 107 0x95, 0x05, 0x75, 0x01, 0x91, 0x01, 0x95, 0x06, 108 0x75, 0x08, 0x15, 0x00, 0x26, 0xff, 0x00, 0x05, 109 0x07, 0x19, 0x00, 0x2a, 0xff, 0x00, 0x81, 0x00, 110 0xc0 111}; 112 113static const struct usb_temp_endpoint_desc keyboard_ep_0 = { 114 .ppRawDesc = NULL, /* no raw descriptors */ 115 .pPacketSize = &keyboard_intr_mps, 116 .pIntervals = &keyboard_intr_interval, 117 .bEndpointAddress = UE_DIR_IN, 118 .bmAttributes = UE_INTERRUPT, 119}; 120 121static const struct usb_temp_endpoint_desc *keyboard_endpoints[] = { 122 &keyboard_ep_0, 123 NULL, 124}; 125 126static const uint8_t keyboard_raw_desc[] = { 127 0x09, 0x21, 0x10, 0x01, 0x00, 0x01, 0x22, sizeof(keyboard_hid_descriptor), 128 0x00 129}; 130 131static const void *keyboard_iface_0_desc[] = { 132 keyboard_raw_desc, 133 NULL, 134}; 135 136static const struct usb_temp_interface_desc keyboard_iface_0 = { 137 .ppRawDesc = keyboard_iface_0_desc, 138 .ppEndpoints = keyboard_endpoints, 139 .bInterfaceClass = 3, 140 .bInterfaceSubClass = 1, 141 .bInterfaceProtocol = 1, 142 .iInterface = INDEX_KEYBOARD, 143}; 144 145static const struct usb_temp_interface_desc *keyboard_interfaces[] = { 146 &keyboard_iface_0, 147 NULL, 148}; 149 150static const struct usb_temp_config_desc keyboard_config_desc = { 151 .ppIfaceDesc = keyboard_interfaces, 152 .bmAttributes = UC_BUS_POWERED, 153 .bMaxPower = 25, /* 50 mA */ 154 .iConfiguration = INDEX_PRODUCT, 155}; 156 157static const struct usb_temp_config_desc *keyboard_configs[] = { 158 &keyboard_config_desc, 159 NULL, 160}; 161 162static usb_temp_get_string_desc_t keyboard_get_string_desc; 163static usb_temp_get_vendor_desc_t keyboard_get_vendor_desc; 164 165const struct usb_temp_device_desc usb_template_kbd = { 166 .getStringDesc = &keyboard_get_string_desc, 167 .getVendorDesc = &keyboard_get_vendor_desc, 168 .ppConfigDesc = keyboard_configs, 169 .idVendor = USB_TEMPLATE_VENDOR, 170 .idProduct = 0x00CB, 171 .bcdDevice = 0x0100, 172 .bDeviceClass = UDCLASS_COMM, 173 .bDeviceSubClass = 0, 174 .bDeviceProtocol = 0, 175 .iManufacturer = 0, 176 .iProduct = INDEX_PRODUCT, 177 .iSerialNumber = 0, 178}; 179 180/*------------------------------------------------------------------------* 181 * keyboard_get_vendor_desc 182 * 183 * Return values: 184 * NULL: Failure. No such vendor descriptor. 185 * Else: Success. Pointer to vendor descriptor is returned. 186 *------------------------------------------------------------------------*/ 187static const void * 188keyboard_get_vendor_desc(const struct usb_device_request *req, uint16_t *plen) 189{ 190 if ((req->bmRequestType == 0x81) && (req->bRequest == 0x06) && 191 (req->wValue[0] == 0x00) && (req->wValue[1] == 0x22) && 192 (req->wIndex[1] == 0) && (req->wIndex[0] == 0)) { 193 194 *plen = sizeof(keyboard_hid_descriptor); 195 return (keyboard_hid_descriptor); 196 } 197 return (NULL); 198} 199 200/*------------------------------------------------------------------------* 201 * keyboard_get_string_desc 202 * 203 * Return values: 204 * NULL: Failure. No such string. 205 * Else: Success. Pointer to string descriptor is returned. 206 *------------------------------------------------------------------------*/ 207static const void * 208keyboard_get_string_desc(uint16_t lang_id, uint8_t string_index) 209{ 210 static const void *ptr[INDEX_MAX] = { 211 [INDEX_LANG] = &string_lang, 212 [INDEX_KEYBOARD] = &string_keyboard, 213 [INDEX_PRODUCT] = &string_product, 214 }; 215 216 if (string_index == 0) { 217 return (&string_lang); 218 } 219 if (lang_id != 0x0409) { 220 return (NULL); 221 } 222 if (string_index < INDEX_MAX) { 223 return (ptr[string_index]); 224 } 225 return (NULL); 226} 227