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