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