3 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 * Copyright (c) 2014 Hans Petter Selasky
6 * Copyright (c) 2018 The FreeBSD Foundation
9 * Portions of this software were developed by Edward Tomasz Napierala
10 * under sponsorship from the FreeBSD Foundation.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
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.
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
35 * This file contains the USB template for an USB phone device.
38 #ifdef USB_GLOBAL_INCLUDE_FILE
39 #include USB_GLOBAL_INCLUDE_FILE
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>
49 #include <sys/module.h>
51 #include <sys/mutex.h>
52 #include <sys/condvar.h>
53 #include <sys/sysctl.h>
55 #include <sys/unistd.h>
56 #include <sys/callout.h>
57 #include <sys/malloc.h>
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>
67 #include <dev/usb/template/usb_template.h>
68 #endif /* USB_GLOBAL_INCLUDE_FILE */
80 #define PHONE_DEFAULT_MIXER "Mixer interface"
81 #define PHONE_DEFAULT_RECORD "Record interface"
82 #define PHONE_DEFAULT_PLAYBACK "Playback interface"
83 #define PHONE_DEFAULT_PRODUCT "USB Phone Device"
84 #define PHONE_DEFAULT_HID "HID interface"
86 static struct usb_string_descriptor phone_mixer;
87 static struct usb_string_descriptor phone_record;
88 static struct usb_string_descriptor phone_playback;
89 static struct usb_string_descriptor phone_product;
90 static struct usb_string_descriptor phone_hid;
92 static struct sysctl_ctx_list phone_ctx_list;
97 * Phone Mixer description structures
99 * Some of the phone descriptors were dumped from no longer in
100 * production Yealink VOIP USB phone adapter:
102 static uint8_t phone_hid_descriptor[] = {
103 0x05, 0x0b, 0x09, 0x01, 0xa1, 0x01, 0x05, 0x09,
104 0x19, 0x01, 0x29, 0x3f, 0x15, 0x00, 0x25, 0x01,
105 0x75, 0x01, 0x95, 0x80, 0x81, 0x00, 0x05, 0x08,
106 0x19, 0x01, 0x29, 0x10, 0x15, 0x00, 0x25, 0x01,
107 0x75, 0x01, 0x95, 0x80, 0x91, 0x00, 0xc0
110 static const uint8_t phone_raw_desc_0[] = {
111 0x0a, 0x24, 0x01, 0x00, 0x01, 0x4a, 0x00, 0x02,
115 static const uint8_t phone_raw_desc_1[] = {
116 0x0c, 0x24, 0x02, 0x01, 0x01, 0x02, 0x00, 0x01,
117 0x00, 0x00, 0x00, 0x00
120 static const uint8_t phone_raw_desc_2[] = {
121 0x0c, 0x24, 0x02, 0x02, 0x01, 0x01, 0x00, 0x01,
122 0x00, 0x00, 0x00, 0x00
125 static const uint8_t phone_raw_desc_3[] = {
126 0x09, 0x24, 0x03, 0x03, 0x01, 0x03, 0x00, 0x06,
130 static const uint8_t phone_raw_desc_4[] = {
131 0x09, 0x24, 0x03, 0x04, 0x01, 0x01, 0x00, 0x05,
135 static const uint8_t phone_raw_desc_5[] = {
136 0x0b, 0x24, 0x06, 0x05, 0x01, 0x02, 0x03, 0x00,
140 static const uint8_t phone_raw_desc_6[] = {
141 0x0b, 0x24, 0x06, 0x06, 0x02, 0x02, 0x03, 0x00,
145 static const void *phone_raw_iface_0_desc[] = {
156 static const struct usb_temp_interface_desc phone_iface_0 = {
157 .ppEndpoints = NULL, /* no endpoints */
158 .ppRawDesc = phone_raw_iface_0_desc,
159 .bInterfaceClass = UICLASS_AUDIO,
160 .bInterfaceSubClass = UISUBCLASS_AUDIOCONTROL,
161 .bInterfaceProtocol = 0,
162 .iInterface = PHONE_MIXER_INDEX,
165 static const uint8_t phone_raw_desc_20[] = {
166 0x07, 0x24, 0x01, 0x04, 0x01, 0x01, 0x00
169 static const uint8_t phone_raw_desc_21[] = {
170 0x0b, 0x24, 0x02, 0x01, 0x01, 0x02, 0x10, 0x01,
175 static const uint8_t phone_raw_desc_22[] = {
176 0x07, 0x25, 0x01, 0x00, 0x00, 0x00, 0x00
179 static const void *phone_raw_iface_1_desc[] = {
185 static const void *phone_raw_ep_1_desc[] = {
190 static const struct usb_temp_packet_size phone_isoc_mps = {
191 .mps[USB_SPEED_FULL] = 0x10,
192 .mps[USB_SPEED_HIGH] = 0x10,
195 static const struct usb_temp_interval phone_isoc_interval = {
196 .bInterval[USB_SPEED_FULL] = 1, /* 1:1 */
197 .bInterval[USB_SPEED_HIGH] = 4, /* 1:8 */
200 static const struct usb_temp_endpoint_desc phone_isoc_in_ep = {
201 .ppRawDesc = phone_raw_ep_1_desc,
202 .pPacketSize = &phone_isoc_mps,
203 .pIntervals = &phone_isoc_interval,
204 .bEndpointAddress = UE_DIR_IN,
205 .bmAttributes = UE_ISOCHRONOUS,
208 static const struct usb_temp_endpoint_desc *phone_iface_1_ep[] = {
213 static const struct usb_temp_interface_desc phone_iface_1_alt_0 = {
214 .ppEndpoints = NULL, /* no endpoints */
215 .ppRawDesc = NULL, /* no raw descriptors */
216 .bInterfaceClass = UICLASS_AUDIO,
217 .bInterfaceSubClass = UISUBCLASS_AUDIOSTREAM,
218 .bInterfaceProtocol = 0,
219 .iInterface = PHONE_PLAYBACK_INDEX,
222 static const struct usb_temp_interface_desc phone_iface_1_alt_1 = {
223 .ppEndpoints = phone_iface_1_ep,
224 .ppRawDesc = phone_raw_iface_1_desc,
225 .bInterfaceClass = UICLASS_AUDIO,
226 .bInterfaceSubClass = UISUBCLASS_AUDIOSTREAM,
227 .bInterfaceProtocol = 0,
228 .iInterface = PHONE_PLAYBACK_INDEX,
229 .isAltInterface = 1, /* this is an alternate setting */
232 static const uint8_t phone_raw_desc_30[] = {
233 0x07, 0x24, 0x01, 0x02, 0x01, 0x01, 0x00
236 static const uint8_t phone_raw_desc_31[] = {
237 0x0b, 0x24, 0x02, 0x01, 0x01, 0x02, 0x10, 0x01,
242 static const uint8_t phone_raw_desc_32[] = {
243 0x07, 0x25, 0x01, 0x00, 0x00, 0x00, 0x00
246 static const void *phone_raw_iface_2_desc[] = {
252 static const void *phone_raw_ep_2_desc[] = {
257 static const struct usb_temp_endpoint_desc phone_isoc_out_ep = {
258 .ppRawDesc = phone_raw_ep_2_desc,
259 .pPacketSize = &phone_isoc_mps,
260 .pIntervals = &phone_isoc_interval,
261 .bEndpointAddress = UE_DIR_OUT,
262 .bmAttributes = UE_ISOCHRONOUS,
265 static const struct usb_temp_endpoint_desc *phone_iface_2_ep[] = {
270 static const struct usb_temp_interface_desc phone_iface_2_alt_0 = {
271 .ppEndpoints = NULL, /* no endpoints */
272 .ppRawDesc = NULL, /* no raw descriptors */
273 .bInterfaceClass = UICLASS_AUDIO,
274 .bInterfaceSubClass = UISUBCLASS_AUDIOSTREAM,
275 .bInterfaceProtocol = 0,
276 .iInterface = PHONE_RECORD_INDEX,
279 static const struct usb_temp_interface_desc phone_iface_2_alt_1 = {
280 .ppEndpoints = phone_iface_2_ep,
281 .ppRawDesc = phone_raw_iface_2_desc,
282 .bInterfaceClass = UICLASS_AUDIO,
283 .bInterfaceSubClass = UISUBCLASS_AUDIOSTREAM,
284 .bInterfaceProtocol = 0,
285 .iInterface = PHONE_RECORD_INDEX,
286 .isAltInterface = 1, /* this is an alternate setting */
289 static const uint8_t phone_hid_raw_desc_0[] = {
290 0x09, 0x21, 0x00, 0x01, 0x00, 0x01, 0x22, sizeof(phone_hid_descriptor),
294 static const void *phone_hid_desc_0[] = {
295 phone_hid_raw_desc_0,
299 static const struct usb_temp_packet_size phone_hid_mps = {
300 .mps[USB_SPEED_FULL] = 0x10,
301 .mps[USB_SPEED_HIGH] = 0x10,
304 static const struct usb_temp_interval phone_hid_interval = {
305 .bInterval[USB_SPEED_FULL] = 2, /* 2ms */
306 .bInterval[USB_SPEED_HIGH] = 2, /* 2ms */
309 static const struct usb_temp_endpoint_desc phone_hid_in_ep = {
310 .pPacketSize = &phone_hid_mps,
311 .pIntervals = &phone_hid_interval,
312 .bEndpointAddress = UE_DIR_IN,
313 .bmAttributes = UE_INTERRUPT,
316 static const struct usb_temp_endpoint_desc *phone_iface_3_ep[] = {
321 static const struct usb_temp_interface_desc phone_iface_3 = {
322 .ppEndpoints = phone_iface_3_ep,
323 .ppRawDesc = phone_hid_desc_0,
324 .bInterfaceClass = UICLASS_HID,
325 .bInterfaceSubClass = 0,
326 .bInterfaceProtocol = 0,
327 .iInterface = PHONE_HID_INDEX,
330 static const struct usb_temp_interface_desc *phone_interfaces[] = {
332 &phone_iface_1_alt_0,
333 &phone_iface_1_alt_1,
334 &phone_iface_2_alt_0,
335 &phone_iface_2_alt_1,
340 static const struct usb_temp_config_desc phone_config_desc = {
341 .ppIfaceDesc = phone_interfaces,
342 .bmAttributes = UC_BUS_POWERED,
343 .bMaxPower = 25, /* 50 mA */
344 .iConfiguration = PHONE_PRODUCT_INDEX,
347 static const struct usb_temp_config_desc *phone_configs[] = {
352 static usb_temp_get_string_desc_t phone_get_string_desc;
353 static usb_temp_get_vendor_desc_t phone_get_vendor_desc;
355 struct usb_temp_device_desc usb_template_phone = {
356 .getStringDesc = &phone_get_string_desc,
357 .getVendorDesc = &phone_get_vendor_desc,
358 .ppConfigDesc = phone_configs,
359 .idVendor = USB_TEMPLATE_VENDOR,
362 .bDeviceClass = UDCLASS_IN_INTERFACE,
363 .bDeviceSubClass = 0,
364 .bDeviceProtocol = 0,
366 .iProduct = PHONE_PRODUCT_INDEX,
370 /*------------------------------------------------------------------------*
371 * phone_get_vendor_desc
374 * NULL: Failure. No such vendor descriptor.
375 * Else: Success. Pointer to vendor descriptor is returned.
376 *------------------------------------------------------------------------*/
378 phone_get_vendor_desc(const struct usb_device_request *req, uint16_t *plen)
380 if ((req->bmRequestType == 0x81) && (req->bRequest == 0x06) &&
381 (req->wValue[0] == 0x00) && (req->wValue[1] == 0x22) &&
382 (req->wIndex[1] == 0) && (req->wIndex[0] == 3 /* iface */)) {
384 *plen = sizeof(phone_hid_descriptor);
385 return (phone_hid_descriptor);
390 /*------------------------------------------------------------------------*
391 * phone_get_string_desc
394 * NULL: Failure. No such string.
395 * Else: Success. Pointer to string descriptor is returned.
396 *------------------------------------------------------------------------*/
398 phone_get_string_desc(uint16_t lang_id, uint8_t string_index)
400 static const void *ptr[PHONE_MAX_INDEX] = {
401 [PHONE_LANG_INDEX] = &usb_string_lang_en,
402 [PHONE_MIXER_INDEX] = &phone_mixer,
403 [PHONE_RECORD_INDEX] = &phone_record,
404 [PHONE_PLAYBACK_INDEX] = &phone_playback,
405 [PHONE_PRODUCT_INDEX] = &phone_product,
406 [PHONE_HID_INDEX] = &phone_hid,
409 if (string_index == 0) {
410 return (&usb_string_lang_en);
412 if (lang_id != 0x0409) {
415 if (string_index < PHONE_MAX_INDEX) {
416 return (ptr[string_index]);
422 phone_init(void *arg __unused)
424 struct sysctl_oid *parent;
427 usb_make_str_desc(&phone_mixer, sizeof(phone_mixer),
428 PHONE_DEFAULT_MIXER);
429 usb_make_str_desc(&phone_record, sizeof(phone_record),
430 PHONE_DEFAULT_RECORD);
431 usb_make_str_desc(&phone_playback, sizeof(phone_playback),
432 PHONE_DEFAULT_PLAYBACK);
433 usb_make_str_desc(&phone_product, sizeof(phone_product),
434 PHONE_DEFAULT_PRODUCT);
435 usb_make_str_desc(&phone_hid, sizeof(phone_hid),
438 snprintf(parent_name, sizeof(parent_name), "%d", USB_TEMP_PHONE);
439 sysctl_ctx_init(&phone_ctx_list);
441 parent = SYSCTL_ADD_NODE(&phone_ctx_list,
442 SYSCTL_STATIC_CHILDREN(_hw_usb_templates), OID_AUTO,
443 parent_name, CTLFLAG_RW,
444 0, "USB Phone device side template");
445 SYSCTL_ADD_U16(&phone_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
446 "vendor_id", CTLFLAG_RWTUN,
447 &usb_template_cdce.idVendor, 1, "Vendor identifier");
448 SYSCTL_ADD_U16(&phone_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
449 "product_id", CTLFLAG_RWTUN,
450 &usb_template_cdce.idProduct, 1, "Product identifier");
452 SYSCTL_ADD_PROC(&phone_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
453 "mixer", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
454 &phone_mixer, sizeof(phone_mixer), usb_temp_sysctl,
455 "A", "Mixer interface string");
456 SYSCTL_ADD_PROC(&phone_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
457 "record", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
458 &phone_record, sizeof(phone_record), usb_temp_sysctl,
459 "A", "Record interface string");
460 SYSCTL_ADD_PROC(&phone_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
461 "playback", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
462 &phone_playback, sizeof(phone_playback), usb_temp_sysctl,
463 "A", "Playback interface string");
465 SYSCTL_ADD_PROC(&phone_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
466 "product", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
467 &phone_product, sizeof(phone_product), usb_temp_sysctl,
468 "A", "Product string");
469 SYSCTL_ADD_PROC(&phone_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
470 "hid", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
471 &phone_hid, sizeof(phone_hid), usb_temp_sysctl,
472 "A", "HID interface string");
476 phone_uninit(void *arg __unused)
479 sysctl_ctx_free(&phone_ctx_list);
482 SYSINIT(phone_init, SI_SUB_LOCK, SI_ORDER_FIRST, phone_init, NULL);
483 SYSUNINIT(phone_init, SI_SUB_LOCK, SI_ORDER_FIRST, phone_uninit, NULL);