Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 1 | /* |
Linus Walleij | 15e344f | 2006-03-06 15:15:00 +0000 | [diff] [blame] | 2 | * libusb-glue.c |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 3 | * |
Linus Walleij | 15e344f | 2006-03-06 15:15:00 +0000 | [diff] [blame] | 4 | * Created by Richard Low on 24/12/2005. (as mtp-utils.c) |
Linus Walleij | a548364 | 2006-03-09 09:20:38 +0000 | [diff] [blame] | 5 | * Modified by Linus Walleij 2006-03-06 |
| 6 | * (Notice that Anglo-Saxons use little-endian dates and Swedes use big-endian dates.) |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 7 | * |
| 8 | * This file adds some utils (many copied from ptpcam.c from libptp2) to |
| 9 | * use MTP devices. Include mtp-utils.h to use any of the ptp/mtp functions. |
| 10 | * |
| 11 | */ |
| 12 | #include "ptp.h" |
| 13 | #include <errno.h> |
| 14 | #include <stdio.h> |
| 15 | #include <stdlib.h> |
| 16 | #include <string.h> |
| 17 | #include <getopt.h> |
| 18 | #include <unistd.h> |
| 19 | #include <sys/types.h> |
| 20 | #include <utime.h> |
| 21 | #include <sys/stat.h> |
| 22 | #include <fcntl.h> |
| 23 | #include <sys/mman.h> |
| 24 | #include <usb.h> |
| 25 | |
Linus Walleij | 15e344f | 2006-03-06 15:15:00 +0000 | [diff] [blame] | 26 | #include "libmtp.h" |
| 27 | #include "libusb-glue.h" |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 28 | #include "util.h" |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 29 | |
Linus Walleij | e78de6d | 2006-10-31 14:46:36 +0000 | [diff] [blame] | 30 | /* To enable debug prints, switch on this */ |
| 31 | //#define ENABLE_USB_BULK_DEBUG |
| 32 | |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 33 | /* OUR APPLICATION USB URB (2MB) ;) */ |
| 34 | #define PTPCAM_USB_URB 2097152 |
| 35 | |
| 36 | /* this must not be too short - the original 4000 was not long |
| 37 | enough for big file transfers. I imagine the player spends a |
| 38 | bit of time gearing up to receiving lots of data. This also makes |
| 39 | connecting/disconnecting more reliable */ |
| 40 | #define USB_TIMEOUT 10000 |
| 41 | #define USB_CAPTURE_TIMEOUT 20000 |
| 42 | |
| 43 | /* USB control message data phase direction */ |
| 44 | #ifndef USB_DP_HTD |
| 45 | #define USB_DP_HTD (0x00 << 7) /* host to device */ |
| 46 | #endif |
| 47 | #ifndef USB_DP_DTH |
| 48 | #define USB_DP_DTH (0x01 << 7) /* device to host */ |
| 49 | #endif |
| 50 | |
| 51 | /* USB Feature selector HALT */ |
| 52 | #ifndef USB_FEATURE_HALT |
| 53 | #define USB_FEATURE_HALT 0x00 |
| 54 | #endif |
| 55 | |
Linus Walleij | a548364 | 2006-03-09 09:20:38 +0000 | [diff] [blame] | 56 | /* |
Linus Walleij | e8c5464 | 2006-03-28 09:45:00 +0000 | [diff] [blame] | 57 | * MTP device list, trying real bad to get all devices into |
| 58 | * this list by stealing from everyone I know. |
Linus Walleij | a548364 | 2006-03-09 09:20:38 +0000 | [diff] [blame] | 59 | */ |
Linus Walleij | 6fd2f08 | 2006-03-28 07:19:22 +0000 | [diff] [blame] | 60 | static const LIBMTP_device_entry_t mtp_device_table[] = { |
Linus Walleij | 3d2c784 | 2006-08-18 09:35:08 +0000 | [diff] [blame] | 61 | |
| 62 | /* |
| 63 | * Creative Technology |
| 64 | */ |
Linus Walleij | 0558ac5 | 2006-09-07 06:55:03 +0000 | [diff] [blame] | 65 | { "Creative Zen Vision", 0x041e, 0x411f, DEVICE_FLAG_NONE }, |
| 66 | { "Creative Portable Media Center", 0x041e, 0x4123, DEVICE_FLAG_NONE }, |
| 67 | { "Creative Zen Xtra (MTP mode)", 0x041e, 0x4128, DEVICE_FLAG_NONE }, |
| 68 | { "Second generation Dell DJ", 0x041e, 0x412f, DEVICE_FLAG_NONE }, |
| 69 | { "Creative Zen Micro (MTP mode)", 0x041e, 0x4130, DEVICE_FLAG_NONE }, |
| 70 | { "Creative Zen Touch (MTP mode)", 0x041e, 0x4131, DEVICE_FLAG_NONE }, |
| 71 | { "Dell Pocket DJ (MTP mode)", 0x041e, 0x4132, DEVICE_FLAG_NONE }, |
| 72 | { "Creative Zen Sleek (MTP mode)", 0x041e, 0x4137, DEVICE_FLAG_NONE }, |
| 73 | { "Creative Zen MicroPhoto", 0x041e, 0x413c, DEVICE_FLAG_NONE }, |
| 74 | { "Creative Zen Sleek Photo", 0x041e, 0x413d, DEVICE_FLAG_NONE }, |
| 75 | { "Creative Zen Vision:M", 0x041e, 0x413e, DEVICE_FLAG_NONE }, |
Linus Walleij | bdd006b | 2006-09-15 04:54:13 +0000 | [diff] [blame] | 76 | // Reported by marazm@o2.pl |
| 77 | { "Creative Zen V", 0x041e, 0x4150, DEVICE_FLAG_NONE }, |
Linus Walleij | 550bc67 | 2006-10-10 11:11:29 +0000 | [diff] [blame] | 78 | // Reported by Darel on the XNJB forums |
| 79 | { "Creative Zen V plus", 0x041e, 0x4152, DEVICE_FLAG_NONE }, |
Linus Walleij | 3d2c784 | 2006-08-18 09:35:08 +0000 | [diff] [blame] | 80 | |
| 81 | /* |
| 82 | * Samsung |
Linus Walleij | 0558ac5 | 2006-09-07 06:55:03 +0000 | [diff] [blame] | 83 | * We suspect that more of these are dual mode. |
Linus Walleij | 3d2c784 | 2006-08-18 09:35:08 +0000 | [diff] [blame] | 84 | */ |
| 85 | // From libgphoto2 |
Linus Walleij | 0558ac5 | 2006-09-07 06:55:03 +0000 | [diff] [blame] | 86 | { "Samsung YH-820", 0x04e8, 0x502e, DEVICE_FLAG_NONE }, |
Linus Walleij | 3d2c784 | 2006-08-18 09:35:08 +0000 | [diff] [blame] | 87 | // Contributed by polux2001@users.sourceforge.net |
Linus Walleij | 0558ac5 | 2006-09-07 06:55:03 +0000 | [diff] [blame] | 88 | { "Samsung YH-925", 0x04e8, 0x502f, DEVICE_FLAG_NONE }, |
Linus Walleij | 3d2c784 | 2006-08-18 09:35:08 +0000 | [diff] [blame] | 89 | // Contributed by anonymous person on SourceForge |
Linus Walleij | 0558ac5 | 2006-09-07 06:55:03 +0000 | [diff] [blame] | 90 | { "Samsung YP-T7J", 0x04e8, 0x5047, DEVICE_FLAG_NONE }, |
Linus Walleij | 3d2c784 | 2006-08-18 09:35:08 +0000 | [diff] [blame] | 91 | // Reported by cstrickler@gmail.com |
Linus Walleij | 0558ac5 | 2006-09-07 06:55:03 +0000 | [diff] [blame] | 92 | { "Samsung YP-U2J (YP-U2JXB/XAA)", 0x04e8, 0x5054, DEVICE_FLAG_NONE }, |
Linus Walleij | 82ed661 | 2006-08-31 08:06:19 +0000 | [diff] [blame] | 93 | // Reported by Andrew Benson |
Linus Walleij | 0558ac5 | 2006-09-07 06:55:03 +0000 | [diff] [blame] | 94 | { "Samsung YP-F2J", 0x04e8, 0x5057, DEVICE_FLAG_DUALMODE }, |
Linus Walleij | 3d2c784 | 2006-08-18 09:35:08 +0000 | [diff] [blame] | 95 | // From a rouge .INF file |
Linus Walleij | 0558ac5 | 2006-09-07 06:55:03 +0000 | [diff] [blame] | 96 | { "Samsung YH-999 Portable Media Center", 0x04e8, 0x5a0f, DEVICE_FLAG_NONE }, |
Linus Walleij | 3d2c784 | 2006-08-18 09:35:08 +0000 | [diff] [blame] | 97 | |
| 98 | /* |
| 99 | * Intel |
| 100 | */ |
Linus Walleij | 0558ac5 | 2006-09-07 06:55:03 +0000 | [diff] [blame] | 101 | { "Intel Bandon Portable Media Center", 0x045e, 0x00c9, DEVICE_FLAG_NONE }, |
Linus Walleij | 3d2c784 | 2006-08-18 09:35:08 +0000 | [diff] [blame] | 102 | |
| 103 | /* |
| 104 | * JVC |
| 105 | */ |
| 106 | // From Mark Veinot |
Linus Walleij | 0558ac5 | 2006-09-07 06:55:03 +0000 | [diff] [blame] | 107 | { "JVC Alneo XA-HD500", 0x04f1, 0x6105, DEVICE_FLAG_NONE }, |
Linus Walleij | 3d2c784 | 2006-08-18 09:35:08 +0000 | [diff] [blame] | 108 | |
| 109 | /* |
Linus Walleij | 093ca0a | 2006-11-02 21:15:00 +0000 | [diff] [blame] | 110 | * Philips |
Linus Walleij | 3d2c784 | 2006-08-18 09:35:08 +0000 | [diff] [blame] | 111 | */ |
| 112 | // From libgphoto2 source |
Linus Walleij | 093ca0a | 2006-11-02 21:15:00 +0000 | [diff] [blame] | 113 | { "Philips HDD6320", 0x0471, 0x01eb, DEVICE_FLAG_NONE }, |
Linus Walleij | bf7fc27 | 2006-11-10 21:35:06 +0000 | [diff] [blame^] | 114 | { "Philips HDD6320/00", 0x0471, 0x014b, DEVICE_FLAG_NONE }, |
Linus Walleij | 3d2c784 | 2006-08-18 09:35:08 +0000 | [diff] [blame] | 115 | // Anonymous SourceForge user |
Linus Walleij | 093ca0a | 2006-11-02 21:15:00 +0000 | [diff] [blame] | 116 | { "Philips HDD1630/17", 0x0471, 0x014c, DEVICE_FLAG_NONE }, |
| 117 | // From Gerhard Mekenkamp |
| 118 | { "Philips GoGear Audio", 0x0471, 0x0165, DEVICE_FLAG_NONE }, |
Linus Walleij | 3d2c784 | 2006-08-18 09:35:08 +0000 | [diff] [blame] | 119 | |
| 120 | /* |
| 121 | * SanDisk |
| 122 | */ |
| 123 | // Reported by Brian Robison |
Linus Walleij | 0558ac5 | 2006-09-07 06:55:03 +0000 | [diff] [blame] | 124 | { "SanDisk Sansa m240", 0x0781, 0x7400, DEVICE_FLAG_NONE }, |
Linus Walleij | 3d2c784 | 2006-08-18 09:35:08 +0000 | [diff] [blame] | 125 | // Reported by tangent_@users.sourceforge.net |
Linus Walleij | 0558ac5 | 2006-09-07 06:55:03 +0000 | [diff] [blame] | 126 | { "SanDisk Sansa c150", 0x0781, 0x7410, DEVICE_FLAG_NONE }, |
Linus Walleij | 3d2c784 | 2006-08-18 09:35:08 +0000 | [diff] [blame] | 127 | // From libgphoto2 source |
Linus Walleij | 0558ac5 | 2006-09-07 06:55:03 +0000 | [diff] [blame] | 128 | { "SanDisk Sansa e200", 0x0781, 0x7420, DEVICE_FLAG_NONE }, |
Linus Walleij | 3d2c784 | 2006-08-18 09:35:08 +0000 | [diff] [blame] | 129 | // Reported by gonkflea@users.sourceforge.net |
Linus Walleij | 0558ac5 | 2006-09-07 06:55:03 +0000 | [diff] [blame] | 130 | { "SanDisk Sansa e260", 0x0781, 0x7420, DEVICE_FLAG_NONE }, |
Linus Walleij | 3d2c784 | 2006-08-18 09:35:08 +0000 | [diff] [blame] | 131 | |
| 132 | /* |
| 133 | * iRiver |
| 134 | */ |
Linus Walleij | 0558ac5 | 2006-09-07 06:55:03 +0000 | [diff] [blame] | 135 | { "iRiver Portable Media Center", 0x1006, 0x4002, DEVICE_FLAG_NONE }, |
| 136 | { "iRiver Portable Media Center", 0x1006, 0x4003, DEVICE_FLAG_NONE }, |
Linus Walleij | 3d2c784 | 2006-08-18 09:35:08 +0000 | [diff] [blame] | 137 | // From libgphoto2 source |
Linus Walleij | 0558ac5 | 2006-09-07 06:55:03 +0000 | [diff] [blame] | 138 | { "iRiver T10", 0x4102, 0x1113, DEVICE_FLAG_NONE }, |
| 139 | { "iRiver T20 FM", 0x4102, 0x1114, DEVICE_FLAG_NONE }, |
| 140 | { "iRiver U10", 0x4102, 0x1116, DEVICE_FLAG_NONE }, |
| 141 | { "iRiver T10", 0x4102, 0x1117, DEVICE_FLAG_NONE }, |
| 142 | { "iRiver T20", 0x4102, 0x1118, DEVICE_FLAG_NONE }, |
| 143 | { "iRiver T30", 0x4102, 0x1119, DEVICE_FLAG_NONE }, |
Linus Walleij | 77e047c | 2006-09-11 19:09:06 +0000 | [diff] [blame] | 144 | // Reported by David Wolpoff |
| 145 | { "iRiver T10 2GB", 0x4102, 0x1120, DEVICE_FLAG_NONE }, |
Linus Walleij | 04377da | 2006-08-30 13:03:35 +0000 | [diff] [blame] | 146 | // Reported by Adam Torgerson |
Linus Walleij | 0558ac5 | 2006-09-07 06:55:03 +0000 | [diff] [blame] | 147 | { "iRiver Clix", 0x4102, 0x112a, DEVICE_FLAG_NONE }, |
Linus Walleij | 3d2c784 | 2006-08-18 09:35:08 +0000 | [diff] [blame] | 148 | // Reported by Scott Call |
Linus Walleij | 0558ac5 | 2006-09-07 06:55:03 +0000 | [diff] [blame] | 149 | { "iRiver H10 20GB", 0x4102, 0x2101, DEVICE_FLAG_NONE }, |
| 150 | { "iRiver H10", 0x4102, 0x2102, DEVICE_FLAG_NONE }, |
Linus Walleij | 3d2c784 | 2006-08-18 09:35:08 +0000 | [diff] [blame] | 151 | |
| 152 | /* |
| 153 | * Dell |
| 154 | */ |
Linus Walleij | 0558ac5 | 2006-09-07 06:55:03 +0000 | [diff] [blame] | 155 | { "Dell DJ Itty", 0x413c, 0x4500, DEVICE_FLAG_NONE }, |
Linus Walleij | 3d2c784 | 2006-08-18 09:35:08 +0000 | [diff] [blame] | 156 | |
| 157 | /* |
| 158 | * Toshiba |
| 159 | */ |
Linus Walleij | 0558ac5 | 2006-09-07 06:55:03 +0000 | [diff] [blame] | 160 | { "Toshiba Gigabeat MEGF-40", 0x0930, 0x0009, DEVICE_FLAG_NONE }, |
| 161 | { "Toshiba Gigabeat", 0x0930, 0x000c, DEVICE_FLAG_NONE }, |
Linus Walleij | 405e239 | 2006-10-23 07:13:49 +0000 | [diff] [blame] | 162 | // From libgphoto2 |
| 163 | { "Toshiba Gigabeat S", 0x0930, 0x0010, DEVICE_FLAG_NONE }, |
Linus Walleij | 402e4bd | 2006-09-12 07:50:56 +0000 | [diff] [blame] | 164 | // Reported by Rob Brown |
| 165 | { "Toshiba Gigabeat P10", 0x0930, 0x0011, DEVICE_FLAG_NONE }, |
Linus Walleij | 6f68056 | 2006-08-19 22:49:33 +0000 | [diff] [blame] | 166 | |
| 167 | /* |
| 168 | * Archos |
| 169 | */ |
| 170 | // Reported by gudul1@users.sourceforge.net |
Linus Walleij | e543090 | 2006-10-08 14:46:03 +0000 | [diff] [blame] | 171 | { "Archos 104 (MTP mode)", 0x0e79, 0x120a, DEVICE_FLAG_NONE }, |
| 172 | |
| 173 | /* |
Linus Walleij | 2a9d2f0 | 2006-10-15 20:44:27 +0000 | [diff] [blame] | 174 | * Dunlop (OEM of EGOMAN ltd?) reported by Nanomad |
Linus Walleij | 550bc67 | 2006-10-10 11:11:29 +0000 | [diff] [blame] | 175 | * This unit is falsely detected as USB mass storage in Linux |
| 176 | * so special care is needed to blacklist the device in kernel |
| 177 | * or similar to get it properly userspace:d. |
Linus Walleij | e543090 | 2006-10-08 14:46:03 +0000 | [diff] [blame] | 178 | */ |
Linus Walleij | 501ba4d | 2006-10-16 06:17:04 +0000 | [diff] [blame] | 179 | { "Dunlop MP3 player 1GB / EGOMAN MD223AFD", 0x10d6, 0x2200, DEVICE_FLAG_UNLOAD_DRIVER} |
Linus Walleij | e543090 | 2006-10-08 14:46:03 +0000 | [diff] [blame] | 180 | |
Linus Walleij | a548364 | 2006-03-09 09:20:38 +0000 | [diff] [blame] | 181 | }; |
Linus Walleij | 6fd2f08 | 2006-03-28 07:19:22 +0000 | [diff] [blame] | 182 | 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] | 183 | |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 184 | int ptpcam_usb_timeout = USB_TIMEOUT; |
| 185 | |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 186 | // Local functions |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 187 | static struct usb_bus* init_usb(); |
| 188 | static struct usb_device *probe_usb_bus_for_mtp_devices(void); |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 189 | static void close_usb(PTP_USB* ptp_usb, uint8_t interfaceNumber); |
| 190 | static struct usb_device* find_device (int busn, int devicen, short force); |
| 191 | static void find_endpoints(struct usb_device *dev, int* inep, int* inep_maxpacket, int* outep, int* outep_maxpacket, int* intep); |
| 192 | static void clear_stall(PTP_USB* ptp_usb); |
Linus Walleij | 9eb3d31 | 2006-08-04 19:25:59 +0000 | [diff] [blame] | 193 | 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] | 194 | static short ptp_write_func (unsigned char *bytes, unsigned int size, void *data); |
Linus Walleij | b02a066 | 2006-04-25 08:05:09 +0000 | [diff] [blame] | 195 | static short ptp_read_func (unsigned char *bytes, unsigned int size, void *data, unsigned int *readbytes); |
| 196 | static short ptp_check_int (unsigned char *bytes, unsigned int size, void *data, unsigned int *rlen); |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 197 | static int usb_clear_stall_feature(PTP_USB* ptp_usb, int ep); |
| 198 | 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] | 199 | |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 200 | |
Linus Walleij | 6fd2f08 | 2006-03-28 07:19:22 +0000 | [diff] [blame] | 201 | int get_device_list(LIBMTP_device_entry_t ** const devices, int * const numdevs) |
| 202 | { |
| 203 | *devices = (LIBMTP_device_entry_t *) &mtp_device_table; |
| 204 | *numdevs = mtp_device_table_size; |
| 205 | return 0; |
| 206 | } |
| 207 | |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 208 | static struct usb_bus* init_usb() |
| 209 | { |
| 210 | usb_init(); |
| 211 | usb_find_busses(); |
| 212 | usb_find_devices(); |
| 213 | return (usb_get_busses()); |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Check for the Microsoft OS device descriptor and returns device struct |
| 218 | * if the device is MTP-compliant. The function will only recognize |
| 219 | * a single device connected to the USB bus. |
| 220 | * |
| 221 | * @return an MTP-compliant USB device if one was found, else NULL. |
| 222 | */ |
| 223 | static struct usb_device *probe_usb_bus_for_mtp_devices(void) |
| 224 | { |
| 225 | struct usb_bus *bus; |
| 226 | |
| 227 | bus = init_usb(); |
| 228 | for (; bus; bus = bus->next) { |
| 229 | struct usb_device *dev; |
| 230 | |
| 231 | for (dev = bus->devices; dev; dev = dev->next) { |
| 232 | usb_dev_handle *devh; |
| 233 | unsigned char buf[1024], cmd; |
| 234 | int ret; |
| 235 | |
| 236 | devh = usb_open(dev); |
| 237 | if (devh == NULL) { |
| 238 | continue; |
| 239 | } |
| 240 | |
| 241 | // Read the special descripor, if possible... |
| 242 | ret = usb_get_descriptor(devh, 0x03, 0xee, buf, sizeof(buf)); |
| 243 | |
| 244 | if (ret < 10) { |
| 245 | // printf("Device: VID %04x/PID %04x: no extended device property...\n", |
| 246 | // dev->descriptor.idVendor, |
| 247 | // dev->descriptor.idProduct); |
| 248 | usb_close(devh); |
| 249 | continue; |
| 250 | } |
Linus Walleij | d5d51c8 | 2006-09-11 06:57:50 +0000 | [diff] [blame] | 251 | |
| 252 | // It is atleast 10 bytes... |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 253 | if (!((buf[2] == 'M') && (buf[4]=='S') && (buf[6]=='F') && (buf[8]=='T'))) { |
Linus Walleij | d5d51c8 | 2006-09-11 06:57:50 +0000 | [diff] [blame] | 254 | printf("This is not a Microsoft MTP descriptor...\n"); |
| 255 | printf("Device response to read device property 0xee:\n"); |
| 256 | data_dump_ascii (stdout, buf, ret, 0); |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 257 | usb_close(devh); |
| 258 | continue; |
| 259 | } |
| 260 | |
| 261 | cmd = buf[16]; |
| 262 | ret = usb_control_msg (devh, USB_ENDPOINT_IN|USB_RECIP_DEVICE|USB_TYPE_VENDOR, |
| 263 | cmd, 0, 4, (char *) buf, sizeof(buf), 1000); |
| 264 | if (ret == -1) { |
| 265 | //printf("Decice could not respond to control message 1.\n"); |
| 266 | usb_close(devh); |
Linus Walleij | d5d51c8 | 2006-09-11 06:57:50 +0000 | [diff] [blame] | 267 | continue; |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Linus Walleij | d5d51c8 | 2006-09-11 06:57:50 +0000 | [diff] [blame] | 270 | if (ret > 0x15) { |
| 271 | if ((buf[0x12] != 'M') || (buf[0x13] != 'T') || (buf[0x14] != 'P')) { |
| 272 | printf("The device has a Microsoft device descriptor, but it's not MTP.\n"); |
| 273 | printf("This is not an MTP device. Presumable it is USB mass storage\n"); |
| 274 | printf("with some additional Janus (DRM) support.\n"); |
| 275 | printf("Device response to control message 1:\n"); |
| 276 | data_dump_ascii (stdout, buf, ret, 0); |
| 277 | continue; |
| 278 | } |
| 279 | } else { |
| 280 | // Not MTP or broken |
| 281 | continue; |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | ret = usb_control_msg (devh, USB_ENDPOINT_IN|USB_RECIP_DEVICE|USB_TYPE_VENDOR, |
| 285 | cmd, 0, 5, (char *) buf, sizeof(buf), 1000); |
| 286 | if (ret == -1) { |
| 287 | //printf("Device could not respond to control message 2.\n"); |
| 288 | usb_close(devh); |
Linus Walleij | d5d51c8 | 2006-09-11 06:57:50 +0000 | [diff] [blame] | 289 | // Return the device anyway, it said previously it was MTP, right? |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 290 | return dev; |
| 291 | } |
| 292 | |
Linus Walleij | d5d51c8 | 2006-09-11 06:57:50 +0000 | [diff] [blame] | 293 | if (ret > 0x15) { |
| 294 | if ((buf[0x12] != 'M') || (buf[0x13] != 'T') || (buf[0x14] != 'P')) { |
| 295 | printf("This device does not respond with MTP characteristics on\n"); |
| 296 | printf("the second device property read (0x05), but will be regarded\n"); |
| 297 | printf("as MTP anyway.\n"); |
| 298 | printf("Device response to control message 2:\n"); |
| 299 | data_dump_ascii (stdout, buf, ret, 0); |
| 300 | } |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 301 | } |
Linus Walleij | d5d51c8 | 2006-09-11 06:57:50 +0000 | [diff] [blame] | 302 | |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 303 | usb_close(devh); |
| 304 | // We can return the device here, it will be the first. |
| 305 | // If it was not MTP, the loop continues before it reaches this point. |
| 306 | return dev; |
| 307 | } |
| 308 | } |
| 309 | // If nothing was found we end up here. |
| 310 | return NULL; |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * Detect the MTP device descriptor and return the VID and PID |
| 315 | * of the first device found. This is a very low-level function |
| 316 | * which is intended for use with <b>udev</b> or other hotplug |
| 317 | * mechanisms. The idea is that a script may want to know if the |
| 318 | * just plugged-in device was an MTP device or not. |
| 319 | * @param vid the Vendor ID (VID) of the first device found. |
| 320 | * @param pid the Product ID (PID) of the first device found. |
| 321 | * @return the number of detected devices or -1 if the call |
| 322 | * was unsuccessful. |
| 323 | */ |
| 324 | int LIBMTP_Detect_Descriptor(uint16_t *vid, uint16_t *pid) |
| 325 | { |
| 326 | struct usb_device *dev = probe_usb_bus_for_mtp_devices(); |
| 327 | if (dev == NULL) { |
| 328 | return 0; |
| 329 | } |
| 330 | *vid = dev->descriptor.idVendor; |
| 331 | *pid = dev->descriptor.idProduct; |
| 332 | return 1; |
| 333 | } |
| 334 | |
Linus Walleij | c6210fb | 2006-05-08 11:11:41 +0000 | [diff] [blame] | 335 | /** |
| 336 | * This routine just dumps out low-level |
| 337 | * USB information about the current device. |
| 338 | * @param ptp_usb the USB device to get information from. |
| 339 | */ |
| 340 | void dump_usbinfo(PTP_USB *ptp_usb) |
| 341 | { |
| 342 | int res; |
| 343 | struct usb_device *dev; |
| 344 | |
| 345 | #ifdef LIBUSB_HAS_GET_DRIVER_NP |
| 346 | char devname[0x10]; |
| 347 | |
| 348 | devname[0] = '\0'; |
| 349 | res = usb_get_driver_np(ptp_usb->handle, ptp_usb->interface, devname, sizeof(devname)); |
| 350 | if (devname[0] != '\0') { |
| 351 | printf(" Using kernel interface \"%s\"\n", devname); |
| 352 | } |
| 353 | #endif |
| 354 | dev = usb_device(ptp_usb->handle); |
| 355 | printf(" bcdUSB: %d\n", dev->descriptor.bcdUSB); |
| 356 | printf(" bDeviceClass: %d\n", dev->descriptor.bDeviceClass); |
| 357 | printf(" bDeviceSubClass: %d\n", dev->descriptor.bDeviceSubClass); |
| 358 | printf(" bDeviceProtocol: %d\n", dev->descriptor.bDeviceProtocol); |
Linus Walleij | c6210fb | 2006-05-08 11:11:41 +0000 | [diff] [blame] | 359 | printf(" idVendor: %04x\n", dev->descriptor.idVendor); |
| 360 | printf(" idProduct: %04x\n", dev->descriptor.idProduct); |
Linus Walleij | 7f0c72e | 2006-09-06 13:01:58 +0000 | [diff] [blame] | 361 | printf(" IN endpoint maxpacket: %d bytes\n", ptp_usb->inep_maxpacket); |
| 362 | printf(" OUT endpoint maxpacket: %d bytes\n", ptp_usb->outep_maxpacket); |
Linus Walleij | 0558ac5 | 2006-09-07 06:55:03 +0000 | [diff] [blame] | 363 | printf(" Device flags: 0x%08x\n", ptp_usb->device_flags); |
Linus Walleij | c6210fb | 2006-05-08 11:11:41 +0000 | [diff] [blame] | 364 | // TODO: add in string dumps for iManufacturer, iProduct, iSerialnumber... |
| 365 | } |
| 366 | |
| 367 | |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 368 | // Based on same function on library.c in libgphoto2 |
Richard Low | 43fdb88 | 2006-09-06 16:19:05 +0000 | [diff] [blame] | 369 | #define CONTEXT_BLOCK_SIZE 0x100000 |
Linus Walleij | b02a066 | 2006-04-25 08:05:09 +0000 | [diff] [blame] | 370 | static short |
| 371 | ptp_read_func (unsigned char *bytes, unsigned int size, void *data, unsigned int *readbytes) |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 372 | { |
Linus Walleij | b02a066 | 2006-04-25 08:05:09 +0000 | [diff] [blame] | 373 | PTP_USB *ptp_usb = (PTP_USB *)data; |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 374 | int toread = 0; |
| 375 | int result = 0; |
| 376 | int curread = 0; |
| 377 | /* Split into small blocks. Too large blocks (>1x MB) would |
| 378 | * timeout. |
| 379 | */ |
| 380 | while (curread < size) { |
| 381 | toread = size - curread; |
Richard Low | 43fdb88 | 2006-09-06 16:19:05 +0000 | [diff] [blame] | 382 | if (toread > CONTEXT_BLOCK_SIZE) |
| 383 | toread = CONTEXT_BLOCK_SIZE; |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 384 | |
| 385 | result = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep,(char *)(bytes+curread), toread, ptpcam_usb_timeout); |
| 386 | if (result == 0) { |
| 387 | result = USB_BULK_READ(ptp_usb->handle, ptp_usb->inep,(char *)(bytes+curread), toread, ptpcam_usb_timeout); |
| 388 | } |
| 389 | if (result < 0) |
Richard Low | 43fdb88 | 2006-09-06 16:19:05 +0000 | [diff] [blame] | 390 | return PTP_ERROR_IO; |
Linus Walleij | e78de6d | 2006-10-31 14:46:36 +0000 | [diff] [blame] | 391 | #ifdef ENABLE_USB_BULK_DEBUG |
| 392 | printf("<==USB IN\n"); |
Linus Walleij | 42907f2 | 2006-11-07 16:00:39 +0000 | [diff] [blame] | 393 | data_dump_ascii (stdout,(bytes+curread),result,16); |
Linus Walleij | e78de6d | 2006-10-31 14:46:36 +0000 | [diff] [blame] | 394 | #endif |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 395 | curread += result; |
| 396 | if (result < toread) /* short reads are common */ |
| 397 | break; |
| 398 | } |
Linus Walleij | ee73ef2 | 2006-08-27 19:56:00 +0000 | [diff] [blame] | 399 | |
| 400 | // Increase counters, call callback |
| 401 | if (ptp_usb->callback_active) { |
| 402 | ptp_usb->current_transfer_complete += curread; |
Linus Walleij | 7f0c72e | 2006-09-06 13:01:58 +0000 | [diff] [blame] | 403 | if (ptp_usb->current_transfer_complete > ptp_usb->current_transfer_total) { |
| 404 | // Fishy... but some commands have unpredictable lengths. |
| 405 | // send last update and disable callback. |
| 406 | ptp_usb->current_transfer_complete = ptp_usb->current_transfer_total; |
| 407 | ptp_usb->callback_active = 0; |
| 408 | } |
Linus Walleij | ee73ef2 | 2006-08-27 19:56:00 +0000 | [diff] [blame] | 409 | if (ptp_usb->current_transfer_callback != NULL) { |
| 410 | (void) ptp_usb->current_transfer_callback(ptp_usb->current_transfer_complete, |
| 411 | ptp_usb->current_transfer_total, |
| 412 | ptp_usb->current_transfer_callback_data); |
| 413 | } |
| 414 | } |
| 415 | |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 416 | if (result > 0) { |
| 417 | *readbytes = curread; |
| 418 | return (PTP_RC_OK); |
| 419 | } else { |
| 420 | return PTP_ERROR_IO; |
| 421 | } |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 422 | } |
| 423 | |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 424 | // Based on same function on library.c in libgphoto2 |
Linus Walleij | b02a066 | 2006-04-25 08:05:09 +0000 | [diff] [blame] | 425 | static short |
| 426 | ptp_write_func (unsigned char *bytes, unsigned int size, void *data) |
| 427 | { |
| 428 | PTP_USB *ptp_usb = (PTP_USB *)data; |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 429 | int towrite = 0; |
| 430 | int result = 0; |
| 431 | int curwrite = 0; |
Linus Walleij | 7f0c72e | 2006-09-06 13:01:58 +0000 | [diff] [blame] | 432 | |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 433 | /* |
| 434 | * gp_port_write returns (in case of success) the number of bytes |
Linus Walleij | d214b9b | 2006-08-26 22:08:37 +0000 | [diff] [blame] | 435 | * written. Too large blocks (>5x MB) could timeout. |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 436 | */ |
| 437 | while (curwrite < size) { |
| 438 | towrite = size-curwrite; |
Richard Low | 43fdb88 | 2006-09-06 16:19:05 +0000 | [diff] [blame] | 439 | if (towrite > CONTEXT_BLOCK_SIZE) |
| 440 | towrite = CONTEXT_BLOCK_SIZE; |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 441 | result=USB_BULK_WRITE(ptp_usb->handle,ptp_usb->outep,(char *)(bytes+curwrite),towrite,ptpcam_usb_timeout); |
Linus Walleij | e78de6d | 2006-10-31 14:46:36 +0000 | [diff] [blame] | 442 | #ifdef ENABLE_USB_BULK_DEBUG |
| 443 | printf("USB OUT==>\n"); |
| 444 | data_dump_ascii (stdout,(bytes+curwrite),towrite,16); |
| 445 | #endif |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 446 | if (result < 0) |
Richard Low | 43fdb88 | 2006-09-06 16:19:05 +0000 | [diff] [blame] | 447 | return PTP_ERROR_IO; |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 448 | curwrite += result; |
| 449 | if (result < towrite) /* short writes happen */ |
| 450 | break; |
| 451 | } |
Linus Walleij | d214b9b | 2006-08-26 22:08:37 +0000 | [diff] [blame] | 452 | |
| 453 | // Increase counters, call callback |
| 454 | if (ptp_usb->callback_active) { |
Linus Walleij | 4af6fbe | 2006-08-27 09:33:52 +0000 | [diff] [blame] | 455 | ptp_usb->current_transfer_complete += curwrite; |
Linus Walleij | 7f0c72e | 2006-09-06 13:01:58 +0000 | [diff] [blame] | 456 | if (ptp_usb->current_transfer_complete > ptp_usb->current_transfer_total) { |
| 457 | // Fishy... but some commands have unpredictable lengths. |
| 458 | // send last update and disable callback. |
| 459 | ptp_usb->current_transfer_complete = ptp_usb->current_transfer_total; |
| 460 | ptp_usb->callback_active = 0; |
| 461 | } |
Linus Walleij | d214b9b | 2006-08-26 22:08:37 +0000 | [diff] [blame] | 462 | if (ptp_usb->current_transfer_callback != NULL) { |
| 463 | (void) ptp_usb->current_transfer_callback(ptp_usb->current_transfer_complete, |
| 464 | ptp_usb->current_transfer_total, |
| 465 | ptp_usb->current_transfer_callback_data); |
| 466 | } |
Linus Walleij | 80d134a | 2006-08-22 21:41:37 +0000 | [diff] [blame] | 467 | } |
Linus Walleij | d214b9b | 2006-08-26 22:08:37 +0000 | [diff] [blame] | 468 | |
| 469 | // If this is the last transfer (callbacks only active if this function called repeatedly with |
| 470 | // new data, otherwise this is a single large transaction which ends here). |
Linus Walleij | 7f0c72e | 2006-09-06 13:01:58 +0000 | [diff] [blame] | 471 | // a request and the bulk send header. |
| 472 | if (!ptp_usb->callback_active) { |
Linus Walleij | d214b9b | 2006-08-26 22:08:37 +0000 | [diff] [blame] | 473 | // Then terminate an even packet boundary write with a zero length packet |
Linus Walleij | 7f0c72e | 2006-09-06 13:01:58 +0000 | [diff] [blame] | 474 | if ((size % ptp_usb->outep_maxpacket) == 0) { |
Linus Walleij | d214b9b | 2006-08-26 22:08:37 +0000 | [diff] [blame] | 475 | result=USB_BULK_WRITE(ptp_usb->handle,ptp_usb->outep,(char *)"x",0,ptpcam_usb_timeout); |
| 476 | } |
Linus Walleij | 7f0c72e | 2006-09-06 13:01:58 +0000 | [diff] [blame] | 477 | } else if (ptp_usb->current_transfer_complete == ptp_usb->current_transfer_total) { |
| 478 | // This is the amount actually transfered in this large transaction. |
| 479 | uint64_t actual_xfer_size = ptp_usb->current_transfer_total - 2*PTP_USB_BULK_HDR_LEN; |
| 480 | |
| 481 | if ((actual_xfer_size % ptp_usb->outep_maxpacket) == 0) { |
| 482 | result=USB_BULK_WRITE(ptp_usb->handle,ptp_usb->outep,(char *)"x",0,ptpcam_usb_timeout); |
Linus Walleij | 7f0c72e | 2006-09-06 13:01:58 +0000 | [diff] [blame] | 483 | } |
| 484 | // Set as complete and disable callback, just as good. |
| 485 | // This also blocks callbacks from the following response command. |
| 486 | if (ptp_usb->current_transfer_callback != NULL) { |
| 487 | (void) ptp_usb->current_transfer_callback(ptp_usb->current_transfer_total, // Both total = 100% |
| 488 | ptp_usb->current_transfer_total, // Both total = 100% |
| 489 | ptp_usb->current_transfer_callback_data); |
| 490 | } |
| 491 | ptp_usb->callback_active = 0; |
Linus Walleij | d214b9b | 2006-08-26 22:08:37 +0000 | [diff] [blame] | 492 | } |
| 493 | |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 494 | if (result < 0) |
| 495 | return PTP_ERROR_IO; |
| 496 | return PTP_RC_OK; |
Linus Walleij | b02a066 | 2006-04-25 08:05:09 +0000 | [diff] [blame] | 497 | } |
| 498 | |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 499 | // This is a bit hackish at the moment. I wonder if it even works. |
Linus Walleij | b02a066 | 2006-04-25 08:05:09 +0000 | [diff] [blame] | 500 | static short ptp_check_int (unsigned char *bytes, unsigned int size, void *data, unsigned int *rlen) |
| 501 | { |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 502 | PTP_USB *ptp_usb = (PTP_USB *)data; |
Linus Walleij | b02a066 | 2006-04-25 08:05:09 +0000 | [diff] [blame] | 503 | int result; |
Linus Walleij | b02a066 | 2006-04-25 08:05:09 +0000 | [diff] [blame] | 504 | |
| 505 | result=USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *)bytes,size,ptpcam_usb_timeout); |
| 506 | if (result==0) |
| 507 | result = USB_BULK_READ(ptp_usb->handle, ptp_usb->intep,(char *) bytes, size, ptpcam_usb_timeout); |
| 508 | if (result >= 0) { |
| 509 | *rlen = result; |
| 510 | return (PTP_RC_OK); |
| 511 | } else { |
| 512 | return PTP_ERROR_IO; |
| 513 | } |
| 514 | } |
| 515 | |
Linus Walleij | 9eb3d31 | 2006-08-04 19:25:59 +0000 | [diff] [blame] | 516 | 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] | 517 | { |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 518 | usb_dev_handle *device_handle; |
| 519 | |
| 520 | params->write_func=ptp_write_func; |
| 521 | params->read_func=ptp_read_func; |
| 522 | params->check_int_func=ptp_check_int; |
| 523 | params->check_int_fast_func=ptp_check_int; |
| 524 | params->error_func=NULL; |
| 525 | params->debug_func=NULL; |
| 526 | params->sendreq_func=ptp_usb_sendreq; |
| 527 | params->senddata_func=ptp_usb_senddata; |
| 528 | params->getresp_func=ptp_usb_getresp; |
| 529 | params->getdata_func=ptp_usb_getdata; |
| 530 | params->data=ptp_usb; |
| 531 | params->transaction_id=0; |
| 532 | params->byteorder = PTP_DL_LE; |
| 533 | |
Linus Walleij | 0ed2a9f | 2006-05-04 05:31:34 +0000 | [diff] [blame] | 534 | if ((device_handle = usb_open(dev))){ |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 535 | if (!device_handle) { |
| 536 | perror("usb_open()"); |
Linus Walleij | 9eb3d31 | 2006-08-04 19:25:59 +0000 | [diff] [blame] | 537 | return -1; |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 538 | } |
Linus Walleij | 0ed2a9f | 2006-05-04 05:31:34 +0000 | [diff] [blame] | 539 | ptp_usb->handle = device_handle; |
Linus Walleij | 501ba4d | 2006-10-16 06:17:04 +0000 | [diff] [blame] | 540 | #ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP |
| 541 | /* |
| 542 | * If this device is known to be wrongfully claimed by other kernel |
| 543 | * drivers (such as mass storage), then try to unload it to make it |
| 544 | * accessible from user space. |
| 545 | */ |
| 546 | if (ptp_usb->device_flags & DEVICE_FLAG_UNLOAD_DRIVER) { |
| 547 | if (usb_detach_kernel_driver_np(device_handle, dev->config->interface->altsetting->bInterfaceNumber)) { |
| 548 | perror("usb_detach_kernel_driver_np()"); |
| 549 | } |
| 550 | } |
| 551 | #endif |
Linus Walleij | ef5cca9 | 2006-05-05 07:19:08 +0000 | [diff] [blame] | 552 | if (usb_claim_interface(device_handle, dev->config->interface->altsetting->bInterfaceNumber)) { |
Linus Walleij | 0ed2a9f | 2006-05-04 05:31:34 +0000 | [diff] [blame] | 553 | perror("usb_claim_interface()"); |
Linus Walleij | 9eb3d31 | 2006-08-04 19:25:59 +0000 | [diff] [blame] | 554 | return -1; |
Linus Walleij | 0ed2a9f | 2006-05-04 05:31:34 +0000 | [diff] [blame] | 555 | } |
Linus Walleij | c6210fb | 2006-05-08 11:11:41 +0000 | [diff] [blame] | 556 | ptp_usb->interface = dev->config->interface->altsetting->bInterfaceNumber; |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 557 | } |
Linus Walleij | 9eb3d31 | 2006-08-04 19:25:59 +0000 | [diff] [blame] | 558 | return 0; |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 559 | } |
| 560 | |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 561 | static void clear_stall(PTP_USB* ptp_usb) |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 562 | { |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 563 | uint16_t status=0; |
| 564 | int ret; |
| 565 | |
| 566 | /* check the inep status */ |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 567 | ret = usb_get_endpoint_status(ptp_usb,ptp_usb->inep,&status); |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 568 | if (ret<0) perror ("inep: usb_get_endpoint_status()"); |
| 569 | /* and clear the HALT condition if happend */ |
| 570 | else if (status) { |
| 571 | printf("Resetting input pipe!\n"); |
| 572 | ret=usb_clear_stall_feature(ptp_usb,ptp_usb->inep); |
| 573 | /*usb_clear_halt(ptp_usb->handle,ptp_usb->inep); */ |
| 574 | if (ret<0)perror ("usb_clear_stall_feature()"); |
| 575 | } |
| 576 | status=0; |
| 577 | |
| 578 | /* check the outep status */ |
| 579 | ret=usb_get_endpoint_status(ptp_usb,ptp_usb->outep,&status); |
| 580 | if (ret<0) perror ("outep: usb_get_endpoint_status()"); |
| 581 | /* and clear the HALT condition if happend */ |
| 582 | else if (status) { |
| 583 | printf("Resetting output pipe!\n"); |
| 584 | ret=usb_clear_stall_feature(ptp_usb,ptp_usb->outep); |
| 585 | /*usb_clear_halt(ptp_usb->handle,ptp_usb->outep); */ |
| 586 | if (ret<0)perror ("usb_clear_stall_feature()"); |
| 587 | } |
| 588 | |
| 589 | /*usb_clear_halt(ptp_usb->handle,ptp_usb->intep); */ |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 590 | } |
| 591 | |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 592 | static void close_usb(PTP_USB* ptp_usb, uint8_t interfaceNumber) |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 593 | { |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 594 | clear_stall(ptp_usb); |
| 595 | // Added to clear some stuff on the OUT endpoint |
| 596 | // TODO: is this good on the Mac too? |
| 597 | usb_resetep(ptp_usb->handle, ptp_usb->outep); |
| 598 | usb_release_interface(ptp_usb->handle, interfaceNumber); |
| 599 | // Brutally reset device |
| 600 | // TODO: is this good on the Mac too? |
| 601 | usb_reset(ptp_usb->handle); |
| 602 | usb_close(ptp_usb->handle); |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 606 | /* |
| 607 | find_device() returns the pointer to a usb_device structure matching |
| 608 | given busn, devicen numbers. If any or both of arguments are 0 then the |
| 609 | first matching PTP device structure is returned. |
| 610 | */ |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 611 | static struct usb_device* find_device (int busn, int devn, short force) |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 612 | { |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 613 | struct usb_bus *bus; |
| 614 | struct usb_device *dev; |
| 615 | |
| 616 | bus=init_usb(); |
| 617 | for (; bus; bus = bus->next) |
| 618 | for (dev = bus->devices; dev; dev = dev->next) |
| 619 | /* somtimes dev->config is null, not sure why... */ |
| 620 | if (dev->config != NULL) |
| 621 | if (dev->descriptor.bDeviceClass!=USB_CLASS_HUB) |
| 622 | { |
| 623 | int curbusn, curdevn; |
| 624 | |
| 625 | curbusn=strtol(bus->dirname,NULL,10); |
| 626 | curdevn=strtol(dev->filename,NULL,10); |
| 627 | |
| 628 | if (devn==0) { |
| 629 | if (busn==0) return dev; |
| 630 | if (curbusn==busn) return dev; |
| 631 | } else { |
| 632 | if ((busn==0)&&(curdevn==devn)) return dev; |
| 633 | if ((curbusn==busn)&&(curdevn==devn)) return dev; |
| 634 | } |
| 635 | } |
| 636 | return NULL; |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 637 | } |
| 638 | |
Linus Walleij | 6946ac5 | 2006-03-21 06:51:22 +0000 | [diff] [blame] | 639 | /** |
| 640 | * This function scans through the device list to see if there are |
| 641 | * some devices to connect to. The table at the top of this file is |
| 642 | * used to identify potential devices. |
| 643 | */ |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 644 | uint16_t connect_first_device(PTPParams *params, PTP_USB *ptp_usb, uint8_t *interfaceNumber) |
| 645 | { |
Linus Walleij | 6946ac5 | 2006-03-21 06:51:22 +0000 | [diff] [blame] | 646 | struct usb_bus *bus; |
| 647 | struct usb_device *dev; |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 648 | struct usb_endpoint_descriptor *ep; |
| 649 | PTPDeviceInfo deviceinfo; |
| 650 | uint16_t ret=0; |
| 651 | int n; |
Linus Walleij | 6946ac5 | 2006-03-21 06:51:22 +0000 | [diff] [blame] | 652 | |
Linus Walleij | 0558ac5 | 2006-09-07 06:55:03 +0000 | [diff] [blame] | 653 | // Reset device flags |
| 654 | ptp_usb->device_flags = DEVICE_FLAG_NONE; |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 655 | // First try to locate the device using the extended |
| 656 | // device descriptor. |
| 657 | dev = probe_usb_bus_for_mtp_devices(); |
| 658 | |
| 659 | if (dev != NULL) { |
| 660 | int i; |
| 661 | |
| 662 | // See if we can find the name of this beast |
| 663 | for (i = 0; i < mtp_device_table_size; i++) { |
| 664 | LIBMTP_device_entry_t const *mtp_device = &mtp_device_table[i]; |
| 665 | if (dev->descriptor.idVendor == mtp_device->vendor_id && |
| 666 | dev->descriptor.idProduct == mtp_device->product_id ) { |
| 667 | printf("Autodetected device \"%s\" (VID=%04x,PID=%04x) is known.\n", |
| 668 | mtp_device->name, dev->descriptor.idVendor, dev->descriptor.idProduct); |
Linus Walleij | 0558ac5 | 2006-09-07 06:55:03 +0000 | [diff] [blame] | 669 | ptp_usb->device_flags = mtp_device->device_flags; |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 670 | break; |
| 671 | } |
| 672 | } |
| 673 | if (i == mtp_device_table_size) { |
| 674 | printf("Autodetected device with VID=%04x and PID=%04x is UNKNOWN.\n", |
| 675 | dev->descriptor.idVendor, dev->descriptor.idProduct); |
| 676 | printf("Please report this VID/PID and the device model name etc to the\n"); |
| 677 | printf("libmtp development team!\n"); |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | // If autodetection fails, scan the bus for well known devices. |
| 682 | if (dev == NULL) { |
| 683 | bus = init_usb(); |
| 684 | for (; bus; bus = bus->next) { |
| 685 | for (dev = bus->devices; dev; dev = dev->next) { |
| 686 | int i; |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 687 | |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 688 | // Loop over the list of supported devices |
| 689 | for (i = 0; i < mtp_device_table_size; i++) { |
| 690 | LIBMTP_device_entry_t const *mtp_device = &mtp_device_table[i]; |
Linus Walleij | 6946ac5 | 2006-03-21 06:51:22 +0000 | [diff] [blame] | 691 | |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 692 | if (dev->descriptor.bDeviceClass != USB_CLASS_HUB && |
| 693 | dev->descriptor.idVendor == mtp_device->vendor_id && |
| 694 | dev->descriptor.idProduct == mtp_device->product_id ) { |
| 695 | |
| 696 | printf("Found non-autodetected device \"%s\" on USB bus...\n", mtp_device->name); |
Linus Walleij | 0558ac5 | 2006-09-07 06:55:03 +0000 | [diff] [blame] | 697 | ptp_usb->device_flags = mtp_device->device_flags; |
Linus Walleij | d3fdd97 | 2006-05-30 15:51:37 +0000 | [diff] [blame] | 698 | goto next_step; |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 699 | |
Linus Walleij | b02a066 | 2006-04-25 08:05:09 +0000 | [diff] [blame] | 700 | } |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 701 | } |
Linus Walleij | 6946ac5 | 2006-03-21 06:51:22 +0000 | [diff] [blame] | 702 | } |
| 703 | } |
| 704 | } |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 705 | |
Linus Walleij | d3fdd97 | 2006-05-30 15:51:37 +0000 | [diff] [blame] | 706 | next_step: |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 707 | // Still not found any? |
| 708 | if (dev == NULL) { |
| 709 | return PTP_CD_RC_NO_DEVICES; |
| 710 | } |
| 711 | |
Linus Walleij | 0558ac5 | 2006-09-07 06:55:03 +0000 | [diff] [blame] | 712 | // Found a device, then assign endpoints to ptp_usb... |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 713 | ep = dev->config->interface->altsetting->endpoint; |
| 714 | n = dev->config->interface->altsetting->bNumEndpoints; |
Linus Walleij | 7f0c72e | 2006-09-06 13:01:58 +0000 | [diff] [blame] | 715 | find_endpoints(dev, &ptp_usb->inep, &ptp_usb->inep_maxpacket, |
| 716 | &ptp_usb->outep, &ptp_usb->outep_maxpacket, &ptp_usb->intep); |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 717 | |
| 718 | // printf("Init PTP USB...\n"); |
Linus Walleij | 9eb3d31 | 2006-08-04 19:25:59 +0000 | [diff] [blame] | 719 | if (init_ptp_usb(params, ptp_usb, dev) < 0) { |
| 720 | return PTP_CD_RC_ERROR_CONNECTING; |
| 721 | } |
Linus Walleij | 1fd2d27 | 2006-05-08 09:22:01 +0000 | [diff] [blame] | 722 | |
| 723 | ret = ptp_opensession(params,1); |
| 724 | // printf("Session open (%d)...\n", ret); |
| 725 | if (ret == PTP_RC_InvalidTransactionID) { |
| 726 | params->transaction_id += 10; |
| 727 | ret = ptp_opensession(params,1); |
| 728 | } |
| 729 | if (ret != PTP_RC_SessionAlreadyOpened && ret != PTP_RC_OK) { |
| 730 | printf("Could not open session! (Return code %d)\n Try to reset the device.\n", ret); |
| 731 | usb_release_interface(ptp_usb->handle,dev->config->interface->altsetting->bInterfaceNumber); |
| 732 | } |
| 733 | |
| 734 | // It is actually permissible to call this before opening the session |
| 735 | ret = ptp_getdeviceinfo(params, &deviceinfo); |
| 736 | if (ret != PTP_RC_OK) { |
| 737 | printf("Could not get device info!\n"); |
| 738 | usb_release_interface(ptp_usb->handle,dev->config->interface->altsetting->bInterfaceNumber); |
| 739 | return PTP_CD_RC_ERROR_CONNECTING; |
| 740 | } |
| 741 | |
| 742 | // we're connected, return OK |
| 743 | *interfaceNumber = dev->config->interface->altsetting->bInterfaceNumber; |
| 744 | return PTP_CD_RC_CONNECTED; |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 745 | } |
| 746 | |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 747 | static void find_endpoints(struct usb_device *dev, int* inep, int* inep_maxpacket, int* outep, int *outep_maxpacket, int* intep) |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 748 | { |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 749 | int i,n; |
| 750 | struct usb_endpoint_descriptor *ep; |
| 751 | |
| 752 | ep = dev->config->interface->altsetting->endpoint; |
| 753 | n=dev->config->interface->altsetting->bNumEndpoints; |
| 754 | |
| 755 | for (i=0;i<n;i++) { |
| 756 | if (ep[i].bmAttributes==USB_ENDPOINT_TYPE_BULK) { |
| 757 | if ((ep[i].bEndpointAddress&USB_ENDPOINT_DIR_MASK)== |
| 758 | USB_ENDPOINT_DIR_MASK) |
| 759 | { |
| 760 | *inep=ep[i].bEndpointAddress; |
| 761 | *inep_maxpacket=ep[i].wMaxPacketSize; |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 762 | } |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 763 | if ((ep[i].bEndpointAddress&USB_ENDPOINT_DIR_MASK)==0) |
| 764 | { |
| 765 | *outep=ep[i].bEndpointAddress; |
Linus Walleij | 7f0c72e | 2006-09-06 13:01:58 +0000 | [diff] [blame] | 766 | *outep_maxpacket=ep[i].wMaxPacketSize; |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 767 | } |
| 768 | } else if (ep[i].bmAttributes==USB_ENDPOINT_TYPE_INTERRUPT){ |
| 769 | if ((ep[i].bEndpointAddress&USB_ENDPOINT_DIR_MASK)== |
| 770 | USB_ENDPOINT_DIR_MASK) |
| 771 | { |
| 772 | *intep=ep[i].bEndpointAddress; |
| 773 | } |
| 774 | } |
| 775 | } |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 776 | } |
| 777 | |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 778 | int open_device (int busn, int devn, short force, PTP_USB *ptp_usb, PTPParams *params, struct usb_device **dev) |
| 779 | { |
| 780 | #ifdef DEBUG |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 781 | printf("dev %i\tbus %i\n",devn,busn); |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 782 | #endif |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 783 | |
| 784 | *dev=find_device(busn,devn,force); |
| 785 | if (*dev==NULL) { |
| 786 | fprintf(stderr,"could not find any device matching given " |
| 787 | "bus/dev numbers\n"); |
| 788 | exit(-1); |
| 789 | } |
| 790 | find_endpoints(*dev,&ptp_usb->inep,&ptp_usb->inep_maxpacket,&ptp_usb->outep,&ptp_usb->outep_maxpacket,&ptp_usb->intep); |
| 791 | |
Linus Walleij | 9eb3d31 | 2006-08-04 19:25:59 +0000 | [diff] [blame] | 792 | if (init_ptp_usb(params, ptp_usb, *dev) < 0) { |
| 793 | return -1; |
| 794 | } |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 795 | if (ptp_opensession(params,1)!=PTP_RC_OK) { |
| 796 | fprintf(stderr,"ERROR: Could not open session!\n"); |
| 797 | close_usb(ptp_usb, (*dev)->config->interface->altsetting->bInterfaceNumber); |
| 798 | return -1; |
| 799 | } |
| 800 | return 0; |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | void close_device (PTP_USB *ptp_usb, PTPParams *params, uint8_t interfaceNumber) |
| 804 | { |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 805 | if (ptp_closesession(params)!=PTP_RC_OK) |
| 806 | fprintf(stderr,"ERROR: Could not close session!\n"); |
| 807 | close_usb(ptp_usb, interfaceNumber); |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 808 | } |
| 809 | |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 810 | static int usb_clear_stall_feature(PTP_USB* ptp_usb, int ep) |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 811 | { |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 812 | |
| 813 | return (usb_control_msg(ptp_usb->handle, |
| 814 | USB_RECIP_ENDPOINT, USB_REQ_CLEAR_FEATURE, USB_FEATURE_HALT, |
| 815 | ep, NULL, 0, 3000)); |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 816 | } |
| 817 | |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 818 | 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] | 819 | { |
Linus Walleij | d6a4997 | 2006-05-02 08:24:54 +0000 | [diff] [blame] | 820 | return (usb_control_msg(ptp_usb->handle, |
| 821 | USB_DP_DTH|USB_RECIP_ENDPOINT, USB_REQ_GET_STATUS, |
| 822 | USB_FEATURE_HALT, ep, (char *)status, 2, 3000)); |
Linus Walleij | eb8c6fe | 2006-02-03 09:46:22 +0000 | [diff] [blame] | 823 | } |