1 #include <sys/cdefs.h>
2 __FBSDID("$FreeBSD$");
3 
4 /*-
5  * Copyright (c) 2010 Hans Petter Selasky. All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 /*
30  * This file contains the USB template for an USB Modem Device.
31  */
32 
33 #include <sys/stdint.h>
34 #include <sys/stddef.h>
35 #include <sys/param.h>
36 #include <sys/queue.h>
37 #include <sys/types.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/bus.h>
41 #include <sys/module.h>
42 #include <sys/lock.h>
43 #include <sys/mutex.h>
44 #include <sys/condvar.h>
45 #include <sys/sysctl.h>
46 #include <sys/sx.h>
47 #include <sys/unistd.h>
48 #include <sys/callout.h>
49 #include <sys/malloc.h>
50 #include <sys/priv.h>
51 
52 #include <dev/usb/usb.h>
53 #include <dev/usb/usbdi.h>
54 #include <dev/usb/usb_cdc.h>
55 
56 #include <dev/usb/template/usb_template.h>
57 
58 enum {
59 	INDEX_LANG,
60 	INDEX_MODEM,
61 	INDEX_PRODUCT,
62 	INDEX_MAX,
63 };
64 
65 #define	STRING_LANG \
66   0x09, 0x04,				/* American English */
67 
68 #define	STRING_PRODUCT \
69   'M', 0, 'o', 0, 'd', 0, 'e', 0, 'm', 0, ' ', 0, \
70   'T', 0, 'e', 0, 's', 0, 't', 0, ' ', 0, \
71   'D', 0, 'e', 0, 'v', 0, 'i', 0, 'c', 0, 'e', 0, ' ', 0,
72 
73 #define	STRING_MODEM \
74   'M', 0, 'o', 0, 'd', 0, 'e', 0, 'm', 0, ' ', 0, \
75   'i', 0, 'n', 0, 't', 0, 'e', 0, 'r', 0, 'f', 0, 'a', 0, 'c', 0, 'e', 0,
76 
77 /* make the real string descriptors */
78 
79 USB_MAKE_STRING_DESC(STRING_LANG, string_lang);
80 USB_MAKE_STRING_DESC(STRING_MODEM, string_modem);
81 USB_MAKE_STRING_DESC(STRING_PRODUCT, string_product);
82 
83 #define	MODEM_IFACE_0 0
84 #define	MODEM_IFACE_1 1
85 
86 /* prototypes */
87 
88 static const struct usb_temp_packet_size modem_bulk_mps = {
89 	.mps[USB_SPEED_LOW] = 8,
90 	.mps[USB_SPEED_FULL] = 64,
91 	.mps[USB_SPEED_HIGH] = 512,
92 };
93 
94 static const struct usb_temp_packet_size modem_intr_mps = {
95 	.mps[USB_SPEED_LOW] = 8,
96 	.mps[USB_SPEED_FULL] = 8,
97 	.mps[USB_SPEED_HIGH] = 8,
98 };
99 
100 static const struct usb_temp_interval modem_intr_interval = {
101 	.bInterval[USB_SPEED_LOW] = 8,	/* 8ms */
102 	.bInterval[USB_SPEED_FULL] = 8,	/* 8ms */
103 	.bInterval[USB_SPEED_HIGH] = 7,	/* 8ms */
104 };
105 
106 static const struct usb_temp_endpoint_desc modem_ep_0 = {
107 	.pPacketSize = &modem_intr_mps,
108 	.pIntervals = &modem_intr_interval,
109 	.bEndpointAddress = UE_DIR_IN,
110 	.bmAttributes = UE_INTERRUPT,
111 };
112 
113 static const struct usb_temp_endpoint_desc modem_ep_1 = {
114 	.pPacketSize = &modem_bulk_mps,
115 	.bEndpointAddress = UE_DIR_OUT,
116 	.bmAttributes = UE_BULK,
117 };
118 
119 static const struct usb_temp_endpoint_desc modem_ep_2 = {
120 	.pPacketSize = &modem_bulk_mps,
121 	.bEndpointAddress = UE_DIR_IN,
122 	.bmAttributes = UE_BULK,
123 };
124 
125 static const struct usb_temp_endpoint_desc *modem_iface_0_ep[] = {
126 	&modem_ep_0,
127 	NULL,
128 };
129 
130 static const struct usb_temp_endpoint_desc *modem_iface_1_ep[] = {
131 	&modem_ep_1,
132 	&modem_ep_2,
133 	NULL,
134 };
135 
136 static const uint8_t modem_raw_desc_0[] = {
137 	0x05, 0x24, 0x00, 0x10, 0x01
138 };
139 
140 static const uint8_t modem_raw_desc_1[] = {
141 	0x05, 0x24, 0x06, MODEM_IFACE_0, MODEM_IFACE_1
142 };
143 
144 static const uint8_t modem_raw_desc_2[] = {
145 	0x05, 0x24, 0x01, 0x03, MODEM_IFACE_1
146 };
147 
148 static const uint8_t modem_raw_desc_3[] = {
149 	0x04, 0x24, 0x02, 0x07
150 };
151 
152 static const void *modem_iface_0_desc[] = {
153 	&modem_raw_desc_0,
154 	&modem_raw_desc_1,
155 	&modem_raw_desc_2,
156 	&modem_raw_desc_3,
157 	NULL,
158 };
159 
160 static const struct usb_temp_interface_desc modem_iface_0 = {
161 	.ppRawDesc = modem_iface_0_desc,
162 	.ppEndpoints = modem_iface_0_ep,
163 	.bInterfaceClass = 2,
164 	.bInterfaceSubClass = 2,
165 	.bInterfaceProtocol = 1,
166 	.iInterface = INDEX_MODEM,
167 };
168 
169 static const struct usb_temp_interface_desc modem_iface_1 = {
170 	.ppEndpoints = modem_iface_1_ep,
171 	.bInterfaceClass = 10,
172 	.bInterfaceSubClass = 0,
173 	.bInterfaceProtocol = 0,
174 	.iInterface = INDEX_MODEM,
175 };
176 
177 static const struct usb_temp_interface_desc *modem_interfaces[] = {
178 	&modem_iface_0,
179 	&modem_iface_1,
180 	NULL,
181 };
182 
183 static const struct usb_temp_config_desc modem_config_desc = {
184 	.ppIfaceDesc = modem_interfaces,
185 	.bmAttributes = UC_BUS_POWERED,
186 	.bMaxPower = 25,		/* 50 mA */
187 	.iConfiguration = INDEX_PRODUCT,
188 };
189 
190 static const struct usb_temp_config_desc *modem_configs[] = {
191 	&modem_config_desc,
192 	NULL,
193 };
194 
195 static usb_temp_get_string_desc_t modem_get_string_desc;
196 static usb_temp_get_vendor_desc_t modem_get_vendor_desc;
197 
198 const struct usb_temp_device_desc usb_template_modem = {
199 	.getStringDesc = &modem_get_string_desc,
200 	.getVendorDesc = &modem_get_vendor_desc,
201 	.ppConfigDesc = modem_configs,
202 	.idVendor = USB_TEMPLATE_VENDOR,
203 	.idProduct = 0x000E,
204 	.bcdDevice = 0x0100,
205 	.bDeviceClass = UDCLASS_COMM,
206 	.bDeviceSubClass = 0,
207 	.bDeviceProtocol = 0,
208 	.iManufacturer = 0,
209 	.iProduct = INDEX_PRODUCT,
210 	.iSerialNumber = 0,
211 };
212 
213 /*------------------------------------------------------------------------*
214  *      modem_get_vendor_desc
215  *
216  * Return values:
217  * NULL: Failure. No such vendor descriptor.
218  * Else: Success. Pointer to vendor descriptor is returned.
219  *------------------------------------------------------------------------*/
220 static const void *
221 modem_get_vendor_desc(const struct usb_device_request *req, uint16_t *plen)
222 {
223 	return (NULL);
224 }
225 
226 /*------------------------------------------------------------------------*
227  *	modem_get_string_desc
228  *
229  * Return values:
230  * NULL: Failure. No such string.
231  * Else: Success. Pointer to string descriptor is returned.
232  *------------------------------------------------------------------------*/
233 static const void *
234 modem_get_string_desc(uint16_t lang_id, uint8_t string_index)
235 {
236 	static const void *ptr[INDEX_MAX] = {
237 		[INDEX_LANG] = &string_lang,
238 		[INDEX_MODEM] = &string_modem,
239 		[INDEX_PRODUCT] = &string_product,
240 	};
241 
242 	if (string_index == 0) {
243 		return (&string_lang);
244 	}
245 	if (lang_id != 0x0409) {
246 		return (NULL);
247 	}
248 	if (string_index < INDEX_MAX) {
249 		return (ptr[string_index]);
250 	}
251 	return (NULL);
252 }
253