3 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 * Copyright (c) 2010 Hans Petter Selasky
6 * Copyright (c) 2018 The FreeBSD Foundation
9 * Portions of this software were developed by Edward Tomasz Napierala
10 * under sponsorship from the FreeBSD Foundation.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
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.
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
35 * This file contains the USB template for an USB Modem Device.
38 #ifdef USB_GLOBAL_INCLUDE_FILE
39 #include USB_GLOBAL_INCLUDE_FILE
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>
49 #include <sys/module.h>
51 #include <sys/mutex.h>
52 #include <sys/condvar.h>
53 #include <sys/sysctl.h>
55 #include <sys/unistd.h>
56 #include <sys/callout.h>
57 #include <sys/malloc.h>
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>
67 #include <dev/usb/template/usb_template.h>
68 #endif /* USB_GLOBAL_INCLUDE_FILE */
72 MODEM_INTERFACE_INDEX,
73 MODEM_MANUFACTURER_INDEX,
75 MODEM_SERIAL_NUMBER_INDEX,
79 #define MODEM_DEFAULT_VENDOR_ID USB_TEMPLATE_VENDOR
80 #define MODEM_DEFAULT_PRODUCT_ID 0x27dd
81 #define MODEM_DEFAULT_INTERFACE "Virtual serial port"
82 #define MODEM_DEFAULT_MANUFACTURER USB_TEMPLATE_MANUFACTURER
83 #define MODEM_DEFAULT_PRODUCT "Virtual serial port"
85 * The reason for this being called like this is that OSX
86 * derives the device node name from it, resulting in a somewhat
87 * user-friendly "/dev/cu.usbmodemFreeBSD1". And yes, the "1"
88 * needs to be there, otherwise OSX will mangle it.
90 #define MODEM_DEFAULT_SERIAL_NUMBER "FreeBSD1"
92 static struct usb_string_descriptor modem_interface;
93 static struct usb_string_descriptor modem_manufacturer;
94 static struct usb_string_descriptor modem_product;
95 static struct usb_string_descriptor modem_serial_number;
97 static struct sysctl_ctx_list modem_ctx_list;
99 #define MODEM_IFACE_0 0
100 #define MODEM_IFACE_1 1
104 static const struct usb_temp_packet_size modem_bulk_mps = {
105 .mps[USB_SPEED_LOW] = 8,
106 .mps[USB_SPEED_FULL] = 64,
107 .mps[USB_SPEED_HIGH] = 512,
110 static const struct usb_temp_packet_size modem_intr_mps = {
111 .mps[USB_SPEED_LOW] = 8,
112 .mps[USB_SPEED_FULL] = 8,
113 .mps[USB_SPEED_HIGH] = 8,
116 static const struct usb_temp_interval modem_intr_interval = {
117 .bInterval[USB_SPEED_LOW] = 8, /* 8ms */
118 .bInterval[USB_SPEED_FULL] = 8, /* 8ms */
119 .bInterval[USB_SPEED_HIGH] = 7, /* 8ms */
122 static const struct usb_temp_endpoint_desc modem_ep_0 = {
123 .pPacketSize = &modem_intr_mps,
124 .pIntervals = &modem_intr_interval,
125 .bEndpointAddress = UE_DIR_IN,
126 .bmAttributes = UE_INTERRUPT,
129 static const struct usb_temp_endpoint_desc modem_ep_1 = {
130 .pPacketSize = &modem_bulk_mps,
131 .bEndpointAddress = UE_DIR_OUT,
132 .bmAttributes = UE_BULK,
135 static const struct usb_temp_endpoint_desc modem_ep_2 = {
136 .pPacketSize = &modem_bulk_mps,
137 .bEndpointAddress = UE_DIR_IN,
138 .bmAttributes = UE_BULK,
141 static const struct usb_temp_endpoint_desc *modem_iface_0_ep[] = {
146 static const struct usb_temp_endpoint_desc *modem_iface_1_ep[] = {
152 static const uint8_t modem_raw_desc_0[] = {
153 0x05, 0x24, 0x00, 0x10, 0x01
156 static const uint8_t modem_raw_desc_1[] = {
157 0x05, 0x24, 0x06, MODEM_IFACE_0, MODEM_IFACE_1
160 static const uint8_t modem_raw_desc_2[] = {
161 0x05, 0x24, 0x01, 0x03, MODEM_IFACE_1
164 static const uint8_t modem_raw_desc_3[] = {
165 0x04, 0x24, 0x02, 0x07
168 static const void *modem_iface_0_desc[] = {
176 static const struct usb_temp_interface_desc modem_iface_0 = {
177 .ppRawDesc = modem_iface_0_desc,
178 .ppEndpoints = modem_iface_0_ep,
179 .bInterfaceClass = UICLASS_CDC,
180 .bInterfaceSubClass = UISUBCLASS_ABSTRACT_CONTROL_MODEL,
181 .bInterfaceProtocol = UIPROTO_CDC_NONE,
182 .iInterface = MODEM_INTERFACE_INDEX,
185 static const struct usb_temp_interface_desc modem_iface_1 = {
186 .ppEndpoints = modem_iface_1_ep,
187 .bInterfaceClass = UICLASS_CDC_DATA,
188 .bInterfaceSubClass = UISUBCLASS_DATA,
189 .bInterfaceProtocol = UIPROTO_CDC_NONE,
190 .iInterface = MODEM_INTERFACE_INDEX,
193 static const struct usb_temp_interface_desc *modem_interfaces[] = {
199 static const struct usb_temp_config_desc modem_config_desc = {
200 .ppIfaceDesc = modem_interfaces,
203 .iConfiguration = MODEM_PRODUCT_INDEX,
206 static const struct usb_temp_config_desc *modem_configs[] = {
211 static usb_temp_get_string_desc_t modem_get_string_desc;
212 static usb_temp_get_vendor_desc_t modem_get_vendor_desc;
214 struct usb_temp_device_desc usb_template_modem = {
215 .getStringDesc = &modem_get_string_desc,
216 .getVendorDesc = &modem_get_vendor_desc,
217 .ppConfigDesc = modem_configs,
218 .idVendor = MODEM_DEFAULT_VENDOR_ID,
219 .idProduct = MODEM_DEFAULT_PRODUCT_ID,
221 .bDeviceClass = UDCLASS_COMM,
222 .bDeviceSubClass = 0,
223 .bDeviceProtocol = 0,
224 .iManufacturer = MODEM_MANUFACTURER_INDEX,
225 .iProduct = MODEM_PRODUCT_INDEX,
226 .iSerialNumber = MODEM_SERIAL_NUMBER_INDEX,
229 /*------------------------------------------------------------------------*
230 * modem_get_vendor_desc
233 * NULL: Failure. No such vendor descriptor.
234 * Else: Success. Pointer to vendor descriptor is returned.
235 *------------------------------------------------------------------------*/
237 modem_get_vendor_desc(const struct usb_device_request *req, uint16_t *plen)
242 /*------------------------------------------------------------------------*
243 * modem_get_string_desc
246 * NULL: Failure. No such string.
247 * Else: Success. Pointer to string descriptor is returned.
248 *------------------------------------------------------------------------*/
250 modem_get_string_desc(uint16_t lang_id, uint8_t string_index)
252 static const void *ptr[MODEM_MAX_INDEX] = {
253 [MODEM_LANG_INDEX] = &usb_string_lang_en,
254 [MODEM_INTERFACE_INDEX] = &modem_interface,
255 [MODEM_MANUFACTURER_INDEX] = &modem_manufacturer,
256 [MODEM_PRODUCT_INDEX] = &modem_product,
257 [MODEM_SERIAL_NUMBER_INDEX] = &modem_serial_number,
260 if (string_index == 0) {
261 return (&usb_string_lang_en);
263 if (lang_id != 0x0409) {
266 if (string_index < MODEM_MAX_INDEX) {
267 return (ptr[string_index]);
273 modem_init(void *arg __unused)
275 struct sysctl_oid *parent;
278 usb_make_str_desc(&modem_interface, sizeof(modem_interface),
279 MODEM_DEFAULT_INTERFACE);
280 usb_make_str_desc(&modem_manufacturer, sizeof(modem_manufacturer),
281 MODEM_DEFAULT_MANUFACTURER);
282 usb_make_str_desc(&modem_product, sizeof(modem_product),
283 MODEM_DEFAULT_PRODUCT);
284 usb_make_str_desc(&modem_serial_number, sizeof(modem_serial_number),
285 MODEM_DEFAULT_SERIAL_NUMBER);
287 snprintf(parent_name, sizeof(parent_name), "%d", USB_TEMP_MODEM);
288 sysctl_ctx_init(&modem_ctx_list);
290 parent = SYSCTL_ADD_NODE(&modem_ctx_list,
291 SYSCTL_STATIC_CHILDREN(_hw_usb_templates), OID_AUTO,
292 parent_name, CTLFLAG_RW,
293 0, "Virtual serial port device side template");
294 SYSCTL_ADD_U16(&modem_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
295 "vendor_id", CTLFLAG_RWTUN,
296 &usb_template_modem.idVendor, 1, "Vendor identifier");
297 SYSCTL_ADD_U16(&modem_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
298 "product_id", CTLFLAG_RWTUN,
299 &usb_template_modem.idProduct, 1, "Product identifier");
301 SYSCTL_ADD_PROC(&modem_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
302 "keyboard", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
303 &modem_interface, sizeof(modem_interface), usb_temp_sysctl,
304 "A", "Interface string");
306 SYSCTL_ADD_PROC(&modem_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
307 "manufacturer", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
308 &modem_manufacturer, sizeof(modem_manufacturer), usb_temp_sysctl,
309 "A", "Manufacturer string");
310 SYSCTL_ADD_PROC(&modem_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
311 "product", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
312 &modem_product, sizeof(modem_product), usb_temp_sysctl,
313 "A", "Product string");
314 SYSCTL_ADD_PROC(&modem_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
315 "serial_number", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
316 &modem_serial_number, sizeof(modem_serial_number), usb_temp_sysctl,
317 "A", "Serial number string");
321 modem_uninit(void *arg __unused)
324 sysctl_ctx_free(&modem_ctx_list);
327 SYSINIT(modem_init, SI_SUB_LOCK, SI_ORDER_FIRST, modem_init, NULL);
328 SYSUNINIT(modem_uninit, SI_SUB_LOCK, SI_ORDER_FIRST, modem_uninit, NULL);