usb_template_phone.c revision 302408
145135Sobrien/* $FreeBSD: stable/11/sys/dev/usb/template/usb_template_phone.c 269567 2014-08-05 07:03:16Z hselasky $ */ 295811Sobrien/*- 345135Sobrien * Copyright (c) 2014 Hans Petter Selasky. All rights reserved. 4132738Skan * 545135Sobrien * Redistribution and use in source and binary forms, with or without 6132738Skan * modification, are permitted provided that the following conditions 745135Sobrien * are met: 845135Sobrien * 1. Redistributions of source code must retain the above copyright 945135Sobrien * notice, this list of conditions and the following disclaimer. 1045135Sobrien * 2. Redistributions in binary form must reproduce the above copyright 11132738Skan * notice, this list of conditions and the following disclaimer in the 1245135Sobrien * documentation and/or other materials provided with the distribution. 1345135Sobrien * 1445135Sobrien * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1545135Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1645135Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17132738Skan * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18169706Skan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19169706Skan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2045135Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2145135Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2245135Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2345135Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2495811Sobrien * SUCH DAMAGE. 2595811Sobrien */ 2695811Sobrien 2795811Sobrien/* 2845135Sobrien * This file contains the USB template for an USB phone device. 2952113Sobrien */ 3045135Sobrien 3195811Sobrien#ifdef USB_GLOBAL_INCLUDE_FILE 3295811Sobrien#include USB_GLOBAL_INCLUDE_FILE 3352113Sobrien#else 3446122Sobrien#include <sys/stdint.h> 3545135Sobrien#include <sys/stddef.h> 3673243Sobrien#include <sys/param.h> 3748474Sobrien#include <sys/queue.h> 3845135Sobrien#include <sys/types.h> 3958478Sobrien#include <sys/systm.h> 4073304Sobrien#include <sys/kernel.h> 4149854Sobrien#include <sys/bus.h> 4258478Sobrien#include <sys/module.h> 4373304Sobrien#include <sys/lock.h> 4449854Sobrien#include <sys/mutex.h> 45117420Skan#include <sys/condvar.h> 46117420Skan#include <sys/sysctl.h> 47117420Skan#include <sys/sx.h> 4858478Sobrien#include <sys/unistd.h> 4954807Sobrien#include <sys/callout.h> 5054807Sobrien#include <sys/malloc.h> 5195811Sobrien#include <sys/priv.h> 5295811Sobrien 5373304Sobrien#include <dev/usb/usb.h> 5495811Sobrien#include <dev/usb/usbdi.h> 5595811Sobrien#include <dev/usb/usb_core.h> 5695811Sobrien#include <dev/usb/usb_cdc.h> 5758478Sobrien 5895811Sobrien#include <dev/usb/template/usb_template.h> 5945135Sobrien#endif /* USB_GLOBAL_INCLUDE_FILE */ 6045458Sobrien 6158478Sobrienenum { 6258478Sobrien INDEX_PHONE_LANG, 6358478Sobrien INDEX_PHONE_MIXER, 6458478Sobrien INDEX_PHONE_RECORD, 6595811Sobrien INDEX_PHONE_PLAYBACK, 66229715Sandreast INDEX_PHONE_PRODUCT, 67182627Sobrien INDEX_PHONE_HID, 6858478Sobrien INDEX_PHONE_MAX, 6958478Sobrien}; 7058478Sobrien 7195811Sobrien#define STRING_PHONE_PRODUCT \ 7258478Sobrien "U\0S\0B\0 \0P\0h\0o\0n\0e\0 \0D\0e\0v\0i\0c\0e" 7395811Sobrien 74182627Sobrien#define STRING_PHONE_MIXER \ 75182627Sobrien "M\0i\0x\0e\0r\0 \0i\0n\0t\0e\0r\0f\0a\0c\0e" 76182627Sobrien 77182627Sobrien#define STRING_PHONE_RECORD \ 7858478Sobrien "R\0e\0c\0o\0r\0d\0 \0i\0n\0t\0e\0r\0f\0a\0c\0e" 7995811Sobrien 80117420Skan#define STRING_PHONE_PLAYBACK \ 81182627Sobrien "P\0l\0a\0y\0b\0a\0c\0k\0 \0i\0n\0t\0e\0r\0f\0a\0c\0e" 82182627Sobrien 83182627Sobrien#define STRING_PHONE_HID \ 84182627Sobrien "H\0I\0D\0 \0i\0n\0t\0e\0r\0f\0a\0c\0e" 85182627Sobrien 86182627Sobrien/* make the real string descriptors */ 8795811Sobrien 8895811SobrienUSB_MAKE_STRING_DESC(STRING_PHONE_MIXER, string_phone_mixer); 8995811SobrienUSB_MAKE_STRING_DESC(STRING_PHONE_RECORD, string_phone_record); 9095811SobrienUSB_MAKE_STRING_DESC(STRING_PHONE_PLAYBACK, string_phone_playback); 9195811SobrienUSB_MAKE_STRING_DESC(STRING_PHONE_PRODUCT, string_phone_product); 9295811SobrienUSB_MAKE_STRING_DESC(STRING_PHONE_HID, string_phone_hid); 9345135Sobrien 9445135Sobrien/* prototypes */ 9558478Sobrien 9658478Sobrien/* 9758478Sobrien * Phone Mixer description structures 9858478Sobrien * 9958478Sobrien * Some of the phone descriptors were dumped from no longer in 10058478Sobrien * production Yealink VOIP USB phone adapter: 10158478Sobrien */ 10258478Sobrienstatic uint8_t phone_hid_descriptor[] = { 10345135Sobrien 0x05, 0x0b, 0x09, 0x01, 0xa1, 0x01, 0x05, 0x09, 10445135Sobrien 0x19, 0x01, 0x29, 0x3f, 0x15, 0x00, 0x25, 0x01, 105146939Skan 0x75, 0x01, 0x95, 0x80, 0x81, 0x00, 0x05, 0x08, 106182627Sobrien 0x19, 0x01, 0x29, 0x10, 0x15, 0x00, 0x25, 0x01, 107146939Skan 0x75, 0x01, 0x95, 0x80, 0x91, 0x00, 0xc0 108169706Skan}; 109 110static const uint8_t phone_raw_desc_0[] = { 111 0x0a, 0x24, 0x01, 0x00, 0x01, 0x4a, 0x00, 0x02, 112 0x01, 0x02 113}; 114 115static const uint8_t phone_raw_desc_1[] = { 116 0x0c, 0x24, 0x02, 0x01, 0x01, 0x02, 0x00, 0x01, 117 0x00, 0x00, 0x00, 0x00 118}; 119 120static const uint8_t phone_raw_desc_2[] = { 121 0x0c, 0x24, 0x02, 0x02, 0x01, 0x01, 0x00, 0x01, 122 0x00, 0x00, 0x00, 0x00 123}; 124 125static const uint8_t phone_raw_desc_3[] = { 126 0x09, 0x24, 0x03, 0x03, 0x01, 0x03, 0x00, 0x06, 127 0x00 128}; 129 130static const uint8_t phone_raw_desc_4[] = { 131 0x09, 0x24, 0x03, 0x04, 0x01, 0x01, 0x00, 0x05, 132 0x00 133}; 134 135static const uint8_t phone_raw_desc_5[] = { 136 0x0b, 0x24, 0x06, 0x05, 0x01, 0x02, 0x03, 0x00, 137 0x03, 0x00, 0x00 138}; 139 140static const uint8_t phone_raw_desc_6[] = { 141 0x0b, 0x24, 0x06, 0x06, 0x02, 0x02, 0x03, 0x00, 142 0x03, 0x00, 0x00 143}; 144 145static const void *phone_raw_iface_0_desc[] = { 146 phone_raw_desc_0, 147 phone_raw_desc_1, 148 phone_raw_desc_2, 149 phone_raw_desc_3, 150 phone_raw_desc_4, 151 phone_raw_desc_5, 152 phone_raw_desc_6, 153 NULL, 154}; 155 156static const struct usb_temp_interface_desc phone_iface_0 = { 157 .ppEndpoints = NULL, /* no endpoints */ 158 .ppRawDesc = phone_raw_iface_0_desc, 159 .bInterfaceClass = 1, 160 .bInterfaceSubClass = 1, 161 .bInterfaceProtocol = 0, 162 .iInterface = INDEX_PHONE_MIXER, 163}; 164 165static const uint8_t phone_raw_desc_20[] = { 166 0x07, 0x24, 0x01, 0x04, 0x01, 0x01, 0x00 167}; 168 169static const uint8_t phone_raw_desc_21[] = { 170 0x0b, 0x24, 0x02, 0x01, 0x01, 0x02, 0x10, 0x01, 171 /* 8kHz */ 172 0x40, 0x1f, 0x00 173}; 174 175static const uint8_t phone_raw_desc_22[] = { 176 0x07, 0x25, 0x01, 0x00, 0x00, 0x00, 0x00 177}; 178 179static const void *phone_raw_iface_1_desc[] = { 180 phone_raw_desc_20, 181 phone_raw_desc_21, 182 NULL, 183}; 184 185static const void *phone_raw_ep_1_desc[] = { 186 phone_raw_desc_22, 187 NULL, 188}; 189 190static const struct usb_temp_packet_size phone_isoc_mps = { 191 .mps[USB_SPEED_FULL] = 0x10, 192 .mps[USB_SPEED_HIGH] = 0x10, 193}; 194 195static const struct usb_temp_interval phone_isoc_interval = { 196 .bInterval[USB_SPEED_FULL] = 1, /* 1:1 */ 197 .bInterval[USB_SPEED_HIGH] = 4, /* 1:8 */ 198}; 199 200static 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, 206}; 207 208static const struct usb_temp_endpoint_desc *phone_iface_1_ep[] = { 209 &phone_isoc_in_ep, 210 NULL, 211}; 212 213static const struct usb_temp_interface_desc phone_iface_1_alt_0 = { 214 .ppEndpoints = NULL, /* no endpoints */ 215 .ppRawDesc = NULL, /* no raw descriptors */ 216 .bInterfaceClass = 1, 217 .bInterfaceSubClass = 2, 218 .bInterfaceProtocol = 0, 219 .iInterface = INDEX_PHONE_PLAYBACK, 220}; 221 222static 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 = 1, 226 .bInterfaceSubClass = 2, 227 .bInterfaceProtocol = 0, 228 .iInterface = INDEX_PHONE_PLAYBACK, 229 .isAltInterface = 1, /* this is an alternate setting */ 230}; 231 232static const uint8_t phone_raw_desc_30[] = { 233 0x07, 0x24, 0x01, 0x02, 0x01, 0x01, 0x00 234}; 235 236static const uint8_t phone_raw_desc_31[] = { 237 0x0b, 0x24, 0x02, 0x01, 0x01, 0x02, 0x10, 0x01, 238 /* 8kHz */ 239 0x40, 0x1f, 0x00 240}; 241 242static const uint8_t phone_raw_desc_32[] = { 243 0x07, 0x25, 0x01, 0x00, 0x00, 0x00, 0x00 244}; 245 246static const void *phone_raw_iface_2_desc[] = { 247 phone_raw_desc_30, 248 phone_raw_desc_31, 249 NULL, 250}; 251 252static const void *phone_raw_ep_2_desc[] = { 253 phone_raw_desc_32, 254 NULL, 255}; 256 257static 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, 263}; 264 265static const struct usb_temp_endpoint_desc *phone_iface_2_ep[] = { 266 &phone_isoc_out_ep, 267 NULL, 268}; 269 270static const struct usb_temp_interface_desc phone_iface_2_alt_0 = { 271 .ppEndpoints = NULL, /* no endpoints */ 272 .ppRawDesc = NULL, /* no raw descriptors */ 273 .bInterfaceClass = 1, 274 .bInterfaceSubClass = 2, 275 .bInterfaceProtocol = 0, 276 .iInterface = INDEX_PHONE_RECORD, 277}; 278 279static 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 = 1, 283 .bInterfaceSubClass = 2, 284 .bInterfaceProtocol = 0, 285 .iInterface = INDEX_PHONE_RECORD, 286 .isAltInterface = 1, /* this is an alternate setting */ 287}; 288 289static const uint8_t phone_hid_raw_desc_0[] = { 290 0x09, 0x21, 0x00, 0x01, 0x00, 0x01, 0x22, sizeof(phone_hid_descriptor), 291 0x00 292}; 293 294static const void *phone_hid_desc_0[] = { 295 phone_hid_raw_desc_0, 296 NULL, 297}; 298 299static const struct usb_temp_packet_size phone_hid_mps = { 300 .mps[USB_SPEED_FULL] = 0x10, 301 .mps[USB_SPEED_HIGH] = 0x10, 302}; 303 304static const struct usb_temp_interval phone_hid_interval = { 305 .bInterval[USB_SPEED_FULL] = 2, /* 2ms */ 306 .bInterval[USB_SPEED_HIGH] = 2, /* 2ms */ 307}; 308 309static 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, 314}; 315 316static const struct usb_temp_endpoint_desc *phone_iface_3_ep[] = { 317 &phone_hid_in_ep, 318 NULL, 319}; 320 321static const struct usb_temp_interface_desc phone_iface_3 = { 322 .ppEndpoints = phone_iface_3_ep, 323 .ppRawDesc = phone_hid_desc_0, 324 .bInterfaceClass = 3, 325 .bInterfaceSubClass = 0, 326 .bInterfaceProtocol = 0, 327 .iInterface = INDEX_PHONE_HID, 328}; 329 330static const struct usb_temp_interface_desc *phone_interfaces[] = { 331 &phone_iface_0, 332 &phone_iface_1_alt_0, 333 &phone_iface_1_alt_1, 334 &phone_iface_2_alt_0, 335 &phone_iface_2_alt_1, 336 &phone_iface_3, 337 NULL, 338}; 339 340static 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 = INDEX_PHONE_PRODUCT, 345}; 346 347static const struct usb_temp_config_desc *phone_configs[] = { 348 &phone_config_desc, 349 NULL, 350}; 351 352static usb_temp_get_string_desc_t phone_get_string_desc; 353static usb_temp_get_vendor_desc_t phone_get_vendor_desc; 354 355const 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, 360 .idProduct = 0xb001, 361 .bcdDevice = 0x0100, 362 .bDeviceClass = UDCLASS_IN_INTERFACE, 363 .bDeviceSubClass = 0, 364 .bDeviceProtocol = 0, 365 .iManufacturer = 0, 366 .iProduct = INDEX_PHONE_PRODUCT, 367 .iSerialNumber = 0, 368}; 369 370/*------------------------------------------------------------------------* 371 * phone_get_vendor_desc 372 * 373 * Return values: 374 * NULL: Failure. No such vendor descriptor. 375 * Else: Success. Pointer to vendor descriptor is returned. 376 *------------------------------------------------------------------------*/ 377static const void * 378phone_get_vendor_desc(const struct usb_device_request *req, uint16_t *plen) 379{ 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 */)) { 383 384 *plen = sizeof(phone_hid_descriptor); 385 return (phone_hid_descriptor); 386 } 387 return (NULL); 388} 389 390/*------------------------------------------------------------------------* 391 * phone_get_string_desc 392 * 393 * Return values: 394 * NULL: Failure. No such string. 395 * Else: Success. Pointer to string descriptor is returned. 396 *------------------------------------------------------------------------*/ 397static const void * 398phone_get_string_desc(uint16_t lang_id, uint8_t string_index) 399{ 400 static const void *ptr[INDEX_PHONE_MAX] = { 401 [INDEX_PHONE_LANG] = &usb_string_lang_en, 402 [INDEX_PHONE_MIXER] = &string_phone_mixer, 403 [INDEX_PHONE_RECORD] = &string_phone_record, 404 [INDEX_PHONE_PLAYBACK] = &string_phone_playback, 405 [INDEX_PHONE_PRODUCT] = &string_phone_product, 406 [INDEX_PHONE_HID] = &string_phone_hid, 407 }; 408 409 if (string_index == 0) { 410 return (&usb_string_lang_en); 411 } 412 if (lang_id != 0x0409) { 413 return (NULL); 414 } 415 if (string_index < INDEX_PHONE_MAX) { 416 return (ptr[string_index]); 417 } 418 return (NULL); 419} 420