1 /* $FreeBSD$ */
2 /*-
3  * Copyright (c) 2014 Hans Petter Selasky. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 /*
28  * This file contains the USB template for an USB phone device.
29  */
30 
31 #ifdef USB_GLOBAL_INCLUDE_FILE
32 #include USB_GLOBAL_INCLUDE_FILE
33 #else
34 #include <sys/stdint.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/condvar.h>
44 #include <sys/sysctl.h>
45 #include <sys/unistd.h>
46 #include <sys/callout.h>
47 #include <sys/malloc.h>
48 #include <sys/caps.h>
49 
50 #include <bus/u4b/usb.h>
51 #include <bus/u4b/usbdi.h>
52 #include <bus/u4b/usb_core.h>
53 #include <bus/u4b/usb_cdc.h>
54 
55 #include <bus/u4b/template/usb_template.h>
56 #endif			/* USB_GLOBAL_INCLUDE_FILE */
57 
58 enum {
59 	INDEX_PHONE_LANG,
60 	INDEX_PHONE_MIXER,
61 	INDEX_PHONE_RECORD,
62 	INDEX_PHONE_PLAYBACK,
63 	INDEX_PHONE_PRODUCT,
64 	INDEX_PHONE_HID,
65 	INDEX_PHONE_MAX,
66 };
67 
68 #define	STRING_PHONE_PRODUCT \
69   "U\0S\0B\0 \0P\0h\0o\0n\0e\0 \0D\0e\0v\0i\0c\0e"
70 
71 #define	STRING_PHONE_MIXER \
72   "M\0i\0x\0e\0r\0 \0i\0n\0t\0e\0r\0f\0a\0c\0e"
73 
74 #define	STRING_PHONE_RECORD \
75   "R\0e\0c\0o\0r\0d\0 \0i\0n\0t\0e\0r\0f\0a\0c\0e"
76 
77 #define	STRING_PHONE_PLAYBACK \
78   "P\0l\0a\0y\0b\0a\0c\0k\0 \0i\0n\0t\0e\0r\0f\0a\0c\0e"
79 
80 #define	STRING_PHONE_HID \
81   "H\0I\0D\0 \0i\0n\0t\0e\0r\0f\0a\0c\0e"
82 
83 /* make the real string descriptors */
84 
85 USB_MAKE_STRING_DESC(STRING_PHONE_MIXER, string_phone_mixer);
86 USB_MAKE_STRING_DESC(STRING_PHONE_RECORD, string_phone_record);
87 USB_MAKE_STRING_DESC(STRING_PHONE_PLAYBACK, string_phone_playback);
88 USB_MAKE_STRING_DESC(STRING_PHONE_PRODUCT, string_phone_product);
89 USB_MAKE_STRING_DESC(STRING_PHONE_HID, string_phone_hid);
90 
91 /* prototypes */
92 
93 /*
94  * Phone Mixer description structures
95  *
96  * Some of the phone descriptors were dumped from no longer in
97  * production Yealink VOIP USB phone adapter:
98  */
99 static uint8_t phone_hid_descriptor[] = {
100 	0x05, 0x0b, 0x09, 0x01, 0xa1, 0x01, 0x05, 0x09,
101 	0x19, 0x01, 0x29, 0x3f, 0x15, 0x00, 0x25, 0x01,
102 	0x75, 0x01, 0x95, 0x80, 0x81, 0x00, 0x05, 0x08,
103 	0x19, 0x01, 0x29, 0x10, 0x15, 0x00, 0x25, 0x01,
104 	0x75, 0x01, 0x95, 0x80, 0x91, 0x00, 0xc0
105 };
106 
107 static const uint8_t phone_raw_desc_0[] = {
108 	0x0a, 0x24, 0x01, 0x00, 0x01, 0x4a, 0x00, 0x02,
109 	0x01, 0x02
110 };
111 
112 static const uint8_t phone_raw_desc_1[] = {
113 	0x0c, 0x24, 0x02, 0x01, 0x01, 0x02, 0x00, 0x01,
114 	0x00, 0x00, 0x00, 0x00
115 };
116 
117 static const uint8_t phone_raw_desc_2[] = {
118 	0x0c, 0x24, 0x02, 0x02, 0x01, 0x01, 0x00, 0x01,
119 	0x00, 0x00, 0x00, 0x00
120 };
121 
122 static const uint8_t phone_raw_desc_3[] = {
123 	0x09, 0x24, 0x03, 0x03, 0x01, 0x03, 0x00, 0x06,
124 	0x00
125 };
126 
127 static const uint8_t phone_raw_desc_4[] = {
128 	0x09, 0x24, 0x03, 0x04, 0x01, 0x01, 0x00, 0x05,
129 	0x00
130 };
131 
132 static const uint8_t phone_raw_desc_5[] = {
133 	0x0b, 0x24, 0x06, 0x05, 0x01, 0x02, 0x03, 0x00,
134 	0x03, 0x00, 0x00
135 };
136 
137 static const uint8_t phone_raw_desc_6[] = {
138 	0x0b, 0x24, 0x06, 0x06, 0x02, 0x02, 0x03, 0x00,
139 	0x03, 0x00, 0x00
140 };
141 
142 static const void *phone_raw_iface_0_desc[] = {
143 	phone_raw_desc_0,
144 	phone_raw_desc_1,
145 	phone_raw_desc_2,
146 	phone_raw_desc_3,
147 	phone_raw_desc_4,
148 	phone_raw_desc_5,
149 	phone_raw_desc_6,
150 	NULL,
151 };
152 
153 static const struct usb_temp_interface_desc phone_iface_0 = {
154 	.ppEndpoints = NULL,		/* no endpoints */
155 	.ppRawDesc = phone_raw_iface_0_desc,
156 	.bInterfaceClass = 1,
157 	.bInterfaceSubClass = 1,
158 	.bInterfaceProtocol = 0,
159 	.iInterface = INDEX_PHONE_MIXER,
160 };
161 
162 static const uint8_t phone_raw_desc_20[] = {
163 	0x07, 0x24, 0x01, 0x04, 0x01, 0x01, 0x00
164 };
165 
166 static const uint8_t phone_raw_desc_21[] = {
167 	0x0b, 0x24, 0x02, 0x01, 0x01, 0x02, 0x10, 0x01,
168 	/* 8kHz */
169 	0x40, 0x1f, 0x00
170 };
171 
172 static const uint8_t phone_raw_desc_22[] = {
173 	0x07, 0x25, 0x01, 0x00, 0x00, 0x00, 0x00
174 };
175 
176 static const void *phone_raw_iface_1_desc[] = {
177 	phone_raw_desc_20,
178 	phone_raw_desc_21,
179 	NULL,
180 };
181 
182 static const void *phone_raw_ep_1_desc[] = {
183 	phone_raw_desc_22,
184 	NULL,
185 };
186 
187 static const struct usb_temp_packet_size phone_isoc_mps = {
188 	.mps[USB_SPEED_FULL] = 0x10,
189 	.mps[USB_SPEED_HIGH] = 0x10,
190 };
191 
192 static const struct usb_temp_interval phone_isoc_interval = {
193 	.bInterval[USB_SPEED_FULL] = 1,	/* 1:1 */
194 	.bInterval[USB_SPEED_HIGH] = 4,	/* 1:8 */
195 };
196 
197 static const struct usb_temp_endpoint_desc phone_isoc_in_ep = {
198 	.ppRawDesc = phone_raw_ep_1_desc,
199 	.pPacketSize = &phone_isoc_mps,
200 	.pIntervals = &phone_isoc_interval,
201 	.bEndpointAddress = UE_DIR_IN,
202 	.bmAttributes = UE_ISOCHRONOUS,
203 };
204 
205 static const struct usb_temp_endpoint_desc *phone_iface_1_ep[] = {
206 	&phone_isoc_in_ep,
207 	NULL,
208 };
209 
210 static const struct usb_temp_interface_desc phone_iface_1_alt_0 = {
211 	.ppEndpoints = NULL,		/* no endpoints */
212 	.ppRawDesc = NULL,		/* no raw descriptors */
213 	.bInterfaceClass = 1,
214 	.bInterfaceSubClass = 2,
215 	.bInterfaceProtocol = 0,
216 	.iInterface = INDEX_PHONE_PLAYBACK,
217 };
218 
219 static const struct usb_temp_interface_desc phone_iface_1_alt_1 = {
220 	.ppEndpoints = phone_iface_1_ep,
221 	.ppRawDesc = phone_raw_iface_1_desc,
222 	.bInterfaceClass = 1,
223 	.bInterfaceSubClass = 2,
224 	.bInterfaceProtocol = 0,
225 	.iInterface = INDEX_PHONE_PLAYBACK,
226 	.isAltInterface = 1,		/* this is an alternate setting */
227 };
228 
229 static const uint8_t phone_raw_desc_30[] = {
230 	0x07, 0x24, 0x01, 0x02, 0x01, 0x01, 0x00
231 };
232 
233 static const uint8_t phone_raw_desc_31[] = {
234 	0x0b, 0x24, 0x02, 0x01, 0x01, 0x02, 0x10, 0x01,
235 	/* 8kHz */
236 	0x40, 0x1f, 0x00
237 };
238 
239 static const uint8_t phone_raw_desc_32[] = {
240 	0x07, 0x25, 0x01, 0x00, 0x00, 0x00, 0x00
241 };
242 
243 static const void *phone_raw_iface_2_desc[] = {
244 	phone_raw_desc_30,
245 	phone_raw_desc_31,
246 	NULL,
247 };
248 
249 static const void *phone_raw_ep_2_desc[] = {
250 	phone_raw_desc_32,
251 	NULL,
252 };
253 
254 static const struct usb_temp_endpoint_desc phone_isoc_out_ep = {
255 	.ppRawDesc = phone_raw_ep_2_desc,
256 	.pPacketSize = &phone_isoc_mps,
257 	.pIntervals = &phone_isoc_interval,
258 	.bEndpointAddress = UE_DIR_OUT,
259 	.bmAttributes = UE_ISOCHRONOUS,
260 };
261 
262 static const struct usb_temp_endpoint_desc *phone_iface_2_ep[] = {
263 	&phone_isoc_out_ep,
264 	NULL,
265 };
266 
267 static const struct usb_temp_interface_desc phone_iface_2_alt_0 = {
268 	.ppEndpoints = NULL,		/* no endpoints */
269 	.ppRawDesc = NULL,		/* no raw descriptors */
270 	.bInterfaceClass = 1,
271 	.bInterfaceSubClass = 2,
272 	.bInterfaceProtocol = 0,
273 	.iInterface = INDEX_PHONE_RECORD,
274 };
275 
276 static const struct usb_temp_interface_desc phone_iface_2_alt_1 = {
277 	.ppEndpoints = phone_iface_2_ep,
278 	.ppRawDesc = phone_raw_iface_2_desc,
279 	.bInterfaceClass = 1,
280 	.bInterfaceSubClass = 2,
281 	.bInterfaceProtocol = 0,
282 	.iInterface = INDEX_PHONE_RECORD,
283 	.isAltInterface = 1,		/* this is an alternate setting */
284 };
285 
286 static const uint8_t phone_hid_raw_desc_0[] = {
287 	0x09, 0x21, 0x00, 0x01, 0x00, 0x01, 0x22, sizeof(phone_hid_descriptor),
288 	0x00
289 };
290 
291 static const void *phone_hid_desc_0[] = {
292 	phone_hid_raw_desc_0,
293 	NULL,
294 };
295 
296 static const struct usb_temp_packet_size phone_hid_mps = {
297 	.mps[USB_SPEED_FULL] = 0x10,
298 	.mps[USB_SPEED_HIGH] = 0x10,
299 };
300 
301 static const struct usb_temp_interval phone_hid_interval = {
302 	.bInterval[USB_SPEED_FULL] = 2,		/* 2ms */
303 	.bInterval[USB_SPEED_HIGH] = 2,		/* 2ms */
304 };
305 
306 static const struct usb_temp_endpoint_desc phone_hid_in_ep = {
307 	.pPacketSize = &phone_hid_mps,
308 	.pIntervals = &phone_hid_interval,
309 	.bEndpointAddress = UE_DIR_IN,
310 	.bmAttributes = UE_INTERRUPT,
311 };
312 
313 static const struct usb_temp_endpoint_desc *phone_iface_3_ep[] = {
314 	&phone_hid_in_ep,
315 	NULL,
316 };
317 
318 static const struct usb_temp_interface_desc phone_iface_3 = {
319 	.ppEndpoints = phone_iface_3_ep,
320 	.ppRawDesc = phone_hid_desc_0,
321 	.bInterfaceClass = 3,
322 	.bInterfaceSubClass = 0,
323 	.bInterfaceProtocol = 0,
324 	.iInterface = INDEX_PHONE_HID,
325 };
326 
327 static const struct usb_temp_interface_desc *phone_interfaces[] = {
328 	&phone_iface_0,
329 	&phone_iface_1_alt_0,
330 	&phone_iface_1_alt_1,
331 	&phone_iface_2_alt_0,
332 	&phone_iface_2_alt_1,
333 	&phone_iface_3,
334 	NULL,
335 };
336 
337 static const struct usb_temp_config_desc phone_config_desc = {
338 	.ppIfaceDesc = phone_interfaces,
339 	.bmAttributes = UC_BUS_POWERED,
340 	.bMaxPower = 25,		/* 50 mA */
341 	.iConfiguration = INDEX_PHONE_PRODUCT,
342 };
343 
344 static const struct usb_temp_config_desc *phone_configs[] = {
345 	&phone_config_desc,
346 	NULL,
347 };
348 
349 static usb_temp_get_string_desc_t phone_get_string_desc;
350 static usb_temp_get_vendor_desc_t phone_get_vendor_desc;
351 
352 const struct usb_temp_device_desc usb_template_phone = {
353 	.getStringDesc = &phone_get_string_desc,
354 	.getVendorDesc = &phone_get_vendor_desc,
355 	.ppConfigDesc = phone_configs,
356 	.idVendor = USB_TEMPLATE_VENDOR,
357 	.idProduct = 0xb001,
358 	.bcdDevice = 0x0100,
359 	.bDeviceClass = UDCLASS_IN_INTERFACE,
360 	.bDeviceSubClass = 0,
361 	.bDeviceProtocol = 0,
362 	.iManufacturer = 0,
363 	.iProduct = INDEX_PHONE_PRODUCT,
364 	.iSerialNumber = 0,
365 };
366 
367 /*------------------------------------------------------------------------*
368  *      phone_get_vendor_desc
369  *
370  * Return values:
371  * NULL: Failure. No such vendor descriptor.
372  * Else: Success. Pointer to vendor descriptor is returned.
373  *------------------------------------------------------------------------*/
374 static const void *
375 phone_get_vendor_desc(const struct usb_device_request *req, uint16_t *plen)
376 {
377 	if ((req->bmRequestType == 0x81) && (req->bRequest == 0x06) &&
378 	    (req->wValue[0] == 0x00) && (req->wValue[1] == 0x22) &&
379 	    (req->wIndex[1] == 0) && (req->wIndex[0] == 3 /* iface */)) {
380 
381 		*plen = sizeof(phone_hid_descriptor);
382 		return (phone_hid_descriptor);
383 	}
384 	return (NULL);
385 }
386 
387 /*------------------------------------------------------------------------*
388  *	phone_get_string_desc
389  *
390  * Return values:
391  * NULL: Failure. No such string.
392  * Else: Success. Pointer to string descriptor is returned.
393  *------------------------------------------------------------------------*/
394 static const void *
395 phone_get_string_desc(uint16_t lang_id, uint8_t string_index)
396 {
397 	static const void *ptr[INDEX_PHONE_MAX] = {
398 		[INDEX_PHONE_LANG] = &usb_string_lang_en,
399 		[INDEX_PHONE_MIXER] = &string_phone_mixer,
400 		[INDEX_PHONE_RECORD] = &string_phone_record,
401 		[INDEX_PHONE_PLAYBACK] = &string_phone_playback,
402 		[INDEX_PHONE_PRODUCT] = &string_phone_product,
403 		[INDEX_PHONE_HID] = &string_phone_hid,
404 	};
405 
406 	if (string_index == 0) {
407 		return (&usb_string_lang_en);
408 	}
409 	if (lang_id != 0x0409) {
410 		return (NULL);
411 	}
412 	if (string_index < INDEX_PHONE_MAX) {
413 		return (ptr[string_index]);
414 	}
415 	return (NULL);
416 }
417