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