xref: /freebsd/sys/dev/usb/template/usb_template_modem.c (revision c66ec88fed842fbaad62c30d510644ceb7bd2d71)
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 Modem 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 	MODEM_LANG_INDEX,
72 	MODEM_INTERFACE_INDEX,
73 	MODEM_MANUFACTURER_INDEX,
74 	MODEM_PRODUCT_INDEX,
75 	MODEM_SERIAL_NUMBER_INDEX,
76 	MODEM_MAX_INDEX,
77 };
78 
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"
84 /*
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.
89  */
90 #define	MODEM_DEFAULT_SERIAL_NUMBER	"FreeBSD1"
91 
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;
96 
97 static struct sysctl_ctx_list		modem_ctx_list;
98 
99 #define	MODEM_IFACE_0 0
100 #define	MODEM_IFACE_1 1
101 
102 /* prototypes */
103 
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,
108 };
109 
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,
114 };
115 
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 */
120 };
121 
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,
127 };
128 
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,
133 };
134 
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,
139 };
140 
141 static const struct usb_temp_endpoint_desc *modem_iface_0_ep[] = {
142 	&modem_ep_0,
143 	NULL,
144 };
145 
146 static const struct usb_temp_endpoint_desc *modem_iface_1_ep[] = {
147 	&modem_ep_1,
148 	&modem_ep_2,
149 	NULL,
150 };
151 
152 static const uint8_t modem_raw_desc_0[] = {
153 	0x05, 0x24, 0x00, 0x10, 0x01
154 };
155 
156 static const uint8_t modem_raw_desc_1[] = {
157 	0x05, 0x24, 0x06, MODEM_IFACE_0, MODEM_IFACE_1
158 };
159 
160 static const uint8_t modem_raw_desc_2[] = {
161 	0x05, 0x24, 0x01, 0x03, MODEM_IFACE_1
162 };
163 
164 static const uint8_t modem_raw_desc_3[] = {
165 	0x04, 0x24, 0x02, 0x07
166 };
167 
168 static const void *modem_iface_0_desc[] = {
169 	&modem_raw_desc_0,
170 	&modem_raw_desc_1,
171 	&modem_raw_desc_2,
172 	&modem_raw_desc_3,
173 	NULL,
174 };
175 
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,
183 };
184 
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,
191 };
192 
193 static const struct usb_temp_interface_desc *modem_interfaces[] = {
194 	&modem_iface_0,
195 	&modem_iface_1,
196 	NULL,
197 };
198 
199 static const struct usb_temp_config_desc modem_config_desc = {
200 	.ppIfaceDesc = modem_interfaces,
201 	.bmAttributes = 0,
202 	.bMaxPower = 0,
203 	.iConfiguration = MODEM_PRODUCT_INDEX,
204 };
205 
206 static const struct usb_temp_config_desc *modem_configs[] = {
207 	&modem_config_desc,
208 	NULL,
209 };
210 
211 static usb_temp_get_string_desc_t modem_get_string_desc;
212 static usb_temp_get_vendor_desc_t modem_get_vendor_desc;
213 
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,
220 	.bcdDevice = 0x0100,
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,
227 };
228 
229 /*------------------------------------------------------------------------*
230  *      modem_get_vendor_desc
231  *
232  * Return values:
233  * NULL: Failure. No such vendor descriptor.
234  * Else: Success. Pointer to vendor descriptor is returned.
235  *------------------------------------------------------------------------*/
236 static const void *
237 modem_get_vendor_desc(const struct usb_device_request *req, uint16_t *plen)
238 {
239 	return (NULL);
240 }
241 
242 /*------------------------------------------------------------------------*
243  *	modem_get_string_desc
244  *
245  * Return values:
246  * NULL: Failure. No such string.
247  * Else: Success. Pointer to string descriptor is returned.
248  *------------------------------------------------------------------------*/
249 static const void *
250 modem_get_string_desc(uint16_t lang_id, uint8_t string_index)
251 {
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,
258 	};
259 
260 	if (string_index == 0) {
261 		return (&usb_string_lang_en);
262 	}
263 	if (lang_id != 0x0409) {
264 		return (NULL);
265 	}
266 	if (string_index < MODEM_MAX_INDEX) {
267 		return (ptr[string_index]);
268 	}
269 	return (NULL);
270 }
271 
272 static void
273 modem_init(void *arg __unused)
274 {
275 	struct sysctl_oid *parent;
276 	char parent_name[3];
277 
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);
286 
287 	snprintf(parent_name, sizeof(parent_name), "%d", USB_TEMP_MODEM);
288 	sysctl_ctx_init(&modem_ctx_list);
289 
290 	parent = SYSCTL_ADD_NODE(&modem_ctx_list,
291 	    SYSCTL_STATIC_CHILDREN(_hw_usb_templates), OID_AUTO,
292 	    parent_name, CTLFLAG_RW | CTLFLAG_MPSAFE,
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");
300 #if 0
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");
305 #endif
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");
318 }
319 
320 static void
321 modem_uninit(void *arg __unused)
322 {
323 
324 	sysctl_ctx_free(&modem_ctx_list);
325 }
326 
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);
329