1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2008 Hans Petter Selasky <hselasky@FreeBSD.org>
5 * Copyright (c) 2018 The FreeBSD Foundation
6 * All rights reserved.
7 *
8 * Portions of this software were developed by Edward Tomasz Napierala
9 * under sponsorship from the FreeBSD Foundation.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 /*
34 * This file contains the USB templates for an USB Media Transfer
35 * Protocol device.
36 *
37 * NOTE: It is common practice that MTP devices use some dummy
38 * descriptor cludges to be automatically detected by the host
39 * operating system. These descriptors are documented in the LibMTP
40 * library at sourceforge.net. The alternative is to supply the host
41 * operating system the VID and PID of your device.
42 */
43
44 #ifdef USB_GLOBAL_INCLUDE_FILE
45 #include USB_GLOBAL_INCLUDE_FILE
46 #else
47 #include <sys/stdint.h>
48 #include <sys/stddef.h>
49 #include <sys/param.h>
50 #include <sys/queue.h>
51 #include <sys/types.h>
52 #include <sys/systm.h>
53 #include <sys/kernel.h>
54 #include <sys/bus.h>
55 #include <sys/module.h>
56 #include <sys/lock.h>
57 #include <sys/mutex.h>
58 #include <sys/condvar.h>
59 #include <sys/sysctl.h>
60 #include <sys/sx.h>
61 #include <sys/unistd.h>
62 #include <sys/callout.h>
63 #include <sys/malloc.h>
64 #include <sys/priv.h>
65
66 #include <dev/usb/usb.h>
67 #include <dev/usb/usbdi.h>
68 #include <dev/usb/usb_core.h>
69 #include <dev/usb/usb_ioctl.h>
70 #include <dev/usb/usb_util.h>
71
72 #include <dev/usb/template/usb_template.h>
73 #endif /* USB_GLOBAL_INCLUDE_FILE */
74
75 #define MTP_BREQUEST 0x08
76
77 enum {
78 MTP_LANG_INDEX,
79 MTP_INTERFACE_INDEX,
80 MTP_CONFIGURATION_INDEX,
81 MTP_MANUFACTURER_INDEX,
82 MTP_PRODUCT_INDEX,
83 MTP_SERIAL_NUMBER_INDEX,
84 MTP_MAX_INDEX,
85 };
86
87 #define MTP_DEFAULT_VENDOR_ID USB_TEMPLATE_VENDOR
88 #define MTP_DEFAULT_PRODUCT_ID 0x27e2
89 #define MTP_DEFAULT_INTERFACE "USB MTP Interface"
90 #define MTP_DEFAULT_CONFIGURATION "Default Config"
91 #define MTP_DEFAULT_MANUFACTURER USB_TEMPLATE_MANUFACTURER
92 #define MTP_DEFAULT_PRODUCT "USB MTP"
93 #define MTP_DEFAULT_SERIAL_NUMBER "June 2008"
94
95 static struct usb_string_descriptor mtp_interface;
96 static struct usb_string_descriptor mtp_configuration;
97 static struct usb_string_descriptor mtp_manufacturer;
98 static struct usb_string_descriptor mtp_product;
99 static struct usb_string_descriptor mtp_serial_number;
100
101 static struct sysctl_ctx_list mtp_ctx_list;
102
103 /* prototypes */
104
105 static usb_temp_get_string_desc_t mtp_get_string_desc;
106 static usb_temp_get_vendor_desc_t mtp_get_vendor_desc;
107
108 static const struct usb_temp_packet_size bulk_mps = {
109 .mps[USB_SPEED_FULL] = 64,
110 .mps[USB_SPEED_HIGH] = 512,
111 };
112
113 static const struct usb_temp_packet_size intr_mps = {
114 .mps[USB_SPEED_FULL] = 64,
115 .mps[USB_SPEED_HIGH] = 64,
116 };
117
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,
122 #else
123 .bEndpointAddress = UE_DIR_OUT,
124 #endif
125 .bmAttributes = UE_BULK,
126 };
127
128 static const struct usb_temp_endpoint_desc intr_in_ep = {
129 .pPacketSize = &intr_mps,
130 .bEndpointAddress = UE_DIR_IN,
131 .bmAttributes = UE_INTERRUPT,
132 };
133
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,
138 #else
139 .bEndpointAddress = UE_DIR_IN,
140 #endif
141 .bmAttributes = UE_BULK,
142 };
143
144 static const struct usb_temp_endpoint_desc *mtp_data_endpoints[] = {
145 &bulk_in_ep,
146 &bulk_out_ep,
147 &intr_in_ep,
148 NULL,
149 };
150
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 = MTP_INTERFACE_INDEX,
157 };
158
159 static const struct usb_temp_interface_desc *mtp_interfaces[] = {
160 &mtp_data_interface,
161 NULL,
162 };
163
164 static const struct usb_temp_config_desc mtp_config_desc = {
165 .ppIfaceDesc = mtp_interfaces,
166 .bmAttributes = 0,
167 .bMaxPower = 0,
168 .iConfiguration = MTP_CONFIGURATION_INDEX,
169 };
170
171 static const struct usb_temp_config_desc *mtp_configs[] = {
172 &mtp_config_desc,
173 NULL,
174 };
175
176 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 = MTP_DEFAULT_VENDOR_ID,
181 .idProduct = MTP_DEFAULT_PRODUCT_ID,
182 .bcdDevice = 0x0100,
183 .bDeviceClass = 0,
184 .bDeviceSubClass = 0,
185 .bDeviceProtocol = 0,
186 .iManufacturer = MTP_MANUFACTURER_INDEX,
187 .iProduct = MTP_PRODUCT_INDEX,
188 .iSerialNumber = MTP_SERIAL_NUMBER_INDEX,
189 };
190
191 /*------------------------------------------------------------------------*
192 * mtp_get_vendor_desc
193 *
194 * Return values:
195 * NULL: Failure. No such vendor descriptor.
196 * Else: Success. Pointer to vendor descriptor is returned.
197 *------------------------------------------------------------------------*/
198 static const void *
mtp_get_vendor_desc(const struct usb_device_request * req,uint16_t * plen)199 mtp_get_vendor_desc(const struct usb_device_request *req, uint16_t *plen)
200 {
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,
207 };
208
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))) {
213 /*
214 * By returning this descriptor LibMTP will
215 * automatically pickup our device.
216 */
217 return (dummy_desc);
218 }
219 return (NULL);
220 }
221
222 /*------------------------------------------------------------------------*
223 * mtp_get_string_desc
224 *
225 * Return values:
226 * NULL: Failure. No such string.
227 * Else: Success. Pointer to string descriptor is returned.
228 *------------------------------------------------------------------------*/
229 static const void *
mtp_get_string_desc(uint16_t lang_id,uint8_t string_index)230 mtp_get_string_desc(uint16_t lang_id, uint8_t string_index)
231 {
232 static const void *ptr[MTP_MAX_INDEX] = {
233 [MTP_LANG_INDEX] = &usb_string_lang_en,
234 [MTP_INTERFACE_INDEX] = &mtp_interface,
235 [MTP_CONFIGURATION_INDEX] = &mtp_configuration,
236 [MTP_MANUFACTURER_INDEX] = &mtp_manufacturer,
237 [MTP_PRODUCT_INDEX] = &mtp_product,
238 [MTP_SERIAL_NUMBER_INDEX] = &mtp_serial_number,
239 };
240
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,
244 MTP_BREQUEST, 0x00,
245 };
246
247 if (string_index == 0xEE) {
248 /*
249 * By returning this string LibMTP will automatically
250 * pickup our device.
251 */
252 return (dummy_desc);
253 }
254 if (string_index == 0) {
255 return (&usb_string_lang_en);
256 }
257 if (lang_id != 0x0409) {
258 return (NULL);
259 }
260 if (string_index < MTP_MAX_INDEX) {
261 return (ptr[string_index]);
262 }
263 return (NULL);
264 }
265
266 static void
mtp_init(void * arg __unused)267 mtp_init(void *arg __unused)
268 {
269 struct sysctl_oid *parent;
270 char parent_name[3];
271
272 usb_make_str_desc(&mtp_interface, sizeof(mtp_interface),
273 MTP_DEFAULT_INTERFACE);
274 usb_make_str_desc(&mtp_configuration, sizeof(mtp_configuration),
275 MTP_DEFAULT_CONFIGURATION);
276 usb_make_str_desc(&mtp_manufacturer, sizeof(mtp_manufacturer),
277 MTP_DEFAULT_MANUFACTURER);
278 usb_make_str_desc(&mtp_product, sizeof(mtp_product),
279 MTP_DEFAULT_PRODUCT);
280 usb_make_str_desc(&mtp_serial_number, sizeof(mtp_serial_number),
281 MTP_DEFAULT_SERIAL_NUMBER);
282
283 snprintf(parent_name, sizeof(parent_name), "%d", USB_TEMP_MTP);
284 sysctl_ctx_init(&mtp_ctx_list);
285
286 parent = SYSCTL_ADD_NODE(&mtp_ctx_list,
287 SYSCTL_STATIC_CHILDREN(_hw_usb_templates), OID_AUTO,
288 parent_name, CTLFLAG_RW | CTLFLAG_MPSAFE,
289 0, "USB Media Transfer Protocol device side template");
290 SYSCTL_ADD_U16(&mtp_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
291 "vendor_id", CTLFLAG_RWTUN,
292 &usb_template_mtp.idVendor, 1, "Vendor identifier");
293 SYSCTL_ADD_U16(&mtp_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
294 "product_id", CTLFLAG_RWTUN,
295 &usb_template_mtp.idProduct, 1, "Product identifier");
296 #if 0
297 SYSCTL_ADD_PROC(&mtp_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
298 "interface", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
299 &mtp_interface, sizeof(mtp_interface), usb_temp_sysctl,
300 "A", "Interface string");
301 SYSCTL_ADD_PROC(&mtp_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
302 "configuration", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
303 &mtp_configuration, sizeof(mtp_configuration), usb_temp_sysctl,
304 "A", "Configuration string");
305 #endif
306 SYSCTL_ADD_PROC(&mtp_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
307 "manufacturer", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
308 &mtp_manufacturer, sizeof(mtp_manufacturer), usb_temp_sysctl,
309 "A", "Manufacturer string");
310 SYSCTL_ADD_PROC(&mtp_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
311 "product", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
312 &mtp_product, sizeof(mtp_product), usb_temp_sysctl,
313 "A", "Product string");
314 SYSCTL_ADD_PROC(&mtp_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO,
315 "serial_number", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
316 &mtp_serial_number, sizeof(mtp_serial_number), usb_temp_sysctl,
317 "A", "Serial number string");
318 }
319
320 static void
mtp_uninit(void * arg __unused)321 mtp_uninit(void *arg __unused)
322 {
323
324 sysctl_ctx_free(&mtp_ctx_list);
325 }
326
327 SYSINIT(mtp_init, SI_SUB_LOCK, SI_ORDER_FIRST, mtp_init, NULL);
328 SYSUNINIT(mtp_uninit, SI_SUB_LOCK, SI_ORDER_FIRST, mtp_uninit, NULL);
329