xref: /freebsd/sys/dev/usb/template/usb_template_modem.c (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2010 Hans Petter Selasky
5  * Copyright (c) 2018 The FreeBSD Foundation
6  * All rights reserved.
7  *
8  * Portions of this software were developed by Edward Tomasz Napierala
9  * under sponsorship from the FreeBSD Foundation.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 /*
34  * This file contains the USB template for an USB Modem Device.
35  */
36 
37 #ifdef USB_GLOBAL_INCLUDE_FILE
38 #include USB_GLOBAL_INCLUDE_FILE
39 #else
40 #include <sys/stdint.h>
41 #include <sys/stddef.h>
42 #include <sys/param.h>
43 #include <sys/queue.h>
44 #include <sys/types.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/bus.h>
48 #include <sys/module.h>
49 #include <sys/lock.h>
50 #include <sys/mutex.h>
51 #include <sys/condvar.h>
52 #include <sys/sysctl.h>
53 #include <sys/sx.h>
54 #include <sys/unistd.h>
55 #include <sys/callout.h>
56 #include <sys/malloc.h>
57 #include <sys/priv.h>
58 
59 #include <dev/usb/usb.h>
60 #include <dev/usb/usbdi.h>
61 #include <dev/usb/usb_core.h>
62 #include <dev/usb/usb_cdc.h>
63 #include <dev/usb/usb_ioctl.h>
64 #include <dev/usb/usb_util.h>
65 
66 #include <dev/usb/template/usb_template.h>
67 #endif			/* USB_GLOBAL_INCLUDE_FILE */
68 
69 enum {
70 	MODEM_LANG_INDEX,
71 	MODEM_INTERFACE_INDEX,
72 	MODEM_MANUFACTURER_INDEX,
73 	MODEM_PRODUCT_INDEX,
74 	MODEM_SERIAL_NUMBER_INDEX,
75 	MODEM_MAX_INDEX,
76 };
77 
78 #define	MODEM_DEFAULT_VENDOR_ID		USB_TEMPLATE_VENDOR
79 #define	MODEM_DEFAULT_PRODUCT_ID	0x27dd
80 #define	MODEM_DEFAULT_INTERFACE		"Virtual serial port"
81 #define	MODEM_DEFAULT_MANUFACTURER	USB_TEMPLATE_MANUFACTURER
82 #define	MODEM_DEFAULT_PRODUCT		"Virtual serial port"
83 /*
84  * The reason for this being called like this is that OSX
85  * derives the device node name from it, resulting in a somewhat
86  * user-friendly "/dev/cu.usbmodemFreeBSD1".  And yes, the "1"
87  * needs to be there, otherwise OSX will mangle it.
88  */
89 #define	MODEM_DEFAULT_SERIAL_NUMBER	"FreeBSD1"
90 
91 static struct usb_string_descriptor	modem_interface;
92 static struct usb_string_descriptor	modem_manufacturer;
93 static struct usb_string_descriptor	modem_product;
94 static struct usb_string_descriptor	modem_serial_number;
95 
96 static struct sysctl_ctx_list		modem_ctx_list;
97 
98 #define	MODEM_IFACE_0 0
99 #define	MODEM_IFACE_1 1
100 
101 /* prototypes */
102 
103 static const struct usb_temp_packet_size modem_bulk_mps = {
104 	.mps[USB_SPEED_LOW] = 8,
105 	.mps[USB_SPEED_FULL] = 64,
106 	.mps[USB_SPEED_HIGH] = 512,
107 };
108 
109 static const struct usb_temp_packet_size modem_intr_mps = {
110 	.mps[USB_SPEED_LOW] = 8,
111 	.mps[USB_SPEED_FULL] = 8,
112 	.mps[USB_SPEED_HIGH] = 8,
113 };
114 
115 static const struct usb_temp_interval modem_intr_interval = {
116 	.bInterval[USB_SPEED_LOW] = 8,	/* 8ms */
117 	.bInterval[USB_SPEED_FULL] = 8,	/* 8ms */
118 	.bInterval[USB_SPEED_HIGH] = 7,	/* 8ms */
119 };
120 
121 static const struct usb_temp_endpoint_desc modem_ep_0 = {
122 	.pPacketSize = &modem_intr_mps,
123 	.pIntervals = &modem_intr_interval,
124 	.bEndpointAddress = UE_DIR_IN,
125 	.bmAttributes = UE_INTERRUPT,
126 };
127 
128 static const struct usb_temp_endpoint_desc modem_ep_1 = {
129 	.pPacketSize = &modem_bulk_mps,
130 	.bEndpointAddress = UE_DIR_OUT,
131 	.bmAttributes = UE_BULK,
132 };
133 
134 static const struct usb_temp_endpoint_desc modem_ep_2 = {
135 	.pPacketSize = &modem_bulk_mps,
136 	.bEndpointAddress = UE_DIR_IN,
137 	.bmAttributes = UE_BULK,
138 };
139 
140 static const struct usb_temp_endpoint_desc *modem_iface_0_ep[] = {
141 	&modem_ep_0,
142 	NULL,
143 };
144 
145 static const struct usb_temp_endpoint_desc *modem_iface_1_ep[] = {
146 	&modem_ep_1,
147 	&modem_ep_2,
148 	NULL,
149 };
150 
151 static const uint8_t modem_raw_desc_0[] = {
152 	0x05, 0x24, 0x00, 0x10, 0x01
153 };
154 
155 static const uint8_t modem_raw_desc_1[] = {
156 	0x05, 0x24, 0x06, MODEM_IFACE_0, MODEM_IFACE_1
157 };
158 
159 static const uint8_t modem_raw_desc_2[] = {
160 	0x05, 0x24, 0x01, 0x03, MODEM_IFACE_1
161 };
162 
163 static const uint8_t modem_raw_desc_3[] = {
164 	0x04, 0x24, 0x02, 0x07
165 };
166 
167 static const void *modem_iface_0_desc[] = {
168 	&modem_raw_desc_0,
169 	&modem_raw_desc_1,
170 	&modem_raw_desc_2,
171 	&modem_raw_desc_3,
172 	NULL,
173 };
174 
175 static const struct usb_temp_interface_desc modem_iface_0 = {
176 	.ppRawDesc = modem_iface_0_desc,
177 	.ppEndpoints = modem_iface_0_ep,
178 	.bInterfaceClass = UICLASS_CDC,
179 	.bInterfaceSubClass = UISUBCLASS_ABSTRACT_CONTROL_MODEL,
180 	.bInterfaceProtocol = UIPROTO_CDC_NONE,
181 	.iInterface = MODEM_INTERFACE_INDEX,
182 };
183 
184 static const struct usb_temp_interface_desc modem_iface_1 = {
185 	.ppEndpoints = modem_iface_1_ep,
186 	.bInterfaceClass = UICLASS_CDC_DATA,
187 	.bInterfaceSubClass = UISUBCLASS_DATA,
188 	.bInterfaceProtocol = UIPROTO_CDC_NONE,
189 	.iInterface = MODEM_INTERFACE_INDEX,
190 };
191 
192 static const struct usb_temp_interface_desc *modem_interfaces[] = {
193 	&modem_iface_0,
194 	&modem_iface_1,
195 	NULL,
196 };
197 
198 static const struct usb_temp_config_desc modem_config_desc = {
199 	.ppIfaceDesc = modem_interfaces,
200 	.bmAttributes = 0,
201 	.bMaxPower = 0,
202 	.iConfiguration = MODEM_PRODUCT_INDEX,
203 };
204 
205 static const struct usb_temp_config_desc *modem_configs[] = {
206 	&modem_config_desc,
207 	NULL,
208 };
209 
210 static usb_temp_get_string_desc_t modem_get_string_desc;
211 static usb_temp_get_vendor_desc_t modem_get_vendor_desc;
212 
213 struct usb_temp_device_desc usb_template_modem = {
214 	.getStringDesc = &modem_get_string_desc,
215 	.getVendorDesc = &modem_get_vendor_desc,
216 	.ppConfigDesc = modem_configs,
217 	.idVendor = MODEM_DEFAULT_VENDOR_ID,
218 	.idProduct = MODEM_DEFAULT_PRODUCT_ID,
219 	.bcdDevice = 0x0100,
220 	.bDeviceClass = UDCLASS_COMM,
221 	.bDeviceSubClass = 0,
222 	.bDeviceProtocol = 0,
223 	.iManufacturer = MODEM_MANUFACTURER_INDEX,
224 	.iProduct = MODEM_PRODUCT_INDEX,
225 	.iSerialNumber = MODEM_SERIAL_NUMBER_INDEX,
226 };
227 
228 /*------------------------------------------------------------------------*
229  *      modem_get_vendor_desc
230  *
231  * Return values:
232  * NULL: Failure. No such vendor descriptor.
233  * Else: Success. Pointer to vendor descriptor is returned.
234  *------------------------------------------------------------------------*/
235 static const void *
236 modem_get_vendor_desc(const struct usb_device_request *req, uint16_t *plen)
237 {
238 	return (NULL);
239 }
240 
241 /*------------------------------------------------------------------------*
242  *	modem_get_string_desc
243  *
244  * Return values:
245  * NULL: Failure. No such string.
246  * Else: Success. Pointer to string descriptor is returned.
247  *------------------------------------------------------------------------*/
248 static const void *
249 modem_get_string_desc(uint16_t lang_id, uint8_t string_index)
250 {
251 	static const void *ptr[MODEM_MAX_INDEX] = {
252 		[MODEM_LANG_INDEX] = &usb_string_lang_en,
253 		[MODEM_INTERFACE_INDEX] = &modem_interface,
254 		[MODEM_MANUFACTURER_INDEX] = &modem_manufacturer,
255 		[MODEM_PRODUCT_INDEX] = &modem_product,
256 		[MODEM_SERIAL_NUMBER_INDEX] = &modem_serial_number,
257 	};
258 
259 	if (string_index == 0) {
260 		return (&usb_string_lang_en);
261 	}
262 	if (lang_id != 0x0409) {
263 		return (NULL);
264 	}
265 	if (string_index < MODEM_MAX_INDEX) {
266 		return (ptr[string_index]);
267 	}
268 	return (NULL);
269 }
270 
271 static void
272 modem_init(void *arg __unused)
273 {
274 	struct sysctl_oid *parent;
275 	char parent_name[3];
276 
277 	usb_make_str_desc(&modem_interface, sizeof(modem_interface),
278 	    MODEM_DEFAULT_INTERFACE);
279 	usb_make_str_desc(&modem_manufacturer, sizeof(modem_manufacturer),
280 	    MODEM_DEFAULT_MANUFACTURER);
281 	usb_make_str_desc(&modem_product, sizeof(modem_product),
282 	    MODEM_DEFAULT_PRODUCT);
283 	usb_make_str_desc(&modem_serial_number, sizeof(modem_serial_number),
284 	    MODEM_DEFAULT_SERIAL_NUMBER);
285 
286 	snprintf(parent_name, sizeof(parent_name), "%d", USB_TEMP_MODEM);
287 	sysctl_ctx_init(&modem_ctx_list);
288 
289 	parent = SYSCTL_ADD_NODE(&modem_ctx_list,
290 	    SYSCTL_STATIC_CHILDREN(_hw_usb_templates), OID_AUTO,
291 	    parent_name, CTLFLAG_RW | CTLFLAG_MPSAFE,
292 	    0, "Virtual serial port device side template");
293 	SYSCTL_ADD_U16(&modem_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
294 	    "vendor_id", CTLFLAG_RWTUN,
295 	    &usb_template_modem.idVendor, 1, "Vendor identifier");
296 	SYSCTL_ADD_U16(&modem_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
297 	    "product_id", CTLFLAG_RWTUN,
298 	    &usb_template_modem.idProduct, 1, "Product identifier");
299 #if 0
300 	SYSCTL_ADD_PROC(&modem_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
301 	    "keyboard", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
302 	    &modem_interface, sizeof(modem_interface), usb_temp_sysctl,
303 	    "A", "Interface string");
304 #endif
305 	SYSCTL_ADD_PROC(&modem_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
306 	    "manufacturer", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
307 	    &modem_manufacturer, sizeof(modem_manufacturer), usb_temp_sysctl,
308 	    "A", "Manufacturer string");
309 	SYSCTL_ADD_PROC(&modem_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
310 	    "product", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
311 	    &modem_product, sizeof(modem_product), usb_temp_sysctl,
312 	    "A", "Product string");
313 	SYSCTL_ADD_PROC(&modem_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
314 	    "serial_number", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
315 	    &modem_serial_number, sizeof(modem_serial_number), usb_temp_sysctl,
316 	    "A", "Serial number string");
317 }
318 
319 static void
320 modem_uninit(void *arg __unused)
321 {
322 
323 	sysctl_ctx_free(&modem_ctx_list);
324 }
325 
326 SYSINIT(modem_init, SI_SUB_LOCK, SI_ORDER_FIRST, modem_init, NULL);
327 SYSUNINIT(modem_uninit, SI_SUB_LOCK, SI_ORDER_FIRST, modem_uninit, NULL);
328