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