5 * Copyright (c) 2008 Hans Petter Selasky <hselasky@FreeBSD.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
31 * This file contains the USB templates for an USB Message Transfer
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.
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/linker_set.h>
50 #include <sys/module.h>
52 #include <sys/mutex.h>
53 #include <sys/condvar.h>
54 #include <sys/sysctl.h>
56 #include <sys/unistd.h>
57 #include <sys/callout.h>
58 #include <sys/malloc.h>
61 #include <dev/usb/usb.h>
62 #include <dev/usb/usbdi.h>
63 #include <dev/usb/template/usb_template.h>
65 #define MTP_BREQUEST 0x08
69 STRING_MTP_DATA_INDEX,
70 STRING_MTP_CONFIG_INDEX,
71 STRING_MTP_VENDOR_INDEX,
72 STRING_MTP_PRODUCT_INDEX,
73 STRING_MTP_SERIAL_INDEX,
78 0x09, 0x04, /* American English */
80 #define STRING_MTP_DATA \
81 'U', 0, 'S', 0, 'B', 0, ' ', 0, \
82 'M', 0, 'T', 0, 'P', 0, \
83 ' ', 0, 'I', 0, 'n', 0, 't', 0, \
84 'e', 0, 'r', 0, 'f', 0, 'a', 0, \
87 #define STRING_MTP_CONFIG \
88 'D', 0, 'e', 0, 'f', 0, 'a', 0, \
89 'u', 0, 'l', 0, 't', 0, ' ', 0, \
90 'c', 0, 'o', 0, 'n', 0, 'f', 0, \
93 #define STRING_MTP_VENDOR \
94 'F', 0, 'r', 0, 'e', 0, 'e', 0, \
95 'B', 0, 'S', 0, 'D', 0, ' ', 0, \
96 'f', 0, 'o', 0, 'u', 0, 'n', 0, \
97 'd', 0, 'a', 0, 't', 0, 'i', 0, \
100 #define STRING_MTP_PRODUCT \
101 'U', 0, 'S', 0, 'B', 0, ' ', 0, \
102 'M', 0, 'T', 0, 'P', 0,
104 #define STRING_MTP_SERIAL \
105 'J', 0, 'u', 0, 'n', 0, 'e', 0, \
106 ' ', 0, '2', 0, '0', 0, '0', 0, \
109 /* make the real string descriptors */
111 USB_MAKE_STRING_DESC(STRING_LANG, string_lang);
112 USB_MAKE_STRING_DESC(STRING_MTP_DATA, string_mtp_data);
113 USB_MAKE_STRING_DESC(STRING_MTP_CONFIG, string_mtp_config);
114 USB_MAKE_STRING_DESC(STRING_MTP_VENDOR, string_mtp_vendor);
115 USB_MAKE_STRING_DESC(STRING_MTP_PRODUCT, string_mtp_product);
116 USB_MAKE_STRING_DESC(STRING_MTP_SERIAL, string_mtp_serial);
120 static usb_temp_get_string_desc_t mtp_get_string_desc;
121 static usb_temp_get_vendor_desc_t mtp_get_vendor_desc;
123 static const struct usb_temp_packet_size bulk_mps = {
124 .mps[USB_SPEED_FULL] = 64,
125 .mps[USB_SPEED_HIGH] = 512,
128 static const struct usb_temp_packet_size intr_mps = {
129 .mps[USB_SPEED_FULL] = 64,
130 .mps[USB_SPEED_HIGH] = 64,
133 static const struct usb_temp_endpoint_desc bulk_out_ep = {
134 .pPacketSize = &bulk_mps,
135 #ifdef USB_HIP_OUT_EP_0
136 .bEndpointAddress = USB_HIP_OUT_EP_0,
138 .bEndpointAddress = UE_DIR_OUT,
140 .bmAttributes = UE_BULK,
143 static const struct usb_temp_endpoint_desc intr_in_ep = {
144 .pPacketSize = &intr_mps,
145 .bEndpointAddress = UE_DIR_IN,
146 .bmAttributes = UE_INTERRUPT,
149 static const struct usb_temp_endpoint_desc bulk_in_ep = {
150 .pPacketSize = &bulk_mps,
151 #ifdef USB_HIP_IN_EP_0
152 .bEndpointAddress = USB_HIP_IN_EP_0,
154 .bEndpointAddress = UE_DIR_IN,
156 .bmAttributes = UE_BULK,
159 static const struct usb_temp_endpoint_desc *mtp_data_endpoints[] = {
166 static const struct usb_temp_interface_desc mtp_data_interface = {
167 .ppEndpoints = mtp_data_endpoints,
168 .bInterfaceClass = UICLASS_IMAGE,
169 .bInterfaceSubClass = UISUBCLASS_SIC, /* Still Image Class */
170 .bInterfaceProtocol = 1, /* PIMA 15740 */
171 .iInterface = STRING_MTP_DATA_INDEX,
174 static const struct usb_temp_interface_desc *mtp_interfaces[] = {
179 static const struct usb_temp_config_desc mtp_config_desc = {
180 .ppIfaceDesc = mtp_interfaces,
181 .bmAttributes = UC_BUS_POWERED,
182 .bMaxPower = 25, /* 50 mA */
183 .iConfiguration = STRING_MTP_CONFIG_INDEX,
186 static const struct usb_temp_config_desc *mtp_configs[] = {
191 const struct usb_temp_device_desc usb_template_mtp = {
192 .getStringDesc = &mtp_get_string_desc,
193 .getVendorDesc = &mtp_get_vendor_desc,
194 .ppConfigDesc = mtp_configs,
199 .bDeviceSubClass = 0,
200 .bDeviceProtocol = 0,
201 .iManufacturer = STRING_MTP_VENDOR_INDEX,
202 .iProduct = STRING_MTP_PRODUCT_INDEX,
203 .iSerialNumber = STRING_MTP_SERIAL_INDEX,
206 /*------------------------------------------------------------------------*
207 * mtp_get_vendor_desc
210 * NULL: Failure. No such vendor descriptor.
211 * Else: Success. Pointer to vendor descriptor is returned.
212 *------------------------------------------------------------------------*/
214 mtp_get_vendor_desc(const struct usb_device_request *req)
216 static const uint8_t dummy_desc[0x28] = {
217 0x28, 0, 0, 0, 0, 1, 4, 0,
218 1, 0, 0, 0, 0, 0, 0, 0,
219 0, 1, 0x4D, 0x54, 0x50, 0, 0, 0,
220 0, 0, 0, 0, 0, 0, 0, 0,
221 0, 0, 0, 0, 0, 0, 0, 0,
224 if ((req->bmRequestType == UT_READ_VENDOR_DEVICE) &&
225 (req->bRequest == MTP_BREQUEST) && (req->wValue[0] == 0) &&
226 (req->wValue[1] == 0) && (req->wIndex[1] == 0) &&
227 ((req->wIndex[0] == 4) || (req->wIndex[0] == 5))) {
229 * By returning this descriptor LibMTP will
230 * automatically pickup our device.
237 /*------------------------------------------------------------------------*
238 * mtp_get_string_desc
241 * NULL: Failure. No such string.
242 * Else: Success. Pointer to string descriptor is returned.
243 *------------------------------------------------------------------------*/
245 mtp_get_string_desc(uint16_t lang_id, uint8_t string_index)
247 static const void *ptr[STRING_MTP_MAX] = {
248 [STRING_LANG_INDEX] = &string_lang,
249 [STRING_MTP_DATA_INDEX] = &string_mtp_data,
250 [STRING_MTP_CONFIG_INDEX] = &string_mtp_config,
251 [STRING_MTP_VENDOR_INDEX] = &string_mtp_vendor,
252 [STRING_MTP_PRODUCT_INDEX] = &string_mtp_product,
253 [STRING_MTP_SERIAL_INDEX] = &string_mtp_serial,
256 static const uint8_t dummy_desc[0x12] = {
257 0x12, 0x03, 0x4D, 0x00, 0x53, 0x00, 0x46, 0x00,
258 0x54, 0x00, 0x31, 0x00, 0x30, 0x00, 0x30, 0x00,
262 if (string_index == 0xEE) {
264 * By returning this string LibMTP will automatically
269 if (string_index == 0) {
270 return (&string_lang);
272 if (lang_id != 0x0409) {
275 if (string_index < STRING_MTP_MAX) {
276 return (ptr[string_index]);