3 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 * Copyright (c) 2008 Hans Petter Selasky <hselasky@FreeBSD.org>
6 * Copyright (c) 2018 The FreeBSD Foundation
9 * Portions of this software were developed by Edward Tomasz Napierala
10 * under sponsorship from the FreeBSD Foundation.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
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.
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
35 * This file contains the USB templates for an USB Media Transfer
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.
45 #ifdef USB_GLOBAL_INCLUDE_FILE
46 #include USB_GLOBAL_INCLUDE_FILE
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>
56 #include <sys/module.h>
58 #include <sys/mutex.h>
59 #include <sys/condvar.h>
60 #include <sys/sysctl.h>
62 #include <sys/unistd.h>
63 #include <sys/callout.h>
64 #include <sys/malloc.h>
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>
73 #include <dev/usb/template/usb_template.h>
74 #endif /* USB_GLOBAL_INCLUDE_FILE */
76 #define MTP_BREQUEST 0x08
81 MTP_CONFIGURATION_INDEX,
82 MTP_MANUFACTURER_INDEX,
84 MTP_SERIAL_NUMBER_INDEX,
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"
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;
100 static struct sysctl_ctx_list mtp_ctx_list;
104 static usb_temp_get_string_desc_t mtp_get_string_desc;
105 static usb_temp_get_vendor_desc_t mtp_get_vendor_desc;
107 static const struct usb_temp_packet_size bulk_mps = {
108 .mps[USB_SPEED_FULL] = 64,
109 .mps[USB_SPEED_HIGH] = 512,
112 static const struct usb_temp_packet_size intr_mps = {
113 .mps[USB_SPEED_FULL] = 64,
114 .mps[USB_SPEED_HIGH] = 64,
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,
122 .bEndpointAddress = UE_DIR_OUT,
124 .bmAttributes = UE_BULK,
127 static const struct usb_temp_endpoint_desc intr_in_ep = {
128 .pPacketSize = &intr_mps,
129 .bEndpointAddress = UE_DIR_IN,
130 .bmAttributes = UE_INTERRUPT,
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,
138 .bEndpointAddress = UE_DIR_IN,
140 .bmAttributes = UE_BULK,
143 static const struct usb_temp_endpoint_desc *mtp_data_endpoints[] = {
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,
158 static const struct usb_temp_interface_desc *mtp_interfaces[] = {
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,
170 static const struct usb_temp_config_desc *mtp_configs[] = {
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,
183 .bDeviceSubClass = 0,
184 .bDeviceProtocol = 0,
185 .iManufacturer = MTP_MANUFACTURER_INDEX,
186 .iProduct = MTP_PRODUCT_INDEX,
187 .iSerialNumber = MTP_SERIAL_NUMBER_INDEX,
190 /*------------------------------------------------------------------------*
191 * mtp_get_vendor_desc
194 * NULL: Failure. No such vendor descriptor.
195 * Else: Success. Pointer to vendor descriptor is returned.
196 *------------------------------------------------------------------------*/
198 mtp_get_vendor_desc(const struct usb_device_request *req, uint16_t *plen)
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,
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))) {
213 * By returning this descriptor LibMTP will
214 * automatically pickup our device.
221 /*------------------------------------------------------------------------*
222 * mtp_get_string_desc
225 * NULL: Failure. No such string.
226 * Else: Success. Pointer to string descriptor is returned.
227 *------------------------------------------------------------------------*/
229 mtp_get_string_desc(uint16_t lang_id, uint8_t string_index)
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,
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,
246 if (string_index == 0xEE) {
248 * By returning this string LibMTP will automatically
253 if (string_index == 0) {
254 return (&usb_string_lang_en);
256 if (lang_id != 0x0409) {
259 if (string_index < MTP_MAX_INDEX) {
260 return (ptr[string_index]);
266 mtp_init(void *arg __unused)
268 struct sysctl_oid *parent;
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);
282 snprintf(parent_name, sizeof(parent_name), "%d", USB_TEMP_MTP);
283 sysctl_ctx_init(&mtp_ctx_list);
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");
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");
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");
320 mtp_uninit(void *arg __unused)
323 sysctl_ctx_free(&mtp_ctx_list);
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);