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 | 2f62281 | 2008-08-30 22:06:58 +0000 | [diff] [blame] | 52 | /* Default USB timeout length. This can be overridden as needed |
| 53 | * but should start with a reasonable value so most common |
| 54 | * requests can be completed. The original value of 4000 was |
| 55 | * not long enough for large file transfer. Also, players can |
| 56 | * spend a bit of time collecting data. Higher values also |
| 57 | * make connecting/disconnecting more reliable. |
| 58 | */ |
| 59 | #define USB_TIMEOUT_DEFAULT 10000 |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 60 | |
| 61 | /* USB control message data phase direction */ |
| 62 | #ifndef USB_DP_HTD |
| 63 | #define USB_DP_HTD (0x00 << 7) /* host to device */ |
| 64 | #endif |
| 65 | #ifndef USB_DP_DTH |
| 66 | #define USB_DP_DTH (0x01 << 7) /* device to host */ |
| 67 | #endif |
| 68 | |
| 69 | /* USB Feature selector HALT */ |
| 70 | #ifndef USB_FEATURE_HALT |
| 71 | #define USB_FEATURE_HALT 0x00 |
| 72 | #endif |
| 73 | |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 74 | /* Internal data types */ |
| 75 | struct mtpdevice_list_struct { |
| 76 | struct usb_device *libusb_device; |
| 77 | PTPParams *params; |
| 78 | PTP_USB *ptp_usb; |
| 79 | uint32_t bus_location; |
| 80 | struct mtpdevice_list_struct *next; |
| 81 | }; |
| 82 | typedef struct mtpdevice_list_struct mtpdevice_list_t; |
| 83 | |
Linus Walleij | 6fd2f08 | 2006-03-28 07:19:22 +0000 | [diff] [blame] | 84 | static const LIBMTP_device_entry_t mtp_device_table[] = { |
Linus Walleij | 1a673de | 2007-10-29 23:10:05 +0000 | [diff] [blame] | 85 | /* We include an .h file which is shared between us and libgphoto2 */ |
| 86 | #include "music-players.h" |
Linus Walleij | a548364 | 2006-03-09 09:20:38 +0000 | [diff] [blame] | 87 | }; |
Linus Walleij | 6fd2f08 | 2006-03-28 07:19:22 +0000 | [diff] [blame] | 88 | 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] | 89 | |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 90 | // Local functions |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 91 | static struct usb_bus* init_usb(); |
Linus Walleij | b0ab548 | 2007-08-29 21:08:54 +0000 | [diff] [blame] | 92 | static void close_usb(PTP_USB* ptp_usb); |
Linus Walleij | 5f3c44b | 2009-09-12 21:03:06 +0000 | [diff] [blame] | 93 | static int find_interface_and_endpoints(struct usb_device *dev, |
| 94 | uint8_t *interface, |
| 95 | int* inep, |
| 96 | int* inep_maxpacket, |
| 97 | int* outep, |
| 98 | int* outep_maxpacket, |
| 99 | int* intep); |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 100 | static void clear_stall(PTP_USB* ptp_usb); |
Linus Walleij | 9eb3d31 | 2006-08-04 19:25:59 +0000 | [diff] [blame] | 101 | 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] | 102 | static short ptp_write_func (unsigned long,PTPDataHandler*,void *data,unsigned long*); |
Richard Low | 4df32f8 | 2007-01-11 20:04:39 +0000 | [diff] [blame] | 103 | 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] | 104 | static int usb_clear_stall_feature(PTP_USB* ptp_usb, int ep); |
| 105 | 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] | 106 | |
Linus Walleij | 552ba32 | 2007-01-22 11:49:59 +0000 | [diff] [blame] | 107 | /** |
| 108 | * Get a list of the supported USB devices. |
| 109 | * |
| 110 | * The developers depend on users of this library to constantly |
| 111 | * add in to the list of supported devices. What we need is the |
| 112 | * device name, USB Vendor ID (VID) and USB Product ID (PID). |
| 113 | * put this into a bug ticket at the project homepage, please. |
| 114 | * The VID/PID is used to let e.g. udev lift the device to |
| 115 | * console userspace access when it's plugged in. |
| 116 | * |
| 117 | * @param devices a pointer to a pointer that will hold a device |
| 118 | * list after the call to this function, if it was |
| 119 | * successful. |
| 120 | * @param numdevs a pointer to an integer that will hold the number |
| 121 | * of devices in the device list if the call was successful. |
| 122 | * @return 0 if the list was successfull retrieved, any other |
| 123 | * value means failure. |
| 124 | */ |
| 125 | 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] | 126 | { |
| 127 | *devices = (LIBMTP_device_entry_t *) &mtp_device_table; |
| 128 | *numdevs = mtp_device_table_size; |
| 129 | return 0; |
| 130 | } |
| 131 | |
Linus Walleij | 552ba32 | 2007-01-22 11:49:59 +0000 | [diff] [blame] | 132 | |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 133 | static struct usb_bus* init_usb() |
| 134 | { |
Linus Walleij | c1a82f1 | 2009-11-30 00:06:28 +0000 | [diff] [blame] | 135 | struct usb_bus* busses; |
| 136 | struct usb_bus* bus; |
| 137 | |
| 138 | /* |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 139 | * Some additional libusb debugging please. |
| 140 | * We use the same level debug between MTP and USB. |
| 141 | */ |
nicklas79 | 0358408 | 2009-09-28 18:20:16 +0000 | [diff] [blame] | 142 | if ((LIBMTP_debug & LIBMTP_DEBUG_USB) != 0) |
nicklas79 | a99196a | 2009-09-28 18:19:57 +0000 | [diff] [blame] | 143 | usb_set_debug(9); |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 144 | |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 145 | usb_init(); |
| 146 | usb_find_busses(); |
| 147 | usb_find_devices(); |
Linus Walleij | c1a82f1 | 2009-11-30 00:06:28 +0000 | [diff] [blame] | 148 | /* Workaround a libusb 0.1 bug : bus location is not initialised */ |
| 149 | busses = usb_get_busses(); |
| 150 | for (bus = busses; bus != NULL; bus = bus->next) { |
Richard Low | ea73f5c | 2010-05-16 11:57:56 +0000 | [diff] [blame] | 151 | if (!bus->location) |
| 152 | bus->location = strtoul(bus->dirname, NULL, 10); |
Linus Walleij | c1a82f1 | 2009-11-30 00:06:28 +0000 | [diff] [blame] | 153 | } |
| 154 | return (busses); |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | /** |
tedbullock | 0f033cb | 2007-02-14 20:56:54 +0000 | [diff] [blame] | 158 | * Small recursive function to append a new usb_device to the linked list of |
| 159 | * USB MTP devices |
Linus Walleij | c1a82f1 | 2009-11-30 00:06:28 +0000 | [diff] [blame] | 160 | * @param devlist dynamic linked list of pointers to usb devices with MTP |
Linus Walleij | 63fd1e6 | 2008-04-23 23:49:43 +0000 | [diff] [blame] | 161 | * properties, to be extended with new device. |
| 162 | * @param newdevice the new device to add. |
| 163 | * @param bus_location bus for this device. |
| 164 | * @return an extended array or NULL on failure. |
tedbullock | 0f033cb | 2007-02-14 20:56:54 +0000 | [diff] [blame] | 165 | */ |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 166 | static mtpdevice_list_t *append_to_mtpdevice_list(mtpdevice_list_t *devlist, |
Linus Walleij | 63fd1e6 | 2008-04-23 23:49:43 +0000 | [diff] [blame] | 167 | struct usb_device *newdevice, |
| 168 | uint32_t bus_location) |
tedbullock | 0f033cb | 2007-02-14 20:56:54 +0000 | [diff] [blame] | 169 | { |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 170 | mtpdevice_list_t *new_list_entry; |
Linus Walleij | c1a82f1 | 2009-11-30 00:06:28 +0000 | [diff] [blame] | 171 | |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 172 | new_list_entry = (mtpdevice_list_t *) malloc(sizeof(mtpdevice_list_t)); |
| 173 | if (new_list_entry == NULL) { |
| 174 | return NULL; |
| 175 | } |
| 176 | // Fill in USB device, if we *HAVE* to make a copy of the device do it here. |
| 177 | new_list_entry->libusb_device = newdevice; |
Linus Walleij | 63fd1e6 | 2008-04-23 23:49:43 +0000 | [diff] [blame] | 178 | new_list_entry->bus_location = bus_location; |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 179 | new_list_entry->next = NULL; |
Linus Walleij | 5d987e1 | 2010-05-22 22:35:33 +0000 | [diff] [blame] | 180 | |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 181 | if (devlist == NULL) { |
| 182 | return new_list_entry; |
| 183 | } else { |
| 184 | mtpdevice_list_t *tmp = devlist; |
| 185 | while (tmp->next != NULL) { |
| 186 | tmp = tmp->next; |
| 187 | } |
| 188 | tmp->next = new_list_entry; |
| 189 | } |
| 190 | return devlist; |
tedbullock | 0f033cb | 2007-02-14 20:56:54 +0000 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Small recursive function to free dynamic memory allocated to the linked list |
| 195 | * of USB MTP devices |
Linus Walleij | 5d987e1 | 2010-05-22 22:35:33 +0000 | [diff] [blame] | 196 | * @param devlist dynamic linked list of pointers to usb devices with MTP |
tedbullock | 0f033cb | 2007-02-14 20:56:54 +0000 | [diff] [blame] | 197 | * properties. |
| 198 | * @return nothing |
| 199 | */ |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 200 | static void free_mtpdevice_list(mtpdevice_list_t *devlist) |
tedbullock | 0f033cb | 2007-02-14 20:56:54 +0000 | [diff] [blame] | 201 | { |
Linus Walleij | ac06186 | 2007-03-07 08:28:33 +0000 | [diff] [blame] | 202 | mtpdevice_list_t *tmplist = devlist; |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 203 | |
| 204 | if (devlist == NULL) |
| 205 | return; |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 206 | while (tmplist != NULL) { |
| 207 | mtpdevice_list_t *tmp = tmplist; |
| 208 | tmplist = tmplist->next; |
Richard Low | 61edc1a | 2007-09-23 10:35:48 +0000 | [diff] [blame] | 209 | // Do not free() the fields (ptp_usb, params)! These are used elsewhere. |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 210 | free(tmp); |
tedbullock | 0f033cb | 2007-02-14 20:56:54 +0000 | [diff] [blame] | 211 | } |
tedbullock | 0f033cb | 2007-02-14 20:56:54 +0000 | [diff] [blame] | 212 | return; |
| 213 | } |
| 214 | |
| 215 | /** |
Linus Walleij | 9521e2b | 2007-07-10 21:50:42 +0000 | [diff] [blame] | 216 | * This checks if a device has an MTP descriptor. The descriptor was |
| 217 | * elaborated about in gPhoto bug 1482084, and some official documentation |
| 218 | * with no strings attached was published by Microsoft at |
| 219 | * http://www.microsoft.com/whdc/system/bus/USB/USBFAQ_intermed.mspx#E3HAC |
| 220 | * |
Linus Walleij | a4942fc | 2007-03-12 19:23:21 +0000 | [diff] [blame] | 221 | * @param dev a device struct from libusb. |
Linus Walleij | 9521e2b | 2007-07-10 21:50:42 +0000 | [diff] [blame] | 222 | * @param dumpfile set to non-NULL to make the descriptors dump out |
| 223 | * to this file in human-readable hex so we can scruitinze them. |
Linus Walleij | a4942fc | 2007-03-12 19:23:21 +0000 | [diff] [blame] | 224 | * @return 1 if the device is MTP compliant, 0 if not. |
| 225 | */ |
Linus Walleij | 9521e2b | 2007-07-10 21:50:42 +0000 | [diff] [blame] | 226 | static int probe_device_descriptor(struct usb_device *dev, FILE *dumpfile) |
Linus Walleij | a4942fc | 2007-03-12 19:23:21 +0000 | [diff] [blame] | 227 | { |
| 228 | usb_dev_handle *devh; |
| 229 | unsigned char buf[1024], cmd; |
Linus Walleij | bc550bc | 2007-11-08 23:25:09 +0000 | [diff] [blame] | 230 | int i; |
Linus Walleij | a4942fc | 2007-03-12 19:23:21 +0000 | [diff] [blame] | 231 | int ret; |
Linus Walleij | fb3e302 | 2011-01-19 19:20:34 +0100 | [diff] [blame] | 232 | /* This is to indicate if we find some vendor interface */ |
| 233 | int found_vendor_spec_interface = 0; |
Linus Walleij | 5d987e1 | 2010-05-22 22:35:33 +0000 | [diff] [blame] | 234 | |
| 235 | /* |
Linus Walleij | fb3e302 | 2011-01-19 19:20:34 +0100 | [diff] [blame] | 236 | * Don't examine devices that are not likely to |
| 237 | * contain any MTP interface, update this the day |
| 238 | * you find some weird combination... |
Linus Walleij | 5d987e1 | 2010-05-22 22:35:33 +0000 | [diff] [blame] | 239 | */ |
Linus Walleij | 4eb8ad9 | 2011-01-27 22:52:07 +0100 | [diff] [blame] | 240 | if (!(dev->descriptor.bDeviceClass == USB_CLASS_PER_INTERFACE || |
| 241 | dev->descriptor.bDeviceClass == USB_CLASS_COMM || |
| 242 | dev->descriptor.bDeviceClass == USB_CLASS_PTP || |
| 243 | dev->descriptor.bDeviceClass == USB_CLASS_VENDOR_SPEC)) { |
Linus Walleij | a4942fc | 2007-03-12 19:23:21 +0000 | [diff] [blame] | 244 | return 0; |
| 245 | } |
Linus Walleij | 5d987e1 | 2010-05-22 22:35:33 +0000 | [diff] [blame] | 246 | |
Linus Walleij | a4942fc | 2007-03-12 19:23:21 +0000 | [diff] [blame] | 247 | /* Attempt to open Device on this port */ |
| 248 | devh = usb_open(dev); |
| 249 | if (devh == NULL) { |
| 250 | /* Could not open this device */ |
| 251 | return 0; |
| 252 | } |
Linus Walleij | bc550bc | 2007-11-08 23:25:09 +0000 | [diff] [blame] | 253 | |
| 254 | /* |
Richard Low | e029eba | 2009-04-11 12:36:25 +0000 | [diff] [blame] | 255 | * This sometimes crashes on the j for loop below |
Linus Walleij | 8824421 | 2009-08-12 16:05:56 +0000 | [diff] [blame] | 256 | * I think it is because config is NULL yet |
Richard Low | e029eba | 2009-04-11 12:36:25 +0000 | [diff] [blame] | 257 | * dev->descriptor.bNumConfigurations > 0 |
| 258 | * this check should stop this |
Linus Walleij | bc550bc | 2007-11-08 23:25:09 +0000 | [diff] [blame] | 259 | */ |
Richard Low | e029eba | 2009-04-11 12:36:25 +0000 | [diff] [blame] | 260 | if (dev->config) { |
| 261 | /* |
Linus Walleij | 8824421 | 2009-08-12 16:05:56 +0000 | [diff] [blame] | 262 | * Loop over the device configurations and interfaces. Nokia MTP-capable |
| 263 | * handsets (possibly others) typically have the string "MTP" in their |
Richard Low | e029eba | 2009-04-11 12:36:25 +0000 | [diff] [blame] | 264 | * MTP interface descriptions, that's how they can be detected, before |
| 265 | * we try the more esoteric "OS descriptors" (below). |
| 266 | */ |
| 267 | for (i = 0; i < dev->descriptor.bNumConfigurations; i++) { |
| 268 | uint8_t j; |
Linus Walleij | 8824421 | 2009-08-12 16:05:56 +0000 | [diff] [blame] | 269 | |
Richard Low | e029eba | 2009-04-11 12:36:25 +0000 | [diff] [blame] | 270 | for (j = 0; j < dev->config[i].bNumInterfaces; j++) { |
| 271 | int k; |
| 272 | for (k = 0; k < dev->config[i].interface[j].num_altsetting; k++) { |
Linus Walleij | 8824421 | 2009-08-12 16:05:56 +0000 | [diff] [blame] | 273 | /* Current interface descriptor */ |
| 274 | struct usb_interface_descriptor *intf = |
| 275 | &dev->config[i].interface[j].altsetting[k]; |
| 276 | |
| 277 | /* |
Linus Walleij | fb3e302 | 2011-01-19 19:20:34 +0100 | [diff] [blame] | 278 | * We only want to probe for the OS descriptor if the |
| 279 | * device is USB_CLASS_VENDOR_SPEC or one of the interfaces |
| 280 | * in it is, so flag if we find an interface like this. |
| 281 | */ |
| 282 | if (intf->bInterfaceClass == USB_CLASS_VENDOR_SPEC) { |
| 283 | found_vendor_spec_interface = 1; |
| 284 | } |
| 285 | |
| 286 | /* |
Linus Walleij | 8824421 | 2009-08-12 16:05:56 +0000 | [diff] [blame] | 287 | * Check for Still Image Capture class with PIMA 15740 protocol, |
| 288 | * also known as PTP |
| 289 | */ |
Linus Walleij | 5d92c8c | 2011-01-25 23:42:19 +0100 | [diff] [blame] | 290 | #if 0 |
Linus Walleij | 8824421 | 2009-08-12 16:05:56 +0000 | [diff] [blame] | 291 | if (intf->bInterfaceClass == USB_CLASS_PTP |
| 292 | && intf->bInterfaceSubClass == 0x01 |
| 293 | && intf->bInterfaceProtocol == 0x01) { |
| 294 | if (dumpfile != NULL) { |
Linus Walleij | 5d92c8c | 2011-01-25 23:42:19 +0100 | [diff] [blame] | 295 | fprintf(dumpfile, " Found PTP device, check vendor " |
| 296 | "extension...\n"); |
Linus Walleij | 8824421 | 2009-08-12 16:05:56 +0000 | [diff] [blame] | 297 | } |
Linus Walleij | 5d92c8c | 2011-01-25 23:42:19 +0100 | [diff] [blame] | 298 | // This is where we may insert code to open a PTP |
| 299 | // session and query the vendor extension ID to see |
Linus Walleij | 4eb8ad9 | 2011-01-27 22:52:07 +0100 | [diff] [blame] | 300 | // if it is 0xffffffff, i.e. MTP according to the spec. |
Linus Walleij | 5d92c8c | 2011-01-25 23:42:19 +0100 | [diff] [blame] | 301 | if (was_mtp_extension) { |
| 302 | usb_close(devh); |
| 303 | return 1; |
| 304 | } |
Linus Walleij | 8824421 | 2009-08-12 16:05:56 +0000 | [diff] [blame] | 305 | } |
Linus Walleij | 5d92c8c | 2011-01-25 23:42:19 +0100 | [diff] [blame] | 306 | #endif |
| 307 | |
| 308 | /* |
| 309 | * Next we search for the MTP substring in the interface name. |
| 310 | * For example : "RIM MS/MTP" should work. |
| 311 | */ |
Linus Walleij | 8824421 | 2009-08-12 16:05:56 +0000 | [diff] [blame] | 312 | buf[0] = '\0'; |
| 313 | ret = usb_get_string_simple(devh, |
| 314 | dev->config[i].interface[j].altsetting[k].iInterface, |
| 315 | (char *) buf, |
| 316 | 1024); |
| 317 | if (ret < 3) |
| 318 | continue; |
Linus Walleij | fea4f53 | 2009-09-22 22:08:35 +0000 | [diff] [blame] | 319 | if (strstr((char *) buf, "MTP") != NULL) { |
Linus Walleij | 8824421 | 2009-08-12 16:05:56 +0000 | [diff] [blame] | 320 | if (dumpfile != NULL) { |
| 321 | fprintf(dumpfile, "Configuration %d, interface %d, altsetting %d:\n", i, j, k); |
| 322 | fprintf(dumpfile, " Interface description contains the string \"MTP\"\n"); |
| 323 | fprintf(dumpfile, " Device recognized as MTP, no further probing.\n"); |
| 324 | } |
Richard Low | e029eba | 2009-04-11 12:36:25 +0000 | [diff] [blame] | 325 | usb_close(devh); |
| 326 | return 1; |
| 327 | } |
| 328 | #ifdef LIBUSB_HAS_GET_DRIVER_NP |
Linus Walleij | 8824421 | 2009-08-12 16:05:56 +0000 | [diff] [blame] | 329 | { |
| 330 | /* |
| 331 | * Specifically avoid probing anything else than USB mass storage devices |
| 332 | * and non-associated drivers in Linux. |
| 333 | */ |
| 334 | char devname[0x10]; |
| 335 | |
| 336 | devname[0] = '\0'; |
| 337 | ret = usb_get_driver_np(devh, |
| 338 | dev->config[i].interface[j].altsetting[k].iInterface, |
| 339 | devname, |
| 340 | sizeof(devname)); |
| 341 | if (devname[0] != '\0' && strcmp(devname, "usb-storage")) { |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 342 | LIBMTP_INFO("avoid probing device using kernel interface \"%s\"\n", devname); |
Linus Walleij | 8824421 | 2009-08-12 16:05:56 +0000 | [diff] [blame] | 343 | return 0; |
| 344 | } |
| 345 | } |
Richard Low | e029eba | 2009-04-11 12:36:25 +0000 | [diff] [blame] | 346 | #endif |
| 347 | } |
| 348 | } |
| 349 | } |
| 350 | } else { |
| 351 | if (dev->descriptor.bNumConfigurations) |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 352 | 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] | 353 | } |
Linus Walleij | fea4f53 | 2009-09-22 22:08:35 +0000 | [diff] [blame] | 354 | |
Linus Walleij | 49e57b8 | 2009-12-16 09:21:33 +0000 | [diff] [blame] | 355 | /* |
Linus Walleij | fb3e302 | 2011-01-19 19:20:34 +0100 | [diff] [blame] | 356 | * Only probe for OS descriptor if the device is vendor specific |
| 357 | * or one of the interfaces found is. |
Linus Walleij | 49e57b8 | 2009-12-16 09:21:33 +0000 | [diff] [blame] | 358 | */ |
Linus Walleij | fb3e302 | 2011-01-19 19:20:34 +0100 | [diff] [blame] | 359 | if (dev->descriptor.bDeviceClass == USB_CLASS_VENDOR_SPEC || |
| 360 | found_vendor_spec_interface) { |
Linus Walleij | 9521e2b | 2007-07-10 21:50:42 +0000 | [diff] [blame] | 361 | |
Linus Walleij | fb3e302 | 2011-01-19 19:20:34 +0100 | [diff] [blame] | 362 | /* Read the special descriptor */ |
| 363 | ret = usb_get_descriptor(devh, 0x03, 0xee, buf, sizeof(buf)); |
Linus Walleij | fea4f53 | 2009-09-22 22:08:35 +0000 | [diff] [blame] | 364 | |
Linus Walleij | fb3e302 | 2011-01-19 19:20:34 +0100 | [diff] [blame] | 365 | /* |
| 366 | * If something failed we're probably stalled to we need |
| 367 | * to clear the stall off the endpoint and say this is not |
| 368 | * MTP. |
| 369 | */ |
| 370 | if (ret < 0) { |
| 371 | /* EP0 is the default control endpoint */ |
| 372 | usb_clear_halt(devh, 0); |
| 373 | usb_close(devh); |
| 374 | return 0; |
| 375 | } |
| 376 | |
| 377 | // Dump it, if requested |
| 378 | if (dumpfile != NULL && ret > 0) { |
| 379 | fprintf(dumpfile, "Microsoft device descriptor 0xee:\n"); |
| 380 | data_dump_ascii(dumpfile, buf, ret, 16); |
| 381 | } |
| 382 | |
| 383 | /* Check if descriptor length is at least 10 bytes */ |
| 384 | if (ret < 10) { |
| 385 | usb_close(devh); |
| 386 | return 0; |
| 387 | } |
| 388 | |
| 389 | /* Check if this device has a Microsoft Descriptor */ |
| 390 | if (!((buf[2] == 'M') && (buf[4] == 'S') && |
| 391 | (buf[6] == 'F') && (buf[8] == 'T'))) { |
| 392 | usb_close(devh); |
| 393 | return 0; |
| 394 | } |
| 395 | |
| 396 | /* Check if device responds to control message 1 or if there is an error */ |
| 397 | cmd = buf[16]; |
| 398 | ret = usb_control_msg (devh, |
| 399 | USB_ENDPOINT_IN | USB_RECIP_DEVICE | USB_TYPE_VENDOR, |
| 400 | cmd, |
| 401 | 0, |
| 402 | 4, |
| 403 | (char *) buf, |
| 404 | sizeof(buf), |
| 405 | USB_TIMEOUT_DEFAULT); |
| 406 | |
| 407 | // Dump it, if requested |
| 408 | if (dumpfile != NULL && ret > 0) { |
| 409 | fprintf(dumpfile, "Microsoft device response to control message 1, CMD 0x%02x:\n", cmd); |
| 410 | data_dump_ascii(dumpfile, buf, ret, 16); |
| 411 | } |
| 412 | |
| 413 | /* If this is true, the device either isn't MTP or there was an error */ |
| 414 | if (ret <= 0x15) { |
| 415 | /* TODO: If there was an error, flag it and let the user know somehow */ |
| 416 | /* if(ret == -1) {} */ |
| 417 | usb_close(devh); |
| 418 | return 0; |
| 419 | } |
| 420 | |
| 421 | /* Check if device is MTP or if it is something like a USB Mass Storage |
| 422 | device with Janus DRM support */ |
| 423 | if ((buf[0x12] != 'M') || (buf[0x13] != 'T') || (buf[0x14] != 'P')) { |
| 424 | usb_close(devh); |
| 425 | return 0; |
| 426 | } |
| 427 | |
| 428 | /* After this point we are probably dealing with an MTP device */ |
| 429 | |
| 430 | /* |
| 431 | * Check if device responds to control message 2, which is |
| 432 | * the extended device parameters. Most devices will just |
| 433 | * respond with a copy of the same message as for the first |
| 434 | * message, some respond with zero-length (which is OK) |
| 435 | * and some with pure garbage. We're not parsing the result |
| 436 | * so this is not very important. |
| 437 | */ |
| 438 | ret = usb_control_msg (devh, |
| 439 | USB_ENDPOINT_IN | USB_RECIP_DEVICE | USB_TYPE_VENDOR, |
| 440 | cmd, |
| 441 | 0, |
| 442 | 5, |
| 443 | (char *) buf, |
| 444 | sizeof(buf), |
| 445 | USB_TIMEOUT_DEFAULT); |
| 446 | |
| 447 | // Dump it, if requested |
| 448 | if (dumpfile != NULL && ret > 0) { |
| 449 | fprintf(dumpfile, "Microsoft device response to control message 2, CMD 0x%02x:\n", cmd); |
| 450 | data_dump_ascii(dumpfile, buf, ret, 16); |
| 451 | } |
| 452 | |
| 453 | /* If this is true, the device errored against control message 2 */ |
| 454 | if (ret == -1) { |
| 455 | /* TODO: Implement callback function to let managing program know there |
| 456 | was a problem, along with description of the problem */ |
| 457 | LIBMTP_ERROR("Potential MTP Device with VendorID:%04x and " |
| 458 | "ProductID:%04x encountered an error responding to " |
| 459 | "control message 2.\n" |
| 460 | "Problems may arrise but continuing\n", |
| 461 | dev->descriptor.idVendor, dev->descriptor.idProduct); |
| 462 | } else if (dumpfile != NULL && ret == 0) { |
| 463 | fprintf(dumpfile, "Zero-length response to control message 2 (OK)\n"); |
| 464 | } else if (dumpfile != NULL) { |
| 465 | fprintf(dumpfile, "Device responds to control message 2 with some data.\n"); |
| 466 | } |
Linus Walleij | a4942fc | 2007-03-12 19:23:21 +0000 | [diff] [blame] | 467 | } |
Linus Walleij | fea4f53 | 2009-09-22 22:08:35 +0000 | [diff] [blame] | 468 | |
Linus Walleij | a4942fc | 2007-03-12 19:23:21 +0000 | [diff] [blame] | 469 | /* Close the USB device handle */ |
| 470 | usb_close(devh); |
| 471 | return 1; |
| 472 | } |
| 473 | |
| 474 | /** |
| 475 | * This function scans through the connected usb devices on a machine and |
| 476 | * if they match known Vendor and Product identifiers appends them to the |
Linus Walleij | 4955748 | 2010-06-22 09:14:47 +0000 | [diff] [blame] | 477 | * dynamic array mtp_device_list. Be sure to call |
| 478 | * <code>free_mtpdevice_list(mtp_device_list)</code> when you are done |
Linus Walleij | 63fd1e6 | 2008-04-23 23:49:43 +0000 | [diff] [blame] | 479 | * with it, assuming it is not NULL. |
Linus Walleij | 4955748 | 2010-06-22 09:14:47 +0000 | [diff] [blame] | 480 | * @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] | 481 | * properties (if this list is not empty, new entries will be appended |
| 482 | * to the list). |
Linus Walleij | a4942fc | 2007-03-12 19:23:21 +0000 | [diff] [blame] | 483 | * @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] | 484 | * appropriately. LIBMTP_ERROR_NO_DEVICE_ATTACHED implies that no |
Linus Walleij | 9c6c154 | 2007-09-19 13:43:06 +0000 | [diff] [blame] | 485 | * devices have been found. |
Linus Walleij | 2e1c8a3 | 2007-01-22 06:46:08 +0000 | [diff] [blame] | 486 | */ |
Linus Walleij | 45a8637 | 2007-03-07 09:36:19 +0000 | [diff] [blame] | 487 | 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] | 488 | { |
Linus Walleij | 2e1c8a3 | 2007-01-22 06:46:08 +0000 | [diff] [blame] | 489 | struct usb_bus *bus = init_usb(); |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 490 | for (; bus != NULL; bus = bus->next) { |
Linus Walleij | 2e1c8a3 | 2007-01-22 06:46:08 +0000 | [diff] [blame] | 491 | struct usb_device *dev = bus->devices; |
Linus Walleij | f27d1cd | 2007-03-05 14:23:00 +0000 | [diff] [blame] | 492 | for (; dev != NULL; dev = dev->next) { |
Richard Low | e9f205f | 2007-09-16 16:25:50 +0000 | [diff] [blame] | 493 | if (dev->descriptor.bDeviceClass != USB_CLASS_HUB) { |
Linus Walleij | 9c6c154 | 2007-09-19 13:43:06 +0000 | [diff] [blame] | 494 | int i; |
| 495 | int found = 0; |
| 496 | |
| 497 | // First check if we know about the device already. |
| 498 | // Devices well known to us will not have their descriptors |
| 499 | // probed, it caused problems with some devices. |
Richard Low | e9f205f | 2007-09-16 16:25:50 +0000 | [diff] [blame] | 500 | for(i = 0; i < mtp_device_table_size; i++) { |
| 501 | if(dev->descriptor.idVendor == mtp_device_table[i].vendor_id && |
| 502 | dev->descriptor.idProduct == mtp_device_table[i].product_id) { |
| 503 | /* Append this usb device to the MTP device list */ |
Linus Walleij | 4955748 | 2010-06-22 09:14:47 +0000 | [diff] [blame] | 504 | *mtp_device_list = append_to_mtpdevice_list(*mtp_device_list, |
| 505 | dev, |
Linus Walleij | 63fd1e6 | 2008-04-23 23:49:43 +0000 | [diff] [blame] | 506 | bus->location); |
Richard Low | e9f205f | 2007-09-16 16:25:50 +0000 | [diff] [blame] | 507 | found = 1; |
| 508 | break; |
| 509 | } |
| 510 | } |
Linus Walleij | 9c6c154 | 2007-09-19 13:43:06 +0000 | [diff] [blame] | 511 | // If we didn't know it, try probing the "OS Descriptor". |
Richard Low | e9f205f | 2007-09-16 16:25:50 +0000 | [diff] [blame] | 512 | if (!found) { |
Richard Low | e9f205f | 2007-09-16 16:25:50 +0000 | [diff] [blame] | 513 | if (probe_device_descriptor(dev, NULL)) { |
| 514 | /* Append this usb device to the MTP USB Device List */ |
Linus Walleij | 4955748 | 2010-06-22 09:14:47 +0000 | [diff] [blame] | 515 | *mtp_device_list = append_to_mtpdevice_list(*mtp_device_list, |
Linus Walleij | 63fd1e6 | 2008-04-23 23:49:43 +0000 | [diff] [blame] | 516 | dev, |
| 517 | bus->location); |
Richard Low | e9f205f | 2007-09-16 16:25:50 +0000 | [diff] [blame] | 518 | } |
Linus Walleij | d866d24 | 2009-08-23 21:50:39 +0000 | [diff] [blame] | 519 | /* |
| 520 | * By thomas_-_s: Also append devices that are no MTP but PTP devices |
| 521 | * if this is commented out. |
| 522 | */ |
| 523 | /* |
| 524 | else { |
| 525 | // Check whether the device is no USB hub but a PTP. |
| 526 | if ( dev->config != NULL &&dev->config->interface->altsetting->bInterfaceClass == USB_CLASS_PTP && dev->descriptor.bDeviceClass != USB_CLASS_HUB ) { |
| 527 | *mtp_device_list = append_to_mtpdevice_list(*mtp_device_list, dev, bus->location); |
| 528 | } |
| 529 | } |
| 530 | */ |
Richard Low | e9f205f | 2007-09-16 16:25:50 +0000 | [diff] [blame] | 531 | } |
Linus Walleij | 9c6c154 | 2007-09-19 13:43:06 +0000 | [diff] [blame] | 532 | } |
Linus Walleij | 2e1c8a3 | 2007-01-22 06:46:08 +0000 | [diff] [blame] | 533 | } |
| 534 | } |
Linus Walleij | 4955748 | 2010-06-22 09:14:47 +0000 | [diff] [blame] | 535 | |
Linus Walleij | 2e1c8a3 | 2007-01-22 06:46:08 +0000 | [diff] [blame] | 536 | /* If nothing was found we end up here. */ |
Linus Walleij | a4942fc | 2007-03-12 19:23:21 +0000 | [diff] [blame] | 537 | if(*mtp_device_list == NULL) { |
tedbullock | 433d217 | 2007-02-23 22:39:12 +0000 | [diff] [blame] | 538 | return LIBMTP_ERROR_NO_DEVICE_ATTACHED; |
Linus Walleij | a4942fc | 2007-03-12 19:23:21 +0000 | [diff] [blame] | 539 | } |
| 540 | return LIBMTP_ERROR_NONE; |
Linus Walleij | 2e1c8a3 | 2007-01-22 06:46:08 +0000 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | /** |
Linus Walleij | 549f49a | 2010-12-05 14:00:34 +0000 | [diff] [blame] | 544 | * Checks if a specific device with a certain bus and device |
| 545 | * number has an MTP type device descriptor. |
| 546 | * |
| 547 | * @param busno the bus number of the device to check |
| 548 | * @param deviceno the device number of the device to check |
| 549 | * @return 1 if the device is MTP else 0 |
| 550 | */ |
| 551 | int LIBMTP_Check_Specific_Device(int busno, int devno) |
| 552 | { |
| 553 | struct usb_bus *bus = init_usb(); |
| 554 | for (; bus != NULL; bus = bus->next) { |
| 555 | struct usb_device *dev = bus->devices; |
| 556 | if (bus->location != busno) |
| 557 | continue; |
| 558 | |
| 559 | for (; dev != NULL; dev = dev->next) { |
| 560 | |
| 561 | if (dev->devnum != devno) |
| 562 | continue; |
| 563 | |
| 564 | if (probe_device_descriptor(dev, NULL)) |
| 565 | return 1; |
| 566 | } |
| 567 | } |
| 568 | return 0; |
| 569 | } |
| 570 | |
| 571 | /** |
Linus Walleij | 63fd1e6 | 2008-04-23 23:49:43 +0000 | [diff] [blame] | 572 | * Detect the raw MTP device descriptors and return a list of |
| 573 | * of the devices found. |
Linus Walleij | 4955748 | 2010-06-22 09:14:47 +0000 | [diff] [blame] | 574 | * |
Linus Walleij | 63fd1e6 | 2008-04-23 23:49:43 +0000 | [diff] [blame] | 575 | * @param devices a pointer to a variable that will hold |
| 576 | * the list of raw devices found. This may be NULL |
| 577 | * on return if the number of detected devices is zero. |
| 578 | * The user shall simply <code>free()</code> this |
| 579 | * variable when finished with the raw devices, |
| 580 | * in order to release memory. |
Linus Walleij | 4955748 | 2010-06-22 09:14:47 +0000 | [diff] [blame] | 581 | * @param numdevs a pointer to an integer that will hold |
Linus Walleij | 63fd1e6 | 2008-04-23 23:49:43 +0000 | [diff] [blame] | 582 | * the number of devices in the list. This may |
| 583 | * be 0. |
| 584 | * @return 0 if successful, any other value means failure. |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 585 | */ |
Linus Walleij | 4955748 | 2010-06-22 09:14:47 +0000 | [diff] [blame] | 586 | 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] | 587 | int * numdevs) |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 588 | { |
Linus Walleij | 63fd1e6 | 2008-04-23 23:49:43 +0000 | [diff] [blame] | 589 | mtpdevice_list_t *devlist = NULL; |
| 590 | mtpdevice_list_t *dev; |
| 591 | LIBMTP_error_number_t ret; |
Linus Walleij | 63fd1e6 | 2008-04-23 23:49:43 +0000 | [diff] [blame] | 592 | LIBMTP_raw_device_t *retdevs; |
| 593 | int devs = 0; |
Linus Walleij | f106aa6 | 2008-05-04 22:55:51 +0000 | [diff] [blame] | 594 | int i, j; |
Linus Walleij | b6b6913 | 2007-03-07 09:45:05 +0000 | [diff] [blame] | 595 | |
| 596 | ret = get_mtp_usb_device_list(&devlist); |
Linus Walleij | ef2fb36 | 2008-05-28 20:59:09 +0000 | [diff] [blame] | 597 | if (ret == LIBMTP_ERROR_NO_DEVICE_ATTACHED) { |
| 598 | *devices = NULL; |
| 599 | *numdevs = 0; |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 600 | return ret; |
Linus Walleij | ef2fb36 | 2008-05-28 20:59:09 +0000 | [diff] [blame] | 601 | } else if (ret != LIBMTP_ERROR_NONE) { |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 602 | LIBMTP_ERROR("LIBMTP PANIC: get_mtp_usb_device_list() " |
Linus Walleij | f106aa6 | 2008-05-04 22:55:51 +0000 | [diff] [blame] | 603 | "error code: %d on line %d\n", ret, __LINE__); |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 604 | return ret; |
Linus Walleij | b6b6913 | 2007-03-07 09:45:05 +0000 | [diff] [blame] | 605 | } |
Linus Walleij | f106aa6 | 2008-05-04 22:55:51 +0000 | [diff] [blame] | 606 | |
Linus Walleij | 63fd1e6 | 2008-04-23 23:49:43 +0000 | [diff] [blame] | 607 | // Get list size |
| 608 | dev = devlist; |
| 609 | while (dev != NULL) { |
| 610 | devs++; |
| 611 | dev = dev->next; |
| 612 | } |
| 613 | if (devs == 0) { |
| 614 | *devices = NULL; |
| 615 | *numdevs = 0; |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 616 | return LIBMTP_ERROR_NONE; |
Linus Walleij | 63fd1e6 | 2008-04-23 23:49:43 +0000 | [diff] [blame] | 617 | } |
| 618 | // Conjure a device list |
| 619 | retdevs = (LIBMTP_raw_device_t *) malloc(sizeof(LIBMTP_raw_device_t) * devs); |
| 620 | if (retdevs == NULL) { |
| 621 | // Out of memory |
| 622 | *devices = NULL; |
| 623 | *numdevs = 0; |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 624 | return LIBMTP_ERROR_MEMORY_ALLOCATION; |
Linus Walleij | 63fd1e6 | 2008-04-23 23:49:43 +0000 | [diff] [blame] | 625 | } |
| 626 | dev = devlist; |
| 627 | i = 0; |
| 628 | while (dev != NULL) { |
Linus Walleij | f106aa6 | 2008-05-04 22:55:51 +0000 | [diff] [blame] | 629 | int device_known = 0; |
| 630 | |
Linus Walleij | 63fd1e6 | 2008-04-23 23:49:43 +0000 | [diff] [blame] | 631 | // Assign default device info |
| 632 | retdevs[i].device_entry.vendor = NULL; |
| 633 | retdevs[i].device_entry.vendor_id = dev->libusb_device->descriptor.idVendor; |
| 634 | retdevs[i].device_entry.product = NULL; |
| 635 | retdevs[i].device_entry.product_id = dev->libusb_device->descriptor.idProduct; |
| 636 | retdevs[i].device_entry.device_flags = 0x00000000U; |
Linus Walleij | fec4d56 | 2008-06-01 22:30:36 +0000 | [diff] [blame] | 637 | // See if we can locate some additional vendor info and device flags |
Linus Walleij | f106aa6 | 2008-05-04 22:55:51 +0000 | [diff] [blame] | 638 | for(j = 0; j < mtp_device_table_size; j++) { |
| 639 | if(dev->libusb_device->descriptor.idVendor == mtp_device_table[j].vendor_id && |
| 640 | dev->libusb_device->descriptor.idProduct == mtp_device_table[j].product_id) { |
| 641 | device_known = 1; |
| 642 | retdevs[i].device_entry.vendor = mtp_device_table[j].vendor; |
| 643 | retdevs[i].device_entry.product = mtp_device_table[j].product; |
| 644 | retdevs[i].device_entry.device_flags = mtp_device_table[j].device_flags; |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 645 | |
Linus Walleij | f106aa6 | 2008-05-04 22:55:51 +0000 | [diff] [blame] | 646 | // This device is known to the developers |
Linus Walleij | 4955748 | 2010-06-22 09:14:47 +0000 | [diff] [blame] | 647 | 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] | 648 | i, |
| 649 | dev->libusb_device->descriptor.idVendor, |
| 650 | dev->libusb_device->descriptor.idProduct, |
| 651 | mtp_device_table[j].vendor, |
| 652 | mtp_device_table[j].product); |
Linus Walleij | f106aa6 | 2008-05-04 22:55:51 +0000 | [diff] [blame] | 653 | break; |
| 654 | } |
| 655 | } |
| 656 | if (!device_known) { |
| 657 | // This device is unknown to the developers |
Linus Walleij | 4955748 | 2010-06-22 09:14:47 +0000 | [diff] [blame] | 658 | LIBMTP_ERROR("Device %d (VID=%04x and PID=%04x) is UNKNOWN.\n", |
Linus Walleij | f106aa6 | 2008-05-04 22:55:51 +0000 | [diff] [blame] | 659 | i, |
| 660 | dev->libusb_device->descriptor.idVendor, |
| 661 | dev->libusb_device->descriptor.idProduct); |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 662 | 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] | 663 | "libmtp development team\n"); |
Linus Walleij | fec4d56 | 2008-06-01 22:30:36 +0000 | [diff] [blame] | 664 | /* |
| 665 | * Trying to get iManufacturer or iProduct from the device at this |
| 666 | * point would require opening a device handle, that we don't want |
| 667 | * to do right now. (Takes time for no good enough reason.) |
| 668 | */ |
Linus Walleij | f106aa6 | 2008-05-04 22:55:51 +0000 | [diff] [blame] | 669 | } |
| 670 | // Save the location on the bus |
Linus Walleij | 63fd1e6 | 2008-04-23 23:49:43 +0000 | [diff] [blame] | 671 | retdevs[i].bus_location = dev->bus_location; |
| 672 | retdevs[i].devnum = dev->libusb_device->devnum; |
| 673 | i++; |
| 674 | dev = dev->next; |
Linus Walleij | 4955748 | 2010-06-22 09:14:47 +0000 | [diff] [blame] | 675 | } |
Linus Walleij | 63fd1e6 | 2008-04-23 23:49:43 +0000 | [diff] [blame] | 676 | *devices = retdevs; |
| 677 | *numdevs = i; |
Linus Walleij | b6b6913 | 2007-03-07 09:45:05 +0000 | [diff] [blame] | 678 | free_mtpdevice_list(devlist); |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 679 | return LIBMTP_ERROR_NONE; |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 680 | } |
| 681 | |
Linus Walleij | c6210fb | 2006-05-08 11:11:41 +0000 | [diff] [blame] | 682 | /** |
| 683 | * This routine just dumps out low-level |
| 684 | * USB information about the current device. |
| 685 | * @param ptp_usb the USB device to get information from. |
| 686 | */ |
| 687 | void dump_usbinfo(PTP_USB *ptp_usb) |
| 688 | { |
Linus Walleij | c6210fb | 2006-05-08 11:11:41 +0000 | [diff] [blame] | 689 | struct usb_device *dev; |
| 690 | |
| 691 | #ifdef LIBUSB_HAS_GET_DRIVER_NP |
| 692 | char devname[0x10]; |
Richard Low | e029eba | 2009-04-11 12:36:25 +0000 | [diff] [blame] | 693 | int res; |
Linus Walleij | 4955748 | 2010-06-22 09:14:47 +0000 | [diff] [blame] | 694 | |
Linus Walleij | c6210fb | 2006-05-08 11:11:41 +0000 | [diff] [blame] | 695 | devname[0] = '\0'; |
Linus Walleij | b0ab548 | 2007-08-29 21:08:54 +0000 | [diff] [blame] | 696 | 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] | 697 | if (devname[0] != '\0') { |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 698 | LIBMTP_INFO(" Using kernel interface \"%s\"\n", devname); |
Linus Walleij | c6210fb | 2006-05-08 11:11:41 +0000 | [diff] [blame] | 699 | } |
| 700 | #endif |
| 701 | dev = usb_device(ptp_usb->handle); |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 702 | LIBMTP_INFO(" bcdUSB: %d\n", dev->descriptor.bcdUSB); |
| 703 | LIBMTP_INFO(" bDeviceClass: %d\n", dev->descriptor.bDeviceClass); |
| 704 | LIBMTP_INFO(" bDeviceSubClass: %d\n", dev->descriptor.bDeviceSubClass); |
| 705 | LIBMTP_INFO(" bDeviceProtocol: %d\n", dev->descriptor.bDeviceProtocol); |
| 706 | LIBMTP_INFO(" idVendor: %04x\n", dev->descriptor.idVendor); |
| 707 | LIBMTP_INFO(" idProduct: %04x\n", dev->descriptor.idProduct); |
| 708 | LIBMTP_INFO(" IN endpoint maxpacket: %d bytes\n", ptp_usb->inep_maxpacket); |
| 709 | LIBMTP_INFO(" OUT endpoint maxpacket: %d bytes\n", ptp_usb->outep_maxpacket); |
| 710 | LIBMTP_INFO(" Raw device info:\n"); |
| 711 | LIBMTP_INFO(" Bus location: %d\n", ptp_usb->rawdevice.bus_location); |
| 712 | LIBMTP_INFO(" Device number: %d\n", ptp_usb->rawdevice.devnum); |
| 713 | LIBMTP_INFO(" Device entry info:\n"); |
| 714 | LIBMTP_INFO(" Vendor: %s\n", ptp_usb->rawdevice.device_entry.vendor); |
| 715 | LIBMTP_INFO(" Vendor id: 0x%04x\n", ptp_usb->rawdevice.device_entry.vendor_id); |
| 716 | LIBMTP_INFO(" Product: %s\n", ptp_usb->rawdevice.device_entry.product); |
| 717 | LIBMTP_INFO(" Vendor id: 0x%04x\n", ptp_usb->rawdevice.device_entry.product_id); |
| 718 | 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] | 719 | (void) probe_device_descriptor(dev, stdout); |
Linus Walleij | c6210fb | 2006-05-08 11:11:41 +0000 | [diff] [blame] | 720 | } |
| 721 | |
Linus Walleij | a4e6bdc | 2008-05-23 22:31:53 +0000 | [diff] [blame] | 722 | /** |
| 723 | * Retrieve the apropriate playlist extension for this |
| 724 | * device. Rather hacky at the moment. This is probably |
| 725 | * desired by the managing software, but when creating |
| 726 | * lists on the device itself you notice certain preferences. |
| 727 | * @param ptp_usb the USB device to get suggestion for. |
| 728 | * @return the suggested playlist extension. |
| 729 | */ |
Richard Low | e029eba | 2009-04-11 12:36:25 +0000 | [diff] [blame] | 730 | const char *get_playlist_extension(PTP_USB *ptp_usb) |
Linus Walleij | a4e6bdc | 2008-05-23 22:31:53 +0000 | [diff] [blame] | 731 | { |
| 732 | struct usb_device *dev; |
| 733 | static char creative_pl_extension[] = ".zpl"; |
| 734 | static char default_pl_extension[] = ".pla"; |
| 735 | |
| 736 | dev = usb_device(ptp_usb->handle); |
| 737 | if (dev->descriptor.idVendor == 0x041e) { |
| 738 | return creative_pl_extension; |
| 739 | } |
| 740 | return default_pl_extension; |
| 741 | } |
| 742 | |
Linus Walleij | d1e14e0 | 2008-12-14 01:07:30 +0000 | [diff] [blame] | 743 | static void |
| 744 | libusb_glue_debug (PTPParams *params, const char *format, ...) |
Linus Walleij | 1473595 | 2010-04-25 04:56:49 +0000 | [diff] [blame] | 745 | { |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 746 | va_list args; |
| 747 | |
| 748 | va_start (args, format); |
| 749 | if (params->debug_func!=NULL) |
| 750 | params->debug_func (params->data, format, args); |
| 751 | else |
| 752 | { |
| 753 | vfprintf (stderr, format, args); |
| 754 | fprintf (stderr,"\n"); |
| 755 | fflush (stderr); |
| 756 | } |
| 757 | va_end (args); |
Linus Walleij | 1473595 | 2010-04-25 04:56:49 +0000 | [diff] [blame] | 758 | } |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 759 | |
| 760 | static void |
Linus Walleij | d1e14e0 | 2008-12-14 01:07:30 +0000 | [diff] [blame] | 761 | libusb_glue_error (PTPParams *params, const char *format, ...) |
Linus Walleij | 4955748 | 2010-06-22 09:14:47 +0000 | [diff] [blame] | 762 | { |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 763 | va_list args; |
| 764 | |
| 765 | va_start (args, format); |
| 766 | if (params->error_func!=NULL) |
| 767 | params->error_func (params->data, format, args); |
| 768 | else |
| 769 | { |
| 770 | vfprintf (stderr, format, args); |
| 771 | fprintf (stderr,"\n"); |
| 772 | fflush (stderr); |
| 773 | } |
| 774 | va_end (args); |
| 775 | } |
| 776 | |
Linus Walleij | c6210fb | 2006-05-08 11:11:41 +0000 | [diff] [blame] | 777 | |
Linus Walleij | 99ed83c | 2007-01-02 18:46:02 +0000 | [diff] [blame] | 778 | /* |
| 779 | * ptp_read_func() and ptp_write_func() are |
Linus Walleij | c0a1143 | 2007-03-02 21:21:18 +0000 | [diff] [blame] | 780 | * based on same functions usb.c in libgphoto2. |
Linus Walleij | 99ed83c | 2007-01-02 18:46:02 +0000 | [diff] [blame] | 781 | * Much reading packet logs and having fun with trials and errors |
| 782 | * reveals that WMP / Windows is probably using an algorithm like this |
| 783 | * for large transfers: |
| 784 | * |
Linus Walleij | 4955748 | 2010-06-22 09:14:47 +0000 | [diff] [blame] | 785 | * 1. Send the command (0x0c bytes) if headers are split, else, send |
Linus Walleij | 99ed83c | 2007-01-02 18:46:02 +0000 | [diff] [blame] | 786 | * command plus sizeof(endpoint) - 0x0c bytes. |
| 787 | * 2. Send first packet, max size to be sizeof(endpoint) but only when using |
| 788 | * split headers. Else goto 3. |
| 789 | * 3. REPEAT send 0x10000 byte chunks UNTIL remaining bytes < 0x10000 |
| 790 | * We call 0x10000 CONTEXT_BLOCK_SIZE. |
| 791 | * 4. Send remaining bytes MOD sizeof(endpoint) |
| 792 | * 5. Send remaining bytes. If this happens to be exactly sizeof(endpoint) |
| 793 | * then also send a zero-length package. |
Linus Walleij | 049f50c | 2007-03-03 20:30:43 +0000 | [diff] [blame] | 794 | * |
| 795 | * Further there is some special quirks to handle zero reads from the |
| 796 | * device, since some devices can't do them at all due to shortcomings |
| 797 | * of the USB slave controller in the device. |
Linus Walleij | 99ed83c | 2007-01-02 18:46:02 +0000 | [diff] [blame] | 798 | */ |
Richard Low | 4fd59d6 | 2007-03-03 16:34:37 +0000 | [diff] [blame] | 799 | #define CONTEXT_BLOCK_SIZE_1 0x3e00 |
| 800 | #define CONTEXT_BLOCK_SIZE_2 0x200 |
| 801 | #define CONTEXT_BLOCK_SIZE CONTEXT_BLOCK_SIZE_1+CONTEXT_BLOCK_SIZE_2 |
Linus Walleij | b02a066 | 2006-04-25 08:05:09 +0000 | [diff] [blame] | 802 | static short |
Linus Walleij | 784ac3f | 2006-12-29 10:36:51 +0000 | [diff] [blame] | 803 | ptp_read_func ( |
| 804 | unsigned long size, PTPDataHandler *handler,void *data, |
Richard Low | 4df32f8 | 2007-01-11 20:04:39 +0000 | [diff] [blame] | 805 | unsigned long *readbytes, |
| 806 | int readzero |
Linus Walleij | 784ac3f | 2006-12-29 10:36:51 +0000 | [diff] [blame] | 807 | ) { |
Linus Walleij | b02a066 | 2006-04-25 08:05:09 +0000 | [diff] [blame] | 808 | PTP_USB *ptp_usb = (PTP_USB *)data; |
Linus Walleij | 784ac3f | 2006-12-29 10:36:51 +0000 | [diff] [blame] | 809 | unsigned long toread = 0; |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 810 | int result = 0; |
Linus Walleij | 99ed83c | 2007-01-02 18:46:02 +0000 | [diff] [blame] | 811 | unsigned long curread = 0; |
| 812 | unsigned long written; |
Linus Walleij | 784ac3f | 2006-12-29 10:36:51 +0000 | [diff] [blame] | 813 | unsigned char *bytes; |
Linus Walleij | 049f50c | 2007-03-03 20:30:43 +0000 | [diff] [blame] | 814 | int expect_terminator_byte = 0; |
Linus Walleij | 99ed83c | 2007-01-02 18:46:02 +0000 | [diff] [blame] | 815 | |
| 816 | // This is the largest block we'll need to read in. |
Linus Walleij | 784ac3f | 2006-12-29 10:36:51 +0000 | [diff] [blame] | 817 | bytes = malloc(CONTEXT_BLOCK_SIZE); |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 818 | while (curread < size) { |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 819 | |
| 820 | LIBMTP_USB_DEBUG("Remaining size to read: 0x%04lx bytes\n", size - curread); |
| 821 | |
Richard Low | 4fd59d6 | 2007-03-03 16:34:37 +0000 | [diff] [blame] | 822 | // check equal to condition here |
| 823 | if (size - curread < CONTEXT_BLOCK_SIZE) |
| 824 | { |
| 825 | // this is the last packet |
| 826 | toread = size - curread; |
| 827 | // this is equivalent to zero read for these devices |
Linus Walleij | fec4d56 | 2008-06-01 22:30:36 +0000 | [diff] [blame] | 828 | if (readzero && FLAG_NO_ZERO_READS(ptp_usb) && toread % 64 == 0) { |
Richard Low | 4fd59d6 | 2007-03-03 16:34:37 +0000 | [diff] [blame] | 829 | toread += 1; |
Linus Walleij | 049f50c | 2007-03-03 20:30:43 +0000 | [diff] [blame] | 830 | expect_terminator_byte = 1; |
Richard Low | 4fd59d6 | 2007-03-03 16:34:37 +0000 | [diff] [blame] | 831 | } |
Linus Walleij | c49c637 | 2007-01-09 00:18:51 +0000 | [diff] [blame] | 832 | } |
Richard Low | 4fd59d6 | 2007-03-03 16:34:37 +0000 | [diff] [blame] | 833 | else if (curread == 0) |
| 834 | // we are first packet, but not last packet |
| 835 | toread = CONTEXT_BLOCK_SIZE_1; |
| 836 | else if (toread == CONTEXT_BLOCK_SIZE_1) |
| 837 | toread = CONTEXT_BLOCK_SIZE_2; |
| 838 | else if (toread == CONTEXT_BLOCK_SIZE_2) |
| 839 | toread = CONTEXT_BLOCK_SIZE_1; |
| 840 | else |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 841 | LIBMTP_INFO("unexpected toread size 0x%04x, 0x%04x remaining bytes\n", |
Linus Walleij | 4cec987 | 2007-03-03 21:00:53 +0000 | [diff] [blame] | 842 | (unsigned int) toread, (unsigned int) (size-curread)); |
Linus Walleij | 784ac3f | 2006-12-29 10:36:51 +0000 | [diff] [blame] | 843 | |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 844 | LIBMTP_USB_DEBUG("Reading in 0x%04lx bytes\n", toread); |
| 845 | |
Linus Walleij | 2f62281 | 2008-08-30 22:06:58 +0000 | [diff] [blame] | 846 | result = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep, (char*)bytes, toread, ptp_usb->timeout); |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 847 | |
| 848 | LIBMTP_USB_DEBUG("Result of read: 0x%04x\n", result); |
Linus Walleij | 4955748 | 2010-06-22 09:14:47 +0000 | [diff] [blame] | 849 | |
Linus Walleij | c49c637 | 2007-01-09 00:18:51 +0000 | [diff] [blame] | 850 | if (result < 0) { |
Richard Low | 43fdb88 | 2006-09-06 16:19:05 +0000 | [diff] [blame] | 851 | return PTP_ERROR_IO; |
Linus Walleij | c49c637 | 2007-01-09 00:18:51 +0000 | [diff] [blame] | 852 | } |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 853 | |
| 854 | LIBMTP_USB_DEBUG("<==USB IN\n"); |
Richard Low | 4df32f8 | 2007-01-11 20:04:39 +0000 | [diff] [blame] | 855 | if (result == 0) |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 856 | LIBMTP_USB_DEBUG("Zero Read\n"); |
Richard Low | 4df32f8 | 2007-01-11 20:04:39 +0000 | [diff] [blame] | 857 | else |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 858 | LIBMTP_USB_DATA(bytes, result, 16); |
Linus Walleij | 4955748 | 2010-06-22 09:14:47 +0000 | [diff] [blame] | 859 | |
Richard Low | 4fd59d6 | 2007-03-03 16:34:37 +0000 | [diff] [blame] | 860 | // want to discard extra byte |
Linus Walleij | 049f50c | 2007-03-03 20:30:43 +0000 | [diff] [blame] | 861 | if (expect_terminator_byte && result == toread) |
Richard Low | bf2675c | 2007-03-18 18:20:26 +0000 | [diff] [blame] | 862 | { |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 863 | LIBMTP_USB_DEBUG("<==USB IN\nDiscarding extra byte\n"); |
| 864 | |
Richard Low | 4fd59d6 | 2007-03-03 16:34:37 +0000 | [diff] [blame] | 865 | result--; |
Richard Low | bf2675c | 2007-03-18 18:20:26 +0000 | [diff] [blame] | 866 | } |
Linus Walleij | 4955748 | 2010-06-22 09:14:47 +0000 | [diff] [blame] | 867 | |
Linus Walleij | d866d24 | 2009-08-23 21:50:39 +0000 | [diff] [blame] | 868 | int putfunc_ret = handler->putfunc(NULL, handler->priv, result, bytes, &written); |
Richard Low | d24f923 | 2009-05-03 10:22:08 +0000 | [diff] [blame] | 869 | if (putfunc_ret != PTP_RC_OK) |
| 870 | return putfunc_ret; |
Linus Walleij | 4955748 | 2010-06-22 09:14:47 +0000 | [diff] [blame] | 871 | |
Linus Walleij | c49c637 | 2007-01-09 00:18:51 +0000 | [diff] [blame] | 872 | ptp_usb->current_transfer_complete += result; |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 873 | curread += result; |
Linus Walleij | c49c637 | 2007-01-09 00:18:51 +0000 | [diff] [blame] | 874 | |
| 875 | // Increase counters, call callback |
| 876 | if (ptp_usb->callback_active) { |
| 877 | if (ptp_usb->current_transfer_complete >= ptp_usb->current_transfer_total) { |
| 878 | // send last update and disable callback. |
| 879 | ptp_usb->current_transfer_complete = ptp_usb->current_transfer_total; |
| 880 | ptp_usb->callback_active = 0; |
| 881 | } |
| 882 | if (ptp_usb->current_transfer_callback != NULL) { |
Linus Walleij | bd3bf9e | 2007-09-14 19:31:54 +0000 | [diff] [blame] | 883 | int ret; |
| 884 | ret = ptp_usb->current_transfer_callback(ptp_usb->current_transfer_complete, |
| 885 | ptp_usb->current_transfer_total, |
| 886 | ptp_usb->current_transfer_callback_data); |
| 887 | if (ret != 0) { |
| 888 | return PTP_ERROR_CANCEL; |
| 889 | } |
Linus Walleij | c49c637 | 2007-01-09 00:18:51 +0000 | [diff] [blame] | 890 | } |
Linus Walleij | 4955748 | 2010-06-22 09:14:47 +0000 | [diff] [blame] | 891 | } |
Linus Walleij | c49c637 | 2007-01-09 00:18:51 +0000 | [diff] [blame] | 892 | |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 893 | if (result < toread) /* short reads are common */ |
| 894 | break; |
| 895 | } |
Linus Walleij | 784ac3f | 2006-12-29 10:36:51 +0000 | [diff] [blame] | 896 | if (readbytes) *readbytes = curread; |
| 897 | free (bytes); |
Linus Walleij | 4955748 | 2010-06-22 09:14:47 +0000 | [diff] [blame] | 898 | |
Richard Low | 4df32f8 | 2007-01-11 20:04:39 +0000 | [diff] [blame] | 899 | // there might be a zero packet waiting for us... |
Linus Walleij | 4955748 | 2010-06-22 09:14:47 +0000 | [diff] [blame] | 900 | if (readzero && |
| 901 | !FLAG_NO_ZERO_READS(ptp_usb) && |
Linus Walleij | 049f50c | 2007-03-03 20:30:43 +0000 | [diff] [blame] | 902 | curread % ptp_usb->outep_maxpacket == 0) { |
Richard Low | 4df32f8 | 2007-01-11 20:04:39 +0000 | [diff] [blame] | 903 | char temp; |
| 904 | int zeroresult = 0; |
Linus Walleij | 049f50c | 2007-03-03 20:30:43 +0000 | [diff] [blame] | 905 | |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 906 | LIBMTP_USB_DEBUG("<==USB IN\n"); |
| 907 | LIBMTP_USB_DEBUG("Zero Read\n"); |
| 908 | |
Linus Walleij | 2f62281 | 2008-08-30 22:06:58 +0000 | [diff] [blame] | 909 | zeroresult = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep, &temp, 0, ptp_usb->timeout); |
Richard Low | 4df32f8 | 2007-01-11 20:04:39 +0000 | [diff] [blame] | 910 | if (zeroresult != 0) |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 911 | 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] | 912 | } |
Linus Walleij | 4955748 | 2010-06-22 09:14:47 +0000 | [diff] [blame] | 913 | |
Richard Low | 4d9165f | 2008-09-23 20:13:17 +0000 | [diff] [blame] | 914 | return PTP_RC_OK; |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 915 | } |
| 916 | |
Linus Walleij | b02a066 | 2006-04-25 08:05:09 +0000 | [diff] [blame] | 917 | static short |
Linus Walleij | 784ac3f | 2006-12-29 10:36:51 +0000 | [diff] [blame] | 918 | ptp_write_func ( |
| 919 | unsigned long size, |
| 920 | PTPDataHandler *handler, |
| 921 | void *data, |
| 922 | unsigned long *written |
| 923 | ) { |
Linus Walleij | b02a066 | 2006-04-25 08:05:09 +0000 | [diff] [blame] | 924 | PTP_USB *ptp_usb = (PTP_USB *)data; |
Linus Walleij | 784ac3f | 2006-12-29 10:36:51 +0000 | [diff] [blame] | 925 | unsigned long towrite = 0; |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 926 | int result = 0; |
Linus Walleij | 784ac3f | 2006-12-29 10:36:51 +0000 | [diff] [blame] | 927 | unsigned long curwrite = 0; |
| 928 | unsigned char *bytes; |
Linus Walleij | 99ed83c | 2007-01-02 18:46:02 +0000 | [diff] [blame] | 929 | |
Linus Walleij | 4955748 | 2010-06-22 09:14:47 +0000 | [diff] [blame] | 930 | // This is the largest block we'll need to read in. |
Linus Walleij | 784ac3f | 2006-12-29 10:36:51 +0000 | [diff] [blame] | 931 | bytes = malloc(CONTEXT_BLOCK_SIZE); |
Linus Walleij | c49c637 | 2007-01-09 00:18:51 +0000 | [diff] [blame] | 932 | if (!bytes) { |
| 933 | return PTP_ERROR_IO; |
| 934 | } |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 935 | while (curwrite < size) { |
Linus Walleij | b84b385 | 2009-03-04 15:11:08 +0000 | [diff] [blame] | 936 | unsigned long usbwritten = 0; |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 937 | towrite = size-curwrite; |
Linus Walleij | c49c637 | 2007-01-09 00:18:51 +0000 | [diff] [blame] | 938 | if (towrite > CONTEXT_BLOCK_SIZE) { |
Richard Low | 43fdb88 | 2006-09-06 16:19:05 +0000 | [diff] [blame] | 939 | towrite = CONTEXT_BLOCK_SIZE; |
Linus Walleij | c49c637 | 2007-01-09 00:18:51 +0000 | [diff] [blame] | 940 | } else { |
| 941 | // This magic makes packets the same size that WMP send them. |
| 942 | if (towrite > ptp_usb->outep_maxpacket && towrite % ptp_usb->outep_maxpacket != 0) { |
Richard Low | 021421e | 2007-01-01 18:08:57 +0000 | [diff] [blame] | 943 | towrite -= towrite % ptp_usb->outep_maxpacket; |
Linus Walleij | c49c637 | 2007-01-09 00:18:51 +0000 | [diff] [blame] | 944 | } |
| 945 | } |
Linus Walleij | d866d24 | 2009-08-23 21:50:39 +0000 | [diff] [blame] | 946 | int getfunc_ret = handler->getfunc(NULL, handler->priv,towrite,bytes,&towrite); |
Richard Low | d24f923 | 2009-05-03 10:22:08 +0000 | [diff] [blame] | 947 | if (getfunc_ret != PTP_RC_OK) |
| 948 | return getfunc_ret; |
Linus Walleij | b84b385 | 2009-03-04 15:11:08 +0000 | [diff] [blame] | 949 | while (usbwritten < towrite) { |
| 950 | result = USB_BULK_WRITE(ptp_usb->handle,ptp_usb->outep,((char*)bytes+usbwritten),towrite-usbwritten,ptp_usb->timeout); |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 951 | |
| 952 | LIBMTP_USB_DEBUG("USB OUT==>\n"); |
| 953 | LIBMTP_USB_DATA(bytes+usbwritten, result, 16); |
| 954 | |
Linus Walleij | b84b385 | 2009-03-04 15:11:08 +0000 | [diff] [blame] | 955 | if (result < 0) { |
| 956 | return PTP_ERROR_IO; |
| 957 | } |
| 958 | // check for result == 0 perhaps too. |
| 959 | // Increase counters |
| 960 | ptp_usb->current_transfer_complete += result; |
| 961 | curwrite += result; |
| 962 | usbwritten += result; |
Linus Walleij | c49c637 | 2007-01-09 00:18:51 +0000 | [diff] [blame] | 963 | } |
Linus Walleij | c49c637 | 2007-01-09 00:18:51 +0000 | [diff] [blame] | 964 | // call callback |
| 965 | if (ptp_usb->callback_active) { |
| 966 | if (ptp_usb->current_transfer_complete >= ptp_usb->current_transfer_total) { |
| 967 | // send last update and disable callback. |
| 968 | ptp_usb->current_transfer_complete = ptp_usb->current_transfer_total; |
| 969 | ptp_usb->callback_active = 0; |
| 970 | } |
| 971 | if (ptp_usb->current_transfer_callback != NULL) { |
Linus Walleij | bd3bf9e | 2007-09-14 19:31:54 +0000 | [diff] [blame] | 972 | int ret; |
| 973 | ret = ptp_usb->current_transfer_callback(ptp_usb->current_transfer_complete, |
| 974 | ptp_usb->current_transfer_total, |
| 975 | ptp_usb->current_transfer_callback_data); |
| 976 | if (ret != 0) { |
| 977 | return PTP_ERROR_CANCEL; |
| 978 | } |
Linus Walleij | c49c637 | 2007-01-09 00:18:51 +0000 | [diff] [blame] | 979 | } |
| 980 | } |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 981 | if (result < towrite) /* short writes happen */ |
| 982 | break; |
| 983 | } |
Linus Walleij | 784ac3f | 2006-12-29 10:36:51 +0000 | [diff] [blame] | 984 | free (bytes); |
Linus Walleij | c49c637 | 2007-01-09 00:18:51 +0000 | [diff] [blame] | 985 | if (written) { |
| 986 | *written = curwrite; |
Linus Walleij | 80d134a | 2006-08-22 21:41:37 +0000 | [diff] [blame] | 987 | } |
Linus Walleij | c49c637 | 2007-01-09 00:18:51 +0000 | [diff] [blame] | 988 | |
Richard Low | ef05b89 | 2007-01-03 21:18:56 +0000 | [diff] [blame] | 989 | // If this is the last transfer send a zero write if required |
| 990 | if (ptp_usb->current_transfer_complete >= ptp_usb->current_transfer_total) { |
Richard Low | 68f4588 | 2007-01-03 10:08:31 +0000 | [diff] [blame] | 991 | if ((towrite % ptp_usb->outep_maxpacket) == 0) { |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 992 | |
| 993 | LIBMTP_USB_DEBUG("USB OUT==>\n"); |
| 994 | LIBMTP_USB_DEBUG("Zero Write\n"); |
| 995 | |
Linus Walleij | 2f62281 | 2008-08-30 22:06:58 +0000 | [diff] [blame] | 996 | result=USB_BULK_WRITE(ptp_usb->handle,ptp_usb->outep,(char *)"x",0,ptp_usb->timeout); |
Linus Walleij | 7f0c72e | 2006-09-06 13:01:58 +0000 | [diff] [blame] | 997 | } |
Linus Walleij | d214b9b | 2006-08-26 22:08:37 +0000 | [diff] [blame] | 998 | } |
Linus Walleij | 4955748 | 2010-06-22 09:14:47 +0000 | [diff] [blame] | 999 | |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1000 | if (result < 0) |
| 1001 | return PTP_ERROR_IO; |
| 1002 | return PTP_RC_OK; |
Linus Walleij | b02a066 | 2006-04-25 08:05:09 +0000 | [diff] [blame] | 1003 | } |
| 1004 | |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1005 | /* memory data get/put handler */ |
| 1006 | typedef struct { |
| 1007 | unsigned char *data; |
| 1008 | unsigned long size, curoff; |
| 1009 | } PTPMemHandlerPrivate; |
Linus Walleij | 784ac3f | 2006-12-29 10:36:51 +0000 | [diff] [blame] | 1010 | |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1011 | static uint16_t |
| 1012 | memory_getfunc(PTPParams* params, void* private, |
| 1013 | unsigned long wantlen, unsigned char *data, |
| 1014 | unsigned long *gotlen |
| 1015 | ) { |
| 1016 | PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)private; |
| 1017 | unsigned long tocopy = wantlen; |
| 1018 | |
| 1019 | if (priv->curoff + tocopy > priv->size) |
| 1020 | tocopy = priv->size - priv->curoff; |
| 1021 | memcpy (data, priv->data + priv->curoff, tocopy); |
| 1022 | priv->curoff += tocopy; |
| 1023 | *gotlen = tocopy; |
| 1024 | return PTP_RC_OK; |
Linus Walleij | b02a066 | 2006-04-25 08:05:09 +0000 | [diff] [blame] | 1025 | } |
| 1026 | |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1027 | static uint16_t |
| 1028 | memory_putfunc(PTPParams* params, void* private, |
| 1029 | unsigned long sendlen, unsigned char *data, |
| 1030 | unsigned long *putlen |
| 1031 | ) { |
| 1032 | PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)private; |
| 1033 | |
| 1034 | if (priv->curoff + sendlen > priv->size) { |
| 1035 | priv->data = realloc (priv->data, priv->curoff+sendlen); |
| 1036 | priv->size = priv->curoff + sendlen; |
| 1037 | } |
| 1038 | memcpy (priv->data + priv->curoff, data, sendlen); |
| 1039 | priv->curoff += sendlen; |
| 1040 | *putlen = sendlen; |
| 1041 | return PTP_RC_OK; |
| 1042 | } |
| 1043 | |
| 1044 | /* init private struct for receiving data. */ |
| 1045 | static uint16_t |
| 1046 | ptp_init_recv_memory_handler(PTPDataHandler *handler) { |
| 1047 | PTPMemHandlerPrivate* priv; |
| 1048 | priv = malloc (sizeof(PTPMemHandlerPrivate)); |
Linus Walleij | d866d24 | 2009-08-23 21:50:39 +0000 | [diff] [blame] | 1049 | handler->priv = priv; |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1050 | handler->getfunc = memory_getfunc; |
| 1051 | handler->putfunc = memory_putfunc; |
| 1052 | priv->data = NULL; |
| 1053 | priv->size = 0; |
| 1054 | priv->curoff = 0; |
| 1055 | return PTP_RC_OK; |
| 1056 | } |
| 1057 | |
| 1058 | /* init private struct and put data in for sending data. |
| 1059 | * data is still owned by caller. |
| 1060 | */ |
| 1061 | static uint16_t |
| 1062 | ptp_init_send_memory_handler(PTPDataHandler *handler, |
| 1063 | unsigned char *data, unsigned long len |
| 1064 | ) { |
| 1065 | PTPMemHandlerPrivate* priv; |
| 1066 | priv = malloc (sizeof(PTPMemHandlerPrivate)); |
| 1067 | if (!priv) |
| 1068 | return PTP_RC_GeneralError; |
Linus Walleij | d866d24 | 2009-08-23 21:50:39 +0000 | [diff] [blame] | 1069 | handler->priv = priv; |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1070 | handler->getfunc = memory_getfunc; |
| 1071 | handler->putfunc = memory_putfunc; |
| 1072 | priv->data = data; |
| 1073 | priv->size = len; |
| 1074 | priv->curoff = 0; |
| 1075 | return PTP_RC_OK; |
| 1076 | } |
| 1077 | |
| 1078 | /* free private struct + data */ |
| 1079 | static uint16_t |
| 1080 | ptp_exit_send_memory_handler (PTPDataHandler *handler) { |
Linus Walleij | d866d24 | 2009-08-23 21:50:39 +0000 | [diff] [blame] | 1081 | PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)handler->priv; |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1082 | /* data is owned by caller */ |
| 1083 | free (priv); |
| 1084 | return PTP_RC_OK; |
| 1085 | } |
| 1086 | |
| 1087 | /* hand over our internal data to caller */ |
| 1088 | static uint16_t |
| 1089 | ptp_exit_recv_memory_handler (PTPDataHandler *handler, |
| 1090 | unsigned char **data, unsigned long *size |
| 1091 | ) { |
Linus Walleij | d866d24 | 2009-08-23 21:50:39 +0000 | [diff] [blame] | 1092 | PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)handler->priv; |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1093 | *data = priv->data; |
| 1094 | *size = priv->size; |
| 1095 | free (priv); |
| 1096 | return PTP_RC_OK; |
| 1097 | } |
| 1098 | |
| 1099 | /* send / receive functions */ |
| 1100 | |
| 1101 | uint16_t |
| 1102 | ptp_usb_sendreq (PTPParams* params, PTPContainer* req) |
| 1103 | { |
| 1104 | uint16_t ret; |
| 1105 | PTPUSBBulkContainer usbreq; |
| 1106 | PTPDataHandler memhandler; |
Linus Walleij | 5ba4040 | 2008-05-23 21:36:54 +0000 | [diff] [blame] | 1107 | unsigned long written = 0; |
| 1108 | unsigned long towrite; |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 1109 | |
Linus Walleij | b8f78c7 | 2008-03-02 20:22:56 +0000 | [diff] [blame] | 1110 | char txt[256]; |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1111 | |
Linus Walleij | b8f78c7 | 2008-03-02 20:22:56 +0000 | [diff] [blame] | 1112 | (void) ptp_render_opcode (params, req->Code, sizeof(txt), txt); |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 1113 | LIBMTP_USB_DEBUG("REQUEST: 0x%04x, %s\n", req->Code, txt); |
| 1114 | |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1115 | /* build appropriate USB container */ |
| 1116 | usbreq.length=htod32(PTP_USB_BULK_REQ_LEN- |
| 1117 | (sizeof(uint32_t)*(5-req->Nparam))); |
| 1118 | usbreq.type=htod16(PTP_USB_CONTAINER_COMMAND); |
| 1119 | usbreq.code=htod16(req->Code); |
| 1120 | usbreq.trans_id=htod32(req->Transaction_ID); |
| 1121 | usbreq.payload.params.param1=htod32(req->Param1); |
| 1122 | usbreq.payload.params.param2=htod32(req->Param2); |
| 1123 | usbreq.payload.params.param3=htod32(req->Param3); |
| 1124 | usbreq.payload.params.param4=htod32(req->Param4); |
| 1125 | usbreq.payload.params.param5=htod32(req->Param5); |
| 1126 | /* send it to responder */ |
| 1127 | towrite = PTP_USB_BULK_REQ_LEN-(sizeof(uint32_t)*(5-req->Nparam)); |
| 1128 | ptp_init_send_memory_handler (&memhandler, (unsigned char*)&usbreq, towrite); |
| 1129 | ret=ptp_write_func( |
| 1130 | towrite, |
| 1131 | &memhandler, |
| 1132 | params->data, |
| 1133 | &written |
| 1134 | ); |
| 1135 | ptp_exit_send_memory_handler (&memhandler); |
Linus Walleij | ff01cb1 | 2007-09-16 19:49:48 +0000 | [diff] [blame] | 1136 | if (ret!=PTP_RC_OK && ret!=PTP_ERROR_CANCEL) { |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1137 | ret = PTP_ERROR_IO; |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1138 | } |
Linus Walleij | 724f010 | 2007-09-17 20:10:12 +0000 | [diff] [blame] | 1139 | if (written != towrite && ret != PTP_ERROR_CANCEL && ret != PTP_ERROR_IO) { |
Linus Walleij | 4955748 | 2010-06-22 09:14:47 +0000 | [diff] [blame] | 1140 | libusb_glue_error (params, |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1141 | "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] | 1142 | req->Code, written, towrite |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1143 | ); |
| 1144 | ret = PTP_ERROR_IO; |
| 1145 | } |
| 1146 | return ret; |
| 1147 | } |
| 1148 | |
| 1149 | uint16_t |
| 1150 | ptp_usb_senddata (PTPParams* params, PTPContainer* ptp, |
| 1151 | unsigned long size, PTPDataHandler *handler |
| 1152 | ) { |
| 1153 | uint16_t ret; |
| 1154 | int wlen, datawlen; |
| 1155 | unsigned long written; |
| 1156 | PTPUSBBulkContainer usbdata; |
| 1157 | uint32_t bytes_left_to_transfer; |
| 1158 | PTPDataHandler memhandler; |
| 1159 | |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 1160 | |
| 1161 | LIBMTP_USB_DEBUG("SEND DATA PHASE\n"); |
| 1162 | |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1163 | /* build appropriate USB container */ |
| 1164 | usbdata.length = htod32(PTP_USB_BULK_HDR_LEN+size); |
| 1165 | usbdata.type = htod16(PTP_USB_CONTAINER_DATA); |
| 1166 | usbdata.code = htod16(ptp->Code); |
| 1167 | usbdata.trans_id= htod32(ptp->Transaction_ID); |
Linus Walleij | 1473595 | 2010-04-25 04:56:49 +0000 | [diff] [blame] | 1168 | |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1169 | ((PTP_USB*)params->data)->current_transfer_complete = 0; |
Richard Low | 4eb0112 | 2007-02-24 10:11:32 +0000 | [diff] [blame] | 1170 | ((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] | 1171 | |
| 1172 | if (params->split_header_data) { |
| 1173 | datawlen = 0; |
| 1174 | wlen = PTP_USB_BULK_HDR_LEN; |
| 1175 | } else { |
| 1176 | unsigned long gotlen; |
| 1177 | /* For all camera devices. */ |
Richard Low | 02724f6 | 2007-02-17 09:47:26 +0000 | [diff] [blame] | 1178 | 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] | 1179 | wlen = PTP_USB_BULK_HDR_LEN + datawlen; |
Linus Walleij | 1473595 | 2010-04-25 04:56:49 +0000 | [diff] [blame] | 1180 | |
Linus Walleij | d866d24 | 2009-08-23 21:50:39 +0000 | [diff] [blame] | 1181 | ret = handler->getfunc(params, handler->priv, datawlen, usbdata.payload.data, &gotlen); |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1182 | if (ret != PTP_RC_OK) |
| 1183 | return ret; |
| 1184 | if (gotlen != datawlen) |
| 1185 | return PTP_RC_GeneralError; |
| 1186 | } |
| 1187 | ptp_init_send_memory_handler (&memhandler, (unsigned char *)&usbdata, wlen); |
| 1188 | /* send first part of data */ |
| 1189 | ret = ptp_write_func(wlen, &memhandler, params->data, &written); |
| 1190 | ptp_exit_send_memory_handler (&memhandler); |
| 1191 | if (ret!=PTP_RC_OK) { |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1192 | return ret; |
| 1193 | } |
| 1194 | if (size <= datawlen) return ret; |
| 1195 | /* if everything OK send the rest */ |
| 1196 | bytes_left_to_transfer = size-datawlen; |
| 1197 | ret = PTP_RC_OK; |
| 1198 | while(bytes_left_to_transfer > 0) { |
| 1199 | ret = ptp_write_func (bytes_left_to_transfer, handler, params->data, &written); |
| 1200 | if (ret != PTP_RC_OK) |
| 1201 | break; |
| 1202 | if (written == 0) { |
| 1203 | ret = PTP_ERROR_IO; |
| 1204 | break; |
| 1205 | } |
| 1206 | bytes_left_to_transfer -= written; |
| 1207 | } |
Linus Walleij | ff01cb1 | 2007-09-16 19:49:48 +0000 | [diff] [blame] | 1208 | if (ret!=PTP_RC_OK && ret!=PTP_ERROR_CANCEL) |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1209 | ret = PTP_ERROR_IO; |
| 1210 | return ret; |
| 1211 | } |
| 1212 | |
| 1213 | static uint16_t ptp_usb_getpacket(PTPParams *params, |
| 1214 | PTPUSBBulkContainer *packet, unsigned long *rlen) |
| 1215 | { |
| 1216 | PTPDataHandler memhandler; |
| 1217 | uint16_t ret; |
| 1218 | unsigned char *x = NULL; |
| 1219 | |
| 1220 | /* read the header and potentially the first data */ |
| 1221 | if (params->response_packet_size > 0) { |
| 1222 | /* If there is a buffered packet, just use it. */ |
| 1223 | memcpy(packet, params->response_packet, params->response_packet_size); |
| 1224 | *rlen = params->response_packet_size; |
| 1225 | free(params->response_packet); |
| 1226 | params->response_packet = NULL; |
| 1227 | params->response_packet_size = 0; |
| 1228 | /* Here this signifies a "virtual read" */ |
| 1229 | return PTP_RC_OK; |
| 1230 | } |
| 1231 | ptp_init_recv_memory_handler (&memhandler); |
Richard Low | 02724f6 | 2007-02-17 09:47:26 +0000 | [diff] [blame] | 1232 | 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] | 1233 | ptp_exit_recv_memory_handler (&memhandler, &x, rlen); |
| 1234 | if (x) { |
| 1235 | memcpy (packet, x, *rlen); |
| 1236 | free (x); |
| 1237 | } |
| 1238 | return ret; |
| 1239 | } |
| 1240 | |
| 1241 | uint16_t |
| 1242 | ptp_usb_getdata (PTPParams* params, PTPContainer* ptp, PTPDataHandler *handler) |
| 1243 | { |
| 1244 | uint16_t ret; |
| 1245 | PTPUSBBulkContainer usbdata; |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1246 | unsigned long written; |
Linus Walleij | 8e3af00 | 2007-09-28 19:21:54 +0000 | [diff] [blame] | 1247 | PTP_USB *ptp_usb = (PTP_USB *) params->data; |
Linus Walleij | 1473595 | 2010-04-25 04:56:49 +0000 | [diff] [blame] | 1248 | int putfunc_ret; |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 1249 | |
| 1250 | LIBMTP_USB_DEBUG("GET DATA PHASE\n"); |
| 1251 | |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1252 | memset(&usbdata,0,sizeof(usbdata)); |
| 1253 | do { |
| 1254 | unsigned long len, rlen; |
| 1255 | |
| 1256 | ret = ptp_usb_getpacket(params, &usbdata, &rlen); |
| 1257 | if (ret!=PTP_RC_OK) { |
| 1258 | ret = PTP_ERROR_IO; |
| 1259 | break; |
Linus Walleij | 8e3af00 | 2007-09-28 19:21:54 +0000 | [diff] [blame] | 1260 | } |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1261 | if (dtoh16(usbdata.type)!=PTP_USB_CONTAINER_DATA) { |
| 1262 | ret = PTP_ERROR_DATA_EXPECTED; |
| 1263 | break; |
Linus Walleij | 8e3af00 | 2007-09-28 19:21:54 +0000 | [diff] [blame] | 1264 | } |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1265 | if (dtoh16(usbdata.code)!=ptp->Code) { |
Linus Walleij | fec4d56 | 2008-06-01 22:30:36 +0000 | [diff] [blame] | 1266 | if (FLAG_IGNORE_HEADER_ERRORS(ptp_usb)) { |
Linus Walleij | d1e14e0 | 2008-12-14 01:07:30 +0000 | [diff] [blame] | 1267 | libusb_glue_debug (params, "ptp2/ptp_usb_getdata: detected a broken " |
Linus Walleij | 913a306 | 2007-09-28 21:42:39 +0000 | [diff] [blame] | 1268 | "PTP header, code field insane, expect problems! (But continuing)"); |
Linus Walleij | 0654212 | 2007-10-14 22:13:48 +0000 | [diff] [blame] | 1269 | // Repair the header, so it won't wreak more havoc, don't just ignore it. |
| 1270 | // Typically these two fields will be broken. |
| 1271 | usbdata.code = htod16(ptp->Code); |
| 1272 | usbdata.trans_id = htod32(ptp->Transaction_ID); |
Linus Walleij | 94cd972 | 2007-09-28 21:29:53 +0000 | [diff] [blame] | 1273 | ret = PTP_RC_OK; |
Linus Walleij | 8e3af00 | 2007-09-28 19:21:54 +0000 | [diff] [blame] | 1274 | } else { |
Linus Walleij | 913a306 | 2007-09-28 21:42:39 +0000 | [diff] [blame] | 1275 | ret = dtoh16(usbdata.code); |
| 1276 | // This filters entirely insane garbage return codes, but still |
| 1277 | // makes it possible to return error codes in the code field when |
Linus Walleij | 4955748 | 2010-06-22 09:14:47 +0000 | [diff] [blame] | 1278 | // getting data. It appears Windows ignores the contents of this |
Linus Walleij | 913a306 | 2007-09-28 21:42:39 +0000 | [diff] [blame] | 1279 | // field entirely. |
| 1280 | if (ret < PTP_RC_Undefined || ret > PTP_RC_SpecificationOfDestinationUnsupported) { |
Linus Walleij | d1e14e0 | 2008-12-14 01:07:30 +0000 | [diff] [blame] | 1281 | libusb_glue_debug (params, "ptp2/ptp_usb_getdata: detected a broken " |
Linus Walleij | 913a306 | 2007-09-28 21:42:39 +0000 | [diff] [blame] | 1282 | "PTP header, code field insane."); |
| 1283 | ret = PTP_ERROR_IO; |
| 1284 | } |
Linus Walleij | 8e3af00 | 2007-09-28 19:21:54 +0000 | [diff] [blame] | 1285 | break; |
| 1286 | } |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1287 | } |
| 1288 | if (usbdata.length == 0xffffffffU) { |
Linus Walleij | 1473595 | 2010-04-25 04:56:49 +0000 | [diff] [blame] | 1289 | /* Copy first part of data to 'data' */ |
| 1290 | putfunc_ret = |
| 1291 | handler->putfunc( |
| 1292 | params, handler->priv, rlen - PTP_USB_BULK_HDR_LEN, usbdata.payload.data, |
| 1293 | &written |
| 1294 | ); |
| 1295 | if (putfunc_ret != PTP_RC_OK) |
| 1296 | return putfunc_ret; |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1297 | |
Linus Walleij | 1473595 | 2010-04-25 04:56:49 +0000 | [diff] [blame] | 1298 | /* stuff data directly to passed data handler */ |
| 1299 | while (1) { |
| 1300 | unsigned long readdata; |
| 1301 | uint16_t xret; |
| 1302 | |
| 1303 | xret = ptp_read_func( |
| 1304 | PTP_USB_BULK_HS_MAX_PACKET_LEN_READ, |
| 1305 | handler, |
| 1306 | params->data, |
| 1307 | &readdata, |
| 1308 | 0 |
| 1309 | ); |
| 1310 | if (xret != PTP_RC_OK) |
| 1311 | return xret; |
| 1312 | if (readdata < PTP_USB_BULK_HS_MAX_PACKET_LEN_READ) |
| 1313 | break; |
| 1314 | } |
| 1315 | return PTP_RC_OK; |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1316 | } |
| 1317 | if (rlen > dtoh32(usbdata.length)) { |
| 1318 | /* |
| 1319 | * Buffer the surplus response packet if it is >= |
| 1320 | * PTP_USB_BULK_HDR_LEN |
| 1321 | * (i.e. it is probably an entire package) |
| 1322 | * else discard it as erroneous surplus data. |
| 1323 | * This will even work if more than 2 packets appear |
| 1324 | * in the same transaction, they will just be handled |
| 1325 | * iteratively. |
| 1326 | * |
| 1327 | * Marcus observed stray bytes on iRiver devices; |
| 1328 | * these are still discarded. |
| 1329 | */ |
| 1330 | unsigned int packlen = dtoh32(usbdata.length); |
| 1331 | unsigned int surplen = rlen - packlen; |
| 1332 | |
| 1333 | if (surplen >= PTP_USB_BULK_HDR_LEN) { |
| 1334 | params->response_packet = malloc(surplen); |
| 1335 | memcpy(params->response_packet, |
| 1336 | (uint8_t *) &usbdata + packlen, surplen); |
| 1337 | params->response_packet_size = surplen; |
tedbullock | 36f2afb | 2007-03-03 22:16:44 +0000 | [diff] [blame] | 1338 | /* Ignore reading one extra byte if device flags have been set */ |
Linus Walleij | fec4d56 | 2008-06-01 22:30:36 +0000 | [diff] [blame] | 1339 | } else if(!FLAG_NO_ZERO_READS(ptp_usb) && |
| 1340 | (rlen - dtoh32(usbdata.length) == 1)) { |
Linus Walleij | d1e14e0 | 2008-12-14 01:07:30 +0000 | [diff] [blame] | 1341 | libusb_glue_debug (params, "ptp2/ptp_usb_getdata: read %d bytes " |
Linus Walleij | 4955748 | 2010-06-22 09:14:47 +0000 | [diff] [blame] | 1342 | "too much, expect problems!", |
Linus Walleij | 8e3af00 | 2007-09-28 19:21:54 +0000 | [diff] [blame] | 1343 | rlen - dtoh32(usbdata.length)); |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1344 | } |
| 1345 | rlen = packlen; |
| 1346 | } |
| 1347 | |
| 1348 | /* For most PTP devices rlen is 512 == sizeof(usbdata) |
| 1349 | * here. For MTP devices splitting header and data it might |
| 1350 | * be 12. |
| 1351 | */ |
| 1352 | /* Evaluate full data length. */ |
| 1353 | len=dtoh32(usbdata.length)-PTP_USB_BULK_HDR_LEN; |
| 1354 | |
| 1355 | /* autodetect split header/data MTP devices */ |
| 1356 | if (dtoh32(usbdata.length) > 12 && (rlen==12)) |
| 1357 | params->split_header_data = 1; |
| 1358 | |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1359 | /* Copy first part of data to 'data' */ |
Linus Walleij | 1473595 | 2010-04-25 04:56:49 +0000 | [diff] [blame] | 1360 | putfunc_ret = |
| 1361 | handler->putfunc( |
| 1362 | params, handler->priv, rlen - PTP_USB_BULK_HDR_LEN, |
| 1363 | usbdata.payload.data, |
| 1364 | &written |
| 1365 | ); |
| 1366 | if (putfunc_ret != PTP_RC_OK) |
| 1367 | return putfunc_ret; |
Linus Walleij | d866d24 | 2009-08-23 21:50:39 +0000 | [diff] [blame] | 1368 | |
| 1369 | if (FLAG_NO_ZERO_READS(ptp_usb) && |
Linus Walleij | 049f50c | 2007-03-03 20:30:43 +0000 | [diff] [blame] | 1370 | 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] | 1371 | |
| 1372 | LIBMTP_USB_DEBUG("Reading in extra terminating byte\n"); |
| 1373 | |
Linus Walleij | 049f50c | 2007-03-03 20:30:43 +0000 | [diff] [blame] | 1374 | // need to read in extra byte and discard it |
| 1375 | int result = 0; |
| 1376 | char byte = 0; |
Linus Walleij | 2f62281 | 2008-08-30 22:06:58 +0000 | [diff] [blame] | 1377 | result = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep, &byte, 1, ptp_usb->timeout); |
Linus Walleij | d866d24 | 2009-08-23 21:50:39 +0000 | [diff] [blame] | 1378 | |
Linus Walleij | 049f50c | 2007-03-03 20:30:43 +0000 | [diff] [blame] | 1379 | if (result != 1) |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 1380 | 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] | 1381 | } 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] | 1382 | int zeroresult = 0; |
| 1383 | char zerobyte = 0; |
| 1384 | |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 1385 | |
| 1386 | LIBMTP_INFO("Reading in zero packet after header\n"); |
| 1387 | |
Linus Walleij | 1473595 | 2010-04-25 04:56:49 +0000 | [diff] [blame] | 1388 | zeroresult = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep, &zerobyte, 0, ptp_usb->timeout); |
Linus Walleij | 5f3c44b | 2009-09-12 21:03:06 +0000 | [diff] [blame] | 1389 | |
Linus Walleij | 94cd972 | 2007-09-28 21:29:53 +0000 | [diff] [blame] | 1390 | if (zeroresult != 0) |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 1391 | 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] | 1392 | } |
Linus Walleij | 5f3c44b | 2009-09-12 21:03:06 +0000 | [diff] [blame] | 1393 | |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1394 | /* Is that all of data? */ |
Linus Walleij | 94cd972 | 2007-09-28 21:29:53 +0000 | [diff] [blame] | 1395 | if (len+PTP_USB_BULK_HDR_LEN<=rlen) { |
| 1396 | break; |
| 1397 | } |
Linus Walleij | 5f3c44b | 2009-09-12 21:03:06 +0000 | [diff] [blame] | 1398 | |
Linus Walleij | 913a306 | 2007-09-28 21:42:39 +0000 | [diff] [blame] | 1399 | ret = ptp_read_func(len - (rlen - PTP_USB_BULK_HDR_LEN), |
| 1400 | handler, |
| 1401 | params->data, &rlen, 1); |
Linus Walleij | 5f3c44b | 2009-09-12 21:03:06 +0000 | [diff] [blame] | 1402 | |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1403 | if (ret!=PTP_RC_OK) { |
Linus Walleij | 049f50c | 2007-03-03 20:30:43 +0000 | [diff] [blame] | 1404 | break; |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1405 | } |
| 1406 | } while (0); |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1407 | return ret; |
| 1408 | } |
| 1409 | |
| 1410 | uint16_t |
| 1411 | ptp_usb_getresp (PTPParams* params, PTPContainer* resp) |
| 1412 | { |
| 1413 | uint16_t ret; |
| 1414 | unsigned long rlen; |
| 1415 | PTPUSBBulkContainer usbresp; |
Linus Walleij | 3e4b114 | 2007-11-01 23:54:16 +0000 | [diff] [blame] | 1416 | PTP_USB *ptp_usb = (PTP_USB *)(params->data); |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1417 | |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 1418 | |
| 1419 | LIBMTP_USB_DEBUG("RESPONSE: "); |
| 1420 | |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1421 | memset(&usbresp,0,sizeof(usbresp)); |
| 1422 | /* read response, it should never be longer than sizeof(usbresp) */ |
| 1423 | ret = ptp_usb_getpacket(params, &usbresp, &rlen); |
| 1424 | |
Linus Walleij | b8f78c7 | 2008-03-02 20:22:56 +0000 | [diff] [blame] | 1425 | // Fix for bevahiour reported by Scott Snyder on Samsung YP-U3. The player |
| 1426 | // sends a packet containing just zeroes of length 2 (up to 4 has been seen too) |
| 1427 | // after a NULL packet when it should send the response. This code ignores |
| 1428 | // such illegal packets. |
Linus Walleij | 3191bba | 2008-02-10 21:16:25 +0000 | [diff] [blame] | 1429 | 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] | 1430 | libusb_glue_debug (params, "ptp_usb_getresp: detected short response " |
Linus Walleij | 3191bba | 2008-02-10 21:16:25 +0000 | [diff] [blame] | 1431 | "of %d bytes, expect problems! (re-reading " |
| 1432 | "response), rlen"); |
Linus Walleij | 00411ee | 2008-01-27 12:24:51 +0000 | [diff] [blame] | 1433 | ret = ptp_usb_getpacket(params, &usbresp, &rlen); |
| 1434 | } |
| 1435 | |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1436 | if (ret!=PTP_RC_OK) { |
| 1437 | ret = PTP_ERROR_IO; |
| 1438 | } else |
| 1439 | if (dtoh16(usbresp.type)!=PTP_USB_CONTAINER_RESPONSE) { |
| 1440 | ret = PTP_ERROR_RESP_EXPECTED; |
| 1441 | } else |
| 1442 | if (dtoh16(usbresp.code)!=resp->Code) { |
| 1443 | ret = dtoh16(usbresp.code); |
| 1444 | } |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 1445 | |
| 1446 | LIBMTP_USB_DEBUG("%04x\n", ret); |
| 1447 | |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1448 | if (ret!=PTP_RC_OK) { |
Linus Walleij | d1e14e0 | 2008-12-14 01:07:30 +0000 | [diff] [blame] | 1449 | /* libusb_glue_error (params, |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1450 | "PTP: request code 0x%04x getting resp error 0x%04x", |
| 1451 | resp->Code, ret);*/ |
| 1452 | return ret; |
| 1453 | } |
| 1454 | /* build an appropriate PTPContainer */ |
| 1455 | resp->Code=dtoh16(usbresp.code); |
| 1456 | resp->SessionID=params->session_id; |
| 1457 | resp->Transaction_ID=dtoh32(usbresp.trans_id); |
Linus Walleij | fec4d56 | 2008-06-01 22:30:36 +0000 | [diff] [blame] | 1458 | if (FLAG_IGNORE_HEADER_ERRORS(ptp_usb)) { |
Linus Walleij | 3e4b114 | 2007-11-01 23:54:16 +0000 | [diff] [blame] | 1459 | if (resp->Transaction_ID != params->transaction_id-1) { |
Linus Walleij | d1e14e0 | 2008-12-14 01:07:30 +0000 | [diff] [blame] | 1460 | libusb_glue_debug (params, "ptp_usb_getresp: detected a broken " |
Linus Walleij | 00411ee | 2008-01-27 12:24:51 +0000 | [diff] [blame] | 1461 | "PTP header, transaction ID insane, expect " |
| 1462 | "problems! (But continuing)"); |
Linus Walleij | 3e4b114 | 2007-11-01 23:54:16 +0000 | [diff] [blame] | 1463 | // Repair the header, so it won't wreak more havoc. |
| 1464 | resp->Transaction_ID = params->transaction_id-1; |
| 1465 | } |
| 1466 | } |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1467 | resp->Param1=dtoh32(usbresp.payload.params.param1); |
| 1468 | resp->Param2=dtoh32(usbresp.payload.params.param2); |
| 1469 | resp->Param3=dtoh32(usbresp.payload.params.param3); |
| 1470 | resp->Param4=dtoh32(usbresp.payload.params.param4); |
| 1471 | resp->Param5=dtoh32(usbresp.payload.params.param5); |
| 1472 | return ret; |
| 1473 | } |
| 1474 | |
| 1475 | /* Event handling functions */ |
| 1476 | |
| 1477 | /* PTP Events wait for or check mode */ |
| 1478 | #define PTP_EVENT_CHECK 0x0000 /* waits for */ |
| 1479 | #define PTP_EVENT_CHECK_FAST 0x0001 /* checks */ |
| 1480 | |
| 1481 | static inline uint16_t |
| 1482 | ptp_usb_event (PTPParams* params, PTPContainer* event, int wait) |
| 1483 | { |
| 1484 | uint16_t ret; |
| 1485 | int result; |
| 1486 | unsigned long rlen; |
| 1487 | PTPUSBEventContainer usbevent; |
| 1488 | PTP_USB *ptp_usb = (PTP_USB *)(params->data); |
| 1489 | |
| 1490 | memset(&usbevent,0,sizeof(usbevent)); |
| 1491 | |
Linus Walleij | 5f3c44b | 2009-09-12 21:03:06 +0000 | [diff] [blame] | 1492 | if ((params==NULL) || (event==NULL)) |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1493 | return PTP_ERROR_BADPARAM; |
| 1494 | ret = PTP_RC_OK; |
| 1495 | switch(wait) { |
| 1496 | case PTP_EVENT_CHECK: |
Linus Walleij | 2f62281 | 2008-08-30 22:06:58 +0000 | [diff] [blame] | 1497 | result=USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *)&usbevent,sizeof(usbevent),ptp_usb->timeout); |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1498 | if (result==0) |
Linus Walleij | 2f62281 | 2008-08-30 22:06:58 +0000 | [diff] [blame] | 1499 | result = USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *) &usbevent, sizeof(usbevent), ptp_usb->timeout); |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1500 | if (result < 0) ret = PTP_ERROR_IO; |
| 1501 | break; |
| 1502 | case PTP_EVENT_CHECK_FAST: |
Linus Walleij | 2f62281 | 2008-08-30 22:06:58 +0000 | [diff] [blame] | 1503 | result=USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *)&usbevent,sizeof(usbevent),ptp_usb->timeout); |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1504 | if (result==0) |
Linus Walleij | 2f62281 | 2008-08-30 22:06:58 +0000 | [diff] [blame] | 1505 | result = USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *) &usbevent, sizeof(usbevent), ptp_usb->timeout); |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1506 | if (result < 0) ret = PTP_ERROR_IO; |
| 1507 | break; |
| 1508 | default: |
| 1509 | ret=PTP_ERROR_BADPARAM; |
| 1510 | break; |
| 1511 | } |
| 1512 | if (ret!=PTP_RC_OK) { |
Linus Walleij | d1e14e0 | 2008-12-14 01:07:30 +0000 | [diff] [blame] | 1513 | libusb_glue_error (params, |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1514 | "PTP: reading event an error 0x%04x occurred", ret); |
| 1515 | return PTP_ERROR_IO; |
| 1516 | } |
| 1517 | rlen = result; |
| 1518 | if (rlen < 8) { |
Linus Walleij | d1e14e0 | 2008-12-14 01:07:30 +0000 | [diff] [blame] | 1519 | libusb_glue_error (params, |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1520 | "PTP: reading event an short read of %ld bytes occurred", rlen); |
| 1521 | return PTP_ERROR_IO; |
| 1522 | } |
| 1523 | /* if we read anything over interrupt endpoint it must be an event */ |
| 1524 | /* build an appropriate PTPContainer */ |
| 1525 | event->Code=dtoh16(usbevent.code); |
| 1526 | event->SessionID=params->session_id; |
| 1527 | event->Transaction_ID=dtoh32(usbevent.trans_id); |
| 1528 | event->Param1=dtoh32(usbevent.param1); |
| 1529 | event->Param2=dtoh32(usbevent.param2); |
| 1530 | event->Param3=dtoh32(usbevent.param3); |
| 1531 | return ret; |
| 1532 | } |
| 1533 | |
| 1534 | uint16_t |
| 1535 | ptp_usb_event_check (PTPParams* params, PTPContainer* event) { |
| 1536 | |
| 1537 | return ptp_usb_event (params, event, PTP_EVENT_CHECK_FAST); |
| 1538 | } |
| 1539 | |
| 1540 | uint16_t |
| 1541 | ptp_usb_event_wait (PTPParams* params, PTPContainer* event) { |
| 1542 | |
| 1543 | return ptp_usb_event (params, event, PTP_EVENT_CHECK); |
| 1544 | } |
| 1545 | |
Linus Walleij | bd3bf9e | 2007-09-14 19:31:54 +0000 | [diff] [blame] | 1546 | uint16_t |
| 1547 | ptp_usb_control_cancel_request (PTPParams *params, uint32_t transactionid) { |
| 1548 | PTP_USB *ptp_usb = (PTP_USB *)(params->data); |
| 1549 | int ret; |
| 1550 | unsigned char buffer[6]; |
| 1551 | |
| 1552 | htod16a(&buffer[0],PTP_EC_CancelTransaction); |
| 1553 | htod32a(&buffer[2],transactionid); |
Linus Walleij | 5f3c44b | 2009-09-12 21:03:06 +0000 | [diff] [blame] | 1554 | ret = usb_control_msg(ptp_usb->handle, |
Linus Walleij | bd3bf9e | 2007-09-14 19:31:54 +0000 | [diff] [blame] | 1555 | USB_TYPE_CLASS | USB_RECIP_INTERFACE, |
Linus Walleij | 2f62281 | 2008-08-30 22:06:58 +0000 | [diff] [blame] | 1556 | 0x64, 0x0000, 0x0000, (char *) buffer, sizeof(buffer), ptp_usb->timeout); |
Linus Walleij | bd3bf9e | 2007-09-14 19:31:54 +0000 | [diff] [blame] | 1557 | if (ret < sizeof(buffer)) |
| 1558 | return PTP_ERROR_IO; |
| 1559 | return PTP_RC_OK; |
| 1560 | } |
Linus Walleij | a032327 | 2007-01-07 12:21:30 +0000 | [diff] [blame] | 1561 | |
Linus Walleij | 9eb3d31 | 2006-08-04 19:25:59 +0000 | [diff] [blame] | 1562 | 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] | 1563 | { |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1564 | usb_dev_handle *device_handle; |
Linus Walleij | fea4f53 | 2009-09-22 22:08:35 +0000 | [diff] [blame] | 1565 | char buf[255]; |
Linus Walleij | f2d747a | 2010-01-19 00:46:18 +0000 | [diff] [blame] | 1566 | int usbresult; |
Linus Walleij | 5f3c44b | 2009-09-12 21:03:06 +0000 | [diff] [blame] | 1567 | |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1568 | params->sendreq_func=ptp_usb_sendreq; |
| 1569 | params->senddata_func=ptp_usb_senddata; |
| 1570 | params->getresp_func=ptp_usb_getresp; |
| 1571 | params->getdata_func=ptp_usb_getdata; |
Linus Walleij | ff01cb1 | 2007-09-16 19:49:48 +0000 | [diff] [blame] | 1572 | params->cancelreq_func=ptp_usb_control_cancel_request; |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1573 | params->data=ptp_usb; |
| 1574 | params->transaction_id=0; |
Linus Walleij | 462a5e7 | 2007-02-13 06:54:56 +0000 | [diff] [blame] | 1575 | /* |
| 1576 | * This is hardcoded here since we have no devices whatsoever that are BE. |
| 1577 | * Change this the day we run into our first BE device (if ever). |
| 1578 | */ |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1579 | params->byteorder = PTP_DL_LE; |
Linus Walleij | 5f3c44b | 2009-09-12 21:03:06 +0000 | [diff] [blame] | 1580 | |
Linus Walleij | 2f62281 | 2008-08-30 22:06:58 +0000 | [diff] [blame] | 1581 | ptp_usb->timeout = USB_TIMEOUT_DEFAULT; |
Linus Walleij | 5f3c44b | 2009-09-12 21:03:06 +0000 | [diff] [blame] | 1582 | |
Linus Walleij | 0ed2a9f | 2006-05-04 05:31:34 +0000 | [diff] [blame] | 1583 | if ((device_handle = usb_open(dev))){ |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1584 | if (!device_handle) { |
| 1585 | perror("usb_open()"); |
Linus Walleij | 9eb3d31 | 2006-08-04 19:25:59 +0000 | [diff] [blame] | 1586 | return -1; |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1587 | } |
Linus Walleij | 0ed2a9f | 2006-05-04 05:31:34 +0000 | [diff] [blame] | 1588 | ptp_usb->handle = device_handle; |
Linus Walleij | 501ba4d | 2006-10-16 06:17:04 +0000 | [diff] [blame] | 1589 | #ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP |
| 1590 | /* |
| 1591 | * If this device is known to be wrongfully claimed by other kernel |
| 1592 | * drivers (such as mass storage), then try to unload it to make it |
| 1593 | * accessible from user space. |
| 1594 | */ |
Linus Walleij | fec4d56 | 2008-06-01 22:30:36 +0000 | [diff] [blame] | 1595 | if (FLAG_UNLOAD_DRIVER(ptp_usb)) { |
Linus Walleij | fea4f53 | 2009-09-22 22:08:35 +0000 | [diff] [blame] | 1596 | if (usb_get_driver_np(device_handle, (int) ptp_usb->interface, |
| 1597 | buf, sizeof(buf)) == 0) { |
| 1598 | if (usb_detach_kernel_driver_np(device_handle, |
| 1599 | (int) ptp_usb->interface)) { |
Linus Walleij | cea83ff | 2009-09-25 21:44:38 +0000 | [diff] [blame] | 1600 | perror("usb_detach_kernel_driver_np()"); |
| 1601 | return -1; |
Linus Walleij | fea4f53 | 2009-09-22 22:08:35 +0000 | [diff] [blame] | 1602 | } |
Linus Walleij | 501ba4d | 2006-10-16 06:17:04 +0000 | [diff] [blame] | 1603 | } |
| 1604 | } |
| 1605 | #endif |
Linus Walleij | eac1e00 | 2006-11-30 22:06:02 +0000 | [diff] [blame] | 1606 | #ifdef __WIN32__ |
Linus Walleij | 99ed83c | 2007-01-02 18:46:02 +0000 | [diff] [blame] | 1607 | // Only needed on Windows, and cause problems on other platforms. |
Linus Walleij | 7beba57 | 2006-11-29 08:56:12 +0000 | [diff] [blame] | 1608 | if (usb_set_configuration(device_handle, dev->config->bConfigurationValue)) { |
| 1609 | perror("usb_set_configuration()"); |
| 1610 | return -1; |
| 1611 | } |
Linus Walleij | eac1e00 | 2006-11-30 22:06:02 +0000 | [diff] [blame] | 1612 | #endif |
Linus Walleij | 4955748 | 2010-06-22 09:14:47 +0000 | [diff] [blame] | 1613 | // It seems like on kernel 2.6.31 if we already have it open on another |
Linus Walleij | f2d747a | 2010-01-19 00:46:18 +0000 | [diff] [blame] | 1614 | // pthread in our app, we'll get an error if we try to claim it again, |
| 1615 | // but that error is harmless because our process already claimed the interface |
| 1616 | usbresult = usb_claim_interface(device_handle, (int) ptp_usb->interface); |
| 1617 | |
| 1618 | if (usbresult != 0) |
| 1619 | fprintf(stderr, "ignoring usb_claim_interface = %d", usbresult); |
| 1620 | |
Linus Walleij | 5f3c44b | 2009-09-12 21:03:06 +0000 | [diff] [blame] | 1621 | // FIXME : Discovered in the Barry project |
| 1622 | // kernels >= 2.6.28 don't set the interface the same way as |
| 1623 | // previous versions did, and the Blackberry gets confused |
| 1624 | // if it isn't explicitly set |
Linus Walleij | f2d747a | 2010-01-19 00:46:18 +0000 | [diff] [blame] | 1625 | // See above, same issue with pthreads means that if this fails it is not |
| 1626 | // fatal |
Richard Low | ea73f5c | 2010-05-16 11:57:56 +0000 | [diff] [blame] | 1627 | // However, this causes problems on Macs so disable here |
| 1628 | #ifndef __APPLE__ |
Linus Walleij | f2d747a | 2010-01-19 00:46:18 +0000 | [diff] [blame] | 1629 | usbresult = usb_set_altinterface(device_handle, 0); |
| 1630 | if (usbresult) |
| 1631 | fprintf(stderr, "ignoring usb_claim_interface = %d", usbresult); |
Richard Low | ea73f5c | 2010-05-16 11:57:56 +0000 | [diff] [blame] | 1632 | #endif |
Linus Walleij | f2d747a | 2010-01-19 00:46:18 +0000 | [diff] [blame] | 1633 | |
Linus Walleij | fea4f53 | 2009-09-22 22:08:35 +0000 | [diff] [blame] | 1634 | if (FLAG_SWITCH_MODE_BLACKBERRY(ptp_usb)) { |
Linus Walleij | b6e06e2 | 2009-09-22 22:20:49 +0000 | [diff] [blame] | 1635 | int ret; |
| 1636 | |
Linus Walleij | fea4f53 | 2009-09-22 22:08:35 +0000 | [diff] [blame] | 1637 | // FIXME : Only for BlackBerry Storm |
| 1638 | // What does it mean? Maybe switch mode... |
Linus Walleij | cea83ff | 2009-09-25 21:44:38 +0000 | [diff] [blame] | 1639 | // This first control message is absolutely necessary |
Linus Walleij | fea4f53 | 2009-09-22 22:08:35 +0000 | [diff] [blame] | 1640 | usleep(1000); |
Linus Walleij | b6e06e2 | 2009-09-22 22:20:49 +0000 | [diff] [blame] | 1641 | ret = usb_control_msg(device_handle, |
Linus Walleij | fea4f53 | 2009-09-22 22:08:35 +0000 | [diff] [blame] | 1642 | USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, |
| 1643 | 0xaa, 0x00, 0x04, buf, 0x40, 1000); |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 1644 | LIBMTP_USB_DEBUG("BlackBerry magic part 1:\n"); |
| 1645 | LIBMTP_USB_DATA(buf, ret, 16); |
| 1646 | |
Linus Walleij | fea4f53 | 2009-09-22 22:08:35 +0000 | [diff] [blame] | 1647 | usleep(1000); |
Linus Walleij | cea83ff | 2009-09-25 21:44:38 +0000 | [diff] [blame] | 1648 | // This control message is unnecessary |
Linus Walleij | b6e06e2 | 2009-09-22 22:20:49 +0000 | [diff] [blame] | 1649 | ret = usb_control_msg(device_handle, |
Linus Walleij | fea4f53 | 2009-09-22 22:08:35 +0000 | [diff] [blame] | 1650 | USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, |
| 1651 | 0xa5, 0x00, 0x01, buf, 0x02, 1000); |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 1652 | LIBMTP_USB_DEBUG("BlackBerry magic part 2:\n"); |
| 1653 | LIBMTP_USB_DATA(buf, ret, 16); |
| 1654 | |
Linus Walleij | fea4f53 | 2009-09-22 22:08:35 +0000 | [diff] [blame] | 1655 | usleep(1000); |
Linus Walleij | cea83ff | 2009-09-25 21:44:38 +0000 | [diff] [blame] | 1656 | // This control message is unnecessary |
Linus Walleij | b6e06e2 | 2009-09-22 22:20:49 +0000 | [diff] [blame] | 1657 | ret = usb_control_msg(device_handle, |
Linus Walleij | fea4f53 | 2009-09-22 22:08:35 +0000 | [diff] [blame] | 1658 | USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, |
| 1659 | 0xa8, 0x00, 0x01, buf, 0x05, 1000); |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 1660 | LIBMTP_USB_DEBUG("BlackBerry magic part 3:\n"); |
| 1661 | LIBMTP_USB_DATA(buf, ret, 16); |
| 1662 | |
Linus Walleij | fea4f53 | 2009-09-22 22:08:35 +0000 | [diff] [blame] | 1663 | usleep(1000); |
Linus Walleij | cea83ff | 2009-09-25 21:44:38 +0000 | [diff] [blame] | 1664 | // This control message is unnecessary |
Linus Walleij | b6e06e2 | 2009-09-22 22:20:49 +0000 | [diff] [blame] | 1665 | ret = usb_control_msg(device_handle, |
Linus Walleij | fea4f53 | 2009-09-22 22:08:35 +0000 | [diff] [blame] | 1666 | USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, |
| 1667 | 0xa8, 0x00, 0x01, buf, 0x11, 1000); |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 1668 | LIBMTP_USB_DEBUG("BlackBerry magic part 4:\n"); |
| 1669 | LIBMTP_USB_DATA(buf, ret, 16); |
| 1670 | |
Linus Walleij | fea4f53 | 2009-09-22 22:08:35 +0000 | [diff] [blame] | 1671 | usleep(1000); |
| 1672 | } |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1673 | } |
Linus Walleij | 9eb3d31 | 2006-08-04 19:25:59 +0000 | [diff] [blame] | 1674 | return 0; |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1675 | } |
| 1676 | |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1677 | static void clear_stall(PTP_USB* ptp_usb) |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1678 | { |
Linus Walleij | 58b6279 | 2007-01-07 12:38:59 +0000 | [diff] [blame] | 1679 | uint16_t status; |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1680 | int ret; |
Linus Walleij | 5f3c44b | 2009-09-12 21:03:06 +0000 | [diff] [blame] | 1681 | |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1682 | /* check the inep status */ |
Linus Walleij | 58b6279 | 2007-01-07 12:38:59 +0000 | [diff] [blame] | 1683 | status = 0; |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 1684 | ret = usb_get_endpoint_status(ptp_usb,ptp_usb->inep,&status); |
Linus Walleij | 58b6279 | 2007-01-07 12:38:59 +0000 | [diff] [blame] | 1685 | if (ret<0) { |
| 1686 | perror ("inep: usb_get_endpoint_status()"); |
| 1687 | } else if (status) { |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 1688 | LIBMTP_INFO("Clearing stall on IN endpoint\n"); |
Linus Walleij | 58b6279 | 2007-01-07 12:38:59 +0000 | [diff] [blame] | 1689 | ret = usb_clear_stall_feature(ptp_usb,ptp_usb->inep); |
| 1690 | if (ret<0) { |
| 1691 | perror ("usb_clear_stall_feature()"); |
| 1692 | } |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1693 | } |
Linus Walleij | 5f3c44b | 2009-09-12 21:03:06 +0000 | [diff] [blame] | 1694 | |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1695 | /* check the outep status */ |
Linus Walleij | 58b6279 | 2007-01-07 12:38:59 +0000 | [diff] [blame] | 1696 | status=0; |
| 1697 | ret = usb_get_endpoint_status(ptp_usb,ptp_usb->outep,&status); |
| 1698 | if (ret<0) { |
| 1699 | perror("outep: usb_get_endpoint_status()"); |
| 1700 | } else if (status) { |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 1701 | LIBMTP_INFO("Clearing stall on OUT endpoint\n"); |
Linus Walleij | 58b6279 | 2007-01-07 12:38:59 +0000 | [diff] [blame] | 1702 | ret = usb_clear_stall_feature(ptp_usb,ptp_usb->outep); |
| 1703 | if (ret<0) { |
| 1704 | perror("usb_clear_stall_feature()"); |
| 1705 | } |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1706 | } |
Linus Walleij | 58b6279 | 2007-01-07 12:38:59 +0000 | [diff] [blame] | 1707 | |
| 1708 | /* TODO: do we need this for INTERRUPT (ptp_usb->intep) too? */ |
| 1709 | } |
| 1710 | |
| 1711 | static void clear_halt(PTP_USB* ptp_usb) |
| 1712 | { |
| 1713 | int ret; |
| 1714 | |
| 1715 | ret = usb_clear_halt(ptp_usb->handle,ptp_usb->inep); |
| 1716 | if (ret<0) { |
| 1717 | perror("usb_clear_halt() on IN endpoint"); |
| 1718 | } |
| 1719 | ret = usb_clear_halt(ptp_usb->handle,ptp_usb->outep); |
| 1720 | if (ret<0) { |
| 1721 | perror("usb_clear_halt() on OUT endpoint"); |
| 1722 | } |
| 1723 | ret = usb_clear_halt(ptp_usb->handle,ptp_usb->intep); |
| 1724 | if (ret<0) { |
| 1725 | perror("usb_clear_halt() on INTERRUPT endpoint"); |
| 1726 | } |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1727 | } |
| 1728 | |
Linus Walleij | b0ab548 | 2007-08-29 21:08:54 +0000 | [diff] [blame] | 1729 | static void close_usb(PTP_USB* ptp_usb) |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1730 | { |
Linus Walleij | da17cda | 2007-09-18 21:12:05 +0000 | [diff] [blame] | 1731 | // Commented out since it was confusing some |
| 1732 | // devices to do these things. |
Linus Walleij | fec4d56 | 2008-06-01 22:30:36 +0000 | [diff] [blame] | 1733 | if (!FLAG_NO_RELEASE_INTERFACE(ptp_usb)) { |
Linus Walleij | eabc231 | 2008-02-13 00:02:00 +0000 | [diff] [blame] | 1734 | /* |
| 1735 | * Clear any stalled endpoints |
| 1736 | * On misbehaving devices designed for Windows/Mac, quote from: |
| 1737 | * http://www2.one-eyed-alien.net/~mdharm/linux-usb/target_offenses.txt |
| 1738 | * 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] | 1739 | * (...) Windows, when clearing a stall, only sends the CLEAR_HALT command, |
| 1740 | * and presumes that the stall has cleared. Some devices actually choke |
| 1741 | * 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] | 1742 | * STALL is persistant or not). |
| 1743 | */ |
Linus Walleij | 29f03ba | 2007-09-25 19:22:12 +0000 | [diff] [blame] | 1744 | clear_stall(ptp_usb); |
| 1745 | // Clear halts on any endpoints |
| 1746 | clear_halt(ptp_usb); |
| 1747 | // Added to clear some stuff on the OUT endpoint |
| 1748 | // TODO: is this good on the Mac too? |
| 1749 | // HINT: some devices may need that you comment these two out too. |
| 1750 | usb_resetep(ptp_usb->handle, ptp_usb->outep); |
| 1751 | usb_release_interface(ptp_usb->handle, (int) ptp_usb->interface); |
| 1752 | } |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1753 | usb_close(ptp_usb->handle); |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1754 | } |
| 1755 | |
Linus Walleij | 550d6d5 | 2007-01-24 14:32:51 +0000 | [diff] [blame] | 1756 | /** |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 1757 | * Self-explanatory? |
| 1758 | */ |
Linus Walleij | 5f3c44b | 2009-09-12 21:03:06 +0000 | [diff] [blame] | 1759 | static int find_interface_and_endpoints(struct usb_device *dev, |
| 1760 | uint8_t *interface, |
| 1761 | int* inep, |
| 1762 | int* inep_maxpacket, |
| 1763 | int* outep, |
| 1764 | int *outep_maxpacket, |
| 1765 | int* intep) |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1766 | { |
Linus Walleij | d7373cd | 2007-08-28 21:42:05 +0000 | [diff] [blame] | 1767 | int i; |
| 1768 | |
| 1769 | // Loop over the device configurations |
| 1770 | for (i = 0; i < dev->descriptor.bNumConfigurations; i++) { |
Linus Walleij | b0ab548 | 2007-08-29 21:08:54 +0000 | [diff] [blame] | 1771 | uint8_t j; |
Linus Walleij | d7373cd | 2007-08-28 21:42:05 +0000 | [diff] [blame] | 1772 | |
Linus Walleij | 5f3c44b | 2009-09-12 21:03:06 +0000 | [diff] [blame] | 1773 | // Loop over each configurations interfaces |
Linus Walleij | d7373cd | 2007-08-28 21:42:05 +0000 | [diff] [blame] | 1774 | for (j = 0; j < dev->config[i].bNumInterfaces; j++) { |
Linus Walleij | b0ab548 | 2007-08-29 21:08:54 +0000 | [diff] [blame] | 1775 | uint8_t k; |
| 1776 | uint8_t no_ep; |
Linus Walleij | 5f3c44b | 2009-09-12 21:03:06 +0000 | [diff] [blame] | 1777 | int found_inep = 0; |
| 1778 | int found_outep = 0; |
| 1779 | int found_intep = 0; |
Linus Walleij | d7373cd | 2007-08-28 21:42:05 +0000 | [diff] [blame] | 1780 | struct usb_endpoint_descriptor *ep; |
Linus Walleij | 5f3c44b | 2009-09-12 21:03:06 +0000 | [diff] [blame] | 1781 | |
| 1782 | // MTP devices shall have 3 endpoints, ignore those interfaces |
| 1783 | // that haven't. |
| 1784 | no_ep = dev->config[i].interface[j].altsetting->bNumEndpoints; |
| 1785 | if (no_ep != 3) |
| 1786 | continue; |
Linus Walleij | b0ab548 | 2007-08-29 21:08:54 +0000 | [diff] [blame] | 1787 | |
| 1788 | *interface = dev->config[i].interface[j].altsetting->bInterfaceNumber; |
| 1789 | ep = dev->config[i].interface[j].altsetting->endpoint; |
Linus Walleij | 5f3c44b | 2009-09-12 21:03:06 +0000 | [diff] [blame] | 1790 | |
| 1791 | // Loop over the three endpoints to locate two bulk and |
| 1792 | // one interrupt endpoint and FAIL if we cannot, and continue. |
Linus Walleij | b0ab548 | 2007-08-29 21:08:54 +0000 | [diff] [blame] | 1793 | for (k = 0; k < no_ep; k++) { |
Linus Walleij | e73854e | 2010-06-22 09:07:34 +0000 | [diff] [blame] | 1794 | if (ep[k].bmAttributes == USB_ENDPOINT_TYPE_BULK) { |
| 1795 | if ((ep[k].bEndpointAddress & USB_ENDPOINT_DIR_MASK) == |
| 1796 | USB_ENDPOINT_DIR_MASK) { |
| 1797 | *inep = ep[k].bEndpointAddress; |
| 1798 | *inep_maxpacket = ep[k].wMaxPacketSize; |
| 1799 | found_inep = 1; |
| 1800 | } |
| 1801 | if ((ep[k].bEndpointAddress & USB_ENDPOINT_DIR_MASK) == 0) { |
Linus Walleij | 48fda57 | 2010-06-22 09:09:03 +0000 | [diff] [blame] | 1802 | *outep = ep[k].bEndpointAddress; |
| 1803 | *outep_maxpacket = ep[k].wMaxPacketSize; |
Linus Walleij | e73854e | 2010-06-22 09:07:34 +0000 | [diff] [blame] | 1804 | found_outep = 1; |
| 1805 | } |
| 1806 | } else if (ep[k].bmAttributes == USB_ENDPOINT_TYPE_INTERRUPT) { |
| 1807 | if ((ep[k].bEndpointAddress & USB_ENDPOINT_DIR_MASK) == |
| 1808 | USB_ENDPOINT_DIR_MASK) { |
Linus Walleij | 48fda57 | 2010-06-22 09:09:03 +0000 | [diff] [blame] | 1809 | *intep = ep[k].bEndpointAddress; |
Linus Walleij | e73854e | 2010-06-22 09:07:34 +0000 | [diff] [blame] | 1810 | found_intep = 1; |
| 1811 | } |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1812 | } |
Linus Walleij | d7373cd | 2007-08-28 21:42:05 +0000 | [diff] [blame] | 1813 | } |
Linus Walleij | 5f3c44b | 2009-09-12 21:03:06 +0000 | [diff] [blame] | 1814 | if (found_inep && found_outep && found_intep) |
| 1815 | // We assigned the endpoints so return here. |
| 1816 | return 0; |
| 1817 | // Else loop to next interface/config |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1818 | } |
| 1819 | } |
Linus Walleij | 5f3c44b | 2009-09-12 21:03:06 +0000 | [diff] [blame] | 1820 | return -1; |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1821 | } |
| 1822 | |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 1823 | /** |
| 1824 | * This function assigns params and usbinfo given a raw device |
| 1825 | * as input. |
| 1826 | * @param device the device to be assigned. |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 1827 | * @param usbinfo a pointer to the new usbinfo. |
| 1828 | * @return an error code. |
| 1829 | */ |
Linus Walleij | 4955748 | 2010-06-22 09:14:47 +0000 | [diff] [blame] | 1830 | LIBMTP_error_number_t configure_usb_device(LIBMTP_raw_device_t *device, |
Linus Walleij | 3c64325 | 2008-05-31 23:22:28 +0000 | [diff] [blame] | 1831 | PTPParams *params, |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 1832 | void **usbinfo) |
| 1833 | { |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 1834 | PTP_USB *ptp_usb; |
| 1835 | struct usb_device *libusb_device; |
| 1836 | uint16_t ret = 0; |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 1837 | struct usb_bus *bus; |
| 1838 | int found = 0; |
Linus Walleij | 5f3c44b | 2009-09-12 21:03:06 +0000 | [diff] [blame] | 1839 | int err; |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 1840 | |
| 1841 | /* See if we can find this raw device again... */ |
| 1842 | bus = init_usb(); |
| 1843 | for (; bus != NULL; bus = bus->next) { |
| 1844 | if (bus->location == device->bus_location) { |
| 1845 | struct usb_device *dev = bus->devices; |
| 1846 | |
| 1847 | for (; dev != NULL; dev = dev->next) { |
| 1848 | if(dev->devnum == device->devnum && |
| 1849 | dev->descriptor.idVendor == device->device_entry.vendor_id && |
| 1850 | dev->descriptor.idProduct == device->device_entry.product_id ) { |
| 1851 | libusb_device = dev; |
| 1852 | found = 1; |
| 1853 | break; |
| 1854 | } |
| 1855 | } |
| 1856 | if (found) |
| 1857 | break; |
| 1858 | } |
| 1859 | } |
| 1860 | /* Device has gone since detecting raw devices! */ |
| 1861 | if (!found) { |
| 1862 | return LIBMTP_ERROR_NO_DEVICE_ATTACHED; |
| 1863 | } |
| 1864 | |
| 1865 | /* Allocate structs */ |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 1866 | ptp_usb = (PTP_USB *) malloc(sizeof(PTP_USB)); |
Linus Walleij | 3c64325 | 2008-05-31 23:22:28 +0000 | [diff] [blame] | 1867 | if (ptp_usb == NULL) { |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 1868 | return LIBMTP_ERROR_MEMORY_ALLOCATION; |
| 1869 | } |
| 1870 | /* Start with a blank slate (includes setting device_flags to 0) */ |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 1871 | memset(ptp_usb, 0, sizeof(PTP_USB)); |
| 1872 | |
Linus Walleij | fec4d56 | 2008-06-01 22:30:36 +0000 | [diff] [blame] | 1873 | /* Copy the raw device */ |
| 1874 | memcpy(&ptp_usb->rawdevice, device, sizeof(LIBMTP_raw_device_t)); |
Linus Walleij | 335a81c | 2008-06-02 23:01:00 +0000 | [diff] [blame] | 1875 | |
| 1876 | /* |
| 1877 | * Some devices must have their "OS Descriptor" massaged in order |
| 1878 | * to work. |
| 1879 | */ |
| 1880 | if (FLAG_ALWAYS_PROBE_DESCRIPTOR(ptp_usb)) { |
| 1881 | // Massage the device descriptor |
| 1882 | (void) probe_device_descriptor(libusb_device, NULL); |
| 1883 | } |
Linus Walleij | 5f3c44b | 2009-09-12 21:03:06 +0000 | [diff] [blame] | 1884 | |
| 1885 | /* Assign interface and endpoints to usbinfo... */ |
| 1886 | err = find_interface_and_endpoints(libusb_device, |
| 1887 | &ptp_usb->interface, |
| 1888 | &ptp_usb->inep, |
| 1889 | &ptp_usb->inep_maxpacket, |
| 1890 | &ptp_usb->outep, |
| 1891 | &ptp_usb->outep_maxpacket, |
| 1892 | &ptp_usb->intep); |
| 1893 | |
| 1894 | if (err) { |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 1895 | LIBMTP_ERROR("LIBMTP PANIC: Unable to find interface & endpoints of device\n"); |
Linus Walleij | 5f3c44b | 2009-09-12 21:03:06 +0000 | [diff] [blame] | 1896 | return LIBMTP_ERROR_CONNECTING; |
| 1897 | } |
| 1898 | |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 1899 | /* Attempt to initialize this device */ |
Linus Walleij | 3c64325 | 2008-05-31 23:22:28 +0000 | [diff] [blame] | 1900 | if (init_ptp_usb(params, ptp_usb, libusb_device) < 0) { |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 1901 | LIBMTP_ERROR("LIBMTP PANIC: Unable to initialize device\n"); |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 1902 | return LIBMTP_ERROR_CONNECTING; |
| 1903 | } |
Linus Walleij | 5f3c44b | 2009-09-12 21:03:06 +0000 | [diff] [blame] | 1904 | |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 1905 | /* |
| 1906 | * This works in situations where previous bad applications |
Linus Walleij | 5f3c44b | 2009-09-12 21:03:06 +0000 | [diff] [blame] | 1907 | * have not used LIBMTP_Release_Device on exit |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 1908 | */ |
Linus Walleij | 3c64325 | 2008-05-31 23:22:28 +0000 | [diff] [blame] | 1909 | if ((ret = ptp_opensession(params, 1)) == PTP_ERROR_IO) { |
Linus Walleij | f9137dc | 2010-10-31 17:13:06 +0000 | [diff] [blame] | 1910 | 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] | 1911 | close_usb(ptp_usb); |
Linus Walleij | 5f3c44b | 2009-09-12 21:03:06 +0000 | [diff] [blame] | 1912 | |
Linus Walleij | f9137dc | 2010-10-31 17:13:06 +0000 | [diff] [blame] | 1913 | LIBMTP_ERROR("LIBMTP libusb: Attempt to reset device\n"); |
| 1914 | usb_reset(ptp_usb->handle); |
| 1915 | |
Linus Walleij | 3c64325 | 2008-05-31 23:22:28 +0000 | [diff] [blame] | 1916 | if(init_ptp_usb(params, ptp_usb, libusb_device) <0) { |
Linus Walleij | f9137dc | 2010-10-31 17:13:06 +0000 | [diff] [blame] | 1917 | LIBMTP_ERROR("LIBMTP PANIC: Could not init USB on second attempt\n"); |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 1918 | return LIBMTP_ERROR_CONNECTING; |
| 1919 | } |
Linus Walleij | 5f3c44b | 2009-09-12 21:03:06 +0000 | [diff] [blame] | 1920 | |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 1921 | /* Device has been reset, try again */ |
Linus Walleij | f9137dc | 2010-10-31 17:13:06 +0000 | [diff] [blame] | 1922 | if ((ret = ptp_opensession(params, 1)) == PTP_ERROR_IO) { |
| 1923 | LIBMTP_ERROR("LIBMTP PANIC: failed to open session on second attempt\n"); |
| 1924 | return LIBMTP_ERROR_CONNECTING; |
| 1925 | } |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 1926 | } |
Linus Walleij | 5f3c44b | 2009-09-12 21:03:06 +0000 | [diff] [blame] | 1927 | |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 1928 | /* Was the transaction id invalid? Try again */ |
| 1929 | if (ret == PTP_RC_InvalidTransactionID) { |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 1930 | 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] | 1931 | params->transaction_id += 10; |
| 1932 | ret = ptp_opensession(params, 1); |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 1933 | } |
| 1934 | |
| 1935 | if (ret != PTP_RC_SessionAlreadyOpened && ret != PTP_RC_OK) { |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 1936 | LIBMTP_ERROR("LIBMTP PANIC: Could not open session! " |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 1937 | "(Return code %d)\n Try to reset the device.\n", |
| 1938 | ret); |
| 1939 | usb_release_interface(ptp_usb->handle, |
| 1940 | (int) ptp_usb->interface); |
| 1941 | return LIBMTP_ERROR_CONNECTING; |
| 1942 | } |
| 1943 | |
| 1944 | /* OK configured properly */ |
Linus Walleij | a700d22 | 2008-05-28 23:06:14 +0000 | [diff] [blame] | 1945 | *usbinfo = (void *) ptp_usb; |
| 1946 | return LIBMTP_ERROR_NONE; |
| 1947 | } |
| 1948 | |
| 1949 | |
Linus Walleij | b0ab548 | 2007-08-29 21:08:54 +0000 | [diff] [blame] | 1950 | void close_device (PTP_USB *ptp_usb, PTPParams *params) |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1951 | { |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1952 | if (ptp_closesession(params)!=PTP_RC_OK) |
nicklas79 | daadbf2 | 2009-09-28 18:19:34 +0000 | [diff] [blame] | 1953 | LIBMTP_ERROR("ERROR: Could not close session!\n"); |
Linus Walleij | b0ab548 | 2007-08-29 21:08:54 +0000 | [diff] [blame] | 1954 | close_usb(ptp_usb); |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1955 | } |
| 1956 | |
Linus Walleij | 2f62281 | 2008-08-30 22:06:58 +0000 | [diff] [blame] | 1957 | void set_usb_device_timeout(PTP_USB *ptp_usb, int timeout) |
| 1958 | { |
| 1959 | ptp_usb->timeout = timeout; |
| 1960 | } |
| 1961 | |
| 1962 | void get_usb_device_timeout(PTP_USB *ptp_usb, int *timeout) |
| 1963 | { |
| 1964 | *timeout = ptp_usb->timeout; |
| 1965 | } |
| 1966 | |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1967 | static int usb_clear_stall_feature(PTP_USB* ptp_usb, int ep) |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1968 | { |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1969 | return (usb_control_msg(ptp_usb->handle, |
| 1970 | USB_RECIP_ENDPOINT, USB_REQ_CLEAR_FEATURE, USB_FEATURE_HALT, |
Linus Walleij | 2f62281 | 2008-08-30 22:06:58 +0000 | [diff] [blame] | 1971 | ep, NULL, 0, ptp_usb->timeout)); |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1972 | } |
| 1973 | |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1974 | 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] | 1975 | { |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 1976 | return (usb_control_msg(ptp_usb->handle, |
| 1977 | USB_DP_DTH|USB_RECIP_ENDPOINT, USB_REQ_GET_STATUS, |
Linus Walleij | 2f62281 | 2008-08-30 22:06:58 +0000 | [diff] [blame] | 1978 | USB_FEATURE_HALT, ep, (char *)status, 2, ptp_usb->timeout)); |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1979 | } |