xref: /freebsd/sys/dev/usb/template/usb_template_modem.c (revision ac2fffa4b74cd83963f0d462c379c7f50eeabf20)
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_PRODUCT_INDEX,
74 	MODEM_MAX_INDEX,
75 };
76 
77 #define	MODEM_DEFAULT_INTERFACE		"Modem interface"
78 #define MODEM_DEFAULT_PRODUCT		"Modem Test Device"
79 
80 static struct usb_string_descriptor	modem_interface;
81 static struct usb_string_descriptor	modem_product;
82 
83 static struct sysctl_ctx_list		modem_ctx_list;
84 
85 #define	MODEM_IFACE_0 0
86 #define	MODEM_IFACE_1 1
87 
88 /* prototypes */
89 
90 static const struct usb_temp_packet_size modem_bulk_mps = {
91 	.mps[USB_SPEED_LOW] = 8,
92 	.mps[USB_SPEED_FULL] = 64,
93 	.mps[USB_SPEED_HIGH] = 512,
94 };
95 
96 static const struct usb_temp_packet_size modem_intr_mps = {
97 	.mps[USB_SPEED_LOW] = 8,
98 	.mps[USB_SPEED_FULL] = 8,
99 	.mps[USB_SPEED_HIGH] = 8,
100 };
101 
102 static const struct usb_temp_interval modem_intr_interval = {
103 	.bInterval[USB_SPEED_LOW] = 8,	/* 8ms */
104 	.bInterval[USB_SPEED_FULL] = 8,	/* 8ms */
105 	.bInterval[USB_SPEED_HIGH] = 7,	/* 8ms */
106 };
107 
108 static const struct usb_temp_endpoint_desc modem_ep_0 = {
109 	.pPacketSize = &modem_intr_mps,
110 	.pIntervals = &modem_intr_interval,
111 	.bEndpointAddress = UE_DIR_IN,
112 	.bmAttributes = UE_INTERRUPT,
113 };
114 
115 static const struct usb_temp_endpoint_desc modem_ep_1 = {
116 	.pPacketSize = &modem_bulk_mps,
117 	.bEndpointAddress = UE_DIR_OUT,
118 	.bmAttributes = UE_BULK,
119 };
120 
121 static const struct usb_temp_endpoint_desc modem_ep_2 = {
122 	.pPacketSize = &modem_bulk_mps,
123 	.bEndpointAddress = UE_DIR_IN,
124 	.bmAttributes = UE_BULK,
125 };
126 
127 static const struct usb_temp_endpoint_desc *modem_iface_0_ep[] = {
128 	&modem_ep_0,
129 	NULL,
130 };
131 
132 static const struct usb_temp_endpoint_desc *modem_iface_1_ep[] = {
133 	&modem_ep_1,
134 	&modem_ep_2,
135 	NULL,
136 };
137 
138 static const uint8_t modem_raw_desc_0[] = {
139 	0x05, 0x24, 0x00, 0x10, 0x01
140 };
141 
142 static const uint8_t modem_raw_desc_1[] = {
143 	0x05, 0x24, 0x06, MODEM_IFACE_0, MODEM_IFACE_1
144 };
145 
146 static const uint8_t modem_raw_desc_2[] = {
147 	0x05, 0x24, 0x01, 0x03, MODEM_IFACE_1
148 };
149 
150 static const uint8_t modem_raw_desc_3[] = {
151 	0x04, 0x24, 0x02, 0x07
152 };
153 
154 static const void *modem_iface_0_desc[] = {
155 	&modem_raw_desc_0,
156 	&modem_raw_desc_1,
157 	&modem_raw_desc_2,
158 	&modem_raw_desc_3,
159 	NULL,
160 };
161 
162 static const struct usb_temp_interface_desc modem_iface_0 = {
163 	.ppRawDesc = modem_iface_0_desc,
164 	.ppEndpoints = modem_iface_0_ep,
165 	.bInterfaceClass = UICLASS_CDC,
166 	.bInterfaceSubClass = UISUBCLASS_ABSTRACT_CONTROL_MODEL,
167 	.bInterfaceProtocol = UIPROTO_CDC_AT,
168 	.iInterface = MODEM_INTERFACE_INDEX,
169 };
170 
171 static const struct usb_temp_interface_desc modem_iface_1 = {
172 	.ppEndpoints = modem_iface_1_ep,
173 	.bInterfaceClass = UICLASS_CDC_DATA,
174 	.bInterfaceSubClass = UISUBCLASS_DATA,
175 	.bInterfaceProtocol = UIPROTO_CDC_NONE,
176 	.iInterface = MODEM_INTERFACE_INDEX,
177 };
178 
179 static const struct usb_temp_interface_desc *modem_interfaces[] = {
180 	&modem_iface_0,
181 	&modem_iface_1,
182 	NULL,
183 };
184 
185 static const struct usb_temp_config_desc modem_config_desc = {
186 	.ppIfaceDesc = modem_interfaces,
187 	.bmAttributes = UC_BUS_POWERED,
188 	.bMaxPower = 25,		/* 50 mA */
189 	.iConfiguration = MODEM_PRODUCT_INDEX,
190 };
191 
192 static const struct usb_temp_config_desc *modem_configs[] = {
193 	&modem_config_desc,
194 	NULL,
195 };
196 
197 static usb_temp_get_string_desc_t modem_get_string_desc;
198 static usb_temp_get_vendor_desc_t modem_get_vendor_desc;
199 
200 struct usb_temp_device_desc usb_template_modem = {
201 	.getStringDesc = &modem_get_string_desc,
202 	.getVendorDesc = &modem_get_vendor_desc,
203 	.ppConfigDesc = modem_configs,
204 	.idVendor = USB_TEMPLATE_VENDOR,
205 	.idProduct = 0x000E,
206 	.bcdDevice = 0x0100,
207 	.bDeviceClass = UDCLASS_COMM,
208 	.bDeviceSubClass = 0,
209 	.bDeviceProtocol = 0,
210 	.iManufacturer = 0,
211 	.iProduct = MODEM_PRODUCT_INDEX,
212 	.iSerialNumber = 0,
213 };
214 
215 /*------------------------------------------------------------------------*
216  *      modem_get_vendor_desc
217  *
218  * Return values:
219  * NULL: Failure. No such vendor descriptor.
220  * Else: Success. Pointer to vendor descriptor is returned.
221  *------------------------------------------------------------------------*/
222 static const void *
223 modem_get_vendor_desc(const struct usb_device_request *req, uint16_t *plen)
224 {
225 	return (NULL);
226 }
227 
228 /*------------------------------------------------------------------------*
229  *	modem_get_string_desc
230  *
231  * Return values:
232  * NULL: Failure. No such string.
233  * Else: Success. Pointer to string descriptor is returned.
234  *------------------------------------------------------------------------*/
235 static const void *
236 modem_get_string_desc(uint16_t lang_id, uint8_t string_index)
237 {
238 	static const void *ptr[MODEM_MAX_INDEX] = {
239 		[MODEM_LANG_INDEX] = &usb_string_lang_en,
240 		[MODEM_INTERFACE_INDEX] = &modem_interface,
241 		[MODEM_PRODUCT_INDEX] = &modem_product,
242 	};
243 
244 	if (string_index == 0) {
245 		return (&usb_string_lang_en);
246 	}
247 	if (lang_id != 0x0409) {
248 		return (NULL);
249 	}
250 	if (string_index < MODEM_MAX_INDEX) {
251 		return (ptr[string_index]);
252 	}
253 	return (NULL);
254 }
255 
256 static void
257 modem_init(void *arg __unused)
258 {
259 	struct sysctl_oid *parent;
260 	char parent_name[3];
261 
262 	usb_make_str_desc(&modem_interface, sizeof(modem_interface),
263 	    MODEM_DEFAULT_INTERFACE);
264 	usb_make_str_desc(&modem_product, sizeof(modem_product),
265 	    MODEM_DEFAULT_PRODUCT);
266 
267 	snprintf(parent_name, sizeof(parent_name), "%d", USB_TEMP_MODEM);
268 	sysctl_ctx_init(&modem_ctx_list);
269 
270 	parent = SYSCTL_ADD_NODE(&modem_ctx_list,
271 	    SYSCTL_STATIC_CHILDREN(_hw_usb_templates), OID_AUTO,
272 	    parent_name, CTLFLAG_RW,
273 	    0, "USB Modem device side template");
274 	SYSCTL_ADD_U16(&modem_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
275 	    "vendor_id", CTLFLAG_RWTUN,
276 	    &usb_template_modem.idVendor, 1, "Vendor identifier");
277 	SYSCTL_ADD_U16(&modem_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
278 	    "product_id", CTLFLAG_RWTUN,
279 	    &usb_template_modem.idProduct, 1, "Product identifier");
280 #if 0
281 	SYSCTL_ADD_PROC(&modem_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
282 	    "keyboard", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
283 	    &modem_interface, sizeof(modem_interface), usb_temp_sysctl,
284 	    "A", "Interface string");
285 #endif
286 	SYSCTL_ADD_PROC(&modem_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
287 	    "product", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
288 	    &modem_product, sizeof(modem_product), usb_temp_sysctl,
289 	    "A", "Product string");
290 }
291 
292 static void
293 modem_uninit(void *arg __unused)
294 {
295 
296 	sysctl_ctx_free(&modem_ctx_list);
297 }
298 
299 SYSINIT(modem_init, SI_SUB_LOCK, SI_ORDER_FIRST, modem_init, NULL);
300 SYSUNINIT(modem_init, SI_SUB_LOCK, SI_ORDER_FIRST, modem_uninit, NULL);
301