1 /* $FreeBSD$ */ 2 /*- 3 * SPDX-License-Identifier: BSD-2-Clause 4 * 5 * Copyright (c) 2008 Hans Petter Selasky <hselasky@FreeBSD.org> 6 * Copyright (c) 2018 The FreeBSD Foundation 7 * Copyright (c) 2019 Edward Tomasz Napierala <trasz@FreeBSD.org> 8 * All rights reserved. 9 * 10 * Portions of this software were developed by Edward Tomasz Napierala 11 * under sponsorship from the FreeBSD Foundation. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 /* 36 * This file contains the USB templates for an USB Mass Storage Device. 37 */ 38 39 #ifdef USB_GLOBAL_INCLUDE_FILE 40 #include USB_GLOBAL_INCLUDE_FILE 41 #else 42 #include <sys/stdint.h> 43 #include <sys/stddef.h> 44 #include <sys/param.h> 45 #include <sys/queue.h> 46 #include <sys/types.h> 47 #include <sys/systm.h> 48 #include <sys/kernel.h> 49 #include <sys/bus.h> 50 #include <sys/module.h> 51 #include <sys/lock.h> 52 #include <sys/mutex.h> 53 #include <sys/condvar.h> 54 #include <sys/sysctl.h> 55 #include <sys/sx.h> 56 #include <sys/unistd.h> 57 #include <sys/callout.h> 58 #include <sys/malloc.h> 59 #include <sys/priv.h> 60 61 #include <dev/usb/usb.h> 62 #include <dev/usb/usbdi.h> 63 #include <dev/usb/usb_core.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 CDCEEM_LANG_INDEX, 72 CDCEEM_INTERFACE_INDEX, 73 CDCEEM_CONFIGURATION_INDEX, 74 CDCEEM_MANUFACTURER_INDEX, 75 CDCEEM_PRODUCT_INDEX, 76 CDCEEM_SERIAL_NUMBER_INDEX, 77 CDCEEM_MAX_INDEX, 78 }; 79 80 #define CDCEEM_DEFAULT_VENDOR_ID USB_TEMPLATE_VENDOR 81 #define CDCEEM_DEFAULT_PRODUCT_ID 0x27df 82 #define CDCEEM_DEFAULT_INTERFACE "USB CDC EEM Interface" 83 #define CDCEEM_DEFAULT_CONFIGURATION "Default Config" 84 #define CDCEEM_DEFAULT_MANUFACTURER USB_TEMPLATE_MANUFACTURER 85 #define CDCEEM_DEFAULT_PRODUCT "CDC EEM" 86 #define CDCEEM_DEFAULT_SERIAL_NUMBER "March 2008" 87 88 static struct usb_string_descriptor cdceem_interface; 89 static struct usb_string_descriptor cdceem_configuration; 90 static struct usb_string_descriptor cdceem_manufacturer; 91 static struct usb_string_descriptor cdceem_product; 92 static struct usb_string_descriptor cdceem_serial_number; 93 94 static struct sysctl_ctx_list cdceem_ctx_list; 95 96 /* prototypes */ 97 98 static usb_temp_get_string_desc_t cdceem_get_string_desc; 99 100 static const struct usb_temp_packet_size bulk_mps = { 101 .mps[USB_SPEED_FULL] = 64, 102 .mps[USB_SPEED_HIGH] = 512, 103 }; 104 105 static const struct usb_temp_endpoint_desc bulk_in_ep = { 106 .pPacketSize = &bulk_mps, 107 #ifdef USB_HIP_IN_EP_0 108 .bEndpointAddress = USB_HIP_IN_EP_0, 109 #else 110 .bEndpointAddress = UE_DIR_IN, 111 #endif 112 .bmAttributes = UE_BULK, 113 }; 114 115 static const struct usb_temp_endpoint_desc bulk_out_ep = { 116 .pPacketSize = &bulk_mps, 117 #ifdef USB_HIP_OUT_EP_0 118 .bEndpointAddress = USB_HIP_OUT_EP_0, 119 #else 120 .bEndpointAddress = UE_DIR_OUT, 121 #endif 122 .bmAttributes = UE_BULK, 123 }; 124 125 static const struct usb_temp_endpoint_desc *cdceem_data_endpoints[] = { 126 &bulk_in_ep, 127 &bulk_out_ep, 128 NULL, 129 }; 130 131 static const struct usb_temp_interface_desc cdceem_data_interface = { 132 .ppEndpoints = cdceem_data_endpoints, 133 .bInterfaceClass = UICLASS_CDC, 134 .bInterfaceSubClass = UISUBCLASS_ETHERNET_EMULATION_MODEL, 135 .bInterfaceProtocol = UIPROTO_CDC_EEM, 136 .iInterface = CDCEEM_INTERFACE_INDEX, 137 }; 138 139 static const struct usb_temp_interface_desc *cdceem_interfaces[] = { 140 &cdceem_data_interface, 141 NULL, 142 }; 143 144 static const struct usb_temp_config_desc cdceem_config_desc = { 145 .ppIfaceDesc = cdceem_interfaces, 146 .bmAttributes = 0, 147 .bMaxPower = 0, 148 .iConfiguration = CDCEEM_CONFIGURATION_INDEX, 149 }; 150 151 static const struct usb_temp_config_desc *cdceem_configs[] = { 152 &cdceem_config_desc, 153 NULL, 154 }; 155 156 struct usb_temp_device_desc usb_template_cdceem = { 157 .getStringDesc = &cdceem_get_string_desc, 158 .ppConfigDesc = cdceem_configs, 159 .idVendor = CDCEEM_DEFAULT_VENDOR_ID, 160 .idProduct = CDCEEM_DEFAULT_PRODUCT_ID, 161 .bcdDevice = 0x0100, 162 .bDeviceClass = UDCLASS_COMM, 163 .bDeviceSubClass = 0, 164 .bDeviceProtocol = 0, 165 .iManufacturer = CDCEEM_MANUFACTURER_INDEX, 166 .iProduct = CDCEEM_PRODUCT_INDEX, 167 .iSerialNumber = CDCEEM_SERIAL_NUMBER_INDEX, 168 }; 169 170 /*------------------------------------------------------------------------* 171 * cdceem_get_string_desc 172 * 173 * Return values: 174 * NULL: Failure. No such string. 175 * Else: Success. Pointer to string descriptor is returned. 176 *------------------------------------------------------------------------*/ 177 static const void * 178 cdceem_get_string_desc(uint16_t lang_id, uint8_t string_index) 179 { 180 static const void *ptr[CDCEEM_MAX_INDEX] = { 181 [CDCEEM_LANG_INDEX] = &usb_string_lang_en, 182 [CDCEEM_INTERFACE_INDEX] = &cdceem_interface, 183 [CDCEEM_CONFIGURATION_INDEX] = &cdceem_configuration, 184 [CDCEEM_MANUFACTURER_INDEX] = &cdceem_manufacturer, 185 [CDCEEM_PRODUCT_INDEX] = &cdceem_product, 186 [CDCEEM_SERIAL_NUMBER_INDEX] = &cdceem_serial_number, 187 }; 188 189 if (string_index == 0) { 190 return (&usb_string_lang_en); 191 } 192 if (lang_id != 0x0409) { 193 return (NULL); 194 } 195 if (string_index < CDCEEM_MAX_INDEX) { 196 return (ptr[string_index]); 197 } 198 return (NULL); 199 } 200 201 static void 202 cdceem_init(void *arg __unused) 203 { 204 struct sysctl_oid *parent; 205 char parent_name[3]; 206 207 usb_make_str_desc(&cdceem_interface, sizeof(cdceem_interface), 208 CDCEEM_DEFAULT_INTERFACE); 209 usb_make_str_desc(&cdceem_configuration, sizeof(cdceem_configuration), 210 CDCEEM_DEFAULT_CONFIGURATION); 211 usb_make_str_desc(&cdceem_manufacturer, sizeof(cdceem_manufacturer), 212 CDCEEM_DEFAULT_MANUFACTURER); 213 usb_make_str_desc(&cdceem_product, sizeof(cdceem_product), 214 CDCEEM_DEFAULT_PRODUCT); 215 usb_make_str_desc(&cdceem_serial_number, sizeof(cdceem_serial_number), 216 CDCEEM_DEFAULT_SERIAL_NUMBER); 217 218 snprintf(parent_name, sizeof(parent_name), "%d", USB_TEMP_CDCEEM); 219 sysctl_ctx_init(&cdceem_ctx_list); 220 221 parent = SYSCTL_ADD_NODE(&cdceem_ctx_list, 222 SYSCTL_STATIC_CHILDREN(_hw_usb_templates), OID_AUTO, 223 parent_name, CTLFLAG_RW | CTLFLAG_MPSAFE, 224 0, "USB CDC EEM device side template"); 225 SYSCTL_ADD_U16(&cdceem_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, 226 "vendor_id", CTLFLAG_RWTUN, 227 &usb_template_cdceem.idVendor, 1, "Vendor identifier"); 228 SYSCTL_ADD_U16(&cdceem_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, 229 "product_id", CTLFLAG_RWTUN, 230 &usb_template_cdceem.idProduct, 1, "Product identifier"); 231 #if 0 232 SYSCTL_ADD_PROC(&cdceem_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, 233 "interface", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, 234 &cdceem_interface, sizeof(cdceem_interface), usb_temp_sysctl, 235 "A", "Interface string"); 236 SYSCTL_ADD_PROC(&cdceem_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, 237 "configuration", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, 238 &cdceem_configuration, sizeof(cdceem_configuration), usb_temp_sysctl, 239 "A", "Configuration string"); 240 #endif 241 SYSCTL_ADD_PROC(&cdceem_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, 242 "manufacturer", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, 243 &cdceem_manufacturer, sizeof(cdceem_manufacturer), usb_temp_sysctl, 244 "A", "Manufacturer string"); 245 SYSCTL_ADD_PROC(&cdceem_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, 246 "product", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, 247 &cdceem_product, sizeof(cdceem_product), usb_temp_sysctl, 248 "A", "Product string"); 249 SYSCTL_ADD_PROC(&cdceem_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, 250 "serial_number", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, 251 &cdceem_serial_number, sizeof(cdceem_serial_number), usb_temp_sysctl, 252 "A", "Serial number string"); 253 } 254 255 static void 256 cdceem_uninit(void *arg __unused) 257 { 258 259 sysctl_ctx_free(&cdceem_ctx_list); 260 } 261 262 SYSINIT(cdceem_init, SI_SUB_LOCK, SI_ORDER_FIRST, cdceem_init, NULL); 263 SYSUNINIT(cdceem_uninit, SI_SUB_LOCK, SI_ORDER_FIRST, cdceem_uninit, NULL); 264