Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1 | /* |
Linus Walleij | 2f45d22 | 2007-02-02 22:47:39 +0000 | [diff] [blame] | 2 | * \file libusb-glue.c |
| 3 | * Low-level USB interface glue towards libusb. |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 4 | * |
Linus Walleij | 2f45d22 | 2007-02-02 22:47:39 +0000 | [diff] [blame] | 5 | * Copyright (C) 2005-2007 Richard A. Low <richard@wentnet.com> |
Linus Walleij | bac1eed | 2011-02-04 23:55:19 +0100 | [diff] [blame] | 6 | * Copyright (C) 2005-2011 Linus Walleij <triad@df.lth.se> |
Linus Walleij | 2f45d22 | 2007-02-02 22:47:39 +0000 | [diff] [blame] | 7 | * Copyright (C) 2006-2007 Marcus Meissner |
| 8 | * Copyright (C) 2007 Ted Bullock |
Linus Walleij | 2f62281 | 2008-08-30 22:06:58 +0000 | [diff] [blame] | 9 | * Copyright (C) 2008 Chris Bagwell <chris@cnpbagwell.com> |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 10 | * |
Linus Walleij | 2f45d22 | 2007-02-02 22:47:39 +0000 | [diff] [blame] | 11 | * 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 Walleij | 2e1c8a3 | 2007-01-22 06:46:08 +0000 | [diff] [blame] | 15 | * |
Linus Walleij | 2f45d22 | 2007-02-02 22:47:39 +0000 | [diff] [blame] | 16 | * 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 Walleij | 2e1c8a3 | 2007-01-22 06:46:08 +0000 | [diff] [blame] | 20 | * |
Linus Walleij | 2f45d22 | 2007-02-02 22:47:39 +0000 | [diff] [blame] | 21 | * 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 Walleij | d866d24 | 2009-08-23 21:50:39 +0000 | [diff] [blame] | 28 | * (Notice that Anglo-Saxons use little-endian dates and Swedes |
Linus Walleij | 2f45d22 | 2007-02-02 22:47:39 +0000 | [diff] [blame] | 29 | * use big-endian dates.) |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 30 | * |
| 31 | */ |
Linus Walleij | 21b2dde | 2009-06-02 19:55:34 +0000 | [diff] [blame] | 32 | #include "config.h" |
Linus Walleij | 7beba57 | 2006-11-29 08:56:12 +0000 | [diff] [blame] | 33 | #include "libmtp.h" |
| 34 | #include "libusb-glue.h" |
Linus Walleij | 3e667ae | 2007-10-29 23:29:39 +0000 | [diff] [blame] | 35 | #include "device-flags.h" |
Linus Walleij | 7beba57 | 2006-11-29 08:56:12 +0000 | [diff] [blame] | 36 | #include "util.h" |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 37 | #include "ptp.h" |
Linus Walleij | 7beba57 | 2006-11-29 08:56:12 +0000 | [diff] [blame] | 38 | |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 39 | #include <errno.h> |
| 40 | #include <stdio.h> |
| 41 | #include <stdlib.h> |
| 42 | #include <string.h> |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 43 | #include <usb.h> |
| 44 | |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 45 | #include "ptp-pack.c" |
| 46 | |
Linus Walleij | d866d24 | 2009-08-23 21:50:39 +0000 | [diff] [blame] | 47 | /* Aha, older libusb does not have USB_CLASS_PTP */ |
| 48 | #ifndef USB_CLASS_PTP |
| 49 | #define USB_CLASS_PTP 6 |
| 50 | #endif |
| 51 | |
Linus Walleij | e04a1b9 | 2011-03-09 18:00:24 +0100 | [diff] [blame^] | 52 | /* |
| 53 | * Default USB timeout length. This can be overridden as needed |
| 54 | * but should start with a reasonable value so most common |
Linus Walleij | 2f62281 | 2008-08-30 22:06:58 +0000 | [diff] [blame] | 55 | * 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 Walleij | e04a1b9 | 2011-03-09 18:00:24 +0100 | [diff] [blame^] | 60 | #define USB_TIMEOUT_DEFAULT 10000 |
| 61 | #define USB_TIMEOUT_LONG 60000 |
| 62 | static 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 Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 69 | |
| 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 Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 83 | /* Internal data types */ |
| 84 | struct 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 | }; |
| 91 | typedef struct mtpdevice_list_struct mtpdevice_list_t; |
| 92 | |
Linus Walleij | 6fd2f08 | 2006-03-28 07:19:22 +0000 | [diff] [blame] | 93 | static const LIBMTP_device_entry_t mtp_device_table[] = { |
Linus Walleij | 1a673de | 2007-10-29 23:10:05 +0000 | [diff] [blame] | 94 | /* We include an .h file which is shared between us and libgphoto2 */ |
| 95 | #include "music-players.h" |
Linus Walleij | a548364 | 2006-03-09 09:20:38 +0000 | [diff] [blame] | 96 | }; |
Linus Walleij | 6fd2f08 | 2006-03-28 07:19:22 +0000 | [diff] [blame] | 97 | static const int mtp_device_table_size = sizeof(mtp_device_table) / sizeof(LIBMTP_device_entry_t); |
Linus Walleij | a548364 | 2006-03-09 09:20:38 +0000 | [diff] [blame] | 98 | |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 99 | // Local functions |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 100 | static struct usb_bus* init_usb(); |
Linus Walleij | b0ab548 | 2007-08-29 21:08:54 +0000 | [diff] [blame] | 101 | static void close_usb(PTP_USB* ptp_usb); |
Linus Walleij | 5f3c44b | 2009-09-12 21:03:06 +0000 | [diff] [blame] | 102 | static 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 Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 109 | static void clear_stall(PTP_USB* ptp_usb); |
Linus Walleij | 9eb3d31 | 2006-08-04 19:25:59 +0000 | [diff] [blame] | 110 | static int init_ptp_usb (PTPParams* params, PTP_USB* ptp_usb, struct usb_device* dev); |
Linus Walleij | 784ac3f | 2006-12-29 10:36:51 +0000 | [diff] [blame] | 111 | static short ptp_write_func (unsigned long,PTPDataHandler*,void *data,unsigned long*); |
Richard Low | 4df32f8 | 2007-01-11 20:04:39 +0000 | [diff] [blame] | 112 | static short ptp_read_func (unsigned long,PTPDataHandler*,void *data,unsigned long*,int); |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 113 | static int usb_clear_stall_feature(PTP_USB* ptp_usb, int ep); |
| 114 | static int usb_get_endpoint_status(PTP_USB* ptp_usb, int ep, uint16_t* status); |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 115 | |
Linus Walleij | 552ba32 | 2007-01-22 11:49:59 +0000 | [diff] [blame] | 116 | /** |
| 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 | */ |
| 134 | int LIBMTP_Get_Supported_Devices_List(LIBMTP_device_entry_t ** const devices, int * const numdevs) |
Linus Walleij | 6fd2f08 | 2006-03-28 07:19:22 +0000 | [diff] [blame] | 135 | { |
| 136 | *devices = (LIBMTP_device_entry_t *) &mtp_device_table; |
| 137 | *numdevs = mtp_device_table_size; |
| 138 | return 0; |
| 139 | } |
| 140 | |
Linus Walleij | 552ba32 | 2007-01-22 11:49:59 +0000 | [diff] [blame] | 141 | |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 142 | static struct usb_bus* init_usb() |
| 143 | { |
Linus Walleij | c1a82f1 | 2009-11-30 00:06:28 +0000 | [diff] [blame] | 144 | struct usb_bus* busses; |
| 145 | struct usb_bus* bus; |
| 146 | |
| 147 | /* |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 148 | * Some additional libusb debugging please. |
| 149 | * We use the same level debug between MTP and USB. |
| 150 | */ |
nicklas79 | 0358408 | 2009-09-28 18:20:16 +0000 | [diff] [blame] | 151 | if ((LIBMTP_debug & LIBMTP_DEBUG_USB) != 0) |
nicklas79 | a99196a | 2009-09-28 18:19:57 +0000 | [diff] [blame] | 152 | usb_set_debug(9); |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 153 | |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 154 | usb_init(); |
| 155 | usb_find_busses(); |
| 156 | usb_find_devices(); |
Linus Walleij | c1a82f1 | 2009-11-30 00:06:28 +0000 | [diff] [blame] | 157 | /* 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 Low | ea73f5c | 2010-05-16 11:57:56 +0000 | [diff] [blame] | 160 | if (!bus->location) |
| 161 | bus->location = strtoul(bus->dirname, NULL, 10); |
Linus Walleij | c1a82f1 | 2009-11-30 00:06:28 +0000 | [diff] [blame] | 162 | } |
| 163 | return (busses); |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | /** |
tedbullock | 0f033cb | 2007-02-14 20:56:54 +0000 | [diff] [blame] | 167 | * Small recursive function to append a new usb_device to the linked list of |
| 168 | * USB MTP devices |
Linus Walleij | c1a82f1 | 2009-11-30 00:06:28 +0000 | [diff] [blame] | 169 | * @param devlist dynamic linked list of pointers to usb devices with MTP |
Linus Walleij | 63fd1e6 | 2008-04-23 23:49:43 +0000 | [diff] [blame] | 170 | * 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. |
tedbullock | 0f033cb | 2007-02-14 20:56:54 +0000 | [diff] [blame] | 174 | */ |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 175 | static mtpdevice_list_t *append_to_mtpdevice_list(mtpdevice_list_t *devlist, |
Linus Walleij | 63fd1e6 | 2008-04-23 23:49:43 +0000 | [diff] [blame] | 176 | struct usb_device *newdevice, |
| 177 | uint32_t bus_location) |
tedbullock | 0f033cb | 2007-02-14 20:56:54 +0000 | [diff] [blame] | 178 | { |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 179 | mtpdevice_list_t *new_list_entry; |
Linus Walleij | c1a82f1 | 2009-11-30 00:06:28 +0000 | [diff] [blame] | 180 | |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 181 | 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 Walleij | 63fd1e6 | 2008-04-23 23:49:43 +0000 | [diff] [blame] | 187 | new_list_entry->bus_location = bus_location; |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 188 | new_list_entry->next = NULL; |
Linus Walleij | 5d987e1 | 2010-05-22 22:35:33 +0000 | [diff] [blame] | 189 | |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 190 | 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; |
tedbullock | 0f033cb | 2007-02-14 20:56:54 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Small recursive function to free dynamic memory allocated to the linked list |
| 204 | * of USB MTP devices |
Linus Walleij | 5d987e1 | 2010-05-22 22:35:33 +0000 | [diff] [blame] | 205 | * @param devlist dynamic linked list of pointers to usb devices with MTP |
tedbullock | 0f033cb | 2007-02-14 20:56:54 +0000 | [diff] [blame] | 206 | * properties. |
| 207 | * @return nothing |
| 208 | */ |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 209 | static void free_mtpdevice_list(mtpdevice_list_t *devlist) |
tedbullock | 0f033cb | 2007-02-14 20:56:54 +0000 | [diff] [blame] | 210 | { |
Linus Walleij | ac06186 | 2007-03-07 08:28:33 +0000 | [diff] [blame] | 211 | mtpdevice_list_t *tmplist = devlist; |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 212 | |
| 213 | if (devlist == NULL) |
| 214 | return; |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 215 | while (tmplist != NULL) { |
| 216 | mtpdevice_list_t *tmp = tmplist; |
| 217 | tmplist = tmplist->next; |
Richard Low | 61edc1a | 2007-09-23 10:35:48 +0000 | [diff] [blame] | 218 | // Do not free() the fields (ptp_usb, params)! These are used elsewhere. |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 219 | free(tmp); |
tedbullock | 0f033cb | 2007-02-14 20:56:54 +0000 | [diff] [blame] | 220 | } |
tedbullock | 0f033cb | 2007-02-14 20:56:54 +0000 | [diff] [blame] | 221 | return; |
| 222 | } |
| 223 | |
| 224 | /** |
Linus Walleij | 9521e2b | 2007-07-10 21:50:42 +0000 | [diff] [blame] | 225 | * 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 Walleij | a4942fc | 2007-03-12 19:23:21 +0000 | [diff] [blame] | 230 | * @param dev a device struct from libusb. |
Linus Walleij | 9521e2b | 2007-07-10 21:50:42 +0000 | [diff] [blame] | 231 | * @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 Walleij | a4942fc | 2007-03-12 19:23:21 +0000 | [diff] [blame] | 233 | * @return 1 if the device is MTP compliant, 0 if not. |
| 234 | */ |
Linus Walleij | 9521e2b | 2007-07-10 21:50:42 +0000 | [diff] [blame] | 235 | static int probe_device_descriptor(struct usb_device *dev, FILE *dumpfile) |
Linus Walleij | a4942fc | 2007-03-12 19:23:21 +0000 | [diff] [blame] | 236 | { |
| 237 | usb_dev_handle *devh; |
| 238 | unsigned char buf[1024], cmd; |
Linus Walleij | bc550bc | 2007-11-08 23:25:09 +0000 | [diff] [blame] | 239 | int i; |
Linus Walleij | a4942fc | 2007-03-12 19:23:21 +0000 | [diff] [blame] | 240 | int ret; |
Linus Walleij | fb3e302 | 2011-01-19 19:20:34 +0100 | [diff] [blame] | 241 | /* This is to indicate if we find some vendor interface */ |
| 242 | int found_vendor_spec_interface = 0; |
Linus Walleij | 5d987e1 | 2010-05-22 22:35:33 +0000 | [diff] [blame] | 243 | |
| 244 | /* |
Linus Walleij | fb3e302 | 2011-01-19 19:20:34 +0100 | [diff] [blame] | 245 | * 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 Walleij | 5d987e1 | 2010-05-22 22:35:33 +0000 | [diff] [blame] | 248 | */ |
Linus Walleij | 4eb8ad9 | 2011-01-27 22:52:07 +0100 | [diff] [blame] | 249 | 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 Walleij | a4942fc | 2007-03-12 19:23:21 +0000 | [diff] [blame] | 253 | return 0; |
| 254 | } |
Linus Walleij | 5d987e1 | 2010-05-22 22:35:33 +0000 | [diff] [blame] | 255 | |
Linus Walleij | a4942fc | 2007-03-12 19:23:21 +0000 | [diff] [blame] | 256 | /* 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 Walleij | bc550bc | 2007-11-08 23:25:09 +0000 | [diff] [blame] | 262 | |
| 263 | /* |
Richard Low | e029eba | 2009-04-11 12:36:25 +0000 | [diff] [blame] | 264 | * This sometimes crashes on the j for loop below |
Linus Walleij | 8824421 | 2009-08-12 16:05:56 +0000 | [diff] [blame] | 265 | * I think it is because config is NULL yet |
Richard Low | e029eba | 2009-04-11 12:36:25 +0000 | [diff] [blame] | 266 | * dev->descriptor.bNumConfigurations > 0 |
| 267 | * this check should stop this |
Linus Walleij | bc550bc | 2007-11-08 23:25:09 +0000 | [diff] [blame] | 268 | */ |
Richard Low | e029eba | 2009-04-11 12:36:25 +0000 | [diff] [blame] | 269 | if (dev->config) { |
| 270 | /* |
Linus Walleij | 8824421 | 2009-08-12 16:05:56 +0000 | [diff] [blame] | 271 | * Loop over the device configurations and interfaces. Nokia MTP-capable |
| 272 | * handsets (possibly others) typically have the string "MTP" in their |
Richard Low | e029eba | 2009-04-11 12:36:25 +0000 | [diff] [blame] | 273 | * 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 Walleij | 8824421 | 2009-08-12 16:05:56 +0000 | [diff] [blame] | 278 | |
Richard Low | e029eba | 2009-04-11 12:36:25 +0000 | [diff] [blame] | 279 | 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 Walleij | 8824421 | 2009-08-12 16:05:56 +0000 | [diff] [blame] | 282 | /* Current interface descriptor */ |
| 283 | struct usb_interface_descriptor *intf = |
| 284 | &dev->config[i].interface[j].altsetting[k]; |
| 285 | |
| 286 | /* |
Linus Walleij | fb3e302 | 2011-01-19 19:20:34 +0100 | [diff] [blame] | 287 | * 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 Walleij | 8824421 | 2009-08-12 16:05:56 +0000 | [diff] [blame] | 296 | * Check for Still Image Capture class with PIMA 15740 protocol, |
| 297 | * also known as PTP |
| 298 | */ |
Linus Walleij | 5d92c8c | 2011-01-25 23:42:19 +0100 | [diff] [blame] | 299 | #if 0 |
Linus Walleij | 8824421 | 2009-08-12 16:05:56 +0000 | [diff] [blame] | 300 | if (intf->bInterfaceClass == USB_CLASS_PTP |
| 301 | && intf->bInterfaceSubClass == 0x01 |
| 302 | && intf->bInterfaceProtocol == 0x01) { |
| 303 | if (dumpfile != NULL) { |
Linus Walleij | 5d92c8c | 2011-01-25 23:42:19 +0100 | [diff] [blame] | 304 | fprintf(dumpfile, " Found PTP device, check vendor " |
| 305 | "extension...\n"); |
Linus Walleij | 8824421 | 2009-08-12 16:05:56 +0000 | [diff] [blame] | 306 | } |
Linus Walleij | 5d92c8c | 2011-01-25 23:42:19 +0100 | [diff] [blame] | 307 | // This is where we may insert code to open a PTP |
| 308 | // session and query the vendor extension ID to see |
Linus Walleij | 4eb8ad9 | 2011-01-27 22:52:07 +0100 | [diff] [blame] | 309 | // if it is 0xffffffff, i.e. MTP according to the spec. |
Linus Walleij | 5d92c8c | 2011-01-25 23:42:19 +0100 | [diff] [blame] | 310 | if (was_mtp_extension) { |
| 311 | usb_close(devh); |
| 312 | return 1; |
| 313 | } |
Linus Walleij | 8824421 | 2009-08-12 16:05:56 +0000 | [diff] [blame] | 314 | } |
Linus Walleij | 5d92c8c | 2011-01-25 23:42:19 +0100 | [diff] [blame] | 315 | #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 Walleij | 8824421 | 2009-08-12 16:05:56 +0000 | [diff] [blame] | 321 | 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 Walleij | fea4f53 | 2009-09-22 22:08:35 +0000 | [diff] [blame] | 328 | if (strstr((char *) buf, "MTP") != NULL) { |
Linus Walleij | 8824421 | 2009-08-12 16:05:56 +0000 | [diff] [blame] | 329 | 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 Low | e029eba | 2009-04-11 12:36:25 +0000 | [diff] [blame] | 334 | usb_close(devh); |
| 335 | return 1; |
| 336 | } |
| 337 | #ifdef LIBUSB_HAS_GET_DRIVER_NP |
Linus Walleij | 8824421 | 2009-08-12 16:05:56 +0000 | [diff] [blame] | 338 | { |
| 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")) { |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 351 | LIBMTP_INFO("avoid probing device using kernel interface \"%s\"\n", devname); |
Linus Walleij | 8824421 | 2009-08-12 16:05:56 +0000 | [diff] [blame] | 352 | return 0; |
| 353 | } |
| 354 | } |
Richard Low | e029eba | 2009-04-11 12:36:25 +0000 | [diff] [blame] | 355 | #endif |
| 356 | } |
| 357 | } |
| 358 | } |
| 359 | } else { |
| 360 | if (dev->descriptor.bNumConfigurations) |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 361 | LIBMTP_INFO("dev->config is NULL in probe_device_descriptor yet dev->descriptor.bNumConfigurations > 0\n"); |
Linus Walleij | bc550bc | 2007-11-08 23:25:09 +0000 | [diff] [blame] | 362 | } |
Linus Walleij | fea4f53 | 2009-09-22 22:08:35 +0000 | [diff] [blame] | 363 | |
Linus Walleij | 49e57b8 | 2009-12-16 09:21:33 +0000 | [diff] [blame] | 364 | /* |
Linus Walleij | fb3e302 | 2011-01-19 19:20:34 +0100 | [diff] [blame] | 365 | * Only probe for OS descriptor if the device is vendor specific |
| 366 | * or one of the interfaces found is. |
Linus Walleij | 49e57b8 | 2009-12-16 09:21:33 +0000 | [diff] [blame] | 367 | */ |
Linus Walleij | fb3e302 | 2011-01-19 19:20:34 +0100 | [diff] [blame] | 368 | if (dev->descriptor.bDeviceClass == USB_CLASS_VENDOR_SPEC || |
| 369 | found_vendor_spec_interface) { |
Linus Walleij | 9521e2b | 2007-07-10 21:50:42 +0000 | [diff] [blame] | 370 | |
Linus Walleij | fb3e302 | 2011-01-19 19:20:34 +0100 | [diff] [blame] | 371 | /* Read the special descriptor */ |
| 372 | ret = usb_get_descriptor(devh, 0x03, 0xee, buf, sizeof(buf)); |
Linus Walleij | fea4f53 | 2009-09-22 22:08:35 +0000 | [diff] [blame] | 373 | |
Linus Walleij | fb3e302 | 2011-01-19 19:20:34 +0100 | [diff] [blame] | 374 | /* |
| 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 Meissner | e0bb6c6 | 2011-02-07 13:26:58 +0100 | [diff] [blame] | 476 | /* Close the USB device handle */ |
| 477 | usb_close(devh); |
| 478 | return 1; |
Linus Walleij | a4942fc | 2007-03-12 19:23:21 +0000 | [diff] [blame] | 479 | } |
Linus Walleij | fea4f53 | 2009-09-22 22:08:35 +0000 | [diff] [blame] | 480 | |
Linus Walleij | a4942fc | 2007-03-12 19:23:21 +0000 | [diff] [blame] | 481 | /* Close the USB device handle */ |
| 482 | usb_close(devh); |
Marcus Meissner | e0bb6c6 | 2011-02-07 13:26:58 +0100 | [diff] [blame] | 483 | return 0; |
Linus Walleij | a4942fc | 2007-03-12 19:23:21 +0000 | [diff] [blame] | 484 | } |
| 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 Walleij | 4955748 | 2010-06-22 09:14:47 +0000 | [diff] [blame] | 489 | * dynamic array mtp_device_list. Be sure to call |
| 490 | * <code>free_mtpdevice_list(mtp_device_list)</code> when you are done |
Linus Walleij | 63fd1e6 | 2008-04-23 23:49:43 +0000 | [diff] [blame] | 491 | * with it, assuming it is not NULL. |
Linus Walleij | 4955748 | 2010-06-22 09:14:47 +0000 | [diff] [blame] | 492 | * @param mtp_device_list dynamic array of pointers to usb devices with MTP |
Linus Walleij | 45a8637 | 2007-03-07 09:36:19 +0000 | [diff] [blame] | 493 | * properties (if this list is not empty, new entries will be appended |
| 494 | * to the list). |
Linus Walleij | a4942fc | 2007-03-12 19:23:21 +0000 | [diff] [blame] | 495 | * @return LIBMTP_ERROR_NONE implies that devices have been found, scan the list |
Linus Walleij | 4955748 | 2010-06-22 09:14:47 +0000 | [diff] [blame] | 496 | * appropriately. LIBMTP_ERROR_NO_DEVICE_ATTACHED implies that no |
Linus Walleij | 9c6c154 | 2007-09-19 13:43:06 +0000 | [diff] [blame] | 497 | * devices have been found. |
Linus Walleij | 2e1c8a3 | 2007-01-22 06:46:08 +0000 | [diff] [blame] | 498 | */ |
Linus Walleij | 45a8637 | 2007-03-07 09:36:19 +0000 | [diff] [blame] | 499 | static LIBMTP_error_number_t get_mtp_usb_device_list(mtpdevice_list_t ** mtp_device_list) |
Linus Walleij | 2e1c8a3 | 2007-01-22 06:46:08 +0000 | [diff] [blame] | 500 | { |
Linus Walleij | 2e1c8a3 | 2007-01-22 06:46:08 +0000 | [diff] [blame] | 501 | struct usb_bus *bus = init_usb(); |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 502 | for (; bus != NULL; bus = bus->next) { |
Linus Walleij | 2e1c8a3 | 2007-01-22 06:46:08 +0000 | [diff] [blame] | 503 | struct usb_device *dev = bus->devices; |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 504 | for (; dev != NULL; dev = dev->next) { |
Richard Low | e9f205f | 2007-09-16 16:25:50 +0000 | [diff] [blame] | 505 | if (dev->descriptor.bDeviceClass != USB_CLASS_HUB) { |
Linus Walleij | 9c6c154 | 2007-09-19 13:43:06 +0000 | [diff] [blame] | 506 | 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 Low | e9f205f | 2007-09-16 16:25:50 +0000 | [diff] [blame] | 512 | 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 Walleij | 4955748 | 2010-06-22 09:14:47 +0000 | [diff] [blame] | 516 | *mtp_device_list = append_to_mtpdevice_list(*mtp_device_list, |
| 517 | dev, |
Linus Walleij | 63fd1e6 | 2008-04-23 23:49:43 +0000 | [diff] [blame] | 518 | bus->location); |
Richard Low | e9f205f | 2007-09-16 16:25:50 +0000 | [diff] [blame] | 519 | found = 1; |
| 520 | break; |
| 521 | } |
| 522 | } |
Linus Walleij | 9c6c154 | 2007-09-19 13:43:06 +0000 | [diff] [blame] | 523 | // If we didn't know it, try probing the "OS Descriptor". |
Richard Low | e9f205f | 2007-09-16 16:25:50 +0000 | [diff] [blame] | 524 | if (!found) { |
Richard Low | e9f205f | 2007-09-16 16:25:50 +0000 | [diff] [blame] | 525 | if (probe_device_descriptor(dev, NULL)) { |
| 526 | /* Append this usb device to the MTP USB Device List */ |
Linus Walleij | 4955748 | 2010-06-22 09:14:47 +0000 | [diff] [blame] | 527 | *mtp_device_list = append_to_mtpdevice_list(*mtp_device_list, |
Linus Walleij | 63fd1e6 | 2008-04-23 23:49:43 +0000 | [diff] [blame] | 528 | dev, |
| 529 | bus->location); |
Richard Low | e9f205f | 2007-09-16 16:25:50 +0000 | [diff] [blame] | 530 | } |
Linus Walleij | d866d24 | 2009-08-23 21:50:39 +0000 | [diff] [blame] | 531 | /* |
| 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 Low | e9f205f | 2007-09-16 16:25:50 +0000 | [diff] [blame] | 543 | } |
Linus Walleij | 9c6c154 | 2007-09-19 13:43:06 +0000 | [diff] [blame] | 544 | } |
Linus Walleij | 2e1c8a3 | 2007-01-22 06:46:08 +0000 | [diff] [blame] | 545 | } |
| 546 | } |
Linus Walleij | 4955748 | 2010-06-22 09:14:47 +0000 | [diff] [blame] | 547 | |
Linus Walleij | 2e1c8a3 | 2007-01-22 06:46:08 +0000 | [diff] [blame] | 548 | /* If nothing was found we end up here. */ |
Linus Walleij | a4942fc | 2007-03-12 19:23:21 +0000 | [diff] [blame] | 549 | if(*mtp_device_list == NULL) { |
tedbullock | 433d217 | 2007-02-23 22:39:12 +0000 | [diff] [blame] | 550 | return LIBMTP_ERROR_NO_DEVICE_ATTACHED; |
Linus Walleij | a4942fc | 2007-03-12 19:23:21 +0000 | [diff] [blame] | 551 | } |
| 552 | return LIBMTP_ERROR_NONE; |
Linus Walleij | 2e1c8a3 | 2007-01-22 06:46:08 +0000 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | /** |
Linus Walleij | 549f49a | 2010-12-05 14:00:34 +0000 | [diff] [blame] | 556 | * 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 | */ |
| 563 | int 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 Walleij | 63fd1e6 | 2008-04-23 23:49:43 +0000 | [diff] [blame] | 584 | * Detect the raw MTP device descriptors and return a list of |
| 585 | * of the devices found. |
Linus Walleij | 4955748 | 2010-06-22 09:14:47 +0000 | [diff] [blame] | 586 | * |
Linus Walleij | 63fd1e6 | 2008-04-23 23:49:43 +0000 | [diff] [blame] | 587 | * @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 Walleij | 4955748 | 2010-06-22 09:14:47 +0000 | [diff] [blame] | 593 | * @param numdevs a pointer to an integer that will hold |
Linus Walleij | 63fd1e6 | 2008-04-23 23:49:43 +0000 | [diff] [blame] | 594 | * the number of devices in the list. This may |
| 595 | * be 0. |
| 596 | * @return 0 if successful, any other value means failure. |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 597 | */ |
Linus Walleij | 4955748 | 2010-06-22 09:14:47 +0000 | [diff] [blame] | 598 | LIBMTP_error_number_t LIBMTP_Detect_Raw_Devices(LIBMTP_raw_device_t ** devices, |
Linus Walleij | 63fd1e6 | 2008-04-23 23:49:43 +0000 | [diff] [blame] | 599 | int * numdevs) |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 600 | { |
Linus Walleij | 63fd1e6 | 2008-04-23 23:49:43 +0000 | [diff] [blame] | 601 | mtpdevice_list_t *devlist = NULL; |
| 602 | mtpdevice_list_t *dev; |
| 603 | LIBMTP_error_number_t ret; |
Linus Walleij | 63fd1e6 | 2008-04-23 23:49:43 +0000 | [diff] [blame] | 604 | LIBMTP_raw_device_t *retdevs; |
| 605 | int devs = 0; |
Linus Walleij | f106aa6 | 2008-05-04 22:55:51 +0000 | [diff] [blame] | 606 | int i, j; |
Linus Walleij | b6b6913 | 2007-03-07 09:45:05 +0000 | [diff] [blame] | 607 | |
| 608 | ret = get_mtp_usb_device_list(&devlist); |
Linus Walleij | ef2fb36 | 2008-05-28 20:59:09 +0000 | [diff] [blame] | 609 | if (ret == LIBMTP_ERROR_NO_DEVICE_ATTACHED) { |
| 610 | *devices = NULL; |
| 611 | *numdevs = 0; |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 612 | return ret; |
Linus Walleij | ef2fb36 | 2008-05-28 20:59:09 +0000 | [diff] [blame] | 613 | } else if (ret != LIBMTP_ERROR_NONE) { |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 614 | LIBMTP_ERROR("LIBMTP PANIC: get_mtp_usb_device_list() " |
Linus Walleij | f106aa6 | 2008-05-04 22:55:51 +0000 | [diff] [blame] | 615 | "error code: %d on line %d\n", ret, __LINE__); |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 616 | return ret; |
Linus Walleij | b6b6913 | 2007-03-07 09:45:05 +0000 | [diff] [blame] | 617 | } |
Linus Walleij | f106aa6 | 2008-05-04 22:55:51 +0000 | [diff] [blame] | 618 | |
Linus Walleij | 63fd1e6 | 2008-04-23 23:49:43 +0000 | [diff] [blame] | 619 | // 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 Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 628 | return LIBMTP_ERROR_NONE; |
Linus Walleij | 63fd1e6 | 2008-04-23 23:49:43 +0000 | [diff] [blame] | 629 | } |
| 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 Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 636 | return LIBMTP_ERROR_MEMORY_ALLOCATION; |
Linus Walleij | 63fd1e6 | 2008-04-23 23:49:43 +0000 | [diff] [blame] | 637 | } |
| 638 | dev = devlist; |
| 639 | i = 0; |
| 640 | while (dev != NULL) { |
Linus Walleij | f106aa6 | 2008-05-04 22:55:51 +0000 | [diff] [blame] | 641 | int device_known = 0; |
| 642 | |
Linus Walleij | 63fd1e6 | 2008-04-23 23:49:43 +0000 | [diff] [blame] | 643 | // 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 Walleij | fec4d56 | 2008-06-01 22:30:36 +0000 | [diff] [blame] | 649 | // See if we can locate some additional vendor info and device flags |
Linus Walleij | f106aa6 | 2008-05-04 22:55:51 +0000 | [diff] [blame] | 650 | 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; |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 657 | |
Linus Walleij | f106aa6 | 2008-05-04 22:55:51 +0000 | [diff] [blame] | 658 | // This device is known to the developers |
Linus Walleij | 4955748 | 2010-06-22 09:14:47 +0000 | [diff] [blame] | 659 | LIBMTP_ERROR("Device %d (VID=%04x and PID=%04x) is a %s %s.\n", |
Linus Walleij | f106aa6 | 2008-05-04 22:55:51 +0000 | [diff] [blame] | 660 | 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 Walleij | f106aa6 | 2008-05-04 22:55:51 +0000 | [diff] [blame] | 665 | break; |
| 666 | } |
| 667 | } |
| 668 | if (!device_known) { |
| 669 | // This device is unknown to the developers |
Linus Walleij | 4955748 | 2010-06-22 09:14:47 +0000 | [diff] [blame] | 670 | LIBMTP_ERROR("Device %d (VID=%04x and PID=%04x) is UNKNOWN.\n", |
Linus Walleij | f106aa6 | 2008-05-04 22:55:51 +0000 | [diff] [blame] | 671 | i, |
| 672 | dev->libusb_device->descriptor.idVendor, |
| 673 | dev->libusb_device->descriptor.idProduct); |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 674 | LIBMTP_ERROR("Please report this VID/PID and the device model to the " |
Linus Walleij | f106aa6 | 2008-05-04 22:55:51 +0000 | [diff] [blame] | 675 | "libmtp development team\n"); |
Linus Walleij | fec4d56 | 2008-06-01 22:30:36 +0000 | [diff] [blame] | 676 | /* |
| 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 Walleij | f106aa6 | 2008-05-04 22:55:51 +0000 | [diff] [blame] | 681 | } |
| 682 | // Save the location on the bus |
Linus Walleij | 63fd1e6 | 2008-04-23 23:49:43 +0000 | [diff] [blame] | 683 | retdevs[i].bus_location = dev->bus_location; |
| 684 | retdevs[i].devnum = dev->libusb_device->devnum; |
| 685 | i++; |
| 686 | dev = dev->next; |
Linus Walleij | 4955748 | 2010-06-22 09:14:47 +0000 | [diff] [blame] | 687 | } |
Linus Walleij | 63fd1e6 | 2008-04-23 23:49:43 +0000 | [diff] [blame] | 688 | *devices = retdevs; |
| 689 | *numdevs = i; |
Linus Walleij | b6b6913 | 2007-03-07 09:45:05 +0000 | [diff] [blame] | 690 | free_mtpdevice_list(devlist); |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 691 | return LIBMTP_ERROR_NONE; |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 692 | } |
| 693 | |
Linus Walleij | c6210fb | 2006-05-08 11:11:41 +0000 | [diff] [blame] | 694 | /** |
| 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 | */ |
| 699 | void dump_usbinfo(PTP_USB *ptp_usb) |
| 700 | { |
Linus Walleij | c6210fb | 2006-05-08 11:11:41 +0000 | [diff] [blame] | 701 | struct usb_device *dev; |
| 702 | |
| 703 | #ifdef LIBUSB_HAS_GET_DRIVER_NP |
| 704 | char devname[0x10]; |
Richard Low | e029eba | 2009-04-11 12:36:25 +0000 | [diff] [blame] | 705 | int res; |
Linus Walleij | 4955748 | 2010-06-22 09:14:47 +0000 | [diff] [blame] | 706 | |
Linus Walleij | c6210fb | 2006-05-08 11:11:41 +0000 | [diff] [blame] | 707 | devname[0] = '\0'; |
Linus Walleij | b0ab548 | 2007-08-29 21:08:54 +0000 | [diff] [blame] | 708 | res = usb_get_driver_np(ptp_usb->handle, (int) ptp_usb->interface, devname, sizeof(devname)); |
Linus Walleij | c6210fb | 2006-05-08 11:11:41 +0000 | [diff] [blame] | 709 | if (devname[0] != '\0') { |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 710 | LIBMTP_INFO(" Using kernel interface \"%s\"\n", devname); |
Linus Walleij | c6210fb | 2006-05-08 11:11:41 +0000 | [diff] [blame] | 711 | } |
| 712 | #endif |
| 713 | dev = usb_device(ptp_usb->handle); |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 714 | 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 Walleij | 9521e2b | 2007-07-10 21:50:42 +0000 | [diff] [blame] | 731 | (void) probe_device_descriptor(dev, stdout); |
Linus Walleij | c6210fb | 2006-05-08 11:11:41 +0000 | [diff] [blame] | 732 | } |
| 733 | |
Linus Walleij | a4e6bdc | 2008-05-23 22:31:53 +0000 | [diff] [blame] | 734 | /** |
| 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 Low | e029eba | 2009-04-11 12:36:25 +0000 | [diff] [blame] | 742 | const char *get_playlist_extension(PTP_USB *ptp_usb) |
Linus Walleij | a4e6bdc | 2008-05-23 22:31:53 +0000 | [diff] [blame] | 743 | { |
| 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 Walleij | d1e14e0 | 2008-12-14 01:07:30 +0000 | [diff] [blame] | 755 | static void |
| 756 | libusb_glue_debug (PTPParams *params, const char *format, ...) |
Linus Walleij | 1473595 | 2010-04-25 04:56:49 +0000 | [diff] [blame] | 757 | { |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 758 | 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 Walleij | 1473595 | 2010-04-25 04:56:49 +0000 | [diff] [blame] | 770 | } |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 771 | |
| 772 | static void |
Linus Walleij | d1e14e0 | 2008-12-14 01:07:30 +0000 | [diff] [blame] | 773 | libusb_glue_error (PTPParams *params, const char *format, ...) |
Linus Walleij | 4955748 | 2010-06-22 09:14:47 +0000 | [diff] [blame] | 774 | { |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 775 | 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 Walleij | c6210fb | 2006-05-08 11:11:41 +0000 | [diff] [blame] | 789 | |
Linus Walleij | 99ed83c | 2007-01-02 18:46:02 +0000 | [diff] [blame] | 790 | /* |
| 791 | * ptp_read_func() and ptp_write_func() are |
Linus Walleij | c0a1143 | 2007-03-02 21:21:18 +0000 | [diff] [blame] | 792 | * based on same functions usb.c in libgphoto2. |
Linus Walleij | 99ed83c | 2007-01-02 18:46:02 +0000 | [diff] [blame] | 793 | * 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 Walleij | 4955748 | 2010-06-22 09:14:47 +0000 | [diff] [blame] | 797 | * 1. Send the command (0x0c bytes) if headers are split, else, send |
Linus Walleij | 99ed83c | 2007-01-02 18:46:02 +0000 | [diff] [blame] | 798 | * 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 Walleij | 049f50c | 2007-03-03 20:30:43 +0000 | [diff] [blame] | 806 | * |
| 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 Walleij | 99ed83c | 2007-01-02 18:46:02 +0000 | [diff] [blame] | 810 | */ |
Richard Low | 4fd59d6 | 2007-03-03 16:34:37 +0000 | [diff] [blame] | 811 | #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 Walleij | b02a066 | 2006-04-25 08:05:09 +0000 | [diff] [blame] | 814 | static short |
Linus Walleij | 784ac3f | 2006-12-29 10:36:51 +0000 | [diff] [blame] | 815 | ptp_read_func ( |
| 816 | unsigned long size, PTPDataHandler *handler,void *data, |
Richard Low | 4df32f8 | 2007-01-11 20:04:39 +0000 | [diff] [blame] | 817 | unsigned long *readbytes, |
| 818 | int readzero |
Linus Walleij | 784ac3f | 2006-12-29 10:36:51 +0000 | [diff] [blame] | 819 | ) { |
Linus Walleij | b02a066 | 2006-04-25 08:05:09 +0000 | [diff] [blame] | 820 | PTP_USB *ptp_usb = (PTP_USB *)data; |
Linus Walleij | 784ac3f | 2006-12-29 10:36:51 +0000 | [diff] [blame] | 821 | unsigned long toread = 0; |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 822 | int result = 0; |
Linus Walleij | 99ed83c | 2007-01-02 18:46:02 +0000 | [diff] [blame] | 823 | unsigned long curread = 0; |
| 824 | unsigned long written; |
Linus Walleij | 784ac3f | 2006-12-29 10:36:51 +0000 | [diff] [blame] | 825 | unsigned char *bytes; |
Linus Walleij | 049f50c | 2007-03-03 20:30:43 +0000 | [diff] [blame] | 826 | int expect_terminator_byte = 0; |
Linus Walleij | 99ed83c | 2007-01-02 18:46:02 +0000 | [diff] [blame] | 827 | |
| 828 | // This is the largest block we'll need to read in. |
Linus Walleij | 784ac3f | 2006-12-29 10:36:51 +0000 | [diff] [blame] | 829 | bytes = malloc(CONTEXT_BLOCK_SIZE); |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 830 | while (curread < size) { |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 831 | |
| 832 | LIBMTP_USB_DEBUG("Remaining size to read: 0x%04lx bytes\n", size - curread); |
| 833 | |
Richard Low | 4fd59d6 | 2007-03-03 16:34:37 +0000 | [diff] [blame] | 834 | // 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 Walleij | fec4d56 | 2008-06-01 22:30:36 +0000 | [diff] [blame] | 840 | if (readzero && FLAG_NO_ZERO_READS(ptp_usb) && toread % 64 == 0) { |
Richard Low | 4fd59d6 | 2007-03-03 16:34:37 +0000 | [diff] [blame] | 841 | toread += 1; |
Linus Walleij | 049f50c | 2007-03-03 20:30:43 +0000 | [diff] [blame] | 842 | expect_terminator_byte = 1; |
Richard Low | 4fd59d6 | 2007-03-03 16:34:37 +0000 | [diff] [blame] | 843 | } |
Linus Walleij | c49c637 | 2007-01-09 00:18:51 +0000 | [diff] [blame] | 844 | } |
Richard Low | 4fd59d6 | 2007-03-03 16:34:37 +0000 | [diff] [blame] | 845 | 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 |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 853 | LIBMTP_INFO("unexpected toread size 0x%04x, 0x%04x remaining bytes\n", |
Linus Walleij | 4cec987 | 2007-03-03 21:00:53 +0000 | [diff] [blame] | 854 | (unsigned int) toread, (unsigned int) (size-curread)); |
Linus Walleij | 784ac3f | 2006-12-29 10:36:51 +0000 | [diff] [blame] | 855 | |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 856 | LIBMTP_USB_DEBUG("Reading in 0x%04lx bytes\n", toread); |
| 857 | |
Linus Walleij | e04a1b9 | 2011-03-09 18:00:24 +0100 | [diff] [blame^] | 858 | result = USB_BULK_READ(ptp_usb->handle, |
| 859 | ptp_usb->inep, |
| 860 | (char*) bytes, |
| 861 | toread, |
| 862 | ptp_usb->timeout); |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 863 | |
| 864 | LIBMTP_USB_DEBUG("Result of read: 0x%04x\n", result); |
Linus Walleij | 4955748 | 2010-06-22 09:14:47 +0000 | [diff] [blame] | 865 | |
Linus Walleij | c49c637 | 2007-01-09 00:18:51 +0000 | [diff] [blame] | 866 | if (result < 0) { |
Richard Low | 43fdb88 | 2006-09-06 16:19:05 +0000 | [diff] [blame] | 867 | return PTP_ERROR_IO; |
Linus Walleij | c49c637 | 2007-01-09 00:18:51 +0000 | [diff] [blame] | 868 | } |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 869 | |
| 870 | LIBMTP_USB_DEBUG("<==USB IN\n"); |
Richard Low | 4df32f8 | 2007-01-11 20:04:39 +0000 | [diff] [blame] | 871 | if (result == 0) |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 872 | LIBMTP_USB_DEBUG("Zero Read\n"); |
Richard Low | 4df32f8 | 2007-01-11 20:04:39 +0000 | [diff] [blame] | 873 | else |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 874 | LIBMTP_USB_DATA(bytes, result, 16); |
Linus Walleij | 4955748 | 2010-06-22 09:14:47 +0000 | [diff] [blame] | 875 | |
Richard Low | 4fd59d6 | 2007-03-03 16:34:37 +0000 | [diff] [blame] | 876 | // want to discard extra byte |
Linus Walleij | 049f50c | 2007-03-03 20:30:43 +0000 | [diff] [blame] | 877 | if (expect_terminator_byte && result == toread) |
Richard Low | bf2675c | 2007-03-18 18:20:26 +0000 | [diff] [blame] | 878 | { |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 879 | LIBMTP_USB_DEBUG("<==USB IN\nDiscarding extra byte\n"); |
| 880 | |
Richard Low | 4fd59d6 | 2007-03-03 16:34:37 +0000 | [diff] [blame] | 881 | result--; |
Richard Low | bf2675c | 2007-03-18 18:20:26 +0000 | [diff] [blame] | 882 | } |
Linus Walleij | 4955748 | 2010-06-22 09:14:47 +0000 | [diff] [blame] | 883 | |
Linus Walleij | d866d24 | 2009-08-23 21:50:39 +0000 | [diff] [blame] | 884 | int putfunc_ret = handler->putfunc(NULL, handler->priv, result, bytes, &written); |
Richard Low | d24f923 | 2009-05-03 10:22:08 +0000 | [diff] [blame] | 885 | if (putfunc_ret != PTP_RC_OK) |
| 886 | return putfunc_ret; |
Linus Walleij | 4955748 | 2010-06-22 09:14:47 +0000 | [diff] [blame] | 887 | |
Linus Walleij | c49c637 | 2007-01-09 00:18:51 +0000 | [diff] [blame] | 888 | ptp_usb->current_transfer_complete += result; |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 889 | curread += result; |
Linus Walleij | c49c637 | 2007-01-09 00:18:51 +0000 | [diff] [blame] | 890 | |
| 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 Walleij | bd3bf9e | 2007-09-14 19:31:54 +0000 | [diff] [blame] | 899 | 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 Walleij | c49c637 | 2007-01-09 00:18:51 +0000 | [diff] [blame] | 906 | } |
Linus Walleij | 4955748 | 2010-06-22 09:14:47 +0000 | [diff] [blame] | 907 | } |
Linus Walleij | c49c637 | 2007-01-09 00:18:51 +0000 | [diff] [blame] | 908 | |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 909 | if (result < toread) /* short reads are common */ |
| 910 | break; |
| 911 | } |
Linus Walleij | 784ac3f | 2006-12-29 10:36:51 +0000 | [diff] [blame] | 912 | if (readbytes) *readbytes = curread; |
| 913 | free (bytes); |
Linus Walleij | 4955748 | 2010-06-22 09:14:47 +0000 | [diff] [blame] | 914 | |
Richard Low | 4df32f8 | 2007-01-11 20:04:39 +0000 | [diff] [blame] | 915 | // there might be a zero packet waiting for us... |
Linus Walleij | 4955748 | 2010-06-22 09:14:47 +0000 | [diff] [blame] | 916 | if (readzero && |
| 917 | !FLAG_NO_ZERO_READS(ptp_usb) && |
Linus Walleij | 049f50c | 2007-03-03 20:30:43 +0000 | [diff] [blame] | 918 | curread % ptp_usb->outep_maxpacket == 0) { |
Richard Low | 4df32f8 | 2007-01-11 20:04:39 +0000 | [diff] [blame] | 919 | char temp; |
| 920 | int zeroresult = 0; |
Linus Walleij | 049f50c | 2007-03-03 20:30:43 +0000 | [diff] [blame] | 921 | |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 922 | LIBMTP_USB_DEBUG("<==USB IN\n"); |
| 923 | LIBMTP_USB_DEBUG("Zero Read\n"); |
| 924 | |
Linus Walleij | e04a1b9 | 2011-03-09 18:00:24 +0100 | [diff] [blame^] | 925 | zeroresult = USB_BULK_READ(ptp_usb->handle, |
| 926 | ptp_usb->inep, |
| 927 | &temp, |
| 928 | 0, |
| 929 | ptp_usb->timeout); |
Richard Low | 4df32f8 | 2007-01-11 20:04:39 +0000 | [diff] [blame] | 930 | if (zeroresult != 0) |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 931 | LIBMTP_INFO("LIBMTP panic: unable to read in zero packet, response 0x%04x", zeroresult); |
Richard Low | 4df32f8 | 2007-01-11 20:04:39 +0000 | [diff] [blame] | 932 | } |
Linus Walleij | 4955748 | 2010-06-22 09:14:47 +0000 | [diff] [blame] | 933 | |
Richard Low | 4d9165f | 2008-09-23 20:13:17 +0000 | [diff] [blame] | 934 | return PTP_RC_OK; |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 935 | } |
| 936 | |
Linus Walleij | b02a066 | 2006-04-25 08:05:09 +0000 | [diff] [blame] | 937 | static short |
Linus Walleij | 784ac3f | 2006-12-29 10:36:51 +0000 | [diff] [blame] | 938 | ptp_write_func ( |
| 939 | unsigned long size, |
| 940 | PTPDataHandler *handler, |
| 941 | void *data, |
| 942 | unsigned long *written |
| 943 | ) { |
Linus Walleij | b02a066 | 2006-04-25 08:05:09 +0000 | [diff] [blame] | 944 | PTP_USB *ptp_usb = (PTP_USB *)data; |
Linus Walleij | 784ac3f | 2006-12-29 10:36:51 +0000 | [diff] [blame] | 945 | unsigned long towrite = 0; |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 946 | int result = 0; |
Linus Walleij | 784ac3f | 2006-12-29 10:36:51 +0000 | [diff] [blame] | 947 | unsigned long curwrite = 0; |
| 948 | unsigned char *bytes; |
Linus Walleij | 99ed83c | 2007-01-02 18:46:02 +0000 | [diff] [blame] | 949 | |
Linus Walleij | 4955748 | 2010-06-22 09:14:47 +0000 | [diff] [blame] | 950 | // This is the largest block we'll need to read in. |
Linus Walleij | 784ac3f | 2006-12-29 10:36:51 +0000 | [diff] [blame] | 951 | bytes = malloc(CONTEXT_BLOCK_SIZE); |
Linus Walleij | c49c637 | 2007-01-09 00:18:51 +0000 | [diff] [blame] | 952 | if (!bytes) { |
| 953 | return PTP_ERROR_IO; |
| 954 | } |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 955 | while (curwrite < size) { |
Linus Walleij | b84b385 | 2009-03-04 15:11:08 +0000 | [diff] [blame] | 956 | unsigned long usbwritten = 0; |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 957 | towrite = size-curwrite; |
Linus Walleij | c49c637 | 2007-01-09 00:18:51 +0000 | [diff] [blame] | 958 | if (towrite > CONTEXT_BLOCK_SIZE) { |
Richard Low | 43fdb88 | 2006-09-06 16:19:05 +0000 | [diff] [blame] | 959 | towrite = CONTEXT_BLOCK_SIZE; |
Linus Walleij | c49c637 | 2007-01-09 00:18:51 +0000 | [diff] [blame] | 960 | } 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 Low | 021421e | 2007-01-01 18:08:57 +0000 | [diff] [blame] | 963 | towrite -= towrite % ptp_usb->outep_maxpacket; |
Linus Walleij | c49c637 | 2007-01-09 00:18:51 +0000 | [diff] [blame] | 964 | } |
| 965 | } |
Linus Walleij | d866d24 | 2009-08-23 21:50:39 +0000 | [diff] [blame] | 966 | int getfunc_ret = handler->getfunc(NULL, handler->priv,towrite,bytes,&towrite); |
Richard Low | d24f923 | 2009-05-03 10:22:08 +0000 | [diff] [blame] | 967 | if (getfunc_ret != PTP_RC_OK) |
| 968 | return getfunc_ret; |
Linus Walleij | b84b385 | 2009-03-04 15:11:08 +0000 | [diff] [blame] | 969 | while (usbwritten < towrite) { |
Linus Walleij | e04a1b9 | 2011-03-09 18:00:24 +0100 | [diff] [blame^] | 970 | result = USB_BULK_WRITE(ptp_usb->handle, |
| 971 | ptp_usb->outep, |
| 972 | ((char*) bytes+usbwritten), |
| 973 | towrite-usbwritten, |
| 974 | ptp_usb->timeout); |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 975 | |
| 976 | LIBMTP_USB_DEBUG("USB OUT==>\n"); |
| 977 | LIBMTP_USB_DATA(bytes+usbwritten, result, 16); |
| 978 | |
Linus Walleij | b84b385 | 2009-03-04 15:11:08 +0000 | [diff] [blame] | 979 | 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 Walleij | c49c637 | 2007-01-09 00:18:51 +0000 | [diff] [blame] | 987 | } |
Linus Walleij | c49c637 | 2007-01-09 00:18:51 +0000 | [diff] [blame] | 988 | // 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 Walleij | bd3bf9e | 2007-09-14 19:31:54 +0000 | [diff] [blame] | 996 | 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 Walleij | c49c637 | 2007-01-09 00:18:51 +0000 | [diff] [blame] | 1003 | } |
| 1004 | } |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1005 | if (result < towrite) /* short writes happen */ |
| 1006 | break; |
| 1007 | } |
Linus Walleij | 784ac3f | 2006-12-29 10:36:51 +0000 | [diff] [blame] | 1008 | free (bytes); |
Linus Walleij | c49c637 | 2007-01-09 00:18:51 +0000 | [diff] [blame] | 1009 | if (written) { |
| 1010 | *written = curwrite; |
Linus Walleij | 80d134a | 2006-08-22 21:41:37 +0000 | [diff] [blame] | 1011 | } |
Linus Walleij | c49c637 | 2007-01-09 00:18:51 +0000 | [diff] [blame] | 1012 | |
Richard Low | ef05b89 | 2007-01-03 21:18:56 +0000 | [diff] [blame] | 1013 | // 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 Low | 68f4588 | 2007-01-03 10:08:31 +0000 | [diff] [blame] | 1015 | if ((towrite % ptp_usb->outep_maxpacket) == 0) { |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 1016 | |
| 1017 | LIBMTP_USB_DEBUG("USB OUT==>\n"); |
| 1018 | LIBMTP_USB_DEBUG("Zero Write\n"); |
| 1019 | |
Linus Walleij | e04a1b9 | 2011-03-09 18:00:24 +0100 | [diff] [blame^] | 1020 | result=USB_BULK_WRITE(ptp_usb->handle, |
| 1021 | ptp_usb->outep, |
| 1022 | (char *) "x", |
| 1023 | 0, |
| 1024 | ptp_usb->timeout); |
Linus Walleij | 7f0c72e | 2006-09-06 13:01:58 +0000 | [diff] [blame] | 1025 | } |
Linus Walleij | d214b9b | 2006-08-26 22:08:37 +0000 | [diff] [blame] | 1026 | } |
Linus Walleij | 4955748 | 2010-06-22 09:14:47 +0000 | [diff] [blame] | 1027 | |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1028 | if (result < 0) |
| 1029 | return PTP_ERROR_IO; |
| 1030 | return PTP_RC_OK; |
Linus Walleij | b02a066 | 2006-04-25 08:05:09 +0000 | [diff] [blame] | 1031 | } |
| 1032 | |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1033 | /* memory data get/put handler */ |
| 1034 | typedef struct { |
| 1035 | unsigned char *data; |
| 1036 | unsigned long size, curoff; |
| 1037 | } PTPMemHandlerPrivate; |
Linus Walleij | 784ac3f | 2006-12-29 10:36:51 +0000 | [diff] [blame] | 1038 | |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1039 | static uint16_t |
| 1040 | memory_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 Walleij | b02a066 | 2006-04-25 08:05:09 +0000 | [diff] [blame] | 1053 | } |
| 1054 | |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1055 | static uint16_t |
| 1056 | memory_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. */ |
| 1073 | static uint16_t |
| 1074 | ptp_init_recv_memory_handler(PTPDataHandler *handler) { |
| 1075 | PTPMemHandlerPrivate* priv; |
| 1076 | priv = malloc (sizeof(PTPMemHandlerPrivate)); |
Linus Walleij | d866d24 | 2009-08-23 21:50:39 +0000 | [diff] [blame] | 1077 | handler->priv = priv; |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1078 | 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 | */ |
| 1089 | static uint16_t |
| 1090 | ptp_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 Walleij | d866d24 | 2009-08-23 21:50:39 +0000 | [diff] [blame] | 1097 | handler->priv = priv; |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1098 | 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 */ |
| 1107 | static uint16_t |
| 1108 | ptp_exit_send_memory_handler (PTPDataHandler *handler) { |
Linus Walleij | d866d24 | 2009-08-23 21:50:39 +0000 | [diff] [blame] | 1109 | PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)handler->priv; |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1110 | /* data is owned by caller */ |
| 1111 | free (priv); |
| 1112 | return PTP_RC_OK; |
| 1113 | } |
| 1114 | |
| 1115 | /* hand over our internal data to caller */ |
| 1116 | static uint16_t |
| 1117 | ptp_exit_recv_memory_handler (PTPDataHandler *handler, |
| 1118 | unsigned char **data, unsigned long *size |
| 1119 | ) { |
Linus Walleij | d866d24 | 2009-08-23 21:50:39 +0000 | [diff] [blame] | 1120 | PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)handler->priv; |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1121 | *data = priv->data; |
| 1122 | *size = priv->size; |
| 1123 | free (priv); |
| 1124 | return PTP_RC_OK; |
| 1125 | } |
| 1126 | |
| 1127 | /* send / receive functions */ |
| 1128 | |
| 1129 | uint16_t |
| 1130 | ptp_usb_sendreq (PTPParams* params, PTPContainer* req) |
| 1131 | { |
| 1132 | uint16_t ret; |
| 1133 | PTPUSBBulkContainer usbreq; |
| 1134 | PTPDataHandler memhandler; |
Linus Walleij | 5ba4040 | 2008-05-23 21:36:54 +0000 | [diff] [blame] | 1135 | unsigned long written = 0; |
| 1136 | unsigned long towrite; |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 1137 | |
Linus Walleij | b8f78c7 | 2008-03-02 20:22:56 +0000 | [diff] [blame] | 1138 | char txt[256]; |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1139 | |
Linus Walleij | b8f78c7 | 2008-03-02 20:22:56 +0000 | [diff] [blame] | 1140 | (void) ptp_render_opcode (params, req->Code, sizeof(txt), txt); |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 1141 | LIBMTP_USB_DEBUG("REQUEST: 0x%04x, %s\n", req->Code, txt); |
| 1142 | |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1143 | /* 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 Walleij | ff01cb1 | 2007-09-16 19:49:48 +0000 | [diff] [blame] | 1164 | if (ret!=PTP_RC_OK && ret!=PTP_ERROR_CANCEL) { |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1165 | ret = PTP_ERROR_IO; |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1166 | } |
Linus Walleij | 724f010 | 2007-09-17 20:10:12 +0000 | [diff] [blame] | 1167 | if (written != towrite && ret != PTP_ERROR_CANCEL && ret != PTP_ERROR_IO) { |
Linus Walleij | 4955748 | 2010-06-22 09:14:47 +0000 | [diff] [blame] | 1168 | libusb_glue_error (params, |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1169 | "PTP: request code 0x%04x sending req wrote only %ld bytes instead of %d", |
Richard Low | d284f07 | 2007-02-17 08:52:41 +0000 | [diff] [blame] | 1170 | req->Code, written, towrite |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1171 | ); |
| 1172 | ret = PTP_ERROR_IO; |
| 1173 | } |
| 1174 | return ret; |
| 1175 | } |
| 1176 | |
| 1177 | uint16_t |
| 1178 | ptp_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 | |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 1188 | |
| 1189 | LIBMTP_USB_DEBUG("SEND DATA PHASE\n"); |
| 1190 | |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1191 | /* 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 Walleij | 1473595 | 2010-04-25 04:56:49 +0000 | [diff] [blame] | 1196 | |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1197 | ((PTP_USB*)params->data)->current_transfer_complete = 0; |
Richard Low | 4eb0112 | 2007-02-24 10:11:32 +0000 | [diff] [blame] | 1198 | ((PTP_USB*)params->data)->current_transfer_total = size+PTP_USB_BULK_HDR_LEN; |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1199 | |
| 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 Low | 02724f6 | 2007-02-17 09:47:26 +0000 | [diff] [blame] | 1206 | datawlen = (size<PTP_USB_BULK_PAYLOAD_LEN_WRITE)?size:PTP_USB_BULK_PAYLOAD_LEN_WRITE; |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1207 | wlen = PTP_USB_BULK_HDR_LEN + datawlen; |
Linus Walleij | 1473595 | 2010-04-25 04:56:49 +0000 | [diff] [blame] | 1208 | |
Linus Walleij | d866d24 | 2009-08-23 21:50:39 +0000 | [diff] [blame] | 1209 | ret = handler->getfunc(params, handler->priv, datawlen, usbdata.payload.data, &gotlen); |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1210 | 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 Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1220 | 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 Walleij | ff01cb1 | 2007-09-16 19:49:48 +0000 | [diff] [blame] | 1236 | if (ret!=PTP_RC_OK && ret!=PTP_ERROR_CANCEL) |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1237 | ret = PTP_ERROR_IO; |
| 1238 | return ret; |
| 1239 | } |
| 1240 | |
| 1241 | static 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 Low | 02724f6 | 2007-02-17 09:47:26 +0000 | [diff] [blame] | 1260 | ret = ptp_read_func(PTP_USB_BULK_HS_MAX_PACKET_LEN_READ, &memhandler, params->data, rlen, 0); |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1261 | 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 | |
| 1269 | uint16_t |
| 1270 | ptp_usb_getdata (PTPParams* params, PTPContainer* ptp, PTPDataHandler *handler) |
| 1271 | { |
| 1272 | uint16_t ret; |
| 1273 | PTPUSBBulkContainer usbdata; |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1274 | unsigned long written; |
Linus Walleij | 8e3af00 | 2007-09-28 19:21:54 +0000 | [diff] [blame] | 1275 | PTP_USB *ptp_usb = (PTP_USB *) params->data; |
Linus Walleij | 1473595 | 2010-04-25 04:56:49 +0000 | [diff] [blame] | 1276 | int putfunc_ret; |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 1277 | |
| 1278 | LIBMTP_USB_DEBUG("GET DATA PHASE\n"); |
| 1279 | |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1280 | 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 Walleij | 8e3af00 | 2007-09-28 19:21:54 +0000 | [diff] [blame] | 1288 | } |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1289 | if (dtoh16(usbdata.type)!=PTP_USB_CONTAINER_DATA) { |
| 1290 | ret = PTP_ERROR_DATA_EXPECTED; |
| 1291 | break; |
Linus Walleij | 8e3af00 | 2007-09-28 19:21:54 +0000 | [diff] [blame] | 1292 | } |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1293 | if (dtoh16(usbdata.code)!=ptp->Code) { |
Linus Walleij | fec4d56 | 2008-06-01 22:30:36 +0000 | [diff] [blame] | 1294 | if (FLAG_IGNORE_HEADER_ERRORS(ptp_usb)) { |
Linus Walleij | d1e14e0 | 2008-12-14 01:07:30 +0000 | [diff] [blame] | 1295 | libusb_glue_debug (params, "ptp2/ptp_usb_getdata: detected a broken " |
Linus Walleij | 913a306 | 2007-09-28 21:42:39 +0000 | [diff] [blame] | 1296 | "PTP header, code field insane, expect problems! (But continuing)"); |
Linus Walleij | 0654212 | 2007-10-14 22:13:48 +0000 | [diff] [blame] | 1297 | // 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 Walleij | 94cd972 | 2007-09-28 21:29:53 +0000 | [diff] [blame] | 1301 | ret = PTP_RC_OK; |
Linus Walleij | 8e3af00 | 2007-09-28 19:21:54 +0000 | [diff] [blame] | 1302 | } else { |
Linus Walleij | 913a306 | 2007-09-28 21:42:39 +0000 | [diff] [blame] | 1303 | 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 Walleij | 4955748 | 2010-06-22 09:14:47 +0000 | [diff] [blame] | 1306 | // getting data. It appears Windows ignores the contents of this |
Linus Walleij | 913a306 | 2007-09-28 21:42:39 +0000 | [diff] [blame] | 1307 | // field entirely. |
| 1308 | if (ret < PTP_RC_Undefined || ret > PTP_RC_SpecificationOfDestinationUnsupported) { |
Linus Walleij | d1e14e0 | 2008-12-14 01:07:30 +0000 | [diff] [blame] | 1309 | libusb_glue_debug (params, "ptp2/ptp_usb_getdata: detected a broken " |
Linus Walleij | 913a306 | 2007-09-28 21:42:39 +0000 | [diff] [blame] | 1310 | "PTP header, code field insane."); |
| 1311 | ret = PTP_ERROR_IO; |
| 1312 | } |
Linus Walleij | 8e3af00 | 2007-09-28 19:21:54 +0000 | [diff] [blame] | 1313 | break; |
| 1314 | } |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1315 | } |
| 1316 | if (usbdata.length == 0xffffffffU) { |
Linus Walleij | 1473595 | 2010-04-25 04:56:49 +0000 | [diff] [blame] | 1317 | /* 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 Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1325 | |
Linus Walleij | 1473595 | 2010-04-25 04:56:49 +0000 | [diff] [blame] | 1326 | /* 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 Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1344 | } |
| 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; |
tedbullock | 36f2afb | 2007-03-03 22:16:44 +0000 | [diff] [blame] | 1366 | /* Ignore reading one extra byte if device flags have been set */ |
Linus Walleij | fec4d56 | 2008-06-01 22:30:36 +0000 | [diff] [blame] | 1367 | } else if(!FLAG_NO_ZERO_READS(ptp_usb) && |
| 1368 | (rlen - dtoh32(usbdata.length) == 1)) { |
Linus Walleij | d1e14e0 | 2008-12-14 01:07:30 +0000 | [diff] [blame] | 1369 | libusb_glue_debug (params, "ptp2/ptp_usb_getdata: read %d bytes " |
Linus Walleij | 4955748 | 2010-06-22 09:14:47 +0000 | [diff] [blame] | 1370 | "too much, expect problems!", |
Linus Walleij | 8e3af00 | 2007-09-28 19:21:54 +0000 | [diff] [blame] | 1371 | rlen - dtoh32(usbdata.length)); |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1372 | } |
| 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 Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1387 | /* Copy first part of data to 'data' */ |
Linus Walleij | 1473595 | 2010-04-25 04:56:49 +0000 | [diff] [blame] | 1388 | 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 Walleij | d866d24 | 2009-08-23 21:50:39 +0000 | [diff] [blame] | 1396 | |
| 1397 | if (FLAG_NO_ZERO_READS(ptp_usb) && |
Linus Walleij | 049f50c | 2007-03-03 20:30:43 +0000 | [diff] [blame] | 1398 | len+PTP_USB_BULK_HDR_LEN == PTP_USB_BULK_HS_MAX_PACKET_LEN_READ) { |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 1399 | |
| 1400 | LIBMTP_USB_DEBUG("Reading in extra terminating byte\n"); |
| 1401 | |
Linus Walleij | 049f50c | 2007-03-03 20:30:43 +0000 | [diff] [blame] | 1402 | // need to read in extra byte and discard it |
| 1403 | int result = 0; |
| 1404 | char byte = 0; |
Linus Walleij | e04a1b9 | 2011-03-09 18:00:24 +0100 | [diff] [blame^] | 1405 | result = USB_BULK_READ(ptp_usb->handle, |
| 1406 | ptp_usb->inep, |
| 1407 | &byte, |
| 1408 | 1, |
| 1409 | ptp_usb->timeout); |
Linus Walleij | d866d24 | 2009-08-23 21:50:39 +0000 | [diff] [blame] | 1410 | |
Linus Walleij | 049f50c | 2007-03-03 20:30:43 +0000 | [diff] [blame] | 1411 | if (result != 1) |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 1412 | 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 Low | 057ea77 | 2007-03-24 13:37:38 +0000 | [diff] [blame] | 1413 | } else if (len+PTP_USB_BULK_HDR_LEN == PTP_USB_BULK_HS_MAX_PACKET_LEN_READ && params->split_header_data == 0) { |
Linus Walleij | 94cd972 | 2007-09-28 21:29:53 +0000 | [diff] [blame] | 1414 | int zeroresult = 0; |
| 1415 | char zerobyte = 0; |
| 1416 | |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 1417 | |
| 1418 | LIBMTP_INFO("Reading in zero packet after header\n"); |
| 1419 | |
Linus Walleij | e04a1b9 | 2011-03-09 18:00:24 +0100 | [diff] [blame^] | 1420 | zeroresult = USB_BULK_READ(ptp_usb->handle, |
| 1421 | ptp_usb->inep, |
| 1422 | &zerobyte, |
| 1423 | 0, |
| 1424 | ptp_usb->timeout); |
Linus Walleij | 5f3c44b | 2009-09-12 21:03:06 +0000 | [diff] [blame] | 1425 | |
Linus Walleij | 94cd972 | 2007-09-28 21:29:53 +0000 | [diff] [blame] | 1426 | if (zeroresult != 0) |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 1427 | LIBMTP_INFO("LIBMTP panic: unable to read in zero packet, response 0x%04x", zeroresult); |
Linus Walleij | 94cd972 | 2007-09-28 21:29:53 +0000 | [diff] [blame] | 1428 | } |
Linus Walleij | 5f3c44b | 2009-09-12 21:03:06 +0000 | [diff] [blame] | 1429 | |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1430 | /* Is that all of data? */ |
Linus Walleij | 94cd972 | 2007-09-28 21:29:53 +0000 | [diff] [blame] | 1431 | if (len+PTP_USB_BULK_HDR_LEN<=rlen) { |
| 1432 | break; |
| 1433 | } |
Linus Walleij | 5f3c44b | 2009-09-12 21:03:06 +0000 | [diff] [blame] | 1434 | |
Linus Walleij | 913a306 | 2007-09-28 21:42:39 +0000 | [diff] [blame] | 1435 | ret = ptp_read_func(len - (rlen - PTP_USB_BULK_HDR_LEN), |
| 1436 | handler, |
| 1437 | params->data, &rlen, 1); |
Linus Walleij | 5f3c44b | 2009-09-12 21:03:06 +0000 | [diff] [blame] | 1438 | |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1439 | if (ret!=PTP_RC_OK) { |
Linus Walleij | 049f50c | 2007-03-03 20:30:43 +0000 | [diff] [blame] | 1440 | break; |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1441 | } |
| 1442 | } while (0); |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1443 | return ret; |
| 1444 | } |
| 1445 | |
| 1446 | uint16_t |
| 1447 | ptp_usb_getresp (PTPParams* params, PTPContainer* resp) |
| 1448 | { |
| 1449 | uint16_t ret; |
| 1450 | unsigned long rlen; |
| 1451 | PTPUSBBulkContainer usbresp; |
Linus Walleij | 3e4b114 | 2007-11-01 23:54:16 +0000 | [diff] [blame] | 1452 | PTP_USB *ptp_usb = (PTP_USB *)(params->data); |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1453 | |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 1454 | |
| 1455 | LIBMTP_USB_DEBUG("RESPONSE: "); |
| 1456 | |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1457 | 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 Walleij | b8f78c7 | 2008-03-02 20:22:56 +0000 | [diff] [blame] | 1461 | // 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 Walleij | 3191bba | 2008-02-10 21:16:25 +0000 | [diff] [blame] | 1465 | while (ret==PTP_RC_OK && rlen<PTP_USB_BULK_HDR_LEN && usbresp.length==0) { |
Linus Walleij | d1e14e0 | 2008-12-14 01:07:30 +0000 | [diff] [blame] | 1466 | libusb_glue_debug (params, "ptp_usb_getresp: detected short response " |
Linus Walleij | 3191bba | 2008-02-10 21:16:25 +0000 | [diff] [blame] | 1467 | "of %d bytes, expect problems! (re-reading " |
| 1468 | "response), rlen"); |
Linus Walleij | 00411ee | 2008-01-27 12:24:51 +0000 | [diff] [blame] | 1469 | ret = ptp_usb_getpacket(params, &usbresp, &rlen); |
| 1470 | } |
| 1471 | |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1472 | 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 | } |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 1481 | |
| 1482 | LIBMTP_USB_DEBUG("%04x\n", ret); |
| 1483 | |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1484 | if (ret!=PTP_RC_OK) { |
Linus Walleij | d1e14e0 | 2008-12-14 01:07:30 +0000 | [diff] [blame] | 1485 | /* libusb_glue_error (params, |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1486 | "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 Walleij | fec4d56 | 2008-06-01 22:30:36 +0000 | [diff] [blame] | 1494 | if (FLAG_IGNORE_HEADER_ERRORS(ptp_usb)) { |
Linus Walleij | 3e4b114 | 2007-11-01 23:54:16 +0000 | [diff] [blame] | 1495 | if (resp->Transaction_ID != params->transaction_id-1) { |
Linus Walleij | d1e14e0 | 2008-12-14 01:07:30 +0000 | [diff] [blame] | 1496 | libusb_glue_debug (params, "ptp_usb_getresp: detected a broken " |
Linus Walleij | 00411ee | 2008-01-27 12:24:51 +0000 | [diff] [blame] | 1497 | "PTP header, transaction ID insane, expect " |
| 1498 | "problems! (But continuing)"); |
Linus Walleij | 3e4b114 | 2007-11-01 23:54:16 +0000 | [diff] [blame] | 1499 | // Repair the header, so it won't wreak more havoc. |
| 1500 | resp->Transaction_ID = params->transaction_id-1; |
| 1501 | } |
| 1502 | } |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1503 | 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 | |
| 1517 | static inline uint16_t |
| 1518 | ptp_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 Walleij | 5f3c44b | 2009-09-12 21:03:06 +0000 | [diff] [blame] | 1528 | if ((params==NULL) || (event==NULL)) |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1529 | return PTP_ERROR_BADPARAM; |
| 1530 | ret = PTP_RC_OK; |
| 1531 | switch(wait) { |
| 1532 | case PTP_EVENT_CHECK: |
Linus Walleij | e04a1b9 | 2011-03-09 18:00:24 +0100 | [diff] [blame^] | 1533 | result=USB_BULK_READ(ptp_usb->handle, |
| 1534 | ptp_usb->intep, |
| 1535 | (char *) &usbevent, |
| 1536 | sizeof(usbevent), |
| 1537 | ptp_usb->timeout); |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1538 | if (result==0) |
Linus Walleij | e04a1b9 | 2011-03-09 18:00:24 +0100 | [diff] [blame^] | 1539 | result = USB_BULK_READ(ptp_usb->handle, |
| 1540 | ptp_usb->intep, |
| 1541 | (char *) &usbevent, |
| 1542 | sizeof(usbevent), |
| 1543 | ptp_usb->timeout); |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1544 | if (result < 0) ret = PTP_ERROR_IO; |
| 1545 | break; |
| 1546 | case PTP_EVENT_CHECK_FAST: |
Linus Walleij | e04a1b9 | 2011-03-09 18:00:24 +0100 | [diff] [blame^] | 1547 | result=USB_BULK_READ(ptp_usb->handle, |
| 1548 | ptp_usb->intep, |
| 1549 | (char *) &usbevent, |
| 1550 | sizeof(usbevent), |
| 1551 | ptp_usb->timeout); |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1552 | if (result==0) |
Linus Walleij | e04a1b9 | 2011-03-09 18:00:24 +0100 | [diff] [blame^] | 1553 | result = USB_BULK_READ(ptp_usb->handle, |
| 1554 | ptp_usb->intep, |
| 1555 | (char *) &usbevent, |
| 1556 | sizeof(usbevent), |
| 1557 | ptp_usb->timeout); |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1558 | 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 Walleij | d1e14e0 | 2008-12-14 01:07:30 +0000 | [diff] [blame] | 1565 | libusb_glue_error (params, |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1566 | "PTP: reading event an error 0x%04x occurred", ret); |
| 1567 | return PTP_ERROR_IO; |
| 1568 | } |
| 1569 | rlen = result; |
| 1570 | if (rlen < 8) { |
Linus Walleij | d1e14e0 | 2008-12-14 01:07:30 +0000 | [diff] [blame] | 1571 | libusb_glue_error (params, |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1572 | "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 | |
| 1586 | uint16_t |
| 1587 | ptp_usb_event_check (PTPParams* params, PTPContainer* event) { |
| 1588 | |
| 1589 | return ptp_usb_event (params, event, PTP_EVENT_CHECK_FAST); |
| 1590 | } |
| 1591 | |
| 1592 | uint16_t |
| 1593 | ptp_usb_event_wait (PTPParams* params, PTPContainer* event) { |
| 1594 | |
| 1595 | return ptp_usb_event (params, event, PTP_EVENT_CHECK); |
| 1596 | } |
| 1597 | |
Linus Walleij | bd3bf9e | 2007-09-14 19:31:54 +0000 | [diff] [blame] | 1598 | uint16_t |
| 1599 | ptp_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 Walleij | 5f3c44b | 2009-09-12 21:03:06 +0000 | [diff] [blame] | 1606 | ret = usb_control_msg(ptp_usb->handle, |
Linus Walleij | bd3bf9e | 2007-09-14 19:31:54 +0000 | [diff] [blame] | 1607 | USB_TYPE_CLASS | USB_RECIP_INTERFACE, |
Linus Walleij | e04a1b9 | 2011-03-09 18:00:24 +0100 | [diff] [blame^] | 1608 | 0x64, 0x0000, 0x0000, |
| 1609 | (char *) buffer, |
| 1610 | sizeof(buffer), |
| 1611 | ptp_usb->timeout); |
Linus Walleij | bd3bf9e | 2007-09-14 19:31:54 +0000 | [diff] [blame] | 1612 | if (ret < sizeof(buffer)) |
| 1613 | return PTP_ERROR_IO; |
| 1614 | return PTP_RC_OK; |
| 1615 | } |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1616 | |
Linus Walleij | 9eb3d31 | 2006-08-04 19:25:59 +0000 | [diff] [blame] | 1617 | static int init_ptp_usb (PTPParams* params, PTP_USB* ptp_usb, struct usb_device* dev) |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1618 | { |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1619 | usb_dev_handle *device_handle; |
Linus Walleij | fea4f53 | 2009-09-22 22:08:35 +0000 | [diff] [blame] | 1620 | char buf[255]; |
Linus Walleij | f2d747a | 2010-01-19 00:46:18 +0000 | [diff] [blame] | 1621 | int usbresult; |
Linus Walleij | 5f3c44b | 2009-09-12 21:03:06 +0000 | [diff] [blame] | 1622 | |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1623 | 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 Walleij | ff01cb1 | 2007-09-16 19:49:48 +0000 | [diff] [blame] | 1627 | params->cancelreq_func=ptp_usb_control_cancel_request; |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1628 | params->data=ptp_usb; |
| 1629 | params->transaction_id=0; |
Linus Walleij | 462a5e7 | 2007-02-13 06:54:56 +0000 | [diff] [blame] | 1630 | /* |
| 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 Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1634 | params->byteorder = PTP_DL_LE; |
Linus Walleij | 5f3c44b | 2009-09-12 21:03:06 +0000 | [diff] [blame] | 1635 | |
Linus Walleij | e04a1b9 | 2011-03-09 18:00:24 +0100 | [diff] [blame^] | 1636 | ptp_usb->timeout = get_timeout(ptp_usb); |
Linus Walleij | 5f3c44b | 2009-09-12 21:03:06 +0000 | [diff] [blame] | 1637 | |
Yavor Goulishev | 825ff13 | 2011-02-26 00:29:01 +0100 | [diff] [blame] | 1638 | device_handle = usb_open(dev); |
| 1639 | if (!device_handle) { |
| 1640 | perror("usb_open()"); |
| 1641 | return -1; |
| 1642 | } |
| 1643 | ptp_usb->handle = device_handle; |
Linus Walleij | 501ba4d | 2006-10-16 06:17:04 +0000 | [diff] [blame] | 1644 | #ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP |
Yavor Goulishev | 825ff13 | 2011-02-26 00:29:01 +0100 | [diff] [blame] | 1645 | /* |
| 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 Walleij | 501ba4d | 2006-10-16 06:17:04 +0000 | [diff] [blame] | 1657 | } |
| 1658 | } |
Yavor Goulishev | 825ff13 | 2011-02-26 00:29:01 +0100 | [diff] [blame] | 1659 | } |
Linus Walleij | 501ba4d | 2006-10-16 06:17:04 +0000 | [diff] [blame] | 1660 | #endif |
Linus Walleij | eac1e00 | 2006-11-30 22:06:02 +0000 | [diff] [blame] | 1661 | #ifdef __WIN32__ |
Yavor Goulishev | 825ff13 | 2011-02-26 00:29:01 +0100 | [diff] [blame] | 1662 | // 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 Walleij | eac1e00 | 2006-11-30 22:06:02 +0000 | [diff] [blame] | 1667 | #endif |
Yavor Goulishev | 825ff13 | 2011-02-26 00:29:01 +0100 | [diff] [blame] | 1668 | // 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 Walleij | f2d747a | 2010-01-19 00:46:18 +0000 | [diff] [blame] | 1672 | |
Yavor Goulishev | 825ff13 | 2011-02-26 00:29:01 +0100 | [diff] [blame] | 1673 | if (usbresult != 0) |
| 1674 | fprintf(stderr, "ignoring usb_claim_interface = %d", usbresult); |
Linus Walleij | f2d747a | 2010-01-19 00:46:18 +0000 | [diff] [blame] | 1675 | |
Yavor Goulishev | 825ff13 | 2011-02-26 00:29:01 +0100 | [diff] [blame] | 1676 | // 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 Walleij | f2d747a | 2010-01-19 00:46:18 +0000 | [diff] [blame] | 1688 | |
Yavor Goulishev | 825ff13 | 2011-02-26 00:29:01 +0100 | [diff] [blame] | 1689 | if (FLAG_SWITCH_MODE_BLACKBERRY(ptp_usb)) { |
| 1690 | int ret; |
Linus Walleij | b6e06e2 | 2009-09-22 22:20:49 +0000 | [diff] [blame] | 1691 | |
Yavor Goulishev | 825ff13 | 2011-02-26 00:29:01 +0100 | [diff] [blame] | 1692 | // 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); |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 1701 | |
Yavor Goulishev | 825ff13 | 2011-02-26 00:29:01 +0100 | [diff] [blame] | 1702 | 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); |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 1709 | |
Yavor Goulishev | 825ff13 | 2011-02-26 00:29:01 +0100 | [diff] [blame] | 1710 | 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); |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 1717 | |
Yavor Goulishev | 825ff13 | 2011-02-26 00:29:01 +0100 | [diff] [blame] | 1718 | 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); |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 1725 | |
Yavor Goulishev | 825ff13 | 2011-02-26 00:29:01 +0100 | [diff] [blame] | 1726 | usleep(1000); |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1727 | } |
Linus Walleij | 9eb3d31 | 2006-08-04 19:25:59 +0000 | [diff] [blame] | 1728 | return 0; |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1729 | } |
| 1730 | |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1731 | static void clear_stall(PTP_USB* ptp_usb) |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1732 | { |
Linus Walleij | 58b6279 | 2007-01-07 12:38:59 +0000 | [diff] [blame] | 1733 | uint16_t status; |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1734 | int ret; |
Linus Walleij | 5f3c44b | 2009-09-12 21:03:06 +0000 | [diff] [blame] | 1735 | |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1736 | /* check the inep status */ |
Linus Walleij | 58b6279 | 2007-01-07 12:38:59 +0000 | [diff] [blame] | 1737 | status = 0; |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 1738 | ret = usb_get_endpoint_status(ptp_usb,ptp_usb->inep,&status); |
Linus Walleij | 58b6279 | 2007-01-07 12:38:59 +0000 | [diff] [blame] | 1739 | if (ret<0) { |
| 1740 | perror ("inep: usb_get_endpoint_status()"); |
| 1741 | } else if (status) { |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 1742 | LIBMTP_INFO("Clearing stall on IN endpoint\n"); |
Linus Walleij | 58b6279 | 2007-01-07 12:38:59 +0000 | [diff] [blame] | 1743 | ret = usb_clear_stall_feature(ptp_usb,ptp_usb->inep); |
| 1744 | if (ret<0) { |
| 1745 | perror ("usb_clear_stall_feature()"); |
| 1746 | } |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1747 | } |
Linus Walleij | 5f3c44b | 2009-09-12 21:03:06 +0000 | [diff] [blame] | 1748 | |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1749 | /* check the outep status */ |
Linus Walleij | 58b6279 | 2007-01-07 12:38:59 +0000 | [diff] [blame] | 1750 | 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) { |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 1755 | LIBMTP_INFO("Clearing stall on OUT endpoint\n"); |
Linus Walleij | 58b6279 | 2007-01-07 12:38:59 +0000 | [diff] [blame] | 1756 | ret = usb_clear_stall_feature(ptp_usb,ptp_usb->outep); |
| 1757 | if (ret<0) { |
| 1758 | perror("usb_clear_stall_feature()"); |
| 1759 | } |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1760 | } |
Linus Walleij | 58b6279 | 2007-01-07 12:38:59 +0000 | [diff] [blame] | 1761 | |
| 1762 | /* TODO: do we need this for INTERRUPT (ptp_usb->intep) too? */ |
| 1763 | } |
| 1764 | |
| 1765 | static 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 Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1781 | } |
| 1782 | |
Linus Walleij | b0ab548 | 2007-08-29 21:08:54 +0000 | [diff] [blame] | 1783 | static void close_usb(PTP_USB* ptp_usb) |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1784 | { |
Linus Walleij | da17cda | 2007-09-18 21:12:05 +0000 | [diff] [blame] | 1785 | // Commented out since it was confusing some |
| 1786 | // devices to do these things. |
Linus Walleij | fec4d56 | 2008-06-01 22:30:36 +0000 | [diff] [blame] | 1787 | if (!FLAG_NO_RELEASE_INTERFACE(ptp_usb)) { |
Linus Walleij | eabc231 | 2008-02-13 00:02:00 +0000 | [diff] [blame] | 1788 | /* |
| 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 Walleij | 5f3c44b | 2009-09-12 21:03:06 +0000 | [diff] [blame] | 1793 | * (...) 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 Walleij | eabc231 | 2008-02-13 00:02:00 +0000 | [diff] [blame] | 1796 | * STALL is persistant or not). |
| 1797 | */ |
Linus Walleij | 29f03ba | 2007-09-25 19:22:12 +0000 | [diff] [blame] | 1798 | 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 Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1807 | usb_close(ptp_usb->handle); |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1808 | } |
| 1809 | |
Linus Walleij | 550d6d5 | 2007-01-24 14:32:51 +0000 | [diff] [blame] | 1810 | /** |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 1811 | * Self-explanatory? |
| 1812 | */ |
Linus Walleij | 5f3c44b | 2009-09-12 21:03:06 +0000 | [diff] [blame] | 1813 | static 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 Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1820 | { |
Linus Walleij | d7373cd | 2007-08-28 21:42:05 +0000 | [diff] [blame] | 1821 | int i; |
| 1822 | |
| 1823 | // Loop over the device configurations |
| 1824 | for (i = 0; i < dev->descriptor.bNumConfigurations; i++) { |
Linus Walleij | b0ab548 | 2007-08-29 21:08:54 +0000 | [diff] [blame] | 1825 | uint8_t j; |
Linus Walleij | d7373cd | 2007-08-28 21:42:05 +0000 | [diff] [blame] | 1826 | |
Linus Walleij | 5f3c44b | 2009-09-12 21:03:06 +0000 | [diff] [blame] | 1827 | // Loop over each configurations interfaces |
Linus Walleij | d7373cd | 2007-08-28 21:42:05 +0000 | [diff] [blame] | 1828 | for (j = 0; j < dev->config[i].bNumInterfaces; j++) { |
Linus Walleij | b0ab548 | 2007-08-29 21:08:54 +0000 | [diff] [blame] | 1829 | uint8_t k; |
| 1830 | uint8_t no_ep; |
Linus Walleij | 5f3c44b | 2009-09-12 21:03:06 +0000 | [diff] [blame] | 1831 | int found_inep = 0; |
| 1832 | int found_outep = 0; |
| 1833 | int found_intep = 0; |
Linus Walleij | d7373cd | 2007-08-28 21:42:05 +0000 | [diff] [blame] | 1834 | struct usb_endpoint_descriptor *ep; |
Linus Walleij | 5f3c44b | 2009-09-12 21:03:06 +0000 | [diff] [blame] | 1835 | |
| 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 Walleij | b0ab548 | 2007-08-29 21:08:54 +0000 | [diff] [blame] | 1841 | |
| 1842 | *interface = dev->config[i].interface[j].altsetting->bInterfaceNumber; |
| 1843 | ep = dev->config[i].interface[j].altsetting->endpoint; |
Linus Walleij | 5f3c44b | 2009-09-12 21:03:06 +0000 | [diff] [blame] | 1844 | |
| 1845 | // Loop over the three endpoints to locate two bulk and |
| 1846 | // one interrupt endpoint and FAIL if we cannot, and continue. |
Linus Walleij | b0ab548 | 2007-08-29 21:08:54 +0000 | [diff] [blame] | 1847 | for (k = 0; k < no_ep; k++) { |
Linus Walleij | e73854e | 2010-06-22 09:07:34 +0000 | [diff] [blame] | 1848 | 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 Walleij | 48fda57 | 2010-06-22 09:09:03 +0000 | [diff] [blame] | 1856 | *outep = ep[k].bEndpointAddress; |
| 1857 | *outep_maxpacket = ep[k].wMaxPacketSize; |
Linus Walleij | e73854e | 2010-06-22 09:07:34 +0000 | [diff] [blame] | 1858 | 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 Walleij | 48fda57 | 2010-06-22 09:09:03 +0000 | [diff] [blame] | 1863 | *intep = ep[k].bEndpointAddress; |
Linus Walleij | e73854e | 2010-06-22 09:07:34 +0000 | [diff] [blame] | 1864 | found_intep = 1; |
| 1865 | } |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1866 | } |
Linus Walleij | d7373cd | 2007-08-28 21:42:05 +0000 | [diff] [blame] | 1867 | } |
Linus Walleij | 5f3c44b | 2009-09-12 21:03:06 +0000 | [diff] [blame] | 1868 | 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 Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1872 | } |
| 1873 | } |
Linus Walleij | 5f3c44b | 2009-09-12 21:03:06 +0000 | [diff] [blame] | 1874 | return -1; |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1875 | } |
| 1876 | |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 1877 | /** |
| 1878 | * This function assigns params and usbinfo given a raw device |
| 1879 | * as input. |
| 1880 | * @param device the device to be assigned. |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 1881 | * @param usbinfo a pointer to the new usbinfo. |
| 1882 | * @return an error code. |
| 1883 | */ |
Linus Walleij | 4955748 | 2010-06-22 09:14:47 +0000 | [diff] [blame] | 1884 | LIBMTP_error_number_t configure_usb_device(LIBMTP_raw_device_t *device, |
Linus Walleij | 3c64325 | 2008-05-31 23:22:28 +0000 | [diff] [blame] | 1885 | PTPParams *params, |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 1886 | void **usbinfo) |
| 1887 | { |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 1888 | PTP_USB *ptp_usb; |
| 1889 | struct usb_device *libusb_device; |
| 1890 | uint16_t ret = 0; |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 1891 | struct usb_bus *bus; |
| 1892 | int found = 0; |
Linus Walleij | 5f3c44b | 2009-09-12 21:03:06 +0000 | [diff] [blame] | 1893 | int err; |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 1894 | |
| 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 Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 1920 | ptp_usb = (PTP_USB *) malloc(sizeof(PTP_USB)); |
Linus Walleij | 3c64325 | 2008-05-31 23:22:28 +0000 | [diff] [blame] | 1921 | if (ptp_usb == NULL) { |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 1922 | return LIBMTP_ERROR_MEMORY_ALLOCATION; |
| 1923 | } |
| 1924 | /* Start with a blank slate (includes setting device_flags to 0) */ |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 1925 | memset(ptp_usb, 0, sizeof(PTP_USB)); |
| 1926 | |
Linus Walleij | fec4d56 | 2008-06-01 22:30:36 +0000 | [diff] [blame] | 1927 | /* Copy the raw device */ |
| 1928 | memcpy(&ptp_usb->rawdevice, device, sizeof(LIBMTP_raw_device_t)); |
Linus Walleij | 335a81c | 2008-06-02 23:01:00 +0000 | [diff] [blame] | 1929 | |
| 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 Walleij | 5f3c44b | 2009-09-12 21:03:06 +0000 | [diff] [blame] | 1938 | |
| 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) { |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 1949 | LIBMTP_ERROR("LIBMTP PANIC: Unable to find interface & endpoints of device\n"); |
Linus Walleij | 5f3c44b | 2009-09-12 21:03:06 +0000 | [diff] [blame] | 1950 | return LIBMTP_ERROR_CONNECTING; |
| 1951 | } |
| 1952 | |
Linus Walleij | e04a1b9 | 2011-03-09 18:00:24 +0100 | [diff] [blame^] | 1953 | /* Copy USB version number */ |
| 1954 | ptp_usb->bcdusb = libusb_device->descriptor.bcdUSB; |
| 1955 | |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 1956 | /* Attempt to initialize this device */ |
Linus Walleij | 3c64325 | 2008-05-31 23:22:28 +0000 | [diff] [blame] | 1957 | if (init_ptp_usb(params, ptp_usb, libusb_device) < 0) { |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 1958 | LIBMTP_ERROR("LIBMTP PANIC: Unable to initialize device\n"); |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 1959 | return LIBMTP_ERROR_CONNECTING; |
| 1960 | } |
Linus Walleij | 5f3c44b | 2009-09-12 21:03:06 +0000 | [diff] [blame] | 1961 | |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 1962 | /* |
| 1963 | * This works in situations where previous bad applications |
Linus Walleij | 5f3c44b | 2009-09-12 21:03:06 +0000 | [diff] [blame] | 1964 | * have not used LIBMTP_Release_Device on exit |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 1965 | */ |
Linus Walleij | 3c64325 | 2008-05-31 23:22:28 +0000 | [diff] [blame] | 1966 | if ((ret = ptp_opensession(params, 1)) == PTP_ERROR_IO) { |
Linus Walleij | f9137dc | 2010-10-31 17:13:06 +0000 | [diff] [blame] | 1967 | LIBMTP_ERROR("PTP_ERROR_IO: failed to open session, trying again after resetting USB interface\n"); |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 1968 | close_usb(ptp_usb); |
Linus Walleij | 5f3c44b | 2009-09-12 21:03:06 +0000 | [diff] [blame] | 1969 | |
Linus Walleij | f9137dc | 2010-10-31 17:13:06 +0000 | [diff] [blame] | 1970 | LIBMTP_ERROR("LIBMTP libusb: Attempt to reset device\n"); |
| 1971 | usb_reset(ptp_usb->handle); |
| 1972 | |
Linus Walleij | 3c64325 | 2008-05-31 23:22:28 +0000 | [diff] [blame] | 1973 | if(init_ptp_usb(params, ptp_usb, libusb_device) <0) { |
Linus Walleij | f9137dc | 2010-10-31 17:13:06 +0000 | [diff] [blame] | 1974 | LIBMTP_ERROR("LIBMTP PANIC: Could not init USB on second attempt\n"); |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 1975 | return LIBMTP_ERROR_CONNECTING; |
| 1976 | } |
Linus Walleij | 5f3c44b | 2009-09-12 21:03:06 +0000 | [diff] [blame] | 1977 | |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 1978 | /* Device has been reset, try again */ |
Linus Walleij | f9137dc | 2010-10-31 17:13:06 +0000 | [diff] [blame] | 1979 | 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 Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 1983 | } |
Linus Walleij | 5f3c44b | 2009-09-12 21:03:06 +0000 | [diff] [blame] | 1984 | |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 1985 | /* Was the transaction id invalid? Try again */ |
| 1986 | if (ret == PTP_RC_InvalidTransactionID) { |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 1987 | LIBMTP_ERROR("LIBMTP WARNING: Transaction ID was invalid, increment and try again\n"); |
Linus Walleij | 3c64325 | 2008-05-31 23:22:28 +0000 | [diff] [blame] | 1988 | params->transaction_id += 10; |
| 1989 | ret = ptp_opensession(params, 1); |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 1990 | } |
| 1991 | |
| 1992 | if (ret != PTP_RC_SessionAlreadyOpened && ret != PTP_RC_OK) { |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 1993 | LIBMTP_ERROR("LIBMTP PANIC: Could not open session! " |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 1994 | "(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 Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 2002 | *usbinfo = (void *) ptp_usb; |
| 2003 | return LIBMTP_ERROR_NONE; |
| 2004 | } |
| 2005 | |
| 2006 | |
Linus Walleij | b0ab548 | 2007-08-29 21:08:54 +0000 | [diff] [blame] | 2007 | void close_device (PTP_USB *ptp_usb, PTPParams *params) |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 2008 | { |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 2009 | if (ptp_closesession(params)!=PTP_RC_OK) |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 2010 | LIBMTP_ERROR("ERROR: Could not close session!\n"); |
Linus Walleij | b0ab548 | 2007-08-29 21:08:54 +0000 | [diff] [blame] | 2011 | close_usb(ptp_usb); |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 2012 | } |
| 2013 | |
Linus Walleij | 2f62281 | 2008-08-30 22:06:58 +0000 | [diff] [blame] | 2014 | void set_usb_device_timeout(PTP_USB *ptp_usb, int timeout) |
| 2015 | { |
Linus Walleij | e04a1b9 | 2011-03-09 18:00:24 +0100 | [diff] [blame^] | 2016 | ptp_usb->timeout = timeout; |
Linus Walleij | 2f62281 | 2008-08-30 22:06:58 +0000 | [diff] [blame] | 2017 | } |
| 2018 | |
| 2019 | void get_usb_device_timeout(PTP_USB *ptp_usb, int *timeout) |
| 2020 | { |
Linus Walleij | e04a1b9 | 2011-03-09 18:00:24 +0100 | [diff] [blame^] | 2021 | *timeout = ptp_usb->timeout; |
| 2022 | } |
| 2023 | |
| 2024 | int 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 Walleij | 2f62281 | 2008-08-30 22:06:58 +0000 | [diff] [blame] | 2051 | } |
| 2052 | |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 2053 | static int usb_clear_stall_feature(PTP_USB* ptp_usb, int ep) |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 2054 | { |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 2055 | return (usb_control_msg(ptp_usb->handle, |
Linus Walleij | e04a1b9 | 2011-03-09 18:00:24 +0100 | [diff] [blame^] | 2056 | USB_RECIP_ENDPOINT, |
| 2057 | USB_REQ_CLEAR_FEATURE, |
| 2058 | USB_FEATURE_HALT, |
| 2059 | ep, |
| 2060 | NULL, |
| 2061 | 0, |
| 2062 | ptp_usb->timeout)); |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 2063 | } |
| 2064 | |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 2065 | static int usb_get_endpoint_status(PTP_USB* ptp_usb, int ep, uint16_t* status) |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 2066 | { |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 2067 | return (usb_control_msg(ptp_usb->handle, |
Linus Walleij | e04a1b9 | 2011-03-09 18:00:24 +0100 | [diff] [blame^] | 2068 | 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 Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 2075 | } |