blob: 281b1db54d98b68587071287a0d03bacdade688b [file] [log] [blame]
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001/*
Linus Walleij2f45d222007-02-02 22:47:39 +00002 * \file libusb-glue.c
3 * Low-level USB interface glue towards libusb.
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00004 *
Linus Walleij2f45d222007-02-02 22:47:39 +00005 * Copyright (C) 2005-2007 Richard A. Low <richard@wentnet.com>
Linus Walleijbac1eed2011-02-04 23:55:19 +01006 * Copyright (C) 2005-2011 Linus Walleij <triad@df.lth.se>
Linus Walleij2f45d222007-02-02 22:47:39 +00007 * Copyright (C) 2006-2007 Marcus Meissner
8 * Copyright (C) 2007 Ted Bullock
Linus Walleij2f622812008-08-30 22:06:58 +00009 * Copyright (C) 2008 Chris Bagwell <chris@cnpbagwell.com>
Linus Walleijeb8c6fe2006-02-03 09:46:22 +000010 *
Linus Walleij2f45d222007-02-02 22:47:39 +000011 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2 of the License, or (at your option) any later version.
Linus Walleij2e1c8a32007-01-22 06:46:08 +000015 *
Linus Walleij2f45d222007-02-02 22:47:39 +000016 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
Linus Walleij2e1c8a32007-01-22 06:46:08 +000020 *
Linus Walleij2f45d222007-02-02 22:47:39 +000021 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the
23 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 * Boston, MA 02111-1307, USA.
25 *
26 * Created by Richard Low on 24/12/2005. (as mtp-utils.c)
27 * Modified by Linus Walleij 2006-03-06
Linus Walleijd866d242009-08-23 21:50:39 +000028 * (Notice that Anglo-Saxons use little-endian dates and Swedes
Linus Walleij2f45d222007-02-02 22:47:39 +000029 * use big-endian dates.)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +000030 *
31 */
Linus Walleij21b2dde2009-06-02 19:55:34 +000032#include "config.h"
Linus Walleij7beba572006-11-29 08:56:12 +000033#include "libmtp.h"
34#include "libusb-glue.h"
Linus Walleij3e667ae2007-10-29 23:29:39 +000035#include "device-flags.h"
Linus Walleij7beba572006-11-29 08:56:12 +000036#include "util.h"
Linus Walleijeb8c6fe2006-02-03 09:46:22 +000037#include "ptp.h"
Linus Walleij7beba572006-11-29 08:56:12 +000038
Linus Walleijeb8c6fe2006-02-03 09:46:22 +000039#include <errno.h>
40#include <stdio.h>
41#include <stdlib.h>
42#include <string.h>
Linus Walleijeb8c6fe2006-02-03 09:46:22 +000043#include <usb.h>
44
Linus Walleija0323272007-01-07 12:21:30 +000045#include "ptp-pack.c"
46
Linus Walleijd866d242009-08-23 21:50:39 +000047/* Aha, older libusb does not have USB_CLASS_PTP */
48#ifndef USB_CLASS_PTP
49#define USB_CLASS_PTP 6
50#endif
51
Linus Walleij2f622812008-08-30 22:06:58 +000052/* Default USB timeout length. This can be overridden as needed
53 * but should start with a reasonable value so most common
54 * requests can be completed. The original value of 4000 was
55 * not long enough for large file transfer. Also, players can
56 * spend a bit of time collecting data. Higher values also
57 * make connecting/disconnecting more reliable.
58 */
59#define USB_TIMEOUT_DEFAULT 10000
Linus Walleijeb8c6fe2006-02-03 09:46:22 +000060
61/* USB control message data phase direction */
62#ifndef USB_DP_HTD
63#define USB_DP_HTD (0x00 << 7) /* host to device */
64#endif
65#ifndef USB_DP_DTH
66#define USB_DP_DTH (0x01 << 7) /* device to host */
67#endif
68
69/* USB Feature selector HALT */
70#ifndef USB_FEATURE_HALT
71#define USB_FEATURE_HALT 0x00
72#endif
73
Linus Walleija700d222008-05-28 23:06:14 +000074/* Internal data types */
75struct mtpdevice_list_struct {
76 struct usb_device *libusb_device;
77 PTPParams *params;
78 PTP_USB *ptp_usb;
79 uint32_t bus_location;
80 struct mtpdevice_list_struct *next;
81};
82typedef struct mtpdevice_list_struct mtpdevice_list_t;
83
Linus Walleij6fd2f082006-03-28 07:19:22 +000084static const LIBMTP_device_entry_t mtp_device_table[] = {
Linus Walleij1a673de2007-10-29 23:10:05 +000085/* We include an .h file which is shared between us and libgphoto2 */
86#include "music-players.h"
Linus Walleija5483642006-03-09 09:20:38 +000087};
Linus Walleij6fd2f082006-03-28 07:19:22 +000088static const int mtp_device_table_size = sizeof(mtp_device_table) / sizeof(LIBMTP_device_entry_t);
Linus Walleija5483642006-03-09 09:20:38 +000089
Linus Walleijd6a49972006-05-02 08:24:54 +000090// Local functions
Linus Walleij1fd2d272006-05-08 09:22:01 +000091static struct usb_bus* init_usb();
Linus Walleijb0ab5482007-08-29 21:08:54 +000092static void close_usb(PTP_USB* ptp_usb);
Linus Walleij5f3c44b2009-09-12 21:03:06 +000093static int find_interface_and_endpoints(struct usb_device *dev,
94 uint8_t *interface,
95 int* inep,
96 int* inep_maxpacket,
97 int* outep,
98 int* outep_maxpacket,
99 int* intep);
Linus Walleijd6a49972006-05-02 08:24:54 +0000100static void clear_stall(PTP_USB* ptp_usb);
Linus Walleij9eb3d312006-08-04 19:25:59 +0000101static int init_ptp_usb (PTPParams* params, PTP_USB* ptp_usb, struct usb_device* dev);
Linus Walleij784ac3f2006-12-29 10:36:51 +0000102static short ptp_write_func (unsigned long,PTPDataHandler*,void *data,unsigned long*);
Richard Low4df32f82007-01-11 20:04:39 +0000103static short ptp_read_func (unsigned long,PTPDataHandler*,void *data,unsigned long*,int);
Linus Walleijd6a49972006-05-02 08:24:54 +0000104static int usb_clear_stall_feature(PTP_USB* ptp_usb, int ep);
105static int usb_get_endpoint_status(PTP_USB* ptp_usb, int ep, uint16_t* status);
Linus Walleij1fd2d272006-05-08 09:22:01 +0000106
Linus Walleij552ba322007-01-22 11:49:59 +0000107/**
108 * Get a list of the supported USB devices.
109 *
110 * The developers depend on users of this library to constantly
111 * add in to the list of supported devices. What we need is the
112 * device name, USB Vendor ID (VID) and USB Product ID (PID).
113 * put this into a bug ticket at the project homepage, please.
114 * The VID/PID is used to let e.g. udev lift the device to
115 * console userspace access when it's plugged in.
116 *
117 * @param devices a pointer to a pointer that will hold a device
118 * list after the call to this function, if it was
119 * successful.
120 * @param numdevs a pointer to an integer that will hold the number
121 * of devices in the device list if the call was successful.
122 * @return 0 if the list was successfull retrieved, any other
123 * value means failure.
124 */
125int LIBMTP_Get_Supported_Devices_List(LIBMTP_device_entry_t ** const devices, int * const numdevs)
Linus Walleij6fd2f082006-03-28 07:19:22 +0000126{
127 *devices = (LIBMTP_device_entry_t *) &mtp_device_table;
128 *numdevs = mtp_device_table_size;
129 return 0;
130}
131
Linus Walleij552ba322007-01-22 11:49:59 +0000132
Linus Walleij1fd2d272006-05-08 09:22:01 +0000133static struct usb_bus* init_usb()
134{
Linus Walleijc1a82f12009-11-30 00:06:28 +0000135 struct usb_bus* busses;
136 struct usb_bus* bus;
137
138 /*
nicklas79daadbf22009-09-28 18:19:34 +0000139 * Some additional libusb debugging please.
140 * We use the same level debug between MTP and USB.
141 */
nicklas7903584082009-09-28 18:20:16 +0000142 if ((LIBMTP_debug & LIBMTP_DEBUG_USB) != 0)
nicklas79a99196a2009-09-28 18:19:57 +0000143 usb_set_debug(9);
nicklas79daadbf22009-09-28 18:19:34 +0000144
Linus Walleij1fd2d272006-05-08 09:22:01 +0000145 usb_init();
146 usb_find_busses();
147 usb_find_devices();
Linus Walleijc1a82f12009-11-30 00:06:28 +0000148 /* Workaround a libusb 0.1 bug : bus location is not initialised */
149 busses = usb_get_busses();
150 for (bus = busses; bus != NULL; bus = bus->next) {
Richard Lowea73f5c2010-05-16 11:57:56 +0000151 if (!bus->location)
152 bus->location = strtoul(bus->dirname, NULL, 10);
Linus Walleijc1a82f12009-11-30 00:06:28 +0000153 }
154 return (busses);
Linus Walleij1fd2d272006-05-08 09:22:01 +0000155}
156
157/**
tedbullock0f033cb2007-02-14 20:56:54 +0000158 * Small recursive function to append a new usb_device to the linked list of
159 * USB MTP devices
Linus Walleijc1a82f12009-11-30 00:06:28 +0000160 * @param devlist dynamic linked list of pointers to usb devices with MTP
Linus Walleij63fd1e62008-04-23 23:49:43 +0000161 * properties, to be extended with new device.
162 * @param newdevice the new device to add.
163 * @param bus_location bus for this device.
164 * @return an extended array or NULL on failure.
tedbullock0f033cb2007-02-14 20:56:54 +0000165 */
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000166static mtpdevice_list_t *append_to_mtpdevice_list(mtpdevice_list_t *devlist,
Linus Walleij63fd1e62008-04-23 23:49:43 +0000167 struct usb_device *newdevice,
168 uint32_t bus_location)
tedbullock0f033cb2007-02-14 20:56:54 +0000169{
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000170 mtpdevice_list_t *new_list_entry;
Linus Walleijc1a82f12009-11-30 00:06:28 +0000171
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000172 new_list_entry = (mtpdevice_list_t *) malloc(sizeof(mtpdevice_list_t));
173 if (new_list_entry == NULL) {
174 return NULL;
175 }
176 // Fill in USB device, if we *HAVE* to make a copy of the device do it here.
177 new_list_entry->libusb_device = newdevice;
Linus Walleij63fd1e62008-04-23 23:49:43 +0000178 new_list_entry->bus_location = bus_location;
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000179 new_list_entry->next = NULL;
Linus Walleij5d987e12010-05-22 22:35:33 +0000180
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000181 if (devlist == NULL) {
182 return new_list_entry;
183 } else {
184 mtpdevice_list_t *tmp = devlist;
185 while (tmp->next != NULL) {
186 tmp = tmp->next;
187 }
188 tmp->next = new_list_entry;
189 }
190 return devlist;
tedbullock0f033cb2007-02-14 20:56:54 +0000191}
192
193/**
194 * Small recursive function to free dynamic memory allocated to the linked list
195 * of USB MTP devices
Linus Walleij5d987e12010-05-22 22:35:33 +0000196 * @param devlist dynamic linked list of pointers to usb devices with MTP
tedbullock0f033cb2007-02-14 20:56:54 +0000197 * properties.
198 * @return nothing
199 */
Linus Walleija700d222008-05-28 23:06:14 +0000200static void free_mtpdevice_list(mtpdevice_list_t *devlist)
tedbullock0f033cb2007-02-14 20:56:54 +0000201{
Linus Walleijac061862007-03-07 08:28:33 +0000202 mtpdevice_list_t *tmplist = devlist;
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000203
204 if (devlist == NULL)
205 return;
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000206 while (tmplist != NULL) {
207 mtpdevice_list_t *tmp = tmplist;
208 tmplist = tmplist->next;
Richard Low61edc1a2007-09-23 10:35:48 +0000209 // Do not free() the fields (ptp_usb, params)! These are used elsewhere.
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000210 free(tmp);
tedbullock0f033cb2007-02-14 20:56:54 +0000211 }
tedbullock0f033cb2007-02-14 20:56:54 +0000212 return;
213}
214
215/**
Linus Walleij9521e2b2007-07-10 21:50:42 +0000216 * This checks if a device has an MTP descriptor. The descriptor was
217 * elaborated about in gPhoto bug 1482084, and some official documentation
218 * with no strings attached was published by Microsoft at
219 * http://www.microsoft.com/whdc/system/bus/USB/USBFAQ_intermed.mspx#E3HAC
220 *
Linus Walleija4942fc2007-03-12 19:23:21 +0000221 * @param dev a device struct from libusb.
Linus Walleij9521e2b2007-07-10 21:50:42 +0000222 * @param dumpfile set to non-NULL to make the descriptors dump out
223 * to this file in human-readable hex so we can scruitinze them.
Linus Walleija4942fc2007-03-12 19:23:21 +0000224 * @return 1 if the device is MTP compliant, 0 if not.
225 */
Linus Walleij9521e2b2007-07-10 21:50:42 +0000226static int probe_device_descriptor(struct usb_device *dev, FILE *dumpfile)
Linus Walleija4942fc2007-03-12 19:23:21 +0000227{
228 usb_dev_handle *devh;
229 unsigned char buf[1024], cmd;
Linus Walleijbc550bc2007-11-08 23:25:09 +0000230 int i;
Linus Walleija4942fc2007-03-12 19:23:21 +0000231 int ret;
Linus Walleijfb3e3022011-01-19 19:20:34 +0100232 /* This is to indicate if we find some vendor interface */
233 int found_vendor_spec_interface = 0;
Linus Walleij5d987e12010-05-22 22:35:33 +0000234
235 /*
Linus Walleijfb3e3022011-01-19 19:20:34 +0100236 * Don't examine devices that are not likely to
237 * contain any MTP interface, update this the day
238 * you find some weird combination...
Linus Walleij5d987e12010-05-22 22:35:33 +0000239 */
Linus Walleij4eb8ad92011-01-27 22:52:07 +0100240 if (!(dev->descriptor.bDeviceClass == USB_CLASS_PER_INTERFACE ||
241 dev->descriptor.bDeviceClass == USB_CLASS_COMM ||
242 dev->descriptor.bDeviceClass == USB_CLASS_PTP ||
243 dev->descriptor.bDeviceClass == USB_CLASS_VENDOR_SPEC)) {
Linus Walleija4942fc2007-03-12 19:23:21 +0000244 return 0;
245 }
Linus Walleij5d987e12010-05-22 22:35:33 +0000246
Linus Walleija4942fc2007-03-12 19:23:21 +0000247 /* Attempt to open Device on this port */
248 devh = usb_open(dev);
249 if (devh == NULL) {
250 /* Could not open this device */
251 return 0;
252 }
Linus Walleijbc550bc2007-11-08 23:25:09 +0000253
254 /*
Richard Lowe029eba2009-04-11 12:36:25 +0000255 * This sometimes crashes on the j for loop below
Linus Walleij88244212009-08-12 16:05:56 +0000256 * I think it is because config is NULL yet
Richard Lowe029eba2009-04-11 12:36:25 +0000257 * dev->descriptor.bNumConfigurations > 0
258 * this check should stop this
Linus Walleijbc550bc2007-11-08 23:25:09 +0000259 */
Richard Lowe029eba2009-04-11 12:36:25 +0000260 if (dev->config) {
261 /*
Linus Walleij88244212009-08-12 16:05:56 +0000262 * Loop over the device configurations and interfaces. Nokia MTP-capable
263 * handsets (possibly others) typically have the string "MTP" in their
Richard Lowe029eba2009-04-11 12:36:25 +0000264 * MTP interface descriptions, that's how they can be detected, before
265 * we try the more esoteric "OS descriptors" (below).
266 */
267 for (i = 0; i < dev->descriptor.bNumConfigurations; i++) {
268 uint8_t j;
Linus Walleij88244212009-08-12 16:05:56 +0000269
Richard Lowe029eba2009-04-11 12:36:25 +0000270 for (j = 0; j < dev->config[i].bNumInterfaces; j++) {
271 int k;
272 for (k = 0; k < dev->config[i].interface[j].num_altsetting; k++) {
Linus Walleij88244212009-08-12 16:05:56 +0000273 /* Current interface descriptor */
274 struct usb_interface_descriptor *intf =
275 &dev->config[i].interface[j].altsetting[k];
276
277 /*
Linus Walleijfb3e3022011-01-19 19:20:34 +0100278 * We only want to probe for the OS descriptor if the
279 * device is USB_CLASS_VENDOR_SPEC or one of the interfaces
280 * in it is, so flag if we find an interface like this.
281 */
282 if (intf->bInterfaceClass == USB_CLASS_VENDOR_SPEC) {
283 found_vendor_spec_interface = 1;
284 }
285
286 /*
Linus Walleij88244212009-08-12 16:05:56 +0000287 * Check for Still Image Capture class with PIMA 15740 protocol,
288 * also known as PTP
289 */
Linus Walleij5d92c8c2011-01-25 23:42:19 +0100290#if 0
Linus Walleij88244212009-08-12 16:05:56 +0000291 if (intf->bInterfaceClass == USB_CLASS_PTP
292 && intf->bInterfaceSubClass == 0x01
293 && intf->bInterfaceProtocol == 0x01) {
294 if (dumpfile != NULL) {
Linus Walleij5d92c8c2011-01-25 23:42:19 +0100295 fprintf(dumpfile, " Found PTP device, check vendor "
296 "extension...\n");
Linus Walleij88244212009-08-12 16:05:56 +0000297 }
Linus Walleij5d92c8c2011-01-25 23:42:19 +0100298 // This is where we may insert code to open a PTP
299 // session and query the vendor extension ID to see
Linus Walleij4eb8ad92011-01-27 22:52:07 +0100300 // if it is 0xffffffff, i.e. MTP according to the spec.
Linus Walleij5d92c8c2011-01-25 23:42:19 +0100301 if (was_mtp_extension) {
302 usb_close(devh);
303 return 1;
304 }
Linus Walleij88244212009-08-12 16:05:56 +0000305 }
Linus Walleij5d92c8c2011-01-25 23:42:19 +0100306#endif
307
308 /*
309 * Next we search for the MTP substring in the interface name.
310 * For example : "RIM MS/MTP" should work.
311 */
Linus Walleij88244212009-08-12 16:05:56 +0000312 buf[0] = '\0';
313 ret = usb_get_string_simple(devh,
314 dev->config[i].interface[j].altsetting[k].iInterface,
315 (char *) buf,
316 1024);
317 if (ret < 3)
318 continue;
Linus Walleijfea4f532009-09-22 22:08:35 +0000319 if (strstr((char *) buf, "MTP") != NULL) {
Linus Walleij88244212009-08-12 16:05:56 +0000320 if (dumpfile != NULL) {
321 fprintf(dumpfile, "Configuration %d, interface %d, altsetting %d:\n", i, j, k);
322 fprintf(dumpfile, " Interface description contains the string \"MTP\"\n");
323 fprintf(dumpfile, " Device recognized as MTP, no further probing.\n");
324 }
Richard Lowe029eba2009-04-11 12:36:25 +0000325 usb_close(devh);
326 return 1;
327 }
328 #ifdef LIBUSB_HAS_GET_DRIVER_NP
Linus Walleij88244212009-08-12 16:05:56 +0000329 {
330 /*
331 * Specifically avoid probing anything else than USB mass storage devices
332 * and non-associated drivers in Linux.
333 */
334 char devname[0x10];
335
336 devname[0] = '\0';
337 ret = usb_get_driver_np(devh,
338 dev->config[i].interface[j].altsetting[k].iInterface,
339 devname,
340 sizeof(devname));
341 if (devname[0] != '\0' && strcmp(devname, "usb-storage")) {
nicklas79daadbf22009-09-28 18:19:34 +0000342 LIBMTP_INFO("avoid probing device using kernel interface \"%s\"\n", devname);
Linus Walleij88244212009-08-12 16:05:56 +0000343 return 0;
344 }
345 }
Richard Lowe029eba2009-04-11 12:36:25 +0000346 #endif
347 }
348 }
349 }
350 } else {
351 if (dev->descriptor.bNumConfigurations)
nicklas79daadbf22009-09-28 18:19:34 +0000352 LIBMTP_INFO("dev->config is NULL in probe_device_descriptor yet dev->descriptor.bNumConfigurations > 0\n");
Linus Walleijbc550bc2007-11-08 23:25:09 +0000353 }
Linus Walleijfea4f532009-09-22 22:08:35 +0000354
Linus Walleij49e57b82009-12-16 09:21:33 +0000355 /*
Linus Walleijfb3e3022011-01-19 19:20:34 +0100356 * Only probe for OS descriptor if the device is vendor specific
357 * or one of the interfaces found is.
Linus Walleij49e57b82009-12-16 09:21:33 +0000358 */
Linus Walleijfb3e3022011-01-19 19:20:34 +0100359 if (dev->descriptor.bDeviceClass == USB_CLASS_VENDOR_SPEC ||
360 found_vendor_spec_interface) {
Linus Walleij9521e2b2007-07-10 21:50:42 +0000361
Linus Walleijfb3e3022011-01-19 19:20:34 +0100362 /* Read the special descriptor */
363 ret = usb_get_descriptor(devh, 0x03, 0xee, buf, sizeof(buf));
Linus Walleijfea4f532009-09-22 22:08:35 +0000364
Linus Walleijfb3e3022011-01-19 19:20:34 +0100365 /*
366 * If something failed we're probably stalled to we need
367 * to clear the stall off the endpoint and say this is not
368 * MTP.
369 */
370 if (ret < 0) {
371 /* EP0 is the default control endpoint */
372 usb_clear_halt(devh, 0);
373 usb_close(devh);
374 return 0;
375 }
376
377 // Dump it, if requested
378 if (dumpfile != NULL && ret > 0) {
379 fprintf(dumpfile, "Microsoft device descriptor 0xee:\n");
380 data_dump_ascii(dumpfile, buf, ret, 16);
381 }
382
383 /* Check if descriptor length is at least 10 bytes */
384 if (ret < 10) {
385 usb_close(devh);
386 return 0;
387 }
388
389 /* Check if this device has a Microsoft Descriptor */
390 if (!((buf[2] == 'M') && (buf[4] == 'S') &&
391 (buf[6] == 'F') && (buf[8] == 'T'))) {
392 usb_close(devh);
393 return 0;
394 }
395
396 /* Check if device responds to control message 1 or if there is an error */
397 cmd = buf[16];
398 ret = usb_control_msg (devh,
399 USB_ENDPOINT_IN | USB_RECIP_DEVICE | USB_TYPE_VENDOR,
400 cmd,
401 0,
402 4,
403 (char *) buf,
404 sizeof(buf),
405 USB_TIMEOUT_DEFAULT);
406
407 // Dump it, if requested
408 if (dumpfile != NULL && ret > 0) {
409 fprintf(dumpfile, "Microsoft device response to control message 1, CMD 0x%02x:\n", cmd);
410 data_dump_ascii(dumpfile, buf, ret, 16);
411 }
412
413 /* If this is true, the device either isn't MTP or there was an error */
414 if (ret <= 0x15) {
415 /* TODO: If there was an error, flag it and let the user know somehow */
416 /* if(ret == -1) {} */
417 usb_close(devh);
418 return 0;
419 }
420
421 /* Check if device is MTP or if it is something like a USB Mass Storage
422 device with Janus DRM support */
423 if ((buf[0x12] != 'M') || (buf[0x13] != 'T') || (buf[0x14] != 'P')) {
424 usb_close(devh);
425 return 0;
426 }
427
428 /* After this point we are probably dealing with an MTP device */
429
430 /*
431 * Check if device responds to control message 2, which is
432 * the extended device parameters. Most devices will just
433 * respond with a copy of the same message as for the first
434 * message, some respond with zero-length (which is OK)
435 * and some with pure garbage. We're not parsing the result
436 * so this is not very important.
437 */
438 ret = usb_control_msg (devh,
439 USB_ENDPOINT_IN | USB_RECIP_DEVICE | USB_TYPE_VENDOR,
440 cmd,
441 0,
442 5,
443 (char *) buf,
444 sizeof(buf),
445 USB_TIMEOUT_DEFAULT);
446
447 // Dump it, if requested
448 if (dumpfile != NULL && ret > 0) {
449 fprintf(dumpfile, "Microsoft device response to control message 2, CMD 0x%02x:\n", cmd);
450 data_dump_ascii(dumpfile, buf, ret, 16);
451 }
452
453 /* If this is true, the device errored against control message 2 */
454 if (ret == -1) {
455 /* TODO: Implement callback function to let managing program know there
456 was a problem, along with description of the problem */
457 LIBMTP_ERROR("Potential MTP Device with VendorID:%04x and "
458 "ProductID:%04x encountered an error responding to "
459 "control message 2.\n"
460 "Problems may arrise but continuing\n",
461 dev->descriptor.idVendor, dev->descriptor.idProduct);
462 } else if (dumpfile != NULL && ret == 0) {
463 fprintf(dumpfile, "Zero-length response to control message 2 (OK)\n");
464 } else if (dumpfile != NULL) {
465 fprintf(dumpfile, "Device responds to control message 2 with some data.\n");
466 }
Marcus Meissnere0bb6c62011-02-07 13:26:58 +0100467 /* Close the USB device handle */
468 usb_close(devh);
469 return 1;
Linus Walleija4942fc2007-03-12 19:23:21 +0000470 }
Linus Walleijfea4f532009-09-22 22:08:35 +0000471
Linus Walleija4942fc2007-03-12 19:23:21 +0000472 /* Close the USB device handle */
473 usb_close(devh);
Marcus Meissnere0bb6c62011-02-07 13:26:58 +0100474 return 0;
Linus Walleija4942fc2007-03-12 19:23:21 +0000475}
476
477/**
478 * This function scans through the connected usb devices on a machine and
479 * if they match known Vendor and Product identifiers appends them to the
Linus Walleij49557482010-06-22 09:14:47 +0000480 * dynamic array mtp_device_list. Be sure to call
481 * <code>free_mtpdevice_list(mtp_device_list)</code> when you are done
Linus Walleij63fd1e62008-04-23 23:49:43 +0000482 * with it, assuming it is not NULL.
Linus Walleij49557482010-06-22 09:14:47 +0000483 * @param mtp_device_list dynamic array of pointers to usb devices with MTP
Linus Walleij45a86372007-03-07 09:36:19 +0000484 * properties (if this list is not empty, new entries will be appended
485 * to the list).
Linus Walleija4942fc2007-03-12 19:23:21 +0000486 * @return LIBMTP_ERROR_NONE implies that devices have been found, scan the list
Linus Walleij49557482010-06-22 09:14:47 +0000487 * appropriately. LIBMTP_ERROR_NO_DEVICE_ATTACHED implies that no
Linus Walleij9c6c1542007-09-19 13:43:06 +0000488 * devices have been found.
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000489 */
Linus Walleij45a86372007-03-07 09:36:19 +0000490static LIBMTP_error_number_t get_mtp_usb_device_list(mtpdevice_list_t ** mtp_device_list)
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000491{
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000492 struct usb_bus *bus = init_usb();
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000493 for (; bus != NULL; bus = bus->next) {
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000494 struct usb_device *dev = bus->devices;
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000495 for (; dev != NULL; dev = dev->next) {
Richard Lowe9f205f2007-09-16 16:25:50 +0000496 if (dev->descriptor.bDeviceClass != USB_CLASS_HUB) {
Linus Walleij9c6c1542007-09-19 13:43:06 +0000497 int i;
498 int found = 0;
499
500 // First check if we know about the device already.
501 // Devices well known to us will not have their descriptors
502 // probed, it caused problems with some devices.
Richard Lowe9f205f2007-09-16 16:25:50 +0000503 for(i = 0; i < mtp_device_table_size; i++) {
504 if(dev->descriptor.idVendor == mtp_device_table[i].vendor_id &&
505 dev->descriptor.idProduct == mtp_device_table[i].product_id) {
506 /* Append this usb device to the MTP device list */
Linus Walleij49557482010-06-22 09:14:47 +0000507 *mtp_device_list = append_to_mtpdevice_list(*mtp_device_list,
508 dev,
Linus Walleij63fd1e62008-04-23 23:49:43 +0000509 bus->location);
Richard Lowe9f205f2007-09-16 16:25:50 +0000510 found = 1;
511 break;
512 }
513 }
Linus Walleij9c6c1542007-09-19 13:43:06 +0000514 // If we didn't know it, try probing the "OS Descriptor".
Richard Lowe9f205f2007-09-16 16:25:50 +0000515 if (!found) {
Richard Lowe9f205f2007-09-16 16:25:50 +0000516 if (probe_device_descriptor(dev, NULL)) {
517 /* Append this usb device to the MTP USB Device List */
Linus Walleij49557482010-06-22 09:14:47 +0000518 *mtp_device_list = append_to_mtpdevice_list(*mtp_device_list,
Linus Walleij63fd1e62008-04-23 23:49:43 +0000519 dev,
520 bus->location);
Richard Lowe9f205f2007-09-16 16:25:50 +0000521 }
Linus Walleijd866d242009-08-23 21:50:39 +0000522 /*
523 * By thomas_-_s: Also append devices that are no MTP but PTP devices
524 * if this is commented out.
525 */
526 /*
527 else {
528 // Check whether the device is no USB hub but a PTP.
529 if ( dev->config != NULL &&dev->config->interface->altsetting->bInterfaceClass == USB_CLASS_PTP && dev->descriptor.bDeviceClass != USB_CLASS_HUB ) {
530 *mtp_device_list = append_to_mtpdevice_list(*mtp_device_list, dev, bus->location);
531 }
532 }
533 */
Richard Lowe9f205f2007-09-16 16:25:50 +0000534 }
Linus Walleij9c6c1542007-09-19 13:43:06 +0000535 }
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000536 }
537 }
Linus Walleij49557482010-06-22 09:14:47 +0000538
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000539 /* If nothing was found we end up here. */
Linus Walleija4942fc2007-03-12 19:23:21 +0000540 if(*mtp_device_list == NULL) {
tedbullock433d2172007-02-23 22:39:12 +0000541 return LIBMTP_ERROR_NO_DEVICE_ATTACHED;
Linus Walleija4942fc2007-03-12 19:23:21 +0000542 }
543 return LIBMTP_ERROR_NONE;
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000544}
545
546/**
Linus Walleij549f49a2010-12-05 14:00:34 +0000547 * Checks if a specific device with a certain bus and device
548 * number has an MTP type device descriptor.
549 *
550 * @param busno the bus number of the device to check
551 * @param deviceno the device number of the device to check
552 * @return 1 if the device is MTP else 0
553 */
554int LIBMTP_Check_Specific_Device(int busno, int devno)
555{
556 struct usb_bus *bus = init_usb();
557 for (; bus != NULL; bus = bus->next) {
558 struct usb_device *dev = bus->devices;
559 if (bus->location != busno)
560 continue;
561
562 for (; dev != NULL; dev = dev->next) {
563
564 if (dev->devnum != devno)
565 continue;
566
567 if (probe_device_descriptor(dev, NULL))
568 return 1;
569 }
570 }
571 return 0;
572}
573
574/**
Linus Walleij63fd1e62008-04-23 23:49:43 +0000575 * Detect the raw MTP device descriptors and return a list of
576 * of the devices found.
Linus Walleij49557482010-06-22 09:14:47 +0000577 *
Linus Walleij63fd1e62008-04-23 23:49:43 +0000578 * @param devices a pointer to a variable that will hold
579 * the list of raw devices found. This may be NULL
580 * on return if the number of detected devices is zero.
581 * The user shall simply <code>free()</code> this
582 * variable when finished with the raw devices,
583 * in order to release memory.
Linus Walleij49557482010-06-22 09:14:47 +0000584 * @param numdevs a pointer to an integer that will hold
Linus Walleij63fd1e62008-04-23 23:49:43 +0000585 * the number of devices in the list. This may
586 * be 0.
587 * @return 0 if successful, any other value means failure.
Linus Walleij1fd2d272006-05-08 09:22:01 +0000588 */
Linus Walleij49557482010-06-22 09:14:47 +0000589LIBMTP_error_number_t LIBMTP_Detect_Raw_Devices(LIBMTP_raw_device_t ** devices,
Linus Walleij63fd1e62008-04-23 23:49:43 +0000590 int * numdevs)
Linus Walleij1fd2d272006-05-08 09:22:01 +0000591{
Linus Walleij63fd1e62008-04-23 23:49:43 +0000592 mtpdevice_list_t *devlist = NULL;
593 mtpdevice_list_t *dev;
594 LIBMTP_error_number_t ret;
Linus Walleij63fd1e62008-04-23 23:49:43 +0000595 LIBMTP_raw_device_t *retdevs;
596 int devs = 0;
Linus Walleijf106aa62008-05-04 22:55:51 +0000597 int i, j;
Linus Walleijb6b69132007-03-07 09:45:05 +0000598
599 ret = get_mtp_usb_device_list(&devlist);
Linus Walleijef2fb362008-05-28 20:59:09 +0000600 if (ret == LIBMTP_ERROR_NO_DEVICE_ATTACHED) {
601 *devices = NULL;
602 *numdevs = 0;
Linus Walleija700d222008-05-28 23:06:14 +0000603 return ret;
Linus Walleijef2fb362008-05-28 20:59:09 +0000604 } else if (ret != LIBMTP_ERROR_NONE) {
nicklas79daadbf22009-09-28 18:19:34 +0000605 LIBMTP_ERROR("LIBMTP PANIC: get_mtp_usb_device_list() "
Linus Walleijf106aa62008-05-04 22:55:51 +0000606 "error code: %d on line %d\n", ret, __LINE__);
Linus Walleija700d222008-05-28 23:06:14 +0000607 return ret;
Linus Walleijb6b69132007-03-07 09:45:05 +0000608 }
Linus Walleijf106aa62008-05-04 22:55:51 +0000609
Linus Walleij63fd1e62008-04-23 23:49:43 +0000610 // Get list size
611 dev = devlist;
612 while (dev != NULL) {
613 devs++;
614 dev = dev->next;
615 }
616 if (devs == 0) {
617 *devices = NULL;
618 *numdevs = 0;
Linus Walleija700d222008-05-28 23:06:14 +0000619 return LIBMTP_ERROR_NONE;
Linus Walleij63fd1e62008-04-23 23:49:43 +0000620 }
621 // Conjure a device list
622 retdevs = (LIBMTP_raw_device_t *) malloc(sizeof(LIBMTP_raw_device_t) * devs);
623 if (retdevs == NULL) {
624 // Out of memory
625 *devices = NULL;
626 *numdevs = 0;
Linus Walleija700d222008-05-28 23:06:14 +0000627 return LIBMTP_ERROR_MEMORY_ALLOCATION;
Linus Walleij63fd1e62008-04-23 23:49:43 +0000628 }
629 dev = devlist;
630 i = 0;
631 while (dev != NULL) {
Linus Walleijf106aa62008-05-04 22:55:51 +0000632 int device_known = 0;
633
Linus Walleij63fd1e62008-04-23 23:49:43 +0000634 // Assign default device info
635 retdevs[i].device_entry.vendor = NULL;
636 retdevs[i].device_entry.vendor_id = dev->libusb_device->descriptor.idVendor;
637 retdevs[i].device_entry.product = NULL;
638 retdevs[i].device_entry.product_id = dev->libusb_device->descriptor.idProduct;
639 retdevs[i].device_entry.device_flags = 0x00000000U;
Linus Walleijfec4d562008-06-01 22:30:36 +0000640 // See if we can locate some additional vendor info and device flags
Linus Walleijf106aa62008-05-04 22:55:51 +0000641 for(j = 0; j < mtp_device_table_size; j++) {
642 if(dev->libusb_device->descriptor.idVendor == mtp_device_table[j].vendor_id &&
643 dev->libusb_device->descriptor.idProduct == mtp_device_table[j].product_id) {
644 device_known = 1;
645 retdevs[i].device_entry.vendor = mtp_device_table[j].vendor;
646 retdevs[i].device_entry.product = mtp_device_table[j].product;
647 retdevs[i].device_entry.device_flags = mtp_device_table[j].device_flags;
nicklas79daadbf22009-09-28 18:19:34 +0000648
Linus Walleijf106aa62008-05-04 22:55:51 +0000649 // This device is known to the developers
Linus Walleij49557482010-06-22 09:14:47 +0000650 LIBMTP_ERROR("Device %d (VID=%04x and PID=%04x) is a %s %s.\n",
Linus Walleijf106aa62008-05-04 22:55:51 +0000651 i,
652 dev->libusb_device->descriptor.idVendor,
653 dev->libusb_device->descriptor.idProduct,
654 mtp_device_table[j].vendor,
655 mtp_device_table[j].product);
Linus Walleijf106aa62008-05-04 22:55:51 +0000656 break;
657 }
658 }
659 if (!device_known) {
660 // This device is unknown to the developers
Linus Walleij49557482010-06-22 09:14:47 +0000661 LIBMTP_ERROR("Device %d (VID=%04x and PID=%04x) is UNKNOWN.\n",
Linus Walleijf106aa62008-05-04 22:55:51 +0000662 i,
663 dev->libusb_device->descriptor.idVendor,
664 dev->libusb_device->descriptor.idProduct);
nicklas79daadbf22009-09-28 18:19:34 +0000665 LIBMTP_ERROR("Please report this VID/PID and the device model to the "
Linus Walleijf106aa62008-05-04 22:55:51 +0000666 "libmtp development team\n");
Linus Walleijfec4d562008-06-01 22:30:36 +0000667 /*
668 * Trying to get iManufacturer or iProduct from the device at this
669 * point would require opening a device handle, that we don't want
670 * to do right now. (Takes time for no good enough reason.)
671 */
Linus Walleijf106aa62008-05-04 22:55:51 +0000672 }
673 // Save the location on the bus
Linus Walleij63fd1e62008-04-23 23:49:43 +0000674 retdevs[i].bus_location = dev->bus_location;
675 retdevs[i].devnum = dev->libusb_device->devnum;
676 i++;
677 dev = dev->next;
Linus Walleij49557482010-06-22 09:14:47 +0000678 }
Linus Walleij63fd1e62008-04-23 23:49:43 +0000679 *devices = retdevs;
680 *numdevs = i;
Linus Walleijb6b69132007-03-07 09:45:05 +0000681 free_mtpdevice_list(devlist);
Linus Walleija700d222008-05-28 23:06:14 +0000682 return LIBMTP_ERROR_NONE;
Linus Walleij1fd2d272006-05-08 09:22:01 +0000683}
684
Linus Walleijc6210fb2006-05-08 11:11:41 +0000685/**
686 * This routine just dumps out low-level
687 * USB information about the current device.
688 * @param ptp_usb the USB device to get information from.
689 */
690void dump_usbinfo(PTP_USB *ptp_usb)
691{
Linus Walleijc6210fb2006-05-08 11:11:41 +0000692 struct usb_device *dev;
693
694#ifdef LIBUSB_HAS_GET_DRIVER_NP
695 char devname[0x10];
Richard Lowe029eba2009-04-11 12:36:25 +0000696 int res;
Linus Walleij49557482010-06-22 09:14:47 +0000697
Linus Walleijc6210fb2006-05-08 11:11:41 +0000698 devname[0] = '\0';
Linus Walleijb0ab5482007-08-29 21:08:54 +0000699 res = usb_get_driver_np(ptp_usb->handle, (int) ptp_usb->interface, devname, sizeof(devname));
Linus Walleijc6210fb2006-05-08 11:11:41 +0000700 if (devname[0] != '\0') {
nicklas79daadbf22009-09-28 18:19:34 +0000701 LIBMTP_INFO(" Using kernel interface \"%s\"\n", devname);
Linus Walleijc6210fb2006-05-08 11:11:41 +0000702 }
703#endif
704 dev = usb_device(ptp_usb->handle);
nicklas79daadbf22009-09-28 18:19:34 +0000705 LIBMTP_INFO(" bcdUSB: %d\n", dev->descriptor.bcdUSB);
706 LIBMTP_INFO(" bDeviceClass: %d\n", dev->descriptor.bDeviceClass);
707 LIBMTP_INFO(" bDeviceSubClass: %d\n", dev->descriptor.bDeviceSubClass);
708 LIBMTP_INFO(" bDeviceProtocol: %d\n", dev->descriptor.bDeviceProtocol);
709 LIBMTP_INFO(" idVendor: %04x\n", dev->descriptor.idVendor);
710 LIBMTP_INFO(" idProduct: %04x\n", dev->descriptor.idProduct);
711 LIBMTP_INFO(" IN endpoint maxpacket: %d bytes\n", ptp_usb->inep_maxpacket);
712 LIBMTP_INFO(" OUT endpoint maxpacket: %d bytes\n", ptp_usb->outep_maxpacket);
713 LIBMTP_INFO(" Raw device info:\n");
714 LIBMTP_INFO(" Bus location: %d\n", ptp_usb->rawdevice.bus_location);
715 LIBMTP_INFO(" Device number: %d\n", ptp_usb->rawdevice.devnum);
716 LIBMTP_INFO(" Device entry info:\n");
717 LIBMTP_INFO(" Vendor: %s\n", ptp_usb->rawdevice.device_entry.vendor);
718 LIBMTP_INFO(" Vendor id: 0x%04x\n", ptp_usb->rawdevice.device_entry.vendor_id);
719 LIBMTP_INFO(" Product: %s\n", ptp_usb->rawdevice.device_entry.product);
720 LIBMTP_INFO(" Vendor id: 0x%04x\n", ptp_usb->rawdevice.device_entry.product_id);
721 LIBMTP_INFO(" Device flags: 0x%08x\n", ptp_usb->rawdevice.device_entry.device_flags);
Linus Walleij9521e2b2007-07-10 21:50:42 +0000722 (void) probe_device_descriptor(dev, stdout);
Linus Walleijc6210fb2006-05-08 11:11:41 +0000723}
724
Linus Walleija4e6bdc2008-05-23 22:31:53 +0000725/**
726 * Retrieve the apropriate playlist extension for this
727 * device. Rather hacky at the moment. This is probably
728 * desired by the managing software, but when creating
729 * lists on the device itself you notice certain preferences.
730 * @param ptp_usb the USB device to get suggestion for.
731 * @return the suggested playlist extension.
732 */
Richard Lowe029eba2009-04-11 12:36:25 +0000733const char *get_playlist_extension(PTP_USB *ptp_usb)
Linus Walleija4e6bdc2008-05-23 22:31:53 +0000734{
735 struct usb_device *dev;
736 static char creative_pl_extension[] = ".zpl";
737 static char default_pl_extension[] = ".pla";
738
739 dev = usb_device(ptp_usb->handle);
740 if (dev->descriptor.idVendor == 0x041e) {
741 return creative_pl_extension;
742 }
743 return default_pl_extension;
744}
745
Linus Walleijd1e14e02008-12-14 01:07:30 +0000746static void
747libusb_glue_debug (PTPParams *params, const char *format, ...)
Linus Walleij14735952010-04-25 04:56:49 +0000748{
Linus Walleija0323272007-01-07 12:21:30 +0000749 va_list args;
750
751 va_start (args, format);
752 if (params->debug_func!=NULL)
753 params->debug_func (params->data, format, args);
754 else
755 {
756 vfprintf (stderr, format, args);
757 fprintf (stderr,"\n");
758 fflush (stderr);
759 }
760 va_end (args);
Linus Walleij14735952010-04-25 04:56:49 +0000761}
Linus Walleija0323272007-01-07 12:21:30 +0000762
763static void
Linus Walleijd1e14e02008-12-14 01:07:30 +0000764libusb_glue_error (PTPParams *params, const char *format, ...)
Linus Walleij49557482010-06-22 09:14:47 +0000765{
Linus Walleija0323272007-01-07 12:21:30 +0000766 va_list args;
767
768 va_start (args, format);
769 if (params->error_func!=NULL)
770 params->error_func (params->data, format, args);
771 else
772 {
773 vfprintf (stderr, format, args);
774 fprintf (stderr,"\n");
775 fflush (stderr);
776 }
777 va_end (args);
778}
779
Linus Walleijc6210fb2006-05-08 11:11:41 +0000780
Linus Walleij99ed83c2007-01-02 18:46:02 +0000781/*
782 * ptp_read_func() and ptp_write_func() are
Linus Walleijc0a11432007-03-02 21:21:18 +0000783 * based on same functions usb.c in libgphoto2.
Linus Walleij99ed83c2007-01-02 18:46:02 +0000784 * Much reading packet logs and having fun with trials and errors
785 * reveals that WMP / Windows is probably using an algorithm like this
786 * for large transfers:
787 *
Linus Walleij49557482010-06-22 09:14:47 +0000788 * 1. Send the command (0x0c bytes) if headers are split, else, send
Linus Walleij99ed83c2007-01-02 18:46:02 +0000789 * command plus sizeof(endpoint) - 0x0c bytes.
790 * 2. Send first packet, max size to be sizeof(endpoint) but only when using
791 * split headers. Else goto 3.
792 * 3. REPEAT send 0x10000 byte chunks UNTIL remaining bytes < 0x10000
793 * We call 0x10000 CONTEXT_BLOCK_SIZE.
794 * 4. Send remaining bytes MOD sizeof(endpoint)
795 * 5. Send remaining bytes. If this happens to be exactly sizeof(endpoint)
796 * then also send a zero-length package.
Linus Walleij049f50c2007-03-03 20:30:43 +0000797 *
798 * Further there is some special quirks to handle zero reads from the
799 * device, since some devices can't do them at all due to shortcomings
800 * of the USB slave controller in the device.
Linus Walleij99ed83c2007-01-02 18:46:02 +0000801 */
Richard Low4fd59d62007-03-03 16:34:37 +0000802#define CONTEXT_BLOCK_SIZE_1 0x3e00
803#define CONTEXT_BLOCK_SIZE_2 0x200
804#define CONTEXT_BLOCK_SIZE CONTEXT_BLOCK_SIZE_1+CONTEXT_BLOCK_SIZE_2
Linus Walleijb02a0662006-04-25 08:05:09 +0000805static short
Linus Walleij784ac3f2006-12-29 10:36:51 +0000806ptp_read_func (
807 unsigned long size, PTPDataHandler *handler,void *data,
Richard Low4df32f82007-01-11 20:04:39 +0000808 unsigned long *readbytes,
809 int readzero
Linus Walleij784ac3f2006-12-29 10:36:51 +0000810) {
Linus Walleijb02a0662006-04-25 08:05:09 +0000811 PTP_USB *ptp_usb = (PTP_USB *)data;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000812 unsigned long toread = 0;
Linus Walleijd6a49972006-05-02 08:24:54 +0000813 int result = 0;
Linus Walleij99ed83c2007-01-02 18:46:02 +0000814 unsigned long curread = 0;
815 unsigned long written;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000816 unsigned char *bytes;
Linus Walleij049f50c2007-03-03 20:30:43 +0000817 int expect_terminator_byte = 0;
Linus Walleij99ed83c2007-01-02 18:46:02 +0000818
819 // This is the largest block we'll need to read in.
Linus Walleij784ac3f2006-12-29 10:36:51 +0000820 bytes = malloc(CONTEXT_BLOCK_SIZE);
Linus Walleijd6a49972006-05-02 08:24:54 +0000821 while (curread < size) {
nicklas79daadbf22009-09-28 18:19:34 +0000822
823 LIBMTP_USB_DEBUG("Remaining size to read: 0x%04lx bytes\n", size - curread);
824
Richard Low4fd59d62007-03-03 16:34:37 +0000825 // check equal to condition here
826 if (size - curread < CONTEXT_BLOCK_SIZE)
827 {
828 // this is the last packet
829 toread = size - curread;
830 // this is equivalent to zero read for these devices
Linus Walleijfec4d562008-06-01 22:30:36 +0000831 if (readzero && FLAG_NO_ZERO_READS(ptp_usb) && toread % 64 == 0) {
Richard Low4fd59d62007-03-03 16:34:37 +0000832 toread += 1;
Linus Walleij049f50c2007-03-03 20:30:43 +0000833 expect_terminator_byte = 1;
Richard Low4fd59d62007-03-03 16:34:37 +0000834 }
Linus Walleijc49c6372007-01-09 00:18:51 +0000835 }
Richard Low4fd59d62007-03-03 16:34:37 +0000836 else if (curread == 0)
837 // we are first packet, but not last packet
838 toread = CONTEXT_BLOCK_SIZE_1;
839 else if (toread == CONTEXT_BLOCK_SIZE_1)
840 toread = CONTEXT_BLOCK_SIZE_2;
841 else if (toread == CONTEXT_BLOCK_SIZE_2)
842 toread = CONTEXT_BLOCK_SIZE_1;
843 else
nicklas79daadbf22009-09-28 18:19:34 +0000844 LIBMTP_INFO("unexpected toread size 0x%04x, 0x%04x remaining bytes\n",
Linus Walleij4cec9872007-03-03 21:00:53 +0000845 (unsigned int) toread, (unsigned int) (size-curread));
Linus Walleij784ac3f2006-12-29 10:36:51 +0000846
nicklas79daadbf22009-09-28 18:19:34 +0000847 LIBMTP_USB_DEBUG("Reading in 0x%04lx bytes\n", toread);
848
Linus Walleij2f622812008-08-30 22:06:58 +0000849 result = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep, (char*)bytes, toread, ptp_usb->timeout);
nicklas79daadbf22009-09-28 18:19:34 +0000850
851 LIBMTP_USB_DEBUG("Result of read: 0x%04x\n", result);
Linus Walleij49557482010-06-22 09:14:47 +0000852
Linus Walleijc49c6372007-01-09 00:18:51 +0000853 if (result < 0) {
Richard Low43fdb882006-09-06 16:19:05 +0000854 return PTP_ERROR_IO;
Linus Walleijc49c6372007-01-09 00:18:51 +0000855 }
nicklas79daadbf22009-09-28 18:19:34 +0000856
857 LIBMTP_USB_DEBUG("<==USB IN\n");
Richard Low4df32f82007-01-11 20:04:39 +0000858 if (result == 0)
nicklas79daadbf22009-09-28 18:19:34 +0000859 LIBMTP_USB_DEBUG("Zero Read\n");
Richard Low4df32f82007-01-11 20:04:39 +0000860 else
nicklas79daadbf22009-09-28 18:19:34 +0000861 LIBMTP_USB_DATA(bytes, result, 16);
Linus Walleij49557482010-06-22 09:14:47 +0000862
Richard Low4fd59d62007-03-03 16:34:37 +0000863 // want to discard extra byte
Linus Walleij049f50c2007-03-03 20:30:43 +0000864 if (expect_terminator_byte && result == toread)
Richard Lowbf2675c2007-03-18 18:20:26 +0000865 {
nicklas79daadbf22009-09-28 18:19:34 +0000866 LIBMTP_USB_DEBUG("<==USB IN\nDiscarding extra byte\n");
867
Richard Low4fd59d62007-03-03 16:34:37 +0000868 result--;
Richard Lowbf2675c2007-03-18 18:20:26 +0000869 }
Linus Walleij49557482010-06-22 09:14:47 +0000870
Linus Walleijd866d242009-08-23 21:50:39 +0000871 int putfunc_ret = handler->putfunc(NULL, handler->priv, result, bytes, &written);
Richard Lowd24f9232009-05-03 10:22:08 +0000872 if (putfunc_ret != PTP_RC_OK)
873 return putfunc_ret;
Linus Walleij49557482010-06-22 09:14:47 +0000874
Linus Walleijc49c6372007-01-09 00:18:51 +0000875 ptp_usb->current_transfer_complete += result;
Linus Walleijd6a49972006-05-02 08:24:54 +0000876 curread += result;
Linus Walleijc49c6372007-01-09 00:18:51 +0000877
878 // Increase counters, call callback
879 if (ptp_usb->callback_active) {
880 if (ptp_usb->current_transfer_complete >= ptp_usb->current_transfer_total) {
881 // send last update and disable callback.
882 ptp_usb->current_transfer_complete = ptp_usb->current_transfer_total;
883 ptp_usb->callback_active = 0;
884 }
885 if (ptp_usb->current_transfer_callback != NULL) {
Linus Walleijbd3bf9e2007-09-14 19:31:54 +0000886 int ret;
887 ret = ptp_usb->current_transfer_callback(ptp_usb->current_transfer_complete,
888 ptp_usb->current_transfer_total,
889 ptp_usb->current_transfer_callback_data);
890 if (ret != 0) {
891 return PTP_ERROR_CANCEL;
892 }
Linus Walleijc49c6372007-01-09 00:18:51 +0000893 }
Linus Walleij49557482010-06-22 09:14:47 +0000894 }
Linus Walleijc49c6372007-01-09 00:18:51 +0000895
Linus Walleijd6a49972006-05-02 08:24:54 +0000896 if (result < toread) /* short reads are common */
897 break;
898 }
Linus Walleij784ac3f2006-12-29 10:36:51 +0000899 if (readbytes) *readbytes = curread;
900 free (bytes);
Linus Walleij49557482010-06-22 09:14:47 +0000901
Richard Low4df32f82007-01-11 20:04:39 +0000902 // there might be a zero packet waiting for us...
Linus Walleij49557482010-06-22 09:14:47 +0000903 if (readzero &&
904 !FLAG_NO_ZERO_READS(ptp_usb) &&
Linus Walleij049f50c2007-03-03 20:30:43 +0000905 curread % ptp_usb->outep_maxpacket == 0) {
Richard Low4df32f82007-01-11 20:04:39 +0000906 char temp;
907 int zeroresult = 0;
Linus Walleij049f50c2007-03-03 20:30:43 +0000908
nicklas79daadbf22009-09-28 18:19:34 +0000909 LIBMTP_USB_DEBUG("<==USB IN\n");
910 LIBMTP_USB_DEBUG("Zero Read\n");
911
Linus Walleij2f622812008-08-30 22:06:58 +0000912 zeroresult = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep, &temp, 0, ptp_usb->timeout);
Richard Low4df32f82007-01-11 20:04:39 +0000913 if (zeroresult != 0)
nicklas79daadbf22009-09-28 18:19:34 +0000914 LIBMTP_INFO("LIBMTP panic: unable to read in zero packet, response 0x%04x", zeroresult);
Richard Low4df32f82007-01-11 20:04:39 +0000915 }
Linus Walleij49557482010-06-22 09:14:47 +0000916
Richard Low4d9165f2008-09-23 20:13:17 +0000917 return PTP_RC_OK;
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000918}
919
Linus Walleijb02a0662006-04-25 08:05:09 +0000920static short
Linus Walleij784ac3f2006-12-29 10:36:51 +0000921ptp_write_func (
922 unsigned long size,
923 PTPDataHandler *handler,
924 void *data,
925 unsigned long *written
926) {
Linus Walleijb02a0662006-04-25 08:05:09 +0000927 PTP_USB *ptp_usb = (PTP_USB *)data;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000928 unsigned long towrite = 0;
Linus Walleijd6a49972006-05-02 08:24:54 +0000929 int result = 0;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000930 unsigned long curwrite = 0;
931 unsigned char *bytes;
Linus Walleij99ed83c2007-01-02 18:46:02 +0000932
Linus Walleij49557482010-06-22 09:14:47 +0000933 // This is the largest block we'll need to read in.
Linus Walleij784ac3f2006-12-29 10:36:51 +0000934 bytes = malloc(CONTEXT_BLOCK_SIZE);
Linus Walleijc49c6372007-01-09 00:18:51 +0000935 if (!bytes) {
936 return PTP_ERROR_IO;
937 }
Linus Walleijd6a49972006-05-02 08:24:54 +0000938 while (curwrite < size) {
Linus Walleijb84b3852009-03-04 15:11:08 +0000939 unsigned long usbwritten = 0;
Linus Walleijd6a49972006-05-02 08:24:54 +0000940 towrite = size-curwrite;
Linus Walleijc49c6372007-01-09 00:18:51 +0000941 if (towrite > CONTEXT_BLOCK_SIZE) {
Richard Low43fdb882006-09-06 16:19:05 +0000942 towrite = CONTEXT_BLOCK_SIZE;
Linus Walleijc49c6372007-01-09 00:18:51 +0000943 } else {
944 // This magic makes packets the same size that WMP send them.
945 if (towrite > ptp_usb->outep_maxpacket && towrite % ptp_usb->outep_maxpacket != 0) {
Richard Low021421e2007-01-01 18:08:57 +0000946 towrite -= towrite % ptp_usb->outep_maxpacket;
Linus Walleijc49c6372007-01-09 00:18:51 +0000947 }
948 }
Linus Walleijd866d242009-08-23 21:50:39 +0000949 int getfunc_ret = handler->getfunc(NULL, handler->priv,towrite,bytes,&towrite);
Richard Lowd24f9232009-05-03 10:22:08 +0000950 if (getfunc_ret != PTP_RC_OK)
951 return getfunc_ret;
Linus Walleijb84b3852009-03-04 15:11:08 +0000952 while (usbwritten < towrite) {
953 result = USB_BULK_WRITE(ptp_usb->handle,ptp_usb->outep,((char*)bytes+usbwritten),towrite-usbwritten,ptp_usb->timeout);
nicklas79daadbf22009-09-28 18:19:34 +0000954
955 LIBMTP_USB_DEBUG("USB OUT==>\n");
956 LIBMTP_USB_DATA(bytes+usbwritten, result, 16);
957
Linus Walleijb84b3852009-03-04 15:11:08 +0000958 if (result < 0) {
959 return PTP_ERROR_IO;
960 }
961 // check for result == 0 perhaps too.
962 // Increase counters
963 ptp_usb->current_transfer_complete += result;
964 curwrite += result;
965 usbwritten += result;
Linus Walleijc49c6372007-01-09 00:18:51 +0000966 }
Linus Walleijc49c6372007-01-09 00:18:51 +0000967 // call callback
968 if (ptp_usb->callback_active) {
969 if (ptp_usb->current_transfer_complete >= ptp_usb->current_transfer_total) {
970 // send last update and disable callback.
971 ptp_usb->current_transfer_complete = ptp_usb->current_transfer_total;
972 ptp_usb->callback_active = 0;
973 }
974 if (ptp_usb->current_transfer_callback != NULL) {
Linus Walleijbd3bf9e2007-09-14 19:31:54 +0000975 int ret;
976 ret = ptp_usb->current_transfer_callback(ptp_usb->current_transfer_complete,
977 ptp_usb->current_transfer_total,
978 ptp_usb->current_transfer_callback_data);
979 if (ret != 0) {
980 return PTP_ERROR_CANCEL;
981 }
Linus Walleijc49c6372007-01-09 00:18:51 +0000982 }
983 }
Linus Walleijd6a49972006-05-02 08:24:54 +0000984 if (result < towrite) /* short writes happen */
985 break;
986 }
Linus Walleij784ac3f2006-12-29 10:36:51 +0000987 free (bytes);
Linus Walleijc49c6372007-01-09 00:18:51 +0000988 if (written) {
989 *written = curwrite;
Linus Walleij80d134a2006-08-22 21:41:37 +0000990 }
Linus Walleijc49c6372007-01-09 00:18:51 +0000991
Richard Lowef05b892007-01-03 21:18:56 +0000992 // If this is the last transfer send a zero write if required
993 if (ptp_usb->current_transfer_complete >= ptp_usb->current_transfer_total) {
Richard Low68f45882007-01-03 10:08:31 +0000994 if ((towrite % ptp_usb->outep_maxpacket) == 0) {
nicklas79daadbf22009-09-28 18:19:34 +0000995
996 LIBMTP_USB_DEBUG("USB OUT==>\n");
997 LIBMTP_USB_DEBUG("Zero Write\n");
998
Linus Walleij2f622812008-08-30 22:06:58 +0000999 result=USB_BULK_WRITE(ptp_usb->handle,ptp_usb->outep,(char *)"x",0,ptp_usb->timeout);
Linus Walleij7f0c72e2006-09-06 13:01:58 +00001000 }
Linus Walleijd214b9b2006-08-26 22:08:37 +00001001 }
Linus Walleij49557482010-06-22 09:14:47 +00001002
Linus Walleijd6a49972006-05-02 08:24:54 +00001003 if (result < 0)
1004 return PTP_ERROR_IO;
1005 return PTP_RC_OK;
Linus Walleijb02a0662006-04-25 08:05:09 +00001006}
1007
Linus Walleija0323272007-01-07 12:21:30 +00001008/* memory data get/put handler */
1009typedef struct {
1010 unsigned char *data;
1011 unsigned long size, curoff;
1012} PTPMemHandlerPrivate;
Linus Walleij784ac3f2006-12-29 10:36:51 +00001013
Linus Walleija0323272007-01-07 12:21:30 +00001014static uint16_t
1015memory_getfunc(PTPParams* params, void* private,
1016 unsigned long wantlen, unsigned char *data,
1017 unsigned long *gotlen
1018) {
1019 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)private;
1020 unsigned long tocopy = wantlen;
1021
1022 if (priv->curoff + tocopy > priv->size)
1023 tocopy = priv->size - priv->curoff;
1024 memcpy (data, priv->data + priv->curoff, tocopy);
1025 priv->curoff += tocopy;
1026 *gotlen = tocopy;
1027 return PTP_RC_OK;
Linus Walleijb02a0662006-04-25 08:05:09 +00001028}
1029
Linus Walleija0323272007-01-07 12:21:30 +00001030static uint16_t
1031memory_putfunc(PTPParams* params, void* private,
1032 unsigned long sendlen, unsigned char *data,
1033 unsigned long *putlen
1034) {
1035 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)private;
1036
1037 if (priv->curoff + sendlen > priv->size) {
1038 priv->data = realloc (priv->data, priv->curoff+sendlen);
1039 priv->size = priv->curoff + sendlen;
1040 }
1041 memcpy (priv->data + priv->curoff, data, sendlen);
1042 priv->curoff += sendlen;
1043 *putlen = sendlen;
1044 return PTP_RC_OK;
1045}
1046
1047/* init private struct for receiving data. */
1048static uint16_t
1049ptp_init_recv_memory_handler(PTPDataHandler *handler) {
1050 PTPMemHandlerPrivate* priv;
1051 priv = malloc (sizeof(PTPMemHandlerPrivate));
Linus Walleijd866d242009-08-23 21:50:39 +00001052 handler->priv = priv;
Linus Walleija0323272007-01-07 12:21:30 +00001053 handler->getfunc = memory_getfunc;
1054 handler->putfunc = memory_putfunc;
1055 priv->data = NULL;
1056 priv->size = 0;
1057 priv->curoff = 0;
1058 return PTP_RC_OK;
1059}
1060
1061/* init private struct and put data in for sending data.
1062 * data is still owned by caller.
1063 */
1064static uint16_t
1065ptp_init_send_memory_handler(PTPDataHandler *handler,
1066 unsigned char *data, unsigned long len
1067) {
1068 PTPMemHandlerPrivate* priv;
1069 priv = malloc (sizeof(PTPMemHandlerPrivate));
1070 if (!priv)
1071 return PTP_RC_GeneralError;
Linus Walleijd866d242009-08-23 21:50:39 +00001072 handler->priv = priv;
Linus Walleija0323272007-01-07 12:21:30 +00001073 handler->getfunc = memory_getfunc;
1074 handler->putfunc = memory_putfunc;
1075 priv->data = data;
1076 priv->size = len;
1077 priv->curoff = 0;
1078 return PTP_RC_OK;
1079}
1080
1081/* free private struct + data */
1082static uint16_t
1083ptp_exit_send_memory_handler (PTPDataHandler *handler) {
Linus Walleijd866d242009-08-23 21:50:39 +00001084 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)handler->priv;
Linus Walleija0323272007-01-07 12:21:30 +00001085 /* data is owned by caller */
1086 free (priv);
1087 return PTP_RC_OK;
1088}
1089
1090/* hand over our internal data to caller */
1091static uint16_t
1092ptp_exit_recv_memory_handler (PTPDataHandler *handler,
1093 unsigned char **data, unsigned long *size
1094) {
Linus Walleijd866d242009-08-23 21:50:39 +00001095 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)handler->priv;
Linus Walleija0323272007-01-07 12:21:30 +00001096 *data = priv->data;
1097 *size = priv->size;
1098 free (priv);
1099 return PTP_RC_OK;
1100}
1101
1102/* send / receive functions */
1103
1104uint16_t
1105ptp_usb_sendreq (PTPParams* params, PTPContainer* req)
1106{
1107 uint16_t ret;
1108 PTPUSBBulkContainer usbreq;
1109 PTPDataHandler memhandler;
Linus Walleij5ba40402008-05-23 21:36:54 +00001110 unsigned long written = 0;
1111 unsigned long towrite;
nicklas79daadbf22009-09-28 18:19:34 +00001112
Linus Walleijb8f78c72008-03-02 20:22:56 +00001113 char txt[256];
Linus Walleija0323272007-01-07 12:21:30 +00001114
Linus Walleijb8f78c72008-03-02 20:22:56 +00001115 (void) ptp_render_opcode (params, req->Code, sizeof(txt), txt);
nicklas79daadbf22009-09-28 18:19:34 +00001116 LIBMTP_USB_DEBUG("REQUEST: 0x%04x, %s\n", req->Code, txt);
1117
Linus Walleija0323272007-01-07 12:21:30 +00001118 /* build appropriate USB container */
1119 usbreq.length=htod32(PTP_USB_BULK_REQ_LEN-
1120 (sizeof(uint32_t)*(5-req->Nparam)));
1121 usbreq.type=htod16(PTP_USB_CONTAINER_COMMAND);
1122 usbreq.code=htod16(req->Code);
1123 usbreq.trans_id=htod32(req->Transaction_ID);
1124 usbreq.payload.params.param1=htod32(req->Param1);
1125 usbreq.payload.params.param2=htod32(req->Param2);
1126 usbreq.payload.params.param3=htod32(req->Param3);
1127 usbreq.payload.params.param4=htod32(req->Param4);
1128 usbreq.payload.params.param5=htod32(req->Param5);
1129 /* send it to responder */
1130 towrite = PTP_USB_BULK_REQ_LEN-(sizeof(uint32_t)*(5-req->Nparam));
1131 ptp_init_send_memory_handler (&memhandler, (unsigned char*)&usbreq, towrite);
1132 ret=ptp_write_func(
1133 towrite,
1134 &memhandler,
1135 params->data,
1136 &written
1137 );
1138 ptp_exit_send_memory_handler (&memhandler);
Linus Walleijff01cb12007-09-16 19:49:48 +00001139 if (ret!=PTP_RC_OK && ret!=PTP_ERROR_CANCEL) {
Linus Walleija0323272007-01-07 12:21:30 +00001140 ret = PTP_ERROR_IO;
Linus Walleija0323272007-01-07 12:21:30 +00001141 }
Linus Walleij724f0102007-09-17 20:10:12 +00001142 if (written != towrite && ret != PTP_ERROR_CANCEL && ret != PTP_ERROR_IO) {
Linus Walleij49557482010-06-22 09:14:47 +00001143 libusb_glue_error (params,
Linus Walleija0323272007-01-07 12:21:30 +00001144 "PTP: request code 0x%04x sending req wrote only %ld bytes instead of %d",
Richard Lowd284f072007-02-17 08:52:41 +00001145 req->Code, written, towrite
Linus Walleija0323272007-01-07 12:21:30 +00001146 );
1147 ret = PTP_ERROR_IO;
1148 }
1149 return ret;
1150}
1151
1152uint16_t
1153ptp_usb_senddata (PTPParams* params, PTPContainer* ptp,
1154 unsigned long size, PTPDataHandler *handler
1155) {
1156 uint16_t ret;
1157 int wlen, datawlen;
1158 unsigned long written;
1159 PTPUSBBulkContainer usbdata;
1160 uint32_t bytes_left_to_transfer;
1161 PTPDataHandler memhandler;
1162
nicklas79daadbf22009-09-28 18:19:34 +00001163
1164 LIBMTP_USB_DEBUG("SEND DATA PHASE\n");
1165
Linus Walleija0323272007-01-07 12:21:30 +00001166 /* build appropriate USB container */
1167 usbdata.length = htod32(PTP_USB_BULK_HDR_LEN+size);
1168 usbdata.type = htod16(PTP_USB_CONTAINER_DATA);
1169 usbdata.code = htod16(ptp->Code);
1170 usbdata.trans_id= htod32(ptp->Transaction_ID);
Linus Walleij14735952010-04-25 04:56:49 +00001171
Linus Walleija0323272007-01-07 12:21:30 +00001172 ((PTP_USB*)params->data)->current_transfer_complete = 0;
Richard Low4eb01122007-02-24 10:11:32 +00001173 ((PTP_USB*)params->data)->current_transfer_total = size+PTP_USB_BULK_HDR_LEN;
Linus Walleija0323272007-01-07 12:21:30 +00001174
1175 if (params->split_header_data) {
1176 datawlen = 0;
1177 wlen = PTP_USB_BULK_HDR_LEN;
1178 } else {
1179 unsigned long gotlen;
1180 /* For all camera devices. */
Richard Low02724f62007-02-17 09:47:26 +00001181 datawlen = (size<PTP_USB_BULK_PAYLOAD_LEN_WRITE)?size:PTP_USB_BULK_PAYLOAD_LEN_WRITE;
Linus Walleija0323272007-01-07 12:21:30 +00001182 wlen = PTP_USB_BULK_HDR_LEN + datawlen;
Linus Walleij14735952010-04-25 04:56:49 +00001183
Linus Walleijd866d242009-08-23 21:50:39 +00001184 ret = handler->getfunc(params, handler->priv, datawlen, usbdata.payload.data, &gotlen);
Linus Walleija0323272007-01-07 12:21:30 +00001185 if (ret != PTP_RC_OK)
1186 return ret;
1187 if (gotlen != datawlen)
1188 return PTP_RC_GeneralError;
1189 }
1190 ptp_init_send_memory_handler (&memhandler, (unsigned char *)&usbdata, wlen);
1191 /* send first part of data */
1192 ret = ptp_write_func(wlen, &memhandler, params->data, &written);
1193 ptp_exit_send_memory_handler (&memhandler);
1194 if (ret!=PTP_RC_OK) {
Linus Walleija0323272007-01-07 12:21:30 +00001195 return ret;
1196 }
1197 if (size <= datawlen) return ret;
1198 /* if everything OK send the rest */
1199 bytes_left_to_transfer = size-datawlen;
1200 ret = PTP_RC_OK;
1201 while(bytes_left_to_transfer > 0) {
1202 ret = ptp_write_func (bytes_left_to_transfer, handler, params->data, &written);
1203 if (ret != PTP_RC_OK)
1204 break;
1205 if (written == 0) {
1206 ret = PTP_ERROR_IO;
1207 break;
1208 }
1209 bytes_left_to_transfer -= written;
1210 }
Linus Walleijff01cb12007-09-16 19:49:48 +00001211 if (ret!=PTP_RC_OK && ret!=PTP_ERROR_CANCEL)
Linus Walleija0323272007-01-07 12:21:30 +00001212 ret = PTP_ERROR_IO;
1213 return ret;
1214}
1215
1216static uint16_t ptp_usb_getpacket(PTPParams *params,
1217 PTPUSBBulkContainer *packet, unsigned long *rlen)
1218{
1219 PTPDataHandler memhandler;
1220 uint16_t ret;
1221 unsigned char *x = NULL;
1222
1223 /* read the header and potentially the first data */
1224 if (params->response_packet_size > 0) {
1225 /* If there is a buffered packet, just use it. */
1226 memcpy(packet, params->response_packet, params->response_packet_size);
1227 *rlen = params->response_packet_size;
1228 free(params->response_packet);
1229 params->response_packet = NULL;
1230 params->response_packet_size = 0;
1231 /* Here this signifies a "virtual read" */
1232 return PTP_RC_OK;
1233 }
1234 ptp_init_recv_memory_handler (&memhandler);
Richard Low02724f62007-02-17 09:47:26 +00001235 ret = ptp_read_func(PTP_USB_BULK_HS_MAX_PACKET_LEN_READ, &memhandler, params->data, rlen, 0);
Linus Walleija0323272007-01-07 12:21:30 +00001236 ptp_exit_recv_memory_handler (&memhandler, &x, rlen);
1237 if (x) {
1238 memcpy (packet, x, *rlen);
1239 free (x);
1240 }
1241 return ret;
1242}
1243
1244uint16_t
1245ptp_usb_getdata (PTPParams* params, PTPContainer* ptp, PTPDataHandler *handler)
1246{
1247 uint16_t ret;
1248 PTPUSBBulkContainer usbdata;
Linus Walleija0323272007-01-07 12:21:30 +00001249 unsigned long written;
Linus Walleij8e3af002007-09-28 19:21:54 +00001250 PTP_USB *ptp_usb = (PTP_USB *) params->data;
Linus Walleij14735952010-04-25 04:56:49 +00001251 int putfunc_ret;
nicklas79daadbf22009-09-28 18:19:34 +00001252
1253 LIBMTP_USB_DEBUG("GET DATA PHASE\n");
1254
Linus Walleija0323272007-01-07 12:21:30 +00001255 memset(&usbdata,0,sizeof(usbdata));
1256 do {
1257 unsigned long len, rlen;
1258
1259 ret = ptp_usb_getpacket(params, &usbdata, &rlen);
1260 if (ret!=PTP_RC_OK) {
1261 ret = PTP_ERROR_IO;
1262 break;
Linus Walleij8e3af002007-09-28 19:21:54 +00001263 }
Linus Walleija0323272007-01-07 12:21:30 +00001264 if (dtoh16(usbdata.type)!=PTP_USB_CONTAINER_DATA) {
1265 ret = PTP_ERROR_DATA_EXPECTED;
1266 break;
Linus Walleij8e3af002007-09-28 19:21:54 +00001267 }
Linus Walleija0323272007-01-07 12:21:30 +00001268 if (dtoh16(usbdata.code)!=ptp->Code) {
Linus Walleijfec4d562008-06-01 22:30:36 +00001269 if (FLAG_IGNORE_HEADER_ERRORS(ptp_usb)) {
Linus Walleijd1e14e02008-12-14 01:07:30 +00001270 libusb_glue_debug (params, "ptp2/ptp_usb_getdata: detected a broken "
Linus Walleij913a3062007-09-28 21:42:39 +00001271 "PTP header, code field insane, expect problems! (But continuing)");
Linus Walleij06542122007-10-14 22:13:48 +00001272 // Repair the header, so it won't wreak more havoc, don't just ignore it.
1273 // Typically these two fields will be broken.
1274 usbdata.code = htod16(ptp->Code);
1275 usbdata.trans_id = htod32(ptp->Transaction_ID);
Linus Walleij94cd9722007-09-28 21:29:53 +00001276 ret = PTP_RC_OK;
Linus Walleij8e3af002007-09-28 19:21:54 +00001277 } else {
Linus Walleij913a3062007-09-28 21:42:39 +00001278 ret = dtoh16(usbdata.code);
1279 // This filters entirely insane garbage return codes, but still
1280 // makes it possible to return error codes in the code field when
Linus Walleij49557482010-06-22 09:14:47 +00001281 // getting data. It appears Windows ignores the contents of this
Linus Walleij913a3062007-09-28 21:42:39 +00001282 // field entirely.
1283 if (ret < PTP_RC_Undefined || ret > PTP_RC_SpecificationOfDestinationUnsupported) {
Linus Walleijd1e14e02008-12-14 01:07:30 +00001284 libusb_glue_debug (params, "ptp2/ptp_usb_getdata: detected a broken "
Linus Walleij913a3062007-09-28 21:42:39 +00001285 "PTP header, code field insane.");
1286 ret = PTP_ERROR_IO;
1287 }
Linus Walleij8e3af002007-09-28 19:21:54 +00001288 break;
1289 }
Linus Walleija0323272007-01-07 12:21:30 +00001290 }
1291 if (usbdata.length == 0xffffffffU) {
Linus Walleij14735952010-04-25 04:56:49 +00001292 /* Copy first part of data to 'data' */
1293 putfunc_ret =
1294 handler->putfunc(
1295 params, handler->priv, rlen - PTP_USB_BULK_HDR_LEN, usbdata.payload.data,
1296 &written
1297 );
1298 if (putfunc_ret != PTP_RC_OK)
1299 return putfunc_ret;
Linus Walleija0323272007-01-07 12:21:30 +00001300
Linus Walleij14735952010-04-25 04:56:49 +00001301 /* stuff data directly to passed data handler */
1302 while (1) {
1303 unsigned long readdata;
1304 uint16_t xret;
1305
1306 xret = ptp_read_func(
1307 PTP_USB_BULK_HS_MAX_PACKET_LEN_READ,
1308 handler,
1309 params->data,
1310 &readdata,
1311 0
1312 );
1313 if (xret != PTP_RC_OK)
1314 return xret;
1315 if (readdata < PTP_USB_BULK_HS_MAX_PACKET_LEN_READ)
1316 break;
1317 }
1318 return PTP_RC_OK;
Linus Walleija0323272007-01-07 12:21:30 +00001319 }
1320 if (rlen > dtoh32(usbdata.length)) {
1321 /*
1322 * Buffer the surplus response packet if it is >=
1323 * PTP_USB_BULK_HDR_LEN
1324 * (i.e. it is probably an entire package)
1325 * else discard it as erroneous surplus data.
1326 * This will even work if more than 2 packets appear
1327 * in the same transaction, they will just be handled
1328 * iteratively.
1329 *
1330 * Marcus observed stray bytes on iRiver devices;
1331 * these are still discarded.
1332 */
1333 unsigned int packlen = dtoh32(usbdata.length);
1334 unsigned int surplen = rlen - packlen;
1335
1336 if (surplen >= PTP_USB_BULK_HDR_LEN) {
1337 params->response_packet = malloc(surplen);
1338 memcpy(params->response_packet,
1339 (uint8_t *) &usbdata + packlen, surplen);
1340 params->response_packet_size = surplen;
tedbullock36f2afb2007-03-03 22:16:44 +00001341 /* Ignore reading one extra byte if device flags have been set */
Linus Walleijfec4d562008-06-01 22:30:36 +00001342 } else if(!FLAG_NO_ZERO_READS(ptp_usb) &&
1343 (rlen - dtoh32(usbdata.length) == 1)) {
Linus Walleijd1e14e02008-12-14 01:07:30 +00001344 libusb_glue_debug (params, "ptp2/ptp_usb_getdata: read %d bytes "
Linus Walleij49557482010-06-22 09:14:47 +00001345 "too much, expect problems!",
Linus Walleij8e3af002007-09-28 19:21:54 +00001346 rlen - dtoh32(usbdata.length));
Linus Walleija0323272007-01-07 12:21:30 +00001347 }
1348 rlen = packlen;
1349 }
1350
1351 /* For most PTP devices rlen is 512 == sizeof(usbdata)
1352 * here. For MTP devices splitting header and data it might
1353 * be 12.
1354 */
1355 /* Evaluate full data length. */
1356 len=dtoh32(usbdata.length)-PTP_USB_BULK_HDR_LEN;
1357
1358 /* autodetect split header/data MTP devices */
1359 if (dtoh32(usbdata.length) > 12 && (rlen==12))
1360 params->split_header_data = 1;
1361
Linus Walleija0323272007-01-07 12:21:30 +00001362 /* Copy first part of data to 'data' */
Linus Walleij14735952010-04-25 04:56:49 +00001363 putfunc_ret =
1364 handler->putfunc(
1365 params, handler->priv, rlen - PTP_USB_BULK_HDR_LEN,
1366 usbdata.payload.data,
1367 &written
1368 );
1369 if (putfunc_ret != PTP_RC_OK)
1370 return putfunc_ret;
Linus Walleijd866d242009-08-23 21:50:39 +00001371
1372 if (FLAG_NO_ZERO_READS(ptp_usb) &&
Linus Walleij049f50c2007-03-03 20:30:43 +00001373 len+PTP_USB_BULK_HDR_LEN == PTP_USB_BULK_HS_MAX_PACKET_LEN_READ) {
nicklas79daadbf22009-09-28 18:19:34 +00001374
1375 LIBMTP_USB_DEBUG("Reading in extra terminating byte\n");
1376
Linus Walleij049f50c2007-03-03 20:30:43 +00001377 // need to read in extra byte and discard it
1378 int result = 0;
1379 char byte = 0;
Linus Walleij2f622812008-08-30 22:06:58 +00001380 result = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep, &byte, 1, ptp_usb->timeout);
Linus Walleijd866d242009-08-23 21:50:39 +00001381
Linus Walleij049f50c2007-03-03 20:30:43 +00001382 if (result != 1)
nicklas79daadbf22009-09-28 18:19:34 +00001383 LIBMTP_INFO("Could not read in extra byte for PTP_USB_BULK_HS_MAX_PACKET_LEN_READ long file, return value 0x%04x\n", result);
Richard Low057ea772007-03-24 13:37:38 +00001384 } else if (len+PTP_USB_BULK_HDR_LEN == PTP_USB_BULK_HS_MAX_PACKET_LEN_READ && params->split_header_data == 0) {
Linus Walleij94cd9722007-09-28 21:29:53 +00001385 int zeroresult = 0;
1386 char zerobyte = 0;
1387
nicklas79daadbf22009-09-28 18:19:34 +00001388
1389 LIBMTP_INFO("Reading in zero packet after header\n");
1390
Linus Walleij14735952010-04-25 04:56:49 +00001391 zeroresult = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep, &zerobyte, 0, ptp_usb->timeout);
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001392
Linus Walleij94cd9722007-09-28 21:29:53 +00001393 if (zeroresult != 0)
nicklas79daadbf22009-09-28 18:19:34 +00001394 LIBMTP_INFO("LIBMTP panic: unable to read in zero packet, response 0x%04x", zeroresult);
Linus Walleij94cd9722007-09-28 21:29:53 +00001395 }
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001396
Linus Walleija0323272007-01-07 12:21:30 +00001397 /* Is that all of data? */
Linus Walleij94cd9722007-09-28 21:29:53 +00001398 if (len+PTP_USB_BULK_HDR_LEN<=rlen) {
1399 break;
1400 }
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001401
Linus Walleij913a3062007-09-28 21:42:39 +00001402 ret = ptp_read_func(len - (rlen - PTP_USB_BULK_HDR_LEN),
1403 handler,
1404 params->data, &rlen, 1);
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001405
Linus Walleija0323272007-01-07 12:21:30 +00001406 if (ret!=PTP_RC_OK) {
Linus Walleij049f50c2007-03-03 20:30:43 +00001407 break;
Linus Walleija0323272007-01-07 12:21:30 +00001408 }
1409 } while (0);
Linus Walleija0323272007-01-07 12:21:30 +00001410 return ret;
1411}
1412
1413uint16_t
1414ptp_usb_getresp (PTPParams* params, PTPContainer* resp)
1415{
1416 uint16_t ret;
1417 unsigned long rlen;
1418 PTPUSBBulkContainer usbresp;
Linus Walleij3e4b1142007-11-01 23:54:16 +00001419 PTP_USB *ptp_usb = (PTP_USB *)(params->data);
Linus Walleija0323272007-01-07 12:21:30 +00001420
nicklas79daadbf22009-09-28 18:19:34 +00001421
1422 LIBMTP_USB_DEBUG("RESPONSE: ");
1423
Linus Walleija0323272007-01-07 12:21:30 +00001424 memset(&usbresp,0,sizeof(usbresp));
1425 /* read response, it should never be longer than sizeof(usbresp) */
1426 ret = ptp_usb_getpacket(params, &usbresp, &rlen);
1427
Linus Walleijb8f78c72008-03-02 20:22:56 +00001428 // Fix for bevahiour reported by Scott Snyder on Samsung YP-U3. The player
1429 // sends a packet containing just zeroes of length 2 (up to 4 has been seen too)
1430 // after a NULL packet when it should send the response. This code ignores
1431 // such illegal packets.
Linus Walleij3191bba2008-02-10 21:16:25 +00001432 while (ret==PTP_RC_OK && rlen<PTP_USB_BULK_HDR_LEN && usbresp.length==0) {
Linus Walleijd1e14e02008-12-14 01:07:30 +00001433 libusb_glue_debug (params, "ptp_usb_getresp: detected short response "
Linus Walleij3191bba2008-02-10 21:16:25 +00001434 "of %d bytes, expect problems! (re-reading "
1435 "response), rlen");
Linus Walleij00411ee2008-01-27 12:24:51 +00001436 ret = ptp_usb_getpacket(params, &usbresp, &rlen);
1437 }
1438
Linus Walleija0323272007-01-07 12:21:30 +00001439 if (ret!=PTP_RC_OK) {
1440 ret = PTP_ERROR_IO;
1441 } else
1442 if (dtoh16(usbresp.type)!=PTP_USB_CONTAINER_RESPONSE) {
1443 ret = PTP_ERROR_RESP_EXPECTED;
1444 } else
1445 if (dtoh16(usbresp.code)!=resp->Code) {
1446 ret = dtoh16(usbresp.code);
1447 }
nicklas79daadbf22009-09-28 18:19:34 +00001448
1449 LIBMTP_USB_DEBUG("%04x\n", ret);
1450
Linus Walleija0323272007-01-07 12:21:30 +00001451 if (ret!=PTP_RC_OK) {
Linus Walleijd1e14e02008-12-14 01:07:30 +00001452/* libusb_glue_error (params,
Linus Walleija0323272007-01-07 12:21:30 +00001453 "PTP: request code 0x%04x getting resp error 0x%04x",
1454 resp->Code, ret);*/
1455 return ret;
1456 }
1457 /* build an appropriate PTPContainer */
1458 resp->Code=dtoh16(usbresp.code);
1459 resp->SessionID=params->session_id;
1460 resp->Transaction_ID=dtoh32(usbresp.trans_id);
Linus Walleijfec4d562008-06-01 22:30:36 +00001461 if (FLAG_IGNORE_HEADER_ERRORS(ptp_usb)) {
Linus Walleij3e4b1142007-11-01 23:54:16 +00001462 if (resp->Transaction_ID != params->transaction_id-1) {
Linus Walleijd1e14e02008-12-14 01:07:30 +00001463 libusb_glue_debug (params, "ptp_usb_getresp: detected a broken "
Linus Walleij00411ee2008-01-27 12:24:51 +00001464 "PTP header, transaction ID insane, expect "
1465 "problems! (But continuing)");
Linus Walleij3e4b1142007-11-01 23:54:16 +00001466 // Repair the header, so it won't wreak more havoc.
1467 resp->Transaction_ID = params->transaction_id-1;
1468 }
1469 }
Linus Walleija0323272007-01-07 12:21:30 +00001470 resp->Param1=dtoh32(usbresp.payload.params.param1);
1471 resp->Param2=dtoh32(usbresp.payload.params.param2);
1472 resp->Param3=dtoh32(usbresp.payload.params.param3);
1473 resp->Param4=dtoh32(usbresp.payload.params.param4);
1474 resp->Param5=dtoh32(usbresp.payload.params.param5);
1475 return ret;
1476}
1477
1478/* Event handling functions */
1479
1480/* PTP Events wait for or check mode */
1481#define PTP_EVENT_CHECK 0x0000 /* waits for */
1482#define PTP_EVENT_CHECK_FAST 0x0001 /* checks */
1483
1484static inline uint16_t
1485ptp_usb_event (PTPParams* params, PTPContainer* event, int wait)
1486{
1487 uint16_t ret;
1488 int result;
1489 unsigned long rlen;
1490 PTPUSBEventContainer usbevent;
1491 PTP_USB *ptp_usb = (PTP_USB *)(params->data);
1492
1493 memset(&usbevent,0,sizeof(usbevent));
1494
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001495 if ((params==NULL) || (event==NULL))
Linus Walleija0323272007-01-07 12:21:30 +00001496 return PTP_ERROR_BADPARAM;
1497 ret = PTP_RC_OK;
1498 switch(wait) {
1499 case PTP_EVENT_CHECK:
Linus Walleij2f622812008-08-30 22:06:58 +00001500 result=USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *)&usbevent,sizeof(usbevent),ptp_usb->timeout);
Linus Walleija0323272007-01-07 12:21:30 +00001501 if (result==0)
Linus Walleij2f622812008-08-30 22:06:58 +00001502 result = USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *) &usbevent, sizeof(usbevent), ptp_usb->timeout);
Linus Walleija0323272007-01-07 12:21:30 +00001503 if (result < 0) ret = PTP_ERROR_IO;
1504 break;
1505 case PTP_EVENT_CHECK_FAST:
Linus Walleij2f622812008-08-30 22:06:58 +00001506 result=USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *)&usbevent,sizeof(usbevent),ptp_usb->timeout);
Linus Walleija0323272007-01-07 12:21:30 +00001507 if (result==0)
Linus Walleij2f622812008-08-30 22:06:58 +00001508 result = USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *) &usbevent, sizeof(usbevent), ptp_usb->timeout);
Linus Walleija0323272007-01-07 12:21:30 +00001509 if (result < 0) ret = PTP_ERROR_IO;
1510 break;
1511 default:
1512 ret=PTP_ERROR_BADPARAM;
1513 break;
1514 }
1515 if (ret!=PTP_RC_OK) {
Linus Walleijd1e14e02008-12-14 01:07:30 +00001516 libusb_glue_error (params,
Linus Walleija0323272007-01-07 12:21:30 +00001517 "PTP: reading event an error 0x%04x occurred", ret);
1518 return PTP_ERROR_IO;
1519 }
1520 rlen = result;
1521 if (rlen < 8) {
Linus Walleijd1e14e02008-12-14 01:07:30 +00001522 libusb_glue_error (params,
Linus Walleija0323272007-01-07 12:21:30 +00001523 "PTP: reading event an short read of %ld bytes occurred", rlen);
1524 return PTP_ERROR_IO;
1525 }
1526 /* if we read anything over interrupt endpoint it must be an event */
1527 /* build an appropriate PTPContainer */
1528 event->Code=dtoh16(usbevent.code);
1529 event->SessionID=params->session_id;
1530 event->Transaction_ID=dtoh32(usbevent.trans_id);
1531 event->Param1=dtoh32(usbevent.param1);
1532 event->Param2=dtoh32(usbevent.param2);
1533 event->Param3=dtoh32(usbevent.param3);
1534 return ret;
1535}
1536
1537uint16_t
1538ptp_usb_event_check (PTPParams* params, PTPContainer* event) {
1539
1540 return ptp_usb_event (params, event, PTP_EVENT_CHECK_FAST);
1541}
1542
1543uint16_t
1544ptp_usb_event_wait (PTPParams* params, PTPContainer* event) {
1545
1546 return ptp_usb_event (params, event, PTP_EVENT_CHECK);
1547}
1548
Linus Walleijbd3bf9e2007-09-14 19:31:54 +00001549uint16_t
1550ptp_usb_control_cancel_request (PTPParams *params, uint32_t transactionid) {
1551 PTP_USB *ptp_usb = (PTP_USB *)(params->data);
1552 int ret;
1553 unsigned char buffer[6];
1554
1555 htod16a(&buffer[0],PTP_EC_CancelTransaction);
1556 htod32a(&buffer[2],transactionid);
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001557 ret = usb_control_msg(ptp_usb->handle,
Linus Walleijbd3bf9e2007-09-14 19:31:54 +00001558 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
Linus Walleij2f622812008-08-30 22:06:58 +00001559 0x64, 0x0000, 0x0000, (char *) buffer, sizeof(buffer), ptp_usb->timeout);
Linus Walleijbd3bf9e2007-09-14 19:31:54 +00001560 if (ret < sizeof(buffer))
1561 return PTP_ERROR_IO;
1562 return PTP_RC_OK;
1563}
Linus Walleija0323272007-01-07 12:21:30 +00001564
Linus Walleij9eb3d312006-08-04 19:25:59 +00001565static int init_ptp_usb (PTPParams* params, PTP_USB* ptp_usb, struct usb_device* dev)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001566{
Linus Walleijd6a49972006-05-02 08:24:54 +00001567 usb_dev_handle *device_handle;
Linus Walleijfea4f532009-09-22 22:08:35 +00001568 char buf[255];
Linus Walleijf2d747a2010-01-19 00:46:18 +00001569 int usbresult;
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001570
Linus Walleijd6a49972006-05-02 08:24:54 +00001571 params->sendreq_func=ptp_usb_sendreq;
1572 params->senddata_func=ptp_usb_senddata;
1573 params->getresp_func=ptp_usb_getresp;
1574 params->getdata_func=ptp_usb_getdata;
Linus Walleijff01cb12007-09-16 19:49:48 +00001575 params->cancelreq_func=ptp_usb_control_cancel_request;
Linus Walleijd6a49972006-05-02 08:24:54 +00001576 params->data=ptp_usb;
1577 params->transaction_id=0;
Linus Walleij462a5e72007-02-13 06:54:56 +00001578 /*
1579 * This is hardcoded here since we have no devices whatsoever that are BE.
1580 * Change this the day we run into our first BE device (if ever).
1581 */
Linus Walleijd6a49972006-05-02 08:24:54 +00001582 params->byteorder = PTP_DL_LE;
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001583
Linus Walleij2f622812008-08-30 22:06:58 +00001584 ptp_usb->timeout = USB_TIMEOUT_DEFAULT;
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001585
Yavor Goulishev825ff132011-02-26 00:29:01 +01001586 device_handle = usb_open(dev);
1587 if (!device_handle) {
1588 perror("usb_open()");
1589 return -1;
1590 }
1591 ptp_usb->handle = device_handle;
Linus Walleij501ba4d2006-10-16 06:17:04 +00001592#ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP
Yavor Goulishev825ff132011-02-26 00:29:01 +01001593 /*
1594 * If this device is known to be wrongfully claimed by other kernel
1595 * drivers (such as mass storage), then try to unload it to make it
1596 * accessible from user space.
1597 */
1598 if (FLAG_UNLOAD_DRIVER(ptp_usb)) {
1599 if (usb_get_driver_np(device_handle, (int) ptp_usb->interface,
1600 buf, sizeof(buf)) == 0) {
1601 if (usb_detach_kernel_driver_np(device_handle,
1602 (int) ptp_usb->interface)) {
1603 perror("usb_detach_kernel_driver_np()");
1604 return -1;
Linus Walleij501ba4d2006-10-16 06:17:04 +00001605 }
1606 }
Yavor Goulishev825ff132011-02-26 00:29:01 +01001607 }
Linus Walleij501ba4d2006-10-16 06:17:04 +00001608#endif
Linus Walleijeac1e002006-11-30 22:06:02 +00001609#ifdef __WIN32__
Yavor Goulishev825ff132011-02-26 00:29:01 +01001610 // Only needed on Windows, and cause problems on other platforms.
1611 if (usb_set_configuration(device_handle, dev->config->bConfigurationValue)) {
1612 perror("usb_set_configuration()");
1613 return -1;
1614 }
Linus Walleijeac1e002006-11-30 22:06:02 +00001615#endif
Yavor Goulishev825ff132011-02-26 00:29:01 +01001616 // It seems like on kernel 2.6.31 if we already have it open on another
1617 // pthread in our app, we'll get an error if we try to claim it again,
1618 // but that error is harmless because our process already claimed the interface
1619 usbresult = usb_claim_interface(device_handle, (int) ptp_usb->interface);
Linus Walleijf2d747a2010-01-19 00:46:18 +00001620
Yavor Goulishev825ff132011-02-26 00:29:01 +01001621 if (usbresult != 0)
1622 fprintf(stderr, "ignoring usb_claim_interface = %d", usbresult);
Linus Walleijf2d747a2010-01-19 00:46:18 +00001623
Yavor Goulishev825ff132011-02-26 00:29:01 +01001624 // FIXME : Discovered in the Barry project
1625 // kernels >= 2.6.28 don't set the interface the same way as
1626 // previous versions did, and the Blackberry gets confused
1627 // if it isn't explicitly set
1628 // See above, same issue with pthreads means that if this fails it is not
1629 // fatal
1630 // However, this causes problems on Macs so disable here
1631#ifndef __APPLE__
1632 usbresult = usb_set_altinterface(device_handle, 0);
1633 if (usbresult)
1634 fprintf(stderr, "ignoring usb_claim_interface = %d", usbresult);
1635#endif
Linus Walleijf2d747a2010-01-19 00:46:18 +00001636
Yavor Goulishev825ff132011-02-26 00:29:01 +01001637 if (FLAG_SWITCH_MODE_BLACKBERRY(ptp_usb)) {
1638 int ret;
Linus Walleijb6e06e22009-09-22 22:20:49 +00001639
Yavor Goulishev825ff132011-02-26 00:29:01 +01001640 // FIXME : Only for BlackBerry Storm
1641 // What does it mean? Maybe switch mode...
1642 // This first control message is absolutely necessary
1643 usleep(1000);
1644 ret = usb_control_msg(device_handle,
1645 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
1646 0xaa, 0x00, 0x04, buf, 0x40, 1000);
1647 LIBMTP_USB_DEBUG("BlackBerry magic part 1:\n");
1648 LIBMTP_USB_DATA(buf, ret, 16);
nicklas79daadbf22009-09-28 18:19:34 +00001649
Yavor Goulishev825ff132011-02-26 00:29:01 +01001650 usleep(1000);
1651 // This control message is unnecessary
1652 ret = usb_control_msg(device_handle,
1653 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
1654 0xa5, 0x00, 0x01, buf, 0x02, 1000);
1655 LIBMTP_USB_DEBUG("BlackBerry magic part 2:\n");
1656 LIBMTP_USB_DATA(buf, ret, 16);
nicklas79daadbf22009-09-28 18:19:34 +00001657
Yavor Goulishev825ff132011-02-26 00:29:01 +01001658 usleep(1000);
1659 // This control message is unnecessary
1660 ret = usb_control_msg(device_handle,
1661 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
1662 0xa8, 0x00, 0x01, buf, 0x05, 1000);
1663 LIBMTP_USB_DEBUG("BlackBerry magic part 3:\n");
1664 LIBMTP_USB_DATA(buf, ret, 16);
nicklas79daadbf22009-09-28 18:19:34 +00001665
Yavor Goulishev825ff132011-02-26 00:29:01 +01001666 usleep(1000);
1667 // This control message is unnecessary
1668 ret = usb_control_msg(device_handle,
1669 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
1670 0xa8, 0x00, 0x01, buf, 0x11, 1000);
1671 LIBMTP_USB_DEBUG("BlackBerry magic part 4:\n");
1672 LIBMTP_USB_DATA(buf, ret, 16);
nicklas79daadbf22009-09-28 18:19:34 +00001673
Yavor Goulishev825ff132011-02-26 00:29:01 +01001674 usleep(1000);
Linus Walleijd6a49972006-05-02 08:24:54 +00001675 }
Linus Walleij9eb3d312006-08-04 19:25:59 +00001676 return 0;
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001677}
1678
Linus Walleijd6a49972006-05-02 08:24:54 +00001679static void clear_stall(PTP_USB* ptp_usb)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001680{
Linus Walleij58b62792007-01-07 12:38:59 +00001681 uint16_t status;
Linus Walleijd6a49972006-05-02 08:24:54 +00001682 int ret;
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001683
Linus Walleijd6a49972006-05-02 08:24:54 +00001684 /* check the inep status */
Linus Walleij58b62792007-01-07 12:38:59 +00001685 status = 0;
Linus Walleij1fd2d272006-05-08 09:22:01 +00001686 ret = usb_get_endpoint_status(ptp_usb,ptp_usb->inep,&status);
Linus Walleij58b62792007-01-07 12:38:59 +00001687 if (ret<0) {
1688 perror ("inep: usb_get_endpoint_status()");
1689 } else if (status) {
nicklas79daadbf22009-09-28 18:19:34 +00001690 LIBMTP_INFO("Clearing stall on IN endpoint\n");
Linus Walleij58b62792007-01-07 12:38:59 +00001691 ret = usb_clear_stall_feature(ptp_usb,ptp_usb->inep);
1692 if (ret<0) {
1693 perror ("usb_clear_stall_feature()");
1694 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001695 }
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001696
Linus Walleijd6a49972006-05-02 08:24:54 +00001697 /* check the outep status */
Linus Walleij58b62792007-01-07 12:38:59 +00001698 status=0;
1699 ret = usb_get_endpoint_status(ptp_usb,ptp_usb->outep,&status);
1700 if (ret<0) {
1701 perror("outep: usb_get_endpoint_status()");
1702 } else if (status) {
nicklas79daadbf22009-09-28 18:19:34 +00001703 LIBMTP_INFO("Clearing stall on OUT endpoint\n");
Linus Walleij58b62792007-01-07 12:38:59 +00001704 ret = usb_clear_stall_feature(ptp_usb,ptp_usb->outep);
1705 if (ret<0) {
1706 perror("usb_clear_stall_feature()");
1707 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001708 }
Linus Walleij58b62792007-01-07 12:38:59 +00001709
1710 /* TODO: do we need this for INTERRUPT (ptp_usb->intep) too? */
1711}
1712
1713static void clear_halt(PTP_USB* ptp_usb)
1714{
1715 int ret;
1716
1717 ret = usb_clear_halt(ptp_usb->handle,ptp_usb->inep);
1718 if (ret<0) {
1719 perror("usb_clear_halt() on IN endpoint");
1720 }
1721 ret = usb_clear_halt(ptp_usb->handle,ptp_usb->outep);
1722 if (ret<0) {
1723 perror("usb_clear_halt() on OUT endpoint");
1724 }
1725 ret = usb_clear_halt(ptp_usb->handle,ptp_usb->intep);
1726 if (ret<0) {
1727 perror("usb_clear_halt() on INTERRUPT endpoint");
1728 }
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001729}
1730
Linus Walleijb0ab5482007-08-29 21:08:54 +00001731static void close_usb(PTP_USB* ptp_usb)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001732{
Linus Walleijda17cda2007-09-18 21:12:05 +00001733 // Commented out since it was confusing some
1734 // devices to do these things.
Linus Walleijfec4d562008-06-01 22:30:36 +00001735 if (!FLAG_NO_RELEASE_INTERFACE(ptp_usb)) {
Linus Walleijeabc2312008-02-13 00:02:00 +00001736 /*
1737 * Clear any stalled endpoints
1738 * On misbehaving devices designed for Windows/Mac, quote from:
1739 * http://www2.one-eyed-alien.net/~mdharm/linux-usb/target_offenses.txt
1740 * Device does Bad Things(tm) when it gets a GET_STATUS after CLEAR_HALT
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001741 * (...) Windows, when clearing a stall, only sends the CLEAR_HALT command,
1742 * and presumes that the stall has cleared. Some devices actually choke
1743 * if the CLEAR_HALT is followed by a GET_STATUS (used to determine if the
Linus Walleijeabc2312008-02-13 00:02:00 +00001744 * STALL is persistant or not).
1745 */
Linus Walleij29f03ba2007-09-25 19:22:12 +00001746 clear_stall(ptp_usb);
1747 // Clear halts on any endpoints
1748 clear_halt(ptp_usb);
1749 // Added to clear some stuff on the OUT endpoint
1750 // TODO: is this good on the Mac too?
1751 // HINT: some devices may need that you comment these two out too.
1752 usb_resetep(ptp_usb->handle, ptp_usb->outep);
1753 usb_release_interface(ptp_usb->handle, (int) ptp_usb->interface);
1754 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001755 usb_close(ptp_usb->handle);
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001756}
1757
Linus Walleij550d6d52007-01-24 14:32:51 +00001758/**
Linus Walleija700d222008-05-28 23:06:14 +00001759 * Self-explanatory?
1760 */
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001761static int find_interface_and_endpoints(struct usb_device *dev,
1762 uint8_t *interface,
1763 int* inep,
1764 int* inep_maxpacket,
1765 int* outep,
1766 int *outep_maxpacket,
1767 int* intep)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001768{
Linus Walleijd7373cd2007-08-28 21:42:05 +00001769 int i;
1770
1771 // Loop over the device configurations
1772 for (i = 0; i < dev->descriptor.bNumConfigurations; i++) {
Linus Walleijb0ab5482007-08-29 21:08:54 +00001773 uint8_t j;
Linus Walleijd7373cd2007-08-28 21:42:05 +00001774
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001775 // Loop over each configurations interfaces
Linus Walleijd7373cd2007-08-28 21:42:05 +00001776 for (j = 0; j < dev->config[i].bNumInterfaces; j++) {
Linus Walleijb0ab5482007-08-29 21:08:54 +00001777 uint8_t k;
1778 uint8_t no_ep;
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001779 int found_inep = 0;
1780 int found_outep = 0;
1781 int found_intep = 0;
Linus Walleijd7373cd2007-08-28 21:42:05 +00001782 struct usb_endpoint_descriptor *ep;
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001783
1784 // MTP devices shall have 3 endpoints, ignore those interfaces
1785 // that haven't.
1786 no_ep = dev->config[i].interface[j].altsetting->bNumEndpoints;
1787 if (no_ep != 3)
1788 continue;
Linus Walleijb0ab5482007-08-29 21:08:54 +00001789
1790 *interface = dev->config[i].interface[j].altsetting->bInterfaceNumber;
1791 ep = dev->config[i].interface[j].altsetting->endpoint;
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001792
1793 // Loop over the three endpoints to locate two bulk and
1794 // one interrupt endpoint and FAIL if we cannot, and continue.
Linus Walleijb0ab5482007-08-29 21:08:54 +00001795 for (k = 0; k < no_ep; k++) {
Linus Walleije73854e2010-06-22 09:07:34 +00001796 if (ep[k].bmAttributes == USB_ENDPOINT_TYPE_BULK) {
1797 if ((ep[k].bEndpointAddress & USB_ENDPOINT_DIR_MASK) ==
1798 USB_ENDPOINT_DIR_MASK) {
1799 *inep = ep[k].bEndpointAddress;
1800 *inep_maxpacket = ep[k].wMaxPacketSize;
1801 found_inep = 1;
1802 }
1803 if ((ep[k].bEndpointAddress & USB_ENDPOINT_DIR_MASK) == 0) {
Linus Walleij48fda572010-06-22 09:09:03 +00001804 *outep = ep[k].bEndpointAddress;
1805 *outep_maxpacket = ep[k].wMaxPacketSize;
Linus Walleije73854e2010-06-22 09:07:34 +00001806 found_outep = 1;
1807 }
1808 } else if (ep[k].bmAttributes == USB_ENDPOINT_TYPE_INTERRUPT) {
1809 if ((ep[k].bEndpointAddress & USB_ENDPOINT_DIR_MASK) ==
1810 USB_ENDPOINT_DIR_MASK) {
Linus Walleij48fda572010-06-22 09:09:03 +00001811 *intep = ep[k].bEndpointAddress;
Linus Walleije73854e2010-06-22 09:07:34 +00001812 found_intep = 1;
1813 }
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001814 }
Linus Walleijd7373cd2007-08-28 21:42:05 +00001815 }
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001816 if (found_inep && found_outep && found_intep)
1817 // We assigned the endpoints so return here.
1818 return 0;
1819 // Else loop to next interface/config
Linus Walleijd6a49972006-05-02 08:24:54 +00001820 }
1821 }
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001822 return -1;
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001823}
1824
Linus Walleija700d222008-05-28 23:06:14 +00001825/**
1826 * This function assigns params and usbinfo given a raw device
1827 * as input.
1828 * @param device the device to be assigned.
Linus Walleija700d222008-05-28 23:06:14 +00001829 * @param usbinfo a pointer to the new usbinfo.
1830 * @return an error code.
1831 */
Linus Walleij49557482010-06-22 09:14:47 +00001832LIBMTP_error_number_t configure_usb_device(LIBMTP_raw_device_t *device,
Linus Walleij3c643252008-05-31 23:22:28 +00001833 PTPParams *params,
Linus Walleija700d222008-05-28 23:06:14 +00001834 void **usbinfo)
1835{
Linus Walleija700d222008-05-28 23:06:14 +00001836 PTP_USB *ptp_usb;
1837 struct usb_device *libusb_device;
1838 uint16_t ret = 0;
Linus Walleija700d222008-05-28 23:06:14 +00001839 struct usb_bus *bus;
1840 int found = 0;
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001841 int err;
Linus Walleija700d222008-05-28 23:06:14 +00001842
1843 /* See if we can find this raw device again... */
1844 bus = init_usb();
1845 for (; bus != NULL; bus = bus->next) {
1846 if (bus->location == device->bus_location) {
1847 struct usb_device *dev = bus->devices;
1848
1849 for (; dev != NULL; dev = dev->next) {
1850 if(dev->devnum == device->devnum &&
1851 dev->descriptor.idVendor == device->device_entry.vendor_id &&
1852 dev->descriptor.idProduct == device->device_entry.product_id ) {
1853 libusb_device = dev;
1854 found = 1;
1855 break;
1856 }
1857 }
1858 if (found)
1859 break;
1860 }
1861 }
1862 /* Device has gone since detecting raw devices! */
1863 if (!found) {
1864 return LIBMTP_ERROR_NO_DEVICE_ATTACHED;
1865 }
1866
1867 /* Allocate structs */
Linus Walleija700d222008-05-28 23:06:14 +00001868 ptp_usb = (PTP_USB *) malloc(sizeof(PTP_USB));
Linus Walleij3c643252008-05-31 23:22:28 +00001869 if (ptp_usb == NULL) {
Linus Walleija700d222008-05-28 23:06:14 +00001870 return LIBMTP_ERROR_MEMORY_ALLOCATION;
1871 }
1872 /* Start with a blank slate (includes setting device_flags to 0) */
Linus Walleija700d222008-05-28 23:06:14 +00001873 memset(ptp_usb, 0, sizeof(PTP_USB));
1874
Linus Walleijfec4d562008-06-01 22:30:36 +00001875 /* Copy the raw device */
1876 memcpy(&ptp_usb->rawdevice, device, sizeof(LIBMTP_raw_device_t));
Linus Walleij335a81c2008-06-02 23:01:00 +00001877
1878 /*
1879 * Some devices must have their "OS Descriptor" massaged in order
1880 * to work.
1881 */
1882 if (FLAG_ALWAYS_PROBE_DESCRIPTOR(ptp_usb)) {
1883 // Massage the device descriptor
1884 (void) probe_device_descriptor(libusb_device, NULL);
1885 }
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001886
1887 /* Assign interface and endpoints to usbinfo... */
1888 err = find_interface_and_endpoints(libusb_device,
1889 &ptp_usb->interface,
1890 &ptp_usb->inep,
1891 &ptp_usb->inep_maxpacket,
1892 &ptp_usb->outep,
1893 &ptp_usb->outep_maxpacket,
1894 &ptp_usb->intep);
1895
1896 if (err) {
nicklas79daadbf22009-09-28 18:19:34 +00001897 LIBMTP_ERROR("LIBMTP PANIC: Unable to find interface & endpoints of device\n");
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001898 return LIBMTP_ERROR_CONNECTING;
1899 }
1900
Linus Walleija700d222008-05-28 23:06:14 +00001901 /* Attempt to initialize this device */
Linus Walleij3c643252008-05-31 23:22:28 +00001902 if (init_ptp_usb(params, ptp_usb, libusb_device) < 0) {
nicklas79daadbf22009-09-28 18:19:34 +00001903 LIBMTP_ERROR("LIBMTP PANIC: Unable to initialize device\n");
Linus Walleija700d222008-05-28 23:06:14 +00001904 return LIBMTP_ERROR_CONNECTING;
1905 }
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001906
Linus Walleija700d222008-05-28 23:06:14 +00001907 /*
1908 * This works in situations where previous bad applications
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001909 * have not used LIBMTP_Release_Device on exit
Linus Walleija700d222008-05-28 23:06:14 +00001910 */
Linus Walleij3c643252008-05-31 23:22:28 +00001911 if ((ret = ptp_opensession(params, 1)) == PTP_ERROR_IO) {
Linus Walleijf9137dc2010-10-31 17:13:06 +00001912 LIBMTP_ERROR("PTP_ERROR_IO: failed to open session, trying again after resetting USB interface\n");
Linus Walleija700d222008-05-28 23:06:14 +00001913 close_usb(ptp_usb);
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001914
Linus Walleijf9137dc2010-10-31 17:13:06 +00001915 LIBMTP_ERROR("LIBMTP libusb: Attempt to reset device\n");
1916 usb_reset(ptp_usb->handle);
1917
Linus Walleij3c643252008-05-31 23:22:28 +00001918 if(init_ptp_usb(params, ptp_usb, libusb_device) <0) {
Linus Walleijf9137dc2010-10-31 17:13:06 +00001919 LIBMTP_ERROR("LIBMTP PANIC: Could not init USB on second attempt\n");
Linus Walleija700d222008-05-28 23:06:14 +00001920 return LIBMTP_ERROR_CONNECTING;
1921 }
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001922
Linus Walleija700d222008-05-28 23:06:14 +00001923 /* Device has been reset, try again */
Linus Walleijf9137dc2010-10-31 17:13:06 +00001924 if ((ret = ptp_opensession(params, 1)) == PTP_ERROR_IO) {
1925 LIBMTP_ERROR("LIBMTP PANIC: failed to open session on second attempt\n");
1926 return LIBMTP_ERROR_CONNECTING;
1927 }
Linus Walleija700d222008-05-28 23:06:14 +00001928 }
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001929
Linus Walleija700d222008-05-28 23:06:14 +00001930 /* Was the transaction id invalid? Try again */
1931 if (ret == PTP_RC_InvalidTransactionID) {
nicklas79daadbf22009-09-28 18:19:34 +00001932 LIBMTP_ERROR("LIBMTP WARNING: Transaction ID was invalid, increment and try again\n");
Linus Walleij3c643252008-05-31 23:22:28 +00001933 params->transaction_id += 10;
1934 ret = ptp_opensession(params, 1);
Linus Walleija700d222008-05-28 23:06:14 +00001935 }
1936
1937 if (ret != PTP_RC_SessionAlreadyOpened && ret != PTP_RC_OK) {
nicklas79daadbf22009-09-28 18:19:34 +00001938 LIBMTP_ERROR("LIBMTP PANIC: Could not open session! "
Linus Walleija700d222008-05-28 23:06:14 +00001939 "(Return code %d)\n Try to reset the device.\n",
1940 ret);
1941 usb_release_interface(ptp_usb->handle,
1942 (int) ptp_usb->interface);
1943 return LIBMTP_ERROR_CONNECTING;
1944 }
1945
1946 /* OK configured properly */
Linus Walleija700d222008-05-28 23:06:14 +00001947 *usbinfo = (void *) ptp_usb;
1948 return LIBMTP_ERROR_NONE;
1949}
1950
1951
Linus Walleijb0ab5482007-08-29 21:08:54 +00001952void close_device (PTP_USB *ptp_usb, PTPParams *params)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001953{
Linus Walleijd6a49972006-05-02 08:24:54 +00001954 if (ptp_closesession(params)!=PTP_RC_OK)
nicklas79daadbf22009-09-28 18:19:34 +00001955 LIBMTP_ERROR("ERROR: Could not close session!\n");
Linus Walleijb0ab5482007-08-29 21:08:54 +00001956 close_usb(ptp_usb);
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001957}
1958
Linus Walleij2f622812008-08-30 22:06:58 +00001959void set_usb_device_timeout(PTP_USB *ptp_usb, int timeout)
1960{
1961 ptp_usb->timeout = timeout;
1962}
1963
1964void get_usb_device_timeout(PTP_USB *ptp_usb, int *timeout)
1965{
1966 *timeout = ptp_usb->timeout;
1967}
1968
Linus Walleijd6a49972006-05-02 08:24:54 +00001969static int usb_clear_stall_feature(PTP_USB* ptp_usb, int ep)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001970{
Linus Walleijd6a49972006-05-02 08:24:54 +00001971 return (usb_control_msg(ptp_usb->handle,
1972 USB_RECIP_ENDPOINT, USB_REQ_CLEAR_FEATURE, USB_FEATURE_HALT,
Linus Walleij2f622812008-08-30 22:06:58 +00001973 ep, NULL, 0, ptp_usb->timeout));
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001974}
1975
Linus Walleijd6a49972006-05-02 08:24:54 +00001976static int usb_get_endpoint_status(PTP_USB* ptp_usb, int ep, uint16_t* status)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001977{
Linus Walleijd6a49972006-05-02 08:24:54 +00001978 return (usb_control_msg(ptp_usb->handle,
1979 USB_DP_DTH|USB_RECIP_ENDPOINT, USB_REQ_GET_STATUS,
Linus Walleij2f622812008-08-30 22:06:58 +00001980 USB_FEATURE_HALT, ep, (char *)status, 2, ptp_usb->timeout));
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001981}