1 /* $FreeBSD: head/sys/dev/usb/template/usb_template_mtp.c 246125 2013-01-30 16:05:54Z hselasky $ */
2 /*-
3 * Copyright (c) 2008 Hans Petter Selasky <hselasky@FreeBSD.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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.
14 *
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
25 * SUCH DAMAGE.
26 */
27
28 /*
29 * This file contains the USB templates for an USB Message Transfer
30 * Protocol device.
31 *
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.
37 */
38
39 #ifdef USB_GLOBAL_INCLUDE_FILE
40 #include USB_GLOBAL_INCLUDE_FILE
41 #else
42 #include <sys/stdint.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>
48 #include <sys/bus.h>
49 #include <sys/module.h>
50 #include <sys/lock.h>
51 #include <sys/condvar.h>
52 #include <sys/sysctl.h>
53 #include <sys/unistd.h>
54 #include <sys/callout.h>
55 #include <sys/malloc.h>
56 #include <sys/caps.h>
57
58 #include <bus/u4b/usb.h>
59 #include <bus/u4b/usbdi.h>
60 #include <bus/u4b/usb_core.h>
61
62 #include <bus/u4b/template/usb_template.h>
63 #endif /* USB_GLOBAL_INCLUDE_FILE */
64
65 #define MTP_BREQUEST 0x08
66
67 enum {
68 STRING_LANG_INDEX,
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,
74 STRING_MTP_MAX,
75 };
76
77 #define STRING_MTP_DATA \
78 "U\0S\0B\0 \0M\0T\0P\0 \0I\0n\0t\0e\0r\0f\0a\0c\0e"
79
80 #define STRING_MTP_CONFIG \
81 "D\0e\0f\0a\0u\0l\0t\0 \0c\0o\0n\0f\0i\0g"
82
83 #define STRING_MTP_VENDOR \
84 "F\0r\0e\0e\0B\0S\0D\0 \0f\0o\0u\0n\0d\0a\0t\0i\0o\0n"
85
86 #define STRING_MTP_PRODUCT \
87 "U\0S\0B\0 \0M\0T\0P"
88
89 #define STRING_MTP_SERIAL \
90 "J\0u\0n\0e\0 \0002\0000\0000\08"
91
92 /* make the real string descriptors */
93
94 USB_MAKE_STRING_DESC(STRING_MTP_DATA, string_mtp_data);
95 USB_MAKE_STRING_DESC(STRING_MTP_CONFIG, string_mtp_config);
96 USB_MAKE_STRING_DESC(STRING_MTP_VENDOR, string_mtp_vendor);
97 USB_MAKE_STRING_DESC(STRING_MTP_PRODUCT, string_mtp_product);
98 USB_MAKE_STRING_DESC(STRING_MTP_SERIAL, string_mtp_serial);
99
100 /* prototypes */
101
102 static usb_temp_get_string_desc_t mtp_get_string_desc;
103 static usb_temp_get_vendor_desc_t mtp_get_vendor_desc;
104
105 static const struct usb_temp_packet_size bulk_mps = {
106 .mps[USB_SPEED_FULL] = 64,
107 .mps[USB_SPEED_HIGH] = 512,
108 };
109
110 static const struct usb_temp_packet_size intr_mps = {
111 .mps[USB_SPEED_FULL] = 64,
112 .mps[USB_SPEED_HIGH] = 64,
113 };
114
115 static const struct usb_temp_endpoint_desc bulk_out_ep = {
116 .pPacketSize = &bulk_mps,
117 #ifdef USB_HIP_OUT_EP_0
118 .bEndpointAddress = USB_HIP_OUT_EP_0,
119 #else
120 .bEndpointAddress = UE_DIR_OUT,
121 #endif
122 .bmAttributes = UE_BULK,
123 };
124
125 static const struct usb_temp_endpoint_desc intr_in_ep = {
126 .pPacketSize = &intr_mps,
127 .bEndpointAddress = UE_DIR_IN,
128 .bmAttributes = UE_INTERRUPT,
129 };
130
131 static const struct usb_temp_endpoint_desc bulk_in_ep = {
132 .pPacketSize = &bulk_mps,
133 #ifdef USB_HIP_IN_EP_0
134 .bEndpointAddress = USB_HIP_IN_EP_0,
135 #else
136 .bEndpointAddress = UE_DIR_IN,
137 #endif
138 .bmAttributes = UE_BULK,
139 };
140
141 static const struct usb_temp_endpoint_desc *mtp_data_endpoints[] = {
142 &bulk_in_ep,
143 &bulk_out_ep,
144 &intr_in_ep,
145 NULL,
146 };
147
148 static const struct usb_temp_interface_desc mtp_data_interface = {
149 .ppEndpoints = mtp_data_endpoints,
150 .bInterfaceClass = UICLASS_IMAGE,
151 .bInterfaceSubClass = UISUBCLASS_SIC, /* Still Image Class */
152 .bInterfaceProtocol = 1, /* PIMA 15740 */
153 .iInterface = STRING_MTP_DATA_INDEX,
154 };
155
156 static const struct usb_temp_interface_desc *mtp_interfaces[] = {
157 &mtp_data_interface,
158 NULL,
159 };
160
161 static const struct usb_temp_config_desc mtp_config_desc = {
162 .ppIfaceDesc = mtp_interfaces,
163 .bmAttributes = UC_BUS_POWERED,
164 .bMaxPower = 25, /* 50 mA */
165 .iConfiguration = STRING_MTP_CONFIG_INDEX,
166 };
167
168 static const struct usb_temp_config_desc *mtp_configs[] = {
169 &mtp_config_desc,
170 NULL,
171 };
172
173 const struct usb_temp_device_desc usb_template_mtp = {
174 .getStringDesc = &mtp_get_string_desc,
175 .getVendorDesc = &mtp_get_vendor_desc,
176 .ppConfigDesc = mtp_configs,
177 .idVendor = USB_TEMPLATE_VENDOR,
178 .idProduct = 0x0011,
179 .bcdDevice = 0x0100,
180 .bDeviceClass = 0,
181 .bDeviceSubClass = 0,
182 .bDeviceProtocol = 0,
183 .iManufacturer = STRING_MTP_VENDOR_INDEX,
184 .iProduct = STRING_MTP_PRODUCT_INDEX,
185 .iSerialNumber = STRING_MTP_SERIAL_INDEX,
186 };
187
188 /*------------------------------------------------------------------------*
189 * mtp_get_vendor_desc
190 *
191 * Return values:
192 * NULL: Failure. No such vendor descriptor.
193 * Else: Success. Pointer to vendor descriptor is returned.
194 *------------------------------------------------------------------------*/
195 static const void *
mtp_get_vendor_desc(const struct usb_device_request * req,uint16_t * plen)196 mtp_get_vendor_desc(const struct usb_device_request *req, uint16_t *plen)
197 {
198 static const uint8_t dummy_desc[0x28] = {
199 0x28, 0, 0, 0, 0, 1, 4, 0,
200 1, 0, 0, 0, 0, 0, 0, 0,
201 0, 1, 0x4D, 0x54, 0x50, 0, 0, 0,
202 0, 0, 0, 0, 0, 0, 0, 0,
203 0, 0, 0, 0, 0, 0, 0, 0,
204 };
205
206 if ((req->bmRequestType == UT_READ_VENDOR_DEVICE) &&
207 (req->bRequest == MTP_BREQUEST) && (req->wValue[0] == 0) &&
208 (req->wValue[1] == 0) && (req->wIndex[1] == 0) &&
209 ((req->wIndex[0] == 4) || (req->wIndex[0] == 5))) {
210 /*
211 * By returning this descriptor LibMTP will
212 * automatically pickup our device.
213 */
214 return (dummy_desc);
215 }
216 return (NULL);
217 }
218
219 /*------------------------------------------------------------------------*
220 * mtp_get_string_desc
221 *
222 * Return values:
223 * NULL: Failure. No such string.
224 * Else: Success. Pointer to string descriptor is returned.
225 *------------------------------------------------------------------------*/
226 static const void *
mtp_get_string_desc(uint16_t lang_id,uint8_t string_index)227 mtp_get_string_desc(uint16_t lang_id, uint8_t string_index)
228 {
229 static const void *ptr[STRING_MTP_MAX] = {
230 [STRING_LANG_INDEX] = &usb_string_lang_en,
231 [STRING_MTP_DATA_INDEX] = &string_mtp_data,
232 [STRING_MTP_CONFIG_INDEX] = &string_mtp_config,
233 [STRING_MTP_VENDOR_INDEX] = &string_mtp_vendor,
234 [STRING_MTP_PRODUCT_INDEX] = &string_mtp_product,
235 [STRING_MTP_SERIAL_INDEX] = &string_mtp_serial,
236 };
237
238 static const uint8_t dummy_desc[0x12] = {
239 0x12, 0x03, 0x4D, 0x00, 0x53, 0x00, 0x46, 0x00,
240 0x54, 0x00, 0x31, 0x00, 0x30, 0x00, 0x30, 0x00,
241 MTP_BREQUEST, 0x00,
242 };
243
244 if (string_index == 0xEE) {
245 /*
246 * By returning this string LibMTP will automatically
247 * pickup our device.
248 */
249 return (dummy_desc);
250 }
251 if (string_index == 0) {
252 return (&usb_string_lang_en);
253 }
254 if (lang_id != 0x0409) {
255 return (NULL);
256 }
257 if (string_index < STRING_MTP_MAX) {
258 return (ptr[string_index]);
259 }
260 return (NULL);
261 }
262