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