usb_template_mouse.c revision 246122
1/* $FreeBSD: head/sys/dev/usb/template/usb_template_mouse.c 246122 2013-01-30 15:26:04Z hselasky $ */ 2/*- 3 * Copyright (c) 2010 Hans Petter Selasky. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 13 * 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 24 * SUCH DAMAGE. 25 */ 26 27/* 28 * This file contains the USB template for an USB Mouse Device. 29 */ 30 31#ifdef USB_GLOBAL_INCLUDE_FILE 32#include USB_GLOBAL_INCLUDE_FILE 33#else 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> 41#include <sys/bus.h> 42#include <sys/module.h> 43#include <sys/lock.h> 44#include <sys/mutex.h> 45#include <sys/condvar.h> 46#include <sys/sysctl.h> 47#include <sys/sx.h> 48#include <sys/unistd.h> 49#include <sys/callout.h> 50#include <sys/malloc.h> 51#include <sys/priv.h> 52 53#include <dev/usb/usb.h> 54#include <dev/usb/usbdi.h> 55#include <dev/usb/usb_cdc.h> 56 57#include <dev/usb/template/usb_template.h> 58#endif /* USB_GLOBAL_INCLUDE_FILE */ 59 60enum { 61 INDEX_LANG, 62 INDEX_MOUSE, 63 INDEX_PRODUCT, 64 INDEX_MAX, 65}; 66 67#define STRING_LANG \ 68 0x09, 0x04, /* American English */ 69 70#define STRING_PRODUCT \ 71 'M', 0, 'o', 0, 'u', 0, 's', 0, 'e', 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_MOUSE \ 76 'M', 0, 'o', 0, 'u', 0, 's', 0, 'e', 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_MOUSE, string_mouse); 83USB_MAKE_STRING_DESC(STRING_PRODUCT, string_product); 84 85/* prototypes */ 86 87/* The following HID descriptor was dumped from a HP mouse. */ 88 89static uint8_t mouse_hid_descriptor[] = { 90 0x05, 0x01, 0x09, 0x02, 0xa1, 0x01, 0x09, 0x01, 91 0xa1, 0x00, 0x05, 0x09, 0x19, 0x01, 0x29, 0x03, 92 0x15, 0x00, 0x25, 0x01, 0x95, 0x03, 0x75, 0x01, 93 0x81, 0x02, 0x95, 0x05, 0x81, 0x03, 0x05, 0x01, 94 0x09, 0x30, 0x09, 0x31, 0x09, 0x38, 0x15, 0x81, 95 0x25, 0x7f, 0x75, 0x08, 0x95, 0x03, 0x81, 0x06, 96 0xc0, 0xc0 97}; 98 99static const struct usb_temp_packet_size mouse_intr_mps = { 100 .mps[USB_SPEED_LOW] = 8, 101 .mps[USB_SPEED_FULL] = 8, 102 .mps[USB_SPEED_HIGH] = 8, 103}; 104 105static const struct usb_temp_interval mouse_intr_interval = { 106 .bInterval[USB_SPEED_LOW] = 2, /* 2ms */ 107 .bInterval[USB_SPEED_FULL] = 2, /* 2ms */ 108 .bInterval[USB_SPEED_HIGH] = 5, /* 2ms */ 109}; 110 111static const struct usb_temp_endpoint_desc mouse_ep_0 = { 112 .ppRawDesc = NULL, /* no raw descriptors */ 113 .pPacketSize = &mouse_intr_mps, 114 .pIntervals = &mouse_intr_interval, 115 .bEndpointAddress = UE_DIR_IN, 116 .bmAttributes = UE_INTERRUPT, 117}; 118 119static const struct usb_temp_endpoint_desc *mouse_endpoints[] = { 120 &mouse_ep_0, 121 NULL, 122}; 123 124static const uint8_t mouse_raw_desc[] = { 125 0x09, 0x21, 0x10, 0x01, 0x00, 0x01, 0x22, sizeof(mouse_hid_descriptor), 126 0x00 127}; 128 129static const void *mouse_iface_0_desc[] = { 130 mouse_raw_desc, 131 NULL, 132}; 133 134static const struct usb_temp_interface_desc mouse_iface_0 = { 135 .ppRawDesc = mouse_iface_0_desc, 136 .ppEndpoints = mouse_endpoints, 137 .bInterfaceClass = 3, 138 .bInterfaceSubClass = 1, 139 .bInterfaceProtocol = 2, 140 .iInterface = INDEX_MOUSE, 141}; 142 143static const struct usb_temp_interface_desc *mouse_interfaces[] = { 144 &mouse_iface_0, 145 NULL, 146}; 147 148static const struct usb_temp_config_desc mouse_config_desc = { 149 .ppIfaceDesc = mouse_interfaces, 150 .bmAttributes = UC_BUS_POWERED, 151 .bMaxPower = 25, /* 50 mA */ 152 .iConfiguration = INDEX_PRODUCT, 153}; 154 155static const struct usb_temp_config_desc *mouse_configs[] = { 156 &mouse_config_desc, 157 NULL, 158}; 159 160static usb_temp_get_string_desc_t mouse_get_string_desc; 161static usb_temp_get_vendor_desc_t mouse_get_vendor_desc; 162 163const struct usb_temp_device_desc usb_template_mouse = { 164 .getStringDesc = &mouse_get_string_desc, 165 .getVendorDesc = &mouse_get_vendor_desc, 166 .ppConfigDesc = mouse_configs, 167 .idVendor = USB_TEMPLATE_VENDOR, 168 .idProduct = 0x00AE, 169 .bcdDevice = 0x0100, 170 .bDeviceClass = UDCLASS_COMM, 171 .bDeviceSubClass = 0, 172 .bDeviceProtocol = 0, 173 .iManufacturer = 0, 174 .iProduct = INDEX_PRODUCT, 175 .iSerialNumber = 0, 176}; 177 178/*------------------------------------------------------------------------* 179 * mouse_get_vendor_desc 180 * 181 * Return values: 182 * NULL: Failure. No such vendor descriptor. 183 * Else: Success. Pointer to vendor descriptor is returned. 184 *------------------------------------------------------------------------*/ 185static const void * 186mouse_get_vendor_desc(const struct usb_device_request *req, uint16_t *plen) 187{ 188 if ((req->bmRequestType == 0x81) && (req->bRequest == 0x06) && 189 (req->wValue[0] == 0x00) && (req->wValue[1] == 0x22) && 190 (req->wIndex[1] == 0) && (req->wIndex[0] == 0)) { 191 192 *plen = sizeof(mouse_hid_descriptor); 193 return (mouse_hid_descriptor); 194 } 195 return (NULL); 196} 197 198/*------------------------------------------------------------------------* 199 * mouse_get_string_desc 200 * 201 * Return values: 202 * NULL: Failure. No such string. 203 * Else: Success. Pointer to string descriptor is returned. 204 *------------------------------------------------------------------------*/ 205static const void * 206mouse_get_string_desc(uint16_t lang_id, uint8_t string_index) 207{ 208 static const void *ptr[INDEX_MAX] = { 209 [INDEX_LANG] = &string_lang, 210 [INDEX_MOUSE] = &string_mouse, 211 [INDEX_PRODUCT] = &string_product, 212 }; 213 214 if (string_index == 0) { 215 return (&string_lang); 216 } 217 if (lang_id != 0x0409) { 218 return (NULL); 219 } 220 if (string_index < INDEX_MAX) { 221 return (ptr[string_index]); 222 } 223 return (NULL); 224} 225