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