1 #include <sys/cdefs.h> 2 __FBSDID("$FreeBSD$"); 3 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 Message 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 #include <dev/usb/usb.h> 42 #include <dev/usb/usb_mfunc.h> 43 44 #include <dev/usb/usb_core.h> 45 46 #include <dev/usb/template/usb_template.h> 47 48 #define MTP_BREQUEST 0x08 49 50 enum { 51 STRING_LANG_INDEX, 52 STRING_MTP_DATA_INDEX, 53 STRING_MTP_CONFIG_INDEX, 54 STRING_MTP_VENDOR_INDEX, 55 STRING_MTP_PRODUCT_INDEX, 56 STRING_MTP_SERIAL_INDEX, 57 STRING_MTP_MAX, 58 }; 59 60 #define STRING_LANG \ 61 0x09, 0x04, /* American English */ 62 63 #define STRING_MTP_DATA \ 64 'U', 0, 'S', 0, 'B', 0, ' ', 0, \ 65 'M', 0, 'T', 0, 'P', 0, \ 66 ' ', 0, 'I', 0, 'n', 0, 't', 0, \ 67 'e', 0, 'r', 0, 'f', 0, 'a', 0, \ 68 'c', 0, 'e', 0, 69 70 #define STRING_MTP_CONFIG \ 71 'D', 0, 'e', 0, 'f', 0, 'a', 0, \ 72 'u', 0, 'l', 0, 't', 0, ' ', 0, \ 73 'c', 0, 'o', 0, 'n', 0, 'f', 0, \ 74 'i', 0, 'g', 0, 75 76 #define STRING_MTP_VENDOR \ 77 'F', 0, 'r', 0, 'e', 0, 'e', 0, \ 78 'B', 0, 'S', 0, 'D', 0, ' ', 0, \ 79 'f', 0, 'o', 0, 'u', 0, 'n', 0, \ 80 'd', 0, 'a', 0, 't', 0, 'i', 0, \ 81 'o', 0, 'n', 0, 82 83 #define STRING_MTP_PRODUCT \ 84 'U', 0, 'S', 0, 'B', 0, ' ', 0, \ 85 'M', 0, 'T', 0, 'P', 0, 86 87 #define STRING_MTP_SERIAL \ 88 'J', 0, 'u', 0, 'n', 0, 'e', 0, \ 89 ' ', 0, '2', 0, '0', 0, '0', 0, \ 90 '8', 0, 91 92 /* make the real string descriptors */ 93 94 USB_MAKE_STRING_DESC(STRING_LANG, string_lang); 95 USB_MAKE_STRING_DESC(STRING_MTP_DATA, string_mtp_data); 96 USB_MAKE_STRING_DESC(STRING_MTP_CONFIG, string_mtp_config); 97 USB_MAKE_STRING_DESC(STRING_MTP_VENDOR, string_mtp_vendor); 98 USB_MAKE_STRING_DESC(STRING_MTP_PRODUCT, string_mtp_product); 99 USB_MAKE_STRING_DESC(STRING_MTP_SERIAL, string_mtp_serial); 100 101 /* prototypes */ 102 103 static usb2_temp_get_string_desc_t mtp_get_string_desc; 104 static usb2_temp_get_vendor_desc_t mtp_get_vendor_desc; 105 106 static const struct usb2_temp_packet_size bulk_mps = { 107 .mps[USB_SPEED_FULL] = 64, 108 .mps[USB_SPEED_HIGH] = 512, 109 }; 110 111 static const struct usb2_temp_packet_size intr_mps = { 112 .mps[USB_SPEED_FULL] = 64, 113 .mps[USB_SPEED_HIGH] = 64, 114 }; 115 116 static const struct usb2_temp_endpoint_desc bulk_out_ep = { 117 .pPacketSize = &bulk_mps, 118 #ifdef USB_HIP_OUT_EP_0 119 .bEndpointAddress = USB_HIP_OUT_EP_0, 120 #else 121 .bEndpointAddress = UE_DIR_OUT, 122 #endif 123 .bmAttributes = UE_BULK, 124 }; 125 126 static const struct usb2_temp_endpoint_desc intr_in_ep = { 127 .pPacketSize = &intr_mps, 128 .bEndpointAddress = UE_DIR_IN, 129 .bmAttributes = UE_INTERRUPT, 130 }; 131 132 static const struct usb2_temp_endpoint_desc bulk_in_ep = { 133 .pPacketSize = &bulk_mps, 134 #ifdef USB_HIP_IN_EP_0 135 .bEndpointAddress = USB_HIP_IN_EP_0, 136 #else 137 .bEndpointAddress = UE_DIR_IN, 138 #endif 139 .bmAttributes = UE_BULK, 140 }; 141 142 static const struct usb2_temp_endpoint_desc *mtp_data_endpoints[] = { 143 &bulk_in_ep, 144 &bulk_out_ep, 145 &intr_in_ep, 146 NULL, 147 }; 148 149 static const struct usb2_temp_interface_desc mtp_data_interface = { 150 .ppEndpoints = mtp_data_endpoints, 151 .bInterfaceClass = UICLASS_IMAGE, 152 .bInterfaceSubClass = UISUBCLASS_SIC, /* Still Image Class */ 153 .bInterfaceProtocol = 1, /* PIMA 15740 */ 154 .iInterface = STRING_MTP_DATA_INDEX, 155 }; 156 157 static const struct usb2_temp_interface_desc *mtp_interfaces[] = { 158 &mtp_data_interface, 159 NULL, 160 }; 161 162 static const struct usb2_temp_config_desc mtp_config_desc = { 163 .ppIfaceDesc = mtp_interfaces, 164 .bmAttributes = UC_BUS_POWERED, 165 .bMaxPower = 25, /* 50 mA */ 166 .iConfiguration = STRING_MTP_CONFIG_INDEX, 167 }; 168 169 static const struct usb2_temp_config_desc *mtp_configs[] = { 170 &mtp_config_desc, 171 NULL, 172 }; 173 174 const struct usb2_temp_device_desc usb2_template_mtp = { 175 .getStringDesc = &mtp_get_string_desc, 176 .getVendorDesc = &mtp_get_vendor_desc, 177 .ppConfigDesc = mtp_configs, 178 .idVendor = 0x0001, 179 .idProduct = 0x0001, 180 .bcdDevice = 0x0100, 181 .bDeviceClass = 0, 182 .bDeviceSubClass = 0, 183 .bDeviceProtocol = 0, 184 .iManufacturer = STRING_MTP_VENDOR_INDEX, 185 .iProduct = STRING_MTP_PRODUCT_INDEX, 186 .iSerialNumber = STRING_MTP_SERIAL_INDEX, 187 }; 188 189 /*------------------------------------------------------------------------* 190 * mtp_get_vendor_desc 191 * 192 * Return values: 193 * NULL: Failure. No such vendor descriptor. 194 * Else: Success. Pointer to vendor descriptor is returned. 195 *------------------------------------------------------------------------*/ 196 static const void * 197 mtp_get_vendor_desc(const struct usb2_device_request *req) 198 { 199 static const uint8_t dummy_desc[0x28] = { 200 0x28, 0, 0, 0, 0, 1, 4, 0, 201 1, 0, 0, 0, 0, 0, 0, 0, 202 0, 1, 0x4D, 0x54, 0x50, 0, 0, 0, 203 0, 0, 0, 0, 0, 0, 0, 0, 204 0, 0, 0, 0, 0, 0, 0, 0, 205 }; 206 207 if ((req->bmRequestType == UT_READ_VENDOR_DEVICE) && 208 (req->bRequest == MTP_BREQUEST) && (req->wValue[0] == 0) && 209 (req->wValue[1] == 0) && (req->wIndex[1] == 0) && 210 ((req->wIndex[0] == 4) || (req->wIndex[0] == 5))) { 211 /* 212 * By returning this descriptor LibMTP will 213 * automatically pickup our device. 214 */ 215 return (dummy_desc); 216 } 217 return (NULL); 218 } 219 220 /*------------------------------------------------------------------------* 221 * mtp_get_string_desc 222 * 223 * Return values: 224 * NULL: Failure. No such string. 225 * Else: Success. Pointer to string descriptor is returned. 226 *------------------------------------------------------------------------*/ 227 static const void * 228 mtp_get_string_desc(uint16_t lang_id, uint8_t string_index) 229 { 230 static const void *ptr[STRING_MTP_MAX] = { 231 [STRING_LANG_INDEX] = &string_lang, 232 [STRING_MTP_DATA_INDEX] = &string_mtp_data, 233 [STRING_MTP_CONFIG_INDEX] = &string_mtp_config, 234 [STRING_MTP_VENDOR_INDEX] = &string_mtp_vendor, 235 [STRING_MTP_PRODUCT_INDEX] = &string_mtp_product, 236 [STRING_MTP_SERIAL_INDEX] = &string_mtp_serial, 237 }; 238 239 static const uint8_t dummy_desc[0x12] = { 240 0x12, 0x03, 0x4D, 0x00, 0x53, 0x00, 0x46, 0x00, 241 0x54, 0x00, 0x31, 0x00, 0x30, 0x00, 0x30, 0x00, 242 MTP_BREQUEST, 0x00, 243 }; 244 245 if (string_index == 0xEE) { 246 /* 247 * By returning this string LibMTP will automatically 248 * pickup our device. 249 */ 250 return (dummy_desc); 251 } 252 if (string_index == 0) { 253 return (&string_lang); 254 } 255 if (lang_id != 0x0409) { 256 return (NULL); 257 } 258 if (string_index < STRING_MTP_MAX) { 259 return (ptr[string_index]); 260 } 261 return (NULL); 262 } 263