blob: 3c0a1247ea83f1cc4f886cbc1a735107c1805b38 [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 Walleije04a1b92011-03-09 18:00:24 +010052/*
53 * Default USB timeout length. This can be overridden as needed
54 * but should start with a reasonable value so most common
Linus Walleij2f622812008-08-30 22:06:58 +000055 * requests can be completed. The original value of 4000 was
56 * not long enough for large file transfer. Also, players can
57 * spend a bit of time collecting data. Higher values also
58 * make connecting/disconnecting more reliable.
59 */
Linus Walleijd33cd582011-03-10 22:15:20 +010060#define USB_TIMEOUT_DEFAULT 20000
Linus Walleije04a1b92011-03-09 18:00:24 +010061#define USB_TIMEOUT_LONG 60000
62static inline int get_timeout(PTP_USB* ptp_usb)
63{
64 if (FLAG_LONG_TIMEOUT(ptp_usb)) {
65 return USB_TIMEOUT_LONG;
66 }
67 return USB_TIMEOUT_DEFAULT;
68}
Linus Walleijeb8c6fe2006-02-03 09:46:22 +000069
70/* USB control message data phase direction */
71#ifndef USB_DP_HTD
72#define USB_DP_HTD (0x00 << 7) /* host to device */
73#endif
74#ifndef USB_DP_DTH
75#define USB_DP_DTH (0x01 << 7) /* device to host */
76#endif
77
78/* USB Feature selector HALT */
79#ifndef USB_FEATURE_HALT
80#define USB_FEATURE_HALT 0x00
81#endif
82
Linus Walleija700d222008-05-28 23:06:14 +000083/* Internal data types */
84struct mtpdevice_list_struct {
85 struct usb_device *libusb_device;
86 PTPParams *params;
87 PTP_USB *ptp_usb;
88 uint32_t bus_location;
89 struct mtpdevice_list_struct *next;
90};
91typedef struct mtpdevice_list_struct mtpdevice_list_t;
92
Linus Walleij6fd2f082006-03-28 07:19:22 +000093static const LIBMTP_device_entry_t mtp_device_table[] = {
Linus Walleij1a673de2007-10-29 23:10:05 +000094/* We include an .h file which is shared between us and libgphoto2 */
95#include "music-players.h"
Linus Walleija5483642006-03-09 09:20:38 +000096};
Linus Walleij6fd2f082006-03-28 07:19:22 +000097static const int mtp_device_table_size = sizeof(mtp_device_table) / sizeof(LIBMTP_device_entry_t);
Linus Walleija5483642006-03-09 09:20:38 +000098
Linus Walleijd6a49972006-05-02 08:24:54 +000099// Local functions
Linus Walleij1fd2d272006-05-08 09:22:01 +0000100static struct usb_bus* init_usb();
Linus Walleijb0ab5482007-08-29 21:08:54 +0000101static void close_usb(PTP_USB* ptp_usb);
Linus Walleij5f3c44b2009-09-12 21:03:06 +0000102static int find_interface_and_endpoints(struct usb_device *dev,
103 uint8_t *interface,
104 int* inep,
105 int* inep_maxpacket,
106 int* outep,
107 int* outep_maxpacket,
108 int* intep);
Linus Walleijd6a49972006-05-02 08:24:54 +0000109static void clear_stall(PTP_USB* ptp_usb);
Linus Walleij9eb3d312006-08-04 19:25:59 +0000110static int init_ptp_usb (PTPParams* params, PTP_USB* ptp_usb, struct usb_device* dev);
Linus Walleij784ac3f2006-12-29 10:36:51 +0000111static short ptp_write_func (unsigned long,PTPDataHandler*,void *data,unsigned long*);
Richard Low4df32f82007-01-11 20:04:39 +0000112static short ptp_read_func (unsigned long,PTPDataHandler*,void *data,unsigned long*,int);
Linus Walleijd6a49972006-05-02 08:24:54 +0000113static int usb_clear_stall_feature(PTP_USB* ptp_usb, int ep);
114static int usb_get_endpoint_status(PTP_USB* ptp_usb, int ep, uint16_t* status);
Linus Walleij1fd2d272006-05-08 09:22:01 +0000115
Linus Walleij552ba322007-01-22 11:49:59 +0000116/**
117 * Get a list of the supported USB devices.
118 *
119 * The developers depend on users of this library to constantly
120 * add in to the list of supported devices. What we need is the
121 * device name, USB Vendor ID (VID) and USB Product ID (PID).
122 * put this into a bug ticket at the project homepage, please.
123 * The VID/PID is used to let e.g. udev lift the device to
124 * console userspace access when it's plugged in.
125 *
126 * @param devices a pointer to a pointer that will hold a device
127 * list after the call to this function, if it was
128 * successful.
129 * @param numdevs a pointer to an integer that will hold the number
130 * of devices in the device list if the call was successful.
131 * @return 0 if the list was successfull retrieved, any other
132 * value means failure.
133 */
134int LIBMTP_Get_Supported_Devices_List(LIBMTP_device_entry_t ** const devices, int * const numdevs)
Linus Walleij6fd2f082006-03-28 07:19:22 +0000135{
136 *devices = (LIBMTP_device_entry_t *) &mtp_device_table;
137 *numdevs = mtp_device_table_size;
138 return 0;
139}
140
Linus Walleij552ba322007-01-22 11:49:59 +0000141
Linus Walleij1fd2d272006-05-08 09:22:01 +0000142static struct usb_bus* init_usb()
143{
Linus Walleijc1a82f12009-11-30 00:06:28 +0000144 struct usb_bus* busses;
145 struct usb_bus* bus;
146
147 /*
nicklas79daadbf22009-09-28 18:19:34 +0000148 * Some additional libusb debugging please.
149 * We use the same level debug between MTP and USB.
150 */
nicklas7903584082009-09-28 18:20:16 +0000151 if ((LIBMTP_debug & LIBMTP_DEBUG_USB) != 0)
nicklas79a99196a2009-09-28 18:19:57 +0000152 usb_set_debug(9);
nicklas79daadbf22009-09-28 18:19:34 +0000153
Linus Walleij1fd2d272006-05-08 09:22:01 +0000154 usb_init();
155 usb_find_busses();
156 usb_find_devices();
Linus Walleijc1a82f12009-11-30 00:06:28 +0000157 /* Workaround a libusb 0.1 bug : bus location is not initialised */
158 busses = usb_get_busses();
159 for (bus = busses; bus != NULL; bus = bus->next) {
Richard Lowea73f5c2010-05-16 11:57:56 +0000160 if (!bus->location)
161 bus->location = strtoul(bus->dirname, NULL, 10);
Linus Walleijc1a82f12009-11-30 00:06:28 +0000162 }
163 return (busses);
Linus Walleij1fd2d272006-05-08 09:22:01 +0000164}
165
166/**
tedbullock0f033cb2007-02-14 20:56:54 +0000167 * Small recursive function to append a new usb_device to the linked list of
168 * USB MTP devices
Linus Walleijc1a82f12009-11-30 00:06:28 +0000169 * @param devlist dynamic linked list of pointers to usb devices with MTP
Linus Walleij63fd1e62008-04-23 23:49:43 +0000170 * properties, to be extended with new device.
171 * @param newdevice the new device to add.
172 * @param bus_location bus for this device.
173 * @return an extended array or NULL on failure.
tedbullock0f033cb2007-02-14 20:56:54 +0000174 */
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000175static mtpdevice_list_t *append_to_mtpdevice_list(mtpdevice_list_t *devlist,
Linus Walleij63fd1e62008-04-23 23:49:43 +0000176 struct usb_device *newdevice,
177 uint32_t bus_location)
tedbullock0f033cb2007-02-14 20:56:54 +0000178{
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000179 mtpdevice_list_t *new_list_entry;
Linus Walleijc1a82f12009-11-30 00:06:28 +0000180
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000181 new_list_entry = (mtpdevice_list_t *) malloc(sizeof(mtpdevice_list_t));
182 if (new_list_entry == NULL) {
183 return NULL;
184 }
185 // Fill in USB device, if we *HAVE* to make a copy of the device do it here.
186 new_list_entry->libusb_device = newdevice;
Linus Walleij63fd1e62008-04-23 23:49:43 +0000187 new_list_entry->bus_location = bus_location;
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000188 new_list_entry->next = NULL;
Linus Walleij5d987e12010-05-22 22:35:33 +0000189
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000190 if (devlist == NULL) {
191 return new_list_entry;
192 } else {
193 mtpdevice_list_t *tmp = devlist;
194 while (tmp->next != NULL) {
195 tmp = tmp->next;
196 }
197 tmp->next = new_list_entry;
198 }
199 return devlist;
tedbullock0f033cb2007-02-14 20:56:54 +0000200}
201
202/**
203 * Small recursive function to free dynamic memory allocated to the linked list
204 * of USB MTP devices
Linus Walleij5d987e12010-05-22 22:35:33 +0000205 * @param devlist dynamic linked list of pointers to usb devices with MTP
tedbullock0f033cb2007-02-14 20:56:54 +0000206 * properties.
207 * @return nothing
208 */
Linus Walleija700d222008-05-28 23:06:14 +0000209static void free_mtpdevice_list(mtpdevice_list_t *devlist)
tedbullock0f033cb2007-02-14 20:56:54 +0000210{
Linus Walleijac061862007-03-07 08:28:33 +0000211 mtpdevice_list_t *tmplist = devlist;
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000212
213 if (devlist == NULL)
214 return;
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000215 while (tmplist != NULL) {
216 mtpdevice_list_t *tmp = tmplist;
217 tmplist = tmplist->next;
Richard Low61edc1a2007-09-23 10:35:48 +0000218 // Do not free() the fields (ptp_usb, params)! These are used elsewhere.
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000219 free(tmp);
tedbullock0f033cb2007-02-14 20:56:54 +0000220 }
tedbullock0f033cb2007-02-14 20:56:54 +0000221 return;
222}
223
224/**
Linus Walleij9521e2b2007-07-10 21:50:42 +0000225 * This checks if a device has an MTP descriptor. The descriptor was
226 * elaborated about in gPhoto bug 1482084, and some official documentation
227 * with no strings attached was published by Microsoft at
228 * http://www.microsoft.com/whdc/system/bus/USB/USBFAQ_intermed.mspx#E3HAC
229 *
Linus Walleija4942fc2007-03-12 19:23:21 +0000230 * @param dev a device struct from libusb.
Linus Walleij9521e2b2007-07-10 21:50:42 +0000231 * @param dumpfile set to non-NULL to make the descriptors dump out
232 * to this file in human-readable hex so we can scruitinze them.
Linus Walleija4942fc2007-03-12 19:23:21 +0000233 * @return 1 if the device is MTP compliant, 0 if not.
234 */
Linus Walleij9521e2b2007-07-10 21:50:42 +0000235static int probe_device_descriptor(struct usb_device *dev, FILE *dumpfile)
Linus Walleija4942fc2007-03-12 19:23:21 +0000236{
237 usb_dev_handle *devh;
238 unsigned char buf[1024], cmd;
Linus Walleijbc550bc2007-11-08 23:25:09 +0000239 int i;
Linus Walleija4942fc2007-03-12 19:23:21 +0000240 int ret;
Linus Walleijfb3e3022011-01-19 19:20:34 +0100241 /* This is to indicate if we find some vendor interface */
242 int found_vendor_spec_interface = 0;
Linus Walleij5d987e12010-05-22 22:35:33 +0000243
244 /*
Linus Walleijfb3e3022011-01-19 19:20:34 +0100245 * Don't examine devices that are not likely to
246 * contain any MTP interface, update this the day
247 * you find some weird combination...
Linus Walleij5d987e12010-05-22 22:35:33 +0000248 */
Linus Walleij4eb8ad92011-01-27 22:52:07 +0100249 if (!(dev->descriptor.bDeviceClass == USB_CLASS_PER_INTERFACE ||
250 dev->descriptor.bDeviceClass == USB_CLASS_COMM ||
251 dev->descriptor.bDeviceClass == USB_CLASS_PTP ||
252 dev->descriptor.bDeviceClass == USB_CLASS_VENDOR_SPEC)) {
Linus Walleija4942fc2007-03-12 19:23:21 +0000253 return 0;
254 }
Linus Walleij5d987e12010-05-22 22:35:33 +0000255
Linus Walleija4942fc2007-03-12 19:23:21 +0000256 /* Attempt to open Device on this port */
257 devh = usb_open(dev);
258 if (devh == NULL) {
259 /* Could not open this device */
260 return 0;
261 }
Linus Walleijbc550bc2007-11-08 23:25:09 +0000262
263 /*
Richard Lowe029eba2009-04-11 12:36:25 +0000264 * This sometimes crashes on the j for loop below
Linus Walleij88244212009-08-12 16:05:56 +0000265 * I think it is because config is NULL yet
Richard Lowe029eba2009-04-11 12:36:25 +0000266 * dev->descriptor.bNumConfigurations > 0
267 * this check should stop this
Linus Walleijbc550bc2007-11-08 23:25:09 +0000268 */
Richard Lowe029eba2009-04-11 12:36:25 +0000269 if (dev->config) {
270 /*
Linus Walleij88244212009-08-12 16:05:56 +0000271 * Loop over the device configurations and interfaces. Nokia MTP-capable
272 * handsets (possibly others) typically have the string "MTP" in their
Richard Lowe029eba2009-04-11 12:36:25 +0000273 * MTP interface descriptions, that's how they can be detected, before
274 * we try the more esoteric "OS descriptors" (below).
275 */
276 for (i = 0; i < dev->descriptor.bNumConfigurations; i++) {
277 uint8_t j;
Linus Walleij88244212009-08-12 16:05:56 +0000278
Richard Lowe029eba2009-04-11 12:36:25 +0000279 for (j = 0; j < dev->config[i].bNumInterfaces; j++) {
280 int k;
281 for (k = 0; k < dev->config[i].interface[j].num_altsetting; k++) {
Linus Walleij88244212009-08-12 16:05:56 +0000282 /* Current interface descriptor */
283 struct usb_interface_descriptor *intf =
284 &dev->config[i].interface[j].altsetting[k];
285
286 /*
Linus Walleijf765caf2011-04-01 00:54:04 +0200287 * MTP interfaces have three endpoints, two bulk and one
288 * interrupt. Don't probe anything else.
289 */
290 if (intf->bNumEndpoints != 3)
291 continue;
292
293 /*
Linus Walleijfb3e3022011-01-19 19:20:34 +0100294 * We only want to probe for the OS descriptor if the
295 * device is USB_CLASS_VENDOR_SPEC or one of the interfaces
296 * in it is, so flag if we find an interface like this.
297 */
298 if (intf->bInterfaceClass == USB_CLASS_VENDOR_SPEC) {
299 found_vendor_spec_interface = 1;
300 }
301
302 /*
Linus Walleij88244212009-08-12 16:05:56 +0000303 * Check for Still Image Capture class with PIMA 15740 protocol,
304 * also known as PTP
305 */
Linus Walleij5d92c8c2011-01-25 23:42:19 +0100306#if 0
Linus Walleij88244212009-08-12 16:05:56 +0000307 if (intf->bInterfaceClass == USB_CLASS_PTP
308 && intf->bInterfaceSubClass == 0x01
309 && intf->bInterfaceProtocol == 0x01) {
310 if (dumpfile != NULL) {
Linus Walleij5d92c8c2011-01-25 23:42:19 +0100311 fprintf(dumpfile, " Found PTP device, check vendor "
312 "extension...\n");
Linus Walleij88244212009-08-12 16:05:56 +0000313 }
Linus Walleij5d92c8c2011-01-25 23:42:19 +0100314 // This is where we may insert code to open a PTP
315 // session and query the vendor extension ID to see
Linus Walleij4eb8ad92011-01-27 22:52:07 +0100316 // if it is 0xffffffff, i.e. MTP according to the spec.
Linus Walleij5d92c8c2011-01-25 23:42:19 +0100317 if (was_mtp_extension) {
318 usb_close(devh);
319 return 1;
320 }
Linus Walleij88244212009-08-12 16:05:56 +0000321 }
Linus Walleij5d92c8c2011-01-25 23:42:19 +0100322#endif
323
324 /*
325 * Next we search for the MTP substring in the interface name.
326 * For example : "RIM MS/MTP" should work.
327 */
Linus Walleij88244212009-08-12 16:05:56 +0000328 buf[0] = '\0';
329 ret = usb_get_string_simple(devh,
330 dev->config[i].interface[j].altsetting[k].iInterface,
331 (char *) buf,
332 1024);
333 if (ret < 3)
334 continue;
Linus Walleijfea4f532009-09-22 22:08:35 +0000335 if (strstr((char *) buf, "MTP") != NULL) {
Linus Walleij88244212009-08-12 16:05:56 +0000336 if (dumpfile != NULL) {
337 fprintf(dumpfile, "Configuration %d, interface %d, altsetting %d:\n", i, j, k);
338 fprintf(dumpfile, " Interface description contains the string \"MTP\"\n");
339 fprintf(dumpfile, " Device recognized as MTP, no further probing.\n");
340 }
Richard Lowe029eba2009-04-11 12:36:25 +0000341 usb_close(devh);
342 return 1;
343 }
Linus Walleijf765caf2011-04-01 00:54:04 +0200344#ifdef LIBUSB_HAS_GET_DRIVER_NP
Linus Walleij88244212009-08-12 16:05:56 +0000345 {
346 /*
347 * Specifically avoid probing anything else than USB mass storage devices
348 * and non-associated drivers in Linux.
349 */
350 char devname[0x10];
351
352 devname[0] = '\0';
353 ret = usb_get_driver_np(devh,
354 dev->config[i].interface[j].altsetting[k].iInterface,
355 devname,
356 sizeof(devname));
357 if (devname[0] != '\0' && strcmp(devname, "usb-storage")) {
nicklas79daadbf22009-09-28 18:19:34 +0000358 LIBMTP_INFO("avoid probing device using kernel interface \"%s\"\n", devname);
Linus Walleij88244212009-08-12 16:05:56 +0000359 return 0;
360 }
361 }
Linus Walleijf765caf2011-04-01 00:54:04 +0200362#endif
Richard Lowe029eba2009-04-11 12:36:25 +0000363 }
364 }
365 }
366 } else {
367 if (dev->descriptor.bNumConfigurations)
nicklas79daadbf22009-09-28 18:19:34 +0000368 LIBMTP_INFO("dev->config is NULL in probe_device_descriptor yet dev->descriptor.bNumConfigurations > 0\n");
Linus Walleijbc550bc2007-11-08 23:25:09 +0000369 }
Linus Walleijfea4f532009-09-22 22:08:35 +0000370
Linus Walleij49e57b82009-12-16 09:21:33 +0000371 /*
Linus Walleijfb3e3022011-01-19 19:20:34 +0100372 * Only probe for OS descriptor if the device is vendor specific
373 * or one of the interfaces found is.
Linus Walleij49e57b82009-12-16 09:21:33 +0000374 */
Linus Walleijfb3e3022011-01-19 19:20:34 +0100375 if (dev->descriptor.bDeviceClass == USB_CLASS_VENDOR_SPEC ||
376 found_vendor_spec_interface) {
Linus Walleij9521e2b2007-07-10 21:50:42 +0000377
Linus Walleijfb3e3022011-01-19 19:20:34 +0100378 /* Read the special descriptor */
379 ret = usb_get_descriptor(devh, 0x03, 0xee, buf, sizeof(buf));
Linus Walleijfea4f532009-09-22 22:08:35 +0000380
Linus Walleijfb3e3022011-01-19 19:20:34 +0100381 /*
382 * If something failed we're probably stalled to we need
383 * to clear the stall off the endpoint and say this is not
384 * MTP.
385 */
386 if (ret < 0) {
387 /* EP0 is the default control endpoint */
388 usb_clear_halt(devh, 0);
389 usb_close(devh);
390 return 0;
391 }
392
393 // Dump it, if requested
394 if (dumpfile != NULL && ret > 0) {
395 fprintf(dumpfile, "Microsoft device descriptor 0xee:\n");
396 data_dump_ascii(dumpfile, buf, ret, 16);
397 }
398
399 /* Check if descriptor length is at least 10 bytes */
400 if (ret < 10) {
401 usb_close(devh);
402 return 0;
403 }
404
405 /* Check if this device has a Microsoft Descriptor */
406 if (!((buf[2] == 'M') && (buf[4] == 'S') &&
407 (buf[6] == 'F') && (buf[8] == 'T'))) {
408 usb_close(devh);
409 return 0;
410 }
411
412 /* Check if device responds to control message 1 or if there is an error */
413 cmd = buf[16];
414 ret = usb_control_msg (devh,
415 USB_ENDPOINT_IN | USB_RECIP_DEVICE | USB_TYPE_VENDOR,
416 cmd,
417 0,
418 4,
419 (char *) buf,
420 sizeof(buf),
421 USB_TIMEOUT_DEFAULT);
422
423 // Dump it, if requested
424 if (dumpfile != NULL && ret > 0) {
425 fprintf(dumpfile, "Microsoft device response to control message 1, CMD 0x%02x:\n", cmd);
426 data_dump_ascii(dumpfile, buf, ret, 16);
427 }
428
429 /* If this is true, the device either isn't MTP or there was an error */
430 if (ret <= 0x15) {
431 /* TODO: If there was an error, flag it and let the user know somehow */
432 /* if(ret == -1) {} */
433 usb_close(devh);
434 return 0;
435 }
436
437 /* Check if device is MTP or if it is something like a USB Mass Storage
438 device with Janus DRM support */
439 if ((buf[0x12] != 'M') || (buf[0x13] != 'T') || (buf[0x14] != 'P')) {
440 usb_close(devh);
441 return 0;
442 }
443
444 /* After this point we are probably dealing with an MTP device */
445
446 /*
447 * Check if device responds to control message 2, which is
448 * the extended device parameters. Most devices will just
449 * respond with a copy of the same message as for the first
450 * message, some respond with zero-length (which is OK)
451 * and some with pure garbage. We're not parsing the result
452 * so this is not very important.
453 */
454 ret = usb_control_msg (devh,
455 USB_ENDPOINT_IN | USB_RECIP_DEVICE | USB_TYPE_VENDOR,
456 cmd,
457 0,
458 5,
459 (char *) buf,
460 sizeof(buf),
461 USB_TIMEOUT_DEFAULT);
462
463 // Dump it, if requested
464 if (dumpfile != NULL && ret > 0) {
465 fprintf(dumpfile, "Microsoft device response to control message 2, CMD 0x%02x:\n", cmd);
466 data_dump_ascii(dumpfile, buf, ret, 16);
467 }
468
469 /* If this is true, the device errored against control message 2 */
470 if (ret == -1) {
471 /* TODO: Implement callback function to let managing program know there
472 was a problem, along with description of the problem */
473 LIBMTP_ERROR("Potential MTP Device with VendorID:%04x and "
474 "ProductID:%04x encountered an error responding to "
475 "control message 2.\n"
476 "Problems may arrise but continuing\n",
477 dev->descriptor.idVendor, dev->descriptor.idProduct);
478 } else if (dumpfile != NULL && ret == 0) {
479 fprintf(dumpfile, "Zero-length response to control message 2 (OK)\n");
480 } else if (dumpfile != NULL) {
481 fprintf(dumpfile, "Device responds to control message 2 with some data.\n");
482 }
Marcus Meissnere0bb6c62011-02-07 13:26:58 +0100483 /* Close the USB device handle */
484 usb_close(devh);
485 return 1;
Linus Walleija4942fc2007-03-12 19:23:21 +0000486 }
Linus Walleijfea4f532009-09-22 22:08:35 +0000487
Linus Walleija4942fc2007-03-12 19:23:21 +0000488 /* Close the USB device handle */
489 usb_close(devh);
Marcus Meissnere0bb6c62011-02-07 13:26:58 +0100490 return 0;
Linus Walleija4942fc2007-03-12 19:23:21 +0000491}
492
493/**
494 * This function scans through the connected usb devices on a machine and
495 * if they match known Vendor and Product identifiers appends them to the
Linus Walleij49557482010-06-22 09:14:47 +0000496 * dynamic array mtp_device_list. Be sure to call
497 * <code>free_mtpdevice_list(mtp_device_list)</code> when you are done
Linus Walleij63fd1e62008-04-23 23:49:43 +0000498 * with it, assuming it is not NULL.
Linus Walleij49557482010-06-22 09:14:47 +0000499 * @param mtp_device_list dynamic array of pointers to usb devices with MTP
Linus Walleij45a86372007-03-07 09:36:19 +0000500 * properties (if this list is not empty, new entries will be appended
501 * to the list).
Linus Walleija4942fc2007-03-12 19:23:21 +0000502 * @return LIBMTP_ERROR_NONE implies that devices have been found, scan the list
Linus Walleij49557482010-06-22 09:14:47 +0000503 * appropriately. LIBMTP_ERROR_NO_DEVICE_ATTACHED implies that no
Linus Walleij9c6c1542007-09-19 13:43:06 +0000504 * devices have been found.
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000505 */
Linus Walleij45a86372007-03-07 09:36:19 +0000506static LIBMTP_error_number_t get_mtp_usb_device_list(mtpdevice_list_t ** mtp_device_list)
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000507{
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000508 struct usb_bus *bus = init_usb();
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000509 for (; bus != NULL; bus = bus->next) {
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000510 struct usb_device *dev = bus->devices;
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000511 for (; dev != NULL; dev = dev->next) {
Richard Lowe9f205f2007-09-16 16:25:50 +0000512 if (dev->descriptor.bDeviceClass != USB_CLASS_HUB) {
Linus Walleij9c6c1542007-09-19 13:43:06 +0000513 int i;
514 int found = 0;
515
516 // First check if we know about the device already.
517 // Devices well known to us will not have their descriptors
518 // probed, it caused problems with some devices.
Richard Lowe9f205f2007-09-16 16:25:50 +0000519 for(i = 0; i < mtp_device_table_size; i++) {
520 if(dev->descriptor.idVendor == mtp_device_table[i].vendor_id &&
521 dev->descriptor.idProduct == mtp_device_table[i].product_id) {
522 /* Append this usb device to the MTP device list */
Linus Walleij49557482010-06-22 09:14:47 +0000523 *mtp_device_list = append_to_mtpdevice_list(*mtp_device_list,
524 dev,
Linus Walleij63fd1e62008-04-23 23:49:43 +0000525 bus->location);
Richard Lowe9f205f2007-09-16 16:25:50 +0000526 found = 1;
527 break;
528 }
529 }
Linus Walleij9c6c1542007-09-19 13:43:06 +0000530 // If we didn't know it, try probing the "OS Descriptor".
Richard Lowe9f205f2007-09-16 16:25:50 +0000531 if (!found) {
Richard Lowe9f205f2007-09-16 16:25:50 +0000532 if (probe_device_descriptor(dev, NULL)) {
533 /* Append this usb device to the MTP USB Device List */
Linus Walleij49557482010-06-22 09:14:47 +0000534 *mtp_device_list = append_to_mtpdevice_list(*mtp_device_list,
Linus Walleij63fd1e62008-04-23 23:49:43 +0000535 dev,
536 bus->location);
Richard Lowe9f205f2007-09-16 16:25:50 +0000537 }
Linus Walleijd866d242009-08-23 21:50:39 +0000538 /*
539 * By thomas_-_s: Also append devices that are no MTP but PTP devices
540 * if this is commented out.
541 */
542 /*
543 else {
544 // Check whether the device is no USB hub but a PTP.
545 if ( dev->config != NULL &&dev->config->interface->altsetting->bInterfaceClass == USB_CLASS_PTP && dev->descriptor.bDeviceClass != USB_CLASS_HUB ) {
546 *mtp_device_list = append_to_mtpdevice_list(*mtp_device_list, dev, bus->location);
547 }
548 }
549 */
Richard Lowe9f205f2007-09-16 16:25:50 +0000550 }
Linus Walleij9c6c1542007-09-19 13:43:06 +0000551 }
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000552 }
553 }
Linus Walleij49557482010-06-22 09:14:47 +0000554
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000555 /* If nothing was found we end up here. */
Linus Walleija4942fc2007-03-12 19:23:21 +0000556 if(*mtp_device_list == NULL) {
tedbullock433d2172007-02-23 22:39:12 +0000557 return LIBMTP_ERROR_NO_DEVICE_ATTACHED;
Linus Walleija4942fc2007-03-12 19:23:21 +0000558 }
559 return LIBMTP_ERROR_NONE;
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000560}
561
562/**
Linus Walleij549f49a2010-12-05 14:00:34 +0000563 * Checks if a specific device with a certain bus and device
564 * number has an MTP type device descriptor.
565 *
566 * @param busno the bus number of the device to check
567 * @param deviceno the device number of the device to check
568 * @return 1 if the device is MTP else 0
569 */
570int LIBMTP_Check_Specific_Device(int busno, int devno)
571{
572 struct usb_bus *bus = init_usb();
573 for (; bus != NULL; bus = bus->next) {
574 struct usb_device *dev = bus->devices;
575 if (bus->location != busno)
576 continue;
577
578 for (; dev != NULL; dev = dev->next) {
579
580 if (dev->devnum != devno)
581 continue;
582
583 if (probe_device_descriptor(dev, NULL))
584 return 1;
585 }
586 }
587 return 0;
588}
589
590/**
Linus Walleij63fd1e62008-04-23 23:49:43 +0000591 * Detect the raw MTP device descriptors and return a list of
592 * of the devices found.
Linus Walleij49557482010-06-22 09:14:47 +0000593 *
Linus Walleij63fd1e62008-04-23 23:49:43 +0000594 * @param devices a pointer to a variable that will hold
595 * the list of raw devices found. This may be NULL
596 * on return if the number of detected devices is zero.
597 * The user shall simply <code>free()</code> this
598 * variable when finished with the raw devices,
599 * in order to release memory.
Linus Walleij49557482010-06-22 09:14:47 +0000600 * @param numdevs a pointer to an integer that will hold
Linus Walleij63fd1e62008-04-23 23:49:43 +0000601 * the number of devices in the list. This may
602 * be 0.
603 * @return 0 if successful, any other value means failure.
Linus Walleij1fd2d272006-05-08 09:22:01 +0000604 */
Linus Walleij49557482010-06-22 09:14:47 +0000605LIBMTP_error_number_t LIBMTP_Detect_Raw_Devices(LIBMTP_raw_device_t ** devices,
Linus Walleij63fd1e62008-04-23 23:49:43 +0000606 int * numdevs)
Linus Walleij1fd2d272006-05-08 09:22:01 +0000607{
Linus Walleij63fd1e62008-04-23 23:49:43 +0000608 mtpdevice_list_t *devlist = NULL;
609 mtpdevice_list_t *dev;
610 LIBMTP_error_number_t ret;
Linus Walleij63fd1e62008-04-23 23:49:43 +0000611 LIBMTP_raw_device_t *retdevs;
612 int devs = 0;
Linus Walleijf106aa62008-05-04 22:55:51 +0000613 int i, j;
Linus Walleijb6b69132007-03-07 09:45:05 +0000614
615 ret = get_mtp_usb_device_list(&devlist);
Linus Walleijef2fb362008-05-28 20:59:09 +0000616 if (ret == LIBMTP_ERROR_NO_DEVICE_ATTACHED) {
617 *devices = NULL;
618 *numdevs = 0;
Linus Walleija700d222008-05-28 23:06:14 +0000619 return ret;
Linus Walleijef2fb362008-05-28 20:59:09 +0000620 } else if (ret != LIBMTP_ERROR_NONE) {
nicklas79daadbf22009-09-28 18:19:34 +0000621 LIBMTP_ERROR("LIBMTP PANIC: get_mtp_usb_device_list() "
Linus Walleijf106aa62008-05-04 22:55:51 +0000622 "error code: %d on line %d\n", ret, __LINE__);
Linus Walleija700d222008-05-28 23:06:14 +0000623 return ret;
Linus Walleijb6b69132007-03-07 09:45:05 +0000624 }
Linus Walleijf106aa62008-05-04 22:55:51 +0000625
Linus Walleij63fd1e62008-04-23 23:49:43 +0000626 // Get list size
627 dev = devlist;
628 while (dev != NULL) {
629 devs++;
630 dev = dev->next;
631 }
632 if (devs == 0) {
633 *devices = NULL;
634 *numdevs = 0;
Linus Walleija700d222008-05-28 23:06:14 +0000635 return LIBMTP_ERROR_NONE;
Linus Walleij63fd1e62008-04-23 23:49:43 +0000636 }
637 // Conjure a device list
638 retdevs = (LIBMTP_raw_device_t *) malloc(sizeof(LIBMTP_raw_device_t) * devs);
639 if (retdevs == NULL) {
640 // Out of memory
641 *devices = NULL;
642 *numdevs = 0;
Linus Walleija700d222008-05-28 23:06:14 +0000643 return LIBMTP_ERROR_MEMORY_ALLOCATION;
Linus Walleij63fd1e62008-04-23 23:49:43 +0000644 }
645 dev = devlist;
646 i = 0;
647 while (dev != NULL) {
Linus Walleijf106aa62008-05-04 22:55:51 +0000648 int device_known = 0;
649
Linus Walleij63fd1e62008-04-23 23:49:43 +0000650 // Assign default device info
651 retdevs[i].device_entry.vendor = NULL;
652 retdevs[i].device_entry.vendor_id = dev->libusb_device->descriptor.idVendor;
653 retdevs[i].device_entry.product = NULL;
654 retdevs[i].device_entry.product_id = dev->libusb_device->descriptor.idProduct;
655 retdevs[i].device_entry.device_flags = 0x00000000U;
Linus Walleijfec4d562008-06-01 22:30:36 +0000656 // See if we can locate some additional vendor info and device flags
Linus Walleijf106aa62008-05-04 22:55:51 +0000657 for(j = 0; j < mtp_device_table_size; j++) {
658 if(dev->libusb_device->descriptor.idVendor == mtp_device_table[j].vendor_id &&
659 dev->libusb_device->descriptor.idProduct == mtp_device_table[j].product_id) {
660 device_known = 1;
661 retdevs[i].device_entry.vendor = mtp_device_table[j].vendor;
662 retdevs[i].device_entry.product = mtp_device_table[j].product;
663 retdevs[i].device_entry.device_flags = mtp_device_table[j].device_flags;
nicklas79daadbf22009-09-28 18:19:34 +0000664
Linus Walleijf106aa62008-05-04 22:55:51 +0000665 // This device is known to the developers
Linus Walleij49557482010-06-22 09:14:47 +0000666 LIBMTP_ERROR("Device %d (VID=%04x and PID=%04x) is a %s %s.\n",
Linus Walleijf106aa62008-05-04 22:55:51 +0000667 i,
668 dev->libusb_device->descriptor.idVendor,
669 dev->libusb_device->descriptor.idProduct,
670 mtp_device_table[j].vendor,
671 mtp_device_table[j].product);
Linus Walleijf106aa62008-05-04 22:55:51 +0000672 break;
673 }
674 }
675 if (!device_known) {
676 // This device is unknown to the developers
Linus Walleij49557482010-06-22 09:14:47 +0000677 LIBMTP_ERROR("Device %d (VID=%04x and PID=%04x) is UNKNOWN.\n",
Linus Walleijf106aa62008-05-04 22:55:51 +0000678 i,
679 dev->libusb_device->descriptor.idVendor,
680 dev->libusb_device->descriptor.idProduct);
nicklas79daadbf22009-09-28 18:19:34 +0000681 LIBMTP_ERROR("Please report this VID/PID and the device model to the "
Linus Walleijf106aa62008-05-04 22:55:51 +0000682 "libmtp development team\n");
Linus Walleijfec4d562008-06-01 22:30:36 +0000683 /*
684 * Trying to get iManufacturer or iProduct from the device at this
685 * point would require opening a device handle, that we don't want
686 * to do right now. (Takes time for no good enough reason.)
687 */
Linus Walleijf106aa62008-05-04 22:55:51 +0000688 }
689 // Save the location on the bus
Linus Walleij63fd1e62008-04-23 23:49:43 +0000690 retdevs[i].bus_location = dev->bus_location;
691 retdevs[i].devnum = dev->libusb_device->devnum;
692 i++;
693 dev = dev->next;
Linus Walleij49557482010-06-22 09:14:47 +0000694 }
Linus Walleij63fd1e62008-04-23 23:49:43 +0000695 *devices = retdevs;
696 *numdevs = i;
Linus Walleijb6b69132007-03-07 09:45:05 +0000697 free_mtpdevice_list(devlist);
Linus Walleija700d222008-05-28 23:06:14 +0000698 return LIBMTP_ERROR_NONE;
Linus Walleij1fd2d272006-05-08 09:22:01 +0000699}
700
Linus Walleijc6210fb2006-05-08 11:11:41 +0000701/**
702 * This routine just dumps out low-level
703 * USB information about the current device.
704 * @param ptp_usb the USB device to get information from.
705 */
706void dump_usbinfo(PTP_USB *ptp_usb)
707{
Linus Walleijc6210fb2006-05-08 11:11:41 +0000708 struct usb_device *dev;
709
710#ifdef LIBUSB_HAS_GET_DRIVER_NP
711 char devname[0x10];
Richard Lowe029eba2009-04-11 12:36:25 +0000712 int res;
Linus Walleij49557482010-06-22 09:14:47 +0000713
Linus Walleijc6210fb2006-05-08 11:11:41 +0000714 devname[0] = '\0';
Linus Walleijb0ab5482007-08-29 21:08:54 +0000715 res = usb_get_driver_np(ptp_usb->handle, (int) ptp_usb->interface, devname, sizeof(devname));
Linus Walleijc6210fb2006-05-08 11:11:41 +0000716 if (devname[0] != '\0') {
nicklas79daadbf22009-09-28 18:19:34 +0000717 LIBMTP_INFO(" Using kernel interface \"%s\"\n", devname);
Linus Walleijc6210fb2006-05-08 11:11:41 +0000718 }
719#endif
720 dev = usb_device(ptp_usb->handle);
nicklas79daadbf22009-09-28 18:19:34 +0000721 LIBMTP_INFO(" bcdUSB: %d\n", dev->descriptor.bcdUSB);
722 LIBMTP_INFO(" bDeviceClass: %d\n", dev->descriptor.bDeviceClass);
723 LIBMTP_INFO(" bDeviceSubClass: %d\n", dev->descriptor.bDeviceSubClass);
724 LIBMTP_INFO(" bDeviceProtocol: %d\n", dev->descriptor.bDeviceProtocol);
725 LIBMTP_INFO(" idVendor: %04x\n", dev->descriptor.idVendor);
726 LIBMTP_INFO(" idProduct: %04x\n", dev->descriptor.idProduct);
727 LIBMTP_INFO(" IN endpoint maxpacket: %d bytes\n", ptp_usb->inep_maxpacket);
728 LIBMTP_INFO(" OUT endpoint maxpacket: %d bytes\n", ptp_usb->outep_maxpacket);
729 LIBMTP_INFO(" Raw device info:\n");
730 LIBMTP_INFO(" Bus location: %d\n", ptp_usb->rawdevice.bus_location);
731 LIBMTP_INFO(" Device number: %d\n", ptp_usb->rawdevice.devnum);
732 LIBMTP_INFO(" Device entry info:\n");
733 LIBMTP_INFO(" Vendor: %s\n", ptp_usb->rawdevice.device_entry.vendor);
734 LIBMTP_INFO(" Vendor id: 0x%04x\n", ptp_usb->rawdevice.device_entry.vendor_id);
735 LIBMTP_INFO(" Product: %s\n", ptp_usb->rawdevice.device_entry.product);
736 LIBMTP_INFO(" Vendor id: 0x%04x\n", ptp_usb->rawdevice.device_entry.product_id);
737 LIBMTP_INFO(" Device flags: 0x%08x\n", ptp_usb->rawdevice.device_entry.device_flags);
Linus Walleij9521e2b2007-07-10 21:50:42 +0000738 (void) probe_device_descriptor(dev, stdout);
Linus Walleijc6210fb2006-05-08 11:11:41 +0000739}
740
Linus Walleija4e6bdc2008-05-23 22:31:53 +0000741/**
742 * Retrieve the apropriate playlist extension for this
743 * device. Rather hacky at the moment. This is probably
744 * desired by the managing software, but when creating
745 * lists on the device itself you notice certain preferences.
746 * @param ptp_usb the USB device to get suggestion for.
747 * @return the suggested playlist extension.
748 */
Richard Lowe029eba2009-04-11 12:36:25 +0000749const char *get_playlist_extension(PTP_USB *ptp_usb)
Linus Walleija4e6bdc2008-05-23 22:31:53 +0000750{
751 struct usb_device *dev;
752 static char creative_pl_extension[] = ".zpl";
753 static char default_pl_extension[] = ".pla";
754
755 dev = usb_device(ptp_usb->handle);
756 if (dev->descriptor.idVendor == 0x041e) {
757 return creative_pl_extension;
758 }
759 return default_pl_extension;
760}
761
Linus Walleijd1e14e02008-12-14 01:07:30 +0000762static void
763libusb_glue_debug (PTPParams *params, const char *format, ...)
Linus Walleij14735952010-04-25 04:56:49 +0000764{
Linus Walleija0323272007-01-07 12:21:30 +0000765 va_list args;
766
767 va_start (args, format);
768 if (params->debug_func!=NULL)
769 params->debug_func (params->data, format, args);
770 else
771 {
772 vfprintf (stderr, format, args);
773 fprintf (stderr,"\n");
774 fflush (stderr);
775 }
776 va_end (args);
Linus Walleij14735952010-04-25 04:56:49 +0000777}
Linus Walleija0323272007-01-07 12:21:30 +0000778
779static void
Linus Walleijd1e14e02008-12-14 01:07:30 +0000780libusb_glue_error (PTPParams *params, const char *format, ...)
Linus Walleij49557482010-06-22 09:14:47 +0000781{
Linus Walleija0323272007-01-07 12:21:30 +0000782 va_list args;
783
784 va_start (args, format);
785 if (params->error_func!=NULL)
786 params->error_func (params->data, format, args);
787 else
788 {
789 vfprintf (stderr, format, args);
790 fprintf (stderr,"\n");
791 fflush (stderr);
792 }
793 va_end (args);
794}
795
Linus Walleijc6210fb2006-05-08 11:11:41 +0000796
Linus Walleij99ed83c2007-01-02 18:46:02 +0000797/*
798 * ptp_read_func() and ptp_write_func() are
Linus Walleijc0a11432007-03-02 21:21:18 +0000799 * based on same functions usb.c in libgphoto2.
Linus Walleij99ed83c2007-01-02 18:46:02 +0000800 * Much reading packet logs and having fun with trials and errors
801 * reveals that WMP / Windows is probably using an algorithm like this
802 * for large transfers:
803 *
Linus Walleij49557482010-06-22 09:14:47 +0000804 * 1. Send the command (0x0c bytes) if headers are split, else, send
Linus Walleij99ed83c2007-01-02 18:46:02 +0000805 * command plus sizeof(endpoint) - 0x0c bytes.
806 * 2. Send first packet, max size to be sizeof(endpoint) but only when using
807 * split headers. Else goto 3.
808 * 3. REPEAT send 0x10000 byte chunks UNTIL remaining bytes < 0x10000
809 * We call 0x10000 CONTEXT_BLOCK_SIZE.
810 * 4. Send remaining bytes MOD sizeof(endpoint)
811 * 5. Send remaining bytes. If this happens to be exactly sizeof(endpoint)
812 * then also send a zero-length package.
Linus Walleij049f50c2007-03-03 20:30:43 +0000813 *
814 * Further there is some special quirks to handle zero reads from the
815 * device, since some devices can't do them at all due to shortcomings
816 * of the USB slave controller in the device.
Linus Walleij99ed83c2007-01-02 18:46:02 +0000817 */
Richard Low4fd59d62007-03-03 16:34:37 +0000818#define CONTEXT_BLOCK_SIZE_1 0x3e00
819#define CONTEXT_BLOCK_SIZE_2 0x200
820#define CONTEXT_BLOCK_SIZE CONTEXT_BLOCK_SIZE_1+CONTEXT_BLOCK_SIZE_2
Linus Walleijb02a0662006-04-25 08:05:09 +0000821static short
Linus Walleij784ac3f2006-12-29 10:36:51 +0000822ptp_read_func (
823 unsigned long size, PTPDataHandler *handler,void *data,
Richard Low4df32f82007-01-11 20:04:39 +0000824 unsigned long *readbytes,
825 int readzero
Linus Walleij784ac3f2006-12-29 10:36:51 +0000826) {
Linus Walleijb02a0662006-04-25 08:05:09 +0000827 PTP_USB *ptp_usb = (PTP_USB *)data;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000828 unsigned long toread = 0;
Linus Walleijd6a49972006-05-02 08:24:54 +0000829 int result = 0;
Linus Walleij99ed83c2007-01-02 18:46:02 +0000830 unsigned long curread = 0;
831 unsigned long written;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000832 unsigned char *bytes;
Linus Walleij049f50c2007-03-03 20:30:43 +0000833 int expect_terminator_byte = 0;
Linus Walleij99ed83c2007-01-02 18:46:02 +0000834
835 // This is the largest block we'll need to read in.
Linus Walleij784ac3f2006-12-29 10:36:51 +0000836 bytes = malloc(CONTEXT_BLOCK_SIZE);
Linus Walleijd6a49972006-05-02 08:24:54 +0000837 while (curread < size) {
nicklas79daadbf22009-09-28 18:19:34 +0000838
839 LIBMTP_USB_DEBUG("Remaining size to read: 0x%04lx bytes\n", size - curread);
840
Richard Low4fd59d62007-03-03 16:34:37 +0000841 // check equal to condition here
842 if (size - curread < CONTEXT_BLOCK_SIZE)
843 {
844 // this is the last packet
845 toread = size - curread;
846 // this is equivalent to zero read for these devices
Linus Walleijfec4d562008-06-01 22:30:36 +0000847 if (readzero && FLAG_NO_ZERO_READS(ptp_usb) && toread % 64 == 0) {
Richard Low4fd59d62007-03-03 16:34:37 +0000848 toread += 1;
Linus Walleij049f50c2007-03-03 20:30:43 +0000849 expect_terminator_byte = 1;
Richard Low4fd59d62007-03-03 16:34:37 +0000850 }
Linus Walleijc49c6372007-01-09 00:18:51 +0000851 }
Richard Low4fd59d62007-03-03 16:34:37 +0000852 else if (curread == 0)
853 // we are first packet, but not last packet
854 toread = CONTEXT_BLOCK_SIZE_1;
855 else if (toread == CONTEXT_BLOCK_SIZE_1)
856 toread = CONTEXT_BLOCK_SIZE_2;
857 else if (toread == CONTEXT_BLOCK_SIZE_2)
858 toread = CONTEXT_BLOCK_SIZE_1;
859 else
nicklas79daadbf22009-09-28 18:19:34 +0000860 LIBMTP_INFO("unexpected toread size 0x%04x, 0x%04x remaining bytes\n",
Linus Walleij4cec9872007-03-03 21:00:53 +0000861 (unsigned int) toread, (unsigned int) (size-curread));
Linus Walleij784ac3f2006-12-29 10:36:51 +0000862
nicklas79daadbf22009-09-28 18:19:34 +0000863 LIBMTP_USB_DEBUG("Reading in 0x%04lx bytes\n", toread);
864
Linus Walleije04a1b92011-03-09 18:00:24 +0100865 result = USB_BULK_READ(ptp_usb->handle,
866 ptp_usb->inep,
867 (char*) bytes,
868 toread,
869 ptp_usb->timeout);
nicklas79daadbf22009-09-28 18:19:34 +0000870
871 LIBMTP_USB_DEBUG("Result of read: 0x%04x\n", result);
Linus Walleij49557482010-06-22 09:14:47 +0000872
Linus Walleijc49c6372007-01-09 00:18:51 +0000873 if (result < 0) {
Richard Low43fdb882006-09-06 16:19:05 +0000874 return PTP_ERROR_IO;
Linus Walleijc49c6372007-01-09 00:18:51 +0000875 }
nicklas79daadbf22009-09-28 18:19:34 +0000876
877 LIBMTP_USB_DEBUG("<==USB IN\n");
Richard Low4df32f82007-01-11 20:04:39 +0000878 if (result == 0)
nicklas79daadbf22009-09-28 18:19:34 +0000879 LIBMTP_USB_DEBUG("Zero Read\n");
Richard Low4df32f82007-01-11 20:04:39 +0000880 else
nicklas79daadbf22009-09-28 18:19:34 +0000881 LIBMTP_USB_DATA(bytes, result, 16);
Linus Walleij49557482010-06-22 09:14:47 +0000882
Richard Low4fd59d62007-03-03 16:34:37 +0000883 // want to discard extra byte
Linus Walleij049f50c2007-03-03 20:30:43 +0000884 if (expect_terminator_byte && result == toread)
Richard Lowbf2675c2007-03-18 18:20:26 +0000885 {
nicklas79daadbf22009-09-28 18:19:34 +0000886 LIBMTP_USB_DEBUG("<==USB IN\nDiscarding extra byte\n");
887
Richard Low4fd59d62007-03-03 16:34:37 +0000888 result--;
Richard Lowbf2675c2007-03-18 18:20:26 +0000889 }
Linus Walleij49557482010-06-22 09:14:47 +0000890
Linus Walleijd866d242009-08-23 21:50:39 +0000891 int putfunc_ret = handler->putfunc(NULL, handler->priv, result, bytes, &written);
Richard Lowd24f9232009-05-03 10:22:08 +0000892 if (putfunc_ret != PTP_RC_OK)
893 return putfunc_ret;
Linus Walleij49557482010-06-22 09:14:47 +0000894
Linus Walleijc49c6372007-01-09 00:18:51 +0000895 ptp_usb->current_transfer_complete += result;
Linus Walleijd6a49972006-05-02 08:24:54 +0000896 curread += result;
Linus Walleijc49c6372007-01-09 00:18:51 +0000897
898 // Increase counters, call callback
899 if (ptp_usb->callback_active) {
900 if (ptp_usb->current_transfer_complete >= ptp_usb->current_transfer_total) {
901 // send last update and disable callback.
902 ptp_usb->current_transfer_complete = ptp_usb->current_transfer_total;
903 ptp_usb->callback_active = 0;
904 }
905 if (ptp_usb->current_transfer_callback != NULL) {
Linus Walleijbd3bf9e2007-09-14 19:31:54 +0000906 int ret;
907 ret = ptp_usb->current_transfer_callback(ptp_usb->current_transfer_complete,
908 ptp_usb->current_transfer_total,
909 ptp_usb->current_transfer_callback_data);
910 if (ret != 0) {
911 return PTP_ERROR_CANCEL;
912 }
Linus Walleijc49c6372007-01-09 00:18:51 +0000913 }
Linus Walleij49557482010-06-22 09:14:47 +0000914 }
Linus Walleijc49c6372007-01-09 00:18:51 +0000915
Linus Walleijd6a49972006-05-02 08:24:54 +0000916 if (result < toread) /* short reads are common */
917 break;
918 }
Linus Walleij784ac3f2006-12-29 10:36:51 +0000919 if (readbytes) *readbytes = curread;
920 free (bytes);
Linus Walleij49557482010-06-22 09:14:47 +0000921
Richard Low4df32f82007-01-11 20:04:39 +0000922 // there might be a zero packet waiting for us...
Linus Walleij49557482010-06-22 09:14:47 +0000923 if (readzero &&
924 !FLAG_NO_ZERO_READS(ptp_usb) &&
Linus Walleij049f50c2007-03-03 20:30:43 +0000925 curread % ptp_usb->outep_maxpacket == 0) {
Richard Low4df32f82007-01-11 20:04:39 +0000926 char temp;
927 int zeroresult = 0;
Linus Walleij049f50c2007-03-03 20:30:43 +0000928
nicklas79daadbf22009-09-28 18:19:34 +0000929 LIBMTP_USB_DEBUG("<==USB IN\n");
930 LIBMTP_USB_DEBUG("Zero Read\n");
931
Linus Walleije04a1b92011-03-09 18:00:24 +0100932 zeroresult = USB_BULK_READ(ptp_usb->handle,
933 ptp_usb->inep,
934 &temp,
935 0,
936 ptp_usb->timeout);
Richard Low4df32f82007-01-11 20:04:39 +0000937 if (zeroresult != 0)
nicklas79daadbf22009-09-28 18:19:34 +0000938 LIBMTP_INFO("LIBMTP panic: unable to read in zero packet, response 0x%04x", zeroresult);
Richard Low4df32f82007-01-11 20:04:39 +0000939 }
Linus Walleij49557482010-06-22 09:14:47 +0000940
Richard Low4d9165f2008-09-23 20:13:17 +0000941 return PTP_RC_OK;
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000942}
943
Linus Walleijb02a0662006-04-25 08:05:09 +0000944static short
Linus Walleij784ac3f2006-12-29 10:36:51 +0000945ptp_write_func (
946 unsigned long size,
947 PTPDataHandler *handler,
948 void *data,
949 unsigned long *written
950) {
Linus Walleijb02a0662006-04-25 08:05:09 +0000951 PTP_USB *ptp_usb = (PTP_USB *)data;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000952 unsigned long towrite = 0;
Linus Walleijd6a49972006-05-02 08:24:54 +0000953 int result = 0;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000954 unsigned long curwrite = 0;
955 unsigned char *bytes;
Linus Walleij99ed83c2007-01-02 18:46:02 +0000956
Linus Walleij49557482010-06-22 09:14:47 +0000957 // This is the largest block we'll need to read in.
Linus Walleij784ac3f2006-12-29 10:36:51 +0000958 bytes = malloc(CONTEXT_BLOCK_SIZE);
Linus Walleijc49c6372007-01-09 00:18:51 +0000959 if (!bytes) {
960 return PTP_ERROR_IO;
961 }
Linus Walleijd6a49972006-05-02 08:24:54 +0000962 while (curwrite < size) {
Linus Walleijb84b3852009-03-04 15:11:08 +0000963 unsigned long usbwritten = 0;
Linus Walleijd6a49972006-05-02 08:24:54 +0000964 towrite = size-curwrite;
Linus Walleijc49c6372007-01-09 00:18:51 +0000965 if (towrite > CONTEXT_BLOCK_SIZE) {
Richard Low43fdb882006-09-06 16:19:05 +0000966 towrite = CONTEXT_BLOCK_SIZE;
Linus Walleijc49c6372007-01-09 00:18:51 +0000967 } else {
968 // This magic makes packets the same size that WMP send them.
969 if (towrite > ptp_usb->outep_maxpacket && towrite % ptp_usb->outep_maxpacket != 0) {
Richard Low021421e2007-01-01 18:08:57 +0000970 towrite -= towrite % ptp_usb->outep_maxpacket;
Linus Walleijc49c6372007-01-09 00:18:51 +0000971 }
972 }
Linus Walleijd866d242009-08-23 21:50:39 +0000973 int getfunc_ret = handler->getfunc(NULL, handler->priv,towrite,bytes,&towrite);
Richard Lowd24f9232009-05-03 10:22:08 +0000974 if (getfunc_ret != PTP_RC_OK)
975 return getfunc_ret;
Linus Walleijb84b3852009-03-04 15:11:08 +0000976 while (usbwritten < towrite) {
Linus Walleije04a1b92011-03-09 18:00:24 +0100977 result = USB_BULK_WRITE(ptp_usb->handle,
978 ptp_usb->outep,
979 ((char*) bytes+usbwritten),
980 towrite-usbwritten,
981 ptp_usb->timeout);
nicklas79daadbf22009-09-28 18:19:34 +0000982
983 LIBMTP_USB_DEBUG("USB OUT==>\n");
984 LIBMTP_USB_DATA(bytes+usbwritten, result, 16);
985
Linus Walleijb84b3852009-03-04 15:11:08 +0000986 if (result < 0) {
987 return PTP_ERROR_IO;
988 }
989 // check for result == 0 perhaps too.
990 // Increase counters
991 ptp_usb->current_transfer_complete += result;
992 curwrite += result;
993 usbwritten += result;
Linus Walleijc49c6372007-01-09 00:18:51 +0000994 }
Linus Walleijc49c6372007-01-09 00:18:51 +0000995 // call callback
996 if (ptp_usb->callback_active) {
997 if (ptp_usb->current_transfer_complete >= ptp_usb->current_transfer_total) {
998 // send last update and disable callback.
999 ptp_usb->current_transfer_complete = ptp_usb->current_transfer_total;
1000 ptp_usb->callback_active = 0;
1001 }
1002 if (ptp_usb->current_transfer_callback != NULL) {
Linus Walleijbd3bf9e2007-09-14 19:31:54 +00001003 int ret;
1004 ret = ptp_usb->current_transfer_callback(ptp_usb->current_transfer_complete,
1005 ptp_usb->current_transfer_total,
1006 ptp_usb->current_transfer_callback_data);
1007 if (ret != 0) {
1008 return PTP_ERROR_CANCEL;
1009 }
Linus Walleijc49c6372007-01-09 00:18:51 +00001010 }
1011 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001012 if (result < towrite) /* short writes happen */
1013 break;
1014 }
Linus Walleij784ac3f2006-12-29 10:36:51 +00001015 free (bytes);
Linus Walleijc49c6372007-01-09 00:18:51 +00001016 if (written) {
1017 *written = curwrite;
Linus Walleij80d134a2006-08-22 21:41:37 +00001018 }
Linus Walleijc49c6372007-01-09 00:18:51 +00001019
Richard Lowef05b892007-01-03 21:18:56 +00001020 // If this is the last transfer send a zero write if required
1021 if (ptp_usb->current_transfer_complete >= ptp_usb->current_transfer_total) {
Richard Low68f45882007-01-03 10:08:31 +00001022 if ((towrite % ptp_usb->outep_maxpacket) == 0) {
nicklas79daadbf22009-09-28 18:19:34 +00001023
1024 LIBMTP_USB_DEBUG("USB OUT==>\n");
1025 LIBMTP_USB_DEBUG("Zero Write\n");
1026
Linus Walleije04a1b92011-03-09 18:00:24 +01001027 result=USB_BULK_WRITE(ptp_usb->handle,
1028 ptp_usb->outep,
1029 (char *) "x",
1030 0,
1031 ptp_usb->timeout);
Linus Walleij7f0c72e2006-09-06 13:01:58 +00001032 }
Linus Walleijd214b9b2006-08-26 22:08:37 +00001033 }
Linus Walleij49557482010-06-22 09:14:47 +00001034
Linus Walleijd6a49972006-05-02 08:24:54 +00001035 if (result < 0)
1036 return PTP_ERROR_IO;
1037 return PTP_RC_OK;
Linus Walleijb02a0662006-04-25 08:05:09 +00001038}
1039
Linus Walleija0323272007-01-07 12:21:30 +00001040/* memory data get/put handler */
1041typedef struct {
1042 unsigned char *data;
1043 unsigned long size, curoff;
1044} PTPMemHandlerPrivate;
Linus Walleij784ac3f2006-12-29 10:36:51 +00001045
Linus Walleija0323272007-01-07 12:21:30 +00001046static uint16_t
1047memory_getfunc(PTPParams* params, void* private,
1048 unsigned long wantlen, unsigned char *data,
1049 unsigned long *gotlen
1050) {
1051 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)private;
1052 unsigned long tocopy = wantlen;
1053
1054 if (priv->curoff + tocopy > priv->size)
1055 tocopy = priv->size - priv->curoff;
1056 memcpy (data, priv->data + priv->curoff, tocopy);
1057 priv->curoff += tocopy;
1058 *gotlen = tocopy;
1059 return PTP_RC_OK;
Linus Walleijb02a0662006-04-25 08:05:09 +00001060}
1061
Linus Walleija0323272007-01-07 12:21:30 +00001062static uint16_t
1063memory_putfunc(PTPParams* params, void* private,
1064 unsigned long sendlen, unsigned char *data,
1065 unsigned long *putlen
1066) {
1067 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)private;
1068
1069 if (priv->curoff + sendlen > priv->size) {
1070 priv->data = realloc (priv->data, priv->curoff+sendlen);
1071 priv->size = priv->curoff + sendlen;
1072 }
1073 memcpy (priv->data + priv->curoff, data, sendlen);
1074 priv->curoff += sendlen;
1075 *putlen = sendlen;
1076 return PTP_RC_OK;
1077}
1078
1079/* init private struct for receiving data. */
1080static uint16_t
1081ptp_init_recv_memory_handler(PTPDataHandler *handler) {
1082 PTPMemHandlerPrivate* priv;
1083 priv = malloc (sizeof(PTPMemHandlerPrivate));
Linus Walleijd866d242009-08-23 21:50:39 +00001084 handler->priv = priv;
Linus Walleija0323272007-01-07 12:21:30 +00001085 handler->getfunc = memory_getfunc;
1086 handler->putfunc = memory_putfunc;
1087 priv->data = NULL;
1088 priv->size = 0;
1089 priv->curoff = 0;
1090 return PTP_RC_OK;
1091}
1092
1093/* init private struct and put data in for sending data.
1094 * data is still owned by caller.
1095 */
1096static uint16_t
1097ptp_init_send_memory_handler(PTPDataHandler *handler,
1098 unsigned char *data, unsigned long len
1099) {
1100 PTPMemHandlerPrivate* priv;
1101 priv = malloc (sizeof(PTPMemHandlerPrivate));
1102 if (!priv)
1103 return PTP_RC_GeneralError;
Linus Walleijd866d242009-08-23 21:50:39 +00001104 handler->priv = priv;
Linus Walleija0323272007-01-07 12:21:30 +00001105 handler->getfunc = memory_getfunc;
1106 handler->putfunc = memory_putfunc;
1107 priv->data = data;
1108 priv->size = len;
1109 priv->curoff = 0;
1110 return PTP_RC_OK;
1111}
1112
1113/* free private struct + data */
1114static uint16_t
1115ptp_exit_send_memory_handler (PTPDataHandler *handler) {
Linus Walleijd866d242009-08-23 21:50:39 +00001116 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)handler->priv;
Linus Walleija0323272007-01-07 12:21:30 +00001117 /* data is owned by caller */
1118 free (priv);
1119 return PTP_RC_OK;
1120}
1121
1122/* hand over our internal data to caller */
1123static uint16_t
1124ptp_exit_recv_memory_handler (PTPDataHandler *handler,
1125 unsigned char **data, unsigned long *size
1126) {
Linus Walleijd866d242009-08-23 21:50:39 +00001127 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)handler->priv;
Linus Walleija0323272007-01-07 12:21:30 +00001128 *data = priv->data;
1129 *size = priv->size;
1130 free (priv);
1131 return PTP_RC_OK;
1132}
1133
1134/* send / receive functions */
1135
1136uint16_t
1137ptp_usb_sendreq (PTPParams* params, PTPContainer* req)
1138{
1139 uint16_t ret;
1140 PTPUSBBulkContainer usbreq;
1141 PTPDataHandler memhandler;
Linus Walleij5ba40402008-05-23 21:36:54 +00001142 unsigned long written = 0;
1143 unsigned long towrite;
nicklas79daadbf22009-09-28 18:19:34 +00001144
Linus Walleijb8f78c72008-03-02 20:22:56 +00001145 char txt[256];
Linus Walleija0323272007-01-07 12:21:30 +00001146
Linus Walleijb8f78c72008-03-02 20:22:56 +00001147 (void) ptp_render_opcode (params, req->Code, sizeof(txt), txt);
nicklas79daadbf22009-09-28 18:19:34 +00001148 LIBMTP_USB_DEBUG("REQUEST: 0x%04x, %s\n", req->Code, txt);
1149
Linus Walleija0323272007-01-07 12:21:30 +00001150 /* build appropriate USB container */
1151 usbreq.length=htod32(PTP_USB_BULK_REQ_LEN-
1152 (sizeof(uint32_t)*(5-req->Nparam)));
1153 usbreq.type=htod16(PTP_USB_CONTAINER_COMMAND);
1154 usbreq.code=htod16(req->Code);
1155 usbreq.trans_id=htod32(req->Transaction_ID);
1156 usbreq.payload.params.param1=htod32(req->Param1);
1157 usbreq.payload.params.param2=htod32(req->Param2);
1158 usbreq.payload.params.param3=htod32(req->Param3);
1159 usbreq.payload.params.param4=htod32(req->Param4);
1160 usbreq.payload.params.param5=htod32(req->Param5);
1161 /* send it to responder */
1162 towrite = PTP_USB_BULK_REQ_LEN-(sizeof(uint32_t)*(5-req->Nparam));
1163 ptp_init_send_memory_handler (&memhandler, (unsigned char*)&usbreq, towrite);
1164 ret=ptp_write_func(
1165 towrite,
1166 &memhandler,
1167 params->data,
1168 &written
1169 );
1170 ptp_exit_send_memory_handler (&memhandler);
Linus Walleijff01cb12007-09-16 19:49:48 +00001171 if (ret!=PTP_RC_OK && ret!=PTP_ERROR_CANCEL) {
Linus Walleija0323272007-01-07 12:21:30 +00001172 ret = PTP_ERROR_IO;
Linus Walleija0323272007-01-07 12:21:30 +00001173 }
Linus Walleij724f0102007-09-17 20:10:12 +00001174 if (written != towrite && ret != PTP_ERROR_CANCEL && ret != PTP_ERROR_IO) {
Linus Walleij49557482010-06-22 09:14:47 +00001175 libusb_glue_error (params,
Linus Walleija0323272007-01-07 12:21:30 +00001176 "PTP: request code 0x%04x sending req wrote only %ld bytes instead of %d",
Richard Lowd284f072007-02-17 08:52:41 +00001177 req->Code, written, towrite
Linus Walleija0323272007-01-07 12:21:30 +00001178 );
1179 ret = PTP_ERROR_IO;
1180 }
1181 return ret;
1182}
1183
1184uint16_t
1185ptp_usb_senddata (PTPParams* params, PTPContainer* ptp,
1186 unsigned long size, PTPDataHandler *handler
1187) {
1188 uint16_t ret;
1189 int wlen, datawlen;
1190 unsigned long written;
1191 PTPUSBBulkContainer usbdata;
1192 uint32_t bytes_left_to_transfer;
1193 PTPDataHandler memhandler;
1194
nicklas79daadbf22009-09-28 18:19:34 +00001195
1196 LIBMTP_USB_DEBUG("SEND DATA PHASE\n");
1197
Linus Walleija0323272007-01-07 12:21:30 +00001198 /* build appropriate USB container */
1199 usbdata.length = htod32(PTP_USB_BULK_HDR_LEN+size);
1200 usbdata.type = htod16(PTP_USB_CONTAINER_DATA);
1201 usbdata.code = htod16(ptp->Code);
1202 usbdata.trans_id= htod32(ptp->Transaction_ID);
Linus Walleij14735952010-04-25 04:56:49 +00001203
Linus Walleija0323272007-01-07 12:21:30 +00001204 ((PTP_USB*)params->data)->current_transfer_complete = 0;
Richard Low4eb01122007-02-24 10:11:32 +00001205 ((PTP_USB*)params->data)->current_transfer_total = size+PTP_USB_BULK_HDR_LEN;
Linus Walleija0323272007-01-07 12:21:30 +00001206
1207 if (params->split_header_data) {
1208 datawlen = 0;
1209 wlen = PTP_USB_BULK_HDR_LEN;
1210 } else {
1211 unsigned long gotlen;
1212 /* For all camera devices. */
Richard Low02724f62007-02-17 09:47:26 +00001213 datawlen = (size<PTP_USB_BULK_PAYLOAD_LEN_WRITE)?size:PTP_USB_BULK_PAYLOAD_LEN_WRITE;
Linus Walleija0323272007-01-07 12:21:30 +00001214 wlen = PTP_USB_BULK_HDR_LEN + datawlen;
Linus Walleij14735952010-04-25 04:56:49 +00001215
Linus Walleijd866d242009-08-23 21:50:39 +00001216 ret = handler->getfunc(params, handler->priv, datawlen, usbdata.payload.data, &gotlen);
Linus Walleija0323272007-01-07 12:21:30 +00001217 if (ret != PTP_RC_OK)
1218 return ret;
1219 if (gotlen != datawlen)
1220 return PTP_RC_GeneralError;
1221 }
1222 ptp_init_send_memory_handler (&memhandler, (unsigned char *)&usbdata, wlen);
1223 /* send first part of data */
1224 ret = ptp_write_func(wlen, &memhandler, params->data, &written);
1225 ptp_exit_send_memory_handler (&memhandler);
1226 if (ret!=PTP_RC_OK) {
Linus Walleija0323272007-01-07 12:21:30 +00001227 return ret;
1228 }
1229 if (size <= datawlen) return ret;
1230 /* if everything OK send the rest */
1231 bytes_left_to_transfer = size-datawlen;
1232 ret = PTP_RC_OK;
1233 while(bytes_left_to_transfer > 0) {
1234 ret = ptp_write_func (bytes_left_to_transfer, handler, params->data, &written);
1235 if (ret != PTP_RC_OK)
1236 break;
1237 if (written == 0) {
1238 ret = PTP_ERROR_IO;
1239 break;
1240 }
1241 bytes_left_to_transfer -= written;
1242 }
Linus Walleijff01cb12007-09-16 19:49:48 +00001243 if (ret!=PTP_RC_OK && ret!=PTP_ERROR_CANCEL)
Linus Walleija0323272007-01-07 12:21:30 +00001244 ret = PTP_ERROR_IO;
1245 return ret;
1246}
1247
1248static uint16_t ptp_usb_getpacket(PTPParams *params,
1249 PTPUSBBulkContainer *packet, unsigned long *rlen)
1250{
1251 PTPDataHandler memhandler;
1252 uint16_t ret;
1253 unsigned char *x = NULL;
1254
1255 /* read the header and potentially the first data */
1256 if (params->response_packet_size > 0) {
1257 /* If there is a buffered packet, just use it. */
1258 memcpy(packet, params->response_packet, params->response_packet_size);
1259 *rlen = params->response_packet_size;
1260 free(params->response_packet);
1261 params->response_packet = NULL;
1262 params->response_packet_size = 0;
1263 /* Here this signifies a "virtual read" */
1264 return PTP_RC_OK;
1265 }
1266 ptp_init_recv_memory_handler (&memhandler);
Richard Low02724f62007-02-17 09:47:26 +00001267 ret = ptp_read_func(PTP_USB_BULK_HS_MAX_PACKET_LEN_READ, &memhandler, params->data, rlen, 0);
Linus Walleija0323272007-01-07 12:21:30 +00001268 ptp_exit_recv_memory_handler (&memhandler, &x, rlen);
1269 if (x) {
1270 memcpy (packet, x, *rlen);
1271 free (x);
1272 }
1273 return ret;
1274}
1275
1276uint16_t
1277ptp_usb_getdata (PTPParams* params, PTPContainer* ptp, PTPDataHandler *handler)
1278{
1279 uint16_t ret;
1280 PTPUSBBulkContainer usbdata;
Linus Walleija0323272007-01-07 12:21:30 +00001281 unsigned long written;
Linus Walleij8e3af002007-09-28 19:21:54 +00001282 PTP_USB *ptp_usb = (PTP_USB *) params->data;
Linus Walleij14735952010-04-25 04:56:49 +00001283 int putfunc_ret;
nicklas79daadbf22009-09-28 18:19:34 +00001284
1285 LIBMTP_USB_DEBUG("GET DATA PHASE\n");
1286
Linus Walleija0323272007-01-07 12:21:30 +00001287 memset(&usbdata,0,sizeof(usbdata));
1288 do {
1289 unsigned long len, rlen;
1290
1291 ret = ptp_usb_getpacket(params, &usbdata, &rlen);
1292 if (ret!=PTP_RC_OK) {
1293 ret = PTP_ERROR_IO;
1294 break;
Linus Walleij8e3af002007-09-28 19:21:54 +00001295 }
Linus Walleija0323272007-01-07 12:21:30 +00001296 if (dtoh16(usbdata.type)!=PTP_USB_CONTAINER_DATA) {
1297 ret = PTP_ERROR_DATA_EXPECTED;
1298 break;
Linus Walleij8e3af002007-09-28 19:21:54 +00001299 }
Linus Walleija0323272007-01-07 12:21:30 +00001300 if (dtoh16(usbdata.code)!=ptp->Code) {
Linus Walleijfec4d562008-06-01 22:30:36 +00001301 if (FLAG_IGNORE_HEADER_ERRORS(ptp_usb)) {
Linus Walleijd1e14e02008-12-14 01:07:30 +00001302 libusb_glue_debug (params, "ptp2/ptp_usb_getdata: detected a broken "
Linus Walleij913a3062007-09-28 21:42:39 +00001303 "PTP header, code field insane, expect problems! (But continuing)");
Linus Walleij06542122007-10-14 22:13:48 +00001304 // Repair the header, so it won't wreak more havoc, don't just ignore it.
1305 // Typically these two fields will be broken.
1306 usbdata.code = htod16(ptp->Code);
1307 usbdata.trans_id = htod32(ptp->Transaction_ID);
Linus Walleij94cd9722007-09-28 21:29:53 +00001308 ret = PTP_RC_OK;
Linus Walleij8e3af002007-09-28 19:21:54 +00001309 } else {
Linus Walleij913a3062007-09-28 21:42:39 +00001310 ret = dtoh16(usbdata.code);
1311 // This filters entirely insane garbage return codes, but still
1312 // makes it possible to return error codes in the code field when
Linus Walleij49557482010-06-22 09:14:47 +00001313 // getting data. It appears Windows ignores the contents of this
Linus Walleij913a3062007-09-28 21:42:39 +00001314 // field entirely.
1315 if (ret < PTP_RC_Undefined || ret > PTP_RC_SpecificationOfDestinationUnsupported) {
Linus Walleijd1e14e02008-12-14 01:07:30 +00001316 libusb_glue_debug (params, "ptp2/ptp_usb_getdata: detected a broken "
Linus Walleij913a3062007-09-28 21:42:39 +00001317 "PTP header, code field insane.");
1318 ret = PTP_ERROR_IO;
1319 }
Linus Walleij8e3af002007-09-28 19:21:54 +00001320 break;
1321 }
Linus Walleija0323272007-01-07 12:21:30 +00001322 }
1323 if (usbdata.length == 0xffffffffU) {
Linus Walleij14735952010-04-25 04:56:49 +00001324 /* Copy first part of data to 'data' */
1325 putfunc_ret =
1326 handler->putfunc(
1327 params, handler->priv, rlen - PTP_USB_BULK_HDR_LEN, usbdata.payload.data,
1328 &written
1329 );
1330 if (putfunc_ret != PTP_RC_OK)
1331 return putfunc_ret;
Linus Walleija0323272007-01-07 12:21:30 +00001332
Linus Walleij14735952010-04-25 04:56:49 +00001333 /* stuff data directly to passed data handler */
1334 while (1) {
1335 unsigned long readdata;
1336 uint16_t xret;
1337
1338 xret = ptp_read_func(
1339 PTP_USB_BULK_HS_MAX_PACKET_LEN_READ,
1340 handler,
1341 params->data,
1342 &readdata,
1343 0
1344 );
1345 if (xret != PTP_RC_OK)
1346 return xret;
1347 if (readdata < PTP_USB_BULK_HS_MAX_PACKET_LEN_READ)
1348 break;
1349 }
1350 return PTP_RC_OK;
Linus Walleija0323272007-01-07 12:21:30 +00001351 }
1352 if (rlen > dtoh32(usbdata.length)) {
1353 /*
1354 * Buffer the surplus response packet if it is >=
1355 * PTP_USB_BULK_HDR_LEN
1356 * (i.e. it is probably an entire package)
1357 * else discard it as erroneous surplus data.
1358 * This will even work if more than 2 packets appear
1359 * in the same transaction, they will just be handled
1360 * iteratively.
1361 *
1362 * Marcus observed stray bytes on iRiver devices;
1363 * these are still discarded.
1364 */
1365 unsigned int packlen = dtoh32(usbdata.length);
1366 unsigned int surplen = rlen - packlen;
1367
1368 if (surplen >= PTP_USB_BULK_HDR_LEN) {
1369 params->response_packet = malloc(surplen);
1370 memcpy(params->response_packet,
1371 (uint8_t *) &usbdata + packlen, surplen);
1372 params->response_packet_size = surplen;
tedbullock36f2afb2007-03-03 22:16:44 +00001373 /* Ignore reading one extra byte if device flags have been set */
Linus Walleijfec4d562008-06-01 22:30:36 +00001374 } else if(!FLAG_NO_ZERO_READS(ptp_usb) &&
1375 (rlen - dtoh32(usbdata.length) == 1)) {
Linus Walleijd1e14e02008-12-14 01:07:30 +00001376 libusb_glue_debug (params, "ptp2/ptp_usb_getdata: read %d bytes "
Linus Walleij49557482010-06-22 09:14:47 +00001377 "too much, expect problems!",
Linus Walleij8e3af002007-09-28 19:21:54 +00001378 rlen - dtoh32(usbdata.length));
Linus Walleija0323272007-01-07 12:21:30 +00001379 }
1380 rlen = packlen;
1381 }
1382
1383 /* For most PTP devices rlen is 512 == sizeof(usbdata)
1384 * here. For MTP devices splitting header and data it might
1385 * be 12.
1386 */
1387 /* Evaluate full data length. */
1388 len=dtoh32(usbdata.length)-PTP_USB_BULK_HDR_LEN;
1389
1390 /* autodetect split header/data MTP devices */
1391 if (dtoh32(usbdata.length) > 12 && (rlen==12))
1392 params->split_header_data = 1;
1393
Linus Walleija0323272007-01-07 12:21:30 +00001394 /* Copy first part of data to 'data' */
Linus Walleij14735952010-04-25 04:56:49 +00001395 putfunc_ret =
1396 handler->putfunc(
1397 params, handler->priv, rlen - PTP_USB_BULK_HDR_LEN,
1398 usbdata.payload.data,
1399 &written
1400 );
1401 if (putfunc_ret != PTP_RC_OK)
1402 return putfunc_ret;
Linus Walleijd866d242009-08-23 21:50:39 +00001403
1404 if (FLAG_NO_ZERO_READS(ptp_usb) &&
Linus Walleij049f50c2007-03-03 20:30:43 +00001405 len+PTP_USB_BULK_HDR_LEN == PTP_USB_BULK_HS_MAX_PACKET_LEN_READ) {
nicklas79daadbf22009-09-28 18:19:34 +00001406
1407 LIBMTP_USB_DEBUG("Reading in extra terminating byte\n");
1408
Linus Walleij049f50c2007-03-03 20:30:43 +00001409 // need to read in extra byte and discard it
1410 int result = 0;
1411 char byte = 0;
Linus Walleije04a1b92011-03-09 18:00:24 +01001412 result = USB_BULK_READ(ptp_usb->handle,
1413 ptp_usb->inep,
1414 &byte,
1415 1,
1416 ptp_usb->timeout);
Linus Walleijd866d242009-08-23 21:50:39 +00001417
Linus Walleij049f50c2007-03-03 20:30:43 +00001418 if (result != 1)
nicklas79daadbf22009-09-28 18:19:34 +00001419 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 +00001420 } 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 +00001421 int zeroresult = 0;
1422 char zerobyte = 0;
1423
nicklas79daadbf22009-09-28 18:19:34 +00001424
1425 LIBMTP_INFO("Reading in zero packet after header\n");
1426
Linus Walleije04a1b92011-03-09 18:00:24 +01001427 zeroresult = USB_BULK_READ(ptp_usb->handle,
1428 ptp_usb->inep,
1429 &zerobyte,
1430 0,
1431 ptp_usb->timeout);
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001432
Linus Walleij94cd9722007-09-28 21:29:53 +00001433 if (zeroresult != 0)
nicklas79daadbf22009-09-28 18:19:34 +00001434 LIBMTP_INFO("LIBMTP panic: unable to read in zero packet, response 0x%04x", zeroresult);
Linus Walleij94cd9722007-09-28 21:29:53 +00001435 }
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001436
Linus Walleija0323272007-01-07 12:21:30 +00001437 /* Is that all of data? */
Linus Walleij94cd9722007-09-28 21:29:53 +00001438 if (len+PTP_USB_BULK_HDR_LEN<=rlen) {
1439 break;
1440 }
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001441
Linus Walleij913a3062007-09-28 21:42:39 +00001442 ret = ptp_read_func(len - (rlen - PTP_USB_BULK_HDR_LEN),
1443 handler,
1444 params->data, &rlen, 1);
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001445
Linus Walleija0323272007-01-07 12:21:30 +00001446 if (ret!=PTP_RC_OK) {
Linus Walleij049f50c2007-03-03 20:30:43 +00001447 break;
Linus Walleija0323272007-01-07 12:21:30 +00001448 }
1449 } while (0);
Linus Walleija0323272007-01-07 12:21:30 +00001450 return ret;
1451}
1452
1453uint16_t
1454ptp_usb_getresp (PTPParams* params, PTPContainer* resp)
1455{
1456 uint16_t ret;
1457 unsigned long rlen;
1458 PTPUSBBulkContainer usbresp;
Linus Walleij3e4b1142007-11-01 23:54:16 +00001459 PTP_USB *ptp_usb = (PTP_USB *)(params->data);
Linus Walleija0323272007-01-07 12:21:30 +00001460
nicklas79daadbf22009-09-28 18:19:34 +00001461
1462 LIBMTP_USB_DEBUG("RESPONSE: ");
1463
Linus Walleija0323272007-01-07 12:21:30 +00001464 memset(&usbresp,0,sizeof(usbresp));
1465 /* read response, it should never be longer than sizeof(usbresp) */
1466 ret = ptp_usb_getpacket(params, &usbresp, &rlen);
1467
Linus Walleijb8f78c72008-03-02 20:22:56 +00001468 // Fix for bevahiour reported by Scott Snyder on Samsung YP-U3. The player
1469 // sends a packet containing just zeroes of length 2 (up to 4 has been seen too)
1470 // after a NULL packet when it should send the response. This code ignores
1471 // such illegal packets.
Linus Walleij3191bba2008-02-10 21:16:25 +00001472 while (ret==PTP_RC_OK && rlen<PTP_USB_BULK_HDR_LEN && usbresp.length==0) {
Linus Walleijd1e14e02008-12-14 01:07:30 +00001473 libusb_glue_debug (params, "ptp_usb_getresp: detected short response "
Linus Walleij3191bba2008-02-10 21:16:25 +00001474 "of %d bytes, expect problems! (re-reading "
1475 "response), rlen");
Linus Walleij00411ee2008-01-27 12:24:51 +00001476 ret = ptp_usb_getpacket(params, &usbresp, &rlen);
1477 }
1478
Linus Walleija0323272007-01-07 12:21:30 +00001479 if (ret!=PTP_RC_OK) {
1480 ret = PTP_ERROR_IO;
1481 } else
1482 if (dtoh16(usbresp.type)!=PTP_USB_CONTAINER_RESPONSE) {
1483 ret = PTP_ERROR_RESP_EXPECTED;
1484 } else
1485 if (dtoh16(usbresp.code)!=resp->Code) {
1486 ret = dtoh16(usbresp.code);
1487 }
nicklas79daadbf22009-09-28 18:19:34 +00001488
1489 LIBMTP_USB_DEBUG("%04x\n", ret);
1490
Linus Walleija0323272007-01-07 12:21:30 +00001491 if (ret!=PTP_RC_OK) {
Linus Walleijd1e14e02008-12-14 01:07:30 +00001492/* libusb_glue_error (params,
Linus Walleija0323272007-01-07 12:21:30 +00001493 "PTP: request code 0x%04x getting resp error 0x%04x",
1494 resp->Code, ret);*/
1495 return ret;
1496 }
1497 /* build an appropriate PTPContainer */
1498 resp->Code=dtoh16(usbresp.code);
1499 resp->SessionID=params->session_id;
1500 resp->Transaction_ID=dtoh32(usbresp.trans_id);
Linus Walleijfec4d562008-06-01 22:30:36 +00001501 if (FLAG_IGNORE_HEADER_ERRORS(ptp_usb)) {
Linus Walleij3e4b1142007-11-01 23:54:16 +00001502 if (resp->Transaction_ID != params->transaction_id-1) {
Linus Walleijd1e14e02008-12-14 01:07:30 +00001503 libusb_glue_debug (params, "ptp_usb_getresp: detected a broken "
Linus Walleij00411ee2008-01-27 12:24:51 +00001504 "PTP header, transaction ID insane, expect "
1505 "problems! (But continuing)");
Linus Walleij3e4b1142007-11-01 23:54:16 +00001506 // Repair the header, so it won't wreak more havoc.
1507 resp->Transaction_ID = params->transaction_id-1;
1508 }
1509 }
Linus Walleija0323272007-01-07 12:21:30 +00001510 resp->Param1=dtoh32(usbresp.payload.params.param1);
1511 resp->Param2=dtoh32(usbresp.payload.params.param2);
1512 resp->Param3=dtoh32(usbresp.payload.params.param3);
1513 resp->Param4=dtoh32(usbresp.payload.params.param4);
1514 resp->Param5=dtoh32(usbresp.payload.params.param5);
1515 return ret;
1516}
1517
1518/* Event handling functions */
1519
1520/* PTP Events wait for or check mode */
1521#define PTP_EVENT_CHECK 0x0000 /* waits for */
1522#define PTP_EVENT_CHECK_FAST 0x0001 /* checks */
1523
1524static inline uint16_t
1525ptp_usb_event (PTPParams* params, PTPContainer* event, int wait)
1526{
1527 uint16_t ret;
1528 int result;
1529 unsigned long rlen;
1530 PTPUSBEventContainer usbevent;
1531 PTP_USB *ptp_usb = (PTP_USB *)(params->data);
1532
1533 memset(&usbevent,0,sizeof(usbevent));
1534
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001535 if ((params==NULL) || (event==NULL))
Linus Walleija0323272007-01-07 12:21:30 +00001536 return PTP_ERROR_BADPARAM;
1537 ret = PTP_RC_OK;
1538 switch(wait) {
1539 case PTP_EVENT_CHECK:
Linus Walleije04a1b92011-03-09 18:00:24 +01001540 result=USB_BULK_READ(ptp_usb->handle,
1541 ptp_usb->intep,
1542 (char *) &usbevent,
1543 sizeof(usbevent),
1544 ptp_usb->timeout);
Linus Walleija0323272007-01-07 12:21:30 +00001545 if (result==0)
Linus Walleije04a1b92011-03-09 18:00:24 +01001546 result = USB_BULK_READ(ptp_usb->handle,
1547 ptp_usb->intep,
1548 (char *) &usbevent,
1549 sizeof(usbevent),
1550 ptp_usb->timeout);
Linus Walleija0323272007-01-07 12:21:30 +00001551 if (result < 0) ret = PTP_ERROR_IO;
1552 break;
1553 case PTP_EVENT_CHECK_FAST:
Linus Walleije04a1b92011-03-09 18:00:24 +01001554 result=USB_BULK_READ(ptp_usb->handle,
1555 ptp_usb->intep,
1556 (char *) &usbevent,
1557 sizeof(usbevent),
1558 ptp_usb->timeout);
Linus Walleija0323272007-01-07 12:21:30 +00001559 if (result==0)
Linus Walleije04a1b92011-03-09 18:00:24 +01001560 result = USB_BULK_READ(ptp_usb->handle,
1561 ptp_usb->intep,
1562 (char *) &usbevent,
1563 sizeof(usbevent),
1564 ptp_usb->timeout);
Linus Walleija0323272007-01-07 12:21:30 +00001565 if (result < 0) ret = PTP_ERROR_IO;
1566 break;
1567 default:
1568 ret=PTP_ERROR_BADPARAM;
1569 break;
1570 }
1571 if (ret!=PTP_RC_OK) {
Linus Walleijd1e14e02008-12-14 01:07:30 +00001572 libusb_glue_error (params,
Linus Walleija0323272007-01-07 12:21:30 +00001573 "PTP: reading event an error 0x%04x occurred", ret);
1574 return PTP_ERROR_IO;
1575 }
1576 rlen = result;
1577 if (rlen < 8) {
Linus Walleijd1e14e02008-12-14 01:07:30 +00001578 libusb_glue_error (params,
Linus Walleija0323272007-01-07 12:21:30 +00001579 "PTP: reading event an short read of %ld bytes occurred", rlen);
1580 return PTP_ERROR_IO;
1581 }
1582 /* if we read anything over interrupt endpoint it must be an event */
1583 /* build an appropriate PTPContainer */
1584 event->Code=dtoh16(usbevent.code);
1585 event->SessionID=params->session_id;
1586 event->Transaction_ID=dtoh32(usbevent.trans_id);
1587 event->Param1=dtoh32(usbevent.param1);
1588 event->Param2=dtoh32(usbevent.param2);
1589 event->Param3=dtoh32(usbevent.param3);
1590 return ret;
1591}
1592
1593uint16_t
1594ptp_usb_event_check (PTPParams* params, PTPContainer* event) {
1595
1596 return ptp_usb_event (params, event, PTP_EVENT_CHECK_FAST);
1597}
1598
1599uint16_t
1600ptp_usb_event_wait (PTPParams* params, PTPContainer* event) {
1601
1602 return ptp_usb_event (params, event, PTP_EVENT_CHECK);
1603}
1604
Linus Walleijbd3bf9e2007-09-14 19:31:54 +00001605uint16_t
1606ptp_usb_control_cancel_request (PTPParams *params, uint32_t transactionid) {
1607 PTP_USB *ptp_usb = (PTP_USB *)(params->data);
1608 int ret;
1609 unsigned char buffer[6];
1610
1611 htod16a(&buffer[0],PTP_EC_CancelTransaction);
1612 htod32a(&buffer[2],transactionid);
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001613 ret = usb_control_msg(ptp_usb->handle,
Linus Walleijbd3bf9e2007-09-14 19:31:54 +00001614 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
Linus Walleije04a1b92011-03-09 18:00:24 +01001615 0x64, 0x0000, 0x0000,
1616 (char *) buffer,
1617 sizeof(buffer),
1618 ptp_usb->timeout);
Linus Walleijbd3bf9e2007-09-14 19:31:54 +00001619 if (ret < sizeof(buffer))
1620 return PTP_ERROR_IO;
1621 return PTP_RC_OK;
1622}
Linus Walleija0323272007-01-07 12:21:30 +00001623
Linus Walleij9eb3d312006-08-04 19:25:59 +00001624static int init_ptp_usb (PTPParams* params, PTP_USB* ptp_usb, struct usb_device* dev)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001625{
Linus Walleijd6a49972006-05-02 08:24:54 +00001626 usb_dev_handle *device_handle;
Linus Walleijfea4f532009-09-22 22:08:35 +00001627 char buf[255];
Linus Walleijf2d747a2010-01-19 00:46:18 +00001628 int usbresult;
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001629
Linus Walleijd6a49972006-05-02 08:24:54 +00001630 params->sendreq_func=ptp_usb_sendreq;
1631 params->senddata_func=ptp_usb_senddata;
1632 params->getresp_func=ptp_usb_getresp;
1633 params->getdata_func=ptp_usb_getdata;
Linus Walleijff01cb12007-09-16 19:49:48 +00001634 params->cancelreq_func=ptp_usb_control_cancel_request;
Linus Walleijd6a49972006-05-02 08:24:54 +00001635 params->data=ptp_usb;
1636 params->transaction_id=0;
Linus Walleij462a5e72007-02-13 06:54:56 +00001637 /*
1638 * This is hardcoded here since we have no devices whatsoever that are BE.
1639 * Change this the day we run into our first BE device (if ever).
1640 */
Linus Walleijd6a49972006-05-02 08:24:54 +00001641 params->byteorder = PTP_DL_LE;
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001642
Linus Walleije04a1b92011-03-09 18:00:24 +01001643 ptp_usb->timeout = get_timeout(ptp_usb);
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001644
Yavor Goulishev825ff132011-02-26 00:29:01 +01001645 device_handle = usb_open(dev);
1646 if (!device_handle) {
1647 perror("usb_open()");
1648 return -1;
1649 }
1650 ptp_usb->handle = device_handle;
Linus Walleij501ba4d2006-10-16 06:17:04 +00001651#ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP
Yavor Goulishev825ff132011-02-26 00:29:01 +01001652 /*
1653 * If this device is known to be wrongfully claimed by other kernel
1654 * drivers (such as mass storage), then try to unload it to make it
1655 * accessible from user space.
1656 */
1657 if (FLAG_UNLOAD_DRIVER(ptp_usb)) {
1658 if (usb_get_driver_np(device_handle, (int) ptp_usb->interface,
1659 buf, sizeof(buf)) == 0) {
1660 if (usb_detach_kernel_driver_np(device_handle,
1661 (int) ptp_usb->interface)) {
1662 perror("usb_detach_kernel_driver_np()");
1663 return -1;
Linus Walleij501ba4d2006-10-16 06:17:04 +00001664 }
1665 }
Yavor Goulishev825ff132011-02-26 00:29:01 +01001666 }
Linus Walleij501ba4d2006-10-16 06:17:04 +00001667#endif
Linus Walleijeac1e002006-11-30 22:06:02 +00001668#ifdef __WIN32__
Yavor Goulishev825ff132011-02-26 00:29:01 +01001669 // Only needed on Windows, and cause problems on other platforms.
1670 if (usb_set_configuration(device_handle, dev->config->bConfigurationValue)) {
1671 perror("usb_set_configuration()");
1672 return -1;
1673 }
Linus Walleijeac1e002006-11-30 22:06:02 +00001674#endif
Yavor Goulishev825ff132011-02-26 00:29:01 +01001675 // It seems like on kernel 2.6.31 if we already have it open on another
1676 // pthread in our app, we'll get an error if we try to claim it again,
1677 // but that error is harmless because our process already claimed the interface
1678 usbresult = usb_claim_interface(device_handle, (int) ptp_usb->interface);
Linus Walleijf2d747a2010-01-19 00:46:18 +00001679
Yavor Goulishev825ff132011-02-26 00:29:01 +01001680 if (usbresult != 0)
1681 fprintf(stderr, "ignoring usb_claim_interface = %d", usbresult);
Linus Walleijf2d747a2010-01-19 00:46:18 +00001682
Yavor Goulishev825ff132011-02-26 00:29:01 +01001683 // FIXME : Discovered in the Barry project
1684 // kernels >= 2.6.28 don't set the interface the same way as
1685 // previous versions did, and the Blackberry gets confused
1686 // if it isn't explicitly set
1687 // See above, same issue with pthreads means that if this fails it is not
1688 // fatal
1689 // However, this causes problems on Macs so disable here
1690#ifndef __APPLE__
1691 usbresult = usb_set_altinterface(device_handle, 0);
1692 if (usbresult)
1693 fprintf(stderr, "ignoring usb_claim_interface = %d", usbresult);
1694#endif
Linus Walleijf2d747a2010-01-19 00:46:18 +00001695
Yavor Goulishev825ff132011-02-26 00:29:01 +01001696 if (FLAG_SWITCH_MODE_BLACKBERRY(ptp_usb)) {
1697 int ret;
Linus Walleijb6e06e22009-09-22 22:20:49 +00001698
Yavor Goulishev825ff132011-02-26 00:29:01 +01001699 // FIXME : Only for BlackBerry Storm
1700 // What does it mean? Maybe switch mode...
1701 // This first control message is absolutely necessary
1702 usleep(1000);
1703 ret = usb_control_msg(device_handle,
1704 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
1705 0xaa, 0x00, 0x04, buf, 0x40, 1000);
1706 LIBMTP_USB_DEBUG("BlackBerry magic part 1:\n");
1707 LIBMTP_USB_DATA(buf, ret, 16);
nicklas79daadbf22009-09-28 18:19:34 +00001708
Yavor Goulishev825ff132011-02-26 00:29:01 +01001709 usleep(1000);
1710 // This control message is unnecessary
1711 ret = usb_control_msg(device_handle,
1712 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
1713 0xa5, 0x00, 0x01, buf, 0x02, 1000);
1714 LIBMTP_USB_DEBUG("BlackBerry magic part 2:\n");
1715 LIBMTP_USB_DATA(buf, ret, 16);
nicklas79daadbf22009-09-28 18:19:34 +00001716
Yavor Goulishev825ff132011-02-26 00:29:01 +01001717 usleep(1000);
1718 // This control message is unnecessary
1719 ret = usb_control_msg(device_handle,
1720 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
1721 0xa8, 0x00, 0x01, buf, 0x05, 1000);
1722 LIBMTP_USB_DEBUG("BlackBerry magic part 3:\n");
1723 LIBMTP_USB_DATA(buf, ret, 16);
nicklas79daadbf22009-09-28 18:19:34 +00001724
Yavor Goulishev825ff132011-02-26 00:29:01 +01001725 usleep(1000);
1726 // This control message is unnecessary
1727 ret = usb_control_msg(device_handle,
1728 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
1729 0xa8, 0x00, 0x01, buf, 0x11, 1000);
1730 LIBMTP_USB_DEBUG("BlackBerry magic part 4:\n");
1731 LIBMTP_USB_DATA(buf, ret, 16);
nicklas79daadbf22009-09-28 18:19:34 +00001732
Yavor Goulishev825ff132011-02-26 00:29:01 +01001733 usleep(1000);
Linus Walleijd6a49972006-05-02 08:24:54 +00001734 }
Linus Walleij9eb3d312006-08-04 19:25:59 +00001735 return 0;
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001736}
1737
Linus Walleijd6a49972006-05-02 08:24:54 +00001738static void clear_stall(PTP_USB* ptp_usb)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001739{
Linus Walleij58b62792007-01-07 12:38:59 +00001740 uint16_t status;
Linus Walleijd6a49972006-05-02 08:24:54 +00001741 int ret;
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001742
Linus Walleijd6a49972006-05-02 08:24:54 +00001743 /* check the inep status */
Linus Walleij58b62792007-01-07 12:38:59 +00001744 status = 0;
Linus Walleij1fd2d272006-05-08 09:22:01 +00001745 ret = usb_get_endpoint_status(ptp_usb,ptp_usb->inep,&status);
Linus Walleij58b62792007-01-07 12:38:59 +00001746 if (ret<0) {
1747 perror ("inep: usb_get_endpoint_status()");
1748 } else if (status) {
nicklas79daadbf22009-09-28 18:19:34 +00001749 LIBMTP_INFO("Clearing stall on IN endpoint\n");
Linus Walleij58b62792007-01-07 12:38:59 +00001750 ret = usb_clear_stall_feature(ptp_usb,ptp_usb->inep);
1751 if (ret<0) {
1752 perror ("usb_clear_stall_feature()");
1753 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001754 }
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001755
Linus Walleijd6a49972006-05-02 08:24:54 +00001756 /* check the outep status */
Linus Walleij58b62792007-01-07 12:38:59 +00001757 status=0;
1758 ret = usb_get_endpoint_status(ptp_usb,ptp_usb->outep,&status);
1759 if (ret<0) {
1760 perror("outep: usb_get_endpoint_status()");
1761 } else if (status) {
nicklas79daadbf22009-09-28 18:19:34 +00001762 LIBMTP_INFO("Clearing stall on OUT endpoint\n");
Linus Walleij58b62792007-01-07 12:38:59 +00001763 ret = usb_clear_stall_feature(ptp_usb,ptp_usb->outep);
1764 if (ret<0) {
1765 perror("usb_clear_stall_feature()");
1766 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001767 }
Linus Walleij58b62792007-01-07 12:38:59 +00001768
1769 /* TODO: do we need this for INTERRUPT (ptp_usb->intep) too? */
1770}
1771
1772static void clear_halt(PTP_USB* ptp_usb)
1773{
1774 int ret;
1775
1776 ret = usb_clear_halt(ptp_usb->handle,ptp_usb->inep);
1777 if (ret<0) {
1778 perror("usb_clear_halt() on IN endpoint");
1779 }
1780 ret = usb_clear_halt(ptp_usb->handle,ptp_usb->outep);
1781 if (ret<0) {
1782 perror("usb_clear_halt() on OUT endpoint");
1783 }
1784 ret = usb_clear_halt(ptp_usb->handle,ptp_usb->intep);
1785 if (ret<0) {
1786 perror("usb_clear_halt() on INTERRUPT endpoint");
1787 }
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001788}
1789
Linus Walleijb0ab5482007-08-29 21:08:54 +00001790static void close_usb(PTP_USB* ptp_usb)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001791{
Linus Walleijfec4d562008-06-01 22:30:36 +00001792 if (!FLAG_NO_RELEASE_INTERFACE(ptp_usb)) {
Linus Walleijeabc2312008-02-13 00:02:00 +00001793 /*
1794 * Clear any stalled endpoints
1795 * On misbehaving devices designed for Windows/Mac, quote from:
1796 * http://www2.one-eyed-alien.net/~mdharm/linux-usb/target_offenses.txt
1797 * Device does Bad Things(tm) when it gets a GET_STATUS after CLEAR_HALT
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001798 * (...) Windows, when clearing a stall, only sends the CLEAR_HALT command,
1799 * and presumes that the stall has cleared. Some devices actually choke
1800 * if the CLEAR_HALT is followed by a GET_STATUS (used to determine if the
Linus Walleijeabc2312008-02-13 00:02:00 +00001801 * STALL is persistant or not).
1802 */
Linus Walleij29f03ba2007-09-25 19:22:12 +00001803 clear_stall(ptp_usb);
1804 // Clear halts on any endpoints
1805 clear_halt(ptp_usb);
1806 // Added to clear some stuff on the OUT endpoint
1807 // TODO: is this good on the Mac too?
1808 // HINT: some devices may need that you comment these two out too.
1809 usb_resetep(ptp_usb->handle, ptp_usb->outep);
1810 usb_release_interface(ptp_usb->handle, (int) ptp_usb->interface);
1811 }
Linus Walleijf6913172011-03-10 22:10:26 +01001812 if (FLAG_FORCE_RESET_ON_CLOSE(ptp_usb)) {
1813 /*
1814 * Some devices really love to get reset after being
1815 * disconnected. Again, since Windows never disconnects
1816 * a device closing behaviour is seldom or never exercised
1817 * on devices when engineered and often error prone.
1818 * Reset may help some.
1819 */
1820 usb_reset(ptp_usb->handle);
1821 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001822 usb_close(ptp_usb->handle);
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001823}
1824
Linus Walleij550d6d52007-01-24 14:32:51 +00001825/**
Linus Walleija700d222008-05-28 23:06:14 +00001826 * Self-explanatory?
1827 */
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001828static int find_interface_and_endpoints(struct usb_device *dev,
1829 uint8_t *interface,
1830 int* inep,
1831 int* inep_maxpacket,
1832 int* outep,
1833 int *outep_maxpacket,
1834 int* intep)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001835{
Linus Walleijd7373cd2007-08-28 21:42:05 +00001836 int i;
1837
1838 // Loop over the device configurations
1839 for (i = 0; i < dev->descriptor.bNumConfigurations; i++) {
Linus Walleijb0ab5482007-08-29 21:08:54 +00001840 uint8_t j;
Linus Walleijd7373cd2007-08-28 21:42:05 +00001841
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001842 // Loop over each configurations interfaces
Linus Walleijd7373cd2007-08-28 21:42:05 +00001843 for (j = 0; j < dev->config[i].bNumInterfaces; j++) {
Linus Walleijb0ab5482007-08-29 21:08:54 +00001844 uint8_t k;
1845 uint8_t no_ep;
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001846 int found_inep = 0;
1847 int found_outep = 0;
1848 int found_intep = 0;
Linus Walleijd7373cd2007-08-28 21:42:05 +00001849 struct usb_endpoint_descriptor *ep;
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001850
1851 // MTP devices shall have 3 endpoints, ignore those interfaces
1852 // that haven't.
1853 no_ep = dev->config[i].interface[j].altsetting->bNumEndpoints;
1854 if (no_ep != 3)
1855 continue;
Linus Walleijb0ab5482007-08-29 21:08:54 +00001856
1857 *interface = dev->config[i].interface[j].altsetting->bInterfaceNumber;
1858 ep = dev->config[i].interface[j].altsetting->endpoint;
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001859
1860 // Loop over the three endpoints to locate two bulk and
1861 // one interrupt endpoint and FAIL if we cannot, and continue.
Linus Walleijb0ab5482007-08-29 21:08:54 +00001862 for (k = 0; k < no_ep; k++) {
Linus Walleije73854e2010-06-22 09:07:34 +00001863 if (ep[k].bmAttributes == USB_ENDPOINT_TYPE_BULK) {
1864 if ((ep[k].bEndpointAddress & USB_ENDPOINT_DIR_MASK) ==
1865 USB_ENDPOINT_DIR_MASK) {
1866 *inep = ep[k].bEndpointAddress;
1867 *inep_maxpacket = ep[k].wMaxPacketSize;
1868 found_inep = 1;
1869 }
1870 if ((ep[k].bEndpointAddress & USB_ENDPOINT_DIR_MASK) == 0) {
Linus Walleij48fda572010-06-22 09:09:03 +00001871 *outep = ep[k].bEndpointAddress;
1872 *outep_maxpacket = ep[k].wMaxPacketSize;
Linus Walleije73854e2010-06-22 09:07:34 +00001873 found_outep = 1;
1874 }
1875 } else if (ep[k].bmAttributes == USB_ENDPOINT_TYPE_INTERRUPT) {
1876 if ((ep[k].bEndpointAddress & USB_ENDPOINT_DIR_MASK) ==
1877 USB_ENDPOINT_DIR_MASK) {
Linus Walleij48fda572010-06-22 09:09:03 +00001878 *intep = ep[k].bEndpointAddress;
Linus Walleije73854e2010-06-22 09:07:34 +00001879 found_intep = 1;
1880 }
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001881 }
Linus Walleijd7373cd2007-08-28 21:42:05 +00001882 }
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001883 if (found_inep && found_outep && found_intep)
1884 // We assigned the endpoints so return here.
1885 return 0;
1886 // Else loop to next interface/config
Linus Walleijd6a49972006-05-02 08:24:54 +00001887 }
1888 }
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001889 return -1;
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001890}
1891
Linus Walleija700d222008-05-28 23:06:14 +00001892/**
1893 * This function assigns params and usbinfo given a raw device
1894 * as input.
1895 * @param device the device to be assigned.
Linus Walleija700d222008-05-28 23:06:14 +00001896 * @param usbinfo a pointer to the new usbinfo.
1897 * @return an error code.
1898 */
Linus Walleij49557482010-06-22 09:14:47 +00001899LIBMTP_error_number_t configure_usb_device(LIBMTP_raw_device_t *device,
Linus Walleij3c643252008-05-31 23:22:28 +00001900 PTPParams *params,
Linus Walleija700d222008-05-28 23:06:14 +00001901 void **usbinfo)
1902{
Linus Walleija700d222008-05-28 23:06:14 +00001903 PTP_USB *ptp_usb;
1904 struct usb_device *libusb_device;
1905 uint16_t ret = 0;
Linus Walleija700d222008-05-28 23:06:14 +00001906 struct usb_bus *bus;
1907 int found = 0;
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001908 int err;
Linus Walleija700d222008-05-28 23:06:14 +00001909
1910 /* See if we can find this raw device again... */
1911 bus = init_usb();
1912 for (; bus != NULL; bus = bus->next) {
1913 if (bus->location == device->bus_location) {
1914 struct usb_device *dev = bus->devices;
1915
1916 for (; dev != NULL; dev = dev->next) {
1917 if(dev->devnum == device->devnum &&
1918 dev->descriptor.idVendor == device->device_entry.vendor_id &&
1919 dev->descriptor.idProduct == device->device_entry.product_id ) {
1920 libusb_device = dev;
1921 found = 1;
1922 break;
1923 }
1924 }
1925 if (found)
1926 break;
1927 }
1928 }
1929 /* Device has gone since detecting raw devices! */
1930 if (!found) {
1931 return LIBMTP_ERROR_NO_DEVICE_ATTACHED;
1932 }
1933
1934 /* Allocate structs */
Linus Walleija700d222008-05-28 23:06:14 +00001935 ptp_usb = (PTP_USB *) malloc(sizeof(PTP_USB));
Linus Walleij3c643252008-05-31 23:22:28 +00001936 if (ptp_usb == NULL) {
Linus Walleija700d222008-05-28 23:06:14 +00001937 return LIBMTP_ERROR_MEMORY_ALLOCATION;
1938 }
1939 /* Start with a blank slate (includes setting device_flags to 0) */
Linus Walleija700d222008-05-28 23:06:14 +00001940 memset(ptp_usb, 0, sizeof(PTP_USB));
1941
Linus Walleijfec4d562008-06-01 22:30:36 +00001942 /* Copy the raw device */
1943 memcpy(&ptp_usb->rawdevice, device, sizeof(LIBMTP_raw_device_t));
Linus Walleij335a81c2008-06-02 23:01:00 +00001944
1945 /*
1946 * Some devices must have their "OS Descriptor" massaged in order
1947 * to work.
1948 */
1949 if (FLAG_ALWAYS_PROBE_DESCRIPTOR(ptp_usb)) {
1950 // Massage the device descriptor
1951 (void) probe_device_descriptor(libusb_device, NULL);
1952 }
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001953
1954 /* Assign interface and endpoints to usbinfo... */
1955 err = find_interface_and_endpoints(libusb_device,
1956 &ptp_usb->interface,
1957 &ptp_usb->inep,
1958 &ptp_usb->inep_maxpacket,
1959 &ptp_usb->outep,
1960 &ptp_usb->outep_maxpacket,
1961 &ptp_usb->intep);
1962
1963 if (err) {
nicklas79daadbf22009-09-28 18:19:34 +00001964 LIBMTP_ERROR("LIBMTP PANIC: Unable to find interface & endpoints of device\n");
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001965 return LIBMTP_ERROR_CONNECTING;
1966 }
1967
Linus Walleije04a1b92011-03-09 18:00:24 +01001968 /* Copy USB version number */
1969 ptp_usb->bcdusb = libusb_device->descriptor.bcdUSB;
1970
Linus Walleija700d222008-05-28 23:06:14 +00001971 /* Attempt to initialize this device */
Linus Walleij3c643252008-05-31 23:22:28 +00001972 if (init_ptp_usb(params, ptp_usb, libusb_device) < 0) {
nicklas79daadbf22009-09-28 18:19:34 +00001973 LIBMTP_ERROR("LIBMTP PANIC: Unable to initialize device\n");
Linus Walleija700d222008-05-28 23:06:14 +00001974 return LIBMTP_ERROR_CONNECTING;
1975 }
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001976
Linus Walleija700d222008-05-28 23:06:14 +00001977 /*
1978 * This works in situations where previous bad applications
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001979 * have not used LIBMTP_Release_Device on exit
Linus Walleija700d222008-05-28 23:06:14 +00001980 */
Linus Walleij3c643252008-05-31 23:22:28 +00001981 if ((ret = ptp_opensession(params, 1)) == PTP_ERROR_IO) {
Linus Walleijf9137dc2010-10-31 17:13:06 +00001982 LIBMTP_ERROR("PTP_ERROR_IO: failed to open session, trying again after resetting USB interface\n");
Linus Walleija700d222008-05-28 23:06:14 +00001983 close_usb(ptp_usb);
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001984
Linus Walleijf9137dc2010-10-31 17:13:06 +00001985 LIBMTP_ERROR("LIBMTP libusb: Attempt to reset device\n");
1986 usb_reset(ptp_usb->handle);
1987
Linus Walleij3c643252008-05-31 23:22:28 +00001988 if(init_ptp_usb(params, ptp_usb, libusb_device) <0) {
Linus Walleijf9137dc2010-10-31 17:13:06 +00001989 LIBMTP_ERROR("LIBMTP PANIC: Could not init USB on second attempt\n");
Linus Walleija700d222008-05-28 23:06:14 +00001990 return LIBMTP_ERROR_CONNECTING;
1991 }
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001992
Linus Walleija700d222008-05-28 23:06:14 +00001993 /* Device has been reset, try again */
Linus Walleijf9137dc2010-10-31 17:13:06 +00001994 if ((ret = ptp_opensession(params, 1)) == PTP_ERROR_IO) {
1995 LIBMTP_ERROR("LIBMTP PANIC: failed to open session on second attempt\n");
1996 return LIBMTP_ERROR_CONNECTING;
1997 }
Linus Walleija700d222008-05-28 23:06:14 +00001998 }
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001999
Linus Walleija700d222008-05-28 23:06:14 +00002000 /* Was the transaction id invalid? Try again */
2001 if (ret == PTP_RC_InvalidTransactionID) {
nicklas79daadbf22009-09-28 18:19:34 +00002002 LIBMTP_ERROR("LIBMTP WARNING: Transaction ID was invalid, increment and try again\n");
Linus Walleij3c643252008-05-31 23:22:28 +00002003 params->transaction_id += 10;
2004 ret = ptp_opensession(params, 1);
Linus Walleija700d222008-05-28 23:06:14 +00002005 }
2006
2007 if (ret != PTP_RC_SessionAlreadyOpened && ret != PTP_RC_OK) {
nicklas79daadbf22009-09-28 18:19:34 +00002008 LIBMTP_ERROR("LIBMTP PANIC: Could not open session! "
Linus Walleija700d222008-05-28 23:06:14 +00002009 "(Return code %d)\n Try to reset the device.\n",
2010 ret);
2011 usb_release_interface(ptp_usb->handle,
2012 (int) ptp_usb->interface);
2013 return LIBMTP_ERROR_CONNECTING;
2014 }
2015
2016 /* OK configured properly */
Linus Walleija700d222008-05-28 23:06:14 +00002017 *usbinfo = (void *) ptp_usb;
2018 return LIBMTP_ERROR_NONE;
2019}
2020
2021
Linus Walleijb0ab5482007-08-29 21:08:54 +00002022void close_device (PTP_USB *ptp_usb, PTPParams *params)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00002023{
Linus Walleijd6a49972006-05-02 08:24:54 +00002024 if (ptp_closesession(params)!=PTP_RC_OK)
nicklas79daadbf22009-09-28 18:19:34 +00002025 LIBMTP_ERROR("ERROR: Could not close session!\n");
Linus Walleijb0ab5482007-08-29 21:08:54 +00002026 close_usb(ptp_usb);
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00002027}
2028
Linus Walleij2f622812008-08-30 22:06:58 +00002029void set_usb_device_timeout(PTP_USB *ptp_usb, int timeout)
2030{
Linus Walleije04a1b92011-03-09 18:00:24 +01002031 ptp_usb->timeout = timeout;
Linus Walleij2f622812008-08-30 22:06:58 +00002032}
2033
2034void get_usb_device_timeout(PTP_USB *ptp_usb, int *timeout)
2035{
Linus Walleije04a1b92011-03-09 18:00:24 +01002036 *timeout = ptp_usb->timeout;
2037}
2038
2039int guess_usb_speed(PTP_USB *ptp_usb)
2040{
2041 int bytes_per_second;
2042
2043 /*
2044 * We don't know the actual speeds so these are rough guesses
2045 * from the info you can find here:
2046 * http://en.wikipedia.org/wiki/USB#Transfer_rates
2047 * http://www.barefeats.com/usb2.html
2048 */
2049 switch (ptp_usb->bcdusb & 0xFF00) {
2050 case 0x0100:
2051 /* 1.x USB versions let's say 1MiB/s */
2052 bytes_per_second = 1*1024*1024;
2053 break;
2054 case 0x0200:
2055 case 0x0300:
2056 /* USB 2.0 nominal speed 18MiB/s */
2057 /* USB 3.0 won't be worse? */
2058 bytes_per_second = 18*1024*1024;
2059 break;
2060 default:
2061 /* Half-guess something? */
2062 bytes_per_second = 1*1024*1024;
2063 break;
2064 }
2065 return bytes_per_second;
Linus Walleij2f622812008-08-30 22:06:58 +00002066}
2067
Linus Walleijd6a49972006-05-02 08:24:54 +00002068static int usb_clear_stall_feature(PTP_USB* ptp_usb, int ep)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00002069{
Linus Walleijd6a49972006-05-02 08:24:54 +00002070 return (usb_control_msg(ptp_usb->handle,
Linus Walleije04a1b92011-03-09 18:00:24 +01002071 USB_RECIP_ENDPOINT,
2072 USB_REQ_CLEAR_FEATURE,
2073 USB_FEATURE_HALT,
2074 ep,
2075 NULL,
2076 0,
2077 ptp_usb->timeout));
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00002078}
2079
Linus Walleijd6a49972006-05-02 08:24:54 +00002080static int usb_get_endpoint_status(PTP_USB* ptp_usb, int ep, uint16_t* status)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00002081{
Linus Walleijd6a49972006-05-02 08:24:54 +00002082 return (usb_control_msg(ptp_usb->handle,
Linus Walleije04a1b92011-03-09 18:00:24 +01002083 USB_DP_DTH|USB_RECIP_ENDPOINT,
2084 USB_REQ_GET_STATUS,
2085 USB_FEATURE_HALT,
2086 ep,
2087 (char *) status,
2088 2,
2089 ptp_usb->timeout));
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00002090}