1 /* $FreeBSD$ */ 2 /*- 3 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 4 * 5 * Copyright (c) 2010 Hans Petter Selasky 6 * Copyright (c) 2018 The FreeBSD Foundation 7 * All rights reserved. 8 * 9 * Portions of this software were developed by Edward Tomasz Napierala 10 * under sponsorship from the FreeBSD Foundation. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 /* 35 * This file contains the USB template for an USB Mouse Device. 36 */ 37 38 #ifdef USB_GLOBAL_INCLUDE_FILE 39 #include USB_GLOBAL_INCLUDE_FILE 40 #else 41 #include <sys/stdint.h> 42 #include <sys/stddef.h> 43 #include <sys/param.h> 44 #include <sys/queue.h> 45 #include <sys/types.h> 46 #include <sys/systm.h> 47 #include <sys/kernel.h> 48 #include <sys/bus.h> 49 #include <sys/module.h> 50 #include <sys/lock.h> 51 #include <sys/mutex.h> 52 #include <sys/condvar.h> 53 #include <sys/sysctl.h> 54 #include <sys/sx.h> 55 #include <sys/unistd.h> 56 #include <sys/callout.h> 57 #include <sys/malloc.h> 58 #include <sys/priv.h> 59 60 #include <dev/usb/usb.h> 61 #include <dev/usb/usbdi.h> 62 #include <dev/usb/usb_core.h> 63 #include <dev/usb/usb_cdc.h> 64 #include <dev/usb/usb_ioctl.h> 65 #include <dev/usb/usb_util.h> 66 67 #include <dev/usb/template/usb_template.h> 68 #endif /* USB_GLOBAL_INCLUDE_FILE */ 69 70 enum { 71 MOUSE_LANG_INDEX, 72 MOUSE_INTERFACE_INDEX, 73 MOUSE_PRODUCT_INDEX, 74 MOUSE_MAX_INDEX, 75 }; 76 77 #define MOUSE_DEFAULT_INTERFACE "Mouse interface" 78 #define MOUSE_DEFAULT_PRODUCT "Mouse Test Interface" 79 80 static struct usb_string_descriptor mouse_interface; 81 static struct usb_string_descriptor mouse_product; 82 83 static struct sysctl_ctx_list mouse_ctx_list; 84 85 /* prototypes */ 86 87 /* The following HID descriptor was dumped from a HP mouse. */ 88 89 static 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 99 static 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 105 static 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 111 static 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 119 static const struct usb_temp_endpoint_desc *mouse_endpoints[] = { 120 &mouse_ep_0, 121 NULL, 122 }; 123 124 static const uint8_t mouse_raw_desc[] = { 125 0x09, 0x21, 0x10, 0x01, 0x00, 0x01, 0x22, sizeof(mouse_hid_descriptor), 126 0x00 127 }; 128 129 static const void *mouse_iface_0_desc[] = { 130 mouse_raw_desc, 131 NULL, 132 }; 133 134 static const struct usb_temp_interface_desc mouse_iface_0 = { 135 .ppRawDesc = mouse_iface_0_desc, 136 .ppEndpoints = mouse_endpoints, 137 .bInterfaceClass = UICLASS_HID, 138 .bInterfaceSubClass = UISUBCLASS_BOOT, 139 .bInterfaceProtocol = UIPROTO_MOUSE, 140 .iInterface = MOUSE_INTERFACE_INDEX, 141 }; 142 143 static const struct usb_temp_interface_desc *mouse_interfaces[] = { 144 &mouse_iface_0, 145 NULL, 146 }; 147 148 static 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 = MOUSE_INTERFACE_INDEX, 153 }; 154 155 static const struct usb_temp_config_desc *mouse_configs[] = { 156 &mouse_config_desc, 157 NULL, 158 }; 159 160 static usb_temp_get_string_desc_t mouse_get_string_desc; 161 static usb_temp_get_vendor_desc_t mouse_get_vendor_desc; 162 163 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 = MOUSE_PRODUCT_INDEX, 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 *------------------------------------------------------------------------*/ 185 static const void * 186 mouse_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 *------------------------------------------------------------------------*/ 205 static const void * 206 mouse_get_string_desc(uint16_t lang_id, uint8_t string_index) 207 { 208 static const void *ptr[MOUSE_MAX_INDEX] = { 209 [MOUSE_LANG_INDEX] = &usb_string_lang_en, 210 [MOUSE_INTERFACE_INDEX] = &mouse_interface, 211 [MOUSE_PRODUCT_INDEX] = &mouse_product, 212 }; 213 214 if (string_index == 0) { 215 return (&usb_string_lang_en); 216 } 217 if (lang_id != 0x0409) { 218 return (NULL); 219 } 220 if (string_index < MOUSE_MAX_INDEX) { 221 return (ptr[string_index]); 222 } 223 return (NULL); 224 } 225 226 static void 227 mouse_init(void *arg __unused) 228 { 229 struct sysctl_oid *parent; 230 char parent_name[3]; 231 232 usb_make_str_desc(&mouse_interface, sizeof(mouse_interface), 233 MOUSE_DEFAULT_INTERFACE); 234 usb_make_str_desc(&mouse_product, sizeof(mouse_product), 235 MOUSE_DEFAULT_PRODUCT); 236 237 snprintf(parent_name, sizeof(parent_name), "%d", USB_TEMP_MOUSE); 238 sysctl_ctx_init(&mouse_ctx_list); 239 240 parent = SYSCTL_ADD_NODE(&mouse_ctx_list, 241 SYSCTL_STATIC_CHILDREN(_hw_usb_templates), OID_AUTO, 242 parent_name, CTLFLAG_RW, 243 0, "USB Mouse device side template"); 244 SYSCTL_ADD_U16(&mouse_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, 245 "vendor_id", CTLFLAG_RWTUN, 246 &usb_template_mouse.idVendor, 1, "Vendor identifier"); 247 SYSCTL_ADD_U16(&mouse_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, 248 "product_id", CTLFLAG_RWTUN, 249 &usb_template_mouse.idProduct, 1, "Product identifier"); 250 #if 0 251 SYSCTL_ADD_PROC(&mouse_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, 252 "interface", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, 253 &mouse_interface, sizeof(mouse_interface), usb_temp_sysctl, 254 "A", "Interface string"); 255 #endif 256 SYSCTL_ADD_PROC(&mouse_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, 257 "product", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, 258 &mouse_product, sizeof(mouse_product), usb_temp_sysctl, 259 "A", "Product string"); 260 } 261 262 static void 263 mouse_uninit(void *arg __unused) 264 { 265 266 sysctl_ctx_free(&mouse_ctx_list); 267 } 268 269 SYSINIT(mouse_init, SI_SUB_LOCK, SI_ORDER_FIRST, mouse_init, NULL); 270 SYSUNINIT(mouse_init, SI_SUB_LOCK, SI_ORDER_FIRST, mouse_uninit, NULL); 271