blob: 8fc10ce6d42c85f04f0b48319857a7fc91f2186d [file] [log] [blame]
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001/*
Linus Walleij2f45d222007-02-02 22:47:39 +00002 * \file libusb-glue.c
3 * Low-level USB interface glue towards libusb.
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00004 *
Linus Walleij2f45d222007-02-02 22:47:39 +00005 * Copyright (C) 2005-2007 Richard A. Low <richard@wentnet.com>
Linus Walleijbac1eed2011-02-04 23:55:19 +01006 * Copyright (C) 2005-2011 Linus Walleij <triad@df.lth.se>
Linus Walleij2f45d222007-02-02 22:47:39 +00007 * Copyright (C) 2006-2007 Marcus Meissner
8 * Copyright (C) 2007 Ted Bullock
Linus Walleij2f622812008-08-30 22:06:58 +00009 * Copyright (C) 2008 Chris Bagwell <chris@cnpbagwell.com>
Linus Walleijeb8c6fe2006-02-03 09:46:22 +000010 *
Linus Walleij2f45d222007-02-02 22:47:39 +000011 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2 of the License, or (at your option) any later version.
Linus Walleij2e1c8a32007-01-22 06:46:08 +000015 *
Linus Walleij2f45d222007-02-02 22:47:39 +000016 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
Linus Walleij2e1c8a32007-01-22 06:46:08 +000020 *
Linus Walleij2f45d222007-02-02 22:47:39 +000021 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the
23 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 * Boston, MA 02111-1307, USA.
25 *
26 * Created by Richard Low on 24/12/2005. (as mtp-utils.c)
27 * Modified by Linus Walleij 2006-03-06
Linus Walleijd866d242009-08-23 21:50:39 +000028 * (Notice that Anglo-Saxons use little-endian dates and Swedes
Linus Walleij2f45d222007-02-02 22:47:39 +000029 * use big-endian dates.)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +000030 *
31 */
Linus Walleij21b2dde2009-06-02 19:55:34 +000032#include "config.h"
Linus Walleij7beba572006-11-29 08:56:12 +000033#include "libmtp.h"
34#include "libusb-glue.h"
Linus Walleij3e667ae2007-10-29 23:29:39 +000035#include "device-flags.h"
Linus Walleij7beba572006-11-29 08:56:12 +000036#include "util.h"
Linus Walleijeb8c6fe2006-02-03 09:46:22 +000037#include "ptp.h"
Linus Walleij7beba572006-11-29 08:56:12 +000038
Linus Walleijeb8c6fe2006-02-03 09:46:22 +000039#include <errno.h>
40#include <stdio.h>
41#include <stdlib.h>
42#include <string.h>
Linus Walleijeb8c6fe2006-02-03 09:46:22 +000043#include <usb.h>
44
Linus Walleija0323272007-01-07 12:21:30 +000045#include "ptp-pack.c"
46
Linus Walleijd866d242009-08-23 21:50:39 +000047/* Aha, older libusb does not have USB_CLASS_PTP */
48#ifndef USB_CLASS_PTP
49#define USB_CLASS_PTP 6
50#endif
51
Linus Walleij2f622812008-08-30 22:06:58 +000052/* Default USB timeout length. This can be overridden as needed
53 * but should start with a reasonable value so most common
54 * requests can be completed. The original value of 4000 was
55 * not long enough for large file transfer. Also, players can
56 * spend a bit of time collecting data. Higher values also
57 * make connecting/disconnecting more reliable.
58 */
59#define USB_TIMEOUT_DEFAULT 10000
Linus Walleijeb8c6fe2006-02-03 09:46:22 +000060
61/* USB control message data phase direction */
62#ifndef USB_DP_HTD
63#define USB_DP_HTD (0x00 << 7) /* host to device */
64#endif
65#ifndef USB_DP_DTH
66#define USB_DP_DTH (0x01 << 7) /* device to host */
67#endif
68
69/* USB Feature selector HALT */
70#ifndef USB_FEATURE_HALT
71#define USB_FEATURE_HALT 0x00
72#endif
73
Linus Walleija700d222008-05-28 23:06:14 +000074/* Internal data types */
75struct mtpdevice_list_struct {
76 struct usb_device *libusb_device;
77 PTPParams *params;
78 PTP_USB *ptp_usb;
79 uint32_t bus_location;
80 struct mtpdevice_list_struct *next;
81};
82typedef struct mtpdevice_list_struct mtpdevice_list_t;
83
Linus Walleij6fd2f082006-03-28 07:19:22 +000084static const LIBMTP_device_entry_t mtp_device_table[] = {
Linus Walleij1a673de2007-10-29 23:10:05 +000085/* We include an .h file which is shared between us and libgphoto2 */
86#include "music-players.h"
Linus Walleija5483642006-03-09 09:20:38 +000087};
Linus Walleij6fd2f082006-03-28 07:19:22 +000088static const int mtp_device_table_size = sizeof(mtp_device_table) / sizeof(LIBMTP_device_entry_t);
Linus Walleija5483642006-03-09 09:20:38 +000089
Linus Walleijd6a49972006-05-02 08:24:54 +000090// Local functions
Linus Walleij1fd2d272006-05-08 09:22:01 +000091static struct usb_bus* init_usb();
Linus Walleijb0ab5482007-08-29 21:08:54 +000092static void close_usb(PTP_USB* ptp_usb);
Linus Walleij5f3c44b2009-09-12 21:03:06 +000093static int find_interface_and_endpoints(struct usb_device *dev,
94 uint8_t *interface,
95 int* inep,
96 int* inep_maxpacket,
97 int* outep,
98 int* outep_maxpacket,
99 int* intep);
Linus Walleijd6a49972006-05-02 08:24:54 +0000100static void clear_stall(PTP_USB* ptp_usb);
Linus Walleij9eb3d312006-08-04 19:25:59 +0000101static int init_ptp_usb (PTPParams* params, PTP_USB* ptp_usb, struct usb_device* dev);
Linus Walleij784ac3f2006-12-29 10:36:51 +0000102static short ptp_write_func (unsigned long,PTPDataHandler*,void *data,unsigned long*);
Richard Low4df32f82007-01-11 20:04:39 +0000103static short ptp_read_func (unsigned long,PTPDataHandler*,void *data,unsigned long*,int);
Linus Walleijd6a49972006-05-02 08:24:54 +0000104static int usb_clear_stall_feature(PTP_USB* ptp_usb, int ep);
105static int usb_get_endpoint_status(PTP_USB* ptp_usb, int ep, uint16_t* status);
Linus Walleij1fd2d272006-05-08 09:22:01 +0000106
Linus Walleij552ba322007-01-22 11:49:59 +0000107/**
108 * Get a list of the supported USB devices.
109 *
110 * The developers depend on users of this library to constantly
111 * add in to the list of supported devices. What we need is the
112 * device name, USB Vendor ID (VID) and USB Product ID (PID).
113 * put this into a bug ticket at the project homepage, please.
114 * The VID/PID is used to let e.g. udev lift the device to
115 * console userspace access when it's plugged in.
116 *
117 * @param devices a pointer to a pointer that will hold a device
118 * list after the call to this function, if it was
119 * successful.
120 * @param numdevs a pointer to an integer that will hold the number
121 * of devices in the device list if the call was successful.
122 * @return 0 if the list was successfull retrieved, any other
123 * value means failure.
124 */
125int LIBMTP_Get_Supported_Devices_List(LIBMTP_device_entry_t ** const devices, int * const numdevs)
Linus Walleij6fd2f082006-03-28 07:19:22 +0000126{
127 *devices = (LIBMTP_device_entry_t *) &mtp_device_table;
128 *numdevs = mtp_device_table_size;
129 return 0;
130}
131
Linus Walleij552ba322007-01-22 11:49:59 +0000132
Linus Walleij1fd2d272006-05-08 09:22:01 +0000133static struct usb_bus* init_usb()
134{
Linus Walleijc1a82f12009-11-30 00:06:28 +0000135 struct usb_bus* busses;
136 struct usb_bus* bus;
137
138 /*
nicklas79daadbf22009-09-28 18:19:34 +0000139 * Some additional libusb debugging please.
140 * We use the same level debug between MTP and USB.
141 */
nicklas7903584082009-09-28 18:20:16 +0000142 if ((LIBMTP_debug & LIBMTP_DEBUG_USB) != 0)
nicklas79a99196a2009-09-28 18:19:57 +0000143 usb_set_debug(9);
nicklas79daadbf22009-09-28 18:19:34 +0000144
Linus Walleij1fd2d272006-05-08 09:22:01 +0000145 usb_init();
146 usb_find_busses();
147 usb_find_devices();
Linus Walleijc1a82f12009-11-30 00:06:28 +0000148 /* Workaround a libusb 0.1 bug : bus location is not initialised */
149 busses = usb_get_busses();
150 for (bus = busses; bus != NULL; bus = bus->next) {
Richard Lowea73f5c2010-05-16 11:57:56 +0000151 if (!bus->location)
152 bus->location = strtoul(bus->dirname, NULL, 10);
Linus Walleijc1a82f12009-11-30 00:06:28 +0000153 }
154 return (busses);
Linus Walleij1fd2d272006-05-08 09:22:01 +0000155}
156
157/**
tedbullock0f033cb2007-02-14 20:56:54 +0000158 * Small recursive function to append a new usb_device to the linked list of
159 * USB MTP devices
Linus Walleijc1a82f12009-11-30 00:06:28 +0000160 * @param devlist dynamic linked list of pointers to usb devices with MTP
Linus Walleij63fd1e62008-04-23 23:49:43 +0000161 * properties, to be extended with new device.
162 * @param newdevice the new device to add.
163 * @param bus_location bus for this device.
164 * @return an extended array or NULL on failure.
tedbullock0f033cb2007-02-14 20:56:54 +0000165 */
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000166static mtpdevice_list_t *append_to_mtpdevice_list(mtpdevice_list_t *devlist,
Linus Walleij63fd1e62008-04-23 23:49:43 +0000167 struct usb_device *newdevice,
168 uint32_t bus_location)
tedbullock0f033cb2007-02-14 20:56:54 +0000169{
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000170 mtpdevice_list_t *new_list_entry;
Linus Walleijc1a82f12009-11-30 00:06:28 +0000171
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000172 new_list_entry = (mtpdevice_list_t *) malloc(sizeof(mtpdevice_list_t));
173 if (new_list_entry == NULL) {
174 return NULL;
175 }
176 // Fill in USB device, if we *HAVE* to make a copy of the device do it here.
177 new_list_entry->libusb_device = newdevice;
Linus Walleij63fd1e62008-04-23 23:49:43 +0000178 new_list_entry->bus_location = bus_location;
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000179 new_list_entry->next = NULL;
Linus Walleij5d987e12010-05-22 22:35:33 +0000180
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000181 if (devlist == NULL) {
182 return new_list_entry;
183 } else {
184 mtpdevice_list_t *tmp = devlist;
185 while (tmp->next != NULL) {
186 tmp = tmp->next;
187 }
188 tmp->next = new_list_entry;
189 }
190 return devlist;
tedbullock0f033cb2007-02-14 20:56:54 +0000191}
192
193/**
194 * Small recursive function to free dynamic memory allocated to the linked list
195 * of USB MTP devices
Linus Walleij5d987e12010-05-22 22:35:33 +0000196 * @param devlist dynamic linked list of pointers to usb devices with MTP
tedbullock0f033cb2007-02-14 20:56:54 +0000197 * properties.
198 * @return nothing
199 */
Linus Walleija700d222008-05-28 23:06:14 +0000200static void free_mtpdevice_list(mtpdevice_list_t *devlist)
tedbullock0f033cb2007-02-14 20:56:54 +0000201{
Linus Walleijac061862007-03-07 08:28:33 +0000202 mtpdevice_list_t *tmplist = devlist;
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000203
204 if (devlist == NULL)
205 return;
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000206 while (tmplist != NULL) {
207 mtpdevice_list_t *tmp = tmplist;
208 tmplist = tmplist->next;
Richard Low61edc1a2007-09-23 10:35:48 +0000209 // Do not free() the fields (ptp_usb, params)! These are used elsewhere.
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000210 free(tmp);
tedbullock0f033cb2007-02-14 20:56:54 +0000211 }
tedbullock0f033cb2007-02-14 20:56:54 +0000212 return;
213}
214
215/**
Linus Walleij9521e2b2007-07-10 21:50:42 +0000216 * This checks if a device has an MTP descriptor. The descriptor was
217 * elaborated about in gPhoto bug 1482084, and some official documentation
218 * with no strings attached was published by Microsoft at
219 * http://www.microsoft.com/whdc/system/bus/USB/USBFAQ_intermed.mspx#E3HAC
220 *
Linus Walleija4942fc2007-03-12 19:23:21 +0000221 * @param dev a device struct from libusb.
Linus Walleij9521e2b2007-07-10 21:50:42 +0000222 * @param dumpfile set to non-NULL to make the descriptors dump out
223 * to this file in human-readable hex so we can scruitinze them.
Linus Walleija4942fc2007-03-12 19:23:21 +0000224 * @return 1 if the device is MTP compliant, 0 if not.
225 */
Linus Walleij9521e2b2007-07-10 21:50:42 +0000226static int probe_device_descriptor(struct usb_device *dev, FILE *dumpfile)
Linus Walleija4942fc2007-03-12 19:23:21 +0000227{
228 usb_dev_handle *devh;
229 unsigned char buf[1024], cmd;
Linus Walleijbc550bc2007-11-08 23:25:09 +0000230 int i;
Linus Walleija4942fc2007-03-12 19:23:21 +0000231 int ret;
Linus Walleijfb3e3022011-01-19 19:20:34 +0100232 /* This is to indicate if we find some vendor interface */
233 int found_vendor_spec_interface = 0;
Linus Walleij5d987e12010-05-22 22:35:33 +0000234
235 /*
Linus Walleijfb3e3022011-01-19 19:20:34 +0100236 * Don't examine devices that are not likely to
237 * contain any MTP interface, update this the day
238 * you find some weird combination...
Linus Walleij5d987e12010-05-22 22:35:33 +0000239 */
Linus Walleij4eb8ad92011-01-27 22:52:07 +0100240 if (!(dev->descriptor.bDeviceClass == USB_CLASS_PER_INTERFACE ||
241 dev->descriptor.bDeviceClass == USB_CLASS_COMM ||
242 dev->descriptor.bDeviceClass == USB_CLASS_PTP ||
243 dev->descriptor.bDeviceClass == USB_CLASS_VENDOR_SPEC)) {
Linus Walleija4942fc2007-03-12 19:23:21 +0000244 return 0;
245 }
Linus Walleij5d987e12010-05-22 22:35:33 +0000246
Linus Walleija4942fc2007-03-12 19:23:21 +0000247 /* Attempt to open Device on this port */
248 devh = usb_open(dev);
249 if (devh == NULL) {
250 /* Could not open this device */
251 return 0;
252 }
Linus Walleijbc550bc2007-11-08 23:25:09 +0000253
254 /*
Richard Lowe029eba2009-04-11 12:36:25 +0000255 * This sometimes crashes on the j for loop below
Linus Walleij88244212009-08-12 16:05:56 +0000256 * I think it is because config is NULL yet
Richard Lowe029eba2009-04-11 12:36:25 +0000257 * dev->descriptor.bNumConfigurations > 0
258 * this check should stop this
Linus Walleijbc550bc2007-11-08 23:25:09 +0000259 */
Richard Lowe029eba2009-04-11 12:36:25 +0000260 if (dev->config) {
261 /*
Linus Walleij88244212009-08-12 16:05:56 +0000262 * Loop over the device configurations and interfaces. Nokia MTP-capable
263 * handsets (possibly others) typically have the string "MTP" in their
Richard Lowe029eba2009-04-11 12:36:25 +0000264 * MTP interface descriptions, that's how they can be detected, before
265 * we try the more esoteric "OS descriptors" (below).
266 */
267 for (i = 0; i < dev->descriptor.bNumConfigurations; i++) {
268 uint8_t j;
Linus Walleij88244212009-08-12 16:05:56 +0000269
Richard Lowe029eba2009-04-11 12:36:25 +0000270 for (j = 0; j < dev->config[i].bNumInterfaces; j++) {
271 int k;
272 for (k = 0; k < dev->config[i].interface[j].num_altsetting; k++) {
Linus Walleij88244212009-08-12 16:05:56 +0000273 /* Current interface descriptor */
274 struct usb_interface_descriptor *intf =
275 &dev->config[i].interface[j].altsetting[k];
276
277 /*
Linus Walleijfb3e3022011-01-19 19:20:34 +0100278 * We only want to probe for the OS descriptor if the
279 * device is USB_CLASS_VENDOR_SPEC or one of the interfaces
280 * in it is, so flag if we find an interface like this.
281 */
282 if (intf->bInterfaceClass == USB_CLASS_VENDOR_SPEC) {
283 found_vendor_spec_interface = 1;
284 }
285
286 /*
Linus Walleij88244212009-08-12 16:05:56 +0000287 * Check for Still Image Capture class with PIMA 15740 protocol,
288 * also known as PTP
289 */
Linus Walleij5d92c8c2011-01-25 23:42:19 +0100290#if 0
Linus Walleij88244212009-08-12 16:05:56 +0000291 if (intf->bInterfaceClass == USB_CLASS_PTP
292 && intf->bInterfaceSubClass == 0x01
293 && intf->bInterfaceProtocol == 0x01) {
294 if (dumpfile != NULL) {
Linus Walleij5d92c8c2011-01-25 23:42:19 +0100295 fprintf(dumpfile, " Found PTP device, check vendor "
296 "extension...\n");
Linus Walleij88244212009-08-12 16:05:56 +0000297 }
Linus Walleij5d92c8c2011-01-25 23:42:19 +0100298 // This is where we may insert code to open a PTP
299 // session and query the vendor extension ID to see
Linus Walleij4eb8ad92011-01-27 22:52:07 +0100300 // if it is 0xffffffff, i.e. MTP according to the spec.
Linus Walleij5d92c8c2011-01-25 23:42:19 +0100301 if (was_mtp_extension) {
302 usb_close(devh);
303 return 1;
304 }
Linus Walleij88244212009-08-12 16:05:56 +0000305 }
Linus Walleij5d92c8c2011-01-25 23:42:19 +0100306#endif
307
308 /*
309 * Next we search for the MTP substring in the interface name.
310 * For example : "RIM MS/MTP" should work.
311 */
Linus Walleij88244212009-08-12 16:05:56 +0000312 buf[0] = '\0';
313 ret = usb_get_string_simple(devh,
314 dev->config[i].interface[j].altsetting[k].iInterface,
315 (char *) buf,
316 1024);
317 if (ret < 3)
318 continue;
Linus Walleijfea4f532009-09-22 22:08:35 +0000319 if (strstr((char *) buf, "MTP") != NULL) {
Linus Walleij88244212009-08-12 16:05:56 +0000320 if (dumpfile != NULL) {
321 fprintf(dumpfile, "Configuration %d, interface %d, altsetting %d:\n", i, j, k);
322 fprintf(dumpfile, " Interface description contains the string \"MTP\"\n");
323 fprintf(dumpfile, " Device recognized as MTP, no further probing.\n");
324 }
Richard Lowe029eba2009-04-11 12:36:25 +0000325 usb_close(devh);
326 return 1;
327 }
328 #ifdef LIBUSB_HAS_GET_DRIVER_NP
Linus Walleij88244212009-08-12 16:05:56 +0000329 {
330 /*
331 * Specifically avoid probing anything else than USB mass storage devices
332 * and non-associated drivers in Linux.
333 */
334 char devname[0x10];
335
336 devname[0] = '\0';
337 ret = usb_get_driver_np(devh,
338 dev->config[i].interface[j].altsetting[k].iInterface,
339 devname,
340 sizeof(devname));
341 if (devname[0] != '\0' && strcmp(devname, "usb-storage")) {
nicklas79daadbf22009-09-28 18:19:34 +0000342 LIBMTP_INFO("avoid probing device using kernel interface \"%s\"\n", devname);
Linus Walleij88244212009-08-12 16:05:56 +0000343 return 0;
344 }
345 }
Richard Lowe029eba2009-04-11 12:36:25 +0000346 #endif
347 }
348 }
349 }
350 } else {
351 if (dev->descriptor.bNumConfigurations)
nicklas79daadbf22009-09-28 18:19:34 +0000352 LIBMTP_INFO("dev->config is NULL in probe_device_descriptor yet dev->descriptor.bNumConfigurations > 0\n");
Linus Walleijbc550bc2007-11-08 23:25:09 +0000353 }
Linus Walleijfea4f532009-09-22 22:08:35 +0000354
Linus Walleij49e57b82009-12-16 09:21:33 +0000355 /*
Linus Walleijfb3e3022011-01-19 19:20:34 +0100356 * Only probe for OS descriptor if the device is vendor specific
357 * or one of the interfaces found is.
Linus Walleij49e57b82009-12-16 09:21:33 +0000358 */
Linus Walleijfb3e3022011-01-19 19:20:34 +0100359 if (dev->descriptor.bDeviceClass == USB_CLASS_VENDOR_SPEC ||
360 found_vendor_spec_interface) {
Linus Walleij9521e2b2007-07-10 21:50:42 +0000361
Linus Walleijfb3e3022011-01-19 19:20:34 +0100362 /* Read the special descriptor */
363 ret = usb_get_descriptor(devh, 0x03, 0xee, buf, sizeof(buf));
Linus Walleijfea4f532009-09-22 22:08:35 +0000364
Linus Walleijfb3e3022011-01-19 19:20:34 +0100365 /*
366 * If something failed we're probably stalled to we need
367 * to clear the stall off the endpoint and say this is not
368 * MTP.
369 */
370 if (ret < 0) {
371 /* EP0 is the default control endpoint */
372 usb_clear_halt(devh, 0);
373 usb_close(devh);
374 return 0;
375 }
376
377 // Dump it, if requested
378 if (dumpfile != NULL && ret > 0) {
379 fprintf(dumpfile, "Microsoft device descriptor 0xee:\n");
380 data_dump_ascii(dumpfile, buf, ret, 16);
381 }
382
383 /* Check if descriptor length is at least 10 bytes */
384 if (ret < 10) {
385 usb_close(devh);
386 return 0;
387 }
388
389 /* Check if this device has a Microsoft Descriptor */
390 if (!((buf[2] == 'M') && (buf[4] == 'S') &&
391 (buf[6] == 'F') && (buf[8] == 'T'))) {
392 usb_close(devh);
393 return 0;
394 }
395
396 /* Check if device responds to control message 1 or if there is an error */
397 cmd = buf[16];
398 ret = usb_control_msg (devh,
399 USB_ENDPOINT_IN | USB_RECIP_DEVICE | USB_TYPE_VENDOR,
400 cmd,
401 0,
402 4,
403 (char *) buf,
404 sizeof(buf),
405 USB_TIMEOUT_DEFAULT);
406
407 // Dump it, if requested
408 if (dumpfile != NULL && ret > 0) {
409 fprintf(dumpfile, "Microsoft device response to control message 1, CMD 0x%02x:\n", cmd);
410 data_dump_ascii(dumpfile, buf, ret, 16);
411 }
412
413 /* If this is true, the device either isn't MTP or there was an error */
414 if (ret <= 0x15) {
415 /* TODO: If there was an error, flag it and let the user know somehow */
416 /* if(ret == -1) {} */
417 usb_close(devh);
418 return 0;
419 }
420
421 /* Check if device is MTP or if it is something like a USB Mass Storage
422 device with Janus DRM support */
423 if ((buf[0x12] != 'M') || (buf[0x13] != 'T') || (buf[0x14] != 'P')) {
424 usb_close(devh);
425 return 0;
426 }
427
428 /* After this point we are probably dealing with an MTP device */
429
430 /*
431 * Check if device responds to control message 2, which is
432 * the extended device parameters. Most devices will just
433 * respond with a copy of the same message as for the first
434 * message, some respond with zero-length (which is OK)
435 * and some with pure garbage. We're not parsing the result
436 * so this is not very important.
437 */
438 ret = usb_control_msg (devh,
439 USB_ENDPOINT_IN | USB_RECIP_DEVICE | USB_TYPE_VENDOR,
440 cmd,
441 0,
442 5,
443 (char *) buf,
444 sizeof(buf),
445 USB_TIMEOUT_DEFAULT);
446
447 // Dump it, if requested
448 if (dumpfile != NULL && ret > 0) {
449 fprintf(dumpfile, "Microsoft device response to control message 2, CMD 0x%02x:\n", cmd);
450 data_dump_ascii(dumpfile, buf, ret, 16);
451 }
452
453 /* If this is true, the device errored against control message 2 */
454 if (ret == -1) {
455 /* TODO: Implement callback function to let managing program know there
456 was a problem, along with description of the problem */
457 LIBMTP_ERROR("Potential MTP Device with VendorID:%04x and "
458 "ProductID:%04x encountered an error responding to "
459 "control message 2.\n"
460 "Problems may arrise but continuing\n",
461 dev->descriptor.idVendor, dev->descriptor.idProduct);
462 } else if (dumpfile != NULL && ret == 0) {
463 fprintf(dumpfile, "Zero-length response to control message 2 (OK)\n");
464 } else if (dumpfile != NULL) {
465 fprintf(dumpfile, "Device responds to control message 2 with some data.\n");
466 }
Linus Walleija4942fc2007-03-12 19:23:21 +0000467 }
Linus Walleijfea4f532009-09-22 22:08:35 +0000468
Linus Walleija4942fc2007-03-12 19:23:21 +0000469 /* Close the USB device handle */
470 usb_close(devh);
471 return 1;
472}
473
474/**
475 * This function scans through the connected usb devices on a machine and
476 * if they match known Vendor and Product identifiers appends them to the
Linus Walleij49557482010-06-22 09:14:47 +0000477 * dynamic array mtp_device_list. Be sure to call
478 * <code>free_mtpdevice_list(mtp_device_list)</code> when you are done
Linus Walleij63fd1e62008-04-23 23:49:43 +0000479 * with it, assuming it is not NULL.
Linus Walleij49557482010-06-22 09:14:47 +0000480 * @param mtp_device_list dynamic array of pointers to usb devices with MTP
Linus Walleij45a86372007-03-07 09:36:19 +0000481 * properties (if this list is not empty, new entries will be appended
482 * to the list).
Linus Walleija4942fc2007-03-12 19:23:21 +0000483 * @return LIBMTP_ERROR_NONE implies that devices have been found, scan the list
Linus Walleij49557482010-06-22 09:14:47 +0000484 * appropriately. LIBMTP_ERROR_NO_DEVICE_ATTACHED implies that no
Linus Walleij9c6c1542007-09-19 13:43:06 +0000485 * devices have been found.
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000486 */
Linus Walleij45a86372007-03-07 09:36:19 +0000487static LIBMTP_error_number_t get_mtp_usb_device_list(mtpdevice_list_t ** mtp_device_list)
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000488{
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000489 struct usb_bus *bus = init_usb();
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000490 for (; bus != NULL; bus = bus->next) {
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000491 struct usb_device *dev = bus->devices;
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000492 for (; dev != NULL; dev = dev->next) {
Richard Lowe9f205f2007-09-16 16:25:50 +0000493 if (dev->descriptor.bDeviceClass != USB_CLASS_HUB) {
Linus Walleij9c6c1542007-09-19 13:43:06 +0000494 int i;
495 int found = 0;
496
497 // First check if we know about the device already.
498 // Devices well known to us will not have their descriptors
499 // probed, it caused problems with some devices.
Richard Lowe9f205f2007-09-16 16:25:50 +0000500 for(i = 0; i < mtp_device_table_size; i++) {
501 if(dev->descriptor.idVendor == mtp_device_table[i].vendor_id &&
502 dev->descriptor.idProduct == mtp_device_table[i].product_id) {
503 /* Append this usb device to the MTP device list */
Linus Walleij49557482010-06-22 09:14:47 +0000504 *mtp_device_list = append_to_mtpdevice_list(*mtp_device_list,
505 dev,
Linus Walleij63fd1e62008-04-23 23:49:43 +0000506 bus->location);
Richard Lowe9f205f2007-09-16 16:25:50 +0000507 found = 1;
508 break;
509 }
510 }
Linus Walleij9c6c1542007-09-19 13:43:06 +0000511 // If we didn't know it, try probing the "OS Descriptor".
Richard Lowe9f205f2007-09-16 16:25:50 +0000512 if (!found) {
Richard Lowe9f205f2007-09-16 16:25:50 +0000513 if (probe_device_descriptor(dev, NULL)) {
514 /* Append this usb device to the MTP USB Device List */
Linus Walleij49557482010-06-22 09:14:47 +0000515 *mtp_device_list = append_to_mtpdevice_list(*mtp_device_list,
Linus Walleij63fd1e62008-04-23 23:49:43 +0000516 dev,
517 bus->location);
Richard Lowe9f205f2007-09-16 16:25:50 +0000518 }
Linus Walleijd866d242009-08-23 21:50:39 +0000519 /*
520 * By thomas_-_s: Also append devices that are no MTP but PTP devices
521 * if this is commented out.
522 */
523 /*
524 else {
525 // Check whether the device is no USB hub but a PTP.
526 if ( dev->config != NULL &&dev->config->interface->altsetting->bInterfaceClass == USB_CLASS_PTP && dev->descriptor.bDeviceClass != USB_CLASS_HUB ) {
527 *mtp_device_list = append_to_mtpdevice_list(*mtp_device_list, dev, bus->location);
528 }
529 }
530 */
Richard Lowe9f205f2007-09-16 16:25:50 +0000531 }
Linus Walleij9c6c1542007-09-19 13:43:06 +0000532 }
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000533 }
534 }
Linus Walleij49557482010-06-22 09:14:47 +0000535
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000536 /* If nothing was found we end up here. */
Linus Walleija4942fc2007-03-12 19:23:21 +0000537 if(*mtp_device_list == NULL) {
tedbullock433d2172007-02-23 22:39:12 +0000538 return LIBMTP_ERROR_NO_DEVICE_ATTACHED;
Linus Walleija4942fc2007-03-12 19:23:21 +0000539 }
540 return LIBMTP_ERROR_NONE;
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000541}
542
543/**
Linus Walleij549f49a2010-12-05 14:00:34 +0000544 * Checks if a specific device with a certain bus and device
545 * number has an MTP type device descriptor.
546 *
547 * @param busno the bus number of the device to check
548 * @param deviceno the device number of the device to check
549 * @return 1 if the device is MTP else 0
550 */
551int LIBMTP_Check_Specific_Device(int busno, int devno)
552{
553 struct usb_bus *bus = init_usb();
554 for (; bus != NULL; bus = bus->next) {
555 struct usb_device *dev = bus->devices;
556 if (bus->location != busno)
557 continue;
558
559 for (; dev != NULL; dev = dev->next) {
560
561 if (dev->devnum != devno)
562 continue;
563
564 if (probe_device_descriptor(dev, NULL))
565 return 1;
566 }
567 }
568 return 0;
569}
570
571/**
Linus Walleij63fd1e62008-04-23 23:49:43 +0000572 * Detect the raw MTP device descriptors and return a list of
573 * of the devices found.
Linus Walleij49557482010-06-22 09:14:47 +0000574 *
Linus Walleij63fd1e62008-04-23 23:49:43 +0000575 * @param devices a pointer to a variable that will hold
576 * the list of raw devices found. This may be NULL
577 * on return if the number of detected devices is zero.
578 * The user shall simply <code>free()</code> this
579 * variable when finished with the raw devices,
580 * in order to release memory.
Linus Walleij49557482010-06-22 09:14:47 +0000581 * @param numdevs a pointer to an integer that will hold
Linus Walleij63fd1e62008-04-23 23:49:43 +0000582 * the number of devices in the list. This may
583 * be 0.
584 * @return 0 if successful, any other value means failure.
Linus Walleij1fd2d272006-05-08 09:22:01 +0000585 */
Linus Walleij49557482010-06-22 09:14:47 +0000586LIBMTP_error_number_t LIBMTP_Detect_Raw_Devices(LIBMTP_raw_device_t ** devices,
Linus Walleij63fd1e62008-04-23 23:49:43 +0000587 int * numdevs)
Linus Walleij1fd2d272006-05-08 09:22:01 +0000588{
Linus Walleij63fd1e62008-04-23 23:49:43 +0000589 mtpdevice_list_t *devlist = NULL;
590 mtpdevice_list_t *dev;
591 LIBMTP_error_number_t ret;
Linus Walleij63fd1e62008-04-23 23:49:43 +0000592 LIBMTP_raw_device_t *retdevs;
593 int devs = 0;
Linus Walleijf106aa62008-05-04 22:55:51 +0000594 int i, j;
Linus Walleijb6b69132007-03-07 09:45:05 +0000595
596 ret = get_mtp_usb_device_list(&devlist);
Linus Walleijef2fb362008-05-28 20:59:09 +0000597 if (ret == LIBMTP_ERROR_NO_DEVICE_ATTACHED) {
598 *devices = NULL;
599 *numdevs = 0;
Linus Walleija700d222008-05-28 23:06:14 +0000600 return ret;
Linus Walleijef2fb362008-05-28 20:59:09 +0000601 } else if (ret != LIBMTP_ERROR_NONE) {
nicklas79daadbf22009-09-28 18:19:34 +0000602 LIBMTP_ERROR("LIBMTP PANIC: get_mtp_usb_device_list() "
Linus Walleijf106aa62008-05-04 22:55:51 +0000603 "error code: %d on line %d\n", ret, __LINE__);
Linus Walleija700d222008-05-28 23:06:14 +0000604 return ret;
Linus Walleijb6b69132007-03-07 09:45:05 +0000605 }
Linus Walleijf106aa62008-05-04 22:55:51 +0000606
Linus Walleij63fd1e62008-04-23 23:49:43 +0000607 // Get list size
608 dev = devlist;
609 while (dev != NULL) {
610 devs++;
611 dev = dev->next;
612 }
613 if (devs == 0) {
614 *devices = NULL;
615 *numdevs = 0;
Linus Walleija700d222008-05-28 23:06:14 +0000616 return LIBMTP_ERROR_NONE;
Linus Walleij63fd1e62008-04-23 23:49:43 +0000617 }
618 // Conjure a device list
619 retdevs = (LIBMTP_raw_device_t *) malloc(sizeof(LIBMTP_raw_device_t) * devs);
620 if (retdevs == NULL) {
621 // Out of memory
622 *devices = NULL;
623 *numdevs = 0;
Linus Walleija700d222008-05-28 23:06:14 +0000624 return LIBMTP_ERROR_MEMORY_ALLOCATION;
Linus Walleij63fd1e62008-04-23 23:49:43 +0000625 }
626 dev = devlist;
627 i = 0;
628 while (dev != NULL) {
Linus Walleijf106aa62008-05-04 22:55:51 +0000629 int device_known = 0;
630
Linus Walleij63fd1e62008-04-23 23:49:43 +0000631 // Assign default device info
632 retdevs[i].device_entry.vendor = NULL;
633 retdevs[i].device_entry.vendor_id = dev->libusb_device->descriptor.idVendor;
634 retdevs[i].device_entry.product = NULL;
635 retdevs[i].device_entry.product_id = dev->libusb_device->descriptor.idProduct;
636 retdevs[i].device_entry.device_flags = 0x00000000U;
Linus Walleijfec4d562008-06-01 22:30:36 +0000637 // See if we can locate some additional vendor info and device flags
Linus Walleijf106aa62008-05-04 22:55:51 +0000638 for(j = 0; j < mtp_device_table_size; j++) {
639 if(dev->libusb_device->descriptor.idVendor == mtp_device_table[j].vendor_id &&
640 dev->libusb_device->descriptor.idProduct == mtp_device_table[j].product_id) {
641 device_known = 1;
642 retdevs[i].device_entry.vendor = mtp_device_table[j].vendor;
643 retdevs[i].device_entry.product = mtp_device_table[j].product;
644 retdevs[i].device_entry.device_flags = mtp_device_table[j].device_flags;
nicklas79daadbf22009-09-28 18:19:34 +0000645
Linus Walleijf106aa62008-05-04 22:55:51 +0000646 // This device is known to the developers
Linus Walleij49557482010-06-22 09:14:47 +0000647 LIBMTP_ERROR("Device %d (VID=%04x and PID=%04x) is a %s %s.\n",
Linus Walleijf106aa62008-05-04 22:55:51 +0000648 i,
649 dev->libusb_device->descriptor.idVendor,
650 dev->libusb_device->descriptor.idProduct,
651 mtp_device_table[j].vendor,
652 mtp_device_table[j].product);
Linus Walleijf106aa62008-05-04 22:55:51 +0000653 break;
654 }
655 }
656 if (!device_known) {
657 // This device is unknown to the developers
Linus Walleij49557482010-06-22 09:14:47 +0000658 LIBMTP_ERROR("Device %d (VID=%04x and PID=%04x) is UNKNOWN.\n",
Linus Walleijf106aa62008-05-04 22:55:51 +0000659 i,
660 dev->libusb_device->descriptor.idVendor,
661 dev->libusb_device->descriptor.idProduct);
nicklas79daadbf22009-09-28 18:19:34 +0000662 LIBMTP_ERROR("Please report this VID/PID and the device model to the "
Linus Walleijf106aa62008-05-04 22:55:51 +0000663 "libmtp development team\n");
Linus Walleijfec4d562008-06-01 22:30:36 +0000664 /*
665 * Trying to get iManufacturer or iProduct from the device at this
666 * point would require opening a device handle, that we don't want
667 * to do right now. (Takes time for no good enough reason.)
668 */
Linus Walleijf106aa62008-05-04 22:55:51 +0000669 }
670 // Save the location on the bus
Linus Walleij63fd1e62008-04-23 23:49:43 +0000671 retdevs[i].bus_location = dev->bus_location;
672 retdevs[i].devnum = dev->libusb_device->devnum;
673 i++;
674 dev = dev->next;
Linus Walleij49557482010-06-22 09:14:47 +0000675 }
Linus Walleij63fd1e62008-04-23 23:49:43 +0000676 *devices = retdevs;
677 *numdevs = i;
Linus Walleijb6b69132007-03-07 09:45:05 +0000678 free_mtpdevice_list(devlist);
Linus Walleija700d222008-05-28 23:06:14 +0000679 return LIBMTP_ERROR_NONE;
Linus Walleij1fd2d272006-05-08 09:22:01 +0000680}
681
Linus Walleijc6210fb2006-05-08 11:11:41 +0000682/**
683 * This routine just dumps out low-level
684 * USB information about the current device.
685 * @param ptp_usb the USB device to get information from.
686 */
687void dump_usbinfo(PTP_USB *ptp_usb)
688{
Linus Walleijc6210fb2006-05-08 11:11:41 +0000689 struct usb_device *dev;
690
691#ifdef LIBUSB_HAS_GET_DRIVER_NP
692 char devname[0x10];
Richard Lowe029eba2009-04-11 12:36:25 +0000693 int res;
Linus Walleij49557482010-06-22 09:14:47 +0000694
Linus Walleijc6210fb2006-05-08 11:11:41 +0000695 devname[0] = '\0';
Linus Walleijb0ab5482007-08-29 21:08:54 +0000696 res = usb_get_driver_np(ptp_usb->handle, (int) ptp_usb->interface, devname, sizeof(devname));
Linus Walleijc6210fb2006-05-08 11:11:41 +0000697 if (devname[0] != '\0') {
nicklas79daadbf22009-09-28 18:19:34 +0000698 LIBMTP_INFO(" Using kernel interface \"%s\"\n", devname);
Linus Walleijc6210fb2006-05-08 11:11:41 +0000699 }
700#endif
701 dev = usb_device(ptp_usb->handle);
nicklas79daadbf22009-09-28 18:19:34 +0000702 LIBMTP_INFO(" bcdUSB: %d\n", dev->descriptor.bcdUSB);
703 LIBMTP_INFO(" bDeviceClass: %d\n", dev->descriptor.bDeviceClass);
704 LIBMTP_INFO(" bDeviceSubClass: %d\n", dev->descriptor.bDeviceSubClass);
705 LIBMTP_INFO(" bDeviceProtocol: %d\n", dev->descriptor.bDeviceProtocol);
706 LIBMTP_INFO(" idVendor: %04x\n", dev->descriptor.idVendor);
707 LIBMTP_INFO(" idProduct: %04x\n", dev->descriptor.idProduct);
708 LIBMTP_INFO(" IN endpoint maxpacket: %d bytes\n", ptp_usb->inep_maxpacket);
709 LIBMTP_INFO(" OUT endpoint maxpacket: %d bytes\n", ptp_usb->outep_maxpacket);
710 LIBMTP_INFO(" Raw device info:\n");
711 LIBMTP_INFO(" Bus location: %d\n", ptp_usb->rawdevice.bus_location);
712 LIBMTP_INFO(" Device number: %d\n", ptp_usb->rawdevice.devnum);
713 LIBMTP_INFO(" Device entry info:\n");
714 LIBMTP_INFO(" Vendor: %s\n", ptp_usb->rawdevice.device_entry.vendor);
715 LIBMTP_INFO(" Vendor id: 0x%04x\n", ptp_usb->rawdevice.device_entry.vendor_id);
716 LIBMTP_INFO(" Product: %s\n", ptp_usb->rawdevice.device_entry.product);
717 LIBMTP_INFO(" Vendor id: 0x%04x\n", ptp_usb->rawdevice.device_entry.product_id);
718 LIBMTP_INFO(" Device flags: 0x%08x\n", ptp_usb->rawdevice.device_entry.device_flags);
Linus Walleij9521e2b2007-07-10 21:50:42 +0000719 (void) probe_device_descriptor(dev, stdout);
Linus Walleijc6210fb2006-05-08 11:11:41 +0000720}
721
Linus Walleija4e6bdc2008-05-23 22:31:53 +0000722/**
723 * Retrieve the apropriate playlist extension for this
724 * device. Rather hacky at the moment. This is probably
725 * desired by the managing software, but when creating
726 * lists on the device itself you notice certain preferences.
727 * @param ptp_usb the USB device to get suggestion for.
728 * @return the suggested playlist extension.
729 */
Richard Lowe029eba2009-04-11 12:36:25 +0000730const char *get_playlist_extension(PTP_USB *ptp_usb)
Linus Walleija4e6bdc2008-05-23 22:31:53 +0000731{
732 struct usb_device *dev;
733 static char creative_pl_extension[] = ".zpl";
734 static char default_pl_extension[] = ".pla";
735
736 dev = usb_device(ptp_usb->handle);
737 if (dev->descriptor.idVendor == 0x041e) {
738 return creative_pl_extension;
739 }
740 return default_pl_extension;
741}
742
Linus Walleijd1e14e02008-12-14 01:07:30 +0000743static void
744libusb_glue_debug (PTPParams *params, const char *format, ...)
Linus Walleij14735952010-04-25 04:56:49 +0000745{
Linus Walleija0323272007-01-07 12:21:30 +0000746 va_list args;
747
748 va_start (args, format);
749 if (params->debug_func!=NULL)
750 params->debug_func (params->data, format, args);
751 else
752 {
753 vfprintf (stderr, format, args);
754 fprintf (stderr,"\n");
755 fflush (stderr);
756 }
757 va_end (args);
Linus Walleij14735952010-04-25 04:56:49 +0000758}
Linus Walleija0323272007-01-07 12:21:30 +0000759
760static void
Linus Walleijd1e14e02008-12-14 01:07:30 +0000761libusb_glue_error (PTPParams *params, const char *format, ...)
Linus Walleij49557482010-06-22 09:14:47 +0000762{
Linus Walleija0323272007-01-07 12:21:30 +0000763 va_list args;
764
765 va_start (args, format);
766 if (params->error_func!=NULL)
767 params->error_func (params->data, format, args);
768 else
769 {
770 vfprintf (stderr, format, args);
771 fprintf (stderr,"\n");
772 fflush (stderr);
773 }
774 va_end (args);
775}
776
Linus Walleijc6210fb2006-05-08 11:11:41 +0000777
Linus Walleij99ed83c2007-01-02 18:46:02 +0000778/*
779 * ptp_read_func() and ptp_write_func() are
Linus Walleijc0a11432007-03-02 21:21:18 +0000780 * based on same functions usb.c in libgphoto2.
Linus Walleij99ed83c2007-01-02 18:46:02 +0000781 * Much reading packet logs and having fun with trials and errors
782 * reveals that WMP / Windows is probably using an algorithm like this
783 * for large transfers:
784 *
Linus Walleij49557482010-06-22 09:14:47 +0000785 * 1. Send the command (0x0c bytes) if headers are split, else, send
Linus Walleij99ed83c2007-01-02 18:46:02 +0000786 * command plus sizeof(endpoint) - 0x0c bytes.
787 * 2. Send first packet, max size to be sizeof(endpoint) but only when using
788 * split headers. Else goto 3.
789 * 3. REPEAT send 0x10000 byte chunks UNTIL remaining bytes < 0x10000
790 * We call 0x10000 CONTEXT_BLOCK_SIZE.
791 * 4. Send remaining bytes MOD sizeof(endpoint)
792 * 5. Send remaining bytes. If this happens to be exactly sizeof(endpoint)
793 * then also send a zero-length package.
Linus Walleij049f50c2007-03-03 20:30:43 +0000794 *
795 * Further there is some special quirks to handle zero reads from the
796 * device, since some devices can't do them at all due to shortcomings
797 * of the USB slave controller in the device.
Linus Walleij99ed83c2007-01-02 18:46:02 +0000798 */
Richard Low4fd59d62007-03-03 16:34:37 +0000799#define CONTEXT_BLOCK_SIZE_1 0x3e00
800#define CONTEXT_BLOCK_SIZE_2 0x200
801#define CONTEXT_BLOCK_SIZE CONTEXT_BLOCK_SIZE_1+CONTEXT_BLOCK_SIZE_2
Linus Walleijb02a0662006-04-25 08:05:09 +0000802static short
Linus Walleij784ac3f2006-12-29 10:36:51 +0000803ptp_read_func (
804 unsigned long size, PTPDataHandler *handler,void *data,
Richard Low4df32f82007-01-11 20:04:39 +0000805 unsigned long *readbytes,
806 int readzero
Linus Walleij784ac3f2006-12-29 10:36:51 +0000807) {
Linus Walleijb02a0662006-04-25 08:05:09 +0000808 PTP_USB *ptp_usb = (PTP_USB *)data;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000809 unsigned long toread = 0;
Linus Walleijd6a49972006-05-02 08:24:54 +0000810 int result = 0;
Linus Walleij99ed83c2007-01-02 18:46:02 +0000811 unsigned long curread = 0;
812 unsigned long written;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000813 unsigned char *bytes;
Linus Walleij049f50c2007-03-03 20:30:43 +0000814 int expect_terminator_byte = 0;
Linus Walleij99ed83c2007-01-02 18:46:02 +0000815
816 // This is the largest block we'll need to read in.
Linus Walleij784ac3f2006-12-29 10:36:51 +0000817 bytes = malloc(CONTEXT_BLOCK_SIZE);
Linus Walleijd6a49972006-05-02 08:24:54 +0000818 while (curread < size) {
nicklas79daadbf22009-09-28 18:19:34 +0000819
820 LIBMTP_USB_DEBUG("Remaining size to read: 0x%04lx bytes\n", size - curread);
821
Richard Low4fd59d62007-03-03 16:34:37 +0000822 // check equal to condition here
823 if (size - curread < CONTEXT_BLOCK_SIZE)
824 {
825 // this is the last packet
826 toread = size - curread;
827 // this is equivalent to zero read for these devices
Linus Walleijfec4d562008-06-01 22:30:36 +0000828 if (readzero && FLAG_NO_ZERO_READS(ptp_usb) && toread % 64 == 0) {
Richard Low4fd59d62007-03-03 16:34:37 +0000829 toread += 1;
Linus Walleij049f50c2007-03-03 20:30:43 +0000830 expect_terminator_byte = 1;
Richard Low4fd59d62007-03-03 16:34:37 +0000831 }
Linus Walleijc49c6372007-01-09 00:18:51 +0000832 }
Richard Low4fd59d62007-03-03 16:34:37 +0000833 else if (curread == 0)
834 // we are first packet, but not last packet
835 toread = CONTEXT_BLOCK_SIZE_1;
836 else if (toread == CONTEXT_BLOCK_SIZE_1)
837 toread = CONTEXT_BLOCK_SIZE_2;
838 else if (toread == CONTEXT_BLOCK_SIZE_2)
839 toread = CONTEXT_BLOCK_SIZE_1;
840 else
nicklas79daadbf22009-09-28 18:19:34 +0000841 LIBMTP_INFO("unexpected toread size 0x%04x, 0x%04x remaining bytes\n",
Linus Walleij4cec9872007-03-03 21:00:53 +0000842 (unsigned int) toread, (unsigned int) (size-curread));
Linus Walleij784ac3f2006-12-29 10:36:51 +0000843
nicklas79daadbf22009-09-28 18:19:34 +0000844 LIBMTP_USB_DEBUG("Reading in 0x%04lx bytes\n", toread);
845
Linus Walleij2f622812008-08-30 22:06:58 +0000846 result = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep, (char*)bytes, toread, ptp_usb->timeout);
nicklas79daadbf22009-09-28 18:19:34 +0000847
848 LIBMTP_USB_DEBUG("Result of read: 0x%04x\n", result);
Linus Walleij49557482010-06-22 09:14:47 +0000849
Linus Walleijc49c6372007-01-09 00:18:51 +0000850 if (result < 0) {
Richard Low43fdb882006-09-06 16:19:05 +0000851 return PTP_ERROR_IO;
Linus Walleijc49c6372007-01-09 00:18:51 +0000852 }
nicklas79daadbf22009-09-28 18:19:34 +0000853
854 LIBMTP_USB_DEBUG("<==USB IN\n");
Richard Low4df32f82007-01-11 20:04:39 +0000855 if (result == 0)
nicklas79daadbf22009-09-28 18:19:34 +0000856 LIBMTP_USB_DEBUG("Zero Read\n");
Richard Low4df32f82007-01-11 20:04:39 +0000857 else
nicklas79daadbf22009-09-28 18:19:34 +0000858 LIBMTP_USB_DATA(bytes, result, 16);
Linus Walleij49557482010-06-22 09:14:47 +0000859
Richard Low4fd59d62007-03-03 16:34:37 +0000860 // want to discard extra byte
Linus Walleij049f50c2007-03-03 20:30:43 +0000861 if (expect_terminator_byte && result == toread)
Richard Lowbf2675c2007-03-18 18:20:26 +0000862 {
nicklas79daadbf22009-09-28 18:19:34 +0000863 LIBMTP_USB_DEBUG("<==USB IN\nDiscarding extra byte\n");
864
Richard Low4fd59d62007-03-03 16:34:37 +0000865 result--;
Richard Lowbf2675c2007-03-18 18:20:26 +0000866 }
Linus Walleij49557482010-06-22 09:14:47 +0000867
Linus Walleijd866d242009-08-23 21:50:39 +0000868 int putfunc_ret = handler->putfunc(NULL, handler->priv, result, bytes, &written);
Richard Lowd24f9232009-05-03 10:22:08 +0000869 if (putfunc_ret != PTP_RC_OK)
870 return putfunc_ret;
Linus Walleij49557482010-06-22 09:14:47 +0000871
Linus Walleijc49c6372007-01-09 00:18:51 +0000872 ptp_usb->current_transfer_complete += result;
Linus Walleijd6a49972006-05-02 08:24:54 +0000873 curread += result;
Linus Walleijc49c6372007-01-09 00:18:51 +0000874
875 // Increase counters, call callback
876 if (ptp_usb->callback_active) {
877 if (ptp_usb->current_transfer_complete >= ptp_usb->current_transfer_total) {
878 // send last update and disable callback.
879 ptp_usb->current_transfer_complete = ptp_usb->current_transfer_total;
880 ptp_usb->callback_active = 0;
881 }
882 if (ptp_usb->current_transfer_callback != NULL) {
Linus Walleijbd3bf9e2007-09-14 19:31:54 +0000883 int ret;
884 ret = ptp_usb->current_transfer_callback(ptp_usb->current_transfer_complete,
885 ptp_usb->current_transfer_total,
886 ptp_usb->current_transfer_callback_data);
887 if (ret != 0) {
888 return PTP_ERROR_CANCEL;
889 }
Linus Walleijc49c6372007-01-09 00:18:51 +0000890 }
Linus Walleij49557482010-06-22 09:14:47 +0000891 }
Linus Walleijc49c6372007-01-09 00:18:51 +0000892
Linus Walleijd6a49972006-05-02 08:24:54 +0000893 if (result < toread) /* short reads are common */
894 break;
895 }
Linus Walleij784ac3f2006-12-29 10:36:51 +0000896 if (readbytes) *readbytes = curread;
897 free (bytes);
Linus Walleij49557482010-06-22 09:14:47 +0000898
Richard Low4df32f82007-01-11 20:04:39 +0000899 // there might be a zero packet waiting for us...
Linus Walleij49557482010-06-22 09:14:47 +0000900 if (readzero &&
901 !FLAG_NO_ZERO_READS(ptp_usb) &&
Linus Walleij049f50c2007-03-03 20:30:43 +0000902 curread % ptp_usb->outep_maxpacket == 0) {
Richard Low4df32f82007-01-11 20:04:39 +0000903 char temp;
904 int zeroresult = 0;
Linus Walleij049f50c2007-03-03 20:30:43 +0000905
nicklas79daadbf22009-09-28 18:19:34 +0000906 LIBMTP_USB_DEBUG("<==USB IN\n");
907 LIBMTP_USB_DEBUG("Zero Read\n");
908
Linus Walleij2f622812008-08-30 22:06:58 +0000909 zeroresult = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep, &temp, 0, ptp_usb->timeout);
Richard Low4df32f82007-01-11 20:04:39 +0000910 if (zeroresult != 0)
nicklas79daadbf22009-09-28 18:19:34 +0000911 LIBMTP_INFO("LIBMTP panic: unable to read in zero packet, response 0x%04x", zeroresult);
Richard Low4df32f82007-01-11 20:04:39 +0000912 }
Linus Walleij49557482010-06-22 09:14:47 +0000913
Richard Low4d9165f2008-09-23 20:13:17 +0000914 return PTP_RC_OK;
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000915}
916
Linus Walleijb02a0662006-04-25 08:05:09 +0000917static short
Linus Walleij784ac3f2006-12-29 10:36:51 +0000918ptp_write_func (
919 unsigned long size,
920 PTPDataHandler *handler,
921 void *data,
922 unsigned long *written
923) {
Linus Walleijb02a0662006-04-25 08:05:09 +0000924 PTP_USB *ptp_usb = (PTP_USB *)data;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000925 unsigned long towrite = 0;
Linus Walleijd6a49972006-05-02 08:24:54 +0000926 int result = 0;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000927 unsigned long curwrite = 0;
928 unsigned char *bytes;
Linus Walleij99ed83c2007-01-02 18:46:02 +0000929
Linus Walleij49557482010-06-22 09:14:47 +0000930 // This is the largest block we'll need to read in.
Linus Walleij784ac3f2006-12-29 10:36:51 +0000931 bytes = malloc(CONTEXT_BLOCK_SIZE);
Linus Walleijc49c6372007-01-09 00:18:51 +0000932 if (!bytes) {
933 return PTP_ERROR_IO;
934 }
Linus Walleijd6a49972006-05-02 08:24:54 +0000935 while (curwrite < size) {
Linus Walleijb84b3852009-03-04 15:11:08 +0000936 unsigned long usbwritten = 0;
Linus Walleijd6a49972006-05-02 08:24:54 +0000937 towrite = size-curwrite;
Linus Walleijc49c6372007-01-09 00:18:51 +0000938 if (towrite > CONTEXT_BLOCK_SIZE) {
Richard Low43fdb882006-09-06 16:19:05 +0000939 towrite = CONTEXT_BLOCK_SIZE;
Linus Walleijc49c6372007-01-09 00:18:51 +0000940 } else {
941 // This magic makes packets the same size that WMP send them.
942 if (towrite > ptp_usb->outep_maxpacket && towrite % ptp_usb->outep_maxpacket != 0) {
Richard Low021421e2007-01-01 18:08:57 +0000943 towrite -= towrite % ptp_usb->outep_maxpacket;
Linus Walleijc49c6372007-01-09 00:18:51 +0000944 }
945 }
Linus Walleijd866d242009-08-23 21:50:39 +0000946 int getfunc_ret = handler->getfunc(NULL, handler->priv,towrite,bytes,&towrite);
Richard Lowd24f9232009-05-03 10:22:08 +0000947 if (getfunc_ret != PTP_RC_OK)
948 return getfunc_ret;
Linus Walleijb84b3852009-03-04 15:11:08 +0000949 while (usbwritten < towrite) {
950 result = USB_BULK_WRITE(ptp_usb->handle,ptp_usb->outep,((char*)bytes+usbwritten),towrite-usbwritten,ptp_usb->timeout);
nicklas79daadbf22009-09-28 18:19:34 +0000951
952 LIBMTP_USB_DEBUG("USB OUT==>\n");
953 LIBMTP_USB_DATA(bytes+usbwritten, result, 16);
954
Linus Walleijb84b3852009-03-04 15:11:08 +0000955 if (result < 0) {
956 return PTP_ERROR_IO;
957 }
958 // check for result == 0 perhaps too.
959 // Increase counters
960 ptp_usb->current_transfer_complete += result;
961 curwrite += result;
962 usbwritten += result;
Linus Walleijc49c6372007-01-09 00:18:51 +0000963 }
Linus Walleijc49c6372007-01-09 00:18:51 +0000964 // call callback
965 if (ptp_usb->callback_active) {
966 if (ptp_usb->current_transfer_complete >= ptp_usb->current_transfer_total) {
967 // send last update and disable callback.
968 ptp_usb->current_transfer_complete = ptp_usb->current_transfer_total;
969 ptp_usb->callback_active = 0;
970 }
971 if (ptp_usb->current_transfer_callback != NULL) {
Linus Walleijbd3bf9e2007-09-14 19:31:54 +0000972 int ret;
973 ret = ptp_usb->current_transfer_callback(ptp_usb->current_transfer_complete,
974 ptp_usb->current_transfer_total,
975 ptp_usb->current_transfer_callback_data);
976 if (ret != 0) {
977 return PTP_ERROR_CANCEL;
978 }
Linus Walleijc49c6372007-01-09 00:18:51 +0000979 }
980 }
Linus Walleijd6a49972006-05-02 08:24:54 +0000981 if (result < towrite) /* short writes happen */
982 break;
983 }
Linus Walleij784ac3f2006-12-29 10:36:51 +0000984 free (bytes);
Linus Walleijc49c6372007-01-09 00:18:51 +0000985 if (written) {
986 *written = curwrite;
Linus Walleij80d134a2006-08-22 21:41:37 +0000987 }
Linus Walleijc49c6372007-01-09 00:18:51 +0000988
Richard Lowef05b892007-01-03 21:18:56 +0000989 // If this is the last transfer send a zero write if required
990 if (ptp_usb->current_transfer_complete >= ptp_usb->current_transfer_total) {
Richard Low68f45882007-01-03 10:08:31 +0000991 if ((towrite % ptp_usb->outep_maxpacket) == 0) {
nicklas79daadbf22009-09-28 18:19:34 +0000992
993 LIBMTP_USB_DEBUG("USB OUT==>\n");
994 LIBMTP_USB_DEBUG("Zero Write\n");
995
Linus Walleij2f622812008-08-30 22:06:58 +0000996 result=USB_BULK_WRITE(ptp_usb->handle,ptp_usb->outep,(char *)"x",0,ptp_usb->timeout);
Linus Walleij7f0c72e2006-09-06 13:01:58 +0000997 }
Linus Walleijd214b9b2006-08-26 22:08:37 +0000998 }
Linus Walleij49557482010-06-22 09:14:47 +0000999
Linus Walleijd6a49972006-05-02 08:24:54 +00001000 if (result < 0)
1001 return PTP_ERROR_IO;
1002 return PTP_RC_OK;
Linus Walleijb02a0662006-04-25 08:05:09 +00001003}
1004
Linus Walleija0323272007-01-07 12:21:30 +00001005/* memory data get/put handler */
1006typedef struct {
1007 unsigned char *data;
1008 unsigned long size, curoff;
1009} PTPMemHandlerPrivate;
Linus Walleij784ac3f2006-12-29 10:36:51 +00001010
Linus Walleija0323272007-01-07 12:21:30 +00001011static uint16_t
1012memory_getfunc(PTPParams* params, void* private,
1013 unsigned long wantlen, unsigned char *data,
1014 unsigned long *gotlen
1015) {
1016 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)private;
1017 unsigned long tocopy = wantlen;
1018
1019 if (priv->curoff + tocopy > priv->size)
1020 tocopy = priv->size - priv->curoff;
1021 memcpy (data, priv->data + priv->curoff, tocopy);
1022 priv->curoff += tocopy;
1023 *gotlen = tocopy;
1024 return PTP_RC_OK;
Linus Walleijb02a0662006-04-25 08:05:09 +00001025}
1026
Linus Walleija0323272007-01-07 12:21:30 +00001027static uint16_t
1028memory_putfunc(PTPParams* params, void* private,
1029 unsigned long sendlen, unsigned char *data,
1030 unsigned long *putlen
1031) {
1032 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)private;
1033
1034 if (priv->curoff + sendlen > priv->size) {
1035 priv->data = realloc (priv->data, priv->curoff+sendlen);
1036 priv->size = priv->curoff + sendlen;
1037 }
1038 memcpy (priv->data + priv->curoff, data, sendlen);
1039 priv->curoff += sendlen;
1040 *putlen = sendlen;
1041 return PTP_RC_OK;
1042}
1043
1044/* init private struct for receiving data. */
1045static uint16_t
1046ptp_init_recv_memory_handler(PTPDataHandler *handler) {
1047 PTPMemHandlerPrivate* priv;
1048 priv = malloc (sizeof(PTPMemHandlerPrivate));
Linus Walleijd866d242009-08-23 21:50:39 +00001049 handler->priv = priv;
Linus Walleija0323272007-01-07 12:21:30 +00001050 handler->getfunc = memory_getfunc;
1051 handler->putfunc = memory_putfunc;
1052 priv->data = NULL;
1053 priv->size = 0;
1054 priv->curoff = 0;
1055 return PTP_RC_OK;
1056}
1057
1058/* init private struct and put data in for sending data.
1059 * data is still owned by caller.
1060 */
1061static uint16_t
1062ptp_init_send_memory_handler(PTPDataHandler *handler,
1063 unsigned char *data, unsigned long len
1064) {
1065 PTPMemHandlerPrivate* priv;
1066 priv = malloc (sizeof(PTPMemHandlerPrivate));
1067 if (!priv)
1068 return PTP_RC_GeneralError;
Linus Walleijd866d242009-08-23 21:50:39 +00001069 handler->priv = priv;
Linus Walleija0323272007-01-07 12:21:30 +00001070 handler->getfunc = memory_getfunc;
1071 handler->putfunc = memory_putfunc;
1072 priv->data = data;
1073 priv->size = len;
1074 priv->curoff = 0;
1075 return PTP_RC_OK;
1076}
1077
1078/* free private struct + data */
1079static uint16_t
1080ptp_exit_send_memory_handler (PTPDataHandler *handler) {
Linus Walleijd866d242009-08-23 21:50:39 +00001081 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)handler->priv;
Linus Walleija0323272007-01-07 12:21:30 +00001082 /* data is owned by caller */
1083 free (priv);
1084 return PTP_RC_OK;
1085}
1086
1087/* hand over our internal data to caller */
1088static uint16_t
1089ptp_exit_recv_memory_handler (PTPDataHandler *handler,
1090 unsigned char **data, unsigned long *size
1091) {
Linus Walleijd866d242009-08-23 21:50:39 +00001092 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)handler->priv;
Linus Walleija0323272007-01-07 12:21:30 +00001093 *data = priv->data;
1094 *size = priv->size;
1095 free (priv);
1096 return PTP_RC_OK;
1097}
1098
1099/* send / receive functions */
1100
1101uint16_t
1102ptp_usb_sendreq (PTPParams* params, PTPContainer* req)
1103{
1104 uint16_t ret;
1105 PTPUSBBulkContainer usbreq;
1106 PTPDataHandler memhandler;
Linus Walleij5ba40402008-05-23 21:36:54 +00001107 unsigned long written = 0;
1108 unsigned long towrite;
nicklas79daadbf22009-09-28 18:19:34 +00001109
Linus Walleijb8f78c72008-03-02 20:22:56 +00001110 char txt[256];
Linus Walleija0323272007-01-07 12:21:30 +00001111
Linus Walleijb8f78c72008-03-02 20:22:56 +00001112 (void) ptp_render_opcode (params, req->Code, sizeof(txt), txt);
nicklas79daadbf22009-09-28 18:19:34 +00001113 LIBMTP_USB_DEBUG("REQUEST: 0x%04x, %s\n", req->Code, txt);
1114
Linus Walleija0323272007-01-07 12:21:30 +00001115 /* build appropriate USB container */
1116 usbreq.length=htod32(PTP_USB_BULK_REQ_LEN-
1117 (sizeof(uint32_t)*(5-req->Nparam)));
1118 usbreq.type=htod16(PTP_USB_CONTAINER_COMMAND);
1119 usbreq.code=htod16(req->Code);
1120 usbreq.trans_id=htod32(req->Transaction_ID);
1121 usbreq.payload.params.param1=htod32(req->Param1);
1122 usbreq.payload.params.param2=htod32(req->Param2);
1123 usbreq.payload.params.param3=htod32(req->Param3);
1124 usbreq.payload.params.param4=htod32(req->Param4);
1125 usbreq.payload.params.param5=htod32(req->Param5);
1126 /* send it to responder */
1127 towrite = PTP_USB_BULK_REQ_LEN-(sizeof(uint32_t)*(5-req->Nparam));
1128 ptp_init_send_memory_handler (&memhandler, (unsigned char*)&usbreq, towrite);
1129 ret=ptp_write_func(
1130 towrite,
1131 &memhandler,
1132 params->data,
1133 &written
1134 );
1135 ptp_exit_send_memory_handler (&memhandler);
Linus Walleijff01cb12007-09-16 19:49:48 +00001136 if (ret!=PTP_RC_OK && ret!=PTP_ERROR_CANCEL) {
Linus Walleija0323272007-01-07 12:21:30 +00001137 ret = PTP_ERROR_IO;
Linus Walleija0323272007-01-07 12:21:30 +00001138 }
Linus Walleij724f0102007-09-17 20:10:12 +00001139 if (written != towrite && ret != PTP_ERROR_CANCEL && ret != PTP_ERROR_IO) {
Linus Walleij49557482010-06-22 09:14:47 +00001140 libusb_glue_error (params,
Linus Walleija0323272007-01-07 12:21:30 +00001141 "PTP: request code 0x%04x sending req wrote only %ld bytes instead of %d",
Richard Lowd284f072007-02-17 08:52:41 +00001142 req->Code, written, towrite
Linus Walleija0323272007-01-07 12:21:30 +00001143 );
1144 ret = PTP_ERROR_IO;
1145 }
1146 return ret;
1147}
1148
1149uint16_t
1150ptp_usb_senddata (PTPParams* params, PTPContainer* ptp,
1151 unsigned long size, PTPDataHandler *handler
1152) {
1153 uint16_t ret;
1154 int wlen, datawlen;
1155 unsigned long written;
1156 PTPUSBBulkContainer usbdata;
1157 uint32_t bytes_left_to_transfer;
1158 PTPDataHandler memhandler;
1159
nicklas79daadbf22009-09-28 18:19:34 +00001160
1161 LIBMTP_USB_DEBUG("SEND DATA PHASE\n");
1162
Linus Walleija0323272007-01-07 12:21:30 +00001163 /* build appropriate USB container */
1164 usbdata.length = htod32(PTP_USB_BULK_HDR_LEN+size);
1165 usbdata.type = htod16(PTP_USB_CONTAINER_DATA);
1166 usbdata.code = htod16(ptp->Code);
1167 usbdata.trans_id= htod32(ptp->Transaction_ID);
Linus Walleij14735952010-04-25 04:56:49 +00001168
Linus Walleija0323272007-01-07 12:21:30 +00001169 ((PTP_USB*)params->data)->current_transfer_complete = 0;
Richard Low4eb01122007-02-24 10:11:32 +00001170 ((PTP_USB*)params->data)->current_transfer_total = size+PTP_USB_BULK_HDR_LEN;
Linus Walleija0323272007-01-07 12:21:30 +00001171
1172 if (params->split_header_data) {
1173 datawlen = 0;
1174 wlen = PTP_USB_BULK_HDR_LEN;
1175 } else {
1176 unsigned long gotlen;
1177 /* For all camera devices. */
Richard Low02724f62007-02-17 09:47:26 +00001178 datawlen = (size<PTP_USB_BULK_PAYLOAD_LEN_WRITE)?size:PTP_USB_BULK_PAYLOAD_LEN_WRITE;
Linus Walleija0323272007-01-07 12:21:30 +00001179 wlen = PTP_USB_BULK_HDR_LEN + datawlen;
Linus Walleij14735952010-04-25 04:56:49 +00001180
Linus Walleijd866d242009-08-23 21:50:39 +00001181 ret = handler->getfunc(params, handler->priv, datawlen, usbdata.payload.data, &gotlen);
Linus Walleija0323272007-01-07 12:21:30 +00001182 if (ret != PTP_RC_OK)
1183 return ret;
1184 if (gotlen != datawlen)
1185 return PTP_RC_GeneralError;
1186 }
1187 ptp_init_send_memory_handler (&memhandler, (unsigned char *)&usbdata, wlen);
1188 /* send first part of data */
1189 ret = ptp_write_func(wlen, &memhandler, params->data, &written);
1190 ptp_exit_send_memory_handler (&memhandler);
1191 if (ret!=PTP_RC_OK) {
Linus Walleija0323272007-01-07 12:21:30 +00001192 return ret;
1193 }
1194 if (size <= datawlen) return ret;
1195 /* if everything OK send the rest */
1196 bytes_left_to_transfer = size-datawlen;
1197 ret = PTP_RC_OK;
1198 while(bytes_left_to_transfer > 0) {
1199 ret = ptp_write_func (bytes_left_to_transfer, handler, params->data, &written);
1200 if (ret != PTP_RC_OK)
1201 break;
1202 if (written == 0) {
1203 ret = PTP_ERROR_IO;
1204 break;
1205 }
1206 bytes_left_to_transfer -= written;
1207 }
Linus Walleijff01cb12007-09-16 19:49:48 +00001208 if (ret!=PTP_RC_OK && ret!=PTP_ERROR_CANCEL)
Linus Walleija0323272007-01-07 12:21:30 +00001209 ret = PTP_ERROR_IO;
1210 return ret;
1211}
1212
1213static uint16_t ptp_usb_getpacket(PTPParams *params,
1214 PTPUSBBulkContainer *packet, unsigned long *rlen)
1215{
1216 PTPDataHandler memhandler;
1217 uint16_t ret;
1218 unsigned char *x = NULL;
1219
1220 /* read the header and potentially the first data */
1221 if (params->response_packet_size > 0) {
1222 /* If there is a buffered packet, just use it. */
1223 memcpy(packet, params->response_packet, params->response_packet_size);
1224 *rlen = params->response_packet_size;
1225 free(params->response_packet);
1226 params->response_packet = NULL;
1227 params->response_packet_size = 0;
1228 /* Here this signifies a "virtual read" */
1229 return PTP_RC_OK;
1230 }
1231 ptp_init_recv_memory_handler (&memhandler);
Richard Low02724f62007-02-17 09:47:26 +00001232 ret = ptp_read_func(PTP_USB_BULK_HS_MAX_PACKET_LEN_READ, &memhandler, params->data, rlen, 0);
Linus Walleija0323272007-01-07 12:21:30 +00001233 ptp_exit_recv_memory_handler (&memhandler, &x, rlen);
1234 if (x) {
1235 memcpy (packet, x, *rlen);
1236 free (x);
1237 }
1238 return ret;
1239}
1240
1241uint16_t
1242ptp_usb_getdata (PTPParams* params, PTPContainer* ptp, PTPDataHandler *handler)
1243{
1244 uint16_t ret;
1245 PTPUSBBulkContainer usbdata;
Linus Walleija0323272007-01-07 12:21:30 +00001246 unsigned long written;
Linus Walleij8e3af002007-09-28 19:21:54 +00001247 PTP_USB *ptp_usb = (PTP_USB *) params->data;
Linus Walleij14735952010-04-25 04:56:49 +00001248 int putfunc_ret;
nicklas79daadbf22009-09-28 18:19:34 +00001249
1250 LIBMTP_USB_DEBUG("GET DATA PHASE\n");
1251
Linus Walleija0323272007-01-07 12:21:30 +00001252 memset(&usbdata,0,sizeof(usbdata));
1253 do {
1254 unsigned long len, rlen;
1255
1256 ret = ptp_usb_getpacket(params, &usbdata, &rlen);
1257 if (ret!=PTP_RC_OK) {
1258 ret = PTP_ERROR_IO;
1259 break;
Linus Walleij8e3af002007-09-28 19:21:54 +00001260 }
Linus Walleija0323272007-01-07 12:21:30 +00001261 if (dtoh16(usbdata.type)!=PTP_USB_CONTAINER_DATA) {
1262 ret = PTP_ERROR_DATA_EXPECTED;
1263 break;
Linus Walleij8e3af002007-09-28 19:21:54 +00001264 }
Linus Walleija0323272007-01-07 12:21:30 +00001265 if (dtoh16(usbdata.code)!=ptp->Code) {
Linus Walleijfec4d562008-06-01 22:30:36 +00001266 if (FLAG_IGNORE_HEADER_ERRORS(ptp_usb)) {
Linus Walleijd1e14e02008-12-14 01:07:30 +00001267 libusb_glue_debug (params, "ptp2/ptp_usb_getdata: detected a broken "
Linus Walleij913a3062007-09-28 21:42:39 +00001268 "PTP header, code field insane, expect problems! (But continuing)");
Linus Walleij06542122007-10-14 22:13:48 +00001269 // Repair the header, so it won't wreak more havoc, don't just ignore it.
1270 // Typically these two fields will be broken.
1271 usbdata.code = htod16(ptp->Code);
1272 usbdata.trans_id = htod32(ptp->Transaction_ID);
Linus Walleij94cd9722007-09-28 21:29:53 +00001273 ret = PTP_RC_OK;
Linus Walleij8e3af002007-09-28 19:21:54 +00001274 } else {
Linus Walleij913a3062007-09-28 21:42:39 +00001275 ret = dtoh16(usbdata.code);
1276 // This filters entirely insane garbage return codes, but still
1277 // makes it possible to return error codes in the code field when
Linus Walleij49557482010-06-22 09:14:47 +00001278 // getting data. It appears Windows ignores the contents of this
Linus Walleij913a3062007-09-28 21:42:39 +00001279 // field entirely.
1280 if (ret < PTP_RC_Undefined || ret > PTP_RC_SpecificationOfDestinationUnsupported) {
Linus Walleijd1e14e02008-12-14 01:07:30 +00001281 libusb_glue_debug (params, "ptp2/ptp_usb_getdata: detected a broken "
Linus Walleij913a3062007-09-28 21:42:39 +00001282 "PTP header, code field insane.");
1283 ret = PTP_ERROR_IO;
1284 }
Linus Walleij8e3af002007-09-28 19:21:54 +00001285 break;
1286 }
Linus Walleija0323272007-01-07 12:21:30 +00001287 }
1288 if (usbdata.length == 0xffffffffU) {
Linus Walleij14735952010-04-25 04:56:49 +00001289 /* Copy first part of data to 'data' */
1290 putfunc_ret =
1291 handler->putfunc(
1292 params, handler->priv, rlen - PTP_USB_BULK_HDR_LEN, usbdata.payload.data,
1293 &written
1294 );
1295 if (putfunc_ret != PTP_RC_OK)
1296 return putfunc_ret;
Linus Walleija0323272007-01-07 12:21:30 +00001297
Linus Walleij14735952010-04-25 04:56:49 +00001298 /* stuff data directly to passed data handler */
1299 while (1) {
1300 unsigned long readdata;
1301 uint16_t xret;
1302
1303 xret = ptp_read_func(
1304 PTP_USB_BULK_HS_MAX_PACKET_LEN_READ,
1305 handler,
1306 params->data,
1307 &readdata,
1308 0
1309 );
1310 if (xret != PTP_RC_OK)
1311 return xret;
1312 if (readdata < PTP_USB_BULK_HS_MAX_PACKET_LEN_READ)
1313 break;
1314 }
1315 return PTP_RC_OK;
Linus Walleija0323272007-01-07 12:21:30 +00001316 }
1317 if (rlen > dtoh32(usbdata.length)) {
1318 /*
1319 * Buffer the surplus response packet if it is >=
1320 * PTP_USB_BULK_HDR_LEN
1321 * (i.e. it is probably an entire package)
1322 * else discard it as erroneous surplus data.
1323 * This will even work if more than 2 packets appear
1324 * in the same transaction, they will just be handled
1325 * iteratively.
1326 *
1327 * Marcus observed stray bytes on iRiver devices;
1328 * these are still discarded.
1329 */
1330 unsigned int packlen = dtoh32(usbdata.length);
1331 unsigned int surplen = rlen - packlen;
1332
1333 if (surplen >= PTP_USB_BULK_HDR_LEN) {
1334 params->response_packet = malloc(surplen);
1335 memcpy(params->response_packet,
1336 (uint8_t *) &usbdata + packlen, surplen);
1337 params->response_packet_size = surplen;
tedbullock36f2afb2007-03-03 22:16:44 +00001338 /* Ignore reading one extra byte if device flags have been set */
Linus Walleijfec4d562008-06-01 22:30:36 +00001339 } else if(!FLAG_NO_ZERO_READS(ptp_usb) &&
1340 (rlen - dtoh32(usbdata.length) == 1)) {
Linus Walleijd1e14e02008-12-14 01:07:30 +00001341 libusb_glue_debug (params, "ptp2/ptp_usb_getdata: read %d bytes "
Linus Walleij49557482010-06-22 09:14:47 +00001342 "too much, expect problems!",
Linus Walleij8e3af002007-09-28 19:21:54 +00001343 rlen - dtoh32(usbdata.length));
Linus Walleija0323272007-01-07 12:21:30 +00001344 }
1345 rlen = packlen;
1346 }
1347
1348 /* For most PTP devices rlen is 512 == sizeof(usbdata)
1349 * here. For MTP devices splitting header and data it might
1350 * be 12.
1351 */
1352 /* Evaluate full data length. */
1353 len=dtoh32(usbdata.length)-PTP_USB_BULK_HDR_LEN;
1354
1355 /* autodetect split header/data MTP devices */
1356 if (dtoh32(usbdata.length) > 12 && (rlen==12))
1357 params->split_header_data = 1;
1358
Linus Walleija0323272007-01-07 12:21:30 +00001359 /* Copy first part of data to 'data' */
Linus Walleij14735952010-04-25 04:56:49 +00001360 putfunc_ret =
1361 handler->putfunc(
1362 params, handler->priv, rlen - PTP_USB_BULK_HDR_LEN,
1363 usbdata.payload.data,
1364 &written
1365 );
1366 if (putfunc_ret != PTP_RC_OK)
1367 return putfunc_ret;
Linus Walleijd866d242009-08-23 21:50:39 +00001368
1369 if (FLAG_NO_ZERO_READS(ptp_usb) &&
Linus Walleij049f50c2007-03-03 20:30:43 +00001370 len+PTP_USB_BULK_HDR_LEN == PTP_USB_BULK_HS_MAX_PACKET_LEN_READ) {
nicklas79daadbf22009-09-28 18:19:34 +00001371
1372 LIBMTP_USB_DEBUG("Reading in extra terminating byte\n");
1373
Linus Walleij049f50c2007-03-03 20:30:43 +00001374 // need to read in extra byte and discard it
1375 int result = 0;
1376 char byte = 0;
Linus Walleij2f622812008-08-30 22:06:58 +00001377 result = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep, &byte, 1, ptp_usb->timeout);
Linus Walleijd866d242009-08-23 21:50:39 +00001378
Linus Walleij049f50c2007-03-03 20:30:43 +00001379 if (result != 1)
nicklas79daadbf22009-09-28 18:19:34 +00001380 LIBMTP_INFO("Could not read in extra byte for PTP_USB_BULK_HS_MAX_PACKET_LEN_READ long file, return value 0x%04x\n", result);
Richard Low057ea772007-03-24 13:37:38 +00001381 } else if (len+PTP_USB_BULK_HDR_LEN == PTP_USB_BULK_HS_MAX_PACKET_LEN_READ && params->split_header_data == 0) {
Linus Walleij94cd9722007-09-28 21:29:53 +00001382 int zeroresult = 0;
1383 char zerobyte = 0;
1384
nicklas79daadbf22009-09-28 18:19:34 +00001385
1386 LIBMTP_INFO("Reading in zero packet after header\n");
1387
Linus Walleij14735952010-04-25 04:56:49 +00001388 zeroresult = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep, &zerobyte, 0, ptp_usb->timeout);
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001389
Linus Walleij94cd9722007-09-28 21:29:53 +00001390 if (zeroresult != 0)
nicklas79daadbf22009-09-28 18:19:34 +00001391 LIBMTP_INFO("LIBMTP panic: unable to read in zero packet, response 0x%04x", zeroresult);
Linus Walleij94cd9722007-09-28 21:29:53 +00001392 }
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001393
Linus Walleija0323272007-01-07 12:21:30 +00001394 /* Is that all of data? */
Linus Walleij94cd9722007-09-28 21:29:53 +00001395 if (len+PTP_USB_BULK_HDR_LEN<=rlen) {
1396 break;
1397 }
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001398
Linus Walleij913a3062007-09-28 21:42:39 +00001399 ret = ptp_read_func(len - (rlen - PTP_USB_BULK_HDR_LEN),
1400 handler,
1401 params->data, &rlen, 1);
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001402
Linus Walleija0323272007-01-07 12:21:30 +00001403 if (ret!=PTP_RC_OK) {
Linus Walleij049f50c2007-03-03 20:30:43 +00001404 break;
Linus Walleija0323272007-01-07 12:21:30 +00001405 }
1406 } while (0);
Linus Walleija0323272007-01-07 12:21:30 +00001407 return ret;
1408}
1409
1410uint16_t
1411ptp_usb_getresp (PTPParams* params, PTPContainer* resp)
1412{
1413 uint16_t ret;
1414 unsigned long rlen;
1415 PTPUSBBulkContainer usbresp;
Linus Walleij3e4b1142007-11-01 23:54:16 +00001416 PTP_USB *ptp_usb = (PTP_USB *)(params->data);
Linus Walleija0323272007-01-07 12:21:30 +00001417
nicklas79daadbf22009-09-28 18:19:34 +00001418
1419 LIBMTP_USB_DEBUG("RESPONSE: ");
1420
Linus Walleija0323272007-01-07 12:21:30 +00001421 memset(&usbresp,0,sizeof(usbresp));
1422 /* read response, it should never be longer than sizeof(usbresp) */
1423 ret = ptp_usb_getpacket(params, &usbresp, &rlen);
1424
Linus Walleijb8f78c72008-03-02 20:22:56 +00001425 // Fix for bevahiour reported by Scott Snyder on Samsung YP-U3. The player
1426 // sends a packet containing just zeroes of length 2 (up to 4 has been seen too)
1427 // after a NULL packet when it should send the response. This code ignores
1428 // such illegal packets.
Linus Walleij3191bba2008-02-10 21:16:25 +00001429 while (ret==PTP_RC_OK && rlen<PTP_USB_BULK_HDR_LEN && usbresp.length==0) {
Linus Walleijd1e14e02008-12-14 01:07:30 +00001430 libusb_glue_debug (params, "ptp_usb_getresp: detected short response "
Linus Walleij3191bba2008-02-10 21:16:25 +00001431 "of %d bytes, expect problems! (re-reading "
1432 "response), rlen");
Linus Walleij00411ee2008-01-27 12:24:51 +00001433 ret = ptp_usb_getpacket(params, &usbresp, &rlen);
1434 }
1435
Linus Walleija0323272007-01-07 12:21:30 +00001436 if (ret!=PTP_RC_OK) {
1437 ret = PTP_ERROR_IO;
1438 } else
1439 if (dtoh16(usbresp.type)!=PTP_USB_CONTAINER_RESPONSE) {
1440 ret = PTP_ERROR_RESP_EXPECTED;
1441 } else
1442 if (dtoh16(usbresp.code)!=resp->Code) {
1443 ret = dtoh16(usbresp.code);
1444 }
nicklas79daadbf22009-09-28 18:19:34 +00001445
1446 LIBMTP_USB_DEBUG("%04x\n", ret);
1447
Linus Walleija0323272007-01-07 12:21:30 +00001448 if (ret!=PTP_RC_OK) {
Linus Walleijd1e14e02008-12-14 01:07:30 +00001449/* libusb_glue_error (params,
Linus Walleija0323272007-01-07 12:21:30 +00001450 "PTP: request code 0x%04x getting resp error 0x%04x",
1451 resp->Code, ret);*/
1452 return ret;
1453 }
1454 /* build an appropriate PTPContainer */
1455 resp->Code=dtoh16(usbresp.code);
1456 resp->SessionID=params->session_id;
1457 resp->Transaction_ID=dtoh32(usbresp.trans_id);
Linus Walleijfec4d562008-06-01 22:30:36 +00001458 if (FLAG_IGNORE_HEADER_ERRORS(ptp_usb)) {
Linus Walleij3e4b1142007-11-01 23:54:16 +00001459 if (resp->Transaction_ID != params->transaction_id-1) {
Linus Walleijd1e14e02008-12-14 01:07:30 +00001460 libusb_glue_debug (params, "ptp_usb_getresp: detected a broken "
Linus Walleij00411ee2008-01-27 12:24:51 +00001461 "PTP header, transaction ID insane, expect "
1462 "problems! (But continuing)");
Linus Walleij3e4b1142007-11-01 23:54:16 +00001463 // Repair the header, so it won't wreak more havoc.
1464 resp->Transaction_ID = params->transaction_id-1;
1465 }
1466 }
Linus Walleija0323272007-01-07 12:21:30 +00001467 resp->Param1=dtoh32(usbresp.payload.params.param1);
1468 resp->Param2=dtoh32(usbresp.payload.params.param2);
1469 resp->Param3=dtoh32(usbresp.payload.params.param3);
1470 resp->Param4=dtoh32(usbresp.payload.params.param4);
1471 resp->Param5=dtoh32(usbresp.payload.params.param5);
1472 return ret;
1473}
1474
1475/* Event handling functions */
1476
1477/* PTP Events wait for or check mode */
1478#define PTP_EVENT_CHECK 0x0000 /* waits for */
1479#define PTP_EVENT_CHECK_FAST 0x0001 /* checks */
1480
1481static inline uint16_t
1482ptp_usb_event (PTPParams* params, PTPContainer* event, int wait)
1483{
1484 uint16_t ret;
1485 int result;
1486 unsigned long rlen;
1487 PTPUSBEventContainer usbevent;
1488 PTP_USB *ptp_usb = (PTP_USB *)(params->data);
1489
1490 memset(&usbevent,0,sizeof(usbevent));
1491
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001492 if ((params==NULL) || (event==NULL))
Linus Walleija0323272007-01-07 12:21:30 +00001493 return PTP_ERROR_BADPARAM;
1494 ret = PTP_RC_OK;
1495 switch(wait) {
1496 case PTP_EVENT_CHECK:
Linus Walleij2f622812008-08-30 22:06:58 +00001497 result=USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *)&usbevent,sizeof(usbevent),ptp_usb->timeout);
Linus Walleija0323272007-01-07 12:21:30 +00001498 if (result==0)
Linus Walleij2f622812008-08-30 22:06:58 +00001499 result = USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *) &usbevent, sizeof(usbevent), ptp_usb->timeout);
Linus Walleija0323272007-01-07 12:21:30 +00001500 if (result < 0) ret = PTP_ERROR_IO;
1501 break;
1502 case PTP_EVENT_CHECK_FAST:
Linus Walleij2f622812008-08-30 22:06:58 +00001503 result=USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *)&usbevent,sizeof(usbevent),ptp_usb->timeout);
Linus Walleija0323272007-01-07 12:21:30 +00001504 if (result==0)
Linus Walleij2f622812008-08-30 22:06:58 +00001505 result = USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *) &usbevent, sizeof(usbevent), ptp_usb->timeout);
Linus Walleija0323272007-01-07 12:21:30 +00001506 if (result < 0) ret = PTP_ERROR_IO;
1507 break;
1508 default:
1509 ret=PTP_ERROR_BADPARAM;
1510 break;
1511 }
1512 if (ret!=PTP_RC_OK) {
Linus Walleijd1e14e02008-12-14 01:07:30 +00001513 libusb_glue_error (params,
Linus Walleija0323272007-01-07 12:21:30 +00001514 "PTP: reading event an error 0x%04x occurred", ret);
1515 return PTP_ERROR_IO;
1516 }
1517 rlen = result;
1518 if (rlen < 8) {
Linus Walleijd1e14e02008-12-14 01:07:30 +00001519 libusb_glue_error (params,
Linus Walleija0323272007-01-07 12:21:30 +00001520 "PTP: reading event an short read of %ld bytes occurred", rlen);
1521 return PTP_ERROR_IO;
1522 }
1523 /* if we read anything over interrupt endpoint it must be an event */
1524 /* build an appropriate PTPContainer */
1525 event->Code=dtoh16(usbevent.code);
1526 event->SessionID=params->session_id;
1527 event->Transaction_ID=dtoh32(usbevent.trans_id);
1528 event->Param1=dtoh32(usbevent.param1);
1529 event->Param2=dtoh32(usbevent.param2);
1530 event->Param3=dtoh32(usbevent.param3);
1531 return ret;
1532}
1533
1534uint16_t
1535ptp_usb_event_check (PTPParams* params, PTPContainer* event) {
1536
1537 return ptp_usb_event (params, event, PTP_EVENT_CHECK_FAST);
1538}
1539
1540uint16_t
1541ptp_usb_event_wait (PTPParams* params, PTPContainer* event) {
1542
1543 return ptp_usb_event (params, event, PTP_EVENT_CHECK);
1544}
1545
Linus Walleijbd3bf9e2007-09-14 19:31:54 +00001546uint16_t
1547ptp_usb_control_cancel_request (PTPParams *params, uint32_t transactionid) {
1548 PTP_USB *ptp_usb = (PTP_USB *)(params->data);
1549 int ret;
1550 unsigned char buffer[6];
1551
1552 htod16a(&buffer[0],PTP_EC_CancelTransaction);
1553 htod32a(&buffer[2],transactionid);
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001554 ret = usb_control_msg(ptp_usb->handle,
Linus Walleijbd3bf9e2007-09-14 19:31:54 +00001555 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
Linus Walleij2f622812008-08-30 22:06:58 +00001556 0x64, 0x0000, 0x0000, (char *) buffer, sizeof(buffer), ptp_usb->timeout);
Linus Walleijbd3bf9e2007-09-14 19:31:54 +00001557 if (ret < sizeof(buffer))
1558 return PTP_ERROR_IO;
1559 return PTP_RC_OK;
1560}
Linus Walleija0323272007-01-07 12:21:30 +00001561
Linus Walleij9eb3d312006-08-04 19:25:59 +00001562static int init_ptp_usb (PTPParams* params, PTP_USB* ptp_usb, struct usb_device* dev)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001563{
Linus Walleijd6a49972006-05-02 08:24:54 +00001564 usb_dev_handle *device_handle;
Linus Walleijfea4f532009-09-22 22:08:35 +00001565 char buf[255];
Linus Walleijf2d747a2010-01-19 00:46:18 +00001566 int usbresult;
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001567
Linus Walleijd6a49972006-05-02 08:24:54 +00001568 params->sendreq_func=ptp_usb_sendreq;
1569 params->senddata_func=ptp_usb_senddata;
1570 params->getresp_func=ptp_usb_getresp;
1571 params->getdata_func=ptp_usb_getdata;
Linus Walleijff01cb12007-09-16 19:49:48 +00001572 params->cancelreq_func=ptp_usb_control_cancel_request;
Linus Walleijd6a49972006-05-02 08:24:54 +00001573 params->data=ptp_usb;
1574 params->transaction_id=0;
Linus Walleij462a5e72007-02-13 06:54:56 +00001575 /*
1576 * This is hardcoded here since we have no devices whatsoever that are BE.
1577 * Change this the day we run into our first BE device (if ever).
1578 */
Linus Walleijd6a49972006-05-02 08:24:54 +00001579 params->byteorder = PTP_DL_LE;
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001580
Linus Walleij2f622812008-08-30 22:06:58 +00001581 ptp_usb->timeout = USB_TIMEOUT_DEFAULT;
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001582
Linus Walleij0ed2a9f2006-05-04 05:31:34 +00001583 if ((device_handle = usb_open(dev))){
Linus Walleijd6a49972006-05-02 08:24:54 +00001584 if (!device_handle) {
1585 perror("usb_open()");
Linus Walleij9eb3d312006-08-04 19:25:59 +00001586 return -1;
Linus Walleijd6a49972006-05-02 08:24:54 +00001587 }
Linus Walleij0ed2a9f2006-05-04 05:31:34 +00001588 ptp_usb->handle = device_handle;
Linus Walleij501ba4d2006-10-16 06:17:04 +00001589#ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP
1590 /*
1591 * If this device is known to be wrongfully claimed by other kernel
1592 * drivers (such as mass storage), then try to unload it to make it
1593 * accessible from user space.
1594 */
Linus Walleijfec4d562008-06-01 22:30:36 +00001595 if (FLAG_UNLOAD_DRIVER(ptp_usb)) {
Linus Walleijfea4f532009-09-22 22:08:35 +00001596 if (usb_get_driver_np(device_handle, (int) ptp_usb->interface,
1597 buf, sizeof(buf)) == 0) {
1598 if (usb_detach_kernel_driver_np(device_handle,
1599 (int) ptp_usb->interface)) {
Linus Walleijcea83ff2009-09-25 21:44:38 +00001600 perror("usb_detach_kernel_driver_np()");
1601 return -1;
Linus Walleijfea4f532009-09-22 22:08:35 +00001602 }
Linus Walleij501ba4d2006-10-16 06:17:04 +00001603 }
1604 }
1605#endif
Linus Walleijeac1e002006-11-30 22:06:02 +00001606#ifdef __WIN32__
Linus Walleij99ed83c2007-01-02 18:46:02 +00001607 // Only needed on Windows, and cause problems on other platforms.
Linus Walleij7beba572006-11-29 08:56:12 +00001608 if (usb_set_configuration(device_handle, dev->config->bConfigurationValue)) {
1609 perror("usb_set_configuration()");
1610 return -1;
1611 }
Linus Walleijeac1e002006-11-30 22:06:02 +00001612#endif
Linus Walleij49557482010-06-22 09:14:47 +00001613 // It seems like on kernel 2.6.31 if we already have it open on another
Linus Walleijf2d747a2010-01-19 00:46:18 +00001614 // pthread in our app, we'll get an error if we try to claim it again,
1615 // but that error is harmless because our process already claimed the interface
1616 usbresult = usb_claim_interface(device_handle, (int) ptp_usb->interface);
1617
1618 if (usbresult != 0)
1619 fprintf(stderr, "ignoring usb_claim_interface = %d", usbresult);
1620
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001621 // FIXME : Discovered in the Barry project
1622 // kernels >= 2.6.28 don't set the interface the same way as
1623 // previous versions did, and the Blackberry gets confused
1624 // if it isn't explicitly set
Linus Walleijf2d747a2010-01-19 00:46:18 +00001625 // See above, same issue with pthreads means that if this fails it is not
1626 // fatal
Richard Lowea73f5c2010-05-16 11:57:56 +00001627 // However, this causes problems on Macs so disable here
1628 #ifndef __APPLE__
Linus Walleijf2d747a2010-01-19 00:46:18 +00001629 usbresult = usb_set_altinterface(device_handle, 0);
1630 if (usbresult)
1631 fprintf(stderr, "ignoring usb_claim_interface = %d", usbresult);
Richard Lowea73f5c2010-05-16 11:57:56 +00001632 #endif
Linus Walleijf2d747a2010-01-19 00:46:18 +00001633
Linus Walleijfea4f532009-09-22 22:08:35 +00001634 if (FLAG_SWITCH_MODE_BLACKBERRY(ptp_usb)) {
Linus Walleijb6e06e22009-09-22 22:20:49 +00001635 int ret;
1636
Linus Walleijfea4f532009-09-22 22:08:35 +00001637 // FIXME : Only for BlackBerry Storm
1638 // What does it mean? Maybe switch mode...
Linus Walleijcea83ff2009-09-25 21:44:38 +00001639 // This first control message is absolutely necessary
Linus Walleijfea4f532009-09-22 22:08:35 +00001640 usleep(1000);
Linus Walleijb6e06e22009-09-22 22:20:49 +00001641 ret = usb_control_msg(device_handle,
Linus Walleijfea4f532009-09-22 22:08:35 +00001642 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
1643 0xaa, 0x00, 0x04, buf, 0x40, 1000);
nicklas79daadbf22009-09-28 18:19:34 +00001644 LIBMTP_USB_DEBUG("BlackBerry magic part 1:\n");
1645 LIBMTP_USB_DATA(buf, ret, 16);
1646
Linus Walleijfea4f532009-09-22 22:08:35 +00001647 usleep(1000);
Linus Walleijcea83ff2009-09-25 21:44:38 +00001648 // This control message is unnecessary
Linus Walleijb6e06e22009-09-22 22:20:49 +00001649 ret = usb_control_msg(device_handle,
Linus Walleijfea4f532009-09-22 22:08:35 +00001650 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
1651 0xa5, 0x00, 0x01, buf, 0x02, 1000);
nicklas79daadbf22009-09-28 18:19:34 +00001652 LIBMTP_USB_DEBUG("BlackBerry magic part 2:\n");
1653 LIBMTP_USB_DATA(buf, ret, 16);
1654
Linus Walleijfea4f532009-09-22 22:08:35 +00001655 usleep(1000);
Linus Walleijcea83ff2009-09-25 21:44:38 +00001656 // This control message is unnecessary
Linus Walleijb6e06e22009-09-22 22:20:49 +00001657 ret = usb_control_msg(device_handle,
Linus Walleijfea4f532009-09-22 22:08:35 +00001658 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
1659 0xa8, 0x00, 0x01, buf, 0x05, 1000);
nicklas79daadbf22009-09-28 18:19:34 +00001660 LIBMTP_USB_DEBUG("BlackBerry magic part 3:\n");
1661 LIBMTP_USB_DATA(buf, ret, 16);
1662
Linus Walleijfea4f532009-09-22 22:08:35 +00001663 usleep(1000);
Linus Walleijcea83ff2009-09-25 21:44:38 +00001664 // This control message is unnecessary
Linus Walleijb6e06e22009-09-22 22:20:49 +00001665 ret = usb_control_msg(device_handle,
Linus Walleijfea4f532009-09-22 22:08:35 +00001666 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
1667 0xa8, 0x00, 0x01, buf, 0x11, 1000);
nicklas79daadbf22009-09-28 18:19:34 +00001668 LIBMTP_USB_DEBUG("BlackBerry magic part 4:\n");
1669 LIBMTP_USB_DATA(buf, ret, 16);
1670
Linus Walleijfea4f532009-09-22 22:08:35 +00001671 usleep(1000);
1672 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001673 }
Linus Walleij9eb3d312006-08-04 19:25:59 +00001674 return 0;
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001675}
1676
Linus Walleijd6a49972006-05-02 08:24:54 +00001677static void clear_stall(PTP_USB* ptp_usb)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001678{
Linus Walleij58b62792007-01-07 12:38:59 +00001679 uint16_t status;
Linus Walleijd6a49972006-05-02 08:24:54 +00001680 int ret;
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001681
Linus Walleijd6a49972006-05-02 08:24:54 +00001682 /* check the inep status */
Linus Walleij58b62792007-01-07 12:38:59 +00001683 status = 0;
Linus Walleij1fd2d272006-05-08 09:22:01 +00001684 ret = usb_get_endpoint_status(ptp_usb,ptp_usb->inep,&status);
Linus Walleij58b62792007-01-07 12:38:59 +00001685 if (ret<0) {
1686 perror ("inep: usb_get_endpoint_status()");
1687 } else if (status) {
nicklas79daadbf22009-09-28 18:19:34 +00001688 LIBMTP_INFO("Clearing stall on IN endpoint\n");
Linus Walleij58b62792007-01-07 12:38:59 +00001689 ret = usb_clear_stall_feature(ptp_usb,ptp_usb->inep);
1690 if (ret<0) {
1691 perror ("usb_clear_stall_feature()");
1692 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001693 }
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001694
Linus Walleijd6a49972006-05-02 08:24:54 +00001695 /* check the outep status */
Linus Walleij58b62792007-01-07 12:38:59 +00001696 status=0;
1697 ret = usb_get_endpoint_status(ptp_usb,ptp_usb->outep,&status);
1698 if (ret<0) {
1699 perror("outep: usb_get_endpoint_status()");
1700 } else if (status) {
nicklas79daadbf22009-09-28 18:19:34 +00001701 LIBMTP_INFO("Clearing stall on OUT endpoint\n");
Linus Walleij58b62792007-01-07 12:38:59 +00001702 ret = usb_clear_stall_feature(ptp_usb,ptp_usb->outep);
1703 if (ret<0) {
1704 perror("usb_clear_stall_feature()");
1705 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001706 }
Linus Walleij58b62792007-01-07 12:38:59 +00001707
1708 /* TODO: do we need this for INTERRUPT (ptp_usb->intep) too? */
1709}
1710
1711static void clear_halt(PTP_USB* ptp_usb)
1712{
1713 int ret;
1714
1715 ret = usb_clear_halt(ptp_usb->handle,ptp_usb->inep);
1716 if (ret<0) {
1717 perror("usb_clear_halt() on IN endpoint");
1718 }
1719 ret = usb_clear_halt(ptp_usb->handle,ptp_usb->outep);
1720 if (ret<0) {
1721 perror("usb_clear_halt() on OUT endpoint");
1722 }
1723 ret = usb_clear_halt(ptp_usb->handle,ptp_usb->intep);
1724 if (ret<0) {
1725 perror("usb_clear_halt() on INTERRUPT endpoint");
1726 }
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001727}
1728
Linus Walleijb0ab5482007-08-29 21:08:54 +00001729static void close_usb(PTP_USB* ptp_usb)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001730{
Linus Walleijda17cda2007-09-18 21:12:05 +00001731 // Commented out since it was confusing some
1732 // devices to do these things.
Linus Walleijfec4d562008-06-01 22:30:36 +00001733 if (!FLAG_NO_RELEASE_INTERFACE(ptp_usb)) {
Linus Walleijeabc2312008-02-13 00:02:00 +00001734 /*
1735 * Clear any stalled endpoints
1736 * On misbehaving devices designed for Windows/Mac, quote from:
1737 * http://www2.one-eyed-alien.net/~mdharm/linux-usb/target_offenses.txt
1738 * Device does Bad Things(tm) when it gets a GET_STATUS after CLEAR_HALT
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001739 * (...) Windows, when clearing a stall, only sends the CLEAR_HALT command,
1740 * and presumes that the stall has cleared. Some devices actually choke
1741 * if the CLEAR_HALT is followed by a GET_STATUS (used to determine if the
Linus Walleijeabc2312008-02-13 00:02:00 +00001742 * STALL is persistant or not).
1743 */
Linus Walleij29f03ba2007-09-25 19:22:12 +00001744 clear_stall(ptp_usb);
1745 // Clear halts on any endpoints
1746 clear_halt(ptp_usb);
1747 // Added to clear some stuff on the OUT endpoint
1748 // TODO: is this good on the Mac too?
1749 // HINT: some devices may need that you comment these two out too.
1750 usb_resetep(ptp_usb->handle, ptp_usb->outep);
1751 usb_release_interface(ptp_usb->handle, (int) ptp_usb->interface);
1752 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001753 usb_close(ptp_usb->handle);
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001754}
1755
Linus Walleij550d6d52007-01-24 14:32:51 +00001756/**
Linus Walleija700d222008-05-28 23:06:14 +00001757 * Self-explanatory?
1758 */
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001759static int find_interface_and_endpoints(struct usb_device *dev,
1760 uint8_t *interface,
1761 int* inep,
1762 int* inep_maxpacket,
1763 int* outep,
1764 int *outep_maxpacket,
1765 int* intep)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001766{
Linus Walleijd7373cd2007-08-28 21:42:05 +00001767 int i;
1768
1769 // Loop over the device configurations
1770 for (i = 0; i < dev->descriptor.bNumConfigurations; i++) {
Linus Walleijb0ab5482007-08-29 21:08:54 +00001771 uint8_t j;
Linus Walleijd7373cd2007-08-28 21:42:05 +00001772
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001773 // Loop over each configurations interfaces
Linus Walleijd7373cd2007-08-28 21:42:05 +00001774 for (j = 0; j < dev->config[i].bNumInterfaces; j++) {
Linus Walleijb0ab5482007-08-29 21:08:54 +00001775 uint8_t k;
1776 uint8_t no_ep;
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001777 int found_inep = 0;
1778 int found_outep = 0;
1779 int found_intep = 0;
Linus Walleijd7373cd2007-08-28 21:42:05 +00001780 struct usb_endpoint_descriptor *ep;
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001781
1782 // MTP devices shall have 3 endpoints, ignore those interfaces
1783 // that haven't.
1784 no_ep = dev->config[i].interface[j].altsetting->bNumEndpoints;
1785 if (no_ep != 3)
1786 continue;
Linus Walleijb0ab5482007-08-29 21:08:54 +00001787
1788 *interface = dev->config[i].interface[j].altsetting->bInterfaceNumber;
1789 ep = dev->config[i].interface[j].altsetting->endpoint;
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001790
1791 // Loop over the three endpoints to locate two bulk and
1792 // one interrupt endpoint and FAIL if we cannot, and continue.
Linus Walleijb0ab5482007-08-29 21:08:54 +00001793 for (k = 0; k < no_ep; k++) {
Linus Walleije73854e2010-06-22 09:07:34 +00001794 if (ep[k].bmAttributes == USB_ENDPOINT_TYPE_BULK) {
1795 if ((ep[k].bEndpointAddress & USB_ENDPOINT_DIR_MASK) ==
1796 USB_ENDPOINT_DIR_MASK) {
1797 *inep = ep[k].bEndpointAddress;
1798 *inep_maxpacket = ep[k].wMaxPacketSize;
1799 found_inep = 1;
1800 }
1801 if ((ep[k].bEndpointAddress & USB_ENDPOINT_DIR_MASK) == 0) {
Linus Walleij48fda572010-06-22 09:09:03 +00001802 *outep = ep[k].bEndpointAddress;
1803 *outep_maxpacket = ep[k].wMaxPacketSize;
Linus Walleije73854e2010-06-22 09:07:34 +00001804 found_outep = 1;
1805 }
1806 } else if (ep[k].bmAttributes == USB_ENDPOINT_TYPE_INTERRUPT) {
1807 if ((ep[k].bEndpointAddress & USB_ENDPOINT_DIR_MASK) ==
1808 USB_ENDPOINT_DIR_MASK) {
Linus Walleij48fda572010-06-22 09:09:03 +00001809 *intep = ep[k].bEndpointAddress;
Linus Walleije73854e2010-06-22 09:07:34 +00001810 found_intep = 1;
1811 }
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001812 }
Linus Walleijd7373cd2007-08-28 21:42:05 +00001813 }
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001814 if (found_inep && found_outep && found_intep)
1815 // We assigned the endpoints so return here.
1816 return 0;
1817 // Else loop to next interface/config
Linus Walleijd6a49972006-05-02 08:24:54 +00001818 }
1819 }
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001820 return -1;
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001821}
1822
Linus Walleija700d222008-05-28 23:06:14 +00001823/**
1824 * This function assigns params and usbinfo given a raw device
1825 * as input.
1826 * @param device the device to be assigned.
Linus Walleija700d222008-05-28 23:06:14 +00001827 * @param usbinfo a pointer to the new usbinfo.
1828 * @return an error code.
1829 */
Linus Walleij49557482010-06-22 09:14:47 +00001830LIBMTP_error_number_t configure_usb_device(LIBMTP_raw_device_t *device,
Linus Walleij3c643252008-05-31 23:22:28 +00001831 PTPParams *params,
Linus Walleija700d222008-05-28 23:06:14 +00001832 void **usbinfo)
1833{
Linus Walleija700d222008-05-28 23:06:14 +00001834 PTP_USB *ptp_usb;
1835 struct usb_device *libusb_device;
1836 uint16_t ret = 0;
Linus Walleija700d222008-05-28 23:06:14 +00001837 struct usb_bus *bus;
1838 int found = 0;
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001839 int err;
Linus Walleija700d222008-05-28 23:06:14 +00001840
1841 /* See if we can find this raw device again... */
1842 bus = init_usb();
1843 for (; bus != NULL; bus = bus->next) {
1844 if (bus->location == device->bus_location) {
1845 struct usb_device *dev = bus->devices;
1846
1847 for (; dev != NULL; dev = dev->next) {
1848 if(dev->devnum == device->devnum &&
1849 dev->descriptor.idVendor == device->device_entry.vendor_id &&
1850 dev->descriptor.idProduct == device->device_entry.product_id ) {
1851 libusb_device = dev;
1852 found = 1;
1853 break;
1854 }
1855 }
1856 if (found)
1857 break;
1858 }
1859 }
1860 /* Device has gone since detecting raw devices! */
1861 if (!found) {
1862 return LIBMTP_ERROR_NO_DEVICE_ATTACHED;
1863 }
1864
1865 /* Allocate structs */
Linus Walleija700d222008-05-28 23:06:14 +00001866 ptp_usb = (PTP_USB *) malloc(sizeof(PTP_USB));
Linus Walleij3c643252008-05-31 23:22:28 +00001867 if (ptp_usb == NULL) {
Linus Walleija700d222008-05-28 23:06:14 +00001868 return LIBMTP_ERROR_MEMORY_ALLOCATION;
1869 }
1870 /* Start with a blank slate (includes setting device_flags to 0) */
Linus Walleija700d222008-05-28 23:06:14 +00001871 memset(ptp_usb, 0, sizeof(PTP_USB));
1872
Linus Walleijfec4d562008-06-01 22:30:36 +00001873 /* Copy the raw device */
1874 memcpy(&ptp_usb->rawdevice, device, sizeof(LIBMTP_raw_device_t));
Linus Walleij335a81c2008-06-02 23:01:00 +00001875
1876 /*
1877 * Some devices must have their "OS Descriptor" massaged in order
1878 * to work.
1879 */
1880 if (FLAG_ALWAYS_PROBE_DESCRIPTOR(ptp_usb)) {
1881 // Massage the device descriptor
1882 (void) probe_device_descriptor(libusb_device, NULL);
1883 }
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001884
1885 /* Assign interface and endpoints to usbinfo... */
1886 err = find_interface_and_endpoints(libusb_device,
1887 &ptp_usb->interface,
1888 &ptp_usb->inep,
1889 &ptp_usb->inep_maxpacket,
1890 &ptp_usb->outep,
1891 &ptp_usb->outep_maxpacket,
1892 &ptp_usb->intep);
1893
1894 if (err) {
nicklas79daadbf22009-09-28 18:19:34 +00001895 LIBMTP_ERROR("LIBMTP PANIC: Unable to find interface & endpoints of device\n");
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001896 return LIBMTP_ERROR_CONNECTING;
1897 }
1898
Linus Walleija700d222008-05-28 23:06:14 +00001899 /* Attempt to initialize this device */
Linus Walleij3c643252008-05-31 23:22:28 +00001900 if (init_ptp_usb(params, ptp_usb, libusb_device) < 0) {
nicklas79daadbf22009-09-28 18:19:34 +00001901 LIBMTP_ERROR("LIBMTP PANIC: Unable to initialize device\n");
Linus Walleija700d222008-05-28 23:06:14 +00001902 return LIBMTP_ERROR_CONNECTING;
1903 }
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001904
Linus Walleija700d222008-05-28 23:06:14 +00001905 /*
1906 * This works in situations where previous bad applications
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001907 * have not used LIBMTP_Release_Device on exit
Linus Walleija700d222008-05-28 23:06:14 +00001908 */
Linus Walleij3c643252008-05-31 23:22:28 +00001909 if ((ret = ptp_opensession(params, 1)) == PTP_ERROR_IO) {
Linus Walleijf9137dc2010-10-31 17:13:06 +00001910 LIBMTP_ERROR("PTP_ERROR_IO: failed to open session, trying again after resetting USB interface\n");
Linus Walleija700d222008-05-28 23:06:14 +00001911 close_usb(ptp_usb);
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001912
Linus Walleijf9137dc2010-10-31 17:13:06 +00001913 LIBMTP_ERROR("LIBMTP libusb: Attempt to reset device\n");
1914 usb_reset(ptp_usb->handle);
1915
Linus Walleij3c643252008-05-31 23:22:28 +00001916 if(init_ptp_usb(params, ptp_usb, libusb_device) <0) {
Linus Walleijf9137dc2010-10-31 17:13:06 +00001917 LIBMTP_ERROR("LIBMTP PANIC: Could not init USB on second attempt\n");
Linus Walleija700d222008-05-28 23:06:14 +00001918 return LIBMTP_ERROR_CONNECTING;
1919 }
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001920
Linus Walleija700d222008-05-28 23:06:14 +00001921 /* Device has been reset, try again */
Linus Walleijf9137dc2010-10-31 17:13:06 +00001922 if ((ret = ptp_opensession(params, 1)) == PTP_ERROR_IO) {
1923 LIBMTP_ERROR("LIBMTP PANIC: failed to open session on second attempt\n");
1924 return LIBMTP_ERROR_CONNECTING;
1925 }
Linus Walleija700d222008-05-28 23:06:14 +00001926 }
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001927
Linus Walleija700d222008-05-28 23:06:14 +00001928 /* Was the transaction id invalid? Try again */
1929 if (ret == PTP_RC_InvalidTransactionID) {
nicklas79daadbf22009-09-28 18:19:34 +00001930 LIBMTP_ERROR("LIBMTP WARNING: Transaction ID was invalid, increment and try again\n");
Linus Walleij3c643252008-05-31 23:22:28 +00001931 params->transaction_id += 10;
1932 ret = ptp_opensession(params, 1);
Linus Walleija700d222008-05-28 23:06:14 +00001933 }
1934
1935 if (ret != PTP_RC_SessionAlreadyOpened && ret != PTP_RC_OK) {
nicklas79daadbf22009-09-28 18:19:34 +00001936 LIBMTP_ERROR("LIBMTP PANIC: Could not open session! "
Linus Walleija700d222008-05-28 23:06:14 +00001937 "(Return code %d)\n Try to reset the device.\n",
1938 ret);
1939 usb_release_interface(ptp_usb->handle,
1940 (int) ptp_usb->interface);
1941 return LIBMTP_ERROR_CONNECTING;
1942 }
1943
1944 /* OK configured properly */
Linus Walleija700d222008-05-28 23:06:14 +00001945 *usbinfo = (void *) ptp_usb;
1946 return LIBMTP_ERROR_NONE;
1947}
1948
1949
Linus Walleijb0ab5482007-08-29 21:08:54 +00001950void close_device (PTP_USB *ptp_usb, PTPParams *params)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001951{
Linus Walleijd6a49972006-05-02 08:24:54 +00001952 if (ptp_closesession(params)!=PTP_RC_OK)
nicklas79daadbf22009-09-28 18:19:34 +00001953 LIBMTP_ERROR("ERROR: Could not close session!\n");
Linus Walleijb0ab5482007-08-29 21:08:54 +00001954 close_usb(ptp_usb);
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001955}
1956
Linus Walleij2f622812008-08-30 22:06:58 +00001957void set_usb_device_timeout(PTP_USB *ptp_usb, int timeout)
1958{
1959 ptp_usb->timeout = timeout;
1960}
1961
1962void get_usb_device_timeout(PTP_USB *ptp_usb, int *timeout)
1963{
1964 *timeout = ptp_usb->timeout;
1965}
1966
Linus Walleijd6a49972006-05-02 08:24:54 +00001967static int usb_clear_stall_feature(PTP_USB* ptp_usb, int ep)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001968{
Linus Walleijd6a49972006-05-02 08:24:54 +00001969 return (usb_control_msg(ptp_usb->handle,
1970 USB_RECIP_ENDPOINT, USB_REQ_CLEAR_FEATURE, USB_FEATURE_HALT,
Linus Walleij2f622812008-08-30 22:06:58 +00001971 ep, NULL, 0, ptp_usb->timeout));
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001972}
1973
Linus Walleijd6a49972006-05-02 08:24:54 +00001974static int usb_get_endpoint_status(PTP_USB* ptp_usb, int ep, uint16_t* status)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001975{
Linus Walleijd6a49972006-05-02 08:24:54 +00001976 return (usb_control_msg(ptp_usb->handle,
1977 USB_DP_DTH|USB_RECIP_ENDPOINT, USB_REQ_GET_STATUS,
Linus Walleij2f622812008-08-30 22:06:58 +00001978 USB_FEATURE_HALT, ep, (char *)status, 2, ptp_usb->timeout));
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001979}