1 /* $FreeBSD$ */ 2 /*- 3 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 4 * 5 * Copyright (c) 2008 Hans Petter Selasky <hselasky@FreeBSD.org> 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 /* 31 * This file contains the USB templates for an USB Media Transfer 32 * Protocol device. 33 * 34 * NOTE: It is common practice that MTP devices use some dummy 35 * descriptor cludges to be automatically detected by the host 36 * operating system. These descriptors are documented in the LibMTP 37 * library at sourceforge.net. The alternative is to supply the host 38 * operating system the VID and PID of your device. 39 */ 40 41 #ifdef USB_GLOBAL_INCLUDE_FILE 42 #include USB_GLOBAL_INCLUDE_FILE 43 #else 44 #include <sys/stdint.h> 45 #include <sys/stddef.h> 46 #include <sys/param.h> 47 #include <sys/queue.h> 48 #include <sys/types.h> 49 #include <sys/systm.h> 50 #include <sys/kernel.h> 51 #include <sys/bus.h> 52 #include <sys/module.h> 53 #include <sys/lock.h> 54 #include <sys/mutex.h> 55 #include <sys/condvar.h> 56 #include <sys/sysctl.h> 57 #include <sys/sx.h> 58 #include <sys/unistd.h> 59 #include <sys/callout.h> 60 #include <sys/malloc.h> 61 #include <sys/priv.h> 62 63 #include <dev/usb/usb.h> 64 #include <dev/usb/usbdi.h> 65 #include <dev/usb/usb_core.h> 66 67 #include <dev/usb/template/usb_template.h> 68 #endif /* USB_GLOBAL_INCLUDE_FILE */ 69 70 #define MTP_BREQUEST 0x08 71 72 enum { 73 STRING_LANG_INDEX, 74 STRING_MTP_DATA_INDEX, 75 STRING_MTP_CONFIG_INDEX, 76 STRING_MTP_VENDOR_INDEX, 77 STRING_MTP_PRODUCT_INDEX, 78 STRING_MTP_SERIAL_INDEX, 79 STRING_MTP_MAX, 80 }; 81 82 #define STRING_MTP_DATA \ 83 "U\0S\0B\0 \0M\0T\0P\0 \0I\0n\0t\0e\0r\0f\0a\0c\0e" 84 85 #define STRING_MTP_CONFIG \ 86 "D\0e\0f\0a\0u\0l\0t\0 \0c\0o\0n\0f\0i\0g" 87 88 #define STRING_MTP_VENDOR \ 89 "F\0r\0e\0e\0B\0S\0D\0 \0f\0o\0u\0n\0d\0a\0t\0i\0o\0n" 90 91 #define STRING_MTP_PRODUCT \ 92 "U\0S\0B\0 \0M\0T\0P" 93 94 #define STRING_MTP_SERIAL \ 95 "J\0u\0n\0e\0 \0002\0000\0000\08" 96 97 /* make the real string descriptors */ 98 99 USB_MAKE_STRING_DESC(STRING_MTP_DATA, string_mtp_data); 100 USB_MAKE_STRING_DESC(STRING_MTP_CONFIG, string_mtp_config); 101 USB_MAKE_STRING_DESC(STRING_MTP_VENDOR, string_mtp_vendor); 102 USB_MAKE_STRING_DESC(STRING_MTP_PRODUCT, string_mtp_product); 103 USB_MAKE_STRING_DESC(STRING_MTP_SERIAL, string_mtp_serial); 104 105 /* prototypes */ 106 107 static usb_temp_get_string_desc_t mtp_get_string_desc; 108 static usb_temp_get_vendor_desc_t mtp_get_vendor_desc; 109 110 static const struct usb_temp_packet_size bulk_mps = { 111 .mps[USB_SPEED_FULL] = 64, 112 .mps[USB_SPEED_HIGH] = 512, 113 }; 114 115 static const struct usb_temp_packet_size intr_mps = { 116 .mps[USB_SPEED_FULL] = 64, 117 .mps[USB_SPEED_HIGH] = 64, 118 }; 119 120 static const struct usb_temp_endpoint_desc bulk_out_ep = { 121 .pPacketSize = &bulk_mps, 122 #ifdef USB_HIP_OUT_EP_0 123 .bEndpointAddress = USB_HIP_OUT_EP_0, 124 #else 125 .bEndpointAddress = UE_DIR_OUT, 126 #endif 127 .bmAttributes = UE_BULK, 128 }; 129 130 static const struct usb_temp_endpoint_desc intr_in_ep = { 131 .pPacketSize = &intr_mps, 132 .bEndpointAddress = UE_DIR_IN, 133 .bmAttributes = UE_INTERRUPT, 134 }; 135 136 static const struct usb_temp_endpoint_desc bulk_in_ep = { 137 .pPacketSize = &bulk_mps, 138 #ifdef USB_HIP_IN_EP_0 139 .bEndpointAddress = USB_HIP_IN_EP_0, 140 #else 141 .bEndpointAddress = UE_DIR_IN, 142 #endif 143 .bmAttributes = UE_BULK, 144 }; 145 146 static const struct usb_temp_endpoint_desc *mtp_data_endpoints[] = { 147 &bulk_in_ep, 148 &bulk_out_ep, 149 &intr_in_ep, 150 NULL, 151 }; 152 153 static const struct usb_temp_interface_desc mtp_data_interface = { 154 .ppEndpoints = mtp_data_endpoints, 155 .bInterfaceClass = UICLASS_IMAGE, 156 .bInterfaceSubClass = UISUBCLASS_SIC, /* Still Image Class */ 157 .bInterfaceProtocol = 1, /* PIMA 15740 */ 158 .iInterface = STRING_MTP_DATA_INDEX, 159 }; 160 161 static const struct usb_temp_interface_desc *mtp_interfaces[] = { 162 &mtp_data_interface, 163 NULL, 164 }; 165 166 static const struct usb_temp_config_desc mtp_config_desc = { 167 .ppIfaceDesc = mtp_interfaces, 168 .bmAttributes = UC_BUS_POWERED, 169 .bMaxPower = 25, /* 50 mA */ 170 .iConfiguration = STRING_MTP_CONFIG_INDEX, 171 }; 172 173 static const struct usb_temp_config_desc *mtp_configs[] = { 174 &mtp_config_desc, 175 NULL, 176 }; 177 178 const struct usb_temp_device_desc usb_template_mtp = { 179 .getStringDesc = &mtp_get_string_desc, 180 .getVendorDesc = &mtp_get_vendor_desc, 181 .ppConfigDesc = mtp_configs, 182 .idVendor = USB_TEMPLATE_VENDOR, 183 .idProduct = 0x0011, 184 .bcdDevice = 0x0100, 185 .bDeviceClass = 0, 186 .bDeviceSubClass = 0, 187 .bDeviceProtocol = 0, 188 .iManufacturer = STRING_MTP_VENDOR_INDEX, 189 .iProduct = STRING_MTP_PRODUCT_INDEX, 190 .iSerialNumber = STRING_MTP_SERIAL_INDEX, 191 }; 192 193 /*------------------------------------------------------------------------* 194 * mtp_get_vendor_desc 195 * 196 * Return values: 197 * NULL: Failure. No such vendor descriptor. 198 * Else: Success. Pointer to vendor descriptor is returned. 199 *------------------------------------------------------------------------*/ 200 static const void * 201 mtp_get_vendor_desc(const struct usb_device_request *req, uint16_t *plen) 202 { 203 static const uint8_t dummy_desc[0x28] = { 204 0x28, 0, 0, 0, 0, 1, 4, 0, 205 1, 0, 0, 0, 0, 0, 0, 0, 206 0, 1, 0x4D, 0x54, 0x50, 0, 0, 0, 207 0, 0, 0, 0, 0, 0, 0, 0, 208 0, 0, 0, 0, 0, 0, 0, 0, 209 }; 210 211 if ((req->bmRequestType == UT_READ_VENDOR_DEVICE) && 212 (req->bRequest == MTP_BREQUEST) && (req->wValue[0] == 0) && 213 (req->wValue[1] == 0) && (req->wIndex[1] == 0) && 214 ((req->wIndex[0] == 4) || (req->wIndex[0] == 5))) { 215 /* 216 * By returning this descriptor LibMTP will 217 * automatically pickup our device. 218 */ 219 return (dummy_desc); 220 } 221 return (NULL); 222 } 223 224 /*------------------------------------------------------------------------* 225 * mtp_get_string_desc 226 * 227 * Return values: 228 * NULL: Failure. No such string. 229 * Else: Success. Pointer to string descriptor is returned. 230 *------------------------------------------------------------------------*/ 231 static const void * 232 mtp_get_string_desc(uint16_t lang_id, uint8_t string_index) 233 { 234 static const void *ptr[STRING_MTP_MAX] = { 235 [STRING_LANG_INDEX] = &usb_string_lang_en, 236 [STRING_MTP_DATA_INDEX] = &string_mtp_data, 237 [STRING_MTP_CONFIG_INDEX] = &string_mtp_config, 238 [STRING_MTP_VENDOR_INDEX] = &string_mtp_vendor, 239 [STRING_MTP_PRODUCT_INDEX] = &string_mtp_product, 240 [STRING_MTP_SERIAL_INDEX] = &string_mtp_serial, 241 }; 242 243 static const uint8_t dummy_desc[0x12] = { 244 0x12, 0x03, 0x4D, 0x00, 0x53, 0x00, 0x46, 0x00, 245 0x54, 0x00, 0x31, 0x00, 0x30, 0x00, 0x30, 0x00, 246 MTP_BREQUEST, 0x00, 247 }; 248 249 if (string_index == 0xEE) { 250 /* 251 * By returning this string LibMTP will automatically 252 * pickup our device. 253 */ 254 return (dummy_desc); 255 } 256 if (string_index == 0) { 257 return (&usb_string_lang_en); 258 } 259 if (lang_id != 0x0409) { 260 return (NULL); 261 } 262 if (string_index < STRING_MTP_MAX) { 263 return (ptr[string_index]); 264 } 265 return (NULL); 266 } 267