blob: 51514ce6184cd11b9cdc24ea1a96e881c29e3696 [file] [log] [blame]
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001/*
Linus Walleij2f45d222007-02-02 22:47:39 +00002 * \file libusb-glue.c
3 * Low-level USB interface glue towards libusb.
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00004 *
Linus Walleij2f45d222007-02-02 22:47:39 +00005 * Copyright (C) 2005-2007 Richard A. Low <richard@wentnet.com>
Linus Walleijbac1eed2011-02-04 23:55:19 +01006 * Copyright (C) 2005-2011 Linus Walleij <triad@df.lth.se>
Linus Walleij2f45d222007-02-02 22:47:39 +00007 * Copyright (C) 2006-2007 Marcus Meissner
8 * Copyright (C) 2007 Ted Bullock
Linus Walleij2f622812008-08-30 22:06:58 +00009 * Copyright (C) 2008 Chris Bagwell <chris@cnpbagwell.com>
Linus Walleijeb8c6fe2006-02-03 09:46:22 +000010 *
Linus Walleij2f45d222007-02-02 22:47:39 +000011 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2 of the License, or (at your option) any later version.
Linus Walleij2e1c8a32007-01-22 06:46:08 +000015 *
Linus Walleij2f45d222007-02-02 22:47:39 +000016 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
Linus Walleij2e1c8a32007-01-22 06:46:08 +000020 *
Linus Walleij2f45d222007-02-02 22:47:39 +000021 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the
23 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 * Boston, MA 02111-1307, USA.
25 *
26 * Created by Richard Low on 24/12/2005. (as mtp-utils.c)
27 * Modified by Linus Walleij 2006-03-06
Linus Walleijd866d242009-08-23 21:50:39 +000028 * (Notice that Anglo-Saxons use little-endian dates and Swedes
Linus Walleij2f45d222007-02-02 22:47:39 +000029 * use big-endian dates.)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +000030 *
31 */
Linus Walleij21b2dde2009-06-02 19:55:34 +000032#include "config.h"
Linus Walleij7beba572006-11-29 08:56:12 +000033#include "libmtp.h"
34#include "libusb-glue.h"
Linus Walleij3e667ae2007-10-29 23:29:39 +000035#include "device-flags.h"
Linus Walleij7beba572006-11-29 08:56:12 +000036#include "util.h"
Linus Walleijeb8c6fe2006-02-03 09:46:22 +000037#include "ptp.h"
Linus Walleij7beba572006-11-29 08:56:12 +000038
Linus Walleijeb8c6fe2006-02-03 09:46:22 +000039#include <errno.h>
40#include <stdio.h>
41#include <stdlib.h>
42#include <string.h>
Linus Walleijeb8c6fe2006-02-03 09:46:22 +000043#include <usb.h>
44
Linus Walleija0323272007-01-07 12:21:30 +000045#include "ptp-pack.c"
46
Linus Walleijd866d242009-08-23 21:50:39 +000047/* Aha, older libusb does not have USB_CLASS_PTP */
48#ifndef USB_CLASS_PTP
49#define USB_CLASS_PTP 6
50#endif
51
Linus Walleije04a1b92011-03-09 18:00:24 +010052/*
53 * Default USB timeout length. This can be overridden as needed
54 * but should start with a reasonable value so most common
Linus Walleij2f622812008-08-30 22:06:58 +000055 * requests can be completed. The original value of 4000 was
56 * not long enough for large file transfer. Also, players can
57 * spend a bit of time collecting data. Higher values also
58 * make connecting/disconnecting more reliable.
59 */
Linus Walleije04a1b92011-03-09 18:00:24 +010060#define USB_TIMEOUT_DEFAULT 10000
61#define USB_TIMEOUT_LONG 60000
62static inline int get_timeout(PTP_USB* ptp_usb)
63{
64 if (FLAG_LONG_TIMEOUT(ptp_usb)) {
65 return USB_TIMEOUT_LONG;
66 }
67 return USB_TIMEOUT_DEFAULT;
68}
Linus Walleijeb8c6fe2006-02-03 09:46:22 +000069
70/* USB control message data phase direction */
71#ifndef USB_DP_HTD
72#define USB_DP_HTD (0x00 << 7) /* host to device */
73#endif
74#ifndef USB_DP_DTH
75#define USB_DP_DTH (0x01 << 7) /* device to host */
76#endif
77
78/* USB Feature selector HALT */
79#ifndef USB_FEATURE_HALT
80#define USB_FEATURE_HALT 0x00
81#endif
82
Linus Walleija700d222008-05-28 23:06:14 +000083/* Internal data types */
84struct mtpdevice_list_struct {
85 struct usb_device *libusb_device;
86 PTPParams *params;
87 PTP_USB *ptp_usb;
88 uint32_t bus_location;
89 struct mtpdevice_list_struct *next;
90};
91typedef struct mtpdevice_list_struct mtpdevice_list_t;
92
Linus Walleij6fd2f082006-03-28 07:19:22 +000093static const LIBMTP_device_entry_t mtp_device_table[] = {
Linus Walleij1a673de2007-10-29 23:10:05 +000094/* We include an .h file which is shared between us and libgphoto2 */
95#include "music-players.h"
Linus Walleija5483642006-03-09 09:20:38 +000096};
Linus Walleij6fd2f082006-03-28 07:19:22 +000097static const int mtp_device_table_size = sizeof(mtp_device_table) / sizeof(LIBMTP_device_entry_t);
Linus Walleija5483642006-03-09 09:20:38 +000098
Linus Walleijd6a49972006-05-02 08:24:54 +000099// Local functions
Linus Walleij1fd2d272006-05-08 09:22:01 +0000100static struct usb_bus* init_usb();
Linus Walleijb0ab5482007-08-29 21:08:54 +0000101static void close_usb(PTP_USB* ptp_usb);
Linus Walleij5f3c44b2009-09-12 21:03:06 +0000102static int find_interface_and_endpoints(struct usb_device *dev,
103 uint8_t *interface,
104 int* inep,
105 int* inep_maxpacket,
106 int* outep,
107 int* outep_maxpacket,
108 int* intep);
Linus Walleijd6a49972006-05-02 08:24:54 +0000109static void clear_stall(PTP_USB* ptp_usb);
Linus Walleij9eb3d312006-08-04 19:25:59 +0000110static int init_ptp_usb (PTPParams* params, PTP_USB* ptp_usb, struct usb_device* dev);
Linus Walleij784ac3f2006-12-29 10:36:51 +0000111static short ptp_write_func (unsigned long,PTPDataHandler*,void *data,unsigned long*);
Richard Low4df32f82007-01-11 20:04:39 +0000112static short ptp_read_func (unsigned long,PTPDataHandler*,void *data,unsigned long*,int);
Linus Walleijd6a49972006-05-02 08:24:54 +0000113static int usb_clear_stall_feature(PTP_USB* ptp_usb, int ep);
114static int usb_get_endpoint_status(PTP_USB* ptp_usb, int ep, uint16_t* status);
Linus Walleij1fd2d272006-05-08 09:22:01 +0000115
Linus Walleij552ba322007-01-22 11:49:59 +0000116/**
117 * Get a list of the supported USB devices.
118 *
119 * The developers depend on users of this library to constantly
120 * add in to the list of supported devices. What we need is the
121 * device name, USB Vendor ID (VID) and USB Product ID (PID).
122 * put this into a bug ticket at the project homepage, please.
123 * The VID/PID is used to let e.g. udev lift the device to
124 * console userspace access when it's plugged in.
125 *
126 * @param devices a pointer to a pointer that will hold a device
127 * list after the call to this function, if it was
128 * successful.
129 * @param numdevs a pointer to an integer that will hold the number
130 * of devices in the device list if the call was successful.
131 * @return 0 if the list was successfull retrieved, any other
132 * value means failure.
133 */
134int LIBMTP_Get_Supported_Devices_List(LIBMTP_device_entry_t ** const devices, int * const numdevs)
Linus Walleij6fd2f082006-03-28 07:19:22 +0000135{
136 *devices = (LIBMTP_device_entry_t *) &mtp_device_table;
137 *numdevs = mtp_device_table_size;
138 return 0;
139}
140
Linus Walleij552ba322007-01-22 11:49:59 +0000141
Linus Walleij1fd2d272006-05-08 09:22:01 +0000142static struct usb_bus* init_usb()
143{
Linus Walleijc1a82f12009-11-30 00:06:28 +0000144 struct usb_bus* busses;
145 struct usb_bus* bus;
146
147 /*
nicklas79daadbf22009-09-28 18:19:34 +0000148 * Some additional libusb debugging please.
149 * We use the same level debug between MTP and USB.
150 */
nicklas7903584082009-09-28 18:20:16 +0000151 if ((LIBMTP_debug & LIBMTP_DEBUG_USB) != 0)
nicklas79a99196a2009-09-28 18:19:57 +0000152 usb_set_debug(9);
nicklas79daadbf22009-09-28 18:19:34 +0000153
Linus Walleij1fd2d272006-05-08 09:22:01 +0000154 usb_init();
155 usb_find_busses();
156 usb_find_devices();
Linus Walleijc1a82f12009-11-30 00:06:28 +0000157 /* Workaround a libusb 0.1 bug : bus location is not initialised */
158 busses = usb_get_busses();
159 for (bus = busses; bus != NULL; bus = bus->next) {
Richard Lowea73f5c2010-05-16 11:57:56 +0000160 if (!bus->location)
161 bus->location = strtoul(bus->dirname, NULL, 10);
Linus Walleijc1a82f12009-11-30 00:06:28 +0000162 }
163 return (busses);
Linus Walleij1fd2d272006-05-08 09:22:01 +0000164}
165
166/**
tedbullock0f033cb2007-02-14 20:56:54 +0000167 * Small recursive function to append a new usb_device to the linked list of
168 * USB MTP devices
Linus Walleijc1a82f12009-11-30 00:06:28 +0000169 * @param devlist dynamic linked list of pointers to usb devices with MTP
Linus Walleij63fd1e62008-04-23 23:49:43 +0000170 * properties, to be extended with new device.
171 * @param newdevice the new device to add.
172 * @param bus_location bus for this device.
173 * @return an extended array or NULL on failure.
tedbullock0f033cb2007-02-14 20:56:54 +0000174 */
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000175static mtpdevice_list_t *append_to_mtpdevice_list(mtpdevice_list_t *devlist,
Linus Walleij63fd1e62008-04-23 23:49:43 +0000176 struct usb_device *newdevice,
177 uint32_t bus_location)
tedbullock0f033cb2007-02-14 20:56:54 +0000178{
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000179 mtpdevice_list_t *new_list_entry;
Linus Walleijc1a82f12009-11-30 00:06:28 +0000180
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000181 new_list_entry = (mtpdevice_list_t *) malloc(sizeof(mtpdevice_list_t));
182 if (new_list_entry == NULL) {
183 return NULL;
184 }
185 // Fill in USB device, if we *HAVE* to make a copy of the device do it here.
186 new_list_entry->libusb_device = newdevice;
Linus Walleij63fd1e62008-04-23 23:49:43 +0000187 new_list_entry->bus_location = bus_location;
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000188 new_list_entry->next = NULL;
Linus Walleij5d987e12010-05-22 22:35:33 +0000189
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000190 if (devlist == NULL) {
191 return new_list_entry;
192 } else {
193 mtpdevice_list_t *tmp = devlist;
194 while (tmp->next != NULL) {
195 tmp = tmp->next;
196 }
197 tmp->next = new_list_entry;
198 }
199 return devlist;
tedbullock0f033cb2007-02-14 20:56:54 +0000200}
201
202/**
203 * Small recursive function to free dynamic memory allocated to the linked list
204 * of USB MTP devices
Linus Walleij5d987e12010-05-22 22:35:33 +0000205 * @param devlist dynamic linked list of pointers to usb devices with MTP
tedbullock0f033cb2007-02-14 20:56:54 +0000206 * properties.
207 * @return nothing
208 */
Linus Walleija700d222008-05-28 23:06:14 +0000209static void free_mtpdevice_list(mtpdevice_list_t *devlist)
tedbullock0f033cb2007-02-14 20:56:54 +0000210{
Linus Walleijac061862007-03-07 08:28:33 +0000211 mtpdevice_list_t *tmplist = devlist;
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000212
213 if (devlist == NULL)
214 return;
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000215 while (tmplist != NULL) {
216 mtpdevice_list_t *tmp = tmplist;
217 tmplist = tmplist->next;
Richard Low61edc1a2007-09-23 10:35:48 +0000218 // Do not free() the fields (ptp_usb, params)! These are used elsewhere.
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000219 free(tmp);
tedbullock0f033cb2007-02-14 20:56:54 +0000220 }
tedbullock0f033cb2007-02-14 20:56:54 +0000221 return;
222}
223
224/**
Linus Walleij9521e2b2007-07-10 21:50:42 +0000225 * This checks if a device has an MTP descriptor. The descriptor was
226 * elaborated about in gPhoto bug 1482084, and some official documentation
227 * with no strings attached was published by Microsoft at
228 * http://www.microsoft.com/whdc/system/bus/USB/USBFAQ_intermed.mspx#E3HAC
229 *
Linus Walleija4942fc2007-03-12 19:23:21 +0000230 * @param dev a device struct from libusb.
Linus Walleij9521e2b2007-07-10 21:50:42 +0000231 * @param dumpfile set to non-NULL to make the descriptors dump out
232 * to this file in human-readable hex so we can scruitinze them.
Linus Walleija4942fc2007-03-12 19:23:21 +0000233 * @return 1 if the device is MTP compliant, 0 if not.
234 */
Linus Walleij9521e2b2007-07-10 21:50:42 +0000235static int probe_device_descriptor(struct usb_device *dev, FILE *dumpfile)
Linus Walleija4942fc2007-03-12 19:23:21 +0000236{
237 usb_dev_handle *devh;
238 unsigned char buf[1024], cmd;
Linus Walleijbc550bc2007-11-08 23:25:09 +0000239 int i;
Linus Walleija4942fc2007-03-12 19:23:21 +0000240 int ret;
Linus Walleijfb3e3022011-01-19 19:20:34 +0100241 /* This is to indicate if we find some vendor interface */
242 int found_vendor_spec_interface = 0;
Linus Walleij5d987e12010-05-22 22:35:33 +0000243
244 /*
Linus Walleijfb3e3022011-01-19 19:20:34 +0100245 * Don't examine devices that are not likely to
246 * contain any MTP interface, update this the day
247 * you find some weird combination...
Linus Walleij5d987e12010-05-22 22:35:33 +0000248 */
Linus Walleij4eb8ad92011-01-27 22:52:07 +0100249 if (!(dev->descriptor.bDeviceClass == USB_CLASS_PER_INTERFACE ||
250 dev->descriptor.bDeviceClass == USB_CLASS_COMM ||
251 dev->descriptor.bDeviceClass == USB_CLASS_PTP ||
252 dev->descriptor.bDeviceClass == USB_CLASS_VENDOR_SPEC)) {
Linus Walleija4942fc2007-03-12 19:23:21 +0000253 return 0;
254 }
Linus Walleij5d987e12010-05-22 22:35:33 +0000255
Linus Walleija4942fc2007-03-12 19:23:21 +0000256 /* Attempt to open Device on this port */
257 devh = usb_open(dev);
258 if (devh == NULL) {
259 /* Could not open this device */
260 return 0;
261 }
Linus Walleijbc550bc2007-11-08 23:25:09 +0000262
263 /*
Richard Lowe029eba2009-04-11 12:36:25 +0000264 * This sometimes crashes on the j for loop below
Linus Walleij88244212009-08-12 16:05:56 +0000265 * I think it is because config is NULL yet
Richard Lowe029eba2009-04-11 12:36:25 +0000266 * dev->descriptor.bNumConfigurations > 0
267 * this check should stop this
Linus Walleijbc550bc2007-11-08 23:25:09 +0000268 */
Richard Lowe029eba2009-04-11 12:36:25 +0000269 if (dev->config) {
270 /*
Linus Walleij88244212009-08-12 16:05:56 +0000271 * Loop over the device configurations and interfaces. Nokia MTP-capable
272 * handsets (possibly others) typically have the string "MTP" in their
Richard Lowe029eba2009-04-11 12:36:25 +0000273 * MTP interface descriptions, that's how they can be detected, before
274 * we try the more esoteric "OS descriptors" (below).
275 */
276 for (i = 0; i < dev->descriptor.bNumConfigurations; i++) {
277 uint8_t j;
Linus Walleij88244212009-08-12 16:05:56 +0000278
Richard Lowe029eba2009-04-11 12:36:25 +0000279 for (j = 0; j < dev->config[i].bNumInterfaces; j++) {
280 int k;
281 for (k = 0; k < dev->config[i].interface[j].num_altsetting; k++) {
Linus Walleij88244212009-08-12 16:05:56 +0000282 /* Current interface descriptor */
283 struct usb_interface_descriptor *intf =
284 &dev->config[i].interface[j].altsetting[k];
285
286 /*
Linus Walleijfb3e3022011-01-19 19:20:34 +0100287 * We only want to probe for the OS descriptor if the
288 * device is USB_CLASS_VENDOR_SPEC or one of the interfaces
289 * in it is, so flag if we find an interface like this.
290 */
291 if (intf->bInterfaceClass == USB_CLASS_VENDOR_SPEC) {
292 found_vendor_spec_interface = 1;
293 }
294
295 /*
Linus Walleij88244212009-08-12 16:05:56 +0000296 * Check for Still Image Capture class with PIMA 15740 protocol,
297 * also known as PTP
298 */
Linus Walleij5d92c8c2011-01-25 23:42:19 +0100299#if 0
Linus Walleij88244212009-08-12 16:05:56 +0000300 if (intf->bInterfaceClass == USB_CLASS_PTP
301 && intf->bInterfaceSubClass == 0x01
302 && intf->bInterfaceProtocol == 0x01) {
303 if (dumpfile != NULL) {
Linus Walleij5d92c8c2011-01-25 23:42:19 +0100304 fprintf(dumpfile, " Found PTP device, check vendor "
305 "extension...\n");
Linus Walleij88244212009-08-12 16:05:56 +0000306 }
Linus Walleij5d92c8c2011-01-25 23:42:19 +0100307 // This is where we may insert code to open a PTP
308 // session and query the vendor extension ID to see
Linus Walleij4eb8ad92011-01-27 22:52:07 +0100309 // if it is 0xffffffff, i.e. MTP according to the spec.
Linus Walleij5d92c8c2011-01-25 23:42:19 +0100310 if (was_mtp_extension) {
311 usb_close(devh);
312 return 1;
313 }
Linus Walleij88244212009-08-12 16:05:56 +0000314 }
Linus Walleij5d92c8c2011-01-25 23:42:19 +0100315#endif
316
317 /*
318 * Next we search for the MTP substring in the interface name.
319 * For example : "RIM MS/MTP" should work.
320 */
Linus Walleij88244212009-08-12 16:05:56 +0000321 buf[0] = '\0';
322 ret = usb_get_string_simple(devh,
323 dev->config[i].interface[j].altsetting[k].iInterface,
324 (char *) buf,
325 1024);
326 if (ret < 3)
327 continue;
Linus Walleijfea4f532009-09-22 22:08:35 +0000328 if (strstr((char *) buf, "MTP") != NULL) {
Linus Walleij88244212009-08-12 16:05:56 +0000329 if (dumpfile != NULL) {
330 fprintf(dumpfile, "Configuration %d, interface %d, altsetting %d:\n", i, j, k);
331 fprintf(dumpfile, " Interface description contains the string \"MTP\"\n");
332 fprintf(dumpfile, " Device recognized as MTP, no further probing.\n");
333 }
Richard Lowe029eba2009-04-11 12:36:25 +0000334 usb_close(devh);
335 return 1;
336 }
337 #ifdef LIBUSB_HAS_GET_DRIVER_NP
Linus Walleij88244212009-08-12 16:05:56 +0000338 {
339 /*
340 * Specifically avoid probing anything else than USB mass storage devices
341 * and non-associated drivers in Linux.
342 */
343 char devname[0x10];
344
345 devname[0] = '\0';
346 ret = usb_get_driver_np(devh,
347 dev->config[i].interface[j].altsetting[k].iInterface,
348 devname,
349 sizeof(devname));
350 if (devname[0] != '\0' && strcmp(devname, "usb-storage")) {
nicklas79daadbf22009-09-28 18:19:34 +0000351 LIBMTP_INFO("avoid probing device using kernel interface \"%s\"\n", devname);
Linus Walleij88244212009-08-12 16:05:56 +0000352 return 0;
353 }
354 }
Richard Lowe029eba2009-04-11 12:36:25 +0000355 #endif
356 }
357 }
358 }
359 } else {
360 if (dev->descriptor.bNumConfigurations)
nicklas79daadbf22009-09-28 18:19:34 +0000361 LIBMTP_INFO("dev->config is NULL in probe_device_descriptor yet dev->descriptor.bNumConfigurations > 0\n");
Linus Walleijbc550bc2007-11-08 23:25:09 +0000362 }
Linus Walleijfea4f532009-09-22 22:08:35 +0000363
Linus Walleij49e57b82009-12-16 09:21:33 +0000364 /*
Linus Walleijfb3e3022011-01-19 19:20:34 +0100365 * Only probe for OS descriptor if the device is vendor specific
366 * or one of the interfaces found is.
Linus Walleij49e57b82009-12-16 09:21:33 +0000367 */
Linus Walleijfb3e3022011-01-19 19:20:34 +0100368 if (dev->descriptor.bDeviceClass == USB_CLASS_VENDOR_SPEC ||
369 found_vendor_spec_interface) {
Linus Walleij9521e2b2007-07-10 21:50:42 +0000370
Linus Walleijfb3e3022011-01-19 19:20:34 +0100371 /* Read the special descriptor */
372 ret = usb_get_descriptor(devh, 0x03, 0xee, buf, sizeof(buf));
Linus Walleijfea4f532009-09-22 22:08:35 +0000373
Linus Walleijfb3e3022011-01-19 19:20:34 +0100374 /*
375 * If something failed we're probably stalled to we need
376 * to clear the stall off the endpoint and say this is not
377 * MTP.
378 */
379 if (ret < 0) {
380 /* EP0 is the default control endpoint */
381 usb_clear_halt(devh, 0);
382 usb_close(devh);
383 return 0;
384 }
385
386 // Dump it, if requested
387 if (dumpfile != NULL && ret > 0) {
388 fprintf(dumpfile, "Microsoft device descriptor 0xee:\n");
389 data_dump_ascii(dumpfile, buf, ret, 16);
390 }
391
392 /* Check if descriptor length is at least 10 bytes */
393 if (ret < 10) {
394 usb_close(devh);
395 return 0;
396 }
397
398 /* Check if this device has a Microsoft Descriptor */
399 if (!((buf[2] == 'M') && (buf[4] == 'S') &&
400 (buf[6] == 'F') && (buf[8] == 'T'))) {
401 usb_close(devh);
402 return 0;
403 }
404
405 /* Check if device responds to control message 1 or if there is an error */
406 cmd = buf[16];
407 ret = usb_control_msg (devh,
408 USB_ENDPOINT_IN | USB_RECIP_DEVICE | USB_TYPE_VENDOR,
409 cmd,
410 0,
411 4,
412 (char *) buf,
413 sizeof(buf),
414 USB_TIMEOUT_DEFAULT);
415
416 // Dump it, if requested
417 if (dumpfile != NULL && ret > 0) {
418 fprintf(dumpfile, "Microsoft device response to control message 1, CMD 0x%02x:\n", cmd);
419 data_dump_ascii(dumpfile, buf, ret, 16);
420 }
421
422 /* If this is true, the device either isn't MTP or there was an error */
423 if (ret <= 0x15) {
424 /* TODO: If there was an error, flag it and let the user know somehow */
425 /* if(ret == -1) {} */
426 usb_close(devh);
427 return 0;
428 }
429
430 /* Check if device is MTP or if it is something like a USB Mass Storage
431 device with Janus DRM support */
432 if ((buf[0x12] != 'M') || (buf[0x13] != 'T') || (buf[0x14] != 'P')) {
433 usb_close(devh);
434 return 0;
435 }
436
437 /* After this point we are probably dealing with an MTP device */
438
439 /*
440 * Check if device responds to control message 2, which is
441 * the extended device parameters. Most devices will just
442 * respond with a copy of the same message as for the first
443 * message, some respond with zero-length (which is OK)
444 * and some with pure garbage. We're not parsing the result
445 * so this is not very important.
446 */
447 ret = usb_control_msg (devh,
448 USB_ENDPOINT_IN | USB_RECIP_DEVICE | USB_TYPE_VENDOR,
449 cmd,
450 0,
451 5,
452 (char *) buf,
453 sizeof(buf),
454 USB_TIMEOUT_DEFAULT);
455
456 // Dump it, if requested
457 if (dumpfile != NULL && ret > 0) {
458 fprintf(dumpfile, "Microsoft device response to control message 2, CMD 0x%02x:\n", cmd);
459 data_dump_ascii(dumpfile, buf, ret, 16);
460 }
461
462 /* If this is true, the device errored against control message 2 */
463 if (ret == -1) {
464 /* TODO: Implement callback function to let managing program know there
465 was a problem, along with description of the problem */
466 LIBMTP_ERROR("Potential MTP Device with VendorID:%04x and "
467 "ProductID:%04x encountered an error responding to "
468 "control message 2.\n"
469 "Problems may arrise but continuing\n",
470 dev->descriptor.idVendor, dev->descriptor.idProduct);
471 } else if (dumpfile != NULL && ret == 0) {
472 fprintf(dumpfile, "Zero-length response to control message 2 (OK)\n");
473 } else if (dumpfile != NULL) {
474 fprintf(dumpfile, "Device responds to control message 2 with some data.\n");
475 }
Marcus Meissnere0bb6c62011-02-07 13:26:58 +0100476 /* Close the USB device handle */
477 usb_close(devh);
478 return 1;
Linus Walleija4942fc2007-03-12 19:23:21 +0000479 }
Linus Walleijfea4f532009-09-22 22:08:35 +0000480
Linus Walleija4942fc2007-03-12 19:23:21 +0000481 /* Close the USB device handle */
482 usb_close(devh);
Marcus Meissnere0bb6c62011-02-07 13:26:58 +0100483 return 0;
Linus Walleija4942fc2007-03-12 19:23:21 +0000484}
485
486/**
487 * This function scans through the connected usb devices on a machine and
488 * if they match known Vendor and Product identifiers appends them to the
Linus Walleij49557482010-06-22 09:14:47 +0000489 * dynamic array mtp_device_list. Be sure to call
490 * <code>free_mtpdevice_list(mtp_device_list)</code> when you are done
Linus Walleij63fd1e62008-04-23 23:49:43 +0000491 * with it, assuming it is not NULL.
Linus Walleij49557482010-06-22 09:14:47 +0000492 * @param mtp_device_list dynamic array of pointers to usb devices with MTP
Linus Walleij45a86372007-03-07 09:36:19 +0000493 * properties (if this list is not empty, new entries will be appended
494 * to the list).
Linus Walleija4942fc2007-03-12 19:23:21 +0000495 * @return LIBMTP_ERROR_NONE implies that devices have been found, scan the list
Linus Walleij49557482010-06-22 09:14:47 +0000496 * appropriately. LIBMTP_ERROR_NO_DEVICE_ATTACHED implies that no
Linus Walleij9c6c1542007-09-19 13:43:06 +0000497 * devices have been found.
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000498 */
Linus Walleij45a86372007-03-07 09:36:19 +0000499static LIBMTP_error_number_t get_mtp_usb_device_list(mtpdevice_list_t ** mtp_device_list)
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000500{
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000501 struct usb_bus *bus = init_usb();
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000502 for (; bus != NULL; bus = bus->next) {
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000503 struct usb_device *dev = bus->devices;
Linus Walleijf27d1cd2007-03-05 14:23:00 +0000504 for (; dev != NULL; dev = dev->next) {
Richard Lowe9f205f2007-09-16 16:25:50 +0000505 if (dev->descriptor.bDeviceClass != USB_CLASS_HUB) {
Linus Walleij9c6c1542007-09-19 13:43:06 +0000506 int i;
507 int found = 0;
508
509 // First check if we know about the device already.
510 // Devices well known to us will not have their descriptors
511 // probed, it caused problems with some devices.
Richard Lowe9f205f2007-09-16 16:25:50 +0000512 for(i = 0; i < mtp_device_table_size; i++) {
513 if(dev->descriptor.idVendor == mtp_device_table[i].vendor_id &&
514 dev->descriptor.idProduct == mtp_device_table[i].product_id) {
515 /* Append this usb device to the MTP device list */
Linus Walleij49557482010-06-22 09:14:47 +0000516 *mtp_device_list = append_to_mtpdevice_list(*mtp_device_list,
517 dev,
Linus Walleij63fd1e62008-04-23 23:49:43 +0000518 bus->location);
Richard Lowe9f205f2007-09-16 16:25:50 +0000519 found = 1;
520 break;
521 }
522 }
Linus Walleij9c6c1542007-09-19 13:43:06 +0000523 // If we didn't know it, try probing the "OS Descriptor".
Richard Lowe9f205f2007-09-16 16:25:50 +0000524 if (!found) {
Richard Lowe9f205f2007-09-16 16:25:50 +0000525 if (probe_device_descriptor(dev, NULL)) {
526 /* Append this usb device to the MTP USB Device List */
Linus Walleij49557482010-06-22 09:14:47 +0000527 *mtp_device_list = append_to_mtpdevice_list(*mtp_device_list,
Linus Walleij63fd1e62008-04-23 23:49:43 +0000528 dev,
529 bus->location);
Richard Lowe9f205f2007-09-16 16:25:50 +0000530 }
Linus Walleijd866d242009-08-23 21:50:39 +0000531 /*
532 * By thomas_-_s: Also append devices that are no MTP but PTP devices
533 * if this is commented out.
534 */
535 /*
536 else {
537 // Check whether the device is no USB hub but a PTP.
538 if ( dev->config != NULL &&dev->config->interface->altsetting->bInterfaceClass == USB_CLASS_PTP && dev->descriptor.bDeviceClass != USB_CLASS_HUB ) {
539 *mtp_device_list = append_to_mtpdevice_list(*mtp_device_list, dev, bus->location);
540 }
541 }
542 */
Richard Lowe9f205f2007-09-16 16:25:50 +0000543 }
Linus Walleij9c6c1542007-09-19 13:43:06 +0000544 }
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000545 }
546 }
Linus Walleij49557482010-06-22 09:14:47 +0000547
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000548 /* If nothing was found we end up here. */
Linus Walleija4942fc2007-03-12 19:23:21 +0000549 if(*mtp_device_list == NULL) {
tedbullock433d2172007-02-23 22:39:12 +0000550 return LIBMTP_ERROR_NO_DEVICE_ATTACHED;
Linus Walleija4942fc2007-03-12 19:23:21 +0000551 }
552 return LIBMTP_ERROR_NONE;
Linus Walleij2e1c8a32007-01-22 06:46:08 +0000553}
554
555/**
Linus Walleij549f49a2010-12-05 14:00:34 +0000556 * Checks if a specific device with a certain bus and device
557 * number has an MTP type device descriptor.
558 *
559 * @param busno the bus number of the device to check
560 * @param deviceno the device number of the device to check
561 * @return 1 if the device is MTP else 0
562 */
563int LIBMTP_Check_Specific_Device(int busno, int devno)
564{
565 struct usb_bus *bus = init_usb();
566 for (; bus != NULL; bus = bus->next) {
567 struct usb_device *dev = bus->devices;
568 if (bus->location != busno)
569 continue;
570
571 for (; dev != NULL; dev = dev->next) {
572
573 if (dev->devnum != devno)
574 continue;
575
576 if (probe_device_descriptor(dev, NULL))
577 return 1;
578 }
579 }
580 return 0;
581}
582
583/**
Linus Walleij63fd1e62008-04-23 23:49:43 +0000584 * Detect the raw MTP device descriptors and return a list of
585 * of the devices found.
Linus Walleij49557482010-06-22 09:14:47 +0000586 *
Linus Walleij63fd1e62008-04-23 23:49:43 +0000587 * @param devices a pointer to a variable that will hold
588 * the list of raw devices found. This may be NULL
589 * on return if the number of detected devices is zero.
590 * The user shall simply <code>free()</code> this
591 * variable when finished with the raw devices,
592 * in order to release memory.
Linus Walleij49557482010-06-22 09:14:47 +0000593 * @param numdevs a pointer to an integer that will hold
Linus Walleij63fd1e62008-04-23 23:49:43 +0000594 * the number of devices in the list. This may
595 * be 0.
596 * @return 0 if successful, any other value means failure.
Linus Walleij1fd2d272006-05-08 09:22:01 +0000597 */
Linus Walleij49557482010-06-22 09:14:47 +0000598LIBMTP_error_number_t LIBMTP_Detect_Raw_Devices(LIBMTP_raw_device_t ** devices,
Linus Walleij63fd1e62008-04-23 23:49:43 +0000599 int * numdevs)
Linus Walleij1fd2d272006-05-08 09:22:01 +0000600{
Linus Walleij63fd1e62008-04-23 23:49:43 +0000601 mtpdevice_list_t *devlist = NULL;
602 mtpdevice_list_t *dev;
603 LIBMTP_error_number_t ret;
Linus Walleij63fd1e62008-04-23 23:49:43 +0000604 LIBMTP_raw_device_t *retdevs;
605 int devs = 0;
Linus Walleijf106aa62008-05-04 22:55:51 +0000606 int i, j;
Linus Walleijb6b69132007-03-07 09:45:05 +0000607
608 ret = get_mtp_usb_device_list(&devlist);
Linus Walleijef2fb362008-05-28 20:59:09 +0000609 if (ret == LIBMTP_ERROR_NO_DEVICE_ATTACHED) {
610 *devices = NULL;
611 *numdevs = 0;
Linus Walleija700d222008-05-28 23:06:14 +0000612 return ret;
Linus Walleijef2fb362008-05-28 20:59:09 +0000613 } else if (ret != LIBMTP_ERROR_NONE) {
nicklas79daadbf22009-09-28 18:19:34 +0000614 LIBMTP_ERROR("LIBMTP PANIC: get_mtp_usb_device_list() "
Linus Walleijf106aa62008-05-04 22:55:51 +0000615 "error code: %d on line %d\n", ret, __LINE__);
Linus Walleija700d222008-05-28 23:06:14 +0000616 return ret;
Linus Walleijb6b69132007-03-07 09:45:05 +0000617 }
Linus Walleijf106aa62008-05-04 22:55:51 +0000618
Linus Walleij63fd1e62008-04-23 23:49:43 +0000619 // Get list size
620 dev = devlist;
621 while (dev != NULL) {
622 devs++;
623 dev = dev->next;
624 }
625 if (devs == 0) {
626 *devices = NULL;
627 *numdevs = 0;
Linus Walleija700d222008-05-28 23:06:14 +0000628 return LIBMTP_ERROR_NONE;
Linus Walleij63fd1e62008-04-23 23:49:43 +0000629 }
630 // Conjure a device list
631 retdevs = (LIBMTP_raw_device_t *) malloc(sizeof(LIBMTP_raw_device_t) * devs);
632 if (retdevs == NULL) {
633 // Out of memory
634 *devices = NULL;
635 *numdevs = 0;
Linus Walleija700d222008-05-28 23:06:14 +0000636 return LIBMTP_ERROR_MEMORY_ALLOCATION;
Linus Walleij63fd1e62008-04-23 23:49:43 +0000637 }
638 dev = devlist;
639 i = 0;
640 while (dev != NULL) {
Linus Walleijf106aa62008-05-04 22:55:51 +0000641 int device_known = 0;
642
Linus Walleij63fd1e62008-04-23 23:49:43 +0000643 // Assign default device info
644 retdevs[i].device_entry.vendor = NULL;
645 retdevs[i].device_entry.vendor_id = dev->libusb_device->descriptor.idVendor;
646 retdevs[i].device_entry.product = NULL;
647 retdevs[i].device_entry.product_id = dev->libusb_device->descriptor.idProduct;
648 retdevs[i].device_entry.device_flags = 0x00000000U;
Linus Walleijfec4d562008-06-01 22:30:36 +0000649 // See if we can locate some additional vendor info and device flags
Linus Walleijf106aa62008-05-04 22:55:51 +0000650 for(j = 0; j < mtp_device_table_size; j++) {
651 if(dev->libusb_device->descriptor.idVendor == mtp_device_table[j].vendor_id &&
652 dev->libusb_device->descriptor.idProduct == mtp_device_table[j].product_id) {
653 device_known = 1;
654 retdevs[i].device_entry.vendor = mtp_device_table[j].vendor;
655 retdevs[i].device_entry.product = mtp_device_table[j].product;
656 retdevs[i].device_entry.device_flags = mtp_device_table[j].device_flags;
nicklas79daadbf22009-09-28 18:19:34 +0000657
Linus Walleijf106aa62008-05-04 22:55:51 +0000658 // This device is known to the developers
Linus Walleij49557482010-06-22 09:14:47 +0000659 LIBMTP_ERROR("Device %d (VID=%04x and PID=%04x) is a %s %s.\n",
Linus Walleijf106aa62008-05-04 22:55:51 +0000660 i,
661 dev->libusb_device->descriptor.idVendor,
662 dev->libusb_device->descriptor.idProduct,
663 mtp_device_table[j].vendor,
664 mtp_device_table[j].product);
Linus Walleijf106aa62008-05-04 22:55:51 +0000665 break;
666 }
667 }
668 if (!device_known) {
669 // This device is unknown to the developers
Linus Walleij49557482010-06-22 09:14:47 +0000670 LIBMTP_ERROR("Device %d (VID=%04x and PID=%04x) is UNKNOWN.\n",
Linus Walleijf106aa62008-05-04 22:55:51 +0000671 i,
672 dev->libusb_device->descriptor.idVendor,
673 dev->libusb_device->descriptor.idProduct);
nicklas79daadbf22009-09-28 18:19:34 +0000674 LIBMTP_ERROR("Please report this VID/PID and the device model to the "
Linus Walleijf106aa62008-05-04 22:55:51 +0000675 "libmtp development team\n");
Linus Walleijfec4d562008-06-01 22:30:36 +0000676 /*
677 * Trying to get iManufacturer or iProduct from the device at this
678 * point would require opening a device handle, that we don't want
679 * to do right now. (Takes time for no good enough reason.)
680 */
Linus Walleijf106aa62008-05-04 22:55:51 +0000681 }
682 // Save the location on the bus
Linus Walleij63fd1e62008-04-23 23:49:43 +0000683 retdevs[i].bus_location = dev->bus_location;
684 retdevs[i].devnum = dev->libusb_device->devnum;
685 i++;
686 dev = dev->next;
Linus Walleij49557482010-06-22 09:14:47 +0000687 }
Linus Walleij63fd1e62008-04-23 23:49:43 +0000688 *devices = retdevs;
689 *numdevs = i;
Linus Walleijb6b69132007-03-07 09:45:05 +0000690 free_mtpdevice_list(devlist);
Linus Walleija700d222008-05-28 23:06:14 +0000691 return LIBMTP_ERROR_NONE;
Linus Walleij1fd2d272006-05-08 09:22:01 +0000692}
693
Linus Walleijc6210fb2006-05-08 11:11:41 +0000694/**
695 * This routine just dumps out low-level
696 * USB information about the current device.
697 * @param ptp_usb the USB device to get information from.
698 */
699void dump_usbinfo(PTP_USB *ptp_usb)
700{
Linus Walleijc6210fb2006-05-08 11:11:41 +0000701 struct usb_device *dev;
702
703#ifdef LIBUSB_HAS_GET_DRIVER_NP
704 char devname[0x10];
Richard Lowe029eba2009-04-11 12:36:25 +0000705 int res;
Linus Walleij49557482010-06-22 09:14:47 +0000706
Linus Walleijc6210fb2006-05-08 11:11:41 +0000707 devname[0] = '\0';
Linus Walleijb0ab5482007-08-29 21:08:54 +0000708 res = usb_get_driver_np(ptp_usb->handle, (int) ptp_usb->interface, devname, sizeof(devname));
Linus Walleijc6210fb2006-05-08 11:11:41 +0000709 if (devname[0] != '\0') {
nicklas79daadbf22009-09-28 18:19:34 +0000710 LIBMTP_INFO(" Using kernel interface \"%s\"\n", devname);
Linus Walleijc6210fb2006-05-08 11:11:41 +0000711 }
712#endif
713 dev = usb_device(ptp_usb->handle);
nicklas79daadbf22009-09-28 18:19:34 +0000714 LIBMTP_INFO(" bcdUSB: %d\n", dev->descriptor.bcdUSB);
715 LIBMTP_INFO(" bDeviceClass: %d\n", dev->descriptor.bDeviceClass);
716 LIBMTP_INFO(" bDeviceSubClass: %d\n", dev->descriptor.bDeviceSubClass);
717 LIBMTP_INFO(" bDeviceProtocol: %d\n", dev->descriptor.bDeviceProtocol);
718 LIBMTP_INFO(" idVendor: %04x\n", dev->descriptor.idVendor);
719 LIBMTP_INFO(" idProduct: %04x\n", dev->descriptor.idProduct);
720 LIBMTP_INFO(" IN endpoint maxpacket: %d bytes\n", ptp_usb->inep_maxpacket);
721 LIBMTP_INFO(" OUT endpoint maxpacket: %d bytes\n", ptp_usb->outep_maxpacket);
722 LIBMTP_INFO(" Raw device info:\n");
723 LIBMTP_INFO(" Bus location: %d\n", ptp_usb->rawdevice.bus_location);
724 LIBMTP_INFO(" Device number: %d\n", ptp_usb->rawdevice.devnum);
725 LIBMTP_INFO(" Device entry info:\n");
726 LIBMTP_INFO(" Vendor: %s\n", ptp_usb->rawdevice.device_entry.vendor);
727 LIBMTP_INFO(" Vendor id: 0x%04x\n", ptp_usb->rawdevice.device_entry.vendor_id);
728 LIBMTP_INFO(" Product: %s\n", ptp_usb->rawdevice.device_entry.product);
729 LIBMTP_INFO(" Vendor id: 0x%04x\n", ptp_usb->rawdevice.device_entry.product_id);
730 LIBMTP_INFO(" Device flags: 0x%08x\n", ptp_usb->rawdevice.device_entry.device_flags);
Linus Walleij9521e2b2007-07-10 21:50:42 +0000731 (void) probe_device_descriptor(dev, stdout);
Linus Walleijc6210fb2006-05-08 11:11:41 +0000732}
733
Linus Walleija4e6bdc2008-05-23 22:31:53 +0000734/**
735 * Retrieve the apropriate playlist extension for this
736 * device. Rather hacky at the moment. This is probably
737 * desired by the managing software, but when creating
738 * lists on the device itself you notice certain preferences.
739 * @param ptp_usb the USB device to get suggestion for.
740 * @return the suggested playlist extension.
741 */
Richard Lowe029eba2009-04-11 12:36:25 +0000742const char *get_playlist_extension(PTP_USB *ptp_usb)
Linus Walleija4e6bdc2008-05-23 22:31:53 +0000743{
744 struct usb_device *dev;
745 static char creative_pl_extension[] = ".zpl";
746 static char default_pl_extension[] = ".pla";
747
748 dev = usb_device(ptp_usb->handle);
749 if (dev->descriptor.idVendor == 0x041e) {
750 return creative_pl_extension;
751 }
752 return default_pl_extension;
753}
754
Linus Walleijd1e14e02008-12-14 01:07:30 +0000755static void
756libusb_glue_debug (PTPParams *params, const char *format, ...)
Linus Walleij14735952010-04-25 04:56:49 +0000757{
Linus Walleija0323272007-01-07 12:21:30 +0000758 va_list args;
759
760 va_start (args, format);
761 if (params->debug_func!=NULL)
762 params->debug_func (params->data, format, args);
763 else
764 {
765 vfprintf (stderr, format, args);
766 fprintf (stderr,"\n");
767 fflush (stderr);
768 }
769 va_end (args);
Linus Walleij14735952010-04-25 04:56:49 +0000770}
Linus Walleija0323272007-01-07 12:21:30 +0000771
772static void
Linus Walleijd1e14e02008-12-14 01:07:30 +0000773libusb_glue_error (PTPParams *params, const char *format, ...)
Linus Walleij49557482010-06-22 09:14:47 +0000774{
Linus Walleija0323272007-01-07 12:21:30 +0000775 va_list args;
776
777 va_start (args, format);
778 if (params->error_func!=NULL)
779 params->error_func (params->data, format, args);
780 else
781 {
782 vfprintf (stderr, format, args);
783 fprintf (stderr,"\n");
784 fflush (stderr);
785 }
786 va_end (args);
787}
788
Linus Walleijc6210fb2006-05-08 11:11:41 +0000789
Linus Walleij99ed83c2007-01-02 18:46:02 +0000790/*
791 * ptp_read_func() and ptp_write_func() are
Linus Walleijc0a11432007-03-02 21:21:18 +0000792 * based on same functions usb.c in libgphoto2.
Linus Walleij99ed83c2007-01-02 18:46:02 +0000793 * Much reading packet logs and having fun with trials and errors
794 * reveals that WMP / Windows is probably using an algorithm like this
795 * for large transfers:
796 *
Linus Walleij49557482010-06-22 09:14:47 +0000797 * 1. Send the command (0x0c bytes) if headers are split, else, send
Linus Walleij99ed83c2007-01-02 18:46:02 +0000798 * command plus sizeof(endpoint) - 0x0c bytes.
799 * 2. Send first packet, max size to be sizeof(endpoint) but only when using
800 * split headers. Else goto 3.
801 * 3. REPEAT send 0x10000 byte chunks UNTIL remaining bytes < 0x10000
802 * We call 0x10000 CONTEXT_BLOCK_SIZE.
803 * 4. Send remaining bytes MOD sizeof(endpoint)
804 * 5. Send remaining bytes. If this happens to be exactly sizeof(endpoint)
805 * then also send a zero-length package.
Linus Walleij049f50c2007-03-03 20:30:43 +0000806 *
807 * Further there is some special quirks to handle zero reads from the
808 * device, since some devices can't do them at all due to shortcomings
809 * of the USB slave controller in the device.
Linus Walleij99ed83c2007-01-02 18:46:02 +0000810 */
Richard Low4fd59d62007-03-03 16:34:37 +0000811#define CONTEXT_BLOCK_SIZE_1 0x3e00
812#define CONTEXT_BLOCK_SIZE_2 0x200
813#define CONTEXT_BLOCK_SIZE CONTEXT_BLOCK_SIZE_1+CONTEXT_BLOCK_SIZE_2
Linus Walleijb02a0662006-04-25 08:05:09 +0000814static short
Linus Walleij784ac3f2006-12-29 10:36:51 +0000815ptp_read_func (
816 unsigned long size, PTPDataHandler *handler,void *data,
Richard Low4df32f82007-01-11 20:04:39 +0000817 unsigned long *readbytes,
818 int readzero
Linus Walleij784ac3f2006-12-29 10:36:51 +0000819) {
Linus Walleijb02a0662006-04-25 08:05:09 +0000820 PTP_USB *ptp_usb = (PTP_USB *)data;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000821 unsigned long toread = 0;
Linus Walleijd6a49972006-05-02 08:24:54 +0000822 int result = 0;
Linus Walleij99ed83c2007-01-02 18:46:02 +0000823 unsigned long curread = 0;
824 unsigned long written;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000825 unsigned char *bytes;
Linus Walleij049f50c2007-03-03 20:30:43 +0000826 int expect_terminator_byte = 0;
Linus Walleij99ed83c2007-01-02 18:46:02 +0000827
828 // This is the largest block we'll need to read in.
Linus Walleij784ac3f2006-12-29 10:36:51 +0000829 bytes = malloc(CONTEXT_BLOCK_SIZE);
Linus Walleijd6a49972006-05-02 08:24:54 +0000830 while (curread < size) {
nicklas79daadbf22009-09-28 18:19:34 +0000831
832 LIBMTP_USB_DEBUG("Remaining size to read: 0x%04lx bytes\n", size - curread);
833
Richard Low4fd59d62007-03-03 16:34:37 +0000834 // check equal to condition here
835 if (size - curread < CONTEXT_BLOCK_SIZE)
836 {
837 // this is the last packet
838 toread = size - curread;
839 // this is equivalent to zero read for these devices
Linus Walleijfec4d562008-06-01 22:30:36 +0000840 if (readzero && FLAG_NO_ZERO_READS(ptp_usb) && toread % 64 == 0) {
Richard Low4fd59d62007-03-03 16:34:37 +0000841 toread += 1;
Linus Walleij049f50c2007-03-03 20:30:43 +0000842 expect_terminator_byte = 1;
Richard Low4fd59d62007-03-03 16:34:37 +0000843 }
Linus Walleijc49c6372007-01-09 00:18:51 +0000844 }
Richard Low4fd59d62007-03-03 16:34:37 +0000845 else if (curread == 0)
846 // we are first packet, but not last packet
847 toread = CONTEXT_BLOCK_SIZE_1;
848 else if (toread == CONTEXT_BLOCK_SIZE_1)
849 toread = CONTEXT_BLOCK_SIZE_2;
850 else if (toread == CONTEXT_BLOCK_SIZE_2)
851 toread = CONTEXT_BLOCK_SIZE_1;
852 else
nicklas79daadbf22009-09-28 18:19:34 +0000853 LIBMTP_INFO("unexpected toread size 0x%04x, 0x%04x remaining bytes\n",
Linus Walleij4cec9872007-03-03 21:00:53 +0000854 (unsigned int) toread, (unsigned int) (size-curread));
Linus Walleij784ac3f2006-12-29 10:36:51 +0000855
nicklas79daadbf22009-09-28 18:19:34 +0000856 LIBMTP_USB_DEBUG("Reading in 0x%04lx bytes\n", toread);
857
Linus Walleije04a1b92011-03-09 18:00:24 +0100858 result = USB_BULK_READ(ptp_usb->handle,
859 ptp_usb->inep,
860 (char*) bytes,
861 toread,
862 ptp_usb->timeout);
nicklas79daadbf22009-09-28 18:19:34 +0000863
864 LIBMTP_USB_DEBUG("Result of read: 0x%04x\n", result);
Linus Walleij49557482010-06-22 09:14:47 +0000865
Linus Walleijc49c6372007-01-09 00:18:51 +0000866 if (result < 0) {
Richard Low43fdb882006-09-06 16:19:05 +0000867 return PTP_ERROR_IO;
Linus Walleijc49c6372007-01-09 00:18:51 +0000868 }
nicklas79daadbf22009-09-28 18:19:34 +0000869
870 LIBMTP_USB_DEBUG("<==USB IN\n");
Richard Low4df32f82007-01-11 20:04:39 +0000871 if (result == 0)
nicklas79daadbf22009-09-28 18:19:34 +0000872 LIBMTP_USB_DEBUG("Zero Read\n");
Richard Low4df32f82007-01-11 20:04:39 +0000873 else
nicklas79daadbf22009-09-28 18:19:34 +0000874 LIBMTP_USB_DATA(bytes, result, 16);
Linus Walleij49557482010-06-22 09:14:47 +0000875
Richard Low4fd59d62007-03-03 16:34:37 +0000876 // want to discard extra byte
Linus Walleij049f50c2007-03-03 20:30:43 +0000877 if (expect_terminator_byte && result == toread)
Richard Lowbf2675c2007-03-18 18:20:26 +0000878 {
nicklas79daadbf22009-09-28 18:19:34 +0000879 LIBMTP_USB_DEBUG("<==USB IN\nDiscarding extra byte\n");
880
Richard Low4fd59d62007-03-03 16:34:37 +0000881 result--;
Richard Lowbf2675c2007-03-18 18:20:26 +0000882 }
Linus Walleij49557482010-06-22 09:14:47 +0000883
Linus Walleijd866d242009-08-23 21:50:39 +0000884 int putfunc_ret = handler->putfunc(NULL, handler->priv, result, bytes, &written);
Richard Lowd24f9232009-05-03 10:22:08 +0000885 if (putfunc_ret != PTP_RC_OK)
886 return putfunc_ret;
Linus Walleij49557482010-06-22 09:14:47 +0000887
Linus Walleijc49c6372007-01-09 00:18:51 +0000888 ptp_usb->current_transfer_complete += result;
Linus Walleijd6a49972006-05-02 08:24:54 +0000889 curread += result;
Linus Walleijc49c6372007-01-09 00:18:51 +0000890
891 // Increase counters, call callback
892 if (ptp_usb->callback_active) {
893 if (ptp_usb->current_transfer_complete >= ptp_usb->current_transfer_total) {
894 // send last update and disable callback.
895 ptp_usb->current_transfer_complete = ptp_usb->current_transfer_total;
896 ptp_usb->callback_active = 0;
897 }
898 if (ptp_usb->current_transfer_callback != NULL) {
Linus Walleijbd3bf9e2007-09-14 19:31:54 +0000899 int ret;
900 ret = ptp_usb->current_transfer_callback(ptp_usb->current_transfer_complete,
901 ptp_usb->current_transfer_total,
902 ptp_usb->current_transfer_callback_data);
903 if (ret != 0) {
904 return PTP_ERROR_CANCEL;
905 }
Linus Walleijc49c6372007-01-09 00:18:51 +0000906 }
Linus Walleij49557482010-06-22 09:14:47 +0000907 }
Linus Walleijc49c6372007-01-09 00:18:51 +0000908
Linus Walleijd6a49972006-05-02 08:24:54 +0000909 if (result < toread) /* short reads are common */
910 break;
911 }
Linus Walleij784ac3f2006-12-29 10:36:51 +0000912 if (readbytes) *readbytes = curread;
913 free (bytes);
Linus Walleij49557482010-06-22 09:14:47 +0000914
Richard Low4df32f82007-01-11 20:04:39 +0000915 // there might be a zero packet waiting for us...
Linus Walleij49557482010-06-22 09:14:47 +0000916 if (readzero &&
917 !FLAG_NO_ZERO_READS(ptp_usb) &&
Linus Walleij049f50c2007-03-03 20:30:43 +0000918 curread % ptp_usb->outep_maxpacket == 0) {
Richard Low4df32f82007-01-11 20:04:39 +0000919 char temp;
920 int zeroresult = 0;
Linus Walleij049f50c2007-03-03 20:30:43 +0000921
nicklas79daadbf22009-09-28 18:19:34 +0000922 LIBMTP_USB_DEBUG("<==USB IN\n");
923 LIBMTP_USB_DEBUG("Zero Read\n");
924
Linus Walleije04a1b92011-03-09 18:00:24 +0100925 zeroresult = USB_BULK_READ(ptp_usb->handle,
926 ptp_usb->inep,
927 &temp,
928 0,
929 ptp_usb->timeout);
Richard Low4df32f82007-01-11 20:04:39 +0000930 if (zeroresult != 0)
nicklas79daadbf22009-09-28 18:19:34 +0000931 LIBMTP_INFO("LIBMTP panic: unable to read in zero packet, response 0x%04x", zeroresult);
Richard Low4df32f82007-01-11 20:04:39 +0000932 }
Linus Walleij49557482010-06-22 09:14:47 +0000933
Richard Low4d9165f2008-09-23 20:13:17 +0000934 return PTP_RC_OK;
Linus Walleijeb8c6fe2006-02-03 09:46:22 +0000935}
936
Linus Walleijb02a0662006-04-25 08:05:09 +0000937static short
Linus Walleij784ac3f2006-12-29 10:36:51 +0000938ptp_write_func (
939 unsigned long size,
940 PTPDataHandler *handler,
941 void *data,
942 unsigned long *written
943) {
Linus Walleijb02a0662006-04-25 08:05:09 +0000944 PTP_USB *ptp_usb = (PTP_USB *)data;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000945 unsigned long towrite = 0;
Linus Walleijd6a49972006-05-02 08:24:54 +0000946 int result = 0;
Linus Walleij784ac3f2006-12-29 10:36:51 +0000947 unsigned long curwrite = 0;
948 unsigned char *bytes;
Linus Walleij99ed83c2007-01-02 18:46:02 +0000949
Linus Walleij49557482010-06-22 09:14:47 +0000950 // This is the largest block we'll need to read in.
Linus Walleij784ac3f2006-12-29 10:36:51 +0000951 bytes = malloc(CONTEXT_BLOCK_SIZE);
Linus Walleijc49c6372007-01-09 00:18:51 +0000952 if (!bytes) {
953 return PTP_ERROR_IO;
954 }
Linus Walleijd6a49972006-05-02 08:24:54 +0000955 while (curwrite < size) {
Linus Walleijb84b3852009-03-04 15:11:08 +0000956 unsigned long usbwritten = 0;
Linus Walleijd6a49972006-05-02 08:24:54 +0000957 towrite = size-curwrite;
Linus Walleijc49c6372007-01-09 00:18:51 +0000958 if (towrite > CONTEXT_BLOCK_SIZE) {
Richard Low43fdb882006-09-06 16:19:05 +0000959 towrite = CONTEXT_BLOCK_SIZE;
Linus Walleijc49c6372007-01-09 00:18:51 +0000960 } else {
961 // This magic makes packets the same size that WMP send them.
962 if (towrite > ptp_usb->outep_maxpacket && towrite % ptp_usb->outep_maxpacket != 0) {
Richard Low021421e2007-01-01 18:08:57 +0000963 towrite -= towrite % ptp_usb->outep_maxpacket;
Linus Walleijc49c6372007-01-09 00:18:51 +0000964 }
965 }
Linus Walleijd866d242009-08-23 21:50:39 +0000966 int getfunc_ret = handler->getfunc(NULL, handler->priv,towrite,bytes,&towrite);
Richard Lowd24f9232009-05-03 10:22:08 +0000967 if (getfunc_ret != PTP_RC_OK)
968 return getfunc_ret;
Linus Walleijb84b3852009-03-04 15:11:08 +0000969 while (usbwritten < towrite) {
Linus Walleije04a1b92011-03-09 18:00:24 +0100970 result = USB_BULK_WRITE(ptp_usb->handle,
971 ptp_usb->outep,
972 ((char*) bytes+usbwritten),
973 towrite-usbwritten,
974 ptp_usb->timeout);
nicklas79daadbf22009-09-28 18:19:34 +0000975
976 LIBMTP_USB_DEBUG("USB OUT==>\n");
977 LIBMTP_USB_DATA(bytes+usbwritten, result, 16);
978
Linus Walleijb84b3852009-03-04 15:11:08 +0000979 if (result < 0) {
980 return PTP_ERROR_IO;
981 }
982 // check for result == 0 perhaps too.
983 // Increase counters
984 ptp_usb->current_transfer_complete += result;
985 curwrite += result;
986 usbwritten += result;
Linus Walleijc49c6372007-01-09 00:18:51 +0000987 }
Linus Walleijc49c6372007-01-09 00:18:51 +0000988 // call callback
989 if (ptp_usb->callback_active) {
990 if (ptp_usb->current_transfer_complete >= ptp_usb->current_transfer_total) {
991 // send last update and disable callback.
992 ptp_usb->current_transfer_complete = ptp_usb->current_transfer_total;
993 ptp_usb->callback_active = 0;
994 }
995 if (ptp_usb->current_transfer_callback != NULL) {
Linus Walleijbd3bf9e2007-09-14 19:31:54 +0000996 int ret;
997 ret = ptp_usb->current_transfer_callback(ptp_usb->current_transfer_complete,
998 ptp_usb->current_transfer_total,
999 ptp_usb->current_transfer_callback_data);
1000 if (ret != 0) {
1001 return PTP_ERROR_CANCEL;
1002 }
Linus Walleijc49c6372007-01-09 00:18:51 +00001003 }
1004 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001005 if (result < towrite) /* short writes happen */
1006 break;
1007 }
Linus Walleij784ac3f2006-12-29 10:36:51 +00001008 free (bytes);
Linus Walleijc49c6372007-01-09 00:18:51 +00001009 if (written) {
1010 *written = curwrite;
Linus Walleij80d134a2006-08-22 21:41:37 +00001011 }
Linus Walleijc49c6372007-01-09 00:18:51 +00001012
Richard Lowef05b892007-01-03 21:18:56 +00001013 // If this is the last transfer send a zero write if required
1014 if (ptp_usb->current_transfer_complete >= ptp_usb->current_transfer_total) {
Richard Low68f45882007-01-03 10:08:31 +00001015 if ((towrite % ptp_usb->outep_maxpacket) == 0) {
nicklas79daadbf22009-09-28 18:19:34 +00001016
1017 LIBMTP_USB_DEBUG("USB OUT==>\n");
1018 LIBMTP_USB_DEBUG("Zero Write\n");
1019
Linus Walleije04a1b92011-03-09 18:00:24 +01001020 result=USB_BULK_WRITE(ptp_usb->handle,
1021 ptp_usb->outep,
1022 (char *) "x",
1023 0,
1024 ptp_usb->timeout);
Linus Walleij7f0c72e2006-09-06 13:01:58 +00001025 }
Linus Walleijd214b9b2006-08-26 22:08:37 +00001026 }
Linus Walleij49557482010-06-22 09:14:47 +00001027
Linus Walleijd6a49972006-05-02 08:24:54 +00001028 if (result < 0)
1029 return PTP_ERROR_IO;
1030 return PTP_RC_OK;
Linus Walleijb02a0662006-04-25 08:05:09 +00001031}
1032
Linus Walleija0323272007-01-07 12:21:30 +00001033/* memory data get/put handler */
1034typedef struct {
1035 unsigned char *data;
1036 unsigned long size, curoff;
1037} PTPMemHandlerPrivate;
Linus Walleij784ac3f2006-12-29 10:36:51 +00001038
Linus Walleija0323272007-01-07 12:21:30 +00001039static uint16_t
1040memory_getfunc(PTPParams* params, void* private,
1041 unsigned long wantlen, unsigned char *data,
1042 unsigned long *gotlen
1043) {
1044 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)private;
1045 unsigned long tocopy = wantlen;
1046
1047 if (priv->curoff + tocopy > priv->size)
1048 tocopy = priv->size - priv->curoff;
1049 memcpy (data, priv->data + priv->curoff, tocopy);
1050 priv->curoff += tocopy;
1051 *gotlen = tocopy;
1052 return PTP_RC_OK;
Linus Walleijb02a0662006-04-25 08:05:09 +00001053}
1054
Linus Walleija0323272007-01-07 12:21:30 +00001055static uint16_t
1056memory_putfunc(PTPParams* params, void* private,
1057 unsigned long sendlen, unsigned char *data,
1058 unsigned long *putlen
1059) {
1060 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)private;
1061
1062 if (priv->curoff + sendlen > priv->size) {
1063 priv->data = realloc (priv->data, priv->curoff+sendlen);
1064 priv->size = priv->curoff + sendlen;
1065 }
1066 memcpy (priv->data + priv->curoff, data, sendlen);
1067 priv->curoff += sendlen;
1068 *putlen = sendlen;
1069 return PTP_RC_OK;
1070}
1071
1072/* init private struct for receiving data. */
1073static uint16_t
1074ptp_init_recv_memory_handler(PTPDataHandler *handler) {
1075 PTPMemHandlerPrivate* priv;
1076 priv = malloc (sizeof(PTPMemHandlerPrivate));
Linus Walleijd866d242009-08-23 21:50:39 +00001077 handler->priv = priv;
Linus Walleija0323272007-01-07 12:21:30 +00001078 handler->getfunc = memory_getfunc;
1079 handler->putfunc = memory_putfunc;
1080 priv->data = NULL;
1081 priv->size = 0;
1082 priv->curoff = 0;
1083 return PTP_RC_OK;
1084}
1085
1086/* init private struct and put data in for sending data.
1087 * data is still owned by caller.
1088 */
1089static uint16_t
1090ptp_init_send_memory_handler(PTPDataHandler *handler,
1091 unsigned char *data, unsigned long len
1092) {
1093 PTPMemHandlerPrivate* priv;
1094 priv = malloc (sizeof(PTPMemHandlerPrivate));
1095 if (!priv)
1096 return PTP_RC_GeneralError;
Linus Walleijd866d242009-08-23 21:50:39 +00001097 handler->priv = priv;
Linus Walleija0323272007-01-07 12:21:30 +00001098 handler->getfunc = memory_getfunc;
1099 handler->putfunc = memory_putfunc;
1100 priv->data = data;
1101 priv->size = len;
1102 priv->curoff = 0;
1103 return PTP_RC_OK;
1104}
1105
1106/* free private struct + data */
1107static uint16_t
1108ptp_exit_send_memory_handler (PTPDataHandler *handler) {
Linus Walleijd866d242009-08-23 21:50:39 +00001109 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)handler->priv;
Linus Walleija0323272007-01-07 12:21:30 +00001110 /* data is owned by caller */
1111 free (priv);
1112 return PTP_RC_OK;
1113}
1114
1115/* hand over our internal data to caller */
1116static uint16_t
1117ptp_exit_recv_memory_handler (PTPDataHandler *handler,
1118 unsigned char **data, unsigned long *size
1119) {
Linus Walleijd866d242009-08-23 21:50:39 +00001120 PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)handler->priv;
Linus Walleija0323272007-01-07 12:21:30 +00001121 *data = priv->data;
1122 *size = priv->size;
1123 free (priv);
1124 return PTP_RC_OK;
1125}
1126
1127/* send / receive functions */
1128
1129uint16_t
1130ptp_usb_sendreq (PTPParams* params, PTPContainer* req)
1131{
1132 uint16_t ret;
1133 PTPUSBBulkContainer usbreq;
1134 PTPDataHandler memhandler;
Linus Walleij5ba40402008-05-23 21:36:54 +00001135 unsigned long written = 0;
1136 unsigned long towrite;
nicklas79daadbf22009-09-28 18:19:34 +00001137
Linus Walleijb8f78c72008-03-02 20:22:56 +00001138 char txt[256];
Linus Walleija0323272007-01-07 12:21:30 +00001139
Linus Walleijb8f78c72008-03-02 20:22:56 +00001140 (void) ptp_render_opcode (params, req->Code, sizeof(txt), txt);
nicklas79daadbf22009-09-28 18:19:34 +00001141 LIBMTP_USB_DEBUG("REQUEST: 0x%04x, %s\n", req->Code, txt);
1142
Linus Walleija0323272007-01-07 12:21:30 +00001143 /* build appropriate USB container */
1144 usbreq.length=htod32(PTP_USB_BULK_REQ_LEN-
1145 (sizeof(uint32_t)*(5-req->Nparam)));
1146 usbreq.type=htod16(PTP_USB_CONTAINER_COMMAND);
1147 usbreq.code=htod16(req->Code);
1148 usbreq.trans_id=htod32(req->Transaction_ID);
1149 usbreq.payload.params.param1=htod32(req->Param1);
1150 usbreq.payload.params.param2=htod32(req->Param2);
1151 usbreq.payload.params.param3=htod32(req->Param3);
1152 usbreq.payload.params.param4=htod32(req->Param4);
1153 usbreq.payload.params.param5=htod32(req->Param5);
1154 /* send it to responder */
1155 towrite = PTP_USB_BULK_REQ_LEN-(sizeof(uint32_t)*(5-req->Nparam));
1156 ptp_init_send_memory_handler (&memhandler, (unsigned char*)&usbreq, towrite);
1157 ret=ptp_write_func(
1158 towrite,
1159 &memhandler,
1160 params->data,
1161 &written
1162 );
1163 ptp_exit_send_memory_handler (&memhandler);
Linus Walleijff01cb12007-09-16 19:49:48 +00001164 if (ret!=PTP_RC_OK && ret!=PTP_ERROR_CANCEL) {
Linus Walleija0323272007-01-07 12:21:30 +00001165 ret = PTP_ERROR_IO;
Linus Walleija0323272007-01-07 12:21:30 +00001166 }
Linus Walleij724f0102007-09-17 20:10:12 +00001167 if (written != towrite && ret != PTP_ERROR_CANCEL && ret != PTP_ERROR_IO) {
Linus Walleij49557482010-06-22 09:14:47 +00001168 libusb_glue_error (params,
Linus Walleija0323272007-01-07 12:21:30 +00001169 "PTP: request code 0x%04x sending req wrote only %ld bytes instead of %d",
Richard Lowd284f072007-02-17 08:52:41 +00001170 req->Code, written, towrite
Linus Walleija0323272007-01-07 12:21:30 +00001171 );
1172 ret = PTP_ERROR_IO;
1173 }
1174 return ret;
1175}
1176
1177uint16_t
1178ptp_usb_senddata (PTPParams* params, PTPContainer* ptp,
1179 unsigned long size, PTPDataHandler *handler
1180) {
1181 uint16_t ret;
1182 int wlen, datawlen;
1183 unsigned long written;
1184 PTPUSBBulkContainer usbdata;
1185 uint32_t bytes_left_to_transfer;
1186 PTPDataHandler memhandler;
1187
nicklas79daadbf22009-09-28 18:19:34 +00001188
1189 LIBMTP_USB_DEBUG("SEND DATA PHASE\n");
1190
Linus Walleija0323272007-01-07 12:21:30 +00001191 /* build appropriate USB container */
1192 usbdata.length = htod32(PTP_USB_BULK_HDR_LEN+size);
1193 usbdata.type = htod16(PTP_USB_CONTAINER_DATA);
1194 usbdata.code = htod16(ptp->Code);
1195 usbdata.trans_id= htod32(ptp->Transaction_ID);
Linus Walleij14735952010-04-25 04:56:49 +00001196
Linus Walleija0323272007-01-07 12:21:30 +00001197 ((PTP_USB*)params->data)->current_transfer_complete = 0;
Richard Low4eb01122007-02-24 10:11:32 +00001198 ((PTP_USB*)params->data)->current_transfer_total = size+PTP_USB_BULK_HDR_LEN;
Linus Walleija0323272007-01-07 12:21:30 +00001199
1200 if (params->split_header_data) {
1201 datawlen = 0;
1202 wlen = PTP_USB_BULK_HDR_LEN;
1203 } else {
1204 unsigned long gotlen;
1205 /* For all camera devices. */
Richard Low02724f62007-02-17 09:47:26 +00001206 datawlen = (size<PTP_USB_BULK_PAYLOAD_LEN_WRITE)?size:PTP_USB_BULK_PAYLOAD_LEN_WRITE;
Linus Walleija0323272007-01-07 12:21:30 +00001207 wlen = PTP_USB_BULK_HDR_LEN + datawlen;
Linus Walleij14735952010-04-25 04:56:49 +00001208
Linus Walleijd866d242009-08-23 21:50:39 +00001209 ret = handler->getfunc(params, handler->priv, datawlen, usbdata.payload.data, &gotlen);
Linus Walleija0323272007-01-07 12:21:30 +00001210 if (ret != PTP_RC_OK)
1211 return ret;
1212 if (gotlen != datawlen)
1213 return PTP_RC_GeneralError;
1214 }
1215 ptp_init_send_memory_handler (&memhandler, (unsigned char *)&usbdata, wlen);
1216 /* send first part of data */
1217 ret = ptp_write_func(wlen, &memhandler, params->data, &written);
1218 ptp_exit_send_memory_handler (&memhandler);
1219 if (ret!=PTP_RC_OK) {
Linus Walleija0323272007-01-07 12:21:30 +00001220 return ret;
1221 }
1222 if (size <= datawlen) return ret;
1223 /* if everything OK send the rest */
1224 bytes_left_to_transfer = size-datawlen;
1225 ret = PTP_RC_OK;
1226 while(bytes_left_to_transfer > 0) {
1227 ret = ptp_write_func (bytes_left_to_transfer, handler, params->data, &written);
1228 if (ret != PTP_RC_OK)
1229 break;
1230 if (written == 0) {
1231 ret = PTP_ERROR_IO;
1232 break;
1233 }
1234 bytes_left_to_transfer -= written;
1235 }
Linus Walleijff01cb12007-09-16 19:49:48 +00001236 if (ret!=PTP_RC_OK && ret!=PTP_ERROR_CANCEL)
Linus Walleija0323272007-01-07 12:21:30 +00001237 ret = PTP_ERROR_IO;
1238 return ret;
1239}
1240
1241static uint16_t ptp_usb_getpacket(PTPParams *params,
1242 PTPUSBBulkContainer *packet, unsigned long *rlen)
1243{
1244 PTPDataHandler memhandler;
1245 uint16_t ret;
1246 unsigned char *x = NULL;
1247
1248 /* read the header and potentially the first data */
1249 if (params->response_packet_size > 0) {
1250 /* If there is a buffered packet, just use it. */
1251 memcpy(packet, params->response_packet, params->response_packet_size);
1252 *rlen = params->response_packet_size;
1253 free(params->response_packet);
1254 params->response_packet = NULL;
1255 params->response_packet_size = 0;
1256 /* Here this signifies a "virtual read" */
1257 return PTP_RC_OK;
1258 }
1259 ptp_init_recv_memory_handler (&memhandler);
Richard Low02724f62007-02-17 09:47:26 +00001260 ret = ptp_read_func(PTP_USB_BULK_HS_MAX_PACKET_LEN_READ, &memhandler, params->data, rlen, 0);
Linus Walleija0323272007-01-07 12:21:30 +00001261 ptp_exit_recv_memory_handler (&memhandler, &x, rlen);
1262 if (x) {
1263 memcpy (packet, x, *rlen);
1264 free (x);
1265 }
1266 return ret;
1267}
1268
1269uint16_t
1270ptp_usb_getdata (PTPParams* params, PTPContainer* ptp, PTPDataHandler *handler)
1271{
1272 uint16_t ret;
1273 PTPUSBBulkContainer usbdata;
Linus Walleija0323272007-01-07 12:21:30 +00001274 unsigned long written;
Linus Walleij8e3af002007-09-28 19:21:54 +00001275 PTP_USB *ptp_usb = (PTP_USB *) params->data;
Linus Walleij14735952010-04-25 04:56:49 +00001276 int putfunc_ret;
nicklas79daadbf22009-09-28 18:19:34 +00001277
1278 LIBMTP_USB_DEBUG("GET DATA PHASE\n");
1279
Linus Walleija0323272007-01-07 12:21:30 +00001280 memset(&usbdata,0,sizeof(usbdata));
1281 do {
1282 unsigned long len, rlen;
1283
1284 ret = ptp_usb_getpacket(params, &usbdata, &rlen);
1285 if (ret!=PTP_RC_OK) {
1286 ret = PTP_ERROR_IO;
1287 break;
Linus Walleij8e3af002007-09-28 19:21:54 +00001288 }
Linus Walleija0323272007-01-07 12:21:30 +00001289 if (dtoh16(usbdata.type)!=PTP_USB_CONTAINER_DATA) {
1290 ret = PTP_ERROR_DATA_EXPECTED;
1291 break;
Linus Walleij8e3af002007-09-28 19:21:54 +00001292 }
Linus Walleija0323272007-01-07 12:21:30 +00001293 if (dtoh16(usbdata.code)!=ptp->Code) {
Linus Walleijfec4d562008-06-01 22:30:36 +00001294 if (FLAG_IGNORE_HEADER_ERRORS(ptp_usb)) {
Linus Walleijd1e14e02008-12-14 01:07:30 +00001295 libusb_glue_debug (params, "ptp2/ptp_usb_getdata: detected a broken "
Linus Walleij913a3062007-09-28 21:42:39 +00001296 "PTP header, code field insane, expect problems! (But continuing)");
Linus Walleij06542122007-10-14 22:13:48 +00001297 // Repair the header, so it won't wreak more havoc, don't just ignore it.
1298 // Typically these two fields will be broken.
1299 usbdata.code = htod16(ptp->Code);
1300 usbdata.trans_id = htod32(ptp->Transaction_ID);
Linus Walleij94cd9722007-09-28 21:29:53 +00001301 ret = PTP_RC_OK;
Linus Walleij8e3af002007-09-28 19:21:54 +00001302 } else {
Linus Walleij913a3062007-09-28 21:42:39 +00001303 ret = dtoh16(usbdata.code);
1304 // This filters entirely insane garbage return codes, but still
1305 // makes it possible to return error codes in the code field when
Linus Walleij49557482010-06-22 09:14:47 +00001306 // getting data. It appears Windows ignores the contents of this
Linus Walleij913a3062007-09-28 21:42:39 +00001307 // field entirely.
1308 if (ret < PTP_RC_Undefined || ret > PTP_RC_SpecificationOfDestinationUnsupported) {
Linus Walleijd1e14e02008-12-14 01:07:30 +00001309 libusb_glue_debug (params, "ptp2/ptp_usb_getdata: detected a broken "
Linus Walleij913a3062007-09-28 21:42:39 +00001310 "PTP header, code field insane.");
1311 ret = PTP_ERROR_IO;
1312 }
Linus Walleij8e3af002007-09-28 19:21:54 +00001313 break;
1314 }
Linus Walleija0323272007-01-07 12:21:30 +00001315 }
1316 if (usbdata.length == 0xffffffffU) {
Linus Walleij14735952010-04-25 04:56:49 +00001317 /* Copy first part of data to 'data' */
1318 putfunc_ret =
1319 handler->putfunc(
1320 params, handler->priv, rlen - PTP_USB_BULK_HDR_LEN, usbdata.payload.data,
1321 &written
1322 );
1323 if (putfunc_ret != PTP_RC_OK)
1324 return putfunc_ret;
Linus Walleija0323272007-01-07 12:21:30 +00001325
Linus Walleij14735952010-04-25 04:56:49 +00001326 /* stuff data directly to passed data handler */
1327 while (1) {
1328 unsigned long readdata;
1329 uint16_t xret;
1330
1331 xret = ptp_read_func(
1332 PTP_USB_BULK_HS_MAX_PACKET_LEN_READ,
1333 handler,
1334 params->data,
1335 &readdata,
1336 0
1337 );
1338 if (xret != PTP_RC_OK)
1339 return xret;
1340 if (readdata < PTP_USB_BULK_HS_MAX_PACKET_LEN_READ)
1341 break;
1342 }
1343 return PTP_RC_OK;
Linus Walleija0323272007-01-07 12:21:30 +00001344 }
1345 if (rlen > dtoh32(usbdata.length)) {
1346 /*
1347 * Buffer the surplus response packet if it is >=
1348 * PTP_USB_BULK_HDR_LEN
1349 * (i.e. it is probably an entire package)
1350 * else discard it as erroneous surplus data.
1351 * This will even work if more than 2 packets appear
1352 * in the same transaction, they will just be handled
1353 * iteratively.
1354 *
1355 * Marcus observed stray bytes on iRiver devices;
1356 * these are still discarded.
1357 */
1358 unsigned int packlen = dtoh32(usbdata.length);
1359 unsigned int surplen = rlen - packlen;
1360
1361 if (surplen >= PTP_USB_BULK_HDR_LEN) {
1362 params->response_packet = malloc(surplen);
1363 memcpy(params->response_packet,
1364 (uint8_t *) &usbdata + packlen, surplen);
1365 params->response_packet_size = surplen;
tedbullock36f2afb2007-03-03 22:16:44 +00001366 /* Ignore reading one extra byte if device flags have been set */
Linus Walleijfec4d562008-06-01 22:30:36 +00001367 } else if(!FLAG_NO_ZERO_READS(ptp_usb) &&
1368 (rlen - dtoh32(usbdata.length) == 1)) {
Linus Walleijd1e14e02008-12-14 01:07:30 +00001369 libusb_glue_debug (params, "ptp2/ptp_usb_getdata: read %d bytes "
Linus Walleij49557482010-06-22 09:14:47 +00001370 "too much, expect problems!",
Linus Walleij8e3af002007-09-28 19:21:54 +00001371 rlen - dtoh32(usbdata.length));
Linus Walleija0323272007-01-07 12:21:30 +00001372 }
1373 rlen = packlen;
1374 }
1375
1376 /* For most PTP devices rlen is 512 == sizeof(usbdata)
1377 * here. For MTP devices splitting header and data it might
1378 * be 12.
1379 */
1380 /* Evaluate full data length. */
1381 len=dtoh32(usbdata.length)-PTP_USB_BULK_HDR_LEN;
1382
1383 /* autodetect split header/data MTP devices */
1384 if (dtoh32(usbdata.length) > 12 && (rlen==12))
1385 params->split_header_data = 1;
1386
Linus Walleija0323272007-01-07 12:21:30 +00001387 /* Copy first part of data to 'data' */
Linus Walleij14735952010-04-25 04:56:49 +00001388 putfunc_ret =
1389 handler->putfunc(
1390 params, handler->priv, rlen - PTP_USB_BULK_HDR_LEN,
1391 usbdata.payload.data,
1392 &written
1393 );
1394 if (putfunc_ret != PTP_RC_OK)
1395 return putfunc_ret;
Linus Walleijd866d242009-08-23 21:50:39 +00001396
1397 if (FLAG_NO_ZERO_READS(ptp_usb) &&
Linus Walleij049f50c2007-03-03 20:30:43 +00001398 len+PTP_USB_BULK_HDR_LEN == PTP_USB_BULK_HS_MAX_PACKET_LEN_READ) {
nicklas79daadbf22009-09-28 18:19:34 +00001399
1400 LIBMTP_USB_DEBUG("Reading in extra terminating byte\n");
1401
Linus Walleij049f50c2007-03-03 20:30:43 +00001402 // need to read in extra byte and discard it
1403 int result = 0;
1404 char byte = 0;
Linus Walleije04a1b92011-03-09 18:00:24 +01001405 result = USB_BULK_READ(ptp_usb->handle,
1406 ptp_usb->inep,
1407 &byte,
1408 1,
1409 ptp_usb->timeout);
Linus Walleijd866d242009-08-23 21:50:39 +00001410
Linus Walleij049f50c2007-03-03 20:30:43 +00001411 if (result != 1)
nicklas79daadbf22009-09-28 18:19:34 +00001412 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 +00001413 } 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 +00001414 int zeroresult = 0;
1415 char zerobyte = 0;
1416
nicklas79daadbf22009-09-28 18:19:34 +00001417
1418 LIBMTP_INFO("Reading in zero packet after header\n");
1419
Linus Walleije04a1b92011-03-09 18:00:24 +01001420 zeroresult = USB_BULK_READ(ptp_usb->handle,
1421 ptp_usb->inep,
1422 &zerobyte,
1423 0,
1424 ptp_usb->timeout);
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001425
Linus Walleij94cd9722007-09-28 21:29:53 +00001426 if (zeroresult != 0)
nicklas79daadbf22009-09-28 18:19:34 +00001427 LIBMTP_INFO("LIBMTP panic: unable to read in zero packet, response 0x%04x", zeroresult);
Linus Walleij94cd9722007-09-28 21:29:53 +00001428 }
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001429
Linus Walleija0323272007-01-07 12:21:30 +00001430 /* Is that all of data? */
Linus Walleij94cd9722007-09-28 21:29:53 +00001431 if (len+PTP_USB_BULK_HDR_LEN<=rlen) {
1432 break;
1433 }
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001434
Linus Walleij913a3062007-09-28 21:42:39 +00001435 ret = ptp_read_func(len - (rlen - PTP_USB_BULK_HDR_LEN),
1436 handler,
1437 params->data, &rlen, 1);
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001438
Linus Walleija0323272007-01-07 12:21:30 +00001439 if (ret!=PTP_RC_OK) {
Linus Walleij049f50c2007-03-03 20:30:43 +00001440 break;
Linus Walleija0323272007-01-07 12:21:30 +00001441 }
1442 } while (0);
Linus Walleija0323272007-01-07 12:21:30 +00001443 return ret;
1444}
1445
1446uint16_t
1447ptp_usb_getresp (PTPParams* params, PTPContainer* resp)
1448{
1449 uint16_t ret;
1450 unsigned long rlen;
1451 PTPUSBBulkContainer usbresp;
Linus Walleij3e4b1142007-11-01 23:54:16 +00001452 PTP_USB *ptp_usb = (PTP_USB *)(params->data);
Linus Walleija0323272007-01-07 12:21:30 +00001453
nicklas79daadbf22009-09-28 18:19:34 +00001454
1455 LIBMTP_USB_DEBUG("RESPONSE: ");
1456
Linus Walleija0323272007-01-07 12:21:30 +00001457 memset(&usbresp,0,sizeof(usbresp));
1458 /* read response, it should never be longer than sizeof(usbresp) */
1459 ret = ptp_usb_getpacket(params, &usbresp, &rlen);
1460
Linus Walleijb8f78c72008-03-02 20:22:56 +00001461 // Fix for bevahiour reported by Scott Snyder on Samsung YP-U3. The player
1462 // sends a packet containing just zeroes of length 2 (up to 4 has been seen too)
1463 // after a NULL packet when it should send the response. This code ignores
1464 // such illegal packets.
Linus Walleij3191bba2008-02-10 21:16:25 +00001465 while (ret==PTP_RC_OK && rlen<PTP_USB_BULK_HDR_LEN && usbresp.length==0) {
Linus Walleijd1e14e02008-12-14 01:07:30 +00001466 libusb_glue_debug (params, "ptp_usb_getresp: detected short response "
Linus Walleij3191bba2008-02-10 21:16:25 +00001467 "of %d bytes, expect problems! (re-reading "
1468 "response), rlen");
Linus Walleij00411ee2008-01-27 12:24:51 +00001469 ret = ptp_usb_getpacket(params, &usbresp, &rlen);
1470 }
1471
Linus Walleija0323272007-01-07 12:21:30 +00001472 if (ret!=PTP_RC_OK) {
1473 ret = PTP_ERROR_IO;
1474 } else
1475 if (dtoh16(usbresp.type)!=PTP_USB_CONTAINER_RESPONSE) {
1476 ret = PTP_ERROR_RESP_EXPECTED;
1477 } else
1478 if (dtoh16(usbresp.code)!=resp->Code) {
1479 ret = dtoh16(usbresp.code);
1480 }
nicklas79daadbf22009-09-28 18:19:34 +00001481
1482 LIBMTP_USB_DEBUG("%04x\n", ret);
1483
Linus Walleija0323272007-01-07 12:21:30 +00001484 if (ret!=PTP_RC_OK) {
Linus Walleijd1e14e02008-12-14 01:07:30 +00001485/* libusb_glue_error (params,
Linus Walleija0323272007-01-07 12:21:30 +00001486 "PTP: request code 0x%04x getting resp error 0x%04x",
1487 resp->Code, ret);*/
1488 return ret;
1489 }
1490 /* build an appropriate PTPContainer */
1491 resp->Code=dtoh16(usbresp.code);
1492 resp->SessionID=params->session_id;
1493 resp->Transaction_ID=dtoh32(usbresp.trans_id);
Linus Walleijfec4d562008-06-01 22:30:36 +00001494 if (FLAG_IGNORE_HEADER_ERRORS(ptp_usb)) {
Linus Walleij3e4b1142007-11-01 23:54:16 +00001495 if (resp->Transaction_ID != params->transaction_id-1) {
Linus Walleijd1e14e02008-12-14 01:07:30 +00001496 libusb_glue_debug (params, "ptp_usb_getresp: detected a broken "
Linus Walleij00411ee2008-01-27 12:24:51 +00001497 "PTP header, transaction ID insane, expect "
1498 "problems! (But continuing)");
Linus Walleij3e4b1142007-11-01 23:54:16 +00001499 // Repair the header, so it won't wreak more havoc.
1500 resp->Transaction_ID = params->transaction_id-1;
1501 }
1502 }
Linus Walleija0323272007-01-07 12:21:30 +00001503 resp->Param1=dtoh32(usbresp.payload.params.param1);
1504 resp->Param2=dtoh32(usbresp.payload.params.param2);
1505 resp->Param3=dtoh32(usbresp.payload.params.param3);
1506 resp->Param4=dtoh32(usbresp.payload.params.param4);
1507 resp->Param5=dtoh32(usbresp.payload.params.param5);
1508 return ret;
1509}
1510
1511/* Event handling functions */
1512
1513/* PTP Events wait for or check mode */
1514#define PTP_EVENT_CHECK 0x0000 /* waits for */
1515#define PTP_EVENT_CHECK_FAST 0x0001 /* checks */
1516
1517static inline uint16_t
1518ptp_usb_event (PTPParams* params, PTPContainer* event, int wait)
1519{
1520 uint16_t ret;
1521 int result;
1522 unsigned long rlen;
1523 PTPUSBEventContainer usbevent;
1524 PTP_USB *ptp_usb = (PTP_USB *)(params->data);
1525
1526 memset(&usbevent,0,sizeof(usbevent));
1527
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001528 if ((params==NULL) || (event==NULL))
Linus Walleija0323272007-01-07 12:21:30 +00001529 return PTP_ERROR_BADPARAM;
1530 ret = PTP_RC_OK;
1531 switch(wait) {
1532 case PTP_EVENT_CHECK:
Linus Walleije04a1b92011-03-09 18:00:24 +01001533 result=USB_BULK_READ(ptp_usb->handle,
1534 ptp_usb->intep,
1535 (char *) &usbevent,
1536 sizeof(usbevent),
1537 ptp_usb->timeout);
Linus Walleija0323272007-01-07 12:21:30 +00001538 if (result==0)
Linus Walleije04a1b92011-03-09 18:00:24 +01001539 result = USB_BULK_READ(ptp_usb->handle,
1540 ptp_usb->intep,
1541 (char *) &usbevent,
1542 sizeof(usbevent),
1543 ptp_usb->timeout);
Linus Walleija0323272007-01-07 12:21:30 +00001544 if (result < 0) ret = PTP_ERROR_IO;
1545 break;
1546 case PTP_EVENT_CHECK_FAST:
Linus Walleije04a1b92011-03-09 18:00:24 +01001547 result=USB_BULK_READ(ptp_usb->handle,
1548 ptp_usb->intep,
1549 (char *) &usbevent,
1550 sizeof(usbevent),
1551 ptp_usb->timeout);
Linus Walleija0323272007-01-07 12:21:30 +00001552 if (result==0)
Linus Walleije04a1b92011-03-09 18:00:24 +01001553 result = USB_BULK_READ(ptp_usb->handle,
1554 ptp_usb->intep,
1555 (char *) &usbevent,
1556 sizeof(usbevent),
1557 ptp_usb->timeout);
Linus Walleija0323272007-01-07 12:21:30 +00001558 if (result < 0) ret = PTP_ERROR_IO;
1559 break;
1560 default:
1561 ret=PTP_ERROR_BADPARAM;
1562 break;
1563 }
1564 if (ret!=PTP_RC_OK) {
Linus Walleijd1e14e02008-12-14 01:07:30 +00001565 libusb_glue_error (params,
Linus Walleija0323272007-01-07 12:21:30 +00001566 "PTP: reading event an error 0x%04x occurred", ret);
1567 return PTP_ERROR_IO;
1568 }
1569 rlen = result;
1570 if (rlen < 8) {
Linus Walleijd1e14e02008-12-14 01:07:30 +00001571 libusb_glue_error (params,
Linus Walleija0323272007-01-07 12:21:30 +00001572 "PTP: reading event an short read of %ld bytes occurred", rlen);
1573 return PTP_ERROR_IO;
1574 }
1575 /* if we read anything over interrupt endpoint it must be an event */
1576 /* build an appropriate PTPContainer */
1577 event->Code=dtoh16(usbevent.code);
1578 event->SessionID=params->session_id;
1579 event->Transaction_ID=dtoh32(usbevent.trans_id);
1580 event->Param1=dtoh32(usbevent.param1);
1581 event->Param2=dtoh32(usbevent.param2);
1582 event->Param3=dtoh32(usbevent.param3);
1583 return ret;
1584}
1585
1586uint16_t
1587ptp_usb_event_check (PTPParams* params, PTPContainer* event) {
1588
1589 return ptp_usb_event (params, event, PTP_EVENT_CHECK_FAST);
1590}
1591
1592uint16_t
1593ptp_usb_event_wait (PTPParams* params, PTPContainer* event) {
1594
1595 return ptp_usb_event (params, event, PTP_EVENT_CHECK);
1596}
1597
Linus Walleijbd3bf9e2007-09-14 19:31:54 +00001598uint16_t
1599ptp_usb_control_cancel_request (PTPParams *params, uint32_t transactionid) {
1600 PTP_USB *ptp_usb = (PTP_USB *)(params->data);
1601 int ret;
1602 unsigned char buffer[6];
1603
1604 htod16a(&buffer[0],PTP_EC_CancelTransaction);
1605 htod32a(&buffer[2],transactionid);
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001606 ret = usb_control_msg(ptp_usb->handle,
Linus Walleijbd3bf9e2007-09-14 19:31:54 +00001607 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
Linus Walleije04a1b92011-03-09 18:00:24 +01001608 0x64, 0x0000, 0x0000,
1609 (char *) buffer,
1610 sizeof(buffer),
1611 ptp_usb->timeout);
Linus Walleijbd3bf9e2007-09-14 19:31:54 +00001612 if (ret < sizeof(buffer))
1613 return PTP_ERROR_IO;
1614 return PTP_RC_OK;
1615}
Linus Walleija0323272007-01-07 12:21:30 +00001616
Linus Walleij9eb3d312006-08-04 19:25:59 +00001617static int init_ptp_usb (PTPParams* params, PTP_USB* ptp_usb, struct usb_device* dev)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001618{
Linus Walleijd6a49972006-05-02 08:24:54 +00001619 usb_dev_handle *device_handle;
Linus Walleijfea4f532009-09-22 22:08:35 +00001620 char buf[255];
Linus Walleijf2d747a2010-01-19 00:46:18 +00001621 int usbresult;
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001622
Linus Walleijd6a49972006-05-02 08:24:54 +00001623 params->sendreq_func=ptp_usb_sendreq;
1624 params->senddata_func=ptp_usb_senddata;
1625 params->getresp_func=ptp_usb_getresp;
1626 params->getdata_func=ptp_usb_getdata;
Linus Walleijff01cb12007-09-16 19:49:48 +00001627 params->cancelreq_func=ptp_usb_control_cancel_request;
Linus Walleijd6a49972006-05-02 08:24:54 +00001628 params->data=ptp_usb;
1629 params->transaction_id=0;
Linus Walleij462a5e72007-02-13 06:54:56 +00001630 /*
1631 * This is hardcoded here since we have no devices whatsoever that are BE.
1632 * Change this the day we run into our first BE device (if ever).
1633 */
Linus Walleijd6a49972006-05-02 08:24:54 +00001634 params->byteorder = PTP_DL_LE;
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001635
Linus Walleije04a1b92011-03-09 18:00:24 +01001636 ptp_usb->timeout = get_timeout(ptp_usb);
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001637
Yavor Goulishev825ff132011-02-26 00:29:01 +01001638 device_handle = usb_open(dev);
1639 if (!device_handle) {
1640 perror("usb_open()");
1641 return -1;
1642 }
1643 ptp_usb->handle = device_handle;
Linus Walleij501ba4d2006-10-16 06:17:04 +00001644#ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP
Yavor Goulishev825ff132011-02-26 00:29:01 +01001645 /*
1646 * If this device is known to be wrongfully claimed by other kernel
1647 * drivers (such as mass storage), then try to unload it to make it
1648 * accessible from user space.
1649 */
1650 if (FLAG_UNLOAD_DRIVER(ptp_usb)) {
1651 if (usb_get_driver_np(device_handle, (int) ptp_usb->interface,
1652 buf, sizeof(buf)) == 0) {
1653 if (usb_detach_kernel_driver_np(device_handle,
1654 (int) ptp_usb->interface)) {
1655 perror("usb_detach_kernel_driver_np()");
1656 return -1;
Linus Walleij501ba4d2006-10-16 06:17:04 +00001657 }
1658 }
Yavor Goulishev825ff132011-02-26 00:29:01 +01001659 }
Linus Walleij501ba4d2006-10-16 06:17:04 +00001660#endif
Linus Walleijeac1e002006-11-30 22:06:02 +00001661#ifdef __WIN32__
Yavor Goulishev825ff132011-02-26 00:29:01 +01001662 // Only needed on Windows, and cause problems on other platforms.
1663 if (usb_set_configuration(device_handle, dev->config->bConfigurationValue)) {
1664 perror("usb_set_configuration()");
1665 return -1;
1666 }
Linus Walleijeac1e002006-11-30 22:06:02 +00001667#endif
Yavor Goulishev825ff132011-02-26 00:29:01 +01001668 // It seems like on kernel 2.6.31 if we already have it open on another
1669 // pthread in our app, we'll get an error if we try to claim it again,
1670 // but that error is harmless because our process already claimed the interface
1671 usbresult = usb_claim_interface(device_handle, (int) ptp_usb->interface);
Linus Walleijf2d747a2010-01-19 00:46:18 +00001672
Yavor Goulishev825ff132011-02-26 00:29:01 +01001673 if (usbresult != 0)
1674 fprintf(stderr, "ignoring usb_claim_interface = %d", usbresult);
Linus Walleijf2d747a2010-01-19 00:46:18 +00001675
Yavor Goulishev825ff132011-02-26 00:29:01 +01001676 // FIXME : Discovered in the Barry project
1677 // kernels >= 2.6.28 don't set the interface the same way as
1678 // previous versions did, and the Blackberry gets confused
1679 // if it isn't explicitly set
1680 // See above, same issue with pthreads means that if this fails it is not
1681 // fatal
1682 // However, this causes problems on Macs so disable here
1683#ifndef __APPLE__
1684 usbresult = usb_set_altinterface(device_handle, 0);
1685 if (usbresult)
1686 fprintf(stderr, "ignoring usb_claim_interface = %d", usbresult);
1687#endif
Linus Walleijf2d747a2010-01-19 00:46:18 +00001688
Yavor Goulishev825ff132011-02-26 00:29:01 +01001689 if (FLAG_SWITCH_MODE_BLACKBERRY(ptp_usb)) {
1690 int ret;
Linus Walleijb6e06e22009-09-22 22:20:49 +00001691
Yavor Goulishev825ff132011-02-26 00:29:01 +01001692 // FIXME : Only for BlackBerry Storm
1693 // What does it mean? Maybe switch mode...
1694 // This first control message is absolutely necessary
1695 usleep(1000);
1696 ret = usb_control_msg(device_handle,
1697 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
1698 0xaa, 0x00, 0x04, buf, 0x40, 1000);
1699 LIBMTP_USB_DEBUG("BlackBerry magic part 1:\n");
1700 LIBMTP_USB_DATA(buf, ret, 16);
nicklas79daadbf22009-09-28 18:19:34 +00001701
Yavor Goulishev825ff132011-02-26 00:29:01 +01001702 usleep(1000);
1703 // This control message is unnecessary
1704 ret = usb_control_msg(device_handle,
1705 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
1706 0xa5, 0x00, 0x01, buf, 0x02, 1000);
1707 LIBMTP_USB_DEBUG("BlackBerry magic part 2:\n");
1708 LIBMTP_USB_DATA(buf, ret, 16);
nicklas79daadbf22009-09-28 18:19:34 +00001709
Yavor Goulishev825ff132011-02-26 00:29:01 +01001710 usleep(1000);
1711 // This control message is unnecessary
1712 ret = usb_control_msg(device_handle,
1713 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
1714 0xa8, 0x00, 0x01, buf, 0x05, 1000);
1715 LIBMTP_USB_DEBUG("BlackBerry magic part 3:\n");
1716 LIBMTP_USB_DATA(buf, ret, 16);
nicklas79daadbf22009-09-28 18:19:34 +00001717
Yavor Goulishev825ff132011-02-26 00:29:01 +01001718 usleep(1000);
1719 // This control message is unnecessary
1720 ret = usb_control_msg(device_handle,
1721 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
1722 0xa8, 0x00, 0x01, buf, 0x11, 1000);
1723 LIBMTP_USB_DEBUG("BlackBerry magic part 4:\n");
1724 LIBMTP_USB_DATA(buf, ret, 16);
nicklas79daadbf22009-09-28 18:19:34 +00001725
Yavor Goulishev825ff132011-02-26 00:29:01 +01001726 usleep(1000);
Linus Walleijd6a49972006-05-02 08:24:54 +00001727 }
Linus Walleij9eb3d312006-08-04 19:25:59 +00001728 return 0;
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001729}
1730
Linus Walleijd6a49972006-05-02 08:24:54 +00001731static void clear_stall(PTP_USB* ptp_usb)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001732{
Linus Walleij58b62792007-01-07 12:38:59 +00001733 uint16_t status;
Linus Walleijd6a49972006-05-02 08:24:54 +00001734 int ret;
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001735
Linus Walleijd6a49972006-05-02 08:24:54 +00001736 /* check the inep status */
Linus Walleij58b62792007-01-07 12:38:59 +00001737 status = 0;
Linus Walleij1fd2d272006-05-08 09:22:01 +00001738 ret = usb_get_endpoint_status(ptp_usb,ptp_usb->inep,&status);
Linus Walleij58b62792007-01-07 12:38:59 +00001739 if (ret<0) {
1740 perror ("inep: usb_get_endpoint_status()");
1741 } else if (status) {
nicklas79daadbf22009-09-28 18:19:34 +00001742 LIBMTP_INFO("Clearing stall on IN endpoint\n");
Linus Walleij58b62792007-01-07 12:38:59 +00001743 ret = usb_clear_stall_feature(ptp_usb,ptp_usb->inep);
1744 if (ret<0) {
1745 perror ("usb_clear_stall_feature()");
1746 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001747 }
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001748
Linus Walleijd6a49972006-05-02 08:24:54 +00001749 /* check the outep status */
Linus Walleij58b62792007-01-07 12:38:59 +00001750 status=0;
1751 ret = usb_get_endpoint_status(ptp_usb,ptp_usb->outep,&status);
1752 if (ret<0) {
1753 perror("outep: usb_get_endpoint_status()");
1754 } else if (status) {
nicklas79daadbf22009-09-28 18:19:34 +00001755 LIBMTP_INFO("Clearing stall on OUT endpoint\n");
Linus Walleij58b62792007-01-07 12:38:59 +00001756 ret = usb_clear_stall_feature(ptp_usb,ptp_usb->outep);
1757 if (ret<0) {
1758 perror("usb_clear_stall_feature()");
1759 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001760 }
Linus Walleij58b62792007-01-07 12:38:59 +00001761
1762 /* TODO: do we need this for INTERRUPT (ptp_usb->intep) too? */
1763}
1764
1765static void clear_halt(PTP_USB* ptp_usb)
1766{
1767 int ret;
1768
1769 ret = usb_clear_halt(ptp_usb->handle,ptp_usb->inep);
1770 if (ret<0) {
1771 perror("usb_clear_halt() on IN endpoint");
1772 }
1773 ret = usb_clear_halt(ptp_usb->handle,ptp_usb->outep);
1774 if (ret<0) {
1775 perror("usb_clear_halt() on OUT endpoint");
1776 }
1777 ret = usb_clear_halt(ptp_usb->handle,ptp_usb->intep);
1778 if (ret<0) {
1779 perror("usb_clear_halt() on INTERRUPT endpoint");
1780 }
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001781}
1782
Linus Walleijb0ab5482007-08-29 21:08:54 +00001783static void close_usb(PTP_USB* ptp_usb)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001784{
Linus Walleijda17cda2007-09-18 21:12:05 +00001785 // Commented out since it was confusing some
1786 // devices to do these things.
Linus Walleijfec4d562008-06-01 22:30:36 +00001787 if (!FLAG_NO_RELEASE_INTERFACE(ptp_usb)) {
Linus Walleijeabc2312008-02-13 00:02:00 +00001788 /*
1789 * Clear any stalled endpoints
1790 * On misbehaving devices designed for Windows/Mac, quote from:
1791 * http://www2.one-eyed-alien.net/~mdharm/linux-usb/target_offenses.txt
1792 * Device does Bad Things(tm) when it gets a GET_STATUS after CLEAR_HALT
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001793 * (...) Windows, when clearing a stall, only sends the CLEAR_HALT command,
1794 * and presumes that the stall has cleared. Some devices actually choke
1795 * if the CLEAR_HALT is followed by a GET_STATUS (used to determine if the
Linus Walleijeabc2312008-02-13 00:02:00 +00001796 * STALL is persistant or not).
1797 */
Linus Walleij29f03ba2007-09-25 19:22:12 +00001798 clear_stall(ptp_usb);
1799 // Clear halts on any endpoints
1800 clear_halt(ptp_usb);
1801 // Added to clear some stuff on the OUT endpoint
1802 // TODO: is this good on the Mac too?
1803 // HINT: some devices may need that you comment these two out too.
1804 usb_resetep(ptp_usb->handle, ptp_usb->outep);
1805 usb_release_interface(ptp_usb->handle, (int) ptp_usb->interface);
1806 }
Linus Walleijd6a49972006-05-02 08:24:54 +00001807 usb_close(ptp_usb->handle);
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001808}
1809
Linus Walleij550d6d52007-01-24 14:32:51 +00001810/**
Linus Walleija700d222008-05-28 23:06:14 +00001811 * Self-explanatory?
1812 */
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001813static int find_interface_and_endpoints(struct usb_device *dev,
1814 uint8_t *interface,
1815 int* inep,
1816 int* inep_maxpacket,
1817 int* outep,
1818 int *outep_maxpacket,
1819 int* intep)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001820{
Linus Walleijd7373cd2007-08-28 21:42:05 +00001821 int i;
1822
1823 // Loop over the device configurations
1824 for (i = 0; i < dev->descriptor.bNumConfigurations; i++) {
Linus Walleijb0ab5482007-08-29 21:08:54 +00001825 uint8_t j;
Linus Walleijd7373cd2007-08-28 21:42:05 +00001826
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001827 // Loop over each configurations interfaces
Linus Walleijd7373cd2007-08-28 21:42:05 +00001828 for (j = 0; j < dev->config[i].bNumInterfaces; j++) {
Linus Walleijb0ab5482007-08-29 21:08:54 +00001829 uint8_t k;
1830 uint8_t no_ep;
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001831 int found_inep = 0;
1832 int found_outep = 0;
1833 int found_intep = 0;
Linus Walleijd7373cd2007-08-28 21:42:05 +00001834 struct usb_endpoint_descriptor *ep;
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001835
1836 // MTP devices shall have 3 endpoints, ignore those interfaces
1837 // that haven't.
1838 no_ep = dev->config[i].interface[j].altsetting->bNumEndpoints;
1839 if (no_ep != 3)
1840 continue;
Linus Walleijb0ab5482007-08-29 21:08:54 +00001841
1842 *interface = dev->config[i].interface[j].altsetting->bInterfaceNumber;
1843 ep = dev->config[i].interface[j].altsetting->endpoint;
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001844
1845 // Loop over the three endpoints to locate two bulk and
1846 // one interrupt endpoint and FAIL if we cannot, and continue.
Linus Walleijb0ab5482007-08-29 21:08:54 +00001847 for (k = 0; k < no_ep; k++) {
Linus Walleije73854e2010-06-22 09:07:34 +00001848 if (ep[k].bmAttributes == USB_ENDPOINT_TYPE_BULK) {
1849 if ((ep[k].bEndpointAddress & USB_ENDPOINT_DIR_MASK) ==
1850 USB_ENDPOINT_DIR_MASK) {
1851 *inep = ep[k].bEndpointAddress;
1852 *inep_maxpacket = ep[k].wMaxPacketSize;
1853 found_inep = 1;
1854 }
1855 if ((ep[k].bEndpointAddress & USB_ENDPOINT_DIR_MASK) == 0) {
Linus Walleij48fda572010-06-22 09:09:03 +00001856 *outep = ep[k].bEndpointAddress;
1857 *outep_maxpacket = ep[k].wMaxPacketSize;
Linus Walleije73854e2010-06-22 09:07:34 +00001858 found_outep = 1;
1859 }
1860 } else if (ep[k].bmAttributes == USB_ENDPOINT_TYPE_INTERRUPT) {
1861 if ((ep[k].bEndpointAddress & USB_ENDPOINT_DIR_MASK) ==
1862 USB_ENDPOINT_DIR_MASK) {
Linus Walleij48fda572010-06-22 09:09:03 +00001863 *intep = ep[k].bEndpointAddress;
Linus Walleije73854e2010-06-22 09:07:34 +00001864 found_intep = 1;
1865 }
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001866 }
Linus Walleijd7373cd2007-08-28 21:42:05 +00001867 }
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001868 if (found_inep && found_outep && found_intep)
1869 // We assigned the endpoints so return here.
1870 return 0;
1871 // Else loop to next interface/config
Linus Walleijd6a49972006-05-02 08:24:54 +00001872 }
1873 }
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001874 return -1;
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00001875}
1876
Linus Walleija700d222008-05-28 23:06:14 +00001877/**
1878 * This function assigns params and usbinfo given a raw device
1879 * as input.
1880 * @param device the device to be assigned.
Linus Walleija700d222008-05-28 23:06:14 +00001881 * @param usbinfo a pointer to the new usbinfo.
1882 * @return an error code.
1883 */
Linus Walleij49557482010-06-22 09:14:47 +00001884LIBMTP_error_number_t configure_usb_device(LIBMTP_raw_device_t *device,
Linus Walleij3c643252008-05-31 23:22:28 +00001885 PTPParams *params,
Linus Walleija700d222008-05-28 23:06:14 +00001886 void **usbinfo)
1887{
Linus Walleija700d222008-05-28 23:06:14 +00001888 PTP_USB *ptp_usb;
1889 struct usb_device *libusb_device;
1890 uint16_t ret = 0;
Linus Walleija700d222008-05-28 23:06:14 +00001891 struct usb_bus *bus;
1892 int found = 0;
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001893 int err;
Linus Walleija700d222008-05-28 23:06:14 +00001894
1895 /* See if we can find this raw device again... */
1896 bus = init_usb();
1897 for (; bus != NULL; bus = bus->next) {
1898 if (bus->location == device->bus_location) {
1899 struct usb_device *dev = bus->devices;
1900
1901 for (; dev != NULL; dev = dev->next) {
1902 if(dev->devnum == device->devnum &&
1903 dev->descriptor.idVendor == device->device_entry.vendor_id &&
1904 dev->descriptor.idProduct == device->device_entry.product_id ) {
1905 libusb_device = dev;
1906 found = 1;
1907 break;
1908 }
1909 }
1910 if (found)
1911 break;
1912 }
1913 }
1914 /* Device has gone since detecting raw devices! */
1915 if (!found) {
1916 return LIBMTP_ERROR_NO_DEVICE_ATTACHED;
1917 }
1918
1919 /* Allocate structs */
Linus Walleija700d222008-05-28 23:06:14 +00001920 ptp_usb = (PTP_USB *) malloc(sizeof(PTP_USB));
Linus Walleij3c643252008-05-31 23:22:28 +00001921 if (ptp_usb == NULL) {
Linus Walleija700d222008-05-28 23:06:14 +00001922 return LIBMTP_ERROR_MEMORY_ALLOCATION;
1923 }
1924 /* Start with a blank slate (includes setting device_flags to 0) */
Linus Walleija700d222008-05-28 23:06:14 +00001925 memset(ptp_usb, 0, sizeof(PTP_USB));
1926
Linus Walleijfec4d562008-06-01 22:30:36 +00001927 /* Copy the raw device */
1928 memcpy(&ptp_usb->rawdevice, device, sizeof(LIBMTP_raw_device_t));
Linus Walleij335a81c2008-06-02 23:01:00 +00001929
1930 /*
1931 * Some devices must have their "OS Descriptor" massaged in order
1932 * to work.
1933 */
1934 if (FLAG_ALWAYS_PROBE_DESCRIPTOR(ptp_usb)) {
1935 // Massage the device descriptor
1936 (void) probe_device_descriptor(libusb_device, NULL);
1937 }
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001938
1939 /* Assign interface and endpoints to usbinfo... */
1940 err = find_interface_and_endpoints(libusb_device,
1941 &ptp_usb->interface,
1942 &ptp_usb->inep,
1943 &ptp_usb->inep_maxpacket,
1944 &ptp_usb->outep,
1945 &ptp_usb->outep_maxpacket,
1946 &ptp_usb->intep);
1947
1948 if (err) {
nicklas79daadbf22009-09-28 18:19:34 +00001949 LIBMTP_ERROR("LIBMTP PANIC: Unable to find interface & endpoints of device\n");
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001950 return LIBMTP_ERROR_CONNECTING;
1951 }
1952
Linus Walleije04a1b92011-03-09 18:00:24 +01001953 /* Copy USB version number */
1954 ptp_usb->bcdusb = libusb_device->descriptor.bcdUSB;
1955
Linus Walleija700d222008-05-28 23:06:14 +00001956 /* Attempt to initialize this device */
Linus Walleij3c643252008-05-31 23:22:28 +00001957 if (init_ptp_usb(params, ptp_usb, libusb_device) < 0) {
nicklas79daadbf22009-09-28 18:19:34 +00001958 LIBMTP_ERROR("LIBMTP PANIC: Unable to initialize device\n");
Linus Walleija700d222008-05-28 23:06:14 +00001959 return LIBMTP_ERROR_CONNECTING;
1960 }
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001961
Linus Walleija700d222008-05-28 23:06:14 +00001962 /*
1963 * This works in situations where previous bad applications
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001964 * have not used LIBMTP_Release_Device on exit
Linus Walleija700d222008-05-28 23:06:14 +00001965 */
Linus Walleij3c643252008-05-31 23:22:28 +00001966 if ((ret = ptp_opensession(params, 1)) == PTP_ERROR_IO) {
Linus Walleijf9137dc2010-10-31 17:13:06 +00001967 LIBMTP_ERROR("PTP_ERROR_IO: failed to open session, trying again after resetting USB interface\n");
Linus Walleija700d222008-05-28 23:06:14 +00001968 close_usb(ptp_usb);
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001969
Linus Walleijf9137dc2010-10-31 17:13:06 +00001970 LIBMTP_ERROR("LIBMTP libusb: Attempt to reset device\n");
1971 usb_reset(ptp_usb->handle);
1972
Linus Walleij3c643252008-05-31 23:22:28 +00001973 if(init_ptp_usb(params, ptp_usb, libusb_device) <0) {
Linus Walleijf9137dc2010-10-31 17:13:06 +00001974 LIBMTP_ERROR("LIBMTP PANIC: Could not init USB on second attempt\n");
Linus Walleija700d222008-05-28 23:06:14 +00001975 return LIBMTP_ERROR_CONNECTING;
1976 }
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001977
Linus Walleija700d222008-05-28 23:06:14 +00001978 /* Device has been reset, try again */
Linus Walleijf9137dc2010-10-31 17:13:06 +00001979 if ((ret = ptp_opensession(params, 1)) == PTP_ERROR_IO) {
1980 LIBMTP_ERROR("LIBMTP PANIC: failed to open session on second attempt\n");
1981 return LIBMTP_ERROR_CONNECTING;
1982 }
Linus Walleija700d222008-05-28 23:06:14 +00001983 }
Linus Walleij5f3c44b2009-09-12 21:03:06 +00001984
Linus Walleija700d222008-05-28 23:06:14 +00001985 /* Was the transaction id invalid? Try again */
1986 if (ret == PTP_RC_InvalidTransactionID) {
nicklas79daadbf22009-09-28 18:19:34 +00001987 LIBMTP_ERROR("LIBMTP WARNING: Transaction ID was invalid, increment and try again\n");
Linus Walleij3c643252008-05-31 23:22:28 +00001988 params->transaction_id += 10;
1989 ret = ptp_opensession(params, 1);
Linus Walleija700d222008-05-28 23:06:14 +00001990 }
1991
1992 if (ret != PTP_RC_SessionAlreadyOpened && ret != PTP_RC_OK) {
nicklas79daadbf22009-09-28 18:19:34 +00001993 LIBMTP_ERROR("LIBMTP PANIC: Could not open session! "
Linus Walleija700d222008-05-28 23:06:14 +00001994 "(Return code %d)\n Try to reset the device.\n",
1995 ret);
1996 usb_release_interface(ptp_usb->handle,
1997 (int) ptp_usb->interface);
1998 return LIBMTP_ERROR_CONNECTING;
1999 }
2000
2001 /* OK configured properly */
Linus Walleija700d222008-05-28 23:06:14 +00002002 *usbinfo = (void *) ptp_usb;
2003 return LIBMTP_ERROR_NONE;
2004}
2005
2006
Linus Walleijb0ab5482007-08-29 21:08:54 +00002007void close_device (PTP_USB *ptp_usb, PTPParams *params)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00002008{
Linus Walleijd6a49972006-05-02 08:24:54 +00002009 if (ptp_closesession(params)!=PTP_RC_OK)
nicklas79daadbf22009-09-28 18:19:34 +00002010 LIBMTP_ERROR("ERROR: Could not close session!\n");
Linus Walleijb0ab5482007-08-29 21:08:54 +00002011 close_usb(ptp_usb);
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00002012}
2013
Linus Walleij2f622812008-08-30 22:06:58 +00002014void set_usb_device_timeout(PTP_USB *ptp_usb, int timeout)
2015{
Linus Walleije04a1b92011-03-09 18:00:24 +01002016 ptp_usb->timeout = timeout;
Linus Walleij2f622812008-08-30 22:06:58 +00002017}
2018
2019void get_usb_device_timeout(PTP_USB *ptp_usb, int *timeout)
2020{
Linus Walleije04a1b92011-03-09 18:00:24 +01002021 *timeout = ptp_usb->timeout;
2022}
2023
2024int guess_usb_speed(PTP_USB *ptp_usb)
2025{
2026 int bytes_per_second;
2027
2028 /*
2029 * We don't know the actual speeds so these are rough guesses
2030 * from the info you can find here:
2031 * http://en.wikipedia.org/wiki/USB#Transfer_rates
2032 * http://www.barefeats.com/usb2.html
2033 */
2034 switch (ptp_usb->bcdusb & 0xFF00) {
2035 case 0x0100:
2036 /* 1.x USB versions let's say 1MiB/s */
2037 bytes_per_second = 1*1024*1024;
2038 break;
2039 case 0x0200:
2040 case 0x0300:
2041 /* USB 2.0 nominal speed 18MiB/s */
2042 /* USB 3.0 won't be worse? */
2043 bytes_per_second = 18*1024*1024;
2044 break;
2045 default:
2046 /* Half-guess something? */
2047 bytes_per_second = 1*1024*1024;
2048 break;
2049 }
2050 return bytes_per_second;
Linus Walleij2f622812008-08-30 22:06:58 +00002051}
2052
Linus Walleijd6a49972006-05-02 08:24:54 +00002053static int usb_clear_stall_feature(PTP_USB* ptp_usb, int ep)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00002054{
Linus Walleijd6a49972006-05-02 08:24:54 +00002055 return (usb_control_msg(ptp_usb->handle,
Linus Walleije04a1b92011-03-09 18:00:24 +01002056 USB_RECIP_ENDPOINT,
2057 USB_REQ_CLEAR_FEATURE,
2058 USB_FEATURE_HALT,
2059 ep,
2060 NULL,
2061 0,
2062 ptp_usb->timeout));
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00002063}
2064
Linus Walleijd6a49972006-05-02 08:24:54 +00002065static int usb_get_endpoint_status(PTP_USB* ptp_usb, int ep, uint16_t* status)
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00002066{
Linus Walleijd6a49972006-05-02 08:24:54 +00002067 return (usb_control_msg(ptp_usb->handle,
Linus Walleije04a1b92011-03-09 18:00:24 +01002068 USB_DP_DTH|USB_RECIP_ENDPOINT,
2069 USB_REQ_GET_STATUS,
2070 USB_FEATURE_HALT,
2071 ep,
2072 (char *) status,
2073 2,
2074 ptp_usb->timeout));
Linus Walleijeb8c6fe2006-02-03 09:46:22 +00002075}