3 * Copyright (c) 2008 Hans Petter Selasky <hselasky@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * This file contains the USB templates for an USB Message Transfer
32 * NOTE: It is common practice that MTP devices use some dummy
33 * descriptor cludges to be automatically detected by the host
34 * operating system. These descriptors are documented in the LibMTP
35 * library at sourceforge.net. The alternative is to supply the host
36 * operating system the VID and PID of your device.
39 #ifdef USB_GLOBAL_INCLUDE_FILE
40 #include USB_GLOBAL_INCLUDE_FILE
42 #include <sys/stdint.h>
43 #include <sys/stddef.h>
44 #include <sys/param.h>
45 #include <sys/queue.h>
46 #include <sys/types.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.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/usb_core.h>
65 #include <dev/usb/template/usb_template.h>
66 #endif /* USB_GLOBAL_INCLUDE_FILE */
68 #define MTP_BREQUEST 0x08
72 STRING_MTP_DATA_INDEX,
73 STRING_MTP_CONFIG_INDEX,
74 STRING_MTP_VENDOR_INDEX,
75 STRING_MTP_PRODUCT_INDEX,
76 STRING_MTP_SERIAL_INDEX,
80 #define STRING_MTP_DATA \
81 "U\0S\0B\0 \0M\0T\0P\0 \0I\0n\0t\0e\0r\0f\0a\0c\0e"
83 #define STRING_MTP_CONFIG \
84 "D\0e\0f\0a\0u\0l\0t\0 \0c\0o\0n\0f\0i\0g"
86 #define STRING_MTP_VENDOR \
87 "F\0r\0e\0e\0B\0S\0D\0 \0f\0o\0u\0n\0d\0a\0t\0i\0o\0n"
89 #define STRING_MTP_PRODUCT \
92 #define STRING_MTP_SERIAL \
93 "J\0u\0n\0e\0 \0002\0000\0000\08"
95 /* make the real string descriptors */
97 USB_MAKE_STRING_DESC(STRING_MTP_DATA, string_mtp_data);
98 USB_MAKE_STRING_DESC(STRING_MTP_CONFIG, string_mtp_config);
99 USB_MAKE_STRING_DESC(STRING_MTP_VENDOR, string_mtp_vendor);
100 USB_MAKE_STRING_DESC(STRING_MTP_PRODUCT, string_mtp_product);
101 USB_MAKE_STRING_DESC(STRING_MTP_SERIAL, string_mtp_serial);
105 static usb_temp_get_string_desc_t mtp_get_string_desc;
106 static usb_temp_get_vendor_desc_t mtp_get_vendor_desc;
108 static const struct usb_temp_packet_size bulk_mps = {
109 .mps[USB_SPEED_FULL] = 64,
110 .mps[USB_SPEED_HIGH] = 512,
113 static const struct usb_temp_packet_size intr_mps = {
114 .mps[USB_SPEED_FULL] = 64,
115 .mps[USB_SPEED_HIGH] = 64,
118 static const struct usb_temp_endpoint_desc bulk_out_ep = {
119 .pPacketSize = &bulk_mps,
120 #ifdef USB_HIP_OUT_EP_0
121 .bEndpointAddress = USB_HIP_OUT_EP_0,
123 .bEndpointAddress = UE_DIR_OUT,
125 .bmAttributes = UE_BULK,
128 static const struct usb_temp_endpoint_desc intr_in_ep = {
129 .pPacketSize = &intr_mps,
130 .bEndpointAddress = UE_DIR_IN,
131 .bmAttributes = UE_INTERRUPT,
134 static const struct usb_temp_endpoint_desc bulk_in_ep = {
135 .pPacketSize = &bulk_mps,
136 #ifdef USB_HIP_IN_EP_0
137 .bEndpointAddress = USB_HIP_IN_EP_0,
139 .bEndpointAddress = UE_DIR_IN,
141 .bmAttributes = UE_BULK,
144 static const struct usb_temp_endpoint_desc *mtp_data_endpoints[] = {
151 static const struct usb_temp_interface_desc mtp_data_interface = {
152 .ppEndpoints = mtp_data_endpoints,
153 .bInterfaceClass = UICLASS_IMAGE,
154 .bInterfaceSubClass = UISUBCLASS_SIC, /* Still Image Class */
155 .bInterfaceProtocol = 1, /* PIMA 15740 */
156 .iInterface = STRING_MTP_DATA_INDEX,
159 static const struct usb_temp_interface_desc *mtp_interfaces[] = {
164 static const struct usb_temp_config_desc mtp_config_desc = {
165 .ppIfaceDesc = mtp_interfaces,
166 .bmAttributes = UC_BUS_POWERED,
167 .bMaxPower = 25, /* 50 mA */
168 .iConfiguration = STRING_MTP_CONFIG_INDEX,
171 static const struct usb_temp_config_desc *mtp_configs[] = {
176 const struct usb_temp_device_desc usb_template_mtp = {
177 .getStringDesc = &mtp_get_string_desc,
178 .getVendorDesc = &mtp_get_vendor_desc,
179 .ppConfigDesc = mtp_configs,
180 .idVendor = USB_TEMPLATE_VENDOR,
184 .bDeviceSubClass = 0,
185 .bDeviceProtocol = 0,
186 .iManufacturer = STRING_MTP_VENDOR_INDEX,
187 .iProduct = STRING_MTP_PRODUCT_INDEX,
188 .iSerialNumber = STRING_MTP_SERIAL_INDEX,
191 /*------------------------------------------------------------------------*
192 * mtp_get_vendor_desc
195 * NULL: Failure. No such vendor descriptor.
196 * Else: Success. Pointer to vendor descriptor is returned.
197 *------------------------------------------------------------------------*/
199 mtp_get_vendor_desc(const struct usb_device_request *req, uint16_t *plen)
201 static const uint8_t dummy_desc[0x28] = {
202 0x28, 0, 0, 0, 0, 1, 4, 0,
203 1, 0, 0, 0, 0, 0, 0, 0,
204 0, 1, 0x4D, 0x54, 0x50, 0, 0, 0,
205 0, 0, 0, 0, 0, 0, 0, 0,
206 0, 0, 0, 0, 0, 0, 0, 0,
209 if ((req->bmRequestType == UT_READ_VENDOR_DEVICE) &&
210 (req->bRequest == MTP_BREQUEST) && (req->wValue[0] == 0) &&
211 (req->wValue[1] == 0) && (req->wIndex[1] == 0) &&
212 ((req->wIndex[0] == 4) || (req->wIndex[0] == 5))) {
214 * By returning this descriptor LibMTP will
215 * automatically pickup our device.
222 /*------------------------------------------------------------------------*
223 * mtp_get_string_desc
226 * NULL: Failure. No such string.
227 * Else: Success. Pointer to string descriptor is returned.
228 *------------------------------------------------------------------------*/
230 mtp_get_string_desc(uint16_t lang_id, uint8_t string_index)
232 static const void *ptr[STRING_MTP_MAX] = {
233 [STRING_LANG_INDEX] = &usb_string_lang_en,
234 [STRING_MTP_DATA_INDEX] = &string_mtp_data,
235 [STRING_MTP_CONFIG_INDEX] = &string_mtp_config,
236 [STRING_MTP_VENDOR_INDEX] = &string_mtp_vendor,
237 [STRING_MTP_PRODUCT_INDEX] = &string_mtp_product,
238 [STRING_MTP_SERIAL_INDEX] = &string_mtp_serial,
241 static const uint8_t dummy_desc[0x12] = {
242 0x12, 0x03, 0x4D, 0x00, 0x53, 0x00, 0x46, 0x00,
243 0x54, 0x00, 0x31, 0x00, 0x30, 0x00, 0x30, 0x00,
247 if (string_index == 0xEE) {
249 * By returning this string LibMTP will automatically
254 if (string_index == 0) {
255 return (&usb_string_lang_en);
257 if (lang_id != 0x0409) {
260 if (string_index < STRING_MTP_MAX) {
261 return (ptr[string_index]);