blob: 6006d04b5a3b759d00c77c558978255cb47c9409 [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 Walleij00411ee2008-01-27 12:24:51 +00006 * Copyright (C) 2005-2008 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{
nicklas79daadbf22009-09-28 18:19:34 +0000135 /*
136 * Some additional libusb debugging please.
137 * We use the same level debug between MTP and USB.
138 */
nicklas7903584082009-09-28 18:20:16 +0000139 if ((LIBMTP_debug & LIBMTP_DEBUG_USB) != 0)
nicklas79a99196a2009-09-28 18:19:57 +0000140 usb_set_debug(9);
nicklas79daadbf22009-09-28 18:19:34 +0000141
Linus Walleij1fd2d272006-05-08 09:22:01 +0000142 usb_init();
143 usb_find_busses();
144 usb_find_devices();
145 return (usb_get_busses());
146}
147
148/**
tedbullock0f033cb2007-02-14 20:56:54 +0000149 * Small recursive function to append a new usb_device to the linked list of
150 * USB MTP devices
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000151 * @param devlist dynamic linked list of pointers to usb devices with MTP
Linus Walleij63fd1e62008-04-23 23:49:43 +0000152 * properties, to be extended with new device.
153 * @param newdevice the new device to add.
154 * @param bus_location bus for this device.
155 * @return an extended array or NULL on failure.
tedbullock0f033cb2007-02-14 20:56:54 +0000156 */
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000157static mtpdevice_list_t *append_to_mtpdevice_list(mtpdevice_list_t *devlist,
Linus Walleij63fd1e62008-04-23 23:49:43 +0000158 struct usb_device *newdevice,
159 uint32_t bus_location)
tedbullock0f033cb2007-02-14 20:56:54 +0000160{
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000161 mtpdevice_list_t *new_list_entry;
tedbullock0f033cb2007-02-14 20:56:54 +0000162
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000163 new_list_entry = (mtpdevice_list_t *) malloc(sizeof(mtpdevice_list_t));
164 if (new_list_entry == NULL) {
165 return NULL;
166 }
167 // Fill in USB device, if we *HAVE* to make a copy of the device do it here.
168 new_list_entry->libusb_device = newdevice;
Linus Walleij63fd1e62008-04-23 23:49:43 +0000169 new_list_entry->bus_location = bus_location;
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000170 new_list_entry->next = NULL;
171
172 if (devlist == NULL) {
173 return new_list_entry;
174 } else {
175 mtpdevice_list_t *tmp = devlist;
176 while (tmp->next != NULL) {
177 tmp = tmp->next;
178 }
179 tmp->next = new_list_entry;
180 }
181 return devlist;
tedbullock0f033cb2007-02-14 20:56:54 +0000182}
183
184/**
185 * Small recursive function to free dynamic memory allocated to the linked list
186 * of USB MTP devices
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000187 * @param devlist dynamic linked list of pointers to usb devices with MTP
tedbullock0f033cb2007-02-14 20:56:54 +0000188 * properties.
189 * @return nothing
190 */
Linus Walleija700d222008-05-28 23:06:14 +0000191static void free_mtpdevice_list(mtpdevice_list_t *devlist)
tedbullock0f033cb2007-02-14 20:56:54 +0000192{
Linus Walleijac061862007-03-07 08:28:33 +0000193 mtpdevice_list_t *tmplist = devlist;
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000194
195 if (devlist == NULL)
196 return;
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000197 while (tmplist != NULL) {
198 mtpdevice_list_t *tmp = tmplist;
199 tmplist = tmplist->next;
Richard Low61edc1a2007-09-23 10:35:48 +0000200 // Do not free() the fields (ptp_usb, params)! These are used elsewhere.
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000201 free(tmp);
tedbullock0f033cb2007-02-14 20:56:54 +0000202 }
tedbullock0f033cb2007-02-14 20:56:54 +0000203 return;
204}
205
206/**
Linus Walleij9521e2b2007-07-10 21:50:42 +0000207 * This checks if a device has an MTP descriptor. The descriptor was
208 * elaborated about in gPhoto bug 1482084, and some official documentation
209 * with no strings attached was published by Microsoft at
210 * http://www.microsoft.com/whdc/system/bus/USB/USBFAQ_intermed.mspx#E3HAC
211 *
Linus Walleija4942fc2007-03-12 19:23:21 +0000212 * @param dev a device struct from libusb.
Linus Walleij9521e2b2007-07-10 21:50:42 +0000213 * @param dumpfile set to non-NULL to make the descriptors dump out
214 * to this file in human-readable hex so we can scruitinze them.
Linus Walleija4942fc2007-03-12 19:23:21 +0000215 * @return 1 if the device is MTP compliant, 0 if not.
216 */
Linus Walleij9521e2b2007-07-10 21:50:42 +0000217static int probe_device_descriptor(struct usb_device *dev, FILE *dumpfile)
Linus Walleija4942fc2007-03-12 19:23:21 +0000218{
219 usb_dev_handle *devh;
220 unsigned char buf[1024], cmd;
Linus Walleijbc550bc2007-11-08 23:25:09 +0000221 int i;
Linus Walleija4942fc2007-03-12 19:23:21 +0000222 int ret;
223
224 /* Don't examine hubs (no point in that) */
225 if (dev->descriptor.bDeviceClass == USB_CLASS_HUB) {
226 return 0;
227 }
228
229 /* Attempt to open Device on this port */
230 devh = usb_open(dev);
231 if (devh == NULL) {
232 /* Could not open this device */
233 return 0;
234 }
Linus Walleijbc550bc2007-11-08 23:25:09 +0000235
236 /*
Richard Lowe029eba2009-04-11 12:36:25 +0000237 * This sometimes crashes on the j for loop below
Linus Walleij88244212009-08-12 16:05:56 +0000238 * I think it is because config is NULL yet
Richard Lowe029eba2009-04-11 12:36:25 +0000239 * dev->descriptor.bNumConfigurations > 0
240 * this check should stop this
Linus Walleijbc550bc2007-11-08 23:25:09 +0000241 */
Richard Lowe029eba2009-04-11 12:36:25 +0000242 if (dev->config) {
243 /*
Linus Walleij88244212009-08-12 16:05:56 +0000244 * Loop over the device configurations and interfaces. Nokia MTP-capable
245 * handsets (possibly others) typically have the string "MTP" in their
Richard Lowe029eba2009-04-11 12:36:25 +0000246 * MTP interface descriptions, that's how they can be detected, before
247 * we try the more esoteric "OS descriptors" (below).
248 */
249 for (i = 0; i < dev->descriptor.bNumConfigurations; i++) {
250 uint8_t j;
Linus Walleij88244212009-08-12 16:05:56 +0000251
Richard Lowe029eba2009-04-11 12:36:25 +0000252 for (j = 0; j < dev->config[i].bNumInterfaces; j++) {
253 int k;
254 for (k = 0; k < dev->config[i].interface[j].num_altsetting; k++) {
Linus Walleij88244212009-08-12 16:05:56 +0000255 /* Current interface descriptor */
256 struct usb_interface_descriptor *intf =
257 &dev->config[i].interface[j].altsetting[k];
258
259 /*
260 * Check for Still Image Capture class with PIMA 15740 protocol,
261 * also known as PTP
262 */
263 if (intf->bInterfaceClass == USB_CLASS_PTP
264 && intf->bInterfaceSubClass == 0x01
265 && intf->bInterfaceProtocol == 0x01) {
266 if (dumpfile != NULL) {
Richard Lowe029eba2009-04-11 12:36:25 +0000267 fprintf(dumpfile, "Configuration %d, interface %d, altsetting %d:\n", i, j, k);
Linus Walleij88244212009-08-12 16:05:56 +0000268 fprintf(dumpfile, " Interface implements PTP class,"
269 " no further probing.\n");
270 }
271 usb_close(devh);
272 return 1;
273 }
274 buf[0] = '\0';
275 ret = usb_get_string_simple(devh,
276 dev->config[i].interface[j].altsetting[k].iInterface,
277 (char *) buf,
278 1024);
279 if (ret < 3)
280 continue;
Linus Walleijfea4f532009-09-22 22:08:35 +0000281 // We search for the MTP string.
282 // For example : "RIM MS/MTP" should work.
283 if (strstr((char *) buf, "MTP") != NULL) {
Linus Walleij88244212009-08-12 16:05:56 +0000284 if (dumpfile != NULL) {
285 fprintf(dumpfile, "Configuration %d, interface %d, altsetting %d:\n", i, j, k);
286 fprintf(dumpfile, " Interface description contains the string \"MTP\"\n");
287 fprintf(dumpfile, " Device recognized as MTP, no further probing.\n");
288 }
Richard Lowe029eba2009-04-11 12:36:25 +0000289 usb_close(devh);
290 return 1;
291 }
292 #ifdef LIBUSB_HAS_GET_DRIVER_NP
Linus Walleij88244212009-08-12 16:05:56 +0000293 {
294 /*
295 * Specifically avoid probing anything else than USB mass storage devices
296 * and non-associated drivers in Linux.
297 */
298 char devname[0x10];
299
300 devname[0] = '\0';
301 ret = usb_get_driver_np(devh,
302 dev->config[i].interface[j].altsetting[k].iInterface,
303 devname,
304 sizeof(devname));
305 if (devname[0] != '\0' && strcmp(devname, "usb-storage")) {
nicklas79daadbf22009-09-28 18:19:34 +0000306 LIBMTP_INFO("avoid probing device using kernel interface \"%s\"\n", devname);
Linus Walleij88244212009-08-12 16:05:56 +0000307 return 0;
308 }
309 }
Richard Lowe029eba2009-04-11 12:36:25 +0000310 #endif
311 }
312 }
313 }
314 } else {
315 if (dev->descriptor.bNumConfigurations)
nicklas79daadbf22009-09-28 18:19:34 +0000316 LIBMTP_INFO("dev->config is NULL in probe_device_descriptor yet dev->descriptor.bNumConfigurations > 0\n");
Linus Walleijbc550bc2007-11-08 23:25:09 +0000317 }
Linus Walleijfea4f532009-09-22 22:08:35 +0000318
Linus Walleija4942fc2007-03-12 19:23:21 +0000319 /* Read the special descriptor */
320 ret = usb_get_descriptor(devh, 0x03, 0xee, buf, sizeof(buf));
Linus Walleij9521e2b2007-07-10 21:50:42 +0000321
322 // Dump it, if requested
323 if (dumpfile != NULL && ret > 0) {
324 fprintf(dumpfile, "Microsoft device descriptor 0xee:\n");
325 data_dump_ascii(dumpfile, buf, ret, 16);
326 }
Linus Walleijfea4f532009-09-22 22:08:35 +0000327
Linus Walleija4942fc2007-03-12 19:23:21 +0000328 /* Check if descriptor length is at least 10 bytes */
329 if (ret < 10) {
330 usb_close(devh);
331 return 0;
332 }
Linus Walleijfea4f532009-09-22 22:08:35 +0000333
Linus Walleija4942fc2007-03-12 19:23:21 +0000334 /* Check if this device has a Microsoft Descriptor */
335 if (!((buf[2] == 'M') && (buf[4] == 'S') &&
336 (buf[6] == 'F') && (buf[8] == 'T'))) {
337 usb_close(devh);
338 return 0;
339 }
Linus Walleijfea4f532009-09-22 22:08:35 +0000340
Linus Walleija4942fc2007-03-12 19:23:21 +0000341 /* Check if device responds to control message 1 or if there is an error */
342 cmd = buf[16];
343 ret = usb_control_msg (devh,
Linus Walleijfea4f532009-09-22 22:08:35 +0000344 USB_ENDPOINT_IN | USB_RECIP_DEVICE | USB_TYPE_VENDOR,
Linus Walleija4942fc2007-03-12 19:23:21 +0000345 cmd,
346 0,
347 4,
348 (char *) buf,
349 sizeof(buf),
Linus Walleij2f622812008-08-30 22:06:58 +0000350 USB_TIMEOUT_DEFAULT);
Linus Walleij9521e2b2007-07-10 21:50:42 +0000351
352 // Dump it, if requested
353 if (dumpfile != NULL && ret > 0) {
354 fprintf(dumpfile, "Microsoft device response to control message 1, CMD 0x%02x:\n", cmd);
355 data_dump_ascii(dumpfile, buf, ret, 16);
356 }
Linus Walleijfea4f532009-09-22 22:08:35 +0000357
Linus Walleija4942fc2007-03-12 19:23:21 +0000358 /* If this is true, the device either isn't MTP or there was an error */
359 if (ret <= 0x15) {
360 /* TODO: If there was an error, flag it and let the user know somehow */
361 /* if(ret == -1) {} */
362 usb_close(devh);
363 return 0;
364 }
Linus Walleijfea4f532009-09-22 22:08:35 +0000365
366 /* Check if device is MTP or if it is something like a USB Mass Storage
Linus Walleija4942fc2007-03-12 19:23:21 +0000367 device with Janus DRM support */
368 if ((buf[0x12] != 'M') || (buf[0x13] != 'T') || (buf[0x14] != 'P')) {
369 usb_close(devh);
370 return 0;
371 }
Linus Walleijfea4f532009-09-22 22:08:35 +0000372
Linus Walleija4942fc2007-03-12 19:23:21 +0000373 /* After this point we are probably dealing with an MTP device */
374
375 /* Check if device responds to control message 2 or if there is an error*/
376 ret = usb_control_msg (devh,
Linus Walleijfea4f532009-09-22 22:08:35 +0000377 USB_ENDPOINT_IN | USB_RECIP_DEVICE | USB_TYPE_VENDOR,
Linus Walleija4942fc2007-03-12 19:23:21 +0000378 cmd,
379 0,
380 5,
381 (char *) buf,
382 sizeof(buf),
Linus Walleij2f622812008-08-30 22:06:58 +0000383 USB_TIMEOUT_DEFAULT);
Linus Walleij9521e2b2007-07-10 21:50:42 +0000384
385 // Dump it, if requested
386 if (dumpfile != NULL && ret > 0) {
387 fprintf(dumpfile, "Microsoft device response to control message 2, CMD 0x%02x:\n", cmd);
388 data_dump_ascii(dumpfile, buf, ret, 16);
389 }
Linus Walleijfea4f532009-09-22 22:08:35 +0000390
Linus Walleija4942fc2007-03-12 19:23:21 +0000391 /* If this is true, the device errored against control message 2 */
392 if (ret == -1) {
393 /* TODO: Implement callback function to let managing program know there
394 was a problem, along with description of the problem */
nicklas79daadbf22009-09-28 18:19:34 +0000395 LIBMTP_ERROR("Potential MTP Device with VendorID:%04x and "
Linus Walleija4942fc2007-03-12 19:23:21 +0000396 "ProductID:%04x encountered an error responding to "
397 "control message 2.\n"
398 "Problems may arrise but continuing\n",
399 dev->descriptor.idVendor, dev->descriptor.idProduct);
400 } else if (ret <= 0x15) {
401 /* TODO: Implement callback function to let managing program know there
402 was a problem, along with description of the problem */
nicklas79daadbf22009-09-28 18:19:34 +0000403 LIBMTP_ERROR("Potential MTP Device with VendorID:%04x and "
Linus Walleija4942fc2007-03-12 19:23:21 +0000404 "ProductID:%04x responded to control message 2 with a "
405 "response that was too short. Problems may arrise but "
406 "continuing\n",
407 dev->descriptor.idVendor, dev->descriptor.idProduct);
408 } else if ((buf[0x12] != 'M') || (buf[0x13] != 'T') || (buf[0x14] != 'P')) {
409 /* TODO: Implement callback function to let managing program know there
410 was a problem, along with description of the problem */
nicklas79daadbf22009-09-28 18:19:34 +0000411 LIBMTP_ERROR("Potential MTP Device with VendorID:%04x and "
Linus Walleija4942fc2007-03-12 19:23:21 +0000412 "ProductID:%04x encountered an error responding to "
413 "control message 2\n"
414 "Problems may arrise but continuing\n",
415 dev->descriptor.idVendor, dev->descriptor.idProduct);
416 }
Linus Walleijfea4f532009-09-22 22:08:35 +0000417
Linus Walleija4942fc2007-03-12 19:23:21 +0000418 /* Close the USB device handle */
419 usb_close(devh);
420 return 1;
421}
422
423/**
424 * This function scans through the connected usb devices on a machine and
425 * if they match known Vendor and Product identifiers appends them to the
426 * dynamic array mtp_device_list. Be sure to call
Linus Walleij63fd1e62008-04-23 23:49:43 +0000427 * <code>free_mtpdevice_list(mtp_device_list)</code> when you are done
428 * with it, assuming it is not NULL.
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000429 * @param mtp_device_list dynamic array of pointers to usb devices with MTP
Linus Walleij45a86372007-03-07 09:36:19 +0000430 * properties (if this list is not empty, new entries will be appended
431 * to the list).
Linus Walleija4942fc2007-03-12 19:23:21 +0000432 * @return LIBMTP_ERROR_NONE implies that devices have been found, scan the list
Linus Walleij9c6c1542007-09-19 13:43:06 +0000433 * appropriately. LIBMTP_ERROR_NO_DEVICE_ATTACHED implies that no
434 * devices have been found.
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000435 */
Linus Walleij45a86372007-03-07 09:36:19 +0000436static LIBMTP_error_number_t get_mtp_usb_device_list(mtpdevice_list_t ** mtp_device_list)
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000437{
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000438 struct usb_bus *bus = init_usb();
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000439 for (; bus != NULL; bus = bus->next) {
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000440 struct usb_device *dev = bus->devices;
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000441 for (; dev != NULL; dev = dev->next) {
Richard Lowe9f205f2007-09-16 16:25:50 +0000442 if (dev->descriptor.bDeviceClass != USB_CLASS_HUB) {
Linus Walleij9c6c1542007-09-19 13:43:06 +0000443 int i;
444 int found = 0;
445
446 // First check if we know about the device already.
447 // Devices well known to us will not have their descriptors
448 // probed, it caused problems with some devices.
Richard Lowe9f205f2007-09-16 16:25:50 +0000449 for(i = 0; i < mtp_device_table_size; i++) {
450 if(dev->descriptor.idVendor == mtp_device_table[i].vendor_id &&
451 dev->descriptor.idProduct == mtp_device_table[i].product_id) {
452 /* Append this usb device to the MTP device list */
Linus Walleij63fd1e62008-04-23 23:49:43 +0000453 *mtp_device_list = append_to_mtpdevice_list(*mtp_device_list,
454 dev,
455 bus->location);
Richard Lowe9f205f2007-09-16 16:25:50 +0000456 found = 1;
457 break;
458 }
459 }
Linus Walleij9c6c1542007-09-19 13:43:06 +0000460 // If we didn't know it, try probing the "OS Descriptor".
Richard Lowe9f205f2007-09-16 16:25:50 +0000461 if (!found) {
Richard Lowe9f205f2007-09-16 16:25:50 +0000462 if (probe_device_descriptor(dev, NULL)) {
463 /* Append this usb device to the MTP USB Device List */
Linus Walleij63fd1e62008-04-23 23:49:43 +0000464 *mtp_device_list = append_to_mtpdevice_list(*mtp_device_list,
465 dev,
466 bus->location);
Richard Lowe9f205f2007-09-16 16:25:50 +0000467 }
Linus Walleijd866d242009-08-23 21:50:39 +0000468 /*
469 * By thomas_-_s: Also append devices that are no MTP but PTP devices
470 * if this is commented out.
471 */
472 /*
473 else {
474 // Check whether the device is no USB hub but a PTP.
475 if ( dev->config != NULL &&dev->config->interface->altsetting->bInterfaceClass == USB_CLASS_PTP && dev->descriptor.bDeviceClass != USB_CLASS_HUB ) {
476 *mtp_device_list = append_to_mtpdevice_list(*mtp_device_list, dev, bus->location);
477 }
478 }
479 */
Richard Lowe9f205f2007-09-16 16:25:50 +0000480 }
Linus Walleij9c6c1542007-09-19 13:43:06 +0000481 }
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000482 }
483 }
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000484
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000485 /* If nothing was found we end up here. */
Linus Walleija4942fc2007-03-12 19:23:21 +0000486 if(*mtp_device_list == NULL) {
tedbullock433d2172007-02-23 22:39:12 +0000487 return LIBMTP_ERROR_NO_DEVICE_ATTACHED;
Linus Walleija4942fc2007-03-12 19:23:21 +0000488 }
489 return LIBMTP_ERROR_NONE;
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000490}
491
492/**
Linus Walleij63fd1e62008-04-23 23:49:43 +0000493 * Detect the raw MTP device descriptors and return a list of
494 * of the devices found.
tedbullock2b45f2e2007-02-23 20:16:37 +0000495 *
Linus Walleij63fd1e62008-04-23 23:49:43 +0000496 * @param devices a pointer to a variable that will hold
497 * the list of raw devices found. This may be NULL
498 * on return if the number of detected devices is zero.
499 * The user shall simply <code>free()</code> this
500 * variable when finished with the raw devices,
501 * in order to release memory.
502 * @param numdevs a pointer to an integer that will hold
503 * the number of devices in the list. This may
504 * be 0.
505 * @return 0 if successful, any other value means failure.
Linus Walleij1fd2d272006-05-08 09:22:01 +0000506 */
Linus Walleija700d222008-05-28 23:06:14 +0000507LIBMTP_error_number_t LIBMTP_Detect_Raw_Devices(LIBMTP_raw_device_t ** devices,
Linus Walleij63fd1e62008-04-23 23:49:43 +0000508 int * numdevs)
Linus Walleij1fd2d272006-05-08 09:22:01 +0000509{
Linus Walleij63fd1e62008-04-23 23:49:43 +0000510 mtpdevice_list_t *devlist = NULL;
511 mtpdevice_list_t *dev;
512 LIBMTP_error_number_t ret;
Linus Walleij63fd1e62008-04-23 23:49:43 +0000513 LIBMTP_raw_device_t *retdevs;
514 int devs = 0;
Linus Walleijf106aa62008-05-04 22:55:51 +0000515 int i, j;
Linus Walleijb6b69132007-03-07 09:45:05 +0000516
517 ret = get_mtp_usb_device_list(&devlist);
Linus Walleijef2fb362008-05-28 20:59:09 +0000518 if (ret == LIBMTP_ERROR_NO_DEVICE_ATTACHED) {
519 *devices = NULL;
520 *numdevs = 0;
Linus Walleija700d222008-05-28 23:06:14 +0000521 return ret;
Linus Walleijef2fb362008-05-28 20:59:09 +0000522 } else if (ret != LIBMTP_ERROR_NONE) {
nicklas79daadbf22009-09-28 18:19:34 +0000523 LIBMTP_ERROR("LIBMTP PANIC: get_mtp_usb_device_list() "
Linus Walleijf106aa62008-05-04 22:55:51 +0000524 "error code: %d on line %d\n", ret, __LINE__);
Linus Walleija700d222008-05-28 23:06:14 +0000525 return ret;
Linus Walleijb6b69132007-03-07 09:45:05 +0000526 }
Linus Walleijf106aa62008-05-04 22:55:51 +0000527
Linus Walleij63fd1e62008-04-23 23:49:43 +0000528 // Get list size
529 dev = devlist;
530 while (dev != NULL) {
531 devs++;
532 dev = dev->next;
533 }
534 if (devs == 0) {
535 *devices = NULL;
536 *numdevs = 0;
Linus Walleija700d222008-05-28 23:06:14 +0000537 return LIBMTP_ERROR_NONE;
Linus Walleij63fd1e62008-04-23 23:49:43 +0000538 }
539 // Conjure a device list
540 retdevs = (LIBMTP_raw_device_t *) malloc(sizeof(LIBMTP_raw_device_t) * devs);
541 if (retdevs == NULL) {
542 // Out of memory
543 *devices = NULL;
544 *numdevs = 0;
Linus Walleija700d222008-05-28 23:06:14 +0000545 return LIBMTP_ERROR_MEMORY_ALLOCATION;
Linus Walleij63fd1e62008-04-23 23:49:43 +0000546 }
547 dev = devlist;
548 i = 0;
549 while (dev != NULL) {
Linus Walleijf106aa62008-05-04 22:55:51 +0000550 int device_known = 0;
551
Linus Walleij63fd1e62008-04-23 23:49:43 +0000552 // Assign default device info
553 retdevs[i].device_entry.vendor = NULL;
554 retdevs[i].device_entry.vendor_id = dev->libusb_device->descriptor.idVendor;
555 retdevs[i].device_entry.product = NULL;
556 retdevs[i].device_entry.product_id = dev->libusb_device->descriptor.idProduct;
557 retdevs[i].device_entry.device_flags = 0x00000000U;
Linus Walleijfec4d562008-06-01 22:30:36 +0000558 // See if we can locate some additional vendor info and device flags
Linus Walleijf106aa62008-05-04 22:55:51 +0000559 for(j = 0; j < mtp_device_table_size; j++) {
560 if(dev->libusb_device->descriptor.idVendor == mtp_device_table[j].vendor_id &&
561 dev->libusb_device->descriptor.idProduct == mtp_device_table[j].product_id) {
562 device_known = 1;
563 retdevs[i].device_entry.vendor = mtp_device_table[j].vendor;
564 retdevs[i].device_entry.product = mtp_device_table[j].product;
565 retdevs[i].device_entry.device_flags = mtp_device_table[j].device_flags;
nicklas79daadbf22009-09-28 18:19:34 +0000566
Linus Walleijf106aa62008-05-04 22:55:51 +0000567 // This device is known to the developers
nicklas79daadbf22009-09-28 18:19:34 +0000568 LIBMTP_ERROR("Device %d (VID=%04x and PID=%04x) is a %s %s.\n",
Linus Walleijf106aa62008-05-04 22:55:51 +0000569 i,
570 dev->libusb_device->descriptor.idVendor,
571 dev->libusb_device->descriptor.idProduct,
572 mtp_device_table[j].vendor,
573 mtp_device_table[j].product);
Linus Walleijf106aa62008-05-04 22:55:51 +0000574 break;
575 }
576 }
577 if (!device_known) {
578 // This device is unknown to the developers
nicklas79daadbf22009-09-28 18:19:34 +0000579 LIBMTP_ERROR("Device %d (VID=%04x and PID=%04x) is UNKNOWN.\n",
Linus Walleijf106aa62008-05-04 22:55:51 +0000580 i,
581 dev->libusb_device->descriptor.idVendor,
582 dev->libusb_device->descriptor.idProduct);
nicklas79daadbf22009-09-28 18:19:34 +0000583 LIBMTP_ERROR("Please report this VID/PID and the device model to the "
Linus Walleijf106aa62008-05-04 22:55:51 +0000584 "libmtp development team\n");
Linus Walleijfec4d562008-06-01 22:30:36 +0000585 /*
586 * Trying to get iManufacturer or iProduct from the device at this
587 * point would require opening a device handle, that we don't want
588 * to do right now. (Takes time for no good enough reason.)
589 */
Linus Walleijf106aa62008-05-04 22:55:51 +0000590 }
591 // Save the location on the bus
Linus Walleij63fd1e62008-04-23 23:49:43 +0000592 retdevs[i].bus_location = dev->bus_location;
593 retdevs[i].devnum = dev->libusb_device->devnum;
594 i++;
595 dev = dev->next;
596 }
597 *devices = retdevs;
598 *numdevs = i;
Linus Walleijb6b69132007-03-07 09:45:05 +0000599 free_mtpdevice_list(devlist);
Linus Walleija700d222008-05-28 23:06:14 +0000600 return LIBMTP_ERROR_NONE;
Linus Walleij1fd2d272006-05-08 09:22:01 +0000601}
602
Linus Walleijc6210fb2006-05-08 11:11:41 +0000603/**
604 * This routine just dumps out low-level
605 * USB information about the current device.
606 * @param ptp_usb the USB device to get information from.
607 */
608void dump_usbinfo(PTP_USB *ptp_usb)
609{
Linus Walleijc6210fb2006-05-08 11:11:41 +0000610 struct usb_device *dev;
611
612#ifdef LIBUSB_HAS_GET_DRIVER_NP
613 char devname[0x10];
Richard Lowe029eba2009-04-11 12:36:25 +0000614 int res;
Linus Walleijc6210fb2006-05-08 11:11:41 +0000615
616 devname[0] = '\0';
Linus Walleijb0ab5482007-08-29 21:08:54 +0000617 res = usb_get_driver_np(ptp_usb->handle, (int) ptp_usb->interface, devname, sizeof(devname));
Linus Walleijc6210fb2006-05-08 11:11:41 +0000618 if (devname[0] != '\0') {
nicklas79daadbf22009-09-28 18:19:34 +0000619 LIBMTP_INFO(" Using kernel interface \"%s\"\n", devname);
Linus Walleijc6210fb2006-05-08 11:11:41 +0000620 }
621#endif
622 dev = usb_device(ptp_usb->handle);
nicklas79daadbf22009-09-28 18:19:34 +0000623 LIBMTP_INFO(" bcdUSB: %d\n", dev->descriptor.bcdUSB);
624 LIBMTP_INFO(" bDeviceClass: %d\n", dev->descriptor.bDeviceClass);
625 LIBMTP_INFO(" bDeviceSubClass: %d\n", dev->descriptor.bDeviceSubClass);
626 LIBMTP_INFO(" bDeviceProtocol: %d\n", dev->descriptor.bDeviceProtocol);
627 LIBMTP_INFO(" idVendor: %04x\n", dev->descriptor.idVendor);
628 LIBMTP_INFO(" idProduct: %04x\n", dev->descriptor.idProduct);
629 LIBMTP_INFO(" IN endpoint maxpacket: %d bytes\n", ptp_usb->inep_maxpacket);
630 LIBMTP_INFO(" OUT endpoint maxpacket: %d bytes\n", ptp_usb->outep_maxpacket);
631 LIBMTP_INFO(" Raw device info:\n");
632 LIBMTP_INFO(" Bus location: %d\n", ptp_usb->rawdevice.bus_location);
633 LIBMTP_INFO(" Device number: %d\n", ptp_usb->rawdevice.devnum);
634 LIBMTP_INFO(" Device entry info:\n");
635 LIBMTP_INFO(" Vendor: %s\n", ptp_usb->rawdevice.device_entry.vendor);
636 LIBMTP_INFO(" Vendor id: 0x%04x\n", ptp_usb->rawdevice.device_entry.vendor_id);
637 LIBMTP_INFO(" Product: %s\n", ptp_usb->rawdevice.device_entry.product);
638 LIBMTP_INFO(" Vendor id: 0x%04x\n", ptp_usb->rawdevice.device_entry.product_id);
639 LIBMTP_INFO(" Device flags: 0x%08x\n", ptp_usb->rawdevice.device_entry.device_flags);
Linus Walleij9521e2b2007-07-10 21:50:42 +0000640 (void) probe_device_descriptor(dev, stdout);
Linus Walleijc6210fb2006-05-08 11:11:41 +0000641}
642
Linus Walleija4e6bdc2008-05-23 22:31:53 +0000643/**
644 * Retrieve the apropriate playlist extension for this
645 * device. Rather hacky at the moment. This is probably
646 * desired by the managing software, but when creating
647 * lists on the device itself you notice certain preferences.
648 * @param ptp_usb the USB device to get suggestion for.
649 * @return the suggested playlist extension.
650 */
Richard Lowe029eba2009-04-11 12:36:25 +0000651const char *get_playlist_extension(PTP_USB *ptp_usb)
Linus Walleija4e6bdc2008-05-23 22:31:53 +0000652{
653 struct usb_device *dev;
654 static char creative_pl_extension[] = ".zpl";
655 static char default_pl_extension[] = ".pla";
656
657 dev = usb_device(ptp_usb->handle);
658 if (dev->descriptor.idVendor == 0x041e) {
659 return creative_pl_extension;
660 }
661 return default_pl_extension;
662}
663
Linus Walleijd1e14e02008-12-14 01:07:30 +0000664static void
665libusb_glue_debug (PTPParams *params, const char *format, ...)
Linus Walleija0323272007-01-07 12:21:30 +0000666{
667 va_list args;
668
669 va_start (args, format);
670 if (params->debug_func!=NULL)
671 params->debug_func (params->data, format, args);
672 else
673 {
674 vfprintf (stderr, format, args);
675 fprintf (stderr,"\n");
676 fflush (stderr);
677 }
678 va_end (args);
679}
680
681static void
Linus Walleijd1e14e02008-12-14 01:07:30 +0000682libusb_glue_error (PTPParams *params, const char *format, ...)
Linus Walleija0323272007-01-07 12:21:30 +0000683{
684 va_list args;
685
686 va_start (args, format);
687 if (params->error_func!=NULL)
688 params->error_func (params->data, format, args);
689 else
690 {
691 vfprintf (stderr, format, args);
692 fprintf (stderr,"\n");
693 fflush (stderr);
694 }
695 va_end (args);
696}
697
Linus Walleijc6210fb2006-05-08 11:11:41 +0000698
Linus Walleij99ed83c2007-01-02 18:46:02 +0000699/*
700 * ptp_read_func() and ptp_write_func() are
Linus Walleijc0a11432007-03-02 21:21:18 +0000701 * based on same functions usb.c in libgphoto2.
Linus Walleij99ed83c2007-01-02 18:46:02 +0000702 * Much reading packet logs and having fun with trials and errors
703 * reveals that WMP / Windows is probably using an algorithm like this
704 * for large transfers:
705 *
706 * 1. Send the command (0x0c bytes) if headers are split, else, send
707 * command plus sizeof(endpoint) - 0x0c bytes.
708 * 2. Send first packet, max size to be sizeof(endpoint) but only when using
709 * split headers. Else goto 3.
710 * 3. REPEAT send 0x10000 byte chunks UNTIL remaining bytes < 0x10000
711 * We call 0x10000 CONTEXT_BLOCK_SIZE.
712 * 4. Send remaining bytes MOD sizeof(endpoint)
713 * 5. Send remaining bytes. If this happens to be exactly sizeof(endpoint)
714 * then also send a zero-length package.
Linus Walleij049f50c2007-03-03 20:30:43 +0000715 *
716 * Further there is some special quirks to handle zero reads from the
717 * device, since some devices can't do them at all due to shortcomings
718 * of the USB slave controller in the device.
Linus Walleij99ed83c2007-01-02 18:46:02 +0000719 */
Richard Low4fd59d62007-03-03 16:34:37 +0000720#define CONTEXT_BLOCK_SIZE_1 0x3e00
721#define CONTEXT_BLOCK_SIZE_2 0x200
722#define CONTEXT_BLOCK_SIZE CONTEXT_BLOCK_SIZE_1+CONTEXT_BLOCK_SIZE_2
Linus Walleijb02a0662006-04-25 08:05:09 +0000723static short
Linus Walleij784ac3f2006-12-29 10:36:51 +0000724ptp_read_func (
725 unsigned long size, PTPDataHandler *handler,void *data,
Richard Low4df32f82007-01-11 20:04:39 +0000726 unsigned long *readbytes,
727 int readzero
Linus Walleij784ac3f2006-12-29 10:36:51 +0000728) {
Linus Walleijb02a0662006-04-25 08:05:09 +0000729 PTP_USB *ptp_usb = (PTP_USB *)data;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000730 unsigned long toread = 0;
Linus Walleijd6a49972006-05-02 08:24:54 +0000731 int result = 0;
Linus Walleij99ed83c2007-01-02 18:46:02 +0000732 unsigned long curread = 0;
733 unsigned long written;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000734 unsigned char *bytes;
Linus Walleij049f50c2007-03-03 20:30:43 +0000735 int expect_terminator_byte = 0;
Linus Walleij99ed83c2007-01-02 18:46:02 +0000736
737 // This is the largest block we'll need to read in.
Linus Walleij784ac3f2006-12-29 10:36:51 +0000738 bytes = malloc(CONTEXT_BLOCK_SIZE);
Linus Walleijd6a49972006-05-02 08:24:54 +0000739 while (curread < size) {
Richard Low4fd59d62007-03-03 16:34:37 +0000740
nicklas79daadbf22009-09-28 18:19:34 +0000741
742 LIBMTP_USB_DEBUG("Remaining size to read: 0x%04lx bytes\n", size - curread);
743
Richard Low4fd59d62007-03-03 16:34:37 +0000744 // check equal to condition here
745 if (size - curread < CONTEXT_BLOCK_SIZE)
746 {
747 // this is the last packet
748 toread = size - curread;
749 // this is equivalent to zero read for these devices
Linus Walleijfec4d562008-06-01 22:30:36 +0000750 if (readzero && FLAG_NO_ZERO_READS(ptp_usb) && toread % 64 == 0) {
Richard Low4fd59d62007-03-03 16:34:37 +0000751 toread += 1;
Linus Walleij049f50c2007-03-03 20:30:43 +0000752 expect_terminator_byte = 1;
Richard Low4fd59d62007-03-03 16:34:37 +0000753 }
Linus Walleijc49c6372007-01-09 00:18:51 +0000754 }
Richard Low4fd59d62007-03-03 16:34:37 +0000755 else if (curread == 0)
756 // we are first packet, but not last packet
757 toread = CONTEXT_BLOCK_SIZE_1;
758 else if (toread == CONTEXT_BLOCK_SIZE_1)
759 toread = CONTEXT_BLOCK_SIZE_2;
760 else if (toread == CONTEXT_BLOCK_SIZE_2)
761 toread = CONTEXT_BLOCK_SIZE_1;
762 else
nicklas79daadbf22009-09-28 18:19:34 +0000763 LIBMTP_INFO("unexpected toread size 0x%04x, 0x%04x remaining bytes\n",
Linus Walleij4cec9872007-03-03 21:00:53 +0000764 (unsigned int) toread, (unsigned int) (size-curread));
Linus Walleij784ac3f2006-12-29 10:36:51 +0000765
nicklas79daadbf22009-09-28 18:19:34 +0000766 LIBMTP_USB_DEBUG("Reading in 0x%04lx bytes\n", toread);
767
Linus Walleij2f622812008-08-30 22:06:58 +0000768 result = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep, (char*)bytes, toread, ptp_usb->timeout);
nicklas79daadbf22009-09-28 18:19:34 +0000769
770 LIBMTP_USB_DEBUG("Result of read: 0x%04x\n", result);
Richard Low4fd59d62007-03-03 16:34:37 +0000771
Linus Walleijc49c6372007-01-09 00:18:51 +0000772 if (result < 0) {
Richard Low43fdb882006-09-06 16:19:05 +0000773 return PTP_ERROR_IO;
Linus Walleijc49c6372007-01-09 00:18:51 +0000774 }
nicklas79daadbf22009-09-28 18:19:34 +0000775
776 LIBMTP_USB_DEBUG("<==USB IN\n");
Richard Low4df32f82007-01-11 20:04:39 +0000777 if (result == 0)
nicklas79daadbf22009-09-28 18:19:34 +0000778 LIBMTP_USB_DEBUG("Zero Read\n");
Richard Low4df32f82007-01-11 20:04:39 +0000779 else
nicklas79daadbf22009-09-28 18:19:34 +0000780 LIBMTP_USB_DATA(bytes, result, 16);
Richard Low4fd59d62007-03-03 16:34:37 +0000781
782 // want to discard extra byte
Linus Walleij049f50c2007-03-03 20:30:43 +0000783 if (expect_terminator_byte && result == toread)
Richard Lowbf2675c2007-03-18 18:20:26 +0000784 {
nicklas79daadbf22009-09-28 18:19:34 +0000785 LIBMTP_USB_DEBUG("<==USB IN\nDiscarding extra byte\n");
786
Richard Low4fd59d62007-03-03 16:34:37 +0000787 result--;
Richard Lowbf2675c2007-03-18 18:20:26 +0000788 }
Richard Low4fd59d62007-03-03 16:34:37 +0000789
Linus Walleijd866d242009-08-23 21:50:39 +0000790 int putfunc_ret = handler->putfunc(NULL, handler->priv, result, bytes, &written);
Richard Lowd24f9232009-05-03 10:22:08 +0000791 if (putfunc_ret != PTP_RC_OK)
792 return putfunc_ret;
Richard Lowbf2675c2007-03-18 18:20:26 +0000793
Linus Walleijc49c6372007-01-09 00:18:51 +0000794 ptp_usb->current_transfer_complete += result;
Linus Walleijd6a49972006-05-02 08:24:54 +0000795 curread += result;
Linus Walleijc49c6372007-01-09 00:18:51 +0000796
797 // Increase counters, call callback
798 if (ptp_usb->callback_active) {
799 if (ptp_usb->current_transfer_complete >= ptp_usb->current_transfer_total) {
800 // send last update and disable callback.
801 ptp_usb->current_transfer_complete = ptp_usb->current_transfer_total;
802 ptp_usb->callback_active = 0;
803 }
804 if (ptp_usb->current_transfer_callback != NULL) {
Linus Walleijbd3bf9e2007-09-14 19:31:54 +0000805 int ret;
806 ret = ptp_usb->current_transfer_callback(ptp_usb->current_transfer_complete,
807 ptp_usb->current_transfer_total,
808 ptp_usb->current_transfer_callback_data);
809 if (ret != 0) {
810 return PTP_ERROR_CANCEL;
811 }
Linus Walleijc49c6372007-01-09 00:18:51 +0000812 }
813 }
814
Linus Walleijd6a49972006-05-02 08:24:54 +0000815 if (result < toread) /* short reads are common */
816 break;
817 }
Linus Walleij784ac3f2006-12-29 10:36:51 +0000818 if (readbytes) *readbytes = curread;
819 free (bytes);
Linus Walleijee73ef22006-08-27 19:56:00 +0000820
Richard Low4df32f82007-01-11 20:04:39 +0000821 // there might be a zero packet waiting for us...
Linus Walleij049f50c2007-03-03 20:30:43 +0000822 if (readzero &&
Linus Walleijfec4d562008-06-01 22:30:36 +0000823 !FLAG_NO_ZERO_READS(ptp_usb) &&
Linus Walleij049f50c2007-03-03 20:30:43 +0000824 curread % ptp_usb->outep_maxpacket == 0) {
Richard Low4df32f82007-01-11 20:04:39 +0000825 char temp;
826 int zeroresult = 0;
Linus Walleij049f50c2007-03-03 20:30:43 +0000827
nicklas79daadbf22009-09-28 18:19:34 +0000828 LIBMTP_USB_DEBUG("<==USB IN\n");
829 LIBMTP_USB_DEBUG("Zero Read\n");
830
Linus Walleij2f622812008-08-30 22:06:58 +0000831 zeroresult = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep, &temp, 0, ptp_usb->timeout);
Richard Low4df32f82007-01-11 20:04:39 +0000832 if (zeroresult != 0)
nicklas79daadbf22009-09-28 18:19:34 +0000833 LIBMTP_INFO("LIBMTP panic: unable to read in zero packet, response 0x%04x", zeroresult);
Richard Low4df32f82007-01-11 20:04:39 +0000834 }
835
Richard Low4d9165f2008-09-23 20:13:17 +0000836 return PTP_RC_OK;
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000837}
838
Linus Walleijb02a0662006-04-25 08:05:09 +0000839static short
Linus Walleij784ac3f2006-12-29 10:36:51 +0000840ptp_write_func (
841 unsigned long size,
842 PTPDataHandler *handler,
843 void *data,
844 unsigned long *written
845) {
Linus Walleijb02a0662006-04-25 08:05:09 +0000846 PTP_USB *ptp_usb = (PTP_USB *)data;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000847 unsigned long towrite = 0;
Linus Walleijd6a49972006-05-02 08:24:54 +0000848 int result = 0;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000849 unsigned long curwrite = 0;
850 unsigned char *bytes;
Linus Walleij99ed83c2007-01-02 18:46:02 +0000851
852 // This is the largest block we'll need to read in.
Linus Walleij784ac3f2006-12-29 10:36:51 +0000853 bytes = malloc(CONTEXT_BLOCK_SIZE);
Linus Walleijc49c6372007-01-09 00:18:51 +0000854 if (!bytes) {
855 return PTP_ERROR_IO;
856 }
Linus Walleijd6a49972006-05-02 08:24:54 +0000857 while (curwrite < size) {
Linus Walleijb84b3852009-03-04 15:11:08 +0000858 unsigned long usbwritten = 0;
Linus Walleijd6a49972006-05-02 08:24:54 +0000859 towrite = size-curwrite;
Linus Walleijc49c6372007-01-09 00:18:51 +0000860 if (towrite > CONTEXT_BLOCK_SIZE) {
Richard Low43fdb882006-09-06 16:19:05 +0000861 towrite = CONTEXT_BLOCK_SIZE;
Linus Walleijc49c6372007-01-09 00:18:51 +0000862 } else {
863 // This magic makes packets the same size that WMP send them.
864 if (towrite > ptp_usb->outep_maxpacket && towrite % ptp_usb->outep_maxpacket != 0) {
Richard Low021421e2007-01-01 18:08:57 +0000865 towrite -= towrite % ptp_usb->outep_maxpacket;
Linus Walleijc49c6372007-01-09 00:18:51 +0000866 }
867 }
Linus Walleijd866d242009-08-23 21:50:39 +0000868 int getfunc_ret = handler->getfunc(NULL, handler->priv,towrite,bytes,&towrite);
Richard Lowd24f9232009-05-03 10:22:08 +0000869 if (getfunc_ret != PTP_RC_OK)
870 return getfunc_ret;
Linus Walleijb84b3852009-03-04 15:11:08 +0000871 while (usbwritten < towrite) {
872 result = USB_BULK_WRITE(ptp_usb->handle,ptp_usb->outep,((char*)bytes+usbwritten),towrite-usbwritten,ptp_usb->timeout);
nicklas79daadbf22009-09-28 18:19:34 +0000873
874 LIBMTP_USB_DEBUG("USB OUT==>\n");
875 LIBMTP_USB_DATA(bytes+usbwritten, result, 16);
876
Linus Walleijb84b3852009-03-04 15:11:08 +0000877 if (result < 0) {
878 return PTP_ERROR_IO;
879 }
880 // check for result == 0 perhaps too.
881 // Increase counters
882 ptp_usb->current_transfer_complete += result;
883 curwrite += result;
884 usbwritten += result;
Linus Walleijc49c6372007-01-09 00:18:51 +0000885 }
Linus Walleijc49c6372007-01-09 00:18:51 +0000886 // call callback
887 if (ptp_usb->callback_active) {
888 if (ptp_usb->current_transfer_complete >= ptp_usb->current_transfer_total) {
889 // send last update and disable callback.
890 ptp_usb->current_transfer_complete = ptp_usb->current_transfer_total;
891 ptp_usb->callback_active = 0;
892 }
893 if (ptp_usb->current_transfer_callback != NULL) {
Linus Walleijbd3bf9e2007-09-14 19:31:54 +0000894 int ret;
895 ret = ptp_usb->current_transfer_callback(ptp_usb->current_transfer_complete,
896 ptp_usb->current_transfer_total,
897 ptp_usb->current_transfer_callback_data);
898 if (ret != 0) {
899 return PTP_ERROR_CANCEL;
900 }
Linus Walleijc49c6372007-01-09 00:18:51 +0000901 }
902 }
Linus Walleijd6a49972006-05-02 08:24:54 +0000903 if (result < towrite) /* short writes happen */
904 break;
905 }
Linus Walleij784ac3f2006-12-29 10:36:51 +0000906 free (bytes);
Linus Walleijc49c6372007-01-09 00:18:51 +0000907 if (written) {
908 *written = curwrite;
Linus Walleij80d134a2006-08-22 21:41:37 +0000909 }
Linus Walleijd214b9b2006-08-26 22:08:37 +0000910
Linus Walleijc49c6372007-01-09 00:18:51 +0000911
Richard Lowef05b892007-01-03 21:18:56 +0000912 // If this is the last transfer send a zero write if required
913 if (ptp_usb->current_transfer_complete >= ptp_usb->current_transfer_total) {
Richard Low68f45882007-01-03 10:08:31 +0000914 if ((towrite % ptp_usb->outep_maxpacket) == 0) {
nicklas79daadbf22009-09-28 18:19:34 +0000915
916 LIBMTP_USB_DEBUG("USB OUT==>\n");
917 LIBMTP_USB_DEBUG("Zero Write\n");
918
Linus Walleij2f622812008-08-30 22:06:58 +0000919 result=USB_BULK_WRITE(ptp_usb->handle,ptp_usb->outep,(char *)"x",0,ptp_usb->timeout);
Linus Walleij7f0c72e2006-09-06 13:01:58 +0000920 }
Linus Walleijd214b9b2006-08-26 22:08:37 +0000921 }
Linus Walleijc49c6372007-01-09 00:18:51 +0000922
Linus Walleijd6a49972006-05-02 08:24:54 +0000923 if (result < 0)
924 return PTP_ERROR_IO;
925 return PTP_RC_OK;
Linus Walleijb02a0662006-04-25 08:05:09 +0000926}
927
Linus Walleija0323272007-01-07 12:21:30 +0000928/* memory data get/put handler */
929typedef struct {
930 unsigned char *data;
931 unsigned long size, curoff;
932} PTPMemHandlerPrivate;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000933
Linus Walleija0323272007-01-07 12:21:30 +0000934static uint16_t
935memory_getfunc(PTPParams* params, void* private,
936 unsigned long wantlen, unsigned char *data,
937 unsigned long *gotlen
938) {
939 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)private;
940 unsigned long tocopy = wantlen;
941
942 if (priv->curoff + tocopy > priv->size)
943 tocopy = priv->size - priv->curoff;
944 memcpy (data, priv->data + priv->curoff, tocopy);
945 priv->curoff += tocopy;
946 *gotlen = tocopy;
947 return PTP_RC_OK;
Linus Walleijb02a0662006-04-25 08:05:09 +0000948}
949
Linus Walleija0323272007-01-07 12:21:30 +0000950static uint16_t
951memory_putfunc(PTPParams* params, void* private,
952 unsigned long sendlen, unsigned char *data,
953 unsigned long *putlen
954) {
955 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)private;
956
957 if (priv->curoff + sendlen > priv->size) {
958 priv->data = realloc (priv->data, priv->curoff+sendlen);
959 priv->size = priv->curoff + sendlen;
960 }
961 memcpy (priv->data + priv->curoff, data, sendlen);
962 priv->curoff += sendlen;
963 *putlen = sendlen;
964 return PTP_RC_OK;
965}
966
967/* init private struct for receiving data. */
968static uint16_t
969ptp_init_recv_memory_handler(PTPDataHandler *handler) {
970 PTPMemHandlerPrivate* priv;
971 priv = malloc (sizeof(PTPMemHandlerPrivate));
Linus Walleijd866d242009-08-23 21:50:39 +0000972 handler->priv = priv;
Linus Walleija0323272007-01-07 12:21:30 +0000973 handler->getfunc = memory_getfunc;
974 handler->putfunc = memory_putfunc;
975 priv->data = NULL;
976 priv->size = 0;
977 priv->curoff = 0;
978 return PTP_RC_OK;
979}
980
981/* init private struct and put data in for sending data.
982 * data is still owned by caller.
983 */
984static uint16_t
985ptp_init_send_memory_handler(PTPDataHandler *handler,
986 unsigned char *data, unsigned long len
987) {
988 PTPMemHandlerPrivate* priv;
989 priv = malloc (sizeof(PTPMemHandlerPrivate));
990 if (!priv)
991 return PTP_RC_GeneralError;
Linus Walleijd866d242009-08-23 21:50:39 +0000992 handler->priv = priv;
Linus Walleija0323272007-01-07 12:21:30 +0000993 handler->getfunc = memory_getfunc;
994 handler->putfunc = memory_putfunc;
995 priv->data = data;
996 priv->size = len;
997 priv->curoff = 0;
998 return PTP_RC_OK;
999}
1000
1001/* free private struct + data */
1002static uint16_t
1003ptp_exit_send_memory_handler (PTPDataHandler *handler) {
Linus Walleijd866d242009-08-23 21:50:39 +00001004 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)handler->priv;
Linus Walleija0323272007-01-07 12:21:30 +00001005 /* data is owned by caller */
1006 free (priv);
1007 return PTP_RC_OK;
1008}
1009
1010/* hand over our internal data to caller */
1011static uint16_t
1012ptp_exit_recv_memory_handler (PTPDataHandler *handler,
1013 unsigned char **data, unsigned long *size
1014) {
Linus Walleijd866d242009-08-23 21:50:39 +00001015 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)handler->priv;
Linus Walleija0323272007-01-07 12:21:30 +00001016 *data = priv->data;
1017 *size = priv->size;
1018 free (priv);
1019 return PTP_RC_OK;
1020}
1021
1022/* send / receive functions */
1023
1024uint16_t
1025ptp_usb_sendreq (PTPParams* params, PTPContainer* req)
1026{
1027 uint16_t ret;
1028 PTPUSBBulkContainer usbreq;
1029 PTPDataHandler memhandler;
Linus Walleij5ba40402008-05-23 21:36:54 +00001030 unsigned long written = 0;
1031 unsigned long towrite;
nicklas79daadbf22009-09-28 18:19:34 +00001032
Linus Walleijb8f78c72008-03-02 20:22:56 +00001033 char txt[256];
Linus Walleija0323272007-01-07 12:21:30 +00001034
Linus Walleijb8f78c72008-03-02 20:22:56 +00001035 (void) ptp_render_opcode (params, req->Code, sizeof(txt), txt);
nicklas79daadbf22009-09-28 18:19:34 +00001036 LIBMTP_USB_DEBUG("REQUEST: 0x%04x, %s\n", req->Code, txt);
1037
Linus Walleija0323272007-01-07 12:21:30 +00001038 /* build appropriate USB container */
1039 usbreq.length=htod32(PTP_USB_BULK_REQ_LEN-
1040 (sizeof(uint32_t)*(5-req->Nparam)));
1041 usbreq.type=htod16(PTP_USB_CONTAINER_COMMAND);
1042 usbreq.code=htod16(req->Code);
1043 usbreq.trans_id=htod32(req->Transaction_ID);
1044 usbreq.payload.params.param1=htod32(req->Param1);
1045 usbreq.payload.params.param2=htod32(req->Param2);
1046 usbreq.payload.params.param3=htod32(req->Param3);
1047 usbreq.payload.params.param4=htod32(req->Param4);
1048 usbreq.payload.params.param5=htod32(req->Param5);
1049 /* send it to responder */
1050 towrite = PTP_USB_BULK_REQ_LEN-(sizeof(uint32_t)*(5-req->Nparam));
1051 ptp_init_send_memory_handler (&memhandler, (unsigned char*)&usbreq, towrite);
1052 ret=ptp_write_func(
1053 towrite,
1054 &memhandler,
1055 params->data,
1056 &written
1057 );
1058 ptp_exit_send_memory_handler (&memhandler);
Linus Walleijff01cb12007-09-16 19:49:48 +00001059 if (ret!=PTP_RC_OK && ret!=PTP_ERROR_CANCEL) {
Linus Walleija0323272007-01-07 12:21:30 +00001060 ret = PTP_ERROR_IO;
Linus Walleija0323272007-01-07 12:21:30 +00001061 }
Linus Walleij724f0102007-09-17 20:10:12 +00001062 if (written != towrite && ret != PTP_ERROR_CANCEL && ret != PTP_ERROR_IO) {
Linus Walleijd1e14e02008-12-14 01:07:30 +00001063 libusb_glue_error (params,
Linus Walleija0323272007-01-07 12:21:30 +00001064 "PTP: request code 0x%04x sending req wrote only %ld bytes instead of %d",
Richard Lowd284f072007-02-17 08:52:41 +00001065 req->Code, written, towrite
Linus Walleija0323272007-01-07 12:21:30 +00001066 );
1067 ret = PTP_ERROR_IO;
1068 }
1069 return ret;
1070}
1071
1072uint16_t
1073ptp_usb_senddata (PTPParams* params, PTPContainer* ptp,
1074 unsigned long size, PTPDataHandler *handler
1075) {
1076 uint16_t ret;
1077 int wlen, datawlen;
1078 unsigned long written;
1079 PTPUSBBulkContainer usbdata;
1080 uint32_t bytes_left_to_transfer;
1081 PTPDataHandler memhandler;
1082
nicklas79daadbf22009-09-28 18:19:34 +00001083
1084 LIBMTP_USB_DEBUG("SEND DATA PHASE\n");
1085
Linus Walleija0323272007-01-07 12:21:30 +00001086 /* build appropriate USB container */
1087 usbdata.length = htod32(PTP_USB_BULK_HDR_LEN+size);
1088 usbdata.type = htod16(PTP_USB_CONTAINER_DATA);
1089 usbdata.code = htod16(ptp->Code);
1090 usbdata.trans_id= htod32(ptp->Transaction_ID);
1091
1092 ((PTP_USB*)params->data)->current_transfer_complete = 0;
Richard Low4eb01122007-02-24 10:11:32 +00001093 ((PTP_USB*)params->data)->current_transfer_total = size+PTP_USB_BULK_HDR_LEN;
Linus Walleija0323272007-01-07 12:21:30 +00001094
1095 if (params->split_header_data) {
1096 datawlen = 0;
1097 wlen = PTP_USB_BULK_HDR_LEN;
1098 } else {
1099 unsigned long gotlen;
1100 /* For all camera devices. */
Richard Low02724f62007-02-17 09:47:26 +00001101 datawlen = (size<PTP_USB_BULK_PAYLOAD_LEN_WRITE)?size:PTP_USB_BULK_PAYLOAD_LEN_WRITE;
Linus Walleija0323272007-01-07 12:21:30 +00001102 wlen = PTP_USB_BULK_HDR_LEN + datawlen;
Richard Lowd24f9232009-05-03 10:22:08 +00001103
Linus Walleijd866d242009-08-23 21:50:39 +00001104 ret = handler->getfunc(params, handler->priv, datawlen, usbdata.payload.data, &gotlen);
Linus Walleija0323272007-01-07 12:21:30 +00001105 if (ret != PTP_RC_OK)
1106 return ret;
1107 if (gotlen != datawlen)
1108 return PTP_RC_GeneralError;
1109 }
1110 ptp_init_send_memory_handler (&memhandler, (unsigned char *)&usbdata, wlen);
1111 /* send first part of data */
1112 ret = ptp_write_func(wlen, &memhandler, params->data, &written);
1113 ptp_exit_send_memory_handler (&memhandler);
1114 if (ret!=PTP_RC_OK) {
Linus Walleija0323272007-01-07 12:21:30 +00001115 return ret;
1116 }
1117 if (size <= datawlen) return ret;
1118 /* if everything OK send the rest */
1119 bytes_left_to_transfer = size-datawlen;
1120 ret = PTP_RC_OK;
1121 while(bytes_left_to_transfer > 0) {
1122 ret = ptp_write_func (bytes_left_to_transfer, handler, params->data, &written);
1123 if (ret != PTP_RC_OK)
1124 break;
1125 if (written == 0) {
1126 ret = PTP_ERROR_IO;
1127 break;
1128 }
1129 bytes_left_to_transfer -= written;
1130 }
Linus Walleijff01cb12007-09-16 19:49:48 +00001131 if (ret!=PTP_RC_OK && ret!=PTP_ERROR_CANCEL)
Linus Walleija0323272007-01-07 12:21:30 +00001132 ret = PTP_ERROR_IO;
1133 return ret;
1134}
1135
1136static uint16_t ptp_usb_getpacket(PTPParams *params,
1137 PTPUSBBulkContainer *packet, unsigned long *rlen)
1138{
1139 PTPDataHandler memhandler;
1140 uint16_t ret;
1141 unsigned char *x = NULL;
1142
1143 /* read the header and potentially the first data */
1144 if (params->response_packet_size > 0) {
1145 /* If there is a buffered packet, just use it. */
1146 memcpy(packet, params->response_packet, params->response_packet_size);
1147 *rlen = params->response_packet_size;
1148 free(params->response_packet);
1149 params->response_packet = NULL;
1150 params->response_packet_size = 0;
1151 /* Here this signifies a "virtual read" */
1152 return PTP_RC_OK;
1153 }
1154 ptp_init_recv_memory_handler (&memhandler);
Richard Low02724f62007-02-17 09:47:26 +00001155 ret = ptp_read_func(PTP_USB_BULK_HS_MAX_PACKET_LEN_READ, &memhandler, params->data, rlen, 0);
Linus Walleija0323272007-01-07 12:21:30 +00001156 ptp_exit_recv_memory_handler (&memhandler, &x, rlen);
1157 if (x) {
1158 memcpy (packet, x, *rlen);
1159 free (x);
1160 }
1161 return ret;
1162}
1163
1164uint16_t
1165ptp_usb_getdata (PTPParams* params, PTPContainer* ptp, PTPDataHandler *handler)
1166{
1167 uint16_t ret;
1168 PTPUSBBulkContainer usbdata;
Linus Walleija0323272007-01-07 12:21:30 +00001169 unsigned long written;
Linus Walleij8e3af002007-09-28 19:21:54 +00001170 PTP_USB *ptp_usb = (PTP_USB *) params->data;
Linus Walleija0323272007-01-07 12:21:30 +00001171
nicklas79daadbf22009-09-28 18:19:34 +00001172
1173 LIBMTP_USB_DEBUG("GET DATA PHASE\n");
1174
Linus Walleija0323272007-01-07 12:21:30 +00001175 memset(&usbdata,0,sizeof(usbdata));
1176 do {
1177 unsigned long len, rlen;
1178
1179 ret = ptp_usb_getpacket(params, &usbdata, &rlen);
1180 if (ret!=PTP_RC_OK) {
1181 ret = PTP_ERROR_IO;
1182 break;
Linus Walleij8e3af002007-09-28 19:21:54 +00001183 }
Linus Walleija0323272007-01-07 12:21:30 +00001184 if (dtoh16(usbdata.type)!=PTP_USB_CONTAINER_DATA) {
1185 ret = PTP_ERROR_DATA_EXPECTED;
1186 break;
Linus Walleij8e3af002007-09-28 19:21:54 +00001187 }
Linus Walleija0323272007-01-07 12:21:30 +00001188 if (dtoh16(usbdata.code)!=ptp->Code) {
Linus Walleijfec4d562008-06-01 22:30:36 +00001189 if (FLAG_IGNORE_HEADER_ERRORS(ptp_usb)) {
Linus Walleijd1e14e02008-12-14 01:07:30 +00001190 libusb_glue_debug (params, "ptp2/ptp_usb_getdata: detected a broken "
Linus Walleij913a3062007-09-28 21:42:39 +00001191 "PTP header, code field insane, expect problems! (But continuing)");
Linus Walleij06542122007-10-14 22:13:48 +00001192 // Repair the header, so it won't wreak more havoc, don't just ignore it.
1193 // Typically these two fields will be broken.
1194 usbdata.code = htod16(ptp->Code);
1195 usbdata.trans_id = htod32(ptp->Transaction_ID);
Linus Walleij94cd9722007-09-28 21:29:53 +00001196 ret = PTP_RC_OK;
Linus Walleij8e3af002007-09-28 19:21:54 +00001197 } else {
Linus Walleij913a3062007-09-28 21:42:39 +00001198 ret = dtoh16(usbdata.code);
1199 // This filters entirely insane garbage return codes, but still
1200 // makes it possible to return error codes in the code field when
1201 // getting data. It appears Windows ignores the contents of this
1202 // field entirely.
1203 if (ret < PTP_RC_Undefined || ret > PTP_RC_SpecificationOfDestinationUnsupported) {
Linus Walleijd1e14e02008-12-14 01:07:30 +00001204 libusb_glue_debug (params, "ptp2/ptp_usb_getdata: detected a broken "
Linus Walleij913a3062007-09-28 21:42:39 +00001205 "PTP header, code field insane.");
1206 ret = PTP_ERROR_IO;
1207 }
Linus Walleij8e3af002007-09-28 19:21:54 +00001208 break;
1209 }
Linus Walleija0323272007-01-07 12:21:30 +00001210 }
1211 if (usbdata.length == 0xffffffffU) {
Linus Walleij06e42322008-08-16 23:34:32 +00001212 /* Copy first part of data to 'data' */
Richard Lowd24f9232009-05-03 10:22:08 +00001213 int putfunc_ret =
Linus Walleij06e42322008-08-16 23:34:32 +00001214 handler->putfunc(
Linus Walleijd866d242009-08-23 21:50:39 +00001215 params, handler->priv, rlen - PTP_USB_BULK_HDR_LEN, usbdata.payload.data,
Linus Walleij06e42322008-08-16 23:34:32 +00001216 &written
1217 );
Richard Lowd24f9232009-05-03 10:22:08 +00001218 if (putfunc_ret != PTP_RC_OK)
1219 return putfunc_ret;
Linus Walleija0323272007-01-07 12:21:30 +00001220 /* stuff data directly to passed data handler */
1221 while (1) {
1222 unsigned long readdata;
Linus Walleije1be4b92008-08-18 08:49:22 +00001223 uint16_t xret;
Linus Walleija0323272007-01-07 12:21:30 +00001224
1225 xret = ptp_read_func(
Richard Low02724f62007-02-17 09:47:26 +00001226 PTP_USB_BULK_HS_MAX_PACKET_LEN_READ,
Linus Walleija0323272007-01-07 12:21:30 +00001227 handler,
1228 params->data,
Richard Low4df32f82007-01-11 20:04:39 +00001229 &readdata,
Linus Walleijc0a11432007-03-02 21:21:18 +00001230 0
Linus Walleija0323272007-01-07 12:21:30 +00001231 );
Linus Walleijff01cb12007-09-16 19:49:48 +00001232 if (xret != PTP_RC_OK)
Linus Walleije1be4b92008-08-18 08:49:22 +00001233 return xret;
Richard Low02724f62007-02-17 09:47:26 +00001234 if (readdata < PTP_USB_BULK_HS_MAX_PACKET_LEN_READ)
Linus Walleija0323272007-01-07 12:21:30 +00001235 break;
1236 }
1237 return PTP_RC_OK;
1238 }
1239 if (rlen > dtoh32(usbdata.length)) {
1240 /*
1241 * Buffer the surplus response packet if it is >=
1242 * PTP_USB_BULK_HDR_LEN
1243 * (i.e. it is probably an entire package)
1244 * else discard it as erroneous surplus data.
1245 * This will even work if more than 2 packets appear
1246 * in the same transaction, they will just be handled
1247 * iteratively.
1248 *
1249 * Marcus observed stray bytes on iRiver devices;
1250 * these are still discarded.
1251 */
1252 unsigned int packlen = dtoh32(usbdata.length);
1253 unsigned int surplen = rlen - packlen;
1254
1255 if (surplen >= PTP_USB_BULK_HDR_LEN) {
1256 params->response_packet = malloc(surplen);
1257 memcpy(params->response_packet,
1258 (uint8_t *) &usbdata + packlen, surplen);
1259 params->response_packet_size = surplen;
tedbullock36f2afb2007-03-03 22:16:44 +00001260 /* Ignore reading one extra byte if device flags have been set */
Linus Walleijfec4d562008-06-01 22:30:36 +00001261 } else if(!FLAG_NO_ZERO_READS(ptp_usb) &&
1262 (rlen - dtoh32(usbdata.length) == 1)) {
Linus Walleijd1e14e02008-12-14 01:07:30 +00001263 libusb_glue_debug (params, "ptp2/ptp_usb_getdata: read %d bytes "
Linus Walleij8e3af002007-09-28 19:21:54 +00001264 "too much, expect problems!",
1265 rlen - dtoh32(usbdata.length));
Linus Walleija0323272007-01-07 12:21:30 +00001266 }
1267 rlen = packlen;
1268 }
1269
1270 /* For most PTP devices rlen is 512 == sizeof(usbdata)
1271 * here. For MTP devices splitting header and data it might
1272 * be 12.
1273 */
1274 /* Evaluate full data length. */
1275 len=dtoh32(usbdata.length)-PTP_USB_BULK_HDR_LEN;
1276
1277 /* autodetect split header/data MTP devices */
1278 if (dtoh32(usbdata.length) > 12 && (rlen==12))
1279 params->split_header_data = 1;
1280
Linus Walleija0323272007-01-07 12:21:30 +00001281 /* Copy first part of data to 'data' */
Richard Lowd24f9232009-05-03 10:22:08 +00001282 int putfunc_ret =
Linus Walleija0323272007-01-07 12:21:30 +00001283 handler->putfunc(
Linus Walleijd866d242009-08-23 21:50:39 +00001284 params, handler->priv, rlen - PTP_USB_BULK_HDR_LEN, usbdata.payload.data,
Linus Walleija0323272007-01-07 12:21:30 +00001285 &written
1286 );
Richard Lowd24f9232009-05-03 10:22:08 +00001287 if (putfunc_ret != PTP_RC_OK)
1288 return putfunc_ret;
Linus Walleijd866d242009-08-23 21:50:39 +00001289
1290 if (FLAG_NO_ZERO_READS(ptp_usb) &&
Linus Walleij049f50c2007-03-03 20:30:43 +00001291 len+PTP_USB_BULK_HDR_LEN == PTP_USB_BULK_HS_MAX_PACKET_LEN_READ) {
nicklas79daadbf22009-09-28 18:19:34 +00001292
1293 LIBMTP_USB_DEBUG("Reading in extra terminating byte\n");
1294
Linus Walleij049f50c2007-03-03 20:30:43 +00001295 // need to read in extra byte and discard it
1296 int result = 0;
1297 char byte = 0;
Linus Walleij2f622812008-08-30 22:06:58 +00001298 result = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep, &byte, 1, ptp_usb->timeout);
Linus Walleijd866d242009-08-23 21:50:39 +00001299
Linus Walleij049f50c2007-03-03 20:30:43 +00001300 if (result != 1)
nicklas79daadbf22009-09-28 18:19:34 +00001301 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 +00001302 } 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 +00001303 int zeroresult = 0;
1304 char zerobyte = 0;
1305
nicklas79daadbf22009-09-28 18:19:34 +00001306
1307 LIBMTP_INFO("Reading in zero packet after header\n");
1308
1309 zeroresult = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep, &zerobyte, 0, ptp_usb->timeout);
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001310
Linus Walleij94cd9722007-09-28 21:29:53 +00001311 if (zeroresult != 0)
nicklas79daadbf22009-09-28 18:19:34 +00001312 LIBMTP_INFO("LIBMTP panic: unable to read in zero packet, response 0x%04x", zeroresult);
Linus Walleij94cd9722007-09-28 21:29:53 +00001313 }
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001314
Linus Walleija0323272007-01-07 12:21:30 +00001315 /* Is that all of data? */
Linus Walleij94cd9722007-09-28 21:29:53 +00001316 if (len+PTP_USB_BULK_HDR_LEN<=rlen) {
1317 break;
1318 }
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001319
Linus Walleij913a3062007-09-28 21:42:39 +00001320 ret = ptp_read_func(len - (rlen - PTP_USB_BULK_HDR_LEN),
1321 handler,
1322 params->data, &rlen, 1);
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001323
Linus Walleija0323272007-01-07 12:21:30 +00001324 if (ret!=PTP_RC_OK) {
Linus Walleij049f50c2007-03-03 20:30:43 +00001325 break;
Linus Walleija0323272007-01-07 12:21:30 +00001326 }
1327 } while (0);
Linus Walleija0323272007-01-07 12:21:30 +00001328 return ret;
1329}
1330
1331uint16_t
1332ptp_usb_getresp (PTPParams* params, PTPContainer* resp)
1333{
1334 uint16_t ret;
1335 unsigned long rlen;
1336 PTPUSBBulkContainer usbresp;
Linus Walleij3e4b1142007-11-01 23:54:16 +00001337 PTP_USB *ptp_usb = (PTP_USB *)(params->data);
Linus Walleija0323272007-01-07 12:21:30 +00001338
nicklas79daadbf22009-09-28 18:19:34 +00001339
1340 LIBMTP_USB_DEBUG("RESPONSE: ");
1341
Linus Walleija0323272007-01-07 12:21:30 +00001342 memset(&usbresp,0,sizeof(usbresp));
1343 /* read response, it should never be longer than sizeof(usbresp) */
1344 ret = ptp_usb_getpacket(params, &usbresp, &rlen);
1345
Linus Walleijb8f78c72008-03-02 20:22:56 +00001346 // Fix for bevahiour reported by Scott Snyder on Samsung YP-U3. The player
1347 // sends a packet containing just zeroes of length 2 (up to 4 has been seen too)
1348 // after a NULL packet when it should send the response. This code ignores
1349 // such illegal packets.
Linus Walleij3191bba2008-02-10 21:16:25 +00001350 while (ret==PTP_RC_OK && rlen<PTP_USB_BULK_HDR_LEN && usbresp.length==0) {
Linus Walleijd1e14e02008-12-14 01:07:30 +00001351 libusb_glue_debug (params, "ptp_usb_getresp: detected short response "
Linus Walleij3191bba2008-02-10 21:16:25 +00001352 "of %d bytes, expect problems! (re-reading "
1353 "response), rlen");
Linus Walleij00411ee2008-01-27 12:24:51 +00001354 ret = ptp_usb_getpacket(params, &usbresp, &rlen);
1355 }
1356
Linus Walleija0323272007-01-07 12:21:30 +00001357 if (ret!=PTP_RC_OK) {
1358 ret = PTP_ERROR_IO;
1359 } else
1360 if (dtoh16(usbresp.type)!=PTP_USB_CONTAINER_RESPONSE) {
1361 ret = PTP_ERROR_RESP_EXPECTED;
1362 } else
1363 if (dtoh16(usbresp.code)!=resp->Code) {
1364 ret = dtoh16(usbresp.code);
1365 }
nicklas79daadbf22009-09-28 18:19:34 +00001366
1367 LIBMTP_USB_DEBUG("%04x\n", ret);
1368
Linus Walleija0323272007-01-07 12:21:30 +00001369 if (ret!=PTP_RC_OK) {
Linus Walleijd1e14e02008-12-14 01:07:30 +00001370/* libusb_glue_error (params,
Linus Walleija0323272007-01-07 12:21:30 +00001371 "PTP: request code 0x%04x getting resp error 0x%04x",
1372 resp->Code, ret);*/
1373 return ret;
1374 }
1375 /* build an appropriate PTPContainer */
1376 resp->Code=dtoh16(usbresp.code);
1377 resp->SessionID=params->session_id;
1378 resp->Transaction_ID=dtoh32(usbresp.trans_id);
Linus Walleijfec4d562008-06-01 22:30:36 +00001379 if (FLAG_IGNORE_HEADER_ERRORS(ptp_usb)) {
Linus Walleij3e4b1142007-11-01 23:54:16 +00001380 if (resp->Transaction_ID != params->transaction_id-1) {
Linus Walleijd1e14e02008-12-14 01:07:30 +00001381 libusb_glue_debug (params, "ptp_usb_getresp: detected a broken "
Linus Walleij00411ee2008-01-27 12:24:51 +00001382 "PTP header, transaction ID insane, expect "
1383 "problems! (But continuing)");
Linus Walleij3e4b1142007-11-01 23:54:16 +00001384 // Repair the header, so it won't wreak more havoc.
1385 resp->Transaction_ID = params->transaction_id-1;
1386 }
1387 }
Linus Walleija0323272007-01-07 12:21:30 +00001388 resp->Param1=dtoh32(usbresp.payload.params.param1);
1389 resp->Param2=dtoh32(usbresp.payload.params.param2);
1390 resp->Param3=dtoh32(usbresp.payload.params.param3);
1391 resp->Param4=dtoh32(usbresp.payload.params.param4);
1392 resp->Param5=dtoh32(usbresp.payload.params.param5);
1393 return ret;
1394}
1395
1396/* Event handling functions */
1397
1398/* PTP Events wait for or check mode */
1399#define PTP_EVENT_CHECK 0x0000 /* waits for */
1400#define PTP_EVENT_CHECK_FAST 0x0001 /* checks */
1401
1402static inline uint16_t
1403ptp_usb_event (PTPParams* params, PTPContainer* event, int wait)
1404{
1405 uint16_t ret;
1406 int result;
1407 unsigned long rlen;
1408 PTPUSBEventContainer usbevent;
1409 PTP_USB *ptp_usb = (PTP_USB *)(params->data);
1410
1411 memset(&usbevent,0,sizeof(usbevent));
1412
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001413 if ((params==NULL) || (event==NULL))
Linus Walleija0323272007-01-07 12:21:30 +00001414 return PTP_ERROR_BADPARAM;
1415 ret = PTP_RC_OK;
1416 switch(wait) {
1417 case PTP_EVENT_CHECK:
Linus Walleij2f622812008-08-30 22:06:58 +00001418 result=USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *)&usbevent,sizeof(usbevent),ptp_usb->timeout);
Linus Walleija0323272007-01-07 12:21:30 +00001419 if (result==0)
Linus Walleij2f622812008-08-30 22:06:58 +00001420 result = USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *) &usbevent, sizeof(usbevent), ptp_usb->timeout);
Linus Walleija0323272007-01-07 12:21:30 +00001421 if (result < 0) ret = PTP_ERROR_IO;
1422 break;
1423 case PTP_EVENT_CHECK_FAST:
Linus Walleij2f622812008-08-30 22:06:58 +00001424 result=USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *)&usbevent,sizeof(usbevent),ptp_usb->timeout);
Linus Walleija0323272007-01-07 12:21:30 +00001425 if (result==0)
Linus Walleij2f622812008-08-30 22:06:58 +00001426 result = USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *) &usbevent, sizeof(usbevent), ptp_usb->timeout);
Linus Walleija0323272007-01-07 12:21:30 +00001427 if (result < 0) ret = PTP_ERROR_IO;
1428 break;
1429 default:
1430 ret=PTP_ERROR_BADPARAM;
1431 break;
1432 }
1433 if (ret!=PTP_RC_OK) {
Linus Walleijd1e14e02008-12-14 01:07:30 +00001434 libusb_glue_error (params,
Linus Walleija0323272007-01-07 12:21:30 +00001435 "PTP: reading event an error 0x%04x occurred", ret);
1436 return PTP_ERROR_IO;
1437 }
1438 rlen = result;
1439 if (rlen < 8) {
Linus Walleijd1e14e02008-12-14 01:07:30 +00001440 libusb_glue_error (params,
Linus Walleija0323272007-01-07 12:21:30 +00001441 "PTP: reading event an short read of %ld bytes occurred", rlen);
1442 return PTP_ERROR_IO;
1443 }
1444 /* if we read anything over interrupt endpoint it must be an event */
1445 /* build an appropriate PTPContainer */
1446 event->Code=dtoh16(usbevent.code);
1447 event->SessionID=params->session_id;
1448 event->Transaction_ID=dtoh32(usbevent.trans_id);
1449 event->Param1=dtoh32(usbevent.param1);
1450 event->Param2=dtoh32(usbevent.param2);
1451 event->Param3=dtoh32(usbevent.param3);
1452 return ret;
1453}
1454
1455uint16_t
1456ptp_usb_event_check (PTPParams* params, PTPContainer* event) {
1457
1458 return ptp_usb_event (params, event, PTP_EVENT_CHECK_FAST);
1459}
1460
1461uint16_t
1462ptp_usb_event_wait (PTPParams* params, PTPContainer* event) {
1463
1464 return ptp_usb_event (params, event, PTP_EVENT_CHECK);
1465}
1466
Linus Walleijbd3bf9e2007-09-14 19:31:54 +00001467uint16_t
1468ptp_usb_control_cancel_request (PTPParams *params, uint32_t transactionid) {
1469 PTP_USB *ptp_usb = (PTP_USB *)(params->data);
1470 int ret;
1471 unsigned char buffer[6];
1472
1473 htod16a(&buffer[0],PTP_EC_CancelTransaction);
1474 htod32a(&buffer[2],transactionid);
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001475 ret = usb_control_msg(ptp_usb->handle,
Linus Walleijbd3bf9e2007-09-14 19:31:54 +00001476 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
Linus Walleij2f622812008-08-30 22:06:58 +00001477 0x64, 0x0000, 0x0000, (char *) buffer, sizeof(buffer), ptp_usb->timeout);
Linus Walleijbd3bf9e2007-09-14 19:31:54 +00001478 if (ret < sizeof(buffer))
1479 return PTP_ERROR_IO;
1480 return PTP_RC_OK;
1481}
Linus Walleija0323272007-01-07 12:21:30 +00001482
Linus Walleij9eb3d312006-08-04 19:25:59 +00001483static int init_ptp_usb (PTPParams* params, PTP_USB* ptp_usb, struct usb_device* dev)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001484{
Linus Walleijd6a49972006-05-02 08:24:54 +00001485 usb_dev_handle *device_handle;
Linus Walleijfea4f532009-09-22 22:08:35 +00001486 char buf[255];
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001487
Linus Walleijd6a49972006-05-02 08:24:54 +00001488 params->sendreq_func=ptp_usb_sendreq;
1489 params->senddata_func=ptp_usb_senddata;
1490 params->getresp_func=ptp_usb_getresp;
1491 params->getdata_func=ptp_usb_getdata;
Linus Walleijff01cb12007-09-16 19:49:48 +00001492 params->cancelreq_func=ptp_usb_control_cancel_request;
Linus Walleijd6a49972006-05-02 08:24:54 +00001493 params->data=ptp_usb;
1494 params->transaction_id=0;
Linus Walleij462a5e72007-02-13 06:54:56 +00001495 /*
1496 * This is hardcoded here since we have no devices whatsoever that are BE.
1497 * Change this the day we run into our first BE device (if ever).
1498 */
Linus Walleijd6a49972006-05-02 08:24:54 +00001499 params->byteorder = PTP_DL_LE;
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001500
Linus Walleij2f622812008-08-30 22:06:58 +00001501 ptp_usb->timeout = USB_TIMEOUT_DEFAULT;
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001502
Linus Walleij0ed2a9f2006-05-04 05:31:34 +00001503 if ((device_handle = usb_open(dev))){
Linus Walleijd6a49972006-05-02 08:24:54 +00001504 if (!device_handle) {
1505 perror("usb_open()");
Linus Walleij9eb3d312006-08-04 19:25:59 +00001506 return -1;
Linus Walleijd6a49972006-05-02 08:24:54 +00001507 }
Linus Walleij0ed2a9f2006-05-04 05:31:34 +00001508 ptp_usb->handle = device_handle;
Linus Walleij501ba4d2006-10-16 06:17:04 +00001509#ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP
1510 /*
1511 * If this device is known to be wrongfully claimed by other kernel
1512 * drivers (such as mass storage), then try to unload it to make it
1513 * accessible from user space.
1514 */
Linus Walleijfec4d562008-06-01 22:30:36 +00001515 if (FLAG_UNLOAD_DRIVER(ptp_usb)) {
Linus Walleijfea4f532009-09-22 22:08:35 +00001516 if (usb_get_driver_np(device_handle, (int) ptp_usb->interface,
1517 buf, sizeof(buf)) == 0) {
1518 if (usb_detach_kernel_driver_np(device_handle,
1519 (int) ptp_usb->interface)) {
Linus Walleijcea83ff2009-09-25 21:44:38 +00001520 perror("usb_detach_kernel_driver_np()");
1521 return -1;
Linus Walleijfea4f532009-09-22 22:08:35 +00001522 }
Linus Walleij501ba4d2006-10-16 06:17:04 +00001523 }
1524 }
1525#endif
Linus Walleijeac1e002006-11-30 22:06:02 +00001526#ifdef __WIN32__
Linus Walleij99ed83c2007-01-02 18:46:02 +00001527 // Only needed on Windows, and cause problems on other platforms.
Linus Walleij7beba572006-11-29 08:56:12 +00001528 if (usb_set_configuration(device_handle, dev->config->bConfigurationValue)) {
1529 perror("usb_set_configuration()");
1530 return -1;
1531 }
Linus Walleijeac1e002006-11-30 22:06:02 +00001532#endif
Linus Walleijb0ab5482007-08-29 21:08:54 +00001533 if (usb_claim_interface(device_handle, (int) ptp_usb->interface)) {
Linus Walleij0ed2a9f2006-05-04 05:31:34 +00001534 perror("usb_claim_interface()");
Linus Walleij9eb3d312006-08-04 19:25:59 +00001535 return -1;
Linus Walleij0ed2a9f2006-05-04 05:31:34 +00001536 }
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001537 // FIXME : Discovered in the Barry project
1538 // kernels >= 2.6.28 don't set the interface the same way as
1539 // previous versions did, and the Blackberry gets confused
1540 // if it isn't explicitly set
1541 if (usb_set_altinterface(device_handle, 0)) {
1542 perror("usb_set_altinterface()");
1543 return -1;
1544 }
Linus Walleijfea4f532009-09-22 22:08:35 +00001545 if (FLAG_SWITCH_MODE_BLACKBERRY(ptp_usb)) {
Linus Walleijb6e06e22009-09-22 22:20:49 +00001546 int ret;
1547
Linus Walleijfea4f532009-09-22 22:08:35 +00001548 // FIXME : Only for BlackBerry Storm
1549 // What does it mean? Maybe switch mode...
Linus Walleijcea83ff2009-09-25 21:44:38 +00001550 // This first control message is absolutely necessary
Linus Walleijfea4f532009-09-22 22:08:35 +00001551 usleep(1000);
Linus Walleijb6e06e22009-09-22 22:20:49 +00001552 ret = usb_control_msg(device_handle,
Linus Walleijfea4f532009-09-22 22:08:35 +00001553 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
1554 0xaa, 0x00, 0x04, buf, 0x40, 1000);
nicklas79daadbf22009-09-28 18:19:34 +00001555 LIBMTP_USB_DEBUG("BlackBerry magic part 1:\n");
1556 LIBMTP_USB_DATA(buf, ret, 16);
1557
Linus Walleijfea4f532009-09-22 22:08:35 +00001558 usleep(1000);
Linus Walleijcea83ff2009-09-25 21:44:38 +00001559 // This control message is unnecessary
Linus Walleijb6e06e22009-09-22 22:20:49 +00001560 ret = usb_control_msg(device_handle,
Linus Walleijfea4f532009-09-22 22:08:35 +00001561 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
1562 0xa5, 0x00, 0x01, buf, 0x02, 1000);
nicklas79daadbf22009-09-28 18:19:34 +00001563 LIBMTP_USB_DEBUG("BlackBerry magic part 2:\n");
1564 LIBMTP_USB_DATA(buf, ret, 16);
1565
Linus Walleijfea4f532009-09-22 22:08:35 +00001566 usleep(1000);
Linus Walleijcea83ff2009-09-25 21:44:38 +00001567 // This control message is unnecessary
Linus Walleijb6e06e22009-09-22 22:20:49 +00001568 ret = usb_control_msg(device_handle,
Linus Walleijfea4f532009-09-22 22:08:35 +00001569 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
1570 0xa8, 0x00, 0x01, buf, 0x05, 1000);
nicklas79daadbf22009-09-28 18:19:34 +00001571 LIBMTP_USB_DEBUG("BlackBerry magic part 3:\n");
1572 LIBMTP_USB_DATA(buf, ret, 16);
1573
Linus Walleijfea4f532009-09-22 22:08:35 +00001574 usleep(1000);
Linus Walleijcea83ff2009-09-25 21:44:38 +00001575 // This control message is unnecessary
Linus Walleijb6e06e22009-09-22 22:20:49 +00001576 ret = usb_control_msg(device_handle,
Linus Walleijfea4f532009-09-22 22:08:35 +00001577 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
1578 0xa8, 0x00, 0x01, buf, 0x11, 1000);
nicklas79daadbf22009-09-28 18:19:34 +00001579 LIBMTP_USB_DEBUG("BlackBerry magic part 4:\n");
1580 LIBMTP_USB_DATA(buf, ret, 16);
1581
Linus Walleijfea4f532009-09-22 22:08:35 +00001582 usleep(1000);
1583 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001584 }
Linus Walleij9eb3d312006-08-04 19:25:59 +00001585 return 0;
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001586}
1587
Linus Walleijd6a49972006-05-02 08:24:54 +00001588static void clear_stall(PTP_USB* ptp_usb)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001589{
Linus Walleij58b62792007-01-07 12:38:59 +00001590 uint16_t status;
Linus Walleijd6a49972006-05-02 08:24:54 +00001591 int ret;
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001592
Linus Walleijd6a49972006-05-02 08:24:54 +00001593 /* check the inep status */
Linus Walleij58b62792007-01-07 12:38:59 +00001594 status = 0;
Linus Walleij1fd2d272006-05-08 09:22:01 +00001595 ret = usb_get_endpoint_status(ptp_usb,ptp_usb->inep,&status);
Linus Walleij58b62792007-01-07 12:38:59 +00001596 if (ret<0) {
1597 perror ("inep: usb_get_endpoint_status()");
1598 } else if (status) {
nicklas79daadbf22009-09-28 18:19:34 +00001599 LIBMTP_INFO("Clearing stall on IN endpoint\n");
Linus Walleij58b62792007-01-07 12:38:59 +00001600 ret = usb_clear_stall_feature(ptp_usb,ptp_usb->inep);
1601 if (ret<0) {
1602 perror ("usb_clear_stall_feature()");
1603 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001604 }
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001605
Linus Walleijd6a49972006-05-02 08:24:54 +00001606 /* check the outep status */
Linus Walleij58b62792007-01-07 12:38:59 +00001607 status=0;
1608 ret = usb_get_endpoint_status(ptp_usb,ptp_usb->outep,&status);
1609 if (ret<0) {
1610 perror("outep: usb_get_endpoint_status()");
1611 } else if (status) {
nicklas79daadbf22009-09-28 18:19:34 +00001612 LIBMTP_INFO("Clearing stall on OUT endpoint\n");
Linus Walleij58b62792007-01-07 12:38:59 +00001613 ret = usb_clear_stall_feature(ptp_usb,ptp_usb->outep);
1614 if (ret<0) {
1615 perror("usb_clear_stall_feature()");
1616 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001617 }
Linus Walleij58b62792007-01-07 12:38:59 +00001618
1619 /* TODO: do we need this for INTERRUPT (ptp_usb->intep) too? */
1620}
1621
1622static void clear_halt(PTP_USB* ptp_usb)
1623{
1624 int ret;
1625
1626 ret = usb_clear_halt(ptp_usb->handle,ptp_usb->inep);
1627 if (ret<0) {
1628 perror("usb_clear_halt() on IN endpoint");
1629 }
1630 ret = usb_clear_halt(ptp_usb->handle,ptp_usb->outep);
1631 if (ret<0) {
1632 perror("usb_clear_halt() on OUT endpoint");
1633 }
1634 ret = usb_clear_halt(ptp_usb->handle,ptp_usb->intep);
1635 if (ret<0) {
1636 perror("usb_clear_halt() on INTERRUPT endpoint");
1637 }
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001638}
1639
Linus Walleijb0ab5482007-08-29 21:08:54 +00001640static void close_usb(PTP_USB* ptp_usb)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001641{
Linus Walleijda17cda2007-09-18 21:12:05 +00001642 // Commented out since it was confusing some
1643 // devices to do these things.
Linus Walleijfec4d562008-06-01 22:30:36 +00001644 if (!FLAG_NO_RELEASE_INTERFACE(ptp_usb)) {
Linus Walleijeabc2312008-02-13 00:02:00 +00001645 /*
1646 * Clear any stalled endpoints
1647 * On misbehaving devices designed for Windows/Mac, quote from:
1648 * http://www2.one-eyed-alien.net/~mdharm/linux-usb/target_offenses.txt
1649 * Device does Bad Things(tm) when it gets a GET_STATUS after CLEAR_HALT
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001650 * (...) Windows, when clearing a stall, only sends the CLEAR_HALT command,
1651 * and presumes that the stall has cleared. Some devices actually choke
1652 * if the CLEAR_HALT is followed by a GET_STATUS (used to determine if the
Linus Walleijeabc2312008-02-13 00:02:00 +00001653 * STALL is persistant or not).
1654 */
Linus Walleij29f03ba2007-09-25 19:22:12 +00001655 clear_stall(ptp_usb);
1656 // Clear halts on any endpoints
1657 clear_halt(ptp_usb);
1658 // Added to clear some stuff on the OUT endpoint
1659 // TODO: is this good on the Mac too?
1660 // HINT: some devices may need that you comment these two out too.
1661 usb_resetep(ptp_usb->handle, ptp_usb->outep);
1662 usb_release_interface(ptp_usb->handle, (int) ptp_usb->interface);
1663 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001664 // Brutally reset device
1665 // TODO: is this good on the Mac too?
1666 usb_reset(ptp_usb->handle);
1667 usb_close(ptp_usb->handle);
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001668}
1669
Linus Walleij550d6d52007-01-24 14:32:51 +00001670/**
Linus Walleija700d222008-05-28 23:06:14 +00001671 * Self-explanatory?
1672 */
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001673static int find_interface_and_endpoints(struct usb_device *dev,
1674 uint8_t *interface,
1675 int* inep,
1676 int* inep_maxpacket,
1677 int* outep,
1678 int *outep_maxpacket,
1679 int* intep)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001680{
Linus Walleijd7373cd2007-08-28 21:42:05 +00001681 int i;
1682
1683 // Loop over the device configurations
1684 for (i = 0; i < dev->descriptor.bNumConfigurations; i++) {
Linus Walleijb0ab5482007-08-29 21:08:54 +00001685 uint8_t j;
Linus Walleijd7373cd2007-08-28 21:42:05 +00001686
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001687 // Loop over each configurations interfaces
Linus Walleijd7373cd2007-08-28 21:42:05 +00001688 for (j = 0; j < dev->config[i].bNumInterfaces; j++) {
Linus Walleijb0ab5482007-08-29 21:08:54 +00001689 uint8_t k;
1690 uint8_t no_ep;
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001691 int found_inep = 0;
1692 int found_outep = 0;
1693 int found_intep = 0;
Linus Walleijd7373cd2007-08-28 21:42:05 +00001694 struct usb_endpoint_descriptor *ep;
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001695
1696 // MTP devices shall have 3 endpoints, ignore those interfaces
1697 // that haven't.
1698 no_ep = dev->config[i].interface[j].altsetting->bNumEndpoints;
1699 if (no_ep != 3)
1700 continue;
Linus Walleijb0ab5482007-08-29 21:08:54 +00001701
1702 *interface = dev->config[i].interface[j].altsetting->bInterfaceNumber;
1703 ep = dev->config[i].interface[j].altsetting->endpoint;
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001704
1705 // Loop over the three endpoints to locate two bulk and
1706 // one interrupt endpoint and FAIL if we cannot, and continue.
Linus Walleijb0ab5482007-08-29 21:08:54 +00001707 for (k = 0; k < no_ep; k++) {
Linus Walleijd7373cd2007-08-28 21:42:05 +00001708 if (ep[k].bmAttributes==USB_ENDPOINT_TYPE_BULK) {
1709 if ((ep[k].bEndpointAddress&USB_ENDPOINT_DIR_MASK)==
1710 USB_ENDPOINT_DIR_MASK)
1711 {
1712 *inep=ep[k].bEndpointAddress;
1713 *inep_maxpacket=ep[k].wMaxPacketSize;
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001714 found_inep = 1;
Linus Walleijd7373cd2007-08-28 21:42:05 +00001715 }
1716 if ((ep[k].bEndpointAddress&USB_ENDPOINT_DIR_MASK)==0)
1717 {
1718 *outep=ep[k].bEndpointAddress;
1719 *outep_maxpacket=ep[k].wMaxPacketSize;
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001720 found_outep = 1;
Linus Walleijd7373cd2007-08-28 21:42:05 +00001721 }
1722 } else if (ep[k].bmAttributes==USB_ENDPOINT_TYPE_INTERRUPT){
1723 if ((ep[k].bEndpointAddress&USB_ENDPOINT_DIR_MASK)==
1724 USB_ENDPOINT_DIR_MASK)
1725 {
1726 *intep=ep[k].bEndpointAddress;
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001727 found_intep = 1;
Linus Walleijd7373cd2007-08-28 21:42:05 +00001728 }
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001729 }
Linus Walleijd7373cd2007-08-28 21:42:05 +00001730 }
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001731 if (found_inep && found_outep && found_intep)
1732 // We assigned the endpoints so return here.
1733 return 0;
1734 // Else loop to next interface/config
Linus Walleijd6a49972006-05-02 08:24:54 +00001735 }
1736 }
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001737 return -1;
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001738}
1739
Linus Walleija700d222008-05-28 23:06:14 +00001740/**
1741 * This function assigns params and usbinfo given a raw device
1742 * as input.
1743 * @param device the device to be assigned.
Linus Walleija700d222008-05-28 23:06:14 +00001744 * @param usbinfo a pointer to the new usbinfo.
1745 * @return an error code.
1746 */
1747LIBMTP_error_number_t configure_usb_device(LIBMTP_raw_device_t *device,
Linus Walleij3c643252008-05-31 23:22:28 +00001748 PTPParams *params,
Linus Walleija700d222008-05-28 23:06:14 +00001749 void **usbinfo)
1750{
Linus Walleija700d222008-05-28 23:06:14 +00001751 PTP_USB *ptp_usb;
1752 struct usb_device *libusb_device;
1753 uint16_t ret = 0;
Linus Walleija700d222008-05-28 23:06:14 +00001754 struct usb_bus *bus;
1755 int found = 0;
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001756 int err;
Linus Walleija700d222008-05-28 23:06:14 +00001757
1758 /* See if we can find this raw device again... */
1759 bus = init_usb();
1760 for (; bus != NULL; bus = bus->next) {
1761 if (bus->location == device->bus_location) {
1762 struct usb_device *dev = bus->devices;
1763
1764 for (; dev != NULL; dev = dev->next) {
1765 if(dev->devnum == device->devnum &&
1766 dev->descriptor.idVendor == device->device_entry.vendor_id &&
1767 dev->descriptor.idProduct == device->device_entry.product_id ) {
1768 libusb_device = dev;
1769 found = 1;
1770 break;
1771 }
1772 }
1773 if (found)
1774 break;
1775 }
1776 }
1777 /* Device has gone since detecting raw devices! */
1778 if (!found) {
1779 return LIBMTP_ERROR_NO_DEVICE_ATTACHED;
1780 }
1781
1782 /* Allocate structs */
Linus Walleija700d222008-05-28 23:06:14 +00001783 ptp_usb = (PTP_USB *) malloc(sizeof(PTP_USB));
Linus Walleij3c643252008-05-31 23:22:28 +00001784 if (ptp_usb == NULL) {
Linus Walleija700d222008-05-28 23:06:14 +00001785 return LIBMTP_ERROR_MEMORY_ALLOCATION;
1786 }
1787 /* Start with a blank slate (includes setting device_flags to 0) */
Linus Walleija700d222008-05-28 23:06:14 +00001788 memset(ptp_usb, 0, sizeof(PTP_USB));
1789
Linus Walleijfec4d562008-06-01 22:30:36 +00001790 /* Copy the raw device */
1791 memcpy(&ptp_usb->rawdevice, device, sizeof(LIBMTP_raw_device_t));
Linus Walleij335a81c2008-06-02 23:01:00 +00001792
1793 /*
1794 * Some devices must have their "OS Descriptor" massaged in order
1795 * to work.
1796 */
1797 if (FLAG_ALWAYS_PROBE_DESCRIPTOR(ptp_usb)) {
1798 // Massage the device descriptor
1799 (void) probe_device_descriptor(libusb_device, NULL);
1800 }
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001801
1802 /* Assign interface and endpoints to usbinfo... */
1803 err = find_interface_and_endpoints(libusb_device,
1804 &ptp_usb->interface,
1805 &ptp_usb->inep,
1806 &ptp_usb->inep_maxpacket,
1807 &ptp_usb->outep,
1808 &ptp_usb->outep_maxpacket,
1809 &ptp_usb->intep);
1810
1811 if (err) {
nicklas79daadbf22009-09-28 18:19:34 +00001812 LIBMTP_ERROR("LIBMTP PANIC: Unable to find interface & endpoints of device\n");
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001813 return LIBMTP_ERROR_CONNECTING;
1814 }
1815
Linus Walleija700d222008-05-28 23:06:14 +00001816 /* Attempt to initialize this device */
Linus Walleij3c643252008-05-31 23:22:28 +00001817 if (init_ptp_usb(params, ptp_usb, libusb_device) < 0) {
nicklas79daadbf22009-09-28 18:19:34 +00001818 LIBMTP_ERROR("LIBMTP PANIC: Unable to initialize device\n");
Linus Walleija700d222008-05-28 23:06:14 +00001819 return LIBMTP_ERROR_CONNECTING;
1820 }
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001821
Linus Walleija700d222008-05-28 23:06:14 +00001822 /*
1823 * This works in situations where previous bad applications
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001824 * have not used LIBMTP_Release_Device on exit
Linus Walleija700d222008-05-28 23:06:14 +00001825 */
Linus Walleij3c643252008-05-31 23:22:28 +00001826 if ((ret = ptp_opensession(params, 1)) == PTP_ERROR_IO) {
nicklas79daadbf22009-09-28 18:19:34 +00001827 LIBMTP_ERROR("PTP_ERROR_IO: Trying again after re-initializing USB interface\n");
Linus Walleija700d222008-05-28 23:06:14 +00001828 close_usb(ptp_usb);
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001829
Linus Walleij3c643252008-05-31 23:22:28 +00001830 if(init_ptp_usb(params, ptp_usb, libusb_device) <0) {
nicklas79daadbf22009-09-28 18:19:34 +00001831 LIBMTP_ERROR("LIBMTP PANIC: Could not open session on device\n");
Linus Walleija700d222008-05-28 23:06:14 +00001832 return LIBMTP_ERROR_CONNECTING;
1833 }
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001834
Linus Walleija700d222008-05-28 23:06:14 +00001835 /* Device has been reset, try again */
Linus Walleij3c643252008-05-31 23:22:28 +00001836 ret = ptp_opensession(params, 1);
Linus Walleija700d222008-05-28 23:06:14 +00001837 }
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001838
Linus Walleija700d222008-05-28 23:06:14 +00001839 /* Was the transaction id invalid? Try again */
1840 if (ret == PTP_RC_InvalidTransactionID) {
nicklas79daadbf22009-09-28 18:19:34 +00001841 LIBMTP_ERROR("LIBMTP WARNING: Transaction ID was invalid, increment and try again\n");
Linus Walleij3c643252008-05-31 23:22:28 +00001842 params->transaction_id += 10;
1843 ret = ptp_opensession(params, 1);
Linus Walleija700d222008-05-28 23:06:14 +00001844 }
1845
1846 if (ret != PTP_RC_SessionAlreadyOpened && ret != PTP_RC_OK) {
nicklas79daadbf22009-09-28 18:19:34 +00001847 LIBMTP_ERROR("LIBMTP PANIC: Could not open session! "
Linus Walleija700d222008-05-28 23:06:14 +00001848 "(Return code %d)\n Try to reset the device.\n",
1849 ret);
1850 usb_release_interface(ptp_usb->handle,
1851 (int) ptp_usb->interface);
1852 return LIBMTP_ERROR_CONNECTING;
1853 }
1854
1855 /* OK configured properly */
Linus Walleija700d222008-05-28 23:06:14 +00001856 *usbinfo = (void *) ptp_usb;
1857 return LIBMTP_ERROR_NONE;
1858}
1859
1860
Linus Walleijb0ab5482007-08-29 21:08:54 +00001861void close_device (PTP_USB *ptp_usb, PTPParams *params)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001862{
Linus Walleijd6a49972006-05-02 08:24:54 +00001863 if (ptp_closesession(params)!=PTP_RC_OK)
nicklas79daadbf22009-09-28 18:19:34 +00001864 LIBMTP_ERROR("ERROR: Could not close session!\n");
Linus Walleijb0ab5482007-08-29 21:08:54 +00001865 close_usb(ptp_usb);
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001866}
1867
Linus Walleij2f622812008-08-30 22:06:58 +00001868void set_usb_device_timeout(PTP_USB *ptp_usb, int timeout)
1869{
1870 ptp_usb->timeout = timeout;
1871}
1872
1873void get_usb_device_timeout(PTP_USB *ptp_usb, int *timeout)
1874{
1875 *timeout = ptp_usb->timeout;
1876}
1877
Linus Walleijd6a49972006-05-02 08:24:54 +00001878static int usb_clear_stall_feature(PTP_USB* ptp_usb, int ep)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001879{
Linus Walleijd6a49972006-05-02 08:24:54 +00001880 return (usb_control_msg(ptp_usb->handle,
1881 USB_RECIP_ENDPOINT, USB_REQ_CLEAR_FEATURE, USB_FEATURE_HALT,
Linus Walleij2f622812008-08-30 22:06:58 +00001882 ep, NULL, 0, ptp_usb->timeout));
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001883}
1884
Linus Walleijd6a49972006-05-02 08:24:54 +00001885static int usb_get_endpoint_status(PTP_USB* ptp_usb, int ep, uint16_t* status)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001886{
Linus Walleijd6a49972006-05-02 08:24:54 +00001887 return (usb_control_msg(ptp_usb->handle,
1888 USB_DP_DTH|USB_RECIP_ENDPOINT, USB_REQ_GET_STATUS,
Linus Walleij2f622812008-08-30 22:06:58 +00001889 USB_FEATURE_HALT, ep, (char *)status, 2, ptp_usb->timeout));
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001890}