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