blob: 0079b0876df3273360dba8aada13ab8db4d03573 [file] [log] [blame]
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001/*
Linus Walleij7cf02642011-11-16 23:44:58 +01002 * \file libusb-glue.c
Linus Walleij2f45d222007-02-02 22:47:39 +00003 * 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 Walleij74339382012-01-18 19:56:13 +01006 * Copyright (C) 2005-2012 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"
Linus Walleij7cf02642011-11-16 23:44:58 +010034#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 Walleij26ceac82011-11-17 22:41:54 +010043#include <unistd.h>
Linus Walleijeb8c6fe2006-02-03 09:46:22 +000044
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
Linus Walleij7cf02642011-11-16 23:44:58 +010070/* 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
Linus Walleijeb8c6fe2006-02-03 09:46:22 +000078/* 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 {
Linus Walleij7cf02642011-11-16 23:44:58 +010085 struct usb_device *libusb_device;
Linus Walleija700d222008-05-28 23:06:14 +000086 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 Walleij7cf02642011-11-16 23:44:58 +0100100static struct usb_bus* init_usb();
Linus Walleijb0ab5482007-08-29 21:08:54 +0000101static void close_usb(PTP_USB* ptp_usb);
Linus Walleij7cf02642011-11-16 23:44:58 +0100102static int find_interface_and_endpoints(struct usb_device *dev,
Linus Walleij15d18e32012-08-20 02:00:34 +0200103 uint8_t *conf,
Linus Walleij5f3c44b2009-09-12 21:03:06 +0000104 uint8_t *interface,
Linus Walleij15d18e32012-08-20 02:00:34 +0200105 uint8_t *altsetting,
Linus Walleij5f3c44b2009-09-12 21:03:06 +0000106 int* inep,
107 int* inep_maxpacket,
108 int* outep,
109 int* outep_maxpacket,
110 int* intep);
Linus Walleijd6a49972006-05-02 08:24:54 +0000111static void clear_stall(PTP_USB* ptp_usb);
Linus Walleij15d18e32012-08-20 02:00:34 +0200112static int init_ptp_usb(PTPParams* params, PTP_USB* ptp_usb, struct usb_device* dev);
113static short ptp_write_func(unsigned long,PTPDataHandler*,void *data,unsigned long*);
114static short ptp_read_func(unsigned long,PTPDataHandler*,void *data,unsigned long*,int);
Linus Walleij7cf02642011-11-16 23:44:58 +0100115static int usb_clear_stall_feature(PTP_USB* ptp_usb, int ep);
Linus Walleijd6a49972006-05-02 08:24:54 +0000116static int usb_get_endpoint_status(PTP_USB* ptp_usb, int ep, uint16_t* status);
Linus Walleij1fd2d272006-05-08 09:22:01 +0000117
Linus Walleij552ba322007-01-22 11:49:59 +0000118/**
119 * Get a list of the supported USB devices.
120 *
121 * The developers depend on users of this library to constantly
122 * add in to the list of supported devices. What we need is the
123 * device name, USB Vendor ID (VID) and USB Product ID (PID).
124 * put this into a bug ticket at the project homepage, please.
125 * The VID/PID is used to let e.g. udev lift the device to
126 * console userspace access when it's plugged in.
127 *
128 * @param devices a pointer to a pointer that will hold a device
129 * list after the call to this function, if it was
130 * successful.
131 * @param numdevs a pointer to an integer that will hold the number
132 * of devices in the device list if the call was successful.
133 * @return 0 if the list was successfull retrieved, any other
134 * value means failure.
135 */
136int LIBMTP_Get_Supported_Devices_List(LIBMTP_device_entry_t ** const devices, int * const numdevs)
Linus Walleij6fd2f082006-03-28 07:19:22 +0000137{
138 *devices = (LIBMTP_device_entry_t *) &mtp_device_table;
139 *numdevs = mtp_device_table_size;
140 return 0;
141}
142
Linus Walleij552ba322007-01-22 11:49:59 +0000143
Linus Walleij7cf02642011-11-16 23:44:58 +0100144static struct usb_bus* init_usb()
Linus Walleij1fd2d272006-05-08 09:22:01 +0000145{
Linus Walleij7cf02642011-11-16 23:44:58 +0100146 struct usb_bus* busses;
147 struct usb_bus* bus;
148
Linus Walleijc1a82f12009-11-30 00:06:28 +0000149 /*
nicklas79daadbf22009-09-28 18:19:34 +0000150 * Some additional libusb debugging please.
151 * We use the same level debug between MTP and USB.
152 */
Linus Walleijc9105ab2011-11-14 20:45:02 +0100153 if ((LIBMTP_debug & LIBMTP_DEBUG_USB) != 0)
Linus Walleij7cf02642011-11-16 23:44:58 +0100154 usb_set_debug(9);
155
156 usb_init();
157 usb_find_busses();
158 usb_find_devices();
159 /* Workaround a libusb 0.1 bug : bus location is not initialised */
160 busses = usb_get_busses();
161 for (bus = busses; bus != NULL; bus = bus->next) {
162 if (!bus->location)
163 bus->location = strtoul(bus->dirname, NULL, 10);
164 }
165 return (busses);
Linus Walleij1fd2d272006-05-08 09:22:01 +0000166}
167
168/**
tedbullock0f033cb2007-02-14 20:56:54 +0000169 * Small recursive function to append a new usb_device to the linked list of
170 * USB MTP devices
Linus Walleijc1a82f12009-11-30 00:06:28 +0000171 * @param devlist dynamic linked list of pointers to usb devices with MTP
Linus Walleij63fd1e62008-04-23 23:49:43 +0000172 * properties, to be extended with new device.
173 * @param newdevice the new device to add.
174 * @param bus_location bus for this device.
175 * @return an extended array or NULL on failure.
tedbullock0f033cb2007-02-14 20:56:54 +0000176 */
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000177static mtpdevice_list_t *append_to_mtpdevice_list(mtpdevice_list_t *devlist,
Linus Walleij7cf02642011-11-16 23:44:58 +0100178 struct usb_device *newdevice,
Linus Walleij63fd1e62008-04-23 23:49:43 +0000179 uint32_t bus_location)
tedbullock0f033cb2007-02-14 20:56:54 +0000180{
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000181 mtpdevice_list_t *new_list_entry;
Linus Walleijc1a82f12009-11-30 00:06:28 +0000182
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000183 new_list_entry = (mtpdevice_list_t *) malloc(sizeof(mtpdevice_list_t));
184 if (new_list_entry == NULL) {
185 return NULL;
186 }
187 // Fill in USB device, if we *HAVE* to make a copy of the device do it here.
Linus Walleij7cf02642011-11-16 23:44:58 +0100188 new_list_entry->libusb_device = newdevice;
Linus Walleij63fd1e62008-04-23 23:49:43 +0000189 new_list_entry->bus_location = bus_location;
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000190 new_list_entry->next = NULL;
Linus Walleij5d987e12010-05-22 22:35:33 +0000191
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000192 if (devlist == NULL) {
193 return new_list_entry;
194 } else {
195 mtpdevice_list_t *tmp = devlist;
196 while (tmp->next != NULL) {
197 tmp = tmp->next;
198 }
199 tmp->next = new_list_entry;
200 }
201 return devlist;
tedbullock0f033cb2007-02-14 20:56:54 +0000202}
203
204/**
205 * Small recursive function to free dynamic memory allocated to the linked list
206 * of USB MTP devices
Linus Walleij5d987e12010-05-22 22:35:33 +0000207 * @param devlist dynamic linked list of pointers to usb devices with MTP
tedbullock0f033cb2007-02-14 20:56:54 +0000208 * properties.
209 * @return nothing
210 */
Linus Walleija700d222008-05-28 23:06:14 +0000211static void free_mtpdevice_list(mtpdevice_list_t *devlist)
tedbullock0f033cb2007-02-14 20:56:54 +0000212{
Linus Walleijac061862007-03-07 08:28:33 +0000213 mtpdevice_list_t *tmplist = devlist;
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000214
215 if (devlist == NULL)
216 return;
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000217 while (tmplist != NULL) {
218 mtpdevice_list_t *tmp = tmplist;
219 tmplist = tmplist->next;
Richard Low61edc1a2007-09-23 10:35:48 +0000220 // Do not free() the fields (ptp_usb, params)! These are used elsewhere.
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000221 free(tmp);
tedbullock0f033cb2007-02-14 20:56:54 +0000222 }
tedbullock0f033cb2007-02-14 20:56:54 +0000223 return;
224}
225
226/**
Linus Walleij9521e2b2007-07-10 21:50:42 +0000227 * This checks if a device has an MTP descriptor. The descriptor was
228 * elaborated about in gPhoto bug 1482084, and some official documentation
229 * with no strings attached was published by Microsoft at
230 * http://www.microsoft.com/whdc/system/bus/USB/USBFAQ_intermed.mspx#E3HAC
231 *
Linus Walleija4942fc2007-03-12 19:23:21 +0000232 * @param dev a device struct from libusb.
Linus Walleij9521e2b2007-07-10 21:50:42 +0000233 * @param dumpfile set to non-NULL to make the descriptors dump out
234 * to this file in human-readable hex so we can scruitinze them.
Linus Walleija4942fc2007-03-12 19:23:21 +0000235 * @return 1 if the device is MTP compliant, 0 if not.
236 */
Linus Walleij7cf02642011-11-16 23:44:58 +0100237static int probe_device_descriptor(struct usb_device *dev, FILE *dumpfile)
Linus Walleija4942fc2007-03-12 19:23:21 +0000238{
Linus Walleij7cf02642011-11-16 23:44:58 +0100239 usb_dev_handle *devh;
Linus Walleija4942fc2007-03-12 19:23:21 +0000240 unsigned char buf[1024], cmd;
Linus Walleijbc550bc2007-11-08 23:25:09 +0000241 int i;
Linus Walleija4942fc2007-03-12 19:23:21 +0000242 int ret;
Linus Walleijfb3e3022011-01-19 19:20:34 +0100243 /* This is to indicate if we find some vendor interface */
244 int found_vendor_spec_interface = 0;
Linus Walleij5d987e12010-05-22 22:35:33 +0000245
246 /*
Linus Walleijfb3e3022011-01-19 19:20:34 +0100247 * Don't examine devices that are not likely to
248 * contain any MTP interface, update this the day
249 * you find some weird combination...
Linus Walleij5d987e12010-05-22 22:35:33 +0000250 */
Linus Walleij7cf02642011-11-16 23:44:58 +0100251 if (!(dev->descriptor.bDeviceClass == USB_CLASS_PER_INTERFACE ||
252 dev->descriptor.bDeviceClass == USB_CLASS_COMM ||
253 dev->descriptor.bDeviceClass == USB_CLASS_PTP ||
254 dev->descriptor.bDeviceClass == 0xEF || /* Intf. Association Desc.*/
255 dev->descriptor.bDeviceClass == USB_CLASS_VENDOR_SPEC)) {
Linus Walleija4942fc2007-03-12 19:23:21 +0000256 return 0;
257 }
Linus Walleij5d987e12010-05-22 22:35:33 +0000258
Linus Walleija4942fc2007-03-12 19:23:21 +0000259 /* Attempt to open Device on this port */
Linus Walleij7cf02642011-11-16 23:44:58 +0100260 devh = usb_open(dev);
261 if (devh == NULL) {
Linus Walleija4942fc2007-03-12 19:23:21 +0000262 /* Could not open this device */
263 return 0;
264 }
Linus Walleijbc550bc2007-11-08 23:25:09 +0000265
266 /*
Richard Lowe029eba2009-04-11 12:36:25 +0000267 * This sometimes crashes on the j for loop below
Linus Walleij88244212009-08-12 16:05:56 +0000268 * I think it is because config is NULL yet
Richard Lowe029eba2009-04-11 12:36:25 +0000269 * dev->descriptor.bNumConfigurations > 0
270 * this check should stop this
Linus Walleijbc550bc2007-11-08 23:25:09 +0000271 */
Linus Walleij7cf02642011-11-16 23:44:58 +0100272 if (dev->config) {
273 /*
274 * Loop over the device configurations and interfaces. Nokia MTP-capable
275 * handsets (possibly others) typically have the string "MTP" in their
276 * MTP interface descriptions, that's how they can be detected, before
277 * we try the more esoteric "OS descriptors" (below).
278 */
279 for (i = 0; i < dev->descriptor.bNumConfigurations; i++) {
280 uint8_t j;
Linus Walleij88244212009-08-12 16:05:56 +0000281
Linus Walleij7cf02642011-11-16 23:44:58 +0100282 for (j = 0; j < dev->config[i].bNumInterfaces; j++) {
Richard Lowe029eba2009-04-11 12:36:25 +0000283 int k;
Linus Walleij7cf02642011-11-16 23:44:58 +0100284 for (k = 0; k < dev->config[i].interface[j].num_altsetting; k++) {
Linus Walleij88244212009-08-12 16:05:56 +0000285 /* Current interface descriptor */
Linus Walleij7cf02642011-11-16 23:44:58 +0100286 struct usb_interface_descriptor *intf =
287 &dev->config[i].interface[j].altsetting[k];
Linus Walleij88244212009-08-12 16:05:56 +0000288
289 /*
Linus Walleijf765caf2011-04-01 00:54:04 +0200290 * MTP interfaces have three endpoints, two bulk and one
291 * interrupt. Don't probe anything else.
292 */
293 if (intf->bNumEndpoints != 3)
294 continue;
295
296 /*
Linus Walleijfb3e3022011-01-19 19:20:34 +0100297 * We only want to probe for the OS descriptor if the
298 * device is USB_CLASS_VENDOR_SPEC or one of the interfaces
299 * in it is, so flag if we find an interface like this.
300 */
301 if (intf->bInterfaceClass == USB_CLASS_VENDOR_SPEC) {
302 found_vendor_spec_interface = 1;
303 }
304
305 /*
Linus Walleij88244212009-08-12 16:05:56 +0000306 * Check for Still Image Capture class with PIMA 15740 protocol,
307 * also known as PTP
308 */
Linus Walleij5d92c8c2011-01-25 23:42:19 +0100309#if 0
Linus Walleij88244212009-08-12 16:05:56 +0000310 if (intf->bInterfaceClass == USB_CLASS_PTP
311 && intf->bInterfaceSubClass == 0x01
312 && intf->bInterfaceProtocol == 0x01) {
313 if (dumpfile != NULL) {
Linus Walleij5d92c8c2011-01-25 23:42:19 +0100314 fprintf(dumpfile, " Found PTP device, check vendor "
315 "extension...\n");
Linus Walleij88244212009-08-12 16:05:56 +0000316 }
Linus Walleij5d92c8c2011-01-25 23:42:19 +0100317 // This is where we may insert code to open a PTP
318 // session and query the vendor extension ID to see
Linus Walleij4eb8ad92011-01-27 22:52:07 +0100319 // if it is 0xffffffff, i.e. MTP according to the spec.
Linus Walleij5d92c8c2011-01-25 23:42:19 +0100320 if (was_mtp_extension) {
Linus Walleij7cf02642011-11-16 23:44:58 +0100321 usb_close(devh);
Linus Walleij5d92c8c2011-01-25 23:42:19 +0100322 return 1;
323 }
Linus Walleij88244212009-08-12 16:05:56 +0000324 }
Linus Walleij5d92c8c2011-01-25 23:42:19 +0100325#endif
326
327 /*
328 * Next we search for the MTP substring in the interface name.
329 * For example : "RIM MS/MTP" should work.
330 */
Linus Walleij88244212009-08-12 16:05:56 +0000331 buf[0] = '\0';
Linus Walleij7cf02642011-11-16 23:44:58 +0100332 ret = usb_get_string_simple(devh,
333 dev->config[i].interface[j].altsetting[k].iInterface,
334 (char *) buf,
Linus Walleij88244212009-08-12 16:05:56 +0000335 1024);
336 if (ret < 3)
337 continue;
Linus Walleijfea4f532009-09-22 22:08:35 +0000338 if (strstr((char *) buf, "MTP") != NULL) {
Linus Walleij88244212009-08-12 16:05:56 +0000339 if (dumpfile != NULL) {
340 fprintf(dumpfile, "Configuration %d, interface %d, altsetting %d:\n", i, j, k);
341 fprintf(dumpfile, " Interface description contains the string \"MTP\"\n");
342 fprintf(dumpfile, " Device recognized as MTP, no further probing.\n");
343 }
Linus Walleij7cf02642011-11-16 23:44:58 +0100344 usb_close(devh);
Richard Lowe029eba2009-04-11 12:36:25 +0000345 return 1;
346 }
Linus Walleij7cf02642011-11-16 23:44:58 +0100347#ifdef LIBUSB_HAS_GET_DRIVER_NP
Linus Walleij88244212009-08-12 16:05:56 +0000348 {
349 /*
350 * Specifically avoid probing anything else than USB mass storage devices
351 * and non-associated drivers in Linux.
352 */
Linus Walleij7cf02642011-11-16 23:44:58 +0100353 char devname[0x10];
354
355 devname[0] = '\0';
356 ret = usb_get_driver_np(devh,
357 dev->config[i].interface[j].altsetting[k].iInterface,
358 devname,
359 sizeof(devname));
360 if (devname[0] != '\0' && strcmp(devname, "usb-storage")) {
361 LIBMTP_INFO("avoid probing device using kernel interface \"%s\"\n", devname);
Linus Walleij88244212009-08-12 16:05:56 +0000362 return 0;
363 }
364 }
Linus Walleij7cf02642011-11-16 23:44:58 +0100365#endif
Richard Lowe029eba2009-04-11 12:36:25 +0000366 }
367 }
368 }
Linus Walleij7cf02642011-11-16 23:44:58 +0100369 } else {
370 if (dev->descriptor.bNumConfigurations)
371 LIBMTP_INFO("dev->config is NULL in probe_device_descriptor yet dev->descriptor.bNumConfigurations > 0\n");
372 }
Linus Walleijfea4f532009-09-22 22:08:35 +0000373
Linus Walleij49e57b82009-12-16 09:21:33 +0000374 /*
Linus Walleijfb3e3022011-01-19 19:20:34 +0100375 * Only probe for OS descriptor if the device is vendor specific
376 * or one of the interfaces found is.
Linus Walleij49e57b82009-12-16 09:21:33 +0000377 */
Linus Walleij7cf02642011-11-16 23:44:58 +0100378 if (dev->descriptor.bDeviceClass == USB_CLASS_VENDOR_SPEC ||
Linus Walleijfb3e3022011-01-19 19:20:34 +0100379 found_vendor_spec_interface) {
Linus Walleij9521e2b2007-07-10 21:50:42 +0000380
Linus Walleijfb3e3022011-01-19 19:20:34 +0100381 /* Read the special descriptor */
Linus Walleij7cf02642011-11-16 23:44:58 +0100382 ret = usb_get_descriptor(devh, 0x03, 0xee, buf, sizeof(buf));
Linus Walleijfea4f532009-09-22 22:08:35 +0000383
Linus Walleijfb3e3022011-01-19 19:20:34 +0100384 /*
385 * If something failed we're probably stalled to we need
386 * to clear the stall off the endpoint and say this is not
387 * MTP.
388 */
389 if (ret < 0) {
390 /* EP0 is the default control endpoint */
Linus Walleij7cf02642011-11-16 23:44:58 +0100391 usb_clear_halt(devh, 0);
392 usb_close(devh);
Linus Walleijfb3e3022011-01-19 19:20:34 +0100393 return 0;
394 }
395
396 // Dump it, if requested
397 if (dumpfile != NULL && ret > 0) {
398 fprintf(dumpfile, "Microsoft device descriptor 0xee:\n");
399 data_dump_ascii(dumpfile, buf, ret, 16);
400 }
401
402 /* Check if descriptor length is at least 10 bytes */
403 if (ret < 10) {
Linus Walleij7cf02642011-11-16 23:44:58 +0100404 usb_close(devh);
Linus Walleijfb3e3022011-01-19 19:20:34 +0100405 return 0;
406 }
407
408 /* Check if this device has a Microsoft Descriptor */
409 if (!((buf[2] == 'M') && (buf[4] == 'S') &&
410 (buf[6] == 'F') && (buf[8] == 'T'))) {
Linus Walleij7cf02642011-11-16 23:44:58 +0100411 usb_close(devh);
Linus Walleijfb3e3022011-01-19 19:20:34 +0100412 return 0;
413 }
414
415 /* Check if device responds to control message 1 or if there is an error */
416 cmd = buf[16];
Linus Walleij7cf02642011-11-16 23:44:58 +0100417 ret = usb_control_msg (devh,
418 USB_ENDPOINT_IN | USB_RECIP_DEVICE | USB_TYPE_VENDOR,
Linus Walleijfb3e3022011-01-19 19:20:34 +0100419 cmd,
420 0,
421 4,
Linus Walleij7cf02642011-11-16 23:44:58 +0100422 (char *) buf,
Linus Walleijfb3e3022011-01-19 19:20:34 +0100423 sizeof(buf),
424 USB_TIMEOUT_DEFAULT);
425
426 // Dump it, if requested
427 if (dumpfile != NULL && ret > 0) {
428 fprintf(dumpfile, "Microsoft device response to control message 1, CMD 0x%02x:\n", cmd);
429 data_dump_ascii(dumpfile, buf, ret, 16);
430 }
431
432 /* If this is true, the device either isn't MTP or there was an error */
433 if (ret <= 0x15) {
434 /* TODO: If there was an error, flag it and let the user know somehow */
435 /* if(ret == -1) {} */
Linus Walleij7cf02642011-11-16 23:44:58 +0100436 usb_close(devh);
Linus Walleijfb3e3022011-01-19 19:20:34 +0100437 return 0;
438 }
439
440 /* Check if device is MTP or if it is something like a USB Mass Storage
441 device with Janus DRM support */
442 if ((buf[0x12] != 'M') || (buf[0x13] != 'T') || (buf[0x14] != 'P')) {
Linus Walleij7cf02642011-11-16 23:44:58 +0100443 usb_close(devh);
Linus Walleijfb3e3022011-01-19 19:20:34 +0100444 return 0;
445 }
446
447 /* After this point we are probably dealing with an MTP device */
448
449 /*
450 * Check if device responds to control message 2, which is
451 * the extended device parameters. Most devices will just
452 * respond with a copy of the same message as for the first
453 * message, some respond with zero-length (which is OK)
454 * and some with pure garbage. We're not parsing the result
455 * so this is not very important.
456 */
Linus Walleij7cf02642011-11-16 23:44:58 +0100457 ret = usb_control_msg (devh,
458 USB_ENDPOINT_IN | USB_RECIP_DEVICE | USB_TYPE_VENDOR,
Linus Walleijfb3e3022011-01-19 19:20:34 +0100459 cmd,
460 0,
461 5,
Linus Walleij7cf02642011-11-16 23:44:58 +0100462 (char *) buf,
Linus Walleijfb3e3022011-01-19 19:20:34 +0100463 sizeof(buf),
464 USB_TIMEOUT_DEFAULT);
465
466 // Dump it, if requested
467 if (dumpfile != NULL && ret > 0) {
468 fprintf(dumpfile, "Microsoft device response to control message 2, CMD 0x%02x:\n", cmd);
469 data_dump_ascii(dumpfile, buf, ret, 16);
470 }
471
472 /* If this is true, the device errored against control message 2 */
473 if (ret == -1) {
474 /* TODO: Implement callback function to let managing program know there
475 was a problem, along with description of the problem */
476 LIBMTP_ERROR("Potential MTP Device with VendorID:%04x and "
477 "ProductID:%04x encountered an error responding to "
478 "control message 2.\n"
479 "Problems may arrise but continuing\n",
Linus Walleij7cf02642011-11-16 23:44:58 +0100480 dev->descriptor.idVendor, dev->descriptor.idProduct);
Linus Walleijfb3e3022011-01-19 19:20:34 +0100481 } else if (dumpfile != NULL && ret == 0) {
482 fprintf(dumpfile, "Zero-length response to control message 2 (OK)\n");
483 } else if (dumpfile != NULL) {
484 fprintf(dumpfile, "Device responds to control message 2 with some data.\n");
485 }
Marcus Meissnere0bb6c62011-02-07 13:26:58 +0100486 /* Close the USB device handle */
Linus Walleij7cf02642011-11-16 23:44:58 +0100487 usb_close(devh);
Marcus Meissnere0bb6c62011-02-07 13:26:58 +0100488 return 1;
Linus Walleija4942fc2007-03-12 19:23:21 +0000489 }
Linus Walleijfea4f532009-09-22 22:08:35 +0000490
Linus Walleija4942fc2007-03-12 19:23:21 +0000491 /* Close the USB device handle */
Linus Walleij7cf02642011-11-16 23:44:58 +0100492 usb_close(devh);
Marcus Meissnere0bb6c62011-02-07 13:26:58 +0100493 return 0;
Linus Walleija4942fc2007-03-12 19:23:21 +0000494}
495
496/**
497 * This function scans through the connected usb devices on a machine and
498 * if they match known Vendor and Product identifiers appends them to the
Linus Walleij49557482010-06-22 09:14:47 +0000499 * dynamic array mtp_device_list. Be sure to call
500 * <code>free_mtpdevice_list(mtp_device_list)</code> when you are done
Linus Walleij63fd1e62008-04-23 23:49:43 +0000501 * with it, assuming it is not NULL.
Linus Walleij49557482010-06-22 09:14:47 +0000502 * @param mtp_device_list dynamic array of pointers to usb devices with MTP
Linus Walleij45a86372007-03-07 09:36:19 +0000503 * properties (if this list is not empty, new entries will be appended
504 * to the list).
Linus Walleija4942fc2007-03-12 19:23:21 +0000505 * @return LIBMTP_ERROR_NONE implies that devices have been found, scan the list
Linus Walleij49557482010-06-22 09:14:47 +0000506 * appropriately. LIBMTP_ERROR_NO_DEVICE_ATTACHED implies that no
Linus Walleij9c6c1542007-09-19 13:43:06 +0000507 * devices have been found.
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000508 */
Linus Walleij45a86372007-03-07 09:36:19 +0000509static LIBMTP_error_number_t get_mtp_usb_device_list(mtpdevice_list_t ** mtp_device_list)
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000510{
Linus Walleij7cf02642011-11-16 23:44:58 +0100511 struct usb_bus *bus = init_usb();
512 for (; bus != NULL; bus = bus->next) {
513 struct usb_device *dev = bus->devices;
514 for (; dev != NULL; dev = dev->next) {
515 if (dev->descriptor.bDeviceClass != USB_CLASS_HUB) {
Linus Walleij9c6c1542007-09-19 13:43:06 +0000516 int i;
517 int found = 0;
518
519 // First check if we know about the device already.
520 // Devices well known to us will not have their descriptors
521 // probed, it caused problems with some devices.
Richard Lowe9f205f2007-09-16 16:25:50 +0000522 for(i = 0; i < mtp_device_table_size; i++) {
Linus Walleij7cf02642011-11-16 23:44:58 +0100523 if(dev->descriptor.idVendor == mtp_device_table[i].vendor_id &&
524 dev->descriptor.idProduct == mtp_device_table[i].product_id) {
Richard Lowe9f205f2007-09-16 16:25:50 +0000525 /* Append this usb device to the MTP device list */
Linus Walleij49557482010-06-22 09:14:47 +0000526 *mtp_device_list = append_to_mtpdevice_list(*mtp_device_list,
527 dev,
Linus Walleij7cf02642011-11-16 23:44:58 +0100528 bus->location);
Richard Lowe9f205f2007-09-16 16:25:50 +0000529 found = 1;
530 break;
531 }
532 }
Linus Walleij9c6c1542007-09-19 13:43:06 +0000533 // If we didn't know it, try probing the "OS Descriptor".
Richard Lowe9f205f2007-09-16 16:25:50 +0000534 if (!found) {
Richard Lowe9f205f2007-09-16 16:25:50 +0000535 if (probe_device_descriptor(dev, NULL)) {
536 /* Append this usb device to the MTP USB Device List */
Linus Walleij49557482010-06-22 09:14:47 +0000537 *mtp_device_list = append_to_mtpdevice_list(*mtp_device_list,
Linus Walleij63fd1e62008-04-23 23:49:43 +0000538 dev,
Linus Walleij7cf02642011-11-16 23:44:58 +0100539 bus->location);
Richard Lowe9f205f2007-09-16 16:25:50 +0000540 }
Linus Walleijd866d242009-08-23 21:50:39 +0000541 /*
542 * By thomas_-_s: Also append devices that are no MTP but PTP devices
543 * if this is commented out.
544 */
545 /*
546 else {
547 // Check whether the device is no USB hub but a PTP.
548 if ( dev->config != NULL &&dev->config->interface->altsetting->bInterfaceClass == USB_CLASS_PTP && dev->descriptor.bDeviceClass != USB_CLASS_HUB ) {
549 *mtp_device_list = append_to_mtpdevice_list(*mtp_device_list, dev, bus->location);
550 }
551 }
552 */
Richard Lowe9f205f2007-09-16 16:25:50 +0000553 }
Linus Walleij9c6c1542007-09-19 13:43:06 +0000554 }
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000555 }
Linus Walleij7cf02642011-11-16 23:44:58 +0100556 }
Linus Walleij49557482010-06-22 09:14:47 +0000557
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000558 /* If nothing was found we end up here. */
Linus Walleija4942fc2007-03-12 19:23:21 +0000559 if(*mtp_device_list == NULL) {
tedbullock433d2172007-02-23 22:39:12 +0000560 return LIBMTP_ERROR_NO_DEVICE_ATTACHED;
Linus Walleija4942fc2007-03-12 19:23:21 +0000561 }
562 return LIBMTP_ERROR_NONE;
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000563}
564
565/**
Linus Walleij549f49a2010-12-05 14:00:34 +0000566 * Checks if a specific device with a certain bus and device
567 * number has an MTP type device descriptor.
568 *
569 * @param busno the bus number of the device to check
570 * @param deviceno the device number of the device to check
571 * @return 1 if the device is MTP else 0
572 */
573int LIBMTP_Check_Specific_Device(int busno, int devno)
574{
Linus Walleij7cf02642011-11-16 23:44:58 +0100575 struct usb_bus *bus = init_usb();
576 for (; bus != NULL; bus = bus->next) {
577 struct usb_device *dev = bus->devices;
Linus Walleij549f49a2010-12-05 14:00:34 +0000578 if (bus->location != busno)
579 continue;
Linus Walleij7cf02642011-11-16 23:44:58 +0100580
581 for (; dev != NULL; dev = dev->next) {
582
583 if (dev->devnum != devno)
584 continue;
585
586 if (probe_device_descriptor(dev, NULL))
Linus Walleij549f49a2010-12-05 14:00:34 +0000587 return 1;
Linus Walleij7cf02642011-11-16 23:44:58 +0100588 }
Linus Walleij549f49a2010-12-05 14:00:34 +0000589 }
590 return 0;
591}
592
593/**
Linus Walleij63fd1e62008-04-23 23:49:43 +0000594 * Detect the raw MTP device descriptors and return a list of
595 * of the devices found.
Linus Walleij49557482010-06-22 09:14:47 +0000596 *
Linus Walleij63fd1e62008-04-23 23:49:43 +0000597 * @param devices a pointer to a variable that will hold
598 * the list of raw devices found. This may be NULL
599 * on return if the number of detected devices is zero.
600 * The user shall simply <code>free()</code> this
601 * variable when finished with the raw devices,
602 * in order to release memory.
Linus Walleij49557482010-06-22 09:14:47 +0000603 * @param numdevs a pointer to an integer that will hold
Linus Walleij63fd1e62008-04-23 23:49:43 +0000604 * the number of devices in the list. This may
605 * be 0.
606 * @return 0 if successful, any other value means failure.
Linus Walleij1fd2d272006-05-08 09:22:01 +0000607 */
Linus Walleij49557482010-06-22 09:14:47 +0000608LIBMTP_error_number_t LIBMTP_Detect_Raw_Devices(LIBMTP_raw_device_t ** devices,
Linus Walleij63fd1e62008-04-23 23:49:43 +0000609 int * numdevs)
Linus Walleij1fd2d272006-05-08 09:22:01 +0000610{
Linus Walleij63fd1e62008-04-23 23:49:43 +0000611 mtpdevice_list_t *devlist = NULL;
612 mtpdevice_list_t *dev;
613 LIBMTP_error_number_t ret;
Linus Walleij63fd1e62008-04-23 23:49:43 +0000614 LIBMTP_raw_device_t *retdevs;
615 int devs = 0;
Linus Walleijf106aa62008-05-04 22:55:51 +0000616 int i, j;
Linus Walleijb6b69132007-03-07 09:45:05 +0000617
618 ret = get_mtp_usb_device_list(&devlist);
Linus Walleijef2fb362008-05-28 20:59:09 +0000619 if (ret == LIBMTP_ERROR_NO_DEVICE_ATTACHED) {
620 *devices = NULL;
621 *numdevs = 0;
Linus Walleija700d222008-05-28 23:06:14 +0000622 return ret;
Linus Walleijef2fb362008-05-28 20:59:09 +0000623 } else if (ret != LIBMTP_ERROR_NONE) {
nicklas79daadbf22009-09-28 18:19:34 +0000624 LIBMTP_ERROR("LIBMTP PANIC: get_mtp_usb_device_list() "
Linus Walleijf106aa62008-05-04 22:55:51 +0000625 "error code: %d on line %d\n", ret, __LINE__);
Linus Walleija700d222008-05-28 23:06:14 +0000626 return ret;
Linus Walleijb6b69132007-03-07 09:45:05 +0000627 }
Linus Walleijf106aa62008-05-04 22:55:51 +0000628
Linus Walleij63fd1e62008-04-23 23:49:43 +0000629 // Get list size
630 dev = devlist;
631 while (dev != NULL) {
632 devs++;
633 dev = dev->next;
634 }
635 if (devs == 0) {
636 *devices = NULL;
637 *numdevs = 0;
Linus Walleija700d222008-05-28 23:06:14 +0000638 return LIBMTP_ERROR_NONE;
Linus Walleij63fd1e62008-04-23 23:49:43 +0000639 }
640 // Conjure a device list
641 retdevs = (LIBMTP_raw_device_t *) malloc(sizeof(LIBMTP_raw_device_t) * devs);
642 if (retdevs == NULL) {
643 // Out of memory
644 *devices = NULL;
645 *numdevs = 0;
Linus Walleija700d222008-05-28 23:06:14 +0000646 return LIBMTP_ERROR_MEMORY_ALLOCATION;
Linus Walleij63fd1e62008-04-23 23:49:43 +0000647 }
648 dev = devlist;
649 i = 0;
650 while (dev != NULL) {
Linus Walleijf106aa62008-05-04 22:55:51 +0000651 int device_known = 0;
652
Linus Walleij63fd1e62008-04-23 23:49:43 +0000653 // Assign default device info
654 retdevs[i].device_entry.vendor = NULL;
Linus Walleij7cf02642011-11-16 23:44:58 +0100655 retdevs[i].device_entry.vendor_id = dev->libusb_device->descriptor.idVendor;
Linus Walleij63fd1e62008-04-23 23:49:43 +0000656 retdevs[i].device_entry.product = NULL;
Linus Walleij7cf02642011-11-16 23:44:58 +0100657 retdevs[i].device_entry.product_id = dev->libusb_device->descriptor.idProduct;
Linus Walleij63fd1e62008-04-23 23:49:43 +0000658 retdevs[i].device_entry.device_flags = 0x00000000U;
Linus Walleijfec4d562008-06-01 22:30:36 +0000659 // See if we can locate some additional vendor info and device flags
Linus Walleijf106aa62008-05-04 22:55:51 +0000660 for(j = 0; j < mtp_device_table_size; j++) {
Linus Walleij7cf02642011-11-16 23:44:58 +0100661 if(dev->libusb_device->descriptor.idVendor == mtp_device_table[j].vendor_id &&
662 dev->libusb_device->descriptor.idProduct == mtp_device_table[j].product_id) {
Linus Walleijf106aa62008-05-04 22:55:51 +0000663 device_known = 1;
664 retdevs[i].device_entry.vendor = mtp_device_table[j].vendor;
665 retdevs[i].device_entry.product = mtp_device_table[j].product;
666 retdevs[i].device_entry.device_flags = mtp_device_table[j].device_flags;
nicklas79daadbf22009-09-28 18:19:34 +0000667
Linus Walleijf106aa62008-05-04 22:55:51 +0000668 // This device is known to the developers
Linus Walleij49557482010-06-22 09:14:47 +0000669 LIBMTP_ERROR("Device %d (VID=%04x and PID=%04x) is a %s %s.\n",
Linus Walleijf106aa62008-05-04 22:55:51 +0000670 i,
Linus Walleij7cf02642011-11-16 23:44:58 +0100671 dev->libusb_device->descriptor.idVendor,
672 dev->libusb_device->descriptor.idProduct,
Linus Walleijf106aa62008-05-04 22:55:51 +0000673 mtp_device_table[j].vendor,
674 mtp_device_table[j].product);
Linus Walleijf106aa62008-05-04 22:55:51 +0000675 break;
676 }
677 }
678 if (!device_known) {
Andrés G. Aragoneses524e42c2015-03-31 02:35:21 +0200679 device_unknown(i,
680 dev->libusb_device->descriptor.idVendor,
681 dev->libusb_device->descriptor.idProduct);
Linus Walleijf106aa62008-05-04 22:55:51 +0000682 }
683 // Save the location on the bus
Linus Walleij7cf02642011-11-16 23:44:58 +0100684 retdevs[i].bus_location = dev->bus_location;
685 retdevs[i].devnum = dev->libusb_device->devnum;
Linus Walleij63fd1e62008-04-23 23:49:43 +0000686 i++;
687 dev = dev->next;
Linus Walleij49557482010-06-22 09:14:47 +0000688 }
Linus Walleij63fd1e62008-04-23 23:49:43 +0000689 *devices = retdevs;
690 *numdevs = i;
Linus Walleijb6b69132007-03-07 09:45:05 +0000691 free_mtpdevice_list(devlist);
Linus Walleija700d222008-05-28 23:06:14 +0000692 return LIBMTP_ERROR_NONE;
Linus Walleij1fd2d272006-05-08 09:22:01 +0000693}
694
Linus Walleijc6210fb2006-05-08 11:11:41 +0000695/**
696 * This routine just dumps out low-level
697 * USB information about the current device.
698 * @param ptp_usb the USB device to get information from.
699 */
700void dump_usbinfo(PTP_USB *ptp_usb)
701{
Linus Walleij7cf02642011-11-16 23:44:58 +0100702 struct usb_device *dev;
Linus Walleijc6210fb2006-05-08 11:11:41 +0000703
Linus Walleij7cf02642011-11-16 23:44:58 +0100704#ifdef LIBUSB_HAS_GET_DRIVER_NP
705 char devname[0x10];
706 int res;
Linus Walleij49557482010-06-22 09:14:47 +0000707
Linus Walleij7cf02642011-11-16 23:44:58 +0100708 devname[0] = '\0';
709 res = usb_get_driver_np(ptp_usb->handle, (int) ptp_usb->interface, devname, sizeof(devname));
710 if (devname[0] != '\0') {
711 LIBMTP_INFO(" Using kernel interface \"%s\"\n", devname);
712 }
713#endif
714 dev = usb_device(ptp_usb->handle);
715 LIBMTP_INFO(" bcdUSB: %d\n", dev->descriptor.bcdUSB);
716 LIBMTP_INFO(" bDeviceClass: %d\n", dev->descriptor.bDeviceClass);
717 LIBMTP_INFO(" bDeviceSubClass: %d\n", dev->descriptor.bDeviceSubClass);
718 LIBMTP_INFO(" bDeviceProtocol: %d\n", dev->descriptor.bDeviceProtocol);
719 LIBMTP_INFO(" idVendor: %04x\n", dev->descriptor.idVendor);
720 LIBMTP_INFO(" idProduct: %04x\n", dev->descriptor.idProduct);
nicklas79daadbf22009-09-28 18:19:34 +0000721 LIBMTP_INFO(" IN endpoint maxpacket: %d bytes\n", ptp_usb->inep_maxpacket);
722 LIBMTP_INFO(" OUT endpoint maxpacket: %d bytes\n", ptp_usb->outep_maxpacket);
723 LIBMTP_INFO(" Raw device info:\n");
724 LIBMTP_INFO(" Bus location: %d\n", ptp_usb->rawdevice.bus_location);
725 LIBMTP_INFO(" Device number: %d\n", ptp_usb->rawdevice.devnum);
726 LIBMTP_INFO(" Device entry info:\n");
727 LIBMTP_INFO(" Vendor: %s\n", ptp_usb->rawdevice.device_entry.vendor);
728 LIBMTP_INFO(" Vendor id: 0x%04x\n", ptp_usb->rawdevice.device_entry.vendor_id);
729 LIBMTP_INFO(" Product: %s\n", ptp_usb->rawdevice.device_entry.product);
730 LIBMTP_INFO(" Vendor id: 0x%04x\n", ptp_usb->rawdevice.device_entry.product_id);
731 LIBMTP_INFO(" Device flags: 0x%08x\n", ptp_usb->rawdevice.device_entry.device_flags);
Linus Walleij9521e2b2007-07-10 21:50:42 +0000732 (void) probe_device_descriptor(dev, stdout);
Linus Walleijc6210fb2006-05-08 11:11:41 +0000733}
734
Linus Walleija4e6bdc2008-05-23 22:31:53 +0000735/**
736 * Retrieve the apropriate playlist extension for this
737 * device. Rather hacky at the moment. This is probably
738 * desired by the managing software, but when creating
739 * lists on the device itself you notice certain preferences.
740 * @param ptp_usb the USB device to get suggestion for.
741 * @return the suggested playlist extension.
742 */
Richard Lowe029eba2009-04-11 12:36:25 +0000743const char *get_playlist_extension(PTP_USB *ptp_usb)
Linus Walleija4e6bdc2008-05-23 22:31:53 +0000744{
Linus Walleij7cf02642011-11-16 23:44:58 +0100745 struct usb_device *dev;
Linus Walleija4e6bdc2008-05-23 22:31:53 +0000746 static char creative_pl_extension[] = ".zpl";
747 static char default_pl_extension[] = ".pla";
748
Linus Walleij7cf02642011-11-16 23:44:58 +0100749 dev = usb_device(ptp_usb->handle);
750 if (dev->descriptor.idVendor == 0x041e) {
Linus Walleija4e6bdc2008-05-23 22:31:53 +0000751 return creative_pl_extension;
Linus Walleij7cf02642011-11-16 23:44:58 +0100752 }
Linus Walleija4e6bdc2008-05-23 22:31:53 +0000753 return default_pl_extension;
754}
755
Linus Walleijd1e14e02008-12-14 01:07:30 +0000756static void
757libusb_glue_debug (PTPParams *params, const char *format, ...)
Linus Walleij14735952010-04-25 04:56:49 +0000758{
Linus Walleija0323272007-01-07 12:21:30 +0000759 va_list args;
760
761 va_start (args, format);
762 if (params->debug_func!=NULL)
763 params->debug_func (params->data, format, args);
764 else
765 {
766 vfprintf (stderr, format, args);
767 fprintf (stderr,"\n");
768 fflush (stderr);
769 }
770 va_end (args);
Linus Walleij14735952010-04-25 04:56:49 +0000771}
Linus Walleija0323272007-01-07 12:21:30 +0000772
773static void
Linus Walleijd1e14e02008-12-14 01:07:30 +0000774libusb_glue_error (PTPParams *params, const char *format, ...)
Linus Walleij49557482010-06-22 09:14:47 +0000775{
Linus Walleija0323272007-01-07 12:21:30 +0000776 va_list args;
777
778 va_start (args, format);
779 if (params->error_func!=NULL)
780 params->error_func (params->data, format, args);
781 else
782 {
783 vfprintf (stderr, format, args);
784 fprintf (stderr,"\n");
785 fflush (stderr);
786 }
787 va_end (args);
788}
789
Linus Walleijc6210fb2006-05-08 11:11:41 +0000790
Linus Walleij99ed83c2007-01-02 18:46:02 +0000791/*
792 * ptp_read_func() and ptp_write_func() are
Linus Walleijc0a11432007-03-02 21:21:18 +0000793 * based on same functions usb.c in libgphoto2.
Linus Walleij99ed83c2007-01-02 18:46:02 +0000794 * Much reading packet logs and having fun with trials and errors
795 * reveals that WMP / Windows is probably using an algorithm like this
796 * for large transfers:
797 *
Linus Walleij49557482010-06-22 09:14:47 +0000798 * 1. Send the command (0x0c bytes) if headers are split, else, send
Linus Walleij99ed83c2007-01-02 18:46:02 +0000799 * command plus sizeof(endpoint) - 0x0c bytes.
800 * 2. Send first packet, max size to be sizeof(endpoint) but only when using
801 * split headers. Else goto 3.
802 * 3. REPEAT send 0x10000 byte chunks UNTIL remaining bytes < 0x10000
803 * We call 0x10000 CONTEXT_BLOCK_SIZE.
804 * 4. Send remaining bytes MOD sizeof(endpoint)
805 * 5. Send remaining bytes. If this happens to be exactly sizeof(endpoint)
806 * then also send a zero-length package.
Linus Walleij049f50c2007-03-03 20:30:43 +0000807 *
808 * Further there is some special quirks to handle zero reads from the
809 * device, since some devices can't do them at all due to shortcomings
810 * of the USB slave controller in the device.
Linus Walleij99ed83c2007-01-02 18:46:02 +0000811 */
Richard Low4fd59d62007-03-03 16:34:37 +0000812#define CONTEXT_BLOCK_SIZE_1 0x3e00
813#define CONTEXT_BLOCK_SIZE_2 0x200
814#define CONTEXT_BLOCK_SIZE CONTEXT_BLOCK_SIZE_1+CONTEXT_BLOCK_SIZE_2
Yingxi Yu4a9e6042013-11-06 04:08:33 +0100815
Linus Walleijb02a0662006-04-25 08:05:09 +0000816static short
Linus Walleij784ac3f2006-12-29 10:36:51 +0000817ptp_read_func (
818 unsigned long size, PTPDataHandler *handler,void *data,
Richard Low4df32f82007-01-11 20:04:39 +0000819 unsigned long *readbytes,
820 int readzero
Linus Walleij784ac3f2006-12-29 10:36:51 +0000821) {
Linus Walleijb02a0662006-04-25 08:05:09 +0000822 PTP_USB *ptp_usb = (PTP_USB *)data;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000823 unsigned long toread = 0;
Linus Walleij7cf02642011-11-16 23:44:58 +0100824 int result = 0;
Linus Walleij99ed83c2007-01-02 18:46:02 +0000825 unsigned long curread = 0;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000826 unsigned char *bytes;
Linus Walleij049f50c2007-03-03 20:30:43 +0000827 int expect_terminator_byte = 0;
Yingxi Yu4a9e6042013-11-06 04:08:33 +0100828 unsigned long usb_inep_maxpacket_size;
829 unsigned long context_block_size_1;
830 unsigned long context_block_size_2;
831 uint16_t ptp_dev_vendor_id = ptp_usb->rawdevice.device_entry.vendor_id;
832
833 //"iRiver" device special handling
834 if (ptp_dev_vendor_id == 0x4102 || ptp_dev_vendor_id == 0x1006) {
835 usb_inep_maxpacket_size = ptp_usb->inep_maxpacket;
836 if (usb_inep_maxpacket_size == 0x400) {
837 context_block_size_1 = CONTEXT_BLOCK_SIZE_1 - 0x200;
838 context_block_size_2 = CONTEXT_BLOCK_SIZE_2 + 0x200;
839 }
840 else {
841 context_block_size_1 = CONTEXT_BLOCK_SIZE_1;
842 context_block_size_2 = CONTEXT_BLOCK_SIZE_2;
843 }
844 }
Linus Walleij99ed83c2007-01-02 18:46:02 +0000845
846 // This is the largest block we'll need to read in.
Linus Walleij784ac3f2006-12-29 10:36:51 +0000847 bytes = malloc(CONTEXT_BLOCK_SIZE);
Linus Walleijd6a49972006-05-02 08:24:54 +0000848 while (curread < size) {
nicklas79daadbf22009-09-28 18:19:34 +0000849
850 LIBMTP_USB_DEBUG("Remaining size to read: 0x%04lx bytes\n", size - curread);
851
Richard Low4fd59d62007-03-03 16:34:37 +0000852 // check equal to condition here
853 if (size - curread < CONTEXT_BLOCK_SIZE)
854 {
855 // this is the last packet
856 toread = size - curread;
857 // this is equivalent to zero read for these devices
Linus Walleijfec4d562008-06-01 22:30:36 +0000858 if (readzero && FLAG_NO_ZERO_READS(ptp_usb) && toread % 64 == 0) {
Richard Low4fd59d62007-03-03 16:34:37 +0000859 toread += 1;
Linus Walleij049f50c2007-03-03 20:30:43 +0000860 expect_terminator_byte = 1;
Richard Low4fd59d62007-03-03 16:34:37 +0000861 }
Linus Walleijc49c6372007-01-09 00:18:51 +0000862 }
Yingxi Yu4a9e6042013-11-06 04:08:33 +0100863 else if (ptp_dev_vendor_id == 0x4102 || ptp_dev_vendor_id == 0x1006) {
864 //"iRiver" device special handling
865 if (curread == 0)
866 // we are first packet, but not last packet
867 toread = context_block_size_1;
868 else if (toread == context_block_size_1)
869 toread = context_block_size_2;
870 else if (toread == context_block_size_2)
871 toread = context_block_size_1;
872 else
873 LIBMTP_INFO("unexpected toread size 0x%04x, 0x%04x remaining bytes\n",
874 (unsigned int) toread, (unsigned int) (size-curread));
875
876 } else
877 toread = CONTEXT_BLOCK_SIZE;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000878
nicklas79daadbf22009-09-28 18:19:34 +0000879 LIBMTP_USB_DEBUG("Reading in 0x%04lx bytes\n", toread);
880
Linus Walleij7cf02642011-11-16 23:44:58 +0100881 result = USB_BULK_READ(ptp_usb->handle,
Linus Walleije04a1b92011-03-09 18:00:24 +0100882 ptp_usb->inep,
Linus Walleij7cf02642011-11-16 23:44:58 +0100883 (char*) bytes,
Linus Walleije04a1b92011-03-09 18:00:24 +0100884 toread,
885 ptp_usb->timeout);
nicklas79daadbf22009-09-28 18:19:34 +0000886
Linus Walleij7cf02642011-11-16 23:44:58 +0100887 LIBMTP_USB_DEBUG("Result of read: 0x%04x\n", result);
Linus Walleij49557482010-06-22 09:14:47 +0000888
Linus Walleij7cf02642011-11-16 23:44:58 +0100889 if (result < 0) {
Richard Low43fdb882006-09-06 16:19:05 +0000890 return PTP_ERROR_IO;
Linus Walleij7cf02642011-11-16 23:44:58 +0100891 }
nicklas79daadbf22009-09-28 18:19:34 +0000892
893 LIBMTP_USB_DEBUG("<==USB IN\n");
Linus Walleij7cf02642011-11-16 23:44:58 +0100894 if (result == 0)
nicklas79daadbf22009-09-28 18:19:34 +0000895 LIBMTP_USB_DEBUG("Zero Read\n");
Richard Low4df32f82007-01-11 20:04:39 +0000896 else
Linus Walleij7cf02642011-11-16 23:44:58 +0100897 LIBMTP_USB_DATA(bytes, result, 16);
Linus Walleij49557482010-06-22 09:14:47 +0000898
Richard Low4fd59d62007-03-03 16:34:37 +0000899 // want to discard extra byte
Linus Walleij7cf02642011-11-16 23:44:58 +0100900 if (expect_terminator_byte && result == toread)
Richard Lowbf2675c2007-03-18 18:20:26 +0000901 {
nicklas79daadbf22009-09-28 18:19:34 +0000902 LIBMTP_USB_DEBUG("<==USB IN\nDiscarding extra byte\n");
903
Linus Walleij7cf02642011-11-16 23:44:58 +0100904 result--;
Richard Lowbf2675c2007-03-18 18:20:26 +0000905 }
Linus Walleij49557482010-06-22 09:14:47 +0000906
Marcus Meissner3d692dc2016-02-21 12:34:06 +0100907 int putfunc_ret = handler->putfunc(NULL, handler->priv, result, bytes);
Richard Lowd24f9232009-05-03 10:22:08 +0000908 if (putfunc_ret != PTP_RC_OK)
909 return putfunc_ret;
Linus Walleij49557482010-06-22 09:14:47 +0000910
Linus Walleij7cf02642011-11-16 23:44:58 +0100911 ptp_usb->current_transfer_complete += result;
912 curread += result;
Linus Walleijc49c6372007-01-09 00:18:51 +0000913
914 // Increase counters, call callback
915 if (ptp_usb->callback_active) {
916 if (ptp_usb->current_transfer_complete >= ptp_usb->current_transfer_total) {
917 // send last update and disable callback.
918 ptp_usb->current_transfer_complete = ptp_usb->current_transfer_total;
919 ptp_usb->callback_active = 0;
920 }
921 if (ptp_usb->current_transfer_callback != NULL) {
Linus Walleijbd3bf9e2007-09-14 19:31:54 +0000922 int ret;
923 ret = ptp_usb->current_transfer_callback(ptp_usb->current_transfer_complete,
924 ptp_usb->current_transfer_total,
925 ptp_usb->current_transfer_callback_data);
926 if (ret != 0) {
927 return PTP_ERROR_CANCEL;
928 }
Linus Walleijc49c6372007-01-09 00:18:51 +0000929 }
Linus Walleij49557482010-06-22 09:14:47 +0000930 }
Linus Walleijc49c6372007-01-09 00:18:51 +0000931
Linus Walleij7cf02642011-11-16 23:44:58 +0100932 if (result < toread) /* short reads are common */
Linus Walleijd6a49972006-05-02 08:24:54 +0000933 break;
934 }
Linus Walleij784ac3f2006-12-29 10:36:51 +0000935 if (readbytes) *readbytes = curread;
936 free (bytes);
Linus Walleij49557482010-06-22 09:14:47 +0000937
Richard Low4df32f82007-01-11 20:04:39 +0000938 // there might be a zero packet waiting for us...
Linus Walleij49557482010-06-22 09:14:47 +0000939 if (readzero &&
940 !FLAG_NO_ZERO_READS(ptp_usb) &&
Linus Walleij049f50c2007-03-03 20:30:43 +0000941 curread % ptp_usb->outep_maxpacket == 0) {
Linus Walleij7cf02642011-11-16 23:44:58 +0100942 char temp;
943 int zeroresult = 0;
Linus Walleij049f50c2007-03-03 20:30:43 +0000944
nicklas79daadbf22009-09-28 18:19:34 +0000945 LIBMTP_USB_DEBUG("<==USB IN\n");
946 LIBMTP_USB_DEBUG("Zero Read\n");
947
Linus Walleije04a1b92011-03-09 18:00:24 +0100948 zeroresult = USB_BULK_READ(ptp_usb->handle,
949 ptp_usb->inep,
950 &temp,
951 0,
952 ptp_usb->timeout);
Linus Walleij7cf02642011-11-16 23:44:58 +0100953 if (zeroresult != 0)
nicklas79daadbf22009-09-28 18:19:34 +0000954 LIBMTP_INFO("LIBMTP panic: unable to read in zero packet, response 0x%04x", zeroresult);
Richard Low4df32f82007-01-11 20:04:39 +0000955 }
Linus Walleij49557482010-06-22 09:14:47 +0000956
Richard Low4d9165f2008-09-23 20:13:17 +0000957 return PTP_RC_OK;
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000958}
959
Linus Walleijb02a0662006-04-25 08:05:09 +0000960static short
Linus Walleij784ac3f2006-12-29 10:36:51 +0000961ptp_write_func (
962 unsigned long size,
963 PTPDataHandler *handler,
964 void *data,
965 unsigned long *written
966) {
Linus Walleijb02a0662006-04-25 08:05:09 +0000967 PTP_USB *ptp_usb = (PTP_USB *)data;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000968 unsigned long towrite = 0;
Linus Walleij7cf02642011-11-16 23:44:58 +0100969 int result = 0;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000970 unsigned long curwrite = 0;
971 unsigned char *bytes;
Linus Walleij99ed83c2007-01-02 18:46:02 +0000972
Linus Walleij49557482010-06-22 09:14:47 +0000973 // This is the largest block we'll need to read in.
Linus Walleij784ac3f2006-12-29 10:36:51 +0000974 bytes = malloc(CONTEXT_BLOCK_SIZE);
Linus Walleijc49c6372007-01-09 00:18:51 +0000975 if (!bytes) {
976 return PTP_ERROR_IO;
977 }
Linus Walleijd6a49972006-05-02 08:24:54 +0000978 while (curwrite < size) {
Linus Walleijb84b3852009-03-04 15:11:08 +0000979 unsigned long usbwritten = 0;
Linus Walleijd6a49972006-05-02 08:24:54 +0000980 towrite = size-curwrite;
Linus Walleijc49c6372007-01-09 00:18:51 +0000981 if (towrite > CONTEXT_BLOCK_SIZE) {
Richard Low43fdb882006-09-06 16:19:05 +0000982 towrite = CONTEXT_BLOCK_SIZE;
Linus Walleijc49c6372007-01-09 00:18:51 +0000983 } else {
984 // This magic makes packets the same size that WMP send them.
985 if (towrite > ptp_usb->outep_maxpacket && towrite % ptp_usb->outep_maxpacket != 0) {
Richard Low021421e2007-01-01 18:08:57 +0000986 towrite -= towrite % ptp_usb->outep_maxpacket;
Linus Walleijc49c6372007-01-09 00:18:51 +0000987 }
988 }
Linus Walleijd866d242009-08-23 21:50:39 +0000989 int getfunc_ret = handler->getfunc(NULL, handler->priv,towrite,bytes,&towrite);
Richard Lowd24f9232009-05-03 10:22:08 +0000990 if (getfunc_ret != PTP_RC_OK)
991 return getfunc_ret;
Linus Walleijb84b3852009-03-04 15:11:08 +0000992 while (usbwritten < towrite) {
Linus Walleij7cf02642011-11-16 23:44:58 +0100993 result = USB_BULK_WRITE(ptp_usb->handle,
Linus Walleije04a1b92011-03-09 18:00:24 +0100994 ptp_usb->outep,
Linus Walleij7cf02642011-11-16 23:44:58 +0100995 ((char*) bytes+usbwritten),
Linus Walleije04a1b92011-03-09 18:00:24 +0100996 towrite-usbwritten,
997 ptp_usb->timeout);
nicklas79daadbf22009-09-28 18:19:34 +0000998
999 LIBMTP_USB_DEBUG("USB OUT==>\n");
Linus Walleij7cf02642011-11-16 23:44:58 +01001000 LIBMTP_USB_DATA(bytes+usbwritten, result, 16);
nicklas79daadbf22009-09-28 18:19:34 +00001001
Linus Walleij7cf02642011-11-16 23:44:58 +01001002 if (result < 0) {
Linus Walleijb84b3852009-03-04 15:11:08 +00001003 return PTP_ERROR_IO;
1004 }
1005 // check for result == 0 perhaps too.
1006 // Increase counters
Linus Walleij7cf02642011-11-16 23:44:58 +01001007 ptp_usb->current_transfer_complete += result;
1008 curwrite += result;
1009 usbwritten += result;
Linus Walleijc49c6372007-01-09 00:18:51 +00001010 }
Linus Walleijc49c6372007-01-09 00:18:51 +00001011 // call callback
1012 if (ptp_usb->callback_active) {
1013 if (ptp_usb->current_transfer_complete >= ptp_usb->current_transfer_total) {
1014 // send last update and disable callback.
1015 ptp_usb->current_transfer_complete = ptp_usb->current_transfer_total;
1016 ptp_usb->callback_active = 0;
1017 }
1018 if (ptp_usb->current_transfer_callback != NULL) {
Linus Walleijbd3bf9e2007-09-14 19:31:54 +00001019 int ret;
1020 ret = ptp_usb->current_transfer_callback(ptp_usb->current_transfer_complete,
1021 ptp_usb->current_transfer_total,
1022 ptp_usb->current_transfer_callback_data);
1023 if (ret != 0) {
1024 return PTP_ERROR_CANCEL;
1025 }
Linus Walleijc49c6372007-01-09 00:18:51 +00001026 }
1027 }
Linus Walleij7cf02642011-11-16 23:44:58 +01001028 if (result < towrite) /* short writes happen */
Linus Walleijd6a49972006-05-02 08:24:54 +00001029 break;
1030 }
Linus Walleij784ac3f2006-12-29 10:36:51 +00001031 free (bytes);
Linus Walleijc49c6372007-01-09 00:18:51 +00001032 if (written) {
1033 *written = curwrite;
Linus Walleij80d134a2006-08-22 21:41:37 +00001034 }
Linus Walleijc49c6372007-01-09 00:18:51 +00001035
Richard Lowef05b892007-01-03 21:18:56 +00001036 // If this is the last transfer send a zero write if required
1037 if (ptp_usb->current_transfer_complete >= ptp_usb->current_transfer_total) {
Richard Low68f45882007-01-03 10:08:31 +00001038 if ((towrite % ptp_usb->outep_maxpacket) == 0) {
nicklas79daadbf22009-09-28 18:19:34 +00001039
1040 LIBMTP_USB_DEBUG("USB OUT==>\n");
1041 LIBMTP_USB_DEBUG("Zero Write\n");
1042
Linus Walleij7cf02642011-11-16 23:44:58 +01001043 result=USB_BULK_WRITE(ptp_usb->handle,
Linus Walleije04a1b92011-03-09 18:00:24 +01001044 ptp_usb->outep,
Linus Walleij7cf02642011-11-16 23:44:58 +01001045 (char *) "x",
Linus Walleije04a1b92011-03-09 18:00:24 +01001046 0,
1047 ptp_usb->timeout);
Linus Walleij7f0c72e2006-09-06 13:01:58 +00001048 }
Linus Walleijd214b9b2006-08-26 22:08:37 +00001049 }
Linus Walleij49557482010-06-22 09:14:47 +00001050
Linus Walleij7cf02642011-11-16 23:44:58 +01001051 if (result < 0)
Linus Walleijd6a49972006-05-02 08:24:54 +00001052 return PTP_ERROR_IO;
1053 return PTP_RC_OK;
Linus Walleijb02a0662006-04-25 08:05:09 +00001054}
1055
Linus Walleija0323272007-01-07 12:21:30 +00001056/* memory data get/put handler */
1057typedef struct {
1058 unsigned char *data;
1059 unsigned long size, curoff;
1060} PTPMemHandlerPrivate;
Linus Walleij784ac3f2006-12-29 10:36:51 +00001061
Linus Walleija0323272007-01-07 12:21:30 +00001062static uint16_t
1063memory_getfunc(PTPParams* params, void* private,
1064 unsigned long wantlen, unsigned char *data,
1065 unsigned long *gotlen
1066) {
1067 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)private;
1068 unsigned long tocopy = wantlen;
1069
1070 if (priv->curoff + tocopy > priv->size)
1071 tocopy = priv->size - priv->curoff;
1072 memcpy (data, priv->data + priv->curoff, tocopy);
1073 priv->curoff += tocopy;
1074 *gotlen = tocopy;
1075 return PTP_RC_OK;
Linus Walleijb02a0662006-04-25 08:05:09 +00001076}
1077
Linus Walleija0323272007-01-07 12:21:30 +00001078static uint16_t
1079memory_putfunc(PTPParams* params, void* private,
Marcus Meissner3d692dc2016-02-21 12:34:06 +01001080 unsigned long sendlen, unsigned char *data
Linus Walleija0323272007-01-07 12:21:30 +00001081) {
1082 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)private;
1083
1084 if (priv->curoff + sendlen > priv->size) {
1085 priv->data = realloc (priv->data, priv->curoff+sendlen);
1086 priv->size = priv->curoff + sendlen;
1087 }
1088 memcpy (priv->data + priv->curoff, data, sendlen);
1089 priv->curoff += sendlen;
Linus Walleija0323272007-01-07 12:21:30 +00001090 return PTP_RC_OK;
1091}
1092
1093/* init private struct for receiving data. */
1094static uint16_t
1095ptp_init_recv_memory_handler(PTPDataHandler *handler) {
1096 PTPMemHandlerPrivate* priv;
1097 priv = malloc (sizeof(PTPMemHandlerPrivate));
Linus Walleijd866d242009-08-23 21:50:39 +00001098 handler->priv = priv;
Linus Walleija0323272007-01-07 12:21:30 +00001099 handler->getfunc = memory_getfunc;
1100 handler->putfunc = memory_putfunc;
1101 priv->data = NULL;
1102 priv->size = 0;
1103 priv->curoff = 0;
1104 return PTP_RC_OK;
1105}
1106
1107/* init private struct and put data in for sending data.
1108 * data is still owned by caller.
1109 */
1110static uint16_t
1111ptp_init_send_memory_handler(PTPDataHandler *handler,
1112 unsigned char *data, unsigned long len
1113) {
1114 PTPMemHandlerPrivate* priv;
1115 priv = malloc (sizeof(PTPMemHandlerPrivate));
1116 if (!priv)
1117 return PTP_RC_GeneralError;
Linus Walleijd866d242009-08-23 21:50:39 +00001118 handler->priv = priv;
Linus Walleija0323272007-01-07 12:21:30 +00001119 handler->getfunc = memory_getfunc;
1120 handler->putfunc = memory_putfunc;
1121 priv->data = data;
1122 priv->size = len;
1123 priv->curoff = 0;
1124 return PTP_RC_OK;
1125}
1126
1127/* free private struct + data */
1128static uint16_t
1129ptp_exit_send_memory_handler (PTPDataHandler *handler) {
Linus Walleijd866d242009-08-23 21:50:39 +00001130 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)handler->priv;
Linus Walleija0323272007-01-07 12:21:30 +00001131 /* data is owned by caller */
1132 free (priv);
1133 return PTP_RC_OK;
1134}
1135
1136/* hand over our internal data to caller */
1137static uint16_t
1138ptp_exit_recv_memory_handler (PTPDataHandler *handler,
1139 unsigned char **data, unsigned long *size
1140) {
Linus Walleijd866d242009-08-23 21:50:39 +00001141 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)handler->priv;
Linus Walleija0323272007-01-07 12:21:30 +00001142 *data = priv->data;
1143 *size = priv->size;
1144 free (priv);
1145 return PTP_RC_OK;
1146}
1147
1148/* send / receive functions */
1149
1150uint16_t
Marcus Meissner3d692dc2016-02-21 12:34:06 +01001151ptp_usb_sendreq (PTPParams* params, PTPContainer* req, int dataphase)
Linus Walleija0323272007-01-07 12:21:30 +00001152{
1153 uint16_t ret;
1154 PTPUSBBulkContainer usbreq;
1155 PTPDataHandler memhandler;
Linus Walleij5ba40402008-05-23 21:36:54 +00001156 unsigned long written = 0;
1157 unsigned long towrite;
nicklas79daadbf22009-09-28 18:19:34 +00001158
Marcus Meissnerf76b10a2017-03-30 16:16:44 +02001159 LIBMTP_USB_DEBUG("REQUEST: 0x%04x, %s\n", req->Code, ptp_get_opcode_name(params, req->Code));
nicklas79daadbf22009-09-28 18:19:34 +00001160
Linus Walleija0323272007-01-07 12:21:30 +00001161 /* build appropriate USB container */
1162 usbreq.length=htod32(PTP_USB_BULK_REQ_LEN-
1163 (sizeof(uint32_t)*(5-req->Nparam)));
1164 usbreq.type=htod16(PTP_USB_CONTAINER_COMMAND);
1165 usbreq.code=htod16(req->Code);
1166 usbreq.trans_id=htod32(req->Transaction_ID);
1167 usbreq.payload.params.param1=htod32(req->Param1);
1168 usbreq.payload.params.param2=htod32(req->Param2);
1169 usbreq.payload.params.param3=htod32(req->Param3);
1170 usbreq.payload.params.param4=htod32(req->Param4);
1171 usbreq.payload.params.param5=htod32(req->Param5);
1172 /* send it to responder */
1173 towrite = PTP_USB_BULK_REQ_LEN-(sizeof(uint32_t)*(5-req->Nparam));
1174 ptp_init_send_memory_handler (&memhandler, (unsigned char*)&usbreq, towrite);
1175 ret=ptp_write_func(
1176 towrite,
1177 &memhandler,
1178 params->data,
1179 &written
1180 );
1181 ptp_exit_send_memory_handler (&memhandler);
Linus Walleijff01cb12007-09-16 19:49:48 +00001182 if (ret!=PTP_RC_OK && ret!=PTP_ERROR_CANCEL) {
Linus Walleija0323272007-01-07 12:21:30 +00001183 ret = PTP_ERROR_IO;
Linus Walleija0323272007-01-07 12:21:30 +00001184 }
Linus Walleij724f0102007-09-17 20:10:12 +00001185 if (written != towrite && ret != PTP_ERROR_CANCEL && ret != PTP_ERROR_IO) {
Linus Walleij49557482010-06-22 09:14:47 +00001186 libusb_glue_error (params,
Linus Walleija0323272007-01-07 12:21:30 +00001187 "PTP: request code 0x%04x sending req wrote only %ld bytes instead of %d",
Richard Lowd284f072007-02-17 08:52:41 +00001188 req->Code, written, towrite
Linus Walleija0323272007-01-07 12:21:30 +00001189 );
1190 ret = PTP_ERROR_IO;
1191 }
1192 return ret;
1193}
1194
1195uint16_t
1196ptp_usb_senddata (PTPParams* params, PTPContainer* ptp,
Linus Walleijb3aaade2013-03-05 21:06:55 +01001197 uint64_t size, PTPDataHandler *handler
Linus Walleija0323272007-01-07 12:21:30 +00001198) {
1199 uint16_t ret;
1200 int wlen, datawlen;
1201 unsigned long written;
1202 PTPUSBBulkContainer usbdata;
Philip Langdale1a5fe0a2013-04-05 14:00:27 -07001203 uint64_t bytes_left_to_transfer;
Linus Walleija0323272007-01-07 12:21:30 +00001204 PTPDataHandler memhandler;
Jerry Zhang6c016412017-06-15 12:42:13 -07001205 unsigned long packet_size;
1206 PTP_USB *ptp_usb = (PTP_USB *) params->data;
1207
1208 packet_size = ptp_usb->inep_maxpacket;
Linus Walleija0323272007-01-07 12:21:30 +00001209
nicklas79daadbf22009-09-28 18:19:34 +00001210
1211 LIBMTP_USB_DEBUG("SEND DATA PHASE\n");
1212
Linus Walleija0323272007-01-07 12:21:30 +00001213 /* build appropriate USB container */
1214 usbdata.length = htod32(PTP_USB_BULK_HDR_LEN+size);
1215 usbdata.type = htod16(PTP_USB_CONTAINER_DATA);
1216 usbdata.code = htod16(ptp->Code);
1217 usbdata.trans_id= htod32(ptp->Transaction_ID);
Linus Walleij14735952010-04-25 04:56:49 +00001218
Linus Walleija0323272007-01-07 12:21:30 +00001219 ((PTP_USB*)params->data)->current_transfer_complete = 0;
Richard Low4eb01122007-02-24 10:11:32 +00001220 ((PTP_USB*)params->data)->current_transfer_total = size+PTP_USB_BULK_HDR_LEN;
Linus Walleija0323272007-01-07 12:21:30 +00001221
1222 if (params->split_header_data) {
1223 datawlen = 0;
1224 wlen = PTP_USB_BULK_HDR_LEN;
1225 } else {
1226 unsigned long gotlen;
1227 /* For all camera devices. */
Richard Low02724f62007-02-17 09:47:26 +00001228 datawlen = (size<PTP_USB_BULK_PAYLOAD_LEN_WRITE)?size:PTP_USB_BULK_PAYLOAD_LEN_WRITE;
Linus Walleija0323272007-01-07 12:21:30 +00001229 wlen = PTP_USB_BULK_HDR_LEN + datawlen;
Linus Walleij14735952010-04-25 04:56:49 +00001230
Linus Walleijd866d242009-08-23 21:50:39 +00001231 ret = handler->getfunc(params, handler->priv, datawlen, usbdata.payload.data, &gotlen);
Linus Walleija0323272007-01-07 12:21:30 +00001232 if (ret != PTP_RC_OK)
1233 return ret;
1234 if (gotlen != datawlen)
1235 return PTP_RC_GeneralError;
1236 }
1237 ptp_init_send_memory_handler (&memhandler, (unsigned char *)&usbdata, wlen);
1238 /* send first part of data */
1239 ret = ptp_write_func(wlen, &memhandler, params->data, &written);
1240 ptp_exit_send_memory_handler (&memhandler);
1241 if (ret!=PTP_RC_OK) {
Linus Walleija0323272007-01-07 12:21:30 +00001242 return ret;
1243 }
1244 if (size <= datawlen) return ret;
1245 /* if everything OK send the rest */
1246 bytes_left_to_transfer = size-datawlen;
1247 ret = PTP_RC_OK;
1248 while(bytes_left_to_transfer > 0) {
Jerry Zhang6c016412017-06-15 12:42:13 -07001249 int max_long_transfer = ULONG_MAX + 1 - packet_size;
1250 ret = ptp_write_func (bytes_left_to_transfer > max_long_transfer ? max_long_transfer : bytes_left_to_transfer,
1251 handler, params->data, &written);
Linus Walleija0323272007-01-07 12:21:30 +00001252 if (ret != PTP_RC_OK)
1253 break;
1254 if (written == 0) {
1255 ret = PTP_ERROR_IO;
1256 break;
1257 }
1258 bytes_left_to_transfer -= written;
1259 }
Linus Walleijff01cb12007-09-16 19:49:48 +00001260 if (ret!=PTP_RC_OK && ret!=PTP_ERROR_CANCEL)
Linus Walleija0323272007-01-07 12:21:30 +00001261 ret = PTP_ERROR_IO;
1262 return ret;
1263}
1264
1265static uint16_t ptp_usb_getpacket(PTPParams *params,
1266 PTPUSBBulkContainer *packet, unsigned long *rlen)
1267{
1268 PTPDataHandler memhandler;
1269 uint16_t ret;
1270 unsigned char *x = NULL;
Yingxi Yu4a9e6042013-11-06 04:08:33 +01001271 unsigned long packet_size;
1272 PTP_USB *ptp_usb = (PTP_USB *) params->data;
1273
1274 packet_size = ptp_usb->inep_maxpacket;
Linus Walleija0323272007-01-07 12:21:30 +00001275
1276 /* read the header and potentially the first data */
1277 if (params->response_packet_size > 0) {
1278 /* If there is a buffered packet, just use it. */
1279 memcpy(packet, params->response_packet, params->response_packet_size);
1280 *rlen = params->response_packet_size;
1281 free(params->response_packet);
1282 params->response_packet = NULL;
1283 params->response_packet_size = 0;
1284 /* Here this signifies a "virtual read" */
1285 return PTP_RC_OK;
1286 }
1287 ptp_init_recv_memory_handler (&memhandler);
Yingxi Yu4a9e6042013-11-06 04:08:33 +01001288 ret = ptp_read_func(packet_size, &memhandler, params->data, rlen, 0);
Linus Walleija0323272007-01-07 12:21:30 +00001289 ptp_exit_recv_memory_handler (&memhandler, &x, rlen);
1290 if (x) {
1291 memcpy (packet, x, *rlen);
1292 free (x);
1293 }
1294 return ret;
1295}
1296
1297uint16_t
1298ptp_usb_getdata (PTPParams* params, PTPContainer* ptp, PTPDataHandler *handler)
1299{
1300 uint16_t ret;
1301 PTPUSBBulkContainer usbdata;
Linus Walleij8e3af002007-09-28 19:21:54 +00001302 PTP_USB *ptp_usb = (PTP_USB *) params->data;
Linus Walleij14735952010-04-25 04:56:49 +00001303 int putfunc_ret;
nicklas79daadbf22009-09-28 18:19:34 +00001304
1305 LIBMTP_USB_DEBUG("GET DATA PHASE\n");
1306
Linus Walleija0323272007-01-07 12:21:30 +00001307 memset(&usbdata,0,sizeof(usbdata));
1308 do {
1309 unsigned long len, rlen;
1310
1311 ret = ptp_usb_getpacket(params, &usbdata, &rlen);
1312 if (ret!=PTP_RC_OK) {
1313 ret = PTP_ERROR_IO;
1314 break;
Linus Walleij8e3af002007-09-28 19:21:54 +00001315 }
Linus Walleija0323272007-01-07 12:21:30 +00001316 if (dtoh16(usbdata.type)!=PTP_USB_CONTAINER_DATA) {
1317 ret = PTP_ERROR_DATA_EXPECTED;
1318 break;
Linus Walleij8e3af002007-09-28 19:21:54 +00001319 }
Linus Walleija0323272007-01-07 12:21:30 +00001320 if (dtoh16(usbdata.code)!=ptp->Code) {
Linus Walleijfec4d562008-06-01 22:30:36 +00001321 if (FLAG_IGNORE_HEADER_ERRORS(ptp_usb)) {
Linus Walleijd1e14e02008-12-14 01:07:30 +00001322 libusb_glue_debug (params, "ptp2/ptp_usb_getdata: detected a broken "
Linus Walleij913a3062007-09-28 21:42:39 +00001323 "PTP header, code field insane, expect problems! (But continuing)");
Linus Walleij06542122007-10-14 22:13:48 +00001324 // Repair the header, so it won't wreak more havoc, don't just ignore it.
1325 // Typically these two fields will be broken.
1326 usbdata.code = htod16(ptp->Code);
1327 usbdata.trans_id = htod32(ptp->Transaction_ID);
Linus Walleij94cd9722007-09-28 21:29:53 +00001328 ret = PTP_RC_OK;
Linus Walleij8e3af002007-09-28 19:21:54 +00001329 } else {
Linus Walleij913a3062007-09-28 21:42:39 +00001330 ret = dtoh16(usbdata.code);
1331 // This filters entirely insane garbage return codes, but still
1332 // makes it possible to return error codes in the code field when
Linus Walleij49557482010-06-22 09:14:47 +00001333 // getting data. It appears Windows ignores the contents of this
Linus Walleij913a3062007-09-28 21:42:39 +00001334 // field entirely.
1335 if (ret < PTP_RC_Undefined || ret > PTP_RC_SpecificationOfDestinationUnsupported) {
Linus Walleijd1e14e02008-12-14 01:07:30 +00001336 libusb_glue_debug (params, "ptp2/ptp_usb_getdata: detected a broken "
Linus Walleij913a3062007-09-28 21:42:39 +00001337 "PTP header, code field insane.");
1338 ret = PTP_ERROR_IO;
1339 }
Linus Walleij8e3af002007-09-28 19:21:54 +00001340 break;
1341 }
Linus Walleija0323272007-01-07 12:21:30 +00001342 }
Marcus Meissneraac75022016-02-05 22:24:26 +01001343 if (rlen == ptp_usb->inep_maxpacket) {
Linus Walleij14735952010-04-25 04:56:49 +00001344 /* Copy first part of data to 'data' */
1345 putfunc_ret =
1346 handler->putfunc(
Marcus Meissner3d692dc2016-02-21 12:34:06 +01001347 params, handler->priv, rlen - PTP_USB_BULK_HDR_LEN, usbdata.payload.data
Linus Walleij14735952010-04-25 04:56:49 +00001348 );
1349 if (putfunc_ret != PTP_RC_OK)
1350 return putfunc_ret;
Linus Walleija0323272007-01-07 12:21:30 +00001351
Linus Walleij14735952010-04-25 04:56:49 +00001352 /* stuff data directly to passed data handler */
1353 while (1) {
1354 unsigned long readdata;
1355 uint16_t xret;
1356
1357 xret = ptp_read_func(
Philip Langdalee6f77032013-04-14 21:57:27 -07001358 0x20000000,
Linus Walleij14735952010-04-25 04:56:49 +00001359 handler,
1360 params->data,
1361 &readdata,
1362 0
1363 );
1364 if (xret != PTP_RC_OK)
1365 return xret;
Philip Langdalee6f77032013-04-14 21:57:27 -07001366 if (readdata < 0x20000000)
Linus Walleij14735952010-04-25 04:56:49 +00001367 break;
1368 }
1369 return PTP_RC_OK;
Linus Walleija0323272007-01-07 12:21:30 +00001370 }
1371 if (rlen > dtoh32(usbdata.length)) {
1372 /*
1373 * Buffer the surplus response packet if it is >=
1374 * PTP_USB_BULK_HDR_LEN
1375 * (i.e. it is probably an entire package)
1376 * else discard it as erroneous surplus data.
1377 * This will even work if more than 2 packets appear
1378 * in the same transaction, they will just be handled
1379 * iteratively.
1380 *
1381 * Marcus observed stray bytes on iRiver devices;
1382 * these are still discarded.
1383 */
1384 unsigned int packlen = dtoh32(usbdata.length);
1385 unsigned int surplen = rlen - packlen;
1386
1387 if (surplen >= PTP_USB_BULK_HDR_LEN) {
1388 params->response_packet = malloc(surplen);
1389 memcpy(params->response_packet,
1390 (uint8_t *) &usbdata + packlen, surplen);
1391 params->response_packet_size = surplen;
tedbullock36f2afb2007-03-03 22:16:44 +00001392 /* Ignore reading one extra byte if device flags have been set */
Linus Walleijfec4d562008-06-01 22:30:36 +00001393 } else if(!FLAG_NO_ZERO_READS(ptp_usb) &&
1394 (rlen - dtoh32(usbdata.length) == 1)) {
Linus Walleijd1e14e02008-12-14 01:07:30 +00001395 libusb_glue_debug (params, "ptp2/ptp_usb_getdata: read %d bytes "
Linus Walleij49557482010-06-22 09:14:47 +00001396 "too much, expect problems!",
Linus Walleij8e3af002007-09-28 19:21:54 +00001397 rlen - dtoh32(usbdata.length));
Linus Walleija0323272007-01-07 12:21:30 +00001398 }
1399 rlen = packlen;
1400 }
1401
1402 /* For most PTP devices rlen is 512 == sizeof(usbdata)
1403 * here. For MTP devices splitting header and data it might
1404 * be 12.
1405 */
1406 /* Evaluate full data length. */
1407 len=dtoh32(usbdata.length)-PTP_USB_BULK_HDR_LEN;
1408
1409 /* autodetect split header/data MTP devices */
1410 if (dtoh32(usbdata.length) > 12 && (rlen==12))
1411 params->split_header_data = 1;
1412
Linus Walleija0323272007-01-07 12:21:30 +00001413 /* Copy first part of data to 'data' */
Linus Walleij14735952010-04-25 04:56:49 +00001414 putfunc_ret =
1415 handler->putfunc(
1416 params, handler->priv, rlen - PTP_USB_BULK_HDR_LEN,
Marcus Meissner3d692dc2016-02-21 12:34:06 +01001417 usbdata.payload.data
Linus Walleij14735952010-04-25 04:56:49 +00001418 );
1419 if (putfunc_ret != PTP_RC_OK)
1420 return putfunc_ret;
Linus Walleijd866d242009-08-23 21:50:39 +00001421
1422 if (FLAG_NO_ZERO_READS(ptp_usb) &&
Marcus Meissneraac75022016-02-05 22:24:26 +01001423 len+PTP_USB_BULK_HDR_LEN == ptp_usb->inep_maxpacket) {
nicklas79daadbf22009-09-28 18:19:34 +00001424
1425 LIBMTP_USB_DEBUG("Reading in extra terminating byte\n");
1426
Linus Walleij049f50c2007-03-03 20:30:43 +00001427 // need to read in extra byte and discard it
Linus Walleij7cf02642011-11-16 23:44:58 +01001428 int result = 0;
1429 char byte = 0;
Linus Walleije04a1b92011-03-09 18:00:24 +01001430 result = USB_BULK_READ(ptp_usb->handle,
1431 ptp_usb->inep,
1432 &byte,
1433 1,
1434 ptp_usb->timeout);
Linus Walleijd866d242009-08-23 21:50:39 +00001435
Linus Walleij049f50c2007-03-03 20:30:43 +00001436 if (result != 1)
Marcus Meissneraac75022016-02-05 22:24:26 +01001437 LIBMTP_INFO("Could not read in extra byte for %d bytes long file, return value 0x%04x\n", ptp_usb->inep_maxpacket, result);
1438 } else if (len+PTP_USB_BULK_HDR_LEN == ptp_usb->inep_maxpacket && params->split_header_data == 0) {
Linus Walleij7cf02642011-11-16 23:44:58 +01001439 int zeroresult = 0;
1440 char zerobyte = 0;
1441
nicklas79daadbf22009-09-28 18:19:34 +00001442
1443 LIBMTP_INFO("Reading in zero packet after header\n");
1444
Linus Walleije04a1b92011-03-09 18:00:24 +01001445 zeroresult = USB_BULK_READ(ptp_usb->handle,
1446 ptp_usb->inep,
1447 &zerobyte,
1448 0,
1449 ptp_usb->timeout);
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001450
Linus Walleij94cd9722007-09-28 21:29:53 +00001451 if (zeroresult != 0)
nicklas79daadbf22009-09-28 18:19:34 +00001452 LIBMTP_INFO("LIBMTP panic: unable to read in zero packet, response 0x%04x", zeroresult);
Linus Walleij94cd9722007-09-28 21:29:53 +00001453 }
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001454
Linus Walleija0323272007-01-07 12:21:30 +00001455 /* Is that all of data? */
Linus Walleij94cd9722007-09-28 21:29:53 +00001456 if (len+PTP_USB_BULK_HDR_LEN<=rlen) {
1457 break;
1458 }
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001459
Linus Walleij913a3062007-09-28 21:42:39 +00001460 ret = ptp_read_func(len - (rlen - PTP_USB_BULK_HDR_LEN),
1461 handler,
1462 params->data, &rlen, 1);
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001463
Linus Walleija0323272007-01-07 12:21:30 +00001464 if (ret!=PTP_RC_OK) {
Linus Walleij049f50c2007-03-03 20:30:43 +00001465 break;
Linus Walleija0323272007-01-07 12:21:30 +00001466 }
1467 } while (0);
Linus Walleija0323272007-01-07 12:21:30 +00001468 return ret;
1469}
1470
1471uint16_t
1472ptp_usb_getresp (PTPParams* params, PTPContainer* resp)
1473{
1474 uint16_t ret;
1475 unsigned long rlen;
1476 PTPUSBBulkContainer usbresp;
Linus Walleij3e4b1142007-11-01 23:54:16 +00001477 PTP_USB *ptp_usb = (PTP_USB *)(params->data);
Linus Walleija0323272007-01-07 12:21:30 +00001478
nicklas79daadbf22009-09-28 18:19:34 +00001479
1480 LIBMTP_USB_DEBUG("RESPONSE: ");
1481
Linus Walleija0323272007-01-07 12:21:30 +00001482 memset(&usbresp,0,sizeof(usbresp));
1483 /* read response, it should never be longer than sizeof(usbresp) */
1484 ret = ptp_usb_getpacket(params, &usbresp, &rlen);
1485
Linus Walleijb8f78c72008-03-02 20:22:56 +00001486 // Fix for bevahiour reported by Scott Snyder on Samsung YP-U3. The player
1487 // sends a packet containing just zeroes of length 2 (up to 4 has been seen too)
1488 // after a NULL packet when it should send the response. This code ignores
1489 // such illegal packets.
Linus Walleij3191bba2008-02-10 21:16:25 +00001490 while (ret==PTP_RC_OK && rlen<PTP_USB_BULK_HDR_LEN && usbresp.length==0) {
Linus Walleijd1e14e02008-12-14 01:07:30 +00001491 libusb_glue_debug (params, "ptp_usb_getresp: detected short response "
Linus Walleij3191bba2008-02-10 21:16:25 +00001492 "of %d bytes, expect problems! (re-reading "
1493 "response), rlen");
Linus Walleij00411ee2008-01-27 12:24:51 +00001494 ret = ptp_usb_getpacket(params, &usbresp, &rlen);
1495 }
1496
Linus Walleija0323272007-01-07 12:21:30 +00001497 if (ret!=PTP_RC_OK) {
1498 ret = PTP_ERROR_IO;
1499 } else
1500 if (dtoh16(usbresp.type)!=PTP_USB_CONTAINER_RESPONSE) {
1501 ret = PTP_ERROR_RESP_EXPECTED;
1502 } else
1503 if (dtoh16(usbresp.code)!=resp->Code) {
1504 ret = dtoh16(usbresp.code);
1505 }
nicklas79daadbf22009-09-28 18:19:34 +00001506
1507 LIBMTP_USB_DEBUG("%04x\n", ret);
1508
Linus Walleija0323272007-01-07 12:21:30 +00001509 if (ret!=PTP_RC_OK) {
Linus Walleijd1e14e02008-12-14 01:07:30 +00001510/* libusb_glue_error (params,
Linus Walleija0323272007-01-07 12:21:30 +00001511 "PTP: request code 0x%04x getting resp error 0x%04x",
1512 resp->Code, ret);*/
1513 return ret;
1514 }
1515 /* build an appropriate PTPContainer */
1516 resp->Code=dtoh16(usbresp.code);
1517 resp->SessionID=params->session_id;
1518 resp->Transaction_ID=dtoh32(usbresp.trans_id);
Linus Walleijfec4d562008-06-01 22:30:36 +00001519 if (FLAG_IGNORE_HEADER_ERRORS(ptp_usb)) {
Linus Walleij3e4b1142007-11-01 23:54:16 +00001520 if (resp->Transaction_ID != params->transaction_id-1) {
Linus Walleijd1e14e02008-12-14 01:07:30 +00001521 libusb_glue_debug (params, "ptp_usb_getresp: detected a broken "
Linus Walleij00411ee2008-01-27 12:24:51 +00001522 "PTP header, transaction ID insane, expect "
1523 "problems! (But continuing)");
Linus Walleij3e4b1142007-11-01 23:54:16 +00001524 // Repair the header, so it won't wreak more havoc.
1525 resp->Transaction_ID = params->transaction_id-1;
1526 }
1527 }
Linus Walleija0323272007-01-07 12:21:30 +00001528 resp->Param1=dtoh32(usbresp.payload.params.param1);
1529 resp->Param2=dtoh32(usbresp.payload.params.param2);
1530 resp->Param3=dtoh32(usbresp.payload.params.param3);
1531 resp->Param4=dtoh32(usbresp.payload.params.param4);
1532 resp->Param5=dtoh32(usbresp.payload.params.param5);
1533 return ret;
1534}
1535
1536/* Event handling functions */
1537
1538/* PTP Events wait for or check mode */
1539#define PTP_EVENT_CHECK 0x0000 /* waits for */
1540#define PTP_EVENT_CHECK_FAST 0x0001 /* checks */
1541
1542static inline uint16_t
1543ptp_usb_event (PTPParams* params, PTPContainer* event, int wait)
1544{
1545 uint16_t ret;
Linus Walleij7cf02642011-11-16 23:44:58 +01001546 int result;
Linus Walleija0323272007-01-07 12:21:30 +00001547 unsigned long rlen;
1548 PTPUSBEventContainer usbevent;
1549 PTP_USB *ptp_usb = (PTP_USB *)(params->data);
1550
1551 memset(&usbevent,0,sizeof(usbevent));
1552
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001553 if ((params==NULL) || (event==NULL))
Linus Walleija0323272007-01-07 12:21:30 +00001554 return PTP_ERROR_BADPARAM;
1555 ret = PTP_RC_OK;
1556 switch(wait) {
1557 case PTP_EVENT_CHECK:
Linus Walleij74339382012-01-18 19:56:13 +01001558 result = USB_BULK_READ(ptp_usb->handle,
Linus Walleije04a1b92011-03-09 18:00:24 +01001559 ptp_usb->intep,
Linus Walleij7cf02642011-11-16 23:44:58 +01001560 (char *) &usbevent,
Linus Walleije04a1b92011-03-09 18:00:24 +01001561 sizeof(usbevent),
Linus Walleijc9c01022011-04-18 07:32:17 +02001562 0);
Linus Walleija0323272007-01-07 12:21:30 +00001563 if (result==0)
Linus Walleije04a1b92011-03-09 18:00:24 +01001564 result = USB_BULK_READ(ptp_usb->handle,
1565 ptp_usb->intep,
Linus Walleij7cf02642011-11-16 23:44:58 +01001566 (char *) &usbevent,
Linus Walleije04a1b92011-03-09 18:00:24 +01001567 sizeof(usbevent),
Linus Walleijc9c01022011-04-18 07:32:17 +02001568 0);
Linus Walleija0323272007-01-07 12:21:30 +00001569 if (result < 0) ret = PTP_ERROR_IO;
1570 break;
1571 case PTP_EVENT_CHECK_FAST:
Linus Walleij74339382012-01-18 19:56:13 +01001572 result = USB_BULK_READ(ptp_usb->handle,
Linus Walleije04a1b92011-03-09 18:00:24 +01001573 ptp_usb->intep,
Linus Walleij7cf02642011-11-16 23:44:58 +01001574 (char *) &usbevent,
Linus Walleije04a1b92011-03-09 18:00:24 +01001575 sizeof(usbevent),
1576 ptp_usb->timeout);
Linus Walleija0323272007-01-07 12:21:30 +00001577 if (result==0)
Linus Walleije04a1b92011-03-09 18:00:24 +01001578 result = USB_BULK_READ(ptp_usb->handle,
1579 ptp_usb->intep,
Linus Walleij7cf02642011-11-16 23:44:58 +01001580 (char *) &usbevent,
Linus Walleije04a1b92011-03-09 18:00:24 +01001581 sizeof(usbevent),
1582 ptp_usb->timeout);
Linus Walleija0323272007-01-07 12:21:30 +00001583 if (result < 0) ret = PTP_ERROR_IO;
1584 break;
1585 default:
1586 ret=PTP_ERROR_BADPARAM;
1587 break;
1588 }
1589 if (ret!=PTP_RC_OK) {
Linus Walleijd1e14e02008-12-14 01:07:30 +00001590 libusb_glue_error (params,
Linus Walleija0323272007-01-07 12:21:30 +00001591 "PTP: reading event an error 0x%04x occurred", ret);
1592 return PTP_ERROR_IO;
1593 }
1594 rlen = result;
1595 if (rlen < 8) {
Linus Walleijd1e14e02008-12-14 01:07:30 +00001596 libusb_glue_error (params,
Linus Walleija0323272007-01-07 12:21:30 +00001597 "PTP: reading event an short read of %ld bytes occurred", rlen);
1598 return PTP_ERROR_IO;
1599 }
1600 /* if we read anything over interrupt endpoint it must be an event */
1601 /* build an appropriate PTPContainer */
1602 event->Code=dtoh16(usbevent.code);
1603 event->SessionID=params->session_id;
1604 event->Transaction_ID=dtoh32(usbevent.trans_id);
1605 event->Param1=dtoh32(usbevent.param1);
1606 event->Param2=dtoh32(usbevent.param2);
1607 event->Param3=dtoh32(usbevent.param3);
1608 return ret;
1609}
1610
1611uint16_t
1612ptp_usb_event_check (PTPParams* params, PTPContainer* event) {
1613
1614 return ptp_usb_event (params, event, PTP_EVENT_CHECK_FAST);
1615}
1616
1617uint16_t
1618ptp_usb_event_wait (PTPParams* params, PTPContainer* event) {
1619
1620 return ptp_usb_event (params, event, PTP_EVENT_CHECK);
1621}
1622
Linus Walleijbd3bf9e2007-09-14 19:31:54 +00001623uint16_t
Philip Langdale0a576512016-04-09 14:22:25 -07001624ptp_usb_event_async (PTPParams* params, PTPEventCbFn cb, void *user_data) {
1625 /* Unsupported */
1626 return PTP_ERROR_CANCEL;
1627}
1628
1629int LIBMTP_Handle_Events_Timeout_Completed(struct timeval *tv, int *completed) {
1630 /* Unsupported */
1631 return -12;
1632}
1633
1634uint16_t
Linus Walleijbd3bf9e2007-09-14 19:31:54 +00001635ptp_usb_control_cancel_request (PTPParams *params, uint32_t transactionid) {
1636 PTP_USB *ptp_usb = (PTP_USB *)(params->data);
1637 int ret;
1638 unsigned char buffer[6];
1639
1640 htod16a(&buffer[0],PTP_EC_CancelTransaction);
1641 htod32a(&buffer[2],transactionid);
Linus Walleij7cf02642011-11-16 23:44:58 +01001642 ret = usb_control_msg(ptp_usb->handle,
1643 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
Linus Walleije04a1b92011-03-09 18:00:24 +01001644 0x64, 0x0000, 0x0000,
Linus Walleij7cf02642011-11-16 23:44:58 +01001645 (char *) buffer,
Linus Walleije04a1b92011-03-09 18:00:24 +01001646 sizeof(buffer),
1647 ptp_usb->timeout);
Linus Walleijbd3bf9e2007-09-14 19:31:54 +00001648 if (ret < sizeof(buffer))
1649 return PTP_ERROR_IO;
1650 return PTP_RC_OK;
1651}
Linus Walleija0323272007-01-07 12:21:30 +00001652
Linus Walleij15d18e32012-08-20 02:00:34 +02001653static int init_ptp_usb(PTPParams* params, PTP_USB* ptp_usb, struct usb_device* dev)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001654{
Linus Walleij7cf02642011-11-16 23:44:58 +01001655 usb_dev_handle *device_handle;
1656 char buf[255];
1657 int usbresult;
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001658
Linus Walleijd6a49972006-05-02 08:24:54 +00001659 params->sendreq_func=ptp_usb_sendreq;
1660 params->senddata_func=ptp_usb_senddata;
1661 params->getresp_func=ptp_usb_getresp;
1662 params->getdata_func=ptp_usb_getdata;
Linus Walleijff01cb12007-09-16 19:49:48 +00001663 params->cancelreq_func=ptp_usb_control_cancel_request;
Linus Walleijd6a49972006-05-02 08:24:54 +00001664 params->data=ptp_usb;
1665 params->transaction_id=0;
Linus Walleij462a5e72007-02-13 06:54:56 +00001666 /*
1667 * This is hardcoded here since we have no devices whatsoever that are BE.
1668 * Change this the day we run into our first BE device (if ever).
1669 */
Linus Walleijd6a49972006-05-02 08:24:54 +00001670 params->byteorder = PTP_DL_LE;
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001671
Linus Walleije04a1b92011-03-09 18:00:24 +01001672 ptp_usb->timeout = get_timeout(ptp_usb);
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001673
Linus Walleij7cf02642011-11-16 23:44:58 +01001674 device_handle = usb_open(dev);
1675 if (!device_handle) {
Yavor Goulishev825ff132011-02-26 00:29:01 +01001676 perror("usb_open()");
1677 return -1;
1678 }
1679 ptp_usb->handle = device_handle;
Linus Walleij15d18e32012-08-20 02:00:34 +02001680
Linus Walleij7cf02642011-11-16 23:44:58 +01001681#ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP
Yavor Goulishev825ff132011-02-26 00:29:01 +01001682 /*
1683 * If this device is known to be wrongfully claimed by other kernel
1684 * drivers (such as mass storage), then try to unload it to make it
1685 * accessible from user space.
1686 */
Linus Walleij7cf02642011-11-16 23:44:58 +01001687 if (FLAG_UNLOAD_DRIVER(ptp_usb)) {
1688 if (usb_get_driver_np(device_handle, (int) ptp_usb->interface,
1689 buf, sizeof(buf)) == 0) {
1690 if (usb_detach_kernel_driver_np(device_handle,
1691 (int) ptp_usb->interface)) {
1692 perror("usb_detach_kernel_driver_np()");
Yavor Goulishev825ff132011-02-26 00:29:01 +01001693 return -1;
Linus Walleij501ba4d2006-10-16 06:17:04 +00001694 }
Linus Walleij7cf02642011-11-16 23:44:58 +01001695 }
Yavor Goulishev825ff132011-02-26 00:29:01 +01001696 }
Linus Walleij7cf02642011-11-16 23:44:58 +01001697#endif
Linus Walleij15d18e32012-08-20 02:00:34 +02001698
1699 /*
1700 * Check if the config is set to something else than what we want
1701 * to use. Only set the configuration if we absolutely have to.
1702 * Also do not bail out if we fail.
1703 */
1704 if (dev->config->bConfigurationValue != ptp_usb->config) {
1705 if (usb_set_configuration(device_handle, dev->config->bConfigurationValue)) {
1706 perror("error in usb_set_configuration()- continuing anyway");
1707 }
Yavor Goulishev825ff132011-02-26 00:29:01 +01001708 }
Linus Walleij15d18e32012-08-20 02:00:34 +02001709
1710 /*
1711 * It seems like on kernel 2.6.31 if we already have it open on another
1712 * pthread in our app, we'll get an error if we try to claim it again,
1713 * but that error is harmless because our process already claimed the interface
1714 */
Linus Walleij7cf02642011-11-16 23:44:58 +01001715 usbresult = usb_claim_interface(device_handle, (int) ptp_usb->interface);
Linus Walleijf2d747a2010-01-19 00:46:18 +00001716
Yavor Goulishev825ff132011-02-26 00:29:01 +01001717 if (usbresult != 0)
1718 fprintf(stderr, "ignoring usb_claim_interface = %d", usbresult);
Linus Walleijf2d747a2010-01-19 00:46:18 +00001719
Linus Walleij15d18e32012-08-20 02:00:34 +02001720 /*
1721 * If the altsetting is set to something different than we want, switch
1722 * it.
1723 *
1724 * FIXME: this seems to cause trouble on the Mac:s so disable it. Retry
1725 * this on the Mac now that it only sets this when the altsetting differs.
1726 */
Yavor Goulishev825ff132011-02-26 00:29:01 +01001727#ifndef __APPLE__
Linus Walleij15d18e32012-08-20 02:00:34 +02001728#if 0 /* Disable this always, no idea on how to handle it */
1729 if (dev->config->interface[].altsetting[] !=
1730 ptp_usb->altsetting) {
1731 fprintf(stderr, "desired altsetting different from current, trying to set altsetting\n");
1732 usbresult = usb_set_altinterface(device_handle, 0);
1733 if (usbresult)
1734 fprintf(stderr, "ignoring error from usb_claim_interface = %d\n", usbresult);
1735 }
1736#endif
Yavor Goulishev825ff132011-02-26 00:29:01 +01001737#endif
Linus Walleijf2d747a2010-01-19 00:46:18 +00001738
Yavor Goulishev825ff132011-02-26 00:29:01 +01001739 if (FLAG_SWITCH_MODE_BLACKBERRY(ptp_usb)) {
1740 int ret;
Linus Walleijb6e06e22009-09-22 22:20:49 +00001741
Yavor Goulishev825ff132011-02-26 00:29:01 +01001742 // FIXME : Only for BlackBerry Storm
1743 // What does it mean? Maybe switch mode...
1744 // This first control message is absolutely necessary
1745 usleep(1000);
Linus Walleij7cf02642011-11-16 23:44:58 +01001746 ret = usb_control_msg(device_handle,
1747 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
Yavor Goulishev825ff132011-02-26 00:29:01 +01001748 0xaa, 0x00, 0x04, buf, 0x40, 1000);
1749 LIBMTP_USB_DEBUG("BlackBerry magic part 1:\n");
1750 LIBMTP_USB_DATA(buf, ret, 16);
nicklas79daadbf22009-09-28 18:19:34 +00001751
Yavor Goulishev825ff132011-02-26 00:29:01 +01001752 usleep(1000);
1753 // This control message is unnecessary
Linus Walleij7cf02642011-11-16 23:44:58 +01001754 ret = usb_control_msg(device_handle,
1755 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
Yavor Goulishev825ff132011-02-26 00:29:01 +01001756 0xa5, 0x00, 0x01, buf, 0x02, 1000);
1757 LIBMTP_USB_DEBUG("BlackBerry magic part 2:\n");
1758 LIBMTP_USB_DATA(buf, ret, 16);
nicklas79daadbf22009-09-28 18:19:34 +00001759
Yavor Goulishev825ff132011-02-26 00:29:01 +01001760 usleep(1000);
1761 // This control message is unnecessary
Linus Walleij7cf02642011-11-16 23:44:58 +01001762 ret = usb_control_msg(device_handle,
1763 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
Yavor Goulishev825ff132011-02-26 00:29:01 +01001764 0xa8, 0x00, 0x01, buf, 0x05, 1000);
1765 LIBMTP_USB_DEBUG("BlackBerry magic part 3:\n");
1766 LIBMTP_USB_DATA(buf, ret, 16);
nicklas79daadbf22009-09-28 18:19:34 +00001767
Yavor Goulishev825ff132011-02-26 00:29:01 +01001768 usleep(1000);
1769 // This control message is unnecessary
Linus Walleij7cf02642011-11-16 23:44:58 +01001770 ret = usb_control_msg(device_handle,
1771 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
Yavor Goulishev825ff132011-02-26 00:29:01 +01001772 0xa8, 0x00, 0x01, buf, 0x11, 1000);
1773 LIBMTP_USB_DEBUG("BlackBerry magic part 4:\n");
1774 LIBMTP_USB_DATA(buf, ret, 16);
nicklas79daadbf22009-09-28 18:19:34 +00001775
Yavor Goulishev825ff132011-02-26 00:29:01 +01001776 usleep(1000);
Linus Walleijd6a49972006-05-02 08:24:54 +00001777 }
Linus Walleij9eb3d312006-08-04 19:25:59 +00001778 return 0;
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001779}
1780
Linus Walleijd6a49972006-05-02 08:24:54 +00001781static void clear_stall(PTP_USB* ptp_usb)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001782{
Linus Walleij58b62792007-01-07 12:38:59 +00001783 uint16_t status;
Linus Walleijd6a49972006-05-02 08:24:54 +00001784 int ret;
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001785
Linus Walleijd6a49972006-05-02 08:24:54 +00001786 /* check the inep status */
Linus Walleij58b62792007-01-07 12:38:59 +00001787 status = 0;
Linus Walleij1fd2d272006-05-08 09:22:01 +00001788 ret = usb_get_endpoint_status(ptp_usb,ptp_usb->inep,&status);
Linus Walleij58b62792007-01-07 12:38:59 +00001789 if (ret<0) {
1790 perror ("inep: usb_get_endpoint_status()");
1791 } else if (status) {
nicklas79daadbf22009-09-28 18:19:34 +00001792 LIBMTP_INFO("Clearing stall on IN endpoint\n");
Linus Walleij7cf02642011-11-16 23:44:58 +01001793 ret = usb_clear_stall_feature(ptp_usb,ptp_usb->inep);
1794 if (ret<0) {
Linus Walleij58b62792007-01-07 12:38:59 +00001795 perror ("usb_clear_stall_feature()");
1796 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001797 }
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001798
Linus Walleijd6a49972006-05-02 08:24:54 +00001799 /* check the outep status */
Linus Walleij58b62792007-01-07 12:38:59 +00001800 status=0;
1801 ret = usb_get_endpoint_status(ptp_usb,ptp_usb->outep,&status);
1802 if (ret<0) {
1803 perror("outep: usb_get_endpoint_status()");
1804 } else if (status) {
nicklas79daadbf22009-09-28 18:19:34 +00001805 LIBMTP_INFO("Clearing stall on OUT endpoint\n");
Linus Walleij7cf02642011-11-16 23:44:58 +01001806 ret = usb_clear_stall_feature(ptp_usb,ptp_usb->outep);
1807 if (ret<0) {
Linus Walleij58b62792007-01-07 12:38:59 +00001808 perror("usb_clear_stall_feature()");
1809 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001810 }
Linus Walleij58b62792007-01-07 12:38:59 +00001811
1812 /* TODO: do we need this for INTERRUPT (ptp_usb->intep) too? */
1813}
1814
1815static void clear_halt(PTP_USB* ptp_usb)
1816{
1817 int ret;
1818
Linus Walleij7cf02642011-11-16 23:44:58 +01001819 ret = usb_clear_halt(ptp_usb->handle,ptp_usb->inep);
Linus Walleij58b62792007-01-07 12:38:59 +00001820 if (ret<0) {
1821 perror("usb_clear_halt() on IN endpoint");
1822 }
Linus Walleij7cf02642011-11-16 23:44:58 +01001823 ret = usb_clear_halt(ptp_usb->handle,ptp_usb->outep);
Linus Walleij58b62792007-01-07 12:38:59 +00001824 if (ret<0) {
1825 perror("usb_clear_halt() on OUT endpoint");
1826 }
Linus Walleij7cf02642011-11-16 23:44:58 +01001827 ret = usb_clear_halt(ptp_usb->handle,ptp_usb->intep);
Linus Walleij58b62792007-01-07 12:38:59 +00001828 if (ret<0) {
1829 perror("usb_clear_halt() on INTERRUPT endpoint");
1830 }
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001831}
1832
Linus Walleijb0ab5482007-08-29 21:08:54 +00001833static void close_usb(PTP_USB* ptp_usb)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001834{
Linus Walleijfec4d562008-06-01 22:30:36 +00001835 if (!FLAG_NO_RELEASE_INTERFACE(ptp_usb)) {
Linus Walleijeabc2312008-02-13 00:02:00 +00001836 /*
1837 * Clear any stalled endpoints
1838 * On misbehaving devices designed for Windows/Mac, quote from:
1839 * http://www2.one-eyed-alien.net/~mdharm/linux-usb/target_offenses.txt
1840 * Device does Bad Things(tm) when it gets a GET_STATUS after CLEAR_HALT
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001841 * (...) Windows, when clearing a stall, only sends the CLEAR_HALT command,
1842 * and presumes that the stall has cleared. Some devices actually choke
1843 * if the CLEAR_HALT is followed by a GET_STATUS (used to determine if the
Linus Walleijeabc2312008-02-13 00:02:00 +00001844 * STALL is persistant or not).
1845 */
Linus Walleij29f03ba2007-09-25 19:22:12 +00001846 clear_stall(ptp_usb);
Marcus Meissner423e1fe2013-04-29 00:00:48 +02001847#if 0
1848 // causes troubles due to a kernel bug in 3.x kernels before/around 3.8
Linus Walleij29f03ba2007-09-25 19:22:12 +00001849 // Clear halts on any endpoints
1850 clear_halt(ptp_usb);
1851 // Added to clear some stuff on the OUT endpoint
1852 // TODO: is this good on the Mac too?
1853 // HINT: some devices may need that you comment these two out too.
Marcus Meissner423e1fe2013-04-29 00:00:48 +02001854#endif
Linus Walleij7cf02642011-11-16 23:44:58 +01001855 usb_resetep(ptp_usb->handle, ptp_usb->outep);
1856 usb_release_interface(ptp_usb->handle, (int) ptp_usb->interface);
Linus Walleij29f03ba2007-09-25 19:22:12 +00001857 }
Linus Walleijf6913172011-03-10 22:10:26 +01001858 if (FLAG_FORCE_RESET_ON_CLOSE(ptp_usb)) {
1859 /*
1860 * Some devices really love to get reset after being
1861 * disconnected. Again, since Windows never disconnects
1862 * a device closing behaviour is seldom or never exercised
1863 * on devices when engineered and often error prone.
1864 * Reset may help some.
1865 */
Linus Walleij7cf02642011-11-16 23:44:58 +01001866 usb_reset(ptp_usb->handle);
Linus Walleijf6913172011-03-10 22:10:26 +01001867 }
Linus Walleij7cf02642011-11-16 23:44:58 +01001868 usb_close(ptp_usb->handle);
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001869}
1870
Linus Walleij550d6d52007-01-24 14:32:51 +00001871/**
Linus Walleija700d222008-05-28 23:06:14 +00001872 * Self-explanatory?
1873 */
Linus Walleij7cf02642011-11-16 23:44:58 +01001874static int find_interface_and_endpoints(struct usb_device *dev,
Linus Walleij15d18e32012-08-20 02:00:34 +02001875 uint8_t *conf,
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001876 uint8_t *interface,
Linus Walleij15d18e32012-08-20 02:00:34 +02001877 uint8_t *altsetting,
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001878 int* inep,
1879 int* inep_maxpacket,
1880 int* outep,
1881 int *outep_maxpacket,
1882 int* intep)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001883{
Linus Walleij15d18e32012-08-20 02:00:34 +02001884 uint8_t i;
Linus Walleijd7373cd2007-08-28 21:42:05 +00001885
1886 // Loop over the device configurations
Linus Walleij7cf02642011-11-16 23:44:58 +01001887 for (i = 0; i < dev->descriptor.bNumConfigurations; i++) {
Linus Walleijb0ab5482007-08-29 21:08:54 +00001888 uint8_t j;
Linus Walleijd7373cd2007-08-28 21:42:05 +00001889
Linus Walleij15d18e32012-08-20 02:00:34 +02001890 *conf = dev->config->bConfigurationValue;;
1891
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001892 // Loop over each configurations interfaces
Linus Walleij7cf02642011-11-16 23:44:58 +01001893 for (j = 0; j < dev->config[i].bNumInterfaces; j++) {
Linus Walleij15d18e32012-08-20 02:00:34 +02001894 uint8_t k, l;
Linus Walleijb0ab5482007-08-29 21:08:54 +00001895 uint8_t no_ep;
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001896 int found_inep = 0;
1897 int found_outep = 0;
1898 int found_intep = 0;
Linus Walleij7cf02642011-11-16 23:44:58 +01001899 struct usb_endpoint_descriptor *ep;
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001900
Linus Walleij15d18e32012-08-20 02:00:34 +02001901 // Inspect the altsettings of this interface
1902 for (k = 0; k < dev->config[i].interface[j].num_altsetting; k++) {
Linus Walleijb0ab5482007-08-29 21:08:54 +00001903
Linus Walleij15d18e32012-08-20 02:00:34 +02001904 // MTP devices shall have 3 endpoints, ignore those interfaces
1905 // that haven't.
1906 no_ep = dev->config[i].interface[j].altsetting[k].bNumEndpoints;
1907 if (no_ep != 3)
1908 continue;
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001909
Linus Walleij15d18e32012-08-20 02:00:34 +02001910 *interface = dev->config[i].interface[j].altsetting[k].bInterfaceNumber;
1911 *altsetting = dev->config[i].interface[j].altsetting[k].bAlternateSetting;
1912 ep = dev->config[i].interface[j].altsetting[k].endpoint;
1913
1914 // Loop over the three endpoints to locate two bulk and
1915 // one interrupt endpoint and FAIL if we cannot, and continue.
1916 for (l = 0; l < no_ep; l++) {
1917 if (ep[l].bmAttributes == USB_ENDPOINT_TYPE_BULK) {
1918 if ((ep[l].bEndpointAddress & USB_ENDPOINT_DIR_MASK) ==
1919 USB_ENDPOINT_DIR_MASK) {
1920 *inep = ep[l].bEndpointAddress;
1921 *inep_maxpacket = ep[l].wMaxPacketSize;
1922 found_inep = 1;
1923 }
1924 if ((ep[l].bEndpointAddress & USB_ENDPOINT_DIR_MASK) == 0) {
1925 *outep = ep[l].bEndpointAddress;
1926 *outep_maxpacket = ep[l].wMaxPacketSize;
1927 found_outep = 1;
1928 }
1929 } else if (ep[l].bmAttributes == USB_ENDPOINT_TYPE_INTERRUPT) {
1930 if ((ep[l].bEndpointAddress & USB_ENDPOINT_DIR_MASK) ==
1931 USB_ENDPOINT_DIR_MASK) {
1932 *intep = ep[l].bEndpointAddress;
1933 found_intep = 1;
1934 }
Linus Walleije73854e2010-06-22 09:07:34 +00001935 }
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001936 }
Linus Walleij15d18e32012-08-20 02:00:34 +02001937 if (found_inep && found_outep && found_intep)
1938 // We assigned the endpoints so return here.
1939 return 0;
1940 // Else loop to next interface/config
1941 } /* Next altsetting */
1942 } /* Next interface */
1943 } /* Next config */
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001944 return -1;
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001945}
1946
Linus Walleija700d222008-05-28 23:06:14 +00001947/**
1948 * This function assigns params and usbinfo given a raw device
1949 * as input.
1950 * @param device the device to be assigned.
Linus Walleija700d222008-05-28 23:06:14 +00001951 * @param usbinfo a pointer to the new usbinfo.
1952 * @return an error code.
1953 */
Linus Walleij49557482010-06-22 09:14:47 +00001954LIBMTP_error_number_t configure_usb_device(LIBMTP_raw_device_t *device,
Linus Walleij3c643252008-05-31 23:22:28 +00001955 PTPParams *params,
Linus Walleija700d222008-05-28 23:06:14 +00001956 void **usbinfo)
1957{
Linus Walleija700d222008-05-28 23:06:14 +00001958 PTP_USB *ptp_usb;
Linus Walleij7cf02642011-11-16 23:44:58 +01001959 struct usb_device *libusb_device;
Linus Walleija700d222008-05-28 23:06:14 +00001960 uint16_t ret = 0;
Linus Walleij7cf02642011-11-16 23:44:58 +01001961 struct usb_bus *bus;
1962 int found = 0;
1963 int err;
Linus Walleija700d222008-05-28 23:06:14 +00001964
1965 /* See if we can find this raw device again... */
Linus Walleij7cf02642011-11-16 23:44:58 +01001966 bus = init_usb();
1967 for (; bus != NULL; bus = bus->next) {
1968 if (bus->location == device->bus_location) {
1969 struct usb_device *dev = bus->devices;
Linus Walleija700d222008-05-28 23:06:14 +00001970
Linus Walleij7cf02642011-11-16 23:44:58 +01001971 for (; dev != NULL; dev = dev->next) {
1972 if(dev->devnum == device->devnum &&
1973 dev->descriptor.idVendor == device->device_entry.vendor_id &&
1974 dev->descriptor.idProduct == device->device_entry.product_id ) {
1975 libusb_device = dev;
Linus Walleija700d222008-05-28 23:06:14 +00001976 found = 1;
1977 break;
Linus Walleij7cf02642011-11-16 23:44:58 +01001978 }
1979 }
1980 if (found)
1981 break;
Linus Walleija700d222008-05-28 23:06:14 +00001982 }
1983 }
1984 /* Device has gone since detecting raw devices! */
1985 if (!found) {
1986 return LIBMTP_ERROR_NO_DEVICE_ATTACHED;
1987 }
1988
1989 /* Allocate structs */
Linus Walleija700d222008-05-28 23:06:14 +00001990 ptp_usb = (PTP_USB *) malloc(sizeof(PTP_USB));
Linus Walleij3c643252008-05-31 23:22:28 +00001991 if (ptp_usb == NULL) {
Linus Walleija700d222008-05-28 23:06:14 +00001992 return LIBMTP_ERROR_MEMORY_ALLOCATION;
1993 }
1994 /* Start with a blank slate (includes setting device_flags to 0) */
Linus Walleija700d222008-05-28 23:06:14 +00001995 memset(ptp_usb, 0, sizeof(PTP_USB));
1996
Linus Walleijfec4d562008-06-01 22:30:36 +00001997 /* Copy the raw device */
1998 memcpy(&ptp_usb->rawdevice, device, sizeof(LIBMTP_raw_device_t));
Linus Walleij335a81c2008-06-02 23:01:00 +00001999
2000 /*
2001 * Some devices must have their "OS Descriptor" massaged in order
2002 * to work.
2003 */
2004 if (FLAG_ALWAYS_PROBE_DESCRIPTOR(ptp_usb)) {
2005 // Massage the device descriptor
Linus Walleij7cf02642011-11-16 23:44:58 +01002006 (void) probe_device_descriptor(libusb_device, NULL);
Linus Walleij335a81c2008-06-02 23:01:00 +00002007 }
Linus Walleij5f3c44b2009-09-12 21:03:06 +00002008
2009 /* Assign interface and endpoints to usbinfo... */
Linus Walleij7cf02642011-11-16 23:44:58 +01002010 err = find_interface_and_endpoints(libusb_device,
Linus Walleij15d18e32012-08-20 02:00:34 +02002011 &ptp_usb->config,
Linus Walleij5f3c44b2009-09-12 21:03:06 +00002012 &ptp_usb->interface,
Linus Walleij15d18e32012-08-20 02:00:34 +02002013 &ptp_usb->altsetting,
Linus Walleij5f3c44b2009-09-12 21:03:06 +00002014 &ptp_usb->inep,
2015 &ptp_usb->inep_maxpacket,
2016 &ptp_usb->outep,
2017 &ptp_usb->outep_maxpacket,
2018 &ptp_usb->intep);
2019
2020 if (err) {
nicklas79daadbf22009-09-28 18:19:34 +00002021 LIBMTP_ERROR("LIBMTP PANIC: Unable to find interface & endpoints of device\n");
Linus Walleij5f3c44b2009-09-12 21:03:06 +00002022 return LIBMTP_ERROR_CONNECTING;
2023 }
2024
Linus Walleije04a1b92011-03-09 18:00:24 +01002025 /* Copy USB version number */
Linus Walleij7cf02642011-11-16 23:44:58 +01002026 ptp_usb->bcdusb = libusb_device->descriptor.bcdUSB;
Linus Walleije04a1b92011-03-09 18:00:24 +01002027
Linus Walleija700d222008-05-28 23:06:14 +00002028 /* Attempt to initialize this device */
Linus Walleij7cf02642011-11-16 23:44:58 +01002029 if (init_ptp_usb(params, ptp_usb, libusb_device) < 0) {
nicklas79daadbf22009-09-28 18:19:34 +00002030 LIBMTP_ERROR("LIBMTP PANIC: Unable to initialize device\n");
Linus Walleija700d222008-05-28 23:06:14 +00002031 return LIBMTP_ERROR_CONNECTING;
2032 }
Linus Walleij5f3c44b2009-09-12 21:03:06 +00002033
Linus Walleija700d222008-05-28 23:06:14 +00002034 /*
2035 * This works in situations where previous bad applications
Linus Walleij5f3c44b2009-09-12 21:03:06 +00002036 * have not used LIBMTP_Release_Device on exit
Linus Walleija700d222008-05-28 23:06:14 +00002037 */
Linus Walleij3c643252008-05-31 23:22:28 +00002038 if ((ret = ptp_opensession(params, 1)) == PTP_ERROR_IO) {
Linus Walleijf9137dc2010-10-31 17:13:06 +00002039 LIBMTP_ERROR("PTP_ERROR_IO: failed to open session, trying again after resetting USB interface\n");
Linus Walleijf9137dc2010-10-31 17:13:06 +00002040 LIBMTP_ERROR("LIBMTP libusb: Attempt to reset device\n");
Linus Walleij7cf02642011-11-16 23:44:58 +01002041 usb_reset(ptp_usb->handle);
Linus Walleij1fefa172011-04-18 07:36:18 +02002042 close_usb(ptp_usb);
Linus Walleijf9137dc2010-10-31 17:13:06 +00002043
Linus Walleij7cf02642011-11-16 23:44:58 +01002044 if(init_ptp_usb(params, ptp_usb, libusb_device) <0) {
Linus Walleijf9137dc2010-10-31 17:13:06 +00002045 LIBMTP_ERROR("LIBMTP PANIC: Could not init USB on second attempt\n");
Linus Walleija700d222008-05-28 23:06:14 +00002046 return LIBMTP_ERROR_CONNECTING;
2047 }
Linus Walleij5f3c44b2009-09-12 21:03:06 +00002048
Linus Walleija700d222008-05-28 23:06:14 +00002049 /* Device has been reset, try again */
Linus Walleijf9137dc2010-10-31 17:13:06 +00002050 if ((ret = ptp_opensession(params, 1)) == PTP_ERROR_IO) {
2051 LIBMTP_ERROR("LIBMTP PANIC: failed to open session on second attempt\n");
2052 return LIBMTP_ERROR_CONNECTING;
2053 }
Linus Walleija700d222008-05-28 23:06:14 +00002054 }
Linus Walleij5f3c44b2009-09-12 21:03:06 +00002055
Linus Walleija700d222008-05-28 23:06:14 +00002056 /* Was the transaction id invalid? Try again */
2057 if (ret == PTP_RC_InvalidTransactionID) {
nicklas79daadbf22009-09-28 18:19:34 +00002058 LIBMTP_ERROR("LIBMTP WARNING: Transaction ID was invalid, increment and try again\n");
Linus Walleij3c643252008-05-31 23:22:28 +00002059 params->transaction_id += 10;
2060 ret = ptp_opensession(params, 1);
Linus Walleija700d222008-05-28 23:06:14 +00002061 }
2062
2063 if (ret != PTP_RC_SessionAlreadyOpened && ret != PTP_RC_OK) {
nicklas79daadbf22009-09-28 18:19:34 +00002064 LIBMTP_ERROR("LIBMTP PANIC: Could not open session! "
Linus Walleija700d222008-05-28 23:06:14 +00002065 "(Return code %d)\n Try to reset the device.\n",
2066 ret);
Linus Walleij7cf02642011-11-16 23:44:58 +01002067 usb_release_interface(ptp_usb->handle,
2068 (int) ptp_usb->interface);
Linus Walleija700d222008-05-28 23:06:14 +00002069 return LIBMTP_ERROR_CONNECTING;
2070 }
2071
2072 /* OK configured properly */
Linus Walleija700d222008-05-28 23:06:14 +00002073 *usbinfo = (void *) ptp_usb;
2074 return LIBMTP_ERROR_NONE;
2075}
2076
2077
Linus Walleijb0ab5482007-08-29 21:08:54 +00002078void close_device (PTP_USB *ptp_usb, PTPParams *params)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00002079{
Linus Walleijd6a49972006-05-02 08:24:54 +00002080 if (ptp_closesession(params)!=PTP_RC_OK)
nicklas79daadbf22009-09-28 18:19:34 +00002081 LIBMTP_ERROR("ERROR: Could not close session!\n");
Linus Walleijb0ab5482007-08-29 21:08:54 +00002082 close_usb(ptp_usb);
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00002083}
2084
Linus Walleij2f622812008-08-30 22:06:58 +00002085void set_usb_device_timeout(PTP_USB *ptp_usb, int timeout)
2086{
Linus Walleije04a1b92011-03-09 18:00:24 +01002087 ptp_usb->timeout = timeout;
Linus Walleij2f622812008-08-30 22:06:58 +00002088}
2089
2090void get_usb_device_timeout(PTP_USB *ptp_usb, int *timeout)
2091{
Linus Walleije04a1b92011-03-09 18:00:24 +01002092 *timeout = ptp_usb->timeout;
2093}
2094
2095int guess_usb_speed(PTP_USB *ptp_usb)
2096{
2097 int bytes_per_second;
2098
2099 /*
2100 * We don't know the actual speeds so these are rough guesses
2101 * from the info you can find here:
2102 * http://en.wikipedia.org/wiki/USB#Transfer_rates
2103 * http://www.barefeats.com/usb2.html
2104 */
2105 switch (ptp_usb->bcdusb & 0xFF00) {
2106 case 0x0100:
2107 /* 1.x USB versions let's say 1MiB/s */
2108 bytes_per_second = 1*1024*1024;
2109 break;
2110 case 0x0200:
2111 case 0x0300:
2112 /* USB 2.0 nominal speed 18MiB/s */
2113 /* USB 3.0 won't be worse? */
2114 bytes_per_second = 18*1024*1024;
2115 break;
2116 default:
2117 /* Half-guess something? */
2118 bytes_per_second = 1*1024*1024;
2119 break;
2120 }
2121 return bytes_per_second;
Linus Walleij2f622812008-08-30 22:06:58 +00002122}
2123
Linus Walleij7cf02642011-11-16 23:44:58 +01002124static int usb_clear_stall_feature(PTP_USB* ptp_usb, int ep)
2125{
2126 return (usb_control_msg(ptp_usb->handle,
2127 USB_RECIP_ENDPOINT,
2128 USB_REQ_CLEAR_FEATURE,
2129 USB_FEATURE_HALT,
2130 ep,
2131 NULL,
2132 0,
2133 ptp_usb->timeout));
2134}
2135
Linus Walleijd6a49972006-05-02 08:24:54 +00002136static int usb_get_endpoint_status(PTP_USB* ptp_usb, int ep, uint16_t* status)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00002137{
Linus Walleij7cf02642011-11-16 23:44:58 +01002138 return (usb_control_msg(ptp_usb->handle,
2139 USB_DP_DTH|USB_RECIP_ENDPOINT,
2140 USB_REQ_GET_STATUS,
Linus Walleije04a1b92011-03-09 18:00:24 +01002141 USB_FEATURE_HALT,
2142 ep,
Linus Walleij7cf02642011-11-16 23:44:58 +01002143 (char *) status,
Linus Walleije04a1b92011-03-09 18:00:24 +01002144 2,
Linus Walleij7cf02642011-11-16 23:44:58 +01002145 ptp_usb->timeout));
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00002146}